gtt.h 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Zhi Wang <zhi.a.wang@intel.com>
 *    Zhenyu Wang <zhenyuw@linux.intel.com>
 *    Xiao Zheng <xiao.zheng@intel.com>
 *
 * Contributors:
 *    Min He <min.he@intel.com>
 *    Bing Niu <bing.niu@intel.com>
 *
 */

#ifndef _GVT_GTT_H_
#define _GVT_GTT_H_

Z
Zhi Wang 已提交
37 38
#define I915_GTT_PAGE_SHIFT         12
#define I915_GTT_PAGE_MASK		(~(I915_GTT_PAGE_SIZE - 1))
39 40 41 42 43 44 45 46 47 48 49 50

struct intel_vgpu_mm;

#define INTEL_GVT_GTT_HASH_BITS 8
#define INTEL_GVT_INVALID_ADDR (~0UL)

struct intel_gvt_gtt_entry {
	u64 val64;
	int type;
};

struct intel_gvt_gtt_pte_ops {
51 52 53 54 55 56 57 58 59 60 61 62
	int (*get_entry)(void *pt,
			 struct intel_gvt_gtt_entry *e,
			 unsigned long index,
			 bool hypervisor_access,
			 unsigned long gpa,
			 struct intel_vgpu *vgpu);
	int (*set_entry)(void *pt,
			 struct intel_gvt_gtt_entry *e,
			 unsigned long index,
			 bool hypervisor_access,
			 unsigned long gpa,
			 struct intel_vgpu *vgpu);
63 64
	bool (*test_present)(struct intel_gvt_gtt_entry *e);
	void (*clear_present)(struct intel_gvt_gtt_entry *e);
65
	void (*set_present)(struct intel_gvt_gtt_entry *e);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	bool (*test_pse)(struct intel_gvt_gtt_entry *e);
	void (*set_pfn)(struct intel_gvt_gtt_entry *e, unsigned long pfn);
	unsigned long (*get_pfn)(struct intel_gvt_gtt_entry *e);
};

struct intel_gvt_gtt_gma_ops {
	unsigned long (*gma_to_ggtt_pte_index)(unsigned long gma);
	unsigned long (*gma_to_pte_index)(unsigned long gma);
	unsigned long (*gma_to_pde_index)(unsigned long gma);
	unsigned long (*gma_to_l3_pdp_index)(unsigned long gma);
	unsigned long (*gma_to_l4_pdp_index)(unsigned long gma);
	unsigned long (*gma_to_pml4_index)(unsigned long gma);
};

struct intel_gvt_gtt {
	struct intel_gvt_gtt_pte_ops *pte_ops;
	struct intel_gvt_gtt_gma_ops *gma_ops;
	int (*mm_alloc_page_table)(struct intel_vgpu_mm *mm);
	void (*mm_free_page_table)(struct intel_vgpu_mm *mm);
	struct list_head oos_page_use_list_head;
	struct list_head oos_page_free_list_head;
87
	struct list_head ppgtt_mm_lru_list_head;
88

89 90
	struct page *scratch_page;
	unsigned long scratch_mfn;
91 92
};

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
typedef enum {
	GTT_TYPE_INVALID = -1,

	GTT_TYPE_GGTT_PTE,

	GTT_TYPE_PPGTT_PTE_4K_ENTRY,
	GTT_TYPE_PPGTT_PTE_2M_ENTRY,
	GTT_TYPE_PPGTT_PTE_1G_ENTRY,

	GTT_TYPE_PPGTT_PTE_ENTRY,

	GTT_TYPE_PPGTT_PDE_ENTRY,
	GTT_TYPE_PPGTT_PDP_ENTRY,
	GTT_TYPE_PPGTT_PML4_ENTRY,

	GTT_TYPE_PPGTT_ROOT_ENTRY,

	GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
	GTT_TYPE_PPGTT_ROOT_L4_ENTRY,

	GTT_TYPE_PPGTT_ENTRY,

	GTT_TYPE_PPGTT_PTE_PT,
	GTT_TYPE_PPGTT_PDE_PT,
	GTT_TYPE_PPGTT_PDP_PT,
	GTT_TYPE_PPGTT_PML4_PT,

	GTT_TYPE_MAX,
} intel_gvt_gtt_type_t;

123 124 125 126
enum intel_gvt_mm_type {
	INTEL_GVT_MM_GGTT,
	INTEL_GVT_MM_PPGTT,
};
127

128
#define GVT_RING_CTX_NR_PDPS	GEN8_3LVL_PDPES
129

130 131 132
struct intel_vgpu_mm {
	enum intel_gvt_mm_type type;
	struct intel_vgpu *vgpu;
133 134 135

