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