intel_overlay.c 41 KB

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