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/dma-direction.h>
30 #include <linux/virtio.h>
31 #include <linux/virtio_ids.h>
32 #include <linux/virtio_config.h>
33 #include <linux/virtio_gpu.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_drv.h>
37 #include <drm/drm_encoder.h>
38 #include <drm/drm_fb_helper.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_gem.h>
41 #include <drm/drm_gem_shmem_helper.h>
42 #include <drm/drm_ioctl.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/virtgpu_drm.h>
46 #define DRIVER_NAME "virtio_gpu"
47 #define DRIVER_DESC "virtio GPU"
48 #define DRIVER_DATE "0"
50 #define DRIVER_MAJOR 0
51 #define DRIVER_MINOR 1
52 #define DRIVER_PATCHLEVEL 0
54 #define STATE_INITIALIZING 0
58 struct virtio_gpu_object_params {
65 /* classic resources only */
77 /* blob resources only */
84 struct virtio_gpu_object {
85 struct drm_gem_shmem_object base;
86 uint32_t hw_res_handle;
89 bool host3d_blob, guest_blob;
90 uint32_t blob_mem, blob_flags;
95 #define gem_to_virtio_gpu_obj(gobj) \
96 container_of((gobj), struct virtio_gpu_object, base.base)
98 struct virtio_gpu_object_shmem {
99 struct virtio_gpu_object base;
100 struct sg_table *pages;
104 struct virtio_gpu_object_vram {
105 struct virtio_gpu_object base;
108 struct drm_mm_node vram_node;
111 #define to_virtio_gpu_shmem(virtio_gpu_object) \
112 container_of((virtio_gpu_object), struct virtio_gpu_object_shmem, base)
114 #define to_virtio_gpu_vram(virtio_gpu_object) \
115 container_of((virtio_gpu_object), struct virtio_gpu_object_vram, base)
117 struct virtio_gpu_object_array {
118 struct ww_acquire_ctx ticket;
119 struct list_head next;
121 struct drm_gem_object *objs[];
124 struct virtio_gpu_vbuffer;
125 struct virtio_gpu_device;
127 typedef void (*virtio_gpu_resp_cb)(struct virtio_gpu_device *vgdev,
128 struct virtio_gpu_vbuffer *vbuf);
130 struct virtio_gpu_fence_driver {
131 atomic64_t last_fence_id;
132 uint64_t current_fence_id;
134 struct list_head fences;
138 struct virtio_gpu_fence {
141 struct virtio_gpu_fence_driver *drv;
142 struct list_head node;
145 struct virtio_gpu_vbuffer {
154 virtio_gpu_resp_cb resp_cb;
157 struct virtio_gpu_object_array *objs;
158 struct list_head list;
161 struct virtio_gpu_output {
163 struct drm_crtc crtc;
164 struct drm_connector conn;
165 struct drm_encoder enc;
166 struct virtio_gpu_display_one info;
167 struct virtio_gpu_update_cursor cursor;
173 #define drm_crtc_to_virtio_gpu_output(x) \
174 container_of(x, struct virtio_gpu_output, crtc)
176 struct virtio_gpu_framebuffer {
177 struct drm_framebuffer base;
178 struct virtio_gpu_fence *fence;
180 #define to_virtio_gpu_framebuffer(x) \
181 container_of(x, struct virtio_gpu_framebuffer, base)
183 struct virtio_gpu_queue {
184 struct virtqueue *vq;
186 wait_queue_head_t ack_queue;
187 struct work_struct dequeue_work;
190 struct virtio_gpu_drv_capset {
192 uint32_t max_version;
196 struct virtio_gpu_drv_cap_cache {
197 struct list_head head;
205 struct virtio_gpu_device {
207 struct drm_device *ddev;
209 struct virtio_device *vdev;
211 struct virtio_gpu_output outputs[VIRTIO_GPU_MAX_SCANOUTS];
212 uint32_t num_scanouts;
214 struct virtio_gpu_queue ctrlq;
215 struct virtio_gpu_queue cursorq;
216 struct kmem_cache *vbufs;
218 atomic_t pending_commands;
220 struct ida resource_ida;
222 wait_queue_head_t resp_wq;
223 /* current display info */
224 spinlock_t display_info_lock;
225 bool display_info_pending;
227 struct virtio_gpu_fence_driver fence_drv;
229 struct ida ctx_id_ida;
234 bool has_resource_assign_uuid;
235 bool has_resource_blob;
236 bool has_host_visible;
237 struct virtio_shm_region host_visible_region;
238 struct drm_mm host_visible_mm;
240 struct work_struct config_changed_work;
242 struct work_struct obj_free_work;
243 spinlock_t obj_free_lock;
244 struct list_head obj_free_list;
246 struct virtio_gpu_drv_capset *capsets;
247 uint32_t num_capsets;
248 struct list_head cap_cache;
250 /* protects uuid state when exporting */
251 spinlock_t resource_export_lock;
252 /* protects map state and host_visible_mm */
253 spinlock_t host_visible_lock;
256 struct virtio_gpu_fpriv {
258 bool context_created;
259 struct mutex context_lock;
262 /* virtgpu_ioctl.c */
263 #define DRM_VIRTIO_NUM_IOCTLS 11
264 extern struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS];
265 void virtio_gpu_create_context(struct drm_device *dev, struct drm_file *file);
268 int virtio_gpu_init(struct drm_device *dev);
269 void virtio_gpu_deinit(struct drm_device *dev);
270 void virtio_gpu_release(struct drm_device *dev);
271 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file);
272 void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file);
275 int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
276 struct drm_file *file);
277 void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
278 struct drm_file *file);
279 int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
280 struct drm_device *dev,
281 struct drm_mode_create_dumb *args);
282 int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
283 struct drm_device *dev,
284 uint32_t handle, uint64_t *offset_p);
286 struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents);
287 struct virtio_gpu_object_array*
288 virtio_gpu_array_from_handles(struct drm_file *drm_file, u32 *handles, u32 nents);
289 void virtio_gpu_array_add_obj(struct virtio_gpu_object_array *objs,
290 struct drm_gem_object *obj);
291 int virtio_gpu_array_lock_resv(struct virtio_gpu_object_array *objs);
292 void virtio_gpu_array_unlock_resv(struct virtio_gpu_object_array *objs);
293 void virtio_gpu_array_add_fence(struct virtio_gpu_object_array *objs,
294 struct dma_fence *fence);
295 void virtio_gpu_array_put_free(struct virtio_gpu_object_array *objs);
296 void virtio_gpu_array_put_free_delayed(struct virtio_gpu_device *vgdev,
297 struct virtio_gpu_object_array *objs);
298 void virtio_gpu_array_put_free_work(struct work_struct *work);
301 int virtio_gpu_alloc_vbufs(struct virtio_gpu_device *vgdev);
302 void virtio_gpu_free_vbufs(struct virtio_gpu_device *vgdev);
303 void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
304 struct virtio_gpu_object *bo,
305 struct virtio_gpu_object_params *params,
306 struct virtio_gpu_object_array *objs,
307 struct virtio_gpu_fence *fence);
308 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
309 struct virtio_gpu_object *bo);
310 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
312 uint32_t width, uint32_t height,
313 uint32_t x, uint32_t y,
314 struct virtio_gpu_object_array *objs,
315 struct virtio_gpu_fence *fence);
316 void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device *vgdev,
317 uint32_t resource_id,
318 uint32_t x, uint32_t y,
319 uint32_t width, uint32_t height,
320 struct virtio_gpu_object_array *objs,
321 struct virtio_gpu_fence *fence);
322 void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
323 uint32_t scanout_id, uint32_t resource_id,
324 uint32_t width, uint32_t height,
325 uint32_t x, uint32_t y);
326 void virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
327 struct virtio_gpu_object *obj,
328 struct virtio_gpu_mem_entry *ents,
330 int virtio_gpu_attach_status_page(struct virtio_gpu_device *vgdev);
331 int virtio_gpu_detach_status_page(struct virtio_gpu_device *vgdev);
332 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
333 struct virtio_gpu_output *output);
334 int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev);
335 int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device *vgdev, int idx);
336 int virtio_gpu_cmd_get_capset(struct virtio_gpu_device *vgdev,
337 int idx, int version,
338 struct virtio_gpu_drv_cap_cache **cache_p);
339 int virtio_gpu_cmd_get_edids(struct virtio_gpu_device *vgdev);
340 void virtio_gpu_cmd_context_create(struct virtio_gpu_device *vgdev, uint32_t id,
341 uint32_t nlen, const char *name);
342 void virtio_gpu_cmd_context_destroy(struct virtio_gpu_device *vgdev,
344 void virtio_gpu_cmd_context_attach_resource(struct virtio_gpu_device *vgdev,
346 struct virtio_gpu_object_array *objs);
347 void virtio_gpu_cmd_context_detach_resource(struct virtio_gpu_device *vgdev,
349 struct virtio_gpu_object_array *objs);
350 void virtio_gpu_cmd_submit(struct virtio_gpu_device *vgdev,
351 void *data, uint32_t data_size,
353 struct virtio_gpu_object_array *objs,
354 struct virtio_gpu_fence *fence);
355 void virtio_gpu_cmd_transfer_from_host_3d(struct virtio_gpu_device *vgdev,
357 uint64_t offset, uint32_t level,
359 uint32_t layer_stride,
360 struct drm_virtgpu_3d_box *box,
361 struct virtio_gpu_object_array *objs,
362 struct virtio_gpu_fence *fence);
363 void virtio_gpu_cmd_transfer_to_host_3d(struct virtio_gpu_device *vgdev,
365 uint64_t offset, uint32_t level,
367 uint32_t layer_stride,
368 struct drm_virtgpu_3d_box *box,
369 struct virtio_gpu_object_array *objs,
370 struct virtio_gpu_fence *fence);
372 virtio_gpu_cmd_resource_create_3d(struct virtio_gpu_device *vgdev,
373 struct virtio_gpu_object *bo,
374 struct virtio_gpu_object_params *params,
375 struct virtio_gpu_object_array *objs,
376 struct virtio_gpu_fence *fence);
377 void virtio_gpu_ctrl_ack(struct virtqueue *vq);
378 void virtio_gpu_cursor_ack(struct virtqueue *vq);
379 void virtio_gpu_fence_ack(struct virtqueue *vq);
380 void virtio_gpu_dequeue_ctrl_func(struct work_struct *work);
381 void virtio_gpu_dequeue_cursor_func(struct work_struct *work);
382 void virtio_gpu_dequeue_fence_func(struct work_struct *work);
384 void virtio_gpu_notify(struct virtio_gpu_device *vgdev);
387 virtio_gpu_cmd_resource_assign_uuid(struct virtio_gpu_device *vgdev,
388 struct virtio_gpu_object_array *objs);
390 int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev,
391 struct virtio_gpu_object_array *objs, uint64_t offset);
393 void virtio_gpu_cmd_unmap(struct virtio_gpu_device *vgdev,
394 struct virtio_gpu_object *bo);
397 virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev,
398 struct virtio_gpu_object *bo,
399 struct virtio_gpu_object_params *params,
400 struct virtio_gpu_mem_entry *ents,
403 virtio_gpu_cmd_set_scanout_blob(struct virtio_gpu_device *vgdev,
405 struct virtio_gpu_object *bo,
406 struct drm_framebuffer *fb,
407 uint32_t width, uint32_t height,
408 uint32_t x, uint32_t y);
410 /* virtgpu_display.c */
411 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev);
412 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev);
414 /* virtgpu_plane.c */
415 uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc);
416 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
417 enum drm_plane_type type,
420 /* virtgpu_fence.c */
421 struct virtio_gpu_fence *virtio_gpu_fence_alloc(
422 struct virtio_gpu_device *vgdev);
423 void virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
424 struct virtio_gpu_ctrl_hdr *cmd_hdr,
425 struct virtio_gpu_fence *fence);
426 void virtio_gpu_fence_event_process(struct virtio_gpu_device *vdev,
429 /* virtgpu_object.c */
430 void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo);
431 struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
433 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
434 struct virtio_gpu_object_params *params,
435 struct virtio_gpu_object **bo_ptr,
436 struct virtio_gpu_fence *fence);
438 bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
440 int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
442 /* virtgpu_prime.c */
443 int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
444 struct virtio_gpu_object *bo);
445 struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj,
447 struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
448 struct dma_buf *buf);
449 int virtgpu_gem_prime_get_uuid(struct drm_gem_object *obj,
451 struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
452 struct drm_device *dev, struct dma_buf_attachment *attach,
453 struct sg_table *sgt);
455 /* virtgpu_debugfs.c */
456 void virtio_gpu_debugfs_init(struct drm_minor *minor);
459 bool virtio_gpu_is_vram(struct virtio_gpu_object *bo);
460 int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,
461 struct virtio_gpu_object_params *params,
462 struct virtio_gpu_object **bo_ptr);
463 struct sg_table *virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo,
465 enum dma_data_direction dir);
466 void virtio_gpu_vram_unmap_dma_buf(struct device *dev,
467 struct sg_table *sgt,
468 enum dma_data_direction dir);