spapr_pci.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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/pci.h"
  27. #include "pci/msi.h"
  28. #include "pci/msix.h"
  29. #include "pci/pci_host.h"
  30. #include "hw/spapr.h"
  31. #include "hw/spapr_pci.h"
  32. #include "exec/address-spaces.h"
  33. #include <libfdt.h>
  34. #include "trace.h"
  35. #include "hw/pci/pci_bus.h"
  36. /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
  37. #define RTAS_QUERY_FN 0
  38. #define RTAS_CHANGE_FN 1
  39. #define RTAS_RESET_FN 2
  40. #define RTAS_CHANGE_MSI_FN 3
  41. #define RTAS_CHANGE_MSIX_FN 4
  42. /* Interrupt types to return on RTAS_CHANGE_* */
  43. #define RTAS_TYPE_MSI 1
  44. #define RTAS_TYPE_MSIX 2
  45. static sPAPRPHBState *find_phb(sPAPREnvironment *spapr, uint64_t buid)
  46. {
  47. sPAPRPHBState *sphb;
  48. QLIST_FOREACH(sphb, &spapr->phbs, list) {
  49. if (sphb->buid != buid) {
  50. continue;
  51. }
  52. return sphb;
  53. }
  54. return NULL;
  55. }
  56. static PCIDevice *find_dev(sPAPREnvironment *spapr, uint64_t buid,
  57. uint32_t config_addr)
  58. {
  59. sPAPRPHBState *sphb = find_phb(spapr, buid);
  60. PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
  61. BusState *bus = BUS(phb->bus);
  62. BusChild *kid;
  63. int devfn = (config_addr >> 8) & 0xFF;
  64. if (!phb) {
  65. return NULL;
  66. }
  67. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  68. PCIDevice *dev = (PCIDevice *)kid->child;
  69. if (dev->devfn == devfn) {
  70. return dev;
  71. }
  72. }
  73. return NULL;
  74. }
  75. static uint32_t rtas_pci_cfgaddr(uint32_t arg)
  76. {
  77. /* This handles the encoding of extended config space addresses */
  78. return ((arg >> 20) & 0xf00) | (arg & 0xff);
  79. }
  80. static void finish_read_pci_config(sPAPREnvironment *spapr, uint64_t buid,
  81. uint32_t addr, uint32_t size,
  82. target_ulong rets)
  83. {
  84. PCIDevice *pci_dev;
  85. uint32_t val;
  86. if ((size != 1) && (size != 2) && (size != 4)) {
  87. /* access must be 1, 2 or 4 bytes */
  88. rtas_st(rets, 0, -1);
  89. return;
  90. }
  91. pci_dev = find_dev(spapr, buid, addr);
  92. addr = rtas_pci_cfgaddr(addr);
  93. if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
  94. /* Access must be to a valid device, within bounds and
  95. * naturally aligned */
  96. rtas_st(rets, 0, -1);
  97. return;
  98. }
  99. val = pci_host_config_read_common(pci_dev, addr,
  100. pci_config_size(pci_dev), size);
  101. rtas_st(rets, 0, 0);
  102. rtas_st(rets, 1, val);
  103. }
  104. static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr,
  105. uint32_t token, uint32_t nargs,
  106. target_ulong args,
  107. uint32_t nret, target_ulong rets)
  108. {
  109. uint64_t buid;
  110. uint32_t size, addr;
  111. if ((nargs != 4) || (nret != 2)) {
  112. rtas_st(rets, 0, -1);
  113. return;
  114. }
  115. buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  116. size = rtas_ld(args, 3);
  117. addr = rtas_ld(args, 0);
  118. finish_read_pci_config(spapr, buid, addr, size, rets);
  119. }
  120. static void rtas_read_pci_config(sPAPREnvironment *spapr,
  121. uint32_t token, uint32_t nargs,
  122. target_ulong args,
  123. uint32_t nret, target_ulong rets)
  124. {
  125. uint32_t size, addr;
  126. if ((nargs != 2) || (nret != 2)) {
  127. rtas_st(rets, 0, -1);
  128. return;
  129. }
  130. size = rtas_ld(args, 1);
  131. addr = rtas_ld(args, 0);
  132. finish_read_pci_config(spapr, 0, addr, size, rets);
  133. }
  134. static void finish_write_pci_config(sPAPREnvironment *spapr, uint64_t buid,
  135. uint32_t addr, uint32_t size,
  136. uint32_t val, target_ulong rets)
  137. {
  138. PCIDevice *pci_dev;
  139. if ((size != 1) && (size != 2) && (size != 4)) {
  140. /* access must be 1, 2 or 4 bytes */
  141. rtas_st(rets, 0, -1);
  142. return;
  143. }
  144. pci_dev = find_dev(spapr, buid, addr);
  145. addr = rtas_pci_cfgaddr(addr);
  146. if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
  147. /* Access must be to a valid device, within bounds and
  148. * naturally aligned */
  149. rtas_st(rets, 0, -1);
  150. return;
  151. }
  152. pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
  153. val, size);
  154. rtas_st(rets, 0, 0);
  155. }
  156. static void rtas_ibm_write_pci_config(sPAPREnvironment *spapr,
  157. uint32_t token, uint32_t nargs,
  158. target_ulong args,
  159. uint32_t nret, target_ulong rets)
  160. {
  161. uint64_t buid;
  162. uint32_t val, size, addr;
  163. if ((nargs != 5) || (nret != 1)) {
  164. rtas_st(rets, 0, -1);
  165. return;
  166. }
  167. buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  168. val = rtas_ld(args, 4);
  169. size = rtas_ld(args, 3);
  170. addr = rtas_ld(args, 0);
  171. finish_write_pci_config(spapr, buid, addr, size, val, rets);
  172. }
  173. static void rtas_write_pci_config(sPAPREnvironment *spapr,
  174. uint32_t token, uint32_t nargs,
  175. target_ulong args,
  176. uint32_t nret, target_ulong rets)
  177. {
  178. uint32_t val, size, addr;
  179. if ((nargs != 3) || (nret != 1)) {
  180. rtas_st(rets, 0, -1);
  181. return;
  182. }
  183. val = rtas_ld(args, 2);
  184. size = rtas_ld(args, 1);
  185. addr = rtas_ld(args, 0);
  186. finish_write_pci_config(spapr, 0, addr, size, val, rets);
  187. }
  188. /*
  189. * Find an entry with config_addr or returns the empty one if not found AND
  190. * alloc_new is set.
  191. * At the moment the msi_table entries are never released so there is
  192. * no point to look till the end of the list if we need to find the free entry.
  193. */
  194. static int spapr_msicfg_find(sPAPRPHBState *phb, uint32_t config_addr,
  195. bool alloc_new)
  196. {
  197. int i;
  198. for (i = 0; i < SPAPR_MSIX_MAX_DEVS; ++i) {
  199. if (!phb->msi_table[i].nvec) {
  200. break;
  201. }
  202. if (phb->msi_table[i].config_addr == config_addr) {
  203. return i;
  204. }
  205. }
  206. if ((i < SPAPR_MSIX_MAX_DEVS) && alloc_new) {
  207. trace_spapr_pci_msi("Allocating new MSI config", i, config_addr);
  208. return i;
  209. }
  210. return -1;
  211. }
  212. /*
  213. * Set MSI/MSIX message data.
  214. * This is required for msi_notify()/msix_notify() which
  215. * will write at the addresses via spapr_msi_write().
  216. */
  217. static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr,
  218. bool msix, unsigned req_num)
  219. {
  220. unsigned i;
  221. MSIMessage msg = { .address = addr, .data = 0 };
  222. if (!msix) {
  223. msi_set_message(pdev, msg);
  224. trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
  225. return;
  226. }
  227. for (i = 0; i < req_num; ++i) {
  228. msg.address = addr | (i << 2);
  229. msix_set_message(pdev, i, msg);
  230. trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
  231. }
  232. }
  233. static void rtas_ibm_change_msi(sPAPREnvironment *spapr,
  234. uint32_t token, uint32_t nargs,
  235. target_ulong args, uint32_t nret,
  236. target_ulong rets)
  237. {
  238. uint32_t config_addr = rtas_ld(args, 0);
  239. uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  240. unsigned int func = rtas_ld(args, 3);
  241. unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
  242. unsigned int seq_num = rtas_ld(args, 5);
  243. unsigned int ret_intr_type;
  244. int ndev, irq;
  245. sPAPRPHBState *phb = NULL;
  246. PCIDevice *pdev = NULL;
  247. switch (func) {
  248. case RTAS_CHANGE_MSI_FN:
  249. case RTAS_CHANGE_FN:
  250. ret_intr_type = RTAS_TYPE_MSI;
  251. break;
  252. case RTAS_CHANGE_MSIX_FN:
  253. ret_intr_type = RTAS_TYPE_MSIX;
  254. break;
  255. default:
  256. fprintf(stderr, "rtas_ibm_change_msi(%u) is not implemented\n", func);
  257. rtas_st(rets, 0, -3); /* Parameter error */
  258. return;
  259. }
  260. /* Fins sPAPRPHBState */
  261. phb = find_phb(spapr, buid);
  262. if (phb) {
  263. pdev = find_dev(spapr, buid, config_addr);
  264. }
  265. if (!phb || !pdev) {
  266. rtas_st(rets, 0, -3); /* Parameter error */
  267. return;
  268. }
  269. /* Releasing MSIs */
  270. if (!req_num) {
  271. ndev = spapr_msicfg_find(phb, config_addr, false);
  272. if (ndev < 0) {
  273. trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
  274. rtas_st(rets, 0, -1); /* Hardware error */
  275. return;
  276. }
  277. trace_spapr_pci_msi("Released MSIs", ndev, config_addr);
  278. rtas_st(rets, 0, 0);
  279. rtas_st(rets, 1, 0);
  280. return;
  281. }
  282. /* Enabling MSI */
  283. /* Find a device number in the map to add or reuse the existing one */
  284. ndev = spapr_msicfg_find(phb, config_addr, true);
  285. if (ndev >= SPAPR_MSIX_MAX_DEVS || ndev < 0) {
  286. fprintf(stderr, "No free entry for a new MSI device\n");
  287. rtas_st(rets, 0, -1); /* Hardware error */
  288. return;
  289. }
  290. trace_spapr_pci_msi("Configuring MSI", ndev, config_addr);
  291. /* Check if there is an old config and MSI number has not changed */
  292. if (phb->msi_table[ndev].nvec && (req_num != phb->msi_table[ndev].nvec)) {
  293. /* Unexpected behaviour */
  294. fprintf(stderr, "Cannot reuse MSI config for device#%d", ndev);
  295. rtas_st(rets, 0, -1); /* Hardware error */
  296. return;
  297. }
  298. /* There is no cached config, allocate MSIs */
  299. if (!phb->msi_table[ndev].nvec) {
  300. irq = spapr_allocate_irq_block(req_num, false);
  301. if (irq < 0) {
  302. fprintf(stderr, "Cannot allocate MSIs for device#%d", ndev);
  303. rtas_st(rets, 0, -1); /* Hardware error */
  304. return;
  305. }
  306. phb->msi_table[ndev].irq = irq;
  307. phb->msi_table[ndev].nvec = req_num;
  308. phb->msi_table[ndev].config_addr = config_addr;
  309. }
  310. /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
  311. spapr_msi_setmsg(pdev, phb->msi_win_addr | (ndev << 16),
  312. ret_intr_type == RTAS_TYPE_MSIX, req_num);
  313. rtas_st(rets, 0, 0);
  314. rtas_st(rets, 1, req_num);
  315. rtas_st(rets, 2, ++seq_num);
  316. rtas_st(rets, 3, ret_intr_type);
  317. trace_spapr_pci_rtas_ibm_change_msi(func, req_num);
  318. }
  319. static void rtas_ibm_query_interrupt_source_number(sPAPREnvironment *spapr,
  320. uint32_t token,
  321. uint32_t nargs,
  322. target_ulong args,
  323. uint32_t nret,
  324. target_ulong rets)
  325. {
  326. uint32_t config_addr = rtas_ld(args, 0);
  327. uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
  328. unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
  329. int ndev;
  330. sPAPRPHBState *phb = NULL;
  331. /* Fins sPAPRPHBState */
  332. phb = find_phb(spapr, buid);
  333. if (!phb) {
  334. rtas_st(rets, 0, -3); /* Parameter error */
  335. return;
  336. }
  337. /* Find device descriptor and start IRQ */
  338. ndev = spapr_msicfg_find(phb, config_addr, false);
  339. if (ndev < 0) {
  340. trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
  341. rtas_st(rets, 0, -1); /* Hardware error */
  342. return;
  343. }
  344. intr_src_num = phb->msi_table[ndev].irq + ioa_intr_num;
  345. trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
  346. intr_src_num);
  347. rtas_st(rets, 0, 0);
  348. rtas_st(rets, 1, intr_src_num);
  349. rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
  350. }
  351. static int pci_spapr_swizzle(int slot, int pin)
  352. {
  353. return (slot + pin) % PCI_NUM_PINS;
  354. }
  355. static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
  356. {
  357. /*
  358. * Here we need to convert pci_dev + irq_num to some unique value
  359. * which is less than number of IRQs on the specific bus (4). We
  360. * use standard PCI swizzling, that is (slot number + pin number)
  361. * % 4.
  362. */
  363. return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
  364. }
  365. static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
  366. {
  367. /*
  368. * Here we use the number returned by pci_spapr_map_irq to find a
  369. * corresponding qemu_irq.
  370. */
  371. sPAPRPHBState *phb = opaque;
  372. trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
  373. qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
  374. }
  375. static uint64_t spapr_io_read(void *opaque, hwaddr addr,
  376. unsigned size)
  377. {
  378. switch (size) {
  379. case 1:
  380. return cpu_inb(addr);
  381. case 2:
  382. return cpu_inw(addr);
  383. case 4:
  384. return cpu_inl(addr);
  385. }
  386. assert(0);
  387. }
  388. static void spapr_io_write(void *opaque, hwaddr addr,
  389. uint64_t data, unsigned size)
  390. {
  391. switch (size) {
  392. case 1:
  393. cpu_outb(addr, data);
  394. return;
  395. case 2:
  396. cpu_outw(addr, data);
  397. return;
  398. case 4:
  399. cpu_outl(addr, data);
  400. return;
  401. }
  402. assert(0);
  403. }
  404. static const MemoryRegionOps spapr_io_ops = {
  405. .endianness = DEVICE_LITTLE_ENDIAN,
  406. .read = spapr_io_read,
  407. .write = spapr_io_write
  408. };
  409. /*
  410. * MSI/MSIX memory region implementation.
  411. * The handler handles both MSI and MSIX.
  412. * For MSI-X, the vector number is encoded as a part of the address,
  413. * data is set to 0.
  414. * For MSI, the vector number is encoded in least bits in data.
  415. */
  416. static void spapr_msi_write(void *opaque, hwaddr addr,
  417. uint64_t data, unsigned size)
  418. {
  419. sPAPRPHBState *phb = opaque;
  420. int ndev = addr >> 16;
  421. int vec = ((addr & 0xFFFF) >> 2) | data;
  422. uint32_t irq = phb->msi_table[ndev].irq + vec;
  423. trace_spapr_pci_msi_write(addr, data, irq);
  424. qemu_irq_pulse(xics_get_qirq(spapr->icp, irq));
  425. }
  426. static const MemoryRegionOps spapr_msi_ops = {
  427. /* There is no .read as the read result is undefined by PCI spec */
  428. .read = NULL,
  429. .write = spapr_msi_write,
  430. .endianness = DEVICE_LITTLE_ENDIAN
  431. };
  432. /*
  433. * PHB PCI device
  434. */
  435. static DMAContext *spapr_pci_dma_context_fn(PCIBus *bus, void *opaque,
  436. int devfn)
  437. {
  438. sPAPRPHBState *phb = opaque;
  439. return phb->dma;
  440. }
  441. static int spapr_phb_init(SysBusDevice *s)
  442. {
  443. sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
  444. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  445. char *namebuf;
  446. int i;
  447. PCIBus *bus;
  448. if (sphb->index != -1) {
  449. hwaddr windows_base;
  450. if ((sphb->buid != -1) || (sphb->dma_liobn != -1)
  451. || (sphb->mem_win_addr != -1)
  452. || (sphb->io_win_addr != -1)
  453. || (sphb->msi_win_addr != -1)) {
  454. fprintf(stderr, "Either \"index\" or other parameters must"
  455. " be specified for PAPR PHB, not both\n");
  456. return -1;
  457. }
  458. sphb->buid = SPAPR_PCI_BASE_BUID + sphb->index;
  459. sphb->dma_liobn = SPAPR_PCI_BASE_LIOBN + sphb->index;
  460. windows_base = SPAPR_PCI_WINDOW_BASE
  461. + sphb->index * SPAPR_PCI_WINDOW_SPACING;
  462. sphb->mem_win_addr = windows_base + SPAPR_PCI_MMIO_WIN_OFF;
  463. sphb->io_win_addr = windows_base + SPAPR_PCI_IO_WIN_OFF;
  464. sphb->msi_win_addr = windows_base + SPAPR_PCI_MSI_WIN_OFF;
  465. }
  466. if (sphb->buid == -1) {
  467. fprintf(stderr, "BUID not specified for PHB\n");
  468. return -1;
  469. }
  470. if (sphb->dma_liobn == -1) {
  471. fprintf(stderr, "LIOBN not specified for PHB\n");
  472. return -1;
  473. }
  474. if (sphb->mem_win_addr == -1) {
  475. fprintf(stderr, "Memory window address not specified for PHB\n");
  476. return -1;
  477. }
  478. if (sphb->io_win_addr == -1) {
  479. fprintf(stderr, "IO window address not specified for PHB\n");
  480. return -1;
  481. }
  482. if (sphb->msi_win_addr == -1) {
  483. fprintf(stderr, "MSI window address not specified for PHB\n");
  484. return -1;
  485. }
  486. if (find_phb(spapr, sphb->buid)) {
  487. fprintf(stderr, "PCI host bridges must have unique BUIDs\n");
  488. return -1;
  489. }
  490. sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
  491. if (!sphb->busname) {
  492. sphb->busname = sphb->dtbusname;
  493. }
  494. namebuf = alloca(strlen(sphb->dtbusname) + 32);
  495. /* Initialize memory regions */
  496. sprintf(namebuf, "%s.mmio", sphb->dtbusname);
  497. memory_region_init(&sphb->memspace, namebuf, INT64_MAX);
  498. sprintf(namebuf, "%s.mmio-alias", sphb->dtbusname);
  499. memory_region_init_alias(&sphb->memwindow, namebuf, &sphb->memspace,
  500. SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
  501. memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
  502. &sphb->memwindow);
  503. /* On ppc, we only have MMIO no specific IO space from the CPU
  504. * perspective. In theory we ought to be able to embed the PCI IO
  505. * memory region direction in the system memory space. However,
  506. * if any of the IO BAR subregions use the old_portio mechanism,
  507. * that won't be processed properly unless accessed from the
  508. * system io address space. This hack to bounce things via
  509. * system_io works around the problem until all the users of
  510. * old_portion are updated */
  511. sprintf(namebuf, "%s.io", sphb->dtbusname);
  512. memory_region_init(&sphb->iospace, namebuf, SPAPR_PCI_IO_WIN_SIZE);
  513. /* FIXME: fix to support multiple PHBs */
  514. memory_region_add_subregion(get_system_io(), 0, &sphb->iospace);
  515. sprintf(namebuf, "%s.io-alias", sphb->dtbusname);
  516. memory_region_init_io(&sphb->iowindow, &spapr_io_ops, sphb,
  517. namebuf, SPAPR_PCI_IO_WIN_SIZE);
  518. memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
  519. &sphb->iowindow);
  520. /* As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
  521. * we need to allocate some memory to catch those writes coming
  522. * from msi_notify()/msix_notify() */
  523. if (msi_supported) {
  524. sprintf(namebuf, "%s.msi", sphb->dtbusname);
  525. memory_region_init_io(&sphb->msiwindow, &spapr_msi_ops, sphb,
  526. namebuf, SPAPR_MSIX_MAX_DEVS * 0x10000);
  527. memory_region_add_subregion(get_system_memory(), sphb->msi_win_addr,
  528. &sphb->msiwindow);
  529. }
  530. bus = pci_register_bus(DEVICE(s), sphb->busname,
  531. pci_spapr_set_irq, pci_spapr_map_irq, sphb,
  532. &sphb->memspace, &sphb->iospace,
  533. PCI_DEVFN(0, 0), PCI_NUM_PINS);
  534. phb->bus = bus;
  535. sphb->dma_window_start = 0;
  536. sphb->dma_window_size = 0x40000000;
  537. sphb->dma = spapr_tce_new_dma_context(sphb->dma_liobn, sphb->dma_window_size);
  538. if (!sphb->dma) {
  539. fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
  540. return -1;
  541. }
  542. pci_setup_iommu(bus, spapr_pci_dma_context_fn, sphb);
  543. QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
  544. /* Initialize the LSI table */
  545. for (i = 0; i < PCI_NUM_PINS; i++) {
  546. uint32_t irq;
  547. irq = spapr_allocate_lsi(0);
  548. if (!irq) {
  549. return -1;
  550. }
  551. sphb->lsi_table[i].irq = irq;
  552. }
  553. return 0;
  554. }
  555. static void spapr_phb_reset(DeviceState *qdev)
  556. {
  557. SysBusDevice *s = SYS_BUS_DEVICE(qdev);
  558. sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
  559. /* Reset the IOMMU state */
  560. spapr_tce_reset(sphb->dma);
  561. }
  562. static Property spapr_phb_properties[] = {
  563. DEFINE_PROP_STRING("busname", sPAPRPHBState, busname),
  564. DEFINE_PROP_INT32("index", sPAPRPHBState, index, -1),
  565. DEFINE_PROP_HEX64("buid", sPAPRPHBState, buid, -1),
  566. DEFINE_PROP_HEX32("liobn", sPAPRPHBState, dma_liobn, -1),
  567. DEFINE_PROP_HEX64("mem_win_addr", sPAPRPHBState, mem_win_addr, -1),
  568. DEFINE_PROP_HEX64("mem_win_size", sPAPRPHBState, mem_win_size,
  569. SPAPR_PCI_MMIO_WIN_SIZE),
  570. DEFINE_PROP_HEX64("io_win_addr", sPAPRPHBState, io_win_addr, -1),
  571. DEFINE_PROP_HEX64("io_win_size", sPAPRPHBState, io_win_size,
  572. SPAPR_PCI_IO_WIN_SIZE),
  573. DEFINE_PROP_HEX64("msi_win_addr", sPAPRPHBState, msi_win_addr, -1),
  574. DEFINE_PROP_END_OF_LIST(),
  575. };
  576. static void spapr_phb_class_init(ObjectClass *klass, void *data)
  577. {
  578. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  579. DeviceClass *dc = DEVICE_CLASS(klass);
  580. sdc->init = spapr_phb_init;
  581. dc->props = spapr_phb_properties;
  582. dc->reset = spapr_phb_reset;
  583. }
  584. static const TypeInfo spapr_phb_info = {
  585. .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
  586. .parent = TYPE_PCI_HOST_BRIDGE,
  587. .instance_size = sizeof(sPAPRPHBState),
  588. .class_init = spapr_phb_class_init,
  589. };
  590. PCIHostState *spapr_create_phb(sPAPREnvironment *spapr, int index,
  591. const char *busname)
  592. {
  593. DeviceState *dev;
  594. dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
  595. qdev_prop_set_uint32(dev, "index", index);
  596. qdev_prop_set_string(dev, "busname", busname);
  597. qdev_init_nofail(dev);
  598. return PCI_HOST_BRIDGE(dev);
  599. }
  600. /* Macros to operate with address in OF binding to PCI */
  601. #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
  602. #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
  603. #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
  604. #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
  605. #define b_ss(x) b_x((x), 24, 2) /* the space code */
  606. #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
  607. #define b_ddddd(x) b_x((x), 11, 5) /* device number */
  608. #define b_fff(x) b_x((x), 8, 3) /* function number */
  609. #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
  610. int spapr_populate_pci_dt(sPAPRPHBState *phb,
  611. uint32_t xics_phandle,
  612. void *fdt)
  613. {
  614. int bus_off, i, j;
  615. char nodename[256];
  616. uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
  617. struct {
  618. uint32_t hi;
  619. uint64_t child;
  620. uint64_t parent;
  621. uint64_t size;
  622. } QEMU_PACKED ranges[] = {
  623. {
  624. cpu_to_be32(b_ss(1)), cpu_to_be64(0),
  625. cpu_to_be64(phb->io_win_addr),
  626. cpu_to_be64(memory_region_size(&phb->iospace)),
  627. },
  628. {
  629. cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
  630. cpu_to_be64(phb->mem_win_addr),
  631. cpu_to_be64(memory_region_size(&phb->memwindow)),
  632. },
  633. };
  634. uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
  635. uint32_t interrupt_map_mask[] = {
  636. cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
  637. uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
  638. /* Start populating the FDT */
  639. sprintf(nodename, "pci@%" PRIx64, phb->buid);
  640. bus_off = fdt_add_subnode(fdt, 0, nodename);
  641. if (bus_off < 0) {
  642. return bus_off;
  643. }
  644. #define _FDT(exp) \
  645. do { \
  646. int ret = (exp); \
  647. if (ret < 0) { \
  648. return ret; \
  649. } \
  650. } while (0)
  651. /* Write PHB properties */
  652. _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
  653. _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
  654. _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
  655. _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
  656. _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
  657. _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
  658. _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
  659. _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges)));
  660. _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
  661. _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
  662. /* Build the interrupt-map, this must matches what is done
  663. * in pci_spapr_map_irq
  664. */
  665. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
  666. &interrupt_map_mask, sizeof(interrupt_map_mask)));
  667. for (i = 0; i < PCI_SLOT_MAX; i++) {
  668. for (j = 0; j < PCI_NUM_PINS; j++) {
  669. uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
  670. int lsi_num = pci_spapr_swizzle(i, j);
  671. irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
  672. irqmap[1] = 0;
  673. irqmap[2] = 0;
  674. irqmap[3] = cpu_to_be32(j+1);
  675. irqmap[4] = cpu_to_be32(xics_phandle);
  676. irqmap[5] = cpu_to_be32(phb->lsi_table[lsi_num].irq);
  677. irqmap[6] = cpu_to_be32(0x8);
  678. }
  679. }
  680. /* Write interrupt map */
  681. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
  682. sizeof(interrupt_map)));
  683. spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
  684. phb->dma_liobn, phb->dma_window_start,
  685. phb->dma_window_size);
  686. return 0;
  687. }
  688. void spapr_pci_rtas_init(void)
  689. {
  690. spapr_rtas_register("read-pci-config", rtas_read_pci_config);
  691. spapr_rtas_register("write-pci-config", rtas_write_pci_config);
  692. spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config);
  693. spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config);
  694. if (msi_supported) {
  695. spapr_rtas_register("ibm,query-interrupt-source-number",
  696. rtas_ibm_query_interrupt_source_number);
  697. spapr_rtas_register("ibm,change-msi", rtas_ibm_change_msi);
  698. }
  699. }
  700. static void spapr_pci_register_types(void)
  701. {
  702. type_register_static(&spapr_phb_info);
  703. }
  704. type_init(spapr_pci_register_types)