intel_overlay.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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 "drmP.h"
  29. #include "drm.h"
  30. #include "i915_drm.h"
  31. #include "i915_drv.h"
  32. #include "i915_reg.h"
  33. #include "intel_drv.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 (Ox1<<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. /* overlay flip addr flag */
  163. #define OFC_UPDATE 0x1
  164. #define OVERLAY_NONPHYSICAL(dev) (IS_G33(dev) || IS_I965G(dev))
  165. #define OVERLAY_EXISTS(dev) (!IS_G4X(dev) && !IS_IGDNG(dev))
  166. static struct overlay_registers *intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
  167. {
  168. drm_i915_private_t *dev_priv = overlay->dev->dev_private;
  169. struct overlay_registers *regs;
  170. /* no recursive mappings */
  171. BUG_ON(overlay->virt_addr);
  172. if (OVERLAY_NONPHYSICAL(overlay->dev)) {
  173. regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  174. overlay->reg_bo->gtt_offset);
  175. if (!regs) {
  176. DRM_ERROR("failed to map overlay regs in GTT\n");
  177. return NULL;
  178. }
  179. } else
  180. regs = overlay->reg_bo->phys_obj->handle->vaddr;
  181. return overlay->virt_addr = regs;
  182. }
  183. static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay)
  184. {
  185. struct drm_device *dev = overlay->dev;
  186. drm_i915_private_t *dev_priv = dev->dev_private;
  187. if (OVERLAY_NONPHYSICAL(overlay->dev))
  188. io_mapping_unmap_atomic(overlay->virt_addr);
  189. overlay->virt_addr = NULL;
  190. I915_READ(OVADD); /* flush wc cashes */
  191. return;
  192. }
  193. /* overlay needs to be disable in OCMD reg */
  194. static int intel_overlay_on(struct intel_overlay *overlay)
  195. {
  196. struct drm_device *dev = overlay->dev;
  197. drm_i915_private_t *dev_priv = dev->dev_private;
  198. int ret;
  199. RING_LOCALS;
  200. BUG_ON(overlay->active);
  201. overlay->active = 1;
  202. overlay->hw_wedged = NEEDS_WAIT_FOR_FLIP;
  203. BEGIN_LP_RING(6);
  204. OUT_RING(MI_FLUSH);
  205. OUT_RING(MI_NOOP);
  206. OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
  207. OUT_RING(overlay->flip_addr | OFC_UPDATE);
  208. OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
  209. OUT_RING(MI_NOOP);
  210. ADVANCE_LP_RING();
  211. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  212. if (overlay->last_flip_req == 0)
  213. return -ENOMEM;
  214. ret = i915_do_wait_request(dev, overlay->last_flip_req, 1);
  215. if (ret != 0)
  216. return ret;
  217. overlay->hw_wedged = 0;
  218. overlay->last_flip_req = 0;
  219. return 0;
  220. }
  221. /* overlay needs to be enabled in OCMD reg */
  222. static void intel_overlay_continue(struct intel_overlay *overlay,
  223. bool load_polyphase_filter)
  224. {
  225. struct drm_device *dev = overlay->dev;
  226. drm_i915_private_t *dev_priv = dev->dev_private;
  227. u32 flip_addr = overlay->flip_addr;
  228. u32 tmp;
  229. RING_LOCALS;
  230. BUG_ON(!overlay->active);
  231. if (load_polyphase_filter)
  232. flip_addr |= OFC_UPDATE;
  233. /* check for underruns */
  234. tmp = I915_READ(DOVSTA);
  235. if (tmp & (1 << 17))
  236. DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
  237. BEGIN_LP_RING(4);
  238. OUT_RING(MI_FLUSH);
  239. OUT_RING(MI_NOOP);
  240. OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
  241. OUT_RING(flip_addr);
  242. ADVANCE_LP_RING();
  243. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  244. }
  245. static int intel_overlay_wait_flip(struct intel_overlay *overlay)
  246. {
  247. struct drm_device *dev = overlay->dev;
  248. drm_i915_private_t *dev_priv = dev->dev_private;
  249. int ret;
  250. u32 tmp;
  251. RING_LOCALS;
  252. if (overlay->last_flip_req != 0) {
  253. ret = i915_do_wait_request(dev, overlay->last_flip_req, 0);
  254. if (ret != 0)
  255. return ret;
  256. overlay->last_flip_req = 0;
  257. tmp = I915_READ(ISR);
  258. if (!(tmp & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT))
  259. return 0;
  260. }
  261. /* synchronous slowpath */
  262. overlay->hw_wedged = RELEASE_OLD_VID;
  263. BEGIN_LP_RING(2);
  264. OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
  265. OUT_RING(MI_NOOP);
  266. ADVANCE_LP_RING();
  267. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  268. if (overlay->last_flip_req == 0)
  269. return -ENOMEM;
  270. ret = i915_do_wait_request(dev, overlay->last_flip_req, 1);
  271. if (ret != 0)
  272. return ret;
  273. overlay->hw_wedged = 0;
  274. overlay->last_flip_req = 0;
  275. return 0;
  276. }
  277. /* overlay needs to be disabled in OCMD reg */
  278. static int intel_overlay_off(struct intel_overlay *overlay)
  279. {
  280. u32 flip_addr = overlay->flip_addr;
  281. struct drm_device *dev = overlay->dev;
  282. drm_i915_private_t *dev_priv = dev->dev_private;
  283. int ret;
  284. RING_LOCALS;
  285. BUG_ON(!overlay->active);
  286. /* According to intel docs the overlay hw may hang (when switching
  287. * off) without loading the filter coeffs. It is however unclear whether
  288. * this applies to the disabling of the overlay or to the switching off
  289. * of the hw. Do it in both cases */
  290. flip_addr |= OFC_UPDATE;
  291. /* wait for overlay to go idle */
  292. overlay->hw_wedged = SWITCH_OFF_STAGE_1;
  293. BEGIN_LP_RING(6);
  294. OUT_RING(MI_FLUSH);
  295. OUT_RING(MI_NOOP);
  296. OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
  297. OUT_RING(flip_addr);
  298. OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
  299. OUT_RING(MI_NOOP);
  300. ADVANCE_LP_RING();
  301. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  302. if (overlay->last_flip_req == 0)
  303. return -ENOMEM;
  304. ret = i915_do_wait_request(dev, overlay->last_flip_req, 1);
  305. if (ret != 0)
  306. return ret;
  307. /* turn overlay off */
  308. overlay->hw_wedged = SWITCH_OFF_STAGE_2;
  309. BEGIN_LP_RING(6);
  310. OUT_RING(MI_FLUSH);
  311. OUT_RING(MI_NOOP);
  312. OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
  313. OUT_RING(flip_addr);
  314. OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
  315. OUT_RING(MI_NOOP);
  316. ADVANCE_LP_RING();
  317. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  318. if (overlay->last_flip_req == 0)
  319. return -ENOMEM;
  320. ret = i915_do_wait_request(dev, overlay->last_flip_req, 1);
  321. if (ret != 0)
  322. return ret;
  323. overlay->active = 0;
  324. overlay->hw_wedged = 0;
  325. overlay->last_flip_req = 0;
  326. return ret;
  327. }
  328. /* recover from an interruption due to a signal
  329. * We have to be careful not to repeat work forever an make forward progess. */
  330. int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
  331. int interruptible)
  332. {
  333. struct drm_device *dev = overlay->dev;
  334. drm_i915_private_t *dev_priv = dev->dev_private;
  335. struct drm_gem_object *obj;
  336. u32 flip_addr;
  337. int ret;
  338. RING_LOCALS;
  339. if (overlay->hw_wedged == HW_WEDGED)
  340. return -EIO;
  341. if (overlay->last_flip_req == 0) {
  342. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  343. if (overlay->last_flip_req == 0)
  344. return -ENOMEM;
  345. }
  346. ret = i915_do_wait_request(dev, overlay->last_flip_req, interruptible);
  347. if (ret != 0)
  348. return ret;
  349. switch (overlay->hw_wedged) {
  350. case RELEASE_OLD_VID:
  351. obj = overlay->old_vid_bo->obj;
  352. i915_gem_object_unpin(obj);
  353. drm_gem_object_unreference(obj);
  354. overlay->old_vid_bo = NULL;
  355. break;
  356. case SWITCH_OFF_STAGE_1:
  357. flip_addr = overlay->flip_addr;
  358. flip_addr |= OFC_UPDATE;
  359. overlay->hw_wedged = SWITCH_OFF_STAGE_2;
  360. BEGIN_LP_RING(6);
  361. OUT_RING(MI_FLUSH);
  362. OUT_RING(MI_NOOP);
  363. OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
  364. OUT_RING(flip_addr);
  365. OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
  366. OUT_RING(MI_NOOP);
  367. ADVANCE_LP_RING();
  368. overlay->last_flip_req = i915_add_request(dev, NULL, 0);
  369. if (overlay->last_flip_req == 0)
  370. return -ENOMEM;
  371. ret = i915_do_wait_request(dev, overlay->last_flip_req,
  372. interruptible);
  373. if (ret != 0)
  374. return ret;
  375. case SWITCH_OFF_STAGE_2:
  376. printk("switch off 2\n");
  377. BUG_ON(!overlay->vid_bo);
  378. obj = overlay->vid_bo->obj;
  379. i915_gem_object_unpin(obj);
  380. drm_gem_object_unreference(obj);
  381. overlay->vid_bo = NULL;
  382. overlay->crtc->overlay = NULL;
  383. overlay->crtc = NULL;
  384. overlay->active = 0;
  385. break;
  386. default:
  387. BUG_ON(overlay->hw_wedged != NEEDS_WAIT_FOR_FLIP);
  388. }
  389. overlay->hw_wedged = 0;
  390. overlay->last_flip_req = 0;
  391. return 0;
  392. }
  393. /* Wait for pending overlay flip and release old frame.
  394. * Needs to be called before the overlay register are changed
  395. * via intel_overlay_(un)map_regs_atomic */
  396. static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
  397. {
  398. int ret;
  399. struct drm_gem_object *obj;
  400. /* only wait if there is actually an old frame to release to
  401. * guarantee forward progress */
  402. if (!overlay->old_vid_bo)
  403. return 0;
  404. ret = intel_overlay_wait_flip(overlay);
  405. if (ret != 0)
  406. return ret;
  407. obj = overlay->old_vid_bo->obj;
  408. i915_gem_object_unpin(obj);
  409. drm_gem_object_unreference(obj);
  410. overlay->old_vid_bo = NULL;
  411. return 0;
  412. }
  413. struct put_image_params {
  414. int format;
  415. short dst_x;
  416. short dst_y;
  417. short dst_w;
  418. short dst_h;
  419. short src_w;
  420. short src_scan_h;
  421. short src_scan_w;
  422. short src_h;
  423. short stride_Y;
  424. short stride_UV;
  425. int offset_Y;
  426. int offset_U;
  427. int offset_V;
  428. };
  429. static int packed_depth_bytes(u32 format)
  430. {
  431. switch (format & I915_OVERLAY_DEPTH_MASK) {
  432. case I915_OVERLAY_YUV422:
  433. return 4;
  434. case I915_OVERLAY_YUV411:
  435. /* return 6; not implemented */
  436. default:
  437. return -EINVAL;
  438. }
  439. }
  440. static int packed_width_bytes(u32 format, short width)
  441. {
  442. switch (format & I915_OVERLAY_DEPTH_MASK) {
  443. case I915_OVERLAY_YUV422:
  444. return width << 1;
  445. default:
  446. return -EINVAL;
  447. }
  448. }
  449. static int uv_hsubsampling(u32 format)
  450. {
  451. switch (format & I915_OVERLAY_DEPTH_MASK) {
  452. case I915_OVERLAY_YUV422:
  453. case I915_OVERLAY_YUV420:
  454. return 2;
  455. case I915_OVERLAY_YUV411:
  456. case I915_OVERLAY_YUV410:
  457. return 4;
  458. default:
  459. return -EINVAL;
  460. }
  461. }
  462. static int uv_vsubsampling(u32 format)
  463. {
  464. switch (format & I915_OVERLAY_DEPTH_MASK) {
  465. case I915_OVERLAY_YUV420:
  466. case I915_OVERLAY_YUV410:
  467. return 2;
  468. case I915_OVERLAY_YUV422:
  469. case I915_OVERLAY_YUV411:
  470. return 1;
  471. default:
  472. return -EINVAL;
  473. }
  474. }
  475. static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
  476. {
  477. u32 mask, shift, ret;
  478. if (IS_I9XX(dev)) {
  479. mask = 0x3f;
  480. shift = 6;
  481. } else {
  482. mask = 0x1f;
  483. shift = 5;
  484. }
  485. ret = ((offset + width + mask) >> shift) - (offset >> shift);
  486. if (IS_I9XX(dev))
  487. ret <<= 1;
  488. ret -=1;
  489. return ret << 2;
  490. }
  491. static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
  492. 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
  493. 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
  494. 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
  495. 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
  496. 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
  497. 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
  498. 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
  499. 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
  500. 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
  501. 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
  502. 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
  503. 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
  504. 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
  505. 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
  506. 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
  507. 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
  508. 0xb000, 0x3000, 0x0800, 0x3000, 0xb000};
  509. static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
  510. 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
  511. 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
  512. 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
  513. 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
  514. 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
  515. 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
  516. 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
  517. 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
  518. 0x3000, 0x0800, 0x3000};
  519. static void update_polyphase_filter(struct overlay_registers *regs)
  520. {
  521. memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
  522. memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
  523. }
  524. static bool update_scaling_factors(struct intel_overlay *overlay,
  525. struct overlay_registers *regs,
  526. struct put_image_params *params)
  527. {
  528. /* fixed point with a 12 bit shift */
  529. u32 xscale, yscale, xscale_UV, yscale_UV;
  530. #define FP_SHIFT 12
  531. #define FRACT_MASK 0xfff
  532. bool scale_changed = false;
  533. int uv_hscale = uv_hsubsampling(params->format);
  534. int uv_vscale = uv_vsubsampling(params->format);
  535. if (params->dst_w > 1)
  536. xscale = ((params->src_scan_w - 1) << FP_SHIFT)
  537. /(params->dst_w);
  538. else
  539. xscale = 1 << FP_SHIFT;
  540. if (params->dst_h > 1)
  541. yscale = ((params->src_scan_h - 1) << FP_SHIFT)
  542. /(params->dst_h);
  543. else
  544. yscale = 1 << FP_SHIFT;
  545. /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
  546. xscale_UV = xscale/uv_hscale;
  547. yscale_UV = yscale/uv_vscale;
  548. /* make the Y scale to UV scale ratio an exact multiply */
  549. xscale = xscale_UV * uv_hscale;
  550. yscale = yscale_UV * uv_vscale;
  551. /*} else {
  552. xscale_UV = 0;
  553. yscale_UV = 0;
  554. }*/
  555. if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
  556. scale_changed = true;
  557. overlay->old_xscale = xscale;
  558. overlay->old_yscale = yscale;
  559. regs->YRGBSCALE = ((yscale & FRACT_MASK) << 20)
  560. | ((xscale >> FP_SHIFT) << 16)
  561. | ((xscale & FRACT_MASK) << 3);
  562. regs->UVSCALE = ((yscale_UV & FRACT_MASK) << 20)
  563. | ((xscale_UV >> FP_SHIFT) << 16)
  564. | ((xscale_UV & FRACT_MASK) << 3);
  565. regs->UVSCALEV = ((yscale >> FP_SHIFT) << 16)
  566. | ((yscale_UV >> FP_SHIFT) << 0);
  567. if (scale_changed)
  568. update_polyphase_filter(regs);
  569. return scale_changed;
  570. }
  571. static void update_colorkey(struct intel_overlay *overlay,
  572. struct overlay_registers *regs)
  573. {
  574. u32 key = overlay->color_key;
  575. switch (overlay->crtc->base.fb->bits_per_pixel) {
  576. case 8:
  577. regs->DCLRKV = 0;
  578. regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
  579. case 16:
  580. if (overlay->crtc->base.fb->depth == 15) {
  581. regs->DCLRKV = RGB15_TO_COLORKEY(key);
  582. regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
  583. } else {
  584. regs->DCLRKV = RGB16_TO_COLORKEY(key);
  585. regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
  586. }
  587. case 24:
  588. case 32:
  589. regs->DCLRKV = key;
  590. regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
  591. }
  592. }
  593. static u32 overlay_cmd_reg(struct put_image_params *params)
  594. {
  595. u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
  596. if (params->format & I915_OVERLAY_YUV_PLANAR) {
  597. switch (params->format & I915_OVERLAY_DEPTH_MASK) {
  598. case I915_OVERLAY_YUV422:
  599. cmd |= OCMD_YUV_422_PLANAR;
  600. break;
  601. case I915_OVERLAY_YUV420:
  602. cmd |= OCMD_YUV_420_PLANAR;
  603. break;
  604. case I915_OVERLAY_YUV411:
  605. case I915_OVERLAY_YUV410:
  606. cmd |= OCMD_YUV_410_PLANAR;
  607. break;
  608. }
  609. } else { /* YUV packed */
  610. switch (params->format & I915_OVERLAY_DEPTH_MASK) {
  611. case I915_OVERLAY_YUV422:
  612. cmd |= OCMD_YUV_422_PACKED;
  613. break;
  614. case I915_OVERLAY_YUV411:
  615. cmd |= OCMD_YUV_411_PACKED;
  616. break;
  617. }
  618. switch (params->format & I915_OVERLAY_SWAP_MASK) {
  619. case I915_OVERLAY_NO_SWAP:
  620. break;
  621. case I915_OVERLAY_UV_SWAP:
  622. cmd |= OCMD_UV_SWAP;
  623. break;
  624. case I915_OVERLAY_Y_SWAP:
  625. cmd |= OCMD_Y_SWAP;
  626. break;
  627. case I915_OVERLAY_Y_AND_UV_SWAP:
  628. cmd |= OCMD_Y_AND_UV_SWAP;
  629. break;
  630. }
  631. }
  632. return cmd;
  633. }
  634. int intel_overlay_do_put_image(struct intel_overlay *overlay,
  635. struct drm_gem_object *new_bo,
  636. struct put_image_params *params)
  637. {
  638. int ret, tmp_width;
  639. struct overlay_registers *regs;
  640. bool scale_changed = false;
  641. struct drm_i915_gem_object *bo_priv = new_bo->driver_private;
  642. struct drm_device *dev = overlay->dev;
  643. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  644. BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
  645. BUG_ON(!overlay);
  646. ret = intel_overlay_release_old_vid(overlay);
  647. if (ret != 0)
  648. return ret;
  649. ret = i915_gem_object_pin(new_bo, PAGE_SIZE);
  650. if (ret != 0)
  651. return ret;
  652. ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
  653. if (ret != 0)
  654. goto out_unpin;
  655. if (!overlay->active) {
  656. regs = intel_overlay_map_regs_atomic(overlay);
  657. if (!regs) {
  658. ret = -ENOMEM;
  659. goto out_unpin;
  660. }
  661. regs->OCONFIG = OCONF_CC_OUT_8BIT;
  662. if (IS_I965GM(overlay->dev))
  663. regs->OCONFIG |= OCONF_CSC_MODE_BT709;
  664. regs->OCONFIG |= overlay->crtc->pipe == 0 ?
  665. OCONF_PIPE_A : OCONF_PIPE_B;
  666. intel_overlay_unmap_regs_atomic(overlay);
  667. ret = intel_overlay_on(overlay);
  668. if (ret != 0)
  669. goto out_unpin;
  670. }
  671. regs = intel_overlay_map_regs_atomic(overlay);
  672. if (!regs) {
  673. ret = -ENOMEM;
  674. goto out_unpin;
  675. }
  676. regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
  677. regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
  678. if (params->format & I915_OVERLAY_YUV_PACKED)
  679. tmp_width = packed_width_bytes(params->format, params->src_w);
  680. else
  681. tmp_width = params->src_w;
  682. regs->SWIDTH = params->src_w;
  683. regs->SWIDTHSW = calc_swidthsw(overlay->dev,
  684. params->offset_Y, tmp_width);
  685. regs->SHEIGHT = params->src_h;
  686. regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
  687. regs->OSTRIDE = params->stride_Y;
  688. if (params->format & I915_OVERLAY_YUV_PLANAR) {
  689. int uv_hscale = uv_hsubsampling(params->format);
  690. int uv_vscale = uv_vsubsampling(params->format);
  691. u32 tmp_U, tmp_V;
  692. regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
  693. tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
  694. params->src_w/uv_hscale);
  695. tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
  696. params->src_w/uv_hscale);
  697. regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
  698. regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
  699. regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
  700. regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
  701. regs->OSTRIDE |= params->stride_UV << 16;
  702. }
  703. scale_changed = update_scaling_factors(overlay, regs, params);
  704. update_colorkey(overlay, regs);
  705. regs->OCMD = overlay_cmd_reg(params);
  706. intel_overlay_unmap_regs_atomic(overlay);
  707. intel_overlay_continue(overlay, scale_changed);
  708. overlay->old_vid_bo = overlay->vid_bo;
  709. overlay->vid_bo = new_bo->driver_private;
  710. return 0;
  711. out_unpin:
  712. i915_gem_object_unpin(new_bo);
  713. return ret;
  714. }
  715. int intel_overlay_switch_off(struct intel_overlay *overlay)
  716. {
  717. int ret;
  718. struct overlay_registers *regs;
  719. struct drm_gem_object *obj;
  720. struct drm_device *dev = overlay->dev;
  721. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  722. BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
  723. if (!overlay->active)
  724. return 0;
  725. if (overlay->hw_wedged)
  726. return -EBUSY;
  727. ret = intel_overlay_release_old_vid(overlay);
  728. if (ret != 0)
  729. return ret;
  730. regs = intel_overlay_map_regs_atomic(overlay);
  731. regs->OCMD = 0;
  732. intel_overlay_unmap_regs_atomic(overlay);
  733. ret = intel_overlay_off(overlay);
  734. if (ret != 0)
  735. return ret;
  736. /* never have the overlay hw on without showing a frame */
  737. BUG_ON(!overlay->vid_bo);
  738. obj = overlay->vid_bo->obj;
  739. i915_gem_object_unpin(obj);
  740. drm_gem_object_unreference(obj);
  741. overlay->vid_bo = NULL;
  742. overlay->crtc->overlay = NULL;
  743. overlay->crtc = NULL;
  744. return 0;
  745. }
  746. static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
  747. struct intel_crtc *crtc)
  748. {
  749. drm_i915_private_t *dev_priv = overlay->dev->dev_private;
  750. u32 pipeconf;
  751. int pipeconf_reg = (crtc->pipe == 0) ? PIPEACONF : PIPEBCONF;
  752. if (!crtc->base.enabled || crtc->dpms_mode != DRM_MODE_DPMS_ON)
  753. return -EINVAL;
  754. pipeconf = I915_READ(pipeconf_reg);
  755. /* can't use the overlay with double wide pipe */
  756. if (!IS_I965G(overlay->dev) && pipeconf & PIPEACONF_DOUBLE_WIDE)
  757. return -EINVAL;
  758. return 0;
  759. }
  760. static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
  761. {
  762. struct drm_device *dev = overlay->dev;
  763. drm_i915_private_t *dev_priv = dev->dev_private;
  764. u32 ratio;
  765. u32 pfit_control = I915_READ(PFIT_CONTROL);
  766. /* XXX: This is not the same logic as in the xorg driver, but more in
  767. * line with the intel documentation for the i965 */
  768. if (!IS_I965G(dev) && (pfit_control & VERT_AUTO_SCALE)) {
  769. ratio = I915_READ(PFIT_AUTO_RATIOS) >> PFIT_VERT_SCALE_SHIFT;
  770. } else { /* on i965 use the PGM reg to read out the autoscaler values */
  771. ratio = I915_READ(PFIT_PGM_RATIOS);
  772. if (IS_I965G(dev))
  773. ratio >>= PFIT_VERT_SCALE_SHIFT_965;
  774. else
  775. ratio >>= PFIT_VERT_SCALE_SHIFT;
  776. }
  777. overlay->pfit_vscale_ratio = ratio;
  778. }
  779. static int check_overlay_dst(struct intel_overlay *overlay,
  780. struct drm_intel_overlay_put_image *rec)
  781. {
  782. struct drm_display_mode *mode = &overlay->crtc->base.mode;
  783. if ((rec->dst_x < mode->crtc_hdisplay)
  784. && (rec->dst_x + rec->dst_width
  785. <= mode->crtc_hdisplay)
  786. && (rec->dst_y < mode->crtc_vdisplay)
  787. && (rec->dst_y + rec->dst_height
  788. <= mode->crtc_vdisplay))
  789. return 0;
  790. else
  791. return -EINVAL;
  792. }
  793. static int check_overlay_scaling(struct put_image_params *rec)
  794. {
  795. u32 tmp;
  796. /* downscaling limit is 8.0 */
  797. tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
  798. if (tmp > 7)
  799. return -EINVAL;
  800. tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
  801. if (tmp > 7)
  802. return -EINVAL;
  803. return 0;
  804. }
  805. static int check_overlay_src(struct drm_device *dev,
  806. struct drm_intel_overlay_put_image *rec,
  807. struct drm_gem_object *new_bo)
  808. {
  809. u32 stride_mask;
  810. int depth;
  811. int uv_hscale = uv_hsubsampling(rec->flags);
  812. int uv_vscale = uv_vsubsampling(rec->flags);
  813. size_t tmp;
  814. /* check src dimensions */
  815. if (IS_845G(dev) || IS_I830(dev)) {
  816. if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY
  817. || rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
  818. return -EINVAL;
  819. } else {
  820. if (rec->src_height > IMAGE_MAX_HEIGHT
  821. || rec->src_width > IMAGE_MAX_WIDTH)
  822. return -EINVAL;
  823. }
  824. /* better safe than sorry, use 4 as the maximal subsampling ratio */
  825. if (rec->src_height < N_VERT_Y_TAPS*4
  826. || rec->src_width < N_HORIZ_Y_TAPS*4)
  827. return -EINVAL;
  828. /* check alingment constrains */
  829. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  830. case I915_OVERLAY_RGB:
  831. /* not implemented */
  832. return -EINVAL;
  833. case I915_OVERLAY_YUV_PACKED:
  834. depth = packed_depth_bytes(rec->flags);
  835. if (uv_vscale != 1)
  836. return -EINVAL;
  837. if (depth < 0)
  838. return depth;
  839. /* ignore UV planes */
  840. rec->stride_UV = 0;
  841. rec->offset_U = 0;
  842. rec->offset_V = 0;
  843. /* check pixel alignment */
  844. if (rec->offset_Y % depth)
  845. return -EINVAL;
  846. break;
  847. case I915_OVERLAY_YUV_PLANAR:
  848. if (uv_vscale < 0 || uv_hscale < 0)
  849. return -EINVAL;
  850. /* no offset restrictions for planar formats */
  851. break;
  852. default:
  853. return -EINVAL;
  854. }
  855. if (rec->src_width % uv_hscale)
  856. return -EINVAL;
  857. /* stride checking */
  858. stride_mask = 63;
  859. if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
  860. return -EINVAL;
  861. if (IS_I965G(dev) && rec->stride_Y < 512)
  862. return -EINVAL;
  863. tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
  864. 4 : 8;
  865. if (rec->stride_Y > tmp*1024 || rec->stride_UV > 2*1024)
  866. return -EINVAL;
  867. /* check buffer dimensions */
  868. switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
  869. case I915_OVERLAY_RGB:
  870. case I915_OVERLAY_YUV_PACKED:
  871. /* always 4 Y values per depth pixels */
  872. if (packed_width_bytes(rec->flags, rec->src_width)
  873. > rec->stride_Y)
  874. return -EINVAL;
  875. tmp = rec->stride_Y*rec->src_height;
  876. if (rec->offset_Y + tmp > new_bo->size)
  877. return -EINVAL;
  878. break;
  879. case I915_OVERLAY_YUV_PLANAR:
  880. if (rec->src_width > rec->stride_Y)
  881. return -EINVAL;
  882. if (rec->src_width/uv_hscale > rec->stride_UV)
  883. return -EINVAL;
  884. tmp = rec->stride_Y*rec->src_height;
  885. if (rec->offset_Y + tmp > new_bo->size)
  886. return -EINVAL;
  887. tmp = rec->stride_UV*rec->src_height;
  888. tmp /= uv_vscale;
  889. if (rec->offset_U + tmp > new_bo->size
  890. || rec->offset_V + tmp > new_bo->size)
  891. return -EINVAL;
  892. break;
  893. }
  894. return 0;
  895. }
  896. int intel_overlay_put_image(struct drm_device *dev, void *data,
  897. struct drm_file *file_priv)
  898. {
  899. struct drm_intel_overlay_put_image *put_image_rec = data;
  900. drm_i915_private_t *dev_priv = dev->dev_private;
  901. struct intel_overlay *overlay;
  902. struct drm_mode_object *drmmode_obj;
  903. struct intel_crtc *crtc;
  904. struct drm_gem_object *new_bo;
  905. struct put_image_params *params;
  906. int ret;
  907. if (!dev_priv) {
  908. DRM_ERROR("called with no initialization\n");
  909. return -EINVAL;
  910. }
  911. overlay = dev_priv->overlay;
  912. if (!overlay) {
  913. DRM_DEBUG("userspace bug: no overlay\n");
  914. return -ENODEV;
  915. }
  916. if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
  917. mutex_lock(&dev->mode_config.mutex);
  918. mutex_lock(&dev->struct_mutex);
  919. ret = intel_overlay_switch_off(overlay);
  920. mutex_unlock(&dev->struct_mutex);
  921. mutex_unlock(&dev->mode_config.mutex);
  922. return ret;
  923. }
  924. params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
  925. if (!params)
  926. return -ENOMEM;
  927. drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
  928. DRM_MODE_OBJECT_CRTC);
  929. if (!drmmode_obj)
  930. return -ENOENT;
  931. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  932. new_bo = drm_gem_object_lookup(dev, file_priv,
  933. put_image_rec->bo_handle);
  934. if (!new_bo)
  935. return -ENOENT;
  936. mutex_lock(&dev->mode_config.mutex);
  937. mutex_lock(&dev->struct_mutex);
  938. if (overlay->hw_wedged) {
  939. ret = intel_overlay_recover_from_interrupt(overlay, 1);
  940. if (ret != 0)
  941. goto out_unlock;
  942. }
  943. if (overlay->crtc != crtc) {
  944. struct drm_display_mode *mode = &crtc->base.mode;
  945. ret = intel_overlay_switch_off(overlay);
  946. if (ret != 0)
  947. goto out_unlock;
  948. ret = check_overlay_possible_on_crtc(overlay, crtc);
  949. if (ret != 0)
  950. goto out_unlock;
  951. overlay->crtc = crtc;
  952. crtc->overlay = overlay;
  953. if (intel_panel_fitter_pipe(dev) == crtc->pipe
  954. /* and line to wide, i.e. one-line-mode */
  955. && mode->hdisplay > 1024) {
  956. overlay->pfit_active = 1;
  957. update_pfit_vscale_ratio(overlay);
  958. } else
  959. overlay->pfit_active = 0;
  960. }
  961. ret = check_overlay_dst(overlay, put_image_rec);
  962. if (ret != 0)
  963. goto out_unlock;
  964. if (overlay->pfit_active) {
  965. params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
  966. overlay->pfit_vscale_ratio);
  967. /* shifting right rounds downwards, so add 1 */
  968. params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
  969. overlay->pfit_vscale_ratio) + 1;
  970. } else {
  971. params->dst_y = put_image_rec->dst_y;
  972. params->dst_h = put_image_rec->dst_height;
  973. }
  974. params->dst_x = put_image_rec->dst_x;
  975. params->dst_w = put_image_rec->dst_width;
  976. params->src_w = put_image_rec->src_width;
  977. params->src_h = put_image_rec->src_height;
  978. params->src_scan_w = put_image_rec->src_scan_width;
  979. params->src_scan_h = put_image_rec->src_scan_height;
  980. if (params->src_scan_h > params->src_h
  981. || params->src_scan_w > params->src_w) {
  982. ret = -EINVAL;
  983. goto out_unlock;
  984. }
  985. ret = check_overlay_src(dev, put_image_rec, new_bo);
  986. if (ret != 0)
  987. goto out_unlock;
  988. params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
  989. params->stride_Y = put_image_rec->stride_Y;
  990. params->stride_UV = put_image_rec->stride_UV;
  991. params->offset_Y = put_image_rec->offset_Y;
  992. params->offset_U = put_image_rec->offset_U;
  993. params->offset_V = put_image_rec->offset_V;
  994. /* Check scaling after src size to prevent a divide-by-zero. */
  995. ret = check_overlay_scaling(params);
  996. if (ret != 0)
  997. goto out_unlock;
  998. ret = intel_overlay_do_put_image(overlay, new_bo, params);
  999. if (ret != 0)
  1000. goto out_unlock;
  1001. mutex_unlock(&dev->struct_mutex);
  1002. mutex_unlock(&dev->mode_config.mutex);
  1003. kfree(params);
  1004. return 0;
  1005. out_unlock:
  1006. mutex_unlock(&dev->struct_mutex);
  1007. mutex_unlock(&dev->mode_config.mutex);
  1008. drm_gem_object_unreference(new_bo);
  1009. kfree(params);
  1010. return ret;
  1011. }
  1012. static void update_reg_attrs(struct intel_overlay *overlay,
  1013. struct overlay_registers *regs)
  1014. {
  1015. regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
  1016. regs->OCLRC1 = overlay->saturation;
  1017. }
  1018. static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
  1019. {
  1020. int i;
  1021. if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
  1022. return false;
  1023. for (i = 0; i < 3; i++) {
  1024. if (((gamma1 >> i * 8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
  1025. return false;
  1026. }
  1027. return true;
  1028. }
  1029. static bool check_gamma5_errata(u32 gamma5)
  1030. {
  1031. int i;
  1032. for (i = 0; i < 3; i++) {
  1033. if (((gamma5 >> i*8) & 0xff) == 0x80)
  1034. return false;
  1035. }
  1036. return true;
  1037. }
  1038. static int check_gamma(struct drm_intel_overlay_attrs *attrs)
  1039. {
  1040. if (!check_gamma_bounds(0, attrs->gamma0)
  1041. || !check_gamma_bounds(attrs->gamma0, attrs->gamma1)
  1042. || !check_gamma_bounds(attrs->gamma1, attrs->gamma2)
  1043. || !check_gamma_bounds(attrs->gamma2, attrs->gamma3)
  1044. || !check_gamma_bounds(attrs->gamma3, attrs->gamma4)
  1045. || !check_gamma_bounds(attrs->gamma4, attrs->gamma5)
  1046. || !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
  1047. return -EINVAL;
  1048. if (!check_gamma5_errata(attrs->gamma5))
  1049. return -EINVAL;
  1050. return 0;
  1051. }
  1052. int intel_overlay_attrs(struct drm_device *dev, void *data,
  1053. struct drm_file *file_priv)
  1054. {
  1055. struct drm_intel_overlay_attrs *attrs = data;
  1056. drm_i915_private_t *dev_priv = dev->dev_private;
  1057. struct intel_overlay *overlay;
  1058. struct overlay_registers *regs;
  1059. int ret;
  1060. if (!dev_priv) {
  1061. DRM_ERROR("called with no initialization\n");
  1062. return -EINVAL;
  1063. }
  1064. overlay = dev_priv->overlay;
  1065. if (!overlay) {
  1066. DRM_DEBUG("userspace bug: no overlay\n");
  1067. return -ENODEV;
  1068. }
  1069. mutex_lock(&dev->mode_config.mutex);
  1070. mutex_lock(&dev->struct_mutex);
  1071. if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
  1072. attrs->color_key = overlay->color_key;
  1073. attrs->brightness = overlay->brightness;
  1074. attrs->contrast = overlay->contrast;
  1075. attrs->saturation = overlay->saturation;
  1076. if (IS_I9XX(dev)) {
  1077. attrs->gamma0 = I915_READ(OGAMC0);
  1078. attrs->gamma1 = I915_READ(OGAMC1);
  1079. attrs->gamma2 = I915_READ(OGAMC2);
  1080. attrs->gamma3 = I915_READ(OGAMC3);
  1081. attrs->gamma4 = I915_READ(OGAMC4);
  1082. attrs->gamma5 = I915_READ(OGAMC5);
  1083. }
  1084. ret = 0;
  1085. } else {
  1086. overlay->color_key = attrs->color_key;
  1087. if (attrs->brightness >= -128 && attrs->brightness <= 127) {
  1088. overlay->brightness = attrs->brightness;
  1089. } else {
  1090. ret = -EINVAL;
  1091. goto out_unlock;
  1092. }
  1093. if (attrs->contrast <= 255) {
  1094. overlay->contrast = attrs->contrast;
  1095. } else {
  1096. ret = -EINVAL;
  1097. goto out_unlock;
  1098. }
  1099. if (attrs->saturation <= 1023) {
  1100. overlay->saturation = attrs->saturation;
  1101. } else {
  1102. ret = -EINVAL;
  1103. goto out_unlock;
  1104. }
  1105. regs = intel_overlay_map_regs_atomic(overlay);
  1106. if (!regs) {
  1107. ret = -ENOMEM;
  1108. goto out_unlock;
  1109. }
  1110. update_reg_attrs(overlay, regs);
  1111. intel_overlay_unmap_regs_atomic(overlay);
  1112. if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
  1113. if (!IS_I9XX(dev)) {
  1114. ret = -EINVAL;
  1115. goto out_unlock;
  1116. }
  1117. if (overlay->active) {
  1118. ret = -EBUSY;
  1119. goto out_unlock;
  1120. }
  1121. ret = check_gamma(attrs);
  1122. if (ret != 0)
  1123. goto out_unlock;
  1124. I915_WRITE(OGAMC0, attrs->gamma0);
  1125. I915_WRITE(OGAMC1, attrs->gamma1);
  1126. I915_WRITE(OGAMC2, attrs->gamma2);
  1127. I915_WRITE(OGAMC3, attrs->gamma3);
  1128. I915_WRITE(OGAMC4, attrs->gamma4);
  1129. I915_WRITE(OGAMC5, attrs->gamma5);
  1130. }
  1131. ret = 0;
  1132. }
  1133. out_unlock:
  1134. mutex_unlock(&dev->struct_mutex);
  1135. mutex_unlock(&dev->mode_config.mutex);
  1136. return ret;
  1137. }
  1138. void intel_setup_overlay(struct drm_device *dev)
  1139. {
  1140. drm_i915_private_t *dev_priv = dev->dev_private;
  1141. struct intel_overlay *overlay;
  1142. struct drm_gem_object *reg_bo;
  1143. struct overlay_registers *regs;
  1144. int ret;
  1145. if (!OVERLAY_EXISTS(dev))
  1146. return;
  1147. overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
  1148. if (!overlay)
  1149. return;
  1150. overlay->dev = dev;
  1151. reg_bo = drm_gem_object_alloc(dev, PAGE_SIZE);
  1152. if (!reg_bo)
  1153. goto out_free;
  1154. overlay->reg_bo = reg_bo->driver_private;
  1155. if (OVERLAY_NONPHYSICAL(dev)) {
  1156. ret = i915_gem_object_pin(reg_bo, PAGE_SIZE);
  1157. if (ret) {
  1158. DRM_ERROR("failed to pin overlay register bo\n");
  1159. goto out_free_bo;
  1160. }
  1161. overlay->flip_addr = overlay->reg_bo->gtt_offset;
  1162. } else {
  1163. ret = i915_gem_attach_phys_object(dev, reg_bo,
  1164. I915_GEM_PHYS_OVERLAY_REGS);
  1165. if (ret) {
  1166. DRM_ERROR("failed to attach phys overlay regs\n");
  1167. goto out_free_bo;
  1168. }
  1169. overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
  1170. }
  1171. /* init all values */
  1172. overlay->color_key = 0x0101fe;
  1173. overlay->brightness = -19;
  1174. overlay->contrast = 75;
  1175. overlay->saturation = 146;
  1176. regs = intel_overlay_map_regs_atomic(overlay);
  1177. if (!regs)
  1178. goto out_free_bo;
  1179. memset(regs, 0, sizeof(struct overlay_registers));
  1180. update_polyphase_filter(regs);
  1181. update_reg_attrs(overlay, regs);
  1182. intel_overlay_unmap_regs_atomic(overlay);
  1183. dev_priv->overlay = overlay;
  1184. DRM_INFO("initialized overlay support\n");
  1185. return;
  1186. out_free_bo:
  1187. drm_gem_object_unreference(reg_bo);
  1188. out_free:
  1189. kfree(overlay);
  1190. return;
  1191. }
  1192. void intel_cleanup_overlay(struct drm_device *dev)
  1193. {
  1194. drm_i915_private_t *dev_priv = dev->dev_private;
  1195. if (dev_priv->overlay) {
  1196. /* The bo's should be free'd by the generic code already.
  1197. * Furthermore modesetting teardown happens beforehand so the
  1198. * hardware should be off already */
  1199. BUG_ON(dev_priv->overlay->active);
  1200. kfree(dev_priv->overlay);
  1201. }
  1202. }