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