Home | History | Annotate | Line # | Download | only in gvt
      1  1.1  riastrad /*	$NetBSD: gvt.h,v 1.2 2021/12/18 23:45:31 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  1.1  riastrad  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  1.1  riastrad  * SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors:
     26  1.1  riastrad  *    Kevin Tian <kevin.tian (at) intel.com>
     27  1.1  riastrad  *    Eddie Dong <eddie.dong (at) intel.com>
     28  1.1  riastrad  *
     29  1.1  riastrad  * Contributors:
     30  1.1  riastrad  *    Niu Bing <bing.niu (at) intel.com>
     31  1.1  riastrad  *    Zhi Wang <zhi.a.wang (at) intel.com>
     32  1.1  riastrad  *
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.1  riastrad #ifndef _GVT_H_
     36  1.1  riastrad #define _GVT_H_
     37  1.1  riastrad 
     38  1.1  riastrad #include "debug.h"
     39  1.1  riastrad #include "hypercall.h"
     40  1.1  riastrad #include "mmio.h"
     41  1.1  riastrad #include "reg.h"
     42  1.1  riastrad #include "interrupt.h"
     43  1.1  riastrad #include "gtt.h"
     44  1.1  riastrad #include "display.h"
     45  1.1  riastrad #include "edid.h"
     46  1.1  riastrad #include "execlist.h"
     47  1.1  riastrad #include "scheduler.h"
     48  1.1  riastrad #include "sched_policy.h"
     49  1.1  riastrad #include "mmio_context.h"
     50  1.1  riastrad #include "cmd_parser.h"
     51  1.1  riastrad #include "fb_decoder.h"
     52  1.1  riastrad #include "dmabuf.h"
     53  1.1  riastrad #include "page_track.h"
     54  1.1  riastrad 
     55  1.1  riastrad #define GVT_MAX_VGPU 8
     56  1.1  riastrad 
     57  1.1  riastrad struct intel_gvt_host {
     58  1.1  riastrad 	struct device *dev;
     59  1.1  riastrad 	bool initialized;
     60  1.1  riastrad 	int hypervisor_type;
     61  1.1  riastrad 	struct intel_gvt_mpt *mpt;
     62  1.1  riastrad };
     63  1.1  riastrad 
     64  1.1  riastrad extern struct intel_gvt_host intel_gvt_host;
     65  1.1  riastrad 
     66  1.1  riastrad /* Describe per-platform limitations. */
     67  1.1  riastrad struct intel_gvt_device_info {
     68  1.1  riastrad 	u32 max_support_vgpus;
     69  1.1  riastrad 	u32 cfg_space_size;
     70  1.1  riastrad 	u32 mmio_size;
     71  1.1  riastrad 	u32 mmio_bar;
     72  1.1  riastrad 	unsigned long msi_cap_offset;
     73  1.1  riastrad 	u32 gtt_start_offset;
     74  1.1  riastrad 	u32 gtt_entry_size;
     75  1.1  riastrad 	u32 gtt_entry_size_shift;
     76  1.1  riastrad 	int gmadr_bytes_in_cmd;
     77  1.1  riastrad 	u32 max_surface_size;
     78  1.1  riastrad };
     79  1.1  riastrad 
     80  1.1  riastrad /* GM resources owned by a vGPU */
     81  1.1  riastrad struct intel_vgpu_gm {
     82  1.1  riastrad 	u64 aperture_sz;
     83  1.1  riastrad 	u64 hidden_sz;
     84  1.1  riastrad 	struct drm_mm_node low_gm_node;
     85  1.1  riastrad 	struct drm_mm_node high_gm_node;
     86  1.1  riastrad };
     87  1.1  riastrad 
     88  1.1  riastrad #define INTEL_GVT_MAX_NUM_FENCES 32
     89  1.1  riastrad 
     90  1.1  riastrad /* Fences owned by a vGPU */
     91  1.1  riastrad struct intel_vgpu_fence {
     92  1.1  riastrad 	struct i915_fence_reg *regs[INTEL_GVT_MAX_NUM_FENCES];
     93  1.1  riastrad 	u32 base;
     94  1.1  riastrad 	u32 size;
     95  1.1  riastrad };
     96  1.1  riastrad 
     97  1.1  riastrad struct intel_vgpu_mmio {
     98  1.1  riastrad 	void *vreg;
     99  1.1  riastrad };
    100  1.1  riastrad 
    101  1.1  riastrad #define INTEL_GVT_MAX_BAR_NUM 4
    102  1.1  riastrad 
    103  1.1  riastrad struct intel_vgpu_pci_bar {
    104  1.1  riastrad 	u64 size;
    105  1.1  riastrad 	bool tracked;
    106  1.1  riastrad };
    107  1.1  riastrad 
    108  1.1  riastrad struct intel_vgpu_cfg_space {
    109  1.1  riastrad 	unsigned char virtual_cfg_space[PCI_CFG_SPACE_EXP_SIZE];
    110  1.1  riastrad 	struct intel_vgpu_pci_bar bar[INTEL_GVT_MAX_BAR_NUM];
    111  1.1  riastrad };
    112  1.1  riastrad 
    113  1.1  riastrad #define vgpu_cfg_space(vgpu) ((vgpu)->cfg_space.virtual_cfg_space)
    114  1.1  riastrad 
    115  1.1  riastrad struct intel_vgpu_irq {
    116  1.1  riastrad 	bool irq_warn_once[INTEL_GVT_EVENT_MAX];
    117  1.1  riastrad 	DECLARE_BITMAP(flip_done_event[I915_MAX_PIPES],
    118  1.1  riastrad 		       INTEL_GVT_EVENT_MAX);
    119  1.1  riastrad };
    120  1.1  riastrad 
    121  1.1  riastrad struct intel_vgpu_opregion {
    122  1.1  riastrad 	bool mapped;
    123  1.1  riastrad 	void *va;
    124  1.1  riastrad 	u32 gfn[INTEL_GVT_OPREGION_PAGES];
    125  1.1  riastrad };
    126  1.1  riastrad 
    127  1.1  riastrad #define vgpu_opregion(vgpu) (&(vgpu->opregion))
    128  1.1  riastrad 
    129  1.1  riastrad struct intel_vgpu_display {
    130  1.1  riastrad 	struct intel_vgpu_i2c_edid i2c_edid;
    131  1.1  riastrad 	struct intel_vgpu_port ports[I915_MAX_PORTS];
    132  1.1  riastrad 	struct intel_vgpu_sbi sbi;
    133  1.1  riastrad };
    134  1.1  riastrad 
    135  1.1  riastrad struct vgpu_sched_ctl {
    136  1.1  riastrad 	int weight;
    137  1.1  riastrad };
    138  1.1  riastrad 
    139  1.1  riastrad enum {
    140  1.1  riastrad 	INTEL_VGPU_EXECLIST_SUBMISSION = 1,
    141  1.1  riastrad 	INTEL_VGPU_GUC_SUBMISSION,
    142  1.1  riastrad };
    143  1.1  riastrad 
    144  1.1  riastrad struct intel_vgpu_submission_ops {
    145  1.1  riastrad 	const char *name;
    146  1.1  riastrad 	int (*init)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
    147  1.1  riastrad 	void (*clean)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
    148  1.1  riastrad 	void (*reset)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
    149  1.1  riastrad };
    150  1.1  riastrad 
    151  1.1  riastrad struct intel_vgpu_submission {
    152  1.1  riastrad 	struct intel_vgpu_execlist execlist[I915_NUM_ENGINES];
    153  1.1  riastrad 	struct list_head workload_q_head[I915_NUM_ENGINES];
    154  1.1  riastrad 	struct intel_context *shadow[I915_NUM_ENGINES];
    155  1.1  riastrad 	struct kmem_cache *workloads;
    156  1.1  riastrad 	atomic_t running_workload_num;
    157  1.1  riastrad 	union {
    158  1.1  riastrad 		u64 i915_context_pml4;
    159  1.1  riastrad 		u64 i915_context_pdps[GEN8_3LVL_PDPES];
    160  1.1  riastrad 	};
    161  1.1  riastrad 	DECLARE_BITMAP(shadow_ctx_desc_updated, I915_NUM_ENGINES);
    162  1.1  riastrad 	DECLARE_BITMAP(tlb_handle_pending, I915_NUM_ENGINES);
    163  1.1  riastrad 	void *ring_scan_buffer[I915_NUM_ENGINES];
    164  1.1  riastrad 	int ring_scan_buffer_size[I915_NUM_ENGINES];
    165  1.1  riastrad 	const struct intel_vgpu_submission_ops *ops;
    166  1.1  riastrad 	int virtual_submission_interface;
    167  1.1  riastrad 	bool active;
    168  1.1  riastrad };
    169  1.1  riastrad 
    170  1.1  riastrad struct intel_vgpu {
    171  1.1  riastrad 	struct intel_gvt *gvt;
    172  1.1  riastrad 	struct mutex vgpu_lock;
    173  1.1  riastrad 	int id;
    174  1.1  riastrad 	unsigned long handle; /* vGPU handle used by hypervisor MPT modules */
    175  1.1  riastrad 	bool active;
    176  1.1  riastrad 	bool pv_notified;
    177  1.1  riastrad 	bool failsafe;
    178  1.1  riastrad 	unsigned int resetting_eng;
    179  1.1  riastrad 
    180  1.1  riastrad 	/* Both sched_data and sched_ctl can be seen a part of the global gvt
    181  1.1  riastrad 	 * scheduler structure. So below 2 vgpu data are protected
    182  1.1  riastrad 	 * by sched_lock, not vgpu_lock.
    183  1.1  riastrad 	 */
    184  1.1  riastrad 	void *sched_data;
    185  1.1  riastrad 	struct vgpu_sched_ctl sched_ctl;
    186  1.1  riastrad 
    187  1.1  riastrad 	struct intel_vgpu_fence fence;
    188  1.1  riastrad 	struct intel_vgpu_gm gm;
    189  1.1  riastrad 	struct intel_vgpu_cfg_space cfg_space;
    190  1.1  riastrad 	struct intel_vgpu_mmio mmio;
    191  1.1  riastrad 	struct intel_vgpu_irq irq;
    192  1.1  riastrad 	struct intel_vgpu_gtt gtt;
    193  1.1  riastrad 	struct intel_vgpu_opregion opregion;
    194  1.1  riastrad 	struct intel_vgpu_display display;
    195  1.1  riastrad 	struct intel_vgpu_submission submission;
    196  1.1  riastrad 	struct radix_tree_root page_track_tree;
    197  1.1  riastrad 	u32 hws_pga[I915_NUM_ENGINES];
    198  1.1  riastrad 
    199  1.1  riastrad 	struct dentry *debugfs;
    200  1.1  riastrad 
    201  1.1  riastrad #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
    202  1.1  riastrad 	struct {
    203  1.1  riastrad 		struct mdev_device *mdev;
    204  1.1  riastrad 		struct vfio_region *region;
    205  1.1  riastrad 		int num_regions;
    206  1.1  riastrad 		struct eventfd_ctx *intx_trigger;
    207  1.1  riastrad 		struct eventfd_ctx *msi_trigger;
    208  1.1  riastrad 
    209  1.1  riastrad 		/*
    210  1.1  riastrad 		 * Two caches are used to avoid mapping duplicated pages (eg.
    211  1.1  riastrad 		 * scratch pages). This help to reduce dma setup overhead.
    212  1.1  riastrad 		 */
    213  1.1  riastrad 		struct rb_root gfn_cache;
    214  1.1  riastrad 		struct rb_root dma_addr_cache;
    215  1.1  riastrad 		unsigned long nr_cache_entries;
    216  1.1  riastrad 		struct mutex cache_lock;
    217  1.1  riastrad 
    218  1.1  riastrad 		struct notifier_block iommu_notifier;
    219  1.1  riastrad 		struct notifier_block group_notifier;
    220  1.1  riastrad 		struct kvm *kvm;
    221  1.1  riastrad 		struct work_struct release_work;
    222  1.1  riastrad 		atomic_t released;
    223  1.1  riastrad 		struct vfio_device *vfio_device;
    224  1.1  riastrad 	} vdev;
    225  1.1  riastrad #endif
    226  1.1  riastrad 
    227  1.1  riastrad 	struct list_head dmabuf_obj_list_head;
    228  1.1  riastrad 	struct mutex dmabuf_lock;
    229  1.1  riastrad 	struct idr object_idr;
    230  1.1  riastrad 
    231  1.1  riastrad 	struct completion vblank_done;
    232  1.1  riastrad 
    233  1.1  riastrad 	u32 scan_nonprivbb;
    234  1.1  riastrad };
    235  1.1  riastrad 
    236  1.1  riastrad /* validating GM healthy status*/
    237  1.1  riastrad #define vgpu_is_vm_unhealthy(ret_val) \
    238  1.1  riastrad 	(((ret_val) == -EBADRQC) || ((ret_val) == -EFAULT))
    239  1.1  riastrad 
    240  1.1  riastrad struct intel_gvt_gm {
    241  1.1  riastrad 	unsigned long vgpu_allocated_low_gm_size;
    242  1.1  riastrad 	unsigned long vgpu_allocated_high_gm_size;
    243  1.1  riastrad };
    244  1.1  riastrad 
    245  1.1  riastrad struct intel_gvt_fence {
    246  1.1  riastrad 	unsigned long vgpu_allocated_fence_num;
    247  1.1  riastrad };
    248  1.1  riastrad 
    249  1.1  riastrad /* Special MMIO blocks. */
    250  1.1  riastrad struct gvt_mmio_block {
    251  1.1  riastrad 	unsigned int device;
    252  1.1  riastrad 	i915_reg_t   offset;
    253  1.1  riastrad 	unsigned int size;
    254  1.1  riastrad 	gvt_mmio_func read;
    255  1.1  riastrad 	gvt_mmio_func write;
    256  1.1  riastrad };
    257  1.1  riastrad 
    258  1.1  riastrad #define INTEL_GVT_MMIO_HASH_BITS 11
    259  1.1  riastrad 
    260  1.1  riastrad struct intel_gvt_mmio {
    261  1.1  riastrad 	u8 *mmio_attribute;
    262  1.1  riastrad /* Register contains RO bits */
    263  1.1  riastrad #define F_RO		(1 << 0)
    264  1.1  riastrad /* Register contains graphics address */
    265  1.1  riastrad #define F_GMADR		(1 << 1)
    266  1.1  riastrad /* Mode mask registers with high 16 bits as the mask bits */
    267  1.1  riastrad #define F_MODE_MASK	(1 << 2)
    268  1.1  riastrad /* This reg can be accessed by GPU commands */
    269  1.1  riastrad #define F_CMD_ACCESS	(1 << 3)
    270  1.1  riastrad /* This reg has been accessed by a VM */
    271  1.1  riastrad #define F_ACCESSED	(1 << 4)
    272  1.1  riastrad /* This reg has been accessed through GPU commands */
    273  1.1  riastrad #define F_CMD_ACCESSED	(1 << 5)
    274  1.1  riastrad /* This reg could be accessed by unaligned address */
    275  1.1  riastrad #define F_UNALIGN	(1 << 6)
    276  1.1  riastrad /* This reg is saved/restored in context */
    277  1.1  riastrad #define F_IN_CTX	(1 << 7)
    278  1.1  riastrad 
    279  1.1  riastrad 	struct gvt_mmio_block *mmio_block;
    280  1.1  riastrad 	unsigned int num_mmio_block;
    281  1.1  riastrad 
    282  1.1  riastrad 	DECLARE_HASHTABLE(mmio_info_table, INTEL_GVT_MMIO_HASH_BITS);
    283  1.1  riastrad 	unsigned long num_tracked_mmio;
    284  1.1  riastrad };
    285  1.1  riastrad 
    286  1.1  riastrad struct intel_gvt_firmware {
    287  1.1  riastrad 	void *cfg_space;
    288  1.1  riastrad 	void *mmio;
    289  1.1  riastrad 	bool firmware_loaded;
    290  1.1  riastrad };
    291  1.1  riastrad 
    292  1.1  riastrad #define NR_MAX_INTEL_VGPU_TYPES 20
    293  1.1  riastrad struct intel_vgpu_type {
    294  1.1  riastrad 	char name[16];
    295  1.1  riastrad 	unsigned int avail_instance;
    296  1.1  riastrad 	unsigned int low_gm_size;
    297  1.1  riastrad 	unsigned int high_gm_size;
    298  1.1  riastrad 	unsigned int fence;
    299  1.1  riastrad 	unsigned int weight;
    300  1.1  riastrad 	enum intel_vgpu_edid resolution;
    301  1.1  riastrad };
    302  1.1  riastrad 
    303  1.1  riastrad struct intel_gvt {
    304  1.1  riastrad 	/* GVT scope lock, protect GVT itself, and all resource currently
    305  1.1  riastrad 	 * not yet protected by special locks(vgpu and scheduler lock).
    306  1.1  riastrad 	 */
    307  1.1  riastrad 	struct mutex lock;
    308  1.1  riastrad 	/* scheduler scope lock, protect gvt and vgpu schedule related data */
    309  1.1  riastrad 	struct mutex sched_lock;
    310  1.1  riastrad 
    311  1.1  riastrad 	struct drm_i915_private *dev_priv;
    312  1.1  riastrad 	struct idr vgpu_idr;	/* vGPU IDR pool */
    313  1.1  riastrad 
    314  1.1  riastrad 	struct intel_gvt_device_info device_info;
    315  1.1  riastrad 	struct intel_gvt_gm gm;
    316  1.1  riastrad 	struct intel_gvt_fence fence;
    317  1.1  riastrad 	struct intel_gvt_mmio mmio;
    318  1.1  riastrad 	struct intel_gvt_firmware firmware;
    319  1.1  riastrad 	struct intel_gvt_irq irq;
    320  1.1  riastrad 	struct intel_gvt_gtt gtt;
    321  1.1  riastrad 	struct intel_gvt_workload_scheduler scheduler;
    322  1.1  riastrad 	struct notifier_block shadow_ctx_notifier_block[I915_NUM_ENGINES];
    323  1.1  riastrad 	DECLARE_HASHTABLE(cmd_table, GVT_CMD_HASH_BITS);
    324  1.1  riastrad 	struct intel_vgpu_type *types;
    325  1.1  riastrad 	unsigned int num_types;
    326  1.1  riastrad 	struct intel_vgpu *idle_vgpu;
    327  1.1  riastrad 
    328  1.1  riastrad 	struct task_struct *service_thread;
    329  1.1  riastrad 	wait_queue_head_t service_thread_wq;
    330  1.1  riastrad 
    331  1.1  riastrad 	/* service_request is always used in bit operation, we should always
    332  1.1  riastrad 	 * use it with atomic bit ops so that no need to use gvt big lock.
    333  1.1  riastrad 	 */
    334  1.1  riastrad 	unsigned long service_request;
    335  1.1  riastrad 
    336  1.1  riastrad 	struct {
    337  1.1  riastrad 		struct engine_mmio *mmio;
    338  1.1  riastrad 		int ctx_mmio_count[I915_NUM_ENGINES];
    339  1.1  riastrad 		u32 *tlb_mmio_offset_list;
    340  1.1  riastrad 		u32 tlb_mmio_offset_list_cnt;
    341  1.1  riastrad 		u32 *mocs_mmio_offset_list;
    342  1.1  riastrad 		u32 mocs_mmio_offset_list_cnt;
    343  1.1  riastrad 	} engine_mmio_list;
    344  1.1  riastrad 
    345  1.1  riastrad 	struct dentry *debugfs_root;
    346  1.1  riastrad };
    347  1.1  riastrad 
    348  1.1  riastrad static inline struct intel_gvt *to_gvt(struct drm_i915_private *i915)
    349  1.1  riastrad {
    350  1.1  riastrad 	return i915->gvt;
    351  1.1  riastrad }
    352  1.1  riastrad 
    353  1.1  riastrad enum {
    354  1.1  riastrad 	INTEL_GVT_REQUEST_EMULATE_VBLANK = 0,
    355  1.1  riastrad 
    356  1.1  riastrad 	/* Scheduling trigger by timer */
    357  1.1  riastrad 	INTEL_GVT_REQUEST_SCHED = 1,
    358  1.1  riastrad 
    359  1.1  riastrad 	/* Scheduling trigger by event */
    360  1.1  riastrad 	INTEL_GVT_REQUEST_EVENT_SCHED = 2,
    361  1.1  riastrad };
    362  1.1  riastrad 
    363  1.1  riastrad static inline void intel_gvt_request_service(struct intel_gvt *gvt,
    364  1.1  riastrad 		int service)
    365  1.1  riastrad {
    366  1.1  riastrad 	set_bit(service, (void *)&gvt->service_request);
    367  1.1  riastrad 	wake_up(&gvt->service_thread_wq);
    368  1.1  riastrad }
    369  1.1  riastrad 
    370  1.1  riastrad void intel_gvt_free_firmware(struct intel_gvt *gvt);
    371  1.1  riastrad int intel_gvt_load_firmware(struct intel_gvt *gvt);
    372  1.1  riastrad 
    373  1.1  riastrad /* Aperture/GM space definitions for GVT device */
    374  1.1  riastrad #define MB_TO_BYTES(mb) ((mb) << 20ULL)
    375  1.1  riastrad #define BYTES_TO_MB(b) ((b) >> 20ULL)
    376  1.1  riastrad 
    377  1.1  riastrad #define HOST_LOW_GM_SIZE MB_TO_BYTES(128)
    378  1.1  riastrad #define HOST_HIGH_GM_SIZE MB_TO_BYTES(384)
    379  1.1  riastrad #define HOST_FENCE 4
    380  1.1  riastrad 
    381  1.1  riastrad /* Aperture/GM space definitions for GVT device */
    382  1.1  riastrad #define gvt_aperture_sz(gvt)	  (gvt->dev_priv->ggtt.mappable_end)
    383  1.1  riastrad #define gvt_aperture_pa_base(gvt) (gvt->dev_priv->ggtt.gmadr.start)
    384  1.1  riastrad 
    385  1.1  riastrad #define gvt_ggtt_gm_sz(gvt)	  (gvt->dev_priv->ggtt.vm.total)
    386  1.1  riastrad #define gvt_ggtt_sz(gvt) \
    387  1.1  riastrad 	((gvt->dev_priv->ggtt.vm.total >> PAGE_SHIFT) << 3)
    388  1.1  riastrad #define gvt_hidden_sz(gvt)	  (gvt_ggtt_gm_sz(gvt) - gvt_aperture_sz(gvt))
    389  1.1  riastrad 
    390  1.1  riastrad #define gvt_aperture_gmadr_base(gvt) (0)
    391  1.1  riastrad #define gvt_aperture_gmadr_end(gvt) (gvt_aperture_gmadr_base(gvt) \
    392  1.1  riastrad 				     + gvt_aperture_sz(gvt) - 1)
    393  1.1  riastrad 
    394  1.1  riastrad #define gvt_hidden_gmadr_base(gvt) (gvt_aperture_gmadr_base(gvt) \
    395  1.1  riastrad 				    + gvt_aperture_sz(gvt))
    396  1.1  riastrad #define gvt_hidden_gmadr_end(gvt) (gvt_hidden_gmadr_base(gvt) \
    397  1.1  riastrad 				   + gvt_hidden_sz(gvt) - 1)
    398  1.1  riastrad 
    399  1.1  riastrad #define gvt_fence_sz(gvt) ((gvt)->dev_priv->ggtt.num_fences)
    400  1.1  riastrad 
    401  1.1  riastrad /* Aperture/GM space definitions for vGPU */
    402  1.1  riastrad #define vgpu_aperture_offset(vgpu)	((vgpu)->gm.low_gm_node.start)
    403  1.1  riastrad #define vgpu_hidden_offset(vgpu)	((vgpu)->gm.high_gm_node.start)
    404  1.1  riastrad #define vgpu_aperture_sz(vgpu)		((vgpu)->gm.aperture_sz)
    405  1.1  riastrad #define vgpu_hidden_sz(vgpu)		((vgpu)->gm.hidden_sz)
    406  1.1  riastrad 
    407  1.1  riastrad #define vgpu_aperture_pa_base(vgpu) \
    408  1.1  riastrad 	(gvt_aperture_pa_base(vgpu->gvt) + vgpu_aperture_offset(vgpu))
    409  1.1  riastrad 
    410  1.1  riastrad #define vgpu_ggtt_gm_sz(vgpu) ((vgpu)->gm.aperture_sz + (vgpu)->gm.hidden_sz)
    411  1.1  riastrad 
    412  1.1  riastrad #define vgpu_aperture_pa_end(vgpu) \
    413  1.1  riastrad 	(vgpu_aperture_pa_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
    414  1.1  riastrad 
    415  1.1  riastrad #define vgpu_aperture_gmadr_base(vgpu) (vgpu_aperture_offset(vgpu))
    416  1.1  riastrad #define vgpu_aperture_gmadr_end(vgpu) \
    417  1.1  riastrad 	(vgpu_aperture_gmadr_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
    418  1.1  riastrad 
    419  1.1  riastrad #define vgpu_hidden_gmadr_base(vgpu) (vgpu_hidden_offset(vgpu))
    420  1.1  riastrad #define vgpu_hidden_gmadr_end(vgpu) \
    421  1.1  riastrad 	(vgpu_hidden_gmadr_base(vgpu) + vgpu_hidden_sz(vgpu) - 1)
    422  1.1  riastrad 
    423  1.1  riastrad #define vgpu_fence_base(vgpu) (vgpu->fence.base)
    424  1.1  riastrad #define vgpu_fence_sz(vgpu) (vgpu->fence.size)
    425  1.1  riastrad 
    426  1.1  riastrad struct intel_vgpu_creation_params {
    427  1.1  riastrad 	__u64 handle;
    428  1.1  riastrad 	__u64 low_gm_sz;  /* in MB */
    429  1.1  riastrad 	__u64 high_gm_sz; /* in MB */
    430  1.1  riastrad 	__u64 fence_sz;
    431  1.1  riastrad 	__u64 resolution;
    432  1.1  riastrad 	__s32 primary;
    433  1.1  riastrad 	__u64 vgpu_id;
    434  1.1  riastrad 
    435  1.1  riastrad 	__u32 weight;
    436  1.1  riastrad };
    437  1.1  riastrad 
    438  1.1  riastrad int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
    439  1.1  riastrad 			      struct intel_vgpu_creation_params *param);
    440  1.1  riastrad void intel_vgpu_reset_resource(struct intel_vgpu *vgpu);
    441  1.1  riastrad void intel_vgpu_free_resource(struct intel_vgpu *vgpu);
    442  1.1  riastrad void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
    443  1.1  riastrad 	u32 fence, u64 value);
    444  1.1  riastrad 
    445  1.1  riastrad /* Macros for easily accessing vGPU virtual/shadow register.
    446  1.1  riastrad    Explicitly seperate use for typed MMIO reg or real offset.*/
    447  1.1  riastrad #define vgpu_vreg_t(vgpu, reg) \
    448  1.1  riastrad 	(*(u32 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
    449  1.1  riastrad #define vgpu_vreg(vgpu, offset) \
    450  1.1  riastrad 	(*(u32 *)(vgpu->mmio.vreg + (offset)))
    451  1.1  riastrad #define vgpu_vreg64_t(vgpu, reg) \
    452  1.1  riastrad 	(*(u64 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
    453  1.1  riastrad #define vgpu_vreg64(vgpu, offset) \
    454  1.1  riastrad 	(*(u64 *)(vgpu->mmio.vreg + (offset)))
    455  1.1  riastrad 
    456  1.1  riastrad #define for_each_active_vgpu(gvt, vgpu, id) \
    457  1.1  riastrad 	idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) \
    458  1.1  riastrad 		for_each_if(vgpu->active)
    459  1.1  riastrad 
    460  1.1  riastrad static inline void intel_vgpu_write_pci_bar(struct intel_vgpu *vgpu,
    461  1.1  riastrad 					    u32 offset, u32 val, bool low)
    462  1.1  riastrad {
    463  1.1  riastrad 	u32 *pval;
    464  1.1  riastrad 
    465  1.1  riastrad 	/* BAR offset should be 32 bits algiend */
    466  1.1  riastrad 	offset = rounddown(offset, 4);
    467  1.1  riastrad 	pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
    468  1.1  riastrad 
    469  1.1  riastrad 	if (low) {
    470  1.1  riastrad 		/*
    471  1.1  riastrad 		 * only update bit 31 - bit 4,
    472  1.1  riastrad 		 * leave the bit 3 - bit 0 unchanged.
    473  1.1  riastrad 		 */
    474  1.1  riastrad 		*pval = (val & GENMASK(31, 4)) | (*pval & GENMASK(3, 0));
    475  1.1  riastrad 	} else {
    476  1.1  riastrad 		*pval = val;
    477  1.1  riastrad 	}
    478  1.1  riastrad }
    479  1.1  riastrad 
    480  1.1  riastrad int intel_gvt_init_vgpu_types(struct intel_gvt *gvt);
    481  1.1  riastrad void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt);
    482  1.1  riastrad 
    483  1.1  riastrad struct intel_vgpu *intel_gvt_create_idle_vgpu(struct intel_gvt *gvt);
    484  1.1  riastrad void intel_gvt_destroy_idle_vgpu(struct intel_vgpu *vgpu);
    485  1.1  riastrad struct intel_vgpu *intel_gvt_create_vgpu(struct intel_gvt *gvt,
    486  1.1  riastrad 					 struct intel_vgpu_type *type);
    487  1.1  riastrad void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu);
    488  1.1  riastrad void intel_gvt_release_vgpu(struct intel_vgpu *vgpu);
    489  1.1  riastrad void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
    490  1.1  riastrad 				 intel_engine_mask_t engine_mask);
    491  1.1  riastrad void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
    492  1.1  riastrad void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
    493  1.1  riastrad void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
    494  1.1  riastrad 
    495  1.1  riastrad /* validating GM functions */
    496  1.1  riastrad #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
    497  1.1  riastrad 	((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
    498  1.1  riastrad 	 (gmadr <= vgpu_aperture_gmadr_end(vgpu)))
    499  1.1  riastrad 
    500  1.1  riastrad #define vgpu_gmadr_is_hidden(vgpu, gmadr) \
    501  1.1  riastrad 	((gmadr >= vgpu_hidden_gmadr_base(vgpu)) && \
    502  1.1  riastrad 	 (gmadr <= vgpu_hidden_gmadr_end(vgpu)))
    503  1.1  riastrad 
    504  1.1  riastrad #define vgpu_gmadr_is_valid(vgpu, gmadr) \
    505  1.1  riastrad 	 ((vgpu_gmadr_is_aperture(vgpu, gmadr) || \
    506  1.1  riastrad 	  (vgpu_gmadr_is_hidden(vgpu, gmadr))))
    507  1.1  riastrad 
    508  1.1  riastrad #define gvt_gmadr_is_aperture(gvt, gmadr) \
    509  1.1  riastrad 	 ((gmadr >= gvt_aperture_gmadr_base(gvt)) && \
    510  1.1  riastrad 	  (gmadr <= gvt_aperture_gmadr_end(gvt)))
    511  1.1  riastrad 
    512  1.1  riastrad #define gvt_gmadr_is_hidden(gvt, gmadr) \
    513  1.1  riastrad 	  ((gmadr >= gvt_hidden_gmadr_base(gvt)) && \
    514  1.1  riastrad 	   (gmadr <= gvt_hidden_gmadr_end(gvt)))
    515  1.1  riastrad 
    516  1.1  riastrad #define gvt_gmadr_is_valid(gvt, gmadr) \
    517  1.1  riastrad 	  (gvt_gmadr_is_aperture(gvt, gmadr) || \
    518  1.1  riastrad 	    gvt_gmadr_is_hidden(gvt, gmadr))
    519  1.1  riastrad 
    520  1.1  riastrad bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size);
    521  1.1  riastrad int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr);
    522  1.1  riastrad int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr);
    523  1.1  riastrad int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
    524  1.1  riastrad 			     unsigned long *h_index);
    525  1.1  riastrad int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
    526  1.1  riastrad 			     unsigned long *g_index);
    527  1.1  riastrad 
    528  1.1  riastrad void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
    529  1.1  riastrad 		bool primary);
    530  1.1  riastrad void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu);
    531  1.1  riastrad 
    532  1.1  riastrad int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
    533  1.1  riastrad 		void *p_data, unsigned int bytes);
    534  1.1  riastrad 
    535  1.1  riastrad int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
    536  1.1  riastrad 		void *p_data, unsigned int bytes);
    537  1.1  riastrad 
    538  1.1  riastrad void intel_vgpu_emulate_hotplug(struct intel_vgpu *vgpu, bool connected);
    539  1.1  riastrad 
    540  1.1  riastrad static inline u64 intel_vgpu_get_bar_gpa(struct intel_vgpu *vgpu, int bar)
    541  1.1  riastrad {
    542  1.1  riastrad 	/* We are 64bit bar. */
    543  1.1  riastrad 	return (*(u64 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
    544  1.1  riastrad 			PCI_BASE_ADDRESS_MEM_MASK;
    545  1.1  riastrad }
    546  1.1  riastrad 
    547  1.1  riastrad void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu);
    548  1.1  riastrad int intel_vgpu_init_opregion(struct intel_vgpu *vgpu);
    549  1.1  riastrad int intel_vgpu_opregion_base_write_handler(struct intel_vgpu *vgpu, u32 gpa);
    550  1.1  riastrad 
    551  1.1  riastrad int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci);
    552  1.1  riastrad void populate_pvinfo_page(struct intel_vgpu *vgpu);
    553  1.1  riastrad 
    554  1.1  riastrad int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
    555  1.1  riastrad void enter_failsafe_mode(struct intel_vgpu *vgpu, int reason);
    556  1.1  riastrad 
    557  1.1  riastrad struct intel_gvt_ops {
    558  1.1  riastrad 	int (*emulate_cfg_read)(struct intel_vgpu *, unsigned int, void *,
    559  1.1  riastrad 				unsigned int);
    560  1.1  riastrad 	int (*emulate_cfg_write)(struct intel_vgpu *, unsigned int, void *,
    561  1.1  riastrad 				unsigned int);
    562  1.1  riastrad 	int (*emulate_mmio_read)(struct intel_vgpu *, u64, void *,
    563  1.1  riastrad 				unsigned int);
    564  1.1  riastrad 	int (*emulate_mmio_write)(struct intel_vgpu *, u64, void *,
    565  1.1  riastrad 				unsigned int);
    566  1.1  riastrad 	struct intel_vgpu *(*vgpu_create)(struct intel_gvt *,
    567  1.1  riastrad 				struct intel_vgpu_type *);
    568  1.1  riastrad 	void (*vgpu_destroy)(struct intel_vgpu *vgpu);
    569  1.1  riastrad 	void (*vgpu_release)(struct intel_vgpu *vgpu);
    570  1.1  riastrad 	void (*vgpu_reset)(struct intel_vgpu *);
    571  1.1  riastrad 	void (*vgpu_activate)(struct intel_vgpu *);
    572  1.1  riastrad 	void (*vgpu_deactivate)(struct intel_vgpu *);
    573  1.1  riastrad 	struct intel_vgpu_type *(*gvt_find_vgpu_type)(struct intel_gvt *gvt,
    574  1.1  riastrad 			const char *name);
    575  1.1  riastrad 	bool (*get_gvt_attrs)(struct attribute ***type_attrs,
    576  1.1  riastrad 			struct attribute_group ***intel_vgpu_type_groups);
    577  1.1  riastrad 	int (*vgpu_query_plane)(struct intel_vgpu *vgpu, void *);
    578  1.1  riastrad 	int (*vgpu_get_dmabuf)(struct intel_vgpu *vgpu, unsigned int);
    579  1.1  riastrad 	int (*write_protect_handler)(struct intel_vgpu *, u64, void *,
    580  1.1  riastrad 				     unsigned int);
    581  1.1  riastrad 	void (*emulate_hotplug)(struct intel_vgpu *vgpu, bool connected);
    582  1.1  riastrad };
    583  1.1  riastrad 
    584  1.1  riastrad 
    585  1.1  riastrad enum {
    586  1.1  riastrad 	GVT_FAILSAFE_UNSUPPORTED_GUEST,
    587  1.1  riastrad 	GVT_FAILSAFE_INSUFFICIENT_RESOURCE,
    588  1.1  riastrad 	GVT_FAILSAFE_GUEST_ERR,
    589  1.1  riastrad };
    590  1.1  riastrad 
    591  1.1  riastrad static inline void mmio_hw_access_pre(struct drm_i915_private *dev_priv)
    592  1.1  riastrad {
    593  1.1  riastrad 	intel_runtime_pm_get(&dev_priv->runtime_pm);
    594  1.1  riastrad }
    595  1.1  riastrad 
    596  1.1  riastrad static inline void mmio_hw_access_post(struct drm_i915_private *dev_priv)
    597  1.1  riastrad {
    598  1.1  riastrad 	intel_runtime_pm_put_unchecked(&dev_priv->runtime_pm);
    599  1.1  riastrad }
    600  1.1  riastrad 
    601  1.1  riastrad /**
    602  1.1  riastrad  * intel_gvt_mmio_set_accessed - mark a MMIO has been accessed
    603  1.1  riastrad  * @gvt: a GVT device
    604  1.1  riastrad  * @offset: register offset
    605  1.1  riastrad  *
    606  1.1  riastrad  */
    607  1.1  riastrad static inline void intel_gvt_mmio_set_accessed(
    608  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    609  1.1  riastrad {
    610  1.1  riastrad 	gvt->mmio.mmio_attribute[offset >> 2] |= F_ACCESSED;
    611  1.1  riastrad }
    612  1.1  riastrad 
    613  1.1  riastrad /**
    614  1.1  riastrad  * intel_gvt_mmio_is_cmd_accessed - mark a MMIO could be accessed by command
    615  1.1  riastrad  * @gvt: a GVT device
    616  1.1  riastrad  * @offset: register offset
    617  1.1  riastrad  *
    618  1.1  riastrad  */
    619  1.1  riastrad static inline bool intel_gvt_mmio_is_cmd_access(
    620  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    621  1.1  riastrad {
    622  1.1  riastrad 	return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_ACCESS;
    623  1.1  riastrad }
    624  1.1  riastrad 
    625  1.1  riastrad /**
    626  1.1  riastrad  * intel_gvt_mmio_is_unalign - mark a MMIO could be accessed unaligned
    627  1.1  riastrad  * @gvt: a GVT device
    628  1.1  riastrad  * @offset: register offset
    629  1.1  riastrad  *
    630  1.1  riastrad  */
    631  1.1  riastrad static inline bool intel_gvt_mmio_is_unalign(
    632  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    633  1.1  riastrad {
    634  1.1  riastrad 	return gvt->mmio.mmio_attribute[offset >> 2] & F_UNALIGN;
    635  1.1  riastrad }
    636  1.1  riastrad 
    637  1.1  riastrad /**
    638  1.1  riastrad  * intel_gvt_mmio_set_cmd_accessed - mark a MMIO has been accessed by command
    639  1.1  riastrad  * @gvt: a GVT device
    640  1.1  riastrad  * @offset: register offset
    641  1.1  riastrad  *
    642  1.1  riastrad  */
    643  1.1  riastrad static inline void intel_gvt_mmio_set_cmd_accessed(
    644  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    645  1.1  riastrad {
    646  1.1  riastrad 	gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_ACCESSED;
    647  1.1  riastrad }
    648  1.1  riastrad 
    649  1.1  riastrad /**
    650  1.1  riastrad  * intel_gvt_mmio_has_mode_mask - if a MMIO has a mode mask
    651  1.1  riastrad  * @gvt: a GVT device
    652  1.1  riastrad  * @offset: register offset
    653  1.1  riastrad  *
    654  1.1  riastrad  * Returns:
    655  1.1  riastrad  * True if a MMIO has a mode mask in its higher 16 bits, false if it isn't.
    656  1.1  riastrad  *
    657  1.1  riastrad  */
    658  1.1  riastrad static inline bool intel_gvt_mmio_has_mode_mask(
    659  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    660  1.1  riastrad {
    661  1.1  riastrad 	return gvt->mmio.mmio_attribute[offset >> 2] & F_MODE_MASK;
    662  1.1  riastrad }
    663  1.1  riastrad 
    664  1.1  riastrad /**
    665  1.1  riastrad  * intel_gvt_mmio_is_in_ctx - check if a MMIO has in-ctx mask
    666  1.1  riastrad  * @gvt: a GVT device
    667  1.1  riastrad  * @offset: register offset
    668  1.1  riastrad  *
    669  1.1  riastrad  * Returns:
    670  1.1  riastrad  * True if a MMIO has a in-context mask, false if it isn't.
    671  1.1  riastrad  *
    672  1.1  riastrad  */
    673  1.1  riastrad static inline bool intel_gvt_mmio_is_in_ctx(
    674  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    675  1.1  riastrad {
    676  1.1  riastrad 	return gvt->mmio.mmio_attribute[offset >> 2] & F_IN_CTX;
    677  1.1  riastrad }
    678  1.1  riastrad 
    679  1.1  riastrad /**
    680  1.1  riastrad  * intel_gvt_mmio_set_in_ctx - mask a MMIO in logical context
    681  1.1  riastrad  * @gvt: a GVT device
    682  1.1  riastrad  * @offset: register offset
    683  1.1  riastrad  *
    684  1.1  riastrad  */
    685  1.1  riastrad static inline void intel_gvt_mmio_set_in_ctx(
    686  1.1  riastrad 			struct intel_gvt *gvt, unsigned int offset)
    687  1.1  riastrad {
    688  1.1  riastrad 	gvt->mmio.mmio_attribute[offset >> 2] |= F_IN_CTX;
    689  1.1  riastrad }
    690  1.1  riastrad 
    691  1.1  riastrad void intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu);
    692  1.1  riastrad void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu);
    693  1.1  riastrad void intel_gvt_debugfs_init(struct intel_gvt *gvt);
    694  1.1  riastrad void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
    695  1.1  riastrad 
    696  1.1  riastrad 
    697  1.1  riastrad #include "trace.h"
    698  1.1  riastrad #include "mpt.h"
    699  1.1  riastrad 
    700  1.1  riastrad #endif
    701