xref: /dragonfly/sys/dev/drm/i915/gvt/gvt.h (revision 3f2dd94a)
11487f786SFrançois Tigeot /*
21487f786SFrançois Tigeot  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
31487f786SFrançois Tigeot  *
41487f786SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
51487f786SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
61487f786SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
71487f786SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81487f786SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
91487f786SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
101487f786SFrançois Tigeot  *
111487f786SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
121487f786SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
131487f786SFrançois Tigeot  * Software.
141487f786SFrançois Tigeot  *
151487f786SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161487f786SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171487f786SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181487f786SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191487f786SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
201487f786SFrançois Tigeot  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
211487f786SFrançois Tigeot  * SOFTWARE.
224be47400SFrançois Tigeot  *
234be47400SFrançois Tigeot  * Authors:
244be47400SFrançois Tigeot  *    Kevin Tian <kevin.tian@intel.com>
254be47400SFrançois Tigeot  *    Eddie Dong <eddie.dong@intel.com>
264be47400SFrançois Tigeot  *
274be47400SFrançois Tigeot  * Contributors:
284be47400SFrançois Tigeot  *    Niu Bing <bing.niu@intel.com>
294be47400SFrançois Tigeot  *    Zhi Wang <zhi.a.wang@intel.com>
304be47400SFrançois Tigeot  *
311487f786SFrançois Tigeot  */
321487f786SFrançois Tigeot 
331487f786SFrançois Tigeot #ifndef _GVT_H_
341487f786SFrançois Tigeot #define _GVT_H_
351487f786SFrançois Tigeot 
361487f786SFrançois Tigeot #include "debug.h"
371487f786SFrançois Tigeot #include "hypercall.h"
384be47400SFrançois Tigeot #include "mmio.h"
394be47400SFrançois Tigeot #include "reg.h"
404be47400SFrançois Tigeot #include "interrupt.h"
414be47400SFrançois Tigeot #include "gtt.h"
424be47400SFrançois Tigeot #include "display.h"
434be47400SFrançois Tigeot #include "edid.h"
444be47400SFrançois Tigeot #include "execlist.h"
454be47400SFrançois Tigeot #include "scheduler.h"
464be47400SFrançois Tigeot #include "sched_policy.h"
474be47400SFrançois Tigeot #include "render.h"
484be47400SFrançois Tigeot #include "cmd_parser.h"
491487f786SFrançois Tigeot 
501487f786SFrançois Tigeot #define GVT_MAX_VGPU 8
511487f786SFrançois Tigeot 
521487f786SFrançois Tigeot enum {
531487f786SFrançois Tigeot 	INTEL_GVT_HYPERVISOR_XEN = 0,
541487f786SFrançois Tigeot 	INTEL_GVT_HYPERVISOR_KVM,
551487f786SFrançois Tigeot };
561487f786SFrançois Tigeot 
571487f786SFrançois Tigeot struct intel_gvt_host {
581487f786SFrançois Tigeot 	bool initialized;
591487f786SFrançois Tigeot 	int hypervisor_type;
601487f786SFrançois Tigeot 	struct intel_gvt_mpt *mpt;
611487f786SFrançois Tigeot };
621487f786SFrançois Tigeot 
631487f786SFrançois Tigeot extern struct intel_gvt_host intel_gvt_host;
641487f786SFrançois Tigeot 
651487f786SFrançois Tigeot /* Describe per-platform limitations. */
661487f786SFrançois Tigeot struct intel_gvt_device_info {
671487f786SFrançois Tigeot 	u32 max_support_vgpus;
684be47400SFrançois Tigeot 	u32 cfg_space_size;
694be47400SFrançois Tigeot 	u32 mmio_size;
704be47400SFrançois Tigeot 	u32 mmio_bar;
714be47400SFrançois Tigeot 	unsigned long msi_cap_offset;
724be47400SFrançois Tigeot 	u32 gtt_start_offset;
734be47400SFrançois Tigeot 	u32 gtt_entry_size;
744be47400SFrançois Tigeot 	u32 gtt_entry_size_shift;
754be47400SFrançois Tigeot 	int gmadr_bytes_in_cmd;
764be47400SFrançois Tigeot 	u32 max_surface_size;
774be47400SFrançois Tigeot };
784be47400SFrançois Tigeot 
794be47400SFrançois Tigeot /* GM resources owned by a vGPU */
804be47400SFrançois Tigeot struct intel_vgpu_gm {
814be47400SFrançois Tigeot 	u64 aperture_sz;
824be47400SFrançois Tigeot 	u64 hidden_sz;
83*3f2dd94aSFrançois Tigeot 	void *aperture_va;
844be47400SFrançois Tigeot 	struct drm_mm_node low_gm_node;
854be47400SFrançois Tigeot 	struct drm_mm_node high_gm_node;
864be47400SFrançois Tigeot };
874be47400SFrançois Tigeot 
884be47400SFrançois Tigeot #define INTEL_GVT_MAX_NUM_FENCES 32
894be47400SFrançois Tigeot 
904be47400SFrançois Tigeot /* Fences owned by a vGPU */
914be47400SFrançois Tigeot struct intel_vgpu_fence {
924be47400SFrançois Tigeot 	struct drm_i915_fence_reg *regs[INTEL_GVT_MAX_NUM_FENCES];
934be47400SFrançois Tigeot 	u32 base;
944be47400SFrançois Tigeot 	u32 size;
954be47400SFrançois Tigeot };
964be47400SFrançois Tigeot 
974be47400SFrançois Tigeot struct intel_vgpu_mmio {
984be47400SFrançois Tigeot 	void *vreg;
994be47400SFrançois Tigeot 	void *sreg;
1004be47400SFrançois Tigeot 	bool disable_warn_untrack;
1014be47400SFrançois Tigeot };
1024be47400SFrançois Tigeot 
1034be47400SFrançois Tigeot #define INTEL_GVT_MAX_BAR_NUM 4
1044be47400SFrançois Tigeot 
1054be47400SFrançois Tigeot struct intel_vgpu_pci_bar {
1064be47400SFrançois Tigeot 	u64 size;
1074be47400SFrançois Tigeot 	bool tracked;
1084be47400SFrançois Tigeot };
1094be47400SFrançois Tigeot 
1104be47400SFrançois Tigeot struct intel_vgpu_cfg_space {
111*3f2dd94aSFrançois Tigeot 	unsigned char virtual_cfg_space[PCI_CFG_SPACE_EXP_SIZE];
1124be47400SFrançois Tigeot 	struct intel_vgpu_pci_bar bar[INTEL_GVT_MAX_BAR_NUM];
1134be47400SFrançois Tigeot };
1144be47400SFrançois Tigeot 
1154be47400SFrançois Tigeot #define vgpu_cfg_space(vgpu) ((vgpu)->cfg_space.virtual_cfg_space)
1164be47400SFrançois Tigeot 
1174be47400SFrançois Tigeot #define INTEL_GVT_MAX_PIPE 4
1184be47400SFrançois Tigeot 
1194be47400SFrançois Tigeot struct intel_vgpu_irq {
1204be47400SFrançois Tigeot 	bool irq_warn_once[INTEL_GVT_EVENT_MAX];
1214be47400SFrançois Tigeot 	DECLARE_BITMAP(flip_done_event[INTEL_GVT_MAX_PIPE],
1224be47400SFrançois Tigeot 		       INTEL_GVT_EVENT_MAX);
1234be47400SFrançois Tigeot };
1244be47400SFrançois Tigeot 
1254be47400SFrançois Tigeot struct intel_vgpu_opregion {
1264be47400SFrançois Tigeot 	void *va;
1274be47400SFrançois Tigeot 	u32 gfn[INTEL_GVT_OPREGION_PAGES];
1284be47400SFrançois Tigeot 	struct page *pages[INTEL_GVT_OPREGION_PAGES];
1294be47400SFrançois Tigeot };
1304be47400SFrançois Tigeot 
1314be47400SFrançois Tigeot #define vgpu_opregion(vgpu) (&(vgpu->opregion))
1324be47400SFrançois Tigeot 
1334be47400SFrançois Tigeot #define INTEL_GVT_MAX_PORT 5
1344be47400SFrançois Tigeot 
1354be47400SFrançois Tigeot struct intel_vgpu_display {
1364be47400SFrançois Tigeot 	struct intel_vgpu_i2c_edid i2c_edid;
1374be47400SFrançois Tigeot 	struct intel_vgpu_port ports[INTEL_GVT_MAX_PORT];
1384be47400SFrançois Tigeot 	struct intel_vgpu_sbi sbi;
1391487f786SFrançois Tigeot };
1401487f786SFrançois Tigeot 
141a85cb24fSFrançois Tigeot struct vgpu_sched_ctl {
142a85cb24fSFrançois Tigeot 	int weight;
143a85cb24fSFrançois Tigeot };
144a85cb24fSFrançois Tigeot 
1451487f786SFrançois Tigeot struct intel_vgpu {
1461487f786SFrançois Tigeot 	struct intel_gvt *gvt;
1471487f786SFrançois Tigeot 	int id;
1481487f786SFrançois Tigeot 	unsigned long handle; /* vGPU handle used by hypervisor MPT modules */
1494be47400SFrançois Tigeot 	bool active;
150a85cb24fSFrançois Tigeot 	bool pv_notified;
151a85cb24fSFrançois Tigeot 	bool failsafe;
152*3f2dd94aSFrançois Tigeot 	unsigned int resetting_eng;
1534be47400SFrançois Tigeot 	void *sched_data;
154a85cb24fSFrançois Tigeot 	struct vgpu_sched_ctl sched_ctl;
1554be47400SFrançois Tigeot 
1564be47400SFrançois Tigeot 	struct intel_vgpu_fence fence;
1574be47400SFrançois Tigeot 	struct intel_vgpu_gm gm;
1584be47400SFrançois Tigeot 	struct intel_vgpu_cfg_space cfg_space;
1594be47400SFrançois Tigeot 	struct intel_vgpu_mmio mmio;
1604be47400SFrançois Tigeot 	struct intel_vgpu_irq irq;
1614be47400SFrançois Tigeot 	struct intel_vgpu_gtt gtt;
1624be47400SFrançois Tigeot 	struct intel_vgpu_opregion opregion;
1634be47400SFrançois Tigeot 	struct intel_vgpu_display display;
1644be47400SFrançois Tigeot 	struct intel_vgpu_execlist execlist[I915_NUM_ENGINES];
1654be47400SFrançois Tigeot 	struct list_head workload_q_head[I915_NUM_ENGINES];
1664be47400SFrançois Tigeot 	struct kmem_cache *workloads;
1674be47400SFrançois Tigeot 	atomic_t running_workload_num;
168a85cb24fSFrançois Tigeot 	ktime_t last_ctx_submit_time;
1694be47400SFrançois Tigeot 	DECLARE_BITMAP(tlb_handle_pending, I915_NUM_ENGINES);
1704be47400SFrançois Tigeot 	struct i915_gem_context *shadow_ctx;
171*3f2dd94aSFrançois Tigeot 	DECLARE_BITMAP(shadow_ctx_desc_updated, I915_NUM_ENGINES);
1724be47400SFrançois Tigeot 
1734be47400SFrançois Tigeot #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
1744be47400SFrançois Tigeot 	struct {
1754be47400SFrançois Tigeot 		struct mdev_device *mdev;
1764be47400SFrançois Tigeot 		struct vfio_region *region;
1774be47400SFrançois Tigeot 		int num_regions;
1784be47400SFrançois Tigeot 		struct eventfd_ctx *intx_trigger;
1794be47400SFrançois Tigeot 		struct eventfd_ctx *msi_trigger;
1804be47400SFrançois Tigeot 		struct rb_root cache;
1814be47400SFrançois Tigeot 		struct mutex cache_lock;
1824be47400SFrançois Tigeot 		struct notifier_block iommu_notifier;
1834be47400SFrançois Tigeot 		struct notifier_block group_notifier;
1844be47400SFrançois Tigeot 		struct kvm *kvm;
1854be47400SFrançois Tigeot 		struct work_struct release_work;
1864be47400SFrançois Tigeot 		atomic_t released;
1874be47400SFrançois Tigeot 	} vdev;
1884be47400SFrançois Tigeot #endif
1894be47400SFrançois Tigeot };
1904be47400SFrançois Tigeot 
1914be47400SFrançois Tigeot struct intel_gvt_gm {
1924be47400SFrançois Tigeot 	unsigned long vgpu_allocated_low_gm_size;
1934be47400SFrançois Tigeot 	unsigned long vgpu_allocated_high_gm_size;
1944be47400SFrançois Tigeot };
1954be47400SFrançois Tigeot 
1964be47400SFrançois Tigeot struct intel_gvt_fence {
1974be47400SFrançois Tigeot 	unsigned long vgpu_allocated_fence_num;
1984be47400SFrançois Tigeot };
1994be47400SFrançois Tigeot 
200*3f2dd94aSFrançois Tigeot /* Special MMIO blocks. */
201*3f2dd94aSFrançois Tigeot struct gvt_mmio_block {
202*3f2dd94aSFrançois Tigeot 	unsigned int device;
203*3f2dd94aSFrançois Tigeot 	i915_reg_t   offset;
204*3f2dd94aSFrançois Tigeot 	unsigned int size;
205*3f2dd94aSFrançois Tigeot 	gvt_mmio_func read;
206*3f2dd94aSFrançois Tigeot 	gvt_mmio_func write;
207*3f2dd94aSFrançois Tigeot };
208*3f2dd94aSFrançois Tigeot 
209*3f2dd94aSFrançois Tigeot #define INTEL_GVT_MMIO_HASH_BITS 11
2104be47400SFrançois Tigeot 
2114be47400SFrançois Tigeot struct intel_gvt_mmio {
212*3f2dd94aSFrançois Tigeot 	u8 *mmio_attribute;
213*3f2dd94aSFrançois Tigeot /* Register contains RO bits */
214*3f2dd94aSFrançois Tigeot #define F_RO		(1 << 0)
215*3f2dd94aSFrançois Tigeot /* Register contains graphics address */
216*3f2dd94aSFrançois Tigeot #define F_GMADR		(1 << 1)
217*3f2dd94aSFrançois Tigeot /* Mode mask registers with high 16 bits as the mask bits */
218*3f2dd94aSFrançois Tigeot #define F_MODE_MASK	(1 << 2)
219*3f2dd94aSFrançois Tigeot /* This reg can be accessed by GPU commands */
220*3f2dd94aSFrançois Tigeot #define F_CMD_ACCESS	(1 << 3)
221*3f2dd94aSFrançois Tigeot /* This reg has been accessed by a VM */
222*3f2dd94aSFrançois Tigeot #define F_ACCESSED	(1 << 4)
223*3f2dd94aSFrançois Tigeot /* This reg has been accessed through GPU commands */
224*3f2dd94aSFrançois Tigeot #define F_CMD_ACCESSED	(1 << 5)
225*3f2dd94aSFrançois Tigeot /* This reg could be accessed by unaligned address */
226*3f2dd94aSFrançois Tigeot #define F_UNALIGN	(1 << 6)
227*3f2dd94aSFrançois Tigeot 
228*3f2dd94aSFrançois Tigeot 	struct gvt_mmio_block *mmio_block;
229*3f2dd94aSFrançois Tigeot 	unsigned int num_mmio_block;
230*3f2dd94aSFrançois Tigeot 
2314be47400SFrançois Tigeot 	DECLARE_HASHTABLE(mmio_info_table, INTEL_GVT_MMIO_HASH_BITS);
232*3f2dd94aSFrançois Tigeot 	unsigned int num_tracked_mmio;
2334be47400SFrançois Tigeot };
2344be47400SFrançois Tigeot 
2354be47400SFrançois Tigeot struct intel_gvt_firmware {
2364be47400SFrançois Tigeot 	void *cfg_space;
2374be47400SFrançois Tigeot 	void *mmio;
2384be47400SFrançois Tigeot 	bool firmware_loaded;
2394be47400SFrançois Tigeot };
2404be47400SFrançois Tigeot 
2414be47400SFrançois Tigeot struct intel_gvt_opregion {
242a85cb24fSFrançois Tigeot 	void *opregion_va;
2434be47400SFrançois Tigeot 	u32 opregion_pa;
2444be47400SFrançois Tigeot };
2454be47400SFrançois Tigeot 
2464be47400SFrançois Tigeot #define NR_MAX_INTEL_VGPU_TYPES 20
2474be47400SFrançois Tigeot struct intel_vgpu_type {
2484be47400SFrançois Tigeot 	char name[16];
2494be47400SFrançois Tigeot 	unsigned int avail_instance;
2504be47400SFrançois Tigeot 	unsigned int low_gm_size;
2514be47400SFrançois Tigeot 	unsigned int high_gm_size;
2524be47400SFrançois Tigeot 	unsigned int fence;
253a85cb24fSFrançois Tigeot 	unsigned int weight;
254a85cb24fSFrançois Tigeot 	enum intel_vgpu_edid resolution;
2551487f786SFrançois Tigeot };
2561487f786SFrançois Tigeot 
2571487f786SFrançois Tigeot struct intel_gvt {
2584be47400SFrançois Tigeot 	struct mutex lock;
2591487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv;
2601487f786SFrançois Tigeot 	struct idr vgpu_idr;	/* vGPU IDR pool */
2611487f786SFrançois Tigeot 
2621487f786SFrançois Tigeot 	struct intel_gvt_device_info device_info;
2634be47400SFrançois Tigeot 	struct intel_gvt_gm gm;
2644be47400SFrançois Tigeot 	struct intel_gvt_fence fence;
2654be47400SFrançois Tigeot 	struct intel_gvt_mmio mmio;
2664be47400SFrançois Tigeot 	struct intel_gvt_firmware firmware;
2674be47400SFrançois Tigeot 	struct intel_gvt_irq irq;
2684be47400SFrançois Tigeot 	struct intel_gvt_gtt gtt;
2694be47400SFrançois Tigeot 	struct intel_gvt_opregion opregion;
2704be47400SFrançois Tigeot 	struct intel_gvt_workload_scheduler scheduler;
271a85cb24fSFrançois Tigeot 	struct notifier_block shadow_ctx_notifier_block[I915_NUM_ENGINES];
2724be47400SFrançois Tigeot 	DECLARE_HASHTABLE(cmd_table, GVT_CMD_HASH_BITS);
2734be47400SFrançois Tigeot 	struct intel_vgpu_type *types;
2744be47400SFrançois Tigeot 	unsigned int num_types;
275a85cb24fSFrançois Tigeot 	struct intel_vgpu *idle_vgpu;
2764be47400SFrançois Tigeot 
2774be47400SFrançois Tigeot 	struct task_struct *service_thread;
2784be47400SFrançois Tigeot 	wait_queue_head_t service_thread_wq;
2794be47400SFrançois Tigeot 	unsigned long service_request;
2801487f786SFrançois Tigeot };
2811487f786SFrançois Tigeot 
to_gvt(struct drm_i915_private * i915)2824be47400SFrançois Tigeot static inline struct intel_gvt *to_gvt(struct drm_i915_private *i915)
2834be47400SFrançois Tigeot {
2844be47400SFrançois Tigeot 	return i915->gvt;
2854be47400SFrançois Tigeot }
2864be47400SFrançois Tigeot 
2874be47400SFrançois Tigeot enum {
2884be47400SFrançois Tigeot 	INTEL_GVT_REQUEST_EMULATE_VBLANK = 0,
289*3f2dd94aSFrançois Tigeot 
290*3f2dd94aSFrançois Tigeot 	/* Scheduling trigger by timer */
291a85cb24fSFrançois Tigeot 	INTEL_GVT_REQUEST_SCHED = 1,
292*3f2dd94aSFrançois Tigeot 
293*3f2dd94aSFrançois Tigeot 	/* Scheduling trigger by event */
294*3f2dd94aSFrançois Tigeot 	INTEL_GVT_REQUEST_EVENT_SCHED = 2,
2954be47400SFrançois Tigeot };
2964be47400SFrançois Tigeot 
intel_gvt_request_service(struct intel_gvt * gvt,int service)2974be47400SFrançois Tigeot static inline void intel_gvt_request_service(struct intel_gvt *gvt,
2984be47400SFrançois Tigeot 		int service)
2994be47400SFrançois Tigeot {
3004be47400SFrançois Tigeot 	set_bit(service, (void *)&gvt->service_request);
3014be47400SFrançois Tigeot 	wake_up(&gvt->service_thread_wq);
3024be47400SFrançois Tigeot }
3034be47400SFrançois Tigeot 
3044be47400SFrançois Tigeot void intel_gvt_free_firmware(struct intel_gvt *gvt);
3054be47400SFrançois Tigeot int intel_gvt_load_firmware(struct intel_gvt *gvt);
3064be47400SFrançois Tigeot 
3074be47400SFrançois Tigeot /* Aperture/GM space definitions for GVT device */
3084be47400SFrançois Tigeot #define MB_TO_BYTES(mb) ((mb) << 20ULL)
3094be47400SFrançois Tigeot #define BYTES_TO_MB(b) ((b) >> 20ULL)
3104be47400SFrançois Tigeot 
3114be47400SFrançois Tigeot #define HOST_LOW_GM_SIZE MB_TO_BYTES(128)
3124be47400SFrançois Tigeot #define HOST_HIGH_GM_SIZE MB_TO_BYTES(384)
3134be47400SFrançois Tigeot #define HOST_FENCE 4
3144be47400SFrançois Tigeot 
3154be47400SFrançois Tigeot /* Aperture/GM space definitions for GVT device */
3164be47400SFrançois Tigeot #define gvt_aperture_sz(gvt)	  (gvt->dev_priv->ggtt.mappable_end)
3174be47400SFrançois Tigeot #define gvt_aperture_pa_base(gvt) (gvt->dev_priv->ggtt.mappable_base)
3184be47400SFrançois Tigeot 
3194be47400SFrançois Tigeot #define gvt_ggtt_gm_sz(gvt)	  (gvt->dev_priv->ggtt.base.total)
3204be47400SFrançois Tigeot #define gvt_ggtt_sz(gvt) \
3214be47400SFrançois Tigeot 	((gvt->dev_priv->ggtt.base.total >> PAGE_SHIFT) << 3)
3224be47400SFrançois Tigeot #define gvt_hidden_sz(gvt)	  (gvt_ggtt_gm_sz(gvt) - gvt_aperture_sz(gvt))
3234be47400SFrançois Tigeot 
3244be47400SFrançois Tigeot #define gvt_aperture_gmadr_base(gvt) (0)
3254be47400SFrançois Tigeot #define gvt_aperture_gmadr_end(gvt) (gvt_aperture_gmadr_base(gvt) \
3264be47400SFrançois Tigeot 				     + gvt_aperture_sz(gvt) - 1)
3274be47400SFrançois Tigeot 
3284be47400SFrançois Tigeot #define gvt_hidden_gmadr_base(gvt) (gvt_aperture_gmadr_base(gvt) \
3294be47400SFrançois Tigeot 				    + gvt_aperture_sz(gvt))
3304be47400SFrançois Tigeot #define gvt_hidden_gmadr_end(gvt) (gvt_hidden_gmadr_base(gvt) \
3314be47400SFrançois Tigeot 				   + gvt_hidden_sz(gvt) - 1)
3324be47400SFrançois Tigeot 
3334be47400SFrançois Tigeot #define gvt_fence_sz(gvt) (gvt->dev_priv->num_fence_regs)
3344be47400SFrançois Tigeot 
3354be47400SFrançois Tigeot /* Aperture/GM space definitions for vGPU */
3364be47400SFrançois Tigeot #define vgpu_aperture_offset(vgpu)	((vgpu)->gm.low_gm_node.start)
3374be47400SFrançois Tigeot #define vgpu_hidden_offset(vgpu)	((vgpu)->gm.high_gm_node.start)
3384be47400SFrançois Tigeot #define vgpu_aperture_sz(vgpu)		((vgpu)->gm.aperture_sz)
3394be47400SFrançois Tigeot #define vgpu_hidden_sz(vgpu)		((vgpu)->gm.hidden_sz)
3404be47400SFrançois Tigeot 
3414be47400SFrançois Tigeot #define vgpu_aperture_pa_base(vgpu) \
3424be47400SFrançois Tigeot 	(gvt_aperture_pa_base(vgpu->gvt) + vgpu_aperture_offset(vgpu))
3434be47400SFrançois Tigeot 
3444be47400SFrançois Tigeot #define vgpu_ggtt_gm_sz(vgpu) ((vgpu)->gm.aperture_sz + (vgpu)->gm.hidden_sz)
3454be47400SFrançois Tigeot 
3464be47400SFrançois Tigeot #define vgpu_aperture_pa_end(vgpu) \
3474be47400SFrançois Tigeot 	(vgpu_aperture_pa_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
3484be47400SFrançois Tigeot 
3494be47400SFrançois Tigeot #define vgpu_aperture_gmadr_base(vgpu) (vgpu_aperture_offset(vgpu))
3504be47400SFrançois Tigeot #define vgpu_aperture_gmadr_end(vgpu) \
3514be47400SFrançois Tigeot 	(vgpu_aperture_gmadr_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
3524be47400SFrançois Tigeot 
3534be47400SFrançois Tigeot #define vgpu_hidden_gmadr_base(vgpu) (vgpu_hidden_offset(vgpu))
3544be47400SFrançois Tigeot #define vgpu_hidden_gmadr_end(vgpu) \
3554be47400SFrançois Tigeot 	(vgpu_hidden_gmadr_base(vgpu) + vgpu_hidden_sz(vgpu) - 1)
3564be47400SFrançois Tigeot 
3574be47400SFrançois Tigeot #define vgpu_fence_base(vgpu) (vgpu->fence.base)
3584be47400SFrançois Tigeot #define vgpu_fence_sz(vgpu) (vgpu->fence.size)
3594be47400SFrançois Tigeot 
3604be47400SFrançois Tigeot struct intel_vgpu_creation_params {
3614be47400SFrançois Tigeot 	__u64 handle;
3624be47400SFrançois Tigeot 	__u64 low_gm_sz;  /* in MB */
3634be47400SFrançois Tigeot 	__u64 high_gm_sz; /* in MB */
3644be47400SFrançois Tigeot 	__u64 fence_sz;
365a85cb24fSFrançois Tigeot 	__u64 resolution;
3664be47400SFrançois Tigeot 	__s32 primary;
3674be47400SFrançois Tigeot 	__u64 vgpu_id;
368a85cb24fSFrançois Tigeot 
369a85cb24fSFrançois Tigeot 	__u32 weight;
3704be47400SFrançois Tigeot };
3714be47400SFrançois Tigeot 
3724be47400SFrançois Tigeot int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
3734be47400SFrançois Tigeot 			      struct intel_vgpu_creation_params *param);
3744be47400SFrançois Tigeot void intel_vgpu_reset_resource(struct intel_vgpu *vgpu);
3754be47400SFrançois Tigeot void intel_vgpu_free_resource(struct intel_vgpu *vgpu);
3764be47400SFrançois Tigeot void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
3774be47400SFrançois Tigeot 	u32 fence, u64 value);
3784be47400SFrançois Tigeot 
3794be47400SFrançois Tigeot /* Macros for easily accessing vGPU virtual/shadow register */
3804be47400SFrançois Tigeot #define vgpu_vreg(vgpu, reg) \
3814be47400SFrançois Tigeot 	(*(u32 *)(vgpu->mmio.vreg + INTEL_GVT_MMIO_OFFSET(reg)))
3824be47400SFrançois Tigeot #define vgpu_vreg8(vgpu, reg) \
3834be47400SFrançois Tigeot 	(*(u8 *)(vgpu->mmio.vreg + INTEL_GVT_MMIO_OFFSET(reg)))
3844be47400SFrançois Tigeot #define vgpu_vreg16(vgpu, reg) \
3854be47400SFrançois Tigeot 	(*(u16 *)(vgpu->mmio.vreg + INTEL_GVT_MMIO_OFFSET(reg)))
3864be47400SFrançois Tigeot #define vgpu_vreg64(vgpu, reg) \
3874be47400SFrançois Tigeot 	(*(u64 *)(vgpu->mmio.vreg + INTEL_GVT_MMIO_OFFSET(reg)))
3884be47400SFrançois Tigeot #define vgpu_sreg(vgpu, reg) \
3894be47400SFrançois Tigeot 	(*(u32 *)(vgpu->mmio.sreg + INTEL_GVT_MMIO_OFFSET(reg)))
3904be47400SFrançois Tigeot #define vgpu_sreg8(vgpu, reg) \
3914be47400SFrançois Tigeot 	(*(u8 *)(vgpu->mmio.sreg + INTEL_GVT_MMIO_OFFSET(reg)))
3924be47400SFrançois Tigeot #define vgpu_sreg16(vgpu, reg) \
3934be47400SFrançois Tigeot 	(*(u16 *)(vgpu->mmio.sreg + INTEL_GVT_MMIO_OFFSET(reg)))
3944be47400SFrançois Tigeot #define vgpu_sreg64(vgpu, reg) \
3954be47400SFrançois Tigeot 	(*(u64 *)(vgpu->mmio.sreg + INTEL_GVT_MMIO_OFFSET(reg)))
3964be47400SFrançois Tigeot 
3974be47400SFrançois Tigeot #define for_each_active_vgpu(gvt, vgpu, id) \
3984be47400SFrançois Tigeot 	idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) \
3994be47400SFrançois Tigeot 		for_each_if(vgpu->active)
4004be47400SFrançois Tigeot 
intel_vgpu_write_pci_bar(struct intel_vgpu * vgpu,u32 offset,u32 val,bool low)4014be47400SFrançois Tigeot static inline void intel_vgpu_write_pci_bar(struct intel_vgpu *vgpu,
4024be47400SFrançois Tigeot 					    u32 offset, u32 val, bool low)
4034be47400SFrançois Tigeot {
4044be47400SFrançois Tigeot 	u32 *pval;
4054be47400SFrançois Tigeot 
4064be47400SFrançois Tigeot 	/* BAR offset should be 32 bits algiend */
4074be47400SFrançois Tigeot 	offset = rounddown(offset, 4);
4084be47400SFrançois Tigeot 	pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
4094be47400SFrançois Tigeot 
4104be47400SFrançois Tigeot 	if (low) {
4114be47400SFrançois Tigeot 		/*
4124be47400SFrançois Tigeot 		 * only update bit 31 - bit 4,
4134be47400SFrançois Tigeot 		 * leave the bit 3 - bit 0 unchanged.
4144be47400SFrançois Tigeot 		 */
4154be47400SFrançois Tigeot 		*pval = (val & GENMASK(31, 4)) | (*pval & GENMASK(3, 0));
4164be47400SFrançois Tigeot 	} else {
4174be47400SFrançois Tigeot 		*pval = val;
4184be47400SFrançois Tigeot 	}
4194be47400SFrançois Tigeot }
4204be47400SFrançois Tigeot 
4214be47400SFrançois Tigeot int intel_gvt_init_vgpu_types(struct intel_gvt *gvt);
4224be47400SFrançois Tigeot void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt);
4234be47400SFrançois Tigeot 
424a85cb24fSFrançois Tigeot struct intel_vgpu *intel_gvt_create_idle_vgpu(struct intel_gvt *gvt);
425a85cb24fSFrançois Tigeot void intel_gvt_destroy_idle_vgpu(struct intel_vgpu *vgpu);
4264be47400SFrançois Tigeot struct intel_vgpu *intel_gvt_create_vgpu(struct intel_gvt *gvt,
4274be47400SFrançois Tigeot 					 struct intel_vgpu_type *type);
4284be47400SFrançois Tigeot void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu);
4294be47400SFrançois Tigeot void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
4304be47400SFrançois Tigeot 				 unsigned int engine_mask);
4314be47400SFrançois Tigeot void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
432a85cb24fSFrançois Tigeot void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
433a85cb24fSFrançois Tigeot void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
4344be47400SFrançois Tigeot 
4354be47400SFrançois Tigeot /* validating GM functions */
4364be47400SFrançois Tigeot #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
4374be47400SFrançois Tigeot 	((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
4384be47400SFrançois Tigeot 	 (gmadr <= vgpu_aperture_gmadr_end(vgpu)))
4394be47400SFrançois Tigeot 
4404be47400SFrançois Tigeot #define vgpu_gmadr_is_hidden(vgpu, gmadr) \
4414be47400SFrançois Tigeot 	((gmadr >= vgpu_hidden_gmadr_base(vgpu)) && \
4424be47400SFrançois Tigeot 	 (gmadr <= vgpu_hidden_gmadr_end(vgpu)))
4434be47400SFrançois Tigeot 
4444be47400SFrançois Tigeot #define vgpu_gmadr_is_valid(vgpu, gmadr) \
4454be47400SFrançois Tigeot 	 ((vgpu_gmadr_is_aperture(vgpu, gmadr) || \
4464be47400SFrançois Tigeot 	  (vgpu_gmadr_is_hidden(vgpu, gmadr))))
4474be47400SFrançois Tigeot 
4484be47400SFrançois Tigeot #define gvt_gmadr_is_aperture(gvt, gmadr) \
4494be47400SFrançois Tigeot 	 ((gmadr >= gvt_aperture_gmadr_base(gvt)) && \
4504be47400SFrançois Tigeot 	  (gmadr <= gvt_aperture_gmadr_end(gvt)))
4514be47400SFrançois Tigeot 
4524be47400SFrançois Tigeot #define gvt_gmadr_is_hidden(gvt, gmadr) \
4534be47400SFrançois Tigeot 	  ((gmadr >= gvt_hidden_gmadr_base(gvt)) && \
4544be47400SFrançois Tigeot 	   (gmadr <= gvt_hidden_gmadr_end(gvt)))
4554be47400SFrançois Tigeot 
4564be47400SFrançois Tigeot #define gvt_gmadr_is_valid(gvt, gmadr) \
4574be47400SFrançois Tigeot 	  (gvt_gmadr_is_aperture(gvt, gmadr) || \
4584be47400SFrançois Tigeot 	    gvt_gmadr_is_hidden(gvt, gmadr))
4594be47400SFrançois Tigeot 
4604be47400SFrançois Tigeot bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size);
4614be47400SFrançois Tigeot int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr);
4624be47400SFrançois Tigeot int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr);
4634be47400SFrançois Tigeot int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
4644be47400SFrançois Tigeot 			     unsigned long *h_index);
4654be47400SFrançois Tigeot int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
4664be47400SFrançois Tigeot 			     unsigned long *g_index);
4674be47400SFrançois Tigeot 
4684be47400SFrançois Tigeot void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
4694be47400SFrançois Tigeot 		bool primary);
4704be47400SFrançois Tigeot void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu);
4714be47400SFrançois Tigeot 
4724be47400SFrançois Tigeot int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
4734be47400SFrançois Tigeot 		void *p_data, unsigned int bytes);
4744be47400SFrançois Tigeot 
4754be47400SFrançois Tigeot int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
4764be47400SFrançois Tigeot 		void *p_data, unsigned int bytes);
4774be47400SFrançois Tigeot 
intel_vgpu_get_bar_gpa(struct intel_vgpu * vgpu,int bar)478*3f2dd94aSFrançois Tigeot static inline u64 intel_vgpu_get_bar_gpa(struct intel_vgpu *vgpu, int bar)
479*3f2dd94aSFrançois Tigeot {
480*3f2dd94aSFrançois Tigeot 	/* We are 64bit bar. */
481*3f2dd94aSFrançois Tigeot 	return (*(u64 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
482*3f2dd94aSFrançois Tigeot 			PCI_BASE_ADDRESS_MEM_MASK;
483*3f2dd94aSFrançois Tigeot }
484*3f2dd94aSFrançois Tigeot 
4854be47400SFrançois Tigeot void intel_gvt_clean_opregion(struct intel_gvt *gvt);
4864be47400SFrançois Tigeot int intel_gvt_init_opregion(struct intel_gvt *gvt);
4874be47400SFrançois Tigeot 
4884be47400SFrançois Tigeot void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu);
4894be47400SFrançois Tigeot int intel_vgpu_init_opregion(struct intel_vgpu *vgpu, u32 gpa);
4904be47400SFrançois Tigeot 
4914be47400SFrançois Tigeot int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci);
4924be47400SFrançois Tigeot void populate_pvinfo_page(struct intel_vgpu *vgpu);
4934be47400SFrançois Tigeot 
494*3f2dd94aSFrançois Tigeot int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
495*3f2dd94aSFrançois Tigeot 
4964be47400SFrançois Tigeot struct intel_gvt_ops {
4974be47400SFrançois Tigeot 	int (*emulate_cfg_read)(struct intel_vgpu *, unsigned int, void *,
4984be47400SFrançois Tigeot 				unsigned int);
4994be47400SFrançois Tigeot 	int (*emulate_cfg_write)(struct intel_vgpu *, unsigned int, void *,
5004be47400SFrançois Tigeot 				unsigned int);
5014be47400SFrançois Tigeot 	int (*emulate_mmio_read)(struct intel_vgpu *, u64, void *,
5024be47400SFrançois Tigeot 				unsigned int);
5034be47400SFrançois Tigeot 	int (*emulate_mmio_write)(struct intel_vgpu *, u64, void *,
5044be47400SFrançois Tigeot 				unsigned int);
5054be47400SFrançois Tigeot 	struct intel_vgpu *(*vgpu_create)(struct intel_gvt *,
5064be47400SFrançois Tigeot 				struct intel_vgpu_type *);
5074be47400SFrançois Tigeot 	void (*vgpu_destroy)(struct intel_vgpu *);
5084be47400SFrançois Tigeot 	void (*vgpu_reset)(struct intel_vgpu *);
509a85cb24fSFrançois Tigeot 	void (*vgpu_activate)(struct intel_vgpu *);
510a85cb24fSFrançois Tigeot 	void (*vgpu_deactivate)(struct intel_vgpu *);
5114be47400SFrançois Tigeot };
5124be47400SFrançois Tigeot 
5134be47400SFrançois Tigeot 
514a85cb24fSFrançois Tigeot enum {
515a85cb24fSFrançois Tigeot 	GVT_FAILSAFE_UNSUPPORTED_GUEST,
516a85cb24fSFrançois Tigeot 	GVT_FAILSAFE_INSUFFICIENT_RESOURCE,
517a85cb24fSFrançois Tigeot };
518a85cb24fSFrançois Tigeot 
mmio_hw_access_pre(struct drm_i915_private * dev_priv)519*3f2dd94aSFrançois Tigeot static inline void mmio_hw_access_pre(struct drm_i915_private *dev_priv)
520*3f2dd94aSFrançois Tigeot {
521*3f2dd94aSFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
522*3f2dd94aSFrançois Tigeot }
523*3f2dd94aSFrançois Tigeot 
mmio_hw_access_post(struct drm_i915_private * dev_priv)524*3f2dd94aSFrançois Tigeot static inline void mmio_hw_access_post(struct drm_i915_private *dev_priv)
525*3f2dd94aSFrançois Tigeot {
526*3f2dd94aSFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
527*3f2dd94aSFrançois Tigeot }
528*3f2dd94aSFrançois Tigeot 
529*3f2dd94aSFrançois Tigeot /**
530*3f2dd94aSFrançois Tigeot  * intel_gvt_mmio_set_accessed - mark a MMIO has been accessed
531*3f2dd94aSFrançois Tigeot  * @gvt: a GVT device
532*3f2dd94aSFrançois Tigeot  * @offset: register offset
533*3f2dd94aSFrançois Tigeot  *
534*3f2dd94aSFrançois Tigeot  */
intel_gvt_mmio_set_accessed(struct intel_gvt * gvt,unsigned int offset)535*3f2dd94aSFrançois Tigeot static inline void intel_gvt_mmio_set_accessed(
536*3f2dd94aSFrançois Tigeot 			struct intel_gvt *gvt, unsigned int offset)
537*3f2dd94aSFrançois Tigeot {
538*3f2dd94aSFrançois Tigeot 	gvt->mmio.mmio_attribute[offset >> 2] |= F_ACCESSED;
539*3f2dd94aSFrançois Tigeot }
540*3f2dd94aSFrançois Tigeot 
541*3f2dd94aSFrançois Tigeot /**
542*3f2dd94aSFrançois Tigeot  * intel_gvt_mmio_is_cmd_accessed - mark a MMIO could be accessed by command
543*3f2dd94aSFrançois Tigeot  * @gvt: a GVT device
544*3f2dd94aSFrançois Tigeot  * @offset: register offset
545*3f2dd94aSFrançois Tigeot  *
546*3f2dd94aSFrançois Tigeot  */
intel_gvt_mmio_is_cmd_access(struct intel_gvt * gvt,unsigned int offset)547*3f2dd94aSFrançois Tigeot static inline bool intel_gvt_mmio_is_cmd_access(
548*3f2dd94aSFrançois Tigeot 			struct intel_gvt *gvt, unsigned int offset)
549*3f2dd94aSFrançois Tigeot {
550*3f2dd94aSFrançois Tigeot 	return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_ACCESS;
551*3f2dd94aSFrançois Tigeot }
552*3f2dd94aSFrançois Tigeot 
553*3f2dd94aSFrançois Tigeot /**
554*3f2dd94aSFrançois Tigeot  * intel_gvt_mmio_is_unalign - mark a MMIO could be accessed unaligned
555*3f2dd94aSFrançois Tigeot  * @gvt: a GVT device
556*3f2dd94aSFrançois Tigeot  * @offset: register offset
557*3f2dd94aSFrançois Tigeot  *
558*3f2dd94aSFrançois Tigeot  */
intel_gvt_mmio_is_unalign(struct intel_gvt * gvt,unsigned int offset)559*3f2dd94aSFrançois Tigeot static inline bool intel_gvt_mmio_is_unalign(
560*3f2dd94aSFrançois Tigeot 			struct intel_gvt *gvt, unsigned int offset)
561*3f2dd94aSFrançois Tigeot {
562*3f2dd94aSFrançois Tigeot 	return gvt->mmio.mmio_attribute[offset >> 2] & F_UNALIGN;
563*3f2dd94aSFrançois Tigeot }
564*3f2dd94aSFrançois Tigeot 
565*3f2dd94aSFrançois Tigeot /**
566*3f2dd94aSFrançois Tigeot  * intel_gvt_mmio_set_cmd_accessed - mark a MMIO has been accessed by command
567*3f2dd94aSFrançois Tigeot  * @gvt: a GVT device
568*3f2dd94aSFrançois Tigeot  * @offset: register offset
569*3f2dd94aSFrançois Tigeot  *
570*3f2dd94aSFrançois Tigeot  */
intel_gvt_mmio_set_cmd_accessed(struct intel_gvt * gvt,unsigned int offset)571*3f2dd94aSFrançois Tigeot static inline void intel_gvt_mmio_set_cmd_accessed(
572*3f2dd94aSFrançois Tigeot 			struct intel_gvt *gvt, unsigned int offset)
573*3f2dd94aSFrançois Tigeot {
574*3f2dd94aSFrançois Tigeot 	gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_ACCESSED;
575*3f2dd94aSFrançois Tigeot }
576*3f2dd94aSFrançois Tigeot 
577*3f2dd94aSFrançois Tigeot /**
578*3f2dd94aSFrançois Tigeot  * intel_gvt_mmio_has_mode_mask - if a MMIO has a mode mask
579*3f2dd94aSFrançois Tigeot  * @gvt: a GVT device
580*3f2dd94aSFrançois Tigeot  * @offset: register offset
581*3f2dd94aSFrançois Tigeot  *
582*3f2dd94aSFrançois Tigeot  * Returns:
583*3f2dd94aSFrançois Tigeot  * True if a MMIO has a mode mask in its higher 16 bits, false if it isn't.
584*3f2dd94aSFrançois Tigeot  *
585*3f2dd94aSFrançois Tigeot  */
intel_gvt_mmio_has_mode_mask(struct intel_gvt * gvt,unsigned int offset)586*3f2dd94aSFrançois Tigeot static inline bool intel_gvt_mmio_has_mode_mask(
587*3f2dd94aSFrançois Tigeot 			struct intel_gvt *gvt, unsigned int offset)
588*3f2dd94aSFrançois Tigeot {
589*3f2dd94aSFrançois Tigeot 	return gvt->mmio.mmio_attribute[offset >> 2] & F_MODE_MASK;
590*3f2dd94aSFrançois Tigeot }
591*3f2dd94aSFrançois Tigeot 
592*3f2dd94aSFrançois Tigeot #include "trace.h"
5931487f786SFrançois Tigeot #include "mpt.h"
5941487f786SFrançois Tigeot 
5951487f786SFrançois Tigeot #endif
596