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