1 // SPDX-License-Identifier: GPL-2.0+ 2 3 #include <linux/dma-buf-map.h> 4 5 #include <drm/drm_atomic.h> 6 #include <drm/drm_atomic_helper.h> 7 #include <drm/drm_fourcc.h> 8 #include <drm/drm_gem_atomic_helper.h> 9 #include <drm/drm_gem_framebuffer_helper.h> 10 #include <drm/drm_plane_helper.h> 11 #include <drm/drm_gem_shmem_helper.h> 12 13 #include "vkms_drv.h" 14 15 static const u32 vkms_formats[] = { 16 DRM_FORMAT_XRGB8888, 17 }; 18 19 static const u32 vkms_plane_formats[] = { 20 DRM_FORMAT_ARGB8888, 21 DRM_FORMAT_XRGB8888 22 }; 23 24 static struct drm_plane_state * 25 vkms_plane_duplicate_state(struct drm_plane *plane) 26 { 27 struct vkms_plane_state *vkms_state; 28 struct vkms_composer *composer; 29 30 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL); 31 if (!vkms_state) 32 return NULL; 33 34 composer = kzalloc(sizeof(*composer), GFP_KERNEL); 35 if (!composer) { 36 DRM_DEBUG_KMS("Couldn't allocate composer\n"); 37 kfree(vkms_state); 38 return NULL; 39 } 40 41 vkms_state->composer = composer; 42 43 __drm_atomic_helper_plane_duplicate_state(plane, 44 &vkms_state->base); 45 46 return &vkms_state->base; 47 } 48 49 static void vkms_plane_destroy_state(struct drm_plane *plane, 50 struct drm_plane_state *old_state) 51 { 52 struct vkms_plane_state *vkms_state = to_vkms_plane_state(old_state); 53 struct drm_crtc *crtc = vkms_state->base.crtc; 54 55 if (crtc) { 56 /* dropping the reference we acquired in 57 * vkms_primary_plane_update() 58 */ 59 if (drm_framebuffer_read_refcount(&vkms_state->composer->fb)) 60 drm_framebuffer_put(&vkms_state->composer->fb); 61 } 62 63 kfree(vkms_state->composer); 64 vkms_state->composer = NULL; 65 66 __drm_atomic_helper_plane_destroy_state(old_state); 67 kfree(vkms_state); 68 } 69 70 static void vkms_plane_reset(struct drm_plane *plane) 71 { 72 struct vkms_plane_state *vkms_state; 73 74 if (plane->state) 75 vkms_plane_destroy_state(plane, plane->state); 76 77 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL); 78 if (!vkms_state) { 79 DRM_ERROR("Cannot allocate vkms_plane_state\n"); 80 return; 81 } 82 83 plane->state = &vkms_state->base; 84 plane->state->plane = plane; 85 } 86 87 static const struct drm_plane_funcs vkms_plane_funcs = { 88 .update_plane = drm_atomic_helper_update_plane, 89 .disable_plane = drm_atomic_helper_disable_plane, 90 .reset = vkms_plane_reset, 91 .atomic_duplicate_state = vkms_plane_duplicate_state, 92 .atomic_destroy_state = vkms_plane_destroy_state, 93 }; 94 95 static void vkms_plane_atomic_update(struct drm_plane *plane, 96 struct drm_atomic_state *state) 97 { 98 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 99 plane); 100 struct vkms_plane_state *vkms_plane_state; 101 struct drm_framebuffer *fb = new_state->fb; 102 struct vkms_composer *composer; 103 104 if (!new_state->crtc || !fb) 105 return; 106 107 vkms_plane_state = to_vkms_plane_state(new_state); 108 109 composer = vkms_plane_state->composer; 110 memcpy(&composer->src, &new_state->src, sizeof(struct drm_rect)); 111 memcpy(&composer->dst, &new_state->dst, sizeof(struct drm_rect)); 112 memcpy(&composer->fb, fb, sizeof(struct drm_framebuffer)); 113 drm_framebuffer_get(&composer->fb); 114 composer->offset = fb->offsets[0]; 115 composer->pitch = fb->pitches[0]; 116 composer->cpp = fb->format->cpp[0]; 117 } 118 119 static int vkms_plane_atomic_check(struct drm_plane *plane, 120 struct drm_atomic_state *state) 121 { 122 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 123 plane); 124 struct drm_crtc_state *crtc_state; 125 bool can_position = false; 126 int ret; 127 128 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc)) 129 return 0; 130 131 crtc_state = drm_atomic_get_crtc_state(state, 132 new_plane_state->crtc); 133 if (IS_ERR(crtc_state)) 134 return PTR_ERR(crtc_state); 135 136 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 137 can_position = true; 138 139 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state, 140 DRM_PLANE_HELPER_NO_SCALING, 141 DRM_PLANE_HELPER_NO_SCALING, 142 can_position, true); 143 if (ret != 0) 144 return ret; 145 146 /* for now primary plane must be visible and full screen */ 147 if (!new_plane_state->visible && !can_position) 148 return -EINVAL; 149 150 return 0; 151 } 152 153 static int vkms_prepare_fb(struct drm_plane *plane, 154 struct drm_plane_state *state) 155 { 156 struct drm_gem_object *gem_obj; 157 struct dma_buf_map map; 158 int ret; 159 160 if (!state->fb) 161 return 0; 162 163 gem_obj = drm_gem_fb_get_obj(state->fb, 0); 164 ret = drm_gem_shmem_vmap(gem_obj, &map); 165 if (ret) 166 DRM_ERROR("vmap failed: %d\n", ret); 167 168 return drm_gem_plane_helper_prepare_fb(plane, state); 169 } 170 171 static void vkms_cleanup_fb(struct drm_plane *plane, 172 struct drm_plane_state *old_state) 173 { 174 struct drm_gem_object *gem_obj; 175 struct drm_gem_shmem_object *shmem_obj; 176 struct dma_buf_map map; 177 178 if (!old_state->fb) 179 return; 180 181 gem_obj = drm_gem_fb_get_obj(old_state->fb, 0); 182 shmem_obj = to_drm_gem_shmem_obj(drm_gem_fb_get_obj(old_state->fb, 0)); 183 dma_buf_map_set_vaddr(&map, shmem_obj->vaddr); 184 drm_gem_shmem_vunmap(gem_obj, &map); 185 } 186 187 static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = { 188 .atomic_update = vkms_plane_atomic_update, 189 .atomic_check = vkms_plane_atomic_check, 190 .prepare_fb = vkms_prepare_fb, 191 .cleanup_fb = vkms_cleanup_fb, 192 }; 193 194 struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev, 195 enum drm_plane_type type, int index) 196 { 197 struct drm_device *dev = &vkmsdev->drm; 198 const struct drm_plane_helper_funcs *funcs; 199 struct vkms_plane *plane; 200 const u32 *formats; 201 int nformats; 202 203 switch (type) { 204 case DRM_PLANE_TYPE_PRIMARY: 205 formats = vkms_formats; 206 nformats = ARRAY_SIZE(vkms_formats); 207 funcs = &vkms_primary_helper_funcs; 208 break; 209 case DRM_PLANE_TYPE_CURSOR: 210 case DRM_PLANE_TYPE_OVERLAY: 211 formats = vkms_plane_formats; 212 nformats = ARRAY_SIZE(vkms_plane_formats); 213 funcs = &vkms_primary_helper_funcs; 214 break; 215 default: 216 formats = vkms_formats; 217 nformats = ARRAY_SIZE(vkms_formats); 218 funcs = &vkms_primary_helper_funcs; 219 break; 220 } 221 222 plane = drmm_universal_plane_alloc(dev, struct vkms_plane, base, 1 << index, 223 &vkms_plane_funcs, 224 formats, nformats, 225 NULL, type, NULL); 226 if (IS_ERR(plane)) 227 return plane; 228 229 drm_plane_helper_add(&plane->base, funcs); 230 231 return plane; 232 } 233