intel_overlay.c 40 KB

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