intel_sprite.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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. static bool
  44. format_is_yuv(uint32_t format)
  45. {
  46. switch (format) {
  47. case DRM_FORMAT_YUYV:
  48. case DRM_FORMAT_UYVY:
  49. case DRM_FORMAT_VYUY:
  50. case DRM_FORMAT_YVYU:
  51. return true;
  52. default:
  53. return false;
  54. }
  55. }
  56. int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  57. int usecs)
  58. {
  59. /* paranoia */
  60. if (!adjusted_mode->crtc_htotal)
  61. return 1;
  62. return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
  63. 1000 * adjusted_mode->crtc_htotal);
  64. }
  65. #define VBLANK_EVASION_TIME_US 100
  66. /**
  67. * intel_pipe_update_start() - start update of a set of display registers
  68. * @new_crtc_state: the new crtc state
  69. *
  70. * Mark the start of an update to pipe registers that should be updated
  71. * atomically regarding vblank. If the next vblank will happens within
  72. * the next 100 us, this function waits until the vblank passes.
  73. *
  74. * After a successful call to this function, interrupts will be disabled
  75. * until a subsequent call to intel_pipe_update_end(). That is done to
  76. * avoid random delays.
  77. */
  78. void intel_pipe_update_start(const struct intel_crtc_state *new_crtc_state)
  79. {
  80. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  81. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  82. const struct drm_display_mode *adjusted_mode = &new_crtc_state->base.adjusted_mode;
  83. long timeout = msecs_to_jiffies_timeout(1);
  84. int scanline, min, max, vblank_start;
  85. wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
  86. bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  87. intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
  88. DEFINE_WAIT(wait);
  89. vblank_start = adjusted_mode->crtc_vblank_start;
  90. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  91. vblank_start = DIV_ROUND_UP(vblank_start, 2);
  92. /* FIXME needs to be calibrated sensibly */
  93. min = vblank_start - intel_usecs_to_scanlines(adjusted_mode,
  94. VBLANK_EVASION_TIME_US);
  95. max = vblank_start - 1;
  96. local_irq_disable();
  97. if (min <= 0 || max <= 0)
  98. return;
  99. if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
  100. return;
  101. crtc->debug.min_vbl = min;
  102. crtc->debug.max_vbl = max;
  103. trace_i915_pipe_update_start(crtc);
  104. for (;;) {
  105. /*
  106. * prepare_to_wait() has a memory barrier, which guarantees
  107. * other CPUs can see the task state update by the time we
  108. * read the scanline.
  109. */
  110. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  111. scanline = intel_get_crtc_scanline(crtc);
  112. if (scanline < min || scanline > max)
  113. break;
  114. if (timeout <= 0) {
  115. DRM_ERROR("Potential atomic update failure on pipe %c\n",
  116. pipe_name(crtc->pipe));
  117. break;
  118. }
  119. local_irq_enable();
  120. timeout = schedule_timeout(timeout);
  121. local_irq_disable();
  122. }
  123. finish_wait(wq, &wait);
  124. drm_crtc_vblank_put(&crtc->base);
  125. /*
  126. * On VLV/CHV DSI the scanline counter would appear to
  127. * increment approx. 1/3 of a scanline before start of vblank.
  128. * The registers still get latched at start of vblank however.
  129. * This means we must not write any registers on the first
  130. * line of vblank (since not the whole line is actually in
  131. * vblank). And unfortunately we can't use the interrupt to
  132. * wait here since it will fire too soon. We could use the
  133. * frame start interrupt instead since it will fire after the
  134. * critical scanline, but that would require more changes
  135. * in the interrupt code. So for now we'll just do the nasty
  136. * thing and poll for the bad scanline to pass us by.
  137. *
  138. * FIXME figure out if BXT+ DSI suffers from this as well
  139. */
  140. while (need_vlv_dsi_wa && scanline == vblank_start)
  141. scanline = intel_get_crtc_scanline(crtc);
  142. crtc->debug.scanline_start = scanline;
  143. crtc->debug.start_vbl_time = ktime_get();
  144. crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
  145. trace_i915_pipe_update_vblank_evaded(crtc);
  146. }
  147. /**
  148. * intel_pipe_update_end() - end update of a set of display registers
  149. * @new_crtc_state: the new crtc state
  150. *
  151. * Mark the end of an update started with intel_pipe_update_start(). This
  152. * re-enables interrupts and verifies the update was actually completed
  153. * before a vblank.
  154. */
  155. void intel_pipe_update_end(struct intel_crtc_state *new_crtc_state)
  156. {
  157. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  158. enum pipe pipe = crtc->pipe;
  159. int scanline_end = intel_get_crtc_scanline(crtc);
  160. u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
  161. ktime_t end_vbl_time = ktime_get();
  162. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  163. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  164. /* We're still in the vblank-evade critical section, this can't race.
  165. * Would be slightly nice to just grab the vblank count and arm the
  166. * event outside of the critical section - the spinlock might spin for a
  167. * while ... */
  168. if (new_crtc_state->base.event) {
  169. WARN_ON(drm_crtc_vblank_get(&crtc->base) != 0);
  170. spin_lock(&crtc->base.dev->event_lock);
  171. drm_crtc_arm_vblank_event(&crtc->base, new_crtc_state->base.event);
  172. spin_unlock(&crtc->base.dev->event_lock);
  173. new_crtc_state->base.event = NULL;
  174. }
  175. local_irq_enable();
  176. if (intel_vgpu_active(dev_priv))
  177. return;
  178. if (crtc->debug.start_vbl_count &&
  179. crtc->debug.start_vbl_count != end_vbl_count) {
  180. 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",
  181. pipe_name(pipe), crtc->debug.start_vbl_count,
  182. end_vbl_count,
  183. ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
  184. crtc->debug.min_vbl, crtc->debug.max_vbl,
  185. crtc->debug.scanline_start, scanline_end);
  186. }
  187. #ifdef CONFIG_DRM_I915_DEBUG_VBLANK_EVADE
  188. else if (ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time) >
  189. VBLANK_EVASION_TIME_US)
  190. DRM_WARN("Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
  191. pipe_name(pipe),
  192. ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
  193. VBLANK_EVASION_TIME_US);
  194. #endif
  195. }
  196. static void
  197. skl_update_plane(struct intel_plane *plane,
  198. const struct intel_crtc_state *crtc_state,
  199. const struct intel_plane_state *plane_state)
  200. {
  201. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  202. const struct drm_framebuffer *fb = plane_state->base.fb;
  203. enum plane_id plane_id = plane->id;
  204. enum pipe pipe = plane->pipe;
  205. u32 plane_ctl = plane_state->ctl;
  206. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  207. u32 surf_addr = plane_state->main.offset;
  208. unsigned int rotation = plane_state->base.rotation;
  209. u32 stride = skl_plane_stride(fb, 0, rotation);
  210. u32 aux_stride = skl_plane_stride(fb, 1, rotation);
  211. int crtc_x = plane_state->base.dst.x1;
  212. int crtc_y = plane_state->base.dst.y1;
  213. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  214. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  215. uint32_t x = plane_state->main.x;
  216. uint32_t y = plane_state->main.y;
  217. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  218. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  219. unsigned long irqflags;
  220. /* Sizes are 0 based */
  221. src_w--;
  222. src_h--;
  223. crtc_w--;
  224. crtc_h--;
  225. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  226. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  227. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  228. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  229. PLANE_COLOR_PIPE_CSC_ENABLE |
  230. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  231. }
  232. if (key->flags) {
  233. I915_WRITE_FW(PLANE_KEYVAL(pipe, plane_id), key->min_value);
  234. I915_WRITE_FW(PLANE_KEYMAX(pipe, plane_id), key->max_value);
  235. I915_WRITE_FW(PLANE_KEYMSK(pipe, plane_id), key->channel_mask);
  236. }
  237. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (y << 16) | x);
  238. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  239. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  240. I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
  241. (plane_state->aux.offset - surf_addr) | aux_stride);
  242. I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
  243. (plane_state->aux.y << 16) | plane_state->aux.x);
  244. /* program plane scaler */
  245. if (plane_state->scaler_id >= 0) {
  246. int scaler_id = plane_state->scaler_id;
  247. const struct intel_scaler *scaler;
  248. scaler = &crtc_state->scaler_state.scalers[scaler_id];
  249. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id),
  250. PS_SCALER_EN | PS_PLANE_SEL(plane_id) | scaler->mode);
  251. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  252. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
  253. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id),
  254. ((crtc_w + 1) << 16)|(crtc_h + 1));
  255. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  256. } else {
  257. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (crtc_y << 16) | crtc_x);
  258. }
  259. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  260. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  261. intel_plane_ggtt_offset(plane_state) + surf_addr);
  262. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  263. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  264. }
  265. static void
  266. skl_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  267. {
  268. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  269. enum plane_id plane_id = plane->id;
  270. enum pipe pipe = plane->pipe;
  271. unsigned long irqflags;
  272. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  273. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  274. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  275. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  276. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  277. }
  278. static void
  279. chv_update_csc(struct intel_plane *plane, uint32_t format)
  280. {
  281. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  282. enum plane_id plane_id = plane->id;
  283. /* Seems RGB data bypasses the CSC always */
  284. if (!format_is_yuv(format))
  285. return;
  286. /*
  287. * BT.601 limited range YCbCr -> full range RGB
  288. *
  289. * |r| | 6537 4769 0| |cr |
  290. * |g| = |-3330 4769 -1605| x |y-64|
  291. * |b| | 0 4769 8263| |cb |
  292. *
  293. * Cb and Cr apparently come in as signed already, so no
  294. * need for any offset. For Y we need to remove the offset.
  295. */
  296. I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
  297. I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  298. I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  299. I915_WRITE_FW(SPCSCC01(plane_id), SPCSC_C1(4769) | SPCSC_C0(6537));
  300. I915_WRITE_FW(SPCSCC23(plane_id), SPCSC_C1(-3330) | SPCSC_C0(0));
  301. I915_WRITE_FW(SPCSCC45(plane_id), SPCSC_C1(-1605) | SPCSC_C0(4769));
  302. I915_WRITE_FW(SPCSCC67(plane_id), SPCSC_C1(4769) | SPCSC_C0(0));
  303. I915_WRITE_FW(SPCSCC8(plane_id), SPCSC_C0(8263));
  304. I915_WRITE_FW(SPCSCYGICLAMP(plane_id), SPCSC_IMAX(940) | SPCSC_IMIN(64));
  305. I915_WRITE_FW(SPCSCCBICLAMP(plane_id), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
  306. I915_WRITE_FW(SPCSCCRICLAMP(plane_id), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
  307. I915_WRITE_FW(SPCSCYGOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  308. I915_WRITE_FW(SPCSCCBOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  309. I915_WRITE_FW(SPCSCCROCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  310. }
  311. static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
  312. const struct intel_plane_state *plane_state)
  313. {
  314. const struct drm_framebuffer *fb = plane_state->base.fb;
  315. unsigned int rotation = plane_state->base.rotation;
  316. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  317. u32 sprctl;
  318. sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
  319. switch (fb->format->format) {
  320. case DRM_FORMAT_YUYV:
  321. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  322. break;
  323. case DRM_FORMAT_YVYU:
  324. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  325. break;
  326. case DRM_FORMAT_UYVY:
  327. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  328. break;
  329. case DRM_FORMAT_VYUY:
  330. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  331. break;
  332. case DRM_FORMAT_RGB565:
  333. sprctl |= SP_FORMAT_BGR565;
  334. break;
  335. case DRM_FORMAT_XRGB8888:
  336. sprctl |= SP_FORMAT_BGRX8888;
  337. break;
  338. case DRM_FORMAT_ARGB8888:
  339. sprctl |= SP_FORMAT_BGRA8888;
  340. break;
  341. case DRM_FORMAT_XBGR2101010:
  342. sprctl |= SP_FORMAT_RGBX1010102;
  343. break;
  344. case DRM_FORMAT_ABGR2101010:
  345. sprctl |= SP_FORMAT_RGBA1010102;
  346. break;
  347. case DRM_FORMAT_XBGR8888:
  348. sprctl |= SP_FORMAT_RGBX8888;
  349. break;
  350. case DRM_FORMAT_ABGR8888:
  351. sprctl |= SP_FORMAT_RGBA8888;
  352. break;
  353. default:
  354. MISSING_CASE(fb->format->format);
  355. return 0;
  356. }
  357. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  358. sprctl |= SP_TILED;
  359. if (rotation & DRM_MODE_ROTATE_180)
  360. sprctl |= SP_ROTATE_180;
  361. if (rotation & DRM_MODE_REFLECT_X)
  362. sprctl |= SP_MIRROR;
  363. if (key->flags & I915_SET_COLORKEY_SOURCE)
  364. sprctl |= SP_SOURCE_KEY;
  365. return sprctl;
  366. }
  367. static void
  368. vlv_update_plane(struct intel_plane *plane,
  369. const struct intel_crtc_state *crtc_state,
  370. const struct intel_plane_state *plane_state)
  371. {
  372. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  373. const struct drm_framebuffer *fb = plane_state->base.fb;
  374. enum pipe pipe = plane->pipe;
  375. enum plane_id plane_id = plane->id;
  376. u32 sprctl = plane_state->ctl;
  377. u32 sprsurf_offset = plane_state->main.offset;
  378. u32 linear_offset;
  379. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  380. int crtc_x = plane_state->base.dst.x1;
  381. int crtc_y = plane_state->base.dst.y1;
  382. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  383. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  384. uint32_t x = plane_state->main.x;
  385. uint32_t y = plane_state->main.y;
  386. unsigned long irqflags;
  387. /* Sizes are 0 based */
  388. crtc_w--;
  389. crtc_h--;
  390. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  391. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  392. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
  393. chv_update_csc(plane, fb->format->format);
  394. if (key->flags) {
  395. I915_WRITE_FW(SPKEYMINVAL(pipe, plane_id), key->min_value);
  396. I915_WRITE_FW(SPKEYMAXVAL(pipe, plane_id), key->max_value);
  397. I915_WRITE_FW(SPKEYMSK(pipe, plane_id), key->channel_mask);
  398. }
  399. I915_WRITE_FW(SPSTRIDE(pipe, plane_id), fb->pitches[0]);
  400. I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
  401. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  402. I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
  403. else
  404. I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
  405. I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
  406. I915_WRITE_FW(SPSIZE(pipe, plane_id), (crtc_h << 16) | crtc_w);
  407. I915_WRITE_FW(SPCNTR(pipe, plane_id), sprctl);
  408. I915_WRITE_FW(SPSURF(pipe, plane_id),
  409. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  410. POSTING_READ_FW(SPSURF(pipe, plane_id));
  411. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  412. }
  413. static void
  414. vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  415. {
  416. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  417. enum pipe pipe = plane->pipe;
  418. enum plane_id plane_id = plane->id;
  419. unsigned long irqflags;
  420. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  421. I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
  422. I915_WRITE_FW(SPSURF(pipe, plane_id), 0);
  423. POSTING_READ_FW(SPSURF(pipe, plane_id));
  424. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  425. }
  426. static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
  427. const struct intel_plane_state *plane_state)
  428. {
  429. struct drm_i915_private *dev_priv =
  430. to_i915(plane_state->base.plane->dev);
  431. const struct drm_framebuffer *fb = plane_state->base.fb;
  432. unsigned int rotation = plane_state->base.rotation;
  433. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  434. u32 sprctl;
  435. sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
  436. if (IS_IVYBRIDGE(dev_priv))
  437. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  438. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  439. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  440. switch (fb->format->format) {
  441. case DRM_FORMAT_XBGR8888:
  442. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  443. break;
  444. case DRM_FORMAT_XRGB8888:
  445. sprctl |= SPRITE_FORMAT_RGBX888;
  446. break;
  447. case DRM_FORMAT_YUYV:
  448. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  449. break;
  450. case DRM_FORMAT_YVYU:
  451. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  452. break;
  453. case DRM_FORMAT_UYVY:
  454. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  455. break;
  456. case DRM_FORMAT_VYUY:
  457. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  458. break;
  459. default:
  460. MISSING_CASE(fb->format->format);
  461. return 0;
  462. }
  463. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  464. sprctl |= SPRITE_TILED;
  465. if (rotation & DRM_MODE_ROTATE_180)
  466. sprctl |= SPRITE_ROTATE_180;
  467. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  468. sprctl |= SPRITE_DEST_KEY;
  469. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  470. sprctl |= SPRITE_SOURCE_KEY;
  471. return sprctl;
  472. }
  473. static void
  474. ivb_update_plane(struct intel_plane *plane,
  475. const struct intel_crtc_state *crtc_state,
  476. const struct intel_plane_state *plane_state)
  477. {
  478. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  479. const struct drm_framebuffer *fb = plane_state->base.fb;
  480. enum pipe pipe = plane->pipe;
  481. u32 sprctl = plane_state->ctl, sprscale = 0;
  482. u32 sprsurf_offset = plane_state->main.offset;
  483. u32 linear_offset;
  484. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  485. int crtc_x = plane_state->base.dst.x1;
  486. int crtc_y = plane_state->base.dst.y1;
  487. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  488. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  489. uint32_t x = plane_state->main.x;
  490. uint32_t y = plane_state->main.y;
  491. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  492. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  493. unsigned long irqflags;
  494. /* Sizes are 0 based */
  495. src_w--;
  496. src_h--;
  497. crtc_w--;
  498. crtc_h--;
  499. if (crtc_w != src_w || crtc_h != src_h)
  500. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  501. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  502. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  503. if (key->flags) {
  504. I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
  505. I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
  506. I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
  507. }
  508. I915_WRITE_FW(SPRSTRIDE(pipe), fb->pitches[0]);
  509. I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  510. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  511. * register */
  512. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  513. I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
  514. else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  515. I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
  516. else
  517. I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
  518. I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  519. if (plane->can_scale)
  520. I915_WRITE_FW(SPRSCALE(pipe), sprscale);
  521. I915_WRITE_FW(SPRCTL(pipe), sprctl);
  522. I915_WRITE_FW(SPRSURF(pipe),
  523. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  524. POSTING_READ_FW(SPRSURF(pipe));
  525. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  526. }
  527. static void
  528. ivb_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  529. {
  530. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  531. enum pipe pipe = plane->pipe;
  532. unsigned long irqflags;
  533. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  534. I915_WRITE_FW(SPRCTL(pipe), 0);
  535. /* Can't leave the scaler enabled... */
  536. if (plane->can_scale)
  537. I915_WRITE_FW(SPRSCALE(pipe), 0);
  538. I915_WRITE_FW(SPRSURF(pipe), 0);
  539. POSTING_READ_FW(SPRSURF(pipe));
  540. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  541. }
  542. static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
  543. const struct intel_plane_state *plane_state)
  544. {
  545. struct drm_i915_private *dev_priv =
  546. to_i915(plane_state->base.plane->dev);
  547. const struct drm_framebuffer *fb = plane_state->base.fb;
  548. unsigned int rotation = plane_state->base.rotation;
  549. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  550. u32 dvscntr;
  551. dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
  552. if (IS_GEN6(dev_priv))
  553. dvscntr |= DVS_TRICKLE_FEED_DISABLE;
  554. switch (fb->format->format) {
  555. case DRM_FORMAT_XBGR8888:
  556. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  557. break;
  558. case DRM_FORMAT_XRGB8888:
  559. dvscntr |= DVS_FORMAT_RGBX888;
  560. break;
  561. case DRM_FORMAT_YUYV:
  562. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  563. break;
  564. case DRM_FORMAT_YVYU:
  565. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  566. break;
  567. case DRM_FORMAT_UYVY:
  568. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  569. break;
  570. case DRM_FORMAT_VYUY:
  571. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  572. break;
  573. default:
  574. MISSING_CASE(fb->format->format);
  575. return 0;
  576. }
  577. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  578. dvscntr |= DVS_TILED;
  579. if (rotation & DRM_MODE_ROTATE_180)
  580. dvscntr |= DVS_ROTATE_180;
  581. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  582. dvscntr |= DVS_DEST_KEY;
  583. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  584. dvscntr |= DVS_SOURCE_KEY;
  585. return dvscntr;
  586. }
  587. static void
  588. g4x_update_plane(struct intel_plane *plane,
  589. const struct intel_crtc_state *crtc_state,
  590. const struct intel_plane_state *plane_state)
  591. {
  592. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  593. const struct drm_framebuffer *fb = plane_state->base.fb;
  594. enum pipe pipe = plane->pipe;
  595. u32 dvscntr = plane_state->ctl, dvsscale = 0;
  596. u32 dvssurf_offset = plane_state->main.offset;
  597. u32 linear_offset;
  598. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  599. int crtc_x = plane_state->base.dst.x1;
  600. int crtc_y = plane_state->base.dst.y1;
  601. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  602. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  603. uint32_t x = plane_state->main.x;
  604. uint32_t y = plane_state->main.y;
  605. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  606. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  607. unsigned long irqflags;
  608. /* Sizes are 0 based */
  609. src_w--;
  610. src_h--;
  611. crtc_w--;
  612. crtc_h--;
  613. if (crtc_w != src_w || crtc_h != src_h)
  614. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  615. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  616. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  617. if (key->flags) {
  618. I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
  619. I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
  620. I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
  621. }
  622. I915_WRITE_FW(DVSSTRIDE(pipe), fb->pitches[0]);
  623. I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  624. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  625. I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
  626. else
  627. I915_WRITE_FW(DVSLINOFF(pipe), linear_offset);
  628. I915_WRITE_FW(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  629. I915_WRITE_FW(DVSSCALE(pipe), dvsscale);
  630. I915_WRITE_FW(DVSCNTR(pipe), dvscntr);
  631. I915_WRITE_FW(DVSSURF(pipe),
  632. intel_plane_ggtt_offset(plane_state) + dvssurf_offset);
  633. POSTING_READ_FW(DVSSURF(pipe));
  634. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  635. }
  636. static void
  637. g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  638. {
  639. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  640. enum pipe pipe = plane->pipe;
  641. unsigned long irqflags;
  642. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  643. I915_WRITE_FW(DVSCNTR(pipe), 0);
  644. /* Disable the scaler */
  645. I915_WRITE_FW(DVSSCALE(pipe), 0);
  646. I915_WRITE_FW(DVSSURF(pipe), 0);
  647. POSTING_READ_FW(DVSSURF(pipe));
  648. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  649. }
  650. static int
  651. intel_check_sprite_plane(struct intel_plane *plane,
  652. struct intel_crtc_state *crtc_state,
  653. struct intel_plane_state *state)
  654. {
  655. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  656. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  657. struct drm_framebuffer *fb = state->base.fb;
  658. int crtc_x, crtc_y;
  659. unsigned int crtc_w, crtc_h;
  660. uint32_t src_x, src_y, src_w, src_h;
  661. struct drm_rect *src = &state->base.src;
  662. struct drm_rect *dst = &state->base.dst;
  663. const struct drm_rect *clip = &state->clip;
  664. int hscale, vscale;
  665. int max_scale, min_scale;
  666. bool can_scale;
  667. int ret;
  668. *src = drm_plane_state_src(&state->base);
  669. *dst = drm_plane_state_dest(&state->base);
  670. if (!fb) {
  671. state->base.visible = false;
  672. return 0;
  673. }
  674. /* Don't modify another pipe's plane */
  675. if (plane->pipe != crtc->pipe) {
  676. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  677. return -EINVAL;
  678. }
  679. /* FIXME check all gen limits */
  680. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
  681. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  682. return -EINVAL;
  683. }
  684. /* setup can_scale, min_scale, max_scale */
  685. if (INTEL_GEN(dev_priv) >= 9) {
  686. /* use scaler when colorkey is not required */
  687. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  688. can_scale = 1;
  689. min_scale = 1;
  690. max_scale = skl_max_scale(crtc, crtc_state);
  691. } else {
  692. can_scale = 0;
  693. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  694. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  695. }
  696. } else {
  697. can_scale = plane->can_scale;
  698. max_scale = plane->max_downscale << 16;
  699. min_scale = plane->can_scale ? 1 : (1 << 16);
  700. }
  701. /*
  702. * FIXME the following code does a bunch of fuzzy adjustments to the
  703. * coordinates and sizes. We probably need some way to decide whether
  704. * more strict checking should be done instead.
  705. */
  706. drm_rect_rotate(src, fb->width << 16, fb->height << 16,
  707. state->base.rotation);
  708. hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
  709. BUG_ON(hscale < 0);
  710. vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
  711. BUG_ON(vscale < 0);
  712. state->base.visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
  713. crtc_x = dst->x1;
  714. crtc_y = dst->y1;
  715. crtc_w = drm_rect_width(dst);
  716. crtc_h = drm_rect_height(dst);
  717. if (state->base.visible) {
  718. /* check again in case clipping clamped the results */
  719. hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
  720. if (hscale < 0) {
  721. DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
  722. drm_rect_debug_print("src: ", src, true);
  723. drm_rect_debug_print("dst: ", dst, false);
  724. return hscale;
  725. }
  726. vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
  727. if (vscale < 0) {
  728. DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
  729. drm_rect_debug_print("src: ", src, true);
  730. drm_rect_debug_print("dst: ", dst, false);
  731. return vscale;
  732. }
  733. /* Make the source viewport size an exact multiple of the scaling factors. */
  734. drm_rect_adjust_size(src,
  735. drm_rect_width(dst) * hscale - drm_rect_width(src),
  736. drm_rect_height(dst) * vscale - drm_rect_height(src));
  737. drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
  738. state->base.rotation);
  739. /* sanity check to make sure the src viewport wasn't enlarged */
  740. WARN_ON(src->x1 < (int) state->base.src_x ||
  741. src->y1 < (int) state->base.src_y ||
  742. src->x2 > (int) state->base.src_x + state->base.src_w ||
  743. src->y2 > (int) state->base.src_y + state->base.src_h);
  744. /*
  745. * Hardware doesn't handle subpixel coordinates.
  746. * Adjust to (macro)pixel boundary, but be careful not to
  747. * increase the source viewport size, because that could
  748. * push the downscaling factor out of bounds.
  749. */
  750. src_x = src->x1 >> 16;
  751. src_w = drm_rect_width(src) >> 16;
  752. src_y = src->y1 >> 16;
  753. src_h = drm_rect_height(src) >> 16;
  754. if (format_is_yuv(fb->format->format)) {
  755. src_x &= ~1;
  756. src_w &= ~1;
  757. /*
  758. * Must keep src and dst the
  759. * same if we can't scale.
  760. */
  761. if (!can_scale)
  762. crtc_w &= ~1;
  763. if (crtc_w == 0)
  764. state->base.visible = false;
  765. }
  766. }
  767. /* Check size restrictions when scaling */
  768. if (state->base.visible && (src_w != crtc_w || src_h != crtc_h)) {
  769. unsigned int width_bytes;
  770. int cpp = fb->format->cpp[0];
  771. WARN_ON(!can_scale);
  772. /* FIXME interlacing min height is 6 */
  773. if (crtc_w < 3 || crtc_h < 3)
  774. state->base.visible = false;
  775. if (src_w < 3 || src_h < 3)
  776. state->base.visible = false;
  777. width_bytes = ((src_x * cpp) & 63) + src_w * cpp;
  778. if (INTEL_GEN(dev_priv) < 9 && (src_w > 2048 || src_h > 2048 ||
  779. width_bytes > 4096 || fb->pitches[0] > 4096)) {
  780. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  781. return -EINVAL;
  782. }
  783. }
  784. if (state->base.visible) {
  785. src->x1 = src_x << 16;
  786. src->x2 = (src_x + src_w) << 16;
  787. src->y1 = src_y << 16;
  788. src->y2 = (src_y + src_h) << 16;
  789. }
  790. dst->x1 = crtc_x;
  791. dst->x2 = crtc_x + crtc_w;
  792. dst->y1 = crtc_y;
  793. dst->y2 = crtc_y + crtc_h;
  794. if (INTEL_GEN(dev_priv) >= 9) {
  795. ret = skl_check_plane_surface(state);
  796. if (ret)
  797. return ret;
  798. state->ctl = skl_plane_ctl(crtc_state, state);
  799. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  800. ret = i9xx_check_plane_surface(state);
  801. if (ret)
  802. return ret;
  803. state->ctl = vlv_sprite_ctl(crtc_state, state);
  804. } else if (INTEL_GEN(dev_priv) >= 7) {
  805. ret = i9xx_check_plane_surface(state);
  806. if (ret)
  807. return ret;
  808. state->ctl = ivb_sprite_ctl(crtc_state, state);
  809. } else {
  810. ret = i9xx_check_plane_surface(state);
  811. if (ret)
  812. return ret;
  813. state->ctl = g4x_sprite_ctl(crtc_state, state);
  814. }
  815. return 0;
  816. }
  817. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  818. struct drm_file *file_priv)
  819. {
  820. struct drm_i915_private *dev_priv = to_i915(dev);
  821. struct drm_intel_sprite_colorkey *set = data;
  822. struct drm_plane *plane;
  823. struct drm_plane_state *plane_state;
  824. struct drm_atomic_state *state;
  825. struct drm_modeset_acquire_ctx ctx;
  826. int ret = 0;
  827. /* Make sure we don't try to enable both src & dest simultaneously */
  828. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  829. return -EINVAL;
  830. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  831. set->flags & I915_SET_COLORKEY_DESTINATION)
  832. return -EINVAL;
  833. plane = drm_plane_find(dev, file_priv, set->plane_id);
  834. if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
  835. return -ENOENT;
  836. drm_modeset_acquire_init(&ctx, 0);
  837. state = drm_atomic_state_alloc(plane->dev);
  838. if (!state) {
  839. ret = -ENOMEM;
  840. goto out;
  841. }
  842. state->acquire_ctx = &ctx;
  843. while (1) {
  844. plane_state = drm_atomic_get_plane_state(state, plane);
  845. ret = PTR_ERR_OR_ZERO(plane_state);
  846. if (!ret) {
  847. to_intel_plane_state(plane_state)->ckey = *set;
  848. ret = drm_atomic_commit(state);
  849. }
  850. if (ret != -EDEADLK)
  851. break;
  852. drm_atomic_state_clear(state);
  853. drm_modeset_backoff(&ctx);
  854. }
  855. drm_atomic_state_put(state);
  856. out:
  857. drm_modeset_drop_locks(&ctx);
  858. drm_modeset_acquire_fini(&ctx);
  859. return ret;
  860. }
  861. static const uint32_t g4x_plane_formats[] = {
  862. DRM_FORMAT_XRGB8888,
  863. DRM_FORMAT_YUYV,
  864. DRM_FORMAT_YVYU,
  865. DRM_FORMAT_UYVY,
  866. DRM_FORMAT_VYUY,
  867. };
  868. static const uint64_t i9xx_plane_format_modifiers[] = {
  869. I915_FORMAT_MOD_X_TILED,
  870. DRM_FORMAT_MOD_LINEAR,
  871. DRM_FORMAT_MOD_INVALID
  872. };
  873. static const uint32_t snb_plane_formats[] = {
  874. DRM_FORMAT_XBGR8888,
  875. DRM_FORMAT_XRGB8888,
  876. DRM_FORMAT_YUYV,
  877. DRM_FORMAT_YVYU,
  878. DRM_FORMAT_UYVY,
  879. DRM_FORMAT_VYUY,
  880. };
  881. static const uint32_t vlv_plane_formats[] = {
  882. DRM_FORMAT_RGB565,
  883. DRM_FORMAT_ABGR8888,
  884. DRM_FORMAT_ARGB8888,
  885. DRM_FORMAT_XBGR8888,
  886. DRM_FORMAT_XRGB8888,
  887. DRM_FORMAT_XBGR2101010,
  888. DRM_FORMAT_ABGR2101010,
  889. DRM_FORMAT_YUYV,
  890. DRM_FORMAT_YVYU,
  891. DRM_FORMAT_UYVY,
  892. DRM_FORMAT_VYUY,
  893. };
  894. static uint32_t skl_plane_formats[] = {
  895. DRM_FORMAT_RGB565,
  896. DRM_FORMAT_ABGR8888,
  897. DRM_FORMAT_ARGB8888,
  898. DRM_FORMAT_XBGR8888,
  899. DRM_FORMAT_XRGB8888,
  900. DRM_FORMAT_YUYV,
  901. DRM_FORMAT_YVYU,
  902. DRM_FORMAT_UYVY,
  903. DRM_FORMAT_VYUY,
  904. };
  905. static const uint64_t skl_plane_format_modifiers[] = {
  906. I915_FORMAT_MOD_X_TILED,
  907. DRM_FORMAT_MOD_LINEAR,
  908. DRM_FORMAT_MOD_INVALID
  909. };
  910. static bool g4x_sprite_plane_format_mod_supported(struct drm_plane *plane,
  911. uint32_t format,
  912. uint64_t modifier)
  913. {
  914. switch (format) {
  915. case DRM_FORMAT_XBGR8888:
  916. case DRM_FORMAT_XRGB8888:
  917. case DRM_FORMAT_YUYV:
  918. case DRM_FORMAT_YVYU:
  919. case DRM_FORMAT_UYVY:
  920. case DRM_FORMAT_VYUY:
  921. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  922. modifier == I915_FORMAT_MOD_X_TILED)
  923. return true;
  924. /* fall through */
  925. default:
  926. return false;
  927. }
  928. }
  929. static bool vlv_sprite_plane_format_mod_supported(struct drm_plane *plane,
  930. uint32_t format,
  931. uint64_t modifier)
  932. {
  933. switch (format) {
  934. case DRM_FORMAT_YUYV:
  935. case DRM_FORMAT_YVYU:
  936. case DRM_FORMAT_UYVY:
  937. case DRM_FORMAT_VYUY:
  938. case DRM_FORMAT_RGB565:
  939. case DRM_FORMAT_XRGB8888:
  940. case DRM_FORMAT_ARGB8888:
  941. case DRM_FORMAT_XBGR2101010:
  942. case DRM_FORMAT_ABGR2101010:
  943. case DRM_FORMAT_XBGR8888:
  944. case DRM_FORMAT_ABGR8888:
  945. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  946. modifier == I915_FORMAT_MOD_X_TILED)
  947. return true;
  948. /* fall through */
  949. default:
  950. return false;
  951. }
  952. }
  953. static bool skl_sprite_plane_format_mod_supported(struct drm_plane *plane,
  954. uint32_t format,
  955. uint64_t modifier)
  956. {
  957. /* This is the same as primary plane since SKL has universal planes */
  958. switch (format) {
  959. case DRM_FORMAT_XRGB8888:
  960. case DRM_FORMAT_XBGR8888:
  961. case DRM_FORMAT_ARGB8888:
  962. case DRM_FORMAT_ABGR8888:
  963. case DRM_FORMAT_RGB565:
  964. case DRM_FORMAT_XRGB2101010:
  965. case DRM_FORMAT_XBGR2101010:
  966. case DRM_FORMAT_YUYV:
  967. case DRM_FORMAT_YVYU:
  968. case DRM_FORMAT_UYVY:
  969. case DRM_FORMAT_VYUY:
  970. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  971. return true;
  972. /* fall through */
  973. case DRM_FORMAT_C8:
  974. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  975. modifier == I915_FORMAT_MOD_X_TILED ||
  976. modifier == I915_FORMAT_MOD_Y_TILED)
  977. return true;
  978. /* fall through */
  979. default:
  980. return false;
  981. }
  982. }
  983. static bool intel_sprite_plane_format_mod_supported(struct drm_plane *plane,
  984. uint32_t format,
  985. uint64_t modifier)
  986. {
  987. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  988. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  989. return false;
  990. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  991. modifier != DRM_FORMAT_MOD_LINEAR)
  992. return false;
  993. if (INTEL_GEN(dev_priv) >= 9)
  994. return skl_sprite_plane_format_mod_supported(plane, format, modifier);
  995. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  996. return vlv_sprite_plane_format_mod_supported(plane, format, modifier);
  997. else
  998. return g4x_sprite_plane_format_mod_supported(plane, format, modifier);
  999. unreachable();
  1000. }
  1001. static const struct drm_plane_funcs intel_sprite_plane_funcs = {
  1002. .update_plane = drm_atomic_helper_update_plane,
  1003. .disable_plane = drm_atomic_helper_disable_plane,
  1004. .destroy = intel_plane_destroy,
  1005. .atomic_get_property = intel_plane_atomic_get_property,
  1006. .atomic_set_property = intel_plane_atomic_set_property,
  1007. .atomic_duplicate_state = intel_plane_duplicate_state,
  1008. .atomic_destroy_state = intel_plane_destroy_state,
  1009. .format_mod_supported = intel_sprite_plane_format_mod_supported,
  1010. };
  1011. struct intel_plane *
  1012. intel_sprite_plane_create(struct drm_i915_private *dev_priv,
  1013. enum pipe pipe, int plane)
  1014. {
  1015. struct intel_plane *intel_plane = NULL;
  1016. struct intel_plane_state *state = NULL;
  1017. unsigned long possible_crtcs;
  1018. const uint32_t *plane_formats;
  1019. const uint64_t *modifiers;
  1020. unsigned int supported_rotations;
  1021. int num_plane_formats;
  1022. int ret;
  1023. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1024. if (!intel_plane) {
  1025. ret = -ENOMEM;
  1026. goto fail;
  1027. }
  1028. state = intel_create_plane_state(&intel_plane->base);
  1029. if (!state) {
  1030. ret = -ENOMEM;
  1031. goto fail;
  1032. }
  1033. intel_plane->base.state = &state->base;
  1034. if (INTEL_GEN(dev_priv) >= 10) {
  1035. intel_plane->can_scale = true;
  1036. state->scaler_id = -1;
  1037. intel_plane->update_plane = skl_update_plane;
  1038. intel_plane->disable_plane = skl_disable_plane;
  1039. plane_formats = skl_plane_formats;
  1040. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1041. modifiers = skl_plane_format_modifiers;
  1042. } else if (INTEL_GEN(dev_priv) >= 9) {
  1043. intel_plane->can_scale = true;
  1044. state->scaler_id = -1;
  1045. intel_plane->update_plane = skl_update_plane;
  1046. intel_plane->disable_plane = skl_disable_plane;
  1047. plane_formats = skl_plane_formats;
  1048. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1049. modifiers = skl_plane_format_modifiers;
  1050. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1051. intel_plane->can_scale = false;
  1052. intel_plane->max_downscale = 1;
  1053. intel_plane->update_plane = vlv_update_plane;
  1054. intel_plane->disable_plane = vlv_disable_plane;
  1055. plane_formats = vlv_plane_formats;
  1056. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1057. modifiers = i9xx_plane_format_modifiers;
  1058. } else if (INTEL_GEN(dev_priv) >= 7) {
  1059. if (IS_IVYBRIDGE(dev_priv)) {
  1060. intel_plane->can_scale = true;
  1061. intel_plane->max_downscale = 2;
  1062. } else {
  1063. intel_plane->can_scale = false;
  1064. intel_plane->max_downscale = 1;
  1065. }
  1066. intel_plane->update_plane = ivb_update_plane;
  1067. intel_plane->disable_plane = ivb_disable_plane;
  1068. plane_formats = snb_plane_formats;
  1069. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1070. modifiers = i9xx_plane_format_modifiers;
  1071. } else {
  1072. intel_plane->can_scale = true;
  1073. intel_plane->max_downscale = 16;
  1074. intel_plane->update_plane = g4x_update_plane;
  1075. intel_plane->disable_plane = g4x_disable_plane;
  1076. modifiers = i9xx_plane_format_modifiers;
  1077. if (IS_GEN6(dev_priv)) {
  1078. plane_formats = snb_plane_formats;
  1079. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1080. } else {
  1081. plane_formats = g4x_plane_formats;
  1082. num_plane_formats = ARRAY_SIZE(g4x_plane_formats);
  1083. }
  1084. }
  1085. if (INTEL_GEN(dev_priv) >= 9) {
  1086. supported_rotations =
  1087. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  1088. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  1089. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  1090. supported_rotations =
  1091. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  1092. DRM_MODE_REFLECT_X;
  1093. } else {
  1094. supported_rotations =
  1095. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  1096. }
  1097. intel_plane->pipe = pipe;
  1098. intel_plane->plane = plane;
  1099. intel_plane->id = PLANE_SPRITE0 + plane;
  1100. intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER_SPRITE(pipe, plane);
  1101. intel_plane->check_plane = intel_check_sprite_plane;
  1102. possible_crtcs = (1 << pipe);
  1103. if (INTEL_GEN(dev_priv) >= 9)
  1104. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1105. possible_crtcs, &intel_sprite_plane_funcs,
  1106. plane_formats, num_plane_formats,
  1107. modifiers,
  1108. DRM_PLANE_TYPE_OVERLAY,
  1109. "plane %d%c", plane + 2, pipe_name(pipe));
  1110. else
  1111. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1112. possible_crtcs, &intel_sprite_plane_funcs,
  1113. plane_formats, num_plane_formats,
  1114. modifiers,
  1115. DRM_PLANE_TYPE_OVERLAY,
  1116. "sprite %c", sprite_name(pipe, plane));
  1117. if (ret)
  1118. goto fail;
  1119. drm_plane_create_rotation_property(&intel_plane->base,
  1120. DRM_MODE_ROTATE_0,
  1121. supported_rotations);
  1122. drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
  1123. return intel_plane;
  1124. fail:
  1125. kfree(state);
  1126. kfree(intel_plane);
  1127. return ERR_PTR(ret);
  1128. }