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