Linux Audio

Check our new training course

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