blob: 514a3055903c1d277049fc82dc7302b826430447 [file] [log] [blame]
Ilia Mirkin515de6b2013-09-07 20:33:43 -04001/*
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
60static uint32_t formats[] = {
61 DRM_FORMAT_NV12,
62 DRM_FORMAT_UYVY,
63};
64
65/* Sine can be approximated with
66 * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
67 * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
68 * Note that this only works for the range [0, 180].
69 * Also note that sin(x) == -sin(x - 180)
70 */
71static inline int
72sin_mul(int degrees, int factor)
73{
74 if (degrees > 180) {
75 degrees -= 180;
76 factor *= -1;
77 }
78 return factor * 4 * degrees * (180 - degrees) /
79 (40500 - degrees * (180 - degrees));
80}
81
82/* cos(x) = sin(x + 90) */
83static inline int
84cos_mul(int degrees, int factor)
85{
86 return sin_mul((degrees + 90) % 360, factor);
87}
88
89static int
90nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
91 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
92 unsigned int crtc_w, unsigned int crtc_h,
93 uint32_t src_x, uint32_t src_y,
94 uint32_t src_w, uint32_t src_h)
95{
96 struct nouveau_device *dev = nouveau_dev(plane->dev);
97 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
98 struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
99 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
100 struct nouveau_bo *cur = nv_plane->cur;
101 bool flip = nv_plane->flip;
Ilia Mirkin515de6b2013-09-07 20:33:43 -0400102 int soff = NV_PCRTC0_SIZE * nv_crtc->index;
103 int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
Ilia Mirkin92e5b0a2013-11-15 11:26:41 -0500104 int format, ret;
105
106 /* Source parameters given in 16.16 fixed point, ignore fractional. */
107 src_x >>= 16;
108 src_y >>= 16;
109 src_w >>= 16;
110 src_h >>= 16;
111
112 format = ALIGN(src_w * 4, 0x100);
Ilia Mirkin515de6b2013-09-07 20:33:43 -0400113
114 if (format > 0xffff)
115 return -EINVAL;
116
117 ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
118 if (ret)
119 return ret;
120
121 nv_plane->cur = nv_fb->nvbo;
122
Ilia Mirkin515de6b2013-09-07 20:33:43 -0400123 nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
124 nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
125
126 nv_wr32(dev, NV_PVIDEO_BASE(flip), 0);
127 nv_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
128 nv_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
129 nv_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
130 nv_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
131 nv_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
132 nv_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
133 nv_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
134
135 if (fb->pixel_format == DRM_FORMAT_NV12) {
136 format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
137 format |= NV_PVIDEO_FORMAT_PLANAR;
138 }
139 if (nv_plane->iturbt_709)
140 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
141 if (nv_plane->colorkey & (1 << 24))
142 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
143
144 if (fb->pixel_format == DRM_FORMAT_NV12) {
145 nv_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
146 nv_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
147 nv_fb->nvbo->bo.offset + fb->offsets[1]);
148 }
149 nv_wr32(dev, NV_PVIDEO_FORMAT(flip), format);
150 nv_wr32(dev, NV_PVIDEO_STOP, 0);
151 /* TODO: wait for vblank? */
152 nv_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
153 nv_plane->flip = !flip;
154
155 if (cur)
156 nouveau_bo_unpin(cur);
157
158 return 0;
159}
160
161static int
162nv10_disable_plane(struct drm_plane *plane)
163{
164 struct nouveau_device *dev = nouveau_dev(plane->dev);
165 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
166
167 nv_wr32(dev, NV_PVIDEO_STOP, 1);
168 if (nv_plane->cur) {
169 nouveau_bo_unpin(nv_plane->cur);
170 nv_plane->cur = NULL;
171 }
172
173 return 0;
174}
175
176static void
177nv10_destroy_plane(struct drm_plane *plane)
178{
179 nv10_disable_plane(plane);
180 drm_plane_cleanup(plane);
181 kfree(plane);
182}
183
184static void
185nv10_set_params(struct nouveau_plane *plane)
186{
187 struct nouveau_device *dev = nouveau_dev(plane->base.dev);
188 u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
189 u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
190 (cos_mul(plane->hue, plane->saturation) & 0xffff);
191 u32 format = 0;
192
193 nv_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
194 nv_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
195 nv_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
196 nv_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
197 nv_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
198
199 if (plane->cur) {
200 if (plane->iturbt_709)
201 format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
202 if (plane->colorkey & (1 << 24))
203 format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
204 nv_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
205 NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
206 NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
207 format);
208 }
209}
210
211static int
212nv10_set_property(struct drm_plane *plane,
213 struct drm_property *property,
214 uint64_t value)
215{
216 struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
217
218 if (property == nv_plane->props.colorkey)
219 nv_plane->colorkey = value;
220 else if (property == nv_plane->props.contrast)
221 nv_plane->contrast = value;
222 else if (property == nv_plane->props.brightness)
223 nv_plane->brightness = value;
224 else if (property == nv_plane->props.hue)
225 nv_plane->hue = value;
226 else if (property == nv_plane->props.saturation)
227 nv_plane->saturation = value;
228 else if (property == nv_plane->props.iturbt_709)
229 nv_plane->iturbt_709 = value;
230 else
231 return -EINVAL;
232
233 nv10_set_params(nv_plane);
234 return 0;
235}
236
237static const struct drm_plane_funcs nv10_plane_funcs = {
238 .update_plane = nv10_update_plane,
239 .disable_plane = nv10_disable_plane,
240 .set_property = nv10_set_property,
241 .destroy = nv10_destroy_plane,
242};
243
244static void
245nv10_overlay_init(struct drm_device *device)
246{
247 struct nouveau_device *dev = nouveau_dev(device);
248 struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
249 int ret;
250
251 if (!plane)
252 return;
253
254 ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
255 &nv10_plane_funcs,
256 formats, ARRAY_SIZE(formats), false);
257 if (ret)
258 goto err;
259
260 /* Set up the plane properties */
261 plane->props.colorkey = drm_property_create_range(
262 device, 0, "colorkey", 0, 0x01ffffff);
263 plane->props.contrast = drm_property_create_range(
264 device, 0, "contrast", 0, 8192 - 1);
265 plane->props.brightness = drm_property_create_range(
266 device, 0, "brightness", 0, 1024);
267 plane->props.hue = drm_property_create_range(
268 device, 0, "hue", 0, 359);
269 plane->props.saturation = drm_property_create_range(
270 device, 0, "saturation", 0, 8192 - 1);
271 plane->props.iturbt_709 = drm_property_create_range(
272 device, 0, "iturbt_709", 0, 1);
273 if (!plane->props.colorkey ||
274 !plane->props.contrast ||
275 !plane->props.brightness ||
276 !plane->props.hue ||
277 !plane->props.saturation ||
278 !plane->props.iturbt_709)
279 goto cleanup;
280
281 plane->colorkey = 0;
282 drm_object_attach_property(&plane->base.base,
283 plane->props.colorkey, plane->colorkey);
284
285 plane->contrast = 0x1000;
286 drm_object_attach_property(&plane->base.base,
287 plane->props.contrast, plane->contrast);
288
289 plane->brightness = 512;
290 drm_object_attach_property(&plane->base.base,
291 plane->props.brightness, plane->brightness);
292
293 plane->hue = 0;
294 drm_object_attach_property(&plane->base.base,
295 plane->props.hue, plane->hue);
296
297 plane->saturation = 0x1000;
298 drm_object_attach_property(&plane->base.base,
299 plane->props.saturation, plane->saturation);
300
301 plane->iturbt_709 = 0;
302 drm_object_attach_property(&plane->base.base,
303 plane->props.iturbt_709, plane->iturbt_709);
304
305 nv10_set_params(plane);
306 nv_wr32(dev, NV_PVIDEO_STOP, 1);
307 return;
308cleanup:
309 drm_plane_cleanup(&plane->base);
310err:
311 kfree(plane);
312 nv_error(dev, "Failed to create plane\n");
313}
314
315void
316nouveau_overlay_init(struct drm_device *device)
317{
318 struct nouveau_device *dev = nouveau_dev(device);
319 if (dev->chipset >= 0x10 && dev->chipset <= 0x40)
320 nv10_overlay_init(device);
321}