intel_sprite.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. static void
  39. ivb_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
  40. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  41. unsigned int crtc_w, unsigned int crtc_h,
  42. uint32_t x, uint32_t y,
  43. uint32_t src_w, uint32_t src_h)
  44. {
  45. struct drm_device *dev = plane->dev;
  46. struct drm_i915_private *dev_priv = dev->dev_private;
  47. struct intel_plane *intel_plane = to_intel_plane(plane);
  48. int pipe = intel_plane->pipe;
  49. u32 sprctl, sprscale = 0;
  50. unsigned long sprsurf_offset, linear_offset;
  51. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  52. bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
  53. sprctl = I915_READ(SPRCTL(pipe));
  54. /* Mask out pixel format bits in case we change it */
  55. sprctl &= ~SPRITE_PIXFORMAT_MASK;
  56. sprctl &= ~SPRITE_RGB_ORDER_RGBX;
  57. sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
  58. sprctl &= ~SPRITE_TILED;
  59. switch (fb->pixel_format) {
  60. case DRM_FORMAT_XBGR8888:
  61. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  62. break;
  63. case DRM_FORMAT_XRGB8888:
  64. sprctl |= SPRITE_FORMAT_RGBX888;
  65. break;
  66. case DRM_FORMAT_YUYV:
  67. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  68. break;
  69. case DRM_FORMAT_YVYU:
  70. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  71. break;
  72. case DRM_FORMAT_UYVY:
  73. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  74. break;
  75. case DRM_FORMAT_VYUY:
  76. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  77. break;
  78. default:
  79. BUG();
  80. }
  81. if (obj->tiling_mode != I915_TILING_NONE)
  82. sprctl |= SPRITE_TILED;
  83. /* must disable */
  84. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  85. sprctl |= SPRITE_ENABLE;
  86. /* Sizes are 0 based */
  87. src_w--;
  88. src_h--;
  89. crtc_w--;
  90. crtc_h--;
  91. intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
  92. /*
  93. * IVB workaround: must disable low power watermarks for at least
  94. * one frame before enabling scaling. LP watermarks can be re-enabled
  95. * when scaling is disabled.
  96. */
  97. if (crtc_w != src_w || crtc_h != src_h) {
  98. dev_priv->sprite_scaling_enabled |= 1 << pipe;
  99. if (!scaling_was_enabled) {
  100. intel_update_watermarks(dev);
  101. intel_wait_for_vblank(dev, pipe);
  102. }
  103. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  104. } else
  105. dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
  106. I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
  107. I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  108. linear_offset = y * fb->pitches[0] + x * pixel_size;
  109. sprsurf_offset =
  110. intel_gen4_compute_offset_xtiled(&x, &y,
  111. pixel_size, fb->pitches[0]);
  112. linear_offset -= sprsurf_offset;
  113. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  114. * register */
  115. if (IS_HASWELL(dev))
  116. I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
  117. else if (obj->tiling_mode != I915_TILING_NONE)
  118. I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
  119. else
  120. I915_WRITE(SPRLINOFF(pipe), linear_offset);
  121. I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  122. if (intel_plane->can_scale)
  123. I915_WRITE(SPRSCALE(pipe), sprscale);
  124. I915_WRITE(SPRCTL(pipe), sprctl);
  125. I915_MODIFY_DISPBASE(SPRSURF(pipe), obj->gtt_offset + sprsurf_offset);
  126. POSTING_READ(SPRSURF(pipe));
  127. /* potentially re-enable LP watermarks */
  128. if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
  129. intel_update_watermarks(dev);
  130. }
  131. static void
  132. ivb_disable_plane(struct drm_plane *plane)
  133. {
  134. struct drm_device *dev = plane->dev;
  135. struct drm_i915_private *dev_priv = dev->dev_private;
  136. struct intel_plane *intel_plane = to_intel_plane(plane);
  137. int pipe = intel_plane->pipe;
  138. bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
  139. I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
  140. /* Can't leave the scaler enabled... */
  141. if (intel_plane->can_scale)
  142. I915_WRITE(SPRSCALE(pipe), 0);
  143. /* Activate double buffered register update */
  144. I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
  145. POSTING_READ(SPRSURF(pipe));
  146. dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
  147. /* potentially re-enable LP watermarks */
  148. if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
  149. intel_update_watermarks(dev);
  150. }
  151. static int
  152. ivb_update_colorkey(struct drm_plane *plane,
  153. struct drm_intel_sprite_colorkey *key)
  154. {
  155. struct drm_device *dev = plane->dev;
  156. struct drm_i915_private *dev_priv = dev->dev_private;
  157. struct intel_plane *intel_plane;
  158. u32 sprctl;
  159. int ret = 0;
  160. intel_plane = to_intel_plane(plane);
  161. I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
  162. I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
  163. I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
  164. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  165. sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
  166. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  167. sprctl |= SPRITE_DEST_KEY;
  168. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  169. sprctl |= SPRITE_SOURCE_KEY;
  170. I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
  171. POSTING_READ(SPRKEYMSK(intel_plane->pipe));
  172. return ret;
  173. }
  174. static void
  175. ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  176. {
  177. struct drm_device *dev = plane->dev;
  178. struct drm_i915_private *dev_priv = dev->dev_private;
  179. struct intel_plane *intel_plane;
  180. u32 sprctl;
  181. intel_plane = to_intel_plane(plane);
  182. key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
  183. key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
  184. key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
  185. key->flags = 0;
  186. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  187. if (sprctl & SPRITE_DEST_KEY)
  188. key->flags = I915_SET_COLORKEY_DESTINATION;
  189. else if (sprctl & SPRITE_SOURCE_KEY)
  190. key->flags = I915_SET_COLORKEY_SOURCE;
  191. else
  192. key->flags = I915_SET_COLORKEY_NONE;
  193. }
  194. static void
  195. ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
  196. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  197. unsigned int crtc_w, unsigned int crtc_h,
  198. uint32_t x, uint32_t y,
  199. uint32_t src_w, uint32_t src_h)
  200. {
  201. struct drm_device *dev = plane->dev;
  202. struct drm_i915_private *dev_priv = dev->dev_private;
  203. struct intel_plane *intel_plane = to_intel_plane(plane);
  204. int pipe = intel_plane->pipe;
  205. unsigned long dvssurf_offset, linear_offset;
  206. u32 dvscntr, dvsscale;
  207. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  208. dvscntr = I915_READ(DVSCNTR(pipe));
  209. /* Mask out pixel format bits in case we change it */
  210. dvscntr &= ~DVS_PIXFORMAT_MASK;
  211. dvscntr &= ~DVS_RGB_ORDER_XBGR;
  212. dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
  213. dvscntr &= ~DVS_TILED;
  214. switch (fb->pixel_format) {
  215. case DRM_FORMAT_XBGR8888:
  216. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  217. break;
  218. case DRM_FORMAT_XRGB8888:
  219. dvscntr |= DVS_FORMAT_RGBX888;
  220. break;
  221. case DRM_FORMAT_YUYV:
  222. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  223. break;
  224. case DRM_FORMAT_YVYU:
  225. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  226. break;
  227. case DRM_FORMAT_UYVY:
  228. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  229. break;
  230. case DRM_FORMAT_VYUY:
  231. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  232. break;
  233. default:
  234. BUG();
  235. }
  236. if (obj->tiling_mode != I915_TILING_NONE)
  237. dvscntr |= DVS_TILED;
  238. if (IS_GEN6(dev))
  239. dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
  240. dvscntr |= DVS_ENABLE;
  241. /* Sizes are 0 based */
  242. src_w--;
  243. src_h--;
  244. crtc_w--;
  245. crtc_h--;
  246. intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
  247. dvsscale = 0;
  248. if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
  249. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  250. I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
  251. I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  252. linear_offset = y * fb->pitches[0] + x * pixel_size;
  253. dvssurf_offset =
  254. intel_gen4_compute_offset_xtiled(&x, &y,
  255. pixel_size, fb->pitches[0]);
  256. linear_offset -= dvssurf_offset;
  257. if (obj->tiling_mode != I915_TILING_NONE)
  258. I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
  259. else
  260. I915_WRITE(DVSLINOFF(pipe), linear_offset);
  261. I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  262. I915_WRITE(DVSSCALE(pipe), dvsscale);
  263. I915_WRITE(DVSCNTR(pipe), dvscntr);
  264. I915_MODIFY_DISPBASE(DVSSURF(pipe), obj->gtt_offset + dvssurf_offset);
  265. POSTING_READ(DVSSURF(pipe));
  266. }
  267. static void
  268. ilk_disable_plane(struct drm_plane *plane)
  269. {
  270. struct drm_device *dev = plane->dev;
  271. struct drm_i915_private *dev_priv = dev->dev_private;
  272. struct intel_plane *intel_plane = to_intel_plane(plane);
  273. int pipe = intel_plane->pipe;
  274. I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
  275. /* Disable the scaler */
  276. I915_WRITE(DVSSCALE(pipe), 0);
  277. /* Flush double buffered register updates */
  278. I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
  279. POSTING_READ(DVSSURF(pipe));
  280. }
  281. static void
  282. intel_enable_primary(struct drm_crtc *crtc)
  283. {
  284. struct drm_device *dev = crtc->dev;
  285. struct drm_i915_private *dev_priv = dev->dev_private;
  286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  287. int reg = DSPCNTR(intel_crtc->plane);
  288. if (!intel_crtc->primary_disabled)
  289. return;
  290. intel_crtc->primary_disabled = false;
  291. intel_update_fbc(dev);
  292. I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
  293. }
  294. static void
  295. intel_disable_primary(struct drm_crtc *crtc)
  296. {
  297. struct drm_device *dev = crtc->dev;
  298. struct drm_i915_private *dev_priv = dev->dev_private;
  299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  300. int reg = DSPCNTR(intel_crtc->plane);
  301. if (intel_crtc->primary_disabled)
  302. return;
  303. I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
  304. intel_crtc->primary_disabled = true;
  305. intel_update_fbc(dev);
  306. }
  307. static int
  308. ilk_update_colorkey(struct drm_plane *plane,
  309. struct drm_intel_sprite_colorkey *key)
  310. {
  311. struct drm_device *dev = plane->dev;
  312. struct drm_i915_private *dev_priv = dev->dev_private;
  313. struct intel_plane *intel_plane;
  314. u32 dvscntr;
  315. int ret = 0;
  316. intel_plane = to_intel_plane(plane);
  317. I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
  318. I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
  319. I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
  320. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  321. dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
  322. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  323. dvscntr |= DVS_DEST_KEY;
  324. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  325. dvscntr |= DVS_SOURCE_KEY;
  326. I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
  327. POSTING_READ(DVSKEYMSK(intel_plane->pipe));
  328. return ret;
  329. }
  330. static void
  331. ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  332. {
  333. struct drm_device *dev = plane->dev;
  334. struct drm_i915_private *dev_priv = dev->dev_private;
  335. struct intel_plane *intel_plane;
  336. u32 dvscntr;
  337. intel_plane = to_intel_plane(plane);
  338. key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
  339. key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
  340. key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
  341. key->flags = 0;
  342. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  343. if (dvscntr & DVS_DEST_KEY)
  344. key->flags = I915_SET_COLORKEY_DESTINATION;
  345. else if (dvscntr & DVS_SOURCE_KEY)
  346. key->flags = I915_SET_COLORKEY_SOURCE;
  347. else
  348. key->flags = I915_SET_COLORKEY_NONE;
  349. }
  350. static int
  351. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  352. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  353. unsigned int crtc_w, unsigned int crtc_h,
  354. uint32_t src_x, uint32_t src_y,
  355. uint32_t src_w, uint32_t src_h)
  356. {
  357. struct drm_device *dev = plane->dev;
  358. struct drm_i915_private *dev_priv = dev->dev_private;
  359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  360. struct intel_plane *intel_plane = to_intel_plane(plane);
  361. struct intel_framebuffer *intel_fb;
  362. struct drm_i915_gem_object *obj, *old_obj;
  363. int pipe = intel_plane->pipe;
  364. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  365. pipe);
  366. int ret = 0;
  367. int x = src_x >> 16, y = src_y >> 16;
  368. int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
  369. bool disable_primary = false;
  370. intel_fb = to_intel_framebuffer(fb);
  371. obj = intel_fb->obj;
  372. old_obj = intel_plane->obj;
  373. src_w = src_w >> 16;
  374. src_h = src_h >> 16;
  375. /* Pipe must be running... */
  376. if (!(I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE))
  377. return -EINVAL;
  378. if (crtc_x >= primary_w || crtc_y >= primary_h)
  379. return -EINVAL;
  380. /* Don't modify another pipe's plane */
  381. if (intel_plane->pipe != intel_crtc->pipe)
  382. return -EINVAL;
  383. /* Sprite planes can be linear or x-tiled surfaces */
  384. switch (obj->tiling_mode) {
  385. case I915_TILING_NONE:
  386. case I915_TILING_X:
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. /*
  392. * Clamp the width & height into the visible area. Note we don't
  393. * try to scale the source if part of the visible region is offscreen.
  394. * The caller must handle that by adjusting source offset and size.
  395. */
  396. if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
  397. crtc_w += crtc_x;
  398. crtc_x = 0;
  399. }
  400. if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
  401. goto out;
  402. if ((crtc_x + crtc_w) > primary_w)
  403. crtc_w = primary_w - crtc_x;
  404. if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
  405. crtc_h += crtc_y;
  406. crtc_y = 0;
  407. }
  408. if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
  409. goto out;
  410. if (crtc_y + crtc_h > primary_h)
  411. crtc_h = primary_h - crtc_y;
  412. if (!crtc_w || !crtc_h) /* Again, nothing to display */
  413. goto out;
  414. /*
  415. * We may not have a scaler, eg. HSW does not have it any more
  416. */
  417. if (!intel_plane->can_scale && (crtc_w != src_w || crtc_h != src_h))
  418. return -EINVAL;
  419. /*
  420. * We can take a larger source and scale it down, but
  421. * only so much... 16x is the max on SNB.
  422. */
  423. if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
  424. return -EINVAL;
  425. /*
  426. * If the sprite is completely covering the primary plane,
  427. * we can disable the primary and save power.
  428. */
  429. if ((crtc_x == 0) && (crtc_y == 0) &&
  430. (crtc_w == primary_w) && (crtc_h == primary_h))
  431. disable_primary = true;
  432. mutex_lock(&dev->struct_mutex);
  433. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  434. if (ret)
  435. goto out_unlock;
  436. intel_plane->obj = obj;
  437. /*
  438. * Be sure to re-enable the primary before the sprite is no longer
  439. * covering it fully.
  440. */
  441. if (!disable_primary)
  442. intel_enable_primary(crtc);
  443. intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
  444. crtc_w, crtc_h, x, y, src_w, src_h);
  445. if (disable_primary)
  446. intel_disable_primary(crtc);
  447. /* Unpin old obj after new one is active to avoid ugliness */
  448. if (old_obj) {
  449. /*
  450. * It's fairly common to simply update the position of
  451. * an existing object. In that case, we don't need to
  452. * wait for vblank to avoid ugliness, we only need to
  453. * do the pin & ref bookkeeping.
  454. */
  455. if (old_obj != obj) {
  456. mutex_unlock(&dev->struct_mutex);
  457. intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
  458. mutex_lock(&dev->struct_mutex);
  459. }
  460. intel_unpin_fb_obj(old_obj);
  461. }
  462. out_unlock:
  463. mutex_unlock(&dev->struct_mutex);
  464. out:
  465. return ret;
  466. }
  467. static int
  468. intel_disable_plane(struct drm_plane *plane)
  469. {
  470. struct drm_device *dev = plane->dev;
  471. struct intel_plane *intel_plane = to_intel_plane(plane);
  472. int ret = 0;
  473. if (plane->crtc)
  474. intel_enable_primary(plane->crtc);
  475. intel_plane->disable_plane(plane);
  476. if (!intel_plane->obj)
  477. goto out;
  478. mutex_lock(&dev->struct_mutex);
  479. intel_unpin_fb_obj(intel_plane->obj);
  480. intel_plane->obj = NULL;
  481. mutex_unlock(&dev->struct_mutex);
  482. out:
  483. return ret;
  484. }
  485. static void intel_destroy_plane(struct drm_plane *plane)
  486. {
  487. struct intel_plane *intel_plane = to_intel_plane(plane);
  488. intel_disable_plane(plane);
  489. drm_plane_cleanup(plane);
  490. kfree(intel_plane);
  491. }
  492. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  493. struct drm_file *file_priv)
  494. {
  495. struct drm_intel_sprite_colorkey *set = data;
  496. struct drm_mode_object *obj;
  497. struct drm_plane *plane;
  498. struct intel_plane *intel_plane;
  499. int ret = 0;
  500. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  501. return -ENODEV;
  502. /* Make sure we don't try to enable both src & dest simultaneously */
  503. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  504. return -EINVAL;
  505. drm_modeset_lock_all(dev);
  506. obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
  507. if (!obj) {
  508. ret = -EINVAL;
  509. goto out_unlock;
  510. }
  511. plane = obj_to_plane(obj);
  512. intel_plane = to_intel_plane(plane);
  513. ret = intel_plane->update_colorkey(plane, set);
  514. out_unlock:
  515. drm_modeset_unlock_all(dev);
  516. return ret;
  517. }
  518. int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
  519. struct drm_file *file_priv)
  520. {
  521. struct drm_intel_sprite_colorkey *get = data;
  522. struct drm_mode_object *obj;
  523. struct drm_plane *plane;
  524. struct intel_plane *intel_plane;
  525. int ret = 0;
  526. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  527. return -ENODEV;
  528. drm_modeset_lock_all(dev);
  529. obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
  530. if (!obj) {
  531. ret = -EINVAL;
  532. goto out_unlock;
  533. }
  534. plane = obj_to_plane(obj);
  535. intel_plane = to_intel_plane(plane);
  536. intel_plane->get_colorkey(plane, get);
  537. out_unlock:
  538. drm_modeset_unlock_all(dev);
  539. return ret;
  540. }
  541. static const struct drm_plane_funcs intel_plane_funcs = {
  542. .update_plane = intel_update_plane,
  543. .disable_plane = intel_disable_plane,
  544. .destroy = intel_destroy_plane,
  545. };
  546. static uint32_t ilk_plane_formats[] = {
  547. DRM_FORMAT_XRGB8888,
  548. DRM_FORMAT_YUYV,
  549. DRM_FORMAT_YVYU,
  550. DRM_FORMAT_UYVY,
  551. DRM_FORMAT_VYUY,
  552. };
  553. static uint32_t snb_plane_formats[] = {
  554. DRM_FORMAT_XBGR8888,
  555. DRM_FORMAT_XRGB8888,
  556. DRM_FORMAT_YUYV,
  557. DRM_FORMAT_YVYU,
  558. DRM_FORMAT_UYVY,
  559. DRM_FORMAT_VYUY,
  560. };
  561. int
  562. intel_plane_init(struct drm_device *dev, enum pipe pipe)
  563. {
  564. struct intel_plane *intel_plane;
  565. unsigned long possible_crtcs;
  566. const uint32_t *plane_formats;
  567. int num_plane_formats;
  568. int ret;
  569. if (INTEL_INFO(dev)->gen < 5)
  570. return -ENODEV;
  571. intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
  572. if (!intel_plane)
  573. return -ENOMEM;
  574. switch (INTEL_INFO(dev)->gen) {
  575. case 5:
  576. case 6:
  577. intel_plane->can_scale = true;
  578. intel_plane->max_downscale = 16;
  579. intel_plane->update_plane = ilk_update_plane;
  580. intel_plane->disable_plane = ilk_disable_plane;
  581. intel_plane->update_colorkey = ilk_update_colorkey;
  582. intel_plane->get_colorkey = ilk_get_colorkey;
  583. if (IS_GEN6(dev)) {
  584. plane_formats = snb_plane_formats;
  585. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  586. } else {
  587. plane_formats = ilk_plane_formats;
  588. num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
  589. }
  590. break;
  591. case 7:
  592. if (IS_HASWELL(dev) || IS_VALLEYVIEW(dev))
  593. intel_plane->can_scale = false;
  594. else
  595. intel_plane->can_scale = true;
  596. intel_plane->max_downscale = 2;
  597. intel_plane->update_plane = ivb_update_plane;
  598. intel_plane->disable_plane = ivb_disable_plane;
  599. intel_plane->update_colorkey = ivb_update_colorkey;
  600. intel_plane->get_colorkey = ivb_get_colorkey;
  601. plane_formats = snb_plane_formats;
  602. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  603. break;
  604. default:
  605. kfree(intel_plane);
  606. return -ENODEV;
  607. }
  608. intel_plane->pipe = pipe;
  609. possible_crtcs = (1 << pipe);
  610. ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
  611. &intel_plane_funcs,
  612. plane_formats, num_plane_formats,
  613. false);
  614. if (ret)
  615. kfree(intel_plane);
  616. return ret;
  617. }