1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
4 *
5 * Authors:
6 * Dave Airlie
7 * Alon Levy
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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.
26 */
27
28 #include "virtgpu_drv.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32
33 #define XRES_MIN 32
34 #define YRES_MIN 32
35
36 #define XRES_DEF 1024
37 #define YRES_DEF 768
38
39 #define XRES_MAX 8192
40 #define YRES_MAX 8192
41
42 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
43 .set_config = drm_atomic_helper_set_config,
44 .destroy = drm_crtc_cleanup,
45
46 .page_flip = drm_atomic_helper_page_flip,
47 .reset = drm_atomic_helper_crtc_reset,
48 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
49 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
50 };
51
52 static int
virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)53 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
54 struct drm_file *file_priv,
55 unsigned int flags, unsigned int color,
56 struct drm_clip_rect *clips,
57 unsigned int num_clips)
58 {
59 struct virtio_gpu_framebuffer *virtio_gpu_fb
60 = to_virtio_gpu_framebuffer(fb);
61
62 return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
63 }
64
65 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
66 .create_handle = drm_gem_fb_create_handle,
67 .destroy = drm_gem_fb_destroy,
68 .dirty = virtio_gpu_framebuffer_surface_dirty,
69 };
70
71 int
virtio_gpu_framebuffer_init(struct drm_device * dev,struct virtio_gpu_framebuffer * vgfb,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object * obj)72 virtio_gpu_framebuffer_init(struct drm_device *dev,
73 struct virtio_gpu_framebuffer *vgfb,
74 const struct drm_mode_fb_cmd2 *mode_cmd,
75 struct drm_gem_object *obj)
76 {
77 int ret;
78 struct virtio_gpu_object *bo;
79
80 vgfb->base.obj[0] = obj;
81
82 bo = gem_to_virtio_gpu_obj(obj);
83
84 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
85
86 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
87 if (ret) {
88 vgfb->base.obj[0] = NULL;
89 return ret;
90 }
91
92 spin_lock_init(&vgfb->dirty_lock);
93 vgfb->x1 = vgfb->y1 = INT_MAX;
94 vgfb->x2 = vgfb->y2 = 0;
95 return 0;
96 }
97
virtio_gpu_crtc_mode_set_nofb(struct drm_crtc * crtc)98 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
99 {
100 struct drm_device *dev = crtc->dev;
101 struct virtio_gpu_device *vgdev = dev->dev_private;
102 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
103
104 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
105 crtc->mode.hdisplay,
106 crtc->mode.vdisplay, 0, 0);
107 }
108
virtio_gpu_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)109 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
110 struct drm_crtc_state *old_state)
111 {
112 }
113
virtio_gpu_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)114 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
115 struct drm_crtc_state *old_state)
116 {
117 struct drm_device *dev = crtc->dev;
118 struct virtio_gpu_device *vgdev = dev->dev_private;
119 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
120
121 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
122 }
123
virtio_gpu_crtc_atomic_check(struct drm_crtc * crtc,struct drm_crtc_state * state)124 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
125 struct drm_crtc_state *state)
126 {
127 return 0;
128 }
129
virtio_gpu_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_state)130 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
131 struct drm_crtc_state *old_state)
132 {
133 unsigned long flags;
134
135 spin_lock_irqsave(&crtc->dev->event_lock, flags);
136 if (crtc->state->event)
137 drm_crtc_send_vblank_event(crtc, crtc->state->event);
138 crtc->state->event = NULL;
139 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
140 }
141
142 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
143 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
144 .atomic_check = virtio_gpu_crtc_atomic_check,
145 .atomic_flush = virtio_gpu_crtc_atomic_flush,
146 .atomic_enable = virtio_gpu_crtc_atomic_enable,
147 .atomic_disable = virtio_gpu_crtc_atomic_disable,
148 };
149
virtio_gpu_enc_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)150 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
151 struct drm_display_mode *mode,
152 struct drm_display_mode *adjusted_mode)
153 {
154 }
155
virtio_gpu_enc_enable(struct drm_encoder * encoder)156 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
157 {
158 }
159
virtio_gpu_enc_disable(struct drm_encoder * encoder)160 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
161 {
162 }
163
virtio_gpu_conn_get_modes(struct drm_connector * connector)164 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
165 {
166 struct virtio_gpu_output *output =
167 drm_connector_to_virtio_gpu_output(connector);
168 struct drm_display_mode *mode = NULL;
169 int count, width, height;
170
171 width = le32_to_cpu(output->info.r.width);
172 height = le32_to_cpu(output->info.r.height);
173 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
174
175 if (width == 0 || height == 0) {
176 width = XRES_DEF;
177 height = YRES_DEF;
178 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
179 } else {
180 DRM_DEBUG("add mode: %dx%d\n", width, height);
181 mode = drm_cvt_mode(connector->dev, width, height, 60,
182 false, false, false);
183 mode->type |= DRM_MODE_TYPE_PREFERRED;
184 drm_mode_probed_add(connector, mode);
185 count++;
186 }
187
188 return count;
189 }
190
virtio_gpu_conn_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)191 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
192 struct drm_display_mode *mode)
193 {
194 struct virtio_gpu_output *output =
195 drm_connector_to_virtio_gpu_output(connector);
196 int width, height;
197
198 width = le32_to_cpu(output->info.r.width);
199 height = le32_to_cpu(output->info.r.height);
200
201 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
202 return MODE_OK;
203 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
204 return MODE_OK;
205 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
206 mode->vdisplay <= height && mode->vdisplay >= height - 16)
207 return MODE_OK;
208
209 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
210 return MODE_BAD;
211 }
212
213 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
214 .mode_set = virtio_gpu_enc_mode_set,
215 .enable = virtio_gpu_enc_enable,
216 .disable = virtio_gpu_enc_disable,
217 };
218
219 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
220 .get_modes = virtio_gpu_conn_get_modes,
221 .mode_valid = virtio_gpu_conn_mode_valid,
222 };
223
virtio_gpu_conn_detect(struct drm_connector * connector,bool force)224 static enum drm_connector_status virtio_gpu_conn_detect(
225 struct drm_connector *connector,
226 bool force)
227 {
228 struct virtio_gpu_output *output =
229 drm_connector_to_virtio_gpu_output(connector);
230
231 if (output->info.enabled)
232 return connector_status_connected;
233 else
234 return connector_status_disconnected;
235 }
236
virtio_gpu_conn_destroy(struct drm_connector * connector)237 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
238 {
239 struct virtio_gpu_output *virtio_gpu_output =
240 drm_connector_to_virtio_gpu_output(connector);
241
242 drm_connector_unregister(connector);
243 drm_connector_cleanup(connector);
244 kfree(virtio_gpu_output);
245 }
246
247 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
248 .detect = virtio_gpu_conn_detect,
249 .fill_modes = drm_helper_probe_single_connector_modes,
250 .destroy = virtio_gpu_conn_destroy,
251 .reset = drm_atomic_helper_connector_reset,
252 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
253 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
254 };
255
256 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
257 .destroy = drm_encoder_cleanup,
258 };
259
vgdev_output_init(struct virtio_gpu_device * vgdev,int index)260 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
261 {
262 struct drm_device *dev = vgdev->ddev;
263 struct virtio_gpu_output *output = vgdev->outputs + index;
264 struct drm_connector *connector = &output->conn;
265 struct drm_encoder *encoder = &output->enc;
266 struct drm_crtc *crtc = &output->crtc;
267 struct drm_plane *primary, *cursor;
268
269 output->index = index;
270 if (index == 0) {
271 output->info.enabled = cpu_to_le32(true);
272 output->info.r.width = cpu_to_le32(XRES_DEF);
273 output->info.r.height = cpu_to_le32(YRES_DEF);
274 }
275
276 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
277 if (IS_ERR(primary))
278 return PTR_ERR(primary);
279 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
280 if (IS_ERR(cursor))
281 return PTR_ERR(cursor);
282 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
283 &virtio_gpu_crtc_funcs, NULL);
284 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
285
286 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
287 DRM_MODE_CONNECTOR_VIRTUAL);
288 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
289
290 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
291 DRM_MODE_ENCODER_VIRTUAL, NULL);
292 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
293 encoder->possible_crtcs = 1 << index;
294
295 drm_connector_attach_encoder(connector, encoder);
296 drm_connector_register(connector);
297 return 0;
298 }
299
300 static struct drm_framebuffer *
virtio_gpu_user_framebuffer_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)301 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
302 struct drm_file *file_priv,
303 const struct drm_mode_fb_cmd2 *mode_cmd)
304 {
305 struct drm_gem_object *obj = NULL;
306 struct virtio_gpu_framebuffer *virtio_gpu_fb;
307 int ret;
308
309 /* lookup object associated with res handle */
310 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
311 if (!obj)
312 return ERR_PTR(-EINVAL);
313
314 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
315 if (virtio_gpu_fb == NULL)
316 return ERR_PTR(-ENOMEM);
317
318 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
319 if (ret) {
320 kfree(virtio_gpu_fb);
321 drm_gem_object_put_unlocked(obj);
322 return NULL;
323 }
324
325 return &virtio_gpu_fb->base;
326 }
327
vgdev_atomic_commit_tail(struct drm_atomic_state * state)328 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
329 {
330 struct drm_device *dev = state->dev;
331
332 drm_atomic_helper_commit_modeset_disables(dev, state);
333 drm_atomic_helper_commit_modeset_enables(dev, state);
334 drm_atomic_helper_commit_planes(dev, state, 0);
335
336 drm_atomic_helper_commit_hw_done(state);
337
338 drm_atomic_helper_wait_for_vblanks(dev, state);
339 drm_atomic_helper_cleanup_planes(dev, state);
340 }
341
342 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
343 .atomic_commit_tail = vgdev_atomic_commit_tail,
344 };
345
346 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
347 .fb_create = virtio_gpu_user_framebuffer_create,
348 .atomic_check = drm_atomic_helper_check,
349 .atomic_commit = drm_atomic_helper_commit,
350 };
351
virtio_gpu_modeset_init(struct virtio_gpu_device * vgdev)352 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
353 {
354 int i;
355
356 drm_mode_config_init(vgdev->ddev);
357 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
358 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
359
360 /* modes will be validated against the framebuffer size */
361 vgdev->ddev->mode_config.min_width = XRES_MIN;
362 vgdev->ddev->mode_config.min_height = YRES_MIN;
363 vgdev->ddev->mode_config.max_width = XRES_MAX;
364 vgdev->ddev->mode_config.max_height = YRES_MAX;
365
366 for (i = 0 ; i < vgdev->num_scanouts; ++i)
367 vgdev_output_init(vgdev, i);
368
369 drm_mode_config_reset(vgdev->ddev);
370 return 0;
371 }
372
virtio_gpu_modeset_fini(struct virtio_gpu_device * vgdev)373 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
374 {
375 virtio_gpu_fbdev_fini(vgdev);
376 drm_mode_config_cleanup(vgdev->ddev);
377 }
378