1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright 2009-2022 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 <linux/suspend.h>
32 #include <linux/sync_file.h>
33 #include <linux/hashtable.h>
34 
35 #include <drm/drm_auth.h>
36 #include <drm/drm_device.h>
37 #include <drm/drm_file.h>
38 #include <drm/drm_rect.h>
39 
40 #include <drm/ttm/ttm_execbuf_util.h>
41 #include <drm/ttm/ttm_tt.h>
42 #include <drm/ttm/ttm_placement.h>
43 #include <drm/ttm/ttm_bo.h>
44 
45 #include "ttm_object.h"
46 
47 #include "vmwgfx_fence.h"
48 #include "vmwgfx_reg.h"
49 #include "vmwgfx_validation.h"
50 
51 /*
52  * FIXME: vmwgfx_drm.h needs to be last due to dependencies.
53  * uapi headers should not depend on header files outside uapi/.
54  */
55 #include <drm/vmwgfx_drm.h>
56 
57 
58 #define VMWGFX_DRIVER_NAME "vmwgfx"
59 #define VMWGFX_DRIVER_DATE "20211206"
60 #define VMWGFX_DRIVER_MAJOR 2
61 #define VMWGFX_DRIVER_MINOR 20
62 #define VMWGFX_DRIVER_PATCHLEVEL 0
63 #define VMWGFX_FIFO_STATIC_SIZE (1024*1024)
64 #define VMWGFX_MAX_DISPLAYS 16
65 #define VMWGFX_CMD_BOUNCE_INIT_SIZE 32768
66 
67 #define VMWGFX_MIN_INITIAL_WIDTH 1280
68 #define VMWGFX_MIN_INITIAL_HEIGHT 800
69 
70 #define VMWGFX_PCI_ID_SVGA2              0x0405
71 #define VMWGFX_PCI_ID_SVGA3              0x0406
72 
73 /*
74  * This has to match get_count_order(SVGA_IRQFLAG_MAX)
75  */
76 #define VMWGFX_MAX_NUM_IRQS 6
77 
78 /*
79  * Perhaps we should have sysfs entries for these.
80  */
81 #define VMWGFX_NUM_GB_CONTEXT 256
82 #define VMWGFX_NUM_GB_SHADER 20000
83 #define VMWGFX_NUM_GB_SURFACE 32768
84 #define VMWGFX_NUM_GB_SCREEN_TARGET VMWGFX_MAX_DISPLAYS
85 #define VMWGFX_NUM_DXCONTEXT 256
86 #define VMWGFX_NUM_DXQUERY 512
87 #define VMWGFX_NUM_MOB (VMWGFX_NUM_GB_CONTEXT +\
88 			VMWGFX_NUM_GB_SHADER +\
89 			VMWGFX_NUM_GB_SURFACE +\
90 			VMWGFX_NUM_GB_SCREEN_TARGET)
91 
92 #define VMW_PL_GMR      (TTM_PL_PRIV + 0)
93 #define VMW_PL_MOB      (TTM_PL_PRIV + 1)
94 #define VMW_PL_SYSTEM   (TTM_PL_PRIV + 2)
95 
96 #define VMW_RES_CONTEXT ttm_driver_type0
97 #define VMW_RES_SURFACE ttm_driver_type1
98 #define VMW_RES_STREAM ttm_driver_type2
99 #define VMW_RES_FENCE ttm_driver_type3
100 #define VMW_RES_SHADER ttm_driver_type4
101 #define VMW_RES_HT_ORDER 12
102 
103 #define VMW_CURSOR_SNOOP_FORMAT SVGA3D_A8R8G8B8
104 #define VMW_CURSOR_SNOOP_WIDTH 64
105 #define VMW_CURSOR_SNOOP_HEIGHT 64
106 
107 #define MKSSTAT_CAPACITY_LOG2 5U
108 #define MKSSTAT_CAPACITY (1U << MKSSTAT_CAPACITY_LOG2)
109 
110 struct vmw_fpriv {
111 	struct ttm_object_file *tfile;
112 	bool gb_aware; /* user-space is guest-backed aware */
113 };
114 
115 struct vmwgfx_hash_item {
116 	struct hlist_node head;
117 	unsigned long key;
118 };
119 
120 /**
121  * struct vmw_buffer_object - TTM buffer object with vmwgfx additions
122  * @base: The TTM buffer object
123  * @res_tree: RB tree of resources using this buffer object as a backing MOB
124  * @base_mapped_count: ttm BO mapping count; used by KMS atomic helpers.
125  * @cpu_writers: Number of synccpu write grabs. Protected by reservation when
126  * increased. May be decreased without reservation.
127  * @dx_query_ctx: DX context if this buffer object is used as a DX query MOB
128  * @map: Kmap object for semi-persistent mappings
129  * @res_prios: Eviction priority counts for attached resources
130  * @dirty: structure for user-space dirty-tracking
131  */
132 struct vmw_buffer_object {
133 	struct ttm_buffer_object base;
134 	struct rb_root res_tree;
135 	/* For KMS atomic helpers: ttm bo mapping count */
136 	atomic_t base_mapped_count;
137 
138 	atomic_t cpu_writers;
139 	/* Not ref-counted.  Protected by binding_mutex */
140 	struct vmw_resource *dx_query_ctx;
141 	/* Protected by reservation */
142 	struct ttm_bo_kmap_obj map;
143 	u32 res_prios[TTM_MAX_BO_PRIORITY];
144 	struct vmw_bo_dirty *dirty;
145 };
146 
147 /**
148  * struct vmw_validate_buffer - Carries validation info about buffers.
149  *
150  * @base: Validation info for TTM.
151  * @hash: Hash entry for quick lookup of the TTM buffer object.
152  *
153  * This structure contains also driver private validation info
154  * on top of the info needed by TTM.
155  */
156 struct vmw_validate_buffer {
157 	struct ttm_validate_buffer base;
158 	struct vmwgfx_hash_item hash;
159 	bool validate_as_mob;
160 };
161 
162 struct vmw_res_func;
163 
164 
165 /**
166  * struct vmw-resource - base class for hardware resources
167  *
168  * @kref: For refcounting.
169  * @dev_priv: Pointer to the device private for this resource. Immutable.
170  * @id: Device id. Protected by @dev_priv::resource_lock.
171  * @backup_size: Backup buffer size. Immutable.
172  * @res_dirty: Resource contains data not yet in the backup buffer. Protected
173  * by resource reserved.
174  * @backup_dirty: Backup buffer contains data not yet in the HW resource.
175  * Protected by resource reserved.
176  * @coherent: Emulate coherency by tracking vm accesses.
177  * @backup: The backup buffer if any. Protected by resource reserved.
178  * @backup_offset: Offset into the backup buffer if any. Protected by resource
179  * reserved. Note that only a few resource types can have a @backup_offset
180  * different from zero.
181  * @pin_count: The pin count for this resource. A pinned resource has a
182  * pin-count greater than zero. It is not on the resource LRU lists and its
183  * backup buffer is pinned. Hence it can't be evicted.
184  * @func: Method vtable for this resource. Immutable.
185  * @mob_node; Node for the MOB backup rbtree. Protected by @backup reserved.
186  * @lru_head: List head for the LRU list. Protected by @dev_priv::resource_lock.
187  * @binding_head: List head for the context binding list. Protected by
188  * the @dev_priv::binding_mutex
189  * @res_free: The resource destructor.
190  * @hw_destroy: Callback to destroy the resource on the device, as part of
191  * resource destruction.
192  */
193 struct vmw_resource_dirty;
194 struct vmw_resource {
195 	struct kref kref;
196 	struct vmw_private *dev_priv;
197 	int id;
198 	u32 used_prio;
199 	unsigned long backup_size;
200 	u32 res_dirty : 1;
201 	u32 backup_dirty : 1;
202 	u32 coherent : 1;
203 	struct vmw_buffer_object *backup;
204 	unsigned long backup_offset;
205 	unsigned long pin_count;
206 	const struct vmw_res_func *func;
207 	struct rb_node mob_node;
208 	struct list_head lru_head;
209 	struct list_head binding_head;
210 	struct vmw_resource_dirty *dirty;
211 	void (*res_free) (struct vmw_resource *res);
212 	void (*hw_destroy) (struct vmw_resource *res);
213 };
214 
215 
216 /*
217  * Resources that are managed using ioctls.
218  */
219 enum vmw_res_type {
220 	vmw_res_context,
221 	vmw_res_surface,
222 	vmw_res_stream,
223 	vmw_res_shader,
224 	vmw_res_dx_context,
225 	vmw_res_cotable,
226 	vmw_res_view,
227 	vmw_res_streamoutput,
228 	vmw_res_max
229 };
230 
231 /*
232  * Resources that are managed using command streams.
233  */
234 enum vmw_cmdbuf_res_type {
235 	vmw_cmdbuf_res_shader,
236 	vmw_cmdbuf_res_view,
237 	vmw_cmdbuf_res_streamoutput
238 };
239 
240 struct vmw_cmdbuf_res_manager;
241 
242 struct vmw_cursor_snooper {
243 	size_t age;
244 	uint32_t *image;
245 };
246 
247 struct vmw_framebuffer;
248 struct vmw_surface_offset;
249 
250 /**
251  * struct vmw_surface_metadata - Metadata describing a surface.
252  *
253  * @flags: Device flags.
254  * @format: Surface SVGA3D_x format.
255  * @mip_levels: Mip level for each face. For GB first index is used only.
256  * @multisample_count: Sample count.
257  * @multisample_pattern: Sample patterns.
258  * @quality_level: Quality level.
259  * @autogen_filter: Filter for automatically generated mipmaps.
260  * @array_size: Number of array elements for a 1D/2D texture. For cubemap
261                 texture number of faces * array_size. This should be 0 for pre
262 		SM4 device.
263  * @buffer_byte_stride: Buffer byte stride.
264  * @num_sizes: Size of @sizes. For GB surface this should always be 1.
265  * @base_size: Surface dimension.
266  * @sizes: Array representing mip sizes. Legacy only.
267  * @scanout: Whether this surface will be used for scanout.
268  *
269  * This tracks metadata for both legacy and guest backed surface.
270  */
271 struct vmw_surface_metadata {
272 	u64 flags;
273 	u32 format;
274 	u32 mip_levels[DRM_VMW_MAX_SURFACE_FACES];
275 	u32 multisample_count;
276 	u32 multisample_pattern;
277 	u32 quality_level;
278 	u32 autogen_filter;
279 	u32 array_size;
280 	u32 num_sizes;
281 	u32 buffer_byte_stride;
282 	struct drm_vmw_size base_size;
283 	struct drm_vmw_size *sizes;
284 	bool scanout;
285 };
286 
287 /**
288  * struct vmw_surface: Resource structure for a surface.
289  *
290  * @res: The base resource for this surface.
291  * @metadata: Metadata for this surface resource.
292  * @snooper: Cursor data. Legacy surface only.
293  * @offsets: Legacy surface only.
294  * @view_list: List of views bound to this surface.
295  */
296 struct vmw_surface {
297 	struct vmw_resource res;
298 	struct vmw_surface_metadata metadata;
299 	struct vmw_cursor_snooper snooper;
300 	struct vmw_surface_offset *offsets;
301 	struct list_head view_list;
302 };
303 
304 struct vmw_fifo_state {
305 	unsigned long reserved_size;
306 	u32 *dynamic_buffer;
307 	u32 *static_buffer;
308 	unsigned long static_buffer_size;
309 	bool using_bounce_buffer;
310 	uint32_t capabilities;
311 	struct mutex fifo_mutex;
312 	struct rw_semaphore rwsem;
313 };
314 
315 /**
316  * struct vmw_res_cache_entry - resource information cache entry
317  * @handle: User-space handle of a resource.
318  * @res: Non-ref-counted pointer to the resource.
319  * @valid_handle: Whether the @handle member is valid.
320  * @valid: Whether the entry is valid, which also implies that the execbuf
321  * code holds a reference to the resource, and it's placed on the
322  * validation list.
323  *
324  * Used to avoid frequent repeated user-space handle lookups of the
325  * same resource.
326  */
327 struct vmw_res_cache_entry {
328 	uint32_t handle;
329 	struct vmw_resource *res;
330 	void *private;
331 	unsigned short valid_handle;
332 	unsigned short valid;
333 };
334 
335 /**
336  * enum vmw_dma_map_mode - indicate how to perform TTM page dma mappings.
337  */
338 enum vmw_dma_map_mode {
339 	vmw_dma_alloc_coherent, /* Use TTM coherent pages */
340 	vmw_dma_map_populate,   /* Unmap from DMA just after unpopulate */
341 	vmw_dma_map_bind,       /* Unmap from DMA just before unbind */
342 	vmw_dma_map_max
343 };
344 
345 /**
346  * struct vmw_sg_table - Scatter/gather table for binding, with additional
347  * device-specific information.
348  *
349  * @sgt: Pointer to a struct sg_table with binding information
350  * @num_regions: Number of regions with device-address contiguous pages
351  */
352 struct vmw_sg_table {
353 	enum vmw_dma_map_mode mode;
354 	struct page **pages;
355 	const dma_addr_t *addrs;
356 	struct sg_table *sgt;
357 	unsigned long num_pages;
358 };
359 
360 /**
361  * struct vmw_piter - Page iterator that iterates over a list of pages
362  * and DMA addresses that could be either a scatter-gather list or
363  * arrays
364  *
365  * @pages: Array of page pointers to the pages.
366  * @addrs: DMA addresses to the pages if coherent pages are used.
367  * @iter: Scatter-gather page iterator. Current position in SG list.
368  * @i: Current position in arrays.
369  * @num_pages: Number of pages total.
370  * @next: Function to advance the iterator. Returns false if past the list
371  * of pages, true otherwise.
372  * @dma_address: Function to return the DMA address of the current page.
373  */
374 struct vmw_piter {
375 	struct page **pages;
376 	const dma_addr_t *addrs;
377 	struct sg_dma_page_iter iter;
378 	unsigned long i;
379 	unsigned long num_pages;
380 	bool (*next)(struct vmw_piter *);
381 	dma_addr_t (*dma_address)(struct vmw_piter *);
382 };
383 
384 
385 struct vmw_ttm_tt {
386 	struct ttm_tt dma_ttm;
387 	struct vmw_private *dev_priv;
388 	int gmr_id;
389 	struct vmw_mob *mob;
390 	int mem_type;
391 	struct sg_table sgt;
392 	struct vmw_sg_table vsgt;
393 	bool mapped;
394 	bool bound;
395 };
396 
397 /*
398  * enum vmw_display_unit_type - Describes the display unit
399  */
400 enum vmw_display_unit_type {
401 	vmw_du_invalid = 0,
402 	vmw_du_legacy,
403 	vmw_du_screen_object,
404 	vmw_du_screen_target,
405 	vmw_du_max
406 };
407 
408 struct vmw_validation_context;
409 struct vmw_ctx_validation_info;
410 
411 /**
412  * struct vmw_sw_context - Command submission context
413  * @res_ht: Pointer hash table used to find validation duplicates
414  * @kernel: Whether the command buffer originates from kernel code rather
415  * than from user-space
416  * @fp: If @kernel is false, points to the file of the client. Otherwise
417  * NULL
418  * @cmd_bounce: Command bounce buffer used for command validation before
419  * copying to fifo space
420  * @cmd_bounce_size: Current command bounce buffer size
421  * @cur_query_bo: Current buffer object used as query result buffer
422  * @bo_relocations: List of buffer object relocations
423  * @res_relocations: List of resource relocations
424  * @buf_start: Pointer to start of memory where command validation takes
425  * place
426  * @res_cache: Cache of recently looked up resources
427  * @last_query_ctx: Last context that submitted a query
428  * @needs_post_query_barrier: Whether a query barrier is needed after
429  * command submission
430  * @staged_bindings: Cached per-context binding tracker
431  * @staged_bindings_inuse: Whether the cached per-context binding tracker
432  * is in use
433  * @staged_cmd_res: List of staged command buffer managed resources in this
434  * command buffer
435  * @ctx_list: List of context resources referenced in this command buffer
436  * @dx_ctx_node: Validation metadata of the current DX context
437  * @dx_query_mob: The MOB used for DX queries
438  * @dx_query_ctx: The DX context used for the last DX query
439  * @man: Pointer to the command buffer managed resource manager
440  * @ctx: The validation context
441  */
442 struct vmw_sw_context{
443 	DECLARE_HASHTABLE(res_ht, VMW_RES_HT_ORDER);
444 	bool kernel;
445 	struct vmw_fpriv *fp;
446 	struct drm_file *filp;
447 	uint32_t *cmd_bounce;
448 	uint32_t cmd_bounce_size;
449 	struct vmw_buffer_object *cur_query_bo;
450 	struct list_head bo_relocations;
451 	struct list_head res_relocations;
452 	uint32_t *buf_start;
453 	struct vmw_res_cache_entry res_cache[vmw_res_max];
454 	struct vmw_resource *last_query_ctx;
455 	bool needs_post_query_barrier;
456 	struct vmw_ctx_binding_state *staged_bindings;
457 	bool staged_bindings_inuse;
458 	struct list_head staged_cmd_res;
459 	struct list_head ctx_list;
460 	struct vmw_ctx_validation_info *dx_ctx_node;
461 	struct vmw_buffer_object *dx_query_mob;
462 	struct vmw_resource *dx_query_ctx;
463 	struct vmw_cmdbuf_res_manager *man;
464 	struct vmw_validation_context *ctx;
465 };
466 
467 struct vmw_legacy_display;
468 struct vmw_overlay;
469 
470 struct vmw_vga_topology_state {
471 	uint32_t width;
472 	uint32_t height;
473 	uint32_t primary;
474 	uint32_t pos_x;
475 	uint32_t pos_y;
476 };
477 
478 
479 /*
480  * struct vmw_otable - Guest Memory OBject table metadata
481  *
482  * @size:           Size of the table (page-aligned).
483  * @page_table:     Pointer to a struct vmw_mob holding the page table.
484  */
485 struct vmw_otable {
486 	unsigned long size;
487 	struct vmw_mob *page_table;
488 	bool enabled;
489 };
490 
491 struct vmw_otable_batch {
492 	unsigned num_otables;
493 	struct vmw_otable *otables;
494 	struct vmw_resource *context;
495 	struct ttm_buffer_object *otable_bo;
496 };
497 
498 enum {
499 	VMW_IRQTHREAD_FENCE,
500 	VMW_IRQTHREAD_CMDBUF,
501 	VMW_IRQTHREAD_MAX
502 };
503 
504 /**
505  * enum vmw_sm_type - Graphics context capability supported by device.
506  * @VMW_SM_LEGACY: Pre DX context.
507  * @VMW_SM_4: Context support upto SM4.
508  * @VMW_SM_4_1: Context support upto SM4_1.
509  * @VMW_SM_5: Context support up to SM5.
510  * @VMW_SM_5_1X: Adds support for sm5_1 and gl43 extensions.
511  * @VMW_SM_MAX: Should be the last.
512  */
513 enum vmw_sm_type {
514 	VMW_SM_LEGACY = 0,
515 	VMW_SM_4,
516 	VMW_SM_4_1,
517 	VMW_SM_5,
518 	VMW_SM_5_1X,
519 	VMW_SM_MAX
520 };
521 
522 struct vmw_private {
523 	struct drm_device drm;
524 	struct ttm_device bdev;
525 
526 	struct drm_vma_offset_manager vma_manager;
527 	u32 pci_id;
528 	resource_size_t io_start;
529 	resource_size_t vram_start;
530 	resource_size_t vram_size;
531 	resource_size_t max_primary_mem;
532 	u32 __iomem *rmmio;
533 	u32 *fifo_mem;
534 	resource_size_t fifo_mem_size;
535 	uint32_t fb_max_width;
536 	uint32_t fb_max_height;
537 	uint32_t texture_max_width;
538 	uint32_t texture_max_height;
539 	uint32_t stdu_max_width;
540 	uint32_t stdu_max_height;
541 	uint32_t initial_width;
542 	uint32_t initial_height;
543 	uint32_t capabilities;
544 	uint32_t capabilities2;
545 	uint32_t max_gmr_ids;
546 	uint32_t max_gmr_pages;
547 	uint32_t max_mob_pages;
548 	uint32_t max_mob_size;
549 	uint32_t memory_size;
550 	bool has_gmr;
551 	bool has_mob;
552 	spinlock_t hw_lock;
553 	bool assume_16bpp;
554 	u32 irqs[VMWGFX_MAX_NUM_IRQS];
555 	u32 num_irq_vectors;
556 
557 	enum vmw_sm_type sm_type;
558 
559 	/*
560 	 * Framebuffer info.
561 	 */
562 
563 	enum vmw_display_unit_type active_display_unit;
564 	struct vmw_legacy_display *ldu_priv;
565 	struct vmw_overlay *overlay_priv;
566 	struct drm_property *hotplug_mode_update_property;
567 	struct drm_property *implicit_placement_property;
568 	spinlock_t cursor_lock;
569 	struct drm_atomic_state *suspend_state;
570 
571 	/*
572 	 * Context and surface management.
573 	 */
574 
575 	spinlock_t resource_lock;
576 	struct idr res_idr[vmw_res_max];
577 
578 	/*
579 	 * A resource manager for kernel-only surfaces and
580 	 * contexts.
581 	 */
582 
583 	struct ttm_object_device *tdev;
584 
585 	/*
586 	 * Fencing and IRQs.
587 	 */
588 
589 	atomic_t marker_seq;
590 	wait_queue_head_t fence_queue;
591 	wait_queue_head_t fifo_queue;
592 	spinlock_t waiter_lock;
593 	int fence_queue_waiters; /* Protected by waiter_lock */
594 	int goal_queue_waiters; /* Protected by waiter_lock */
595 	int cmdbuf_waiters; /* Protected by waiter_lock */
596 	int error_waiters; /* Protected by waiter_lock */
597 	int fifo_queue_waiters; /* Protected by waiter_lock */
598 	uint32_t last_read_seqno;
599 	struct vmw_fence_manager *fman;
600 	uint32_t irq_mask; /* Updates protected by waiter_lock */
601 
602 	/*
603 	 * Device state
604 	 */
605 
606 	uint32_t traces_state;
607 	uint32_t enable_state;
608 	uint32_t config_done_state;
609 
610 	/**
611 	 * Execbuf
612 	 */
613 	/**
614 	 * Protected by the cmdbuf mutex.
615 	 */
616 
617 	struct vmw_sw_context ctx;
618 	struct mutex cmdbuf_mutex;
619 	struct mutex binding_mutex;
620 
621 	/**
622 	 * PM management.
623 	 */
624 	struct notifier_block pm_nb;
625 	bool refuse_hibernation;
626 	bool suspend_locked;
627 
628 	atomic_t num_fifo_resources;
629 
630 	/*
631 	 * Query processing. These members
632 	 * are protected by the cmdbuf mutex.
633 	 */
634 
635 	struct vmw_buffer_object *dummy_query_bo;
636 	struct vmw_buffer_object *pinned_bo;
637 	uint32_t query_cid;
638 	uint32_t query_cid_valid;
639 	bool dummy_query_bo_pinned;
640 
641 	/*
642 	 * Surface swapping. The "surface_lru" list is protected by the
643 	 * resource lock in order to be able to destroy a surface and take
644 	 * it off the lru atomically. "used_memory_size" is currently
645 	 * protected by the cmdbuf mutex for simplicity.
646 	 */
647 
648 	struct list_head res_lru[vmw_res_max];
649 	uint32_t used_memory_size;
650 
651 	/*
652 	 * DMA mapping stuff.
653 	 */
654 	enum vmw_dma_map_mode map_mode;
655 
656 	/*
657 	 * Guest Backed stuff
658 	 */
659 	struct vmw_otable_batch otable_batch;
660 
661 	struct vmw_fifo_state *fifo;
662 	struct vmw_cmdbuf_man *cman;
663 	DECLARE_BITMAP(irqthread_pending, VMW_IRQTHREAD_MAX);
664 
665 	uint32 *devcaps;
666 
667 	/*
668 	 * mksGuestStat instance-descriptor and pid arrays
669 	 */
670 	struct page *mksstat_user_pages[MKSSTAT_CAPACITY];
671 	atomic_t mksstat_user_pids[MKSSTAT_CAPACITY];
672 
673 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
674 	struct page *mksstat_kern_pages[MKSSTAT_CAPACITY];
675 	u8 mksstat_kern_top_timer[MKSSTAT_CAPACITY];
676 	atomic_t mksstat_kern_pids[MKSSTAT_CAPACITY];
677 #endif
678 };
679 
680 static inline struct vmw_buffer_object *gem_to_vmw_bo(struct drm_gem_object *gobj)
681 {
682 	return container_of((gobj), struct vmw_buffer_object, base.base);
683 }
684 
685 static inline struct vmw_surface *vmw_res_to_srf(struct vmw_resource *res)
686 {
687 	return container_of(res, struct vmw_surface, res);
688 }
689 
690 static inline struct vmw_private *vmw_priv(struct drm_device *dev)
691 {
692 	return (struct vmw_private *)dev->dev_private;
693 }
694 
695 static inline struct vmw_fpriv *vmw_fpriv(struct drm_file *file_priv)
696 {
697 	return (struct vmw_fpriv *)file_priv->driver_priv;
698 }
699 
700 /*
701  * SVGA v3 has mmio register access and lacks fifo cmds
702  */
703 static inline bool vmw_is_svga_v3(const struct vmw_private *dev)
704 {
705 	return dev->pci_id == VMWGFX_PCI_ID_SVGA3;
706 }
707 
708 /*
709  * The locking here is fine-grained, so that it is performed once
710  * for every read- and write operation. This is of course costly, but we
711  * don't perform much register access in the timing critical paths anyway.
712  * Instead we have the extra benefit of being sure that we don't forget
713  * the hw lock around register accesses.
714  */
715 static inline void vmw_write(struct vmw_private *dev_priv,
716 			     unsigned int offset, uint32_t value)
717 {
718 	if (vmw_is_svga_v3(dev_priv)) {
719 		iowrite32(value, dev_priv->rmmio + offset);
720 	} else {
721 		spin_lock(&dev_priv->hw_lock);
722 		outl(offset, dev_priv->io_start + SVGA_INDEX_PORT);
723 		outl(value, dev_priv->io_start + SVGA_VALUE_PORT);
724 		spin_unlock(&dev_priv->hw_lock);
725 	}
726 }
727 
728 static inline uint32_t vmw_read(struct vmw_private *dev_priv,
729 				unsigned int offset)
730 {
731 	u32 val;
732 
733 	if (vmw_is_svga_v3(dev_priv)) {
734 		val = ioread32(dev_priv->rmmio + offset);
735 	} else {
736 		spin_lock(&dev_priv->hw_lock);
737 		outl(offset, dev_priv->io_start + SVGA_INDEX_PORT);
738 		val = inl(dev_priv->io_start + SVGA_VALUE_PORT);
739 		spin_unlock(&dev_priv->hw_lock);
740 	}
741 
742 	return val;
743 }
744 
745 /**
746  * has_sm4_context - Does the device support SM4 context.
747  * @dev_priv: Device private.
748  *
749  * Return: Bool value if device support SM4 context or not.
750  */
751 static inline bool has_sm4_context(const struct vmw_private *dev_priv)
752 {
753 	return (dev_priv->sm_type >= VMW_SM_4);
754 }
755 
756 /**
757  * has_sm4_1_context - Does the device support SM4_1 context.
758  * @dev_priv: Device private.
759  *
760  * Return: Bool value if device support SM4_1 context or not.
761  */
762 static inline bool has_sm4_1_context(const struct vmw_private *dev_priv)
763 {
764 	return (dev_priv->sm_type >= VMW_SM_4_1);
765 }
766 
767 /**
768  * has_sm5_context - Does the device support SM5 context.
769  * @dev_priv: Device private.
770  *
771  * Return: Bool value if device support SM5 context or not.
772  */
773 static inline bool has_sm5_context(const struct vmw_private *dev_priv)
774 {
775 	return (dev_priv->sm_type >= VMW_SM_5);
776 }
777 
778 /**
779  * has_gl43_context - Does the device support GL43 context.
780  * @dev_priv: Device private.
781  *
782  * Return: Bool value if device support SM5 context or not.
783  */
784 static inline bool has_gl43_context(const struct vmw_private *dev_priv)
785 {
786 	return (dev_priv->sm_type >= VMW_SM_5_1X);
787 }
788 
789 
790 static inline u32 vmw_max_num_uavs(struct vmw_private *dev_priv)
791 {
792 	return (has_gl43_context(dev_priv) ?
793 			SVGA3D_DX11_1_MAX_UAVIEWS : SVGA3D_MAX_UAVIEWS);
794 }
795 
796 extern void vmw_svga_enable(struct vmw_private *dev_priv);
797 extern void vmw_svga_disable(struct vmw_private *dev_priv);
798 
799 
800 /**
801  * GMR utilities - vmwgfx_gmr.c
802  */
803 
804 extern int vmw_gmr_bind(struct vmw_private *dev_priv,
805 			const struct vmw_sg_table *vsgt,
806 			unsigned long num_pages,
807 			int gmr_id);
808 extern void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id);
809 
810 /**
811  * Resource utilities - vmwgfx_resource.c
812  */
813 struct vmw_user_resource_conv;
814 
815 extern void vmw_resource_unreference(struct vmw_resource **p_res);
816 extern struct vmw_resource *vmw_resource_reference(struct vmw_resource *res);
817 extern struct vmw_resource *
818 vmw_resource_reference_unless_doomed(struct vmw_resource *res);
819 extern int vmw_resource_validate(struct vmw_resource *res, bool intr,
820 				 bool dirtying);
821 extern int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
822 				bool no_backup);
823 extern bool vmw_resource_needs_backup(const struct vmw_resource *res);
824 extern int vmw_user_lookup_handle(struct vmw_private *dev_priv,
825 				  struct drm_file *filp,
826 				  uint32_t handle,
827 				  struct vmw_surface **out_surf,
828 				  struct vmw_buffer_object **out_buf);
829 extern int vmw_user_resource_lookup_handle(
830 	struct vmw_private *dev_priv,
831 	struct ttm_object_file *tfile,
832 	uint32_t handle,
833 	const struct vmw_user_resource_conv *converter,
834 	struct vmw_resource **p_res);
835 extern struct vmw_resource *
836 vmw_user_resource_noref_lookup_handle(struct vmw_private *dev_priv,
837 				      struct ttm_object_file *tfile,
838 				      uint32_t handle,
839 				      const struct vmw_user_resource_conv *
840 				      converter);
841 extern int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
842 				  struct drm_file *file_priv);
843 extern int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
844 				  struct drm_file *file_priv);
845 extern int vmw_user_stream_lookup(struct vmw_private *dev_priv,
846 				  struct ttm_object_file *tfile,
847 				  uint32_t *inout_id,
848 				  struct vmw_resource **out);
849 extern void vmw_resource_unreserve(struct vmw_resource *res,
850 				   bool dirty_set,
851 				   bool dirty,
852 				   bool switch_backup,
853 				   struct vmw_buffer_object *new_backup,
854 				   unsigned long new_backup_offset);
855 extern void vmw_query_move_notify(struct ttm_buffer_object *bo,
856 				  struct ttm_resource *old_mem,
857 				  struct ttm_resource *new_mem);
858 extern int vmw_query_readback_all(struct vmw_buffer_object *dx_query_mob);
859 extern void vmw_resource_evict_all(struct vmw_private *dev_priv);
860 extern void vmw_resource_unbind_list(struct vmw_buffer_object *vbo);
861 void vmw_resource_mob_attach(struct vmw_resource *res);
862 void vmw_resource_mob_detach(struct vmw_resource *res);
863 void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,
864 			       pgoff_t end);
865 int vmw_resources_clean(struct vmw_buffer_object *vbo, pgoff_t start,
866 			pgoff_t end, pgoff_t *num_prefault);
867 
868 /**
869  * vmw_resource_mob_attached - Whether a resource currently has a mob attached
870  * @res: The resource
871  *
872  * Return: true if the resource has a mob attached, false otherwise.
873  */
874 static inline bool vmw_resource_mob_attached(const struct vmw_resource *res)
875 {
876 	return !RB_EMPTY_NODE(&res->mob_node);
877 }
878 
879 /**
880  * vmw_user_resource_noref_release - release a user resource pointer looked up
881  * without reference
882  */
883 static inline void vmw_user_resource_noref_release(void)
884 {
885 	ttm_base_object_noref_release();
886 }
887 
888 /**
889  * Buffer object helper functions - vmwgfx_bo.c
890  */
891 extern int vmw_bo_pin_in_placement(struct vmw_private *vmw_priv,
892 				   struct vmw_buffer_object *bo,
893 				   struct ttm_placement *placement,
894 				   bool interruptible);
895 extern int vmw_bo_pin_in_vram(struct vmw_private *dev_priv,
896 			      struct vmw_buffer_object *buf,
897 			      bool interruptible);
898 extern int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
899 				     struct vmw_buffer_object *buf,
900 				     bool interruptible);
901 extern int vmw_bo_pin_in_start_of_vram(struct vmw_private *vmw_priv,
902 				       struct vmw_buffer_object *bo,
903 				       bool interruptible);
904 extern int vmw_bo_unpin(struct vmw_private *vmw_priv,
905 			struct vmw_buffer_object *bo,
906 			bool interruptible);
907 extern void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *buf,
908 				 SVGAGuestPtr *ptr);
909 extern void vmw_bo_pin_reserved(struct vmw_buffer_object *bo, bool pin);
910 extern void vmw_bo_bo_free(struct ttm_buffer_object *bo);
911 extern int vmw_bo_create_kernel(struct vmw_private *dev_priv,
912 				unsigned long size,
913 				struct ttm_placement *placement,
914 				struct ttm_buffer_object **p_bo);
915 extern int vmw_bo_create(struct vmw_private *dev_priv,
916 			 size_t size, struct ttm_placement *placement,
917 			 bool interruptible, bool pin,
918 			 void (*bo_free)(struct ttm_buffer_object *bo),
919 			 struct vmw_buffer_object **p_bo);
920 extern int vmw_bo_init(struct vmw_private *dev_priv,
921 		       struct vmw_buffer_object *vmw_bo,
922 		       size_t size, struct ttm_placement *placement,
923 		       bool interruptible, bool pin,
924 		       void (*bo_free)(struct ttm_buffer_object *bo));
925 extern int vmw_bo_unref_ioctl(struct drm_device *dev, void *data,
926 			      struct drm_file *file_priv);
927 extern int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data,
928 				     struct drm_file *file_priv);
929 extern int vmw_user_bo_lookup(struct drm_file *filp,
930 			      uint32_t handle,
931 			      struct vmw_buffer_object **out);
932 extern void vmw_bo_fence_single(struct ttm_buffer_object *bo,
933 				struct vmw_fence_obj *fence);
934 extern void *vmw_bo_map_and_cache(struct vmw_buffer_object *vbo);
935 extern void vmw_bo_unmap(struct vmw_buffer_object *vbo);
936 extern void vmw_bo_move_notify(struct ttm_buffer_object *bo,
937 			       struct ttm_resource *mem);
938 extern void vmw_bo_swap_notify(struct ttm_buffer_object *bo);
939 extern struct vmw_buffer_object *
940 vmw_user_bo_noref_lookup(struct drm_file *filp, u32 handle);
941 
942 /**
943  * vmw_bo_adjust_prio - Adjust the buffer object eviction priority
944  * according to attached resources
945  * @vbo: The struct vmw_buffer_object
946  */
947 static inline void vmw_bo_prio_adjust(struct vmw_buffer_object *vbo)
948 {
949 	int i = ARRAY_SIZE(vbo->res_prios);
950 
951 	while (i--) {
952 		if (vbo->res_prios[i]) {
953 			vbo->base.priority = i;
954 			return;
955 		}
956 	}
957 
958 	vbo->base.priority = 3;
959 }
960 
961 /**
962  * vmw_bo_prio_add - Notify a buffer object of a newly attached resource
963  * eviction priority
964  * @vbo: The struct vmw_buffer_object
965  * @prio: The resource priority
966  *
967  * After being notified, the code assigns the highest resource eviction priority
968  * to the backing buffer object (mob).
969  */
970 static inline void vmw_bo_prio_add(struct vmw_buffer_object *vbo, int prio)
971 {
972 	if (vbo->res_prios[prio]++ == 0)
973 		vmw_bo_prio_adjust(vbo);
974 }
975 
976 /**
977  * vmw_bo_prio_del - Notify a buffer object of a resource with a certain
978  * priority being removed
979  * @vbo: The struct vmw_buffer_object
980  * @prio: The resource priority
981  *
982  * After being notified, the code assigns the highest resource eviction priority
983  * to the backing buffer object (mob).
984  */
985 static inline void vmw_bo_prio_del(struct vmw_buffer_object *vbo, int prio)
986 {
987 	if (--vbo->res_prios[prio] == 0)
988 		vmw_bo_prio_adjust(vbo);
989 }
990 
991 /**
992  * GEM related functionality - vmwgfx_gem.c
993  */
994 extern int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
995 					     struct drm_file *filp,
996 					     uint32_t size,
997 					     uint32_t *handle,
998 					     struct vmw_buffer_object **p_vbo);
999 extern int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
1000 				       struct drm_file *filp);
1001 extern void vmw_gem_destroy(struct ttm_buffer_object *bo);
1002 extern void vmw_debugfs_gem_init(struct vmw_private *vdev);
1003 
1004 /**
1005  * Misc Ioctl functionality - vmwgfx_ioctl.c
1006  */
1007 
1008 extern int vmw_getparam_ioctl(struct drm_device *dev, void *data,
1009 			      struct drm_file *file_priv);
1010 extern int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
1011 				struct drm_file *file_priv);
1012 extern int vmw_present_ioctl(struct drm_device *dev, void *data,
1013 			     struct drm_file *file_priv);
1014 extern int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
1015 				      struct drm_file *file_priv);
1016 
1017 /**
1018  * Fifo utilities - vmwgfx_fifo.c
1019  */
1020 
1021 extern struct vmw_fifo_state *vmw_fifo_create(struct vmw_private *dev_priv);
1022 extern void vmw_fifo_destroy(struct vmw_private *dev_priv);
1023 extern bool vmw_cmd_supported(struct vmw_private *vmw);
1024 extern void *
1025 vmw_cmd_ctx_reserve(struct vmw_private *dev_priv, uint32_t bytes, int ctx_id);
1026 extern void vmw_cmd_commit(struct vmw_private *dev_priv, uint32_t bytes);
1027 extern void vmw_cmd_commit_flush(struct vmw_private *dev_priv, uint32_t bytes);
1028 extern int vmw_cmd_send_fence(struct vmw_private *dev_priv, uint32_t *seqno);
1029 extern bool vmw_supports_3d(struct vmw_private *dev_priv);
1030 extern void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason);
1031 extern bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv);
1032 extern int vmw_cmd_emit_dummy_query(struct vmw_private *dev_priv,
1033 				    uint32_t cid);
1034 extern int vmw_cmd_flush(struct vmw_private *dev_priv,
1035 			 bool interruptible);
1036 
1037 #define VMW_CMD_CTX_RESERVE(__priv, __bytes, __ctx_id)                        \
1038 ({                                                                            \
1039 	vmw_cmd_ctx_reserve(__priv, __bytes, __ctx_id) ? : ({                 \
1040 		DRM_ERROR("FIFO reserve failed at %s for %u bytes\n",         \
1041 			  __func__, (unsigned int) __bytes);                  \
1042 		NULL;                                                         \
1043 	});                                                                   \
1044 })
1045 
1046 #define VMW_CMD_RESERVE(__priv, __bytes)                                     \
1047 	VMW_CMD_CTX_RESERVE(__priv, __bytes, SVGA3D_INVALID_ID)
1048 
1049 
1050 /**
1051  * vmw_fifo_caps - Returns the capabilities of the FIFO command
1052  * queue or 0 if fifo memory isn't present.
1053  * @dev_priv: The device private context
1054  */
1055 static inline uint32_t vmw_fifo_caps(const struct vmw_private *dev_priv)
1056 {
1057 	if (!dev_priv->fifo_mem || !dev_priv->fifo)
1058 		return 0;
1059 	return dev_priv->fifo->capabilities;
1060 }
1061 
1062 
1063 /**
1064  * vmw_is_cursor_bypass3_enabled - Returns TRUE iff Cursor Bypass 3
1065  * is enabled in the FIFO.
1066  * @dev_priv: The device private context
1067  */
1068 static inline bool
1069 vmw_is_cursor_bypass3_enabled(const struct vmw_private *dev_priv)
1070 {
1071 	return (vmw_fifo_caps(dev_priv) & SVGA_FIFO_CAP_CURSOR_BYPASS_3) != 0;
1072 }
1073 
1074 /**
1075  * TTM glue - vmwgfx_ttm_glue.c
1076  */
1077 
1078 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
1079 
1080 /**
1081  * TTM buffer object driver - vmwgfx_ttm_buffer.c
1082  */
1083 
1084 extern const size_t vmw_tt_size;
1085 extern struct ttm_placement vmw_vram_placement;
1086 extern struct ttm_placement vmw_vram_sys_placement;
1087 extern struct ttm_placement vmw_vram_gmr_placement;
1088 extern struct ttm_placement vmw_sys_placement;
1089 extern struct ttm_placement vmw_srf_placement;
1090 extern struct ttm_placement vmw_mob_placement;
1091 extern struct ttm_placement vmw_nonfixed_placement;
1092 extern struct ttm_device_funcs vmw_bo_driver;
1093 extern const struct vmw_sg_table *
1094 vmw_bo_sg_table(struct ttm_buffer_object *bo);
1095 extern int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
1096 				      unsigned long bo_size,
1097 				      struct ttm_buffer_object **bo_p);
1098 
1099 extern void vmw_piter_start(struct vmw_piter *viter,
1100 			    const struct vmw_sg_table *vsgt,
1101 			    unsigned long p_offs);
1102 
1103 /**
1104  * vmw_piter_next - Advance the iterator one page.
1105  *
1106  * @viter: Pointer to the iterator to advance.
1107  *
1108  * Returns false if past the list of pages, true otherwise.
1109  */
1110 static inline bool vmw_piter_next(struct vmw_piter *viter)
1111 {
1112 	return viter->next(viter);
1113 }
1114 
1115 /**
1116  * vmw_piter_dma_addr - Return the DMA address of the current page.
1117  *
1118  * @viter: Pointer to the iterator
1119  *
1120  * Returns the DMA address of the page pointed to by @viter.
1121  */
1122 static inline dma_addr_t vmw_piter_dma_addr(struct vmw_piter *viter)
1123 {
1124 	return viter->dma_address(viter);
1125 }
1126 
1127 /**
1128  * vmw_piter_page - Return a pointer to the current page.
1129  *
1130  * @viter: Pointer to the iterator
1131  *
1132  * Returns the DMA address of the page pointed to by @viter.
1133  */
1134 static inline struct page *vmw_piter_page(struct vmw_piter *viter)
1135 {
1136 	return viter->pages[viter->i];
1137 }
1138 
1139 /**
1140  * Command submission - vmwgfx_execbuf.c
1141  */
1142 
1143 extern int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
1144 			     struct drm_file *file_priv);
1145 extern int vmw_execbuf_process(struct drm_file *file_priv,
1146 			       struct vmw_private *dev_priv,
1147 			       void __user *user_commands,
1148 			       void *kernel_commands,
1149 			       uint32_t command_size,
1150 			       uint64_t throttle_us,
1151 			       uint32_t dx_context_handle,
1152 			       struct drm_vmw_fence_rep __user
1153 			       *user_fence_rep,
1154 			       struct vmw_fence_obj **out_fence,
1155 			       uint32_t flags);
1156 extern void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
1157 					    struct vmw_fence_obj *fence);
1158 extern void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv);
1159 
1160 extern int vmw_execbuf_fence_commands(struct drm_file *file_priv,
1161 				      struct vmw_private *dev_priv,
1162 				      struct vmw_fence_obj **p_fence,
1163 				      uint32_t *p_handle);
1164 extern int vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
1165 					struct vmw_fpriv *vmw_fp,
1166 					int ret,
1167 					struct drm_vmw_fence_rep __user
1168 					*user_fence_rep,
1169 					struct vmw_fence_obj *fence,
1170 					uint32_t fence_handle,
1171 					int32_t out_fence_fd);
1172 bool vmw_cmd_describe(const void *buf, u32 *size, char const **cmd);
1173 
1174 /**
1175  * IRQs and wating - vmwgfx_irq.c
1176  */
1177 
1178 extern int vmw_irq_install(struct vmw_private *dev_priv);
1179 extern void vmw_irq_uninstall(struct drm_device *dev);
1180 extern bool vmw_seqno_passed(struct vmw_private *dev_priv,
1181 				uint32_t seqno);
1182 extern int vmw_fallback_wait(struct vmw_private *dev_priv,
1183 			     bool lazy,
1184 			     bool fifo_idle,
1185 			     uint32_t seqno,
1186 			     bool interruptible,
1187 			     unsigned long timeout);
1188 extern void vmw_update_seqno(struct vmw_private *dev_priv);
1189 extern void vmw_seqno_waiter_add(struct vmw_private *dev_priv);
1190 extern void vmw_seqno_waiter_remove(struct vmw_private *dev_priv);
1191 extern void vmw_goal_waiter_add(struct vmw_private *dev_priv);
1192 extern void vmw_goal_waiter_remove(struct vmw_private *dev_priv);
1193 extern void vmw_generic_waiter_add(struct vmw_private *dev_priv, u32 flag,
1194 				   int *waiter_count);
1195 extern void vmw_generic_waiter_remove(struct vmw_private *dev_priv,
1196 				      u32 flag, int *waiter_count);
1197 
1198 /**
1199  * Kernel modesetting - vmwgfx_kms.c
1200  */
1201 
1202 int vmw_kms_init(struct vmw_private *dev_priv);
1203 int vmw_kms_close(struct vmw_private *dev_priv);
1204 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1205 				struct drm_file *file_priv);
1206 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv);
1207 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
1208 			  struct ttm_object_file *tfile,
1209 			  struct ttm_buffer_object *bo,
1210 			  SVGA3dCmdHeader *header);
1211 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1212 		       unsigned width, unsigned height, unsigned pitch,
1213 		       unsigned bpp, unsigned depth);
1214 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1215 				uint32_t pitch,
1216 				uint32_t height);
1217 int vmw_kms_present(struct vmw_private *dev_priv,
1218 		    struct drm_file *file_priv,
1219 		    struct vmw_framebuffer *vfb,
1220 		    struct vmw_surface *surface,
1221 		    uint32_t sid, int32_t destX, int32_t destY,
1222 		    struct drm_vmw_rect *clips,
1223 		    uint32_t num_clips);
1224 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1225 				struct drm_file *file_priv);
1226 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv);
1227 int vmw_kms_suspend(struct drm_device *dev);
1228 int vmw_kms_resume(struct drm_device *dev);
1229 void vmw_kms_lost_device(struct drm_device *dev);
1230 
1231 int vmw_dumb_create(struct drm_file *file_priv,
1232 		    struct drm_device *dev,
1233 		    struct drm_mode_create_dumb *args);
1234 extern int vmw_resource_pin(struct vmw_resource *res, bool interruptible);
1235 extern void vmw_resource_unpin(struct vmw_resource *res);
1236 extern enum vmw_res_type vmw_res_type(const struct vmw_resource *res);
1237 
1238 /**
1239  * Overlay control - vmwgfx_overlay.c
1240  */
1241 
1242 int vmw_overlay_init(struct vmw_private *dev_priv);
1243 int vmw_overlay_close(struct vmw_private *dev_priv);
1244 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
1245 		      struct drm_file *file_priv);
1246 int vmw_overlay_resume_all(struct vmw_private *dev_priv);
1247 int vmw_overlay_pause_all(struct vmw_private *dev_priv);
1248 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out);
1249 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id);
1250 int vmw_overlay_num_overlays(struct vmw_private *dev_priv);
1251 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv);
1252 
1253 /**
1254  * GMR Id manager
1255  */
1256 
1257 int vmw_gmrid_man_init(struct vmw_private *dev_priv, int type);
1258 void vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type);
1259 
1260 /**
1261  * System memory manager
1262  */
1263 int vmw_sys_man_init(struct vmw_private *dev_priv);
1264 void vmw_sys_man_fini(struct vmw_private *dev_priv);
1265 
1266 /**
1267  * Prime - vmwgfx_prime.c
1268  */
1269 
1270 extern const struct dma_buf_ops vmw_prime_dmabuf_ops;
1271 extern int vmw_prime_fd_to_handle(struct drm_device *dev,
1272 				  struct drm_file *file_priv,
1273 				  int fd, u32 *handle);
1274 extern int vmw_prime_handle_to_fd(struct drm_device *dev,
1275 				  struct drm_file *file_priv,
1276 				  uint32_t handle, uint32_t flags,
1277 				  int *prime_fd);
1278 
1279 /*
1280  * MemoryOBject management -  vmwgfx_mob.c
1281  */
1282 struct vmw_mob;
1283 extern int vmw_mob_bind(struct vmw_private *dev_priv, struct vmw_mob *mob,
1284 			const struct vmw_sg_table *vsgt,
1285 			unsigned long num_data_pages, int32_t mob_id);
1286 extern void vmw_mob_unbind(struct vmw_private *dev_priv,
1287 			   struct vmw_mob *mob);
1288 extern void vmw_mob_destroy(struct vmw_mob *mob);
1289 extern struct vmw_mob *vmw_mob_create(unsigned long data_pages);
1290 extern int vmw_otables_setup(struct vmw_private *dev_priv);
1291 extern void vmw_otables_takedown(struct vmw_private *dev_priv);
1292 
1293 /*
1294  * Context management - vmwgfx_context.c
1295  */
1296 
1297 extern const struct vmw_user_resource_conv *user_context_converter;
1298 
1299 extern int vmw_context_define_ioctl(struct drm_device *dev, void *data,
1300 				    struct drm_file *file_priv);
1301 extern int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data,
1302 					     struct drm_file *file_priv);
1303 extern int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
1304 				     struct drm_file *file_priv);
1305 extern struct list_head *vmw_context_binding_list(struct vmw_resource *ctx);
1306 extern struct vmw_cmdbuf_res_manager *
1307 vmw_context_res_man(struct vmw_resource *ctx);
1308 extern struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx,
1309 						SVGACOTableType cotable_type);
1310 struct vmw_ctx_binding_state;
1311 extern struct vmw_ctx_binding_state *
1312 vmw_context_binding_state(struct vmw_resource *ctx);
1313 extern void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx,
1314 					  bool readback);
1315 extern int vmw_context_bind_dx_query(struct vmw_resource *ctx_res,
1316 				     struct vmw_buffer_object *mob);
1317 extern struct vmw_buffer_object *
1318 vmw_context_get_dx_query_mob(struct vmw_resource *ctx_res);
1319 
1320 
1321 /*
1322  * Surface management - vmwgfx_surface.c
1323  */
1324 
1325 extern const struct vmw_user_resource_conv *user_surface_converter;
1326 
1327 extern int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
1328 				     struct drm_file *file_priv);
1329 extern int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
1330 				    struct drm_file *file_priv);
1331 extern int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
1332 				       struct drm_file *file_priv);
1333 extern int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
1334 				       struct drm_file *file_priv);
1335 extern int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
1336 					  struct drm_file *file_priv);
1337 extern int vmw_gb_surface_define_ext_ioctl(struct drm_device *dev,
1338 					   void *data,
1339 					   struct drm_file *file_priv);
1340 extern int vmw_gb_surface_reference_ext_ioctl(struct drm_device *dev,
1341 					      void *data,
1342 					      struct drm_file *file_priv);
1343 
1344 int vmw_gb_surface_define(struct vmw_private *dev_priv,
1345 			  const struct vmw_surface_metadata *req,
1346 			  struct vmw_surface **srf_out);
1347 
1348 /*
1349  * Shader management - vmwgfx_shader.c
1350  */
1351 
1352 extern const struct vmw_user_resource_conv *user_shader_converter;
1353 
1354 extern int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
1355 				   struct drm_file *file_priv);
1356 extern int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
1357 				    struct drm_file *file_priv);
1358 extern int vmw_compat_shader_add(struct vmw_private *dev_priv,
1359 				 struct vmw_cmdbuf_res_manager *man,
1360 				 u32 user_key, const void *bytecode,
1361 				 SVGA3dShaderType shader_type,
1362 				 size_t size,
1363 				 struct list_head *list);
1364 extern int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
1365 			     u32 user_key, SVGA3dShaderType shader_type,
1366 			     struct list_head *list);
1367 extern int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
1368 			     struct vmw_resource *ctx,
1369 			     u32 user_key,
1370 			     SVGA3dShaderType shader_type,
1371 			     struct list_head *list);
1372 extern void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
1373 					     struct list_head *list,
1374 					     bool readback);
1375 
1376 extern struct vmw_resource *
1377 vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
1378 		  u32 user_key, SVGA3dShaderType shader_type);
1379 
1380 /*
1381  * Streamoutput management
1382  */
1383 struct vmw_resource *
1384 vmw_dx_streamoutput_lookup(struct vmw_cmdbuf_res_manager *man,
1385 			   u32 user_key);
1386 int vmw_dx_streamoutput_add(struct vmw_cmdbuf_res_manager *man,
1387 			    struct vmw_resource *ctx,
1388 			    SVGA3dStreamOutputId user_key,
1389 			    struct list_head *list);
1390 void vmw_dx_streamoutput_set_size(struct vmw_resource *res, u32 size);
1391 int vmw_dx_streamoutput_remove(struct vmw_cmdbuf_res_manager *man,
1392 			       SVGA3dStreamOutputId user_key,
1393 			       struct list_head *list);
1394 void vmw_dx_streamoutput_cotable_list_scrub(struct vmw_private *dev_priv,
1395 					    struct list_head *list,
1396 					    bool readback);
1397 
1398 /*
1399  * Command buffer managed resources - vmwgfx_cmdbuf_res.c
1400  */
1401 
1402 extern struct vmw_cmdbuf_res_manager *
1403 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv);
1404 extern void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man);
1405 extern struct vmw_resource *
1406 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
1407 		      enum vmw_cmdbuf_res_type res_type,
1408 		      u32 user_key);
1409 extern void vmw_cmdbuf_res_revert(struct list_head *list);
1410 extern void vmw_cmdbuf_res_commit(struct list_head *list);
1411 extern int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
1412 			      enum vmw_cmdbuf_res_type res_type,
1413 			      u32 user_key,
1414 			      struct vmw_resource *res,
1415 			      struct list_head *list);
1416 extern int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
1417 				 enum vmw_cmdbuf_res_type res_type,
1418 				 u32 user_key,
1419 				 struct list_head *list,
1420 				 struct vmw_resource **res);
1421 
1422 /*
1423  * COTable management - vmwgfx_cotable.c
1424  */
1425 extern const SVGACOTableType vmw_cotable_scrub_order[];
1426 extern struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
1427 					      struct vmw_resource *ctx,
1428 					      u32 type);
1429 extern int vmw_cotable_notify(struct vmw_resource *res, int id);
1430 extern int vmw_cotable_scrub(struct vmw_resource *res, bool readback);
1431 extern void vmw_cotable_add_resource(struct vmw_resource *ctx,
1432 				     struct list_head *head);
1433 
1434 /*
1435  * Command buffer managerment vmwgfx_cmdbuf.c
1436  */
1437 struct vmw_cmdbuf_man;
1438 struct vmw_cmdbuf_header;
1439 
1440 extern struct vmw_cmdbuf_man *
1441 vmw_cmdbuf_man_create(struct vmw_private *dev_priv);
1442 extern int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man, size_t size);
1443 extern void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man);
1444 extern void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man);
1445 extern int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
1446 			   unsigned long timeout);
1447 extern void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
1448 				int ctx_id, bool interruptible,
1449 				struct vmw_cmdbuf_header *header);
1450 extern void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
1451 			      struct vmw_cmdbuf_header *header,
1452 			      bool flush);
1453 extern void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
1454 			      size_t size, bool interruptible,
1455 			      struct vmw_cmdbuf_header **p_header);
1456 extern void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header);
1457 extern int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
1458 				bool interruptible);
1459 extern void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man);
1460 
1461 /* CPU blit utilities - vmwgfx_blit.c */
1462 
1463 /**
1464  * struct vmw_diff_cpy - CPU blit information structure
1465  *
1466  * @rect: The output bounding box rectangle.
1467  * @line: The current line of the blit.
1468  * @line_offset: Offset of the current line segment.
1469  * @cpp: Bytes per pixel (granularity information).
1470  * @memcpy: Which memcpy function to use.
1471  */
1472 struct vmw_diff_cpy {
1473 	struct drm_rect rect;
1474 	size_t line;
1475 	size_t line_offset;
1476 	int cpp;
1477 	void (*do_cpy)(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
1478 		       size_t n);
1479 };
1480 
1481 #define VMW_CPU_BLIT_INITIALIZER {	\
1482 	.do_cpy = vmw_memcpy,		\
1483 }
1484 
1485 #define VMW_CPU_BLIT_DIFF_INITIALIZER(_cpp) {	  \
1486 	.line = 0,				  \
1487 	.line_offset = 0,			  \
1488 	.rect = { .x1 = INT_MAX/2,		  \
1489 		  .y1 = INT_MAX/2,		  \
1490 		  .x2 = INT_MIN/2,		  \
1491 		  .y2 = INT_MIN/2		  \
1492 	},					  \
1493 	.cpp = _cpp,				  \
1494 	.do_cpy = vmw_diff_memcpy,		  \
1495 }
1496 
1497 void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
1498 		     size_t n);
1499 
1500 void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n);
1501 
1502 int vmw_bo_cpu_blit(struct ttm_buffer_object *dst,
1503 		    u32 dst_offset, u32 dst_stride,
1504 		    struct ttm_buffer_object *src,
1505 		    u32 src_offset, u32 src_stride,
1506 		    u32 w, u32 h,
1507 		    struct vmw_diff_cpy *diff);
1508 
1509 /* Host messaging -vmwgfx_msg.c: */
1510 int vmw_host_get_guestinfo(const char *guest_info_param,
1511 			   char *buffer, size_t *length);
1512 __printf(1, 2) int vmw_host_printf(const char *fmt, ...);
1513 int vmw_msg_ioctl(struct drm_device *dev, void *data,
1514 		  struct drm_file *file_priv);
1515 
1516 /* Host mksGuestStats -vmwgfx_msg.c: */
1517 int vmw_mksstat_get_kern_slot(pid_t pid, struct vmw_private *dev_priv);
1518 
1519 int vmw_mksstat_reset_ioctl(struct drm_device *dev, void *data,
1520 		      struct drm_file *file_priv);
1521 int vmw_mksstat_add_ioctl(struct drm_device *dev, void *data,
1522 		      struct drm_file *file_priv);
1523 int vmw_mksstat_remove_ioctl(struct drm_device *dev, void *data,
1524 		      struct drm_file *file_priv);
1525 int vmw_mksstat_remove_all(struct vmw_private *dev_priv);
1526 
1527 /* VMW logging */
1528 
1529 /**
1530  * VMW_DEBUG_USER - Debug output for user-space debugging.
1531  *
1532  * @fmt: printf() like format string.
1533  *
1534  * This macro is for logging user-space error and debugging messages for e.g.
1535  * command buffer execution errors due to malformed commands, invalid context,
1536  * etc.
1537  */
1538 #define VMW_DEBUG_USER(fmt, ...)                                              \
1539 	DRM_DEBUG_DRIVER(fmt, ##__VA_ARGS__)
1540 
1541 /* Resource dirtying - vmwgfx_page_dirty.c */
1542 void vmw_bo_dirty_scan(struct vmw_buffer_object *vbo);
1543 int vmw_bo_dirty_add(struct vmw_buffer_object *vbo);
1544 void vmw_bo_dirty_transfer_to_res(struct vmw_resource *res);
1545 void vmw_bo_dirty_clear_res(struct vmw_resource *res);
1546 void vmw_bo_dirty_release(struct vmw_buffer_object *vbo);
1547 void vmw_bo_dirty_unmap(struct vmw_buffer_object *vbo,
1548 			pgoff_t start, pgoff_t end);
1549 vm_fault_t vmw_bo_vm_fault(struct vm_fault *vmf);
1550 vm_fault_t vmw_bo_vm_mkwrite(struct vm_fault *vmf);
1551 
1552 
1553 /**
1554  * VMW_DEBUG_KMS - Debug output for kernel mode-setting
1555  *
1556  * This macro is for debugging vmwgfx mode-setting code.
1557  */
1558 #define VMW_DEBUG_KMS(fmt, ...)                                               \
1559 	DRM_DEBUG_DRIVER(fmt, ##__VA_ARGS__)
1560 
1561 /**
1562  * Inline helper functions
1563  */
1564 
1565 static inline void vmw_surface_unreference(struct vmw_surface **srf)
1566 {
1567 	struct vmw_surface *tmp_srf = *srf;
1568 	struct vmw_resource *res = &tmp_srf->res;
1569 	*srf = NULL;
1570 
1571 	vmw_resource_unreference(&res);
1572 }
1573 
1574 static inline struct vmw_surface *vmw_surface_reference(struct vmw_surface *srf)
1575 {
1576 	(void) vmw_resource_reference(&srf->res);
1577 	return srf;
1578 }
1579 
1580 static inline void vmw_bo_unreference(struct vmw_buffer_object **buf)
1581 {
1582 	struct vmw_buffer_object *tmp_buf = *buf;
1583 
1584 	*buf = NULL;
1585 	if (tmp_buf != NULL)
1586 		ttm_bo_put(&tmp_buf->base);
1587 }
1588 
1589 static inline struct vmw_buffer_object *
1590 vmw_bo_reference(struct vmw_buffer_object *buf)
1591 {
1592 	ttm_bo_get(&buf->base);
1593 	return buf;
1594 }
1595 
1596 static inline void vmw_fifo_resource_inc(struct vmw_private *dev_priv)
1597 {
1598 	atomic_inc(&dev_priv->num_fifo_resources);
1599 }
1600 
1601 static inline void vmw_fifo_resource_dec(struct vmw_private *dev_priv)
1602 {
1603 	atomic_dec(&dev_priv->num_fifo_resources);
1604 }
1605 
1606 /**
1607  * vmw_fifo_mem_read - Perform a MMIO read from the fifo memory
1608  *
1609  * @fifo_reg: The fifo register to read from
1610  *
1611  * This function is intended to be equivalent to ioread32() on
1612  * memremap'd memory, but without byteswapping.
1613  */
1614 static inline u32 vmw_fifo_mem_read(struct vmw_private *vmw, uint32 fifo_reg)
1615 {
1616 	BUG_ON(vmw_is_svga_v3(vmw));
1617 	return READ_ONCE(*(vmw->fifo_mem + fifo_reg));
1618 }
1619 
1620 /**
1621  * vmw_fifo_mem_write - Perform a MMIO write to volatile memory
1622  *
1623  * @addr: The fifo register to write to
1624  *
1625  * This function is intended to be equivalent to iowrite32 on
1626  * memremap'd memory, but without byteswapping.
1627  */
1628 static inline void vmw_fifo_mem_write(struct vmw_private *vmw, u32 fifo_reg,
1629 				      u32 value)
1630 {
1631 	BUG_ON(vmw_is_svga_v3(vmw));
1632 	WRITE_ONCE(*(vmw->fifo_mem + fifo_reg), value);
1633 }
1634 
1635 static inline u32 vmw_fence_read(struct vmw_private *dev_priv)
1636 {
1637 	u32 fence;
1638 	if (vmw_is_svga_v3(dev_priv))
1639 		fence = vmw_read(dev_priv, SVGA_REG_FENCE);
1640 	else
1641 		fence = vmw_fifo_mem_read(dev_priv, SVGA_FIFO_FENCE);
1642 	return fence;
1643 }
1644 
1645 static inline void vmw_fence_write(struct vmw_private *dev_priv,
1646 				  u32 fence)
1647 {
1648 	BUG_ON(vmw_is_svga_v3(dev_priv));
1649 	vmw_fifo_mem_write(dev_priv, SVGA_FIFO_FENCE, fence);
1650 }
1651 
1652 static inline u32 vmw_irq_status_read(struct vmw_private *vmw)
1653 {
1654 	u32 status;
1655 	if (vmw_is_svga_v3(vmw))
1656 		status = vmw_read(vmw, SVGA_REG_IRQ_STATUS);
1657 	else
1658 		status = inl(vmw->io_start + SVGA_IRQSTATUS_PORT);
1659 	return status;
1660 }
1661 
1662 static inline void vmw_irq_status_write(struct vmw_private *vmw,
1663 					uint32 status)
1664 {
1665 	if (vmw_is_svga_v3(vmw))
1666 		vmw_write(vmw, SVGA_REG_IRQ_STATUS, status);
1667 	else
1668 		outl(status, vmw->io_start + SVGA_IRQSTATUS_PORT);
1669 }
1670 
1671 static inline bool vmw_has_fences(struct vmw_private *vmw)
1672 {
1673 	if ((vmw->capabilities & (SVGA_CAP_COMMAND_BUFFERS |
1674 				  SVGA_CAP_CMD_BUFFERS_2)) != 0)
1675 		return true;
1676 	return (vmw_fifo_caps(vmw) & SVGA_FIFO_CAP_FENCE) != 0;
1677 }
1678 
1679 #endif
1680