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