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>
39 * DOC: Hypervisor Service APIs for GVT-g Core Logic
41 * This is the glue layer between specific hypervisor MPT modules and GVT-g core
42 * logic. Each kind of hypervisor MPT module provides a collection of function
43 * callbacks and will be attached to GVT host when the driver is loading.
44 * GVT-g core logic will call these APIs to request specific services from
49 * intel_gvt_hypervisor_host_init - init GVT-g host side
52 * Zero on success, negative error code if failed
54 static inline int intel_gvt_hypervisor_host_init(struct device *dev,
55 void *gvt, const void *ops)
57 if (!intel_gvt_host.mpt->host_init)
60 return intel_gvt_host.mpt->host_init(dev, gvt, ops);
64 * intel_gvt_hypervisor_host_exit - exit GVT-g host side
66 static inline void intel_gvt_hypervisor_host_exit(struct device *dev, void *gvt)
68 /* optional to provide */
69 if (!intel_gvt_host.mpt->host_exit)
72 intel_gvt_host.mpt->host_exit(dev, gvt);
76 * intel_gvt_hypervisor_attach_vgpu - call hypervisor to initialize vGPU
77 * related stuffs inside hypervisor.
80 * Zero on success, negative error code if failed.
82 static inline int intel_gvt_hypervisor_attach_vgpu(struct intel_vgpu *vgpu)
84 /* optional to provide */
85 if (!intel_gvt_host.mpt->attach_vgpu)
88 return intel_gvt_host.mpt->attach_vgpu(vgpu, &vgpu->handle);
92 * intel_gvt_hypervisor_detach_vgpu - call hypervisor to release vGPU
93 * related stuffs inside hypervisor.
96 * Zero on success, negative error code if failed.
98 static inline void intel_gvt_hypervisor_detach_vgpu(struct intel_vgpu *vgpu)
100 /* optional to provide */
101 if (!intel_gvt_host.mpt->detach_vgpu)
104 intel_gvt_host.mpt->detach_vgpu(vgpu);
107 #define MSI_CAP_CONTROL(offset) (offset + 2)
108 #define MSI_CAP_ADDRESS(offset) (offset + 4)
109 #define MSI_CAP_DATA(offset) (offset + 8)
110 #define MSI_CAP_EN 0x1
113 * intel_gvt_hypervisor_inject_msi - inject a MSI interrupt into vGPU
116 * Zero on success, negative error code if failed.
118 static inline int intel_gvt_hypervisor_inject_msi(struct intel_vgpu *vgpu)
120 unsigned long offset = vgpu->gvt->device_info.msi_cap_offset;
125 control = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_CONTROL(offset));
126 addr = *(u32 *)(vgpu_cfg_space(vgpu) + MSI_CAP_ADDRESS(offset));
127 data = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_DATA(offset));
129 /* Do not generate MSI if MSIEN is disable */
130 if (!(control & MSI_CAP_EN))
133 if (WARN(control & GENMASK(15, 1), "only support one MSI format\n"))
136 trace_inject_msi(vgpu->id, addr, data);
138 ret = intel_gvt_host.mpt->inject_msi(vgpu->handle, addr, data);
145 * intel_gvt_hypervisor_set_wp_page - translate a host VA into MFN
146 * @p: host kernel virtual address
149 * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
151 static inline unsigned long intel_gvt_hypervisor_virt_to_mfn(void *p)
153 return intel_gvt_host.mpt->from_virt_to_mfn(p);
157 * intel_gvt_hypervisor_enable_page_track - track a guest page
159 * @gfn: the gfn of guest
162 * Zero on success, negative error code if failed.
164 static inline int intel_gvt_hypervisor_enable_page_track(
165 struct intel_vgpu *vgpu, unsigned long gfn)
167 return intel_gvt_host.mpt->enable_page_track(vgpu->handle, gfn);
171 * intel_gvt_hypervisor_disable_page_track - untrack a guest page
173 * @gfn: the gfn of guest
176 * Zero on success, negative error code if failed.
178 static inline int intel_gvt_hypervisor_disable_page_track(
179 struct intel_vgpu *vgpu, unsigned long gfn)
181 return intel_gvt_host.mpt->disable_page_track(vgpu->handle, gfn);
185 * intel_gvt_hypervisor_read_gpa - copy data from GPA to host data buffer
187 * @gpa: guest physical address
188 * @buf: host data buffer
192 * Zero on success, negative error code if failed.
194 static inline int intel_gvt_hypervisor_read_gpa(struct intel_vgpu *vgpu,
195 unsigned long gpa, void *buf, unsigned long len)
197 return intel_gvt_host.mpt->read_gpa(vgpu->handle, gpa, buf, len);
201 * intel_gvt_hypervisor_write_gpa - copy data from host data buffer to GPA
203 * @gpa: guest physical address
204 * @buf: host data buffer
208 * Zero on success, negative error code if failed.
210 static inline int intel_gvt_hypervisor_write_gpa(struct intel_vgpu *vgpu,
211 unsigned long gpa, void *buf, unsigned long len)
213 return intel_gvt_host.mpt->write_gpa(vgpu->handle, gpa, buf, len);
217 * intel_gvt_hypervisor_gfn_to_mfn - translate a GFN to MFN
222 * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
224 static inline unsigned long intel_gvt_hypervisor_gfn_to_mfn(
225 struct intel_vgpu *vgpu, unsigned long gfn)
227 return intel_gvt_host.mpt->gfn_to_mfn(vgpu->handle, gfn);
231 * intel_gvt_hypervisor_dma_map_guest_page - setup dma map for guest page
235 * @dma_addr: retrieve allocated dma addr
238 * 0 on success, negative error code if failed.
240 static inline int intel_gvt_hypervisor_dma_map_guest_page(
241 struct intel_vgpu *vgpu, unsigned long gfn, unsigned long size,
242 dma_addr_t *dma_addr)
244 return intel_gvt_host.mpt->dma_map_guest_page(vgpu->handle, gfn, size,
249 * intel_gvt_hypervisor_dma_unmap_guest_page - cancel dma map for guest page
251 * @dma_addr: the mapped dma addr
253 static inline void intel_gvt_hypervisor_dma_unmap_guest_page(
254 struct intel_vgpu *vgpu, dma_addr_t dma_addr)
256 intel_gvt_host.mpt->dma_unmap_guest_page(vgpu->handle, dma_addr);
260 * intel_gvt_hypervisor_dma_pin_guest_page - pin guest dma buf
262 * @dma_addr: guest dma addr
265 * 0 on success, negative error code if failed.
268 intel_gvt_hypervisor_dma_pin_guest_page(struct intel_vgpu *vgpu,
271 return intel_gvt_host.mpt->dma_pin_guest_page(vgpu->handle, dma_addr);
275 * intel_gvt_hypervisor_map_gfn_to_mfn - map a GFN region to MFN
279 * @nr: amount of PFNs
283 * Zero on success, negative error code if failed.
285 static inline int intel_gvt_hypervisor_map_gfn_to_mfn(
286 struct intel_vgpu *vgpu, unsigned long gfn,
287 unsigned long mfn, unsigned int nr,
290 /* a MPT implementation could have MMIO mapped elsewhere */
291 if (!intel_gvt_host.mpt->map_gfn_to_mfn)
294 return intel_gvt_host.mpt->map_gfn_to_mfn(vgpu->handle, gfn, mfn, nr,
299 * intel_gvt_hypervisor_set_trap_area - Trap a guest PA region
301 * @start: the beginning of the guest physical address region
302 * @end: the end of the guest physical address region
306 * Zero on success, negative error code if failed.
308 static inline int intel_gvt_hypervisor_set_trap_area(
309 struct intel_vgpu *vgpu, u64 start, u64 end, bool map)
311 /* a MPT implementation could have MMIO trapped elsewhere */
312 if (!intel_gvt_host.mpt->set_trap_area)
315 return intel_gvt_host.mpt->set_trap_area(vgpu->handle, start, end, map);
319 * intel_gvt_hypervisor_set_opregion - Set opregion for guest
323 * Zero on success, negative error code if failed.
325 static inline int intel_gvt_hypervisor_set_opregion(struct intel_vgpu *vgpu)
327 if (!intel_gvt_host.mpt->set_opregion)
330 return intel_gvt_host.mpt->set_opregion(vgpu);
334 * intel_gvt_hypervisor_set_edid - Set EDID region for guest
336 * @port_num: display port number
339 * Zero on success, negative error code if failed.
341 static inline int intel_gvt_hypervisor_set_edid(struct intel_vgpu *vgpu,
344 if (!intel_gvt_host.mpt->set_edid)
347 return intel_gvt_host.mpt->set_edid(vgpu, port_num);
351 * intel_gvt_hypervisor_get_vfio_device - increase vfio device ref count
355 * Zero on success, negative error code if failed.
357 static inline int intel_gvt_hypervisor_get_vfio_device(struct intel_vgpu *vgpu)
359 if (!intel_gvt_host.mpt->get_vfio_device)
362 return intel_gvt_host.mpt->get_vfio_device(vgpu);
366 * intel_gvt_hypervisor_put_vfio_device - decrease vfio device ref count
370 * Zero on success, negative error code if failed.
372 static inline void intel_gvt_hypervisor_put_vfio_device(struct intel_vgpu *vgpu)
374 if (!intel_gvt_host.mpt->put_vfio_device)
377 intel_gvt_host.mpt->put_vfio_device(vgpu);
381 * intel_gvt_hypervisor_is_valid_gfn - check if a visible gfn
386 * true on valid gfn, false on not.
388 static inline bool intel_gvt_hypervisor_is_valid_gfn(
389 struct intel_vgpu *vgpu, unsigned long gfn)
391 if (!intel_gvt_host.mpt->is_valid_gfn)
394 return intel_gvt_host.mpt->is_valid_gfn(vgpu->handle, gfn);
397 int intel_gvt_register_hypervisor(const struct intel_gvt_mpt *);
398 void intel_gvt_unregister_hypervisor(void);
400 #endif /* _GVT_MPT_H_ */