2 * Copyright 2013 Ilia Mirkin
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * Implementation based on the pre-KMS implementation in xf86-video-nouveau,
23 * written by Arthur Huillet.
27 #include <drm/drm_crtc.h>
28 #include <drm/drm_fourcc.h>
30 #include "nouveau_drv.h"
32 #include "nouveau_bo.h"
33 #include "nouveau_connector.h"
34 #include "nouveau_display.h"
38 struct nouveau_plane {
39 struct drm_plane base;
41 struct nouveau_bo *cur;
44 struct drm_property *colorkey;
45 struct drm_property *contrast;
46 struct drm_property *brightness;
47 struct drm_property *hue;
48 struct drm_property *saturation;
56 enum drm_color_encoding color_encoding;
58 void (*set_params)(struct nouveau_plane *);
61 static uint32_t formats[] = {
68 /* Sine can be approximated with
69 * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
70 * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
71 * Note that this only works for the range [0, 180].
72 * Also note that sin(x) == -sin(x - 180)
75 sin_mul(int degrees, int factor)
81 return factor * 4 * degrees * (180 - degrees) /
82 (40500 - degrees * (180 - degrees));
85 /* cos(x) = sin(x + 90) */
87 cos_mul(int degrees, int factor)
89 return sin_mul((degrees + 90) % 360, factor);
93 verify_scaling(const struct drm_framebuffer *fb, uint8_t shift,
94 uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h,
95 uint32_t crtc_w, uint32_t crtc_h)
97 if (crtc_w < (src_w >> shift) || crtc_h < (src_h >> shift)) {
98 DRM_DEBUG_KMS("Unsuitable framebuffer scaling: %dx%d -> %dx%d\n",
99 src_w, src_h, crtc_w, crtc_h);
103 if (src_x != 0 || src_y != 0) {
104 DRM_DEBUG_KMS("Unsuitable framebuffer offset: %d,%d\n",
113 nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
114 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
115 unsigned int crtc_w, unsigned int crtc_h,
116 uint32_t src_x, uint32_t src_y,
117 uint32_t src_w, uint32_t src_h,
118 struct drm_modeset_acquire_ctx *ctx)
120 struct nouveau_drm *drm = nouveau_drm(plane->dev);
121 struct nvif_object *dev = &drm->client.device.object;
122 struct nouveau_plane *nv_plane =
123 container_of(plane, struct nouveau_plane, base);
124 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
125 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
126 struct nouveau_bo *cur = nv_plane->cur;
127 bool flip = nv_plane->flip;
128 int soff = NV_PCRTC0_SIZE * nv_crtc->index;
129 int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
130 unsigned shift = drm->client.device.info.chipset >= 0x30 ? 1 : 3;
134 /* Source parameters given in 16.16 fixed point, ignore fractional. */
140 ret = verify_scaling(fb, shift, 0, 0, src_w, src_h, crtc_w, crtc_h);
144 ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM, false);
148 nv_plane->cur = nv_fb->nvbo;
150 nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
151 nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
153 nvif_wr32(dev, NV_PVIDEO_BASE(flip), 0);
154 nvif_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
155 nvif_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
156 nvif_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
157 nvif_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
158 nvif_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
159 nvif_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
160 nvif_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
162 if (fb->format->format == DRM_FORMAT_YUYV ||
163 fb->format->format == DRM_FORMAT_NV12)
164 format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
165 if (fb->format->format == DRM_FORMAT_NV12 ||
166 fb->format->format == DRM_FORMAT_NV21)
167 format |= NV_PVIDEO_FORMAT_PLANAR;
168 if (nv_plane->color_encoding == DRM_COLOR_YCBCR_BT709)
169 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
170 if (nv_plane->colorkey & (1 << 24))
171 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
173 if (format & NV_PVIDEO_FORMAT_PLANAR) {
174 nvif_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
175 nvif_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
176 nv_fb->nvbo->bo.offset + fb->offsets[1]);
178 nvif_wr32(dev, NV_PVIDEO_FORMAT(flip), format | fb->pitches[0]);
179 nvif_wr32(dev, NV_PVIDEO_STOP, 0);
180 /* TODO: wait for vblank? */
181 nvif_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
182 nv_plane->flip = !flip;
185 nouveau_bo_unpin(cur);
191 nv10_disable_plane(struct drm_plane *plane,
192 struct drm_modeset_acquire_ctx *ctx)
194 struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
195 struct nouveau_plane *nv_plane =
196 container_of(plane, struct nouveau_plane, base);
198 nvif_wr32(dev, NV_PVIDEO_STOP, 1);
200 nouveau_bo_unpin(nv_plane->cur);
201 nv_plane->cur = NULL;
208 nv_destroy_plane(struct drm_plane *plane)
210 drm_plane_force_disable(plane);
211 drm_plane_cleanup(plane);
216 nv10_set_params(struct nouveau_plane *plane)
218 struct nvif_object *dev = &nouveau_drm(plane->base.dev)->client.device.object;
219 u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
220 u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
221 (cos_mul(plane->hue, plane->saturation) & 0xffff);
224 nvif_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
225 nvif_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
226 nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
227 nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
228 nvif_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
231 if (plane->color_encoding == DRM_COLOR_YCBCR_BT709)
232 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
233 if (plane->colorkey & (1 << 24))
234 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
235 nvif_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
236 NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
237 NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
243 nv_set_property(struct drm_plane *plane,
244 struct drm_property *property,
247 struct nouveau_plane *nv_plane =
248 container_of(plane, struct nouveau_plane, base);
250 if (property == nv_plane->props.colorkey)
251 nv_plane->colorkey = value;
252 else if (property == nv_plane->props.contrast)
253 nv_plane->contrast = value;
254 else if (property == nv_plane->props.brightness)
255 nv_plane->brightness = value;
256 else if (property == nv_plane->props.hue)
257 nv_plane->hue = value;
258 else if (property == nv_plane->props.saturation)
259 nv_plane->saturation = value;
260 else if (property == nv_plane->base.color_encoding_property)
261 nv_plane->color_encoding = value;
265 if (nv_plane->set_params)
266 nv_plane->set_params(nv_plane);
270 static const struct drm_plane_funcs nv10_plane_funcs = {
271 .update_plane = nv10_update_plane,
272 .disable_plane = nv10_disable_plane,
273 .set_property = nv_set_property,
274 .destroy = nv_destroy_plane,
278 nv10_overlay_init(struct drm_device *device)
280 struct nouveau_drm *drm = nouveau_drm(device);
281 struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
282 unsigned int num_formats = ARRAY_SIZE(formats);
288 switch (drm->client.device.info.chipset) {
298 ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
300 formats, num_formats, false);
304 /* Set up the plane properties */
305 plane->props.colorkey = drm_property_create_range(
306 device, 0, "colorkey", 0, 0x01ffffff);
307 plane->props.contrast = drm_property_create_range(
308 device, 0, "contrast", 0, 8192 - 1);
309 plane->props.brightness = drm_property_create_range(
310 device, 0, "brightness", 0, 1024);
311 plane->props.hue = drm_property_create_range(
312 device, 0, "hue", 0, 359);
313 plane->props.saturation = drm_property_create_range(
314 device, 0, "saturation", 0, 8192 - 1);
315 if (!plane->props.colorkey ||
316 !plane->props.contrast ||
317 !plane->props.brightness ||
319 !plane->props.saturation)
323 drm_object_attach_property(&plane->base.base,
324 plane->props.colorkey, plane->colorkey);
326 plane->contrast = 0x1000;
327 drm_object_attach_property(&plane->base.base,
328 plane->props.contrast, plane->contrast);
330 plane->brightness = 512;
331 drm_object_attach_property(&plane->base.base,
332 plane->props.brightness, plane->brightness);
335 drm_object_attach_property(&plane->base.base,
336 plane->props.hue, plane->hue);
338 plane->saturation = 0x1000;
339 drm_object_attach_property(&plane->base.base,
340 plane->props.saturation, plane->saturation);
342 plane->color_encoding = DRM_COLOR_YCBCR_BT601;
343 drm_plane_create_color_properties(&plane->base,
344 BIT(DRM_COLOR_YCBCR_BT601) |
345 BIT(DRM_COLOR_YCBCR_BT709),
346 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
347 DRM_COLOR_YCBCR_BT601,
348 DRM_COLOR_YCBCR_LIMITED_RANGE);
350 plane->set_params = nv10_set_params;
351 nv10_set_params(plane);
352 drm_plane_force_disable(&plane->base);
355 drm_plane_cleanup(&plane->base);
358 NV_ERROR(drm, "Failed to create plane\n");
362 nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
363 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
364 unsigned int crtc_w, unsigned int crtc_h,
365 uint32_t src_x, uint32_t src_y,
366 uint32_t src_w, uint32_t src_h,
367 struct drm_modeset_acquire_ctx *ctx)
369 struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
370 struct nouveau_plane *nv_plane =
371 container_of(plane, struct nouveau_plane, base);
372 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
373 struct nouveau_bo *cur = nv_plane->cur;
374 uint32_t overlay = 1;
375 int brightness = (nv_plane->brightness - 512) * 62 / 512;
378 /* Source parameters given in 16.16 fixed point, ignore fractional. */
384 ret = verify_scaling(fb, 0, src_x, src_y, src_w, src_h, crtc_w, crtc_h);
388 ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM, false);
392 nv_plane->cur = nv_fb->nvbo;
394 nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
395 nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
396 nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
398 for (i = 0; i < 2; i++) {
399 nvif_wr32(dev, NV_PVIDEO_BUFF0_START_ADDRESS + 4 * i,
400 nv_fb->nvbo->bo.offset);
401 nvif_wr32(dev, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4 * i,
403 nvif_wr32(dev, NV_PVIDEO_BUFF0_OFFSET + 4 * i, 0);
405 nvif_wr32(dev, NV_PVIDEO_WINDOW_START, crtc_y << 16 | crtc_x);
406 nvif_wr32(dev, NV_PVIDEO_WINDOW_SIZE, crtc_h << 16 | crtc_w);
407 nvif_wr32(dev, NV_PVIDEO_STEP_SIZE,
408 (uint32_t)(((src_h - 1) << 11) / (crtc_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (crtc_w - 1)));
410 /* It should be possible to convert hue/contrast to this */
411 nvif_wr32(dev, NV_PVIDEO_RED_CSC_OFFSET, 0x69 - brightness);
412 nvif_wr32(dev, NV_PVIDEO_GREEN_CSC_OFFSET, 0x3e + brightness);
413 nvif_wr32(dev, NV_PVIDEO_BLUE_CSC_OFFSET, 0x89 - brightness);
414 nvif_wr32(dev, NV_PVIDEO_CSC_ADJUST, 0);
416 nvif_wr32(dev, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
417 nvif_wr32(dev, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
419 nvif_wr32(dev, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
420 nvif_wr32(dev, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
422 nvif_wr32(dev, NV_PVIDEO_KEY, nv_plane->colorkey);
424 if (nv_plane->colorkey & (1 << 24))
426 if (fb->format->format == DRM_FORMAT_YUYV)
429 nvif_wr32(dev, NV_PVIDEO_OVERLAY, overlay);
431 nvif_wr32(dev, NV_PVIDEO_SU_STATE, nvif_rd32(dev, NV_PVIDEO_SU_STATE) ^ (1 << 16));
434 nouveau_bo_unpin(cur);
440 nv04_disable_plane(struct drm_plane *plane,
441 struct drm_modeset_acquire_ctx *ctx)
443 struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
444 struct nouveau_plane *nv_plane =
445 container_of(plane, struct nouveau_plane, base);
447 nvif_mask(dev, NV_PVIDEO_OVERLAY, 1, 0);
448 nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
449 nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
450 nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
452 nouveau_bo_unpin(nv_plane->cur);
453 nv_plane->cur = NULL;
459 static const struct drm_plane_funcs nv04_plane_funcs = {
460 .update_plane = nv04_update_plane,
461 .disable_plane = nv04_disable_plane,
462 .set_property = nv_set_property,
463 .destroy = nv_destroy_plane,
467 nv04_overlay_init(struct drm_device *device)
469 struct nouveau_drm *drm = nouveau_drm(device);
470 struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
476 ret = drm_plane_init(device, &plane->base, 1 /* single crtc */,
482 /* Set up the plane properties */
483 plane->props.colorkey = drm_property_create_range(
484 device, 0, "colorkey", 0, 0x01ffffff);
485 plane->props.brightness = drm_property_create_range(
486 device, 0, "brightness", 0, 1024);
487 if (!plane->props.colorkey ||
488 !plane->props.brightness)
492 drm_object_attach_property(&plane->base.base,
493 plane->props.colorkey, plane->colorkey);
495 plane->brightness = 512;
496 drm_object_attach_property(&plane->base.base,
497 plane->props.brightness, plane->brightness);
499 drm_plane_force_disable(&plane->base);
502 drm_plane_cleanup(&plane->base);
505 NV_ERROR(drm, "Failed to create plane\n");
509 nouveau_overlay_init(struct drm_device *device)
511 struct nvif_device *dev = &nouveau_drm(device)->client.device;
512 if (dev->info.chipset < 0x10)
513 nv04_overlay_init(device);
514 else if (dev->info.chipset <= 0x40)
515 nv10_overlay_init(device);