intel_sprite.c 21 KB

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