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_update(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 struct drm_crtc *crtc = plane->state->crtc; 172 struct drm_framebuffer *fb = plane->state->fb; 173 struct drm_gem_object *gem; 174 struct mtk_drm_gem_obj *mtk_gem; 175 unsigned int pitch, format; 176 dma_addr_t addr; 177 178 if (!crtc || WARN_ON(!fb)) 179 return; 180 181 gem = fb->obj[0]; 182 mtk_gem = to_mtk_gem_obj(gem); 183 addr = mtk_gem->dma_addr; 184 pitch = fb->pitches[0]; 185 format = fb->format->format; 186 187 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0]; 188 addr += (plane->state->src.y1 >> 16) * pitch; 189 190 state->pending.enable = true; 191 state->pending.pitch = pitch; 192 state->pending.format = format; 193 state->pending.addr = addr; 194 state->pending.x = plane->state->dst.x1; 195 state->pending.y = plane->state->dst.y1; 196 state->pending.width = drm_rect_width(&plane->state->dst); 197 state->pending.height = drm_rect_height(&plane->state->dst); 198 state->pending.rotation = plane->state->rotation; 199 wmb(); /* Make sure the above parameters are set before update */ 200 state->pending.dirty = true; 201 } 202 203 static void mtk_plane_atomic_disable(struct drm_plane *plane, 204 struct drm_plane_state *old_state) 205 { 206 struct mtk_plane_state *state = to_mtk_plane_state(plane->state); 207 208 state->pending.enable = false; 209 wmb(); /* Make sure the above parameter is set before update */ 210 state->pending.dirty = true; 211 } 212 213 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = { 214 .prepare_fb = drm_gem_fb_prepare_fb, 215 .atomic_check = mtk_plane_atomic_check, 216 .atomic_update = mtk_plane_atomic_update, 217 .atomic_disable = mtk_plane_atomic_disable, 218 .atomic_async_update = mtk_plane_atomic_async_update, 219 .atomic_async_check = mtk_plane_atomic_async_check, 220 }; 221 222 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane, 223 unsigned long possible_crtcs, enum drm_plane_type type, 224 unsigned int supported_rotations) 225 { 226 int err; 227 228 err = drm_universal_plane_init(dev, plane, possible_crtcs, 229 &mtk_plane_funcs, formats, 230 ARRAY_SIZE(formats), NULL, type, NULL); 231 if (err) { 232 DRM_ERROR("failed to initialize plane\n"); 233 return err; 234 } 235 236 if (supported_rotations & ~DRM_MODE_ROTATE_0) { 237 err = drm_plane_create_rotation_property(plane, 238 DRM_MODE_ROTATE_0, 239 supported_rotations); 240 if (err) 241 DRM_INFO("Create rotation property failed\n"); 242 } 243 244 drm_plane_helper_add(plane, &mtk_plane_helper_funcs); 245 246 return 0; 247 } 248