blob: f9001af4a30d2093d27d76e857ceec98bad1fad3 [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 */
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/drm_crtc.h>
34#include <drm/drm_fourcc.h>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080035#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/i915_drm.h>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080037#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;
Jesse Barnese86fe0d2012-06-26 13:10:11 -070059 sprctl &= ~SPRITE_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080060
61 switch (fb->pixel_format) {
62 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +053063 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080064 pixel_size = 4;
65 break;
66 case DRM_FORMAT_XRGB8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +053067 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080068 pixel_size = 4;
69 break;
70 case DRM_FORMAT_YUYV:
71 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
72 pixel_size = 2;
73 break;
74 case DRM_FORMAT_YVYU:
75 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
76 pixel_size = 2;
77 break;
78 case DRM_FORMAT_UYVY:
79 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
80 pixel_size = 2;
81 break;
82 case DRM_FORMAT_VYUY:
83 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
84 pixel_size = 2;
85 break;
86 default:
87 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
Jesse Barnesf4d71052012-06-26 13:10:12 -070088 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080089 pixel_size = 4;
90 break;
91 }
92
93 if (obj->tiling_mode != I915_TILING_NONE)
94 sprctl |= SPRITE_TILED;
95
96 /* must disable */
97 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
98 sprctl |= SPRITE_ENABLE;
99
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) {
Chris Wilson828ed3e2012-04-18 17:12:26 +0100114 if (!dev_priv->sprite_scaling_enabled) {
115 dev_priv->sprite_scaling_enabled = true;
116 intel_update_watermarks(dev);
117 intel_wait_for_vblank(dev, pipe);
118 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800119 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
120 } else {
Chris Wilson828ed3e2012-04-18 17:12:26 +0100121 if (dev_priv->sprite_scaling_enabled) {
122 dev_priv->sprite_scaling_enabled = false;
123 /* potentially re-enable LP watermarks */
124 intel_update_watermarks(dev);
125 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800126 }
127
128 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
129 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
130 if (obj->tiling_mode != I915_TILING_NONE) {
131 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
132 } else {
133 unsigned long offset;
134
135 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
136 I915_WRITE(SPRLINOFF(pipe), offset);
137 }
138 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100139 if (intel_plane->can_scale)
140 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800141 I915_WRITE(SPRCTL(pipe), sprctl);
Armin Reese446f2542012-03-30 16:20:16 -0700142 I915_MODIFY_DISPBASE(SPRSURF(pipe), obj->gtt_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800143 POSTING_READ(SPRSURF(pipe));
144}
145
146static void
147ivb_disable_plane(struct drm_plane *plane)
148{
149 struct drm_device *dev = plane->dev;
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct intel_plane *intel_plane = to_intel_plane(plane);
152 int pipe = intel_plane->pipe;
153
154 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
155 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100156 if (intel_plane->can_scale)
157 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800158 /* Activate double buffered register update */
Armin Reese446f2542012-03-30 16:20:16 -0700159 I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800160 POSTING_READ(SPRSURF(pipe));
Chris Wilson828ed3e2012-04-18 17:12:26 +0100161
162 dev_priv->sprite_scaling_enabled = false;
163 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800164}
165
Jesse Barnes8ea30862012-01-03 08:05:39 -0800166static int
167ivb_update_colorkey(struct drm_plane *plane,
168 struct drm_intel_sprite_colorkey *key)
169{
170 struct drm_device *dev = plane->dev;
171 struct drm_i915_private *dev_priv = dev->dev_private;
172 struct intel_plane *intel_plane;
173 u32 sprctl;
174 int ret = 0;
175
176 intel_plane = to_intel_plane(plane);
177
178 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
179 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
180 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
181
182 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
183 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
184 if (key->flags & I915_SET_COLORKEY_DESTINATION)
185 sprctl |= SPRITE_DEST_KEY;
186 else if (key->flags & I915_SET_COLORKEY_SOURCE)
187 sprctl |= SPRITE_SOURCE_KEY;
188 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
189
190 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
191
192 return ret;
193}
194
195static void
196ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
197{
198 struct drm_device *dev = plane->dev;
199 struct drm_i915_private *dev_priv = dev->dev_private;
200 struct intel_plane *intel_plane;
201 u32 sprctl;
202
203 intel_plane = to_intel_plane(plane);
204
205 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
206 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
207 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
208 key->flags = 0;
209
210 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
211
212 if (sprctl & SPRITE_DEST_KEY)
213 key->flags = I915_SET_COLORKEY_DESTINATION;
214 else if (sprctl & SPRITE_SOURCE_KEY)
215 key->flags = I915_SET_COLORKEY_SOURCE;
216 else
217 key->flags = I915_SET_COLORKEY_NONE;
218}
219
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800220static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100221ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800222 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
223 unsigned int crtc_w, unsigned int crtc_h,
224 uint32_t x, uint32_t y,
225 uint32_t src_w, uint32_t src_h)
226{
227 struct drm_device *dev = plane->dev;
228 struct drm_i915_private *dev_priv = dev->dev_private;
229 struct intel_plane *intel_plane = to_intel_plane(plane);
230 int pipe = intel_plane->pipe, pixel_size;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100231 u32 dvscntr, dvsscale;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800232
233 dvscntr = I915_READ(DVSCNTR(pipe));
234
235 /* Mask out pixel format bits in case we change it */
236 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800237 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800238 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300239 dvscntr &= ~DVS_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800240
241 switch (fb->pixel_format) {
242 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800243 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800244 pixel_size = 4;
245 break;
246 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800247 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800248 pixel_size = 4;
249 break;
250 case DRM_FORMAT_YUYV:
251 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
252 pixel_size = 2;
253 break;
254 case DRM_FORMAT_YVYU:
255 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
256 pixel_size = 2;
257 break;
258 case DRM_FORMAT_UYVY:
259 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
260 pixel_size = 2;
261 break;
262 case DRM_FORMAT_VYUY:
263 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
264 pixel_size = 2;
265 break;
266 default:
267 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
268 dvscntr |= DVS_FORMAT_RGBX888;
269 pixel_size = 4;
270 break;
271 }
272
273 if (obj->tiling_mode != I915_TILING_NONE)
274 dvscntr |= DVS_TILED;
275
Chris Wilsond1686ae2012-04-10 11:41:49 +0100276 if (IS_GEN6(dev))
277 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800278 dvscntr |= DVS_ENABLE;
279
280 /* Sizes are 0 based */
281 src_w--;
282 src_h--;
283 crtc_w--;
284 crtc_h--;
285
286 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
287
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100288 dvsscale = 0;
289 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800290 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
291
292 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
293 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
294 if (obj->tiling_mode != I915_TILING_NONE) {
295 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
296 } else {
297 unsigned long offset;
298
299 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
300 I915_WRITE(DVSLINOFF(pipe), offset);
301 }
302 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
303 I915_WRITE(DVSSCALE(pipe), dvsscale);
304 I915_WRITE(DVSCNTR(pipe), dvscntr);
Armin Reese446f2542012-03-30 16:20:16 -0700305 I915_MODIFY_DISPBASE(DVSSURF(pipe), obj->gtt_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800306 POSTING_READ(DVSSURF(pipe));
307}
308
309static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100310ilk_disable_plane(struct drm_plane *plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800311{
312 struct drm_device *dev = plane->dev;
313 struct drm_i915_private *dev_priv = dev->dev_private;
314 struct intel_plane *intel_plane = to_intel_plane(plane);
315 int pipe = intel_plane->pipe;
316
317 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
318 /* Disable the scaler */
319 I915_WRITE(DVSSCALE(pipe), 0);
320 /* Flush double buffered register updates */
Armin Reese446f2542012-03-30 16:20:16 -0700321 I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800322 POSTING_READ(DVSSURF(pipe));
323}
324
Jesse Barnes175bd422011-12-13 13:19:39 -0800325static void
326intel_enable_primary(struct drm_crtc *crtc)
327{
328 struct drm_device *dev = crtc->dev;
329 struct drm_i915_private *dev_priv = dev->dev_private;
330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
331 int reg = DSPCNTR(intel_crtc->plane);
332
Chris Wilson93314b52012-06-13 17:36:55 +0100333 if (!intel_crtc->primary_disabled)
334 return;
335
336 intel_crtc->primary_disabled = false;
337 intel_update_fbc(dev);
338
Jesse Barnes175bd422011-12-13 13:19:39 -0800339 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
340}
341
342static void
343intel_disable_primary(struct drm_crtc *crtc)
344{
345 struct drm_device *dev = crtc->dev;
346 struct drm_i915_private *dev_priv = dev->dev_private;
347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
348 int reg = DSPCNTR(intel_crtc->plane);
349
Chris Wilson93314b52012-06-13 17:36:55 +0100350 if (intel_crtc->primary_disabled)
351 return;
352
Jesse Barnes175bd422011-12-13 13:19:39 -0800353 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
Chris Wilson93314b52012-06-13 17:36:55 +0100354
355 intel_crtc->primary_disabled = true;
356 intel_update_fbc(dev);
Jesse Barnes175bd422011-12-13 13:19:39 -0800357}
358
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800359static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100360ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800361 struct drm_intel_sprite_colorkey *key)
362{
363 struct drm_device *dev = plane->dev;
364 struct drm_i915_private *dev_priv = dev->dev_private;
365 struct intel_plane *intel_plane;
366 u32 dvscntr;
367 int ret = 0;
368
369 intel_plane = to_intel_plane(plane);
370
371 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
372 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
373 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
374
375 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
376 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
377 if (key->flags & I915_SET_COLORKEY_DESTINATION)
378 dvscntr |= DVS_DEST_KEY;
379 else if (key->flags & I915_SET_COLORKEY_SOURCE)
380 dvscntr |= DVS_SOURCE_KEY;
381 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
382
383 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
384
385 return ret;
386}
387
388static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100389ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800390{
391 struct drm_device *dev = plane->dev;
392 struct drm_i915_private *dev_priv = dev->dev_private;
393 struct intel_plane *intel_plane;
394 u32 dvscntr;
395
396 intel_plane = to_intel_plane(plane);
397
398 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
399 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
400 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
401 key->flags = 0;
402
403 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
404
405 if (dvscntr & DVS_DEST_KEY)
406 key->flags = I915_SET_COLORKEY_DESTINATION;
407 else if (dvscntr & DVS_SOURCE_KEY)
408 key->flags = I915_SET_COLORKEY_SOURCE;
409 else
410 key->flags = I915_SET_COLORKEY_NONE;
411}
412
413static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800414intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
415 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
416 unsigned int crtc_w, unsigned int crtc_h,
417 uint32_t src_x, uint32_t src_y,
418 uint32_t src_w, uint32_t src_h)
419{
420 struct drm_device *dev = plane->dev;
421 struct drm_i915_private *dev_priv = dev->dev_private;
422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
423 struct intel_plane *intel_plane = to_intel_plane(plane);
424 struct intel_framebuffer *intel_fb;
425 struct drm_i915_gem_object *obj, *old_obj;
426 int pipe = intel_plane->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200427 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
428 pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800429 int ret = 0;
430 int x = src_x >> 16, y = src_y >> 16;
431 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
432 bool disable_primary = false;
433
434 intel_fb = to_intel_framebuffer(fb);
435 obj = intel_fb->obj;
436
437 old_obj = intel_plane->obj;
438
Jesse Barnesb4db1e32012-03-20 10:59:09 -0700439 src_w = src_w >> 16;
440 src_h = src_h >> 16;
441
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800442 /* Pipe must be running... */
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200443 if (!(I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE))
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800444 return -EINVAL;
445
446 if (crtc_x >= primary_w || crtc_y >= primary_h)
447 return -EINVAL;
448
449 /* Don't modify another pipe's plane */
450 if (intel_plane->pipe != intel_crtc->pipe)
451 return -EINVAL;
452
Damien Lespiau94c64192012-10-29 15:14:51 +0000453 /* Sprite planes can be linear or x-tiled surfaces */
454 switch (obj->tiling_mode) {
455 case I915_TILING_NONE:
456 case I915_TILING_X:
457 break;
458 default:
459 return -EINVAL;
460 }
461
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800462 /*
463 * Clamp the width & height into the visible area. Note we don't
464 * try to scale the source if part of the visible region is offscreen.
465 * The caller must handle that by adjusting source offset and size.
466 */
467 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
468 crtc_w += crtc_x;
469 crtc_x = 0;
470 }
471 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
472 goto out;
473 if ((crtc_x + crtc_w) > primary_w)
474 crtc_w = primary_w - crtc_x;
475
476 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
477 crtc_h += crtc_y;
478 crtc_y = 0;
479 }
480 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
481 goto out;
482 if (crtc_y + crtc_h > primary_h)
483 crtc_h = primary_h - crtc_y;
484
485 if (!crtc_w || !crtc_h) /* Again, nothing to display */
486 goto out;
487
488 /*
Damien Lespiau2d354c32012-10-22 18:19:27 +0100489 * We may not have a scaler, eg. HSW does not have it any more
490 */
491 if (!intel_plane->can_scale && (crtc_w != src_w || crtc_h != src_h))
492 return -EINVAL;
493
494 /*
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800495 * We can take a larger source and scale it down, but
496 * only so much... 16x is the max on SNB.
497 */
498 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
499 return -EINVAL;
500
501 /*
502 * If the sprite is completely covering the primary plane,
503 * we can disable the primary and save power.
504 */
505 if ((crtc_x == 0) && (crtc_y == 0) &&
506 (crtc_w == primary_w) && (crtc_h == primary_h))
507 disable_primary = true;
508
509 mutex_lock(&dev->struct_mutex);
510
511 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800512 if (ret)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800513 goto out_unlock;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800514
515 intel_plane->obj = obj;
516
Jesse Barnes175bd422011-12-13 13:19:39 -0800517 /*
518 * Be sure to re-enable the primary before the sprite is no longer
519 * covering it fully.
520 */
Chris Wilson93314b52012-06-13 17:36:55 +0100521 if (!disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800522 intel_enable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800523
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800524 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
525 crtc_w, crtc_h, x, y, src_w, src_h);
526
Chris Wilson93314b52012-06-13 17:36:55 +0100527 if (disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800528 intel_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800529
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800530 /* Unpin old obj after new one is active to avoid ugliness */
531 if (old_obj) {
532 /*
533 * It's fairly common to simply update the position of
534 * an existing object. In that case, we don't need to
535 * wait for vblank to avoid ugliness, we only need to
536 * do the pin & ref bookkeeping.
537 */
538 if (old_obj != obj) {
539 mutex_unlock(&dev->struct_mutex);
540 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
541 mutex_lock(&dev->struct_mutex);
542 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100543 intel_unpin_fb_obj(old_obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800544 }
545
546out_unlock:
547 mutex_unlock(&dev->struct_mutex);
548out:
549 return ret;
550}
551
552static int
553intel_disable_plane(struct drm_plane *plane)
554{
555 struct drm_device *dev = plane->dev;
556 struct intel_plane *intel_plane = to_intel_plane(plane);
557 int ret = 0;
558
Chris Wilson93314b52012-06-13 17:36:55 +0100559 if (plane->crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800560 intel_enable_primary(plane->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800561 intel_plane->disable_plane(plane);
562
563 if (!intel_plane->obj)
564 goto out;
565
566 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100567 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800568 intel_plane->obj = NULL;
569 mutex_unlock(&dev->struct_mutex);
570out:
571
572 return ret;
573}
574
575static void intel_destroy_plane(struct drm_plane *plane)
576{
577 struct intel_plane *intel_plane = to_intel_plane(plane);
578 intel_disable_plane(plane);
579 drm_plane_cleanup(plane);
580 kfree(intel_plane);
581}
582
Jesse Barnes8ea30862012-01-03 08:05:39 -0800583int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
584 struct drm_file *file_priv)
585{
586 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800587 struct drm_mode_object *obj;
588 struct drm_plane *plane;
589 struct intel_plane *intel_plane;
590 int ret = 0;
591
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200592 if (!drm_core_check_feature(dev, DRIVER_MODESET))
593 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800594
595 /* Make sure we don't try to enable both src & dest simultaneously */
596 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
597 return -EINVAL;
598
599 mutex_lock(&dev->mode_config.mutex);
600
601 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
602 if (!obj) {
603 ret = -EINVAL;
604 goto out_unlock;
605 }
606
607 plane = obj_to_plane(obj);
608 intel_plane = to_intel_plane(plane);
609 ret = intel_plane->update_colorkey(plane, set);
610
611out_unlock:
612 mutex_unlock(&dev->mode_config.mutex);
613 return ret;
614}
615
616int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
617 struct drm_file *file_priv)
618{
619 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800620 struct drm_mode_object *obj;
621 struct drm_plane *plane;
622 struct intel_plane *intel_plane;
623 int ret = 0;
624
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200625 if (!drm_core_check_feature(dev, DRIVER_MODESET))
626 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800627
628 mutex_lock(&dev->mode_config.mutex);
629
630 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
631 if (!obj) {
632 ret = -EINVAL;
633 goto out_unlock;
634 }
635
636 plane = obj_to_plane(obj);
637 intel_plane = to_intel_plane(plane);
638 intel_plane->get_colorkey(plane, get);
639
640out_unlock:
641 mutex_unlock(&dev->mode_config.mutex);
642 return ret;
643}
644
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800645static const struct drm_plane_funcs intel_plane_funcs = {
646 .update_plane = intel_update_plane,
647 .disable_plane = intel_disable_plane,
648 .destroy = intel_destroy_plane,
649};
650
Chris Wilsond1686ae2012-04-10 11:41:49 +0100651static uint32_t ilk_plane_formats[] = {
652 DRM_FORMAT_XRGB8888,
653 DRM_FORMAT_YUYV,
654 DRM_FORMAT_YVYU,
655 DRM_FORMAT_UYVY,
656 DRM_FORMAT_VYUY,
657};
658
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800659static uint32_t snb_plane_formats[] = {
660 DRM_FORMAT_XBGR8888,
661 DRM_FORMAT_XRGB8888,
662 DRM_FORMAT_YUYV,
663 DRM_FORMAT_YVYU,
664 DRM_FORMAT_UYVY,
665 DRM_FORMAT_VYUY,
666};
667
668int
669intel_plane_init(struct drm_device *dev, enum pipe pipe)
670{
671 struct intel_plane *intel_plane;
672 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100673 const uint32_t *plane_formats;
674 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800675 int ret;
676
Chris Wilsond1686ae2012-04-10 11:41:49 +0100677 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800678 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800679
680 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
681 if (!intel_plane)
682 return -ENOMEM;
683
Chris Wilsond1686ae2012-04-10 11:41:49 +0100684 switch (INTEL_INFO(dev)->gen) {
685 case 5:
686 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +0100687 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800688 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100689 intel_plane->update_plane = ilk_update_plane;
690 intel_plane->disable_plane = ilk_disable_plane;
691 intel_plane->update_colorkey = ilk_update_colorkey;
692 intel_plane->get_colorkey = ilk_get_colorkey;
693
694 if (IS_GEN6(dev)) {
695 plane_formats = snb_plane_formats;
696 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
697 } else {
698 plane_formats = ilk_plane_formats;
699 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
700 }
701 break;
702
703 case 7:
Damien Lespiau4d8d71b2012-10-25 18:06:19 +0100704 if (IS_HASWELL(dev) || IS_VALLEYVIEW(dev))
Damien Lespiau2d354c32012-10-22 18:19:27 +0100705 intel_plane->can_scale = false;
706 else
707 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800708 intel_plane->max_downscale = 2;
709 intel_plane->update_plane = ivb_update_plane;
710 intel_plane->disable_plane = ivb_disable_plane;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800711 intel_plane->update_colorkey = ivb_update_colorkey;
712 intel_plane->get_colorkey = ivb_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100713
714 plane_formats = snb_plane_formats;
715 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
716 break;
717
718 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +0200719 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +0100720 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800721 }
722
723 intel_plane->pipe = pipe;
724 possible_crtcs = (1 << pipe);
725 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +0100726 &intel_plane_funcs,
727 plane_formats, num_plane_formats,
728 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800729 if (ret)
730 kfree(intel_plane);
731
732 return ret;
733}