intel_sprite.c 42 KB

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