intel_overlay.c 40 KB

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