	struct kref ref;
	atomic_t pincount;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	union {
		struct {
			intel_gvt_gtt_type_t root_entry_type;
			/*
			 * The 4 PDPs in ring context. For 48bit addressing,
			 * only PDP0 is valid and point to PML4. For 32it
			 * addressing, all 4 are used as true PDPs.
			 */
			u64 guest_pdps[GVT_RING_CTX_NR_PDPS];
			u64 shadow_pdps[GVT_RING_CTX_NR_PDPS];
			bool shadowed;

			struct list_head list;
			struct list_head lru_list;
		} ppgtt_mm;
		struct {
			void *virtual_ggtt;
		} ggtt_mm;
	};
156 157
};

158 159
struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
		intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

static inline void intel_vgpu_mm_get(struct intel_vgpu_mm *mm)
{
	kref_get(&mm->ref);
}

void _intel_vgpu_mm_release(struct kref *mm_ref);

static inline void intel_vgpu_mm_put(struct intel_vgpu_mm *mm)
{
	kref_put(&mm->ref, _intel_vgpu_mm_release);
}

static inline void intel_vgpu_destroy_mm(struct intel_vgpu_mm *mm)
{
	intel_vgpu_mm_put(mm);
}
177 178 179

struct intel_vgpu_guest_page;

180
struct intel_vgpu_scratch_pt {
181
	struct page *page;
182
	unsigned long page_mfn;
183 184
};

185 186 187
struct intel_vgpu_gtt {
	struct intel_vgpu_mm *ggtt_mm;
	unsigned long active_ppgtt_mm_bitmap;
188
	struct list_head ppgtt_mm_list_head;
189
	DECLARE_HASHTABLE(shadow_page_hash_table, INTEL_GVT_GTT_HASH_BITS);
190 191
	DECLARE_HASHTABLE(tracked_guest_page_hash_table, INTEL_GVT_GTT_HASH_BITS);
	atomic_t n_tracked_guest_page;
192 193
	struct list_head oos_page_list_head;
	struct list_head post_shadow_list_head;
194
	struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
195 196 197 198
};

extern int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
extern void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
199
void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu);
200 201

extern int intel_gvt_init_gtt(struct intel_gvt *gvt);
202
void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu);
203 204 205 206 207 208 209
extern void intel_gvt_clean_gtt(struct intel_gvt *gvt);

extern struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
		int page_table_level, void *root_entry);

struct intel_vgpu_oos_page;

210 211 212 213 214 215 216 217
struct intel_vgpu_shadow_page {
	void *vaddr;
	struct page *page;
	int type;
	struct hlist_node node;
	unsigned long mfn;
};

218
struct intel_vgpu_page_track {
219
	struct hlist_node node;
220
	bool tracked;
221 222 223
	unsigned long gfn;
	int (*handler)(void *, u64, void *, int);
	void *data;
224 225 226 227
};

struct intel_vgpu_guest_page {
	struct intel_vgpu_page_track track;
228 229 230 231 232 233 234 235 236
	unsigned long write_cnt;
	struct intel_vgpu_oos_page *oos_page;
};

struct intel_vgpu_oos_page {
	struct intel_vgpu_guest_page *guest_page;
	struct list_head list;
	struct list_head vm_list;
	int id;
Z
Zhi Wang 已提交
237
	unsigned char mem[I915_GTT_PAGE_SIZE];
238 239 240 241 242 243 244 245 246 247 248 249 250 251
};

#define GTT_ENTRY_NUM_IN_ONE_PAGE 512

struct intel_vgpu_ppgtt_spt {
	struct intel_vgpu_shadow_page shadow_page;
	struct intel_vgpu_guest_page guest_page;
	int guest_page_type;
	atomic_t refcount;
	struct intel_vgpu *vgpu;
	DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
	struct list_head post_shadow_list;
};

252 253
int intel_vgpu_init_page_track(struct intel_vgpu *vgpu,
		struct intel_vgpu_page_track *t,
254 255 256 257
		unsigned long gfn,
		int (*handler)(void *gp, u64, void *, int),
		void *data);

258 259
void intel_vgpu_clean_page_track(struct intel_vgpu *vgpu,
		struct intel_vgpu_page_track *t);
260

261
struct intel_vgpu_page_track *intel_vgpu_find_tracked_page(
262 263 264 265 266 267 268 269 270 271 272 273 274 275
		struct intel_vgpu *vgpu, unsigned long gfn);

int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);

int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);

int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);

void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);

unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
		unsigned long gma);

struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
276
		u64 pdps[]);
277 278

int intel_vgpu_g2v_create_ppgtt_mm(struct intel_vgpu *vgpu,
279
		intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
280

281
int intel_vgpu_g2v_destroy_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
282

283
int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
284 285
	unsigned int off, void *p_data, unsigned int bytes);

286
int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
287 288
	unsigned int off, void *p_data, unsigned int bytes);

289 290 291
int intel_vgpu_write_protect_handler(struct intel_vgpu *vgpu, u64 pa,
				     void *p_data, unsigned int bytes);

292
#endif /* _GVT_GTT_H_ */