intel_sprite.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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_crtc.h>
  34. #include <drm/drm_fourcc.h>
  35. #include <drm/drm_rect.h>
  36. #include "intel_drv.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. static int usecs_to_scanlines(const struct drm_display_mode *mode, int usecs)
  40. {
  41. /* paranoia */
  42. if (!mode->crtc_htotal)
  43. return 1;
  44. return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
  45. }
  46. static bool intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl_count)
  47. {
  48. struct drm_device *dev = crtc->base.dev;
  49. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  50. enum pipe pipe = crtc->pipe;
  51. long timeout = msecs_to_jiffies_timeout(1);
  52. int scanline, min, max, vblank_start;
  53. wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
  54. DEFINE_WAIT(wait);
  55. WARN_ON(!drm_modeset_is_locked(&crtc->base.mutex));
  56. vblank_start = mode->crtc_vblank_start;
  57. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  58. vblank_start = DIV_ROUND_UP(vblank_start, 2);
  59. /* FIXME needs to be calibrated sensibly */
  60. min = vblank_start - usecs_to_scanlines(mode, 100);
  61. max = vblank_start - 1;
  62. if (min <= 0 || max <= 0)
  63. return false;
  64. if (WARN_ON(drm_vblank_get(dev, pipe)))
  65. return false;
  66. local_irq_disable();
  67. trace_i915_pipe_update_start(crtc, min, max);
  68. for (;;) {
  69. /*
  70. * prepare_to_wait() has a memory barrier, which guarantees
  71. * other CPUs can see the task state update by the time we
  72. * read the scanline.
  73. */
  74. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  75. scanline = intel_get_crtc_scanline(crtc);
  76. if (scanline < min || scanline > max)
  77. break;
  78. if (timeout <= 0) {
  79. DRM_ERROR("Potential atomic update failure on pipe %c\n",
  80. pipe_name(crtc->pipe));
  81. break;
  82. }
  83. local_irq_enable();
  84. timeout = schedule_timeout(timeout);
  85. local_irq_disable();
  86. }
  87. finish_wait(wq, &wait);
  88. drm_vblank_put(dev, pipe);
  89. *start_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  90. trace_i915_pipe_update_vblank_evaded(crtc, min, max, *start_vbl_count);
  91. return true;
  92. }
  93. static void intel_pipe_update_end(struct intel_crtc *crtc, u32 start_vbl_count)
  94. {
  95. struct drm_device *dev = crtc->base.dev;
  96. enum pipe pipe = crtc->pipe;
  97. u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  98. trace_i915_pipe_update_end(crtc, end_vbl_count);
  99. local_irq_enable();
  100. if (start_vbl_count != end_vbl_count)
  101. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
  102. pipe_name(pipe), start_vbl_count, end_vbl_count);
  103. }
  104. static void intel_update_primary_plane(struct intel_crtc *crtc)
  105. {
  106. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  107. int reg = DSPCNTR(crtc->plane);
  108. if (crtc->primary_enabled)
  109. I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
  110. else
  111. I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
  112. }
  113. static void
  114. skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
  115. struct drm_framebuffer *fb,
  116. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  117. unsigned int crtc_w, unsigned int crtc_h,
  118. uint32_t x, uint32_t y,
  119. uint32_t src_w, uint32_t src_h)
  120. {
  121. struct drm_device *dev = drm_plane->dev;
  122. struct drm_i915_private *dev_priv = dev->dev_private;
  123. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  124. const int pipe = intel_plane->pipe;
  125. const int plane = intel_plane->plane + 1;
  126. u32 plane_ctl, stride;
  127. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  128. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  129. /* Mask out pixel format bits in case we change it */
  130. plane_ctl &= ~PLANE_CTL_FORMAT_MASK;
  131. plane_ctl &= ~PLANE_CTL_ORDER_RGBX;
  132. plane_ctl &= ~PLANE_CTL_YUV422_ORDER_MASK;
  133. plane_ctl &= ~PLANE_CTL_TILED_MASK;
  134. plane_ctl &= ~PLANE_CTL_ALPHA_MASK;
  135. /* Trickle feed has to be enabled */
  136. plane_ctl &= ~PLANE_CTL_TRICKLE_FEED_DISABLE;
  137. switch (fb->pixel_format) {
  138. case DRM_FORMAT_RGB565:
  139. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  140. break;
  141. case DRM_FORMAT_XBGR8888:
  142. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  143. break;
  144. case DRM_FORMAT_XRGB8888:
  145. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  146. break;
  147. /*
  148. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  149. * to be already pre-multiplied. We need to add a knob (or a different
  150. * DRM_FORMAT) for user-space to configure that.
  151. */
  152. case DRM_FORMAT_ABGR8888:
  153. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  154. PLANE_CTL_ORDER_RGBX |
  155. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  156. break;
  157. case DRM_FORMAT_ARGB8888:
  158. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  159. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  160. break;
  161. case DRM_FORMAT_YUYV:
  162. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  163. break;
  164. case DRM_FORMAT_YVYU:
  165. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  166. break;
  167. case DRM_FORMAT_UYVY:
  168. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  169. break;
  170. case DRM_FORMAT_VYUY:
  171. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  172. break;
  173. default:
  174. BUG();
  175. }
  176. switch (obj->tiling_mode) {
  177. case I915_TILING_NONE:
  178. stride = fb->pitches[0] >> 6;
  179. break;
  180. case I915_TILING_X:
  181. plane_ctl |= PLANE_CTL_TILED_X;
  182. stride = fb->pitches[0] >> 9;
  183. break;
  184. default:
  185. BUG();
  186. }
  187. plane_ctl |= PLANE_CTL_ENABLE;
  188. plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
  189. intel_update_sprite_watermarks(drm_plane, crtc, src_w, src_h,
  190. pixel_size, true,
  191. src_w != crtc_w || src_h != crtc_h);
  192. /* Sizes are 0 based */
  193. src_w--;
  194. src_h--;
  195. crtc_w--;
  196. crtc_h--;
  197. I915_WRITE(PLANE_OFFSET(pipe, plane), (y << 16) | x);
  198. I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
  199. I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
  200. I915_WRITE(PLANE_SIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  201. I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
  202. I915_WRITE(PLANE_SURF(pipe, plane), i915_gem_obj_ggtt_offset(obj));
  203. POSTING_READ(PLANE_SURF(pipe, plane));
  204. }
  205. static void
  206. skl_disable_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc)
  207. {
  208. struct drm_device *dev = drm_plane->dev;
  209. struct drm_i915_private *dev_priv = dev->dev_private;
  210. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  211. const int pipe = intel_plane->pipe;
  212. const int plane = intel_plane->plane + 1;
  213. I915_WRITE(PLANE_CTL(pipe, plane),
  214. I915_READ(PLANE_CTL(pipe, plane)) & ~PLANE_CTL_ENABLE);
  215. /* Activate double buffered register update */
  216. I915_WRITE(PLANE_CTL(pipe, plane), 0);
  217. POSTING_READ(PLANE_CTL(pipe, plane));
  218. intel_update_sprite_watermarks(drm_plane, crtc, 0, 0, 0, false, false);
  219. }
  220. static int
  221. skl_update_colorkey(struct drm_plane *drm_plane,
  222. struct drm_intel_sprite_colorkey *key)
  223. {
  224. struct drm_device *dev = drm_plane->dev;
  225. struct drm_i915_private *dev_priv = dev->dev_private;
  226. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  227. const int pipe = intel_plane->pipe;
  228. const int plane = intel_plane->plane;
  229. u32 plane_ctl;
  230. I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
  231. I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
  232. I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
  233. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  234. plane_ctl &= ~PLANE_CTL_KEY_ENABLE_MASK;
  235. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  236. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  237. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  238. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  239. I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
  240. POSTING_READ(PLANE_CTL(pipe, plane));
  241. return 0;
  242. }
  243. static void
  244. skl_get_colorkey(struct drm_plane *drm_plane,
  245. struct drm_intel_sprite_colorkey *key)
  246. {
  247. struct drm_device *dev = drm_plane->dev;
  248. struct drm_i915_private *dev_priv = dev->dev_private;
  249. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  250. const int pipe = intel_plane->pipe;
  251. const int plane = intel_plane->plane;
  252. u32 plane_ctl;
  253. key->min_value = I915_READ(PLANE_KEYVAL(pipe, plane));
  254. key->max_value = I915_READ(PLANE_KEYMAX(pipe, plane));
  255. key->channel_mask = I915_READ(PLANE_KEYMSK(pipe, plane));
  256. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  257. switch (plane_ctl & PLANE_CTL_KEY_ENABLE_MASK) {
  258. case PLANE_CTL_KEY_ENABLE_DESTINATION:
  259. key->flags = I915_SET_COLORKEY_DESTINATION;
  260. break;
  261. case PLANE_CTL_KEY_ENABLE_SOURCE:
  262. key->flags = I915_SET_COLORKEY_SOURCE;
  263. break;
  264. default:
  265. key->flags = I915_SET_COLORKEY_NONE;
  266. }
  267. }
  268. static void
  269. vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
  270. struct drm_framebuffer *fb,
  271. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  272. unsigned int crtc_w, unsigned int crtc_h,
  273. uint32_t x, uint32_t y,
  274. uint32_t src_w, uint32_t src_h)
  275. {
  276. struct drm_device *dev = dplane->dev;
  277. struct drm_i915_private *dev_priv = dev->dev_private;
  278. struct intel_plane *intel_plane = to_intel_plane(dplane);
  279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  280. int pipe = intel_plane->pipe;
  281. int plane = intel_plane->plane;
  282. u32 sprctl;
  283. unsigned long sprsurf_offset, linear_offset;
  284. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  285. u32 start_vbl_count;
  286. bool atomic_update;
  287. sprctl = I915_READ(SPCNTR(pipe, plane));
  288. /* Mask out pixel format bits in case we change it */
  289. sprctl &= ~SP_PIXFORMAT_MASK;
  290. sprctl &= ~SP_YUV_BYTE_ORDER_MASK;
  291. sprctl &= ~SP_TILED;
  292. sprctl &= ~SP_ROTATE_180;
  293. switch (fb->pixel_format) {
  294. case DRM_FORMAT_YUYV:
  295. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  296. break;
  297. case DRM_FORMAT_YVYU:
  298. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  299. break;
  300. case DRM_FORMAT_UYVY:
  301. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  302. break;
  303. case DRM_FORMAT_VYUY:
  304. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  305. break;
  306. case DRM_FORMAT_RGB565:
  307. sprctl |= SP_FORMAT_BGR565;
  308. break;
  309. case DRM_FORMAT_XRGB8888:
  310. sprctl |= SP_FORMAT_BGRX8888;
  311. break;
  312. case DRM_FORMAT_ARGB8888:
  313. sprctl |= SP_FORMAT_BGRA8888;
  314. break;
  315. case DRM_FORMAT_XBGR2101010:
  316. sprctl |= SP_FORMAT_RGBX1010102;
  317. break;
  318. case DRM_FORMAT_ABGR2101010:
  319. sprctl |= SP_FORMAT_RGBA1010102;
  320. break;
  321. case DRM_FORMAT_XBGR8888:
  322. sprctl |= SP_FORMAT_RGBX8888;
  323. break;
  324. case DRM_FORMAT_ABGR8888:
  325. sprctl |= SP_FORMAT_RGBA8888;
  326. break;
  327. default:
  328. /*
  329. * If we get here one of the upper layers failed to filter
  330. * out the unsupported plane formats
  331. */
  332. BUG();
  333. break;
  334. }
  335. /*
  336. * Enable gamma to match primary/cursor plane behaviour.
  337. * FIXME should be user controllable via propertiesa.
  338. */
  339. sprctl |= SP_GAMMA_ENABLE;
  340. if (obj->tiling_mode != I915_TILING_NONE)
  341. sprctl |= SP_TILED;
  342. sprctl |= SP_ENABLE;
  343. intel_update_sprite_watermarks(dplane, crtc, src_w, src_h,
  344. pixel_size, true,
  345. src_w != crtc_w || src_h != crtc_h);
  346. /* Sizes are 0 based */
  347. src_w--;
  348. src_h--;
  349. crtc_w--;
  350. crtc_h--;
  351. linear_offset = y * fb->pitches[0] + x * pixel_size;
  352. sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
  353. obj->tiling_mode,
  354. pixel_size,
  355. fb->pitches[0]);
  356. linear_offset -= sprsurf_offset;
  357. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  358. sprctl |= SP_ROTATE_180;
  359. x += src_w;
  360. y += src_h;
  361. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  362. }
  363. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  364. intel_update_primary_plane(intel_crtc);
  365. I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
  366. I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
  367. if (obj->tiling_mode != I915_TILING_NONE)
  368. I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
  369. else
  370. I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
  371. I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  372. I915_WRITE(SPCNTR(pipe, plane), sprctl);
  373. I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
  374. sprsurf_offset);
  375. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  376. if (atomic_update)
  377. intel_pipe_update_end(intel_crtc, start_vbl_count);
  378. }
  379. static void
  380. vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
  381. {
  382. struct drm_device *dev = dplane->dev;
  383. struct drm_i915_private *dev_priv = dev->dev_private;
  384. struct intel_plane *intel_plane = to_intel_plane(dplane);
  385. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  386. int pipe = intel_plane->pipe;
  387. int plane = intel_plane->plane;
  388. u32 start_vbl_count;
  389. bool atomic_update;
  390. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  391. intel_update_primary_plane(intel_crtc);
  392. I915_WRITE(SPCNTR(pipe, plane), I915_READ(SPCNTR(pipe, plane)) &
  393. ~SP_ENABLE);
  394. /* Activate double buffered register update */
  395. I915_WRITE(SPSURF(pipe, plane), 0);
  396. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  397. if (atomic_update)
  398. intel_pipe_update_end(intel_crtc, start_vbl_count);
  399. intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
  400. }
  401. static int
  402. vlv_update_colorkey(struct drm_plane *dplane,
  403. struct drm_intel_sprite_colorkey *key)
  404. {
  405. struct drm_device *dev = dplane->dev;
  406. struct drm_i915_private *dev_priv = dev->dev_private;
  407. struct intel_plane *intel_plane = to_intel_plane(dplane);
  408. int pipe = intel_plane->pipe;
  409. int plane = intel_plane->plane;
  410. u32 sprctl;
  411. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  412. return -EINVAL;
  413. I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
  414. I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
  415. I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
  416. sprctl = I915_READ(SPCNTR(pipe, plane));
  417. sprctl &= ~SP_SOURCE_KEY;
  418. if (key->flags & I915_SET_COLORKEY_SOURCE)
  419. sprctl |= SP_SOURCE_KEY;
  420. I915_WRITE(SPCNTR(pipe, plane), sprctl);
  421. POSTING_READ(SPKEYMSK(pipe, plane));
  422. return 0;
  423. }
  424. static void
  425. vlv_get_colorkey(struct drm_plane *dplane,
  426. struct drm_intel_sprite_colorkey *key)
  427. {
  428. struct drm_device *dev = dplane->dev;
  429. struct drm_i915_private *dev_priv = dev->dev_private;
  430. struct intel_plane *intel_plane = to_intel_plane(dplane);
  431. int pipe = intel_plane->pipe;
  432. int plane = intel_plane->plane;
  433. u32 sprctl;
  434. key->min_value = I915_READ(SPKEYMINVAL(pipe, plane));
  435. key->max_value = I915_READ(SPKEYMAXVAL(pipe, plane));
  436. key->channel_mask = I915_READ(SPKEYMSK(pipe, plane));
  437. sprctl = I915_READ(SPCNTR(pipe, plane));
  438. if (sprctl & SP_SOURCE_KEY)
  439. key->flags = I915_SET_COLORKEY_SOURCE;
  440. else
  441. key->flags = I915_SET_COLORKEY_NONE;
  442. }
  443. static void
  444. ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  445. struct drm_framebuffer *fb,
  446. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  447. unsigned int crtc_w, unsigned int crtc_h,
  448. uint32_t x, uint32_t y,
  449. uint32_t src_w, uint32_t src_h)
  450. {
  451. struct drm_device *dev = plane->dev;
  452. struct drm_i915_private *dev_priv = dev->dev_private;
  453. struct intel_plane *intel_plane = to_intel_plane(plane);
  454. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  455. int pipe = intel_plane->pipe;
  456. u32 sprctl, sprscale = 0;
  457. unsigned long sprsurf_offset, linear_offset;
  458. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  459. u32 start_vbl_count;
  460. bool atomic_update;
  461. sprctl = I915_READ(SPRCTL(pipe));
  462. /* Mask out pixel format bits in case we change it */
  463. sprctl &= ~SPRITE_PIXFORMAT_MASK;
  464. sprctl &= ~SPRITE_RGB_ORDER_RGBX;
  465. sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
  466. sprctl &= ~SPRITE_TILED;
  467. sprctl &= ~SPRITE_ROTATE_180;
  468. switch (fb->pixel_format) {
  469. case DRM_FORMAT_XBGR8888:
  470. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  471. break;
  472. case DRM_FORMAT_XRGB8888:
  473. sprctl |= SPRITE_FORMAT_RGBX888;
  474. break;
  475. case DRM_FORMAT_YUYV:
  476. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  477. break;
  478. case DRM_FORMAT_YVYU:
  479. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  480. break;
  481. case DRM_FORMAT_UYVY:
  482. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  483. break;
  484. case DRM_FORMAT_VYUY:
  485. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  486. break;
  487. default:
  488. BUG();
  489. }
  490. /*
  491. * Enable gamma to match primary/cursor plane behaviour.
  492. * FIXME should be user controllable via propertiesa.
  493. */
  494. sprctl |= SPRITE_GAMMA_ENABLE;
  495. if (obj->tiling_mode != I915_TILING_NONE)
  496. sprctl |= SPRITE_TILED;
  497. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  498. sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
  499. else
  500. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  501. sprctl |= SPRITE_ENABLE;
  502. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  503. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  504. intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
  505. true,
  506. src_w != crtc_w || src_h != crtc_h);
  507. /* Sizes are 0 based */
  508. src_w--;
  509. src_h--;
  510. crtc_w--;
  511. crtc_h--;
  512. if (crtc_w != src_w || crtc_h != src_h)
  513. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  514. linear_offset = y * fb->pitches[0] + x * pixel_size;
  515. sprsurf_offset =
  516. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  517. pixel_size, fb->pitches[0]);
  518. linear_offset -= sprsurf_offset;
  519. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  520. sprctl |= SPRITE_ROTATE_180;
  521. /* HSW and BDW does this automagically in hardware */
  522. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  523. x += src_w;
  524. y += src_h;
  525. linear_offset += src_h * fb->pitches[0] +
  526. src_w * pixel_size;
  527. }
  528. }
  529. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  530. intel_update_primary_plane(intel_crtc);
  531. I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
  532. I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  533. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  534. * register */
  535. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  536. I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
  537. else if (obj->tiling_mode != I915_TILING_NONE)
  538. I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
  539. else
  540. I915_WRITE(SPRLINOFF(pipe), linear_offset);
  541. I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  542. if (intel_plane->can_scale)
  543. I915_WRITE(SPRSCALE(pipe), sprscale);
  544. I915_WRITE(SPRCTL(pipe), sprctl);
  545. I915_WRITE(SPRSURF(pipe),
  546. i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
  547. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  548. if (atomic_update)
  549. intel_pipe_update_end(intel_crtc, start_vbl_count);
  550. }
  551. static void
  552. ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  553. {
  554. struct drm_device *dev = plane->dev;
  555. struct drm_i915_private *dev_priv = dev->dev_private;
  556. struct intel_plane *intel_plane = to_intel_plane(plane);
  557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  558. int pipe = intel_plane->pipe;
  559. u32 start_vbl_count;
  560. bool atomic_update;
  561. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  562. intel_update_primary_plane(intel_crtc);
  563. I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
  564. /* Can't leave the scaler enabled... */
  565. if (intel_plane->can_scale)
  566. I915_WRITE(SPRSCALE(pipe), 0);
  567. /* Activate double buffered register update */
  568. I915_WRITE(SPRSURF(pipe), 0);
  569. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  570. if (atomic_update)
  571. intel_pipe_update_end(intel_crtc, start_vbl_count);
  572. /*
  573. * Avoid underruns when disabling the sprite.
  574. * FIXME remove once watermark updates are done properly.
  575. */
  576. intel_wait_for_vblank(dev, pipe);
  577. intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
  578. }
  579. static int
  580. ivb_update_colorkey(struct drm_plane *plane,
  581. struct drm_intel_sprite_colorkey *key)
  582. {
  583. struct drm_device *dev = plane->dev;
  584. struct drm_i915_private *dev_priv = dev->dev_private;
  585. struct intel_plane *intel_plane;
  586. u32 sprctl;
  587. int ret = 0;
  588. intel_plane = to_intel_plane(plane);
  589. I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
  590. I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
  591. I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
  592. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  593. sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
  594. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  595. sprctl |= SPRITE_DEST_KEY;
  596. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  597. sprctl |= SPRITE_SOURCE_KEY;
  598. I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
  599. POSTING_READ(SPRKEYMSK(intel_plane->pipe));
  600. return ret;
  601. }
  602. static void
  603. ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  604. {
  605. struct drm_device *dev = plane->dev;
  606. struct drm_i915_private *dev_priv = dev->dev_private;
  607. struct intel_plane *intel_plane;
  608. u32 sprctl;
  609. intel_plane = to_intel_plane(plane);
  610. key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
  611. key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
  612. key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
  613. key->flags = 0;
  614. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  615. if (sprctl & SPRITE_DEST_KEY)
  616. key->flags = I915_SET_COLORKEY_DESTINATION;
  617. else if (sprctl & SPRITE_SOURCE_KEY)
  618. key->flags = I915_SET_COLORKEY_SOURCE;
  619. else
  620. key->flags = I915_SET_COLORKEY_NONE;
  621. }
  622. static void
  623. ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  624. struct drm_framebuffer *fb,
  625. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  626. unsigned int crtc_w, unsigned int crtc_h,
  627. uint32_t x, uint32_t y,
  628. uint32_t src_w, uint32_t src_h)
  629. {
  630. struct drm_device *dev = plane->dev;
  631. struct drm_i915_private *dev_priv = dev->dev_private;
  632. struct intel_plane *intel_plane = to_intel_plane(plane);
  633. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  634. int pipe = intel_plane->pipe;
  635. unsigned long dvssurf_offset, linear_offset;
  636. u32 dvscntr, dvsscale;
  637. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  638. u32 start_vbl_count;
  639. bool atomic_update;
  640. dvscntr = I915_READ(DVSCNTR(pipe));
  641. /* Mask out pixel format bits in case we change it */
  642. dvscntr &= ~DVS_PIXFORMAT_MASK;
  643. dvscntr &= ~DVS_RGB_ORDER_XBGR;
  644. dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
  645. dvscntr &= ~DVS_TILED;
  646. dvscntr &= ~DVS_ROTATE_180;
  647. switch (fb->pixel_format) {
  648. case DRM_FORMAT_XBGR8888:
  649. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  650. break;
  651. case DRM_FORMAT_XRGB8888:
  652. dvscntr |= DVS_FORMAT_RGBX888;
  653. break;
  654. case DRM_FORMAT_YUYV:
  655. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  656. break;
  657. case DRM_FORMAT_YVYU:
  658. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  659. break;
  660. case DRM_FORMAT_UYVY:
  661. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  662. break;
  663. case DRM_FORMAT_VYUY:
  664. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  665. break;
  666. default:
  667. BUG();
  668. }
  669. /*
  670. * Enable gamma to match primary/cursor plane behaviour.
  671. * FIXME should be user controllable via propertiesa.
  672. */
  673. dvscntr |= DVS_GAMMA_ENABLE;
  674. if (obj->tiling_mode != I915_TILING_NONE)
  675. dvscntr |= DVS_TILED;
  676. if (IS_GEN6(dev))
  677. dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
  678. dvscntr |= DVS_ENABLE;
  679. intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
  680. pixel_size, true,
  681. src_w != crtc_w || src_h != crtc_h);
  682. /* Sizes are 0 based */
  683. src_w--;
  684. src_h--;
  685. crtc_w--;
  686. crtc_h--;
  687. dvsscale = 0;
  688. if (crtc_w != src_w || crtc_h != src_h)
  689. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  690. linear_offset = y * fb->pitches[0] + x * pixel_size;
  691. dvssurf_offset =
  692. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  693. pixel_size, fb->pitches[0]);
  694. linear_offset -= dvssurf_offset;
  695. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  696. dvscntr |= DVS_ROTATE_180;
  697. x += src_w;
  698. y += src_h;
  699. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  700. }
  701. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  702. intel_update_primary_plane(intel_crtc);
  703. I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
  704. I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  705. if (obj->tiling_mode != I915_TILING_NONE)
  706. I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
  707. else
  708. I915_WRITE(DVSLINOFF(pipe), linear_offset);
  709. I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  710. I915_WRITE(DVSSCALE(pipe), dvsscale);
  711. I915_WRITE(DVSCNTR(pipe), dvscntr);
  712. I915_WRITE(DVSSURF(pipe),
  713. i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
  714. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  715. if (atomic_update)
  716. intel_pipe_update_end(intel_crtc, start_vbl_count);
  717. }
  718. static void
  719. ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  720. {
  721. struct drm_device *dev = plane->dev;
  722. struct drm_i915_private *dev_priv = dev->dev_private;
  723. struct intel_plane *intel_plane = to_intel_plane(plane);
  724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  725. int pipe = intel_plane->pipe;
  726. u32 start_vbl_count;
  727. bool atomic_update;
  728. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  729. intel_update_primary_plane(intel_crtc);
  730. I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
  731. /* Disable the scaler */
  732. I915_WRITE(DVSSCALE(pipe), 0);
  733. /* Flush double buffered register updates */
  734. I915_WRITE(DVSSURF(pipe), 0);
  735. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  736. if (atomic_update)
  737. intel_pipe_update_end(intel_crtc, start_vbl_count);
  738. /*
  739. * Avoid underruns when disabling the sprite.
  740. * FIXME remove once watermark updates are done properly.
  741. */
  742. intel_wait_for_vblank(dev, pipe);
  743. intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
  744. }
  745. static void
  746. intel_post_enable_primary(struct drm_crtc *crtc)
  747. {
  748. struct drm_device *dev = crtc->dev;
  749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  750. /*
  751. * BDW signals flip done immediately if the plane
  752. * is disabled, even if the plane enable is already
  753. * armed to occur at the next vblank :(
  754. */
  755. if (IS_BROADWELL(dev))
  756. intel_wait_for_vblank(dev, intel_crtc->pipe);
  757. /*
  758. * FIXME IPS should be fine as long as one plane is
  759. * enabled, but in practice it seems to have problems
  760. * when going from primary only to sprite only and vice
  761. * versa.
  762. */
  763. hsw_enable_ips(intel_crtc);
  764. mutex_lock(&dev->struct_mutex);
  765. intel_update_fbc(dev);
  766. mutex_unlock(&dev->struct_mutex);
  767. }
  768. static void
  769. intel_pre_disable_primary(struct drm_crtc *crtc)
  770. {
  771. struct drm_device *dev = crtc->dev;
  772. struct drm_i915_private *dev_priv = dev->dev_private;
  773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  774. mutex_lock(&dev->struct_mutex);
  775. if (dev_priv->fbc.plane == intel_crtc->plane)
  776. intel_disable_fbc(dev);
  777. mutex_unlock(&dev->struct_mutex);
  778. /*
  779. * FIXME IPS should be fine as long as one plane is
  780. * enabled, but in practice it seems to have problems
  781. * when going from primary only to sprite only and vice
  782. * versa.
  783. */
  784. hsw_disable_ips(intel_crtc);
  785. }
  786. static int
  787. ilk_update_colorkey(struct drm_plane *plane,
  788. struct drm_intel_sprite_colorkey *key)
  789. {
  790. struct drm_device *dev = plane->dev;
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. struct intel_plane *intel_plane;
  793. u32 dvscntr;
  794. int ret = 0;
  795. intel_plane = to_intel_plane(plane);
  796. I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
  797. I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
  798. I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
  799. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  800. dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
  801. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  802. dvscntr |= DVS_DEST_KEY;
  803. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  804. dvscntr |= DVS_SOURCE_KEY;
  805. I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
  806. POSTING_READ(DVSKEYMSK(intel_plane->pipe));
  807. return ret;
  808. }
  809. static void
  810. ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  811. {
  812. struct drm_device *dev = plane->dev;
  813. struct drm_i915_private *dev_priv = dev->dev_private;
  814. struct intel_plane *intel_plane;
  815. u32 dvscntr;
  816. intel_plane = to_intel_plane(plane);
  817. key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
  818. key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
  819. key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
  820. key->flags = 0;
  821. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  822. if (dvscntr & DVS_DEST_KEY)
  823. key->flags = I915_SET_COLORKEY_DESTINATION;
  824. else if (dvscntr & DVS_SOURCE_KEY)
  825. key->flags = I915_SET_COLORKEY_SOURCE;
  826. else
  827. key->flags = I915_SET_COLORKEY_NONE;
  828. }
  829. static bool
  830. format_is_yuv(uint32_t format)
  831. {
  832. switch (format) {
  833. case DRM_FORMAT_YUYV:
  834. case DRM_FORMAT_UYVY:
  835. case DRM_FORMAT_VYUY:
  836. case DRM_FORMAT_YVYU:
  837. return true;
  838. default:
  839. return false;
  840. }
  841. }
  842. static bool colorkey_enabled(struct intel_plane *intel_plane)
  843. {
  844. struct drm_intel_sprite_colorkey key;
  845. intel_plane->get_colorkey(&intel_plane->base, &key);
  846. return key.flags != I915_SET_COLORKEY_NONE;
  847. }
  848. static int
  849. intel_check_sprite_plane(struct drm_plane *plane,
  850. struct intel_plane_state *state)
  851. {
  852. struct intel_crtc *intel_crtc = to_intel_crtc(state->crtc);
  853. struct intel_plane *intel_plane = to_intel_plane(plane);
  854. struct drm_framebuffer *fb = state->fb;
  855. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  856. struct drm_i915_gem_object *obj = intel_fb->obj;
  857. int crtc_x, crtc_y;
  858. unsigned int crtc_w, crtc_h;
  859. uint32_t src_x, src_y, src_w, src_h;
  860. struct drm_rect *src = &state->src;
  861. struct drm_rect *dst = &state->dst;
  862. struct drm_rect *orig_src = &state->orig_src;
  863. const struct drm_rect *clip = &state->clip;
  864. int hscale, vscale;
  865. int max_scale, min_scale;
  866. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  867. /* Don't modify another pipe's plane */
  868. if (intel_plane->pipe != intel_crtc->pipe) {
  869. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  870. return -EINVAL;
  871. }
  872. /* FIXME check all gen limits */
  873. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
  874. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  875. return -EINVAL;
  876. }
  877. /* Sprite planes can be linear or x-tiled surfaces */
  878. switch (obj->tiling_mode) {
  879. case I915_TILING_NONE:
  880. case I915_TILING_X:
  881. break;
  882. default:
  883. DRM_DEBUG_KMS("Unsupported tiling mode\n");
  884. return -EINVAL;
  885. }
  886. /*
  887. * FIXME the following code does a bunch of fuzzy adjustments to the
  888. * coordinates and sizes. We probably need some way to decide whether
  889. * more strict checking should be done instead.
  890. */
  891. max_scale = intel_plane->max_downscale << 16;
  892. min_scale = intel_plane->can_scale ? 1 : (1 << 16);
  893. drm_rect_rotate(src, fb->width << 16, fb->height << 16,
  894. intel_plane->rotation);
  895. hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
  896. BUG_ON(hscale < 0);
  897. vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
  898. BUG_ON(vscale < 0);
  899. state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
  900. crtc_x = dst->x1;
  901. crtc_y = dst->y1;
  902. crtc_w = drm_rect_width(dst);
  903. crtc_h = drm_rect_height(dst);
  904. if (state->visible) {
  905. /* check again in case clipping clamped the results */
  906. hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
  907. if (hscale < 0) {
  908. DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
  909. drm_rect_debug_print(src, true);
  910. drm_rect_debug_print(dst, false);
  911. return hscale;
  912. }
  913. vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
  914. if (vscale < 0) {
  915. DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
  916. drm_rect_debug_print(src, true);
  917. drm_rect_debug_print(dst, false);
  918. return vscale;
  919. }
  920. /* Make the source viewport size an exact multiple of the scaling factors. */
  921. drm_rect_adjust_size(src,
  922. drm_rect_width(dst) * hscale - drm_rect_width(src),
  923. drm_rect_height(dst) * vscale - drm_rect_height(src));
  924. drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
  925. intel_plane->rotation);
  926. /* sanity check to make sure the src viewport wasn't enlarged */
  927. WARN_ON(src->x1 < (int) orig_src->x1 ||
  928. src->y1 < (int) orig_src->y1 ||
  929. src->x2 > (int) orig_src->x2 ||
  930. src->y2 > (int) orig_src->y2);
  931. /*
  932. * Hardware doesn't handle subpixel coordinates.
  933. * Adjust to (macro)pixel boundary, but be careful not to
  934. * increase the source viewport size, because that could
  935. * push the downscaling factor out of bounds.
  936. */
  937. src_x = src->x1 >> 16;
  938. src_w = drm_rect_width(src) >> 16;
  939. src_y = src->y1 >> 16;
  940. src_h = drm_rect_height(src) >> 16;
  941. if (format_is_yuv(fb->pixel_format)) {
  942. src_x &= ~1;
  943. src_w &= ~1;
  944. /*
  945. * Must keep src and dst the
  946. * same if we can't scale.
  947. */
  948. if (!intel_plane->can_scale)
  949. crtc_w &= ~1;
  950. if (crtc_w == 0)
  951. state->visible = false;
  952. }
  953. }
  954. /* Check size restrictions when scaling */
  955. if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
  956. unsigned int width_bytes;
  957. WARN_ON(!intel_plane->can_scale);
  958. /* FIXME interlacing min height is 6 */
  959. if (crtc_w < 3 || crtc_h < 3)
  960. state->visible = false;
  961. if (src_w < 3 || src_h < 3)
  962. state->visible = false;
  963. width_bytes = ((src_x * pixel_size) & 63) +
  964. src_w * pixel_size;
  965. if (src_w > 2048 || src_h > 2048 ||
  966. width_bytes > 4096 || fb->pitches[0] > 4096) {
  967. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  968. return -EINVAL;
  969. }
  970. }
  971. if (state->visible) {
  972. src->x1 = src_x;
  973. src->x2 = src_x + src_w;
  974. src->y1 = src_y;
  975. src->y2 = src_y + src_h;
  976. }
  977. dst->x1 = crtc_x;
  978. dst->x2 = crtc_x + crtc_w;
  979. dst->y1 = crtc_y;
  980. dst->y2 = crtc_y + crtc_h;
  981. return 0;
  982. }
  983. static int
  984. intel_commit_sprite_plane(struct drm_plane *plane,
  985. struct intel_plane_state *state)
  986. {
  987. struct drm_device *dev = plane->dev;
  988. struct drm_crtc *crtc = state->crtc;
  989. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  990. struct intel_plane *intel_plane = to_intel_plane(plane);
  991. enum pipe pipe = intel_crtc->pipe;
  992. struct drm_framebuffer *fb = state->fb;
  993. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  994. struct drm_i915_gem_object *obj = intel_fb->obj;
  995. struct drm_i915_gem_object *old_obj = intel_plane->obj;
  996. int crtc_x, crtc_y;
  997. unsigned int crtc_w, crtc_h;
  998. uint32_t src_x, src_y, src_w, src_h;
  999. struct drm_rect *dst = &state->dst;
  1000. const struct drm_rect *clip = &state->clip;
  1001. bool primary_enabled;
  1002. int ret;
  1003. /*
  1004. * If the sprite is completely covering the primary plane,
  1005. * we can disable the primary and save power.
  1006. */
  1007. primary_enabled = !drm_rect_equals(dst, clip) || colorkey_enabled(intel_plane);
  1008. WARN_ON(!primary_enabled && !state->visible && intel_crtc->active);
  1009. if (old_obj != obj) {
  1010. mutex_lock(&dev->struct_mutex);
  1011. /* Note that this will apply the VT-d workaround for scanouts,
  1012. * which is more restrictive than required for sprites. (The
  1013. * primary plane requires 256KiB alignment with 64 PTE padding,
  1014. * the sprite planes only require 128KiB alignment and 32 PTE
  1015. * padding.
  1016. */
  1017. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  1018. if (ret == 0)
  1019. i915_gem_track_fb(old_obj, obj,
  1020. INTEL_FRONTBUFFER_SPRITE(pipe));
  1021. mutex_unlock(&dev->struct_mutex);
  1022. if (ret)
  1023. return ret;
  1024. }
  1025. intel_plane->crtc_x = state->orig_dst.x1;
  1026. intel_plane->crtc_y = state->orig_dst.y1;
  1027. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  1028. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  1029. intel_plane->src_x = state->orig_src.x1;
  1030. intel_plane->src_y = state->orig_src.y1;
  1031. intel_plane->src_w = drm_rect_width(&state->orig_src);
  1032. intel_plane->src_h = drm_rect_height(&state->orig_src);
  1033. intel_plane->obj = obj;
  1034. if (intel_crtc->active) {
  1035. bool primary_was_enabled = intel_crtc->primary_enabled;
  1036. intel_crtc->primary_enabled = primary_enabled;
  1037. if (primary_was_enabled != primary_enabled)
  1038. intel_crtc_wait_for_pending_flips(crtc);
  1039. if (primary_was_enabled && !primary_enabled)
  1040. intel_pre_disable_primary(crtc);
  1041. if (state->visible) {
  1042. crtc_x = state->dst.x1;
  1043. crtc_y = state->dst.y1;
  1044. crtc_w = drm_rect_width(&state->dst);
  1045. crtc_h = drm_rect_height(&state->dst);
  1046. src_x = state->src.x1;
  1047. src_y = state->src.y1;
  1048. src_w = drm_rect_width(&state->src);
  1049. src_h = drm_rect_height(&state->src);
  1050. intel_plane->update_plane(plane, crtc, fb, obj,
  1051. crtc_x, crtc_y, crtc_w, crtc_h,
  1052. src_x, src_y, src_w, src_h);
  1053. } else {
  1054. intel_plane->disable_plane(plane, crtc);
  1055. }
  1056. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_SPRITE(pipe));
  1057. if (!primary_was_enabled && primary_enabled)
  1058. intel_post_enable_primary(crtc);
  1059. }
  1060. /* Unpin old obj after new one is active to avoid ugliness */
  1061. if (old_obj && old_obj != obj) {
  1062. /*
  1063. * It's fairly common to simply update the position of
  1064. * an existing object. In that case, we don't need to
  1065. * wait for vblank to avoid ugliness, we only need to
  1066. * do the pin & ref bookkeeping.
  1067. */
  1068. if (intel_crtc->active)
  1069. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1070. mutex_lock(&dev->struct_mutex);
  1071. intel_unpin_fb_obj(old_obj);
  1072. mutex_unlock(&dev->struct_mutex);
  1073. }
  1074. return 0;
  1075. }
  1076. static int
  1077. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  1078. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  1079. unsigned int crtc_w, unsigned int crtc_h,
  1080. uint32_t src_x, uint32_t src_y,
  1081. uint32_t src_w, uint32_t src_h)
  1082. {
  1083. struct intel_plane_state state;
  1084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1085. int ret;
  1086. state.crtc = crtc;
  1087. state.fb = fb;
  1088. /* sample coordinates in 16.16 fixed point */
  1089. state.src.x1 = src_x;
  1090. state.src.x2 = src_x + src_w;
  1091. state.src.y1 = src_y;
  1092. state.src.y2 = src_y + src_h;
  1093. /* integer pixels */
  1094. state.dst.x1 = crtc_x;
  1095. state.dst.x2 = crtc_x + crtc_w;
  1096. state.dst.y1 = crtc_y;
  1097. state.dst.y2 = crtc_y + crtc_h;
  1098. state.clip.x1 = 0;
  1099. state.clip.y1 = 0;
  1100. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  1101. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  1102. state.orig_src = state.src;
  1103. state.orig_dst = state.dst;
  1104. ret = intel_check_sprite_plane(plane, &state);
  1105. if (ret)
  1106. return ret;
  1107. return intel_commit_sprite_plane(plane, &state);
  1108. }
  1109. static int
  1110. intel_disable_plane(struct drm_plane *plane)
  1111. {
  1112. struct drm_device *dev = plane->dev;
  1113. struct intel_plane *intel_plane = to_intel_plane(plane);
  1114. struct intel_crtc *intel_crtc;
  1115. enum pipe pipe;
  1116. if (!plane->fb)
  1117. return 0;
  1118. if (WARN_ON(!plane->crtc))
  1119. return -EINVAL;
  1120. intel_crtc = to_intel_crtc(plane->crtc);
  1121. pipe = intel_crtc->pipe;
  1122. if (intel_crtc->active) {
  1123. bool primary_was_enabled = intel_crtc->primary_enabled;
  1124. intel_crtc->primary_enabled = true;
  1125. intel_plane->disable_plane(plane, plane->crtc);
  1126. if (!primary_was_enabled && intel_crtc->primary_enabled)
  1127. intel_post_enable_primary(plane->crtc);
  1128. }
  1129. if (intel_plane->obj) {
  1130. if (intel_crtc->active)
  1131. intel_wait_for_vblank(dev, intel_plane->pipe);
  1132. mutex_lock(&dev->struct_mutex);
  1133. intel_unpin_fb_obj(intel_plane->obj);
  1134. i915_gem_track_fb(intel_plane->obj, NULL,
  1135. INTEL_FRONTBUFFER_SPRITE(pipe));
  1136. mutex_unlock(&dev->struct_mutex);
  1137. intel_plane->obj = NULL;
  1138. }
  1139. return 0;
  1140. }
  1141. static void intel_destroy_plane(struct drm_plane *plane)
  1142. {
  1143. struct intel_plane *intel_plane = to_intel_plane(plane);
  1144. intel_disable_plane(plane);
  1145. drm_plane_cleanup(plane);
  1146. kfree(intel_plane);
  1147. }
  1148. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  1149. struct drm_file *file_priv)
  1150. {
  1151. struct drm_intel_sprite_colorkey *set = data;
  1152. struct drm_plane *plane;
  1153. struct intel_plane *intel_plane;
  1154. int ret = 0;
  1155. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1156. return -ENODEV;
  1157. /* Make sure we don't try to enable both src & dest simultaneously */
  1158. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  1159. return -EINVAL;
  1160. drm_modeset_lock_all(dev);
  1161. plane = drm_plane_find(dev, set->plane_id);
  1162. if (!plane) {
  1163. ret = -ENOENT;
  1164. goto out_unlock;
  1165. }
  1166. intel_plane = to_intel_plane(plane);
  1167. ret = intel_plane->update_colorkey(plane, set);
  1168. out_unlock:
  1169. drm_modeset_unlock_all(dev);
  1170. return ret;
  1171. }
  1172. int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
  1173. struct drm_file *file_priv)
  1174. {
  1175. struct drm_intel_sprite_colorkey *get = data;
  1176. struct drm_plane *plane;
  1177. struct intel_plane *intel_plane;
  1178. int ret = 0;
  1179. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1180. return -ENODEV;
  1181. drm_modeset_lock_all(dev);
  1182. plane = drm_plane_find(dev, get->plane_id);
  1183. if (!plane) {
  1184. ret = -ENOENT;
  1185. goto out_unlock;
  1186. }
  1187. intel_plane = to_intel_plane(plane);
  1188. intel_plane->get_colorkey(plane, get);
  1189. out_unlock:
  1190. drm_modeset_unlock_all(dev);
  1191. return ret;
  1192. }
  1193. int intel_plane_set_property(struct drm_plane *plane,
  1194. struct drm_property *prop,
  1195. uint64_t val)
  1196. {
  1197. struct drm_device *dev = plane->dev;
  1198. struct intel_plane *intel_plane = to_intel_plane(plane);
  1199. uint64_t old_val;
  1200. int ret = -ENOENT;
  1201. if (prop == dev->mode_config.rotation_property) {
  1202. /* exactly one rotation angle please */
  1203. if (hweight32(val & 0xf) != 1)
  1204. return -EINVAL;
  1205. if (intel_plane->rotation == val)
  1206. return 0;
  1207. old_val = intel_plane->rotation;
  1208. intel_plane->rotation = val;
  1209. ret = intel_plane_restore(plane);
  1210. if (ret)
  1211. intel_plane->rotation = old_val;
  1212. }
  1213. return ret;
  1214. }
  1215. int intel_plane_restore(struct drm_plane *plane)
  1216. {
  1217. struct intel_plane *intel_plane = to_intel_plane(plane);
  1218. if (!plane->crtc || !plane->fb)
  1219. return 0;
  1220. return plane->funcs->update_plane(plane, plane->crtc, plane->fb,
  1221. intel_plane->crtc_x, intel_plane->crtc_y,
  1222. intel_plane->crtc_w, intel_plane->crtc_h,
  1223. intel_plane->src_x, intel_plane->src_y,
  1224. intel_plane->src_w, intel_plane->src_h);
  1225. }
  1226. void intel_plane_disable(struct drm_plane *plane)
  1227. {
  1228. if (!plane->crtc || !plane->fb)
  1229. return;
  1230. intel_disable_plane(plane);
  1231. }
  1232. static const struct drm_plane_funcs intel_plane_funcs = {
  1233. .update_plane = intel_update_plane,
  1234. .disable_plane = intel_disable_plane,
  1235. .destroy = intel_destroy_plane,
  1236. .set_property = intel_plane_set_property,
  1237. };
  1238. static uint32_t ilk_plane_formats[] = {
  1239. DRM_FORMAT_XRGB8888,
  1240. DRM_FORMAT_YUYV,
  1241. DRM_FORMAT_YVYU,
  1242. DRM_FORMAT_UYVY,
  1243. DRM_FORMAT_VYUY,
  1244. };
  1245. static uint32_t snb_plane_formats[] = {
  1246. DRM_FORMAT_XBGR8888,
  1247. DRM_FORMAT_XRGB8888,
  1248. DRM_FORMAT_YUYV,
  1249. DRM_FORMAT_YVYU,
  1250. DRM_FORMAT_UYVY,
  1251. DRM_FORMAT_VYUY,
  1252. };
  1253. static uint32_t vlv_plane_formats[] = {
  1254. DRM_FORMAT_RGB565,
  1255. DRM_FORMAT_ABGR8888,
  1256. DRM_FORMAT_ARGB8888,
  1257. DRM_FORMAT_XBGR8888,
  1258. DRM_FORMAT_XRGB8888,
  1259. DRM_FORMAT_XBGR2101010,
  1260. DRM_FORMAT_ABGR2101010,
  1261. DRM_FORMAT_YUYV,
  1262. DRM_FORMAT_YVYU,
  1263. DRM_FORMAT_UYVY,
  1264. DRM_FORMAT_VYUY,
  1265. };
  1266. static uint32_t skl_plane_formats[] = {
  1267. DRM_FORMAT_RGB565,
  1268. DRM_FORMAT_ABGR8888,
  1269. DRM_FORMAT_ARGB8888,
  1270. DRM_FORMAT_XBGR8888,
  1271. DRM_FORMAT_XRGB8888,
  1272. DRM_FORMAT_YUYV,
  1273. DRM_FORMAT_YVYU,
  1274. DRM_FORMAT_UYVY,
  1275. DRM_FORMAT_VYUY,
  1276. };
  1277. int
  1278. intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
  1279. {
  1280. struct intel_plane *intel_plane;
  1281. unsigned long possible_crtcs;
  1282. const uint32_t *plane_formats;
  1283. int num_plane_formats;
  1284. int ret;
  1285. if (INTEL_INFO(dev)->gen < 5)
  1286. return -ENODEV;
  1287. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1288. if (!intel_plane)
  1289. return -ENOMEM;
  1290. switch (INTEL_INFO(dev)->gen) {
  1291. case 5:
  1292. case 6:
  1293. intel_plane->can_scale = true;
  1294. intel_plane->max_downscale = 16;
  1295. intel_plane->update_plane = ilk_update_plane;
  1296. intel_plane->disable_plane = ilk_disable_plane;
  1297. intel_plane->update_colorkey = ilk_update_colorkey;
  1298. intel_plane->get_colorkey = ilk_get_colorkey;
  1299. if (IS_GEN6(dev)) {
  1300. plane_formats = snb_plane_formats;
  1301. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1302. } else {
  1303. plane_formats = ilk_plane_formats;
  1304. num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
  1305. }
  1306. break;
  1307. case 7:
  1308. case 8:
  1309. if (IS_IVYBRIDGE(dev)) {
  1310. intel_plane->can_scale = true;
  1311. intel_plane->max_downscale = 2;
  1312. } else {
  1313. intel_plane->can_scale = false;
  1314. intel_plane->max_downscale = 1;
  1315. }
  1316. if (IS_VALLEYVIEW(dev)) {
  1317. intel_plane->update_plane = vlv_update_plane;
  1318. intel_plane->disable_plane = vlv_disable_plane;
  1319. intel_plane->update_colorkey = vlv_update_colorkey;
  1320. intel_plane->get_colorkey = vlv_get_colorkey;
  1321. plane_formats = vlv_plane_formats;
  1322. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1323. } else {
  1324. intel_plane->update_plane = ivb_update_plane;
  1325. intel_plane->disable_plane = ivb_disable_plane;
  1326. intel_plane->update_colorkey = ivb_update_colorkey;
  1327. intel_plane->get_colorkey = ivb_get_colorkey;
  1328. plane_formats = snb_plane_formats;
  1329. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1330. }
  1331. break;
  1332. case 9:
  1333. /*
  1334. * FIXME: Skylake planes can be scaled (with some restrictions),
  1335. * but this is for another time.
  1336. */
  1337. intel_plane->can_scale = false;
  1338. intel_plane->max_downscale = 1;
  1339. intel_plane->update_plane = skl_update_plane;
  1340. intel_plane->disable_plane = skl_disable_plane;
  1341. intel_plane->update_colorkey = skl_update_colorkey;
  1342. intel_plane->get_colorkey = skl_get_colorkey;
  1343. plane_formats = skl_plane_formats;
  1344. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1345. break;
  1346. default:
  1347. kfree(intel_plane);
  1348. return -ENODEV;
  1349. }
  1350. intel_plane->pipe = pipe;
  1351. intel_plane->plane = plane;
  1352. intel_plane->rotation = BIT(DRM_ROTATE_0);
  1353. possible_crtcs = (1 << pipe);
  1354. ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
  1355. &intel_plane_funcs,
  1356. plane_formats, num_plane_formats,
  1357. DRM_PLANE_TYPE_OVERLAY);
  1358. if (ret) {
  1359. kfree(intel_plane);
  1360. goto out;
  1361. }
  1362. if (!dev->mode_config.rotation_property)
  1363. dev->mode_config.rotation_property =
  1364. drm_mode_create_rotation_property(dev,
  1365. BIT(DRM_ROTATE_0) |
  1366. BIT(DRM_ROTATE_180));
  1367. if (dev->mode_config.rotation_property)
  1368. drm_object_attach_property(&intel_plane->base.base,
  1369. dev->mode_config.rotation_property,
  1370. intel_plane->rotation);
  1371. out:
  1372. return ret;
  1373. }