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