spapr_pci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * QEMU sPAPR PCI host originated from Uninorth PCI host
  3. *
  4. * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
  5. * Copyright (C) 2011 David Gibson, IBM Corporation.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw.h"
  26. #include "pci.h"
  27. #include "pci_host.h"
  28. #include "hw/spapr.h"
  29. #include "hw/spapr_pci.h"
  30. #include "exec-memory.h"
  31. #include <libfdt.h>
  32. #include "hw/pci_internals.h"
  33. static const uint32_t bars[] = {
  34. PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_1,
  35. PCI_BASE_ADDRESS_2, PCI_BASE_ADDRESS_3,
  36. PCI_BASE_ADDRESS_4, PCI_BASE_ADDRESS_5
  37. /*, PCI_ROM_ADDRESS*/
  38. };
  39. static PCIDevice *find_dev(sPAPREnvironment *spapr,
  40. uint64_t buid, uint32_t config_addr)
  41. {
  42. DeviceState *qdev;
  43. int devfn = (config_addr >> 8) & 0xFF;
  44. sPAPRPHBState *phb;
  45. QLIST_FOREACH(phb, &spapr->phbs, list) {
  46. if (phb->buid != buid) {
  47. continue;
  48. }
  49. QTAILQ_FOREACH(qdev, &phb->host_state.bus->qbus.children, sibling) {
  50. PCIDevice *dev = (PCIDevice *)qdev;
  51. if (dev->devfn == devfn) {
  52. return dev;
  53. }
  54. }
  55. }
  56. return NULL;
  57. }
  58. static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr,
  59. uint32_t token, uint32_t nargs,
  60. target_ulong args,
  61. uint32_t nret, target_ulong rets)
  62. {
  63. uint32_t val, size, addr;
  64. uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  65. PCIDevice *dev = find_dev(spapr, buid, rtas_ld(args, 0));
  66. if (!dev) {
  67. rtas_st(rets, 0, -1);
  68. return;
  69. }
  70. size = rtas_ld(args, 3);
  71. addr = rtas_ld(args, 0) & 0xFF;
  72. val = pci_default_read_config(dev, addr, size);
  73. rtas_st(rets, 0, 0);
  74. rtas_st(rets, 1, val);
  75. }
  76. static void rtas_read_pci_config(sPAPREnvironment *spapr,
  77. uint32_t token, uint32_t nargs,
  78. target_ulong args,
  79. uint32_t nret, target_ulong rets)
  80. {
  81. uint32_t val, size, addr;
  82. PCIDevice *dev = find_dev(spapr, 0, rtas_ld(args, 0));
  83. if (!dev) {
  84. rtas_st(rets, 0, -1);
  85. return;
  86. }
  87. size = rtas_ld(args, 1);
  88. addr = rtas_ld(args, 0) & 0xFF;
  89. val = pci_default_read_config(dev, addr, size);
  90. rtas_st(rets, 0, 0);
  91. rtas_st(rets, 1, val);
  92. }
  93. static void rtas_ibm_write_pci_config(sPAPREnvironment *spapr,
  94. uint32_t token, uint32_t nargs,
  95. target_ulong args,
  96. uint32_t nret, target_ulong rets)
  97. {
  98. uint32_t val, size, addr;
  99. uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  100. PCIDevice *dev = find_dev(spapr, buid, rtas_ld(args, 0));
  101. if (!dev) {
  102. rtas_st(rets, 0, -1);
  103. return;
  104. }
  105. val = rtas_ld(args, 4);
  106. size = rtas_ld(args, 3);
  107. addr = rtas_ld(args, 0) & 0xFF;
  108. pci_default_write_config(dev, addr, val, size);
  109. rtas_st(rets, 0, 0);
  110. }
  111. static void rtas_write_pci_config(sPAPREnvironment *spapr,
  112. uint32_t token, uint32_t nargs,
  113. target_ulong args,
  114. uint32_t nret, target_ulong rets)
  115. {
  116. uint32_t val, size, addr;
  117. PCIDevice *dev = find_dev(spapr, 0, rtas_ld(args, 0));
  118. if (!dev) {
  119. rtas_st(rets, 0, -1);
  120. return;
  121. }
  122. val = rtas_ld(args, 2);
  123. size = rtas_ld(args, 1);
  124. addr = rtas_ld(args, 0) & 0xFF;
  125. pci_default_write_config(dev, addr, val, size);
  126. rtas_st(rets, 0, 0);
  127. }
  128. static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
  129. {
  130. /*
  131. * Here we need to convert pci_dev + irq_num to some unique value
  132. * which is less than number of IRQs on the specific bus (now it
  133. * is 16). At the moment irq_num == device_id (number of the
  134. * slot?)
  135. * FIXME: we should swizzle in fn and irq_num
  136. */
  137. return (pci_dev->devfn >> 3) % SPAPR_PCI_NUM_LSI;
  138. }
  139. static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
  140. {
  141. /*
  142. * Here we use the number returned by pci_spapr_map_irq to find a
  143. * corresponding qemu_irq.
  144. */
  145. sPAPRPHBState *phb = opaque;
  146. qemu_set_irq(phb->lsi_table[irq_num].qirq, level);
  147. }
  148. static int spapr_phb_init(SysBusDevice *s)
  149. {
  150. sPAPRPHBState *phb = FROM_SYSBUS(sPAPRPHBState, s);
  151. int i;
  152. /* Initialize the LSI table */
  153. for (i = 0; i < SPAPR_PCI_NUM_LSI; i++) {
  154. qemu_irq qirq;
  155. uint32_t num;
  156. qirq = spapr_allocate_irq(0, &num);
  157. if (!qirq) {
  158. return -1;
  159. }
  160. phb->lsi_table[i].dt_irq = num;
  161. phb->lsi_table[i].qirq = qirq;
  162. }
  163. return 0;
  164. }
  165. static int spapr_main_pci_host_init(PCIDevice *d)
  166. {
  167. return 0;
  168. }
  169. static PCIDeviceInfo spapr_main_pci_host_info = {
  170. .qdev.name = "spapr-pci-host-bridge",
  171. .qdev.size = sizeof(PCIDevice),
  172. .init = spapr_main_pci_host_init,
  173. };
  174. static void spapr_register_devices(void)
  175. {
  176. sysbus_register_dev("spapr-pci-host-bridge", sizeof(sPAPRPHBState),
  177. spapr_phb_init);
  178. pci_qdev_register(&spapr_main_pci_host_info);
  179. }
  180. device_init(spapr_register_devices)
  181. static uint64_t spapr_io_read(void *opaque, target_phys_addr_t addr,
  182. unsigned size)
  183. {
  184. switch (size) {
  185. case 1:
  186. return cpu_inb(addr);
  187. case 2:
  188. return cpu_inw(addr);
  189. case 4:
  190. return cpu_inl(addr);
  191. }
  192. assert(0);
  193. }
  194. static void spapr_io_write(void *opaque, target_phys_addr_t addr,
  195. uint64_t data, unsigned size)
  196. {
  197. switch (size) {
  198. case 1:
  199. cpu_outb(addr, data);
  200. return;
  201. case 2:
  202. cpu_outw(addr, data);
  203. return;
  204. case 4:
  205. cpu_outl(addr, data);
  206. return;
  207. }
  208. assert(0);
  209. }
  210. static MemoryRegionOps spapr_io_ops = {
  211. .endianness = DEVICE_LITTLE_ENDIAN,
  212. .read = spapr_io_read,
  213. .write = spapr_io_write
  214. };
  215. void spapr_create_phb(sPAPREnvironment *spapr,
  216. const char *busname, uint64_t buid,
  217. uint64_t mem_win_addr, uint64_t mem_win_size,
  218. uint64_t io_win_addr)
  219. {
  220. DeviceState *dev;
  221. SysBusDevice *s;
  222. sPAPRPHBState *phb;
  223. PCIBus *bus;
  224. char namebuf[strlen(busname)+11];
  225. dev = qdev_create(NULL, "spapr-pci-host-bridge");
  226. qdev_init_nofail(dev);
  227. s = sysbus_from_qdev(dev);
  228. phb = FROM_SYSBUS(sPAPRPHBState, s);
  229. phb->mem_win_addr = mem_win_addr;
  230. sprintf(namebuf, "%s-mem", busname);
  231. memory_region_init(&phb->memspace, namebuf, INT64_MAX);
  232. sprintf(namebuf, "%s-memwindow", busname);
  233. memory_region_init_alias(&phb->memwindow, namebuf, &phb->memspace,
  234. SPAPR_PCI_MEM_WIN_BUS_OFFSET, mem_win_size);
  235. memory_region_add_subregion(get_system_memory(), mem_win_addr,
  236. &phb->memwindow);
  237. phb->io_win_addr = io_win_addr;
  238. /* On ppc, we only have MMIO no specific IO space from the CPU
  239. * perspective. In theory we ought to be able to embed the PCI IO
  240. * memory region direction in the system memory space. However,
  241. * if any of the IO BAR subregions use the old_portio mechanism,
  242. * that won't be processed properly unless accessed from the
  243. * system io address space. This hack to bounce things via
  244. * system_io works around the problem until all the users of
  245. * old_portion are updated */
  246. sprintf(namebuf, "%s-io", busname);
  247. memory_region_init(&phb->iospace, namebuf, SPAPR_PCI_IO_WIN_SIZE);
  248. /* FIXME: fix to support multiple PHBs */
  249. memory_region_add_subregion(get_system_io(), 0, &phb->iospace);
  250. sprintf(namebuf, "%s-iowindow", busname);
  251. memory_region_init_io(&phb->iowindow, &spapr_io_ops, phb,
  252. namebuf, SPAPR_PCI_IO_WIN_SIZE);
  253. memory_region_add_subregion(get_system_memory(), io_win_addr,
  254. &phb->iowindow);
  255. phb->host_state.bus = bus = pci_register_bus(&phb->busdev.qdev, busname,
  256. pci_spapr_set_irq,
  257. pci_spapr_map_irq,
  258. phb,
  259. &phb->memspace, &phb->iospace,
  260. PCI_DEVFN(0, 0),
  261. SPAPR_PCI_NUM_LSI);
  262. spapr_rtas_register("read-pci-config", rtas_read_pci_config);
  263. spapr_rtas_register("write-pci-config", rtas_write_pci_config);
  264. spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config);
  265. spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config);
  266. QLIST_INSERT_HEAD(&spapr->phbs, phb, list);
  267. /* pci_bus_set_mem_base(bus, mem_va_start - SPAPR_PCI_MEM_BAR_START); */
  268. }
  269. /* Macros to operate with address in OF binding to PCI */
  270. #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
  271. #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
  272. #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
  273. #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
  274. #define b_ss(x) b_x((x), 24, 2) /* the space code */
  275. #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
  276. #define b_ddddd(x) b_x((x), 11, 5) /* device number */
  277. #define b_fff(x) b_x((x), 8, 3) /* function number */
  278. #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
  279. static uint32_t regtype_to_ss(uint8_t type)
  280. {
  281. if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  282. return 3;
  283. }
  284. if (type == PCI_BASE_ADDRESS_SPACE_IO) {
  285. return 1;
  286. }
  287. return 2;
  288. }
  289. int spapr_populate_pci_devices(sPAPRPHBState *phb,
  290. uint32_t xics_phandle,
  291. void *fdt)
  292. {
  293. PCIBus *bus = phb->host_state.bus;
  294. int bus_off, node_off = 0, devid, fn, i, n, devices;
  295. DeviceState *qdev;
  296. char nodename[256];
  297. struct {
  298. uint32_t hi;
  299. uint64_t addr;
  300. uint64_t size;
  301. } __attribute__((packed)) reg[PCI_NUM_REGIONS + 1],
  302. assigned_addresses[PCI_NUM_REGIONS];
  303. uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
  304. struct {
  305. uint32_t hi;
  306. uint64_t child;
  307. uint64_t parent;
  308. uint64_t size;
  309. } __attribute__((packed)) ranges[] = {
  310. {
  311. cpu_to_be32(b_ss(1)), cpu_to_be64(0),
  312. cpu_to_be64(phb->io_win_addr),
  313. cpu_to_be64(memory_region_size(&phb->iospace)),
  314. },
  315. {
  316. cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
  317. cpu_to_be64(phb->mem_win_addr),
  318. cpu_to_be64(memory_region_size(&phb->memwindow)),
  319. },
  320. };
  321. uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
  322. uint32_t interrupt_map_mask[] = {
  323. cpu_to_be32(b_ddddd(-1)|b_fff(-1)), 0x0, 0x0, 0x0};
  324. uint32_t interrupt_map[bus->nirq][7];
  325. /* Start populating the FDT */
  326. sprintf(nodename, "pci@%" PRIx64, phb->buid);
  327. bus_off = fdt_add_subnode(fdt, 0, nodename);
  328. if (bus_off < 0) {
  329. return bus_off;
  330. }
  331. #define _FDT(exp) \
  332. do { \
  333. int ret = (exp); \
  334. if (ret < 0) { \
  335. return ret; \
  336. } \
  337. } while (0)
  338. /* Write PHB properties */
  339. _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
  340. _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
  341. _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
  342. _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
  343. _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
  344. _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
  345. _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
  346. _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges)));
  347. _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
  348. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
  349. &interrupt_map_mask, sizeof(interrupt_map_mask)));
  350. /* Populate PCI devices and allocate IRQs */
  351. devices = 0;
  352. QTAILQ_FOREACH(qdev, &bus->qbus.children, sibling) {
  353. PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
  354. int irq_index = pci_spapr_map_irq(dev, 0);
  355. uint32_t *irqmap = interrupt_map[devices];
  356. uint8_t *config = dev->config;
  357. devid = dev->devfn >> 3;
  358. fn = dev->devfn & 7;
  359. sprintf(nodename, "pci@%u,%u", devid, fn);
  360. /* Allocate interrupt from the map */
  361. if (devid > bus->nirq) {
  362. printf("Unexpected behaviour in spapr_populate_pci_devices,"
  363. "wrong devid %u\n", devid);
  364. exit(-1);
  365. }
  366. irqmap[0] = cpu_to_be32(b_ddddd(devid)|b_fff(fn));
  367. irqmap[1] = 0;
  368. irqmap[2] = 0;
  369. irqmap[3] = 0;
  370. irqmap[4] = cpu_to_be32(xics_phandle);
  371. irqmap[5] = cpu_to_be32(phb->lsi_table[irq_index].dt_irq);
  372. irqmap[6] = cpu_to_be32(0x8);
  373. /* Add node to FDT */
  374. node_off = fdt_add_subnode(fdt, bus_off, nodename);
  375. if (node_off < 0) {
  376. return node_off;
  377. }
  378. _FDT(fdt_setprop_cell(fdt, node_off, "vendor-id",
  379. pci_get_word(&config[PCI_VENDOR_ID])));
  380. _FDT(fdt_setprop_cell(fdt, node_off, "device-id",
  381. pci_get_word(&config[PCI_DEVICE_ID])));
  382. _FDT(fdt_setprop_cell(fdt, node_off, "revision-id",
  383. pci_get_byte(&config[PCI_REVISION_ID])));
  384. _FDT(fdt_setprop_cell(fdt, node_off, "class-code",
  385. pci_get_long(&config[PCI_CLASS_REVISION]) >> 8));
  386. _FDT(fdt_setprop_cell(fdt, node_off, "subsystem-id",
  387. pci_get_word(&config[PCI_SUBSYSTEM_ID])));
  388. _FDT(fdt_setprop_cell(fdt, node_off, "subsystem-vendor-id",
  389. pci_get_word(&config[PCI_SUBSYSTEM_VENDOR_ID])));
  390. /* Config space region comes first */
  391. reg[0].hi = cpu_to_be32(
  392. b_n(0) |
  393. b_p(0) |
  394. b_t(0) |
  395. b_ss(0/*config*/) |
  396. b_bbbbbbbb(0) |
  397. b_ddddd(devid) |
  398. b_fff(fn));
  399. reg[0].addr = 0;
  400. reg[0].size = 0;
  401. n = 0;
  402. for (i = 0; i < ARRAY_SIZE(bars); ++i) {
  403. if (0 == dev->io_regions[i].size) {
  404. continue;
  405. }
  406. reg[n+1].hi = cpu_to_be32(
  407. b_n(0) |
  408. b_p(0) |
  409. b_t(0) |
  410. b_ss(regtype_to_ss(dev->io_regions[i].type)) |
  411. b_bbbbbbbb(0) |
  412. b_ddddd(devid) |
  413. b_fff(fn) |
  414. b_rrrrrrrr(bars[i]));
  415. reg[n+1].addr = 0;
  416. reg[n+1].size = cpu_to_be64(dev->io_regions[i].size);
  417. assigned_addresses[n].hi = cpu_to_be32(
  418. b_n(1) |
  419. b_p(0) |
  420. b_t(0) |
  421. b_ss(regtype_to_ss(dev->io_regions[i].type)) |
  422. b_bbbbbbbb(0) |
  423. b_ddddd(devid) |
  424. b_fff(fn) |
  425. b_rrrrrrrr(bars[i]));
  426. /*
  427. * Writing zeroes to assigned_addresses causes the guest kernel to
  428. * reassign BARs
  429. */
  430. assigned_addresses[n].addr = cpu_to_be64(dev->io_regions[i].addr);
  431. assigned_addresses[n].size = reg[n+1].size;
  432. ++n;
  433. }
  434. _FDT(fdt_setprop(fdt, node_off, "reg", reg, sizeof(reg[0])*(n+1)));
  435. _FDT(fdt_setprop(fdt, node_off, "assigned-addresses",
  436. assigned_addresses,
  437. sizeof(assigned_addresses[0])*(n)));
  438. _FDT(fdt_setprop_cell(fdt, node_off, "interrupts",
  439. pci_get_byte(&config[PCI_INTERRUPT_PIN])));
  440. ++devices;
  441. }
  442. /* Write interrupt map */
  443. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
  444. devices * sizeof(interrupt_map[0])));
  445. return 0;
  446. }