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