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