1 /**************************************************************************
2  *
3  * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef _VMWGFX_DRV_H_
29 #define _VMWGFX_DRV_H_
30 
31 #include "vmwgfx_reg.h"
32 #include <drm/drmP.h>
33 #include <drm/vmwgfx_drm.h>
34 #include <drm/drm_hashtab.h>
35 #include <drm/drm_auth.h>
36 #include <linux/suspend.h>
37 #include <drm/ttm/ttm_bo_driver.h>
38 #include <drm/ttm/ttm_object.h>
39 #include <drm/ttm/ttm_lock.h>
40 #include <drm/ttm/ttm_execbuf_util.h>
41 #include <drm/ttm/ttm_module.h>
42 #include "vmwgfx_fence.h"
43 #include <linux/sync_file.h>
44 
45 #define VMWGFX_DRIVER_NAME "vmwgfx"
46 #define VMWGFX_DRIVER_DATE "20180322"
47 #define VMWGFX_DRIVER_MAJOR 2
48 #define VMWGFX_DRIVER_MINOR 14
49 #define VMWGFX_DRIVER_PATCHLEVEL 1
50 #define VMWGFX_FILE_PAGE_OFFSET 0x00100000
51 #define VMWGFX_FIFO_STATIC_SIZE (1024*1024)
52 #define VMWGFX_MAX_RELOCATIONS 2048
53 #define VMWGFX_MAX_VALIDATIONS 2048
54 #define VMWGFX_MAX_DISPLAYS 16
55 #define VMWGFX_CMD_BOUNCE_INIT_SIZE 32768
56 #define VMWGFX_ENABLE_SCREEN_TARGET_OTABLE 1
57 
58 /*
59  * Perhaps we should have sysfs entries for these.
60  */
61 #define VMWGFX_NUM_GB_CONTEXT 256
62 #define VMWGFX_NUM_GB_SHADER 20000
63 #define VMWGFX_NUM_GB_SURFACE 32768
64 #define VMWGFX_NUM_GB_SCREEN_TARGET VMWGFX_MAX_DISPLAYS
65 #define VMWGFX_NUM_DXCONTEXT 256
66 #define VMWGFX_NUM_DXQUERY 512
67 #define VMWGFX_NUM_MOB (VMWGFX_NUM_GB_CONTEXT +\
68 			VMWGFX_NUM_GB_SHADER +\
69 			VMWGFX_NUM_GB_SURFACE +\
70 			VMWGFX_NUM_GB_SCREEN_TARGET)
71 
72 #define VMW_PL_GMR (TTM_PL_PRIV + 0)
73 #define VMW_PL_FLAG_GMR (TTM_PL_FLAG_PRIV << 0)
74 #define VMW_PL_MOB (TTM_PL_PRIV + 1)
75 #define VMW_PL_FLAG_MOB (TTM_PL_FLAG_PRIV << 1)
76 
77 #define VMW_RES_CONTEXT ttm_driver_type0
78 #define VMW_RES_SURFACE ttm_driver_type1
79 #define VMW_RES_STREAM ttm_driver_type2
80 #define VMW_RES_FENCE ttm_driver_type3
81 #define VMW_RES_SHADER ttm_driver_type4
82 
83 struct vmw_fpriv {
84 	struct drm_master *locked_master;
85 	struct ttm_object_file *tfile;
86 	bool gb_aware;
87 };
88 
89 struct vmw_dma_buffer {
90 	struct ttm_buffer_object base;
91 	struct list_head res_list;
92 	s32 pin_count;
93 	/* Not ref-counted.  Protected by binding_mutex */
94 	struct vmw_resource *dx_query_ctx;
95 	/* Protected by reservation */
96 	struct ttm_bo_kmap_obj map;
97 };
98 
99 /**
100  * struct vmw_validate_buffer - Carries validation info about buffers.
101  *
102  * @base: Validation info for TTM.
103  * @hash: Hash entry for quick lookup of the TTM buffer object.
104  *
105  * This structure contains also driver private validation info
106  * on top of the info needed by TTM.
107  */
108 struct vmw_validate_buffer {
109 	struct ttm_validate_buffer base;
110 	struct drm_hash_item hash;
111 	bool validate_as_mob;
112 };
113 
114 struct vmw_res_func;
115 struct vmw_resource {
116 	struct kref kref;
117 	struct vmw_private *dev_priv;
118 	int id;
119 	bool avail;
120 	unsigned long backup_size;
121 	bool res_dirty; /* Protected by backup buffer reserved */
122 	bool backup_dirty; /* Protected by backup buffer reserved */
123 	struct vmw_dma_buffer *backup;
124 	unsigned long backup_offset;
125 	unsigned long pin_count; /* Protected by resource reserved */
126 	const struct vmw_res_func *func;
127 	struct list_head lru_head; /* Protected by the resource lock */
128 	struct list_head mob_head; /* Protected by @backup reserved */
129 	struct list_head binding_head; /* Protected by binding_mutex */
130 	void (*res_free) (struct vmw_resource *res);
131 	void (*hw_destroy) (struct vmw_resource *res);
132 };
133 
134 
135 /*
136  * Resources that are managed using ioctls.
137  */
138 enum vmw_res_type {
139 	vmw_res_context,
140 	vmw_res_surface,
141 	vmw_res_stream,
142 	vmw_res_shader,
143 	vmw_res_dx_context,
144 	vmw_res_cotable,
145 	vmw_res_view,
146 	vmw_res_max
147 };
148 
149 /*
150  * Resources that are managed using command streams.
151  */
152 enum vmw_cmdbuf_res_type {
153 	vmw_cmdbuf_res_shader,
154 	vmw_cmdbuf_res_view
155 };
156 
157 struct vmw_cmdbuf_res_manager;
158 
159 struct vmw_cursor_snooper {
160 	size_t age;
161 	uint32_t *image;
162 };
163 
164 struct vmw_framebuffer;
165 struct vmw_surface_offset;
166 
167 struct vmw_surface {
168 	struct vmw_resource res;
169 	uint32_t flags;
170 	uint32_t format;
171 	uint32_t mip_levels[DRM_VMW_MAX_SURFACE_FACES];
172 	struct drm_vmw_size base_size;
173 	struct drm_vmw_size *sizes;
174 	uint32_t num_sizes;
175 	bool scanout;
176 	uint32_t array_size;
177 	/* TODO so far just a extra pointer */
178 	struct vmw_cursor_snooper snooper;
179 	struct vmw_surface_offset *offsets;
180 	SVGA3dTextureFilter autogen_filter;
181 	uint32_t multisample_count;
182 	struct list_head view_list;
183 };
184 
185 struct vmw_marker_queue {
186 	struct list_head head;
187 	u64 lag;
188 	u64 lag_time;
189 	spinlock_t lock;
190 };
191 
192 struct vmw_fifo_state {
193 	unsigned long reserved_size;
194 	u32 *dynamic_buffer;
195 	u32 *static_buffer;
196 	unsigned long static_buffer_size;
197 	bool using_bounce_buffer;
198 	uint32_t capabilities;
199 	struct mutex fifo_mutex;
200 	struct rw_semaphore rwsem;
201 	struct vmw_marker_queue marker_queue;
202 	bool dx;
203 };
204 
205 struct vmw_relocation {
206 	SVGAMobId *mob_loc;
207 	SVGAGuestPtr *location;
208 	uint32_t index;
209 };
210 
211 /**
212  * struct vmw_res_cache_entry - resource information cache entry
213  *
214  * @valid: Whether the entry is valid, which also implies that the execbuf
215  * code holds a reference to the resource, and it's placed on the
216  * validation list.
217  * @handle: User-space handle of a resource.
218  * @res: Non-ref-counted pointer to the resource.
219  *
220  * Used to avoid frequent repeated user-space handle lookups of the
221  * same resource.
222  */
223 struct vmw_res_cache_entry {
224 	bool valid;
225 	uint32_t handle;
226 	struct vmw_resource *res;
227 	struct vmw_resource_val_node *node;
228 };
229 
230 /**
231  * enum vmw_dma_map_mode - indicate how to perform TTM page dma mappings.
232  */
233 enum vmw_dma_map_mode {
234 	vmw_dma_phys,           /* Use physical page addresses */
235 	vmw_dma_alloc_coherent, /* Use TTM coherent pages */
236 	vmw_dma_map_populate,   /* Unmap from DMA just after unpopulate */
237 	vmw_dma_map_bind,       /* Unmap from DMA just before unbind */
238 	vmw_dma_map_max
239 };
240 
241 /**
242  * struct vmw_sg_table - Scatter/gather table for binding, with additional
243  * device-specific information.
244  *
245  * @sgt: Pointer to a struct sg_table with binding information
246  * @num_regions: Number of regions with device-address contiguous pages
247  */
248 struct vmw_sg_table {
249 	enum vmw_dma_map_mode mode;
250 	struct page **pages;
251 	const dma_addr_t *addrs;
252 	struct sg_table *sgt;
253 	unsigned long num_regions;
254 	unsigned long num_pages;
255 };
256 
257 /**
258  * struct vmw_piter - Page iterator that iterates over a list of pages
259  * and DMA addresses that could be either a scatter-gather list or
260  * arrays
261  *
262  * @pages: Array of page pointers to the pages.
263  * @addrs: DMA addresses to the pages if coherent pages are used.
264  * @iter: Scatter-gather page iterator. Current position in SG list.
265  * @i: Current position in arrays.
266  * @num_pages: Number of pages total.
267  * @next: Function to advance the iterator. Returns false if past the list
268  * of pages, true otherwise.
269  * @dma_address: Function to return the DMA address of the current page.
270  */
271 struct vmw_piter {
272 	struct page **pages;
273 	const dma_addr_t *addrs;
274 	struct sg_page_iter iter;
275 	unsigned long i;
276 	unsigned long num_pages;
277 	bool (*next)(struct vmw_piter *);
278 	dma_addr_t (*dma_address)(struct vmw_piter *);
279 	struct page *(*page)(struct vmw_piter *);
280 };
281 
282 /*
283  * enum vmw_display_unit_type - Describes the display unit
284  */
285 enum vmw_display_unit_type {
286 	vmw_du_invalid = 0,
287 	vmw_du_legacy,
288 	vmw_du_screen_object,
289 	vmw_du_screen_target
290 };
291 
292 
293 struct vmw_sw_context{
294 	struct drm_open_hash res_ht;
295 	bool res_ht_initialized;
296 	bool kernel; /**< is the called made from the kernel */
297 	struct vmw_fpriv *fp;
298 	struct list_head validate_nodes;
299 	struct vmw_relocation relocs[VMWGFX_MAX_RELOCATIONS];
300 	uint32_t cur_reloc;
301 	struct vmw_validate_buffer val_bufs[VMWGFX_MAX_VALIDATIONS];
302 	uint32_t cur_val_buf;
303 	uint32_t *cmd_bounce;
304 	uint32_t cmd_bounce_size;
305 	struct list_head resource_list;
306 	struct list_head ctx_resource_list; /* For contexts and cotables */
307 	struct vmw_dma_buffer *cur_query_bo;
308 	struct list_head res_relocations;
309 	uint32_t *buf_start;
310 	struct vmw_res_cache_entry res_cache[vmw_res_max];
311 	struct vmw_resource *last_query_ctx;
312 	bool needs_post_query_barrier;
313 	struct vmw_resource *error_resource;
314 	struct vmw_ctx_binding_state *staged_bindings;
315 	bool staged_bindings_inuse;
316 	struct list_head staged_cmd_res;
317 	struct vmw_resource_val_node *dx_ctx_node;
318 	struct vmw_dma_buffer *dx_query_mob;
319 	struct vmw_resource *dx_query_ctx;
320 	struct vmw_cmdbuf_res_manager *man;
321 };
322 
323 struct vmw_legacy_display;
324 struct vmw_overlay;
325 
326 struct vmw_master {
327 	struct ttm_lock lock;
328 };
329 
330 struct vmw_vga_topology_state {
331 	uint32_t width;
332 	uint32_t height;
333 	uint32_t primary;
334 	uint32_t pos_x;
335 	uint32_t pos_y;
336 };
337 
338 
339 /*
340  * struct vmw_otable - Guest Memory OBject table metadata
341  *
342  * @size:           Size of the table (page-aligned).
343  * @page_table:     Pointer to a struct vmw_mob holding the page table.
344  */
345 struct vmw_otable {
346 	unsigned long size;
347 	struct vmw_mob *page_table;
348 	bool enabled;
349 };
350 
351 struct vmw_otable_batch {
352 	unsigned num_otables;
353 	struct vmw_otable *otables;
354 	struct vmw_resource *context;
355 	struct ttm_buffer_object *otable_bo;
356 };
357 
358 enum {
359 	VMW_IRQTHREAD_FENCE,
360 	VMW_IRQTHREAD_CMDBUF,
361 	VMW_IRQTHREAD_MAX
362 };
363 
364 struct vmw_private {
365 	struct ttm_bo_device bdev;
366 	struct ttm_bo_global_ref bo_global_ref;
367 	struct drm_global_reference mem_global_ref;
368 
369 	struct vmw_fifo_state fifo;
370 
371 	struct drm_device *dev;
372 	unsigned long vmw_chipset;
373 	unsigned int io_start;
374 	uint32_t vram_start;
375 	uint32_t vram_size;
376 	uint32_t prim_bb_mem;
377 	uint32_t mmio_start;
378 	uint32_t mmio_size;
379 	uint32_t fb_max_width;
380 	uint32_t fb_max_height;
381 	uint32_t texture_max_width;
382 	uint32_t texture_max_height;
383 	uint32_t stdu_max_width;
384 	uint32_t stdu_max_height;
385 	uint32_t initial_width;
386 	uint32_t initial_height;
387 	u32 *mmio_virt;
388 	uint32_t capabilities;
389 	uint32_t max_gmr_ids;
390 	uint32_t max_gmr_pages;
391 	uint32_t max_mob_pages;
392 	uint32_t max_mob_size;
393 	uint32_t memory_size;
394 	bool has_gmr;
395 	bool has_mob;
396 	spinlock_t hw_lock;
397 	spinlock_t cap_lock;
398 	bool has_dx;
399 	bool assume_16bpp;
400 
401 	/*
402 	 * VGA registers.
403 	 */
404 
405 	struct vmw_vga_topology_state vga_save[VMWGFX_MAX_DISPLAYS];
406 	uint32_t vga_width;
407 	uint32_t vga_height;
408 	uint32_t vga_bpp;
409 	uint32_t vga_bpl;
410 	uint32_t vga_pitchlock;
411 
412 	uint32_t num_displays;
413 
414 	/*
415 	 * Framebuffer info.
416 	 */
417 
418 	void *fb_info;
419 	enum vmw_display_unit_type active_display_unit;
420 	struct vmw_legacy_display *ldu_priv;
421 	struct vmw_overlay *overlay_priv;
422 	struct drm_property *hotplug_mode_update_property;
423 	struct drm_property *implicit_placement_property;
424 	unsigned num_implicit;
425 	struct vmw_framebuffer *implicit_fb;
426 	struct mutex global_kms_state_mutex;
427 	spinlock_t cursor_lock;
428 	struct drm_atomic_state *suspend_state;
429 
430 	/*
431 	 * Context and surface management.
432 	 */
433 
434 	rwlock_t resource_lock;
435 	struct idr res_idr[vmw_res_max];
436 	/*
437 	 * Block lastclose from racing with firstopen.
438 	 */
439 
440 	struct mutex init_mutex;
441 
442 	/*
443 	 * A resource manager for kernel-only surfaces and
444 	 * contexts.
445 	 */
446 
447 	struct ttm_object_device *tdev;
448 
449 	/*
450 	 * Fencing and IRQs.
451 	 */
452 
453 	atomic_t marker_seq;
454 	wait_queue_head_t fence_queue;
455 	wait_queue_head_t fifo_queue;
456 	spinlock_t waiter_lock;
457 	int fence_queue_waiters; /* Protected by waiter_lock */
458 	int goal_queue_waiters; /* Protected by waiter_lock */
459 	int cmdbuf_waiters; /* Protected by waiter_lock */
460 	int error_waiters; /* Protected by waiter_lock */
461 	int fifo_queue_waiters; /* Protected by waiter_lock */
462 	uint32_t last_read_seqno;
463 	struct vmw_fence_manager *fman;
464 	uint32_t irq_mask; /* Updates protected by waiter_lock */
465 
466 	/*
467 	 * Device state
468 	 */
469 
470 	uint32_t traces_state;
471 	uint32_t enable_state;
472 	uint32_t config_done_state;
473 
474 	/**
475 	 * Execbuf
476 	 */
477 	/**
478 	 * Protected by the cmdbuf mutex.
479 	 */
480 
481 	struct vmw_sw_context ctx;
482 	struct mutex cmdbuf_mutex;
483 	struct mutex binding_mutex;
484 
485 	/**
486 	 * Operating mode.
487 	 */
488 
489 	bool stealth;
490 	bool enable_fb;
491 	spinlock_t svga_lock;
492 
493 	/**
494 	 * Master management.
495 	 */
496 
497 	struct vmw_master *active_master;
498 	struct vmw_master fbdev_master;
499 	struct notifier_block pm_nb;
500 	bool refuse_hibernation;
501 	bool suspend_locked;
502 
503 	struct mutex release_mutex;
504 	atomic_t num_fifo_resources;
505 
506 	/*
507 	 * Replace this with an rwsem as soon as we have down_xx_interruptible()
508 	 */
509 	struct ttm_lock reservation_sem;
510 
511 	/*
512 	 * Query processing. These members
513 	 * are protected by the cmdbuf mutex.
514 	 */
515 
516 	struct vmw_dma_buffer *dummy_query_bo;
517 	struct vmw_dma_buffer *pinned_bo;
518 	uint32_t query_cid;
519 	uint32_t query_cid_valid;
520 	bool dummy_query_bo_pinned;
521 
522 	/*
523 	 * Surface swapping. The "surface_lru" list is protected by the
524 	 * resource lock in order to be able to destroy a surface and take
525 	 * it off the lru atomically. "used_memory_size" is currently
526 	 * protected by the cmdbuf mutex for simplicity.
527 	 */
528 
529 	struct list_head res_lru[vmw_res_max];
530 	uint32_t used_memory_size;
531 
532 	/*
533 	 * DMA mapping stuff.
534 	 */
535 	enum vmw_dma_map_mode map_mode;
536 
537 	/*
538 	 * Guest Backed stuff
539 	 */
540 	struct vmw_otable_batch otable_batch;
541 
542 	struct vmw_cmdbuf_man *cman;
543 	DECLARE_BITMAP(irqthread_pending, VMW_IRQTHREAD_MAX);
544 };
545 
546 static inline struct vmw_surface *vmw_res_to_srf(struct vmw_resource *res)
547 {
548 	return container_of(res, struct vmw_surface, res);
549 }
550 
551 static inline struct vmw_private *vmw_priv(struct drm_device *dev)
552 {
553 	return (struct vmw_private *)dev->dev_private;
554 }
555 
556 static inline struct vmw_fpriv *vmw_fpriv(struct drm_file *file_priv)
557 {
558 	return (struct vmw_fpriv *)file_priv->driver_priv;
559 }
560 
561 static inline struct vmw_master *vmw_master(struct drm_master *master)
562 {
563 	return (struct vmw_master *) master->driver_priv;
564 }
565 
566 /*
567  * The locking here is fine-grained, so that it is performed once
568  * for every read- and write operation. This is of course costly, but we
569  * don't perform much register access in the timing critical paths anyway.
570  * Instead we have the extra benefit of being sure that we don't forget
571  * the hw lock around register accesses.
572  */
573 static inline void vmw_write(struct vmw_private *dev_priv,
574 			     unsigned int offset, uint32_t value)
575 {
576 	spin_lock(&dev_priv->hw_lock);
577 	outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
578 	outl(value, dev_priv->io_start + VMWGFX_VALUE_PORT);
579 	spin_unlock(&dev_priv->hw_lock);
580 }
581 
582 static inline uint32_t vmw_read(struct vmw_private *dev_priv,
583 				unsigned int offset)
584 {
585 	u32 val;
586 
587 	spin_lock(&dev_priv->hw_lock);
588 	outl(offset, dev_priv->io_start + VMWGFX_INDEX_PORT);
589 	val = inl(dev_priv->io_start + VMWGFX_VALUE_PORT);
590 	spin_unlock(&dev_priv->hw_lock);
591 
592 	return val;
593 }
594 
595 extern void vmw_svga_enable(struct vmw_private *dev_priv);
596 extern void vmw_svga_disable(struct vmw_private *dev_priv);
597 
598 
599 /**
600  * GMR utilities - vmwgfx_gmr.c
601  */
602 
603 extern int vmw_gmr_bind(struct vmw_private *dev_priv,
604 			const struct vmw_sg_table *vsgt,
605 			unsigned long num_pages,
606 			int gmr_id);
607 extern void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id);
608 
609 /**
610  * Resource utilities - vmwgfx_resource.c
611  */
612 struct vmw_user_resource_conv;
613 
614 extern void vmw_resource_unreference(struct vmw_resource **p_res);
615 extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
616 extern struct vmw_resource *
617 vmw_resource_reference_unless_doomed(struct vmw_resource *res);
618 extern int vmw_resource_validate(struct vmw_resource *res);
619 extern int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
620 				bool no_backup);
621 extern bool vmw_resource_needs_backup(const struct vmw_resource *res);
622 extern int vmw_user_lookup_handle(struct vmw_private *dev_priv,
623 				  struct ttm_object_file *tfile,
624 				  uint32_t handle,
625 				  struct vmw_surface **out_surf,
626 				  struct vmw_dma_buffer **out_buf);
627 extern int vmw_user_resource_lookup_handle(
628 	struct vmw_private *dev_priv,
629 	struct ttm_object_file *tfile,
630 	uint32_t handle,
631 	const struct vmw_user_resource_conv *converter,
632 	struct vmw_resource **p_res);
633 extern void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo);
634 extern int vmw_dmabuf_init(struct vmw_private *dev_priv,
635 			   struct vmw_dma_buffer *vmw_bo,
636 			   size_t size, struct ttm_placement *placement,
637 			   bool interuptable,
638 			   void (*bo_free) (struct ttm_buffer_object *bo));
639 extern int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
640 				  struct ttm_object_file *tfile);
641 extern int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
642 				 struct ttm_object_file *tfile,
643 				 uint32_t size,
644 				 bool shareable,
645 				 uint32_t *handle,
646 				 struct vmw_dma_buffer **p_dma_buf,
647 				 struct ttm_base_object **p_base);
648 extern int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
649 				     struct vmw_dma_buffer *dma_buf,
650 				     uint32_t *handle);
651 extern int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
652 				  struct drm_file *file_priv);
653 extern int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
654 				  struct drm_file *file_priv);
655 extern int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
656 					 struct drm_file *file_priv);
657 extern uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
658 					 uint32_t cur_validate_node);
659 extern void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo);
660 extern int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
661 				  uint32_t id, struct vmw_dma_buffer **out,
662 				  struct ttm_base_object **base);
663 extern int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
664 				  struct drm_file *file_priv);
665 extern int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
666 				  struct drm_file *file_priv);
667 extern int vmw_user_stream_lookup(struct vmw_private *dev_priv,
668 				  struct ttm_object_file *tfile,
669 				  uint32_t *inout_id,
670 				  struct vmw_resource **out);
671 extern void vmw_resource_unreserve(struct vmw_resource *res,
672 				   bool switch_backup,
673 				   struct vmw_dma_buffer *new_backup,
674 				   unsigned long new_backup_offset);
675 extern void vmw_resource_move_notify(struct ttm_buffer_object *bo,
676 				     struct ttm_mem_reg *mem);
677 extern void vmw_query_move_notify(struct ttm_buffer_object *bo,
678 				  struct ttm_mem_reg *mem);
679 extern void vmw_resource_swap_notify(struct ttm_buffer_object *bo);
680 extern int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob);
681 extern void vmw_fence_single_bo(struct ttm_buffer_object *bo,
682 				struct vmw_fence_obj *fence);
683 extern void vmw_resource_evict_all(struct vmw_private *dev_priv);
684 
685 
686 /**
687  * DMA buffer helper routines - vmwgfx_dmabuf.c
688  */
689 extern int vmw_dmabuf_pin_in_placement(struct vmw_private *vmw_priv,
690 				       struct vmw_dma_buffer *bo,
691 				       struct ttm_placement *placement,
692 				       bool interruptible);
693 extern int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
694 				  struct vmw_dma_buffer *buf,
695 				  bool interruptible);
696 extern int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
697 					 struct vmw_dma_buffer *buf,
698 					 bool interruptible);
699 extern int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *vmw_priv,
700 					   struct vmw_dma_buffer *bo,
701 					   bool interruptible);
702 extern int vmw_dmabuf_unpin(struct vmw_private *vmw_priv,
703 			    struct vmw_dma_buffer *bo,
704 			    bool interruptible);
705 extern void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *buf,
706 				 SVGAGuestPtr *ptr);
707 extern void vmw_bo_pin_reserved(struct vmw_dma_buffer *bo, bool pin);
708 extern void *vmw_dma_buffer_map_and_cache(struct vmw_dma_buffer *vbo);
709 extern void vmw_dma_buffer_unmap(struct vmw_dma_buffer *vbo);
710 
711 /**
712  * Misc Ioctl functionality - vmwgfx_ioctl.c
713  */
714 
715 extern int vmw_getparam_ioctl(struct drm_device *dev, void *data,
716 			      struct drm_file *file_priv);
717 extern int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
718 				struct drm_file *file_priv);
719 extern int vmw_present_ioctl(struct drm_device *dev, void *data,
720 			     struct drm_file *file_priv);
721 extern int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
722 				      struct drm_file *file_priv);
723 extern __poll_t vmw_fops_poll(struct file *filp,
724 				  struct poll_table_struct *wait);
725 extern ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
726 			     size_t count, loff_t *offset);
727 
728 /**
729  * Fifo utilities - vmwgfx_fifo.c
730  */
731 
732 extern int vmw_fifo_init(struct vmw_private *dev_priv,
733 			 struct vmw_fifo_state *fifo);
734 extern void vmw_fifo_release(struct vmw_private *dev_priv,
735 			     struct vmw_fifo_state *fifo);
736 extern void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes);
737 extern void *
738 vmw_fifo_reserve_dx(struct vmw_private *dev_priv, uint32_t bytes, int ctx_id);
739 extern void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes);
740 extern void vmw_fifo_commit_flush(struct vmw_private *dev_priv, uint32_t bytes);
741 extern int vmw_fifo_send_fence(struct vmw_private *dev_priv,
742 			       uint32_t *seqno);
743 extern void vmw_fifo_ping_host_locked(struct vmw_private *, uint32_t reason);
744 extern void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason);
745 extern bool vmw_fifo_have_3d(struct vmw_private *dev_priv);
746 extern bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv);
747 extern int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
748 				     uint32_t cid);
749 extern int vmw_fifo_flush(struct vmw_private *dev_priv,
750 			  bool interruptible);
751 
752 /**
753  * TTM glue - vmwgfx_ttm_glue.c
754  */
755 
756 extern int vmw_ttm_global_init(struct vmw_private *dev_priv);
757 extern void vmw_ttm_global_release(struct vmw_private *dev_priv);
758 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
759 
760 /**
761  * TTM buffer object driver - vmwgfx_buffer.c
762  */
763 
764 extern const size_t vmw_tt_size;
765 extern struct ttm_placement vmw_vram_placement;
766 extern struct ttm_placement vmw_vram_ne_placement;
767 extern struct ttm_placement vmw_vram_sys_placement;
768 extern struct ttm_placement vmw_vram_gmr_placement;
769 extern struct ttm_placement vmw_vram_gmr_ne_placement;
770 extern struct ttm_placement vmw_sys_placement;
771 extern struct ttm_placement vmw_sys_ne_placement;
772 extern struct ttm_placement vmw_evictable_placement;
773 extern struct ttm_placement vmw_srf_placement;
774 extern struct ttm_placement vmw_mob_placement;
775 extern struct ttm_placement vmw_mob_ne_placement;
776 extern struct ttm_placement vmw_nonfixed_placement;
777 extern struct ttm_bo_driver vmw_bo_driver;
778 extern int vmw_dma_quiescent(struct drm_device *dev);
779 extern int vmw_bo_map_dma(struct ttm_buffer_object *bo);
780 extern void vmw_bo_unmap_dma(struct ttm_buffer_object *bo);
781 extern const struct vmw_sg_table *
782 vmw_bo_sg_table(struct ttm_buffer_object *bo);
783 extern void vmw_piter_start(struct vmw_piter *viter,
784 			    const struct vmw_sg_table *vsgt,
785 			    unsigned long p_offs);
786 
787 /**
788  * vmw_piter_next - Advance the iterator one page.
789  *
790  * @viter: Pointer to the iterator to advance.
791  *
792  * Returns false if past the list of pages, true otherwise.
793  */
794 static inline bool vmw_piter_next(struct vmw_piter *viter)
795 {
796 	return viter->next(viter);
797 }
798 
799 /**
800  * vmw_piter_dma_addr - Return the DMA address of the current page.
801  *
802  * @viter: Pointer to the iterator
803  *
804  * Returns the DMA address of the page pointed to by @viter.
805  */
806 static inline dma_addr_t vmw_piter_dma_addr(struct vmw_piter *viter)
807 {
808 	return viter->dma_address(viter);
809 }
810 
811 /**
812  * vmw_piter_page - Return a pointer to the current page.
813  *
814  * @viter: Pointer to the iterator
815  *
816  * Returns the DMA address of the page pointed to by @viter.
817  */
818 static inline struct page *vmw_piter_page(struct vmw_piter *viter)
819 {
820 	return viter->page(viter);
821 }
822 
823 /**
824  * Command submission - vmwgfx_execbuf.c
825  */
826 
827 extern int vmw_execbuf_ioctl(struct drm_device *dev, unsigned long data,
828 			     struct drm_file *file_priv, size_t size);
829 extern int vmw_execbuf_process(struct drm_file *file_priv,
830 			       struct vmw_private *dev_priv,
831 			       void __user *user_commands,
832 			       void *kernel_commands,
833 			       uint32_t command_size,
834 			       uint64_t throttle_us,
835 			       uint32_t dx_context_handle,
836 			       struct drm_vmw_fence_rep __user
837 			       *user_fence_rep,
838 			       struct vmw_fence_obj **out_fence,
839 			       uint32_t flags);
840 extern void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
841 					    struct vmw_fence_obj *fence);
842 extern void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv);
843 
844 extern int vmw_execbuf_fence_commands(struct drm_file *file_priv,
845 				      struct vmw_private *dev_priv,
846 				      struct vmw_fence_obj **p_fence,
847 				      uint32_t *p_handle);
848 extern void vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
849 					struct vmw_fpriv *vmw_fp,
850 					int ret,
851 					struct drm_vmw_fence_rep __user
852 					*user_fence_rep,
853 					struct vmw_fence_obj *fence,
854 					uint32_t fence_handle,
855 					int32_t out_fence_fd,
856 					struct sync_file *sync_file);
857 extern int vmw_validate_single_buffer(struct vmw_private *dev_priv,
858 				      struct ttm_buffer_object *bo,
859 				      bool interruptible,
860 				      bool validate_as_mob);
861 bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd);
862 
863 /**
864  * IRQs and wating - vmwgfx_irq.c
865  */
866 
867 extern int vmw_wait_seqno(struct vmw_private *dev_priv, bool lazy,
868 			  uint32_t seqno, bool interruptible,
869 			  unsigned long timeout);
870 extern int vmw_irq_install(struct drm_device *dev, int irq);
871 extern void vmw_irq_uninstall(struct drm_device *dev);
872 extern bool vmw_seqno_passed(struct vmw_private *dev_priv,
873 				uint32_t seqno);
874 extern int vmw_fallback_wait(struct vmw_private *dev_priv,
875 			     bool lazy,
876 			     bool fifo_idle,
877 			     uint32_t seqno,
878 			     bool interruptible,
879 			     unsigned long timeout);
880 extern void vmw_update_seqno(struct vmw_private *dev_priv,
881 				struct vmw_fifo_state *fifo_state);
882 extern void vmw_seqno_waiter_add(struct vmw_private *dev_priv);
883 extern void vmw_seqno_waiter_remove(struct vmw_private *dev_priv);
884 extern void vmw_goal_waiter_add(struct vmw_private *dev_priv);
885 extern void vmw_goal_waiter_remove(struct vmw_private *dev_priv);
886 extern void vmw_generic_waiter_add(struct vmw_private *dev_priv, u32 flag,
887 				   int *waiter_count);
888 extern void vmw_generic_waiter_remove(struct vmw_private *dev_priv,
889 				      u32 flag, int *waiter_count);
890 
891 /**
892  * Rudimentary fence-like objects currently used only for throttling -
893  * vmwgfx_marker.c
894  */
895 
896 extern void vmw_marker_queue_init(struct vmw_marker_queue *queue);
897 extern void vmw_marker_queue_takedown(struct vmw_marker_queue *queue);
898 extern int vmw_marker_push(struct vmw_marker_queue *queue,
899 			   uint32_t seqno);
900 extern int vmw_marker_pull(struct vmw_marker_queue *queue,
901 			   uint32_t signaled_seqno);
902 extern int vmw_wait_lag(struct vmw_private *dev_priv,
903 			struct vmw_marker_queue *queue, uint32_t us);
904 
905 /**
906  * Kernel framebuffer - vmwgfx_fb.c
907  */
908 
909 int vmw_fb_init(struct vmw_private *vmw_priv);
910 int vmw_fb_close(struct vmw_private *dev_priv);
911 int vmw_fb_off(struct vmw_private *vmw_priv);
912 int vmw_fb_on(struct vmw_private *vmw_priv);
913 
914 /**
915  * Kernel modesetting - vmwgfx_kms.c
916  */
917 
918 int vmw_kms_init(struct vmw_private *dev_priv);
919 int vmw_kms_close(struct vmw_private *dev_priv);
920 int vmw_kms_save_vga(struct vmw_private *vmw_priv);
921 int vmw_kms_restore_vga(struct vmw_private *vmw_priv);
922 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
923 				struct drm_file *file_priv);
924 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv);
925 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
926 			  struct ttm_object_file *tfile,
927 			  struct ttm_buffer_object *bo,
928 			  SVGA3dCmdHeader *header);
929 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
930 		       unsigned width, unsigned height, unsigned pitch,
931 		       unsigned bpp, unsigned depth);
932 void vmw_kms_idle_workqueues(struct vmw_master *vmaster);
933 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
934 				uint32_t pitch,
935 				uint32_t height);
936 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe);
937 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe);
938 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe);
939 int vmw_kms_present(struct vmw_private *dev_priv,
940 		    struct drm_file *file_priv,
941 		    struct vmw_framebuffer *vfb,
942 		    struct vmw_surface *surface,
943 		    uint32_t sid, int32_t destX, int32_t destY,
944 		    struct drm_vmw_rect *clips,
945 		    uint32_t num_clips);
946 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
947 				struct drm_file *file_priv);
948 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv);
949 int vmw_kms_suspend(struct drm_device *dev);
950 int vmw_kms_resume(struct drm_device *dev);
951 void vmw_kms_lost_device(struct drm_device *dev);
952 
953 int vmw_dumb_create(struct drm_file *file_priv,
954 		    struct drm_device *dev,
955 		    struct drm_mode_create_dumb *args);
956 
957 int vmw_dumb_map_offset(struct drm_file *file_priv,
958 			struct drm_device *dev, uint32_t handle,
959 			uint64_t *offset);
960 int vmw_dumb_destroy(struct drm_file *file_priv,
961 		     struct drm_device *dev,
962 		     uint32_t handle);
963 extern int vmw_resource_pin(struct vmw_resource *res, bool interruptible);
964 extern void vmw_resource_unpin(struct vmw_resource *res);
965 extern enum vmw_res_type vmw_res_type(const struct vmw_resource *res);
966 
967 /**
968  * Overlay control - vmwgfx_overlay.c
969  */
970 
971 int vmw_overlay_init(struct vmw_private *dev_priv);
972 int vmw_overlay_close(struct vmw_private *dev_priv);
973 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
974 		      struct drm_file *file_priv);
975 int vmw_overlay_stop_all(struct vmw_private *dev_priv);
976 int vmw_overlay_resume_all(struct vmw_private *dev_priv);
977 int vmw_overlay_pause_all(struct vmw_private *dev_priv);
978 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out);
979 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id);
980 int vmw_overlay_num_overlays(struct vmw_private *dev_priv);
981 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv);
982 
983 /**
984  * GMR Id manager
985  */
986 
987 extern const struct ttm_mem_type_manager_func vmw_gmrid_manager_func;
988 
989 /**
990  * Prime - vmwgfx_prime.c
991  */
992 
993 extern const struct dma_buf_ops vmw_prime_dmabuf_ops;
994 extern int vmw_prime_fd_to_handle(struct drm_device *dev,
995 				  struct drm_file *file_priv,
996 				  int fd, u32 *handle);
997 extern int vmw_prime_handle_to_fd(struct drm_device *dev,
998 				  struct drm_file *file_priv,
999 				  uint32_t handle, uint32_t flags,
1000 				  int *prime_fd);
1001 
1002 /*
1003  * MemoryOBject management -  vmwgfx_mob.c
1004  */
1005 struct vmw_mob;
1006 extern int vmw_mob_bind(struct vmw_private *dev_priv, struct vmw_mob *mob,
1007 			const struct vmw_sg_table *vsgt,
1008 			unsigned long num_data_pages, int32_t mob_id);
1009 extern void vmw_mob_unbind(struct vmw_private *dev_priv,
1010 			   struct vmw_mob *mob);
1011 extern void vmw_mob_destroy(struct vmw_mob *mob);
1012 extern struct vmw_mob *vmw_mob_create(unsigned long data_pages);
1013 extern int vmw_otables_setup(struct vmw_private *dev_priv);
1014 extern void vmw_otables_takedown(struct vmw_private *dev_priv);
1015 
1016 /*
1017  * Context management - vmwgfx_context.c
1018  */
1019 
1020 extern const struct vmw_user_resource_conv *user_context_converter;
1021 
1022 extern int vmw_context_check(struct vmw_private *dev_priv,
1023 			     struct ttm_object_file *tfile,
1024 			     int id,
1025 			     struct vmw_resource **p_res);
1026 extern int vmw_context_define_ioctl(struct drm_device *dev, void *data,
1027 				    struct drm_file *file_priv);
1028 extern int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data,
1029 					     struct drm_file *file_priv);
1030 extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
1031 				     struct drm_file *file_priv);
1032 extern struct list_head *vmw_context_binding_list(struct vmw_resource *ctx);
1033 extern struct vmw_cmdbuf_res_manager *
1034 vmw_context_res_man(struct vmw_resource *ctx);
1035 extern struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx,
1036 						SVGACOTableType cotable_type);
1037 extern struct list_head *vmw_context_binding_list(struct vmw_resource *ctx);
1038 struct vmw_ctx_binding_state;
1039 extern struct vmw_ctx_binding_state *
1040 vmw_context_binding_state(struct vmw_resource *ctx);
1041 extern void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx,
1042 					  bool readback);
1043 extern int vmw_context_bind_dx_query(struct vmw_resource *ctx_res,
1044 				     struct vmw_dma_buffer *mob);
1045 extern struct vmw_dma_buffer *
1046 vmw_context_get_dx_query_mob(struct vmw_resource *ctx_res);
1047 
1048 
1049 /*
1050  * Surface management - vmwgfx_surface.c
1051  */
1052 
1053 extern const struct vmw_user_resource_conv *user_surface_converter;
1054 
1055 extern void vmw_surface_res_free(struct vmw_resource *res);
1056 extern int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
1057 				     struct drm_file *file_priv);
1058 extern int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
1059 				    struct drm_file *file_priv);
1060 extern int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
1061 				       struct drm_file *file_priv);
1062 extern int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1063 				       struct drm_file *file_priv);
1064 extern int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1065 					  struct drm_file *file_priv);
1066 extern int vmw_surface_check(struct vmw_private *dev_priv,
1067 			     struct ttm_object_file *tfile,
1068 			     uint32_t handle, int *id);
1069 extern int vmw_surface_validate(struct vmw_private *dev_priv,
1070 				struct vmw_surface *srf);
1071 int vmw_surface_gb_priv_define(struct drm_device *dev,
1072 			       uint32_t user_accounting_size,
1073 			       uint32_t svga3d_flags,
1074 			       SVGA3dSurfaceFormat format,
1075 			       bool for_scanout,
1076 			       uint32_t num_mip_levels,
1077 			       uint32_t multisample_count,
1078 			       uint32_t array_size,
1079 			       struct drm_vmw_size size,
1080 			       struct vmw_surface **srf_out);
1081 
1082 /*
1083  * Shader management - vmwgfx_shader.c
1084  */
1085 
1086 extern const struct vmw_user_resource_conv *user_shader_converter;
1087 
1088 extern int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
1089 				   struct drm_file *file_priv);
1090 extern int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
1091 				    struct drm_file *file_priv);
1092 extern int vmw_compat_shader_add(struct vmw_private *dev_priv,
1093 				 struct vmw_cmdbuf_res_manager *man,
1094 				 u32 user_key, const void *bytecode,
1095 				 SVGA3dShaderType shader_type,
1096 				 size_t size,
1097 				 struct list_head *list);
1098 extern int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
1099 			     u32 user_key, SVGA3dShaderType shader_type,
1100 			     struct list_head *list);
1101 extern int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
1102 			     struct vmw_resource *ctx,
1103 			     u32 user_key,
1104 			     SVGA3dShaderType shader_type,
1105 			     struct list_head *list);
1106 extern void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
1107 					     struct list_head *list,
1108 					     bool readback);
1109 
1110 extern struct vmw_resource *
1111 vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
1112 		  u32 user_key, SVGA3dShaderType shader_type);
1113 
1114 /*
1115  * Command buffer managed resources - vmwgfx_cmdbuf_res.c
1116  */
1117 
1118 extern struct vmw_cmdbuf_res_manager *
1119 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv);
1120 extern void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man);
1121 extern size_t vmw_cmdbuf_res_man_size(void);
1122 extern struct vmw_resource *
1123 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
1124 		      enum vmw_cmdbuf_res_type res_type,
1125 		      u32 user_key);
1126 extern void vmw_cmdbuf_res_revert(struct list_head *list);
1127 extern void vmw_cmdbuf_res_commit(struct list_head *list);
1128 extern int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
1129 			      enum vmw_cmdbuf_res_type res_type,
1130 			      u32 user_key,
1131 			      struct vmw_resource *res,
1132 			      struct list_head *list);
1133 extern int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
1134 				 enum vmw_cmdbuf_res_type res_type,
1135 				 u32 user_key,
1136 				 struct list_head *list,
1137 				 struct vmw_resource **res);
1138 
1139 /*
1140  * COTable management - vmwgfx_cotable.c
1141  */
1142 extern const SVGACOTableType vmw_cotable_scrub_order[];
1143 extern struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
1144 					      struct vmw_resource *ctx,
1145 					      u32 type);
1146 extern int vmw_cotable_notify(struct vmw_resource *res, int id);
1147 extern int vmw_cotable_scrub(struct vmw_resource *res, bool readback);
1148 extern void vmw_cotable_add_resource(struct vmw_resource *ctx,
1149 				     struct list_head *head);
1150 
1151 /*
1152  * Command buffer managerment vmwgfx_cmdbuf.c
1153  */
1154 struct vmw_cmdbuf_man;
1155 struct vmw_cmdbuf_header;
1156 
1157 extern struct vmw_cmdbuf_man *
1158 vmw_cmdbuf_man_create(struct vmw_private *dev_priv);
1159 extern int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
1160 				    size_t size, size_t default_size);
1161 extern void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man);
1162 extern void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man);
1163 extern int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
1164 			   unsigned long timeout);
1165 extern void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
1166 				int ctx_id, bool interruptible,
1167 				struct vmw_cmdbuf_header *header);
1168 extern void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
1169 			      struct vmw_cmdbuf_header *header,
1170 			      bool flush);
1171 extern void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
1172 			      size_t size, bool interruptible,
1173 			      struct vmw_cmdbuf_header **p_header);
1174 extern void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header);
1175 extern int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
1176 				bool interruptible);
1177 extern void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man);
1178 
1179 /* CPU blit utilities - vmwgfx_blit.c */
1180 
1181 /**
1182  * struct vmw_diff_cpy - CPU blit information structure
1183  *
1184  * @rect: The output bounding box rectangle.
1185  * @line: The current line of the blit.
1186  * @line_offset: Offset of the current line segment.
1187  * @cpp: Bytes per pixel (granularity information).
1188  * @memcpy: Which memcpy function to use.
1189  */
1190 struct vmw_diff_cpy {
1191 	struct drm_rect rect;
1192 	size_t line;
1193 	size_t line_offset;
1194 	int cpp;
1195 	void (*do_cpy)(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
1196 		       size_t n);
1197 };
1198 
1199 #define VMW_CPU_BLIT_INITIALIZER {	\
1200 	.do_cpy = vmw_memcpy,		\
1201 }
1202 
1203 #define VMW_CPU_BLIT_DIFF_INITIALIZER(_cpp) {	  \
1204 	.line = 0,				  \
1205 	.line_offset = 0,			  \
1206 	.rect = { .x1 = INT_MAX/2,		  \
1207 		  .y1 = INT_MAX/2,		  \
1208 		  .x2 = INT_MIN/2,		  \
1209 		  .y2 = INT_MIN/2		  \
1210 	},					  \
1211 	.cpp = _cpp,				  \
1212 	.do_cpy = vmw_diff_memcpy,		  \
1213 }
1214 
1215 void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
1216 		     size_t n);
1217 
1218 void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n);
1219 
1220 int vmw_bo_cpu_blit(struct ttm_buffer_object *dst,
1221 		    u32 dst_offset, u32 dst_stride,
1222 		    struct ttm_buffer_object *src,
1223 		    u32 src_offset, u32 src_stride,
1224 		    u32 w, u32 h,
1225 		    struct vmw_diff_cpy *diff);
1226 
1227 /**
1228  * Inline helper functions
1229  */
1230 
1231 static inline void vmw_surface_unreference(struct vmw_surface **srf)
1232 {
1233 	struct vmw_surface *tmp_srf = *srf;
1234 	struct vmw_resource *res = &tmp_srf->res;
1235 	*srf = NULL;
1236 
1237 	vmw_resource_unreference(&res);
1238 }
1239 
1240 static inline struct vmw_surface *vmw_surface_reference(struct vmw_surface *srf)
1241 {
1242 	(void) vmw_resource_reference(&srf->res);
1243 	return srf;
1244 }
1245 
1246 static inline void vmw_dmabuf_unreference(struct vmw_dma_buffer **buf)
1247 {
1248 	struct vmw_dma_buffer *tmp_buf = *buf;
1249 
1250 	*buf = NULL;
1251 	if (tmp_buf != NULL) {
1252 		struct ttm_buffer_object *bo = &tmp_buf->base;
1253 
1254 		ttm_bo_unref(&bo);
1255 	}
1256 }
1257 
1258 static inline struct vmw_dma_buffer *vmw_dmabuf_reference(struct vmw_dma_buffer *buf)
1259 {
1260 	if (ttm_bo_reference(&buf->base))
1261 		return buf;
1262 	return NULL;
1263 }
1264 
1265 static inline struct ttm_mem_global *vmw_mem_glob(struct vmw_private *dev_priv)
1266 {
1267 	return (struct ttm_mem_global *) dev_priv->mem_global_ref.object;
1268 }
1269 
1270 static inline void vmw_fifo_resource_inc(struct vmw_private *dev_priv)
1271 {
1272 	atomic_inc(&dev_priv->num_fifo_resources);
1273 }
1274 
1275 static inline void vmw_fifo_resource_dec(struct vmw_private *dev_priv)
1276 {
1277 	atomic_dec(&dev_priv->num_fifo_resources);
1278 }
1279 
1280 /**
1281  * vmw_mmio_read - Perform a MMIO read from volatile memory
1282  *
1283  * @addr: The address to read from
1284  *
1285  * This function is intended to be equivalent to ioread32() on
1286  * memremap'd memory, but without byteswapping.
1287  */
1288 static inline u32 vmw_mmio_read(u32 *addr)
1289 {
1290 	return READ_ONCE(*addr);
1291 }
1292 
1293 /**
1294  * vmw_mmio_write - Perform a MMIO write to volatile memory
1295  *
1296  * @addr: The address to write to
1297  *
1298  * This function is intended to be equivalent to iowrite32 on
1299  * memremap'd memory, but without byteswapping.
1300  */
1301 static inline void vmw_mmio_write(u32 value, u32 *addr)
1302 {
1303 	WRITE_ONCE(*addr, value);
1304 }
1305 
1306 /**
1307  * Add vmw_msg module function
1308  */
1309 extern int vmw_host_log(const char *log);
1310 
1311 #endif
1312