spapr_pci_nvlink2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * QEMU sPAPR PCI for NVLink2 pass through
  3. *
  4. * Copyright (c) 2019 Alexey Kardashevskiy, IBM Corporation.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/pci-host/spapr.h"
  28. #include "hw/ppc/spapr_numa.h"
  29. #include "qemu/error-report.h"
  30. #include "hw/ppc/fdt.h"
  31. #include "hw/pci/pci_bridge.h"
  32. #define PHANDLE_PCIDEV(phb, pdev) (0x12000000 | \
  33. (((phb)->index) << 16) | ((pdev)->devfn))
  34. #define PHANDLE_GPURAM(phb, n) (0x110000FF | ((n) << 8) | \
  35. (((phb)->index) << 16))
  36. #define PHANDLE_NVLINK(phb, gn, nn) (0x00130000 | (((phb)->index) << 8) | \
  37. ((gn) << 4) | (nn))
  38. typedef struct SpaprPhbPciNvGpuSlot {
  39. uint64_t tgt;
  40. uint64_t gpa;
  41. unsigned numa_id;
  42. PCIDevice *gpdev;
  43. int linknum;
  44. struct {
  45. uint64_t atsd_gpa;
  46. PCIDevice *npdev;
  47. uint32_t link_speed;
  48. } links[NVGPU_MAX_LINKS];
  49. } SpaprPhbPciNvGpuSlot;
  50. struct SpaprPhbPciNvGpuConfig {
  51. uint64_t nv2_ram_current;
  52. uint64_t nv2_atsd_current;
  53. int num; /* number of non empty (i.e. tgt!=0) entries in slots[] */
  54. SpaprPhbPciNvGpuSlot slots[NVGPU_MAX_NUM];
  55. Error *err;
  56. };
  57. static SpaprPhbPciNvGpuSlot *
  58. spapr_nvgpu_get_slot(SpaprPhbPciNvGpuConfig *nvgpus, uint64_t tgt)
  59. {
  60. int i;
  61. /* Search for partially collected "slot" */
  62. for (i = 0; i < nvgpus->num; ++i) {
  63. if (nvgpus->slots[i].tgt == tgt) {
  64. return &nvgpus->slots[i];
  65. }
  66. }
  67. if (nvgpus->num == ARRAY_SIZE(nvgpus->slots)) {
  68. return NULL;
  69. }
  70. i = nvgpus->num;
  71. nvgpus->slots[i].tgt = tgt;
  72. ++nvgpus->num;
  73. return &nvgpus->slots[i];
  74. }
  75. static void spapr_pci_collect_nvgpu(SpaprPhbPciNvGpuConfig *nvgpus,
  76. PCIDevice *pdev, uint64_t tgt,
  77. MemoryRegion *mr, Error **errp)
  78. {
  79. MachineState *machine = MACHINE(qdev_get_machine());
  80. SpaprMachineState *spapr = SPAPR_MACHINE(machine);
  81. SpaprPhbPciNvGpuSlot *nvslot = spapr_nvgpu_get_slot(nvgpus, tgt);
  82. if (!nvslot) {
  83. error_setg(errp, "Found too many GPUs per vPHB");
  84. return;
  85. }
  86. g_assert(!nvslot->gpdev);
  87. nvslot->gpdev = pdev;
  88. nvslot->gpa = nvgpus->nv2_ram_current;
  89. nvgpus->nv2_ram_current += memory_region_size(mr);
  90. nvslot->numa_id = spapr->gpu_numa_id;
  91. ++spapr->gpu_numa_id;
  92. }
  93. static void spapr_pci_collect_nvnpu(SpaprPhbPciNvGpuConfig *nvgpus,
  94. PCIDevice *pdev, uint64_t tgt,
  95. MemoryRegion *mr, Error **errp)
  96. {
  97. SpaprPhbPciNvGpuSlot *nvslot = spapr_nvgpu_get_slot(nvgpus, tgt);
  98. int j;
  99. if (!nvslot) {
  100. error_setg(errp, "Found too many NVLink bridges per vPHB");
  101. return;
  102. }
  103. j = nvslot->linknum;
  104. if (j == ARRAY_SIZE(nvslot->links)) {
  105. error_setg(errp, "Found too many NVLink bridges per GPU");
  106. return;
  107. }
  108. ++nvslot->linknum;
  109. g_assert(!nvslot->links[j].npdev);
  110. nvslot->links[j].npdev = pdev;
  111. nvslot->links[j].atsd_gpa = nvgpus->nv2_atsd_current;
  112. nvgpus->nv2_atsd_current += memory_region_size(mr);
  113. nvslot->links[j].link_speed =
  114. object_property_get_uint(OBJECT(pdev), "nvlink2-link-speed", NULL);
  115. }
  116. static void spapr_phb_pci_collect_nvgpu(PCIBus *bus, PCIDevice *pdev,
  117. void *opaque)
  118. {
  119. PCIBus *sec_bus;
  120. Object *po = OBJECT(pdev);
  121. uint64_t tgt = object_property_get_uint(po, "nvlink2-tgt", NULL);
  122. if (tgt) {
  123. Error *local_err = NULL;
  124. SpaprPhbPciNvGpuConfig *nvgpus = opaque;
  125. Object *mr_gpu = object_property_get_link(po, "nvlink2-mr[0]", NULL);
  126. Object *mr_npu = object_property_get_link(po, "nvlink2-atsd-mr[0]",
  127. NULL);
  128. g_assert(mr_gpu || mr_npu);
  129. if (mr_gpu) {
  130. spapr_pci_collect_nvgpu(nvgpus, pdev, tgt, MEMORY_REGION(mr_gpu),
  131. &local_err);
  132. } else {
  133. spapr_pci_collect_nvnpu(nvgpus, pdev, tgt, MEMORY_REGION(mr_npu),
  134. &local_err);
  135. }
  136. error_propagate(&nvgpus->err, local_err);
  137. }
  138. if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
  139. PCI_HEADER_TYPE_BRIDGE)) {
  140. return;
  141. }
  142. sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
  143. if (!sec_bus) {
  144. return;
  145. }
  146. pci_for_each_device_under_bus(sec_bus, spapr_phb_pci_collect_nvgpu, opaque);
  147. }
  148. void spapr_phb_nvgpu_setup(SpaprPhbState *sphb, Error **errp)
  149. {
  150. int i, j, valid_gpu_num;
  151. PCIBus *bus;
  152. /* Search for GPUs and NPUs */
  153. if (!sphb->nv2_gpa_win_addr || !sphb->nv2_atsd_win_addr) {
  154. return;
  155. }
  156. sphb->nvgpus = g_new0(SpaprPhbPciNvGpuConfig, 1);
  157. sphb->nvgpus->nv2_ram_current = sphb->nv2_gpa_win_addr;
  158. sphb->nvgpus->nv2_atsd_current = sphb->nv2_atsd_win_addr;
  159. bus = PCI_HOST_BRIDGE(sphb)->bus;
  160. pci_for_each_device_under_bus(bus, spapr_phb_pci_collect_nvgpu,
  161. sphb->nvgpus);
  162. if (sphb->nvgpus->err) {
  163. error_propagate(errp, sphb->nvgpus->err);
  164. sphb->nvgpus->err = NULL;
  165. goto cleanup_exit;
  166. }
  167. /* Add found GPU RAM and ATSD MRs if found */
  168. for (i = 0, valid_gpu_num = 0; i < sphb->nvgpus->num; ++i) {
  169. Object *nvmrobj;
  170. SpaprPhbPciNvGpuSlot *nvslot = &sphb->nvgpus->slots[i];
  171. if (!nvslot->gpdev) {
  172. continue;
  173. }
  174. nvmrobj = object_property_get_link(OBJECT(nvslot->gpdev),
  175. "nvlink2-mr[0]", NULL);
  176. /* ATSD is pointless without GPU RAM MR so skip those */
  177. if (!nvmrobj) {
  178. continue;
  179. }
  180. ++valid_gpu_num;
  181. memory_region_add_subregion(get_system_memory(), nvslot->gpa,
  182. MEMORY_REGION(nvmrobj));
  183. for (j = 0; j < nvslot->linknum; ++j) {
  184. Object *atsdmrobj;
  185. atsdmrobj = object_property_get_link(OBJECT(nvslot->links[j].npdev),
  186. "nvlink2-atsd-mr[0]", NULL);
  187. if (!atsdmrobj) {
  188. continue;
  189. }
  190. memory_region_add_subregion(get_system_memory(),
  191. nvslot->links[j].atsd_gpa,
  192. MEMORY_REGION(atsdmrobj));
  193. }
  194. }
  195. if (valid_gpu_num) {
  196. return;
  197. }
  198. /* We did not find any interesting GPU */
  199. cleanup_exit:
  200. g_free(sphb->nvgpus);
  201. sphb->nvgpus = NULL;
  202. }
  203. void spapr_phb_nvgpu_free(SpaprPhbState *sphb)
  204. {
  205. int i, j;
  206. if (!sphb->nvgpus) {
  207. return;
  208. }
  209. for (i = 0; i < sphb->nvgpus->num; ++i) {
  210. SpaprPhbPciNvGpuSlot *nvslot = &sphb->nvgpus->slots[i];
  211. Object *nv_mrobj = object_property_get_link(OBJECT(nvslot->gpdev),
  212. "nvlink2-mr[0]", NULL);
  213. if (nv_mrobj) {
  214. memory_region_del_subregion(get_system_memory(),
  215. MEMORY_REGION(nv_mrobj));
  216. }
  217. for (j = 0; j < nvslot->linknum; ++j) {
  218. PCIDevice *npdev = nvslot->links[j].npdev;
  219. Object *atsd_mrobj;
  220. atsd_mrobj = object_property_get_link(OBJECT(npdev),
  221. "nvlink2-atsd-mr[0]", NULL);
  222. if (atsd_mrobj) {
  223. memory_region_del_subregion(get_system_memory(),
  224. MEMORY_REGION(atsd_mrobj));
  225. }
  226. }
  227. }
  228. g_free(sphb->nvgpus);
  229. sphb->nvgpus = NULL;
  230. }
  231. void spapr_phb_nvgpu_populate_dt(SpaprPhbState *sphb, void *fdt, int bus_off,
  232. Error **errp)
  233. {
  234. int i, j, atsdnum = 0;
  235. uint64_t atsd[8]; /* The existing limitation of known guests */
  236. if (!sphb->nvgpus) {
  237. return;
  238. }
  239. for (i = 0; (i < sphb->nvgpus->num) && (atsdnum < ARRAY_SIZE(atsd)); ++i) {
  240. SpaprPhbPciNvGpuSlot *nvslot = &sphb->nvgpus->slots[i];
  241. if (!nvslot->gpdev) {
  242. continue;
  243. }
  244. for (j = 0; j < nvslot->linknum; ++j) {
  245. if (!nvslot->links[j].atsd_gpa) {
  246. continue;
  247. }
  248. if (atsdnum == ARRAY_SIZE(atsd)) {
  249. error_report("Only %"PRIuPTR" ATSD registers supported",
  250. ARRAY_SIZE(atsd));
  251. break;
  252. }
  253. atsd[atsdnum] = cpu_to_be64(nvslot->links[j].atsd_gpa);
  254. ++atsdnum;
  255. }
  256. }
  257. if (!atsdnum) {
  258. error_setg(errp, "No ATSD registers found");
  259. return;
  260. }
  261. if (!spapr_phb_eeh_available(sphb)) {
  262. /*
  263. * ibm,mmio-atsd contains ATSD registers; these belong to an NPU PHB
  264. * which we do not emulate as a separate device. Instead we put
  265. * ibm,mmio-atsd to the vPHB with GPU and make sure that we do not
  266. * put GPUs from different IOMMU groups to the same vPHB to ensure
  267. * that the guest will use ATSDs from the corresponding NPU.
  268. */
  269. error_setg(errp, "ATSD requires separate vPHB per GPU IOMMU group");
  270. return;
  271. }
  272. _FDT((fdt_setprop(fdt, bus_off, "ibm,mmio-atsd", atsd,
  273. atsdnum * sizeof(atsd[0]))));
  274. }
  275. void spapr_phb_nvgpu_ram_populate_dt(SpaprPhbState *sphb, void *fdt)
  276. {
  277. int i, j, linkidx, npuoff;
  278. g_autofree char *npuname = NULL;
  279. if (!sphb->nvgpus) {
  280. return;
  281. }
  282. npuname = g_strdup_printf("npuphb%d", sphb->index);
  283. npuoff = fdt_add_subnode(fdt, 0, npuname);
  284. _FDT(npuoff);
  285. _FDT(fdt_setprop_cell(fdt, npuoff, "#address-cells", 1));
  286. _FDT(fdt_setprop_cell(fdt, npuoff, "#size-cells", 0));
  287. /* Advertise NPU as POWER9 so the guest can enable NPU2 contexts */
  288. _FDT((fdt_setprop_string(fdt, npuoff, "compatible", "ibm,power9-npu")));
  289. for (i = 0, linkidx = 0; i < sphb->nvgpus->num; ++i) {
  290. for (j = 0; j < sphb->nvgpus->slots[i].linknum; ++j) {
  291. g_autofree char *linkname = g_strdup_printf("link@%d", linkidx);
  292. int off = fdt_add_subnode(fdt, npuoff, linkname);
  293. _FDT(off);
  294. /* _FDT((fdt_setprop_cell(fdt, off, "reg", linkidx))); */
  295. _FDT((fdt_setprop_string(fdt, off, "compatible",
  296. "ibm,npu-link")));
  297. _FDT((fdt_setprop_cell(fdt, off, "phandle",
  298. PHANDLE_NVLINK(sphb, i, j))));
  299. _FDT((fdt_setprop_cell(fdt, off, "ibm,npu-link-index", linkidx)));
  300. ++linkidx;
  301. }
  302. }
  303. /* Add memory nodes for GPU RAM and mark them unusable */
  304. for (i = 0; i < sphb->nvgpus->num; ++i) {
  305. SpaprPhbPciNvGpuSlot *nvslot = &sphb->nvgpus->slots[i];
  306. Object *nv_mrobj = object_property_get_link(OBJECT(nvslot->gpdev),
  307. "nvlink2-mr[0]",
  308. &error_abort);
  309. uint64_t size = object_property_get_uint(nv_mrobj, "size", NULL);
  310. uint64_t mem_reg[2] = { cpu_to_be64(nvslot->gpa), cpu_to_be64(size) };
  311. g_autofree char *mem_name = g_strdup_printf("memory@%"PRIx64,
  312. nvslot->gpa);
  313. int off = fdt_add_subnode(fdt, 0, mem_name);
  314. _FDT(off);
  315. _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
  316. _FDT((fdt_setprop(fdt, off, "reg", mem_reg, sizeof(mem_reg))));
  317. spapr_numa_write_associativity_dt(SPAPR_MACHINE(qdev_get_machine()),
  318. fdt, off, nvslot->numa_id);
  319. _FDT((fdt_setprop_string(fdt, off, "compatible",
  320. "ibm,coherent-device-memory")));
  321. mem_reg[1] = cpu_to_be64(0);
  322. _FDT((fdt_setprop(fdt, off, "linux,usable-memory", mem_reg,
  323. sizeof(mem_reg))));
  324. _FDT((fdt_setprop_cell(fdt, off, "phandle",
  325. PHANDLE_GPURAM(sphb, i))));
  326. }
  327. }
  328. void spapr_phb_nvgpu_populate_pcidev_dt(PCIDevice *dev, void *fdt, int offset,
  329. SpaprPhbState *sphb)
  330. {
  331. int i, j;
  332. if (!sphb->nvgpus) {
  333. return;
  334. }
  335. for (i = 0; i < sphb->nvgpus->num; ++i) {
  336. SpaprPhbPciNvGpuSlot *nvslot = &sphb->nvgpus->slots[i];
  337. /* Skip "slot" without attached GPU */
  338. if (!nvslot->gpdev) {
  339. continue;
  340. }
  341. if (dev == nvslot->gpdev) {
  342. uint32_t npus[nvslot->linknum];
  343. for (j = 0; j < nvslot->linknum; ++j) {
  344. PCIDevice *npdev = nvslot->links[j].npdev;
  345. npus[j] = cpu_to_be32(PHANDLE_PCIDEV(sphb, npdev));
  346. }
  347. _FDT(fdt_setprop(fdt, offset, "ibm,npu", npus,
  348. j * sizeof(npus[0])));
  349. _FDT((fdt_setprop_cell(fdt, offset, "phandle",
  350. PHANDLE_PCIDEV(sphb, dev))));
  351. continue;
  352. }
  353. for (j = 0; j < nvslot->linknum; ++j) {
  354. if (dev != nvslot->links[j].npdev) {
  355. continue;
  356. }
  357. _FDT((fdt_setprop_cell(fdt, offset, "phandle",
  358. PHANDLE_PCIDEV(sphb, dev))));
  359. _FDT(fdt_setprop_cell(fdt, offset, "ibm,gpu",
  360. PHANDLE_PCIDEV(sphb, nvslot->gpdev)));
  361. _FDT((fdt_setprop_cell(fdt, offset, "ibm,nvlink",
  362. PHANDLE_NVLINK(sphb, i, j))));
  363. /*
  364. * If we ever want to emulate GPU RAM at the same location as on
  365. * the host - here is the encoding GPA->TGT:
  366. *
  367. * gta = ((sphb->nv2_gpa >> 42) & 0x1) << 42;
  368. * gta |= ((sphb->nv2_gpa >> 45) & 0x3) << 43;
  369. * gta |= ((sphb->nv2_gpa >> 49) & 0x3) << 45;
  370. * gta |= sphb->nv2_gpa & ((1UL << 43) - 1);
  371. */
  372. _FDT(fdt_setprop_cell(fdt, offset, "memory-region",
  373. PHANDLE_GPURAM(sphb, i)));
  374. _FDT(fdt_setprop_u64(fdt, offset, "ibm,device-tgt-addr",
  375. nvslot->tgt));
  376. _FDT(fdt_setprop_cell(fdt, offset, "ibm,nvlink-speed",
  377. nvslot->links[j].link_speed));
  378. }
  379. }
  380. }