blob: c77717db635b044517432ceeca7a289c55f51528 [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;
98
99 /* Sizes are 0 based */
100 src_w--;
101 src_h--;
102 crtc_w--;
103 crtc_h--;
104
105 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
106
107 /*
108 * IVB workaround: must disable low power watermarks for at least
109 * one frame before enabling scaling. LP watermarks can be re-enabled
110 * when scaling is disabled.
111 */
112 if (crtc_w != src_w || crtc_h != src_h) {
113 dev_priv->sprite_scaling_enabled = true;
114 sandybridge_update_wm(dev);
115 intel_wait_for_vblank(dev, pipe);
116 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
117 } else {
118 dev_priv->sprite_scaling_enabled = false;
119 /* potentially re-enable LP watermarks */
120 sandybridge_update_wm(dev);
121 }
122
123 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
124 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
125 if (obj->tiling_mode != I915_TILING_NONE) {
126 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
127 } else {
128 unsigned long offset;
129
130 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
131 I915_WRITE(SPRLINOFF(pipe), offset);
132 }
133 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
134 I915_WRITE(SPRSCALE(pipe), sprscale);
135 I915_WRITE(SPRCTL(pipe), sprctl);
136 I915_WRITE(SPRSURF(pipe), obj->gtt_offset);
137 POSTING_READ(SPRSURF(pipe));
138}
139
140static void
141ivb_disable_plane(struct drm_plane *plane)
142{
143 struct drm_device *dev = plane->dev;
144 struct drm_i915_private *dev_priv = dev->dev_private;
145 struct intel_plane *intel_plane = to_intel_plane(plane);
146 int pipe = intel_plane->pipe;
147
148 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
149 /* Can't leave the scaler enabled... */
150 I915_WRITE(SPRSCALE(pipe), 0);
151 /* Activate double buffered register update */
152 I915_WRITE(SPRSURF(pipe), 0);
153 POSTING_READ(SPRSURF(pipe));
154}
155
156static void
157snb_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
158 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
159 unsigned int crtc_w, unsigned int crtc_h,
160 uint32_t x, uint32_t y,
161 uint32_t src_w, uint32_t src_h)
162{
163 struct drm_device *dev = plane->dev;
164 struct drm_i915_private *dev_priv = dev->dev_private;
165 struct intel_plane *intel_plane = to_intel_plane(plane);
166 int pipe = intel_plane->pipe, pixel_size;
167 u32 dvscntr, dvsscale = 0;
168
169 dvscntr = I915_READ(DVSCNTR(pipe));
170
171 /* Mask out pixel format bits in case we change it */
172 dvscntr &= ~DVS_PIXFORMAT_MASK;
173 dvscntr &= ~DVS_RGB_ORDER_RGBX;
174 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
175
176 switch (fb->pixel_format) {
177 case DRM_FORMAT_XBGR8888:
178 dvscntr |= DVS_FORMAT_RGBX888;
179 pixel_size = 4;
180 break;
181 case DRM_FORMAT_XRGB8888:
182 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_RGBX;
183 pixel_size = 4;
184 break;
185 case DRM_FORMAT_YUYV:
186 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
187 pixel_size = 2;
188 break;
189 case DRM_FORMAT_YVYU:
190 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
191 pixel_size = 2;
192 break;
193 case DRM_FORMAT_UYVY:
194 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
195 pixel_size = 2;
196 break;
197 case DRM_FORMAT_VYUY:
198 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
199 pixel_size = 2;
200 break;
201 default:
202 DRM_DEBUG_DRIVER("bad pixel format, assuming RGBX888\n");
203 dvscntr |= DVS_FORMAT_RGBX888;
204 pixel_size = 4;
205 break;
206 }
207
208 if (obj->tiling_mode != I915_TILING_NONE)
209 dvscntr |= DVS_TILED;
210
211 /* must disable */
212 dvscntr |= DVS_TRICKLE_FEED_DISABLE;
213 dvscntr |= DVS_ENABLE;
214
215 /* Sizes are 0 based */
216 src_w--;
217 src_h--;
218 crtc_w--;
219 crtc_h--;
220
221 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
222
223 if (crtc_w != src_w || crtc_h != src_h)
224 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
225
226 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
227 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
228 if (obj->tiling_mode != I915_TILING_NONE) {
229 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
230 } else {
231 unsigned long offset;
232
233 offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
234 I915_WRITE(DVSLINOFF(pipe), offset);
235 }
236 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
237 I915_WRITE(DVSSCALE(pipe), dvsscale);
238 I915_WRITE(DVSCNTR(pipe), dvscntr);
239 I915_WRITE(DVSSURF(pipe), obj->gtt_offset);
240 POSTING_READ(DVSSURF(pipe));
241}
242
243static void
244snb_disable_plane(struct drm_plane *plane)
245{
246 struct drm_device *dev = plane->dev;
247 struct drm_i915_private *dev_priv = dev->dev_private;
248 struct intel_plane *intel_plane = to_intel_plane(plane);
249 int pipe = intel_plane->pipe;
250
251 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
252 /* Disable the scaler */
253 I915_WRITE(DVSSCALE(pipe), 0);
254 /* Flush double buffered register updates */
255 I915_WRITE(DVSSURF(pipe), 0);
256 POSTING_READ(DVSSURF(pipe));
257}
258
259static int
260intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
261 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
262 unsigned int crtc_w, unsigned int crtc_h,
263 uint32_t src_x, uint32_t src_y,
264 uint32_t src_w, uint32_t src_h)
265{
266 struct drm_device *dev = plane->dev;
267 struct drm_i915_private *dev_priv = dev->dev_private;
268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
269 struct intel_plane *intel_plane = to_intel_plane(plane);
270 struct intel_framebuffer *intel_fb;
271 struct drm_i915_gem_object *obj, *old_obj;
272 int pipe = intel_plane->pipe;
273 int ret = 0;
274 int x = src_x >> 16, y = src_y >> 16;
275 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
276 bool disable_primary = false;
277
278 intel_fb = to_intel_framebuffer(fb);
279 obj = intel_fb->obj;
280
281 old_obj = intel_plane->obj;
282
283 /* Pipe must be running... */
284 if (!(I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE))
285 return -EINVAL;
286
287 if (crtc_x >= primary_w || crtc_y >= primary_h)
288 return -EINVAL;
289
290 /* Don't modify another pipe's plane */
291 if (intel_plane->pipe != intel_crtc->pipe)
292 return -EINVAL;
293
294 /*
295 * Clamp the width & height into the visible area. Note we don't
296 * try to scale the source if part of the visible region is offscreen.
297 * The caller must handle that by adjusting source offset and size.
298 */
299 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
300 crtc_w += crtc_x;
301 crtc_x = 0;
302 }
303 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
304 goto out;
305 if ((crtc_x + crtc_w) > primary_w)
306 crtc_w = primary_w - crtc_x;
307
308 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
309 crtc_h += crtc_y;
310 crtc_y = 0;
311 }
312 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
313 goto out;
314 if (crtc_y + crtc_h > primary_h)
315 crtc_h = primary_h - crtc_y;
316
317 if (!crtc_w || !crtc_h) /* Again, nothing to display */
318 goto out;
319
320 /*
321 * We can take a larger source and scale it down, but
322 * only so much... 16x is the max on SNB.
323 */
324 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
325 return -EINVAL;
326
327 /*
328 * If the sprite is completely covering the primary plane,
329 * we can disable the primary and save power.
330 */
331 if ((crtc_x == 0) && (crtc_y == 0) &&
332 (crtc_w == primary_w) && (crtc_h == primary_h))
333 disable_primary = true;
334
335 mutex_lock(&dev->struct_mutex);
336
337 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
338 if (ret) {
339 DRM_ERROR("failed to pin object\n");
340 goto out_unlock;
341 }
342
343 intel_plane->obj = obj;
344
345 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
346 crtc_w, crtc_h, x, y, src_w, src_h);
347
348 /* Unpin old obj after new one is active to avoid ugliness */
349 if (old_obj) {
350 /*
351 * It's fairly common to simply update the position of
352 * an existing object. In that case, we don't need to
353 * wait for vblank to avoid ugliness, we only need to
354 * do the pin & ref bookkeeping.
355 */
356 if (old_obj != obj) {
357 mutex_unlock(&dev->struct_mutex);
358 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
359 mutex_lock(&dev->struct_mutex);
360 }
361 i915_gem_object_unpin(old_obj);
362 }
363
364out_unlock:
365 mutex_unlock(&dev->struct_mutex);
366out:
367 return ret;
368}
369
370static int
371intel_disable_plane(struct drm_plane *plane)
372{
373 struct drm_device *dev = plane->dev;
374 struct intel_plane *intel_plane = to_intel_plane(plane);
375 int ret = 0;
376
377 intel_plane->disable_plane(plane);
378
379 if (!intel_plane->obj)
380 goto out;
381
382 mutex_lock(&dev->struct_mutex);
383 i915_gem_object_unpin(intel_plane->obj);
384 intel_plane->obj = NULL;
385 mutex_unlock(&dev->struct_mutex);
386out:
387
388 return ret;
389}
390
391static void intel_destroy_plane(struct drm_plane *plane)
392{
393 struct intel_plane *intel_plane = to_intel_plane(plane);
394 intel_disable_plane(plane);
395 drm_plane_cleanup(plane);
396 kfree(intel_plane);
397}
398
399static const struct drm_plane_funcs intel_plane_funcs = {
400 .update_plane = intel_update_plane,
401 .disable_plane = intel_disable_plane,
402 .destroy = intel_destroy_plane,
403};
404
405static uint32_t snb_plane_formats[] = {
406 DRM_FORMAT_XBGR8888,
407 DRM_FORMAT_XRGB8888,
408 DRM_FORMAT_YUYV,
409 DRM_FORMAT_YVYU,
410 DRM_FORMAT_UYVY,
411 DRM_FORMAT_VYUY,
412};
413
414int
415intel_plane_init(struct drm_device *dev, enum pipe pipe)
416{
417 struct intel_plane *intel_plane;
418 unsigned long possible_crtcs;
419 int ret;
420
421 if (!(IS_GEN6(dev) || IS_GEN7(dev))) {
422 DRM_ERROR("new plane code only for SNB+\n");
423 return -ENODEV;
424 }
425
426 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
427 if (!intel_plane)
428 return -ENOMEM;
429
430 if (IS_GEN6(dev)) {
431 intel_plane->max_downscale = 16;
432 intel_plane->update_plane = snb_update_plane;
433 intel_plane->disable_plane = snb_disable_plane;
434 } else if (IS_GEN7(dev)) {
435 intel_plane->max_downscale = 2;
436 intel_plane->update_plane = ivb_update_plane;
437 intel_plane->disable_plane = ivb_disable_plane;
438 }
439
440 intel_plane->pipe = pipe;
441 possible_crtcs = (1 << pipe);
442 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
443 &intel_plane_funcs, snb_plane_formats,
444 ARRAY_SIZE(snb_plane_formats));
445 if (ret)
446 kfree(intel_plane);
447
448 return ret;
449}
450