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_atomic_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/drm_damage_helper.h>
43 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
44 .set_config = drm_atomic_helper_set_config,
45 .destroy = drm_crtc_cleanup,
47 .page_flip = drm_atomic_helper_page_flip,
48 .reset = drm_atomic_helper_crtc_reset,
49 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
50 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
53 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
54 .create_handle = drm_gem_fb_create_handle,
55 .destroy = drm_gem_fb_destroy,
56 .dirty = drm_atomic_helper_dirtyfb,
60 virtio_gpu_framebuffer_init(struct drm_device *dev,
61 struct virtio_gpu_framebuffer *vgfb,
62 const struct drm_mode_fb_cmd2 *mode_cmd,
63 struct drm_gem_object *obj)
67 vgfb->base.obj[0] = obj;
69 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
71 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
73 vgfb->base.obj[0] = NULL;
79 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
81 struct drm_device *dev = crtc->dev;
82 struct virtio_gpu_device *vgdev = dev->dev_private;
83 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
85 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
87 crtc->mode.vdisplay, 0, 0);
90 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
91 struct drm_crtc_state *old_state)
93 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
95 output->enabled = true;
98 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
99 struct drm_crtc_state *old_state)
101 struct drm_device *dev = crtc->dev;
102 struct virtio_gpu_device *vgdev = dev->dev_private;
103 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
105 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
106 output->enabled = false;
109 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
110 struct drm_crtc_state *state)
115 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
116 struct drm_crtc_state *old_state)
120 spin_lock_irqsave(&crtc->dev->event_lock, flags);
121 if (crtc->state->event)
122 drm_crtc_send_vblank_event(crtc, crtc->state->event);
123 crtc->state->event = NULL;
124 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
127 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
128 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
129 .atomic_check = virtio_gpu_crtc_atomic_check,
130 .atomic_flush = virtio_gpu_crtc_atomic_flush,
131 .atomic_enable = virtio_gpu_crtc_atomic_enable,
132 .atomic_disable = virtio_gpu_crtc_atomic_disable,
135 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
136 struct drm_display_mode *mode,
137 struct drm_display_mode *adjusted_mode)
141 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
145 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
149 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
151 struct virtio_gpu_output *output =
152 drm_connector_to_virtio_gpu_output(connector);
153 struct drm_display_mode *mode = NULL;
154 int count, width, height;
157 count = drm_add_edid_modes(connector, output->edid);
162 width = le32_to_cpu(output->info.r.width);
163 height = le32_to_cpu(output->info.r.height);
164 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
166 if (width == 0 || height == 0) {
169 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
171 DRM_DEBUG("add mode: %dx%d\n", width, height);
172 mode = drm_cvt_mode(connector->dev, width, height, 60,
173 false, false, false);
174 mode->type |= DRM_MODE_TYPE_PREFERRED;
175 drm_mode_probed_add(connector, mode);
182 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
183 struct drm_display_mode *mode)
185 struct virtio_gpu_output *output =
186 drm_connector_to_virtio_gpu_output(connector);
189 width = le32_to_cpu(output->info.r.width);
190 height = le32_to_cpu(output->info.r.height);
192 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
194 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
196 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
197 mode->vdisplay <= height && mode->vdisplay >= height - 16)
200 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
204 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
205 .mode_set = virtio_gpu_enc_mode_set,
206 .enable = virtio_gpu_enc_enable,
207 .disable = virtio_gpu_enc_disable,
210 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
211 .get_modes = virtio_gpu_conn_get_modes,
212 .mode_valid = virtio_gpu_conn_mode_valid,
215 static enum drm_connector_status virtio_gpu_conn_detect(
216 struct drm_connector *connector,
219 struct virtio_gpu_output *output =
220 drm_connector_to_virtio_gpu_output(connector);
222 if (output->info.enabled)
223 return connector_status_connected;
225 return connector_status_disconnected;
228 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
230 drm_connector_unregister(connector);
231 drm_connector_cleanup(connector);
234 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
235 .detect = virtio_gpu_conn_detect,
236 .fill_modes = drm_helper_probe_single_connector_modes,
237 .destroy = virtio_gpu_conn_destroy,
238 .reset = drm_atomic_helper_connector_reset,
239 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
240 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
243 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
244 .destroy = drm_encoder_cleanup,
247 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
249 struct drm_device *dev = vgdev->ddev;
250 struct virtio_gpu_output *output = vgdev->outputs + index;
251 struct drm_connector *connector = &output->conn;
252 struct drm_encoder *encoder = &output->enc;
253 struct drm_crtc *crtc = &output->crtc;
254 struct drm_plane *primary, *cursor;
256 output->index = index;
258 output->info.enabled = cpu_to_le32(true);
259 output->info.r.width = cpu_to_le32(XRES_DEF);
260 output->info.r.height = cpu_to_le32(YRES_DEF);
263 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
265 return PTR_ERR(primary);
266 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
268 return PTR_ERR(cursor);
269 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
270 &virtio_gpu_crtc_funcs, NULL);
271 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
273 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
274 DRM_MODE_CONNECTOR_VIRTUAL);
275 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
277 drm_connector_attach_edid_property(connector);
279 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
280 DRM_MODE_ENCODER_VIRTUAL, NULL);
281 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
282 encoder->possible_crtcs = 1 << index;
284 drm_connector_attach_encoder(connector, encoder);
285 drm_connector_register(connector);
289 static struct drm_framebuffer *
290 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
291 struct drm_file *file_priv,
292 const struct drm_mode_fb_cmd2 *mode_cmd)
294 struct drm_gem_object *obj = NULL;
295 struct virtio_gpu_framebuffer *virtio_gpu_fb;
298 if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
299 mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
300 return ERR_PTR(-ENOENT);
302 /* lookup object associated with res handle */
303 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
305 return ERR_PTR(-EINVAL);
307 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
308 if (virtio_gpu_fb == NULL)
309 return ERR_PTR(-ENOMEM);
311 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
313 kfree(virtio_gpu_fb);
314 drm_gem_object_put_unlocked(obj);
318 return &virtio_gpu_fb->base;
321 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
323 struct drm_device *dev = state->dev;
325 drm_atomic_helper_commit_modeset_disables(dev, state);
326 drm_atomic_helper_commit_modeset_enables(dev, state);
327 drm_atomic_helper_commit_planes(dev, state, 0);
329 drm_atomic_helper_commit_hw_done(state);
331 drm_atomic_helper_wait_for_vblanks(dev, state);
332 drm_atomic_helper_cleanup_planes(dev, state);
335 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
336 .atomic_commit_tail = vgdev_atomic_commit_tail,
339 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
340 .fb_create = virtio_gpu_user_framebuffer_create,
341 .atomic_check = drm_atomic_helper_check,
342 .atomic_commit = drm_atomic_helper_commit,
345 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
349 drm_mode_config_init(vgdev->ddev);
350 vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
351 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
352 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
354 /* modes will be validated against the framebuffer size */
355 vgdev->ddev->mode_config.min_width = XRES_MIN;
356 vgdev->ddev->mode_config.min_height = YRES_MIN;
357 vgdev->ddev->mode_config.max_width = XRES_MAX;
358 vgdev->ddev->mode_config.max_height = YRES_MAX;
360 for (i = 0 ; i < vgdev->num_scanouts; ++i)
361 vgdev_output_init(vgdev, i);
363 drm_mode_config_reset(vgdev->ddev);
366 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
370 for (i = 0 ; i < vgdev->num_scanouts; ++i)
371 kfree(vgdev->outputs[i].edid);
372 drm_atomic_helper_shutdown(vgdev->ddev);
373 drm_mode_config_cleanup(vgdev->ddev);