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