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