Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Broadcom |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * DOC: VC4 plane module |
| 11 | * |
| 12 | * Each DRM plane is a layer of pixels being scanned out by the HVS. |
| 13 | * |
| 14 | * At atomic modeset check time, we compute the HVS display element |
| 15 | * state that would be necessary for displaying the plane (giving us a |
| 16 | * chance to figure out if a plane configuration is invalid), then at |
| 17 | * atomic flush time the CRTC will ask us to write our element state |
| 18 | * into the region of the HVS that it has allocated for us. |
| 19 | */ |
| 20 | |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 21 | #include <drm/drm_atomic.h> |
| 22 | #include <drm/drm_atomic_helper.h> |
| 23 | #include <drm/drm_fb_cma_helper.h> |
| 24 | #include <drm/drm_plane_helper.h> |
| 25 | |
Boris Brezillon | b9f1925 | 2017-10-19 14:57:48 +0200 | [diff] [blame] | 26 | #include "uapi/drm/vc4_drm.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 27 | #include "vc4_drv.h" |
| 28 | #include "vc4_regs.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 29 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 30 | enum vc4_scaling_mode { |
| 31 | VC4_SCALING_NONE, |
| 32 | VC4_SCALING_TPZ, |
| 33 | VC4_SCALING_PPF, |
| 34 | }; |
| 35 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 36 | struct vc4_plane_state { |
| 37 | struct drm_plane_state base; |
Eric Anholt | f427fb1 | 2015-12-28 14:14:09 -0800 | [diff] [blame] | 38 | /* System memory copy of the display list for this element, computed |
| 39 | * at atomic_check time. |
| 40 | */ |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 41 | u32 *dlist; |
Eric Anholt | f427fb1 | 2015-12-28 14:14:09 -0800 | [diff] [blame] | 42 | u32 dlist_size; /* Number of dwords allocated for the display list */ |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 43 | u32 dlist_count; /* Number of used dwords in the display list. */ |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 44 | |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 45 | /* Offset in the dlist to various words, for pageflip or |
| 46 | * cursor updates. |
| 47 | */ |
| 48 | u32 pos0_offset; |
| 49 | u32 pos2_offset; |
| 50 | u32 ptr0_offset; |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 51 | |
| 52 | /* Offset where the plane's dlist was last stored in the |
Eric Anholt | f427fb1 | 2015-12-28 14:14:09 -0800 | [diff] [blame] | 53 | * hardware at vc4_crtc_atomic_flush() time. |
| 54 | */ |
Eric Anholt | 17eac75 | 2015-12-28 14:14:57 -0800 | [diff] [blame] | 55 | u32 __iomem *hw_dlist; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 56 | |
| 57 | /* Clipped coordinates of the plane on the display. */ |
| 58 | int crtc_x, crtc_y, crtc_w, crtc_h; |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 59 | /* Clipped area being scanned from in the FB. */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 60 | u32 src_x, src_y; |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 61 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 62 | u32 src_w[2], src_h[2]; |
| 63 | |
| 64 | /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */ |
| 65 | enum vc4_scaling_mode x_scaling[2], y_scaling[2]; |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 66 | bool is_unity; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 67 | bool is_yuv; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 68 | |
| 69 | /* Offset to start scanning out from the start of the plane's |
| 70 | * BO. |
| 71 | */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 72 | u32 offsets[3]; |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 73 | |
| 74 | /* Our allocation in LBM for temporary storage during scaling. */ |
| 75 | struct drm_mm_node lbm; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static inline struct vc4_plane_state * |
| 79 | to_vc4_plane_state(struct drm_plane_state *state) |
| 80 | { |
| 81 | return (struct vc4_plane_state *)state; |
| 82 | } |
| 83 | |
| 84 | static const struct hvs_format { |
| 85 | u32 drm; /* DRM_FORMAT_* */ |
| 86 | u32 hvs; /* HVS_FORMAT_* */ |
| 87 | u32 pixel_order; |
| 88 | bool has_alpha; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 89 | bool flip_cbcr; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 90 | } hvs_formats[] = { |
| 91 | { |
| 92 | .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888, |
| 93 | .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false, |
| 94 | }, |
| 95 | { |
| 96 | .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888, |
| 97 | .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true, |
| 98 | }, |
Eric Anholt | fe4cd84 | 2015-10-20 13:59:15 +0100 | [diff] [blame] | 99 | { |
Rob Herring | 9397776 | 2016-06-09 16:19:25 -0500 | [diff] [blame] | 100 | .drm = DRM_FORMAT_ABGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888, |
| 101 | .pixel_order = HVS_PIXEL_ORDER_ARGB, .has_alpha = true, |
| 102 | }, |
| 103 | { |
| 104 | .drm = DRM_FORMAT_XBGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888, |
| 105 | .pixel_order = HVS_PIXEL_ORDER_ARGB, .has_alpha = false, |
| 106 | }, |
| 107 | { |
Eric Anholt | fe4cd84 | 2015-10-20 13:59:15 +0100 | [diff] [blame] | 108 | .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565, |
| 109 | .pixel_order = HVS_PIXEL_ORDER_XRGB, .has_alpha = false, |
| 110 | }, |
| 111 | { |
| 112 | .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565, |
| 113 | .pixel_order = HVS_PIXEL_ORDER_XBGR, .has_alpha = false, |
| 114 | }, |
| 115 | { |
| 116 | .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551, |
| 117 | .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true, |
| 118 | }, |
| 119 | { |
| 120 | .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551, |
| 121 | .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false, |
| 122 | }, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 123 | { |
Dave Stevenson | 88f8156 | 2017-11-16 14:22:29 +0000 | [diff] [blame^] | 124 | .drm = DRM_FORMAT_RGB888, .hvs = HVS_PIXEL_FORMAT_RGB888, |
| 125 | .pixel_order = HVS_PIXEL_ORDER_XRGB, .has_alpha = false, |
| 126 | }, |
| 127 | { |
| 128 | .drm = DRM_FORMAT_BGR888, .hvs = HVS_PIXEL_FORMAT_RGB888, |
| 129 | .pixel_order = HVS_PIXEL_ORDER_XBGR, .has_alpha = false, |
| 130 | }, |
| 131 | { |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 132 | .drm = DRM_FORMAT_YUV422, |
| 133 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE, |
| 134 | }, |
| 135 | { |
| 136 | .drm = DRM_FORMAT_YVU422, |
| 137 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE, |
| 138 | .flip_cbcr = true, |
| 139 | }, |
| 140 | { |
| 141 | .drm = DRM_FORMAT_YUV420, |
| 142 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE, |
| 143 | }, |
| 144 | { |
| 145 | .drm = DRM_FORMAT_YVU420, |
| 146 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE, |
| 147 | .flip_cbcr = true, |
| 148 | }, |
| 149 | { |
| 150 | .drm = DRM_FORMAT_NV12, |
| 151 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE, |
| 152 | }, |
| 153 | { |
| 154 | .drm = DRM_FORMAT_NV16, |
| 155 | .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE, |
| 156 | }, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | static const struct hvs_format *vc4_get_hvs_format(u32 drm_format) |
| 160 | { |
| 161 | unsigned i; |
| 162 | |
| 163 | for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) { |
| 164 | if (hvs_formats[i].drm == drm_format) |
| 165 | return &hvs_formats[i]; |
| 166 | } |
| 167 | |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 171 | static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst) |
| 172 | { |
| 173 | if (dst > src) |
| 174 | return VC4_SCALING_PPF; |
| 175 | else if (dst < src) |
| 176 | return VC4_SCALING_TPZ; |
| 177 | else |
| 178 | return VC4_SCALING_NONE; |
| 179 | } |
| 180 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 181 | static bool plane_enabled(struct drm_plane_state *state) |
| 182 | { |
| 183 | return state->fb && state->crtc; |
| 184 | } |
| 185 | |
kbuild test robot | 91276ae | 2015-10-22 11:12:26 +0800 | [diff] [blame] | 186 | static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 187 | { |
| 188 | struct vc4_plane_state *vc4_state; |
| 189 | |
| 190 | if (WARN_ON(!plane->state)) |
| 191 | return NULL; |
| 192 | |
| 193 | vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL); |
| 194 | if (!vc4_state) |
| 195 | return NULL; |
| 196 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 197 | memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm)); |
| 198 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 199 | __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base); |
| 200 | |
| 201 | if (vc4_state->dlist) { |
| 202 | vc4_state->dlist = kmemdup(vc4_state->dlist, |
| 203 | vc4_state->dlist_count * 4, |
| 204 | GFP_KERNEL); |
| 205 | if (!vc4_state->dlist) { |
| 206 | kfree(vc4_state); |
| 207 | return NULL; |
| 208 | } |
| 209 | vc4_state->dlist_size = vc4_state->dlist_count; |
| 210 | } |
| 211 | |
| 212 | return &vc4_state->base; |
| 213 | } |
| 214 | |
kbuild test robot | 91276ae | 2015-10-22 11:12:26 +0800 | [diff] [blame] | 215 | static void vc4_plane_destroy_state(struct drm_plane *plane, |
| 216 | struct drm_plane_state *state) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 217 | { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 218 | struct vc4_dev *vc4 = to_vc4_dev(plane->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 219 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
| 220 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 221 | if (vc4_state->lbm.allocated) { |
| 222 | unsigned long irqflags; |
| 223 | |
| 224 | spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags); |
| 225 | drm_mm_remove_node(&vc4_state->lbm); |
| 226 | spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags); |
| 227 | } |
| 228 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 229 | kfree(vc4_state->dlist); |
Daniel Vetter | 2f70169 | 2016-05-09 16:34:10 +0200 | [diff] [blame] | 230 | __drm_atomic_helper_plane_destroy_state(&vc4_state->base); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 231 | kfree(state); |
| 232 | } |
| 233 | |
| 234 | /* Called during init to allocate the plane's atomic state. */ |
kbuild test robot | 91276ae | 2015-10-22 11:12:26 +0800 | [diff] [blame] | 235 | static void vc4_plane_reset(struct drm_plane *plane) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 236 | { |
| 237 | struct vc4_plane_state *vc4_state; |
| 238 | |
| 239 | WARN_ON(plane->state); |
| 240 | |
| 241 | vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL); |
| 242 | if (!vc4_state) |
| 243 | return; |
| 244 | |
| 245 | plane->state = &vc4_state->base; |
| 246 | vc4_state->base.plane = plane; |
| 247 | } |
| 248 | |
| 249 | static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val) |
| 250 | { |
| 251 | if (vc4_state->dlist_count == vc4_state->dlist_size) { |
| 252 | u32 new_size = max(4u, vc4_state->dlist_count * 2); |
| 253 | u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL); |
| 254 | |
| 255 | if (!new_dlist) |
| 256 | return; |
| 257 | memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4); |
| 258 | |
| 259 | kfree(vc4_state->dlist); |
| 260 | vc4_state->dlist = new_dlist; |
| 261 | vc4_state->dlist_size = new_size; |
| 262 | } |
| 263 | |
| 264 | vc4_state->dlist[vc4_state->dlist_count++] = val; |
| 265 | } |
| 266 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 267 | /* Returns the scl0/scl1 field based on whether the dimensions need to |
| 268 | * be up/down/non-scaled. |
| 269 | * |
| 270 | * This is a replication of a table from the spec. |
| 271 | */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 272 | static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 273 | { |
| 274 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 275 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 276 | switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 277 | case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF: |
| 278 | return SCALER_CTL0_SCL_H_PPF_V_PPF; |
| 279 | case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF: |
| 280 | return SCALER_CTL0_SCL_H_TPZ_V_PPF; |
| 281 | case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ: |
| 282 | return SCALER_CTL0_SCL_H_PPF_V_TPZ; |
| 283 | case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ: |
| 284 | return SCALER_CTL0_SCL_H_TPZ_V_TPZ; |
| 285 | case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE: |
| 286 | return SCALER_CTL0_SCL_H_PPF_V_NONE; |
| 287 | case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF: |
| 288 | return SCALER_CTL0_SCL_H_NONE_V_PPF; |
| 289 | case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ: |
| 290 | return SCALER_CTL0_SCL_H_NONE_V_TPZ; |
| 291 | case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE: |
| 292 | return SCALER_CTL0_SCL_H_TPZ_V_NONE; |
| 293 | default: |
| 294 | case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE: |
| 295 | /* The unity case is independently handled by |
| 296 | * SCALER_CTL0_UNITY. |
| 297 | */ |
| 298 | return 0; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state) |
| 303 | { |
| 304 | struct drm_plane *plane = state->plane; |
| 305 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 306 | struct drm_framebuffer *fb = state->fb; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 307 | struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 308 | u32 subpixel_src_mask = (1 << 16) - 1; |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 309 | u32 format = fb->format->format; |
Ville Syrjälä | bcb0b46 | 2016-12-14 23:30:22 +0200 | [diff] [blame] | 310 | int num_planes = fb->format->num_planes; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 311 | u32 h_subsample = 1; |
| 312 | u32 v_subsample = 1; |
| 313 | int i; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 314 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 315 | for (i = 0; i < num_planes; i++) |
| 316 | vc4_state->offsets[i] = bo->paddr + fb->offsets[i]; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 317 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 318 | /* We don't support subpixel source positioning for scaling. */ |
| 319 | if ((state->src_x & subpixel_src_mask) || |
| 320 | (state->src_y & subpixel_src_mask) || |
| 321 | (state->src_w & subpixel_src_mask) || |
| 322 | (state->src_h & subpixel_src_mask)) { |
Eric Anholt | bf893ac | 2015-10-23 10:36:27 +0100 | [diff] [blame] | 323 | return -EINVAL; |
| 324 | } |
| 325 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 326 | vc4_state->src_x = state->src_x >> 16; |
| 327 | vc4_state->src_y = state->src_y >> 16; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 328 | vc4_state->src_w[0] = state->src_w >> 16; |
| 329 | vc4_state->src_h[0] = state->src_h >> 16; |
Eric Anholt | f863e35 | 2015-12-28 14:45:25 -0800 | [diff] [blame] | 330 | |
| 331 | vc4_state->crtc_x = state->crtc_x; |
| 332 | vc4_state->crtc_y = state->crtc_y; |
| 333 | vc4_state->crtc_w = state->crtc_w; |
| 334 | vc4_state->crtc_h = state->crtc_h; |
| 335 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 336 | vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0], |
| 337 | vc4_state->crtc_w); |
| 338 | vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0], |
| 339 | vc4_state->crtc_h); |
| 340 | |
| 341 | if (num_planes > 1) { |
| 342 | vc4_state->is_yuv = true; |
| 343 | |
| 344 | h_subsample = drm_format_horz_chroma_subsampling(format); |
| 345 | v_subsample = drm_format_vert_chroma_subsampling(format); |
| 346 | vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample; |
| 347 | vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample; |
| 348 | |
| 349 | vc4_state->x_scaling[1] = |
| 350 | vc4_get_scaling_mode(vc4_state->src_w[1], |
| 351 | vc4_state->crtc_w); |
| 352 | vc4_state->y_scaling[1] = |
| 353 | vc4_get_scaling_mode(vc4_state->src_h[1], |
| 354 | vc4_state->crtc_h); |
| 355 | |
| 356 | /* YUV conversion requires that scaling be enabled, |
| 357 | * even on a plane that's otherwise 1:1. Choose TPZ |
| 358 | * for simplicity. |
| 359 | */ |
| 360 | if (vc4_state->x_scaling[0] == VC4_SCALING_NONE) |
| 361 | vc4_state->x_scaling[0] = VC4_SCALING_TPZ; |
| 362 | if (vc4_state->y_scaling[0] == VC4_SCALING_NONE) |
| 363 | vc4_state->y_scaling[0] = VC4_SCALING_TPZ; |
| 364 | } |
| 365 | |
| 366 | vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE && |
| 367 | vc4_state->y_scaling[0] == VC4_SCALING_NONE && |
| 368 | vc4_state->x_scaling[1] == VC4_SCALING_NONE && |
| 369 | vc4_state->y_scaling[1] == VC4_SCALING_NONE); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 370 | |
| 371 | /* No configuring scaling on the cursor plane, since it gets |
| 372 | non-vblank-synced updates, and scaling requires requires |
| 373 | LBM changes which have to be vblank-synced. |
| 374 | */ |
| 375 | if (plane->type == DRM_PLANE_TYPE_CURSOR && !vc4_state->is_unity) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | /* Clamp the on-screen start x/y to 0. The hardware doesn't |
| 379 | * support negative y, and negative x wastes bandwidth. |
| 380 | */ |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 381 | if (vc4_state->crtc_x < 0) { |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 382 | for (i = 0; i < num_planes; i++) { |
Ville Syrjälä | 353c859 | 2016-12-14 23:30:57 +0200 | [diff] [blame] | 383 | u32 cpp = fb->format->cpp[i]; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 384 | u32 subs = ((i == 0) ? 1 : h_subsample); |
| 385 | |
| 386 | vc4_state->offsets[i] += (cpp * |
| 387 | (-vc4_state->crtc_x) / subs); |
| 388 | } |
| 389 | vc4_state->src_w[0] += vc4_state->crtc_x; |
| 390 | vc4_state->src_w[1] += vc4_state->crtc_x / h_subsample; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 391 | vc4_state->crtc_x = 0; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 394 | if (vc4_state->crtc_y < 0) { |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 395 | for (i = 0; i < num_planes; i++) { |
| 396 | u32 subs = ((i == 0) ? 1 : v_subsample); |
| 397 | |
| 398 | vc4_state->offsets[i] += (fb->pitches[i] * |
| 399 | (-vc4_state->crtc_y) / subs); |
| 400 | } |
| 401 | vc4_state->src_h[0] += vc4_state->crtc_y; |
| 402 | vc4_state->src_h[1] += vc4_state->crtc_y / v_subsample; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 403 | vc4_state->crtc_y = 0; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 409 | static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst) |
| 410 | { |
| 411 | u32 scale, recip; |
| 412 | |
| 413 | scale = (1 << 16) * src / dst; |
| 414 | |
| 415 | /* The specs note that while the reciprocal would be defined |
| 416 | * as (1<<32)/scale, ~0 is close enough. |
| 417 | */ |
| 418 | recip = ~0 / scale; |
| 419 | |
| 420 | vc4_dlist_write(vc4_state, |
| 421 | VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) | |
| 422 | VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE)); |
| 423 | vc4_dlist_write(vc4_state, |
| 424 | VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP)); |
| 425 | } |
| 426 | |
| 427 | static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst) |
| 428 | { |
| 429 | u32 scale = (1 << 16) * src / dst; |
| 430 | |
| 431 | vc4_dlist_write(vc4_state, |
| 432 | SCALER_PPF_AGC | |
| 433 | VC4_SET_FIELD(scale, SCALER_PPF_SCALE) | |
| 434 | VC4_SET_FIELD(0, SCALER_PPF_IPHASE)); |
| 435 | } |
| 436 | |
| 437 | static u32 vc4_lbm_size(struct drm_plane_state *state) |
| 438 | { |
| 439 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
| 440 | /* This is the worst case number. One of the two sizes will |
| 441 | * be used depending on the scaling configuration. |
| 442 | */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 443 | u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 444 | u32 lbm; |
| 445 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 446 | if (!vc4_state->is_yuv) { |
| 447 | if (vc4_state->is_unity) |
| 448 | return 0; |
| 449 | else if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ) |
| 450 | lbm = pix_per_line * 8; |
| 451 | else { |
| 452 | /* In special cases, this multiplier might be 12. */ |
| 453 | lbm = pix_per_line * 16; |
| 454 | } |
| 455 | } else { |
| 456 | /* There are cases for this going down to a multiplier |
| 457 | * of 2, but according to the firmware source, the |
| 458 | * table in the docs is somewhat wrong. |
| 459 | */ |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 460 | lbm = pix_per_line * 16; |
| 461 | } |
| 462 | |
| 463 | lbm = roundup(lbm, 32); |
| 464 | |
| 465 | return lbm; |
| 466 | } |
| 467 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 468 | static void vc4_write_scaling_parameters(struct drm_plane_state *state, |
| 469 | int channel) |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 470 | { |
| 471 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
| 472 | |
| 473 | /* Ch0 H-PPF Word 0: Scaling Parameters */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 474 | if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 475 | vc4_write_ppf(vc4_state, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 476 | vc4_state->src_w[channel], vc4_state->crtc_w); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 480 | if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 481 | vc4_write_ppf(vc4_state, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 482 | vc4_state->src_h[channel], vc4_state->crtc_h); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 483 | vc4_dlist_write(vc4_state, 0xc0c0c0c0); |
| 484 | } |
| 485 | |
| 486 | /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 487 | if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 488 | vc4_write_tpz(vc4_state, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 489 | vc4_state->src_w[channel], vc4_state->crtc_w); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 493 | if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 494 | vc4_write_tpz(vc4_state, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 495 | vc4_state->src_h[channel], vc4_state->crtc_h); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 496 | vc4_dlist_write(vc4_state, 0xc0c0c0c0); |
| 497 | } |
| 498 | } |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 499 | |
| 500 | /* Writes out a full display list for an active plane to the plane's |
| 501 | * private dlist state. |
| 502 | */ |
| 503 | static int vc4_plane_mode_set(struct drm_plane *plane, |
| 504 | struct drm_plane_state *state) |
| 505 | { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 506 | struct vc4_dev *vc4 = to_vc4_dev(plane->dev); |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 507 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
| 508 | struct drm_framebuffer *fb = state->fb; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 509 | u32 ctl0_offset = vc4_state->dlist_count; |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 510 | const struct hvs_format *format = vc4_get_hvs_format(fb->format->format); |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 511 | int num_planes = drm_format_num_planes(format->drm); |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 512 | u32 scl0, scl1, pitch0; |
| 513 | u32 lbm_size, tiling; |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 514 | unsigned long irqflags; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 515 | int ret, i; |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 516 | |
| 517 | ret = vc4_plane_setup_clipping_and_scaling(state); |
| 518 | if (ret) |
| 519 | return ret; |
| 520 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 521 | /* Allocate the LBM memory that the HVS will use for temporary |
| 522 | * storage due to our scaling/format conversion. |
| 523 | */ |
| 524 | lbm_size = vc4_lbm_size(state); |
| 525 | if (lbm_size) { |
| 526 | if (!vc4_state->lbm.allocated) { |
| 527 | spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags); |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 528 | ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm, |
| 529 | &vc4_state->lbm, |
| 530 | lbm_size, 32, 0, 0); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 531 | spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags); |
| 532 | } else { |
| 533 | WARN_ON_ONCE(lbm_size != vc4_state->lbm.size); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (ret) |
| 538 | return ret; |
| 539 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 540 | /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB |
| 541 | * and 4:4:4, scl1 should be set to scl0 so both channels of |
| 542 | * the scaler do the same thing. For YUV, the Y plane needs |
| 543 | * to be put in channel 1 and Cb/Cr in channel 0, so we swap |
| 544 | * the scl fields here. |
| 545 | */ |
| 546 | if (num_planes == 1) { |
| 547 | scl0 = vc4_get_scl_field(state, 1); |
| 548 | scl1 = scl0; |
| 549 | } else { |
| 550 | scl0 = vc4_get_scl_field(state, 1); |
| 551 | scl1 = vc4_get_scl_field(state, 0); |
| 552 | } |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 553 | |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 554 | switch (fb->modifier) { |
| 555 | case DRM_FORMAT_MOD_LINEAR: |
| 556 | tiling = SCALER_CTL0_TILING_LINEAR; |
| 557 | pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH); |
| 558 | break; |
Eric Anholt | 652badb | 2017-09-27 12:32:09 -0700 | [diff] [blame] | 559 | |
| 560 | case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: { |
| 561 | /* For T-tiled, the FB pitch is "how many bytes from |
| 562 | * one row to the next, such that pitch * tile_h == |
| 563 | * tile_size * tiles_per_row." |
| 564 | */ |
| 565 | u32 tile_size_shift = 12; /* T tiles are 4kb */ |
| 566 | u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */ |
| 567 | u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift); |
| 568 | |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 569 | tiling = SCALER_CTL0_TILING_256B_OR_T; |
| 570 | |
Eric Anholt | 652badb | 2017-09-27 12:32:09 -0700 | [diff] [blame] | 571 | pitch0 = (VC4_SET_FIELD(0, SCALER_PITCH0_TILE_Y_OFFSET) | |
| 572 | VC4_SET_FIELD(0, SCALER_PITCH0_TILE_WIDTH_L) | |
| 573 | VC4_SET_FIELD(tiles_w, SCALER_PITCH0_TILE_WIDTH_R)); |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 574 | break; |
Eric Anholt | 652badb | 2017-09-27 12:32:09 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 577 | default: |
| 578 | DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx", |
| 579 | (long long)fb->modifier); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 583 | /* Control word */ |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 584 | vc4_dlist_write(vc4_state, |
| 585 | SCALER_CTL0_VALID | |
| 586 | (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) | |
| 587 | (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) | |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 588 | VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 589 | (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 590 | VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) | |
| 591 | VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 592 | |
| 593 | /* Position Word 0: Image Positions and Alpha Value */ |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 594 | vc4_state->pos0_offset = vc4_state->dlist_count; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 595 | vc4_dlist_write(vc4_state, |
| 596 | VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) | |
Eric Anholt | 5c67999 | 2015-12-28 14:34:44 -0800 | [diff] [blame] | 597 | VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) | |
| 598 | VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 599 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 600 | /* Position Word 1: Scaled Image Dimensions. */ |
| 601 | if (!vc4_state->is_unity) { |
| 602 | vc4_dlist_write(vc4_state, |
| 603 | VC4_SET_FIELD(vc4_state->crtc_w, |
| 604 | SCALER_POS1_SCL_WIDTH) | |
| 605 | VC4_SET_FIELD(vc4_state->crtc_h, |
| 606 | SCALER_POS1_SCL_HEIGHT)); |
| 607 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 608 | |
| 609 | /* Position Word 2: Source Image Size, Alpha Mode */ |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 610 | vc4_state->pos2_offset = vc4_state->dlist_count; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 611 | vc4_dlist_write(vc4_state, |
| 612 | VC4_SET_FIELD(format->has_alpha ? |
| 613 | SCALER_POS2_ALPHA_MODE_PIPELINE : |
| 614 | SCALER_POS2_ALPHA_MODE_FIXED, |
| 615 | SCALER_POS2_ALPHA_MODE) | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 616 | VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) | |
| 617 | VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 618 | |
| 619 | /* Position Word 3: Context. Written by the HVS. */ |
| 620 | vc4_dlist_write(vc4_state, 0xc0c0c0c0); |
| 621 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 622 | |
| 623 | /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers |
| 624 | * |
| 625 | * The pointers may be any byte address. |
| 626 | */ |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 627 | vc4_state->ptr0_offset = vc4_state->dlist_count; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 628 | if (!format->flip_cbcr) { |
| 629 | for (i = 0; i < num_planes; i++) |
| 630 | vc4_dlist_write(vc4_state, vc4_state->offsets[i]); |
| 631 | } else { |
| 632 | WARN_ON_ONCE(num_planes != 3); |
| 633 | vc4_dlist_write(vc4_state, vc4_state->offsets[0]); |
| 634 | vc4_dlist_write(vc4_state, vc4_state->offsets[2]); |
| 635 | vc4_dlist_write(vc4_state, vc4_state->offsets[1]); |
| 636 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 637 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 638 | /* Pointer Context Word 0/1/2: Written by the HVS */ |
| 639 | for (i = 0; i < num_planes; i++) |
| 640 | vc4_dlist_write(vc4_state, 0xc0c0c0c0); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 641 | |
Eric Anholt | 98830d91 | 2017-06-07 17:13:35 -0700 | [diff] [blame] | 642 | /* Pitch word 0 */ |
| 643 | vc4_dlist_write(vc4_state, pitch0); |
| 644 | |
| 645 | /* Pitch word 1/2 */ |
| 646 | for (i = 1; i < num_planes; i++) { |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 647 | vc4_dlist_write(vc4_state, |
| 648 | VC4_SET_FIELD(fb->pitches[i], SCALER_SRC_PITCH)); |
| 649 | } |
| 650 | |
| 651 | /* Colorspace conversion words */ |
| 652 | if (vc4_state->is_yuv) { |
| 653 | vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5); |
| 654 | vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5); |
| 655 | vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5); |
| 656 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 657 | |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 658 | if (!vc4_state->is_unity) { |
| 659 | /* LBM Base Address. */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 660 | if (vc4_state->y_scaling[0] != VC4_SCALING_NONE || |
| 661 | vc4_state->y_scaling[1] != VC4_SCALING_NONE) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 662 | vc4_dlist_write(vc4_state, vc4_state->lbm.start); |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 663 | } |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 664 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 665 | if (num_planes > 1) { |
| 666 | /* Emit Cb/Cr as channel 0 and Y as channel |
| 667 | * 1. This matches how we set up scl0/scl1 |
| 668 | * above. |
| 669 | */ |
| 670 | vc4_write_scaling_parameters(state, 1); |
| 671 | } |
| 672 | vc4_write_scaling_parameters(state, 0); |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 673 | |
| 674 | /* If any PPF setup was done, then all the kernel |
| 675 | * pointers get uploaded. |
| 676 | */ |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 677 | if (vc4_state->x_scaling[0] == VC4_SCALING_PPF || |
| 678 | vc4_state->y_scaling[0] == VC4_SCALING_PPF || |
| 679 | vc4_state->x_scaling[1] == VC4_SCALING_PPF || |
| 680 | vc4_state->y_scaling[1] == VC4_SCALING_PPF) { |
Eric Anholt | 21af94c | 2015-10-20 16:06:57 +0100 | [diff] [blame] | 681 | u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start, |
| 682 | SCALER_PPF_KERNEL_OFFSET); |
| 683 | |
| 684 | /* HPPF plane 0 */ |
| 685 | vc4_dlist_write(vc4_state, kernel); |
| 686 | /* VPPF plane 0 */ |
| 687 | vc4_dlist_write(vc4_state, kernel); |
| 688 | /* HPPF plane 1 */ |
| 689 | vc4_dlist_write(vc4_state, kernel); |
| 690 | /* VPPF plane 1 */ |
| 691 | vc4_dlist_write(vc4_state, kernel); |
| 692 | } |
| 693 | } |
| 694 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 695 | vc4_state->dlist[ctl0_offset] |= |
| 696 | VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* If a modeset involves changing the setup of a plane, the atomic |
| 702 | * infrastructure will call this to validate a proposed plane setup. |
| 703 | * However, if a plane isn't getting updated, this (and the |
| 704 | * corresponding vc4_plane_atomic_update) won't get called. Thus, we |
| 705 | * compute the dlist here and have all active plane dlists get updated |
| 706 | * in the CRTC's flush. |
| 707 | */ |
| 708 | static int vc4_plane_atomic_check(struct drm_plane *plane, |
| 709 | struct drm_plane_state *state) |
| 710 | { |
| 711 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(state); |
| 712 | |
| 713 | vc4_state->dlist_count = 0; |
| 714 | |
| 715 | if (plane_enabled(state)) |
| 716 | return vc4_plane_mode_set(plane, state); |
| 717 | else |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static void vc4_plane_atomic_update(struct drm_plane *plane, |
| 722 | struct drm_plane_state *old_state) |
| 723 | { |
| 724 | /* No contents here. Since we don't know where in the CRTC's |
| 725 | * dlist we should be stored, our dlist is uploaded to the |
| 726 | * hardware with vc4_plane_write_dlist() at CRTC atomic_flush |
| 727 | * time. |
| 728 | */ |
| 729 | } |
| 730 | |
| 731 | u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist) |
| 732 | { |
| 733 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state); |
| 734 | int i; |
| 735 | |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 736 | vc4_state->hw_dlist = dlist; |
| 737 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 738 | /* Can't memcpy_toio() because it needs to be 32-bit writes. */ |
| 739 | for (i = 0; i < vc4_state->dlist_count; i++) |
| 740 | writel(vc4_state->dlist[i], &dlist[i]); |
| 741 | |
| 742 | return vc4_state->dlist_count; |
| 743 | } |
| 744 | |
Daniel Vetter | 2f196b7 | 2016-06-02 16:21:44 +0200 | [diff] [blame] | 745 | u32 vc4_plane_dlist_size(const struct drm_plane_state *state) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 746 | { |
Daniel Vetter | 2f196b7 | 2016-06-02 16:21:44 +0200 | [diff] [blame] | 747 | const struct vc4_plane_state *vc4_state = |
| 748 | container_of(state, typeof(*vc4_state), base); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 749 | |
| 750 | return vc4_state->dlist_count; |
| 751 | } |
| 752 | |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 753 | /* Updates the plane to immediately (well, once the FIFO needs |
| 754 | * refilling) scan out from at a new framebuffer. |
| 755 | */ |
| 756 | void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb) |
| 757 | { |
| 758 | struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state); |
| 759 | struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0); |
| 760 | uint32_t addr; |
| 761 | |
| 762 | /* We're skipping the address adjustment for negative origin, |
| 763 | * because this is only called on the primary plane. |
| 764 | */ |
| 765 | WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0); |
| 766 | addr = bo->paddr + fb->offsets[0]; |
| 767 | |
| 768 | /* Write the new address into the hardware immediately. The |
| 769 | * scanout will start from this address as soon as the FIFO |
| 770 | * needs to refill with pixels. |
| 771 | */ |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 772 | writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]); |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 773 | |
| 774 | /* Also update the CPU-side dlist copy, so that any later |
| 775 | * atomic updates that don't do a new modeset on our plane |
| 776 | * also use our updated address. |
| 777 | */ |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 778 | vc4_state->dlist[vc4_state->ptr0_offset] = addr; |
Eric Anholt | b501bac | 2015-11-30 12:34:01 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Eric Anholt | 334dbd6 | 2017-06-21 11:49:59 -0700 | [diff] [blame] | 781 | static int vc4_prepare_fb(struct drm_plane *plane, |
| 782 | struct drm_plane_state *state) |
| 783 | { |
| 784 | struct vc4_bo *bo; |
| 785 | struct dma_fence *fence; |
Boris Brezillon | b9f1925 | 2017-10-19 14:57:48 +0200 | [diff] [blame] | 786 | int ret; |
Eric Anholt | 334dbd6 | 2017-06-21 11:49:59 -0700 | [diff] [blame] | 787 | |
| 788 | if ((plane->state->fb == state->fb) || !state->fb) |
| 789 | return 0; |
| 790 | |
| 791 | bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base); |
Boris Brezillon | b9f1925 | 2017-10-19 14:57:48 +0200 | [diff] [blame] | 792 | |
| 793 | ret = vc4_bo_inc_usecnt(bo); |
| 794 | if (ret) |
| 795 | return ret; |
| 796 | |
Eric Anholt | 334dbd6 | 2017-06-21 11:49:59 -0700 | [diff] [blame] | 797 | fence = reservation_object_get_excl_rcu(bo->resv); |
| 798 | drm_atomic_set_fence_for_plane(state, fence); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Boris Brezillon | b9f1925 | 2017-10-19 14:57:48 +0200 | [diff] [blame] | 803 | static void vc4_cleanup_fb(struct drm_plane *plane, |
| 804 | struct drm_plane_state *state) |
| 805 | { |
| 806 | struct vc4_bo *bo; |
| 807 | |
| 808 | if (plane->state->fb == state->fb || !state->fb) |
| 809 | return; |
| 810 | |
| 811 | bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base); |
| 812 | vc4_bo_dec_usecnt(bo); |
| 813 | } |
| 814 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 815 | static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 816 | .atomic_check = vc4_plane_atomic_check, |
| 817 | .atomic_update = vc4_plane_atomic_update, |
Eric Anholt | 334dbd6 | 2017-06-21 11:49:59 -0700 | [diff] [blame] | 818 | .prepare_fb = vc4_prepare_fb, |
Boris Brezillon | b9f1925 | 2017-10-19 14:57:48 +0200 | [diff] [blame] | 819 | .cleanup_fb = vc4_cleanup_fb, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 820 | }; |
| 821 | |
| 822 | static void vc4_plane_destroy(struct drm_plane *plane) |
| 823 | { |
| 824 | drm_plane_helper_disable(plane); |
| 825 | drm_plane_cleanup(plane); |
| 826 | } |
| 827 | |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 828 | /* Implements immediate (non-vblank-synced) updates of the cursor |
| 829 | * position, or falls back to the atomic helper otherwise. |
| 830 | */ |
| 831 | static int |
| 832 | vc4_update_plane(struct drm_plane *plane, |
| 833 | struct drm_crtc *crtc, |
| 834 | struct drm_framebuffer *fb, |
| 835 | int crtc_x, int crtc_y, |
| 836 | unsigned int crtc_w, unsigned int crtc_h, |
| 837 | uint32_t src_x, uint32_t src_y, |
Daniel Vetter | 34a2ab5 | 2017-03-22 22:50:41 +0100 | [diff] [blame] | 838 | uint32_t src_w, uint32_t src_h, |
| 839 | struct drm_modeset_acquire_ctx *ctx) |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 840 | { |
| 841 | struct drm_plane_state *plane_state; |
| 842 | struct vc4_plane_state *vc4_state; |
| 843 | |
| 844 | if (plane != crtc->cursor) |
| 845 | goto out; |
| 846 | |
| 847 | plane_state = plane->state; |
| 848 | vc4_state = to_vc4_plane_state(plane_state); |
| 849 | |
| 850 | if (!plane_state) |
| 851 | goto out; |
| 852 | |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 853 | /* No configuring new scaling in the fast path. */ |
| 854 | if (crtc_w != plane_state->crtc_w || |
| 855 | crtc_h != plane_state->crtc_h || |
| 856 | src_w != plane_state->src_w || |
| 857 | src_h != plane_state->src_h) { |
| 858 | goto out; |
| 859 | } |
| 860 | |
Michael Zoran | 6d24c1c | 2017-02-23 17:54:31 -0800 | [diff] [blame] | 861 | if (fb != plane_state->fb) { |
| 862 | drm_atomic_set_fb_for_plane(plane->state, fb); |
| 863 | vc4_plane_async_set_fb(plane, fb); |
| 864 | } |
| 865 | |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 866 | /* Set the cursor's position on the screen. This is the |
| 867 | * expected change from the drm_mode_cursor_universal() |
| 868 | * helper. |
| 869 | */ |
| 870 | plane_state->crtc_x = crtc_x; |
| 871 | plane_state->crtc_y = crtc_y; |
| 872 | |
| 873 | /* Allow changing the start position within the cursor BO, if |
| 874 | * that matters. |
| 875 | */ |
| 876 | plane_state->src_x = src_x; |
| 877 | plane_state->src_y = src_y; |
| 878 | |
| 879 | /* Update the display list based on the new crtc_x/y. */ |
| 880 | vc4_plane_atomic_check(plane, plane_state); |
| 881 | |
| 882 | /* Note that we can't just call vc4_plane_write_dlist() |
| 883 | * because that would smash the context data that the HVS is |
| 884 | * currently using. |
| 885 | */ |
| 886 | writel(vc4_state->dlist[vc4_state->pos0_offset], |
| 887 | &vc4_state->hw_dlist[vc4_state->pos0_offset]); |
| 888 | writel(vc4_state->dlist[vc4_state->pos2_offset], |
| 889 | &vc4_state->hw_dlist[vc4_state->pos2_offset]); |
| 890 | writel(vc4_state->dlist[vc4_state->ptr0_offset], |
| 891 | &vc4_state->hw_dlist[vc4_state->ptr0_offset]); |
| 892 | |
| 893 | return 0; |
| 894 | |
| 895 | out: |
| 896 | return drm_atomic_helper_update_plane(plane, crtc, fb, |
| 897 | crtc_x, crtc_y, |
| 898 | crtc_w, crtc_h, |
| 899 | src_x, src_y, |
Daniel Vetter | 34a2ab5 | 2017-03-22 22:50:41 +0100 | [diff] [blame] | 900 | src_w, src_h, |
| 901 | ctx); |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 904 | static const struct drm_plane_funcs vc4_plane_funcs = { |
Eric Anholt | 6674a904 | 2015-12-30 11:50:22 -0800 | [diff] [blame] | 905 | .update_plane = vc4_update_plane, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 906 | .disable_plane = drm_atomic_helper_disable_plane, |
| 907 | .destroy = vc4_plane_destroy, |
| 908 | .set_property = NULL, |
| 909 | .reset = vc4_plane_reset, |
| 910 | .atomic_duplicate_state = vc4_plane_duplicate_state, |
| 911 | .atomic_destroy_state = vc4_plane_destroy_state, |
| 912 | }; |
| 913 | |
| 914 | struct drm_plane *vc4_plane_init(struct drm_device *dev, |
| 915 | enum drm_plane_type type) |
| 916 | { |
| 917 | struct drm_plane *plane = NULL; |
| 918 | struct vc4_plane *vc4_plane; |
| 919 | u32 formats[ARRAY_SIZE(hvs_formats)]; |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 920 | u32 num_formats = 0; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 921 | int ret = 0; |
| 922 | unsigned i; |
| 923 | |
| 924 | vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane), |
| 925 | GFP_KERNEL); |
Colin Ian King | 7b34734 | 2017-03-16 18:54:18 +0000 | [diff] [blame] | 926 | if (!vc4_plane) |
| 927 | return ERR_PTR(-ENOMEM); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 928 | |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 929 | for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) { |
| 930 | /* Don't allow YUV in cursor planes, since that means |
| 931 | * tuning on the scaler, which we don't allow for the |
| 932 | * cursor. |
| 933 | */ |
| 934 | if (type != DRM_PLANE_TYPE_CURSOR || |
| 935 | hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) { |
| 936 | formats[num_formats++] = hvs_formats[i].drm; |
| 937 | } |
| 938 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 939 | plane = &vc4_plane->base; |
Andrzej Pietrasiewicz | 49d29a0 | 2017-02-01 10:35:08 +0100 | [diff] [blame] | 940 | ret = drm_universal_plane_init(dev, plane, 0, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 941 | &vc4_plane_funcs, |
Eric Anholt | fc04023 | 2015-12-30 12:25:44 -0800 | [diff] [blame] | 942 | formats, num_formats, |
Ben Widawsky | e6fc3b6 | 2017-07-23 20:46:38 -0700 | [diff] [blame] | 943 | NULL, type, NULL); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 944 | |
| 945 | drm_plane_helper_add(plane, &vc4_plane_helper_funcs); |
| 946 | |
| 947 | return plane; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 948 | } |