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) |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 30 | #define LAYER_COMP_MASK (0x3 << 12) |
| 31 | #define LAYER_COMP_PIXEL (0x3 << 12) |
| 32 | #define LAYER_COMP_PLANE (0x2 << 12) |
| 33 | #define MALIDP_LAYER_COMPOSE 0x008 |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 34 | #define MALIDP_LAYER_SIZE 0x00c |
| 35 | #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0) |
| 36 | #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16) |
| 37 | #define MALIDP_LAYER_COMP_SIZE 0x010 |
| 38 | #define MALIDP_LAYER_OFFSET 0x014 |
| 39 | #define MALIDP_LAYER_STRIDE 0x018 |
| 40 | |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 41 | /* |
| 42 | * This 4-entry look-up-table is used to determine the full 8-bit alpha value |
| 43 | * for formats with 1- or 2-bit alpha channels. |
| 44 | * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0% |
| 45 | * opacity for 2-bit formats. |
| 46 | */ |
| 47 | #define MALIDP_ALPHA_LUT 0xffaa5500 |
| 48 | |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 49 | static void malidp_de_plane_destroy(struct drm_plane *plane) |
| 50 | { |
| 51 | struct malidp_plane *mp = to_malidp_plane(plane); |
| 52 | |
| 53 | if (mp->base.fb) |
| 54 | drm_framebuffer_unreference(mp->base.fb); |
| 55 | |
| 56 | drm_plane_helper_disable(plane); |
| 57 | drm_plane_cleanup(plane); |
| 58 | devm_kfree(plane->dev->dev, mp); |
| 59 | } |
| 60 | |
Baoyou Xie | ed8b0c0 | 2016-10-22 17:13:01 +0800 | [diff] [blame] | 61 | static struct |
| 62 | drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 63 | { |
| 64 | struct malidp_plane_state *state, *m_state; |
| 65 | |
| 66 | if (!plane->state) |
| 67 | return NULL; |
| 68 | |
| 69 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
Shailendra Verma | 94d8b9b | 2016-11-11 13:35:00 +0000 | [diff] [blame^] | 70 | if (!state) |
| 71 | return NULL; |
| 72 | |
| 73 | m_state = to_malidp_plane_state(plane->state); |
| 74 | __drm_atomic_helper_plane_duplicate_state(plane, &state->base); |
| 75 | state->rotmem_size = m_state->rotmem_size; |
| 76 | state->format = m_state->format; |
| 77 | state->n_planes = m_state->n_planes; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 78 | |
| 79 | return &state->base; |
| 80 | } |
| 81 | |
Baoyou Xie | ed8b0c0 | 2016-10-22 17:13:01 +0800 | [diff] [blame] | 82 | static void malidp_destroy_plane_state(struct drm_plane *plane, |
| 83 | struct drm_plane_state *state) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 84 | { |
| 85 | struct malidp_plane_state *m_state = to_malidp_plane_state(state); |
| 86 | |
| 87 | __drm_atomic_helper_plane_destroy_state(state); |
| 88 | kfree(m_state); |
| 89 | } |
| 90 | |
| 91 | static const struct drm_plane_funcs malidp_de_plane_funcs = { |
| 92 | .update_plane = drm_atomic_helper_update_plane, |
| 93 | .disable_plane = drm_atomic_helper_disable_plane, |
Liviu Dudau | 2fe1f08 | 2016-10-24 18:35:09 +0100 | [diff] [blame] | 94 | .set_property = drm_atomic_helper_plane_set_property, |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 95 | .destroy = malidp_de_plane_destroy, |
| 96 | .reset = drm_atomic_helper_plane_reset, |
| 97 | .atomic_duplicate_state = malidp_duplicate_plane_state, |
| 98 | .atomic_destroy_state = malidp_destroy_plane_state, |
| 99 | }; |
| 100 | |
| 101 | static int malidp_de_plane_check(struct drm_plane *plane, |
| 102 | struct drm_plane_state *state) |
| 103 | { |
| 104 | struct malidp_plane *mp = to_malidp_plane(plane); |
| 105 | struct malidp_plane_state *ms = to_malidp_plane_state(state); |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame] | 106 | struct drm_framebuffer *fb; |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 107 | int i; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 108 | u32 src_w, src_h; |
| 109 | |
| 110 | if (!state->crtc || !state->fb) |
| 111 | return 0; |
| 112 | |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame] | 113 | fb = state->fb; |
| 114 | |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 115 | ms->format = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id, |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 116 | fb->format->format); |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 117 | if (ms->format == MALIDP_INVALID_FORMAT_ID) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 118 | return -EINVAL; |
| 119 | |
Ville Syrjälä | bcb0b46 | 2016-12-14 23:30:22 +0200 | [diff] [blame] | 120 | ms->n_planes = fb->format->num_planes; |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 121 | for (i = 0; i < ms->n_planes; i++) { |
Brian Starkey | a46a096 | 2016-10-11 15:26:05 +0100 | [diff] [blame] | 122 | if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) { |
| 123 | DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n", |
| 124 | fb->pitches[i], i); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | } |
| 128 | |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 129 | src_w = state->src_w >> 16; |
| 130 | src_h = state->src_h >> 16; |
| 131 | |
| 132 | if ((state->crtc_w > mp->hwdev->max_line_size) || |
| 133 | (state->crtc_h > mp->hwdev->max_line_size) || |
| 134 | (state->crtc_w < mp->hwdev->min_line_size) || |
| 135 | (state->crtc_h < mp->hwdev->min_line_size) || |
| 136 | (state->crtc_w != src_w) || (state->crtc_h != src_h)) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | /* packed RGB888 / BGR888 can't be rotated or flipped */ |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 140 | if (state->rotation != DRM_ROTATE_0 && |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 141 | (fb->format->format == DRM_FORMAT_RGB888 || |
| 142 | fb->format->format == DRM_FORMAT_BGR888)) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | ms->rotmem_size = 0; |
| 146 | if (state->rotation & MALIDP_ROTATED_MASK) { |
| 147 | int val; |
| 148 | |
| 149 | val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h, |
| 150 | state->crtc_w, |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 151 | fb->format->format); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 152 | if (val < 0) |
| 153 | return val; |
| 154 | |
| 155 | ms->rotmem_size = val; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static void malidp_de_plane_update(struct drm_plane *plane, |
| 162 | struct drm_plane_state *old_state) |
| 163 | { |
| 164 | struct drm_gem_cma_object *obj; |
| 165 | struct malidp_plane *mp; |
| 166 | const struct malidp_hw_regmap *map; |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 167 | struct malidp_plane_state *ms = to_malidp_plane_state(plane->state); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 168 | u16 ptr; |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 169 | u32 src_w, src_h, dest_w, dest_h, val; |
| 170 | int i; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 171 | |
| 172 | mp = to_malidp_plane(plane); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 173 | map = &mp->hwdev->map; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 174 | |
| 175 | /* convert src values from Q16 fixed point to integer */ |
| 176 | src_w = plane->state->src_w >> 16; |
| 177 | src_h = plane->state->src_h >> 16; |
| 178 | if (plane->state->rotation & MALIDP_ROTATED_MASK) { |
| 179 | dest_w = plane->state->crtc_h; |
| 180 | dest_h = plane->state->crtc_w; |
| 181 | } else { |
| 182 | dest_w = plane->state->crtc_w; |
| 183 | dest_h = plane->state->crtc_h; |
| 184 | } |
| 185 | |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 186 | malidp_hw_write(mp->hwdev, ms->format, mp->layer->base); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 187 | |
Brian Starkey | 70c94a3 | 2016-10-11 15:26:09 +0100 | [diff] [blame] | 188 | for (i = 0; i < ms->n_planes; i++) { |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 189 | /* calculate the offset for the layer's plane registers */ |
| 190 | ptr = mp->layer->ptr + (i << 4); |
| 191 | |
| 192 | obj = drm_fb_cma_get_gem_obj(plane->state->fb, i); |
| 193 | malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr); |
| 194 | malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4); |
| 195 | malidp_hw_write(mp->hwdev, plane->state->fb->pitches[i], |
| 196 | mp->layer->base + MALIDP_LAYER_STRIDE); |
| 197 | } |
| 198 | |
| 199 | malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h), |
| 200 | mp->layer->base + MALIDP_LAYER_SIZE); |
| 201 | |
| 202 | malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h), |
| 203 | mp->layer->base + MALIDP_LAYER_COMP_SIZE); |
| 204 | |
| 205 | malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) | |
| 206 | LAYER_V_VAL(plane->state->crtc_y), |
| 207 | mp->layer->base + MALIDP_LAYER_OFFSET); |
| 208 | |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 209 | /* first clear the rotation bits */ |
| 210 | val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL); |
| 211 | val &= ~LAYER_ROT_MASK; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 212 | |
| 213 | /* setup the rotation and axis flip bits */ |
| 214 | if (plane->state->rotation & DRM_ROTATE_MASK) |
| 215 | val = ilog2(plane->state->rotation & DRM_ROTATE_MASK) << LAYER_ROT_OFFSET; |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 216 | if (plane->state->rotation & DRM_REFLECT_X) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 217 | val |= LAYER_V_FLIP; |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 218 | if (plane->state->rotation & DRM_REFLECT_Y) |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 219 | val |= LAYER_H_FLIP; |
| 220 | |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 221 | /* |
| 222 | * always enable pixel alpha blending until we have a way to change |
| 223 | * blend modes |
| 224 | */ |
| 225 | val &= ~LAYER_COMP_MASK; |
| 226 | val |= LAYER_COMP_PIXEL; |
| 227 | |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 228 | /* set the 'enable layer' bit */ |
| 229 | val |= LAYER_ENABLE; |
| 230 | |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 231 | malidp_hw_write(mp->hwdev, val, |
| 232 | mp->layer->base + MALIDP_LAYER_CONTROL); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void malidp_de_plane_disable(struct drm_plane *plane, |
| 236 | struct drm_plane_state *state) |
| 237 | { |
| 238 | struct malidp_plane *mp = to_malidp_plane(plane); |
| 239 | |
| 240 | malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE, |
| 241 | mp->layer->base + MALIDP_LAYER_CONTROL); |
| 242 | } |
| 243 | |
| 244 | static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = { |
| 245 | .atomic_check = malidp_de_plane_check, |
| 246 | .atomic_update = malidp_de_plane_update, |
| 247 | .atomic_disable = malidp_de_plane_disable, |
| 248 | }; |
| 249 | |
| 250 | int malidp_de_planes_init(struct drm_device *drm) |
| 251 | { |
| 252 | struct malidp_drm *malidp = drm->dev_private; |
| 253 | const struct malidp_hw_regmap *map = &malidp->dev->map; |
| 254 | struct malidp_plane *plane = NULL; |
| 255 | enum drm_plane_type plane_type; |
| 256 | unsigned long crtcs = 1 << drm->mode_config.num_crtc; |
Brian Starkey | 1580778 | 2016-10-11 15:26:07 +0100 | [diff] [blame] | 257 | unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 | |
| 258 | DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y; |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 259 | u32 *formats; |
| 260 | int ret, i, j, n; |
| 261 | |
| 262 | formats = kcalloc(map->n_input_formats, sizeof(*formats), GFP_KERNEL); |
| 263 | if (!formats) { |
| 264 | ret = -ENOMEM; |
| 265 | goto cleanup; |
| 266 | } |
| 267 | |
| 268 | for (i = 0; i < map->n_layers; i++) { |
| 269 | u8 id = map->layers[i].id; |
| 270 | |
| 271 | plane = kzalloc(sizeof(*plane), GFP_KERNEL); |
| 272 | if (!plane) { |
| 273 | ret = -ENOMEM; |
| 274 | goto cleanup; |
| 275 | } |
| 276 | |
| 277 | /* build the list of DRM supported formats based on the map */ |
| 278 | for (n = 0, j = 0; j < map->n_input_formats; j++) { |
| 279 | if ((map->input_formats[j].layer & id) == id) |
| 280 | formats[n++] = map->input_formats[j].format; |
| 281 | } |
| 282 | |
| 283 | plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY : |
| 284 | DRM_PLANE_TYPE_OVERLAY; |
| 285 | ret = drm_universal_plane_init(drm, &plane->base, crtcs, |
| 286 | &malidp_de_plane_funcs, formats, |
| 287 | n, plane_type, NULL); |
| 288 | if (ret < 0) |
| 289 | goto cleanup; |
| 290 | |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 291 | drm_plane_helper_add(&plane->base, |
| 292 | &malidp_de_plane_helper_funcs); |
| 293 | plane->hwdev = malidp->dev; |
| 294 | plane->layer = &map->layers[i]; |
Brian Starkey | 1580778 | 2016-10-11 15:26:07 +0100 | [diff] [blame] | 295 | |
| 296 | /* Skip the features which the SMART layer doesn't have */ |
| 297 | if (id == DE_SMART) |
| 298 | continue; |
| 299 | |
| 300 | drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags); |
Brian Starkey | c57eb71 | 2016-10-11 15:26:08 +0100 | [diff] [blame] | 301 | malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT, |
| 302 | plane->layer->base + MALIDP_LAYER_COMPOSE); |
Liviu Dudau | ad49f86 | 2016-03-07 10:00:53 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | kfree(formats); |
| 306 | |
| 307 | return 0; |
| 308 | |
| 309 | cleanup: |
| 310 | malidp_de_planes_destroy(drm); |
| 311 | kfree(formats); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | void malidp_de_planes_destroy(struct drm_device *drm) |
| 317 | { |
| 318 | struct drm_plane *p, *pt; |
| 319 | |
| 320 | list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) { |
| 321 | drm_plane_cleanup(p); |
| 322 | kfree(p); |
| 323 | } |
| 324 | } |