intel_sprite.c 19 KB

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