blob: 11578debc1eea7759be7b900ecc8bf1184e9020a [file] [log] [blame]
Liviu Dudauad49f862016-03-07 10:00:53 +00001/*
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
37static 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
49struct 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
66void 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
75static 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
84static 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);
Brian Starkeya46a0962016-10-11 15:26:05 +010089 struct drm_framebuffer *fb;
90 int n_planes, i;
Liviu Dudauad49f862016-03-07 10:00:53 +000091 u8 format_id;
92 u32 src_w, src_h;
93
94 if (!state->crtc || !state->fb)
95 return 0;
96
Brian Starkeya46a0962016-10-11 15:26:05 +010097 fb = state->fb;
98
Liviu Dudauad49f862016-03-07 10:00:53 +000099 format_id = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
Brian Starkeya46a0962016-10-11 15:26:05 +0100100 fb->pixel_format);
Liviu Dudauad49f862016-03-07 10:00:53 +0000101 if (format_id == MALIDP_INVALID_FORMAT_ID)
102 return -EINVAL;
103
Brian Starkeya46a0962016-10-11 15:26:05 +0100104 n_planes = drm_format_num_planes(fb->pixel_format);
105 for (i = 0; i < n_planes; i++) {
106 if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
107 DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
108 fb->pitches[i], i);
109 return -EINVAL;
110 }
111 }
112
Liviu Dudauad49f862016-03-07 10:00:53 +0000113 src_w = state->src_w >> 16;
114 src_h = state->src_h >> 16;
115
116 if ((state->crtc_w > mp->hwdev->max_line_size) ||
117 (state->crtc_h > mp->hwdev->max_line_size) ||
118 (state->crtc_w < mp->hwdev->min_line_size) ||
119 (state->crtc_h < mp->hwdev->min_line_size) ||
120 (state->crtc_w != src_w) || (state->crtc_h != src_h))
121 return -EINVAL;
122
123 /* packed RGB888 / BGR888 can't be rotated or flipped */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300124 if (state->rotation != DRM_ROTATE_0 &&
Liviu Dudauad49f862016-03-07 10:00:53 +0000125 (state->fb->pixel_format == DRM_FORMAT_RGB888 ||
126 state->fb->pixel_format == DRM_FORMAT_BGR888))
127 return -EINVAL;
128
129 ms->rotmem_size = 0;
130 if (state->rotation & MALIDP_ROTATED_MASK) {
131 int val;
132
133 val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
134 state->crtc_w,
135 state->fb->pixel_format);
136 if (val < 0)
137 return val;
138
139 ms->rotmem_size = val;
140 }
141
142 return 0;
143}
144
145static void malidp_de_plane_update(struct drm_plane *plane,
146 struct drm_plane_state *old_state)
147{
148 struct drm_gem_cma_object *obj;
149 struct malidp_plane *mp;
150 const struct malidp_hw_regmap *map;
151 u8 format_id;
152 u16 ptr;
153 u32 format, src_w, src_h, dest_w, dest_h, val = 0;
154 int num_planes, i;
155
156 mp = to_malidp_plane(plane);
157
158 map = &mp->hwdev->map;
159 format = plane->state->fb->pixel_format;
160 format_id = malidp_hw_get_format_id(map, mp->layer->id, format);
161 num_planes = drm_format_num_planes(format);
162
163 /* convert src values from Q16 fixed point to integer */
164 src_w = plane->state->src_w >> 16;
165 src_h = plane->state->src_h >> 16;
166 if (plane->state->rotation & MALIDP_ROTATED_MASK) {
167 dest_w = plane->state->crtc_h;
168 dest_h = plane->state->crtc_w;
169 } else {
170 dest_w = plane->state->crtc_w;
171 dest_h = plane->state->crtc_h;
172 }
173
174 malidp_hw_write(mp->hwdev, format_id, mp->layer->base);
175
176 for (i = 0; i < num_planes; i++) {
177 /* calculate the offset for the layer's plane registers */
178 ptr = mp->layer->ptr + (i << 4);
179
180 obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
181 malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
182 malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
183 malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i],
184 mp->layer->base + MALIDP_LAYER_STRIDE);
185 }
186
187 malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
188 mp->layer->base + MALIDP_LAYER_SIZE);
189
190 malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
191 mp->layer->base + MALIDP_LAYER_COMP_SIZE);
192
193 malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
194 LAYER_V_VAL(plane->state->crtc_y),
195 mp->layer->base + MALIDP_LAYER_OFFSET);
196
197 /* first clear the rotation bits in the register */
198 malidp_hw_clearbits(mp->hwdev, LAYER_ROT_MASK,
199 mp->layer->base + MALIDP_LAYER_CONTROL);
200
201 /* setup the rotation and axis flip bits */
202 if (plane->state->rotation & DRM_ROTATE_MASK)
203 val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300204 if (plane->state->rotation & DRM_REFLECT_X)
Liviu Dudauad49f862016-03-07 10:00:53 +0000205 val |= LAYER_V_FLIP;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300206 if (plane->state->rotation & DRM_REFLECT_Y)
Liviu Dudauad49f862016-03-07 10:00:53 +0000207 val |= LAYER_H_FLIP;
208
209 /* set the 'enable layer' bit */
210 val |= LAYER_ENABLE;
211
212 malidp_hw_setbits(mp->hwdev, val,
213 mp->layer->base + MALIDP_LAYER_CONTROL);
214}
215
216static void malidp_de_plane_disable(struct drm_plane *plane,
217 struct drm_plane_state *state)
218{
219 struct malidp_plane *mp = to_malidp_plane(plane);
220
221 malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
222 mp->layer->base + MALIDP_LAYER_CONTROL);
223}
224
225static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
226 .atomic_check = malidp_de_plane_check,
227 .atomic_update = malidp_de_plane_update,
228 .atomic_disable = malidp_de_plane_disable,
229};
230
231int malidp_de_planes_init(struct drm_device *drm)
232{
233 struct malidp_drm *malidp = drm->dev_private;
234 const struct malidp_hw_regmap *map = &malidp->dev->map;
235 struct malidp_plane *plane = NULL;
236 enum drm_plane_type plane_type;
237 unsigned long crtcs = 1 << drm->mode_config.num_crtc;
Brian Starkey15807782016-10-11 15:26:07 +0100238 unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
239 DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
Liviu Dudauad49f862016-03-07 10:00:53 +0000240 u32 *formats;
241 int ret, i, j, n;
242
243 formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL);
244 if (!formats) {
245 ret = -ENOMEM;
246 goto cleanup;
247 }
248
249 for (i = 0; i < map->n_layers; i++) {
250 u8 id = map->layers[i].id;
251
252 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
253 if (!plane) {
254 ret = -ENOMEM;
255 goto cleanup;
256 }
257
258 /* build the list of DRM supported formats based on the map */
259 for (n = 0, j = 0; j < map->n_input_formats; j++) {
260 if ((map->input_formats[j].layer & id) == id)
261 formats[n++] = map->input_formats[j].format;
262 }
263
264 plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
265 DRM_PLANE_TYPE_OVERLAY;
266 ret = drm_universal_plane_init(drm, &plane->base, crtcs,
267 &malidp_de_plane_funcs, formats,
268 n, plane_type, NULL);
269 if (ret < 0)
270 goto cleanup;
271
Liviu Dudauad49f862016-03-07 10:00:53 +0000272 drm_plane_helper_add(&plane->base,
273 &malidp_de_plane_helper_funcs);
274 plane->hwdev = malidp->dev;
275 plane->layer = &map->layers[i];
Brian Starkey15807782016-10-11 15:26:07 +0100276
277 /* Skip the features which the SMART layer doesn't have */
278 if (id == DE_SMART)
279 continue;
280
281 drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
Liviu Dudauad49f862016-03-07 10:00:53 +0000282 }
283
284 kfree(formats);
285
286 return 0;
287
288cleanup:
289 malidp_de_planes_destroy(drm);
290 kfree(formats);
291
292 return ret;
293}
294
295void malidp_de_planes_destroy(struct drm_device *drm)
296{
297 struct drm_plane *p, *pt;
298
299 list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
300 drm_plane_cleanup(p);
301 kfree(p);
302 }
303}