intel_sprite.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
  290. }
  291. static void
  292. intel_disable_primary(struct drm_crtc *crtc)
  293. {
  294. struct drm_device *dev = crtc->dev;
  295. struct drm_i915_private *dev_priv = dev->dev_private;
  296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  297. int reg = DSPCNTR(intel_crtc->plane);
  298. I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
  299. }
  300. static int
  301. ilk_update_colorkey(struct drm_plane *plane,
  302. struct drm_intel_sprite_colorkey *key)
  303. {
  304. struct drm_device *dev = plane->dev;
  305. struct drm_i915_private *dev_priv = dev->dev_private;
  306. struct intel_plane *intel_plane;
  307. u32 dvscntr;
  308. int ret = 0;
  309. intel_plane = to_intel_plane(plane);
  310. I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
  311. I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
  312. I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
  313. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  314. dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
  315. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  316. dvscntr |= DVS_DEST_KEY;
  317. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  318. dvscntr |= DVS_SOURCE_KEY;
  319. I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
  320. POSTING_READ(DVSKEYMSK(intel_plane->pipe));
  321. return ret;
  322. }
  323. static void
  324. ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  325. {
  326. struct drm_device *dev = plane->dev;
  327. struct drm_i915_private *dev_priv = dev->dev_private;
  328. struct intel_plane *intel_plane;
  329. u32 dvscntr;
  330. intel_plane = to_intel_plane(plane);
  331. key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
  332. key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
  333. key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
  334. key->flags = 0;
  335. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  336. if (dvscntr & DVS_DEST_KEY)
  337. key->flags = I915_SET_COLORKEY_DESTINATION;
  338. else if (dvscntr & DVS_SOURCE_KEY)
  339. key->flags = I915_SET_COLORKEY_SOURCE;
  340. else
  341. key->flags = I915_SET_COLORKEY_NONE;
  342. }
  343. static int
  344. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  345. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  346. unsigned int crtc_w, unsigned int crtc_h,
  347. uint32_t src_x, uint32_t src_y,
  348. uint32_t src_w, uint32_t src_h)
  349. {
  350. struct drm_device *dev = plane->dev;
  351. struct drm_i915_private *dev_priv = dev->dev_private;
  352. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  353. struct intel_plane *intel_plane = to_intel_plane(plane);
  354. struct intel_framebuffer *intel_fb;
  355. struct drm_i915_gem_object *obj, *old_obj;
  356. int pipe = intel_plane->pipe;
  357. int ret = 0;
  358. int x = src_x >> 16, y = src_y >> 16;
  359. int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
  360. bool disable_primary = false;
  361. intel_fb = to_intel_framebuffer(fb);
  362. obj = intel_fb->obj;
  363. old_obj = intel_plane->obj;
  364. src_w = src_w >> 16;
  365. src_h = src_h >> 16;
  366. /* Pipe must be running... */
  367. if (!(I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE))
  368. return -EINVAL;
  369. if (crtc_x >= primary_w || crtc_y >= primary_h)
  370. return -EINVAL;
  371. /* Don't modify another pipe's plane */
  372. if (intel_plane->pipe != intel_crtc->pipe)
  373. return -EINVAL;
  374. /*
  375. * Clamp the width & height into the visible area. Note we don't
  376. * try to scale the source if part of the visible region is offscreen.
  377. * The caller must handle that by adjusting source offset and size.
  378. */
  379. if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
  380. crtc_w += crtc_x;
  381. crtc_x = 0;
  382. }
  383. if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
  384. goto out;
  385. if ((crtc_x + crtc_w) > primary_w)
  386. crtc_w = primary_w - crtc_x;
  387. if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
  388. crtc_h += crtc_y;
  389. crtc_y = 0;
  390. }
  391. if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
  392. goto out;
  393. if (crtc_y + crtc_h > primary_h)
  394. crtc_h = primary_h - crtc_y;
  395. if (!crtc_w || !crtc_h) /* Again, nothing to display */
  396. goto out;
  397. /*
  398. * We can take a larger source and scale it down, but
  399. * only so much... 16x is the max on SNB.
  400. */
  401. if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
  402. return -EINVAL;
  403. /*
  404. * If the sprite is completely covering the primary plane,
  405. * we can disable the primary and save power.
  406. */
  407. if ((crtc_x == 0) && (crtc_y == 0) &&
  408. (crtc_w == primary_w) && (crtc_h == primary_h))
  409. disable_primary = true;
  410. mutex_lock(&dev->struct_mutex);
  411. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  412. if (ret)
  413. goto out_unlock;
  414. intel_plane->obj = obj;
  415. /*
  416. * Be sure to re-enable the primary before the sprite is no longer
  417. * covering it fully.
  418. */
  419. if (!disable_primary && intel_plane->primary_disabled) {
  420. intel_enable_primary(crtc);
  421. intel_plane->primary_disabled = false;
  422. }
  423. intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
  424. crtc_w, crtc_h, x, y, src_w, src_h);
  425. if (disable_primary) {
  426. intel_disable_primary(crtc);
  427. intel_plane->primary_disabled = true;
  428. }
  429. /* Unpin old obj after new one is active to avoid ugliness */
  430. if (old_obj) {
  431. /*
  432. * It's fairly common to simply update the position of
  433. * an existing object. In that case, we don't need to
  434. * wait for vblank to avoid ugliness, we only need to
  435. * do the pin & ref bookkeeping.
  436. */
  437. if (old_obj != obj) {
  438. mutex_unlock(&dev->struct_mutex);
  439. intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
  440. mutex_lock(&dev->struct_mutex);
  441. }
  442. intel_unpin_fb_obj(old_obj);
  443. }
  444. out_unlock:
  445. mutex_unlock(&dev->struct_mutex);
  446. out:
  447. return ret;
  448. }
  449. static int
  450. intel_disable_plane(struct drm_plane *plane)
  451. {
  452. struct drm_device *dev = plane->dev;
  453. struct intel_plane *intel_plane = to_intel_plane(plane);
  454. int ret = 0;
  455. if (intel_plane->primary_disabled) {
  456. intel_enable_primary(plane->crtc);
  457. intel_plane->primary_disabled = false;
  458. }
  459. intel_plane->disable_plane(plane);
  460. if (!intel_plane->obj)
  461. goto out;
  462. mutex_lock(&dev->struct_mutex);
  463. intel_unpin_fb_obj(intel_plane->obj);
  464. intel_plane->obj = NULL;
  465. mutex_unlock(&dev->struct_mutex);
  466. out:
  467. return ret;
  468. }
  469. static void intel_destroy_plane(struct drm_plane *plane)
  470. {
  471. struct intel_plane *intel_plane = to_intel_plane(plane);
  472. intel_disable_plane(plane);
  473. drm_plane_cleanup(plane);
  474. kfree(intel_plane);
  475. }
  476. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  477. struct drm_file *file_priv)
  478. {
  479. struct drm_intel_sprite_colorkey *set = data;
  480. struct drm_mode_object *obj;
  481. struct drm_plane *plane;
  482. struct intel_plane *intel_plane;
  483. int ret = 0;
  484. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  485. return -ENODEV;
  486. /* Make sure we don't try to enable both src & dest simultaneously */
  487. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  488. return -EINVAL;
  489. mutex_lock(&dev->mode_config.mutex);
  490. obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
  491. if (!obj) {
  492. ret = -EINVAL;
  493. goto out_unlock;
  494. }
  495. plane = obj_to_plane(obj);
  496. intel_plane = to_intel_plane(plane);
  497. ret = intel_plane->update_colorkey(plane, set);
  498. out_unlock:
  499. mutex_unlock(&dev->mode_config.mutex);
  500. return ret;
  501. }
  502. int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
  503. struct drm_file *file_priv)
  504. {
  505. struct drm_intel_sprite_colorkey *get = data;
  506. struct drm_mode_object *obj;
  507. struct drm_plane *plane;
  508. struct intel_plane *intel_plane;
  509. int ret = 0;
  510. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  511. return -ENODEV;
  512. mutex_lock(&dev->mode_config.mutex);
  513. obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
  514. if (!obj) {
  515. ret = -EINVAL;
  516. goto out_unlock;
  517. }
  518. plane = obj_to_plane(obj);
  519. intel_plane = to_intel_plane(plane);
  520. intel_plane->get_colorkey(plane, get);
  521. out_unlock:
  522. mutex_unlock(&dev->mode_config.mutex);
  523. return ret;
  524. }
  525. static const struct drm_plane_funcs intel_plane_funcs = {
  526. .update_plane = intel_update_plane,
  527. .disable_plane = intel_disable_plane,
  528. .destroy = intel_destroy_plane,
  529. };
  530. static uint32_t ilk_plane_formats[] = {
  531. DRM_FORMAT_XRGB8888,
  532. DRM_FORMAT_YUYV,
  533. DRM_FORMAT_YVYU,
  534. DRM_FORMAT_UYVY,
  535. DRM_FORMAT_VYUY,
  536. };
  537. static uint32_t snb_plane_formats[] = {
  538. DRM_FORMAT_XBGR8888,
  539. DRM_FORMAT_XRGB8888,
  540. DRM_FORMAT_YUYV,
  541. DRM_FORMAT_YVYU,
  542. DRM_FORMAT_UYVY,
  543. DRM_FORMAT_VYUY,
  544. };
  545. int
  546. intel_plane_init(struct drm_device *dev, enum pipe pipe)
  547. {
  548. struct intel_plane *intel_plane;
  549. unsigned long possible_crtcs;
  550. const uint32_t *plane_formats;
  551. int num_plane_formats;
  552. int ret;
  553. if (INTEL_INFO(dev)->gen < 5)
  554. return -ENODEV;
  555. intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
  556. if (!intel_plane)
  557. return -ENOMEM;
  558. switch (INTEL_INFO(dev)->gen) {
  559. case 5:
  560. case 6:
  561. intel_plane->max_downscale = 16;
  562. intel_plane->update_plane = ilk_update_plane;
  563. intel_plane->disable_plane = ilk_disable_plane;
  564. intel_plane->update_colorkey = ilk_update_colorkey;
  565. intel_plane->get_colorkey = ilk_get_colorkey;
  566. if (IS_GEN6(dev)) {
  567. plane_formats = snb_plane_formats;
  568. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  569. } else {
  570. plane_formats = ilk_plane_formats;
  571. num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
  572. }
  573. break;
  574. case 7:
  575. intel_plane->max_downscale = 2;
  576. intel_plane->update_plane = ivb_update_plane;
  577. intel_plane->disable_plane = ivb_disable_plane;
  578. intel_plane->update_colorkey = ivb_update_colorkey;
  579. intel_plane->get_colorkey = ivb_get_colorkey;
  580. plane_formats = snb_plane_formats;
  581. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  582. break;
  583. default:
  584. return -ENODEV;
  585. }
  586. intel_plane->pipe = pipe;
  587. possible_crtcs = (1 << pipe);
  588. ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
  589. &intel_plane_funcs,
  590. plane_formats, num_plane_formats,
  591. false);
  592. if (ret)
  593. kfree(intel_plane);
  594. return ret;
  595. }