intel_sprite.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * Copyright © 2011 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Jesse Barnes <jbarnes@virtuousgeek.org>
  25. *
  26. * New plane/sprite handling.
  27. *
  28. * The older chips had a separate interface for programming plane related
  29. * registers; newer ones are much simpler and we can use the new DRM plane
  30. * support.
  31. */
  32. #include <drm/drmP.h>
  33. #include <drm/drm_atomic_helper.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_fourcc.h>
  36. #include <drm/drm_rect.h>
  37. #include <drm/drm_atomic.h>
  38. #include <drm/drm_plane_helper.h>
  39. #include "intel_drv.h"
  40. #include "intel_frontbuffer.h"
  41. #include <drm/i915_drm.h>
  42. #include "i915_drv.h"
  43. bool intel_format_is_yuv(u32 format)
  44. {
  45. switch (format) {
  46. case DRM_FORMAT_YUYV:
  47. case DRM_FORMAT_UYVY:
  48. case DRM_FORMAT_VYUY:
  49. case DRM_FORMAT_YVYU:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  56. int usecs)
  57. {
  58. /* paranoia */
  59. if (!adjusted_mode->crtc_htotal)
  60. return 1;
  61. return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
  62. 1000 * adjusted_mode->crtc_htotal);
  63. }
  64. /* FIXME: We should instead only take spinlocks once for the entire update
  65. * instead of once per mmio. */
  66. #if IS_ENABLED(CONFIG_PROVE_LOCKING)
  67. #define VBLANK_EVASION_TIME_US 250
  68. #else
  69. #define VBLANK_EVASION_TIME_US 100
  70. #endif
  71. /**
  72. * intel_pipe_update_start() - start update of a set of display registers
  73. * @new_crtc_state: the new crtc state
  74. *
  75. * Mark the start of an update to pipe registers that should be updated
  76. * atomically regarding vblank. If the next vblank will happens within
  77. * the next 100 us, this function waits until the vblank passes.
  78. *
  79. * After a successful call to this function, interrupts will be disabled
  80. * until a subsequent call to intel_pipe_update_end(). That is done to
  81. * avoid random delays.
  82. */
  83. void intel_pipe_update_start(const struct intel_crtc_state *new_crtc_state)
  84. {
  85. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  86. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  87. const struct drm_display_mode *adjusted_mode = &new_crtc_state->base.adjusted_mode;
  88. long timeout = msecs_to_jiffies_timeout(1);
  89. int scanline, min, max, vblank_start;
  90. wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
  91. bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  92. intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
  93. DEFINE_WAIT(wait);
  94. vblank_start = adjusted_mode->crtc_vblank_start;
  95. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  96. vblank_start = DIV_ROUND_UP(vblank_start, 2);
  97. /* FIXME needs to be calibrated sensibly */
  98. min = vblank_start - intel_usecs_to_scanlines(adjusted_mode,
  99. VBLANK_EVASION_TIME_US);
  100. max = vblank_start - 1;
  101. local_irq_disable();
  102. if (min <= 0 || max <= 0)
  103. return;
  104. if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
  105. return;
  106. crtc->debug.min_vbl = min;
  107. crtc->debug.max_vbl = max;
  108. trace_i915_pipe_update_start(crtc);
  109. for (;;) {
  110. /*
  111. * prepare_to_wait() has a memory barrier, which guarantees
  112. * other CPUs can see the task state update by the time we
  113. * read the scanline.
  114. */
  115. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  116. scanline = intel_get_crtc_scanline(crtc);
  117. if (scanline < min || scanline > max)
  118. break;
  119. if (timeout <= 0) {
  120. DRM_ERROR("Potential atomic update failure on pipe %c\n",
  121. pipe_name(crtc->pipe));
  122. break;
  123. }
  124. local_irq_enable();
  125. timeout = schedule_timeout(timeout);
  126. local_irq_disable();
  127. }
  128. finish_wait(wq, &wait);
  129. drm_crtc_vblank_put(&crtc->base);
  130. /*
  131. * On VLV/CHV DSI the scanline counter would appear to
  132. * increment approx. 1/3 of a scanline before start of vblank.
  133. * The registers still get latched at start of vblank however.
  134. * This means we must not write any registers on the first
  135. * line of vblank (since not the whole line is actually in
  136. * vblank). And unfortunately we can't use the interrupt to
  137. * wait here since it will fire too soon. We could use the
  138. * frame start interrupt instead since it will fire after the
  139. * critical scanline, but that would require more changes
  140. * in the interrupt code. So for now we'll just do the nasty
  141. * thing and poll for the bad scanline to pass us by.
  142. *
  143. * FIXME figure out if BXT+ DSI suffers from this as well
  144. */
  145. while (need_vlv_dsi_wa && scanline == vblank_start)
  146. scanline = intel_get_crtc_scanline(crtc);
  147. crtc->debug.scanline_start = scanline;
  148. crtc->debug.start_vbl_time = ktime_get();
  149. crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
  150. trace_i915_pipe_update_vblank_evaded(crtc);
  151. }
  152. /**
  153. * intel_pipe_update_end() - end update of a set of display registers
  154. * @new_crtc_state: the new crtc state
  155. *
  156. * Mark the end of an update started with intel_pipe_update_start(). This
  157. * re-enables interrupts and verifies the update was actually completed
  158. * before a vblank.
  159. */
  160. void intel_pipe_update_end(struct intel_crtc_state *new_crtc_state)
  161. {
  162. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  163. enum pipe pipe = crtc->pipe;
  164. int scanline_end = intel_get_crtc_scanline(crtc);
  165. u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
  166. ktime_t end_vbl_time = ktime_get();
  167. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  168. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  169. /* We're still in the vblank-evade critical section, this can't race.
  170. * Would be slightly nice to just grab the vblank count and arm the
  171. * event outside of the critical section - the spinlock might spin for a
  172. * while ... */
  173. if (new_crtc_state->base.event) {
  174. WARN_ON(drm_crtc_vblank_get(&crtc->base) != 0);
  175. spin_lock(&crtc->base.dev->event_lock);
  176. drm_crtc_arm_vblank_event(&crtc->base, new_crtc_state->base.event);
  177. spin_unlock(&crtc->base.dev->event_lock);
  178. new_crtc_state->base.event = NULL;
  179. }
  180. local_irq_enable();
  181. if (intel_vgpu_active(dev_priv))
  182. return;
  183. if (crtc->debug.start_vbl_count &&
  184. crtc->debug.start_vbl_count != end_vbl_count) {
  185. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
  186. pipe_name(pipe), crtc->debug.start_vbl_count,
  187. end_vbl_count,
  188. ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
  189. crtc->debug.min_vbl, crtc->debug.max_vbl,
  190. crtc->debug.scanline_start, scanline_end);
  191. }
  192. #ifdef CONFIG_DRM_I915_DEBUG_VBLANK_EVADE
  193. else if (ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time) >
  194. VBLANK_EVASION_TIME_US)
  195. DRM_WARN("Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
  196. pipe_name(pipe),
  197. ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
  198. VBLANK_EVASION_TIME_US);
  199. #endif
  200. }
  201. void
  202. skl_update_plane(struct intel_plane *plane,
  203. const struct intel_crtc_state *crtc_state,
  204. const struct intel_plane_state *plane_state)
  205. {
  206. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  207. const struct drm_framebuffer *fb = plane_state->base.fb;
  208. enum plane_id plane_id = plane->id;
  209. enum pipe pipe = plane->pipe;
  210. u32 plane_ctl = plane_state->ctl;
  211. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  212. u32 surf_addr = plane_state->main.offset;
  213. unsigned int rotation = plane_state->base.rotation;
  214. u32 stride = skl_plane_stride(fb, 0, rotation);
  215. u32 aux_stride = skl_plane_stride(fb, 1, rotation);
  216. int crtc_x = plane_state->base.dst.x1;
  217. int crtc_y = plane_state->base.dst.y1;
  218. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  219. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  220. uint32_t x = plane_state->main.x;
  221. uint32_t y = plane_state->main.y;
  222. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  223. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  224. unsigned long irqflags;
  225. /* Sizes are 0 based */
  226. src_w--;
  227. src_h--;
  228. crtc_w--;
  229. crtc_h--;
  230. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  231. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  232. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  233. plane_state->color_ctl);
  234. if (key->flags) {
  235. I915_WRITE_FW(PLANE_KEYVAL(pipe, plane_id), key->min_value);
  236. I915_WRITE_FW(PLANE_KEYMAX(pipe, plane_id), key->max_value);
  237. I915_WRITE_FW(PLANE_KEYMSK(pipe, plane_id), key->channel_mask);
  238. }
  239. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (y << 16) | x);
  240. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  241. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  242. I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
  243. (plane_state->aux.offset - surf_addr) | aux_stride);
  244. I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
  245. (plane_state->aux.y << 16) | plane_state->aux.x);
  246. /* program plane scaler */
  247. if (plane_state->scaler_id >= 0) {
  248. int scaler_id = plane_state->scaler_id;
  249. const struct intel_scaler *scaler;
  250. scaler = &crtc_state->scaler_state.scalers[scaler_id];
  251. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id),
  252. PS_SCALER_EN | PS_PLANE_SEL(plane_id) | scaler->mode);
  253. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  254. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
  255. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id),
  256. ((crtc_w + 1) << 16)|(crtc_h + 1));
  257. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  258. } else {
  259. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (crtc_y << 16) | crtc_x);
  260. }
  261. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  262. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  263. intel_plane_ggtt_offset(plane_state) + surf_addr);
  264. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  265. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  266. }
  267. void
  268. skl_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  269. {
  270. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  271. enum plane_id plane_id = plane->id;
  272. enum pipe pipe = plane->pipe;
  273. unsigned long irqflags;
  274. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  275. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  276. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  277. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  278. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  279. }
  280. bool
  281. skl_plane_get_hw_state(struct intel_plane *plane)
  282. {
  283. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  284. enum intel_display_power_domain power_domain;
  285. enum plane_id plane_id = plane->id;
  286. enum pipe pipe = plane->pipe;
  287. bool ret;
  288. power_domain = POWER_DOMAIN_PIPE(pipe);
  289. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  290. return false;
  291. ret = I915_READ(PLANE_CTL(pipe, plane_id)) & PLANE_CTL_ENABLE;
  292. intel_display_power_put(dev_priv, power_domain);
  293. return ret;
  294. }
  295. static void
  296. chv_update_csc(const struct intel_plane_state *plane_state)
  297. {
  298. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  299. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  300. const struct drm_framebuffer *fb = plane_state->base.fb;
  301. enum plane_id plane_id = plane->id;
  302. /*
  303. * |r| | c0 c1 c2 | |cr|
  304. * |g| = | c3 c4 c5 | x |y |
  305. * |b| | c6 c7 c8 | |cb|
  306. *
  307. * Coefficients are s3.12.
  308. *
  309. * Cb and Cr apparently come in as signed already, and
  310. * we always get full range data in on account of CLRC0/1.
  311. */
  312. static const s16 csc_matrix[][9] = {
  313. /* BT.601 full range YCbCr -> full range RGB */
  314. [DRM_COLOR_YCBCR_BT601] = {
  315. 5743, 4096, 0,
  316. -2925, 4096, -1410,
  317. 0, 4096, 7258,
  318. },
  319. /* BT.709 full range YCbCr -> full range RGB */
  320. [DRM_COLOR_YCBCR_BT709] = {
  321. 6450, 4096, 0,
  322. -1917, 4096, -767,
  323. 0, 4096, 7601,
  324. },
  325. };
  326. const s16 *csc = csc_matrix[plane_state->base.color_encoding];
  327. /* Seems RGB data bypasses the CSC always */
  328. if (!intel_format_is_yuv(fb->format->format))
  329. return;
  330. I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  331. I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  332. I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  333. I915_WRITE_FW(SPCSCC01(plane_id), SPCSC_C1(csc[1]) | SPCSC_C0(csc[0]));
  334. I915_WRITE_FW(SPCSCC23(plane_id), SPCSC_C1(csc[3]) | SPCSC_C0(csc[2]));
  335. I915_WRITE_FW(SPCSCC45(plane_id), SPCSC_C1(csc[5]) | SPCSC_C0(csc[4]));
  336. I915_WRITE_FW(SPCSCC67(plane_id), SPCSC_C1(csc[7]) | SPCSC_C0(csc[6]));
  337. I915_WRITE_FW(SPCSCC8(plane_id), SPCSC_C0(csc[8]));
  338. I915_WRITE_FW(SPCSCYGICLAMP(plane_id), SPCSC_IMAX(1023) | SPCSC_IMIN(0));
  339. I915_WRITE_FW(SPCSCCBICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
  340. I915_WRITE_FW(SPCSCCRICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
  341. I915_WRITE_FW(SPCSCYGOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  342. I915_WRITE_FW(SPCSCCBOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  343. I915_WRITE_FW(SPCSCCROCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  344. }
  345. #define SIN_0 0
  346. #define COS_0 1
  347. static void
  348. vlv_update_clrc(const struct intel_plane_state *plane_state)
  349. {
  350. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  351. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  352. const struct drm_framebuffer *fb = plane_state->base.fb;
  353. enum pipe pipe = plane->pipe;
  354. enum plane_id plane_id = plane->id;
  355. int contrast, brightness, sh_scale, sh_sin, sh_cos;
  356. if (intel_format_is_yuv(fb->format->format) &&
  357. plane_state->base.color_range == DRM_COLOR_YCBCR_LIMITED_RANGE) {
  358. /*
  359. * Expand limited range to full range:
  360. * Contrast is applied first and is used to expand Y range.
  361. * Brightness is applied second and is used to remove the
  362. * offset from Y. Saturation/hue is used to expand CbCr range.
  363. */
  364. contrast = DIV_ROUND_CLOSEST(255 << 6, 235 - 16);
  365. brightness = -DIV_ROUND_CLOSEST(16 * 255, 235 - 16);
  366. sh_scale = DIV_ROUND_CLOSEST(128 << 7, 240 - 128);
  367. sh_sin = SIN_0 * sh_scale;
  368. sh_cos = COS_0 * sh_scale;
  369. } else {
  370. /* Pass-through everything. */
  371. contrast = 1 << 6;
  372. brightness = 0;
  373. sh_scale = 1 << 7;
  374. sh_sin = SIN_0 * sh_scale;
  375. sh_cos = COS_0 * sh_scale;
  376. }
  377. /* FIXME these register are single buffered :( */
  378. I915_WRITE_FW(SPCLRC0(pipe, plane_id),
  379. SP_CONTRAST(contrast) | SP_BRIGHTNESS(brightness));
  380. I915_WRITE_FW(SPCLRC1(pipe, plane_id),
  381. SP_SH_SIN(sh_sin) | SP_SH_COS(sh_cos));
  382. }
  383. static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
  384. const struct intel_plane_state *plane_state)
  385. {
  386. const struct drm_framebuffer *fb = plane_state->base.fb;
  387. unsigned int rotation = plane_state->base.rotation;
  388. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  389. u32 sprctl;
  390. sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
  391. switch (fb->format->format) {
  392. case DRM_FORMAT_YUYV:
  393. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  394. break;
  395. case DRM_FORMAT_YVYU:
  396. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  397. break;
  398. case DRM_FORMAT_UYVY:
  399. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  400. break;
  401. case DRM_FORMAT_VYUY:
  402. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  403. break;
  404. case DRM_FORMAT_RGB565:
  405. sprctl |= SP_FORMAT_BGR565;
  406. break;
  407. case DRM_FORMAT_XRGB8888:
  408. sprctl |= SP_FORMAT_BGRX8888;
  409. break;
  410. case DRM_FORMAT_ARGB8888:
  411. sprctl |= SP_FORMAT_BGRA8888;
  412. break;
  413. case DRM_FORMAT_XBGR2101010:
  414. sprctl |= SP_FORMAT_RGBX1010102;
  415. break;
  416. case DRM_FORMAT_ABGR2101010:
  417. sprctl |= SP_FORMAT_RGBA1010102;
  418. break;
  419. case DRM_FORMAT_XBGR8888:
  420. sprctl |= SP_FORMAT_RGBX8888;
  421. break;
  422. case DRM_FORMAT_ABGR8888:
  423. sprctl |= SP_FORMAT_RGBA8888;
  424. break;
  425. default:
  426. MISSING_CASE(fb->format->format);
  427. return 0;
  428. }
  429. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  430. sprctl |= SP_YUV_FORMAT_BT709;
  431. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  432. sprctl |= SP_TILED;
  433. if (rotation & DRM_MODE_ROTATE_180)
  434. sprctl |= SP_ROTATE_180;
  435. if (rotation & DRM_MODE_REFLECT_X)
  436. sprctl |= SP_MIRROR;
  437. if (key->flags & I915_SET_COLORKEY_SOURCE)
  438. sprctl |= SP_SOURCE_KEY;
  439. return sprctl;
  440. }
  441. static void
  442. vlv_update_plane(struct intel_plane *plane,
  443. const struct intel_crtc_state *crtc_state,
  444. const struct intel_plane_state *plane_state)
  445. {
  446. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  447. const struct drm_framebuffer *fb = plane_state->base.fb;
  448. enum pipe pipe = plane->pipe;
  449. enum plane_id plane_id = plane->id;
  450. u32 sprctl = plane_state->ctl;
  451. u32 sprsurf_offset = plane_state->main.offset;
  452. u32 linear_offset;
  453. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  454. int crtc_x = plane_state->base.dst.x1;
  455. int crtc_y = plane_state->base.dst.y1;
  456. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  457. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  458. uint32_t x = plane_state->main.x;
  459. uint32_t y = plane_state->main.y;
  460. unsigned long irqflags;
  461. /* Sizes are 0 based */
  462. crtc_w--;
  463. crtc_h--;
  464. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  465. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  466. vlv_update_clrc(plane_state);
  467. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
  468. chv_update_csc(plane_state);
  469. if (key->flags) {
  470. I915_WRITE_FW(SPKEYMINVAL(pipe, plane_id), key->min_value);
  471. I915_WRITE_FW(SPKEYMAXVAL(pipe, plane_id), key->max_value);
  472. I915_WRITE_FW(SPKEYMSK(pipe, plane_id), key->channel_mask);
  473. }
  474. I915_WRITE_FW(SPSTRIDE(pipe, plane_id), fb->pitches[0]);
  475. I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
  476. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  477. I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
  478. else
  479. I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
  480. I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
  481. I915_WRITE_FW(SPSIZE(pipe, plane_id), (crtc_h << 16) | crtc_w);
  482. I915_WRITE_FW(SPCNTR(pipe, plane_id), sprctl);
  483. I915_WRITE_FW(SPSURF(pipe, plane_id),
  484. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  485. POSTING_READ_FW(SPSURF(pipe, plane_id));
  486. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  487. }
  488. static void
  489. vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  490. {
  491. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  492. enum pipe pipe = plane->pipe;
  493. enum plane_id plane_id = plane->id;
  494. unsigned long irqflags;
  495. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  496. I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
  497. I915_WRITE_FW(SPSURF(pipe, plane_id), 0);
  498. POSTING_READ_FW(SPSURF(pipe, plane_id));
  499. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  500. }
  501. static bool
  502. vlv_plane_get_hw_state(struct intel_plane *plane)
  503. {
  504. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  505. enum intel_display_power_domain power_domain;
  506. enum plane_id plane_id = plane->id;
  507. enum pipe pipe = plane->pipe;
  508. bool ret;
  509. power_domain = POWER_DOMAIN_PIPE(pipe);
  510. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  511. return false;
  512. ret = I915_READ(SPCNTR(pipe, plane_id)) & SP_ENABLE;
  513. intel_display_power_put(dev_priv, power_domain);
  514. return ret;
  515. }
  516. static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
  517. const struct intel_plane_state *plane_state)
  518. {
  519. struct drm_i915_private *dev_priv =
  520. to_i915(plane_state->base.plane->dev);
  521. const struct drm_framebuffer *fb = plane_state->base.fb;
  522. unsigned int rotation = plane_state->base.rotation;
  523. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  524. u32 sprctl;
  525. sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
  526. if (IS_IVYBRIDGE(dev_priv))
  527. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  528. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  529. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  530. switch (fb->format->format) {
  531. case DRM_FORMAT_XBGR8888:
  532. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  533. break;
  534. case DRM_FORMAT_XRGB8888:
  535. sprctl |= SPRITE_FORMAT_RGBX888;
  536. break;
  537. case DRM_FORMAT_YUYV:
  538. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  539. break;
  540. case DRM_FORMAT_YVYU:
  541. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  542. break;
  543. case DRM_FORMAT_UYVY:
  544. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  545. break;
  546. case DRM_FORMAT_VYUY:
  547. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  548. break;
  549. default:
  550. MISSING_CASE(fb->format->format);
  551. return 0;
  552. }
  553. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  554. sprctl |= SPRITE_YUV_TO_RGB_CSC_FORMAT_BT709;
  555. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  556. sprctl |= SPRITE_YUV_RANGE_CORRECTION_DISABLE;
  557. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  558. sprctl |= SPRITE_TILED;
  559. if (rotation & DRM_MODE_ROTATE_180)
  560. sprctl |= SPRITE_ROTATE_180;
  561. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  562. sprctl |= SPRITE_DEST_KEY;
  563. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  564. sprctl |= SPRITE_SOURCE_KEY;
  565. return sprctl;
  566. }
  567. static void
  568. ivb_update_plane(struct intel_plane *plane,
  569. const struct intel_crtc_state *crtc_state,
  570. const struct intel_plane_state *plane_state)
  571. {
  572. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  573. const struct drm_framebuffer *fb = plane_state->base.fb;
  574. enum pipe pipe = plane->pipe;
  575. u32 sprctl = plane_state->ctl, sprscale = 0;
  576. u32 sprsurf_offset = plane_state->main.offset;
  577. u32 linear_offset;
  578. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  579. int crtc_x = plane_state->base.dst.x1;
  580. int crtc_y = plane_state->base.dst.y1;
  581. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  582. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  583. uint32_t x = plane_state->main.x;
  584. uint32_t y = plane_state->main.y;
  585. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  586. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  587. unsigned long irqflags;
  588. /* Sizes are 0 based */
  589. src_w--;
  590. src_h--;
  591. crtc_w--;
  592. crtc_h--;
  593. if (crtc_w != src_w || crtc_h != src_h)
  594. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  595. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  596. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  597. if (key->flags) {
  598. I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
  599. I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
  600. I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
  601. }
  602. I915_WRITE_FW(SPRSTRIDE(pipe), fb->pitches[0]);
  603. I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  604. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  605. * register */
  606. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  607. I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
  608. else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  609. I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
  610. else
  611. I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
  612. I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  613. if (plane->can_scale)
  614. I915_WRITE_FW(SPRSCALE(pipe), sprscale);
  615. I915_WRITE_FW(SPRCTL(pipe), sprctl);
  616. I915_WRITE_FW(SPRSURF(pipe),
  617. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  618. POSTING_READ_FW(SPRSURF(pipe));
  619. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  620. }
  621. static void
  622. ivb_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  623. {
  624. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  625. enum pipe pipe = plane->pipe;
  626. unsigned long irqflags;
  627. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  628. I915_WRITE_FW(SPRCTL(pipe), 0);
  629. /* Can't leave the scaler enabled... */
  630. if (plane->can_scale)
  631. I915_WRITE_FW(SPRSCALE(pipe), 0);
  632. I915_WRITE_FW(SPRSURF(pipe), 0);
  633. POSTING_READ_FW(SPRSURF(pipe));
  634. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  635. }
  636. static bool
  637. ivb_plane_get_hw_state(struct intel_plane *plane)
  638. {
  639. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  640. enum intel_display_power_domain power_domain;
  641. enum pipe pipe = plane->pipe;
  642. bool ret;
  643. power_domain = POWER_DOMAIN_PIPE(pipe);
  644. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  645. return false;
  646. ret = I915_READ(SPRCTL(pipe)) & SPRITE_ENABLE;
  647. intel_display_power_put(dev_priv, power_domain);
  648. return ret;
  649. }
  650. static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
  651. const struct intel_plane_state *plane_state)
  652. {
  653. struct drm_i915_private *dev_priv =
  654. to_i915(plane_state->base.plane->dev);
  655. const struct drm_framebuffer *fb = plane_state->base.fb;
  656. unsigned int rotation = plane_state->base.rotation;
  657. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  658. u32 dvscntr;
  659. dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
  660. if (IS_GEN6(dev_priv))
  661. dvscntr |= DVS_TRICKLE_FEED_DISABLE;
  662. switch (fb->format->format) {
  663. case DRM_FORMAT_XBGR8888:
  664. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  665. break;
  666. case DRM_FORMAT_XRGB8888:
  667. dvscntr |= DVS_FORMAT_RGBX888;
  668. break;
  669. case DRM_FORMAT_YUYV:
  670. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  671. break;
  672. case DRM_FORMAT_YVYU:
  673. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  674. break;
  675. case DRM_FORMAT_UYVY:
  676. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  677. break;
  678. case DRM_FORMAT_VYUY:
  679. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  680. break;
  681. default:
  682. MISSING_CASE(fb->format->format);
  683. return 0;
  684. }
  685. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  686. dvscntr |= DVS_YUV_FORMAT_BT709;
  687. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  688. dvscntr |= DVS_YUV_RANGE_CORRECTION_DISABLE;
  689. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  690. dvscntr |= DVS_TILED;
  691. if (rotation & DRM_MODE_ROTATE_180)
  692. dvscntr |= DVS_ROTATE_180;
  693. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  694. dvscntr |= DVS_DEST_KEY;
  695. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  696. dvscntr |= DVS_SOURCE_KEY;
  697. return dvscntr;
  698. }
  699. static void
  700. g4x_update_plane(struct intel_plane *plane,
  701. const struct intel_crtc_state *crtc_state,
  702. const struct intel_plane_state *plane_state)
  703. {
  704. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  705. const struct drm_framebuffer *fb = plane_state->base.fb;
  706. enum pipe pipe = plane->pipe;
  707. u32 dvscntr = plane_state->ctl, dvsscale = 0;
  708. u32 dvssurf_offset = plane_state->main.offset;
  709. u32 linear_offset;
  710. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  711. int crtc_x = plane_state->base.dst.x1;
  712. int crtc_y = plane_state->base.dst.y1;
  713. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  714. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  715. uint32_t x = plane_state->main.x;
  716. uint32_t y = plane_state->main.y;
  717. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  718. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  719. unsigned long irqflags;
  720. /* Sizes are 0 based */
  721. src_w--;
  722. src_h--;
  723. crtc_w--;
  724. crtc_h--;
  725. if (crtc_w != src_w || crtc_h != src_h)
  726. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  727. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  728. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  729. if (key->flags) {
  730. I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
  731. I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
  732. I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
  733. }
  734. I915_WRITE_FW(DVSSTRIDE(pipe), fb->pitches[0]);
  735. I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  736. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  737. I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
  738. else
  739. I915_WRITE_FW(DVSLINOFF(pipe), linear_offset);
  740. I915_WRITE_FW(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  741. I915_WRITE_FW(DVSSCALE(pipe), dvsscale);
  742. I915_WRITE_FW(DVSCNTR(pipe), dvscntr);
  743. I915_WRITE_FW(DVSSURF(pipe),
  744. intel_plane_ggtt_offset(plane_state) + dvssurf_offset);
  745. POSTING_READ_FW(DVSSURF(pipe));
  746. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  747. }
  748. static void
  749. g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  750. {
  751. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  752. enum pipe pipe = plane->pipe;
  753. unsigned long irqflags;
  754. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  755. I915_WRITE_FW(DVSCNTR(pipe), 0);
  756. /* Disable the scaler */
  757. I915_WRITE_FW(DVSSCALE(pipe), 0);
  758. I915_WRITE_FW(DVSSURF(pipe), 0);
  759. POSTING_READ_FW(DVSSURF(pipe));
  760. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  761. }
  762. static bool
  763. g4x_plane_get_hw_state(struct intel_plane *plane)
  764. {
  765. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  766. enum intel_display_power_domain power_domain;
  767. enum pipe pipe = plane->pipe;
  768. bool ret;
  769. power_domain = POWER_DOMAIN_PIPE(pipe);
  770. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  771. return false;
  772. ret = I915_READ(DVSCNTR(pipe)) & DVS_ENABLE;
  773. intel_display_power_put(dev_priv, power_domain);
  774. return ret;
  775. }
  776. static int
  777. intel_check_sprite_plane(struct intel_plane *plane,
  778. struct intel_crtc_state *crtc_state,
  779. struct intel_plane_state *state)
  780. {
  781. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  782. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  783. struct drm_framebuffer *fb = state->base.fb;
  784. int crtc_x, crtc_y;
  785. unsigned int crtc_w, crtc_h;
  786. uint32_t src_x, src_y, src_w, src_h;
  787. struct drm_rect *src = &state->base.src;
  788. struct drm_rect *dst = &state->base.dst;
  789. struct drm_rect clip = {};
  790. int max_stride = INTEL_GEN(dev_priv) >= 9 ? 32768 : 16384;
  791. int hscale, vscale;
  792. int max_scale, min_scale;
  793. bool can_scale;
  794. int ret;
  795. *src = drm_plane_state_src(&state->base);
  796. *dst = drm_plane_state_dest(&state->base);
  797. if (!fb) {
  798. state->base.visible = false;
  799. return 0;
  800. }
  801. /* Don't modify another pipe's plane */
  802. if (plane->pipe != crtc->pipe) {
  803. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  804. return -EINVAL;
  805. }
  806. /* FIXME check all gen limits */
  807. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > max_stride) {
  808. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  809. return -EINVAL;
  810. }
  811. /* setup can_scale, min_scale, max_scale */
  812. if (INTEL_GEN(dev_priv) >= 9) {
  813. /* use scaler when colorkey is not required */
  814. if (!state->ckey.flags) {
  815. can_scale = 1;
  816. min_scale = 1;
  817. max_scale = skl_max_scale(crtc, crtc_state);
  818. } else {
  819. can_scale = 0;
  820. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  821. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  822. }
  823. } else {
  824. can_scale = plane->can_scale;
  825. max_scale = plane->max_downscale << 16;
  826. min_scale = plane->can_scale ? 1 : (1 << 16);
  827. }
  828. /*
  829. * FIXME the following code does a bunch of fuzzy adjustments to the
  830. * coordinates and sizes. We probably need some way to decide whether
  831. * more strict checking should be done instead.
  832. */
  833. drm_rect_rotate(src, fb->width << 16, fb->height << 16,
  834. state->base.rotation);
  835. hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
  836. BUG_ON(hscale < 0);
  837. vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
  838. BUG_ON(vscale < 0);
  839. if (crtc_state->base.enable)
  840. drm_mode_get_hv_timing(&crtc_state->base.mode,
  841. &clip.x2, &clip.y2);
  842. state->base.visible = drm_rect_clip_scaled(src, dst, &clip, hscale, vscale);
  843. crtc_x = dst->x1;
  844. crtc_y = dst->y1;
  845. crtc_w = drm_rect_width(dst);
  846. crtc_h = drm_rect_height(dst);
  847. if (state->base.visible) {
  848. /* check again in case clipping clamped the results */
  849. hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
  850. if (hscale < 0) {
  851. DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
  852. drm_rect_debug_print("src: ", src, true);
  853. drm_rect_debug_print("dst: ", dst, false);
  854. return hscale;
  855. }
  856. vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
  857. if (vscale < 0) {
  858. DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
  859. drm_rect_debug_print("src: ", src, true);
  860. drm_rect_debug_print("dst: ", dst, false);
  861. return vscale;
  862. }
  863. /* Make the source viewport size an exact multiple of the scaling factors. */
  864. drm_rect_adjust_size(src,
  865. drm_rect_width(dst) * hscale - drm_rect_width(src),
  866. drm_rect_height(dst) * vscale - drm_rect_height(src));
  867. drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
  868. state->base.rotation);
  869. /* sanity check to make sure the src viewport wasn't enlarged */
  870. WARN_ON(src->x1 < (int) state->base.src_x ||
  871. src->y1 < (int) state->base.src_y ||
  872. src->x2 > (int) state->base.src_x + state->base.src_w ||
  873. src->y2 > (int) state->base.src_y + state->base.src_h);
  874. /*
  875. * Hardware doesn't handle subpixel coordinates.
  876. * Adjust to (macro)pixel boundary, but be careful not to
  877. * increase the source viewport size, because that could
  878. * push the downscaling factor out of bounds.
  879. */
  880. src_x = src->x1 >> 16;
  881. src_w = drm_rect_width(src) >> 16;
  882. src_y = src->y1 >> 16;
  883. src_h = drm_rect_height(src) >> 16;
  884. if (intel_format_is_yuv(fb->format->format)) {
  885. src_x &= ~1;
  886. src_w &= ~1;
  887. /*
  888. * Must keep src and dst the
  889. * same if we can't scale.
  890. */
  891. if (!can_scale)
  892. crtc_w &= ~1;
  893. if (crtc_w == 0)
  894. state->base.visible = false;
  895. }
  896. }
  897. /* Check size restrictions when scaling */
  898. if (state->base.visible && (src_w != crtc_w || src_h != crtc_h)) {
  899. unsigned int width_bytes;
  900. int cpp = fb->format->cpp[0];
  901. WARN_ON(!can_scale);
  902. /* FIXME interlacing min height is 6 */
  903. if (crtc_w < 3 || crtc_h < 3)
  904. state->base.visible = false;
  905. if (src_w < 3 || src_h < 3)
  906. state->base.visible = false;
  907. width_bytes = ((src_x * cpp) & 63) + src_w * cpp;
  908. if (INTEL_GEN(dev_priv) < 9 && (src_w > 2048 || src_h > 2048 ||
  909. width_bytes > 4096 || fb->pitches[0] > 4096)) {
  910. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  911. return -EINVAL;
  912. }
  913. }
  914. if (state->base.visible) {
  915. src->x1 = src_x << 16;
  916. src->x2 = (src_x + src_w) << 16;
  917. src->y1 = src_y << 16;
  918. src->y2 = (src_y + src_h) << 16;
  919. }
  920. dst->x1 = crtc_x;
  921. dst->x2 = crtc_x + crtc_w;
  922. dst->y1 = crtc_y;
  923. dst->y2 = crtc_y + crtc_h;
  924. if (INTEL_GEN(dev_priv) >= 9) {
  925. ret = skl_check_plane_surface(crtc_state, state);
  926. if (ret)
  927. return ret;
  928. state->ctl = skl_plane_ctl(crtc_state, state);
  929. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  930. ret = i9xx_check_plane_surface(state);
  931. if (ret)
  932. return ret;
  933. state->ctl = vlv_sprite_ctl(crtc_state, state);
  934. } else if (INTEL_GEN(dev_priv) >= 7) {
  935. ret = i9xx_check_plane_surface(state);
  936. if (ret)
  937. return ret;
  938. state->ctl = ivb_sprite_ctl(crtc_state, state);
  939. } else {
  940. ret = i9xx_check_plane_surface(state);
  941. if (ret)
  942. return ret;
  943. state->ctl = g4x_sprite_ctl(crtc_state, state);
  944. }
  945. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  946. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  947. return 0;
  948. }
  949. int intel_sprite_set_colorkey_ioctl(struct drm_device *dev, void *data,
  950. struct drm_file *file_priv)
  951. {
  952. struct drm_i915_private *dev_priv = to_i915(dev);
  953. struct drm_intel_sprite_colorkey *set = data;
  954. struct drm_plane *plane;
  955. struct drm_plane_state *plane_state;
  956. struct drm_atomic_state *state;
  957. struct drm_modeset_acquire_ctx ctx;
  958. int ret = 0;
  959. /* ignore the pointless "none" flag */
  960. set->flags &= ~I915_SET_COLORKEY_NONE;
  961. if (set->flags & ~(I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  962. return -EINVAL;
  963. /* Make sure we don't try to enable both src & dest simultaneously */
  964. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  965. return -EINVAL;
  966. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  967. set->flags & I915_SET_COLORKEY_DESTINATION)
  968. return -EINVAL;
  969. plane = drm_plane_find(dev, file_priv, set->plane_id);
  970. if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
  971. return -ENOENT;
  972. drm_modeset_acquire_init(&ctx, 0);
  973. state = drm_atomic_state_alloc(plane->dev);
  974. if (!state) {
  975. ret = -ENOMEM;
  976. goto out;
  977. }
  978. state->acquire_ctx = &ctx;
  979. while (1) {
  980. plane_state = drm_atomic_get_plane_state(state, plane);
  981. ret = PTR_ERR_OR_ZERO(plane_state);
  982. if (!ret) {
  983. to_intel_plane_state(plane_state)->ckey = *set;
  984. ret = drm_atomic_commit(state);
  985. }
  986. if (ret != -EDEADLK)
  987. break;
  988. drm_atomic_state_clear(state);
  989. drm_modeset_backoff(&ctx);
  990. }
  991. drm_atomic_state_put(state);
  992. out:
  993. drm_modeset_drop_locks(&ctx);
  994. drm_modeset_acquire_fini(&ctx);
  995. return ret;
  996. }
  997. static const uint32_t g4x_plane_formats[] = {
  998. DRM_FORMAT_XRGB8888,
  999. DRM_FORMAT_YUYV,
  1000. DRM_FORMAT_YVYU,
  1001. DRM_FORMAT_UYVY,
  1002. DRM_FORMAT_VYUY,
  1003. };
  1004. static const uint64_t i9xx_plane_format_modifiers[] = {
  1005. I915_FORMAT_MOD_X_TILED,
  1006. DRM_FORMAT_MOD_LINEAR,
  1007. DRM_FORMAT_MOD_INVALID
  1008. };
  1009. static const uint32_t snb_plane_formats[] = {
  1010. DRM_FORMAT_XBGR8888,
  1011. DRM_FORMAT_XRGB8888,
  1012. DRM_FORMAT_YUYV,
  1013. DRM_FORMAT_YVYU,
  1014. DRM_FORMAT_UYVY,
  1015. DRM_FORMAT_VYUY,
  1016. };
  1017. static const uint32_t vlv_plane_formats[] = {
  1018. DRM_FORMAT_RGB565,
  1019. DRM_FORMAT_ABGR8888,
  1020. DRM_FORMAT_ARGB8888,
  1021. DRM_FORMAT_XBGR8888,
  1022. DRM_FORMAT_XRGB8888,
  1023. DRM_FORMAT_XBGR2101010,
  1024. DRM_FORMAT_ABGR2101010,
  1025. DRM_FORMAT_YUYV,
  1026. DRM_FORMAT_YVYU,
  1027. DRM_FORMAT_UYVY,
  1028. DRM_FORMAT_VYUY,
  1029. };
  1030. static uint32_t skl_plane_formats[] = {
  1031. DRM_FORMAT_RGB565,
  1032. DRM_FORMAT_ABGR8888,
  1033. DRM_FORMAT_ARGB8888,
  1034. DRM_FORMAT_XBGR8888,
  1035. DRM_FORMAT_XRGB8888,
  1036. DRM_FORMAT_YUYV,
  1037. DRM_FORMAT_YVYU,
  1038. DRM_FORMAT_UYVY,
  1039. DRM_FORMAT_VYUY,
  1040. };
  1041. static const uint64_t skl_plane_format_modifiers_noccs[] = {
  1042. I915_FORMAT_MOD_Yf_TILED,
  1043. I915_FORMAT_MOD_Y_TILED,
  1044. I915_FORMAT_MOD_X_TILED,
  1045. DRM_FORMAT_MOD_LINEAR,
  1046. DRM_FORMAT_MOD_INVALID
  1047. };
  1048. static const uint64_t skl_plane_format_modifiers_ccs[] = {
  1049. I915_FORMAT_MOD_Yf_TILED_CCS,
  1050. I915_FORMAT_MOD_Y_TILED_CCS,
  1051. I915_FORMAT_MOD_Yf_TILED,
  1052. I915_FORMAT_MOD_Y_TILED,
  1053. I915_FORMAT_MOD_X_TILED,
  1054. DRM_FORMAT_MOD_LINEAR,
  1055. DRM_FORMAT_MOD_INVALID
  1056. };
  1057. static bool g4x_mod_supported(uint32_t format, uint64_t modifier)
  1058. {
  1059. switch (format) {
  1060. case DRM_FORMAT_XRGB8888:
  1061. case DRM_FORMAT_YUYV:
  1062. case DRM_FORMAT_YVYU:
  1063. case DRM_FORMAT_UYVY:
  1064. case DRM_FORMAT_VYUY:
  1065. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1066. modifier == I915_FORMAT_MOD_X_TILED)
  1067. return true;
  1068. /* fall through */
  1069. default:
  1070. return false;
  1071. }
  1072. }
  1073. static bool snb_mod_supported(uint32_t format, uint64_t modifier)
  1074. {
  1075. switch (format) {
  1076. case DRM_FORMAT_XRGB8888:
  1077. case DRM_FORMAT_XBGR8888:
  1078. case DRM_FORMAT_YUYV:
  1079. case DRM_FORMAT_YVYU:
  1080. case DRM_FORMAT_UYVY:
  1081. case DRM_FORMAT_VYUY:
  1082. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1083. modifier == I915_FORMAT_MOD_X_TILED)
  1084. return true;
  1085. /* fall through */
  1086. default:
  1087. return false;
  1088. }
  1089. }
  1090. static bool vlv_mod_supported(uint32_t format, uint64_t modifier)
  1091. {
  1092. switch (format) {
  1093. case DRM_FORMAT_RGB565:
  1094. case DRM_FORMAT_ABGR8888:
  1095. case DRM_FORMAT_ARGB8888:
  1096. case DRM_FORMAT_XBGR8888:
  1097. case DRM_FORMAT_XRGB8888:
  1098. case DRM_FORMAT_XBGR2101010:
  1099. case DRM_FORMAT_ABGR2101010:
  1100. case DRM_FORMAT_YUYV:
  1101. case DRM_FORMAT_YVYU:
  1102. case DRM_FORMAT_UYVY:
  1103. case DRM_FORMAT_VYUY:
  1104. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1105. modifier == I915_FORMAT_MOD_X_TILED)
  1106. return true;
  1107. /* fall through */
  1108. default:
  1109. return false;
  1110. }
  1111. }
  1112. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  1113. {
  1114. switch (format) {
  1115. case DRM_FORMAT_XRGB8888:
  1116. case DRM_FORMAT_XBGR8888:
  1117. case DRM_FORMAT_ARGB8888:
  1118. case DRM_FORMAT_ABGR8888:
  1119. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  1120. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  1121. return true;
  1122. /* fall through */
  1123. case DRM_FORMAT_RGB565:
  1124. case DRM_FORMAT_XRGB2101010:
  1125. case DRM_FORMAT_XBGR2101010:
  1126. case DRM_FORMAT_YUYV:
  1127. case DRM_FORMAT_YVYU:
  1128. case DRM_FORMAT_UYVY:
  1129. case DRM_FORMAT_VYUY:
  1130. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  1131. return true;
  1132. /* fall through */
  1133. case DRM_FORMAT_C8:
  1134. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1135. modifier == I915_FORMAT_MOD_X_TILED ||
  1136. modifier == I915_FORMAT_MOD_Y_TILED)
  1137. return true;
  1138. /* fall through */
  1139. default:
  1140. return false;
  1141. }
  1142. }
  1143. static bool intel_sprite_plane_format_mod_supported(struct drm_plane *plane,
  1144. uint32_t format,
  1145. uint64_t modifier)
  1146. {
  1147. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  1148. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  1149. return false;
  1150. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  1151. modifier != DRM_FORMAT_MOD_LINEAR)
  1152. return false;
  1153. if (INTEL_GEN(dev_priv) >= 9)
  1154. return skl_mod_supported(format, modifier);
  1155. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1156. return vlv_mod_supported(format, modifier);
  1157. else if (INTEL_GEN(dev_priv) >= 6)
  1158. return snb_mod_supported(format, modifier);
  1159. else
  1160. return g4x_mod_supported(format, modifier);
  1161. }
  1162. static const struct drm_plane_funcs intel_sprite_plane_funcs = {
  1163. .update_plane = drm_atomic_helper_update_plane,
  1164. .disable_plane = drm_atomic_helper_disable_plane,
  1165. .destroy = intel_plane_destroy,
  1166. .atomic_get_property = intel_plane_atomic_get_property,
  1167. .atomic_set_property = intel_plane_atomic_set_property,
  1168. .atomic_duplicate_state = intel_plane_duplicate_state,
  1169. .atomic_destroy_state = intel_plane_destroy_state,
  1170. .format_mod_supported = intel_sprite_plane_format_mod_supported,
  1171. };
  1172. bool skl_plane_has_ccs(struct drm_i915_private *dev_priv,
  1173. enum pipe pipe, enum plane_id plane_id)
  1174. {
  1175. if (plane_id == PLANE_CURSOR)
  1176. return false;
  1177. if (INTEL_GEN(dev_priv) >= 10)
  1178. return true;
  1179. if (IS_GEMINILAKE(dev_priv))
  1180. return pipe != PIPE_C;
  1181. return pipe != PIPE_C &&
  1182. (plane_id == PLANE_PRIMARY ||
  1183. plane_id == PLANE_SPRITE0);
  1184. }
  1185. struct intel_plane *
  1186. intel_sprite_plane_create(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe, int plane)
  1188. {
  1189. struct intel_plane *intel_plane = NULL;
  1190. struct intel_plane_state *state = NULL;
  1191. unsigned long possible_crtcs;
  1192. const uint32_t *plane_formats;
  1193. const uint64_t *modifiers;
  1194. unsigned int supported_rotations;
  1195. int num_plane_formats;
  1196. int ret;
  1197. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1198. if (!intel_plane) {
  1199. ret = -ENOMEM;
  1200. goto fail;
  1201. }
  1202. state = intel_create_plane_state(&intel_plane->base);
  1203. if (!state) {
  1204. ret = -ENOMEM;
  1205. goto fail;
  1206. }
  1207. intel_plane->base.state = &state->base;
  1208. if (INTEL_GEN(dev_priv) >= 9) {
  1209. intel_plane->can_scale = true;
  1210. state->scaler_id = -1;
  1211. intel_plane->update_plane = skl_update_plane;
  1212. intel_plane->disable_plane = skl_disable_plane;
  1213. intel_plane->get_hw_state = skl_plane_get_hw_state;
  1214. plane_formats = skl_plane_formats;
  1215. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1216. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_SPRITE0 + plane))
  1217. modifiers = skl_plane_format_modifiers_ccs;
  1218. else
  1219. modifiers = skl_plane_format_modifiers_noccs;
  1220. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1221. intel_plane->can_scale = false;
  1222. intel_plane->max_downscale = 1;
  1223. intel_plane->update_plane = vlv_update_plane;
  1224. intel_plane->disable_plane = vlv_disable_plane;
  1225. intel_plane->get_hw_state = vlv_plane_get_hw_state;
  1226. plane_formats = vlv_plane_formats;
  1227. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1228. modifiers = i9xx_plane_format_modifiers;
  1229. } else if (INTEL_GEN(dev_priv) >= 7) {
  1230. if (IS_IVYBRIDGE(dev_priv)) {
  1231. intel_plane->can_scale = true;
  1232. intel_plane->max_downscale = 2;
  1233. } else {
  1234. intel_plane->can_scale = false;
  1235. intel_plane->max_downscale = 1;
  1236. }
  1237. intel_plane->update_plane = ivb_update_plane;
  1238. intel_plane->disable_plane = ivb_disable_plane;
  1239. intel_plane->get_hw_state = ivb_plane_get_hw_state;
  1240. plane_formats = snb_plane_formats;
  1241. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1242. modifiers = i9xx_plane_format_modifiers;
  1243. } else {
  1244. intel_plane->can_scale = true;
  1245. intel_plane->max_downscale = 16;
  1246. intel_plane->update_plane = g4x_update_plane;
  1247. intel_plane->disable_plane = g4x_disable_plane;
  1248. intel_plane->get_hw_state = g4x_plane_get_hw_state;
  1249. modifiers = i9xx_plane_format_modifiers;
  1250. if (IS_GEN6(dev_priv)) {
  1251. plane_formats = snb_plane_formats;
  1252. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1253. } else {
  1254. plane_formats = g4x_plane_formats;
  1255. num_plane_formats = ARRAY_SIZE(g4x_plane_formats);
  1256. }
  1257. }
  1258. if (INTEL_GEN(dev_priv) >= 9) {
  1259. supported_rotations =
  1260. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  1261. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  1262. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  1263. supported_rotations =
  1264. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  1265. DRM_MODE_REFLECT_X;
  1266. } else {
  1267. supported_rotations =
  1268. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  1269. }
  1270. intel_plane->pipe = pipe;
  1271. intel_plane->i9xx_plane = plane;
  1272. intel_plane->id = PLANE_SPRITE0 + plane;
  1273. intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, intel_plane->id);
  1274. intel_plane->check_plane = intel_check_sprite_plane;
  1275. possible_crtcs = (1 << pipe);
  1276. if (INTEL_GEN(dev_priv) >= 9)
  1277. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1278. possible_crtcs, &intel_sprite_plane_funcs,
  1279. plane_formats, num_plane_formats,
  1280. modifiers,
  1281. DRM_PLANE_TYPE_OVERLAY,
  1282. "plane %d%c", plane + 2, pipe_name(pipe));
  1283. else
  1284. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1285. possible_crtcs, &intel_sprite_plane_funcs,
  1286. plane_formats, num_plane_formats,
  1287. modifiers,
  1288. DRM_PLANE_TYPE_OVERLAY,
  1289. "sprite %c", sprite_name(pipe, plane));
  1290. if (ret)
  1291. goto fail;
  1292. drm_plane_create_rotation_property(&intel_plane->base,
  1293. DRM_MODE_ROTATE_0,
  1294. supported_rotations);
  1295. drm_plane_create_color_properties(&intel_plane->base,
  1296. BIT(DRM_COLOR_YCBCR_BT601) |
  1297. BIT(DRM_COLOR_YCBCR_BT709),
  1298. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  1299. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  1300. DRM_COLOR_YCBCR_BT709,
  1301. DRM_COLOR_YCBCR_LIMITED_RANGE);
  1302. drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
  1303. return intel_plane;
  1304. fail:
  1305. kfree(state);
  1306. kfree(intel_plane);
  1307. return ERR_PTR(ret);
  1308. }