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>
35 #include <drm/drm_gem.h>
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_crtc_helper.h>
38 #include <ttm/ttm_bo_api.h>
39 #include <ttm/ttm_bo_driver.h>
40 #include <ttm/ttm_placement.h>
41 #include <ttm/ttm_module.h>
43 #define DRIVER_NAME "virtio_gpu"
44 #define DRIVER_DESC "virtio GPU"
45 #define DRIVER_DATE "0"
47 #define DRIVER_MAJOR 0
48 #define DRIVER_MINOR 0
49 #define DRIVER_PATCHLEVEL 1
51 /* virtgpu_drm_bus.c */
52 int drm_virtio_set_busid(struct drm_device
*dev
, struct drm_master
*master
);
53 int drm_virtio_init(struct drm_driver
*driver
, struct virtio_device
*vdev
);
55 struct virtio_gpu_object
{
56 struct drm_gem_object gem_base
;
57 uint32_t hw_res_handle
;
59 struct sg_table
*pages
;
62 struct ttm_place placement_code
;
63 struct ttm_placement placement
;
64 struct ttm_buffer_object tbo
;
65 struct ttm_bo_kmap_obj kmap
;
67 #define gem_to_virtio_gpu_obj(gobj) \
68 container_of((gobj), struct virtio_gpu_object, gem_base)
70 struct virtio_gpu_vbuffer
;
71 struct virtio_gpu_device
;
73 typedef void (*virtio_gpu_resp_cb
)(struct virtio_gpu_device
*vgdev
,
74 struct virtio_gpu_vbuffer
*vbuf
);
76 struct virtio_gpu_fence_driver
{
80 struct list_head fences
;
84 struct virtio_gpu_fence
{
86 struct virtio_gpu_fence_driver
*drv
;
87 struct list_head node
;
90 #define to_virtio_fence(x) \
91 container_of(x, struct virtio_gpu_fence, f)
93 struct virtio_gpu_vbuffer
{
103 virtio_gpu_resp_cb resp_cb
;
105 struct list_head list
;
108 struct virtio_gpu_output
{
110 struct drm_crtc crtc
;
111 struct drm_connector conn
;
112 struct drm_encoder enc
;
113 struct virtio_gpu_display_one info
;
114 struct virtio_gpu_update_cursor cursor
;
118 #define drm_crtc_to_virtio_gpu_output(x) \
119 container_of(x, struct virtio_gpu_output, crtc)
120 #define drm_connector_to_virtio_gpu_output(x) \
121 container_of(x, struct virtio_gpu_output, conn)
122 #define drm_encoder_to_virtio_gpu_output(x) \
123 container_of(x, struct virtio_gpu_output, enc)
125 struct virtio_gpu_framebuffer
{
126 struct drm_framebuffer base
;
127 struct drm_gem_object
*obj
;
128 int x1
, y1
, x2
, y2
; /* dirty rect */
129 spinlock_t dirty_lock
;
130 uint32_t hw_res_handle
;
132 #define to_virtio_gpu_framebuffer(x) \
133 container_of(x, struct virtio_gpu_framebuffer, base)
135 struct virtio_gpu_mman
{
136 struct ttm_bo_global_ref bo_global_ref
;
137 struct drm_global_reference mem_global_ref
;
138 bool mem_global_referenced
;
139 struct ttm_bo_device bdev
;
142 struct virtio_gpu_fbdev
;
144 struct virtio_gpu_queue
{
145 struct virtqueue
*vq
;
147 wait_queue_head_t ack_queue
;
148 struct work_struct dequeue_work
;
151 struct virtio_gpu_drv_capset
{
153 uint32_t max_version
;
157 struct virtio_gpu_drv_cap_cache
{
158 struct list_head head
;
166 struct virtio_gpu_device
{
168 struct drm_device
*ddev
;
170 struct virtio_device
*vdev
;
172 struct virtio_gpu_mman mman
;
174 /* pointer to fbdev info structure */
175 struct virtio_gpu_fbdev
*vgfbdev
;
176 struct virtio_gpu_output outputs
[VIRTIO_GPU_MAX_SCANOUTS
];
177 uint32_t num_scanouts
;
179 struct virtio_gpu_queue ctrlq
;
180 struct virtio_gpu_queue cursorq
;
181 struct list_head free_vbufs
;
182 spinlock_t free_vbufs_lock
;
186 struct idr resource_idr
;
187 spinlock_t resource_idr_lock
;
189 wait_queue_head_t resp_wq
;
190 /* current display info */
191 spinlock_t display_info_lock
;
192 bool display_info_pending
;
194 struct virtio_gpu_fence_driver fence_drv
;
196 struct idr ctx_id_idr
;
197 spinlock_t ctx_id_idr_lock
;
201 struct work_struct config_changed_work
;
203 struct virtio_gpu_drv_capset
*capsets
;
204 uint32_t num_capsets
;
205 struct list_head cap_cache
;
208 struct virtio_gpu_fpriv
{
213 #define DRM_VIRTIO_NUM_IOCTLS 10
214 extern struct drm_ioctl_desc virtio_gpu_ioctls
[DRM_VIRTIO_NUM_IOCTLS
];
217 int virtio_gpu_driver_load(struct drm_device
*dev
, unsigned long flags
);
218 int virtio_gpu_driver_unload(struct drm_device
*dev
);
219 int virtio_gpu_driver_open(struct drm_device
*dev
, struct drm_file
*file
);
220 void virtio_gpu_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
);
223 void virtio_gpu_gem_free_object(struct drm_gem_object
*gem_obj
);
224 int virtio_gpu_gem_init(struct virtio_gpu_device
*vgdev
);
225 void virtio_gpu_gem_fini(struct virtio_gpu_device
*vgdev
);
226 int virtio_gpu_gem_create(struct drm_file
*file
,
227 struct drm_device
*dev
,
229 struct drm_gem_object
**obj_p
,
231 int virtio_gpu_gem_object_open(struct drm_gem_object
*obj
,
232 struct drm_file
*file
);
233 void virtio_gpu_gem_object_close(struct drm_gem_object
*obj
,
234 struct drm_file
*file
);
235 struct virtio_gpu_object
*virtio_gpu_alloc_object(struct drm_device
*dev
,
236 size_t size
, bool kernel
,
238 int virtio_gpu_mode_dumb_create(struct drm_file
*file_priv
,
239 struct drm_device
*dev
,
240 struct drm_mode_create_dumb
*args
);
241 int virtio_gpu_mode_dumb_destroy(struct drm_file
*file_priv
,
242 struct drm_device
*dev
,
244 int virtio_gpu_mode_dumb_mmap(struct drm_file
*file_priv
,
245 struct drm_device
*dev
,
246 uint32_t handle
, uint64_t *offset_p
);
249 #define VIRTIO_GPUFB_CONN_LIMIT 1
250 int virtio_gpu_fbdev_init(struct virtio_gpu_device
*vgdev
);
251 void virtio_gpu_fbdev_fini(struct virtio_gpu_device
*vgdev
);
252 int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer
*qfb
,
253 struct drm_clip_rect
*clips
,
256 int virtio_gpu_alloc_vbufs(struct virtio_gpu_device
*vgdev
);
257 void virtio_gpu_free_vbufs(struct virtio_gpu_device
*vgdev
);
258 void virtio_gpu_resource_id_get(struct virtio_gpu_device
*vgdev
,
260 void virtio_gpu_resource_id_put(struct virtio_gpu_device
*vgdev
, uint32_t id
);
261 void virtio_gpu_cmd_create_resource(struct virtio_gpu_device
*vgdev
,
262 uint32_t resource_id
,
266 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device
*vgdev
,
267 uint32_t resource_id
);
268 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device
*vgdev
,
269 uint32_t resource_id
, uint64_t offset
,
270 __le32 width
, __le32 height
,
272 struct virtio_gpu_fence
**fence
);
273 void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device
*vgdev
,
274 uint32_t resource_id
,
275 uint32_t x
, uint32_t y
,
276 uint32_t width
, uint32_t height
);
277 void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device
*vgdev
,
278 uint32_t scanout_id
, uint32_t resource_id
,
279 uint32_t width
, uint32_t height
,
280 uint32_t x
, uint32_t y
);
281 int virtio_gpu_object_attach(struct virtio_gpu_device
*vgdev
,
282 struct virtio_gpu_object
*obj
,
283 uint32_t resource_id
,
284 struct virtio_gpu_fence
**fence
);
285 int virtio_gpu_attach_status_page(struct virtio_gpu_device
*vgdev
);
286 int virtio_gpu_detach_status_page(struct virtio_gpu_device
*vgdev
);
287 void virtio_gpu_cursor_ping(struct virtio_gpu_device
*vgdev
,
288 struct virtio_gpu_output
*output
);
289 int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device
*vgdev
);
290 void virtio_gpu_cmd_resource_inval_backing(struct virtio_gpu_device
*vgdev
,
291 uint32_t resource_id
);
292 int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device
*vgdev
, int idx
);
293 int virtio_gpu_cmd_get_capset(struct virtio_gpu_device
*vgdev
,
294 int idx
, int version
,
295 struct virtio_gpu_drv_cap_cache
**cache_p
);
296 void virtio_gpu_cmd_context_create(struct virtio_gpu_device
*vgdev
, uint32_t id
,
297 uint32_t nlen
, const char *name
);
298 void virtio_gpu_cmd_context_destroy(struct virtio_gpu_device
*vgdev
,
300 void virtio_gpu_cmd_context_attach_resource(struct virtio_gpu_device
*vgdev
,
302 uint32_t resource_id
);
303 void virtio_gpu_cmd_context_detach_resource(struct virtio_gpu_device
*vgdev
,
305 uint32_t resource_id
);
306 void virtio_gpu_cmd_submit(struct virtio_gpu_device
*vgdev
,
307 void *data
, uint32_t data_size
,
308 uint32_t ctx_id
, struct virtio_gpu_fence
**fence
);
309 void virtio_gpu_cmd_transfer_from_host_3d(struct virtio_gpu_device
*vgdev
,
310 uint32_t resource_id
, uint32_t ctx_id
,
311 uint64_t offset
, uint32_t level
,
312 struct virtio_gpu_box
*box
,
313 struct virtio_gpu_fence
**fence
);
314 void virtio_gpu_cmd_transfer_to_host_3d(struct virtio_gpu_device
*vgdev
,
315 uint32_t resource_id
, uint32_t ctx_id
,
316 uint64_t offset
, uint32_t level
,
317 struct virtio_gpu_box
*box
,
318 struct virtio_gpu_fence
**fence
);
320 virtio_gpu_cmd_resource_create_3d(struct virtio_gpu_device
*vgdev
,
321 struct virtio_gpu_resource_create_3d
*rc_3d
,
322 struct virtio_gpu_fence
**fence
);
323 void virtio_gpu_ctrl_ack(struct virtqueue
*vq
);
324 void virtio_gpu_cursor_ack(struct virtqueue
*vq
);
325 void virtio_gpu_fence_ack(struct virtqueue
*vq
);
326 void virtio_gpu_dequeue_ctrl_func(struct work_struct
*work
);
327 void virtio_gpu_dequeue_cursor_func(struct work_struct
*work
);
328 void virtio_gpu_dequeue_fence_func(struct work_struct
*work
);
330 /* virtio_gpu_display.c */
331 int virtio_gpu_framebuffer_init(struct drm_device
*dev
,
332 struct virtio_gpu_framebuffer
*vgfb
,
333 const struct drm_mode_fb_cmd2
*mode_cmd
,
334 struct drm_gem_object
*obj
);
335 int virtio_gpu_modeset_init(struct virtio_gpu_device
*vgdev
);
336 void virtio_gpu_modeset_fini(struct virtio_gpu_device
*vgdev
);
338 /* virtio_gpu_plane.c */
339 struct drm_plane
*virtio_gpu_plane_init(struct virtio_gpu_device
*vgdev
,
340 enum drm_plane_type type
,
343 /* virtio_gpu_ttm.c */
344 int virtio_gpu_ttm_init(struct virtio_gpu_device
*vgdev
);
345 void virtio_gpu_ttm_fini(struct virtio_gpu_device
*vgdev
);
346 int virtio_gpu_mmap(struct file
*filp
, struct vm_area_struct
*vma
);
348 /* virtio_gpu_fence.c */
349 int virtio_gpu_fence_emit(struct virtio_gpu_device
*vgdev
,
350 struct virtio_gpu_ctrl_hdr
*cmd_hdr
,
351 struct virtio_gpu_fence
**fence
);
352 void virtio_gpu_fence_event_process(struct virtio_gpu_device
*vdev
,
355 /* virtio_gpu_object */
356 int virtio_gpu_object_create(struct virtio_gpu_device
*vgdev
,
357 unsigned long size
, bool kernel
, bool pinned
,
358 struct virtio_gpu_object
**bo_ptr
);
359 int virtio_gpu_object_kmap(struct virtio_gpu_object
*bo
, void **ptr
);
360 int virtio_gpu_object_get_sg_table(struct virtio_gpu_device
*qdev
,
361 struct virtio_gpu_object
*bo
);
362 void virtio_gpu_object_free_sg_table(struct virtio_gpu_object
*bo
);
363 int virtio_gpu_object_wait(struct virtio_gpu_object
*bo
, bool no_wait
);
365 /* virtgpu_prime.c */
366 int virtgpu_gem_prime_pin(struct drm_gem_object
*obj
);
367 void virtgpu_gem_prime_unpin(struct drm_gem_object
*obj
);
368 struct sg_table
*virtgpu_gem_prime_get_sg_table(struct drm_gem_object
*obj
);
369 struct drm_gem_object
*virtgpu_gem_prime_import_sg_table(
370 struct drm_device
*dev
, struct dma_buf_attachment
*attach
,
371 struct sg_table
*sgt
);
372 void *virtgpu_gem_prime_vmap(struct drm_gem_object
*obj
);
373 void virtgpu_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
);
374 int virtgpu_gem_prime_mmap(struct drm_gem_object
*obj
,
375 struct vm_area_struct
*vma
);
377 static inline struct virtio_gpu_object
*
378 virtio_gpu_object_ref(struct virtio_gpu_object
*bo
)
380 ttm_bo_reference(&bo
->tbo
);
384 static inline void virtio_gpu_object_unref(struct virtio_gpu_object
**bo
)
386 struct ttm_buffer_object
*tbo
;
396 static inline u64
virtio_gpu_object_mmap_offset(struct virtio_gpu_object
*bo
)
398 return drm_vma_node_offset_addr(&bo
->tbo
.vma_node
);
401 static inline int virtio_gpu_object_reserve(struct virtio_gpu_object
*bo
,
406 r
= ttm_bo_reserve(&bo
->tbo
, true, no_wait
, NULL
);
407 if (unlikely(r
!= 0)) {
408 if (r
!= -ERESTARTSYS
) {
409 struct virtio_gpu_device
*qdev
=
410 bo
->gem_base
.dev
->dev_private
;
411 dev_err(qdev
->dev
, "%p reserve failed\n", bo
);
418 static inline void virtio_gpu_object_unreserve(struct virtio_gpu_object
*bo
)
420 ttm_bo_unreserve(&bo
->tbo
);
424 int virtio_gpu_debugfs_init(struct drm_minor
*minor
);
425 void virtio_gpu_debugfs_takedown(struct drm_minor
*minor
);