blob: e36123d2497e351290a5662f1edd5356b0ff7f23 [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);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100130
131 if (IS_HASWELL(dev)) {
132 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single
133 * SPROFFSET register */
134 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
135 } else if (obj->tiling_mode != I915_TILING_NONE) {
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800136 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
137 } else {
138 unsigned long offset;
139
140 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
141 I915_WRITE(SPRLINOFF(pipe), offset);
142 }
Damien Lespiauc54173a2012-10-26 18:20:11 +0100143
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800144 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100145 if (intel_plane->can_scale)
146 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800147 I915_WRITE(SPRCTL(pipe), sprctl);
Armin Reese446f2542012-03-30 16:20:16 -0700148 I915_MODIFY_DISPBASE(SPRSURF(pipe), obj->gtt_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800149 POSTING_READ(SPRSURF(pipe));
150}
151
152static void
153ivb_disable_plane(struct drm_plane *plane)
154{
155 struct drm_device *dev = plane->dev;
156 struct drm_i915_private *dev_priv = dev->dev_private;
157 struct intel_plane *intel_plane = to_intel_plane(plane);
158 int pipe = intel_plane->pipe;
159
160 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
161 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100162 if (intel_plane->can_scale)
163 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800164 /* Activate double buffered register update */
Armin Reese446f2542012-03-30 16:20:16 -0700165 I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800166 POSTING_READ(SPRSURF(pipe));
Chris Wilson828ed3e2012-04-18 17:12:26 +0100167
168 dev_priv->sprite_scaling_enabled = false;
169 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800170}
171
Jesse Barnes8ea30862012-01-03 08:05:39 -0800172static int
173ivb_update_colorkey(struct drm_plane *plane,
174 struct drm_intel_sprite_colorkey *key)
175{
176 struct drm_device *dev = plane->dev;
177 struct drm_i915_private *dev_priv = dev->dev_private;
178 struct intel_plane *intel_plane;
179 u32 sprctl;
180 int ret = 0;
181
182 intel_plane = to_intel_plane(plane);
183
184 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
185 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
186 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
187
188 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
189 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
190 if (key->flags & I915_SET_COLORKEY_DESTINATION)
191 sprctl |= SPRITE_DEST_KEY;
192 else if (key->flags & I915_SET_COLORKEY_SOURCE)
193 sprctl |= SPRITE_SOURCE_KEY;
194 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
195
196 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
197
198 return ret;
199}
200
201static void
202ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
203{
204 struct drm_device *dev = plane->dev;
205 struct drm_i915_private *dev_priv = dev->dev_private;
206 struct intel_plane *intel_plane;
207 u32 sprctl;
208
209 intel_plane = to_intel_plane(plane);
210
211 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
212 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
213 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
214 key->flags = 0;
215
216 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
217
218 if (sprctl & SPRITE_DEST_KEY)
219 key->flags = I915_SET_COLORKEY_DESTINATION;
220 else if (sprctl & SPRITE_SOURCE_KEY)
221 key->flags = I915_SET_COLORKEY_SOURCE;
222 else
223 key->flags = I915_SET_COLORKEY_NONE;
224}
225
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800226static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100227ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800228 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
229 unsigned int crtc_w, unsigned int crtc_h,
230 uint32_t x, uint32_t y,
231 uint32_t src_w, uint32_t src_h)
232{
233 struct drm_device *dev = plane->dev;
234 struct drm_i915_private *dev_priv = dev->dev_private;
235 struct intel_plane *intel_plane = to_intel_plane(plane);
236 int pipe = intel_plane->pipe, pixel_size;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100237 u32 dvscntr, dvsscale;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800238
239 dvscntr = I915_READ(DVSCNTR(pipe));
240
241 /* Mask out pixel format bits in case we change it */
242 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800243 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800244 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300245 dvscntr &= ~DVS_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800246
247 switch (fb->pixel_format) {
248 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800249 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800250 pixel_size = 4;
251 break;
252 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800253 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800254 pixel_size = 4;
255 break;
256 case DRM_FORMAT_YUYV:
257 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
258 pixel_size = 2;
259 break;
260 case DRM_FORMAT_YVYU:
261 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
262 pixel_size = 2;
263 break;
264 case DRM_FORMAT_UYVY:
265 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
266 pixel_size = 2;
267 break;
268 case DRM_FORMAT_VYUY:
269 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
270 pixel_size = 2;
271 break;
272 default:
273 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
274 dvscntr |= DVS_FORMAT_RGBX888;
275 pixel_size = 4;
276 break;
277 }
278
279 if (obj->tiling_mode != I915_TILING_NONE)
280 dvscntr |= DVS_TILED;
281
Chris Wilsond1686ae2012-04-10 11:41:49 +0100282 if (IS_GEN6(dev))
283 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800284 dvscntr |= DVS_ENABLE;
285
286 /* Sizes are 0 based */
287 src_w--;
288 src_h--;
289 crtc_w--;
290 crtc_h--;
291
292 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
293
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100294 dvsscale = 0;
295 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800296 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
297
298 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
299 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
300 if (obj->tiling_mode != I915_TILING_NONE) {
301 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
302 } else {
303 unsigned long offset;
304
305 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
306 I915_WRITE(DVSLINOFF(pipe), offset);
307 }
308 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
309 I915_WRITE(DVSSCALE(pipe), dvsscale);
310 I915_WRITE(DVSCNTR(pipe), dvscntr);
Armin Reese446f2542012-03-30 16:20:16 -0700311 I915_MODIFY_DISPBASE(DVSSURF(pipe), obj->gtt_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800312 POSTING_READ(DVSSURF(pipe));
313}
314
315static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100316ilk_disable_plane(struct drm_plane *plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800317{
318 struct drm_device *dev = plane->dev;
319 struct drm_i915_private *dev_priv = dev->dev_private;
320 struct intel_plane *intel_plane = to_intel_plane(plane);
321 int pipe = intel_plane->pipe;
322
323 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
324 /* Disable the scaler */
325 I915_WRITE(DVSSCALE(pipe), 0);
326 /* Flush double buffered register updates */
Armin Reese446f2542012-03-30 16:20:16 -0700327 I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800328 POSTING_READ(DVSSURF(pipe));
329}
330
Jesse Barnes175bd422011-12-13 13:19:39 -0800331static void
332intel_enable_primary(struct drm_crtc *crtc)
333{
334 struct drm_device *dev = crtc->dev;
335 struct drm_i915_private *dev_priv = dev->dev_private;
336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
337 int reg = DSPCNTR(intel_crtc->plane);
338
Chris Wilson93314b52012-06-13 17:36:55 +0100339 if (!intel_crtc->primary_disabled)
340 return;
341
342 intel_crtc->primary_disabled = false;
343 intel_update_fbc(dev);
344
Jesse Barnes175bd422011-12-13 13:19:39 -0800345 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
346}
347
348static void
349intel_disable_primary(struct drm_crtc *crtc)
350{
351 struct drm_device *dev = crtc->dev;
352 struct drm_i915_private *dev_priv = dev->dev_private;
353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
354 int reg = DSPCNTR(intel_crtc->plane);
355
Chris Wilson93314b52012-06-13 17:36:55 +0100356 if (intel_crtc->primary_disabled)
357 return;
358
Jesse Barnes175bd422011-12-13 13:19:39 -0800359 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
Chris Wilson93314b52012-06-13 17:36:55 +0100360
361 intel_crtc->primary_disabled = true;
362 intel_update_fbc(dev);
Jesse Barnes175bd422011-12-13 13:19:39 -0800363}
364
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800365static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100366ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800367 struct drm_intel_sprite_colorkey *key)
368{
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 int ret = 0;
374
375 intel_plane = to_intel_plane(plane);
376
377 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
378 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
379 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
380
381 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
382 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
383 if (key->flags & I915_SET_COLORKEY_DESTINATION)
384 dvscntr |= DVS_DEST_KEY;
385 else if (key->flags & I915_SET_COLORKEY_SOURCE)
386 dvscntr |= DVS_SOURCE_KEY;
387 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
388
389 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
390
391 return ret;
392}
393
394static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100395ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800396{
397 struct drm_device *dev = plane->dev;
398 struct drm_i915_private *dev_priv = dev->dev_private;
399 struct intel_plane *intel_plane;
400 u32 dvscntr;
401
402 intel_plane = to_intel_plane(plane);
403
404 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
405 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
406 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
407 key->flags = 0;
408
409 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
410
411 if (dvscntr & DVS_DEST_KEY)
412 key->flags = I915_SET_COLORKEY_DESTINATION;
413 else if (dvscntr & DVS_SOURCE_KEY)
414 key->flags = I915_SET_COLORKEY_SOURCE;
415 else
416 key->flags = I915_SET_COLORKEY_NONE;
417}
418
419static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800420intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
421 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
422 unsigned int crtc_w, unsigned int crtc_h,
423 uint32_t src_x, uint32_t src_y,
424 uint32_t src_w, uint32_t src_h)
425{
426 struct drm_device *dev = plane->dev;
427 struct drm_i915_private *dev_priv = dev->dev_private;
428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
429 struct intel_plane *intel_plane = to_intel_plane(plane);
430 struct intel_framebuffer *intel_fb;
431 struct drm_i915_gem_object *obj, *old_obj;
432 int pipe = intel_plane->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200433 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
434 pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800435 int ret = 0;
436 int x = src_x >> 16, y = src_y >> 16;
437 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
438 bool disable_primary = false;
439
440 intel_fb = to_intel_framebuffer(fb);
441 obj = intel_fb->obj;
442
443 old_obj = intel_plane->obj;
444
Jesse Barnesb4db1e32012-03-20 10:59:09 -0700445 src_w = src_w >> 16;
446 src_h = src_h >> 16;
447
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800448 /* Pipe must be running... */
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200449 if (!(I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE))
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800450 return -EINVAL;
451
452 if (crtc_x >= primary_w || crtc_y >= primary_h)
453 return -EINVAL;
454
455 /* Don't modify another pipe's plane */
456 if (intel_plane->pipe != intel_crtc->pipe)
457 return -EINVAL;
458
Damien Lespiau94c64192012-10-29 15:14:51 +0000459 /* Sprite planes can be linear or x-tiled surfaces */
460 switch (obj->tiling_mode) {
461 case I915_TILING_NONE:
462 case I915_TILING_X:
463 break;
464 default:
465 return -EINVAL;
466 }
467
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800468 /*
469 * Clamp the width & height into the visible area. Note we don't
470 * try to scale the source if part of the visible region is offscreen.
471 * The caller must handle that by adjusting source offset and size.
472 */
473 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
474 crtc_w += crtc_x;
475 crtc_x = 0;
476 }
477 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
478 goto out;
479 if ((crtc_x + crtc_w) > primary_w)
480 crtc_w = primary_w - crtc_x;
481
482 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
483 crtc_h += crtc_y;
484 crtc_y = 0;
485 }
486 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
487 goto out;
488 if (crtc_y + crtc_h > primary_h)
489 crtc_h = primary_h - crtc_y;
490
491 if (!crtc_w || !crtc_h) /* Again, nothing to display */
492 goto out;
493
494 /*
Damien Lespiau2d354c32012-10-22 18:19:27 +0100495 * We may not have a scaler, eg. HSW does not have it any more
496 */
497 if (!intel_plane->can_scale && (crtc_w != src_w || crtc_h != src_h))
498 return -EINVAL;
499
500 /*
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800501 * We can take a larger source and scale it down, but
502 * only so much... 16x is the max on SNB.
503 */
504 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
505 return -EINVAL;
506
507 /*
508 * If the sprite is completely covering the primary plane,
509 * we can disable the primary and save power.
510 */
511 if ((crtc_x == 0) && (crtc_y == 0) &&
512 (crtc_w == primary_w) && (crtc_h == primary_h))
513 disable_primary = true;
514
515 mutex_lock(&dev->struct_mutex);
516
517 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800518 if (ret)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800519 goto out_unlock;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800520
521 intel_plane->obj = obj;
522
Jesse Barnes175bd422011-12-13 13:19:39 -0800523 /*
524 * Be sure to re-enable the primary before the sprite is no longer
525 * covering it fully.
526 */
Chris Wilson93314b52012-06-13 17:36:55 +0100527 if (!disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800528 intel_enable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800529
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800530 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
531 crtc_w, crtc_h, x, y, src_w, src_h);
532
Chris Wilson93314b52012-06-13 17:36:55 +0100533 if (disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800534 intel_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800535
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800536 /* Unpin old obj after new one is active to avoid ugliness */
537 if (old_obj) {
538 /*
539 * It's fairly common to simply update the position of
540 * an existing object. In that case, we don't need to
541 * wait for vblank to avoid ugliness, we only need to
542 * do the pin & ref bookkeeping.
543 */
544 if (old_obj != obj) {
545 mutex_unlock(&dev->struct_mutex);
546 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
547 mutex_lock(&dev->struct_mutex);
548 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100549 intel_unpin_fb_obj(old_obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800550 }
551
552out_unlock:
553 mutex_unlock(&dev->struct_mutex);
554out:
555 return ret;
556}
557
558static int
559intel_disable_plane(struct drm_plane *plane)
560{
561 struct drm_device *dev = plane->dev;
562 struct intel_plane *intel_plane = to_intel_plane(plane);
563 int ret = 0;
564
Chris Wilson93314b52012-06-13 17:36:55 +0100565 if (plane->crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800566 intel_enable_primary(plane->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800567 intel_plane->disable_plane(plane);
568
569 if (!intel_plane->obj)
570 goto out;
571
572 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100573 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800574 intel_plane->obj = NULL;
575 mutex_unlock(&dev->struct_mutex);
576out:
577
578 return ret;
579}
580
581static void intel_destroy_plane(struct drm_plane *plane)
582{
583 struct intel_plane *intel_plane = to_intel_plane(plane);
584 intel_disable_plane(plane);
585 drm_plane_cleanup(plane);
586 kfree(intel_plane);
587}
588
Jesse Barnes8ea30862012-01-03 08:05:39 -0800589int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
590 struct drm_file *file_priv)
591{
592 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800593 struct drm_mode_object *obj;
594 struct drm_plane *plane;
595 struct intel_plane *intel_plane;
596 int ret = 0;
597
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
599 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800600
601 /* Make sure we don't try to enable both src & dest simultaneously */
602 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
603 return -EINVAL;
604
605 mutex_lock(&dev->mode_config.mutex);
606
607 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
608 if (!obj) {
609 ret = -EINVAL;
610 goto out_unlock;
611 }
612
613 plane = obj_to_plane(obj);
614 intel_plane = to_intel_plane(plane);
615 ret = intel_plane->update_colorkey(plane, set);
616
617out_unlock:
618 mutex_unlock(&dev->mode_config.mutex);
619 return ret;
620}
621
622int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
623 struct drm_file *file_priv)
624{
625 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800626 struct drm_mode_object *obj;
627 struct drm_plane *plane;
628 struct intel_plane *intel_plane;
629 int ret = 0;
630
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200631 if (!drm_core_check_feature(dev, DRIVER_MODESET))
632 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800633
634 mutex_lock(&dev->mode_config.mutex);
635
636 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
637 if (!obj) {
638 ret = -EINVAL;
639 goto out_unlock;
640 }
641
642 plane = obj_to_plane(obj);
643 intel_plane = to_intel_plane(plane);
644 intel_plane->get_colorkey(plane, get);
645
646out_unlock:
647 mutex_unlock(&dev->mode_config.mutex);
648 return ret;
649}
650
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800651static const struct drm_plane_funcs intel_plane_funcs = {
652 .update_plane = intel_update_plane,
653 .disable_plane = intel_disable_plane,
654 .destroy = intel_destroy_plane,
655};
656
Chris Wilsond1686ae2012-04-10 11:41:49 +0100657static uint32_t ilk_plane_formats[] = {
658 DRM_FORMAT_XRGB8888,
659 DRM_FORMAT_YUYV,
660 DRM_FORMAT_YVYU,
661 DRM_FORMAT_UYVY,
662 DRM_FORMAT_VYUY,
663};
664
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800665static uint32_t snb_plane_formats[] = {
666 DRM_FORMAT_XBGR8888,
667 DRM_FORMAT_XRGB8888,
668 DRM_FORMAT_YUYV,
669 DRM_FORMAT_YVYU,
670 DRM_FORMAT_UYVY,
671 DRM_FORMAT_VYUY,
672};
673
674int
675intel_plane_init(struct drm_device *dev, enum pipe pipe)
676{
677 struct intel_plane *intel_plane;
678 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100679 const uint32_t *plane_formats;
680 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800681 int ret;
682
Chris Wilsond1686ae2012-04-10 11:41:49 +0100683 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800684 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800685
686 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
687 if (!intel_plane)
688 return -ENOMEM;
689
Chris Wilsond1686ae2012-04-10 11:41:49 +0100690 switch (INTEL_INFO(dev)->gen) {
691 case 5:
692 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +0100693 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800694 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100695 intel_plane->update_plane = ilk_update_plane;
696 intel_plane->disable_plane = ilk_disable_plane;
697 intel_plane->update_colorkey = ilk_update_colorkey;
698 intel_plane->get_colorkey = ilk_get_colorkey;
699
700 if (IS_GEN6(dev)) {
701 plane_formats = snb_plane_formats;
702 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
703 } else {
704 plane_formats = ilk_plane_formats;
705 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
706 }
707 break;
708
709 case 7:
Damien Lespiau4d8d71b2012-10-25 18:06:19 +0100710 if (IS_HASWELL(dev) || IS_VALLEYVIEW(dev))
Damien Lespiau2d354c32012-10-22 18:19:27 +0100711 intel_plane->can_scale = false;
712 else
713 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800714 intel_plane->max_downscale = 2;
715 intel_plane->update_plane = ivb_update_plane;
716 intel_plane->disable_plane = ivb_disable_plane;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800717 intel_plane->update_colorkey = ivb_update_colorkey;
718 intel_plane->get_colorkey = ivb_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100719
720 plane_formats = snb_plane_formats;
721 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
722 break;
723
724 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +0200725 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +0100726 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800727 }
728
729 intel_plane->pipe = pipe;
730 possible_crtcs = (1 << pipe);
731 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +0100732 &intel_plane_funcs,
733 plane_formats, num_plane_formats,
734 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800735 if (ret)
736 kfree(intel_plane);
737
738 return ret;
739}