intel_sprite.c 35 KB

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