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