mpt.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_detect_host - check if GVT-g is running within
  45. * hypervisor host/privilged domain
  46. *
  47. * Returns:
  48. * Zero on success, -ENODEV if current kernel is running inside a VM
  49. */
  50. static inline int intel_gvt_hypervisor_detect_host(void)
  51. {
  52. return intel_gvt_host.mpt->detect_host();
  53. }
  54. /**
  55. * intel_gvt_hypervisor_host_init - init GVT-g host side
  56. *
  57. * Returns:
  58. * Zero on success, negative error code if failed
  59. */
  60. static inline int intel_gvt_hypervisor_host_init(struct device *dev,
  61. void *gvt, const void *ops)
  62. {
  63. /* optional to provide */
  64. if (!intel_gvt_host.mpt->host_init)
  65. return 0;
  66. return intel_gvt_host.mpt->host_init(dev, gvt, ops);
  67. }
  68. /**
  69. * intel_gvt_hypervisor_host_exit - exit GVT-g host side
  70. */
  71. static inline void intel_gvt_hypervisor_host_exit(struct device *dev,
  72. void *gvt)
  73. {
  74. /* optional to provide */
  75. if (!intel_gvt_host.mpt->host_exit)
  76. return;
  77. intel_gvt_host.mpt->host_exit(dev, gvt);
  78. }
  79. /**
  80. * intel_gvt_hypervisor_attach_vgpu - call hypervisor to initialize vGPU
  81. * related stuffs inside hypervisor.
  82. *
  83. * Returns:
  84. * Zero on success, negative error code if failed.
  85. */
  86. static inline int intel_gvt_hypervisor_attach_vgpu(struct intel_vgpu *vgpu)
  87. {
  88. /* optional to provide */
  89. if (!intel_gvt_host.mpt->attach_vgpu)
  90. return 0;
  91. return intel_gvt_host.mpt->attach_vgpu(vgpu, &vgpu->handle);
  92. }
  93. /**
  94. * intel_gvt_hypervisor_detach_vgpu - call hypervisor to release vGPU
  95. * related stuffs inside hypervisor.
  96. *
  97. * Returns:
  98. * Zero on success, negative error code if failed.
  99. */
  100. static inline void intel_gvt_hypervisor_detach_vgpu(struct intel_vgpu *vgpu)
  101. {
  102. /* optional to provide */
  103. if (!intel_gvt_host.mpt->detach_vgpu)
  104. return;
  105. intel_gvt_host.mpt->detach_vgpu(vgpu->handle);
  106. }
  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
  111. /**
  112. * intel_gvt_hypervisor_inject_msi - inject a MSI interrupt into vGPU
  113. *
  114. * Returns:
  115. * Zero on success, negative error code if failed.
  116. */
  117. static inline int intel_gvt_hypervisor_inject_msi(struct intel_vgpu *vgpu)
  118. {
  119. unsigned long offset = vgpu->gvt->device_info.msi_cap_offset;
  120. u16 control, data;
  121. u32 addr;
  122. int ret;
  123. control = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_CONTROL(offset));
  124. addr = *(u32 *)(vgpu_cfg_space(vgpu) + MSI_CAP_ADDRESS(offset));
  125. data = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_DATA(offset));
  126. /* Do not generate MSI if MSIEN is disable */
  127. if (!(control & MSI_CAP_EN))
  128. return 0;
  129. if (WARN(control & GENMASK(15, 1), "only support one MSI format\n"))
  130. return -EINVAL;
  131. gvt_dbg_irq("vgpu%d: inject msi address %x data%x\n", vgpu->id, addr,
  132. data);
  133. ret = intel_gvt_host.mpt->inject_msi(vgpu->handle, addr, data);
  134. if (ret)
  135. return ret;
  136. return 0;
  137. }
  138. /**
  139. * intel_gvt_hypervisor_set_wp_page - translate a host VA into MFN
  140. * @p: host kernel virtual address
  141. *
  142. * Returns:
  143. * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
  144. */
  145. static inline unsigned long intel_gvt_hypervisor_virt_to_mfn(void *p)
  146. {
  147. return intel_gvt_host.mpt->from_virt_to_mfn(p);
  148. }
  149. /**
  150. * intel_gvt_hypervisor_set_wp_page - set a guest page to write-protected
  151. * @vgpu: a vGPU
  152. * @p: intel_vgpu_guest_page
  153. *
  154. * Returns:
  155. * Zero on success, negative error code if failed.
  156. */
  157. static inline int intel_gvt_hypervisor_set_wp_page(struct intel_vgpu *vgpu,
  158. struct intel_vgpu_guest_page *p)
  159. {
  160. int ret;
  161. if (p->writeprotection)
  162. return 0;
  163. ret = intel_gvt_host.mpt->set_wp_page(vgpu->handle, p->gfn);
  164. if (ret)
  165. return ret;
  166. p->writeprotection = true;
  167. atomic_inc(&vgpu->gtt.n_write_protected_guest_page);
  168. return 0;
  169. }
  170. /**
  171. * intel_gvt_hypervisor_unset_wp_page - remove the write-protection of a
  172. * guest page
  173. * @vgpu: a vGPU
  174. * @p: intel_vgpu_guest_page
  175. *
  176. * Returns:
  177. * Zero on success, negative error code if failed.
  178. */
  179. static inline int intel_gvt_hypervisor_unset_wp_page(struct intel_vgpu *vgpu,
  180. struct intel_vgpu_guest_page *p)
  181. {
  182. int ret;
  183. if (!p->writeprotection)
  184. return 0;
  185. ret = intel_gvt_host.mpt->unset_wp_page(vgpu->handle, p->gfn);
  186. if (ret)
  187. return ret;
  188. p->writeprotection = false;
  189. atomic_dec(&vgpu->gtt.n_write_protected_guest_page);
  190. return 0;
  191. }
  192. /**
  193. * intel_gvt_hypervisor_read_gpa - copy data from GPA to host data buffer
  194. * @vgpu: a vGPU
  195. * @gpa: guest physical address
  196. * @buf: host data buffer
  197. * @len: data length
  198. *
  199. * Returns:
  200. * Zero on success, negative error code if failed.
  201. */
  202. static inline int intel_gvt_hypervisor_read_gpa(struct intel_vgpu *vgpu,
  203. unsigned long gpa, void *buf, unsigned long len)
  204. {
  205. return intel_gvt_host.mpt->read_gpa(vgpu->handle, gpa, buf, len);
  206. }
  207. /**
  208. * intel_gvt_hypervisor_write_gpa - copy data from host data buffer to GPA
  209. * @vgpu: a vGPU
  210. * @gpa: guest physical address
  211. * @buf: host data buffer
  212. * @len: data length
  213. *
  214. * Returns:
  215. * Zero on success, negative error code if failed.
  216. */
  217. static inline int intel_gvt_hypervisor_write_gpa(struct intel_vgpu *vgpu,
  218. unsigned long gpa, void *buf, unsigned long len)
  219. {
  220. return intel_gvt_host.mpt->write_gpa(vgpu->handle, gpa, buf, len);
  221. }
  222. /**
  223. * intel_gvt_hypervisor_gfn_to_mfn - translate a GFN to MFN
  224. * @vgpu: a vGPU
  225. * @gpfn: guest pfn
  226. *
  227. * Returns:
  228. * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
  229. */
  230. static inline unsigned long intel_gvt_hypervisor_gfn_to_mfn(
  231. struct intel_vgpu *vgpu, unsigned long gfn)
  232. {
  233. return intel_gvt_host.mpt->gfn_to_mfn(vgpu->handle, gfn);
  234. }
  235. /**
  236. * intel_gvt_hypervisor_map_gfn_to_mfn - map a GFN region to MFN
  237. * @vgpu: a vGPU
  238. * @gfn: guest PFN
  239. * @mfn: host PFN
  240. * @nr: amount of PFNs
  241. * @map: map or unmap
  242. *
  243. * Returns:
  244. * Zero on success, negative error code if failed.
  245. */
  246. static inline int intel_gvt_hypervisor_map_gfn_to_mfn(
  247. struct intel_vgpu *vgpu, unsigned long gfn,
  248. unsigned long mfn, unsigned int nr,
  249. bool map)
  250. {
  251. /* a MPT implementation could have MMIO mapped elsewhere */
  252. if (!intel_gvt_host.mpt->map_gfn_to_mfn)
  253. return 0;
  254. return intel_gvt_host.mpt->map_gfn_to_mfn(vgpu->handle, gfn, mfn, nr,
  255. map);
  256. }
  257. /**
  258. * intel_gvt_hypervisor_set_trap_area - Trap a guest PA region
  259. * @vgpu: a vGPU
  260. * @start: the beginning of the guest physical address region
  261. * @end: the end of the guest physical address region
  262. * @map: map or unmap
  263. *
  264. * Returns:
  265. * Zero on success, negative error code if failed.
  266. */
  267. static inline int intel_gvt_hypervisor_set_trap_area(
  268. struct intel_vgpu *vgpu, u64 start, u64 end, bool map)
  269. {
  270. /* a MPT implementation could have MMIO trapped elsewhere */
  271. if (!intel_gvt_host.mpt->set_trap_area)
  272. return 0;
  273. return intel_gvt_host.mpt->set_trap_area(vgpu->handle, start, end, map);
  274. }
  275. #endif /* _GVT_MPT_H_ */