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 <drm/drm_atomic_helper.h>
29 #include <drm/drm_damage_helper.h>
30 #include <drm/drm_fourcc.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32 #include <drm/drm_probe_helper.h>
33 #include <drm/drm_simple_kms_helper.h>
35 #include "virtgpu_drv.h"
46 #define drm_connector_to_virtio_gpu_output(x) \
47 container_of(x, struct virtio_gpu_output, conn)
49 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
50 .set_config = drm_atomic_helper_set_config,
51 .destroy = drm_crtc_cleanup,
53 .page_flip = drm_atomic_helper_page_flip,
54 .reset = drm_atomic_helper_crtc_reset,
55 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
56 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
59 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
60 .create_handle = drm_gem_fb_create_handle,
61 .destroy = drm_gem_fb_destroy,
62 .dirty = drm_atomic_helper_dirtyfb,
66 virtio_gpu_framebuffer_init(struct drm_device *dev,
67 struct virtio_gpu_framebuffer *vgfb,
68 const struct drm_mode_fb_cmd2 *mode_cmd,
69 struct drm_gem_object *obj)
73 vgfb->base.obj[0] = obj;
75 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
77 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
79 vgfb->base.obj[0] = NULL;
85 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
87 struct drm_device *dev = crtc->dev;
88 struct virtio_gpu_device *vgdev = dev->dev_private;
89 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
91 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
93 crtc->mode.vdisplay, 0, 0);
94 virtio_gpu_notify(vgdev);
97 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
98 struct drm_crtc_state *old_state)
100 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
102 output->enabled = true;
105 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
106 struct drm_crtc_state *old_state)
108 struct drm_device *dev = crtc->dev;
109 struct virtio_gpu_device *vgdev = dev->dev_private;
110 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
112 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
113 virtio_gpu_notify(vgdev);
114 output->enabled = false;
117 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
118 struct drm_crtc_state *state)
123 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
124 struct drm_crtc_state *old_state)
128 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
129 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
130 .atomic_check = virtio_gpu_crtc_atomic_check,
131 .atomic_flush = virtio_gpu_crtc_atomic_flush,
132 .atomic_enable = virtio_gpu_crtc_atomic_enable,
133 .atomic_disable = virtio_gpu_crtc_atomic_disable,
136 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
137 struct drm_display_mode *mode,
138 struct drm_display_mode *adjusted_mode)
142 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
146 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
150 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
152 struct virtio_gpu_output *output =
153 drm_connector_to_virtio_gpu_output(connector);
154 struct drm_display_mode *mode = NULL;
155 int count, width, height;
158 count = drm_add_edid_modes(connector, output->edid);
163 width = le32_to_cpu(output->info.r.width);
164 height = le32_to_cpu(output->info.r.height);
165 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
167 if (width == 0 || height == 0) {
170 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
172 DRM_DEBUG("add mode: %dx%d\n", width, height);
173 mode = drm_cvt_mode(connector->dev, width, height, 60,
174 false, false, false);
175 mode->type |= DRM_MODE_TYPE_PREFERRED;
176 drm_mode_probed_add(connector, mode);
183 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
184 struct drm_display_mode *mode)
186 struct virtio_gpu_output *output =
187 drm_connector_to_virtio_gpu_output(connector);
190 width = le32_to_cpu(output->info.r.width);
191 height = le32_to_cpu(output->info.r.height);
193 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
195 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
197 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
198 mode->vdisplay <= height && mode->vdisplay >= height - 16)
201 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
205 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
206 .mode_set = virtio_gpu_enc_mode_set,
207 .enable = virtio_gpu_enc_enable,
208 .disable = virtio_gpu_enc_disable,
211 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
212 .get_modes = virtio_gpu_conn_get_modes,
213 .mode_valid = virtio_gpu_conn_mode_valid,
216 static enum drm_connector_status virtio_gpu_conn_detect(
217 struct drm_connector *connector,
220 struct virtio_gpu_output *output =
221 drm_connector_to_virtio_gpu_output(connector);
223 if (output->info.enabled)
224 return connector_status_connected;
226 return connector_status_disconnected;
229 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
231 drm_connector_unregister(connector);
232 drm_connector_cleanup(connector);
235 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
236 .detect = virtio_gpu_conn_detect,
237 .fill_modes = drm_helper_probe_single_connector_modes,
238 .destroy = virtio_gpu_conn_destroy,
239 .reset = drm_atomic_helper_connector_reset,
240 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
241 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
244 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
246 struct drm_device *dev = vgdev->ddev;
247 struct virtio_gpu_output *output = vgdev->outputs + index;
248 struct drm_connector *connector = &output->conn;
249 struct drm_encoder *encoder = &output->enc;
250 struct drm_crtc *crtc = &output->crtc;
251 struct drm_plane *primary, *cursor;
253 output->index = index;
255 output->info.enabled = cpu_to_le32(true);
256 output->info.r.width = cpu_to_le32(XRES_DEF);
257 output->info.r.height = cpu_to_le32(YRES_DEF);
260 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
262 return PTR_ERR(primary);
263 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
265 return PTR_ERR(cursor);
266 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
267 &virtio_gpu_crtc_funcs, NULL);
268 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
270 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
271 DRM_MODE_CONNECTOR_VIRTUAL);
272 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
274 drm_connector_attach_edid_property(connector);
276 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
277 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
278 encoder->possible_crtcs = 1 << index;
280 drm_connector_attach_encoder(connector, encoder);
281 drm_connector_register(connector);
285 static struct drm_framebuffer *
286 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
287 struct drm_file *file_priv,
288 const struct drm_mode_fb_cmd2 *mode_cmd)
290 struct drm_gem_object *obj = NULL;
291 struct virtio_gpu_framebuffer *virtio_gpu_fb;
294 if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
295 mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
296 return ERR_PTR(-ENOENT);
298 /* lookup object associated with res handle */
299 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
301 return ERR_PTR(-EINVAL);
303 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
304 if (virtio_gpu_fb == NULL)
305 return ERR_PTR(-ENOMEM);
307 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
309 kfree(virtio_gpu_fb);
310 drm_gem_object_put_unlocked(obj);
314 return &virtio_gpu_fb->base;
317 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
319 struct drm_device *dev = state->dev;
321 drm_atomic_helper_commit_modeset_disables(dev, state);
322 drm_atomic_helper_commit_modeset_enables(dev, state);
323 drm_atomic_helper_commit_planes(dev, state, 0);
325 drm_atomic_helper_fake_vblank(state);
326 drm_atomic_helper_commit_hw_done(state);
328 drm_atomic_helper_wait_for_vblanks(dev, state);
329 drm_atomic_helper_cleanup_planes(dev, state);
332 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
333 .atomic_commit_tail = vgdev_atomic_commit_tail,
336 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
337 .fb_create = virtio_gpu_user_framebuffer_create,
338 .atomic_check = drm_atomic_helper_check,
339 .atomic_commit = drm_atomic_helper_commit,
342 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
346 drm_mode_config_init(vgdev->ddev);
347 vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
348 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
349 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
351 /* modes will be validated against the framebuffer size */
352 vgdev->ddev->mode_config.min_width = XRES_MIN;
353 vgdev->ddev->mode_config.min_height = YRES_MIN;
354 vgdev->ddev->mode_config.max_width = XRES_MAX;
355 vgdev->ddev->mode_config.max_height = YRES_MAX;
357 for (i = 0 ; i < vgdev->num_scanouts; ++i)
358 vgdev_output_init(vgdev, i);
360 drm_mode_config_reset(vgdev->ddev);
363 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
367 for (i = 0 ; i < vgdev->num_scanouts; ++i)
368 kfree(vgdev->outputs[i].edid);
369 drm_mode_config_cleanup(vgdev->ddev);