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