mpt.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * Eddie Dong <eddie.dong@intel.com>
  25. * Dexuan Cui
  26. * Jike Song <jike.song@intel.com>
  27. *
  28. * Contributors:
  29. * Zhi Wang <zhi.a.wang@intel.com>
  30. *
  31. */
  32. #ifndef _GVT_MPT_H_
  33. #define _GVT_MPT_H_
  34. /**
  35. * DOC: Hypervisor Service APIs for GVT-g Core Logic
  36. *
  37. * This is the glue layer between specific hypervisor MPT modules and GVT-g core
  38. * logic. Each kind of hypervisor MPT module provides a collection of function
  39. * callbacks and will be attached to GVT host when the driver is loading.
  40. * GVT-g core logic will call these APIs to request specific services from
  41. * hypervisor.
  42. */
  43. /**
  44. * intel_gvt_hypervisor_host_init - init GVT-g host side
  45. *
  46. * Returns:
  47. * Zero on success, negative error code if failed
  48. */
  49. static inline int intel_gvt_hypervisor_host_init(struct device *dev,
  50. void *gvt, const void *ops)
  51. {
  52. /* optional to provide */
  53. if (!intel_gvt_host.mpt->host_init)
  54. return 0;
  55. return intel_gvt_host.mpt->host_init(dev, gvt, ops);
  56. }
  57. /**
  58. * intel_gvt_hypervisor_host_exit - exit GVT-g host side
  59. */
  60. static inline void intel_gvt_hypervisor_host_exit(struct device *dev,
  61. void *gvt)
  62. {
  63. /* optional to provide */
  64. if (!intel_gvt_host.mpt->host_exit)
  65. return;
  66. intel_gvt_host.mpt->host_exit(dev, gvt);
  67. }
  68. /**
  69. * intel_gvt_hypervisor_attach_vgpu - call hypervisor to initialize vGPU
  70. * related stuffs inside hypervisor.
  71. *
  72. * Returns:
  73. * Zero on success, negative error code if failed.
  74. */
  75. static inline int intel_gvt_hypervisor_attach_vgpu(struct intel_vgpu *vgpu)
  76. {
  77. /* optional to provide */
  78. if (!intel_gvt_host.mpt->attach_vgpu)
  79. return 0;
  80. return intel_gvt_host.mpt->attach_vgpu(vgpu, &vgpu->handle);
  81. }
  82. /**
  83. * intel_gvt_hypervisor_detach_vgpu - call hypervisor to release vGPU
  84. * related stuffs inside hypervisor.
  85. *
  86. * Returns:
  87. * Zero on success, negative error code if failed.
  88. */
  89. static inline void intel_gvt_hypervisor_detach_vgpu(struct intel_vgpu *vgpu)
  90. {
  91. /* optional to provide */
  92. if (!intel_gvt_host.mpt->detach_vgpu)
  93. return;
  94. intel_gvt_host.mpt->detach_vgpu(vgpu->handle);
  95. }
  96. #define MSI_CAP_CONTROL(offset) (offset + 2)
  97. #define MSI_CAP_ADDRESS(offset) (offset + 4)
  98. #define MSI_CAP_DATA(offset) (offset + 8)
  99. #define MSI_CAP_EN 0x1
  100. /**
  101. * intel_gvt_hypervisor_inject_msi - inject a MSI interrupt into vGPU
  102. *
  103. * Returns:
  104. * Zero on success, negative error code if failed.
  105. */
  106. static inline int intel_gvt_hypervisor_inject_msi(struct intel_vgpu *vgpu)
  107. {
  108. unsigned long offset = vgpu->gvt->device_info.msi_cap_offset;
  109. u16 control, data;
  110. u32 addr;
  111. int ret;
  112. control = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_CONTROL(offset));
  113. addr = *(u32 *)(vgpu_cfg_space(vgpu) + MSI_CAP_ADDRESS(offset));
  114. data = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_DATA(offset));
  115. /* Do not generate MSI if MSIEN is disable */
  116. if (!(control & MSI_CAP_EN))
  117. return 0;
  118. if (WARN(control & GENMASK(15, 1), "only support one MSI format\n"))
  119. return -EINVAL;
  120. trace_inject_msi(vgpu->id, addr, data);
  121. ret = intel_gvt_host.mpt->inject_msi(vgpu->handle, addr, data);
  122. if (ret)
  123. return ret;
  124. return 0;
  125. }
  126. /**
  127. * intel_gvt_hypervisor_set_wp_page - translate a host VA into MFN
  128. * @p: host kernel virtual address
  129. *
  130. * Returns:
  131. * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
  132. */
  133. static inline unsigned long intel_gvt_hypervisor_virt_to_mfn(void *p)
  134. {
  135. return intel_gvt_host.mpt->from_virt_to_mfn(p);
  136. }
  137. /**
  138. * intel_gvt_hypervisor_enable_page_track - track a guest page
  139. * @vgpu: a vGPU
  140. * @gfn: the gfn of guest
  141. *
  142. * Returns:
  143. * Zero on success, negative error code if failed.
  144. */
  145. static inline int intel_gvt_hypervisor_enable_page_track(
  146. struct intel_vgpu *vgpu, unsigned long gfn)
  147. {
  148. return intel_gvt_host.mpt->enable_page_track(vgpu->handle, gfn);
  149. }
  150. /**
  151. * intel_gvt_hypervisor_disable_page_track - untrack a guest page
  152. * @vgpu: a vGPU
  153. * @gfn: the gfn of guest
  154. *
  155. * Returns:
  156. * Zero on success, negative error code if failed.
  157. */
  158. static inline int intel_gvt_hypervisor_disable_page_track(
  159. struct intel_vgpu *vgpu, unsigned long gfn)
  160. {
  161. return intel_gvt_host.mpt->disable_page_track(vgpu->handle, gfn);
  162. }
  163. /**
  164. * intel_gvt_hypervisor_read_gpa - copy data from GPA to host data buffer
  165. * @vgpu: a vGPU
  166. * @gpa: guest physical address
  167. * @buf: host data buffer
  168. * @len: data length
  169. *
  170. * Returns:
  171. * Zero on success, negative error code if failed.
  172. */
  173. static inline int intel_gvt_hypervisor_read_gpa(struct intel_vgpu *vgpu,
  174. unsigned long gpa, void *buf, unsigned long len)
  175. {
  176. return intel_gvt_host.mpt->read_gpa(vgpu->handle, gpa, buf, len);
  177. }
  178. /**
  179. * intel_gvt_hypervisor_write_gpa - copy data from host data buffer to GPA
  180. * @vgpu: a vGPU
  181. * @gpa: guest physical address
  182. * @buf: host data buffer
  183. * @len: data length
  184. *
  185. * Returns:
  186. * Zero on success, negative error code if failed.
  187. */
  188. static inline int intel_gvt_hypervisor_write_gpa(struct intel_vgpu *vgpu,
  189. unsigned long gpa, void *buf, unsigned long len)
  190. {
  191. return intel_gvt_host.mpt->write_gpa(vgpu->handle, gpa, buf, len);
  192. }
  193. /**
  194. * intel_gvt_hypervisor_gfn_to_mfn - translate a GFN to MFN
  195. * @vgpu: a vGPU
  196. * @gpfn: guest pfn
  197. *
  198. * Returns:
  199. * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
  200. */
  201. static inline unsigned long intel_gvt_hypervisor_gfn_to_mfn(
  202. struct intel_vgpu *vgpu, unsigned long gfn)
  203. {
  204. return intel_gvt_host.mpt->gfn_to_mfn(vgpu->handle, gfn);
  205. }
  206. /**
  207. * intel_gvt_hypervisor_dma_map_guest_page - setup dma map for guest page
  208. * @vgpu: a vGPU
  209. * @gpfn: guest pfn
  210. * @dma_addr: retrieve allocated dma addr
  211. *
  212. * Returns:
  213. * 0 on success, negative error code if failed.
  214. */
  215. static inline int intel_gvt_hypervisor_dma_map_guest_page(
  216. struct intel_vgpu *vgpu, unsigned long gfn,
  217. dma_addr_t *dma_addr)
  218. {
  219. return intel_gvt_host.mpt->dma_map_guest_page(vgpu->handle, gfn,
  220. dma_addr);
  221. }
  222. /**
  223. * intel_gvt_hypervisor_dma_unmap_guest_page - cancel dma map for guest page
  224. * @vgpu: a vGPU
  225. * @dma_addr: the mapped dma addr
  226. */
  227. static inline void intel_gvt_hypervisor_dma_unmap_guest_page(
  228. struct intel_vgpu *vgpu, dma_addr_t dma_addr)
  229. {
  230. intel_gvt_host.mpt->dma_unmap_guest_page(vgpu->handle, dma_addr);
  231. }
  232. /**
  233. * intel_gvt_hypervisor_map_gfn_to_mfn - map a GFN region to MFN
  234. * @vgpu: a vGPU
  235. * @gfn: guest PFN
  236. * @mfn: host PFN
  237. * @nr: amount of PFNs
  238. * @map: map or unmap
  239. *
  240. * Returns:
  241. * Zero on success, negative error code if failed.
  242. */
  243. static inline int intel_gvt_hypervisor_map_gfn_to_mfn(
  244. struct intel_vgpu *vgpu, unsigned long gfn,
  245. unsigned long mfn, unsigned int nr,
  246. bool map)
  247. {
  248. /* a MPT implementation could have MMIO mapped elsewhere */
  249. if (!intel_gvt_host.mpt->map_gfn_to_mfn)
  250. return 0;
  251. return intel_gvt_host.mpt->map_gfn_to_mfn(vgpu->handle, gfn, mfn, nr,
  252. map);
  253. }
  254. /**
  255. * intel_gvt_hypervisor_set_trap_area - Trap a guest PA region
  256. * @vgpu: a vGPU
  257. * @start: the beginning of the guest physical address region
  258. * @end: the end of the guest physical address region
  259. * @map: map or unmap
  260. *
  261. * Returns:
  262. * Zero on success, negative error code if failed.
  263. */
  264. static inline int intel_gvt_hypervisor_set_trap_area(
  265. struct intel_vgpu *vgpu, u64 start, u64 end, bool map)
  266. {
  267. /* a MPT implementation could have MMIO trapped elsewhere */
  268. if (!intel_gvt_host.mpt->set_trap_area)
  269. return 0;
  270. return intel_gvt_host.mpt->set_trap_area(vgpu->handle, start, end, map);
  271. }
  272. /**
  273. * intel_gvt_hypervisor_set_opregion - Set opregion for guest
  274. * @vgpu: a vGPU
  275. *
  276. * Returns:
  277. * Zero on success, negative error code if failed.
  278. */
  279. static inline int intel_gvt_hypervisor_set_opregion(struct intel_vgpu *vgpu)
  280. {
  281. if (!intel_gvt_host.mpt->set_opregion)
  282. return 0;
  283. return intel_gvt_host.mpt->set_opregion(vgpu);
  284. }
  285. /**
  286. * intel_gvt_hypervisor_get_vfio_device - increase vfio device ref count
  287. * @vgpu: a vGPU
  288. *
  289. * Returns:
  290. * Zero on success, negative error code if failed.
  291. */
  292. static inline int intel_gvt_hypervisor_get_vfio_device(struct intel_vgpu *vgpu)
  293. {
  294. if (!intel_gvt_host.mpt->get_vfio_device)
  295. return 0;
  296. return intel_gvt_host.mpt->get_vfio_device(vgpu);
  297. }
  298. /**
  299. * intel_gvt_hypervisor_put_vfio_device - decrease vfio device ref count
  300. * @vgpu: a vGPU
  301. *
  302. * Returns:
  303. * Zero on success, negative error code if failed.
  304. */
  305. static inline void intel_gvt_hypervisor_put_vfio_device(struct intel_vgpu *vgpu)
  306. {
  307. if (!intel_gvt_host.mpt->put_vfio_device)
  308. return;
  309. intel_gvt_host.mpt->put_vfio_device(vgpu);
  310. }
  311. /**
  312. * intel_gvt_hypervisor_is_valid_gfn - check if a visible gfn
  313. * @vgpu: a vGPU
  314. * @gfn: guest PFN
  315. *
  316. * Returns:
  317. * true on valid gfn, false on not.
  318. */
  319. static inline bool intel_gvt_hypervisor_is_valid_gfn(
  320. struct intel_vgpu *vgpu, unsigned long gfn)
  321. {
  322. if (!intel_gvt_host.mpt->is_valid_gfn)
  323. return true;
  324. return intel_gvt_host.mpt->is_valid_gfn(vgpu->handle, gfn);
  325. }
  326. #endif /* _GVT_MPT_H_ */