cfg_space.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. /* bitmap for writable bits (RW or RW1C bits, but cannot co-exist in one
  42. * byte) byte by byte in standard pci configuration space. (not the full
  43. * 256 bytes.)
  44. */
  45. static const u8 pci_cfg_space_rw_bmp[PCI_INTERRUPT_LINE + 4] = {
  46. [PCI_COMMAND] = 0xff, 0x07,
  47. [PCI_STATUS] = 0x00, 0xf9, /* the only one RW1C byte */
  48. [PCI_CACHE_LINE_SIZE] = 0xff,
  49. [PCI_BASE_ADDRESS_0 ... PCI_CARDBUS_CIS - 1] = 0xff,
  50. [PCI_ROM_ADDRESS] = 0x01, 0xf8, 0xff, 0xff,
  51. [PCI_INTERRUPT_LINE] = 0xff,
  52. };
  53. /**
  54. * vgpu_pci_cfg_mem_write - write virtual cfg space memory
  55. * @vgpu: target vgpu
  56. * @off: offset
  57. * @src: src ptr to write
  58. * @bytes: number of bytes
  59. *
  60. * Use this function to write virtual cfg space memory.
  61. * For standard cfg space, only RW bits can be changed,
  62. * and we emulates the RW1C behavior of PCI_STATUS register.
  63. */
  64. static void vgpu_pci_cfg_mem_write(struct intel_vgpu *vgpu, unsigned int off,
  65. u8 *src, unsigned int bytes)
  66. {
  67. u8 *cfg_base = vgpu_cfg_space(vgpu);
  68. u8 mask, new, old;
  69. int i = 0;
  70. for (; i < bytes && (off + i < sizeof(pci_cfg_space_rw_bmp)); i++) {
  71. mask = pci_cfg_space_rw_bmp[off + i];
  72. old = cfg_base[off + i];
  73. new = src[i] & mask;
  74. /**
  75. * The PCI_STATUS high byte has RW1C bits, here
  76. * emulates clear by writing 1 for these bits.
  77. * Writing a 0b to RW1C bits has no effect.
  78. */
  79. if (off + i == PCI_STATUS + 1)
  80. new = (~new & old) & mask;
  81. cfg_base[off + i] = (old & ~mask) | new;
  82. }
  83. /* For other configuration space directly copy as it is. */
  84. if (i < bytes)
  85. memcpy(cfg_base + off + i, src + i, bytes - i);
  86. }
  87. /**
  88. * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space read
  89. * @vgpu: target vgpu
  90. * @offset: offset
  91. * @p_data: return data ptr
  92. * @bytes: number of bytes to read
  93. *
  94. * Returns:
  95. * Zero on success, negative error code if failed.
  96. */
  97. int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
  98. void *p_data, unsigned int bytes)
  99. {
  100. if (WARN_ON(bytes > 4))
  101. return -EINVAL;
  102. if (WARN_ON(offset + bytes > vgpu->gvt->device_info.cfg_space_size))
  103. return -EINVAL;
  104. memcpy(p_data, vgpu_cfg_space(vgpu) + offset, bytes);
  105. return 0;
  106. }
  107. static int map_aperture(struct intel_vgpu *vgpu, bool map)
  108. {
  109. phys_addr_t aperture_pa = vgpu_aperture_pa_base(vgpu);
  110. unsigned long aperture_sz = vgpu_aperture_sz(vgpu);
  111. u64 first_gfn;
  112. u64 val;
  113. int ret;
  114. if (map == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked)
  115. return 0;
  116. val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_2];
  117. if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  118. val = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
  119. else
  120. val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
  121. first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT;
  122. ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn,
  123. aperture_pa >> PAGE_SHIFT,
  124. aperture_sz >> PAGE_SHIFT,
  125. map);
  126. if (ret)
  127. return ret;
  128. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map;
  129. return 0;
  130. }
  131. static int trap_gttmmio(struct intel_vgpu *vgpu, bool trap)
  132. {
  133. u64 start, end;
  134. u64 val;
  135. int ret;
  136. if (trap == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked)
  137. return 0;
  138. val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_0];
  139. if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  140. start = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
  141. else
  142. start = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
  143. start &= ~GENMASK(3, 0);
  144. end = start + vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size - 1;
  145. ret = intel_gvt_hypervisor_set_trap_area(vgpu, start, end, trap);
  146. if (ret)
  147. return ret;
  148. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap;
  149. return 0;
  150. }
  151. static int emulate_pci_command_write(struct intel_vgpu *vgpu,
  152. unsigned int offset, void *p_data, unsigned int bytes)
  153. {
  154. u8 old = vgpu_cfg_space(vgpu)[offset];
  155. u8 new = *(u8 *)p_data;
  156. u8 changed = old ^ new;
  157. int ret;
  158. vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
  159. if (!(changed & PCI_COMMAND_MEMORY))
  160. return 0;
  161. if (old & PCI_COMMAND_MEMORY) {
  162. ret = trap_gttmmio(vgpu, false);
  163. if (ret)
  164. return ret;
  165. ret = map_aperture(vgpu, false);
  166. if (ret)
  167. return ret;
  168. } else {
  169. ret = trap_gttmmio(vgpu, true);
  170. if (ret)
  171. return ret;
  172. ret = map_aperture(vgpu, true);
  173. if (ret)
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. static int emulate_pci_rom_bar_write(struct intel_vgpu *vgpu,
  179. unsigned int offset, void *p_data, unsigned int bytes)
  180. {
  181. u32 *pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
  182. u32 new = *(u32 *)(p_data);
  183. if ((new & PCI_ROM_ADDRESS_MASK) == PCI_ROM_ADDRESS_MASK)
  184. /* We don't have rom, return size of 0. */
  185. *pval = 0;
  186. else
  187. vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
  188. return 0;
  189. }
  190. static int emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset,
  191. void *p_data, unsigned int bytes)
  192. {
  193. u32 new = *(u32 *)(p_data);
  194. bool lo = IS_ALIGNED(offset, 8);
  195. u64 size;
  196. int ret = 0;
  197. bool mmio_enabled =
  198. vgpu_cfg_space(vgpu)[PCI_COMMAND] & PCI_COMMAND_MEMORY;
  199. struct intel_vgpu_pci_bar *bars = vgpu->cfg_space.bar;
  200. /*
  201. * Power-up software can determine how much address
  202. * space the device requires by writing a value of
  203. * all 1's to the register and then reading the value
  204. * back. The device will return 0's in all don't-care
  205. * address bits.
  206. */
  207. if (new == 0xffffffff) {
  208. switch (offset) {
  209. case PCI_BASE_ADDRESS_0:
  210. case PCI_BASE_ADDRESS_1:
  211. size = ~(bars[INTEL_GVT_PCI_BAR_GTTMMIO].size -1);
  212. intel_vgpu_write_pci_bar(vgpu, offset,
  213. size >> (lo ? 0 : 32), lo);
  214. /*
  215. * Untrap the BAR, since guest hasn't configured a
  216. * valid GPA
  217. */
  218. ret = trap_gttmmio(vgpu, false);
  219. break;
  220. case PCI_BASE_ADDRESS_2:
  221. case PCI_BASE_ADDRESS_3:
  222. size = ~(bars[INTEL_GVT_PCI_BAR_APERTURE].size -1);
  223. intel_vgpu_write_pci_bar(vgpu, offset,
  224. size >> (lo ? 0 : 32), lo);
  225. ret = map_aperture(vgpu, false);
  226. break;
  227. default:
  228. /* Unimplemented BARs */
  229. intel_vgpu_write_pci_bar(vgpu, offset, 0x0, false);
  230. }
  231. } else {
  232. switch (offset) {
  233. case PCI_BASE_ADDRESS_0:
  234. case PCI_BASE_ADDRESS_1:
  235. /*
  236. * Untrap the old BAR first, since guest has
  237. * re-configured the BAR
  238. */
  239. trap_gttmmio(vgpu, false);
  240. intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
  241. ret = trap_gttmmio(vgpu, mmio_enabled);
  242. break;
  243. case PCI_BASE_ADDRESS_2:
  244. case PCI_BASE_ADDRESS_3:
  245. map_aperture(vgpu, false);
  246. intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
  247. ret = map_aperture(vgpu, mmio_enabled);
  248. break;
  249. default:
  250. intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
  251. }
  252. }
  253. return ret;
  254. }
  255. /**
  256. * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space write
  257. * @vgpu: target vgpu
  258. * @offset: offset
  259. * @p_data: write data ptr
  260. * @bytes: number of bytes to write
  261. *
  262. * Returns:
  263. * Zero on success, negative error code if failed.
  264. */
  265. int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
  266. void *p_data, unsigned int bytes)
  267. {
  268. int ret;
  269. if (WARN_ON(bytes > 4))
  270. return -EINVAL;
  271. if (WARN_ON(offset + bytes > vgpu->gvt->device_info.cfg_space_size))
  272. return -EINVAL;
  273. /* First check if it's PCI_COMMAND */
  274. if (IS_ALIGNED(offset, 2) && offset == PCI_COMMAND) {
  275. if (WARN_ON(bytes > 2))
  276. return -EINVAL;
  277. return emulate_pci_command_write(vgpu, offset, p_data, bytes);
  278. }
  279. switch (rounddown(offset, 4)) {
  280. case PCI_ROM_ADDRESS:
  281. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  282. return -EINVAL;
  283. return emulate_pci_rom_bar_write(vgpu, offset, p_data, bytes);
  284. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_5:
  285. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  286. return -EINVAL;
  287. return emulate_pci_bar_write(vgpu, offset, p_data, bytes);
  288. case INTEL_GVT_PCI_SWSCI:
  289. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  290. return -EINVAL;
  291. ret = intel_vgpu_emulate_opregion_request(vgpu, *(u32 *)p_data);
  292. if (ret)
  293. return ret;
  294. break;
  295. case INTEL_GVT_PCI_OPREGION:
  296. if (WARN_ON(!IS_ALIGNED(offset, 4)))
  297. return -EINVAL;
  298. ret = intel_vgpu_opregion_base_write_handler(vgpu,
  299. *(u32 *)p_data);
  300. if (ret)
  301. return ret;
  302. vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
  303. break;
  304. default:
  305. vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
  306. break;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * intel_vgpu_init_cfg_space - init vGPU configuration space when create vGPU
  312. *
  313. * @vgpu: a vGPU
  314. * @primary: is the vGPU presented as primary
  315. *
  316. */
  317. void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
  318. bool primary)
  319. {
  320. struct intel_gvt *gvt = vgpu->gvt;
  321. const struct intel_gvt_device_info *info = &gvt->device_info;
  322. u16 *gmch_ctl;
  323. memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space,
  324. info->cfg_space_size);
  325. if (!primary) {
  326. vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] =
  327. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  328. vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] =
  329. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  330. }
  331. /* Show guest that there isn't any stolen memory.*/
  332. gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL);
  333. *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT);
  334. intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2,
  335. gvt_aperture_pa_base(gvt), true);
  336. vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO
  337. | PCI_COMMAND_MEMORY
  338. | PCI_COMMAND_MASTER);
  339. /*
  340. * Clear the bar upper 32bit and let guest to assign the new value
  341. */
  342. memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4);
  343. memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4);
  344. memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_4, 0, 8);
  345. memset(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_OPREGION, 0, 4);
  346. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size =
  347. pci_resource_len(gvt->dev_priv->drm.pdev, 0);
  348. vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].size =
  349. pci_resource_len(gvt->dev_priv->drm.pdev, 2);
  350. memset(vgpu_cfg_space(vgpu) + PCI_ROM_ADDRESS, 0, 4);
  351. }
  352. /**
  353. * intel_vgpu_reset_cfg_space - reset vGPU configuration space
  354. *
  355. * @vgpu: a vGPU
  356. *
  357. */
  358. void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu)
  359. {
  360. u8 cmd = vgpu_cfg_space(vgpu)[PCI_COMMAND];
  361. bool primary = vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] !=
  362. INTEL_GVT_PCI_CLASS_VGA_OTHER;
  363. if (cmd & PCI_COMMAND_MEMORY) {
  364. trap_gttmmio(vgpu, false);
  365. map_aperture(vgpu, false);
  366. }
  367. /**
  368. * Currently we only do such reset when vGPU is not
  369. * owned by any VM, so we simply restore entire cfg
  370. * space to default value.
  371. */
  372. intel_vgpu_init_cfg_space(vgpu, primary);
  373. }