blob: a337fe930b8b362f818e781c67e4f520a4530c49 [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;
Damien Lespiau5a35e992012-10-26 18:20:12 +010051 unsigned long sprsurf_offset, linear_offset;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +020052 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -080053
54 sprctl = I915_READ(SPRCTL(pipe));
55
56 /* Mask out pixel format bits in case we change it */
57 sprctl &= ~SPRITE_PIXFORMAT_MASK;
58 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
59 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
Jesse Barnese86fe0d2012-06-26 13:10:11 -070060 sprctl &= ~SPRITE_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080061
62 switch (fb->pixel_format) {
63 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +053064 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080065 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 break;
69 case DRM_FORMAT_YUYV:
70 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080071 break;
72 case DRM_FORMAT_YVYU:
73 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080074 break;
75 case DRM_FORMAT_UYVY:
76 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080077 break;
78 case DRM_FORMAT_VYUY:
79 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080080 break;
81 default:
82 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
Jesse Barnesf4d71052012-06-26 13:10:12 -070083 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080084 break;
85 }
86
87 if (obj->tiling_mode != I915_TILING_NONE)
88 sprctl |= SPRITE_TILED;
89
90 /* must disable */
91 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
92 sprctl |= SPRITE_ENABLE;
93
94 /* Sizes are 0 based */
95 src_w--;
96 src_h--;
97 crtc_w--;
98 crtc_h--;
99
100 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
101
102 /*
103 * IVB workaround: must disable low power watermarks for at least
104 * one frame before enabling scaling. LP watermarks can be re-enabled
105 * when scaling is disabled.
106 */
107 if (crtc_w != src_w || crtc_h != src_h) {
Chris Wilson828ed3e2012-04-18 17:12:26 +0100108 if (!dev_priv->sprite_scaling_enabled) {
109 dev_priv->sprite_scaling_enabled = true;
110 intel_update_watermarks(dev);
111 intel_wait_for_vblank(dev, pipe);
112 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800113 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
114 } else {
Chris Wilson828ed3e2012-04-18 17:12:26 +0100115 if (dev_priv->sprite_scaling_enabled) {
116 dev_priv->sprite_scaling_enabled = false;
117 /* potentially re-enable LP watermarks */
118 intel_update_watermarks(dev);
119 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800120 }
121
122 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
123 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100124
Damien Lespiau5a35e992012-10-26 18:20:12 +0100125 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
126 sprsurf_offset =
127 intel_gen4_compute_offset_xtiled(&x, &y,
128 fb->bits_per_pixel / 8,
129 fb->pitches[0]);
130 linear_offset -= sprsurf_offset;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800131
Damien Lespiau5a35e992012-10-26 18:20:12 +0100132 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
133 * register */
134 if (IS_HASWELL(dev))
135 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
136 else if (obj->tiling_mode != I915_TILING_NONE)
137 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
138 else
139 I915_WRITE(SPRLINOFF(pipe), linear_offset);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100140
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800141 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100142 if (intel_plane->can_scale)
143 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800144 I915_WRITE(SPRCTL(pipe), sprctl);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100145 I915_MODIFY_DISPBASE(SPRSURF(pipe), obj->gtt_offset + sprsurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800146 POSTING_READ(SPRSURF(pipe));
147}
148
149static void
150ivb_disable_plane(struct drm_plane *plane)
151{
152 struct drm_device *dev = plane->dev;
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 struct intel_plane *intel_plane = to_intel_plane(plane);
155 int pipe = intel_plane->pipe;
156
157 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
158 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100159 if (intel_plane->can_scale)
160 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800161 /* Activate double buffered register update */
Armin Reese446f2542012-03-30 16:20:16 -0700162 I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800163 POSTING_READ(SPRSURF(pipe));
Chris Wilson828ed3e2012-04-18 17:12:26 +0100164
165 dev_priv->sprite_scaling_enabled = false;
166 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800167}
168
Jesse Barnes8ea30862012-01-03 08:05:39 -0800169static int
170ivb_update_colorkey(struct drm_plane *plane,
171 struct drm_intel_sprite_colorkey *key)
172{
173 struct drm_device *dev = plane->dev;
174 struct drm_i915_private *dev_priv = dev->dev_private;
175 struct intel_plane *intel_plane;
176 u32 sprctl;
177 int ret = 0;
178
179 intel_plane = to_intel_plane(plane);
180
181 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
182 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
183 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
184
185 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
186 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
187 if (key->flags & I915_SET_COLORKEY_DESTINATION)
188 sprctl |= SPRITE_DEST_KEY;
189 else if (key->flags & I915_SET_COLORKEY_SOURCE)
190 sprctl |= SPRITE_SOURCE_KEY;
191 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
192
193 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
194
195 return ret;
196}
197
198static void
199ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
200{
201 struct drm_device *dev = plane->dev;
202 struct drm_i915_private *dev_priv = dev->dev_private;
203 struct intel_plane *intel_plane;
204 u32 sprctl;
205
206 intel_plane = to_intel_plane(plane);
207
208 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
209 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
210 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
211 key->flags = 0;
212
213 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
214
215 if (sprctl & SPRITE_DEST_KEY)
216 key->flags = I915_SET_COLORKEY_DESTINATION;
217 else if (sprctl & SPRITE_SOURCE_KEY)
218 key->flags = I915_SET_COLORKEY_SOURCE;
219 else
220 key->flags = I915_SET_COLORKEY_NONE;
221}
222
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800223static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100224ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800225 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
226 unsigned int crtc_w, unsigned int crtc_h,
227 uint32_t x, uint32_t y,
228 uint32_t src_w, uint32_t src_h)
229{
230 struct drm_device *dev = plane->dev;
231 struct drm_i915_private *dev_priv = dev->dev_private;
232 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200233 int pipe = intel_plane->pipe;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100234 unsigned long dvssurf_offset, linear_offset;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100235 u32 dvscntr, dvsscale;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200236 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800237
238 dvscntr = I915_READ(DVSCNTR(pipe));
239
240 /* Mask out pixel format bits in case we change it */
241 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800242 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800243 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300244 dvscntr &= ~DVS_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800245
246 switch (fb->pixel_format) {
247 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800248 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800249 break;
250 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800251 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800252 break;
253 case DRM_FORMAT_YUYV:
254 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800255 break;
256 case DRM_FORMAT_YVYU:
257 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800258 break;
259 case DRM_FORMAT_UYVY:
260 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800261 break;
262 case DRM_FORMAT_VYUY:
263 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800264 break;
265 default:
266 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
267 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800268 break;
269 }
270
271 if (obj->tiling_mode != I915_TILING_NONE)
272 dvscntr |= DVS_TILED;
273
Chris Wilsond1686ae2012-04-10 11:41:49 +0100274 if (IS_GEN6(dev))
275 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800276 dvscntr |= DVS_ENABLE;
277
278 /* Sizes are 0 based */
279 src_w--;
280 src_h--;
281 crtc_w--;
282 crtc_h--;
283
284 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
285
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100286 dvsscale = 0;
287 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800288 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
289
290 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
291 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800292
Damien Lespiau5a35e992012-10-26 18:20:12 +0100293 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
294 dvssurf_offset =
295 intel_gen4_compute_offset_xtiled(&x, &y,
296 fb->bits_per_pixel / 8,
297 fb->pitches[0]);
298 linear_offset -= dvssurf_offset;
299
300 if (obj->tiling_mode != I915_TILING_NONE)
301 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
302 else
303 I915_WRITE(DVSLINOFF(pipe), linear_offset);
304
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800305 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
306 I915_WRITE(DVSSCALE(pipe), dvsscale);
307 I915_WRITE(DVSCNTR(pipe), dvscntr);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100308 I915_MODIFY_DISPBASE(DVSSURF(pipe), obj->gtt_offset + dvssurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800309 POSTING_READ(DVSSURF(pipe));
310}
311
312static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100313ilk_disable_plane(struct drm_plane *plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800314{
315 struct drm_device *dev = plane->dev;
316 struct drm_i915_private *dev_priv = dev->dev_private;
317 struct intel_plane *intel_plane = to_intel_plane(plane);
318 int pipe = intel_plane->pipe;
319
320 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
321 /* Disable the scaler */
322 I915_WRITE(DVSSCALE(pipe), 0);
323 /* Flush double buffered register updates */
Armin Reese446f2542012-03-30 16:20:16 -0700324 I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800325 POSTING_READ(DVSSURF(pipe));
326}
327
Jesse Barnes175bd422011-12-13 13:19:39 -0800328static void
329intel_enable_primary(struct drm_crtc *crtc)
330{
331 struct drm_device *dev = crtc->dev;
332 struct drm_i915_private *dev_priv = dev->dev_private;
333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
334 int reg = DSPCNTR(intel_crtc->plane);
335
Chris Wilson93314b52012-06-13 17:36:55 +0100336 if (!intel_crtc->primary_disabled)
337 return;
338
339 intel_crtc->primary_disabled = false;
340 intel_update_fbc(dev);
341
Jesse Barnes175bd422011-12-13 13:19:39 -0800342 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
343}
344
345static void
346intel_disable_primary(struct drm_crtc *crtc)
347{
348 struct drm_device *dev = crtc->dev;
349 struct drm_i915_private *dev_priv = dev->dev_private;
350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
351 int reg = DSPCNTR(intel_crtc->plane);
352
Chris Wilson93314b52012-06-13 17:36:55 +0100353 if (intel_crtc->primary_disabled)
354 return;
355
Jesse Barnes175bd422011-12-13 13:19:39 -0800356 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
Chris Wilson93314b52012-06-13 17:36:55 +0100357
358 intel_crtc->primary_disabled = true;
359 intel_update_fbc(dev);
Jesse Barnes175bd422011-12-13 13:19:39 -0800360}
361
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800362static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100363ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800364 struct drm_intel_sprite_colorkey *key)
365{
366 struct drm_device *dev = plane->dev;
367 struct drm_i915_private *dev_priv = dev->dev_private;
368 struct intel_plane *intel_plane;
369 u32 dvscntr;
370 int ret = 0;
371
372 intel_plane = to_intel_plane(plane);
373
374 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
375 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
376 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
377
378 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
379 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
380 if (key->flags & I915_SET_COLORKEY_DESTINATION)
381 dvscntr |= DVS_DEST_KEY;
382 else if (key->flags & I915_SET_COLORKEY_SOURCE)
383 dvscntr |= DVS_SOURCE_KEY;
384 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
385
386 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
387
388 return ret;
389}
390
391static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100392ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800393{
394 struct drm_device *dev = plane->dev;
395 struct drm_i915_private *dev_priv = dev->dev_private;
396 struct intel_plane *intel_plane;
397 u32 dvscntr;
398
399 intel_plane = to_intel_plane(plane);
400
401 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
402 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
403 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
404 key->flags = 0;
405
406 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
407
408 if (dvscntr & DVS_DEST_KEY)
409 key->flags = I915_SET_COLORKEY_DESTINATION;
410 else if (dvscntr & DVS_SOURCE_KEY)
411 key->flags = I915_SET_COLORKEY_SOURCE;
412 else
413 key->flags = I915_SET_COLORKEY_NONE;
414}
415
416static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800417intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
418 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
419 unsigned int crtc_w, unsigned int crtc_h,
420 uint32_t src_x, uint32_t src_y,
421 uint32_t src_w, uint32_t src_h)
422{
423 struct drm_device *dev = plane->dev;
424 struct drm_i915_private *dev_priv = dev->dev_private;
425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
426 struct intel_plane *intel_plane = to_intel_plane(plane);
427 struct intel_framebuffer *intel_fb;
428 struct drm_i915_gem_object *obj, *old_obj;
429 int pipe = intel_plane->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200430 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
431 pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800432 int ret = 0;
433 int x = src_x >> 16, y = src_y >> 16;
434 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
435 bool disable_primary = false;
436
437 intel_fb = to_intel_framebuffer(fb);
438 obj = intel_fb->obj;
439
440 old_obj = intel_plane->obj;
441
Jesse Barnesb4db1e32012-03-20 10:59:09 -0700442 src_w = src_w >> 16;
443 src_h = src_h >> 16;
444
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800445 /* Pipe must be running... */
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200446 if (!(I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE))
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800447 return -EINVAL;
448
449 if (crtc_x >= primary_w || crtc_y >= primary_h)
450 return -EINVAL;
451
452 /* Don't modify another pipe's plane */
453 if (intel_plane->pipe != intel_crtc->pipe)
454 return -EINVAL;
455
Damien Lespiau94c64192012-10-29 15:14:51 +0000456 /* Sprite planes can be linear or x-tiled surfaces */
457 switch (obj->tiling_mode) {
458 case I915_TILING_NONE:
459 case I915_TILING_X:
460 break;
461 default:
462 return -EINVAL;
463 }
464
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800465 /*
466 * Clamp the width & height into the visible area. Note we don't
467 * try to scale the source if part of the visible region is offscreen.
468 * The caller must handle that by adjusting source offset and size.
469 */
470 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
471 crtc_w += crtc_x;
472 crtc_x = 0;
473 }
474 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
475 goto out;
476 if ((crtc_x + crtc_w) > primary_w)
477 crtc_w = primary_w - crtc_x;
478
479 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
480 crtc_h += crtc_y;
481 crtc_y = 0;
482 }
483 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
484 goto out;
485 if (crtc_y + crtc_h > primary_h)
486 crtc_h = primary_h - crtc_y;
487
488 if (!crtc_w || !crtc_h) /* Again, nothing to display */
489 goto out;
490
491 /*
Damien Lespiau2d354c32012-10-22 18:19:27 +0100492 * We may not have a scaler, eg. HSW does not have it any more
493 */
494 if (!intel_plane->can_scale && (crtc_w != src_w || crtc_h != src_h))
495 return -EINVAL;
496
497 /*
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800498 * We can take a larger source and scale it down, but
499 * only so much... 16x is the max on SNB.
500 */
501 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
502 return -EINVAL;
503
504 /*
505 * If the sprite is completely covering the primary plane,
506 * we can disable the primary and save power.
507 */
508 if ((crtc_x == 0) && (crtc_y == 0) &&
509 (crtc_w == primary_w) && (crtc_h == primary_h))
510 disable_primary = true;
511
512 mutex_lock(&dev->struct_mutex);
513
514 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800515 if (ret)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800516 goto out_unlock;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800517
518 intel_plane->obj = obj;
519
Jesse Barnes175bd422011-12-13 13:19:39 -0800520 /*
521 * Be sure to re-enable the primary before the sprite is no longer
522 * covering it fully.
523 */
Chris Wilson93314b52012-06-13 17:36:55 +0100524 if (!disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800525 intel_enable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800526
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800527 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
528 crtc_w, crtc_h, x, y, src_w, src_h);
529
Chris Wilson93314b52012-06-13 17:36:55 +0100530 if (disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800531 intel_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800532
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800533 /* Unpin old obj after new one is active to avoid ugliness */
534 if (old_obj) {
535 /*
536 * It's fairly common to simply update the position of
537 * an existing object. In that case, we don't need to
538 * wait for vblank to avoid ugliness, we only need to
539 * do the pin & ref bookkeeping.
540 */
541 if (old_obj != obj) {
542 mutex_unlock(&dev->struct_mutex);
543 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
544 mutex_lock(&dev->struct_mutex);
545 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546 intel_unpin_fb_obj(old_obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800547 }
548
549out_unlock:
550 mutex_unlock(&dev->struct_mutex);
551out:
552 return ret;
553}
554
555static int
556intel_disable_plane(struct drm_plane *plane)
557{
558 struct drm_device *dev = plane->dev;
559 struct intel_plane *intel_plane = to_intel_plane(plane);
560 int ret = 0;
561
Chris Wilson93314b52012-06-13 17:36:55 +0100562 if (plane->crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800563 intel_enable_primary(plane->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800564 intel_plane->disable_plane(plane);
565
566 if (!intel_plane->obj)
567 goto out;
568
569 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100570 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800571 intel_plane->obj = NULL;
572 mutex_unlock(&dev->struct_mutex);
573out:
574
575 return ret;
576}
577
578static void intel_destroy_plane(struct drm_plane *plane)
579{
580 struct intel_plane *intel_plane = to_intel_plane(plane);
581 intel_disable_plane(plane);
582 drm_plane_cleanup(plane);
583 kfree(intel_plane);
584}
585
Jesse Barnes8ea30862012-01-03 08:05:39 -0800586int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
587 struct drm_file *file_priv)
588{
589 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800590 struct drm_mode_object *obj;
591 struct drm_plane *plane;
592 struct intel_plane *intel_plane;
593 int ret = 0;
594
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200595 if (!drm_core_check_feature(dev, DRIVER_MODESET))
596 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800597
598 /* Make sure we don't try to enable both src & dest simultaneously */
599 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
600 return -EINVAL;
601
602 mutex_lock(&dev->mode_config.mutex);
603
604 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
605 if (!obj) {
606 ret = -EINVAL;
607 goto out_unlock;
608 }
609
610 plane = obj_to_plane(obj);
611 intel_plane = to_intel_plane(plane);
612 ret = intel_plane->update_colorkey(plane, set);
613
614out_unlock:
615 mutex_unlock(&dev->mode_config.mutex);
616 return ret;
617}
618
619int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
620 struct drm_file *file_priv)
621{
622 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800623 struct drm_mode_object *obj;
624 struct drm_plane *plane;
625 struct intel_plane *intel_plane;
626 int ret = 0;
627
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
629 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800630
631 mutex_lock(&dev->mode_config.mutex);
632
633 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
634 if (!obj) {
635 ret = -EINVAL;
636 goto out_unlock;
637 }
638
639 plane = obj_to_plane(obj);
640 intel_plane = to_intel_plane(plane);
641 intel_plane->get_colorkey(plane, get);
642
643out_unlock:
644 mutex_unlock(&dev->mode_config.mutex);
645 return ret;
646}
647
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800648static const struct drm_plane_funcs intel_plane_funcs = {
649 .update_plane = intel_update_plane,
650 .disable_plane = intel_disable_plane,
651 .destroy = intel_destroy_plane,
652};
653
Chris Wilsond1686ae2012-04-10 11:41:49 +0100654static uint32_t ilk_plane_formats[] = {
655 DRM_FORMAT_XRGB8888,
656 DRM_FORMAT_YUYV,
657 DRM_FORMAT_YVYU,
658 DRM_FORMAT_UYVY,
659 DRM_FORMAT_VYUY,
660};
661
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800662static uint32_t snb_plane_formats[] = {
663 DRM_FORMAT_XBGR8888,
664 DRM_FORMAT_XRGB8888,
665 DRM_FORMAT_YUYV,
666 DRM_FORMAT_YVYU,
667 DRM_FORMAT_UYVY,
668 DRM_FORMAT_VYUY,
669};
670
671int
672intel_plane_init(struct drm_device *dev, enum pipe pipe)
673{
674 struct intel_plane *intel_plane;
675 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100676 const uint32_t *plane_formats;
677 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800678 int ret;
679
Chris Wilsond1686ae2012-04-10 11:41:49 +0100680 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800681 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800682
683 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
684 if (!intel_plane)
685 return -ENOMEM;
686
Chris Wilsond1686ae2012-04-10 11:41:49 +0100687 switch (INTEL_INFO(dev)->gen) {
688 case 5:
689 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +0100690 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800691 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100692 intel_plane->update_plane = ilk_update_plane;
693 intel_plane->disable_plane = ilk_disable_plane;
694 intel_plane->update_colorkey = ilk_update_colorkey;
695 intel_plane->get_colorkey = ilk_get_colorkey;
696
697 if (IS_GEN6(dev)) {
698 plane_formats = snb_plane_formats;
699 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
700 } else {
701 plane_formats = ilk_plane_formats;
702 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
703 }
704 break;
705
706 case 7:
Damien Lespiau4d8d71b2012-10-25 18:06:19 +0100707 if (IS_HASWELL(dev) || IS_VALLEYVIEW(dev))
Damien Lespiau2d354c32012-10-22 18:19:27 +0100708 intel_plane->can_scale = false;
709 else
710 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800711 intel_plane->max_downscale = 2;
712 intel_plane->update_plane = ivb_update_plane;
713 intel_plane->disable_plane = ivb_disable_plane;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800714 intel_plane->update_colorkey = ivb_update_colorkey;
715 intel_plane->get_colorkey = ivb_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100716
717 plane_formats = snb_plane_formats;
718 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
719 break;
720
721 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +0200722 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +0100723 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800724 }
725
726 intel_plane->pipe = pipe;
727 possible_crtcs = (1 << pipe);
728 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +0100729 &intel_plane_funcs,
730 plane_formats, num_plane_formats,
731 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800732 if (ret)
733 kfree(intel_plane);
734
735 return ret;
736}