Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 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); |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame^] | 89 | struct drm_framebuffer *fb; |
| 90 | int n_planes, i; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 91 | u8 format_id; |
| 92 | u32 src_w, src_h; |
| 93 | |
| 94 | if (!state->crtc || !state->fb) |
| 95 | return 0; |
| 96 | |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame^] | 97 | fb = state->fb; |
| 98 | |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 99 | format_id = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id, |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame^] | 100 | fb->pixel_format); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 101 | if (format_id == MALIDP_INVALID_FORMAT_ID) |
| 102 | return -EINVAL; |
| 103 | |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame^] | 104 | 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 Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 113 | 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 Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 124 | if (state->rotation != DRM_ROTATE_0 && |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 125 | (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 | |
| 145 | static 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 Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 204 | if (plane->state->rotation & DRM_REFLECT_X) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 205 | val |= LAYER_V_FLIP; |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 206 | if (plane->state->rotation & DRM_REFLECT_Y) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 207 | 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 | |
| 216 | static 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 | |
| 225 | static 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 | |
| 231 | int 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; |
| 238 | u32 *formats; |
| 239 | int ret, i, j, n; |
| 240 | |
| 241 | formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL); |
| 242 | if (!formats) { |
| 243 | ret = -ENOMEM; |
| 244 | goto cleanup; |
| 245 | } |
| 246 | |
| 247 | for (i = 0; i < map->n_layers; i++) { |
| 248 | u8 id = map->layers[i].id; |
| 249 | |
| 250 | plane = kzalloc(sizeof(*plane), GFP_KERNEL); |
| 251 | if (!plane) { |
| 252 | ret = -ENOMEM; |
| 253 | goto cleanup; |
| 254 | } |
| 255 | |
| 256 | /* build the list of DRM supported formats based on the map */ |
| 257 | for (n = 0, j = 0; j < map->n_input_formats; j++) { |
| 258 | if ((map->input_formats[j].layer & id) == id) |
| 259 | formats[n++] = map->input_formats[j].format; |
| 260 | } |
| 261 | |
| 262 | plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY : |
| 263 | DRM_PLANE_TYPE_OVERLAY; |
| 264 | ret = drm_universal_plane_init(drm, &plane->base, crtcs, |
| 265 | &malidp_de_plane_funcs, formats, |
| 266 | n, plane_type, NULL); |
| 267 | if (ret < 0) |
| 268 | goto cleanup; |
| 269 | |
Ville Syrjälä | ea0e1ce | 2016-09-26 19:30:49 +0300 | [diff] [blame] | 270 | /* SMART layer can't be rotated */ |
| 271 | if (id != DE_SMART) { |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 272 | unsigned long flags = DRM_ROTATE_0 | |
| 273 | DRM_ROTATE_90 | |
| 274 | DRM_ROTATE_180 | |
| 275 | DRM_ROTATE_270 | |
| 276 | DRM_REFLECT_X | |
| 277 | DRM_REFLECT_Y; |
Ville Syrjälä | ea0e1ce | 2016-09-26 19:30:49 +0300 | [diff] [blame] | 278 | drm_plane_create_rotation_property(&plane->base, |
| 279 | DRM_ROTATE_0, |
| 280 | flags); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 281 | } |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 282 | |
| 283 | drm_plane_helper_add(&plane->base, |
| 284 | &malidp_de_plane_helper_funcs); |
| 285 | plane->hwdev = malidp->dev; |
| 286 | plane->layer = &map->layers[i]; |
| 287 | } |
| 288 | |
| 289 | kfree(formats); |
| 290 | |
| 291 | return 0; |
| 292 | |
| 293 | cleanup: |
| 294 | malidp_de_planes_destroy(drm); |
| 295 | kfree(formats); |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | void malidp_de_planes_destroy(struct drm_device *drm) |
| 301 | { |
| 302 | struct drm_plane *p, *pt; |
| 303 | |
| 304 | list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) { |
| 305 | drm_plane_cleanup(p); |
| 306 | kfree(p); |
| 307 | } |
| 308 | } |