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