blob: 749d043563068ebd6ebf780a78c986acdb16490d [file] [log] [blame]
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001/*
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
39static void
40ivb_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
41 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
42 unsigned int crtc_w, unsigned int crtc_h,
43 uint32_t x, uint32_t y,
44 uint32_t src_w, uint32_t src_h)
45{
46 struct drm_device *dev = plane->dev;
47 struct drm_i915_private *dev_priv = dev->dev_private;
48 struct intel_plane *intel_plane = to_intel_plane(plane);
49 int pipe = intel_plane->pipe;
50 u32 sprctl, sprscale = 0;
51 int pixel_size;
52
53 sprctl = I915_READ(SPRCTL(pipe));
54
55 /* Mask out pixel format bits in case we change it */
56 sprctl &= ~SPRITE_PIXFORMAT_MASK;
57 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
58 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
59
60 switch (fb->pixel_format) {
61 case DRM_FORMAT_XBGR8888:
62 sprctl |= SPRITE_FORMAT_RGBX888;
63 pixel_size = 4;
64 break;
65 case DRM_FORMAT_XRGB8888:
66 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
67 pixel_size = 4;
68 break;
69 case DRM_FORMAT_YUYV:
70 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
71 pixel_size = 2;
72 break;
73 case DRM_FORMAT_YVYU:
74 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
75 pixel_size = 2;
76 break;
77 case DRM_FORMAT_UYVY:
78 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
79 pixel_size = 2;
80 break;
81 case DRM_FORMAT_VYUY:
82 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
83 pixel_size = 2;
84 break;
85 default:
86 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
87 sprctl |= DVS_FORMAT_RGBX888;
88 pixel_size = 4;
89 break;
90 }
91
92 if (obj->tiling_mode != I915_TILING_NONE)
93 sprctl |= SPRITE_TILED;
94
95 /* must disable */
96 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
97 sprctl |= SPRITE_ENABLE;
Jesse Barnes8ea30862012-01-03 08:05:39 -080098 sprctl |= SPRITE_DEST_KEY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080099
100 /* Sizes are 0 based */
101 src_w--;
102 src_h--;
103 crtc_w--;
104 crtc_h--;
105
106 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
107
108 /*
109 * IVB workaround: must disable low power watermarks for at least
110 * one frame before enabling scaling. LP watermarks can be re-enabled
111 * when scaling is disabled.
112 */
113 if (crtc_w != src_w || crtc_h != src_h) {
114 dev_priv->sprite_scaling_enabled = true;
Chris Wilsonf681fa22012-04-14 21:56:08 +0100115 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800116 intel_wait_for_vblank(dev, pipe);
117 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
118 } else {
119 dev_priv->sprite_scaling_enabled = false;
120 /* potentially re-enable LP watermarks */
Chris Wilsonf681fa22012-04-14 21:56:08 +0100121 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800122 }
123
124 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
125 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
126 if (obj->tiling_mode != I915_TILING_NONE) {
127 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
128 } else {
129 unsigned long offset;
130
131 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
132 I915_WRITE(SPRLINOFF(pipe), offset);
133 }
134 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
135 I915_WRITE(SPRSCALE(pipe), sprscale);
136 I915_WRITE(SPRCTL(pipe), sprctl);
137 I915_WRITE(SPRSURF(pipe), obj->gtt_offset);
138 POSTING_READ(SPRSURF(pipe));
139}
140
141static void
142ivb_disable_plane(struct drm_plane *plane)
143{
144 struct drm_device *dev = plane->dev;
145 struct drm_i915_private *dev_priv = dev->dev_private;
146 struct intel_plane *intel_plane = to_intel_plane(plane);
147 int pipe = intel_plane->pipe;
148
149 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
150 /* Can't leave the scaler enabled... */
151 I915_WRITE(SPRSCALE(pipe), 0);
152 /* Activate double buffered register update */
153 I915_WRITE(SPRSURF(pipe), 0);
154 POSTING_READ(SPRSURF(pipe));
155}
156
Jesse Barnes8ea30862012-01-03 08:05:39 -0800157static int
158ivb_update_colorkey(struct drm_plane *plane,
159 struct drm_intel_sprite_colorkey *key)
160{
161 struct drm_device *dev = plane->dev;
162 struct drm_i915_private *dev_priv = dev->dev_private;
163 struct intel_plane *intel_plane;
164 u32 sprctl;
165 int ret = 0;
166
167 intel_plane = to_intel_plane(plane);
168
169 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
170 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
171 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
172
173 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
174 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
175 if (key->flags & I915_SET_COLORKEY_DESTINATION)
176 sprctl |= SPRITE_DEST_KEY;
177 else if (key->flags & I915_SET_COLORKEY_SOURCE)
178 sprctl |= SPRITE_SOURCE_KEY;
179 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
180
181 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
182
183 return ret;
184}
185
186static void
187ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
188{
189 struct drm_device *dev = plane->dev;
190 struct drm_i915_private *dev_priv = dev->dev_private;
191 struct intel_plane *intel_plane;
192 u32 sprctl;
193
194 intel_plane = to_intel_plane(plane);
195
196 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
197 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
198 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
199 key->flags = 0;
200
201 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
202
203 if (sprctl & SPRITE_DEST_KEY)
204 key->flags = I915_SET_COLORKEY_DESTINATION;
205 else if (sprctl & SPRITE_SOURCE_KEY)
206 key->flags = I915_SET_COLORKEY_SOURCE;
207 else
208 key->flags = I915_SET_COLORKEY_NONE;
209}
210
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800211static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100212ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800213 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
214 unsigned int crtc_w, unsigned int crtc_h,
215 uint32_t x, uint32_t y,
216 uint32_t src_w, uint32_t src_h)
217{
218 struct drm_device *dev = plane->dev;
219 struct drm_i915_private *dev_priv = dev->dev_private;
220 struct intel_plane *intel_plane = to_intel_plane(plane);
221 int pipe = intel_plane->pipe, pixel_size;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100222 u32 dvscntr, dvsscale;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800223
224 dvscntr = I915_READ(DVSCNTR(pipe));
225
226 /* Mask out pixel format bits in case we change it */
227 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800228 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800229 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
230
231 switch (fb->pixel_format) {
232 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800233 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800234 pixel_size = 4;
235 break;
236 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800237 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800238 pixel_size = 4;
239 break;
240 case DRM_FORMAT_YUYV:
241 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
242 pixel_size = 2;
243 break;
244 case DRM_FORMAT_YVYU:
245 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
246 pixel_size = 2;
247 break;
248 case DRM_FORMAT_UYVY:
249 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
250 pixel_size = 2;
251 break;
252 case DRM_FORMAT_VYUY:
253 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
254 pixel_size = 2;
255 break;
256 default:
257 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
258 dvscntr |= DVS_FORMAT_RGBX888;
259 pixel_size = 4;
260 break;
261 }
262
263 if (obj->tiling_mode != I915_TILING_NONE)
264 dvscntr |= DVS_TILED;
265
Chris Wilsond1686ae2012-04-10 11:41:49 +0100266 if (IS_GEN6(dev))
267 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800268 dvscntr |= DVS_ENABLE;
269
270 /* Sizes are 0 based */
271 src_w--;
272 src_h--;
273 crtc_w--;
274 crtc_h--;
275
276 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
277
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100278 dvsscale = 0;
279 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800280 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
281
282 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
283 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
284 if (obj->tiling_mode != I915_TILING_NONE) {
285 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
286 } else {
287 unsigned long offset;
288
289 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
290 I915_WRITE(DVSLINOFF(pipe), offset);
291 }
292 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
293 I915_WRITE(DVSSCALE(pipe), dvsscale);
294 I915_WRITE(DVSCNTR(pipe), dvscntr);
295 I915_WRITE(DVSSURF(pipe), obj->gtt_offset);
296 POSTING_READ(DVSSURF(pipe));
297}
298
299static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100300ilk_disable_plane(struct drm_plane *plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800301{
302 struct drm_device *dev = plane->dev;
303 struct drm_i915_private *dev_priv = dev->dev_private;
304 struct intel_plane *intel_plane = to_intel_plane(plane);
305 int pipe = intel_plane->pipe;
306
307 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
308 /* Disable the scaler */
309 I915_WRITE(DVSSCALE(pipe), 0);
310 /* Flush double buffered register updates */
311 I915_WRITE(DVSSURF(pipe), 0);
312 POSTING_READ(DVSSURF(pipe));
313}
314
Jesse Barnes175bd422011-12-13 13:19:39 -0800315static void
316intel_enable_primary(struct drm_crtc *crtc)
317{
318 struct drm_device *dev = crtc->dev;
319 struct drm_i915_private *dev_priv = dev->dev_private;
320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
321 int reg = DSPCNTR(intel_crtc->plane);
322
323 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
324}
325
326static void
327intel_disable_primary(struct drm_crtc *crtc)
328{
329 struct drm_device *dev = crtc->dev;
330 struct drm_i915_private *dev_priv = dev->dev_private;
331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
332 int reg = DSPCNTR(intel_crtc->plane);
333
334 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
335}
336
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800337static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100338ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800339 struct drm_intel_sprite_colorkey *key)
340{
341 struct drm_device *dev = plane->dev;
342 struct drm_i915_private *dev_priv = dev->dev_private;
343 struct intel_plane *intel_plane;
344 u32 dvscntr;
345 int ret = 0;
346
347 intel_plane = to_intel_plane(plane);
348
349 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
350 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
351 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
352
353 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
354 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
355 if (key->flags & I915_SET_COLORKEY_DESTINATION)
356 dvscntr |= DVS_DEST_KEY;
357 else if (key->flags & I915_SET_COLORKEY_SOURCE)
358 dvscntr |= DVS_SOURCE_KEY;
359 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
360
361 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
362
363 return ret;
364}
365
366static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100367ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800368{
369 struct drm_device *dev = plane->dev;
370 struct drm_i915_private *dev_priv = dev->dev_private;
371 struct intel_plane *intel_plane;
372 u32 dvscntr;
373
374 intel_plane = to_intel_plane(plane);
375
376 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
377 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
378 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
379 key->flags = 0;
380
381 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
382
383 if (dvscntr & DVS_DEST_KEY)
384 key->flags = I915_SET_COLORKEY_DESTINATION;
385 else if (dvscntr & DVS_SOURCE_KEY)
386 key->flags = I915_SET_COLORKEY_SOURCE;
387 else
388 key->flags = I915_SET_COLORKEY_NONE;
389}
390
391static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800392intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
393 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
394 unsigned int crtc_w, unsigned int crtc_h,
395 uint32_t src_x, uint32_t src_y,
396 uint32_t src_w, uint32_t src_h)
397{
398 struct drm_device *dev = plane->dev;
399 struct drm_i915_private *dev_priv = dev->dev_private;
400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
401 struct intel_plane *intel_plane = to_intel_plane(plane);
402 struct intel_framebuffer *intel_fb;
403 struct drm_i915_gem_object *obj, *old_obj;
404 int pipe = intel_plane->pipe;
405 int ret = 0;
406 int x = src_x >> 16, y = src_y >> 16;
407 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
408 bool disable_primary = false;
409
410 intel_fb = to_intel_framebuffer(fb);
411 obj = intel_fb->obj;
412
413 old_obj = intel_plane->obj;
414
Jesse Barnesb4db1e32012-03-20 10:59:09 -0700415 src_w = src_w >> 16;
416 src_h = src_h >> 16;
417
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800418 /* Pipe must be running... */
419 if (!(I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE))
420 return -EINVAL;
421
422 if (crtc_x >= primary_w || crtc_y >= primary_h)
423 return -EINVAL;
424
425 /* Don't modify another pipe's plane */
426 if (intel_plane->pipe != intel_crtc->pipe)
427 return -EINVAL;
428
429 /*
430 * Clamp the width & height into the visible area. Note we don't
431 * try to scale the source if part of the visible region is offscreen.
432 * The caller must handle that by adjusting source offset and size.
433 */
434 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
435 crtc_w += crtc_x;
436 crtc_x = 0;
437 }
438 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
439 goto out;
440 if ((crtc_x + crtc_w) > primary_w)
441 crtc_w = primary_w - crtc_x;
442
443 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
444 crtc_h += crtc_y;
445 crtc_y = 0;
446 }
447 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
448 goto out;
449 if (crtc_y + crtc_h > primary_h)
450 crtc_h = primary_h - crtc_y;
451
452 if (!crtc_w || !crtc_h) /* Again, nothing to display */
453 goto out;
454
455 /*
456 * We can take a larger source and scale it down, but
457 * only so much... 16x is the max on SNB.
458 */
459 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
460 return -EINVAL;
461
462 /*
463 * If the sprite is completely covering the primary plane,
464 * we can disable the primary and save power.
465 */
466 if ((crtc_x == 0) && (crtc_y == 0) &&
467 (crtc_w == primary_w) && (crtc_h == primary_h))
468 disable_primary = true;
469
470 mutex_lock(&dev->struct_mutex);
471
472 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800473 if (ret)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800474 goto out_unlock;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800475
476 intel_plane->obj = obj;
477
Jesse Barnes175bd422011-12-13 13:19:39 -0800478 /*
479 * Be sure to re-enable the primary before the sprite is no longer
480 * covering it fully.
481 */
482 if (!disable_primary && intel_plane->primary_disabled) {
483 intel_enable_primary(crtc);
484 intel_plane->primary_disabled = false;
485 }
486
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800487 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
488 crtc_w, crtc_h, x, y, src_w, src_h);
489
Jesse Barnes175bd422011-12-13 13:19:39 -0800490 if (disable_primary) {
491 intel_disable_primary(crtc);
492 intel_plane->primary_disabled = true;
493 }
494
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800495 /* Unpin old obj after new one is active to avoid ugliness */
496 if (old_obj) {
497 /*
498 * It's fairly common to simply update the position of
499 * an existing object. In that case, we don't need to
500 * wait for vblank to avoid ugliness, we only need to
501 * do the pin & ref bookkeeping.
502 */
503 if (old_obj != obj) {
504 mutex_unlock(&dev->struct_mutex);
505 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
506 mutex_lock(&dev->struct_mutex);
507 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100508 intel_unpin_fb_obj(old_obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800509 }
510
511out_unlock:
512 mutex_unlock(&dev->struct_mutex);
513out:
514 return ret;
515}
516
517static int
518intel_disable_plane(struct drm_plane *plane)
519{
520 struct drm_device *dev = plane->dev;
521 struct intel_plane *intel_plane = to_intel_plane(plane);
522 int ret = 0;
523
Jesse Barnes175bd422011-12-13 13:19:39 -0800524 if (intel_plane->primary_disabled) {
525 intel_enable_primary(plane->crtc);
526 intel_plane->primary_disabled = false;
527 }
528
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800529 intel_plane->disable_plane(plane);
530
531 if (!intel_plane->obj)
532 goto out;
533
534 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100535 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800536 intel_plane->obj = NULL;
537 mutex_unlock(&dev->struct_mutex);
538out:
539
540 return ret;
541}
542
543static void intel_destroy_plane(struct drm_plane *plane)
544{
545 struct intel_plane *intel_plane = to_intel_plane(plane);
546 intel_disable_plane(plane);
547 drm_plane_cleanup(plane);
548 kfree(intel_plane);
549}
550
Jesse Barnes8ea30862012-01-03 08:05:39 -0800551int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
552 struct drm_file *file_priv)
553{
554 struct drm_intel_sprite_colorkey *set = data;
555 struct drm_i915_private *dev_priv = dev->dev_private;
556 struct drm_mode_object *obj;
557 struct drm_plane *plane;
558 struct intel_plane *intel_plane;
559 int ret = 0;
560
561 if (!dev_priv)
562 return -EINVAL;
563
564 /* Make sure we don't try to enable both src & dest simultaneously */
565 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
566 return -EINVAL;
567
568 mutex_lock(&dev->mode_config.mutex);
569
570 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
571 if (!obj) {
572 ret = -EINVAL;
573 goto out_unlock;
574 }
575
576 plane = obj_to_plane(obj);
577 intel_plane = to_intel_plane(plane);
578 ret = intel_plane->update_colorkey(plane, set);
579
580out_unlock:
581 mutex_unlock(&dev->mode_config.mutex);
582 return ret;
583}
584
585int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
586 struct drm_file *file_priv)
587{
588 struct drm_intel_sprite_colorkey *get = data;
589 struct drm_i915_private *dev_priv = dev->dev_private;
590 struct drm_mode_object *obj;
591 struct drm_plane *plane;
592 struct intel_plane *intel_plane;
593 int ret = 0;
594
595 if (!dev_priv)
596 return -EINVAL;
597
598 mutex_lock(&dev->mode_config.mutex);
599
600 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
601 if (!obj) {
602 ret = -EINVAL;
603 goto out_unlock;
604 }
605
606 plane = obj_to_plane(obj);
607 intel_plane = to_intel_plane(plane);
608 intel_plane->get_colorkey(plane, get);
609
610out_unlock:
611 mutex_unlock(&dev->mode_config.mutex);
612 return ret;
613}
614
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800615static const struct drm_plane_funcs intel_plane_funcs = {
616 .update_plane = intel_update_plane,
617 .disable_plane = intel_disable_plane,
618 .destroy = intel_destroy_plane,
619};
620
Chris Wilsond1686ae2012-04-10 11:41:49 +0100621static uint32_t ilk_plane_formats[] = {
622 DRM_FORMAT_XRGB8888,
623 DRM_FORMAT_YUYV,
624 DRM_FORMAT_YVYU,
625 DRM_FORMAT_UYVY,
626 DRM_FORMAT_VYUY,
627};
628
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800629static uint32_t snb_plane_formats[] = {
630 DRM_FORMAT_XBGR8888,
631 DRM_FORMAT_XRGB8888,
632 DRM_FORMAT_YUYV,
633 DRM_FORMAT_YVYU,
634 DRM_FORMAT_UYVY,
635 DRM_FORMAT_VYUY,
636};
637
638int
639intel_plane_init(struct drm_device *dev, enum pipe pipe)
640{
641 struct intel_plane *intel_plane;
642 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100643 const uint32_t *plane_formats;
644 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800645 int ret;
646
Chris Wilsond1686ae2012-04-10 11:41:49 +0100647 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800648 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800649
650 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
651 if (!intel_plane)
652 return -ENOMEM;
653
Chris Wilsond1686ae2012-04-10 11:41:49 +0100654 switch (INTEL_INFO(dev)->gen) {
655 case 5:
656 case 6:
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800657 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100658 intel_plane->update_plane = ilk_update_plane;
659 intel_plane->disable_plane = ilk_disable_plane;
660 intel_plane->update_colorkey = ilk_update_colorkey;
661 intel_plane->get_colorkey = ilk_get_colorkey;
662
663 if (IS_GEN6(dev)) {
664 plane_formats = snb_plane_formats;
665 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
666 } else {
667 plane_formats = ilk_plane_formats;
668 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
669 }
670 break;
671
672 case 7:
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800673 intel_plane->max_downscale = 2;
674 intel_plane->update_plane = ivb_update_plane;
675 intel_plane->disable_plane = ivb_disable_plane;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800676 intel_plane->update_colorkey = ivb_update_colorkey;
677 intel_plane->get_colorkey = ivb_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100678
679 plane_formats = snb_plane_formats;
680 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
681 break;
682
683 default:
684 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800685 }
686
687 intel_plane->pipe = pipe;
688 possible_crtcs = (1 << pipe);
689 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +0100690 &intel_plane_funcs,
691 plane_formats, num_plane_formats,
692 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800693 if (ret)
694 kfree(intel_plane);
695
696 return ret;
697}
698