intel_sprite.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /*
  2. * Copyright © 2011 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Jesse Barnes <jbarnes@virtuousgeek.org>
  25. *
  26. * New plane/sprite handling.
  27. *
  28. * The older chips had a separate interface for programming plane related
  29. * registers; newer ones are much simpler and we can use the new DRM plane
  30. * support.
  31. */
  32. #include <drm/drmP.h>
  33. #include <drm/drm_atomic_helper.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_fourcc.h>
  36. #include <drm/drm_rect.h>
  37. #include <drm/drm_atomic.h>
  38. #include <drm/drm_plane_helper.h>
  39. #include "intel_drv.h"
  40. #include "intel_frontbuffer.h"
  41. #include <drm/i915_drm.h>
  42. #include "i915_drv.h"
  43. bool intel_format_is_yuv(u32 format)
  44. {
  45. switch (format) {
  46. case DRM_FORMAT_YUYV:
  47. case DRM_FORMAT_UYVY:
  48. case DRM_FORMAT_VYUY:
  49. case DRM_FORMAT_YVYU:
  50. case DRM_FORMAT_NV12:
  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) {
  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(const struct intel_plane_state *plane_state)
  298. {
  299. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  300. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  301. const struct drm_framebuffer *fb = plane_state->base.fb;
  302. enum plane_id plane_id = plane->id;
  303. /*
  304. * |r| | c0 c1 c2 | |cr|
  305. * |g| = | c3 c4 c5 | x |y |
  306. * |b| | c6 c7 c8 | |cb|
  307. *
  308. * Coefficients are s3.12.
  309. *
  310. * Cb and Cr apparently come in as signed already, and
  311. * we always get full range data in on account of CLRC0/1.
  312. */
  313. static const s16 csc_matrix[][9] = {
  314. /* BT.601 full range YCbCr -> full range RGB */
  315. [DRM_COLOR_YCBCR_BT601] = {
  316. 5743, 4096, 0,
  317. -2925, 4096, -1410,
  318. 0, 4096, 7258,
  319. },
  320. /* BT.709 full range YCbCr -> full range RGB */
  321. [DRM_COLOR_YCBCR_BT709] = {
  322. 6450, 4096, 0,
  323. -1917, 4096, -767,
  324. 0, 4096, 7601,
  325. },
  326. };
  327. const s16 *csc = csc_matrix[plane_state->base.color_encoding];
  328. /* Seems RGB data bypasses the CSC always */
  329. if (!intel_format_is_yuv(fb->format->format))
  330. return;
  331. I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  332. I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  333. I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  334. I915_WRITE_FW(SPCSCC01(plane_id), SPCSC_C1(csc[1]) | SPCSC_C0(csc[0]));
  335. I915_WRITE_FW(SPCSCC23(plane_id), SPCSC_C1(csc[3]) | SPCSC_C0(csc[2]));
  336. I915_WRITE_FW(SPCSCC45(plane_id), SPCSC_C1(csc[5]) | SPCSC_C0(csc[4]));
  337. I915_WRITE_FW(SPCSCC67(plane_id), SPCSC_C1(csc[7]) | SPCSC_C0(csc[6]));
  338. I915_WRITE_FW(SPCSCC8(plane_id), SPCSC_C0(csc[8]));
  339. I915_WRITE_FW(SPCSCYGICLAMP(plane_id), SPCSC_IMAX(1023) | SPCSC_IMIN(0));
  340. I915_WRITE_FW(SPCSCCBICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
  341. I915_WRITE_FW(SPCSCCRICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
  342. I915_WRITE_FW(SPCSCYGOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  343. I915_WRITE_FW(SPCSCCBOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  344. I915_WRITE_FW(SPCSCCROCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  345. }
  346. #define SIN_0 0
  347. #define COS_0 1
  348. static void
  349. vlv_update_clrc(const struct intel_plane_state *plane_state)
  350. {
  351. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  352. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  353. const struct drm_framebuffer *fb = plane_state->base.fb;
  354. enum pipe pipe = plane->pipe;
  355. enum plane_id plane_id = plane->id;
  356. int contrast, brightness, sh_scale, sh_sin, sh_cos;
  357. if (intel_format_is_yuv(fb->format->format) &&
  358. plane_state->base.color_range == DRM_COLOR_YCBCR_LIMITED_RANGE) {
  359. /*
  360. * Expand limited range to full range:
  361. * Contrast is applied first and is used to expand Y range.
  362. * Brightness is applied second and is used to remove the
  363. * offset from Y. Saturation/hue is used to expand CbCr range.
  364. */
  365. contrast = DIV_ROUND_CLOSEST(255 << 6, 235 - 16);
  366. brightness = -DIV_ROUND_CLOSEST(16 * 255, 235 - 16);
  367. sh_scale = DIV_ROUND_CLOSEST(128 << 7, 240 - 128);
  368. sh_sin = SIN_0 * sh_scale;
  369. sh_cos = COS_0 * sh_scale;
  370. } else {
  371. /* Pass-through everything. */
  372. contrast = 1 << 6;
  373. brightness = 0;
  374. sh_scale = 1 << 7;
  375. sh_sin = SIN_0 * sh_scale;
  376. sh_cos = COS_0 * sh_scale;
  377. }
  378. /* FIXME these register are single buffered :( */
  379. I915_WRITE_FW(SPCLRC0(pipe, plane_id),
  380. SP_CONTRAST(contrast) | SP_BRIGHTNESS(brightness));
  381. I915_WRITE_FW(SPCLRC1(pipe, plane_id),
  382. SP_SH_SIN(sh_sin) | SP_SH_COS(sh_cos));
  383. }
  384. static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
  385. const struct intel_plane_state *plane_state)
  386. {
  387. const struct drm_framebuffer *fb = plane_state->base.fb;
  388. unsigned int rotation = plane_state->base.rotation;
  389. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  390. u32 sprctl;
  391. sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
  392. switch (fb->format->format) {
  393. case DRM_FORMAT_YUYV:
  394. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  395. break;
  396. case DRM_FORMAT_YVYU:
  397. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  398. break;
  399. case DRM_FORMAT_UYVY:
  400. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  401. break;
  402. case DRM_FORMAT_VYUY:
  403. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  404. break;
  405. case DRM_FORMAT_RGB565:
  406. sprctl |= SP_FORMAT_BGR565;
  407. break;
  408. case DRM_FORMAT_XRGB8888:
  409. sprctl |= SP_FORMAT_BGRX8888;
  410. break;
  411. case DRM_FORMAT_ARGB8888:
  412. sprctl |= SP_FORMAT_BGRA8888;
  413. break;
  414. case DRM_FORMAT_XBGR2101010:
  415. sprctl |= SP_FORMAT_RGBX1010102;
  416. break;
  417. case DRM_FORMAT_ABGR2101010:
  418. sprctl |= SP_FORMAT_RGBA1010102;
  419. break;
  420. case DRM_FORMAT_XBGR8888:
  421. sprctl |= SP_FORMAT_RGBX8888;
  422. break;
  423. case DRM_FORMAT_ABGR8888:
  424. sprctl |= SP_FORMAT_RGBA8888;
  425. break;
  426. default:
  427. MISSING_CASE(fb->format->format);
  428. return 0;
  429. }
  430. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  431. sprctl |= SP_YUV_FORMAT_BT709;
  432. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  433. sprctl |= SP_TILED;
  434. if (rotation & DRM_MODE_ROTATE_180)
  435. sprctl |= SP_ROTATE_180;
  436. if (rotation & DRM_MODE_REFLECT_X)
  437. sprctl |= SP_MIRROR;
  438. if (key->flags & I915_SET_COLORKEY_SOURCE)
  439. sprctl |= SP_SOURCE_KEY;
  440. return sprctl;
  441. }
  442. static void
  443. vlv_update_plane(struct intel_plane *plane,
  444. const struct intel_crtc_state *crtc_state,
  445. const struct intel_plane_state *plane_state)
  446. {
  447. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  448. const struct drm_framebuffer *fb = plane_state->base.fb;
  449. enum pipe pipe = plane->pipe;
  450. enum plane_id plane_id = plane->id;
  451. u32 sprctl = plane_state->ctl;
  452. u32 sprsurf_offset = plane_state->main.offset;
  453. u32 linear_offset;
  454. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  455. int crtc_x = plane_state->base.dst.x1;
  456. int crtc_y = plane_state->base.dst.y1;
  457. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  458. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  459. uint32_t x = plane_state->main.x;
  460. uint32_t y = plane_state->main.y;
  461. unsigned long irqflags;
  462. /* Sizes are 0 based */
  463. crtc_w--;
  464. crtc_h--;
  465. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  466. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  467. vlv_update_clrc(plane_state);
  468. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
  469. chv_update_csc(plane_state);
  470. if (key->flags) {
  471. I915_WRITE_FW(SPKEYMINVAL(pipe, plane_id), key->min_value);
  472. I915_WRITE_FW(SPKEYMAXVAL(pipe, plane_id), key->max_value);
  473. I915_WRITE_FW(SPKEYMSK(pipe, plane_id), key->channel_mask);
  474. }
  475. I915_WRITE_FW(SPSTRIDE(pipe, plane_id), fb->pitches[0]);
  476. I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
  477. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  478. I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
  479. else
  480. I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
  481. I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
  482. I915_WRITE_FW(SPSIZE(pipe, plane_id), (crtc_h << 16) | crtc_w);
  483. I915_WRITE_FW(SPCNTR(pipe, plane_id), sprctl);
  484. I915_WRITE_FW(SPSURF(pipe, plane_id),
  485. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  486. POSTING_READ_FW(SPSURF(pipe, plane_id));
  487. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  488. }
  489. static void
  490. vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  491. {
  492. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  493. enum pipe pipe = plane->pipe;
  494. enum plane_id plane_id = plane->id;
  495. unsigned long irqflags;
  496. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  497. I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
  498. I915_WRITE_FW(SPSURF(pipe, plane_id), 0);
  499. POSTING_READ_FW(SPSURF(pipe, plane_id));
  500. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  501. }
  502. static bool
  503. vlv_plane_get_hw_state(struct intel_plane *plane)
  504. {
  505. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  506. enum intel_display_power_domain power_domain;
  507. enum plane_id plane_id = plane->id;
  508. enum pipe pipe = plane->pipe;
  509. bool ret;
  510. power_domain = POWER_DOMAIN_PIPE(pipe);
  511. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  512. return false;
  513. ret = I915_READ(SPCNTR(pipe, plane_id)) & SP_ENABLE;
  514. intel_display_power_put(dev_priv, power_domain);
  515. return ret;
  516. }
  517. static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
  518. const struct intel_plane_state *plane_state)
  519. {
  520. struct drm_i915_private *dev_priv =
  521. to_i915(plane_state->base.plane->dev);
  522. const struct drm_framebuffer *fb = plane_state->base.fb;
  523. unsigned int rotation = plane_state->base.rotation;
  524. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  525. u32 sprctl;
  526. sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
  527. if (IS_IVYBRIDGE(dev_priv))
  528. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  529. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  530. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  531. switch (fb->format->format) {
  532. case DRM_FORMAT_XBGR8888:
  533. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  534. break;
  535. case DRM_FORMAT_XRGB8888:
  536. sprctl |= SPRITE_FORMAT_RGBX888;
  537. break;
  538. case DRM_FORMAT_YUYV:
  539. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  540. break;
  541. case DRM_FORMAT_YVYU:
  542. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  543. break;
  544. case DRM_FORMAT_UYVY:
  545. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  546. break;
  547. case DRM_FORMAT_VYUY:
  548. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  549. break;
  550. default:
  551. MISSING_CASE(fb->format->format);
  552. return 0;
  553. }
  554. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  555. sprctl |= SPRITE_YUV_TO_RGB_CSC_FORMAT_BT709;
  556. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  557. sprctl |= SPRITE_YUV_RANGE_CORRECTION_DISABLE;
  558. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  559. sprctl |= SPRITE_TILED;
  560. if (rotation & DRM_MODE_ROTATE_180)
  561. sprctl |= SPRITE_ROTATE_180;
  562. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  563. sprctl |= SPRITE_DEST_KEY;
  564. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  565. sprctl |= SPRITE_SOURCE_KEY;
  566. return sprctl;
  567. }
  568. static void
  569. ivb_update_plane(struct intel_plane *plane,
  570. const struct intel_crtc_state *crtc_state,
  571. const struct intel_plane_state *plane_state)
  572. {
  573. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  574. const struct drm_framebuffer *fb = plane_state->base.fb;
  575. enum pipe pipe = plane->pipe;
  576. u32 sprctl = plane_state->ctl, sprscale = 0;
  577. u32 sprsurf_offset = plane_state->main.offset;
  578. u32 linear_offset;
  579. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  580. int crtc_x = plane_state->base.dst.x1;
  581. int crtc_y = plane_state->base.dst.y1;
  582. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  583. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  584. uint32_t x = plane_state->main.x;
  585. uint32_t y = plane_state->main.y;
  586. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  587. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  588. unsigned long irqflags;
  589. /* Sizes are 0 based */
  590. src_w--;
  591. src_h--;
  592. crtc_w--;
  593. crtc_h--;
  594. if (crtc_w != src_w || crtc_h != src_h)
  595. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  596. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  597. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  598. if (key->flags) {
  599. I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
  600. I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
  601. I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
  602. }
  603. I915_WRITE_FW(SPRSTRIDE(pipe), fb->pitches[0]);
  604. I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  605. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  606. * register */
  607. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  608. I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
  609. else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  610. I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
  611. else
  612. I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
  613. I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  614. if (plane->can_scale)
  615. I915_WRITE_FW(SPRSCALE(pipe), sprscale);
  616. I915_WRITE_FW(SPRCTL(pipe), sprctl);
  617. I915_WRITE_FW(SPRSURF(pipe),
  618. intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
  619. POSTING_READ_FW(SPRSURF(pipe));
  620. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  621. }
  622. static void
  623. ivb_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  624. {
  625. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  626. enum pipe pipe = plane->pipe;
  627. unsigned long irqflags;
  628. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  629. I915_WRITE_FW(SPRCTL(pipe), 0);
  630. /* Can't leave the scaler enabled... */
  631. if (plane->can_scale)
  632. I915_WRITE_FW(SPRSCALE(pipe), 0);
  633. I915_WRITE_FW(SPRSURF(pipe), 0);
  634. POSTING_READ_FW(SPRSURF(pipe));
  635. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  636. }
  637. static bool
  638. ivb_plane_get_hw_state(struct intel_plane *plane)
  639. {
  640. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  641. enum intel_display_power_domain power_domain;
  642. enum pipe pipe = plane->pipe;
  643. bool ret;
  644. power_domain = POWER_DOMAIN_PIPE(pipe);
  645. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  646. return false;
  647. ret = I915_READ(SPRCTL(pipe)) & SPRITE_ENABLE;
  648. intel_display_power_put(dev_priv, power_domain);
  649. return ret;
  650. }
  651. static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
  652. const struct intel_plane_state *plane_state)
  653. {
  654. struct drm_i915_private *dev_priv =
  655. to_i915(plane_state->base.plane->dev);
  656. const struct drm_framebuffer *fb = plane_state->base.fb;
  657. unsigned int rotation = plane_state->base.rotation;
  658. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  659. u32 dvscntr;
  660. dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
  661. if (IS_GEN6(dev_priv))
  662. dvscntr |= DVS_TRICKLE_FEED_DISABLE;
  663. switch (fb->format->format) {
  664. case DRM_FORMAT_XBGR8888:
  665. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  666. break;
  667. case DRM_FORMAT_XRGB8888:
  668. dvscntr |= DVS_FORMAT_RGBX888;
  669. break;
  670. case DRM_FORMAT_YUYV:
  671. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  672. break;
  673. case DRM_FORMAT_YVYU:
  674. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  675. break;
  676. case DRM_FORMAT_UYVY:
  677. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  678. break;
  679. case DRM_FORMAT_VYUY:
  680. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  681. break;
  682. default:
  683. MISSING_CASE(fb->format->format);
  684. return 0;
  685. }
  686. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  687. dvscntr |= DVS_YUV_FORMAT_BT709;
  688. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  689. dvscntr |= DVS_YUV_RANGE_CORRECTION_DISABLE;
  690. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  691. dvscntr |= DVS_TILED;
  692. if (rotation & DRM_MODE_ROTATE_180)
  693. dvscntr |= DVS_ROTATE_180;
  694. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  695. dvscntr |= DVS_DEST_KEY;
  696. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  697. dvscntr |= DVS_SOURCE_KEY;
  698. return dvscntr;
  699. }
  700. static void
  701. g4x_update_plane(struct intel_plane *plane,
  702. const struct intel_crtc_state *crtc_state,
  703. const struct intel_plane_state *plane_state)
  704. {
  705. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  706. const struct drm_framebuffer *fb = plane_state->base.fb;
  707. enum pipe pipe = plane->pipe;
  708. u32 dvscntr = plane_state->ctl, dvsscale = 0;
  709. u32 dvssurf_offset = plane_state->main.offset;
  710. u32 linear_offset;
  711. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  712. int crtc_x = plane_state->base.dst.x1;
  713. int crtc_y = plane_state->base.dst.y1;
  714. uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
  715. uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
  716. uint32_t x = plane_state->main.x;
  717. uint32_t y = plane_state->main.y;
  718. uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
  719. uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
  720. unsigned long irqflags;
  721. /* Sizes are 0 based */
  722. src_w--;
  723. src_h--;
  724. crtc_w--;
  725. crtc_h--;
  726. if (crtc_w != src_w || crtc_h != src_h)
  727. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  728. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  729. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  730. if (key->flags) {
  731. I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
  732. I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
  733. I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
  734. }
  735. I915_WRITE_FW(DVSSTRIDE(pipe), fb->pitches[0]);
  736. I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  737. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  738. I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
  739. else
  740. I915_WRITE_FW(DVSLINOFF(pipe), linear_offset);
  741. I915_WRITE_FW(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  742. I915_WRITE_FW(DVSSCALE(pipe), dvsscale);
  743. I915_WRITE_FW(DVSCNTR(pipe), dvscntr);
  744. I915_WRITE_FW(DVSSURF(pipe),
  745. intel_plane_ggtt_offset(plane_state) + dvssurf_offset);
  746. POSTING_READ_FW(DVSSURF(pipe));
  747. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  748. }
  749. static void
  750. g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
  751. {
  752. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  753. enum pipe pipe = plane->pipe;
  754. unsigned long irqflags;
  755. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  756. I915_WRITE_FW(DVSCNTR(pipe), 0);
  757. /* Disable the scaler */
  758. I915_WRITE_FW(DVSSCALE(pipe), 0);
  759. I915_WRITE_FW(DVSSURF(pipe), 0);
  760. POSTING_READ_FW(DVSSURF(pipe));
  761. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  762. }
  763. static bool
  764. g4x_plane_get_hw_state(struct intel_plane *plane)
  765. {
  766. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  767. enum intel_display_power_domain power_domain;
  768. enum pipe pipe = plane->pipe;
  769. bool ret;
  770. power_domain = POWER_DOMAIN_PIPE(pipe);
  771. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  772. return false;
  773. ret = I915_READ(DVSCNTR(pipe)) & DVS_ENABLE;
  774. intel_display_power_put(dev_priv, power_domain);
  775. return ret;
  776. }
  777. static int
  778. intel_check_sprite_plane(struct intel_plane *plane,
  779. struct intel_crtc_state *crtc_state,
  780. struct intel_plane_state *state)
  781. {
  782. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  783. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  784. struct drm_framebuffer *fb = state->base.fb;
  785. int max_stride = INTEL_GEN(dev_priv) >= 9 ? 32768 : 16384;
  786. int max_scale, min_scale;
  787. bool can_scale;
  788. int ret;
  789. uint32_t pixel_format = 0;
  790. if (!fb) {
  791. state->base.visible = false;
  792. return 0;
  793. }
  794. /* Don't modify another pipe's plane */
  795. if (plane->pipe != crtc->pipe) {
  796. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  797. return -EINVAL;
  798. }
  799. /* FIXME check all gen limits */
  800. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > max_stride) {
  801. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  802. return -EINVAL;
  803. }
  804. /* setup can_scale, min_scale, max_scale */
  805. if (INTEL_GEN(dev_priv) >= 9) {
  806. if (state->base.fb)
  807. pixel_format = state->base.fb->format->format;
  808. /* use scaler when colorkey is not required */
  809. if (!state->ckey.flags) {
  810. can_scale = 1;
  811. min_scale = 1;
  812. max_scale =
  813. skl_max_scale(crtc, crtc_state, pixel_format);
  814. } else {
  815. can_scale = 0;
  816. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  817. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  818. }
  819. } else {
  820. can_scale = plane->can_scale;
  821. max_scale = plane->max_downscale << 16;
  822. min_scale = plane->can_scale ? 1 : (1 << 16);
  823. }
  824. ret = drm_atomic_helper_check_plane_state(&state->base,
  825. &crtc_state->base,
  826. min_scale, max_scale,
  827. true, true);
  828. if (ret)
  829. return ret;
  830. if (state->base.visible) {
  831. struct drm_rect *src = &state->base.src;
  832. struct drm_rect *dst = &state->base.dst;
  833. unsigned int crtc_w = drm_rect_width(dst);
  834. unsigned int crtc_h = drm_rect_height(dst);
  835. uint32_t src_x, src_y, src_w, src_h;
  836. /*
  837. * Hardware doesn't handle subpixel coordinates.
  838. * Adjust to (macro)pixel boundary, but be careful not to
  839. * increase the source viewport size, because that could
  840. * push the downscaling factor out of bounds.
  841. */
  842. src_x = src->x1 >> 16;
  843. src_w = drm_rect_width(src) >> 16;
  844. src_y = src->y1 >> 16;
  845. src_h = drm_rect_height(src) >> 16;
  846. src->x1 = src_x << 16;
  847. src->x2 = (src_x + src_w) << 16;
  848. src->y1 = src_y << 16;
  849. src->y2 = (src_y + src_h) << 16;
  850. if (intel_format_is_yuv(fb->format->format) &&
  851. fb->format->format != DRM_FORMAT_NV12 &&
  852. (src_x % 2 || src_w % 2)) {
  853. DRM_DEBUG_KMS("src x/w (%u, %u) must be a multiple of 2 for YUV planes\n",
  854. src_x, src_w);
  855. return -EINVAL;
  856. }
  857. /* Check size restrictions when scaling */
  858. if (src_w != crtc_w || src_h != crtc_h) {
  859. unsigned int width_bytes;
  860. int cpp = fb->format->cpp[0];
  861. WARN_ON(!can_scale);
  862. width_bytes = ((src_x * cpp) & 63) + src_w * cpp;
  863. /* FIXME interlacing min height is 6 */
  864. if (INTEL_GEN(dev_priv) < 9 && (
  865. src_w < 3 || src_h < 3 ||
  866. src_w > 2048 || src_h > 2048 ||
  867. crtc_w < 3 || crtc_h < 3 ||
  868. width_bytes > 4096 || fb->pitches[0] > 4096)) {
  869. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  870. return -EINVAL;
  871. }
  872. }
  873. }
  874. if (INTEL_GEN(dev_priv) >= 9) {
  875. ret = skl_check_plane_surface(crtc_state, state);
  876. if (ret)
  877. return ret;
  878. state->ctl = skl_plane_ctl(crtc_state, state);
  879. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  880. ret = i9xx_check_plane_surface(state);
  881. if (ret)
  882. return ret;
  883. state->ctl = vlv_sprite_ctl(crtc_state, state);
  884. } else if (INTEL_GEN(dev_priv) >= 7) {
  885. ret = i9xx_check_plane_surface(state);
  886. if (ret)
  887. return ret;
  888. state->ctl = ivb_sprite_ctl(crtc_state, state);
  889. } else {
  890. ret = i9xx_check_plane_surface(state);
  891. if (ret)
  892. return ret;
  893. state->ctl = g4x_sprite_ctl(crtc_state, state);
  894. }
  895. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  896. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  897. return 0;
  898. }
  899. int intel_sprite_set_colorkey_ioctl(struct drm_device *dev, void *data,
  900. struct drm_file *file_priv)
  901. {
  902. struct drm_i915_private *dev_priv = to_i915(dev);
  903. struct drm_intel_sprite_colorkey *set = data;
  904. struct drm_plane *plane;
  905. struct drm_plane_state *plane_state;
  906. struct drm_atomic_state *state;
  907. struct drm_modeset_acquire_ctx ctx;
  908. int ret = 0;
  909. /* ignore the pointless "none" flag */
  910. set->flags &= ~I915_SET_COLORKEY_NONE;
  911. if (set->flags & ~(I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  912. return -EINVAL;
  913. /* Make sure we don't try to enable both src & dest simultaneously */
  914. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  915. return -EINVAL;
  916. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  917. set->flags & I915_SET_COLORKEY_DESTINATION)
  918. return -EINVAL;
  919. plane = drm_plane_find(dev, file_priv, set->plane_id);
  920. if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
  921. return -ENOENT;
  922. drm_modeset_acquire_init(&ctx, 0);
  923. state = drm_atomic_state_alloc(plane->dev);
  924. if (!state) {
  925. ret = -ENOMEM;
  926. goto out;
  927. }
  928. state->acquire_ctx = &ctx;
  929. while (1) {
  930. plane_state = drm_atomic_get_plane_state(state, plane);
  931. ret = PTR_ERR_OR_ZERO(plane_state);
  932. if (!ret) {
  933. to_intel_plane_state(plane_state)->ckey = *set;
  934. ret = drm_atomic_commit(state);
  935. }
  936. if (ret != -EDEADLK)
  937. break;
  938. drm_atomic_state_clear(state);
  939. drm_modeset_backoff(&ctx);
  940. }
  941. drm_atomic_state_put(state);
  942. out:
  943. drm_modeset_drop_locks(&ctx);
  944. drm_modeset_acquire_fini(&ctx);
  945. return ret;
  946. }
  947. static const uint32_t g4x_plane_formats[] = {
  948. DRM_FORMAT_XRGB8888,
  949. DRM_FORMAT_YUYV,
  950. DRM_FORMAT_YVYU,
  951. DRM_FORMAT_UYVY,
  952. DRM_FORMAT_VYUY,
  953. };
  954. static const uint64_t i9xx_plane_format_modifiers[] = {
  955. I915_FORMAT_MOD_X_TILED,
  956. DRM_FORMAT_MOD_LINEAR,
  957. DRM_FORMAT_MOD_INVALID
  958. };
  959. static const uint32_t snb_plane_formats[] = {
  960. DRM_FORMAT_XBGR8888,
  961. DRM_FORMAT_XRGB8888,
  962. DRM_FORMAT_YUYV,
  963. DRM_FORMAT_YVYU,
  964. DRM_FORMAT_UYVY,
  965. DRM_FORMAT_VYUY,
  966. };
  967. static const uint32_t vlv_plane_formats[] = {
  968. DRM_FORMAT_RGB565,
  969. DRM_FORMAT_ABGR8888,
  970. DRM_FORMAT_ARGB8888,
  971. DRM_FORMAT_XBGR8888,
  972. DRM_FORMAT_XRGB8888,
  973. DRM_FORMAT_XBGR2101010,
  974. DRM_FORMAT_ABGR2101010,
  975. DRM_FORMAT_YUYV,
  976. DRM_FORMAT_YVYU,
  977. DRM_FORMAT_UYVY,
  978. DRM_FORMAT_VYUY,
  979. };
  980. static uint32_t skl_plane_formats[] = {
  981. DRM_FORMAT_RGB565,
  982. DRM_FORMAT_ABGR8888,
  983. DRM_FORMAT_ARGB8888,
  984. DRM_FORMAT_XBGR8888,
  985. DRM_FORMAT_XRGB8888,
  986. DRM_FORMAT_YUYV,
  987. DRM_FORMAT_YVYU,
  988. DRM_FORMAT_UYVY,
  989. DRM_FORMAT_VYUY,
  990. };
  991. static uint32_t skl_planar_formats[] = {
  992. DRM_FORMAT_RGB565,
  993. DRM_FORMAT_ABGR8888,
  994. DRM_FORMAT_ARGB8888,
  995. DRM_FORMAT_XBGR8888,
  996. DRM_FORMAT_XRGB8888,
  997. DRM_FORMAT_YUYV,
  998. DRM_FORMAT_YVYU,
  999. DRM_FORMAT_UYVY,
  1000. DRM_FORMAT_VYUY,
  1001. DRM_FORMAT_NV12,
  1002. };
  1003. static const uint64_t skl_plane_format_modifiers_noccs[] = {
  1004. I915_FORMAT_MOD_Yf_TILED,
  1005. I915_FORMAT_MOD_Y_TILED,
  1006. I915_FORMAT_MOD_X_TILED,
  1007. DRM_FORMAT_MOD_LINEAR,
  1008. DRM_FORMAT_MOD_INVALID
  1009. };
  1010. static const uint64_t skl_plane_format_modifiers_ccs[] = {
  1011. I915_FORMAT_MOD_Yf_TILED_CCS,
  1012. I915_FORMAT_MOD_Y_TILED_CCS,
  1013. I915_FORMAT_MOD_Yf_TILED,
  1014. I915_FORMAT_MOD_Y_TILED,
  1015. I915_FORMAT_MOD_X_TILED,
  1016. DRM_FORMAT_MOD_LINEAR,
  1017. DRM_FORMAT_MOD_INVALID
  1018. };
  1019. static bool g4x_mod_supported(uint32_t format, uint64_t modifier)
  1020. {
  1021. switch (format) {
  1022. case DRM_FORMAT_XRGB8888:
  1023. case DRM_FORMAT_YUYV:
  1024. case DRM_FORMAT_YVYU:
  1025. case DRM_FORMAT_UYVY:
  1026. case DRM_FORMAT_VYUY:
  1027. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1028. modifier == I915_FORMAT_MOD_X_TILED)
  1029. return true;
  1030. /* fall through */
  1031. default:
  1032. return false;
  1033. }
  1034. }
  1035. static bool snb_mod_supported(uint32_t format, uint64_t modifier)
  1036. {
  1037. switch (format) {
  1038. case DRM_FORMAT_XRGB8888:
  1039. case DRM_FORMAT_XBGR8888:
  1040. case DRM_FORMAT_YUYV:
  1041. case DRM_FORMAT_YVYU:
  1042. case DRM_FORMAT_UYVY:
  1043. case DRM_FORMAT_VYUY:
  1044. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1045. modifier == I915_FORMAT_MOD_X_TILED)
  1046. return true;
  1047. /* fall through */
  1048. default:
  1049. return false;
  1050. }
  1051. }
  1052. static bool vlv_mod_supported(uint32_t format, uint64_t modifier)
  1053. {
  1054. switch (format) {
  1055. case DRM_FORMAT_RGB565:
  1056. case DRM_FORMAT_ABGR8888:
  1057. case DRM_FORMAT_ARGB8888:
  1058. case DRM_FORMAT_XBGR8888:
  1059. case DRM_FORMAT_XRGB8888:
  1060. case DRM_FORMAT_XBGR2101010:
  1061. case DRM_FORMAT_ABGR2101010:
  1062. case DRM_FORMAT_YUYV:
  1063. case DRM_FORMAT_YVYU:
  1064. case DRM_FORMAT_UYVY:
  1065. case DRM_FORMAT_VYUY:
  1066. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1067. modifier == I915_FORMAT_MOD_X_TILED)
  1068. return true;
  1069. /* fall through */
  1070. default:
  1071. return false;
  1072. }
  1073. }
  1074. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  1075. {
  1076. switch (format) {
  1077. case DRM_FORMAT_XRGB8888:
  1078. case DRM_FORMAT_XBGR8888:
  1079. case DRM_FORMAT_ARGB8888:
  1080. case DRM_FORMAT_ABGR8888:
  1081. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  1082. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  1083. return true;
  1084. /* fall through */
  1085. case DRM_FORMAT_RGB565:
  1086. case DRM_FORMAT_XRGB2101010:
  1087. case DRM_FORMAT_XBGR2101010:
  1088. case DRM_FORMAT_YUYV:
  1089. case DRM_FORMAT_YVYU:
  1090. case DRM_FORMAT_UYVY:
  1091. case DRM_FORMAT_VYUY:
  1092. case DRM_FORMAT_NV12:
  1093. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  1094. return true;
  1095. /* fall through */
  1096. case DRM_FORMAT_C8:
  1097. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  1098. modifier == I915_FORMAT_MOD_X_TILED ||
  1099. modifier == I915_FORMAT_MOD_Y_TILED)
  1100. return true;
  1101. /* fall through */
  1102. default:
  1103. return false;
  1104. }
  1105. }
  1106. static bool intel_sprite_plane_format_mod_supported(struct drm_plane *plane,
  1107. uint32_t format,
  1108. uint64_t modifier)
  1109. {
  1110. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  1111. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  1112. return false;
  1113. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  1114. modifier != DRM_FORMAT_MOD_LINEAR)
  1115. return false;
  1116. if (INTEL_GEN(dev_priv) >= 9)
  1117. return skl_mod_supported(format, modifier);
  1118. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1119. return vlv_mod_supported(format, modifier);
  1120. else if (INTEL_GEN(dev_priv) >= 6)
  1121. return snb_mod_supported(format, modifier);
  1122. else
  1123. return g4x_mod_supported(format, modifier);
  1124. }
  1125. static const struct drm_plane_funcs intel_sprite_plane_funcs = {
  1126. .update_plane = drm_atomic_helper_update_plane,
  1127. .disable_plane = drm_atomic_helper_disable_plane,
  1128. .destroy = intel_plane_destroy,
  1129. .atomic_get_property = intel_plane_atomic_get_property,
  1130. .atomic_set_property = intel_plane_atomic_set_property,
  1131. .atomic_duplicate_state = intel_plane_duplicate_state,
  1132. .atomic_destroy_state = intel_plane_destroy_state,
  1133. .format_mod_supported = intel_sprite_plane_format_mod_supported,
  1134. };
  1135. bool skl_plane_has_ccs(struct drm_i915_private *dev_priv,
  1136. enum pipe pipe, enum plane_id plane_id)
  1137. {
  1138. if (plane_id == PLANE_CURSOR)
  1139. return false;
  1140. if (INTEL_GEN(dev_priv) >= 10)
  1141. return true;
  1142. if (IS_GEMINILAKE(dev_priv))
  1143. return pipe != PIPE_C;
  1144. return pipe != PIPE_C &&
  1145. (plane_id == PLANE_PRIMARY ||
  1146. plane_id == PLANE_SPRITE0);
  1147. }
  1148. struct intel_plane *
  1149. intel_sprite_plane_create(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, int plane)
  1151. {
  1152. struct intel_plane *intel_plane = NULL;
  1153. struct intel_plane_state *state = NULL;
  1154. unsigned long possible_crtcs;
  1155. const uint32_t *plane_formats;
  1156. const uint64_t *modifiers;
  1157. unsigned int supported_rotations;
  1158. int num_plane_formats;
  1159. int ret;
  1160. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1161. if (!intel_plane) {
  1162. ret = -ENOMEM;
  1163. goto fail;
  1164. }
  1165. state = intel_create_plane_state(&intel_plane->base);
  1166. if (!state) {
  1167. ret = -ENOMEM;
  1168. goto fail;
  1169. }
  1170. intel_plane->base.state = &state->base;
  1171. if (INTEL_GEN(dev_priv) >= 9) {
  1172. intel_plane->can_scale = true;
  1173. state->scaler_id = -1;
  1174. intel_plane->update_plane = skl_update_plane;
  1175. intel_plane->disable_plane = skl_disable_plane;
  1176. intel_plane->get_hw_state = skl_plane_get_hw_state;
  1177. if (skl_plane_has_planar(dev_priv, pipe,
  1178. PLANE_SPRITE0 + plane)) {
  1179. plane_formats = skl_planar_formats;
  1180. num_plane_formats = ARRAY_SIZE(skl_planar_formats);
  1181. } else {
  1182. plane_formats = skl_plane_formats;
  1183. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1184. }
  1185. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_SPRITE0 + plane))
  1186. modifiers = skl_plane_format_modifiers_ccs;
  1187. else
  1188. modifiers = skl_plane_format_modifiers_noccs;
  1189. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1190. intel_plane->can_scale = false;
  1191. intel_plane->max_downscale = 1;
  1192. intel_plane->update_plane = vlv_update_plane;
  1193. intel_plane->disable_plane = vlv_disable_plane;
  1194. intel_plane->get_hw_state = vlv_plane_get_hw_state;
  1195. plane_formats = vlv_plane_formats;
  1196. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1197. modifiers = i9xx_plane_format_modifiers;
  1198. } else if (INTEL_GEN(dev_priv) >= 7) {
  1199. if (IS_IVYBRIDGE(dev_priv)) {
  1200. intel_plane->can_scale = true;
  1201. intel_plane->max_downscale = 2;
  1202. } else {
  1203. intel_plane->can_scale = false;
  1204. intel_plane->max_downscale = 1;
  1205. }
  1206. intel_plane->update_plane = ivb_update_plane;
  1207. intel_plane->disable_plane = ivb_disable_plane;
  1208. intel_plane->get_hw_state = ivb_plane_get_hw_state;
  1209. plane_formats = snb_plane_formats;
  1210. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1211. modifiers = i9xx_plane_format_modifiers;
  1212. } else {
  1213. intel_plane->can_scale = true;
  1214. intel_plane->max_downscale = 16;
  1215. intel_plane->update_plane = g4x_update_plane;
  1216. intel_plane->disable_plane = g4x_disable_plane;
  1217. intel_plane->get_hw_state = g4x_plane_get_hw_state;
  1218. modifiers = i9xx_plane_format_modifiers;
  1219. if (IS_GEN6(dev_priv)) {
  1220. plane_formats = snb_plane_formats;
  1221. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1222. } else {
  1223. plane_formats = g4x_plane_formats;
  1224. num_plane_formats = ARRAY_SIZE(g4x_plane_formats);
  1225. }
  1226. }
  1227. if (INTEL_GEN(dev_priv) >= 9) {
  1228. supported_rotations =
  1229. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  1230. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  1231. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  1232. supported_rotations =
  1233. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  1234. DRM_MODE_REFLECT_X;
  1235. } else {
  1236. supported_rotations =
  1237. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  1238. }
  1239. intel_plane->pipe = pipe;
  1240. intel_plane->i9xx_plane = plane;
  1241. intel_plane->id = PLANE_SPRITE0 + plane;
  1242. intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, intel_plane->id);
  1243. intel_plane->check_plane = intel_check_sprite_plane;
  1244. possible_crtcs = (1 << pipe);
  1245. if (INTEL_GEN(dev_priv) >= 9)
  1246. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1247. possible_crtcs, &intel_sprite_plane_funcs,
  1248. plane_formats, num_plane_formats,
  1249. modifiers,
  1250. DRM_PLANE_TYPE_OVERLAY,
  1251. "plane %d%c", plane + 2, pipe_name(pipe));
  1252. else
  1253. ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
  1254. possible_crtcs, &intel_sprite_plane_funcs,
  1255. plane_formats, num_plane_formats,
  1256. modifiers,
  1257. DRM_PLANE_TYPE_OVERLAY,
  1258. "sprite %c", sprite_name(pipe, plane));
  1259. if (ret)
  1260. goto fail;
  1261. drm_plane_create_rotation_property(&intel_plane->base,
  1262. DRM_MODE_ROTATE_0,
  1263. supported_rotations);
  1264. drm_plane_create_color_properties(&intel_plane->base,
  1265. BIT(DRM_COLOR_YCBCR_BT601) |
  1266. BIT(DRM_COLOR_YCBCR_BT709),
  1267. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  1268. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  1269. DRM_COLOR_YCBCR_BT709,
  1270. DRM_COLOR_YCBCR_LIMITED_RANGE);
  1271. drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
  1272. return intel_plane;
  1273. fail:
  1274. kfree(state);
  1275. kfree(intel_plane);
  1276. return ERR_PTR(ret);
  1277. }