intel_overlay.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. * Copyright © 2009
  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. * Daniel Vetter <daniel@ffwll.ch>
  25. *
  26. * Derived from Xorg ddx, xf86-video-intel, src/i830_video.c
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_reg.h"
  32. #include "intel_drv.h"
  33. #include "intel_frontbuffer.h"
  34. /* Limits for overlay size. According to intel doc, the real limits are:
  35. * Y width: 4095, UV width (planar): 2047, Y height: 2047,
  36. * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
  37. * the mininum of both. */
  38. #define IMAGE_MAX_WIDTH 2048
  39. #define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
  40. /* on 830 and 845 these large limits result in the card hanging */
  41. #define IMAGE_MAX_WIDTH_LEGACY 1024
  42. #define IMAGE_MAX_HEIGHT_LEGACY 1088
  43. /* overlay register definitions */
  44. /* OCMD register */
  45. #define OCMD_TILED_SURFACE (0x1<<19)
  46. #define OCMD_MIRROR_MASK (0x3<<17)
  47. #define OCMD_MIRROR_MODE (0x3<<17)
  48. #define OCMD_MIRROR_HORIZONTAL (0x1<<17)
  49. #define OCMD_MIRROR_VERTICAL (0x2<<17)
  50. #define OCMD_MIRROR_BOTH (0x3<<17)
  51. #define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
  52. #define OCMD_UV_SWAP (0x1<<14) /* YVYU */
  53. #define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
  54. #define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
  55. #define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
  56. #define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
  57. #define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
  58. #define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
  59. #define OCMD_YUV_422_PACKED (0x8<<10)
  60. #define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
  61. #define OCMD_YUV_420_PLANAR (0xc<<10)
  62. #define OCMD_YUV_422_PLANAR (0xd<<10)
  63. #define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
  64. #define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
  65. #define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
  66. #define OCMD_BUF_TYPE_MASK (0x1<<5)
  67. #define OCMD_BUF_TYPE_FRAME (0x0<<5)
  68. #define OCMD_BUF_TYPE_FIELD (0x1<<5)
  69. #define OCMD_TEST_MODE (0x1<<4)
  70. #define OCMD_BUFFER_SELECT (0x3<<2)
  71. #define OCMD_BUFFER0 (0x0<<2)
  72. #define OCMD_BUFFER1 (0x1<<2)
  73. #define OCMD_FIELD_SELECT (0x1<<2)
  74. #define OCMD_FIELD0 (0x0<<1)
  75. #define OCMD_FIELD1 (0x1<<1)
  76. #define OCMD_ENABLE (0x1<<0)
  77. /* OCONFIG register */
  78. #define OCONF_PIPE_MASK (0x1<<18)
  79. #define OCONF_PIPE_A (0x0<<18)
  80. #define OCONF_PIPE_B (0x1<<18)
  81. #define OCONF_GAMMA2_ENABLE (0x1<<16)
  82. #define OCONF_CSC_MODE_BT601 (0x0<<5)
  83. #define OCONF_CSC_MODE_BT709 (0x1<<5)
  84. #define OCONF_CSC_BYPASS (0x1<<4)
  85. #define OCONF_CC_OUT_8BIT (0x1<<3)
  86. #define OCONF_TEST_MODE (0x1<<2)
  87. #define OCONF_THREE_LINE_BUFFER (0x1<<0)
  88. #define OCONF_TWO_LINE_BUFFER (0x0<<0)
  89. /* DCLRKM (dst-key) register */
  90. #define DST_KEY_ENABLE (0x1<<31)
  91. #define CLK_RGB24_MASK 0x0
  92. #define CLK_RGB16_MASK 0x070307
  93. #define CLK_RGB15_MASK 0x070707
  94. #define CLK_RGB8I_MASK 0xffffff
  95. #define RGB16_TO_COLORKEY(c) \
  96. (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
  97. #define RGB15_TO_COLORKEY(c) \
  98. (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
  99. /* overlay flip addr flag */
  100. #define OFC_UPDATE 0x1
  101. /* polyphase filter coefficients */
  102. #define N_HORIZ_Y_TAPS 5
  103. #define N_VERT_Y_TAPS 3
  104. #define N_HORIZ_UV_TAPS 3
  105. #define N_VERT_UV_TAPS 3
  106. #define N_PHASES 17
  107. #define MAX_TAPS 5
  108. /* memory bufferd overlay registers */
  109. struct overlay_registers {
  110. u32 OBUF_0Y;
  111. u32 OBUF_1Y;
  112. u32 OBUF_0U;
  113. u32 OBUF_0V;
  114. u32 OBUF_1U;
  115. u32 OBUF_1V;
  116. u32 OSTRIDE;
  117. u32 YRGB_VPH;
  118. u32 UV_VPH;
  119. u32 HORZ_PH;
  120. u32 INIT_PHS;
  121. u32 DWINPOS;
  122. u32 DWINSZ;
  123. u32 SWIDTH;
  124. u32 SWIDTHSW;
  125. u32 SHEIGHT;
  126. u32 YRGBSCALE;
  127. u32 UVSCALE;
  128. u32 OCLRC0;
  129. u32 OCLRC1;
  130. u32 DCLRKV;
  131. u32 DCLRKM;
  132. u32 SCLRKVH;
  133. u32 SCLRKVL;
  134. u32 SCLRKEN;
  135. u32 OCONFIG;
  136. u32 OCMD;
  137. u32 RESERVED1; /* 0x6C */
  138. u32 OSTART_0Y;
  139. u32 OSTART_1Y;
  140. u32 OSTART_0U;
  141. u32 OSTART_0V;
  142. u32 OSTART_1U;
  143. u32 OSTART_1V;
  144. u32 OTILEOFF_0Y;
  145. u32 OTILEOFF_1Y;
  146. u32 OTILEOFF_0U;
  147. u32 OTILEOFF_0V;
  148. u32 OTILEOFF_1U;
  149. u32 OTILEOFF_1V;
  150. u32 FASTHSCALE; /* 0xA0 */
  151. u32 UVSCALEV; /* 0xA4 */
  152. u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
  153. u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
  154. u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
  155. u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
  156. u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
  157. u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
  158. u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
  159. u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
  160. u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
  161. };
  162. struct intel_overlay {
  163. struct drm_i915_private *i915;
  164. struct intel_crtc *crtc;
  165. struct i915_vma *vma;
  166. struct i915_vma *old_vma;
  167. bool active;
  168. bool pfit_active;
  169. u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
  170. u32 color_key:24;
  171. u32 color_key_enabled:1;
  172. u32 brightness, contrast, saturation;
  173. u32 old_xscale, old_yscale;
  174. /* register access */
  175. struct drm_i915_gem_object *reg_bo;
  176. struct overlay_registers __iomem *regs;
  177. u32 flip_addr;
  178. /* flip handling */
  179. struct i915_gem_active last_flip;
  180. };
  181. static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv,
  182. bool enable)
  183. {
  184. struct pci_dev *pdev = dev_priv->drm.pdev;
  185. u8 val;
  186. /* WA_OVERLAY_CLKGATE:alm */
  187. if (enable)
  188. I915_WRITE(DSPCLK_GATE_D, 0);
  189. else
  190. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  191. /* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */
  192. pci_bus_read_config_byte(pdev->bus,
  193. PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val);
  194. if (enable)
  195. val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE;
  196. else
  197. val |= I830_L2_CACHE_CLOCK_GATE_DISABLE;
  198. pci_bus_write_config_byte(pdev->bus,
  199. PCI_DEVFN(0, 0), I830_CLOCK_GATE, val);
  200. }
  201. static void intel_overlay_submit_request(struct intel_overlay *overlay,
  202. struct i915_request *rq,
  203. i915_gem_retire_fn retire)
  204. {
  205. GEM_BUG_ON(i915_gem_active_peek(&overlay->last_flip,
  206. &overlay->i915->drm.struct_mutex));
  207. i915_gem_active_set_retire_fn(&overlay->last_flip, retire,
  208. &overlay->i915->drm.struct_mutex);
  209. i915_gem_active_set(&overlay->last_flip, rq);
  210. i915_request_add(rq);
  211. }
  212. static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
  213. struct i915_request *rq,
  214. i915_gem_retire_fn retire)
  215. {
  216. intel_overlay_submit_request(overlay, rq, retire);
  217. return i915_gem_active_retire(&overlay->last_flip,
  218. &overlay->i915->drm.struct_mutex);
  219. }
  220. static struct i915_request *alloc_request(struct intel_overlay *overlay)
  221. {
  222. struct drm_i915_private *dev_priv = overlay->i915;
  223. struct intel_engine_cs *engine = dev_priv->engine[RCS];
  224. return i915_request_alloc(engine, dev_priv->kernel_context);
  225. }
  226. /* overlay needs to be disable in OCMD reg */
  227. static int intel_overlay_on(struct intel_overlay *overlay)
  228. {
  229. struct drm_i915_private *dev_priv = overlay->i915;
  230. struct i915_request *rq;
  231. u32 *cs;
  232. WARN_ON(overlay->active);
  233. rq = alloc_request(overlay);
  234. if (IS_ERR(rq))
  235. return PTR_ERR(rq);
  236. cs = intel_ring_begin(rq, 4);
  237. if (IS_ERR(cs)) {
  238. i915_request_add(rq);
  239. return PTR_ERR(cs);
  240. }
  241. overlay->active = true;
  242. if (IS_I830(dev_priv))
  243. i830_overlay_clock_gating(dev_priv, false);
  244. *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON;
  245. *cs++ = overlay->flip_addr | OFC_UPDATE;
  246. *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
  247. *cs++ = MI_NOOP;
  248. intel_ring_advance(rq, cs);
  249. return intel_overlay_do_wait_request(overlay, rq, NULL);
  250. }
  251. static void intel_overlay_flip_prepare(struct intel_overlay *overlay,
  252. struct i915_vma *vma)
  253. {
  254. enum pipe pipe = overlay->crtc->pipe;
  255. WARN_ON(overlay->old_vma);
  256. i915_gem_track_fb(overlay->vma ? overlay->vma->obj : NULL,
  257. vma ? vma->obj : NULL,
  258. INTEL_FRONTBUFFER_OVERLAY(pipe));
  259. intel_frontbuffer_flip_prepare(overlay->i915,
  260. INTEL_FRONTBUFFER_OVERLAY(pipe));
  261. overlay->old_vma = overlay->vma;
  262. if (vma)
  263. overlay->vma = i915_vma_get(vma);
  264. else
  265. overlay->vma = NULL;
  266. }
  267. /* overlay needs to be enabled in OCMD reg */
  268. static int intel_overlay_continue(struct intel_overlay *overlay,
  269. struct i915_vma *vma,
  270. bool load_polyphase_filter)
  271. {
  272. struct drm_i915_private *dev_priv = overlay->i915;
  273. struct i915_request *rq;
  274. u32 flip_addr = overlay->flip_addr;
  275. u32 tmp, *cs;
  276. WARN_ON(!overlay->active);
  277. if (load_polyphase_filter)
  278. flip_addr |= OFC_UPDATE;
  279. /* check for underruns */
  280. tmp = I915_READ(DOVSTA);
  281. if (tmp & (1 << 17))
  282. DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
  283. rq = alloc_request(overlay);
  284. if (IS_ERR(rq))
  285. return PTR_ERR(rq);
  286. cs = intel_ring_begin(rq, 2);
  287. if (IS_ERR(cs)) {
  288. i915_request_add(rq);
  289. return PTR_ERR(cs);
  290. }
  291. *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
  292. *cs++ = flip_addr;
  293. intel_ring_advance(rq, cs);
  294. intel_overlay_flip_prepare(overlay, vma);
  295. intel_overlay_submit_request(overlay, rq, NULL);
  296. return 0;
  297. }
  298. static void intel_overlay_release_old_vma(struct intel_overlay *overlay)
  299. {
  300. struct i915_vma *vma;
  301. vma = fetch_and_zero(&overlay->old_vma);
  302. if (WARN_ON(!vma))
  303. return;
  304. intel_frontbuffer_flip_complete(overlay->i915,
  305. INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
  306. i915_gem_object_unpin_from_display_plane(vma);
  307. i915_vma_put(vma);
  308. }
  309. static void intel_overlay_release_old_vid_tail(struct i915_gem_active *active,
  310. struct i915_request *rq)
  311. {
  312. struct intel_overlay *overlay =
  313. container_of(active, typeof(*overlay), last_flip);
  314. intel_overlay_release_old_vma(overlay);
  315. }
  316. static void intel_overlay_off_tail(struct i915_gem_active *active,
  317. struct i915_request *rq)
  318. {
  319. struct intel_overlay *overlay =
  320. container_of(active, typeof(*overlay), last_flip);
  321. struct drm_i915_private *dev_priv = overlay->i915;
  322. intel_overlay_release_old_vma(overlay);
  323. overlay->crtc->overlay = NULL;
  324. overlay->crtc = NULL;
  325. overlay->active = false;
  326. if (IS_I830(dev_priv))
  327. i830_overlay_clock_gating(dev_priv, true);
  328. }
  329. /* overlay needs to be disabled in OCMD reg */
  330. static int intel_overlay_off(struct intel_overlay *overlay)
  331. {
  332. struct i915_request *rq;
  333. u32 *cs, flip_addr = overlay->flip_addr;
  334. WARN_ON(!overlay->active);
  335. /* According to intel docs the overlay hw may hang (when switching
  336. * off) without loading the filter coeffs. It is however unclear whether
  337. * this applies to the disabling of the overlay or to the switching off
  338. * of the hw. Do it in both cases */
  339. flip_addr |= OFC_UPDATE;
  340. rq = alloc_request(overlay);
  341. if (IS_ERR(rq))
  342. return PTR_ERR(rq);
  343. cs = intel_ring_begin(rq, 6);
  344. if (IS_ERR(cs)) {
  345. i915_request_add(rq);
  346. return PTR_ERR(cs);
  347. }
  348. /* wait for overlay to go idle */
  349. *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
  350. *cs++ = flip_addr;
  351. *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
  352. /* turn overlay off */
  353. *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF;
  354. *cs++ = flip_addr;
  355. *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
  356. intel_ring_advance(rq, cs);
  357. intel_overlay_flip_prepare(overlay, NULL);
  358. return intel_overlay_do_wait_request(overlay, rq,
  359. intel_overlay_off_tail);
  360. }
  361. /* recover from an interruption due to a signal
  362. * We have to be careful not to repeat work forever an make forward progess. */
  363. static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
  364. {
  365. return i915_gem_active_retire(&overlay->last_flip,
  366. &overlay->i915->drm.struct_mutex);
  367. }
  368. /* Wait for pending overlay flip and release old frame.
  369. * Needs to be called before the overlay register are changed
  370. * via intel_overlay_(un)map_regs
  371. */
  372. static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
  373. {
  374. struct drm_i915_private *dev_priv = overlay->i915;
  375. u32 *cs;
  376. int ret;
  377. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  378. /* Only wait if there is actually an old frame to release to
  379. * guarantee forward progress.
  380. */
  381. if (!overlay->old_vma)
  382. return 0;
  383. if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
  384. /* synchronous slowpath */
  385. struct i915_request *rq;
  386. rq = alloc_request(overlay);
  387. if (IS_ERR(rq))
  388. return PTR_ERR(rq);
  389. cs = intel_ring_begin(rq, 2);
  390. if (IS_ERR(cs)) {
  391. i915_request_add(rq);
  392. return PTR_ERR(cs);
  393. }
  394. *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
  395. *cs++ = MI_NOOP;
  396. intel_ring_advance(rq, cs);
  397. ret = intel_overlay_do_wait_request(overlay, rq,
  398. intel_overlay_release_old_vid_tail);
  399. if (ret)
  400. return ret;
  401. } else
  402. intel_overlay_release_old_vid_tail(&overlay->last_flip, NULL);
  403. return 0;
  404. }
  405. void intel_overlay_reset(struct drm_i915_private *dev_priv)
  406. {
  407. struct intel_overlay *overlay = dev_priv->overlay;
  408. if (!overlay)
  409. return;
  410. intel_overlay_release_old_vid(overlay);
  411. overlay->old_xscale = 0;
  412. overlay->old_yscale = 0;
  413. overlay->crtc = NULL;
  414. overlay->active = false;
  415. }
  416. static int packed_depth_bytes(u32 format)
  417. {
  418. switch (format & I915_OVERLAY_DEPTH_MASK) {
  419. case I915_OVERLAY_YUV422:
  420. return 4;
  421. case I915_OVERLAY_YUV411:
  422. /* return 6; not implemented */
  423. default:
  424. return -EINVAL;
  425. }
  426. }
  427. static int packed_width_bytes(u32 format, short width)
  428. {
  429. switch (format & I915_OVERLAY_DEPTH_MASK) {
  430. case I915_OVERLAY_YUV422:
  431. return width << 1;
  432. default:
  433. return -EINVAL;
  434. }
  435. }
  436. static int uv_hsubsampling(u32 format)
  437. {
  438. switch (format & I915_OVERLAY_DEPTH_MASK) {
  439. case I915_OVERLAY_YUV422:
  440. case I915_OVERLAY_YUV420:
  441. return 2;
  442. case I915_OVERLAY_YUV411:
  443. case I915_OVERLAY_YUV410:
  444. return 4;
  445. default:
  446. return -EINVAL;
  447. }
  448. }
  449. static int uv_vsubsampling(u32 format)
  450. {
  451. switch (format & I915_OVERLAY_DEPTH_MASK) {
  452. case I915_OVERLAY_YUV420:
  453. case I915_OVERLAY_YUV410:
  454. return 2;
  455. case I915_OVERLAY_YUV422:
  456. case I915_OVERLAY_YUV411:
  457. return 1;
  458. default:
  459. return -EINVAL;
  460. }
  461. }
  462. static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
  463. {
  464. u32 sw;
  465. if (IS_GEN2(dev_priv))
  466. sw = ALIGN((offset & 31) + width, 32);
  467. else
  468. sw = ALIGN((offset & 63) + width, 64);
  469. if (sw == 0)
  470. return 0;
  471. return (sw - 32) >> 3;
  472. }
  473. static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
  474. [ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
  475. [ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
  476. [ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
  477. [ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
  478. [ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
  479. [ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
  480. [ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
  481. [ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
  482. [ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
  483. [ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
  484. [10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
  485. [11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
  486. [12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
  487. [13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
  488. [14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
  489. [15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
  490. [16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
  491. };
  492. static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
  493. [ 0] = { 0x3000, 0x1800, 0x1800, },
  494. [ 1] = { 0xb000, 0x18d0, 0x2e60, },
  495. [ 2] = { 0xb000, 0x1990, 0x2ce0, },
  496. [ 3] = { 0xb020, 0x1a68, 0x2b40, },
  497. [ 4] = { 0xb040, 0x1b20, 0x29e0, },
  498. [ 5] = { 0xb060, 0x1bd8, 0x2880, },
  499. [ 6] = { 0xb080, 0x1c88, 0x3e60, },
  500. [ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
  501. [ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
  502. [ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
  503. [10] = { 0xb100, 0x1eb8, 0x3620, },
  504. [11] = { 0xb100, 0x1f18, 0x34a0, },
  505. [12] = { 0xb100, 0x1f68, 0x3360, },
  506. [13] = { 0xb0e0, 0x1fa8, 0x3240, },
  507. [14] = { 0xb0c0, 0x1fe0, 0x3140, },
  508. [15] = { 0xb060, 0x1ff0, 0x30a0, },
  509. [16] = { 0x3000, 0x0800, 0x3000, },
  510. };
  511. static void update_polyphase_filter(struct overlay_registers __iomem *regs)
  512. {
  513. memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
  514. memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
  515. sizeof(uv_static_hcoeffs));
  516. }
  517. static bool update_scaling_factors(struct intel_overlay *overlay,
  518. struct overlay_registers __iomem *regs,
  519. struct drm_intel_overlay_put_image *params)
  520. {
  521. /* fixed point with a 12 bit shift */
  522. u32 xscale, yscale, xscale_UV, yscale_UV;
  523. #define FP_SHIFT 12
  524. #define FRACT_MASK 0xfff
  525. bool scale_changed = false;
  526. int uv_hscale = uv_hsubsampling(params->flags);
  527. int uv_vscale = uv_vsubsampling(params->flags);
  528. if (params->dst_width > 1)
  529. xscale = ((params->src_scan_width - 1) << FP_SHIFT) /
  530. params->dst_width;
  531. else
  532. xscale = 1 << FP_SHIFT;
  533. if (params->dst_height > 1)
  534. yscale = ((params->src_scan_height - 1) << FP_SHIFT) /
  535. params->dst_height;
  536. else
  537. yscale = 1 << FP_SHIFT;
  538. /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
  539. xscale_UV = xscale/uv_hscale;
  540. yscale_UV = yscale/uv_vscale;
  541. /* make the Y scale to UV scale ratio an exact multiply */
  542. xscale = xscale_UV * uv_hscale;
  543. yscale = yscale_UV * uv_vscale;
  544. /*} else {
  545. xscale_UV = 0;
  546. yscale_UV = 0;
  547. }*/
  548. if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
  549. scale_changed = true;
  550. overlay->old_xscale = xscale;
  551. overlay->old_yscale = yscale;
  552. iowrite32(((yscale & FRACT_MASK) << 20) |
  553. ((xscale >> FP_SHIFT) << 16) |
  554. ((xscale & FRACT_MASK) << 3),
  555. &regs->YRGBSCALE);
  556. iowrite32(((yscale_UV & FRACT_MASK) << 20) |
  557. ((xscale_UV >> FP_SHIFT) << 16) |
  558. ((xscale_UV & FRACT_MASK) << 3),
  559. &regs->UVSCALE);
  560. iowrite32((((yscale >> FP_SHIFT) << 16) |
  561. ((yscale_UV >> FP_SHIFT) << 0)),
  562. &regs->UVSCALEV);
  563. if (scale_changed)
  564. update_polyphase_filter(regs);
  565. return scale_changed;
  566. }
  567. static void update_colorkey(struct intel_overlay *overlay,
  568. struct overlay_registers __iomem *regs)
  569. {
  570. const struct intel_plane_state *state =
  571. to_intel_plane_state(overlay->crtc->base.primary->state);
  572. u32 key = overlay->color_key;
  573. u32 format = 0;
  574. u32 flags = 0;
  575. if (overlay->color_key_enabled)
  576. flags |= DST_KEY_ENABLE;
  577. if (state->base.visible)
  578. format = state->base.fb->format->format;
  579. switch (format) {
  580. case DRM_FORMAT_C8:
  581. key = 0;
  582. flags |= CLK_RGB8I_MASK;
  583. break;
  584. case DRM_FORMAT_XRGB1555:
  585. key = RGB15_TO_COLORKEY(key);
  586. flags |= CLK_RGB15_MASK;
  587. break;
  588. case DRM_FORMAT_RGB565:
  589. key = RGB16_TO_COLORKEY(key);
  590. flags |= CLK_RGB16_MASK;
  591. break;
  592. default:
  593. flags |= CLK_RGB24_MASK;
  594. break;
  595. }
  596. iowrite32(key, &regs->DCLRKV);
  597. iowrite32(flags, &regs->DCLRKM);
  598. }
  599. static u32 overlay_cmd_reg(struct drm_intel_overlay_put_image *params)
  600. {
  601. u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
  602. if (params->flags & I915_OVERLAY_YUV_PLANAR) {
  603. switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
  604. case I915_OVERLAY_YUV422:
  605. cmd |= OCMD_YUV_422_PLANAR;
  606. break;
  607. case I915_OVERLAY_YUV420:
  608. cmd |= OCMD_YUV_420_PLANAR;
  609. break;
  610. case I915_OVERLAY_YUV411:
  611. case I915_OVERLAY_YUV410:
  612. cmd |= OCMD_YUV_410_PLANAR;
  613. break;
  614. }
  615. } else { /* YUV packed */
  616. switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
  617. case I915_OVERLAY_YUV422:
  618. cmd |= OCMD_YUV_422_PACKED;
  619. break;
  620. case I915_OVERLAY_YUV411:
  621. cmd |= OCMD_YUV_411_PACKED;
  622. break;
  623. }
  624. switch (params->flags & I915_OVERLAY_SWAP_MASK) {
  625. case I915_OVERLAY_NO_SWAP:
  626. break;
  627. case I915_OVERLAY_UV_SWAP:
  628. cmd |= OCMD_UV_SWAP;
  629. break;
  630. case I915_OVERLAY_Y_SWAP:
  631. cmd |= OCMD_Y_SWAP;
  632. break;
  633. case I915_OVERLAY_Y_AND_UV_SWAP:
  634. cmd |= OCMD_Y_AND_UV_SWAP;
  635. break;
  636. }
  637. }
  638. return cmd;
  639. }
  640. static int intel_overlay_do_put_image(struct intel_overlay *overlay,
  641. struct drm_i915_gem_object *new_bo,
  642. struct drm_intel_overlay_put_image *params)
  643. {
  644. struct overlay_registers __iomem *regs = overlay->regs;
  645. struct drm_i915_private *dev_priv = overlay->i915;
  646. u32 swidth, swidthsw, sheight, ostride;
  647. enum pipe pipe = overlay->crtc->pipe;
  648. bool scale_changed = false;
  649. struct i915_vma *vma;
  650. int ret, tmp_width;
  651. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  652. WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
  653. ret = intel_overlay_release_old_vid(overlay);
  654. if (ret != 0)
  655. return ret;
  656. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  657. vma = i915_gem_object_pin_to_display_plane(new_bo,
  658. 0, NULL, PIN_MAPPABLE);
  659. if (IS_ERR(vma)) {
  660. ret = PTR_ERR(vma);
  661. goto out_pin_section;
  662. }
  663. intel_fb_obj_flush(new_bo, ORIGIN_DIRTYFB);
  664. ret = i915_vma_put_fence(vma);
  665. if (ret)
  666. goto out_unpin;
  667. if (!overlay->active) {
  668. u32 oconfig;
  669. oconfig = OCONF_CC_OUT_8BIT;
  670. if (IS_GEN4(dev_priv))
  671. oconfig |= OCONF_CSC_MODE_BT709;
  672. oconfig |= pipe == 0 ?
  673. OCONF_PIPE_A : OCONF_PIPE_B;
  674. iowrite32(oconfig, &regs->OCONFIG);
  675. ret = intel_overlay_on(overlay);
  676. if (ret != 0)
  677. goto out_unpin;
  678. }
  679. iowrite32(params->dst_y << 16 | params->dst_x, &regs->DWINPOS);
  680. iowrite32(params->dst_height << 16 | params->dst_width, &regs->DWINSZ);
  681. if (params->flags & I915_OVERLAY_YUV_PACKED)
  682. tmp_width = packed_width_bytes(params->flags,
  683. params->src_width);
  684. else
  685. tmp_width = params->src_width;
  686. swidth = params->src_width;
  687. swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
  688. sheight = params->src_height;
  689. iowrite32(i915_ggtt_offset(vma) + params->offset_Y, &regs->OBUF_0Y);
  690. ostride = params->stride_Y;
  691. if (params->flags & I915_OVERLAY_YUV_PLANAR) {
  692. int uv_hscale = uv_hsubsampling(params->flags);
  693. int uv_vscale = uv_vsubsampling(params->flags);
  694. u32 tmp_U, tmp_V;
  695. swidth |= (params->src_width / uv_hscale) << 16;
  696. sheight |= (params->src_height / uv_vscale) << 16;
  697. tmp_U = calc_swidthsw(dev_priv, params->offset_U,
  698. params->src_width / uv_hscale);
  699. tmp_V = calc_swidthsw(dev_priv, params->offset_V,
  700. params->src_width / uv_hscale);
  701. swidthsw |= max(tmp_U, tmp_V) << 16;
  702. iowrite32(i915_ggtt_offset(vma) + params->offset_U,
  703. &regs->OBUF_0U);
  704. iowrite32(i915_ggtt_offset(vma) + params->offset_V,
  705. &regs->OBUF_0V);
  706. ostride |= params->stride_UV << 16;
  707. }
  708. iowrite32(swidth, &regs->SWIDTH);
  709. iowrite32(swidthsw, &regs->SWIDTHSW);
  710. iowrite32(sheight, &regs->SHEIGHT);
  711. iowrite32(ostride, &regs->OSTRIDE);
  712. scale_changed = update_scaling_factors(overlay, regs, params);
  713. update_colorkey(overlay, regs);
  714. iowrite32(overlay_cmd_reg(params), &regs->OCMD);
  715. ret = intel_overlay_continue(overlay, vma, scale_changed);
  716. if (ret)
  717. goto out_unpin;
  718. return 0;
  719. out_unpin:
  720. i915_gem_object_unpin_from_display_plane(vma);
  721. out_pin_section:
  722. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  723. return ret;
  724. }
  725. int intel_overlay_switch_off(struct intel_overlay *overlay)
  726. {
  727. struct drm_i915_private *dev_priv = overlay->i915;
  728. int ret;
  729. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  730. WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
  731. ret = intel_overlay_recover_from_interrupt(overlay);
  732. if (ret != 0)
  733. return ret;
  734. if (!overlay->active)
  735. return 0;
  736. ret = intel_overlay_release_old_vid(overlay);
  737. if (ret != 0)
  738. return ret;
  739. iowrite32(0, &overlay->regs->OCMD);
  740. return intel_overlay_off(overlay);
  741. }
  742. static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
  743. struct intel_crtc *crtc)
  744. {
  745. if (!crtc->active)
  746. return -EINVAL;
  747. /* can't use the overlay with double wide pipe */
  748. if (crtc->config->double_wide)
  749. return -EINVAL;
  750. return 0;
  751. }
  752. static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
  753. {
  754. struct drm_i915_private *dev_priv = overlay->i915;
  755. u32 pfit_control = I915_READ(PFIT_CONTROL);
  756. u32 ratio;
  757. /* XXX: This is not the same logic as in the xorg driver, but more in
  758. * line with the intel documentation for the i965
  759. */
  760. if (INTEL_GEN(dev_priv) >= 4) {
  761. /* on i965 use the PGM reg to read out the autoscaler values */
  762. ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
  763. } else {
  764. if (pfit_control & VERT_AUTO_SCALE)
  765. ratio = I915_READ(PFIT_AUTO_RATIOS);
  766. else
  767. ratio = I915_READ(PFIT_PGM_RATIOS);
  768. ratio >>= PFIT_VERT_SCALE_SHIFT;
  769. }
  770. overlay->pfit_vscale_ratio = ratio;
  771. }
  772. static int check_overlay_dst(struct intel_overlay *overlay,
  773. struct drm_intel_overlay_put_image *rec)
  774. {
  775. const struct intel_crtc_state *pipe_config =
  776. overlay->crtc->config;
  777. if (rec->dst_x < pipe_config->pipe_src_w &&
  778. rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
  779. rec->dst_y < pipe_config->pipe_src_h &&
  780. rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
  781. return 0;
  782. else
  783. return -EINVAL;
  784. }
  785. static int check_overlay_scaling(struct drm_intel_overlay_put_image *rec)
  786. {
  787. u32 tmp;
  788. /* downscaling limit is 8.0 */
  789. tmp = ((rec->src_scan_height << 16) / rec->dst_height) >> 16;
  790. if (tmp > 7)
  791. return -EINVAL;
  792. tmp = ((rec->src_scan_width << 16) / rec->dst_width) >> 16;
  793. if (tmp > 7)
  794. return -EINVAL;
  795. return 0;
  796. }
  797. static int check_overlay_src(struct drm_i915_private *dev_priv,
  798. struct drm_intel_overlay_put_image *rec,
  799. struct drm_i915_gem_object *new_bo)
  800. {
  801. int uv_hscale = uv_hsubsampling(rec->flags);
  802. int uv_vscale = uv_vsubsampling(rec->flags);
  803. u32 stride_mask;
  804. int depth;
  805. u32 tmp;
  806. /* check src dimensions */
  807. if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
  808. if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
  809. rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
  810. return -EINVAL;
  811. } else {
  812. if (rec->src_height > IMAGE_MAX_HEIGHT ||
  813. rec->src_width > IMAGE_MAX_WIDTH)
  814. return -EINVAL;
  815. }
  816. /* better safe than sorry, use 4 as the maximal subsampling ratio */
  817. if (rec->src_height < N_VERT_Y_TAPS*4 ||
  818. rec->src_width < N_HORIZ_Y_TAPS*4)
  819. return -EINVAL;
  820. /* check alignment constraints */
  821. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  822. case I915_OVERLAY_RGB:
  823. /* not implemented */
  824. return -EINVAL;
  825. case I915_OVERLAY_YUV_PACKED:
  826. if (uv_vscale != 1)
  827. return -EINVAL;
  828. depth = packed_depth_bytes(rec->flags);
  829. if (depth < 0)
  830. return depth;
  831. /* ignore UV planes */
  832. rec->stride_UV = 0;
  833. rec->offset_U = 0;
  834. rec->offset_V = 0;
  835. /* check pixel alignment */
  836. if (rec->offset_Y % depth)
  837. return -EINVAL;
  838. break;
  839. case I915_OVERLAY_YUV_PLANAR:
  840. if (uv_vscale < 0 || uv_hscale < 0)
  841. return -EINVAL;
  842. /* no offset restrictions for planar formats */
  843. break;
  844. default:
  845. return -EINVAL;
  846. }
  847. if (rec->src_width % uv_hscale)
  848. return -EINVAL;
  849. /* stride checking */
  850. if (IS_I830(dev_priv) || IS_I845G(dev_priv))
  851. stride_mask = 255;
  852. else
  853. stride_mask = 63;
  854. if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
  855. return -EINVAL;
  856. if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
  857. return -EINVAL;
  858. tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
  859. 4096 : 8192;
  860. if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
  861. return -EINVAL;
  862. /* check buffer dimensions */
  863. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  864. case I915_OVERLAY_RGB:
  865. case I915_OVERLAY_YUV_PACKED:
  866. /* always 4 Y values per depth pixels */
  867. if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
  868. return -EINVAL;
  869. tmp = rec->stride_Y*rec->src_height;
  870. if (rec->offset_Y + tmp > new_bo->base.size)
  871. return -EINVAL;
  872. break;
  873. case I915_OVERLAY_YUV_PLANAR:
  874. if (rec->src_width > rec->stride_Y)
  875. return -EINVAL;
  876. if (rec->src_width/uv_hscale > rec->stride_UV)
  877. return -EINVAL;
  878. tmp = rec->stride_Y * rec->src_height;
  879. if (rec->offset_Y + tmp > new_bo->base.size)
  880. return -EINVAL;
  881. tmp = rec->stride_UV * (rec->src_height / uv_vscale);
  882. if (rec->offset_U + tmp > new_bo->base.size ||
  883. rec->offset_V + tmp > new_bo->base.size)
  884. return -EINVAL;
  885. break;
  886. }
  887. return 0;
  888. }
  889. int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
  890. struct drm_file *file_priv)
  891. {
  892. struct drm_intel_overlay_put_image *params = data;
  893. struct drm_i915_private *dev_priv = to_i915(dev);
  894. struct intel_overlay *overlay;
  895. struct drm_crtc *drmmode_crtc;
  896. struct intel_crtc *crtc;
  897. struct drm_i915_gem_object *new_bo;
  898. int ret;
  899. overlay = dev_priv->overlay;
  900. if (!overlay) {
  901. DRM_DEBUG("userspace bug: no overlay\n");
  902. return -ENODEV;
  903. }
  904. if (!(params->flags & I915_OVERLAY_ENABLE)) {
  905. drm_modeset_lock_all(dev);
  906. mutex_lock(&dev->struct_mutex);
  907. ret = intel_overlay_switch_off(overlay);
  908. mutex_unlock(&dev->struct_mutex);
  909. drm_modeset_unlock_all(dev);
  910. return ret;
  911. }
  912. drmmode_crtc = drm_crtc_find(dev, file_priv, params->crtc_id);
  913. if (!drmmode_crtc)
  914. return -ENOENT;
  915. crtc = to_intel_crtc(drmmode_crtc);
  916. new_bo = i915_gem_object_lookup(file_priv, params->bo_handle);
  917. if (!new_bo)
  918. return -ENOENT;
  919. drm_modeset_lock_all(dev);
  920. mutex_lock(&dev->struct_mutex);
  921. if (i915_gem_object_is_tiled(new_bo)) {
  922. DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
  923. ret = -EINVAL;
  924. goto out_unlock;
  925. }
  926. ret = intel_overlay_recover_from_interrupt(overlay);
  927. if (ret != 0)
  928. goto out_unlock;
  929. if (overlay->crtc != crtc) {
  930. ret = intel_overlay_switch_off(overlay);
  931. if (ret != 0)
  932. goto out_unlock;
  933. ret = check_overlay_possible_on_crtc(overlay, crtc);
  934. if (ret != 0)
  935. goto out_unlock;
  936. overlay->crtc = crtc;
  937. crtc->overlay = overlay;
  938. /* line too wide, i.e. one-line-mode */
  939. if (crtc->config->pipe_src_w > 1024 &&
  940. crtc->config->gmch_pfit.control & PFIT_ENABLE) {
  941. overlay->pfit_active = true;
  942. update_pfit_vscale_ratio(overlay);
  943. } else
  944. overlay->pfit_active = false;
  945. }
  946. ret = check_overlay_dst(overlay, params);
  947. if (ret != 0)
  948. goto out_unlock;
  949. if (overlay->pfit_active) {
  950. params->dst_y = (((u32)params->dst_y << 12) /
  951. overlay->pfit_vscale_ratio);
  952. /* shifting right rounds downwards, so add 1 */
  953. params->dst_height = (((u32)params->dst_height << 12) /
  954. overlay->pfit_vscale_ratio) + 1;
  955. }
  956. if (params->src_scan_height > params->src_height ||
  957. params->src_scan_width > params->src_width) {
  958. ret = -EINVAL;
  959. goto out_unlock;
  960. }
  961. ret = check_overlay_src(dev_priv, params, new_bo);
  962. if (ret != 0)
  963. goto out_unlock;
  964. /* Check scaling after src size to prevent a divide-by-zero. */
  965. ret = check_overlay_scaling(params);
  966. if (ret != 0)
  967. goto out_unlock;
  968. ret = intel_overlay_do_put_image(overlay, new_bo, params);
  969. if (ret != 0)
  970. goto out_unlock;
  971. mutex_unlock(&dev->struct_mutex);
  972. drm_modeset_unlock_all(dev);
  973. i915_gem_object_put(new_bo);
  974. return 0;
  975. out_unlock:
  976. mutex_unlock(&dev->struct_mutex);
  977. drm_modeset_unlock_all(dev);
  978. i915_gem_object_put(new_bo);
  979. return ret;
  980. }
  981. static void update_reg_attrs(struct intel_overlay *overlay,
  982. struct overlay_registers __iomem *regs)
  983. {
  984. iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
  985. &regs->OCLRC0);
  986. iowrite32(overlay->saturation, &regs->OCLRC1);
  987. }
  988. static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
  989. {
  990. int i;
  991. if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
  992. return false;
  993. for (i = 0; i < 3; i++) {
  994. if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
  995. return false;
  996. }
  997. return true;
  998. }
  999. static bool check_gamma5_errata(u32 gamma5)
  1000. {
  1001. int i;
  1002. for (i = 0; i < 3; i++) {
  1003. if (((gamma5 >> i*8) & 0xff) == 0x80)
  1004. return false;
  1005. }
  1006. return true;
  1007. }
  1008. static int check_gamma(struct drm_intel_overlay_attrs *attrs)
  1009. {
  1010. if (!check_gamma_bounds(0, attrs->gamma0) ||
  1011. !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
  1012. !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
  1013. !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
  1014. !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
  1015. !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
  1016. !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
  1017. return -EINVAL;
  1018. if (!check_gamma5_errata(attrs->gamma5))
  1019. return -EINVAL;
  1020. return 0;
  1021. }
  1022. int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
  1023. struct drm_file *file_priv)
  1024. {
  1025. struct drm_intel_overlay_attrs *attrs = data;
  1026. struct drm_i915_private *dev_priv = to_i915(dev);
  1027. struct intel_overlay *overlay;
  1028. int ret;
  1029. overlay = dev_priv->overlay;
  1030. if (!overlay) {
  1031. DRM_DEBUG("userspace bug: no overlay\n");
  1032. return -ENODEV;
  1033. }
  1034. drm_modeset_lock_all(dev);
  1035. mutex_lock(&dev->struct_mutex);
  1036. ret = -EINVAL;
  1037. if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
  1038. attrs->color_key = overlay->color_key;
  1039. attrs->brightness = overlay->brightness;
  1040. attrs->contrast = overlay->contrast;
  1041. attrs->saturation = overlay->saturation;
  1042. if (!IS_GEN2(dev_priv)) {
  1043. attrs->gamma0 = I915_READ(OGAMC0);
  1044. attrs->gamma1 = I915_READ(OGAMC1);
  1045. attrs->gamma2 = I915_READ(OGAMC2);
  1046. attrs->gamma3 = I915_READ(OGAMC3);
  1047. attrs->gamma4 = I915_READ(OGAMC4);
  1048. attrs->gamma5 = I915_READ(OGAMC5);
  1049. }
  1050. } else {
  1051. if (attrs->brightness < -128 || attrs->brightness > 127)
  1052. goto out_unlock;
  1053. if (attrs->contrast > 255)
  1054. goto out_unlock;
  1055. if (attrs->saturation > 1023)
  1056. goto out_unlock;
  1057. overlay->color_key = attrs->color_key;
  1058. overlay->brightness = attrs->brightness;
  1059. overlay->contrast = attrs->contrast;
  1060. overlay->saturation = attrs->saturation;
  1061. update_reg_attrs(overlay, overlay->regs);
  1062. if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
  1063. if (IS_GEN2(dev_priv))
  1064. goto out_unlock;
  1065. if (overlay->active) {
  1066. ret = -EBUSY;
  1067. goto out_unlock;
  1068. }
  1069. ret = check_gamma(attrs);
  1070. if (ret)
  1071. goto out_unlock;
  1072. I915_WRITE(OGAMC0, attrs->gamma0);
  1073. I915_WRITE(OGAMC1, attrs->gamma1);
  1074. I915_WRITE(OGAMC2, attrs->gamma2);
  1075. I915_WRITE(OGAMC3, attrs->gamma3);
  1076. I915_WRITE(OGAMC4, attrs->gamma4);
  1077. I915_WRITE(OGAMC5, attrs->gamma5);
  1078. }
  1079. }
  1080. overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
  1081. ret = 0;
  1082. out_unlock:
  1083. mutex_unlock(&dev->struct_mutex);
  1084. drm_modeset_unlock_all(dev);
  1085. return ret;
  1086. }
  1087. static int get_registers(struct intel_overlay *overlay, bool use_phys)
  1088. {
  1089. struct drm_i915_gem_object *obj;
  1090. struct i915_vma *vma;
  1091. int err;
  1092. obj = i915_gem_object_create_stolen(overlay->i915, PAGE_SIZE);
  1093. if (obj == NULL)
  1094. obj = i915_gem_object_create_internal(overlay->i915, PAGE_SIZE);
  1095. if (IS_ERR(obj))
  1096. return PTR_ERR(obj);
  1097. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
  1098. if (IS_ERR(vma)) {
  1099. err = PTR_ERR(vma);
  1100. goto err_put_bo;
  1101. }
  1102. if (use_phys)
  1103. overlay->flip_addr = sg_dma_address(obj->mm.pages->sgl);
  1104. else
  1105. overlay->flip_addr = i915_ggtt_offset(vma);
  1106. overlay->regs = i915_vma_pin_iomap(vma);
  1107. i915_vma_unpin(vma);
  1108. if (IS_ERR(overlay->regs)) {
  1109. err = PTR_ERR(overlay->regs);
  1110. goto err_put_bo;
  1111. }
  1112. overlay->reg_bo = obj;
  1113. return 0;
  1114. err_put_bo:
  1115. i915_gem_object_put(obj);
  1116. return err;
  1117. }
  1118. void intel_setup_overlay(struct drm_i915_private *dev_priv)
  1119. {
  1120. struct intel_overlay *overlay;
  1121. int ret;
  1122. if (!HAS_OVERLAY(dev_priv))
  1123. return;
  1124. overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
  1125. if (!overlay)
  1126. return;
  1127. overlay->i915 = dev_priv;
  1128. overlay->color_key = 0x0101fe;
  1129. overlay->color_key_enabled = true;
  1130. overlay->brightness = -19;
  1131. overlay->contrast = 75;
  1132. overlay->saturation = 146;
  1133. init_request_active(&overlay->last_flip, NULL);
  1134. mutex_lock(&dev_priv->drm.struct_mutex);
  1135. ret = get_registers(overlay, OVERLAY_NEEDS_PHYSICAL(dev_priv));
  1136. if (ret)
  1137. goto out_free;
  1138. ret = i915_gem_object_set_to_gtt_domain(overlay->reg_bo, true);
  1139. if (ret)
  1140. goto out_reg_bo;
  1141. mutex_unlock(&dev_priv->drm.struct_mutex);
  1142. memset_io(overlay->regs, 0, sizeof(struct overlay_registers));
  1143. update_polyphase_filter(overlay->regs);
  1144. update_reg_attrs(overlay, overlay->regs);
  1145. dev_priv->overlay = overlay;
  1146. DRM_INFO("Initialized overlay support.\n");
  1147. return;
  1148. out_reg_bo:
  1149. i915_gem_object_put(overlay->reg_bo);
  1150. out_free:
  1151. mutex_unlock(&dev_priv->drm.struct_mutex);
  1152. kfree(overlay);
  1153. }
  1154. void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
  1155. {
  1156. struct intel_overlay *overlay;
  1157. overlay = fetch_and_zero(&dev_priv->overlay);
  1158. if (!overlay)
  1159. return;
  1160. /*
  1161. * The bo's should be free'd by the generic code already.
  1162. * Furthermore modesetting teardown happens beforehand so the
  1163. * hardware should be off already.
  1164. */
  1165. WARN_ON(overlay->active);
  1166. i915_gem_object_put(overlay->reg_bo);
  1167. kfree(overlay);
  1168. }
  1169. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  1170. struct intel_overlay_error_state {
  1171. struct overlay_registers regs;
  1172. unsigned long base;
  1173. u32 dovsta;
  1174. u32 isr;
  1175. };
  1176. struct intel_overlay_error_state *
  1177. intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
  1178. {
  1179. struct intel_overlay *overlay = dev_priv->overlay;
  1180. struct intel_overlay_error_state *error;
  1181. if (!overlay || !overlay->active)
  1182. return NULL;
  1183. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  1184. if (error == NULL)
  1185. return NULL;
  1186. error->dovsta = I915_READ(DOVSTA);
  1187. error->isr = I915_READ(ISR);
  1188. error->base = overlay->flip_addr;
  1189. memcpy_fromio(&error->regs, overlay->regs, sizeof(error->regs));
  1190. return error;
  1191. }
  1192. void
  1193. intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
  1194. struct intel_overlay_error_state *error)
  1195. {
  1196. i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
  1197. error->dovsta, error->isr);
  1198. i915_error_printf(m, " Register file at 0x%08lx:\n",
  1199. error->base);
  1200. #define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)
  1201. P(OBUF_0Y);
  1202. P(OBUF_1Y);
  1203. P(OBUF_0U);
  1204. P(OBUF_0V);
  1205. P(OBUF_1U);
  1206. P(OBUF_1V);
  1207. P(OSTRIDE);
  1208. P(YRGB_VPH);
  1209. P(UV_VPH);
  1210. P(HORZ_PH);
  1211. P(INIT_PHS);
  1212. P(DWINPOS);
  1213. P(DWINSZ);
  1214. P(SWIDTH);
  1215. P(SWIDTHSW);
  1216. P(SHEIGHT);
  1217. P(YRGBSCALE);
  1218. P(UVSCALE);
  1219. P(OCLRC0);
  1220. P(OCLRC1);
  1221. P(DCLRKV);
  1222. P(DCLRKM);
  1223. P(SCLRKVH);
  1224. P(SCLRKVL);
  1225. P(SCLRKEN);
  1226. P(OCONFIG);
  1227. P(OCMD);
  1228. P(OSTART_0Y);
  1229. P(OSTART_1Y);
  1230. P(OSTART_0U);
  1231. P(OSTART_0V);
  1232. P(OSTART_1U);
  1233. P(OSTART_1V);
  1234. P(OTILEOFF_0Y);
  1235. P(OTILEOFF_1Y);
  1236. P(OTILEOFF_0U);
  1237. P(OTILEOFF_0V);
  1238. P(OTILEOFF_1U);
  1239. P(OTILEOFF_1V);
  1240. P(FASTHSCALE);
  1241. P(UVSCALEV);
  1242. #undef P
  1243. }
  1244. #endif