1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 * Author: CK Hu <ck.hu@mediatek.com> 5 */ 6 7 #include <drm/drm_atomic.h> 8 #include <drm/drm_atomic_helper.h> 9 #include <drm/drm_atomic_uapi.h> 10 #include <drm/drm_blend.h> 11 #include <drm/drm_fourcc.h> 12 #include <drm/drm_framebuffer.h> 13 #include <drm/drm_gem_atomic_helper.h> 14 #include <drm/drm_plane_helper.h> 15 16 #include "mtk_drm_crtc.h" 17 #include "mtk_drm_ddp_comp.h" 18 #include "mtk_drm_drv.h" 19 #include "mtk_drm_gem.h" 20 #include "mtk_drm_plane.h" 21 22 static const u32 formats[] = { 23 DRM_FORMAT_XRGB8888, 24 DRM_FORMAT_ARGB8888, 25 DRM_FORMAT_BGRX8888, 26 DRM_FORMAT_BGRA8888, 27 DRM_FORMAT_ABGR8888, 28 DRM_FORMAT_XBGR8888, 29 DRM_FORMAT_RGB888, 30 DRM_FORMAT_BGR888, 31 DRM_FORMAT_RGB565, 32 DRM_FORMAT_UYVY, 33 DRM_FORMAT_YUYV, 34 }; 35 36 static void mtk_plane_reset(struct drm_plane *plane) 37 { 38 struct mtk_plane_state *state; 39 40 if (plane->state) { 41 __drm_atomic_helper_plane_destroy_state(plane->state); 42 43 state = to_mtk_plane_state(plane->state); 44 memset(state, 0, sizeof(*state)); 45 } else { 46 state = kzalloc(sizeof(*state), GFP_KERNEL); 47 if (!state) 48 return; 49 } 50 51 __drm_atomic_helper_plane_reset(plane, &state->base); 52 53 state->base.plane = plane; 54 state->pending.format = DRM_FORMAT_RGB565; 55 } 56 57 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane) 58 { 59 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state); 60 struct mtk_plane_state *state; 61 62 state = kmalloc(sizeof(*state), GFP_KERNEL); 63 if (!state) 64 return NULL; 65 66 __drm_atomic_helper_plane_duplicate_state(plane, &state->base); 67 68 WARN_ON(state->base.plane != plane); 69 70 state->pending = old_state->pending; 71 72 return &state->base; 73 } 74 75 static void mtk_drm_plane_destroy_state(struct drm_plane *plane, 76 struct drm_plane_state *state) 77 { 78 __drm_atomic_helper_plane_destroy_state(state); 79 kfree(to_mtk_plane_state(state)); 80 } 81 82 static int mtk_plane_atomic_async_check(struct drm_plane *plane, 83 struct drm_atomic_state *state) 84 { 85 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 86 plane); 87 struct drm_crtc_state *crtc_state; 88 int ret; 89 90 if (plane != new_plane_state->crtc->cursor) 91 return -EINVAL; 92 93 if (!plane->state) 94 return -EINVAL; 95 96 if (!plane->state->fb) 97 return -EINVAL; 98 99 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane, 100 to_mtk_plane_state(new_plane_state)); 101 if (ret) 102 return ret; 103 104 if (state) 105 crtc_state = drm_atomic_get_existing_crtc_state(state, 106 new_plane_state->crtc); 107 else /* Special case for asynchronous cursor updates. */ 108 crtc_state = new_plane_state->crtc->state; 109 110 return drm_atomic_helper_check_plane_state(plane->state, crtc_state, 111 DRM_PLANE_HELPER_NO_SCALING, 112 DRM_PLANE_HELPER_NO_SCALING, 113 true, true); 114 } 115 116 static void mtk_plane_update_new_state(struct drm_plane_state *new_state, 117 struct mtk_plane_state *mtk_plane_state) 118 { 119 struct drm_framebuffer *fb = new_state->fb; 120 struct drm_gem_object *gem; 121 struct mtk_drm_gem_obj *mtk_gem; 122 unsigned int pitch, format; 123 dma_addr_t addr; 124 125 gem = fb->obj[0]; 126 mtk_gem = to_mtk_gem_obj(gem); 127 addr = mtk_gem->dma_addr; 128 pitch = fb->pitches[0]; 129 format = fb->format->format; 130 131 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0]; 132 addr += (new_state->src.y1 >> 16) * pitch; 133 134 mtk_plane_state->pending.enable = true; 135 mtk_plane_state->pending.pitch = pitch; 136 mtk_plane_state->pending.format = format; 137 mtk_plane_state->pending.addr = addr; 138 mtk_plane_state->pending.x = new_state->dst.x1; 139 mtk_plane_state->pending.y = new_state->dst.y1; 140 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst); 141 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst); 142 mtk_plane_state->pending.rotation = new_state->rotation; 143 } 144 145 static void mtk_plane_atomic_async_update(struct drm_plane *plane, 146 struct drm_atomic_state *state) 147 { 148 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 149 plane); 150 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state); 151 152 plane->state->crtc_x = new_state->crtc_x; 153 plane->state->crtc_y = new_state->crtc_y; 154 plane->state->crtc_h = new_state->crtc_h; 155 plane->state->crtc_w = new_state->crtc_w; 156 plane->state->src_x = new_state->src_x; 157 plane->state->src_y = new_state->src_y; 158 plane->state->src_h = new_state->src_h; 159 plane->state->src_w = new_state->src_w; 160 swap(plane->state->fb, new_state->fb); 161 162 mtk_plane_update_new_state(new_state, new_plane_state); 163 wmb(); /* Make sure the above parameters are set before update */ 164 new_plane_state->pending.async_dirty = true; 165 mtk_drm_crtc_async_update(new_state->crtc, plane, state); 166 } 167 168 static const struct drm_plane_funcs mtk_plane_funcs = { 169 .update_plane = drm_atomic_helper_update_plane, 170 .disable_plane = drm_atomic_helper_disable_plane, 171 .destroy = drm_plane_cleanup, 172 .reset = mtk_plane_reset, 173 .atomic_duplicate_state = mtk_plane_duplicate_state, 174 .atomic_destroy_state = mtk_drm_plane_destroy_state, 175 }; 176 177 static int mtk_plane_atomic_check(struct drm_plane *plane, 178 struct drm_atomic_state *state) 179 { 180 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 181 plane); 182 struct drm_framebuffer *fb = new_plane_state->fb; 183 struct drm_crtc_state *crtc_state; 184 int ret; 185 186 if (!fb) 187 return 0; 188 189 if (WARN_ON(!new_plane_state->crtc)) 190 return 0; 191 192 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane, 193 to_mtk_plane_state(new_plane_state)); 194 if (ret) 195 return ret; 196 197 crtc_state = drm_atomic_get_crtc_state(state, 198 new_plane_state->crtc); 199 if (IS_ERR(crtc_state)) 200 return PTR_ERR(crtc_state); 201 202 return drm_atomic_helper_check_plane_state(new_plane_state, 203 crtc_state, 204 DRM_PLANE_HELPER_NO_SCALING, 205 DRM_PLANE_HELPER_NO_SCALING, 206 true, true); 207 } 208 209 static void mtk_plane_atomic_disable(struct drm_plane *plane, 210 struct drm_atomic_state *state) 211 { 212 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 213 plane); 214 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state); 215 mtk_plane_state->pending.enable = false; 216 wmb(); /* Make sure the above parameter is set before update */ 217 mtk_plane_state->pending.dirty = true; 218 } 219 220 static void mtk_plane_atomic_update(struct drm_plane *plane, 221 struct drm_atomic_state *state) 222 { 223 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 224 plane); 225 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state); 226 227 if (!new_state->crtc || WARN_ON(!new_state->fb)) 228 return; 229 230 if (!new_state->visible) { 231 mtk_plane_atomic_disable(plane, state); 232 return; 233 } 234 235 mtk_plane_update_new_state(new_state, mtk_plane_state); 236 wmb(); /* Make sure the above parameters are set before update */ 237 mtk_plane_state->pending.dirty = true; 238 } 239 240 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = { 241 .atomic_check = mtk_plane_atomic_check, 242 .atomic_update = mtk_plane_atomic_update, 243 .atomic_disable = mtk_plane_atomic_disable, 244 .atomic_async_update = mtk_plane_atomic_async_update, 245 .atomic_async_check = mtk_plane_atomic_async_check, 246 }; 247 248 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane, 249 unsigned long possible_crtcs, enum drm_plane_type type, 250 unsigned int supported_rotations) 251 { 252 int err; 253 254 err = drm_universal_plane_init(dev, plane, possible_crtcs, 255 &mtk_plane_funcs, formats, 256 ARRAY_SIZE(formats), NULL, type, NULL); 257 if (err) { 258 DRM_ERROR("failed to initialize plane\n"); 259 return err; 260 } 261 262 if (supported_rotations & ~DRM_MODE_ROTATE_0) { 263 err = drm_plane_create_rotation_property(plane, 264 DRM_MODE_ROTATE_0, 265 supported_rotations); 266 if (err) 267 DRM_INFO("Create rotation property failed\n"); 268 } 269 270 drm_plane_helper_add(plane, &mtk_plane_helper_funcs); 271 272 return 0; 273 } 274