1 /* $NetBSD: nouveau_dispnv04_overlay.c,v 1.2 2014/08/06 15:01:34 riastradh Exp $ */
2
3 /*
4 * Copyright 2013 Ilia Mirkin
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Implementation based on the pre-KMS implementation in xf86-video-nouveau,
25 * written by Arthur Huillet.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: nouveau_dispnv04_overlay.c,v 1.2 2014/08/06 15:01:34 riastradh Exp $");
30
31 #include <drm/drmP.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_fourcc.h>
34
35 #include "nouveau_drm.h"
36
37 #include "nouveau_bo.h"
38 #include "nouveau_connector.h"
39 #include "nouveau_display.h"
40 #include "nvreg.h"
41
42 #include "dispnv04/disp.h"
43
44 struct nouveau_plane {
45 struct drm_plane base;
46 bool flip;
47 struct nouveau_bo *cur;
48
49 struct {
50 struct drm_property *colorkey;
51 struct drm_property *contrast;
52 struct drm_property *brightness;
53 struct drm_property *hue;
54 struct drm_property *saturation;
55 struct drm_property *iturbt_709;
56 } props;
57
58 int colorkey;
59 int contrast;
60 int brightness;
61 int hue;
62 int saturation;
63 int iturbt_709;
64
65 void (*set_params)(struct nouveau_plane *);
66 };
67
68 static const uint32_t formats[] = {
69 DRM_FORMAT_YUYV,
70 DRM_FORMAT_UYVY,
71 DRM_FORMAT_NV12,
72 };
73
74 /* Sine can be approximated with
75 * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
76 * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
77 * Note that this only works for the range [0, 180].
78 * Also note that sin(x) == -sin(x - 180)
79 */
80 static inline int
sin_mul(int degrees,int factor)81 sin_mul(int degrees, int factor)
82 {
83 if (degrees > 180) {
84 degrees -= 180;
85 factor *= -1;
86 }
87 return factor * 4 * degrees * (180 - degrees) /
88 (40500 - degrees * (180 - degrees));
89 }
90
91 /* cos(x) = sin(x + 90) */
92 static inline int
cos_mul(int degrees,int factor)93 cos_mul(int degrees, int factor)
94 {
95 return sin_mul((degrees + 90) % 360, factor);
96 }
97
98 static int
nv10_update_plane(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)99 nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
100 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
101 unsigned int crtc_w, unsigned int crtc_h,
102 uint32_t src_x, uint32_t src_y,
103 uint32_t src_w, uint32_t src_h)
104 {
105 struct nouveau_device *dev = nouveau_dev(plane->dev);
106 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
107 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
108 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
109 struct nouveau_bo *cur = nv_plane->cur;
110 bool flip = nv_plane->flip;
111 int soff = NV_PCRTC0_SIZE * nv_crtc->index;
112 int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
113 int format, ret;
114
115 /* Source parameters given in 16.16 fixed point, ignore fractional. */
116 src_x >>= 16;
117 src_y >>= 16;
118 src_w >>= 16;
119 src_h >>= 16;
120
121 #ifdef __NetBSD__ /* XXX ALIGN means something else. */
122 format = round_up(src_w * 4, 0x100);
123 #else
124 format = ALIGN(src_w * 4, 0x100);
125 #endif
126
127 if (format > 0xffff)
128 return -ERANGE;
129
130 if (dev->chipset >= 0x30) {
131 if (crtc_w < (src_w >> 1) || crtc_h < (src_h >> 1))
132 return -ERANGE;
133 } else {
134 if (crtc_w < (src_w >> 3) || crtc_h < (src_h >> 3))
135 return -ERANGE;
136 }
137
138 ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
139 if (ret)
140 return ret;
141
142 nv_plane->cur = nv_fb->nvbo;
143
144 nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
145 nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
146
147 nv_wr32(dev, NV_PVIDEO_BASE(flip), 0);
148 nv_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
149 nv_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
150 nv_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
151 nv_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
152 nv_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
153 nv_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
154 nv_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
155
156 if (fb->pixel_format != DRM_FORMAT_UYVY)
157 format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
158 if (fb->pixel_format == DRM_FORMAT_NV12)
159 format |= NV_PVIDEO_FORMAT_PLANAR;
160 if (nv_plane->iturbt_709)
161 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
162 if (nv_plane->colorkey & (1 << 24))
163 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
164
165 if (fb->pixel_format == DRM_FORMAT_NV12) {
166 nv_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
167 nv_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
168 nv_fb->nvbo->bo.offset + fb->offsets[1]);
169 }
170 nv_wr32(dev, NV_PVIDEO_FORMAT(flip), format);
171 nv_wr32(dev, NV_PVIDEO_STOP, 0);
172 /* TODO: wait for vblank? */
173 nv_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
174 nv_plane->flip = !flip;
175
176 if (cur)
177 nouveau_bo_unpin(cur);
178
179 return 0;
180 }
181
182 static int
nv10_disable_plane(struct drm_plane * plane)183 nv10_disable_plane(struct drm_plane *plane)
184 {
185 struct nouveau_device *dev = nouveau_dev(plane->dev);
186 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
187
188 nv_wr32(dev, NV_PVIDEO_STOP, 1);
189 if (nv_plane->cur) {
190 nouveau_bo_unpin(nv_plane->cur);
191 nv_plane->cur = NULL;
192 }
193
194 return 0;
195 }
196
197 static void
nv_destroy_plane(struct drm_plane * plane)198 nv_destroy_plane(struct drm_plane *plane)
199 {
200 plane->funcs->disable_plane(plane);
201 drm_plane_cleanup(plane);
202 kfree(plane);
203 }
204
205 static void
nv10_set_params(struct nouveau_plane * plane)206 nv10_set_params(struct nouveau_plane *plane)
207 {
208 struct nouveau_device *dev = nouveau_dev(plane->base.dev);
209 u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
210 u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
211 (cos_mul(plane->hue, plane->saturation) & 0xffff);
212 u32 format = 0;
213
214 nv_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
215 nv_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
216 nv_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
217 nv_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
218 nv_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
219
220 if (plane->cur) {
221 if (plane->iturbt_709)
222 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
223 if (plane->colorkey & (1 << 24))
224 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
225 nv_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
226 NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
227 NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
228 format);
229 }
230 }
231
232 static int
nv_set_property(struct drm_plane * plane,struct drm_property * property,uint64_t value)233 nv_set_property(struct drm_plane *plane,
234 struct drm_property *property,
235 uint64_t value)
236 {
237 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
238
239 if (property == nv_plane->props.colorkey)
240 nv_plane->colorkey = value;
241 else if (property == nv_plane->props.contrast)
242 nv_plane->contrast = value;
243 else if (property == nv_plane->props.brightness)
244 nv_plane->brightness = value;
245 else if (property == nv_plane->props.hue)
246 nv_plane->hue = value;
247 else if (property == nv_plane->props.saturation)
248 nv_plane->saturation = value;
249 else if (property == nv_plane->props.iturbt_709)
250 nv_plane->iturbt_709 = value;
251 else
252 return -EINVAL;
253
254 if (nv_plane->set_params)
255 nv_plane->set_params(nv_plane);
256 return 0;
257 }
258
259 static const struct drm_plane_funcs nv10_plane_funcs = {
260 .update_plane = nv10_update_plane,
261 .disable_plane = nv10_disable_plane,
262 .set_property = nv_set_property,
263 .destroy = nv_destroy_plane,
264 };
265
266 static void
nv10_overlay_init(struct drm_device * device)267 nv10_overlay_init(struct drm_device *device)
268 {
269 struct nouveau_device *dev = nouveau_dev(device);
270 struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
271 int num_formats = ARRAY_SIZE(formats);
272 int ret;
273
274 if (!plane)
275 return;
276
277 switch (dev->chipset) {
278 case 0x10:
279 case 0x11:
280 case 0x15:
281 case 0x1a:
282 case 0x20:
283 num_formats = 2;
284 break;
285 }
286
287 ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
288 &nv10_plane_funcs,
289 formats, num_formats, false);
290 if (ret)
291 goto err;
292
293 /* Set up the plane properties */
294 plane->props.colorkey = drm_property_create_range(
295 device, 0, "colorkey", 0, 0x01ffffff);
296 plane->props.contrast = drm_property_create_range(
297 device, 0, "contrast", 0, 8192 - 1);
298 plane->props.brightness = drm_property_create_range(
299 device, 0, "brightness", 0, 1024);
300 plane->props.hue = drm_property_create_range(
301 device, 0, "hue", 0, 359);
302 plane->props.saturation = drm_property_create_range(
303 device, 0, "saturation", 0, 8192 - 1);
304 plane->props.iturbt_709 = drm_property_create_range(
305 device, 0, "iturbt_709", 0, 1);
306 if (!plane->props.colorkey ||
307 !plane->props.contrast ||
308 !plane->props.brightness ||
309 !plane->props.hue ||
310 !plane->props.saturation ||
311 !plane->props.iturbt_709)
312 goto cleanup;
313
314 plane->colorkey = 0;
315 drm_object_attach_property(&plane->base.base,
316 plane->props.colorkey, plane->colorkey);
317
318 plane->contrast = 0x1000;
319 drm_object_attach_property(&plane->base.base,
320 plane->props.contrast, plane->contrast);
321
322 plane->brightness = 512;
323 drm_object_attach_property(&plane->base.base,
324 plane->props.brightness, plane->brightness);
325
326 plane->hue = 0;
327 drm_object_attach_property(&plane->base.base,
328 plane->props.hue, plane->hue);
329
330 plane->saturation = 0x1000;
331 drm_object_attach_property(&plane->base.base,
332 plane->props.saturation, plane->saturation);
333
334 plane->iturbt_709 = 0;
335 drm_object_attach_property(&plane->base.base,
336 plane->props.iturbt_709, plane->iturbt_709);
337
338 plane->set_params = nv10_set_params;
339 nv10_set_params(plane);
340 nv10_disable_plane(&plane->base);
341 return;
342 cleanup:
343 drm_plane_cleanup(&plane->base);
344 err:
345 kfree(plane);
346 nv_error(dev, "Failed to create plane\n");
347 }
348
349 static int
nv04_update_plane(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)350 nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
351 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
352 unsigned int crtc_w, unsigned int crtc_h,
353 uint32_t src_x, uint32_t src_y,
354 uint32_t src_w, uint32_t src_h)
355 {
356 struct nouveau_device *dev = nouveau_dev(plane->dev);
357 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
358 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
359 struct nouveau_bo *cur = nv_plane->cur;
360 uint32_t overlay = 1;
361 int brightness = (nv_plane->brightness - 512) * 62 / 512;
362 int pitch, ret, i;
363
364 /* Source parameters given in 16.16 fixed point, ignore fractional. */
365 src_x >>= 16;
366 src_y >>= 16;
367 src_w >>= 16;
368 src_h >>= 16;
369
370 #ifdef __NetBSD__ /* XXX ALIGN means something else. */
371 pitch = round_up(src_w * 4, 0x100);
372 #else
373 pitch = ALIGN(src_w * 4, 0x100);
374 #endif
375
376 if (pitch > 0xffff)
377 return -ERANGE;
378
379 /* TODO: Compute an offset? Not sure how to do this for YUYV. */
380 if (src_x != 0 || src_y != 0)
381 return -ERANGE;
382
383 if (crtc_w < src_w || crtc_h < src_h)
384 return -ERANGE;
385
386 ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
387 if (ret)
388 return ret;
389
390 nv_plane->cur = nv_fb->nvbo;
391
392 nv_wr32(dev, NV_PVIDEO_OE_STATE, 0);
393 nv_wr32(dev, NV_PVIDEO_SU_STATE, 0);
394 nv_wr32(dev, NV_PVIDEO_RM_STATE, 0);
395
396 for (i = 0; i < 2; i++) {
397 nv_wr32(dev, NV_PVIDEO_BUFF0_START_ADDRESS + 4 * i,
398 nv_fb->nvbo->bo.offset);
399 nv_wr32(dev, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4 * i, pitch);
400 nv_wr32(dev, NV_PVIDEO_BUFF0_OFFSET + 4 * i, 0);
401 }
402 nv_wr32(dev, NV_PVIDEO_WINDOW_START, crtc_y << 16 | crtc_x);
403 nv_wr32(dev, NV_PVIDEO_WINDOW_SIZE, crtc_h << 16 | crtc_w);
404 nv_wr32(dev, NV_PVIDEO_STEP_SIZE,
405 (uint32_t)(((src_h - 1) << 11) / (crtc_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (crtc_w - 1)));
406
407 /* It should be possible to convert hue/contrast to this */
408 nv_wr32(dev, NV_PVIDEO_RED_CSC_OFFSET, 0x69 - brightness);
409 nv_wr32(dev, NV_PVIDEO_GREEN_CSC_OFFSET, 0x3e + brightness);
410 nv_wr32(dev, NV_PVIDEO_BLUE_CSC_OFFSET, 0x89 - brightness);
411 nv_wr32(dev, NV_PVIDEO_CSC_ADJUST, 0);
412
413 nv_wr32(dev, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
414 nv_wr32(dev, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
415
416 nv_wr32(dev, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
417 nv_wr32(dev, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
418
419 nv_wr32(dev, NV_PVIDEO_KEY, nv_plane->colorkey);
420
421 if (nv_plane->colorkey & (1 << 24))
422 overlay |= 0x10;
423 if (fb->pixel_format == DRM_FORMAT_YUYV)
424 overlay |= 0x100;
425
426 nv_wr32(dev, NV_PVIDEO_OVERLAY, overlay);
427
428 nv_wr32(dev, NV_PVIDEO_SU_STATE, nv_rd32(dev, NV_PVIDEO_SU_STATE) ^ (1 << 16));
429
430 if (cur)
431 nouveau_bo_unpin(cur);
432
433 return 0;
434 }
435
436 static int
nv04_disable_plane(struct drm_plane * plane)437 nv04_disable_plane(struct drm_plane *plane)
438 {
439 struct nouveau_device *dev = nouveau_dev(plane->dev);
440 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
441
442 nv_mask(dev, NV_PVIDEO_OVERLAY, 1, 0);
443 nv_wr32(dev, NV_PVIDEO_OE_STATE, 0);
444 nv_wr32(dev, NV_PVIDEO_SU_STATE, 0);
445 nv_wr32(dev, NV_PVIDEO_RM_STATE, 0);
446 if (nv_plane->cur) {
447 nouveau_bo_unpin(nv_plane->cur);
448 nv_plane->cur = NULL;
449 }
450
451 return 0;
452 }
453
454 static const struct drm_plane_funcs nv04_plane_funcs = {
455 .update_plane = nv04_update_plane,
456 .disable_plane = nv04_disable_plane,
457 .set_property = nv_set_property,
458 .destroy = nv_destroy_plane,
459 };
460
461 static void
nv04_overlay_init(struct drm_device * device)462 nv04_overlay_init(struct drm_device *device)
463 {
464 struct nouveau_device *dev = nouveau_dev(device);
465 struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
466 int ret;
467
468 if (!plane)
469 return;
470
471 ret = drm_plane_init(device, &plane->base, 1 /* single crtc */,
472 &nv04_plane_funcs,
473 formats, 2, false);
474 if (ret)
475 goto err;
476
477 /* Set up the plane properties */
478 plane->props.colorkey = drm_property_create_range(
479 device, 0, "colorkey", 0, 0x01ffffff);
480 plane->props.brightness = drm_property_create_range(
481 device, 0, "brightness", 0, 1024);
482 if (!plane->props.colorkey ||
483 !plane->props.brightness)
484 goto cleanup;
485
486 plane->colorkey = 0;
487 drm_object_attach_property(&plane->base.base,
488 plane->props.colorkey, plane->colorkey);
489
490 plane->brightness = 512;
491 drm_object_attach_property(&plane->base.base,
492 plane->props.brightness, plane->brightness);
493
494 nv04_disable_plane(&plane->base);
495 return;
496 cleanup:
497 drm_plane_cleanup(&plane->base);
498 err:
499 kfree(plane);
500 nv_error(dev, "Failed to create plane\n");
501 }
502
503 void
nouveau_overlay_init(struct drm_device * device)504 nouveau_overlay_init(struct drm_device *device)
505 {
506 struct nouveau_device *dev = nouveau_dev(device);
507 if (dev->chipset < 0x10)
508 nv04_overlay_init(device);
509 else if (dev->chipset <= 0x40)
510 nv10_overlay_init(device);
511 }
512