cfg_space.c 11 KB

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