intel_sprite.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * Copyright © 2011 Intel Corporation
  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. * Jesse Barnes <jbarnes@virtuousgeek.org>
  25. *
  26. * New plane/sprite handling.
  27. *
  28. * The older chips had a separate interface for programming plane related
  29. * registers; newer ones are much simpler and we can use the new DRM plane
  30. * support.
  31. */
  32. #include <drm/drmP.h>
  33. #include <drm/drm_crtc.h>
  34. #include <drm/drm_fourcc.h>
  35. #include <drm/drm_rect.h>
  36. #include <drm/drm_plane_helper.h>
  37. #include "intel_drv.h"
  38. #include <drm/i915_drm.h>
  39. #include "i915_drv.h"
  40. static bool
  41. format_is_yuv(uint32_t format)
  42. {
  43. switch (format) {
  44. case DRM_FORMAT_YUYV:
  45. case DRM_FORMAT_UYVY:
  46. case DRM_FORMAT_VYUY:
  47. case DRM_FORMAT_YVYU:
  48. return true;
  49. default:
  50. return false;
  51. }
  52. }
  53. static int usecs_to_scanlines(const struct drm_display_mode *mode, int usecs)
  54. {
  55. /* paranoia */
  56. if (!mode->crtc_htotal)
  57. return 1;
  58. return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
  59. }
  60. /**
  61. * intel_pipe_update_start() - start update of a set of display registers
  62. * @crtc: the crtc of which the registers are going to be updated
  63. * @start_vbl_count: vblank counter return pointer used for error checking
  64. *
  65. * Mark the start of an update to pipe registers that should be updated
  66. * atomically regarding vblank. If the next vblank will happens within
  67. * the next 100 us, this function waits until the vblank passes.
  68. *
  69. * After a successful call to this function, interrupts will be disabled
  70. * until a subsequent call to intel_pipe_update_end(). That is done to
  71. * avoid random delays. The value written to @start_vbl_count should be
  72. * supplied to intel_pipe_update_end() for error checking.
  73. *
  74. * Return: true if the call was successful
  75. */
  76. bool intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl_count)
  77. {
  78. struct drm_device *dev = crtc->base.dev;
  79. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  80. enum pipe pipe = crtc->pipe;
  81. long timeout = msecs_to_jiffies_timeout(1);
  82. int scanline, min, max, vblank_start;
  83. wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
  84. DEFINE_WAIT(wait);
  85. vblank_start = mode->crtc_vblank_start;
  86. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  87. vblank_start = DIV_ROUND_UP(vblank_start, 2);
  88. /* FIXME needs to be calibrated sensibly */
  89. min = vblank_start - usecs_to_scanlines(mode, 100);
  90. max = vblank_start - 1;
  91. if (min <= 0 || max <= 0)
  92. return false;
  93. if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
  94. return false;
  95. local_irq_disable();
  96. trace_i915_pipe_update_start(crtc, min, max);
  97. for (;;) {
  98. /*
  99. * prepare_to_wait() has a memory barrier, which guarantees
  100. * other CPUs can see the task state update by the time we
  101. * read the scanline.
  102. */
  103. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  104. scanline = intel_get_crtc_scanline(crtc);
  105. if (scanline < min || scanline > max)
  106. break;
  107. if (timeout <= 0) {
  108. DRM_ERROR("Potential atomic update failure on pipe %c\n",
  109. pipe_name(crtc->pipe));
  110. break;
  111. }
  112. local_irq_enable();
  113. timeout = schedule_timeout(timeout);
  114. local_irq_disable();
  115. }
  116. finish_wait(wq, &wait);
  117. drm_crtc_vblank_put(&crtc->base);
  118. *start_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  119. trace_i915_pipe_update_vblank_evaded(crtc, min, max, *start_vbl_count);
  120. return true;
  121. }
  122. /**
  123. * intel_pipe_update_end() - end update of a set of display registers
  124. * @crtc: the crtc of which the registers were updated
  125. * @start_vbl_count: start vblank counter (used for error checking)
  126. *
  127. * Mark the end of an update started with intel_pipe_update_start(). This
  128. * re-enables interrupts and verifies the update was actually completed
  129. * before a vblank using the value of @start_vbl_count.
  130. */
  131. void intel_pipe_update_end(struct intel_crtc *crtc, u32 start_vbl_count)
  132. {
  133. struct drm_device *dev = crtc->base.dev;
  134. enum pipe pipe = crtc->pipe;
  135. u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  136. trace_i915_pipe_update_end(crtc, end_vbl_count);
  137. local_irq_enable();
  138. if (start_vbl_count != end_vbl_count)
  139. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
  140. pipe_name(pipe), start_vbl_count, end_vbl_count);
  141. }
  142. static void intel_update_primary_plane(struct intel_crtc *crtc)
  143. {
  144. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  145. int reg = DSPCNTR(crtc->plane);
  146. if (crtc->primary_enabled)
  147. I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
  148. else
  149. I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
  150. }
  151. static void
  152. skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
  153. struct drm_framebuffer *fb,
  154. int crtc_x, int crtc_y,
  155. unsigned int crtc_w, unsigned int crtc_h,
  156. uint32_t x, uint32_t y,
  157. uint32_t src_w, uint32_t src_h)
  158. {
  159. struct drm_device *dev = drm_plane->dev;
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  162. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  163. const int pipe = intel_plane->pipe;
  164. const int plane = intel_plane->plane + 1;
  165. u32 plane_ctl, stride_div;
  166. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  167. const struct drm_intel_sprite_colorkey *key = &intel_plane->ckey;
  168. plane_ctl = PLANE_CTL_ENABLE |
  169. PLANE_CTL_PIPE_CSC_ENABLE;
  170. switch (fb->pixel_format) {
  171. case DRM_FORMAT_RGB565:
  172. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  173. break;
  174. case DRM_FORMAT_XBGR8888:
  175. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  176. break;
  177. case DRM_FORMAT_XRGB8888:
  178. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  179. break;
  180. /*
  181. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  182. * to be already pre-multiplied. We need to add a knob (or a different
  183. * DRM_FORMAT) for user-space to configure that.
  184. */
  185. case DRM_FORMAT_ABGR8888:
  186. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  187. PLANE_CTL_ORDER_RGBX |
  188. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  189. break;
  190. case DRM_FORMAT_ARGB8888:
  191. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  192. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  193. break;
  194. case DRM_FORMAT_YUYV:
  195. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  196. break;
  197. case DRM_FORMAT_YVYU:
  198. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  199. break;
  200. case DRM_FORMAT_UYVY:
  201. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  202. break;
  203. case DRM_FORMAT_VYUY:
  204. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  205. break;
  206. default:
  207. BUG();
  208. }
  209. switch (fb->modifier[0]) {
  210. case DRM_FORMAT_MOD_NONE:
  211. break;
  212. case I915_FORMAT_MOD_X_TILED:
  213. plane_ctl |= PLANE_CTL_TILED_X;
  214. break;
  215. case I915_FORMAT_MOD_Y_TILED:
  216. plane_ctl |= PLANE_CTL_TILED_Y;
  217. break;
  218. case I915_FORMAT_MOD_Yf_TILED:
  219. plane_ctl |= PLANE_CTL_TILED_YF;
  220. break;
  221. default:
  222. MISSING_CASE(fb->modifier[0]);
  223. }
  224. if (drm_plane->state->rotation == BIT(DRM_ROTATE_180))
  225. plane_ctl |= PLANE_CTL_ROTATE_180;
  226. intel_update_sprite_watermarks(drm_plane, crtc, src_w, src_h,
  227. pixel_size, true,
  228. src_w != crtc_w || src_h != crtc_h);
  229. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  230. fb->pixel_format);
  231. /* Sizes are 0 based */
  232. src_w--;
  233. src_h--;
  234. crtc_w--;
  235. crtc_h--;
  236. if (key->flags) {
  237. I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
  238. I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
  239. I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
  240. }
  241. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  242. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  243. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  244. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  245. I915_WRITE(PLANE_OFFSET(pipe, plane), (y << 16) | x);
  246. I915_WRITE(PLANE_STRIDE(pipe, plane), fb->pitches[0] / stride_div);
  247. I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
  248. I915_WRITE(PLANE_SIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  249. I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
  250. I915_WRITE(PLANE_SURF(pipe, plane), i915_gem_obj_ggtt_offset(obj));
  251. POSTING_READ(PLANE_SURF(pipe, plane));
  252. }
  253. static void
  254. skl_disable_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc)
  255. {
  256. struct drm_device *dev = drm_plane->dev;
  257. struct drm_i915_private *dev_priv = dev->dev_private;
  258. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  259. const int pipe = intel_plane->pipe;
  260. const int plane = intel_plane->plane + 1;
  261. I915_WRITE(PLANE_CTL(pipe, plane), 0);
  262. /* Activate double buffered register update */
  263. I915_WRITE(PLANE_SURF(pipe, plane), 0);
  264. POSTING_READ(PLANE_SURF(pipe, plane));
  265. intel_update_sprite_watermarks(drm_plane, crtc, 0, 0, 0, false, false);
  266. }
  267. static void
  268. chv_update_csc(struct intel_plane *intel_plane, uint32_t format)
  269. {
  270. struct drm_i915_private *dev_priv = intel_plane->base.dev->dev_private;
  271. int plane = intel_plane->plane;
  272. /* Seems RGB data bypasses the CSC always */
  273. if (!format_is_yuv(format))
  274. return;
  275. /*
  276. * BT.601 limited range YCbCr -> full range RGB
  277. *
  278. * |r| | 6537 4769 0| |cr |
  279. * |g| = |-3330 4769 -1605| x |y-64|
  280. * |b| | 0 4769 8263| |cb |
  281. *
  282. * Cb and Cr apparently come in as signed already, so no
  283. * need for any offset. For Y we need to remove the offset.
  284. */
  285. I915_WRITE(SPCSCYGOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
  286. I915_WRITE(SPCSCCBOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  287. I915_WRITE(SPCSCCROFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
  288. I915_WRITE(SPCSCC01(plane), SPCSC_C1(4769) | SPCSC_C0(6537));
  289. I915_WRITE(SPCSCC23(plane), SPCSC_C1(-3330) | SPCSC_C0(0));
  290. I915_WRITE(SPCSCC45(plane), SPCSC_C1(-1605) | SPCSC_C0(4769));
  291. I915_WRITE(SPCSCC67(plane), SPCSC_C1(4769) | SPCSC_C0(0));
  292. I915_WRITE(SPCSCC8(plane), SPCSC_C0(8263));
  293. I915_WRITE(SPCSCYGICLAMP(plane), SPCSC_IMAX(940) | SPCSC_IMIN(64));
  294. I915_WRITE(SPCSCCBICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
  295. I915_WRITE(SPCSCCRICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
  296. I915_WRITE(SPCSCYGOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  297. I915_WRITE(SPCSCCBOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  298. I915_WRITE(SPCSCCROCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
  299. }
  300. static void
  301. vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
  302. struct drm_framebuffer *fb,
  303. int crtc_x, int crtc_y,
  304. unsigned int crtc_w, unsigned int crtc_h,
  305. uint32_t x, uint32_t y,
  306. uint32_t src_w, uint32_t src_h)
  307. {
  308. struct drm_device *dev = dplane->dev;
  309. struct drm_i915_private *dev_priv = dev->dev_private;
  310. struct intel_plane *intel_plane = to_intel_plane(dplane);
  311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  312. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  313. int pipe = intel_plane->pipe;
  314. int plane = intel_plane->plane;
  315. u32 sprctl;
  316. unsigned long sprsurf_offset, linear_offset;
  317. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  318. const struct drm_intel_sprite_colorkey *key = &intel_plane->ckey;
  319. sprctl = SP_ENABLE;
  320. switch (fb->pixel_format) {
  321. case DRM_FORMAT_YUYV:
  322. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  323. break;
  324. case DRM_FORMAT_YVYU:
  325. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  326. break;
  327. case DRM_FORMAT_UYVY:
  328. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  329. break;
  330. case DRM_FORMAT_VYUY:
  331. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  332. break;
  333. case DRM_FORMAT_RGB565:
  334. sprctl |= SP_FORMAT_BGR565;
  335. break;
  336. case DRM_FORMAT_XRGB8888:
  337. sprctl |= SP_FORMAT_BGRX8888;
  338. break;
  339. case DRM_FORMAT_ARGB8888:
  340. sprctl |= SP_FORMAT_BGRA8888;
  341. break;
  342. case DRM_FORMAT_XBGR2101010:
  343. sprctl |= SP_FORMAT_RGBX1010102;
  344. break;
  345. case DRM_FORMAT_ABGR2101010:
  346. sprctl |= SP_FORMAT_RGBA1010102;
  347. break;
  348. case DRM_FORMAT_XBGR8888:
  349. sprctl |= SP_FORMAT_RGBX8888;
  350. break;
  351. case DRM_FORMAT_ABGR8888:
  352. sprctl |= SP_FORMAT_RGBA8888;
  353. break;
  354. default:
  355. /*
  356. * If we get here one of the upper layers failed to filter
  357. * out the unsupported plane formats
  358. */
  359. BUG();
  360. break;
  361. }
  362. /*
  363. * Enable gamma to match primary/cursor plane behaviour.
  364. * FIXME should be user controllable via propertiesa.
  365. */
  366. sprctl |= SP_GAMMA_ENABLE;
  367. if (obj->tiling_mode != I915_TILING_NONE)
  368. sprctl |= SP_TILED;
  369. intel_update_sprite_watermarks(dplane, crtc, src_w, src_h,
  370. pixel_size, true,
  371. src_w != crtc_w || src_h != crtc_h);
  372. /* Sizes are 0 based */
  373. src_w--;
  374. src_h--;
  375. crtc_w--;
  376. crtc_h--;
  377. linear_offset = y * fb->pitches[0] + x * pixel_size;
  378. sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
  379. obj->tiling_mode,
  380. pixel_size,
  381. fb->pitches[0]);
  382. linear_offset -= sprsurf_offset;
  383. if (dplane->state->rotation == BIT(DRM_ROTATE_180)) {
  384. sprctl |= SP_ROTATE_180;
  385. x += src_w;
  386. y += src_h;
  387. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  388. }
  389. intel_update_primary_plane(intel_crtc);
  390. if (key->flags) {
  391. I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
  392. I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
  393. I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
  394. }
  395. if (key->flags & I915_SET_COLORKEY_SOURCE)
  396. sprctl |= SP_SOURCE_KEY;
  397. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B)
  398. chv_update_csc(intel_plane, fb->pixel_format);
  399. I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
  400. I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
  401. if (obj->tiling_mode != I915_TILING_NONE)
  402. I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
  403. else
  404. I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
  405. I915_WRITE(SPCONSTALPHA(pipe, plane), 0);
  406. I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  407. I915_WRITE(SPCNTR(pipe, plane), sprctl);
  408. I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
  409. sprsurf_offset);
  410. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  411. }
  412. static void
  413. vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
  414. {
  415. struct drm_device *dev = dplane->dev;
  416. struct drm_i915_private *dev_priv = dev->dev_private;
  417. struct intel_plane *intel_plane = to_intel_plane(dplane);
  418. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  419. int pipe = intel_plane->pipe;
  420. int plane = intel_plane->plane;
  421. intel_update_primary_plane(intel_crtc);
  422. I915_WRITE(SPCNTR(pipe, plane), 0);
  423. /* Activate double buffered register update */
  424. I915_WRITE(SPSURF(pipe, plane), 0);
  425. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  426. intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
  427. }
  428. static void
  429. ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  430. struct drm_framebuffer *fb,
  431. int crtc_x, int crtc_y,
  432. unsigned int crtc_w, unsigned int crtc_h,
  433. uint32_t x, uint32_t y,
  434. uint32_t src_w, uint32_t src_h)
  435. {
  436. struct drm_device *dev = plane->dev;
  437. struct drm_i915_private *dev_priv = dev->dev_private;
  438. struct intel_plane *intel_plane = to_intel_plane(plane);
  439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  440. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  441. enum pipe pipe = intel_plane->pipe;
  442. u32 sprctl, sprscale = 0;
  443. unsigned long sprsurf_offset, linear_offset;
  444. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  445. const struct drm_intel_sprite_colorkey *key = &intel_plane->ckey;
  446. sprctl = SPRITE_ENABLE;
  447. switch (fb->pixel_format) {
  448. case DRM_FORMAT_XBGR8888:
  449. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  450. break;
  451. case DRM_FORMAT_XRGB8888:
  452. sprctl |= SPRITE_FORMAT_RGBX888;
  453. break;
  454. case DRM_FORMAT_YUYV:
  455. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  456. break;
  457. case DRM_FORMAT_YVYU:
  458. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  459. break;
  460. case DRM_FORMAT_UYVY:
  461. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  462. break;
  463. case DRM_FORMAT_VYUY:
  464. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  465. break;
  466. default:
  467. BUG();
  468. }
  469. /*
  470. * Enable gamma to match primary/cursor plane behaviour.
  471. * FIXME should be user controllable via propertiesa.
  472. */
  473. sprctl |= SPRITE_GAMMA_ENABLE;
  474. if (obj->tiling_mode != I915_TILING_NONE)
  475. sprctl |= SPRITE_TILED;
  476. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  477. sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
  478. else
  479. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  480. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  481. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  482. intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
  483. true,
  484. src_w != crtc_w || src_h != crtc_h);
  485. /* Sizes are 0 based */
  486. src_w--;
  487. src_h--;
  488. crtc_w--;
  489. crtc_h--;
  490. if (crtc_w != src_w || crtc_h != src_h)
  491. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  492. linear_offset = y * fb->pitches[0] + x * pixel_size;
  493. sprsurf_offset =
  494. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  495. pixel_size, fb->pitches[0]);
  496. linear_offset -= sprsurf_offset;
  497. if (plane->state->rotation == BIT(DRM_ROTATE_180)) {
  498. sprctl |= SPRITE_ROTATE_180;
  499. /* HSW and BDW does this automagically in hardware */
  500. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  501. x += src_w;
  502. y += src_h;
  503. linear_offset += src_h * fb->pitches[0] +
  504. src_w * pixel_size;
  505. }
  506. }
  507. intel_update_primary_plane(intel_crtc);
  508. if (key->flags) {
  509. I915_WRITE(SPRKEYVAL(pipe), key->min_value);
  510. I915_WRITE(SPRKEYMAX(pipe), key->max_value);
  511. I915_WRITE(SPRKEYMSK(pipe), key->channel_mask);
  512. }
  513. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  514. sprctl |= SPRITE_DEST_KEY;
  515. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  516. sprctl |= SPRITE_SOURCE_KEY;
  517. I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
  518. I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  519. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  520. * register */
  521. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  522. I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
  523. else if (obj->tiling_mode != I915_TILING_NONE)
  524. I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
  525. else
  526. I915_WRITE(SPRLINOFF(pipe), linear_offset);
  527. I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  528. if (intel_plane->can_scale)
  529. I915_WRITE(SPRSCALE(pipe), sprscale);
  530. I915_WRITE(SPRCTL(pipe), sprctl);
  531. I915_WRITE(SPRSURF(pipe),
  532. i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
  533. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  534. }
  535. static void
  536. ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  537. {
  538. struct drm_device *dev = plane->dev;
  539. struct drm_i915_private *dev_priv = dev->dev_private;
  540. struct intel_plane *intel_plane = to_intel_plane(plane);
  541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  542. int pipe = intel_plane->pipe;
  543. intel_update_primary_plane(intel_crtc);
  544. I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
  545. /* Can't leave the scaler enabled... */
  546. if (intel_plane->can_scale)
  547. I915_WRITE(SPRSCALE(pipe), 0);
  548. /* Activate double buffered register update */
  549. I915_WRITE(SPRSURF(pipe), 0);
  550. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  551. }
  552. static void
  553. ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  554. struct drm_framebuffer *fb,
  555. int crtc_x, int crtc_y,
  556. unsigned int crtc_w, unsigned int crtc_h,
  557. uint32_t x, uint32_t y,
  558. uint32_t src_w, uint32_t src_h)
  559. {
  560. struct drm_device *dev = plane->dev;
  561. struct drm_i915_private *dev_priv = dev->dev_private;
  562. struct intel_plane *intel_plane = to_intel_plane(plane);
  563. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  564. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  565. int pipe = intel_plane->pipe;
  566. unsigned long dvssurf_offset, linear_offset;
  567. u32 dvscntr, dvsscale;
  568. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  569. const struct drm_intel_sprite_colorkey *key = &intel_plane->ckey;
  570. dvscntr = DVS_ENABLE;
  571. switch (fb->pixel_format) {
  572. case DRM_FORMAT_XBGR8888:
  573. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  574. break;
  575. case DRM_FORMAT_XRGB8888:
  576. dvscntr |= DVS_FORMAT_RGBX888;
  577. break;
  578. case DRM_FORMAT_YUYV:
  579. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  580. break;
  581. case DRM_FORMAT_YVYU:
  582. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  583. break;
  584. case DRM_FORMAT_UYVY:
  585. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  586. break;
  587. case DRM_FORMAT_VYUY:
  588. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  589. break;
  590. default:
  591. BUG();
  592. }
  593. /*
  594. * Enable gamma to match primary/cursor plane behaviour.
  595. * FIXME should be user controllable via propertiesa.
  596. */
  597. dvscntr |= DVS_GAMMA_ENABLE;
  598. if (obj->tiling_mode != I915_TILING_NONE)
  599. dvscntr |= DVS_TILED;
  600. if (IS_GEN6(dev))
  601. dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
  602. intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
  603. pixel_size, true,
  604. src_w != crtc_w || src_h != crtc_h);
  605. /* Sizes are 0 based */
  606. src_w--;
  607. src_h--;
  608. crtc_w--;
  609. crtc_h--;
  610. dvsscale = 0;
  611. if (crtc_w != src_w || crtc_h != src_h)
  612. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  613. linear_offset = y * fb->pitches[0] + x * pixel_size;
  614. dvssurf_offset =
  615. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  616. pixel_size, fb->pitches[0]);
  617. linear_offset -= dvssurf_offset;
  618. if (plane->state->rotation == BIT(DRM_ROTATE_180)) {
  619. dvscntr |= DVS_ROTATE_180;
  620. x += src_w;
  621. y += src_h;
  622. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  623. }
  624. intel_update_primary_plane(intel_crtc);
  625. if (key->flags) {
  626. I915_WRITE(DVSKEYVAL(pipe), key->min_value);
  627. I915_WRITE(DVSKEYMAX(pipe), key->max_value);
  628. I915_WRITE(DVSKEYMSK(pipe), key->channel_mask);
  629. }
  630. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  631. dvscntr |= DVS_DEST_KEY;
  632. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  633. dvscntr |= DVS_SOURCE_KEY;
  634. I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
  635. I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  636. if (obj->tiling_mode != I915_TILING_NONE)
  637. I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
  638. else
  639. I915_WRITE(DVSLINOFF(pipe), linear_offset);
  640. I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  641. I915_WRITE(DVSSCALE(pipe), dvsscale);
  642. I915_WRITE(DVSCNTR(pipe), dvscntr);
  643. I915_WRITE(DVSSURF(pipe),
  644. i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
  645. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  646. }
  647. static void
  648. ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  649. {
  650. struct drm_device *dev = plane->dev;
  651. struct drm_i915_private *dev_priv = dev->dev_private;
  652. struct intel_plane *intel_plane = to_intel_plane(plane);
  653. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  654. int pipe = intel_plane->pipe;
  655. intel_update_primary_plane(intel_crtc);
  656. I915_WRITE(DVSCNTR(pipe), 0);
  657. /* Disable the scaler */
  658. I915_WRITE(DVSSCALE(pipe), 0);
  659. /* Flush double buffered register updates */
  660. I915_WRITE(DVSSURF(pipe), 0);
  661. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  662. }
  663. /**
  664. * intel_post_enable_primary - Perform operations after enabling primary plane
  665. * @crtc: the CRTC whose primary plane was just enabled
  666. *
  667. * Performs potentially sleeping operations that must be done after the primary
  668. * plane is enabled, such as updating FBC and IPS. Note that this may be
  669. * called due to an explicit primary plane update, or due to an implicit
  670. * re-enable that is caused when a sprite plane is updated to no longer
  671. * completely hide the primary plane.
  672. */
  673. void
  674. intel_post_enable_primary(struct drm_crtc *crtc)
  675. {
  676. struct drm_device *dev = crtc->dev;
  677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  678. /*
  679. * BDW signals flip done immediately if the plane
  680. * is disabled, even if the plane enable is already
  681. * armed to occur at the next vblank :(
  682. */
  683. if (IS_BROADWELL(dev))
  684. intel_wait_for_vblank(dev, intel_crtc->pipe);
  685. /*
  686. * FIXME IPS should be fine as long as one plane is
  687. * enabled, but in practice it seems to have problems
  688. * when going from primary only to sprite only and vice
  689. * versa.
  690. */
  691. hsw_enable_ips(intel_crtc);
  692. mutex_lock(&dev->struct_mutex);
  693. intel_fbc_update(dev);
  694. mutex_unlock(&dev->struct_mutex);
  695. }
  696. /**
  697. * intel_pre_disable_primary - Perform operations before disabling primary plane
  698. * @crtc: the CRTC whose primary plane is to be disabled
  699. *
  700. * Performs potentially sleeping operations that must be done before the
  701. * primary plane is enabled, such as updating FBC and IPS. Note that this may
  702. * be called due to an explicit primary plane update, or due to an implicit
  703. * disable that is caused when a sprite plane completely hides the primary
  704. * plane.
  705. */
  706. void
  707. intel_pre_disable_primary(struct drm_crtc *crtc)
  708. {
  709. struct drm_device *dev = crtc->dev;
  710. struct drm_i915_private *dev_priv = dev->dev_private;
  711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  712. mutex_lock(&dev->struct_mutex);
  713. if (dev_priv->fbc.crtc == intel_crtc)
  714. intel_fbc_disable(dev);
  715. mutex_unlock(&dev->struct_mutex);
  716. /*
  717. * FIXME IPS should be fine as long as one plane is
  718. * enabled, but in practice it seems to have problems
  719. * when going from primary only to sprite only and vice
  720. * versa.
  721. */
  722. hsw_disable_ips(intel_crtc);
  723. }
  724. static bool colorkey_enabled(struct intel_plane *intel_plane)
  725. {
  726. return intel_plane->ckey.flags != I915_SET_COLORKEY_NONE;
  727. }
  728. static int
  729. intel_check_sprite_plane(struct drm_plane *plane,
  730. struct intel_plane_state *state)
  731. {
  732. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  733. struct intel_plane *intel_plane = to_intel_plane(plane);
  734. struct drm_framebuffer *fb = state->base.fb;
  735. int crtc_x, crtc_y;
  736. unsigned int crtc_w, crtc_h;
  737. uint32_t src_x, src_y, src_w, src_h;
  738. struct drm_rect *src = &state->src;
  739. struct drm_rect *dst = &state->dst;
  740. const struct drm_rect *clip = &state->clip;
  741. int hscale, vscale;
  742. int max_scale, min_scale;
  743. int pixel_size;
  744. intel_crtc = intel_crtc ? intel_crtc : to_intel_crtc(plane->crtc);
  745. if (!fb) {
  746. state->visible = false;
  747. goto finish;
  748. }
  749. /* Don't modify another pipe's plane */
  750. if (intel_plane->pipe != intel_crtc->pipe) {
  751. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  752. return -EINVAL;
  753. }
  754. /* FIXME check all gen limits */
  755. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
  756. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  757. return -EINVAL;
  758. }
  759. /*
  760. * FIXME the following code does a bunch of fuzzy adjustments to the
  761. * coordinates and sizes. We probably need some way to decide whether
  762. * more strict checking should be done instead.
  763. */
  764. max_scale = intel_plane->max_downscale << 16;
  765. min_scale = intel_plane->can_scale ? 1 : (1 << 16);
  766. drm_rect_rotate(src, fb->width << 16, fb->height << 16,
  767. state->base.rotation);
  768. hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
  769. BUG_ON(hscale < 0);
  770. vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
  771. BUG_ON(vscale < 0);
  772. state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
  773. crtc_x = dst->x1;
  774. crtc_y = dst->y1;
  775. crtc_w = drm_rect_width(dst);
  776. crtc_h = drm_rect_height(dst);
  777. if (state->visible) {
  778. /* check again in case clipping clamped the results */
  779. hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
  780. if (hscale < 0) {
  781. DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
  782. drm_rect_debug_print(src, true);
  783. drm_rect_debug_print(dst, false);
  784. return hscale;
  785. }
  786. vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
  787. if (vscale < 0) {
  788. DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
  789. drm_rect_debug_print(src, true);
  790. drm_rect_debug_print(dst, false);
  791. return vscale;
  792. }
  793. /* Make the source viewport size an exact multiple of the scaling factors. */
  794. drm_rect_adjust_size(src,
  795. drm_rect_width(dst) * hscale - drm_rect_width(src),
  796. drm_rect_height(dst) * vscale - drm_rect_height(src));
  797. drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
  798. state->base.rotation);
  799. /* sanity check to make sure the src viewport wasn't enlarged */
  800. WARN_ON(src->x1 < (int) state->base.src_x ||
  801. src->y1 < (int) state->base.src_y ||
  802. src->x2 > (int) state->base.src_x + state->base.src_w ||
  803. src->y2 > (int) state->base.src_y + state->base.src_h);
  804. /*
  805. * Hardware doesn't handle subpixel coordinates.
  806. * Adjust to (macro)pixel boundary, but be careful not to
  807. * increase the source viewport size, because that could
  808. * push the downscaling factor out of bounds.
  809. */
  810. src_x = src->x1 >> 16;
  811. src_w = drm_rect_width(src) >> 16;
  812. src_y = src->y1 >> 16;
  813. src_h = drm_rect_height(src) >> 16;
  814. if (format_is_yuv(fb->pixel_format)) {
  815. src_x &= ~1;
  816. src_w &= ~1;
  817. /*
  818. * Must keep src and dst the
  819. * same if we can't scale.
  820. */
  821. if (!intel_plane->can_scale)
  822. crtc_w &= ~1;
  823. if (crtc_w == 0)
  824. state->visible = false;
  825. }
  826. }
  827. /* Check size restrictions when scaling */
  828. if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
  829. unsigned int width_bytes;
  830. WARN_ON(!intel_plane->can_scale);
  831. /* FIXME interlacing min height is 6 */
  832. if (crtc_w < 3 || crtc_h < 3)
  833. state->visible = false;
  834. if (src_w < 3 || src_h < 3)
  835. state->visible = false;
  836. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  837. width_bytes = ((src_x * pixel_size) & 63) +
  838. src_w * pixel_size;
  839. if (src_w > 2048 || src_h > 2048 ||
  840. width_bytes > 4096 || fb->pitches[0] > 4096) {
  841. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  842. return -EINVAL;
  843. }
  844. }
  845. if (state->visible) {
  846. src->x1 = src_x;
  847. src->x2 = src_x + src_w;
  848. src->y1 = src_y;
  849. src->y2 = src_y + src_h;
  850. }
  851. dst->x1 = crtc_x;
  852. dst->x2 = crtc_x + crtc_w;
  853. dst->y1 = crtc_y;
  854. dst->y2 = crtc_y + crtc_h;
  855. finish:
  856. /*
  857. * If the sprite is completely covering the primary plane,
  858. * we can disable the primary and save power.
  859. */
  860. state->hides_primary = fb != NULL && drm_rect_equals(dst, clip) &&
  861. !colorkey_enabled(intel_plane);
  862. WARN_ON(state->hides_primary && !state->visible && intel_crtc->active);
  863. if (intel_crtc->active) {
  864. if (intel_crtc->primary_enabled == state->hides_primary)
  865. intel_crtc->atomic.wait_for_flips = true;
  866. if (intel_crtc->primary_enabled && state->hides_primary)
  867. intel_crtc->atomic.pre_disable_primary = true;
  868. intel_crtc->atomic.fb_bits |=
  869. INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
  870. if (!intel_crtc->primary_enabled && !state->hides_primary)
  871. intel_crtc->atomic.post_enable_primary = true;
  872. /* Update watermarks on tiling changes. */
  873. if (!plane->state->fb || !state->base.fb ||
  874. plane->state->fb->modifier[0] !=
  875. state->base.fb->modifier[0])
  876. intel_crtc->atomic.update_wm = true;
  877. if (!state->visible) {
  878. /*
  879. * Avoid underruns when disabling the sprite.
  880. * FIXME remove once watermark updates are done properly.
  881. */
  882. intel_crtc->atomic.wait_vblank = true;
  883. intel_crtc->atomic.update_sprite_watermarks |=
  884. (1 << drm_plane_index(plane));
  885. }
  886. }
  887. return 0;
  888. }
  889. static void
  890. intel_commit_sprite_plane(struct drm_plane *plane,
  891. struct intel_plane_state *state)
  892. {
  893. struct drm_crtc *crtc = state->base.crtc;
  894. struct intel_crtc *intel_crtc;
  895. struct intel_plane *intel_plane = to_intel_plane(plane);
  896. struct drm_framebuffer *fb = state->base.fb;
  897. int crtc_x, crtc_y;
  898. unsigned int crtc_w, crtc_h;
  899. uint32_t src_x, src_y, src_w, src_h;
  900. crtc = crtc ? crtc : plane->crtc;
  901. intel_crtc = to_intel_crtc(crtc);
  902. plane->fb = fb;
  903. if (intel_crtc->active) {
  904. intel_crtc->primary_enabled = !state->hides_primary;
  905. if (state->visible) {
  906. crtc_x = state->dst.x1;
  907. crtc_y = state->dst.y1;
  908. crtc_w = drm_rect_width(&state->dst);
  909. crtc_h = drm_rect_height(&state->dst);
  910. src_x = state->src.x1;
  911. src_y = state->src.y1;
  912. src_w = drm_rect_width(&state->src);
  913. src_h = drm_rect_height(&state->src);
  914. intel_plane->update_plane(plane, crtc, fb,
  915. crtc_x, crtc_y, crtc_w, crtc_h,
  916. src_x, src_y, src_w, src_h);
  917. } else {
  918. intel_plane->disable_plane(plane, crtc);
  919. }
  920. }
  921. }
  922. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  923. struct drm_file *file_priv)
  924. {
  925. struct drm_intel_sprite_colorkey *set = data;
  926. struct drm_plane *plane;
  927. struct intel_plane *intel_plane;
  928. int ret = 0;
  929. /* Make sure we don't try to enable both src & dest simultaneously */
  930. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  931. return -EINVAL;
  932. if (IS_VALLEYVIEW(dev) &&
  933. set->flags & I915_SET_COLORKEY_DESTINATION)
  934. return -EINVAL;
  935. drm_modeset_lock_all(dev);
  936. plane = drm_plane_find(dev, set->plane_id);
  937. if (!plane) {
  938. ret = -ENOENT;
  939. goto out_unlock;
  940. }
  941. intel_plane = to_intel_plane(plane);
  942. intel_plane->ckey = *set;
  943. /*
  944. * The only way this could fail would be due to
  945. * the current plane state being unsupportable already,
  946. * and we dont't consider that an error for the
  947. * colorkey ioctl. So just ignore any error.
  948. */
  949. intel_plane_restore(plane);
  950. out_unlock:
  951. drm_modeset_unlock_all(dev);
  952. return ret;
  953. }
  954. int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
  955. struct drm_file *file_priv)
  956. {
  957. struct drm_intel_sprite_colorkey *get = data;
  958. struct drm_plane *plane;
  959. struct intel_plane *intel_plane;
  960. int ret = 0;
  961. drm_modeset_lock_all(dev);
  962. plane = drm_plane_find(dev, get->plane_id);
  963. if (!plane) {
  964. ret = -ENOENT;
  965. goto out_unlock;
  966. }
  967. intel_plane = to_intel_plane(plane);
  968. *get = intel_plane->ckey;
  969. out_unlock:
  970. drm_modeset_unlock_all(dev);
  971. return ret;
  972. }
  973. int intel_plane_restore(struct drm_plane *plane)
  974. {
  975. if (!plane->crtc || !plane->state->fb)
  976. return 0;
  977. return plane->funcs->update_plane(plane, plane->crtc, plane->state->fb,
  978. plane->state->crtc_x, plane->state->crtc_y,
  979. plane->state->crtc_w, plane->state->crtc_h,
  980. plane->state->src_x, plane->state->src_y,
  981. plane->state->src_w, plane->state->src_h);
  982. }
  983. static uint32_t ilk_plane_formats[] = {
  984. DRM_FORMAT_XRGB8888,
  985. DRM_FORMAT_YUYV,
  986. DRM_FORMAT_YVYU,
  987. DRM_FORMAT_UYVY,
  988. DRM_FORMAT_VYUY,
  989. };
  990. static uint32_t snb_plane_formats[] = {
  991. DRM_FORMAT_XBGR8888,
  992. DRM_FORMAT_XRGB8888,
  993. DRM_FORMAT_YUYV,
  994. DRM_FORMAT_YVYU,
  995. DRM_FORMAT_UYVY,
  996. DRM_FORMAT_VYUY,
  997. };
  998. static uint32_t vlv_plane_formats[] = {
  999. DRM_FORMAT_RGB565,
  1000. DRM_FORMAT_ABGR8888,
  1001. DRM_FORMAT_ARGB8888,
  1002. DRM_FORMAT_XBGR8888,
  1003. DRM_FORMAT_XRGB8888,
  1004. DRM_FORMAT_XBGR2101010,
  1005. DRM_FORMAT_ABGR2101010,
  1006. DRM_FORMAT_YUYV,
  1007. DRM_FORMAT_YVYU,
  1008. DRM_FORMAT_UYVY,
  1009. DRM_FORMAT_VYUY,
  1010. };
  1011. static uint32_t skl_plane_formats[] = {
  1012. DRM_FORMAT_RGB565,
  1013. DRM_FORMAT_ABGR8888,
  1014. DRM_FORMAT_ARGB8888,
  1015. DRM_FORMAT_XBGR8888,
  1016. DRM_FORMAT_XRGB8888,
  1017. DRM_FORMAT_YUYV,
  1018. DRM_FORMAT_YVYU,
  1019. DRM_FORMAT_UYVY,
  1020. DRM_FORMAT_VYUY,
  1021. };
  1022. int
  1023. intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
  1024. {
  1025. struct intel_plane *intel_plane;
  1026. struct intel_plane_state *state;
  1027. unsigned long possible_crtcs;
  1028. const uint32_t *plane_formats;
  1029. int num_plane_formats;
  1030. int ret;
  1031. if (INTEL_INFO(dev)->gen < 5)
  1032. return -ENODEV;
  1033. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1034. if (!intel_plane)
  1035. return -ENOMEM;
  1036. state = intel_create_plane_state(&intel_plane->base);
  1037. if (!state) {
  1038. kfree(intel_plane);
  1039. return -ENOMEM;
  1040. }
  1041. intel_plane->base.state = &state->base;
  1042. switch (INTEL_INFO(dev)->gen) {
  1043. case 5:
  1044. case 6:
  1045. intel_plane->can_scale = true;
  1046. intel_plane->max_downscale = 16;
  1047. intel_plane->update_plane = ilk_update_plane;
  1048. intel_plane->disable_plane = ilk_disable_plane;
  1049. if (IS_GEN6(dev)) {
  1050. plane_formats = snb_plane_formats;
  1051. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1052. } else {
  1053. plane_formats = ilk_plane_formats;
  1054. num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
  1055. }
  1056. break;
  1057. case 7:
  1058. case 8:
  1059. if (IS_IVYBRIDGE(dev)) {
  1060. intel_plane->can_scale = true;
  1061. intel_plane->max_downscale = 2;
  1062. } else {
  1063. intel_plane->can_scale = false;
  1064. intel_plane->max_downscale = 1;
  1065. }
  1066. if (IS_VALLEYVIEW(dev)) {
  1067. intel_plane->update_plane = vlv_update_plane;
  1068. intel_plane->disable_plane = vlv_disable_plane;
  1069. plane_formats = vlv_plane_formats;
  1070. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1071. } else {
  1072. intel_plane->update_plane = ivb_update_plane;
  1073. intel_plane->disable_plane = ivb_disable_plane;
  1074. plane_formats = snb_plane_formats;
  1075. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1076. }
  1077. break;
  1078. case 9:
  1079. /*
  1080. * FIXME: Skylake planes can be scaled (with some restrictions),
  1081. * but this is for another time.
  1082. */
  1083. intel_plane->can_scale = false;
  1084. intel_plane->max_downscale = 1;
  1085. intel_plane->update_plane = skl_update_plane;
  1086. intel_plane->disable_plane = skl_disable_plane;
  1087. plane_formats = skl_plane_formats;
  1088. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1089. break;
  1090. default:
  1091. kfree(intel_plane);
  1092. return -ENODEV;
  1093. }
  1094. intel_plane->pipe = pipe;
  1095. intel_plane->plane = plane;
  1096. intel_plane->check_plane = intel_check_sprite_plane;
  1097. intel_plane->commit_plane = intel_commit_sprite_plane;
  1098. possible_crtcs = (1 << pipe);
  1099. ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
  1100. &intel_plane_funcs,
  1101. plane_formats, num_plane_formats,
  1102. DRM_PLANE_TYPE_OVERLAY);
  1103. if (ret) {
  1104. kfree(intel_plane);
  1105. goto out;
  1106. }
  1107. if (!dev->mode_config.rotation_property)
  1108. dev->mode_config.rotation_property =
  1109. drm_mode_create_rotation_property(dev,
  1110. BIT(DRM_ROTATE_0) |
  1111. BIT(DRM_ROTATE_180));
  1112. if (dev->mode_config.rotation_property)
  1113. drm_object_attach_property(&intel_plane->base.base,
  1114. dev->mode_config.rotation_property,
  1115. state->base.rotation);
  1116. drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
  1117. out:
  1118. return ret;
  1119. }