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