Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Russell King |
| 3 | * Rewritten from the dovefb driver, and Armada510 manuals. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | #include <drm/drmP.h> |
| 10 | #include <drm/drm_atomic.h> |
| 11 | #include <drm/drm_atomic_helper.h> |
| 12 | #include <drm/drm_plane_helper.h> |
| 13 | #include "armada_crtc.h" |
| 14 | #include "armada_drm.h" |
| 15 | #include "armada_fb.h" |
| 16 | #include "armada_gem.h" |
| 17 | #include "armada_hw.h" |
| 18 | #include "armada_plane.h" |
| 19 | #include "armada_trace.h" |
| 20 | |
| 21 | static const uint32_t armada_primary_formats[] = { |
| 22 | DRM_FORMAT_UYVY, |
| 23 | DRM_FORMAT_YUYV, |
| 24 | DRM_FORMAT_VYUY, |
| 25 | DRM_FORMAT_YVYU, |
| 26 | DRM_FORMAT_ARGB8888, |
| 27 | DRM_FORMAT_ABGR8888, |
| 28 | DRM_FORMAT_XRGB8888, |
| 29 | DRM_FORMAT_XBGR8888, |
| 30 | DRM_FORMAT_RGB888, |
| 31 | DRM_FORMAT_BGR888, |
| 32 | DRM_FORMAT_ARGB1555, |
| 33 | DRM_FORMAT_ABGR1555, |
| 34 | DRM_FORMAT_RGB565, |
| 35 | DRM_FORMAT_BGR565, |
| 36 | }; |
| 37 | |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 38 | void armada_drm_plane_calc_addrs(struct drm_plane_state *state, u32 addrs[3]) |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 39 | { |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 40 | struct drm_framebuffer *fb = state->fb; |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 41 | const struct drm_format_info *format = fb->format; |
| 42 | unsigned int num_planes = format->num_planes; |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 43 | unsigned int x = state->src.x1 >> 16; |
| 44 | unsigned int y = state->src.y1 >> 16; |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 45 | u32 addr = drm_fb_obj(fb)->dev_addr; |
| 46 | int i; |
| 47 | |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 48 | DRM_DEBUG_KMS("pitch %u x %d y %d bpp %d\n", |
| 49 | fb->pitches[0], x, y, format->cpp[0] * 8); |
| 50 | |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 51 | if (num_planes > 3) |
| 52 | num_planes = 3; |
| 53 | |
| 54 | addrs[0] = addr + fb->offsets[0] + y * fb->pitches[0] + |
| 55 | x * format->cpp[0]; |
| 56 | |
| 57 | y /= format->vsub; |
| 58 | x /= format->hsub; |
| 59 | |
| 60 | for (i = 1; i < num_planes; i++) |
| 61 | addrs[i] = addr + fb->offsets[i] + y * fb->pitches[i] + |
| 62 | x * format->cpp[i]; |
| 63 | for (; i < 3; i++) |
| 64 | addrs[i] = 0; |
| 65 | } |
| 66 | |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 67 | static unsigned armada_drm_crtc_calc_fb(struct drm_plane_state *state, |
| 68 | struct armada_regs *regs, bool interlaced) |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 69 | { |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 70 | unsigned pitch = state->fb->pitches[0]; |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 71 | u32 addrs[3], addr_odd, addr_even; |
| 72 | unsigned i = 0; |
| 73 | |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 74 | armada_drm_plane_calc_addrs(state, addrs); |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 75 | |
| 76 | addr_odd = addr_even = addrs[0]; |
| 77 | |
| 78 | if (interlaced) { |
| 79 | addr_even += pitch; |
| 80 | pitch *= 2; |
| 81 | } |
| 82 | |
| 83 | /* write offset, base, and pitch */ |
| 84 | armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0); |
| 85 | armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1); |
| 86 | armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH); |
| 87 | |
| 88 | return i; |
| 89 | } |
| 90 | |
| 91 | int armada_drm_plane_prepare_fb(struct drm_plane *plane, |
| 92 | struct drm_plane_state *state) |
| 93 | { |
| 94 | DRM_DEBUG_KMS("[PLANE:%d:%s] [FB:%d]\n", |
| 95 | plane->base.id, plane->name, |
| 96 | state->fb ? state->fb->base.id : 0); |
| 97 | |
| 98 | /* |
| 99 | * Take a reference on the new framebuffer - we want to |
| 100 | * hold on to it while the hardware is displaying it. |
| 101 | */ |
| 102 | if (state->fb) |
| 103 | drm_framebuffer_get(state->fb); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void armada_drm_plane_cleanup_fb(struct drm_plane *plane, |
| 108 | struct drm_plane_state *old_state) |
| 109 | { |
| 110 | DRM_DEBUG_KMS("[PLANE:%d:%s] [FB:%d]\n", |
| 111 | plane->base.id, plane->name, |
| 112 | old_state->fb ? old_state->fb->base.id : 0); |
| 113 | |
| 114 | if (old_state->fb) |
| 115 | drm_framebuffer_put(old_state->fb); |
| 116 | } |
| 117 | |
| 118 | int armada_drm_plane_atomic_check(struct drm_plane *plane, |
| 119 | struct drm_plane_state *state) |
| 120 | { |
| 121 | if (state->fb && !WARN_ON(!state->crtc)) { |
| 122 | struct drm_crtc *crtc = state->crtc; |
| 123 | struct drm_crtc_state *crtc_state; |
| 124 | |
| 125 | if (state->state) |
| 126 | crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc); |
| 127 | else |
| 128 | crtc_state = crtc->state; |
| 129 | return drm_atomic_helper_check_plane_state(state, crtc_state, |
| 130 | 0, INT_MAX, |
| 131 | true, false); |
| 132 | } else { |
| 133 | state->visible = false; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static void armada_drm_primary_plane_atomic_update(struct drm_plane *plane, |
| 139 | struct drm_plane_state *old_state) |
| 140 | { |
| 141 | struct drm_plane_state *state = plane->state; |
| 142 | struct armada_crtc *dcrtc; |
| 143 | struct armada_regs *regs; |
| 144 | u32 cfg, cfg_mask, val; |
| 145 | unsigned int idx; |
| 146 | |
| 147 | DRM_DEBUG_KMS("[PLANE:%d:%s]\n", plane->base.id, plane->name); |
| 148 | |
| 149 | if (!state->fb || WARN_ON(!state->crtc)) |
| 150 | return; |
| 151 | |
| 152 | DRM_DEBUG_KMS("[PLANE:%d:%s] is on [CRTC:%d:%s] with [FB:%d] visible %u->%u\n", |
| 153 | plane->base.id, plane->name, |
| 154 | state->crtc->base.id, state->crtc->name, |
| 155 | state->fb->base.id, |
| 156 | old_state->visible, state->visible); |
| 157 | |
| 158 | dcrtc = drm_to_armada_crtc(state->crtc); |
| 159 | regs = dcrtc->regs + dcrtc->regs_idx; |
| 160 | |
| 161 | idx = 0; |
| 162 | if (!old_state->visible && state->visible) { |
| 163 | val = CFG_PDWN64x66; |
| 164 | if (drm_fb_to_armada_fb(state->fb)->fmt > CFG_420) |
| 165 | val |= CFG_PDWN256x24; |
| 166 | armada_reg_queue_mod(regs, idx, 0, val, LCD_SPU_SRAM_PARA1); |
| 167 | } |
| 168 | val = armada_rect_hw_fp(&state->src); |
| 169 | if (armada_rect_hw_fp(&old_state->src) != val) |
| 170 | armada_reg_queue_set(regs, idx, val, LCD_SPU_GRA_HPXL_VLN); |
| 171 | val = armada_rect_yx(&state->dst); |
| 172 | if (armada_rect_yx(&old_state->dst) != val) |
| 173 | armada_reg_queue_set(regs, idx, val, LCD_SPU_GRA_OVSA_HPXL_VLN); |
| 174 | val = armada_rect_hw(&state->dst); |
| 175 | if (armada_rect_hw(&old_state->dst) != val) |
| 176 | armada_reg_queue_set(regs, idx, val, LCD_SPU_GZM_HPXL_VLN); |
| 177 | if (old_state->src.x1 != state->src.x1 || |
| 178 | old_state->src.y1 != state->src.y1 || |
| 179 | old_state->fb != state->fb) { |
Russell King | b4df3ba | 2018-07-30 11:52:34 +0100 | [diff] [blame^] | 180 | idx += armada_drm_crtc_calc_fb(state, regs + idx, |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 181 | dcrtc->interlaced); |
| 182 | } |
| 183 | if (old_state->fb != state->fb) { |
| 184 | cfg = CFG_GRA_FMT(drm_fb_to_armada_fb(state->fb)->fmt) | |
| 185 | CFG_GRA_MOD(drm_fb_to_armada_fb(state->fb)->mod); |
| 186 | if (drm_fb_to_armada_fb(state->fb)->fmt > CFG_420) |
| 187 | cfg |= CFG_PALETTE_ENA; |
| 188 | if (state->visible) |
| 189 | cfg |= CFG_GRA_ENA; |
| 190 | if (dcrtc->interlaced) |
| 191 | cfg |= CFG_GRA_FTOGGLE; |
| 192 | cfg_mask = CFG_GRAFORMAT | |
| 193 | CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV | |
| 194 | CFG_SWAPYU | CFG_YUV2RGB) | |
| 195 | CFG_PALETTE_ENA | CFG_GRA_FTOGGLE | |
| 196 | CFG_GRA_ENA; |
| 197 | } else if (old_state->visible != state->visible) { |
| 198 | cfg = state->visible ? CFG_GRA_ENA : 0; |
| 199 | cfg_mask = CFG_GRA_ENA; |
| 200 | } else { |
| 201 | cfg = cfg_mask = 0; |
| 202 | } |
| 203 | if (drm_rect_width(&old_state->src) != drm_rect_width(&state->src) || |
| 204 | drm_rect_width(&old_state->dst) != drm_rect_width(&state->dst)) { |
| 205 | cfg_mask |= CFG_GRA_HSMOOTH; |
| 206 | if (drm_rect_width(&state->src) >> 16 != |
| 207 | drm_rect_width(&state->dst)) |
| 208 | cfg |= CFG_GRA_HSMOOTH; |
| 209 | } |
| 210 | |
| 211 | if (cfg_mask) |
| 212 | armada_reg_queue_mod(regs, idx, cfg, cfg_mask, |
| 213 | LCD_SPU_DMA_CTRL0); |
| 214 | |
| 215 | dcrtc->regs_idx += idx; |
| 216 | } |
| 217 | |
| 218 | static void armada_drm_primary_plane_atomic_disable(struct drm_plane *plane, |
| 219 | struct drm_plane_state *old_state) |
| 220 | { |
| 221 | struct armada_crtc *dcrtc; |
| 222 | struct armada_regs *regs; |
| 223 | unsigned int idx = 0; |
| 224 | |
| 225 | DRM_DEBUG_KMS("[PLANE:%d:%s]\n", plane->base.id, plane->name); |
| 226 | |
| 227 | if (!old_state->crtc) |
| 228 | return; |
| 229 | |
| 230 | DRM_DEBUG_KMS("[PLANE:%d:%s] was on [CRTC:%d:%s] with [FB:%d]\n", |
| 231 | plane->base.id, plane->name, |
| 232 | old_state->crtc->base.id, old_state->crtc->name, |
| 233 | old_state->fb->base.id); |
| 234 | |
| 235 | dcrtc = drm_to_armada_crtc(old_state->crtc); |
| 236 | regs = dcrtc->regs + dcrtc->regs_idx; |
| 237 | |
| 238 | /* Disable plane and power down most RAMs and FIFOs */ |
| 239 | armada_reg_queue_mod(regs, idx, 0, CFG_GRA_ENA, LCD_SPU_DMA_CTRL0); |
| 240 | armada_reg_queue_mod(regs, idx, CFG_PDWN256x32 | CFG_PDWN256x24 | |
| 241 | CFG_PDWN256x8 | CFG_PDWN32x32 | CFG_PDWN64x66, |
| 242 | 0, LCD_SPU_SRAM_PARA1); |
| 243 | |
| 244 | dcrtc->regs_idx += idx; |
| 245 | } |
| 246 | |
| 247 | static const struct drm_plane_helper_funcs armada_primary_plane_helper_funcs = { |
| 248 | .prepare_fb = armada_drm_plane_prepare_fb, |
| 249 | .cleanup_fb = armada_drm_plane_cleanup_fb, |
| 250 | .atomic_check = armada_drm_plane_atomic_check, |
| 251 | .atomic_update = armada_drm_primary_plane_atomic_update, |
| 252 | .atomic_disable = armada_drm_primary_plane_atomic_disable, |
| 253 | }; |
| 254 | |
| 255 | static const struct drm_plane_funcs armada_primary_plane_funcs = { |
| 256 | .update_plane = drm_plane_helper_update, |
| 257 | .disable_plane = drm_plane_helper_disable, |
| 258 | .destroy = drm_primary_helper_destroy, |
| 259 | .reset = drm_atomic_helper_plane_reset, |
| 260 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 261 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 262 | }; |
| 263 | |
| 264 | int armada_drm_plane_init(struct armada_plane *plane) |
| 265 | { |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 266 | init_waitqueue_head(&plane->frame_wait); |
Russell King | d40af7b | 2018-07-30 11:52:34 +0100 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int armada_drm_primary_plane_init(struct drm_device *drm, |
| 271 | struct armada_plane *primary) |
| 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | ret = armada_drm_plane_init(primary); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | |
| 279 | drm_plane_helper_add(&primary->base, |
| 280 | &armada_primary_plane_helper_funcs); |
| 281 | |
| 282 | ret = drm_universal_plane_init(drm, &primary->base, 0, |
| 283 | &armada_primary_plane_funcs, |
| 284 | armada_primary_formats, |
| 285 | ARRAY_SIZE(armada_primary_formats), |
| 286 | NULL, |
| 287 | DRM_PLANE_TYPE_PRIMARY, NULL); |
| 288 | |
| 289 | return ret; |
| 290 | } |