cfg_space.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. * Jike Song <jike.song@intel.com>
  26. *
  27. * Contributors:
  28. * Zhi Wang <zhi.a.wang@intel.com>
  29. * Min He <min.he@intel.com>
  30. * Bing Niu <bing.niu@intel.com>
  31. *
  32. */
  33. #include "i915_drv.h"
  34. #include "gvt.h"
  35. enum {
  36. INTEL_GVT_PCI_BAR_GTTMMIO = 0,
  37. INTEL_GVT_PCI_BAR_APERTURE,
  38. INTEL_GVT_PCI_BAR_PIO,
  39. INTEL_GVT_PCI_BAR_MAX,
  40. };
  41. /**
  42. * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space read
  43. *
  44. * Returns:
  45. * Zero on success, negative error code if failed.
  46. */
  47. int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
  48. void *p_data, unsigned int bytes)
  49. {
  50. if (WARN_ON(bytes > 4))
  51. return -EINVAL;
  52. if (WARN_ON(offset + bytes > INTEL_GVT_MAX_CFG_SPACE_SZ))
  53. return -EINVAL;
  54. memcpy(p_data, vgpu_cfg_space(vgpu) + offset, bytes);
  55. return 0;
  56. }
  57. static int map_aperture(struct intel_vgpu *vgpu, bool map)
  58. {
  59. u64 first_gfn, first_mfn;
  60. u64 val;
  61. int ret;
  62. if (map == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked)
  63. return 0;
  64. val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_2];
  65. if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  66. val = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
  67. else
  68. val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
  69. first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT;
  70. first_mfn = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
  71. ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn,
  72. first_mfn,
  73. vgpu_aperture_sz(vgpu) >>
  74. PAGE_SHIFT, map);
  75. if (ret)
  76. return ret;
  77. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map;
  78. return 0;
  79. }
  80. static int trap_gttmmio(struct intel_vgpu *vgpu, bool trap)
  81. {
  82. u64 start, end;
  83. u64 val;
  84. int ret;
  85. if (trap == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked)
  86. return 0;
  87. val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_0];
  88. if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  89. start = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
  90. else
  91. start = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
  92. start &= ~GENMASK(3, 0);
  93. end = start + vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size - 1;
  94. ret = intel_gvt_hypervisor_set_trap_area(vgpu, start, end, trap);
  95. if (ret)
  96. return ret;
  97. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap;
  98. return 0;
  99. }
  100. static int emulate_pci_command_write(struct intel_vgpu *vgpu,
  101. unsigned int offset, void *p_data, unsigned int bytes)
  102. {
  103. u8 old = vgpu_cfg_space(vgpu)[offset];
  104. u8 new = *(u8 *)p_data;
  105. u8 changed = old ^ new;
  106. int ret;
  107. memcpy(vgpu_cfg_space(vgpu) + offset, p_data, bytes);
  108. if (!(changed & PCI_COMMAND_MEMORY))
  109. return 0;
  110. if (old & PCI_COMMAND_MEMORY) {
  111. ret = trap_gttmmio(vgpu, false);
  112. if (ret)
  113. return ret;
  114. ret = map_aperture(vgpu, false);
  115. if (ret)
  116. return ret;
  117. } else {
  118. ret = trap_gttmmio(vgpu, true);
  119. if (ret)
  120. return ret;
  121. ret = map_aperture(vgpu, true);
  122. if (ret)
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static int emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset,
  128. void *p_data, unsigned int bytes)
  129. {
  130. unsigned int bar_index =
  131. (rounddown(offset, 8) % PCI_BASE_ADDRESS_0) / 8;
  132. u32 new = *(u32 *)(p_data);
  133. bool lo = IS_ALIGNED(offset, 8);
  134. u64 size;
  135. int ret = 0;
  136. bool mmio_enabled =
  137. vgpu_cfg_space(vgpu)[PCI_COMMAND] & PCI_COMMAND_MEMORY;
  138. if (WARN_ON(bar_index >= INTEL_GVT_PCI_BAR_MAX))
  139. return -EINVAL;
  140. if (new == 0xffffffff) {
  141. /*
  142. * Power-up software can determine how much address
  143. * space the device requires by writing a value of
  144. * all 1's to the register and then reading the value
  145. * back. The device will return 0's in all don't-care
  146. * address bits.
  147. */
  148. size = vgpu->cfg_space.bar[bar_index].size;
  149. if (lo) {
  150. new = rounddown(new, size);
  151. } else {
  152. u32 val = vgpu_cfg_space(vgpu)[rounddown(offset, 8)];
  153. /* for 32bit mode bar it returns all-0 in upper 32
  154. * bit, for 64bit mode bar it will calculate the
  155. * size with lower 32bit and return the corresponding
  156. * value
  157. */
  158. if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  159. new &= (~(size-1)) >> 32;
  160. else
  161. new = 0;
  162. }
  163. /*
  164. * Unmapp & untrap the BAR, since guest hasn't configured a
  165. * valid GPA
  166. */
  167. switch (bar_index) {
  168. case INTEL_GVT_PCI_BAR_GTTMMIO:
  169. ret = trap_gttmmio(vgpu, false);
  170. break;
  171. case INTEL_GVT_PCI_BAR_APERTURE:
  172. ret = map_aperture(vgpu, false);
  173. break;
  174. }
  175. intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
  176. } else {
  177. /*
  178. * Unmapp & untrap the old BAR first, since guest has
  179. * re-configured the BAR
  180. */
  181. switch (bar_index) {
  182. case INTEL_GVT_PCI_BAR_GTTMMIO:
  183. ret = trap_gttmmio(vgpu, false);
  184. break;
  185. case INTEL_GVT_PCI_BAR_APERTURE:
  186. ret = map_aperture(vgpu, false);
  187. break;
  188. }
  189. intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
  190. /* Track the new BAR */
  191. if (mmio_enabled) {
  192. switch (bar_index) {
  193. case INTEL_GVT_PCI_BAR_GTTMMIO:
  194. ret = trap_gttmmio(vgpu, true);
  195. break;
  196. case INTEL_GVT_PCI_BAR_APERTURE:
  197. ret = map_aperture(vgpu, true);
  198. break;
  199. }
  200. }
  201. }
  202. return ret;
  203. }
  204. /**
  205. * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space write
  206. *
  207. * Returns:
  208. * Zero on success, negative error code if failed.
  209. */
  210. int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
  211. void *p_data, unsigned int bytes)
  212. {
  213. int ret;
  214. if (WARN_ON(bytes > 4))
  215. return -EINVAL;
  216. if (WARN_ON(offset + bytes > INTEL_GVT_MAX_CFG_SPACE_SZ))
  217. return -EINVAL;
  218. /* First check if it's PCI_COMMAND */
  219. if (IS_ALIGNED(offset, 2) && offset == PCI_COMMAND) {
  220. if (WARN_ON(bytes > 2))
  221. return -EINVAL;
  222. return emulate_pci_command_write(vgpu, offset, p_data, bytes);
  223. }
  224. switch (rounddown(offset, 4)) {
  225. case PCI_BASE_ADDRESS_0:
  226. case PCI_BASE_ADDRESS_1:
  227. case PCI_BASE_ADDRESS_2:
  228. case PCI_BASE_ADDRESS_3:
  229. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  230. return -EINVAL;
  231. return emulate_pci_bar_write(vgpu, offset, p_data, bytes);
  232. case INTEL_GVT_PCI_SWSCI:
  233. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  234. return -EINVAL;
  235. ret = intel_vgpu_emulate_opregion_request(vgpu, *(u32 *)p_data);
  236. if (ret)
  237. return ret;
  238. break;
  239. case INTEL_GVT_PCI_OPREGION:
  240. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  241. return -EINVAL;
  242. ret = intel_vgpu_init_opregion(vgpu, *(u32 *)p_data);
  243. if (ret)
  244. return ret;
  245. memcpy(vgpu_cfg_space(vgpu) + offset, p_data, bytes);
  246. break;
  247. default:
  248. memcpy(vgpu_cfg_space(vgpu) + offset, p_data, bytes);
  249. break;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * intel_vgpu_init_cfg_space - init vGPU configuration space when create vGPU
  255. *
  256. * @vgpu: a vGPU
  257. * @primary: is the vGPU presented as primary
  258. *
  259. */
  260. void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
  261. bool primary)
  262. {
  263. struct intel_gvt *gvt = vgpu->gvt;
  264. const struct intel_gvt_device_info *info = &gvt->device_info;
  265. u16 *gmch_ctl;
  266. int i;
  267. memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space,
  268. info->cfg_space_size);
  269. if (!primary) {
  270. vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] =
  271. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  272. vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] =
  273. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  274. }
  275. /* Show guest that there isn't any stolen memory.*/
  276. gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL);
  277. *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT);
  278. intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2,
  279. gvt_aperture_pa_base(gvt), true);
  280. vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO
  281. | PCI_COMMAND_MEMORY
  282. | PCI_COMMAND_MASTER);
  283. /*
  284. * Clear the bar upper 32bit and let guest to assign the new value
  285. */
  286. memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4);
  287. memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4);
  288. memset(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_OPREGION, 0, 4);
  289. for (i = 0; i < INTEL_GVT_MAX_BAR_NUM; i++) {
  290. vgpu->cfg_space.bar[i].size = pci_resource_len(
  291. gvt->dev_priv->drm.pdev, i * 2);
  292. vgpu->cfg_space.bar[i].tracked = false;
  293. }
  294. }
  295. /**
  296. * intel_vgpu_reset_cfg_space - reset vGPU configuration space
  297. *
  298. * @vgpu: a vGPU
  299. *
  300. */
  301. void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu)
  302. {
  303. u8 cmd = vgpu_cfg_space(vgpu)[PCI_COMMAND];
  304. bool primary = vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] !=
  305. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  306. if (cmd & PCI_COMMAND_MEMORY) {
  307. trap_gttmmio(vgpu, false);
  308. map_aperture(vgpu, false);
  309. }
  310. /**
  311. * Currently we only do such reset when vGPU is not
  312. * owned by any VM, so we simply restore entire cfg
  313. * space to default value.
  314. */
  315. intel_vgpu_init_cfg_space(vgpu, primary);
  316. }