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