2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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:
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
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
24 * Eddie Dong <eddie.dong@intel.com>
26 * Jike Song <jike.song@intel.com>
29 * Zhi Wang <zhi.a.wang@intel.com>
37 * DOC: Hypervisor Service APIs for GVT-g Core Logic
39 * This is the glue layer between specific hypervisor MPT modules and GVT-g core
40 * logic. Each kind of hypervisor MPT module provides a collection of function
41 * callbacks and will be attached to GVT host when the driver is loading.
42 * GVT-g core logic will call these APIs to request specific services from
47 * intel_gvt_hypervisor_detect_host - check if GVT-g is running within
48 * hypervisor host/privilged domain
51 * Zero on success, -ENODEV if current kernel is running inside a VM
53 static inline int intel_gvt_hypervisor_detect_host(void)
55 return intel_gvt_host.mpt->detect_host();
59 * intel_gvt_hypervisor_attach_vgpu - call hypervisor to initialize vGPU
60 * related stuffs inside hypervisor.
63 * Zero on success, negative error code if failed.
65 static inline int intel_gvt_hypervisor_attach_vgpu(struct intel_vgpu *vgpu)
67 return intel_gvt_host.mpt->attach_vgpu(vgpu, &vgpu->handle);
71 * intel_gvt_hypervisor_detach_vgpu - call hypervisor to release vGPU
72 * related stuffs inside hypervisor.
75 * Zero on success, negative error code if failed.
77 static inline void intel_gvt_hypervisor_detach_vgpu(struct intel_vgpu *vgpu)
79 intel_gvt_host.mpt->detach_vgpu(vgpu->handle);
82 #define MSI_CAP_CONTROL(offset) (offset + 2)
83 #define MSI_CAP_ADDRESS(offset) (offset + 4)
84 #define MSI_CAP_DATA(offset) (offset + 8)
85 #define MSI_CAP_EN 0x1
88 * intel_gvt_hypervisor_inject_msi - inject a MSI interrupt into vGPU
91 * Zero on success, negative error code if failed.
93 static inline int intel_gvt_hypervisor_inject_msi(struct intel_vgpu *vgpu)
95 unsigned long offset = vgpu->gvt->device_info.msi_cap_offset;
100 control = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_CONTROL(offset));
101 addr = *(u32 *)(vgpu_cfg_space(vgpu) + MSI_CAP_ADDRESS(offset));
102 data = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_DATA(offset));
104 /* Do not generate MSI if MSIEN is disable */
105 if (!(control & MSI_CAP_EN))
108 if (WARN(control & GENMASK(15, 1), "only support one MSI format\n"))
111 gvt_dbg_irq("vgpu%d: inject msi address %x data%x\n", vgpu->id, addr,
114 ret = intel_gvt_host.mpt->inject_msi(vgpu->handle, addr, data);
121 * intel_gvt_hypervisor_set_wp_page - translate a host VA into MFN
122 * @p: host kernel virtual address
125 * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
127 static inline unsigned long intel_gvt_hypervisor_virt_to_mfn(void *p)
129 return intel_gvt_host.mpt->from_virt_to_mfn(p);
133 * intel_gvt_hypervisor_set_wp_page - set a guest page to write-protected
135 * @p: intel_vgpu_guest_page
138 * Zero on success, negative error code if failed.
140 static inline int intel_gvt_hypervisor_set_wp_page(struct intel_vgpu *vgpu,
141 struct intel_vgpu_guest_page *p)
145 if (p->writeprotection)
148 ret = intel_gvt_host.mpt->set_wp_page(vgpu->handle, p->gfn);
151 p->writeprotection = true;
152 atomic_inc(&vgpu->gtt.n_write_protected_guest_page);
157 * intel_gvt_hypervisor_unset_wp_page - remove the write-protection of a
160 * @p: intel_vgpu_guest_page
163 * Zero on success, negative error code if failed.
165 static inline int intel_gvt_hypervisor_unset_wp_page(struct intel_vgpu *vgpu,
166 struct intel_vgpu_guest_page *p)
170 if (!p->writeprotection)
173 ret = intel_gvt_host.mpt->unset_wp_page(vgpu->handle, p->gfn);
176 p->writeprotection = false;
177 atomic_dec(&vgpu->gtt.n_write_protected_guest_page);
182 * intel_gvt_hypervisor_read_gpa - copy data from GPA to host data buffer
184 * @gpa: guest physical address
185 * @buf: host data buffer
189 * Zero on success, negative error code if failed.
191 static inline int intel_gvt_hypervisor_read_gpa(struct intel_vgpu *vgpu,
192 unsigned long gpa, void *buf, unsigned long len)
194 return intel_gvt_host.mpt->read_gpa(vgpu->handle, gpa, buf, len);
198 * intel_gvt_hypervisor_write_gpa - copy data from host data buffer to GPA
200 * @gpa: guest physical address
201 * @buf: host data buffer
205 * Zero on success, negative error code if failed.
207 static inline int intel_gvt_hypervisor_write_gpa(struct intel_vgpu *vgpu,
208 unsigned long gpa, void *buf, unsigned long len)
210 return intel_gvt_host.mpt->write_gpa(vgpu->handle, gpa, buf, len);
214 * intel_gvt_hypervisor_gfn_to_mfn - translate a GFN to MFN
219 * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
221 static inline unsigned long intel_gvt_hypervisor_gfn_to_mfn(
222 struct intel_vgpu *vgpu, unsigned long gfn)
224 return intel_gvt_host.mpt->gfn_to_mfn(vgpu->handle, gfn);
228 GVT_MAP_APERTURE = 0,
233 * intel_gvt_hypervisor_map_gfn_to_mfn - map a GFN region to MFN
237 * @nr: amount of PFNs
242 * Zero on success, negative error code if failed.
244 static inline int intel_gvt_hypervisor_map_gfn_to_mfn(
245 struct intel_vgpu *vgpu, unsigned long gfn,
246 unsigned long mfn, unsigned int nr,
249 return intel_gvt_host.mpt->map_gfn_to_mfn(vgpu->handle, gfn, mfn, nr,
254 * intel_gvt_hypervisor_set_trap_area - Trap a guest PA region
256 * @start: the beginning of the guest physical address region
257 * @end: the end of the guest physical address region
261 * Zero on success, negative error code if failed.
263 static inline int intel_gvt_hypervisor_set_trap_area(
264 struct intel_vgpu *vgpu, u64 start, u64 end, bool map)
266 return intel_gvt_host.mpt->set_trap_area(vgpu->handle, start, end, map);
269 #endif /* _GVT_MPT_H_ */