intel_overlay.c 41 KB

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