xref: /linux/drivers/gpu/drm/virtio/virtgpu_drv.h (revision efdbd7345f8836f7495f3ac6ee237d86cb3bb6b0)
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  * All Rights Reserved.
4  *
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:
12  *
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.
16  *
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.
24  */
25 
26 #ifndef VIRTIO_DRV_H
27 #define VIRTIO_DRV_H
28 
29 #include <linux/virtio.h>
30 #include <linux/virtio_ids.h>
31 #include <linux/virtio_config.h>
32 #include <linux/virtio_gpu.h>
33 
34 #include <drm/drmP.h>
35 #include <drm/drm_gem.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <ttm/ttm_bo_api.h>
38 #include <ttm/ttm_bo_driver.h>
39 #include <ttm/ttm_placement.h>
40 #include <ttm/ttm_module.h>
41 
42 #define DRIVER_NAME "virtio_gpu"
43 #define DRIVER_DESC "virtio GPU"
44 #define DRIVER_DATE "0"
45 
46 #define DRIVER_MAJOR 0
47 #define DRIVER_MINOR 0
48 #define DRIVER_PATCHLEVEL 1
49 
50 /* virtgpu_drm_bus.c */
51 int drm_virtio_set_busid(struct drm_device *dev, struct drm_master *master);
52 int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev);
53 
54 struct virtio_gpu_object {
55 	struct drm_gem_object gem_base;
56 	uint32_t hw_res_handle;
57 
58 	struct sg_table *pages;
59 	void *vmap;
60 	bool dumb;
61 	struct ttm_place                placement_code;
62 	struct ttm_placement		placement;
63 	struct ttm_buffer_object	tbo;
64 	struct ttm_bo_kmap_obj		kmap;
65 };
66 #define gem_to_virtio_gpu_obj(gobj) \
67 	container_of((gobj), struct virtio_gpu_object, gem_base)
68 
69 struct virtio_gpu_vbuffer;
70 struct virtio_gpu_device;
71 
72 typedef void (*virtio_gpu_resp_cb)(struct virtio_gpu_device *vgdev,
73 				   struct virtio_gpu_vbuffer *vbuf);
74 
75 struct virtio_gpu_fence_driver {
76 	atomic64_t       last_seq;
77 	uint64_t         sync_seq;
78 	struct list_head fences;
79 	spinlock_t       lock;
80 };
81 
82 struct virtio_gpu_fence {
83 	struct fence f;
84 	struct virtio_gpu_fence_driver *drv;
85 	struct list_head node;
86 	uint64_t seq;
87 };
88 #define to_virtio_fence(x) \
89 	container_of(x, struct virtio_gpu_fence, f)
90 
91 struct virtio_gpu_vbuffer {
92 	char *buf;
93 	int size;
94 
95 	void *data_buf;
96 	uint32_t data_size;
97 
98 	char *resp_buf;
99 	int resp_size;
100 
101 	virtio_gpu_resp_cb resp_cb;
102 
103 	struct list_head list;
104 };
105 
106 struct virtio_gpu_output {
107 	int index;
108 	struct drm_crtc crtc;
109 	struct drm_connector conn;
110 	struct drm_encoder enc;
111 	struct virtio_gpu_display_one info;
112 	struct virtio_gpu_update_cursor cursor;
113 	int cur_x;
114 	int cur_y;
115 };
116 #define drm_crtc_to_virtio_gpu_output(x) \
117 	container_of(x, struct virtio_gpu_output, crtc)
118 #define drm_connector_to_virtio_gpu_output(x) \
119 	container_of(x, struct virtio_gpu_output, conn)
120 #define drm_encoder_to_virtio_gpu_output(x) \
121 	container_of(x, struct virtio_gpu_output, enc)
122 
123 struct virtio_gpu_framebuffer {
124 	struct drm_framebuffer base;
125 	struct drm_gem_object *obj;
126 	int x1, y1, x2, y2; /* dirty rect */
127 	spinlock_t dirty_lock;
128 	uint32_t hw_res_handle;
129 };
130 #define to_virtio_gpu_framebuffer(x) \
131 	container_of(x, struct virtio_gpu_framebuffer, base)
132 
133 struct virtio_gpu_mman {
134 	struct ttm_bo_global_ref        bo_global_ref;
135 	struct drm_global_reference	mem_global_ref;
136 	bool				mem_global_referenced;
137 	struct ttm_bo_device		bdev;
138 };
139 
140 struct virtio_gpu_fbdev;
141 
142 struct virtio_gpu_queue {
143 	struct virtqueue *vq;
144 	spinlock_t qlock;
145 	wait_queue_head_t ack_queue;
146 	struct work_struct dequeue_work;
147 };
148 
149 struct virtio_gpu_device {
150 	struct device *dev;
151 	struct drm_device *ddev;
152 
153 	struct virtio_device *vdev;
154 
155 	struct virtio_gpu_mman mman;
156 
157 	/* pointer to fbdev info structure */
158 	struct virtio_gpu_fbdev *vgfbdev;
159 	struct virtio_gpu_output outputs[VIRTIO_GPU_MAX_SCANOUTS];
160 	uint32_t num_scanouts;
161 
162 	struct virtio_gpu_queue ctrlq;
163 	struct virtio_gpu_queue cursorq;
164 	struct list_head free_vbufs;
165 	spinlock_t free_vbufs_lock;
166 	void *vbufs;
167 	bool vqs_ready;
168 
169 	struct idr	resource_idr;
170 	spinlock_t resource_idr_lock;
171 
172 	wait_queue_head_t resp_wq;
173 	/* current display info */
174 	spinlock_t display_info_lock;
175 	bool display_info_pending;
176 
177 	struct virtio_gpu_fence_driver fence_drv;
178 
179 	struct idr	ctx_id_idr;
180 	spinlock_t ctx_id_idr_lock;
181 
182 	struct work_struct config_changed_work;
183 };
184 
185 struct virtio_gpu_fpriv {
186 	uint32_t ctx_id;
187 };
188 
189 /* virtio_ioctl.c */
190 #define DRM_VIRTIO_NUM_IOCTLS 10
191 extern struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS];
192 
193 /* virtio_kms.c */
194 int virtio_gpu_driver_load(struct drm_device *dev, unsigned long flags);
195 int virtio_gpu_driver_unload(struct drm_device *dev);
196 
197 /* virtio_gem.c */
198 void virtio_gpu_gem_free_object(struct drm_gem_object *gem_obj);
199 int virtio_gpu_gem_init(struct virtio_gpu_device *vgdev);
200 void virtio_gpu_gem_fini(struct virtio_gpu_device *vgdev);
201 int virtio_gpu_gem_create(struct drm_file *file,
202 			  struct drm_device *dev,
203 			  uint64_t size,
204 			  struct drm_gem_object **obj_p,
205 			  uint32_t *handle_p);
206 struct virtio_gpu_object *virtio_gpu_alloc_object(struct drm_device *dev,
207 						  size_t size, bool kernel,
208 						  bool pinned);
209 int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
210 				struct drm_device *dev,
211 				struct drm_mode_create_dumb *args);
212 int virtio_gpu_mode_dumb_destroy(struct drm_file *file_priv,
213 				 struct drm_device *dev,
214 				 uint32_t handle);
215 int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
216 			      struct drm_device *dev,
217 			      uint32_t handle, uint64_t *offset_p);
218 
219 /* virtio_fb */
220 #define VIRTIO_GPUFB_CONN_LIMIT 1
221 int virtio_gpu_fbdev_init(struct virtio_gpu_device *vgdev);
222 void virtio_gpu_fbdev_fini(struct virtio_gpu_device *vgdev);
223 int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *qfb,
224 			     struct drm_clip_rect *clips,
225 			     unsigned num_clips);
226 /* virtio vg */
227 int virtio_gpu_alloc_vbufs(struct virtio_gpu_device *vgdev);
228 void virtio_gpu_free_vbufs(struct virtio_gpu_device *vgdev);
229 void virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
230 			       uint32_t *resid);
231 void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id);
232 void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
233 				    uint32_t resource_id,
234 				    uint32_t format,
235 				    uint32_t width,
236 				    uint32_t height);
237 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
238 				   uint32_t resource_id);
239 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
240 					uint32_t resource_id, uint64_t offset,
241 					__le32 width, __le32 height,
242 					__le32 x, __le32 y,
243 					struct virtio_gpu_fence **fence);
244 void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device *vgdev,
245 				   uint32_t resource_id,
246 				   uint32_t x, uint32_t y,
247 				   uint32_t width, uint32_t height);
248 void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
249 				uint32_t scanout_id, uint32_t resource_id,
250 				uint32_t width, uint32_t height,
251 				uint32_t x, uint32_t y);
252 int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
253 			     struct virtio_gpu_object *obj,
254 			     uint32_t resource_id,
255 			     struct virtio_gpu_fence **fence);
256 int virtio_gpu_attach_status_page(struct virtio_gpu_device *vgdev);
257 int virtio_gpu_detach_status_page(struct virtio_gpu_device *vgdev);
258 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
259 			    struct virtio_gpu_output *output);
260 int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev);
261 void virtio_gpu_cmd_resource_inval_backing(struct virtio_gpu_device *vgdev,
262 					   uint32_t resource_id);
263 void virtio_gpu_ctrl_ack(struct virtqueue *vq);
264 void virtio_gpu_cursor_ack(struct virtqueue *vq);
265 void virtio_gpu_dequeue_ctrl_func(struct work_struct *work);
266 void virtio_gpu_dequeue_cursor_func(struct work_struct *work);
267 
268 /* virtio_gpu_display.c */
269 int virtio_gpu_framebuffer_init(struct drm_device *dev,
270 				struct virtio_gpu_framebuffer *vgfb,
271 				struct drm_mode_fb_cmd2 *mode_cmd,
272 				struct drm_gem_object *obj);
273 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev);
274 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev);
275 
276 /* virtio_gpu_plane.c */
277 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
278 					int index);
279 
280 /* virtio_gpu_ttm.c */
281 int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev);
282 void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev);
283 int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma);
284 
285 /* virtio_gpu_fence.c */
286 int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
287 			  struct virtio_gpu_ctrl_hdr *cmd_hdr,
288 			  struct virtio_gpu_fence **fence);
289 void virtio_gpu_fence_event_process(struct virtio_gpu_device *vdev,
290 				    u64 last_seq);
291 
292 /* virtio_gpu_object */
293 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
294 			     unsigned long size, bool kernel, bool pinned,
295 			     struct virtio_gpu_object **bo_ptr);
296 int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr);
297 int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
298 				   struct virtio_gpu_object *bo);
299 void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo);
300 int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait);
301 
302 static inline struct virtio_gpu_object*
303 virtio_gpu_object_ref(struct virtio_gpu_object *bo)
304 {
305 	ttm_bo_reference(&bo->tbo);
306 	return bo;
307 }
308 
309 static inline void virtio_gpu_object_unref(struct virtio_gpu_object **bo)
310 {
311 	struct ttm_buffer_object *tbo;
312 
313 	if ((*bo) == NULL)
314 		return;
315 	tbo = &((*bo)->tbo);
316 	ttm_bo_unref(&tbo);
317 	if (tbo == NULL)
318 		*bo = NULL;
319 }
320 
321 static inline u64 virtio_gpu_object_mmap_offset(struct virtio_gpu_object *bo)
322 {
323 	return drm_vma_node_offset_addr(&bo->tbo.vma_node);
324 }
325 
326 static inline int virtio_gpu_object_reserve(struct virtio_gpu_object *bo,
327 					 bool no_wait)
328 {
329 	int r;
330 
331 	r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, NULL);
332 	if (unlikely(r != 0)) {
333 		if (r != -ERESTARTSYS) {
334 			struct virtio_gpu_device *qdev =
335 				bo->gem_base.dev->dev_private;
336 			dev_err(qdev->dev, "%p reserve failed\n", bo);
337 		}
338 		return r;
339 	}
340 	return 0;
341 }
342 
343 static inline void virtio_gpu_object_unreserve(struct virtio_gpu_object *bo)
344 {
345 	ttm_bo_unreserve(&bo->tbo);
346 }
347 
348 /* virgl debufs */
349 int virtio_gpu_debugfs_init(struct drm_minor *minor);
350 void virtio_gpu_debugfs_takedown(struct drm_minor *minor);
351 
352 #endif
353