2 * Copyright (C) 2015 Red Hat, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #include "virtgpu_drv.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
41 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
42 .set_config = drm_atomic_helper_set_config,
43 .destroy = drm_crtc_cleanup,
45 .page_flip = drm_atomic_helper_page_flip,
46 .reset = drm_atomic_helper_crtc_reset,
47 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
48 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
51 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
53 struct virtio_gpu_framebuffer *virtio_gpu_fb
54 = to_virtio_gpu_framebuffer(fb);
56 drm_gem_object_put_unlocked(virtio_gpu_fb->obj);
57 drm_framebuffer_cleanup(fb);
62 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
63 struct drm_file *file_priv,
64 unsigned int flags, unsigned int color,
65 struct drm_clip_rect *clips,
66 unsigned int num_clips)
68 struct virtio_gpu_framebuffer *virtio_gpu_fb
69 = to_virtio_gpu_framebuffer(fb);
71 return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
75 virtio_gpu_framebuffer_create_handle(struct drm_framebuffer *fb,
76 struct drm_file *file_priv,
79 struct virtio_gpu_framebuffer *virtio_gpu_fb =
80 to_virtio_gpu_framebuffer(fb);
82 return drm_gem_handle_create(file_priv, virtio_gpu_fb->obj, handle);
85 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
86 .create_handle = virtio_gpu_framebuffer_create_handle,
87 .destroy = virtio_gpu_user_framebuffer_destroy,
88 .dirty = virtio_gpu_framebuffer_surface_dirty,
92 virtio_gpu_framebuffer_init(struct drm_device *dev,
93 struct virtio_gpu_framebuffer *vgfb,
94 const struct drm_mode_fb_cmd2 *mode_cmd,
95 struct drm_gem_object *obj)
98 struct virtio_gpu_object *bo;
102 bo = gem_to_virtio_gpu_obj(obj);
104 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
106 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
112 spin_lock_init(&vgfb->dirty_lock);
113 vgfb->x1 = vgfb->y1 = INT_MAX;
114 vgfb->x2 = vgfb->y2 = 0;
118 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
120 struct drm_device *dev = crtc->dev;
121 struct virtio_gpu_device *vgdev = dev->dev_private;
122 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
124 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
126 crtc->mode.vdisplay, 0, 0);
129 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
130 struct drm_crtc_state *old_state)
134 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
135 struct drm_crtc_state *old_state)
137 struct drm_device *dev = crtc->dev;
138 struct virtio_gpu_device *vgdev = dev->dev_private;
139 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
141 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
144 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
145 struct drm_crtc_state *state)
150 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
151 struct drm_crtc_state *old_state)
155 spin_lock_irqsave(&crtc->dev->event_lock, flags);
156 if (crtc->state->event)
157 drm_crtc_send_vblank_event(crtc, crtc->state->event);
158 crtc->state->event = NULL;
159 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
162 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
163 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
164 .atomic_check = virtio_gpu_crtc_atomic_check,
165 .atomic_flush = virtio_gpu_crtc_atomic_flush,
166 .atomic_enable = virtio_gpu_crtc_atomic_enable,
167 .atomic_disable = virtio_gpu_crtc_atomic_disable,
170 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
171 struct drm_display_mode *mode,
172 struct drm_display_mode *adjusted_mode)
176 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
180 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
184 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
186 struct virtio_gpu_output *output =
187 drm_connector_to_virtio_gpu_output(connector);
188 struct drm_display_mode *mode = NULL;
189 int count, width, height;
191 width = le32_to_cpu(output->info.r.width);
192 height = le32_to_cpu(output->info.r.height);
193 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
195 if (width == 0 || height == 0) {
198 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
200 DRM_DEBUG("add mode: %dx%d\n", width, height);
201 mode = drm_cvt_mode(connector->dev, width, height, 60,
202 false, false, false);
203 mode->type |= DRM_MODE_TYPE_PREFERRED;
204 drm_mode_probed_add(connector, mode);
211 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
212 struct drm_display_mode *mode)
214 struct virtio_gpu_output *output =
215 drm_connector_to_virtio_gpu_output(connector);
218 width = le32_to_cpu(output->info.r.width);
219 height = le32_to_cpu(output->info.r.height);
221 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
223 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
225 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
226 mode->vdisplay <= height && mode->vdisplay >= height - 16)
229 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
233 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
234 .mode_set = virtio_gpu_enc_mode_set,
235 .enable = virtio_gpu_enc_enable,
236 .disable = virtio_gpu_enc_disable,
239 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
240 .get_modes = virtio_gpu_conn_get_modes,
241 .mode_valid = virtio_gpu_conn_mode_valid,
244 static enum drm_connector_status virtio_gpu_conn_detect(
245 struct drm_connector *connector,
248 struct virtio_gpu_output *output =
249 drm_connector_to_virtio_gpu_output(connector);
251 if (output->info.enabled)
252 return connector_status_connected;
254 return connector_status_disconnected;
257 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
259 struct virtio_gpu_output *virtio_gpu_output =
260 drm_connector_to_virtio_gpu_output(connector);
262 drm_connector_unregister(connector);
263 drm_connector_cleanup(connector);
264 kfree(virtio_gpu_output);
267 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
268 .detect = virtio_gpu_conn_detect,
269 .fill_modes = drm_helper_probe_single_connector_modes,
270 .destroy = virtio_gpu_conn_destroy,
271 .reset = drm_atomic_helper_connector_reset,
272 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
273 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
276 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
277 .destroy = drm_encoder_cleanup,
280 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
282 struct drm_device *dev = vgdev->ddev;
283 struct virtio_gpu_output *output = vgdev->outputs + index;
284 struct drm_connector *connector = &output->conn;
285 struct drm_encoder *encoder = &output->enc;
286 struct drm_crtc *crtc = &output->crtc;
287 struct drm_plane *primary, *cursor;
289 output->index = index;
291 output->info.enabled = cpu_to_le32(true);
292 output->info.r.width = cpu_to_le32(XRES_DEF);
293 output->info.r.height = cpu_to_le32(YRES_DEF);
296 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
298 return PTR_ERR(primary);
299 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
301 return PTR_ERR(cursor);
302 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
303 &virtio_gpu_crtc_funcs, NULL);
304 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
305 primary->crtc = crtc;
308 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
309 DRM_MODE_CONNECTOR_VIRTUAL);
310 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
312 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
313 DRM_MODE_ENCODER_VIRTUAL, NULL);
314 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
315 encoder->possible_crtcs = 1 << index;
317 drm_mode_connector_attach_encoder(connector, encoder);
318 drm_connector_register(connector);
322 static struct drm_framebuffer *
323 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
324 struct drm_file *file_priv,
325 const struct drm_mode_fb_cmd2 *mode_cmd)
327 struct drm_gem_object *obj = NULL;
328 struct virtio_gpu_framebuffer *virtio_gpu_fb;
331 /* lookup object associated with res handle */
332 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
334 return ERR_PTR(-EINVAL);
336 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
337 if (virtio_gpu_fb == NULL)
338 return ERR_PTR(-ENOMEM);
340 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
342 kfree(virtio_gpu_fb);
343 drm_gem_object_put_unlocked(obj);
347 return &virtio_gpu_fb->base;
350 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
352 struct drm_device *dev = state->dev;
354 drm_atomic_helper_commit_modeset_disables(dev, state);
355 drm_atomic_helper_commit_modeset_enables(dev, state);
356 drm_atomic_helper_commit_planes(dev, state, 0);
358 drm_atomic_helper_commit_hw_done(state);
360 drm_atomic_helper_wait_for_vblanks(dev, state);
361 drm_atomic_helper_cleanup_planes(dev, state);
364 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
365 .atomic_commit_tail = vgdev_atomic_commit_tail,
368 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
369 .fb_create = virtio_gpu_user_framebuffer_create,
370 .atomic_check = drm_atomic_helper_check,
371 .atomic_commit = drm_atomic_helper_commit,
374 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
378 drm_mode_config_init(vgdev->ddev);
379 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
380 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
382 /* modes will be validated against the framebuffer size */
383 vgdev->ddev->mode_config.min_width = XRES_MIN;
384 vgdev->ddev->mode_config.min_height = YRES_MIN;
385 vgdev->ddev->mode_config.max_width = XRES_MAX;
386 vgdev->ddev->mode_config.max_height = YRES_MAX;
388 for (i = 0 ; i < vgdev->num_scanouts; ++i)
389 vgdev_output_init(vgdev, i);
391 drm_mode_config_reset(vgdev->ddev);
395 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
397 virtio_gpu_fbdev_fini(vgdev);
398 drm_mode_config_cleanup(vgdev->ddev);