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 void virtio_gpu_crtc_gamma_set(struct drm_crtc *crtc,
42 u16 *red, u16 *green, u16 *blue,
43 uint32_t start, uint32_t size)
49 virtio_gpu_hide_cursor(struct virtio_gpu_device *vgdev,
50 struct virtio_gpu_output *output)
52 output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
53 output->cursor.resource_id = 0;
54 virtio_gpu_cursor_ping(vgdev, output);
57 static int virtio_gpu_crtc_cursor_set(struct drm_crtc *crtc,
58 struct drm_file *file_priv,
62 int32_t hot_x, int32_t hot_y)
64 struct virtio_gpu_device *vgdev = crtc->dev->dev_private;
65 struct virtio_gpu_output *output =
66 container_of(crtc, struct virtio_gpu_output, crtc);
67 struct drm_gem_object *gobj = NULL;
68 struct virtio_gpu_object *qobj = NULL;
69 struct virtio_gpu_fence *fence = NULL;
73 virtio_gpu_hide_cursor(vgdev, output);
77 /* lookup the cursor */
78 gobj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
82 qobj = gem_to_virtio_gpu_obj(gobj);
84 if (!qobj->hw_res_handle) {
89 virtio_gpu_cmd_transfer_to_host_2d(vgdev, qobj->hw_res_handle, 0,
93 ret = virtio_gpu_object_reserve(qobj, false);
95 reservation_object_add_excl_fence(qobj->tbo.resv,
98 virtio_gpu_object_unreserve(qobj);
99 virtio_gpu_object_wait(qobj, false);
102 output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
103 output->cursor.resource_id = cpu_to_le32(qobj->hw_res_handle);
104 output->cursor.hot_x = cpu_to_le32(hot_x);
105 output->cursor.hot_y = cpu_to_le32(hot_y);
106 virtio_gpu_cursor_ping(vgdev, output);
110 drm_gem_object_unreference_unlocked(gobj);
114 static int virtio_gpu_crtc_cursor_move(struct drm_crtc *crtc,
117 struct virtio_gpu_device *vgdev = crtc->dev->dev_private;
118 struct virtio_gpu_output *output =
119 container_of(crtc, struct virtio_gpu_output, crtc);
121 output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
122 output->cursor.pos.x = cpu_to_le32(x);
123 output->cursor.pos.y = cpu_to_le32(y);
124 virtio_gpu_cursor_ping(vgdev, output);
128 static int virtio_gpu_page_flip(struct drm_crtc *crtc,
129 struct drm_framebuffer *fb,
130 struct drm_pending_vblank_event *event,
133 struct virtio_gpu_device *vgdev = crtc->dev->dev_private;
134 struct virtio_gpu_output *output =
135 container_of(crtc, struct virtio_gpu_output, crtc);
136 struct drm_plane *plane = crtc->primary;
137 struct virtio_gpu_framebuffer *vgfb;
138 struct virtio_gpu_object *bo;
139 unsigned long irqflags;
143 vgfb = to_virtio_gpu_framebuffer(plane->fb);
144 bo = gem_to_virtio_gpu_obj(vgfb->obj);
145 handle = bo->hw_res_handle;
147 DRM_DEBUG("handle 0x%x%s, crtc %dx%d\n", handle,
148 bo->dumb ? ", dumb" : "",
149 crtc->mode.hdisplay, crtc->mode.vdisplay);
151 virtio_gpu_cmd_transfer_to_host_2d
153 cpu_to_le32(crtc->mode.hdisplay),
154 cpu_to_le32(crtc->mode.vdisplay),
157 virtio_gpu_cmd_set_scanout(vgdev, output->index, handle,
159 crtc->mode.vdisplay, 0, 0);
160 virtio_gpu_cmd_resource_flush(vgdev, handle, 0, 0,
162 crtc->mode.vdisplay);
165 spin_lock_irqsave(&crtc->dev->event_lock, irqflags);
166 drm_send_vblank_event(crtc->dev, -1, event);
167 spin_unlock_irqrestore(&crtc->dev->event_lock, irqflags);
173 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
174 .cursor_set2 = virtio_gpu_crtc_cursor_set,
175 .cursor_move = virtio_gpu_crtc_cursor_move,
176 .gamma_set = virtio_gpu_crtc_gamma_set,
177 .set_config = drm_atomic_helper_set_config,
178 .destroy = drm_crtc_cleanup,
180 .page_flip = virtio_gpu_page_flip,
181 .reset = drm_atomic_helper_crtc_reset,
182 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
183 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
186 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
188 struct virtio_gpu_framebuffer *virtio_gpu_fb
189 = to_virtio_gpu_framebuffer(fb);
191 if (virtio_gpu_fb->obj)
192 drm_gem_object_unreference_unlocked(virtio_gpu_fb->obj);
193 drm_framebuffer_cleanup(fb);
194 kfree(virtio_gpu_fb);
198 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
199 struct drm_file *file_priv,
200 unsigned flags, unsigned color,
201 struct drm_clip_rect *clips,
204 struct virtio_gpu_framebuffer *virtio_gpu_fb
205 = to_virtio_gpu_framebuffer(fb);
207 return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
210 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
211 .destroy = virtio_gpu_user_framebuffer_destroy,
212 .dirty = virtio_gpu_framebuffer_surface_dirty,
216 virtio_gpu_framebuffer_init(struct drm_device *dev,
217 struct virtio_gpu_framebuffer *vgfb,
218 const struct drm_mode_fb_cmd2 *mode_cmd,
219 struct drm_gem_object *obj)
222 struct virtio_gpu_object *bo;
225 bo = gem_to_virtio_gpu_obj(obj);
227 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
232 drm_helper_mode_fill_fb_struct(&vgfb->base, mode_cmd);
234 spin_lock_init(&vgfb->dirty_lock);
235 vgfb->x1 = vgfb->y1 = INT_MAX;
236 vgfb->x2 = vgfb->y2 = 0;
240 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
242 struct drm_device *dev = crtc->dev;
243 struct virtio_gpu_device *vgdev = dev->dev_private;
244 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
246 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
248 crtc->mode.vdisplay, 0, 0);
251 static void virtio_gpu_crtc_enable(struct drm_crtc *crtc)
255 static void virtio_gpu_crtc_disable(struct drm_crtc *crtc)
257 struct drm_device *dev = crtc->dev;
258 struct virtio_gpu_device *vgdev = dev->dev_private;
259 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
261 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
264 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
265 struct drm_crtc_state *state)
270 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
271 .enable = virtio_gpu_crtc_enable,
272 .disable = virtio_gpu_crtc_disable,
273 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
274 .atomic_check = virtio_gpu_crtc_atomic_check,
277 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
278 struct drm_display_mode *mode,
279 struct drm_display_mode *adjusted_mode)
283 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
287 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
291 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
293 struct virtio_gpu_output *output =
294 drm_connector_to_virtio_gpu_output(connector);
295 struct drm_display_mode *mode = NULL;
296 int count, width, height;
298 width = le32_to_cpu(output->info.r.width);
299 height = le32_to_cpu(output->info.r.height);
300 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
302 if (width == 0 || height == 0) {
305 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
307 DRM_DEBUG("add mode: %dx%d\n", width, height);
308 mode = drm_cvt_mode(connector->dev, width, height, 60,
309 false, false, false);
310 mode->type |= DRM_MODE_TYPE_PREFERRED;
311 drm_mode_probed_add(connector, mode);
318 static int virtio_gpu_conn_mode_valid(struct drm_connector *connector,
319 struct drm_display_mode *mode)
321 struct virtio_gpu_output *output =
322 drm_connector_to_virtio_gpu_output(connector);
325 width = le32_to_cpu(output->info.r.width);
326 height = le32_to_cpu(output->info.r.height);
328 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
330 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
332 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
333 mode->vdisplay <= height && mode->vdisplay >= height - 16)
336 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
340 static struct drm_encoder*
341 virtio_gpu_best_encoder(struct drm_connector *connector)
343 struct virtio_gpu_output *virtio_gpu_output =
344 drm_connector_to_virtio_gpu_output(connector);
346 return &virtio_gpu_output->enc;
349 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
350 .mode_set = virtio_gpu_enc_mode_set,
351 .enable = virtio_gpu_enc_enable,
352 .disable = virtio_gpu_enc_disable,
355 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
356 .get_modes = virtio_gpu_conn_get_modes,
357 .mode_valid = virtio_gpu_conn_mode_valid,
358 .best_encoder = virtio_gpu_best_encoder,
361 static enum drm_connector_status virtio_gpu_conn_detect(
362 struct drm_connector *connector,
365 struct virtio_gpu_output *output =
366 drm_connector_to_virtio_gpu_output(connector);
368 if (output->info.enabled)
369 return connector_status_connected;
371 return connector_status_disconnected;
374 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
376 struct virtio_gpu_output *virtio_gpu_output =
377 drm_connector_to_virtio_gpu_output(connector);
379 drm_connector_unregister(connector);
380 drm_connector_cleanup(connector);
381 kfree(virtio_gpu_output);
384 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
385 .dpms = drm_atomic_helper_connector_dpms,
386 .detect = virtio_gpu_conn_detect,
387 .fill_modes = drm_helper_probe_single_connector_modes,
388 .destroy = virtio_gpu_conn_destroy,
389 .reset = drm_atomic_helper_connector_reset,
390 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
391 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
394 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
395 .destroy = drm_encoder_cleanup,
398 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
400 struct drm_device *dev = vgdev->ddev;
401 struct virtio_gpu_output *output = vgdev->outputs + index;
402 struct drm_connector *connector = &output->conn;
403 struct drm_encoder *encoder = &output->enc;
404 struct drm_crtc *crtc = &output->crtc;
405 struct drm_plane *plane;
407 output->index = index;
409 output->info.enabled = cpu_to_le32(true);
410 output->info.r.width = cpu_to_le32(XRES_DEF);
411 output->info.r.height = cpu_to_le32(YRES_DEF);
414 plane = virtio_gpu_plane_init(vgdev, index);
416 return PTR_ERR(plane);
417 drm_crtc_init_with_planes(dev, crtc, plane, NULL,
418 &virtio_gpu_crtc_funcs, NULL);
419 drm_mode_crtc_set_gamma_size(crtc, 256);
420 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
423 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
424 DRM_MODE_CONNECTOR_VIRTUAL);
425 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
427 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
428 DRM_MODE_ENCODER_VIRTUAL, NULL);
429 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
430 encoder->possible_crtcs = 1 << index;
432 drm_mode_connector_attach_encoder(connector, encoder);
433 drm_connector_register(connector);
437 static struct drm_framebuffer *
438 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
439 struct drm_file *file_priv,
440 const struct drm_mode_fb_cmd2 *mode_cmd)
442 struct drm_gem_object *obj = NULL;
443 struct virtio_gpu_framebuffer *virtio_gpu_fb;
446 /* lookup object associated with res handle */
447 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
449 return ERR_PTR(-EINVAL);
451 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
452 if (virtio_gpu_fb == NULL)
453 return ERR_PTR(-ENOMEM);
455 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
457 kfree(virtio_gpu_fb);
459 drm_gem_object_unreference_unlocked(obj);
463 return &virtio_gpu_fb->base;
466 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
467 .fb_create = virtio_gpu_user_framebuffer_create,
468 .atomic_check = drm_atomic_helper_check,
469 .atomic_commit = drm_atomic_helper_commit,
472 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
476 drm_mode_config_init(vgdev->ddev);
477 vgdev->ddev->mode_config.funcs = (void *)&virtio_gpu_mode_funcs;
479 /* modes will be validated against the framebuffer size */
480 vgdev->ddev->mode_config.min_width = XRES_MIN;
481 vgdev->ddev->mode_config.min_height = YRES_MIN;
482 vgdev->ddev->mode_config.max_width = XRES_MAX;
483 vgdev->ddev->mode_config.max_height = YRES_MAX;
485 for (i = 0 ; i < vgdev->num_scanouts; ++i)
486 vgdev_output_init(vgdev, i);
488 drm_mode_config_reset(vgdev->ddev);
492 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
494 virtio_gpu_fbdev_fini(vgdev);
495 drm_mode_config_cleanup(vgdev->ddev);