2 * Copyright (C) 2015 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <linux/virtio.h>
30 #include <linux/virtio_ids.h>
31 #include <linux/virtio_config.h>
32 #include <linux/virtio_gpu.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_encoder.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_gem.h>
40 #include <drm/drm_gem_shmem_helper.h>
41 #include <drm/drm_ioctl.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/virtgpu_drm.h>
45 #define DRIVER_NAME "virtio_gpu"
46 #define DRIVER_DESC "virtio GPU"
47 #define DRIVER_DATE "0"
49 #define DRIVER_MAJOR 0
50 #define DRIVER_MINOR 1
51 #define DRIVER_PATCHLEVEL 0
53 #define STATE_INITIALIZING 0
57 struct virtio_gpu_object_params
{
64 /* classic resources only */
76 /* blob resources only */
83 struct virtio_gpu_object
{
84 struct drm_gem_shmem_object base
;
85 uint32_t hw_res_handle
;
88 bool host3d_blob
, guest_blob
;
89 uint32_t blob_mem
, blob_flags
;
94 #define gem_to_virtio_gpu_obj(gobj) \
95 container_of((gobj), struct virtio_gpu_object, base.base)
97 struct virtio_gpu_object_shmem
{
98 struct virtio_gpu_object base
;
99 struct sg_table
*pages
;
103 struct virtio_gpu_object_vram
{
104 struct virtio_gpu_object base
;
107 struct drm_mm_node vram_node
;
110 #define to_virtio_gpu_shmem(virtio_gpu_object) \
111 container_of((virtio_gpu_object), struct virtio_gpu_object_shmem, base)
113 #define to_virtio_gpu_vram(virtio_gpu_object) \
114 container_of((virtio_gpu_object), struct virtio_gpu_object_vram, base)
116 struct virtio_gpu_object_array
{
117 struct ww_acquire_ctx ticket
;
118 struct list_head next
;
120 struct drm_gem_object
*objs
[];
123 struct virtio_gpu_vbuffer
;
124 struct virtio_gpu_device
;
126 typedef void (*virtio_gpu_resp_cb
)(struct virtio_gpu_device
*vgdev
,
127 struct virtio_gpu_vbuffer
*vbuf
);
129 struct virtio_gpu_fence_driver
{
130 atomic64_t last_fence_id
;
131 uint64_t current_fence_id
;
133 struct list_head fences
;
137 struct virtio_gpu_fence
{
139 struct virtio_gpu_fence_driver
*drv
;
140 struct list_head node
;
143 struct virtio_gpu_vbuffer
{
152 virtio_gpu_resp_cb resp_cb
;
155 struct virtio_gpu_object_array
*objs
;
156 struct list_head list
;
159 struct virtio_gpu_output
{
161 struct drm_crtc crtc
;
162 struct drm_connector conn
;
163 struct drm_encoder enc
;
164 struct virtio_gpu_display_one info
;
165 struct virtio_gpu_update_cursor cursor
;
171 #define drm_crtc_to_virtio_gpu_output(x) \
172 container_of(x, struct virtio_gpu_output, crtc)
174 struct virtio_gpu_framebuffer
{
175 struct drm_framebuffer base
;
176 struct virtio_gpu_fence
*fence
;
178 #define to_virtio_gpu_framebuffer(x) \
179 container_of(x, struct virtio_gpu_framebuffer, base)
181 struct virtio_gpu_queue
{
182 struct virtqueue
*vq
;
184 wait_queue_head_t ack_queue
;
185 struct work_struct dequeue_work
;
188 struct virtio_gpu_drv_capset
{
190 uint32_t max_version
;
194 struct virtio_gpu_drv_cap_cache
{
195 struct list_head head
;
203 struct virtio_gpu_device
{
205 struct drm_device
*ddev
;
207 struct virtio_device
*vdev
;
209 struct virtio_gpu_output outputs
[VIRTIO_GPU_MAX_SCANOUTS
];
210 uint32_t num_scanouts
;
212 struct virtio_gpu_queue ctrlq
;
213 struct virtio_gpu_queue cursorq
;
214 struct kmem_cache
*vbufs
;
216 atomic_t pending_commands
;
218 struct ida resource_ida
;
220 wait_queue_head_t resp_wq
;
221 /* current display info */
222 spinlock_t display_info_lock
;
223 bool display_info_pending
;
225 struct virtio_gpu_fence_driver fence_drv
;
227 struct ida ctx_id_ida
;
232 bool has_resource_assign_uuid
;
233 bool has_resource_blob
;
234 bool has_host_visible
;
235 struct virtio_shm_region host_visible_region
;
236 struct drm_mm host_visible_mm
;
238 struct work_struct config_changed_work
;
240 struct work_struct obj_free_work
;
241 spinlock_t obj_free_lock
;
242 struct list_head obj_free_list
;
244 struct virtio_gpu_drv_capset
*capsets
;
245 uint32_t num_capsets
;
246 struct list_head cap_cache
;
248 /* protects uuid state when exporting */
249 spinlock_t resource_export_lock
;
250 /* protects map state and host_visible_mm */
251 spinlock_t host_visible_lock
;
254 struct virtio_gpu_fpriv
{
256 bool context_created
;
257 struct mutex context_lock
;
260 /* virtgpu_ioctl.c */
261 #define DRM_VIRTIO_NUM_IOCTLS 11
262 extern struct drm_ioctl_desc virtio_gpu_ioctls
[DRM_VIRTIO_NUM_IOCTLS
];
263 void virtio_gpu_create_context(struct drm_device
*dev
, struct drm_file
*file
);
266 int virtio_gpu_init(struct drm_device
*dev
);
267 void virtio_gpu_deinit(struct drm_device
*dev
);
268 void virtio_gpu_release(struct drm_device
*dev
);
269 int virtio_gpu_driver_open(struct drm_device
*dev
, struct drm_file
*file
);
270 void virtio_gpu_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
);
273 int virtio_gpu_gem_object_open(struct drm_gem_object
*obj
,
274 struct drm_file
*file
);
275 void virtio_gpu_gem_object_close(struct drm_gem_object
*obj
,
276 struct drm_file
*file
);
277 int virtio_gpu_mode_dumb_create(struct drm_file
*file_priv
,
278 struct drm_device
*dev
,
279 struct drm_mode_create_dumb
*args
);
280 int virtio_gpu_mode_dumb_mmap(struct drm_file
*file_priv
,
281 struct drm_device
*dev
,
282 uint32_t handle
, uint64_t *offset_p
);
284 struct virtio_gpu_object_array
*virtio_gpu_array_alloc(u32 nents
);
285 struct virtio_gpu_object_array
*
286 virtio_gpu_array_from_handles(struct drm_file
*drm_file
, u32
*handles
, u32 nents
);
287 void virtio_gpu_array_add_obj(struct virtio_gpu_object_array
*objs
,
288 struct drm_gem_object
*obj
);
289 int virtio_gpu_array_lock_resv(struct virtio_gpu_object_array
*objs
);
290 void virtio_gpu_array_unlock_resv(struct virtio_gpu_object_array
*objs
);
291 void virtio_gpu_array_add_fence(struct virtio_gpu_object_array
*objs
,
292 struct dma_fence
*fence
);
293 void virtio_gpu_array_put_free(struct virtio_gpu_object_array
*objs
);
294 void virtio_gpu_array_put_free_delayed(struct virtio_gpu_device
*vgdev
,
295 struct virtio_gpu_object_array
*objs
);
296 void virtio_gpu_array_put_free_work(struct work_struct
*work
);
299 int virtio_gpu_alloc_vbufs(struct virtio_gpu_device
*vgdev
);
300 void virtio_gpu_free_vbufs(struct virtio_gpu_device
*vgdev
);
301 void virtio_gpu_cmd_create_resource(struct virtio_gpu_device
*vgdev
,
302 struct virtio_gpu_object
*bo
,
303 struct virtio_gpu_object_params
*params
,
304 struct virtio_gpu_object_array
*objs
,
305 struct virtio_gpu_fence
*fence
);
306 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device
*vgdev
,
307 struct virtio_gpu_object
*bo
);
308 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device
*vgdev
,
310 uint32_t width
, uint32_t height
,
311 uint32_t x
, uint32_t y
,
312 struct virtio_gpu_object_array
*objs
,
313 struct virtio_gpu_fence
*fence
);
314 void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device
*vgdev
,
315 uint32_t resource_id
,
316 uint32_t x
, uint32_t y
,
317 uint32_t width
, uint32_t height
);
318 void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device
*vgdev
,
319 uint32_t scanout_id
, uint32_t resource_id
,
320 uint32_t width
, uint32_t height
,
321 uint32_t x
, uint32_t y
);
322 void virtio_gpu_object_attach(struct virtio_gpu_device
*vgdev
,
323 struct virtio_gpu_object
*obj
,
324 struct virtio_gpu_mem_entry
*ents
,
326 int virtio_gpu_attach_status_page(struct virtio_gpu_device
*vgdev
);
327 int virtio_gpu_detach_status_page(struct virtio_gpu_device
*vgdev
);
328 void virtio_gpu_cursor_ping(struct virtio_gpu_device
*vgdev
,
329 struct virtio_gpu_output
*output
);
330 int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device
*vgdev
);
331 int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device
*vgdev
, int idx
);
332 int virtio_gpu_cmd_get_capset(struct virtio_gpu_device
*vgdev
,
333 int idx
, int version
,
334 struct virtio_gpu_drv_cap_cache
**cache_p
);
335 int virtio_gpu_cmd_get_edids(struct virtio_gpu_device
*vgdev
);
336 void virtio_gpu_cmd_context_create(struct virtio_gpu_device
*vgdev
, uint32_t id
,
337 uint32_t nlen
, const char *name
);
338 void virtio_gpu_cmd_context_destroy(struct virtio_gpu_device
*vgdev
,
340 void virtio_gpu_cmd_context_attach_resource(struct virtio_gpu_device
*vgdev
,
342 struct virtio_gpu_object_array
*objs
);
343 void virtio_gpu_cmd_context_detach_resource(struct virtio_gpu_device
*vgdev
,
345 struct virtio_gpu_object_array
*objs
);
346 void virtio_gpu_cmd_submit(struct virtio_gpu_device
*vgdev
,
347 void *data
, uint32_t data_size
,
349 struct virtio_gpu_object_array
*objs
,
350 struct virtio_gpu_fence
*fence
);
351 void virtio_gpu_cmd_transfer_from_host_3d(struct virtio_gpu_device
*vgdev
,
353 uint64_t offset
, uint32_t level
,
355 uint32_t layer_stride
,
356 struct drm_virtgpu_3d_box
*box
,
357 struct virtio_gpu_object_array
*objs
,
358 struct virtio_gpu_fence
*fence
);
359 void virtio_gpu_cmd_transfer_to_host_3d(struct virtio_gpu_device
*vgdev
,
361 uint64_t offset
, uint32_t level
,
363 uint32_t layer_stride
,
364 struct drm_virtgpu_3d_box
*box
,
365 struct virtio_gpu_object_array
*objs
,
366 struct virtio_gpu_fence
*fence
);
368 virtio_gpu_cmd_resource_create_3d(struct virtio_gpu_device
*vgdev
,
369 struct virtio_gpu_object
*bo
,
370 struct virtio_gpu_object_params
*params
,
371 struct virtio_gpu_object_array
*objs
,
372 struct virtio_gpu_fence
*fence
);
373 void virtio_gpu_ctrl_ack(struct virtqueue
*vq
);
374 void virtio_gpu_cursor_ack(struct virtqueue
*vq
);
375 void virtio_gpu_fence_ack(struct virtqueue
*vq
);
376 void virtio_gpu_dequeue_ctrl_func(struct work_struct
*work
);
377 void virtio_gpu_dequeue_cursor_func(struct work_struct
*work
);
378 void virtio_gpu_dequeue_fence_func(struct work_struct
*work
);
380 void virtio_gpu_notify(struct virtio_gpu_device
*vgdev
);
383 virtio_gpu_cmd_resource_assign_uuid(struct virtio_gpu_device
*vgdev
,
384 struct virtio_gpu_object_array
*objs
);
386 int virtio_gpu_cmd_map(struct virtio_gpu_device
*vgdev
,
387 struct virtio_gpu_object_array
*objs
, uint64_t offset
);
389 void virtio_gpu_cmd_unmap(struct virtio_gpu_device
*vgdev
,
390 struct virtio_gpu_object
*bo
);
393 virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device
*vgdev
,
394 struct virtio_gpu_object
*bo
,
395 struct virtio_gpu_object_params
*params
,
396 struct virtio_gpu_mem_entry
*ents
,
399 virtio_gpu_cmd_set_scanout_blob(struct virtio_gpu_device
*vgdev
,
401 struct virtio_gpu_object
*bo
,
402 struct drm_framebuffer
*fb
,
403 uint32_t width
, uint32_t height
,
404 uint32_t x
, uint32_t y
);
406 /* virtgpu_display.c */
407 int virtio_gpu_modeset_init(struct virtio_gpu_device
*vgdev
);
408 void virtio_gpu_modeset_fini(struct virtio_gpu_device
*vgdev
);
410 /* virtgpu_plane.c */
411 uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc
);
412 struct drm_plane
*virtio_gpu_plane_init(struct virtio_gpu_device
*vgdev
,
413 enum drm_plane_type type
,
416 /* virtgpu_fence.c */
417 struct virtio_gpu_fence
*virtio_gpu_fence_alloc(
418 struct virtio_gpu_device
*vgdev
);
419 void virtio_gpu_fence_emit(struct virtio_gpu_device
*vgdev
,
420 struct virtio_gpu_ctrl_hdr
*cmd_hdr
,
421 struct virtio_gpu_fence
*fence
);
422 void virtio_gpu_fence_event_process(struct virtio_gpu_device
*vdev
,
425 /* virtgpu_object.c */
426 void virtio_gpu_cleanup_object(struct virtio_gpu_object
*bo
);
427 struct drm_gem_object
*virtio_gpu_create_object(struct drm_device
*dev
,
429 int virtio_gpu_object_create(struct virtio_gpu_device
*vgdev
,
430 struct virtio_gpu_object_params
*params
,
431 struct virtio_gpu_object
**bo_ptr
,
432 struct virtio_gpu_fence
*fence
);
434 bool virtio_gpu_is_shmem(struct virtio_gpu_object
*bo
);
436 int virtio_gpu_resource_id_get(struct virtio_gpu_device
*vgdev
,
438 /* virtgpu_prime.c */
439 int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device
*vgdev
,
440 struct virtio_gpu_object
*bo
);
441 struct dma_buf
*virtgpu_gem_prime_export(struct drm_gem_object
*obj
,
443 struct drm_gem_object
*virtgpu_gem_prime_import(struct drm_device
*dev
,
444 struct dma_buf
*buf
);
445 int virtgpu_gem_prime_get_uuid(struct drm_gem_object
*obj
,
447 struct drm_gem_object
*virtgpu_gem_prime_import_sg_table(
448 struct drm_device
*dev
, struct dma_buf_attachment
*attach
,
449 struct sg_table
*sgt
);
451 /* virtgpu_debugfs.c */
452 void virtio_gpu_debugfs_init(struct drm_minor
*minor
);
455 bool virtio_gpu_is_vram(struct virtio_gpu_object
*bo
);
456 int virtio_gpu_vram_create(struct virtio_gpu_device
*vgdev
,
457 struct virtio_gpu_object_params
*params
,
458 struct virtio_gpu_object
**bo_ptr
);