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