GNU Linux-libre 4.9.301-gnu1
[releases.git] / drivers / gpu / drm / arm / malidp_planes.c
1 /*
2  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
3  * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * ARM Mali DP plane manipulation routines.
11  */
12
13 #include <drm/drmP.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_cma_helper.h>
16 #include <drm/drm_gem_cma_helper.h>
17 #include <drm/drm_plane_helper.h>
18
19 #include "malidp_hw.h"
20 #include "malidp_drv.h"
21
22 /* Layer specific register offsets */
23 #define MALIDP_LAYER_FORMAT             0x000
24 #define MALIDP_LAYER_CONTROL            0x004
25 #define   LAYER_ENABLE                  (1 << 0)
26 #define   LAYER_ROT_OFFSET              8
27 #define   LAYER_H_FLIP                  (1 << 10)
28 #define   LAYER_V_FLIP                  (1 << 11)
29 #define   LAYER_ROT_MASK                (0xf << 8)
30 #define MALIDP_LAYER_SIZE               0x00c
31 #define   LAYER_H_VAL(x)                (((x) & 0x1fff) << 0)
32 #define   LAYER_V_VAL(x)                (((x) & 0x1fff) << 16)
33 #define MALIDP_LAYER_COMP_SIZE          0x010
34 #define MALIDP_LAYER_OFFSET             0x014
35 #define MALIDP_LAYER_STRIDE             0x018
36
37 static void malidp_de_plane_destroy(struct drm_plane *plane)
38 {
39         struct malidp_plane *mp = to_malidp_plane(plane);
40
41         if (mp->base.fb)
42                 drm_framebuffer_unreference(mp->base.fb);
43
44         drm_plane_helper_disable(plane);
45         drm_plane_cleanup(plane);
46         devm_kfree(plane->dev->dev, mp);
47 }
48
49 struct drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
50 {
51         struct malidp_plane_state *state, *m_state;
52
53         if (!plane->state)
54                 return NULL;
55
56         state = kmalloc(sizeof(*state), GFP_KERNEL);
57         if (state) {
58                 m_state = to_malidp_plane_state(plane->state);
59                 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
60                 state->rotmem_size = m_state->rotmem_size;
61         }
62
63         return &state->base;
64 }
65
66 void malidp_destroy_plane_state(struct drm_plane *plane,
67                                 struct drm_plane_state *state)
68 {
69         struct malidp_plane_state *m_state = to_malidp_plane_state(state);
70
71         __drm_atomic_helper_plane_destroy_state(state);
72         kfree(m_state);
73 }
74
75 static const struct drm_plane_funcs malidp_de_plane_funcs = {
76         .update_plane = drm_atomic_helper_update_plane,
77         .disable_plane = drm_atomic_helper_disable_plane,
78         .destroy = malidp_de_plane_destroy,
79         .reset = drm_atomic_helper_plane_reset,
80         .atomic_duplicate_state = malidp_duplicate_plane_state,
81         .atomic_destroy_state = malidp_destroy_plane_state,
82 };
83
84 static int malidp_de_plane_check(struct drm_plane *plane,
85                                  struct drm_plane_state *state)
86 {
87         struct malidp_plane *mp = to_malidp_plane(plane);
88         struct malidp_plane_state *ms = to_malidp_plane_state(state);
89         u8 format_id;
90         u32 src_w, src_h;
91
92         if (!state->crtc || !state->fb)
93                 return 0;
94
95         format_id = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
96                                             state->fb->pixel_format);
97         if (format_id == MALIDP_INVALID_FORMAT_ID)
98                 return -EINVAL;
99
100         src_w = state->src_w >> 16;
101         src_h = state->src_h >> 16;
102
103         if ((state->crtc_w > mp->hwdev->max_line_size) ||
104             (state->crtc_h > mp->hwdev->max_line_size) ||
105             (state->crtc_w < mp->hwdev->min_line_size) ||
106             (state->crtc_h < mp->hwdev->min_line_size) ||
107             (state->crtc_w != src_w) || (state->crtc_h != src_h))
108                 return -EINVAL;
109
110         /* packed RGB888 / BGR888 can't be rotated or flipped */
111         if (state->rotation != DRM_ROTATE_0 &&
112             (state->fb->pixel_format == DRM_FORMAT_RGB888 ||
113              state->fb->pixel_format == DRM_FORMAT_BGR888))
114                 return -EINVAL;
115
116         ms->rotmem_size = 0;
117         if (state->rotation & MALIDP_ROTATED_MASK) {
118                 int val;
119
120                 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
121                                                  state->crtc_w,
122                                                  state->fb->pixel_format);
123                 if (val < 0)
124                         return val;
125
126                 ms->rotmem_size = val;
127         }
128
129         return 0;
130 }
131
132 static void malidp_de_plane_update(struct drm_plane *plane,
133                                    struct drm_plane_state *old_state)
134 {
135         struct drm_gem_cma_object *obj;
136         struct malidp_plane *mp;
137         const struct malidp_hw_regmap *map;
138         u8 format_id;
139         u16 ptr;
140         u32 format, src_w, src_h, dest_w, dest_h, val = 0;
141         int num_planes, i;
142
143         mp = to_malidp_plane(plane);
144
145         map = &mp->hwdev->map;
146         format = plane->state->fb->pixel_format;
147         format_id = malidp_hw_get_format_id(map, mp->layer->id, format);
148         num_planes = drm_format_num_planes(format);
149
150         /* convert src values from Q16 fixed point to integer */
151         src_w = plane->state->src_w >> 16;
152         src_h = plane->state->src_h >> 16;
153         dest_w = plane->state->crtc_w;
154         dest_h = plane->state->crtc_h;
155
156         malidp_hw_write(mp->hwdev, format_id, mp->layer->base);
157
158         for (i = 0; i < num_planes; i++) {
159                 /* calculate the offset for the layer's plane registers */
160                 ptr = mp->layer->ptr + (i << 4);
161
162                 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
163                 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
164                 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
165                 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
166                                 mp->layer->base + MALIDP_LAYER_STRIDE);
167         }
168
169         malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
170                         mp->layer->base + MALIDP_LAYER_SIZE);
171
172         malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
173                         mp->layer->base + MALIDP_LAYER_COMP_SIZE);
174
175         malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
176                         LAYER_V_VAL(plane->state->crtc_y),
177                         mp->layer->base + MALIDP_LAYER_OFFSET);
178
179         /* first clear the rotation bits in the register */
180         malidp_hw_clearbits(mp->hwdev, LAYER_ROT_MASK,
181                             mp->layer->base + MALIDP_LAYER_CONTROL);
182
183         /* setup the rotation and axis flip bits */
184         if (plane->state->rotation & DRM_ROTATE_MASK)
185                 val |= ilog2(plane->state->rotation & DRM_ROTATE_MASK) <<
186                        LAYER_ROT_OFFSET;
187         if (plane->state->rotation & DRM_REFLECT_X)
188                 val |= LAYER_H_FLIP;
189         if (plane->state->rotation & DRM_REFLECT_Y)
190                 val |= LAYER_V_FLIP;
191
192         /* set the 'enable layer' bit */
193         val |= LAYER_ENABLE;
194
195         malidp_hw_setbits(mp->hwdev, val,
196                           mp->layer->base + MALIDP_LAYER_CONTROL);
197 }
198
199 static void malidp_de_plane_disable(struct drm_plane *plane,
200                                     struct drm_plane_state *state)
201 {
202         struct malidp_plane *mp = to_malidp_plane(plane);
203
204         malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
205                             mp->layer->base + MALIDP_LAYER_CONTROL);
206 }
207
208 static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
209         .atomic_check = malidp_de_plane_check,
210         .atomic_update = malidp_de_plane_update,
211         .atomic_disable = malidp_de_plane_disable,
212 };
213
214 int malidp_de_planes_init(struct drm_device *drm)
215 {
216         struct malidp_drm *malidp = drm->dev_private;
217         const struct malidp_hw_regmap *map = &malidp->dev->map;
218         struct malidp_plane *plane = NULL;
219         enum drm_plane_type plane_type;
220         unsigned long crtcs = 1 << drm->mode_config.num_crtc;
221         u32 *formats;
222         int ret, i, j, n;
223
224         formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
225         if (!formats) {
226                 ret = -ENOMEM;
227                 goto cleanup;
228         }
229
230         for (i = 0; i < map->n_layers; i++) {
231                 u8 id = map->layers[i].id;
232
233                 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
234                 if (!plane) {
235                         ret = -ENOMEM;
236                         goto cleanup;
237                 }
238
239                 /* build the list of DRM supported formats based on the map */
240                 for (n = 0, j = 0;  j < map->n_input_formats; j++) {
241                         if ((map->input_formats[j].layer & id) == id)
242                                 formats[n++] = map->input_formats[j].format;
243                 }
244
245                 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
246                                         DRM_PLANE_TYPE_OVERLAY;
247                 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
248                                                &malidp_de_plane_funcs, formats,
249                                                n, plane_type, NULL);
250                 if (ret < 0)
251                         goto cleanup;
252
253                 if (!drm->mode_config.rotation_property) {
254                         unsigned long flags = DRM_ROTATE_0 |
255                                               DRM_ROTATE_90 |
256                                               DRM_ROTATE_180 |
257                                               DRM_ROTATE_270 |
258                                               DRM_REFLECT_X |
259                                               DRM_REFLECT_Y;
260                         drm->mode_config.rotation_property =
261                                 drm_mode_create_rotation_property(drm, flags);
262                 }
263                 /* SMART layer can't be rotated */
264                 if (drm->mode_config.rotation_property && (id != DE_SMART))
265                         drm_object_attach_property(&plane->base.base,
266                                                    drm->mode_config.rotation_property,
267                                                    DRM_ROTATE_0);
268
269                 drm_plane_helper_add(&plane->base,
270                                      &malidp_de_plane_helper_funcs);
271                 plane->hwdev = malidp->dev;
272                 plane->layer = &map->layers[i];
273         }
274
275         kfree(formats);
276
277         return 0;
278
279 cleanup:
280         malidp_de_planes_destroy(drm);
281         kfree(formats);
282
283         return ret;
284 }
285
286 void malidp_de_planes_destroy(struct drm_device *drm)
287 {
288         struct drm_plane *p, *pt;
289
290         list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
291                 drm_plane_cleanup(p);
292                 kfree(p);
293         }
294 }