intel_overlay.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. struct put_image_params {
  417. int format;
  418. short dst_x;
  419. short dst_y;
  420. short dst_w;
  421. short dst_h;
  422. short src_w;
  423. short src_scan_h;
  424. short src_scan_w;
  425. short src_h;
  426. short stride_Y;
  427. short stride_UV;
  428. int offset_Y;
  429. int offset_U;
  430. int offset_V;
  431. };
  432. static int packed_depth_bytes(u32 format)
  433. {
  434. switch (format & I915_OVERLAY_DEPTH_MASK) {
  435. case I915_OVERLAY_YUV422:
  436. return 4;
  437. case I915_OVERLAY_YUV411:
  438. /* return 6; not implemented */
  439. default:
  440. return -EINVAL;
  441. }
  442. }
  443. static int packed_width_bytes(u32 format, short width)
  444. {
  445. switch (format & I915_OVERLAY_DEPTH_MASK) {
  446. case I915_OVERLAY_YUV422:
  447. return width << 1;
  448. default:
  449. return -EINVAL;
  450. }
  451. }
  452. static int uv_hsubsampling(u32 format)
  453. {
  454. switch (format & I915_OVERLAY_DEPTH_MASK) {
  455. case I915_OVERLAY_YUV422:
  456. case I915_OVERLAY_YUV420:
  457. return 2;
  458. case I915_OVERLAY_YUV411:
  459. case I915_OVERLAY_YUV410:
  460. return 4;
  461. default:
  462. return -EINVAL;
  463. }
  464. }
  465. static int uv_vsubsampling(u32 format)
  466. {
  467. switch (format & I915_OVERLAY_DEPTH_MASK) {
  468. case I915_OVERLAY_YUV420:
  469. case I915_OVERLAY_YUV410:
  470. return 2;
  471. case I915_OVERLAY_YUV422:
  472. case I915_OVERLAY_YUV411:
  473. return 1;
  474. default:
  475. return -EINVAL;
  476. }
  477. }
  478. static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
  479. {
  480. u32 sw;
  481. if (IS_GEN2(dev_priv))
  482. sw = ALIGN((offset & 31) + width, 32);
  483. else
  484. sw = ALIGN((offset & 63) + width, 64);
  485. if (sw == 0)
  486. return 0;
  487. return (sw - 32) >> 3;
  488. }
  489. static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
  490. [ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
  491. [ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
  492. [ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
  493. [ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
  494. [ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
  495. [ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
  496. [ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
  497. [ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
  498. [ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
  499. [ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
  500. [10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
  501. [11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
  502. [12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
  503. [13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
  504. [14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
  505. [15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
  506. [16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
  507. };
  508. static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
  509. [ 0] = { 0x3000, 0x1800, 0x1800, },
  510. [ 1] = { 0xb000, 0x18d0, 0x2e60, },
  511. [ 2] = { 0xb000, 0x1990, 0x2ce0, },
  512. [ 3] = { 0xb020, 0x1a68, 0x2b40, },
  513. [ 4] = { 0xb040, 0x1b20, 0x29e0, },
  514. [ 5] = { 0xb060, 0x1bd8, 0x2880, },
  515. [ 6] = { 0xb080, 0x1c88, 0x3e60, },
  516. [ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
  517. [ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
  518. [ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
  519. [10] = { 0xb100, 0x1eb8, 0x3620, },
  520. [11] = { 0xb100, 0x1f18, 0x34a0, },
  521. [12] = { 0xb100, 0x1f68, 0x3360, },
  522. [13] = { 0xb0e0, 0x1fa8, 0x3240, },
  523. [14] = { 0xb0c0, 0x1fe0, 0x3140, },
  524. [15] = { 0xb060, 0x1ff0, 0x30a0, },
  525. [16] = { 0x3000, 0x0800, 0x3000, },
  526. };
  527. static void update_polyphase_filter(struct overlay_registers __iomem *regs)
  528. {
  529. memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
  530. memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
  531. sizeof(uv_static_hcoeffs));
  532. }
  533. static bool update_scaling_factors(struct intel_overlay *overlay,
  534. struct overlay_registers __iomem *regs,
  535. struct put_image_params *params)
  536. {
  537. /* fixed point with a 12 bit shift */
  538. u32 xscale, yscale, xscale_UV, yscale_UV;
  539. #define FP_SHIFT 12
  540. #define FRACT_MASK 0xfff
  541. bool scale_changed = false;
  542. int uv_hscale = uv_hsubsampling(params->format);
  543. int uv_vscale = uv_vsubsampling(params->format);
  544. if (params->dst_w > 1)
  545. xscale = ((params->src_scan_w - 1) << FP_SHIFT)
  546. /(params->dst_w);
  547. else
  548. xscale = 1 << FP_SHIFT;
  549. if (params->dst_h > 1)
  550. yscale = ((params->src_scan_h - 1) << FP_SHIFT)
  551. /(params->dst_h);
  552. else
  553. yscale = 1 << FP_SHIFT;
  554. /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
  555. xscale_UV = xscale/uv_hscale;
  556. yscale_UV = yscale/uv_vscale;
  557. /* make the Y scale to UV scale ratio an exact multiply */
  558. xscale = xscale_UV * uv_hscale;
  559. yscale = yscale_UV * uv_vscale;
  560. /*} else {
  561. xscale_UV = 0;
  562. yscale_UV = 0;
  563. }*/
  564. if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
  565. scale_changed = true;
  566. overlay->old_xscale = xscale;
  567. overlay->old_yscale = yscale;
  568. iowrite32(((yscale & FRACT_MASK) << 20) |
  569. ((xscale >> FP_SHIFT) << 16) |
  570. ((xscale & FRACT_MASK) << 3),
  571. &regs->YRGBSCALE);
  572. iowrite32(((yscale_UV & FRACT_MASK) << 20) |
  573. ((xscale_UV >> FP_SHIFT) << 16) |
  574. ((xscale_UV & FRACT_MASK) << 3),
  575. &regs->UVSCALE);
  576. iowrite32((((yscale >> FP_SHIFT) << 16) |
  577. ((yscale_UV >> FP_SHIFT) << 0)),
  578. &regs->UVSCALEV);
  579. if (scale_changed)
  580. update_polyphase_filter(regs);
  581. return scale_changed;
  582. }
  583. static void update_colorkey(struct intel_overlay *overlay,
  584. struct overlay_registers __iomem *regs)
  585. {
  586. const struct intel_plane_state *state =
  587. to_intel_plane_state(overlay->crtc->base.primary->state);
  588. u32 key = overlay->color_key;
  589. u32 format = 0;
  590. u32 flags = 0;
  591. if (overlay->color_key_enabled)
  592. flags |= DST_KEY_ENABLE;
  593. if (state->base.visible)
  594. format = state->base.fb->format->format;
  595. switch (format) {
  596. case DRM_FORMAT_C8:
  597. key = 0;
  598. flags |= CLK_RGB8I_MASK;
  599. break;
  600. case DRM_FORMAT_XRGB1555:
  601. key = RGB15_TO_COLORKEY(key);
  602. flags |= CLK_RGB15_MASK;
  603. break;
  604. case DRM_FORMAT_RGB565:
  605. key = RGB16_TO_COLORKEY(key);
  606. flags |= CLK_RGB16_MASK;
  607. break;
  608. default:
  609. flags |= CLK_RGB24_MASK;
  610. break;
  611. }
  612. iowrite32(key, &regs->DCLRKV);
  613. iowrite32(flags, &regs->DCLRKM);
  614. }
  615. static u32 overlay_cmd_reg(struct put_image_params *params)
  616. {
  617. u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
  618. if (params->format & I915_OVERLAY_YUV_PLANAR) {
  619. switch (params->format & I915_OVERLAY_DEPTH_MASK) {
  620. case I915_OVERLAY_YUV422:
  621. cmd |= OCMD_YUV_422_PLANAR;
  622. break;
  623. case I915_OVERLAY_YUV420:
  624. cmd |= OCMD_YUV_420_PLANAR;
  625. break;
  626. case I915_OVERLAY_YUV411:
  627. case I915_OVERLAY_YUV410:
  628. cmd |= OCMD_YUV_410_PLANAR;
  629. break;
  630. }
  631. } else { /* YUV packed */
  632. switch (params->format & I915_OVERLAY_DEPTH_MASK) {
  633. case I915_OVERLAY_YUV422:
  634. cmd |= OCMD_YUV_422_PACKED;
  635. break;
  636. case I915_OVERLAY_YUV411:
  637. cmd |= OCMD_YUV_411_PACKED;
  638. break;
  639. }
  640. switch (params->format & I915_OVERLAY_SWAP_MASK) {
  641. case I915_OVERLAY_NO_SWAP:
  642. break;
  643. case I915_OVERLAY_UV_SWAP:
  644. cmd |= OCMD_UV_SWAP;
  645. break;
  646. case I915_OVERLAY_Y_SWAP:
  647. cmd |= OCMD_Y_SWAP;
  648. break;
  649. case I915_OVERLAY_Y_AND_UV_SWAP:
  650. cmd |= OCMD_Y_AND_UV_SWAP;
  651. break;
  652. }
  653. }
  654. return cmd;
  655. }
  656. static int intel_overlay_do_put_image(struct intel_overlay *overlay,
  657. struct drm_i915_gem_object *new_bo,
  658. struct put_image_params *params)
  659. {
  660. struct overlay_registers __iomem *regs = overlay->regs;
  661. struct drm_i915_private *dev_priv = overlay->i915;
  662. u32 swidth, swidthsw, sheight, ostride;
  663. enum pipe pipe = overlay->crtc->pipe;
  664. bool scale_changed = false;
  665. struct i915_vma *vma;
  666. int ret, tmp_width;
  667. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  668. WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
  669. ret = intel_overlay_release_old_vid(overlay);
  670. if (ret != 0)
  671. return ret;
  672. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  673. vma = i915_gem_object_pin_to_display_plane(new_bo,
  674. 0, NULL, PIN_MAPPABLE);
  675. if (IS_ERR(vma)) {
  676. ret = PTR_ERR(vma);
  677. goto out_pin_section;
  678. }
  679. intel_fb_obj_flush(new_bo, ORIGIN_DIRTYFB);
  680. ret = i915_vma_put_fence(vma);
  681. if (ret)
  682. goto out_unpin;
  683. if (!overlay->active) {
  684. u32 oconfig;
  685. oconfig = OCONF_CC_OUT_8BIT;
  686. if (IS_GEN4(dev_priv))
  687. oconfig |= OCONF_CSC_MODE_BT709;
  688. oconfig |= pipe == 0 ?
  689. OCONF_PIPE_A : OCONF_PIPE_B;
  690. iowrite32(oconfig, &regs->OCONFIG);
  691. ret = intel_overlay_on(overlay);
  692. if (ret != 0)
  693. goto out_unpin;
  694. }
  695. iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
  696. iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
  697. if (params->format & I915_OVERLAY_YUV_PACKED)
  698. tmp_width = packed_width_bytes(params->format, params->src_w);
  699. else
  700. tmp_width = params->src_w;
  701. swidth = params->src_w;
  702. swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
  703. sheight = params->src_h;
  704. iowrite32(i915_ggtt_offset(vma) + params->offset_Y, &regs->OBUF_0Y);
  705. ostride = params->stride_Y;
  706. if (params->format & I915_OVERLAY_YUV_PLANAR) {
  707. int uv_hscale = uv_hsubsampling(params->format);
  708. int uv_vscale = uv_vsubsampling(params->format);
  709. u32 tmp_U, tmp_V;
  710. swidth |= (params->src_w/uv_hscale) << 16;
  711. tmp_U = calc_swidthsw(dev_priv, params->offset_U,
  712. params->src_w/uv_hscale);
  713. tmp_V = calc_swidthsw(dev_priv, params->offset_V,
  714. params->src_w/uv_hscale);
  715. swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
  716. sheight |= (params->src_h/uv_vscale) << 16;
  717. iowrite32(i915_ggtt_offset(vma) + params->offset_U,
  718. &regs->OBUF_0U);
  719. iowrite32(i915_ggtt_offset(vma) + params->offset_V,
  720. &regs->OBUF_0V);
  721. ostride |= params->stride_UV << 16;
  722. }
  723. iowrite32(swidth, &regs->SWIDTH);
  724. iowrite32(swidthsw, &regs->SWIDTHSW);
  725. iowrite32(sheight, &regs->SHEIGHT);
  726. iowrite32(ostride, &regs->OSTRIDE);
  727. scale_changed = update_scaling_factors(overlay, regs, params);
  728. update_colorkey(overlay, regs);
  729. iowrite32(overlay_cmd_reg(params), &regs->OCMD);
  730. ret = intel_overlay_continue(overlay, vma, scale_changed);
  731. if (ret)
  732. goto out_unpin;
  733. return 0;
  734. out_unpin:
  735. i915_gem_object_unpin_from_display_plane(vma);
  736. out_pin_section:
  737. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  738. return ret;
  739. }
  740. int intel_overlay_switch_off(struct intel_overlay *overlay)
  741. {
  742. struct drm_i915_private *dev_priv = overlay->i915;
  743. int ret;
  744. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  745. WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
  746. ret = intel_overlay_recover_from_interrupt(overlay);
  747. if (ret != 0)
  748. return ret;
  749. if (!overlay->active)
  750. return 0;
  751. ret = intel_overlay_release_old_vid(overlay);
  752. if (ret != 0)
  753. return ret;
  754. iowrite32(0, &overlay->regs->OCMD);
  755. return intel_overlay_off(overlay);
  756. }
  757. static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
  758. struct intel_crtc *crtc)
  759. {
  760. if (!crtc->active)
  761. return -EINVAL;
  762. /* can't use the overlay with double wide pipe */
  763. if (crtc->config->double_wide)
  764. return -EINVAL;
  765. return 0;
  766. }
  767. static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
  768. {
  769. struct drm_i915_private *dev_priv = overlay->i915;
  770. u32 pfit_control = I915_READ(PFIT_CONTROL);
  771. u32 ratio;
  772. /* XXX: This is not the same logic as in the xorg driver, but more in
  773. * line with the intel documentation for the i965
  774. */
  775. if (INTEL_GEN(dev_priv) >= 4) {
  776. /* on i965 use the PGM reg to read out the autoscaler values */
  777. ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
  778. } else {
  779. if (pfit_control & VERT_AUTO_SCALE)
  780. ratio = I915_READ(PFIT_AUTO_RATIOS);
  781. else
  782. ratio = I915_READ(PFIT_PGM_RATIOS);
  783. ratio >>= PFIT_VERT_SCALE_SHIFT;
  784. }
  785. overlay->pfit_vscale_ratio = ratio;
  786. }
  787. static int check_overlay_dst(struct intel_overlay *overlay,
  788. struct drm_intel_overlay_put_image *rec)
  789. {
  790. const struct intel_crtc_state *pipe_config =
  791. overlay->crtc->config;
  792. if (rec->dst_x < pipe_config->pipe_src_w &&
  793. rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
  794. rec->dst_y < pipe_config->pipe_src_h &&
  795. rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
  796. return 0;
  797. else
  798. return -EINVAL;
  799. }
  800. static int check_overlay_scaling(struct put_image_params *rec)
  801. {
  802. u32 tmp;
  803. /* downscaling limit is 8.0 */
  804. tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
  805. if (tmp > 7)
  806. return -EINVAL;
  807. tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
  808. if (tmp > 7)
  809. return -EINVAL;
  810. return 0;
  811. }
  812. static int check_overlay_src(struct drm_i915_private *dev_priv,
  813. struct drm_intel_overlay_put_image *rec,
  814. struct drm_i915_gem_object *new_bo)
  815. {
  816. int uv_hscale = uv_hsubsampling(rec->flags);
  817. int uv_vscale = uv_vsubsampling(rec->flags);
  818. u32 stride_mask;
  819. int depth;
  820. u32 tmp;
  821. /* check src dimensions */
  822. if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
  823. if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
  824. rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
  825. return -EINVAL;
  826. } else {
  827. if (rec->src_height > IMAGE_MAX_HEIGHT ||
  828. rec->src_width > IMAGE_MAX_WIDTH)
  829. return -EINVAL;
  830. }
  831. /* better safe than sorry, use 4 as the maximal subsampling ratio */
  832. if (rec->src_height < N_VERT_Y_TAPS*4 ||
  833. rec->src_width < N_HORIZ_Y_TAPS*4)
  834. return -EINVAL;
  835. /* check alignment constraints */
  836. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  837. case I915_OVERLAY_RGB:
  838. /* not implemented */
  839. return -EINVAL;
  840. case I915_OVERLAY_YUV_PACKED:
  841. if (uv_vscale != 1)
  842. return -EINVAL;
  843. depth = packed_depth_bytes(rec->flags);
  844. if (depth < 0)
  845. return depth;
  846. /* ignore UV planes */
  847. rec->stride_UV = 0;
  848. rec->offset_U = 0;
  849. rec->offset_V = 0;
  850. /* check pixel alignment */
  851. if (rec->offset_Y % depth)
  852. return -EINVAL;
  853. break;
  854. case I915_OVERLAY_YUV_PLANAR:
  855. if (uv_vscale < 0 || uv_hscale < 0)
  856. return -EINVAL;
  857. /* no offset restrictions for planar formats */
  858. break;
  859. default:
  860. return -EINVAL;
  861. }
  862. if (rec->src_width % uv_hscale)
  863. return -EINVAL;
  864. /* stride checking */
  865. if (IS_I830(dev_priv) || IS_I845G(dev_priv))
  866. stride_mask = 255;
  867. else
  868. stride_mask = 63;
  869. if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
  870. return -EINVAL;
  871. if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
  872. return -EINVAL;
  873. tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
  874. 4096 : 8192;
  875. if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
  876. return -EINVAL;
  877. /* check buffer dimensions */
  878. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  879. case I915_OVERLAY_RGB:
  880. case I915_OVERLAY_YUV_PACKED:
  881. /* always 4 Y values per depth pixels */
  882. if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
  883. return -EINVAL;
  884. tmp = rec->stride_Y*rec->src_height;
  885. if (rec->offset_Y + tmp > new_bo->base.size)
  886. return -EINVAL;
  887. break;
  888. case I915_OVERLAY_YUV_PLANAR:
  889. if (rec->src_width > rec->stride_Y)
  890. return -EINVAL;
  891. if (rec->src_width/uv_hscale > rec->stride_UV)
  892. return -EINVAL;
  893. tmp = rec->stride_Y * rec->src_height;
  894. if (rec->offset_Y + tmp > new_bo->base.size)
  895. return -EINVAL;
  896. tmp = rec->stride_UV * (rec->src_height / uv_vscale);
  897. if (rec->offset_U + tmp > new_bo->base.size ||
  898. rec->offset_V + tmp > new_bo->base.size)
  899. return -EINVAL;
  900. break;
  901. }
  902. return 0;
  903. }
  904. int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
  905. struct drm_file *file_priv)
  906. {
  907. struct drm_intel_overlay_put_image *put_image_rec = data;
  908. struct drm_i915_private *dev_priv = to_i915(dev);
  909. struct intel_overlay *overlay;
  910. struct drm_crtc *drmmode_crtc;
  911. struct intel_crtc *crtc;
  912. struct drm_i915_gem_object *new_bo;
  913. struct put_image_params *params;
  914. int ret;
  915. overlay = dev_priv->overlay;
  916. if (!overlay) {
  917. DRM_DEBUG("userspace bug: no overlay\n");
  918. return -ENODEV;
  919. }
  920. if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
  921. drm_modeset_lock_all(dev);
  922. mutex_lock(&dev->struct_mutex);
  923. ret = intel_overlay_switch_off(overlay);
  924. mutex_unlock(&dev->struct_mutex);
  925. drm_modeset_unlock_all(dev);
  926. return ret;
  927. }
  928. params = kmalloc(sizeof(*params), GFP_KERNEL);
  929. if (!params)
  930. return -ENOMEM;
  931. drmmode_crtc = drm_crtc_find(dev, file_priv, put_image_rec->crtc_id);
  932. if (!drmmode_crtc) {
  933. ret = -ENOENT;
  934. goto out_free;
  935. }
  936. crtc = to_intel_crtc(drmmode_crtc);
  937. new_bo = i915_gem_object_lookup(file_priv, put_image_rec->bo_handle);
  938. if (!new_bo) {
  939. ret = -ENOENT;
  940. goto out_free;
  941. }
  942. drm_modeset_lock_all(dev);
  943. mutex_lock(&dev->struct_mutex);
  944. if (i915_gem_object_is_tiled(new_bo)) {
  945. DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
  946. ret = -EINVAL;
  947. goto out_unlock;
  948. }
  949. ret = intel_overlay_recover_from_interrupt(overlay);
  950. if (ret != 0)
  951. goto out_unlock;
  952. if (overlay->crtc != crtc) {
  953. ret = intel_overlay_switch_off(overlay);
  954. if (ret != 0)
  955. goto out_unlock;
  956. ret = check_overlay_possible_on_crtc(overlay, crtc);
  957. if (ret != 0)
  958. goto out_unlock;
  959. overlay->crtc = crtc;
  960. crtc->overlay = overlay;
  961. /* line too wide, i.e. one-line-mode */
  962. if (crtc->config->pipe_src_w > 1024 &&
  963. crtc->config->gmch_pfit.control & PFIT_ENABLE) {
  964. overlay->pfit_active = true;
  965. update_pfit_vscale_ratio(overlay);
  966. } else
  967. overlay->pfit_active = false;
  968. }
  969. ret = check_overlay_dst(overlay, put_image_rec);
  970. if (ret != 0)
  971. goto out_unlock;
  972. if (overlay->pfit_active) {
  973. params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
  974. overlay->pfit_vscale_ratio);
  975. /* shifting right rounds downwards, so add 1 */
  976. params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
  977. overlay->pfit_vscale_ratio) + 1;
  978. } else {
  979. params->dst_y = put_image_rec->dst_y;
  980. params->dst_h = put_image_rec->dst_height;
  981. }
  982. params->dst_x = put_image_rec->dst_x;
  983. params->dst_w = put_image_rec->dst_width;
  984. params->src_w = put_image_rec->src_width;
  985. params->src_h = put_image_rec->src_height;
  986. params->src_scan_w = put_image_rec->src_scan_width;
  987. params->src_scan_h = put_image_rec->src_scan_height;
  988. if (params->src_scan_h > params->src_h ||
  989. params->src_scan_w > params->src_w) {
  990. ret = -EINVAL;
  991. goto out_unlock;
  992. }
  993. ret = check_overlay_src(dev_priv, put_image_rec, new_bo);
  994. if (ret != 0)
  995. goto out_unlock;
  996. params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
  997. params->stride_Y = put_image_rec->stride_Y;
  998. params->stride_UV = put_image_rec->stride_UV;
  999. params->offset_Y = put_image_rec->offset_Y;
  1000. params->offset_U = put_image_rec->offset_U;
  1001. params->offset_V = put_image_rec->offset_V;
  1002. /* Check scaling after src size to prevent a divide-by-zero. */
  1003. ret = check_overlay_scaling(params);
  1004. if (ret != 0)
  1005. goto out_unlock;
  1006. ret = intel_overlay_do_put_image(overlay, new_bo, params);
  1007. if (ret != 0)
  1008. goto out_unlock;
  1009. mutex_unlock(&dev->struct_mutex);
  1010. drm_modeset_unlock_all(dev);
  1011. i915_gem_object_put(new_bo);
  1012. kfree(params);
  1013. return 0;
  1014. out_unlock:
  1015. mutex_unlock(&dev->struct_mutex);
  1016. drm_modeset_unlock_all(dev);
  1017. i915_gem_object_put(new_bo);
  1018. out_free:
  1019. kfree(params);
  1020. return ret;
  1021. }
  1022. static void update_reg_attrs(struct intel_overlay *overlay,
  1023. struct overlay_registers __iomem *regs)
  1024. {
  1025. iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
  1026. &regs->OCLRC0);
  1027. iowrite32(overlay->saturation, &regs->OCLRC1);
  1028. }
  1029. static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
  1030. {
  1031. int i;
  1032. if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
  1033. return false;
  1034. for (i = 0; i < 3; i++) {
  1035. if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
  1036. return false;
  1037. }
  1038. return true;
  1039. }
  1040. static bool check_gamma5_errata(u32 gamma5)
  1041. {
  1042. int i;
  1043. for (i = 0; i < 3; i++) {
  1044. if (((gamma5 >> i*8) & 0xff) == 0x80)
  1045. return false;
  1046. }
  1047. return true;
  1048. }
  1049. static int check_gamma(struct drm_intel_overlay_attrs *attrs)
  1050. {
  1051. if (!check_gamma_bounds(0, attrs->gamma0) ||
  1052. !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
  1053. !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
  1054. !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
  1055. !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
  1056. !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
  1057. !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
  1058. return -EINVAL;
  1059. if (!check_gamma5_errata(attrs->gamma5))
  1060. return -EINVAL;
  1061. return 0;
  1062. }
  1063. int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
  1064. struct drm_file *file_priv)
  1065. {
  1066. struct drm_intel_overlay_attrs *attrs = data;
  1067. struct drm_i915_private *dev_priv = to_i915(dev);
  1068. struct intel_overlay *overlay;
  1069. int ret;
  1070. overlay = dev_priv->overlay;
  1071. if (!overlay) {
  1072. DRM_DEBUG("userspace bug: no overlay\n");
  1073. return -ENODEV;
  1074. }
  1075. drm_modeset_lock_all(dev);
  1076. mutex_lock(&dev->struct_mutex);
  1077. ret = -EINVAL;
  1078. if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
  1079. attrs->color_key = overlay->color_key;
  1080. attrs->brightness = overlay->brightness;
  1081. attrs->contrast = overlay->contrast;
  1082. attrs->saturation = overlay->saturation;
  1083. if (!IS_GEN2(dev_priv)) {
  1084. attrs->gamma0 = I915_READ(OGAMC0);
  1085. attrs->gamma1 = I915_READ(OGAMC1);
  1086. attrs->gamma2 = I915_READ(OGAMC2);
  1087. attrs->gamma3 = I915_READ(OGAMC3);
  1088. attrs->gamma4 = I915_READ(OGAMC4);
  1089. attrs->gamma5 = I915_READ(OGAMC5);
  1090. }
  1091. } else {
  1092. if (attrs->brightness < -128 || attrs->brightness > 127)
  1093. goto out_unlock;
  1094. if (attrs->contrast > 255)
  1095. goto out_unlock;
  1096. if (attrs->saturation > 1023)
  1097. goto out_unlock;
  1098. overlay->color_key = attrs->color_key;
  1099. overlay->brightness = attrs->brightness;
  1100. overlay->contrast = attrs->contrast;
  1101. overlay->saturation = attrs->saturation;
  1102. update_reg_attrs(overlay, overlay->regs);
  1103. if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
  1104. if (IS_GEN2(dev_priv))
  1105. goto out_unlock;
  1106. if (overlay->active) {
  1107. ret = -EBUSY;
  1108. goto out_unlock;
  1109. }
  1110. ret = check_gamma(attrs);
  1111. if (ret)
  1112. goto out_unlock;
  1113. I915_WRITE(OGAMC0, attrs->gamma0);
  1114. I915_WRITE(OGAMC1, attrs->gamma1);
  1115. I915_WRITE(OGAMC2, attrs->gamma2);
  1116. I915_WRITE(OGAMC3, attrs->gamma3);
  1117. I915_WRITE(OGAMC4, attrs->gamma4);
  1118. I915_WRITE(OGAMC5, attrs->gamma5);
  1119. }
  1120. }
  1121. overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
  1122. ret = 0;
  1123. out_unlock:
  1124. mutex_unlock(&dev->struct_mutex);
  1125. drm_modeset_unlock_all(dev);
  1126. return ret;
  1127. }
  1128. static int get_registers(struct intel_overlay *overlay, bool use_phys)
  1129. {
  1130. struct drm_i915_gem_object *obj;
  1131. struct i915_vma *vma;
  1132. int err;
  1133. obj = i915_gem_object_create_stolen(overlay->i915, PAGE_SIZE);
  1134. if (obj == NULL)
  1135. obj = i915_gem_object_create_internal(overlay->i915, PAGE_SIZE);
  1136. if (IS_ERR(obj))
  1137. return PTR_ERR(obj);
  1138. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
  1139. if (IS_ERR(vma)) {
  1140. err = PTR_ERR(vma);
  1141. goto err_put_bo;
  1142. }
  1143. if (use_phys)
  1144. overlay->flip_addr = sg_dma_address(obj->mm.pages->sgl);
  1145. else
  1146. overlay->flip_addr = i915_ggtt_offset(vma);
  1147. overlay->regs = i915_vma_pin_iomap(vma);
  1148. i915_vma_unpin(vma);
  1149. if (IS_ERR(overlay->regs)) {
  1150. err = PTR_ERR(overlay->regs);
  1151. goto err_put_bo;
  1152. }
  1153. overlay->reg_bo = obj;
  1154. return 0;
  1155. err_put_bo:
  1156. i915_gem_object_put(obj);
  1157. return err;
  1158. }
  1159. void intel_setup_overlay(struct drm_i915_private *dev_priv)
  1160. {
  1161. struct intel_overlay *overlay;
  1162. int ret;
  1163. if (!HAS_OVERLAY(dev_priv))
  1164. return;
  1165. overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
  1166. if (!overlay)
  1167. return;
  1168. overlay->i915 = dev_priv;
  1169. overlay->color_key = 0x0101fe;
  1170. overlay->color_key_enabled = true;
  1171. overlay->brightness = -19;
  1172. overlay->contrast = 75;
  1173. overlay->saturation = 146;
  1174. init_request_active(&overlay->last_flip, NULL);
  1175. mutex_lock(&dev_priv->drm.struct_mutex);
  1176. ret = get_registers(overlay, OVERLAY_NEEDS_PHYSICAL(dev_priv));
  1177. if (ret)
  1178. goto out_free;
  1179. ret = i915_gem_object_set_to_gtt_domain(overlay->reg_bo, true);
  1180. if (ret)
  1181. goto out_reg_bo;
  1182. mutex_unlock(&dev_priv->drm.struct_mutex);
  1183. memset_io(overlay->regs, 0, sizeof(struct overlay_registers));
  1184. update_polyphase_filter(overlay->regs);
  1185. update_reg_attrs(overlay, overlay->regs);
  1186. dev_priv->overlay = overlay;
  1187. DRM_INFO("Initialized overlay support.\n");
  1188. return;
  1189. out_reg_bo:
  1190. i915_gem_object_put(overlay->reg_bo);
  1191. out_free:
  1192. mutex_unlock(&dev_priv->drm.struct_mutex);
  1193. kfree(overlay);
  1194. }
  1195. void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
  1196. {
  1197. struct intel_overlay *overlay;
  1198. overlay = fetch_and_zero(&dev_priv->overlay);
  1199. if (!overlay)
  1200. return;
  1201. /*
  1202. * The bo's should be free'd by the generic code already.
  1203. * Furthermore modesetting teardown happens beforehand so the
  1204. * hardware should be off already.
  1205. */
  1206. WARN_ON(overlay->active);
  1207. i915_gem_object_put(overlay->reg_bo);
  1208. kfree(overlay);
  1209. }
  1210. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  1211. struct intel_overlay_error_state {
  1212. struct overlay_registers regs;
  1213. unsigned long base;
  1214. u32 dovsta;
  1215. u32 isr;
  1216. };
  1217. struct intel_overlay_error_state *
  1218. intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
  1219. {
  1220. struct intel_overlay *overlay = dev_priv->overlay;
  1221. struct intel_overlay_error_state *error;
  1222. if (!overlay || !overlay->active)
  1223. return NULL;
  1224. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  1225. if (error == NULL)
  1226. return NULL;
  1227. error->dovsta = I915_READ(DOVSTA);
  1228. error->isr = I915_READ(ISR);
  1229. error->base = overlay->flip_addr;
  1230. memcpy_fromio(&error->regs, overlay->regs, sizeof(error->regs));
  1231. return error;
  1232. }
  1233. void
  1234. intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
  1235. struct intel_overlay_error_state *error)
  1236. {
  1237. i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
  1238. error->dovsta, error->isr);
  1239. i915_error_printf(m, " Register file at 0x%08lx:\n",
  1240. error->base);
  1241. #define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)
  1242. P(OBUF_0Y);
  1243. P(OBUF_1Y);
  1244. P(OBUF_0U);
  1245. P(OBUF_0V);
  1246. P(OBUF_1U);
  1247. P(OBUF_1V);
  1248. P(OSTRIDE);
  1249. P(YRGB_VPH);
  1250. P(UV_VPH);
  1251. P(HORZ_PH);
  1252. P(INIT_PHS);
  1253. P(DWINPOS);
  1254. P(DWINSZ);
  1255. P(SWIDTH);
  1256. P(SWIDTHSW);
  1257. P(SHEIGHT);
  1258. P(YRGBSCALE);
  1259. P(UVSCALE);
  1260. P(OCLRC0);
  1261. P(OCLRC1);
  1262. P(DCLRKV);
  1263. P(DCLRKM);
  1264. P(SCLRKVH);
  1265. P(SCLRKVL);
  1266. P(SCLRKEN);
  1267. P(OCONFIG);
  1268. P(OCMD);
  1269. P(OSTART_0Y);
  1270. P(OSTART_1Y);
  1271. P(OSTART_0U);
  1272. P(OSTART_0V);
  1273. P(OSTART_1U);
  1274. P(OSTART_1V);
  1275. P(OTILEOFF_0Y);
  1276. P(OTILEOFF_1Y);
  1277. P(OTILEOFF_0U);
  1278. P(OTILEOFF_0V);
  1279. P(OTILEOFF_1U);
  1280. P(OTILEOFF_1V);
  1281. P(FASTHSCALE);
  1282. P(UVSCALEV);
  1283. #undef P
  1284. }
  1285. #endif