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