pci-ioda.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * Support PCI/PCIe on PowerNV platforms
  3. *
  4. * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/msi.h>
  21. #include <asm/sections.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <asm/msi_bitmap.h>
  27. #include <asm/ppc-pci.h>
  28. #include <asm/opal.h>
  29. #include <asm/iommu.h>
  30. #include <asm/tce.h>
  31. #include <asm/xics.h>
  32. #include "powernv.h"
  33. #include "pci.h"
  34. #define define_pe_printk_level(func, kern_level) \
  35. static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
  36. { \
  37. struct va_format vaf; \
  38. va_list args; \
  39. char pfix[32]; \
  40. int r; \
  41. \
  42. va_start(args, fmt); \
  43. \
  44. vaf.fmt = fmt; \
  45. vaf.va = &args; \
  46. \
  47. if (pe->pdev) \
  48. strlcpy(pfix, dev_name(&pe->pdev->dev), \
  49. sizeof(pfix)); \
  50. else \
  51. sprintf(pfix, "%04x:%02x ", \
  52. pci_domain_nr(pe->pbus), \
  53. pe->pbus->number); \
  54. r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
  55. pfix, pe->pe_number, &vaf); \
  56. \
  57. va_end(args); \
  58. \
  59. return r; \
  60. } \
  61. define_pe_printk_level(pe_err, KERN_ERR);
  62. define_pe_printk_level(pe_warn, KERN_WARNING);
  63. define_pe_printk_level(pe_info, KERN_INFO);
  64. static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
  65. {
  66. struct device_node *np;
  67. np = pci_device_to_OF_node(dev);
  68. if (!np)
  69. return NULL;
  70. return PCI_DN(np);
  71. }
  72. static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
  73. {
  74. unsigned long pe;
  75. do {
  76. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  77. phb->ioda.total_pe, 0);
  78. if (pe >= phb->ioda.total_pe)
  79. return IODA_INVALID_PE;
  80. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  81. phb->ioda.pe_array[pe].phb = phb;
  82. phb->ioda.pe_array[pe].pe_number = pe;
  83. return pe;
  84. }
  85. static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  86. {
  87. WARN_ON(phb->ioda.pe_array[pe].pdev);
  88. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  89. clear_bit(pe, phb->ioda.pe_alloc);
  90. }
  91. /* Currently those 2 are only used when MSIs are enabled, this will change
  92. * but in the meantime, we need to protect them to avoid warnings
  93. */
  94. #ifdef CONFIG_PCI_MSI
  95. static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  96. {
  97. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  98. struct pnv_phb *phb = hose->private_data;
  99. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  100. if (!pdn)
  101. return NULL;
  102. if (pdn->pe_number == IODA_INVALID_PE)
  103. return NULL;
  104. return &phb->ioda.pe_array[pdn->pe_number];
  105. }
  106. #endif /* CONFIG_PCI_MSI */
  107. static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  108. {
  109. struct pci_dev *parent;
  110. uint8_t bcomp, dcomp, fcomp;
  111. long rc, rid_end, rid;
  112. /* Bus validation ? */
  113. if (pe->pbus) {
  114. int count;
  115. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  116. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  117. parent = pe->pbus->self;
  118. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  119. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  120. else
  121. count = 1;
  122. switch(count) {
  123. case 1: bcomp = OpalPciBusAll; break;
  124. case 2: bcomp = OpalPciBus7Bits; break;
  125. case 4: bcomp = OpalPciBus6Bits; break;
  126. case 8: bcomp = OpalPciBus5Bits; break;
  127. case 16: bcomp = OpalPciBus4Bits; break;
  128. case 32: bcomp = OpalPciBus3Bits; break;
  129. default:
  130. pr_err("%s: Number of subordinate busses %d"
  131. " unsupported\n",
  132. pci_name(pe->pbus->self), count);
  133. /* Do an exact match only */
  134. bcomp = OpalPciBusAll;
  135. }
  136. rid_end = pe->rid + (count << 8);
  137. } else {
  138. parent = pe->pdev->bus->self;
  139. bcomp = OpalPciBusAll;
  140. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  141. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  142. rid_end = pe->rid + 1;
  143. }
  144. /* Associate PE in PELT */
  145. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  146. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  147. if (rc) {
  148. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  149. return -ENXIO;
  150. }
  151. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  152. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  153. /* Add to all parents PELT-V */
  154. while (parent) {
  155. struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
  156. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  157. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  158. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  159. /* XXX What to do in case of error ? */
  160. }
  161. parent = parent->bus->self;
  162. }
  163. /* Setup reverse map */
  164. for (rid = pe->rid; rid < rid_end; rid++)
  165. phb->ioda.pe_rmap[rid] = pe->pe_number;
  166. /* Setup one MVTs on IODA1 */
  167. if (phb->type == PNV_PHB_IODA1) {
  168. pe->mve_number = pe->pe_number;
  169. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
  170. pe->pe_number);
  171. if (rc) {
  172. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  173. rc, pe->mve_number);
  174. pe->mve_number = -1;
  175. } else {
  176. rc = opal_pci_set_mve_enable(phb->opal_id,
  177. pe->mve_number, OPAL_ENABLE_MVE);
  178. if (rc) {
  179. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  180. rc, pe->mve_number);
  181. pe->mve_number = -1;
  182. }
  183. }
  184. } else if (phb->type == PNV_PHB_IODA2)
  185. pe->mve_number = 0;
  186. return 0;
  187. }
  188. static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  189. struct pnv_ioda_pe *pe)
  190. {
  191. struct pnv_ioda_pe *lpe;
  192. list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
  193. if (lpe->dma_weight < pe->dma_weight) {
  194. list_add_tail(&pe->dma_link, &lpe->dma_link);
  195. return;
  196. }
  197. }
  198. list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
  199. }
  200. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  201. {
  202. /* This is quite simplistic. The "base" weight of a device
  203. * is 10. 0 means no DMA is to be accounted for it.
  204. */
  205. /* If it's a bridge, no DMA */
  206. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  207. return 0;
  208. /* Reduce the weight of slow USB controllers */
  209. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  210. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  211. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  212. return 3;
  213. /* Increase the weight of RAID (includes Obsidian) */
  214. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  215. return 15;
  216. /* Default */
  217. return 10;
  218. }
  219. #if 0
  220. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  221. {
  222. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  223. struct pnv_phb *phb = hose->private_data;
  224. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  225. struct pnv_ioda_pe *pe;
  226. int pe_num;
  227. if (!pdn) {
  228. pr_err("%s: Device tree node not associated properly\n",
  229. pci_name(dev));
  230. return NULL;
  231. }
  232. if (pdn->pe_number != IODA_INVALID_PE)
  233. return NULL;
  234. /* PE#0 has been pre-set */
  235. if (dev->bus->number == 0)
  236. pe_num = 0;
  237. else
  238. pe_num = pnv_ioda_alloc_pe(phb);
  239. if (pe_num == IODA_INVALID_PE) {
  240. pr_warning("%s: Not enough PE# available, disabling device\n",
  241. pci_name(dev));
  242. return NULL;
  243. }
  244. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  245. * pointer in the PE data structure, both should be destroyed at the
  246. * same time. However, this needs to be looked at more closely again
  247. * once we actually start removing things (Hotplug, SR-IOV, ...)
  248. *
  249. * At some point we want to remove the PDN completely anyways
  250. */
  251. pe = &phb->ioda.pe_array[pe_num];
  252. pci_dev_get(dev);
  253. pdn->pcidev = dev;
  254. pdn->pe_number = pe_num;
  255. pe->pdev = dev;
  256. pe->pbus = NULL;
  257. pe->tce32_seg = -1;
  258. pe->mve_number = -1;
  259. pe->rid = dev->bus->number << 8 | pdn->devfn;
  260. pe_info(pe, "Associated device to PE\n");
  261. if (pnv_ioda_configure_pe(phb, pe)) {
  262. /* XXX What do we do here ? */
  263. if (pe_num)
  264. pnv_ioda_free_pe(phb, pe_num);
  265. pdn->pe_number = IODA_INVALID_PE;
  266. pe->pdev = NULL;
  267. pci_dev_put(dev);
  268. return NULL;
  269. }
  270. /* Assign a DMA weight to the device */
  271. pe->dma_weight = pnv_ioda_dma_weight(dev);
  272. if (pe->dma_weight != 0) {
  273. phb->ioda.dma_weight += pe->dma_weight;
  274. phb->ioda.dma_pe_count++;
  275. }
  276. /* Link the PE */
  277. pnv_ioda_link_pe_by_weight(phb, pe);
  278. return pe;
  279. }
  280. #endif /* Useful for SRIOV case */
  281. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  282. {
  283. struct pci_dev *dev;
  284. list_for_each_entry(dev, &bus->devices, bus_list) {
  285. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  286. if (pdn == NULL) {
  287. pr_warn("%s: No device node associated with device !\n",
  288. pci_name(dev));
  289. continue;
  290. }
  291. pci_dev_get(dev);
  292. pdn->pcidev = dev;
  293. pdn->pe_number = pe->pe_number;
  294. pe->dma_weight += pnv_ioda_dma_weight(dev);
  295. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  296. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  297. }
  298. }
  299. /*
  300. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  301. * single PCI bus. Another one that contains the primary PCI bus and its
  302. * subordinate PCI devices and buses. The second type of PE is normally
  303. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  304. */
  305. static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
  306. {
  307. struct pci_controller *hose = pci_bus_to_host(bus);
  308. struct pnv_phb *phb = hose->private_data;
  309. struct pnv_ioda_pe *pe;
  310. int pe_num;
  311. pe_num = pnv_ioda_alloc_pe(phb);
  312. if (pe_num == IODA_INVALID_PE) {
  313. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  314. __func__, pci_domain_nr(bus), bus->number);
  315. return;
  316. }
  317. pe = &phb->ioda.pe_array[pe_num];
  318. pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  319. pe->pbus = bus;
  320. pe->pdev = NULL;
  321. pe->tce32_seg = -1;
  322. pe->mve_number = -1;
  323. pe->rid = bus->busn_res.start << 8;
  324. pe->dma_weight = 0;
  325. if (all)
  326. pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
  327. bus->busn_res.start, bus->busn_res.end, pe_num);
  328. else
  329. pe_info(pe, "Secondary bus %d associated with PE#%d\n",
  330. bus->busn_res.start, pe_num);
  331. if (pnv_ioda_configure_pe(phb, pe)) {
  332. /* XXX What do we do here ? */
  333. if (pe_num)
  334. pnv_ioda_free_pe(phb, pe_num);
  335. pe->pbus = NULL;
  336. return;
  337. }
  338. /* Associate it with all child devices */
  339. pnv_ioda_setup_same_PE(bus, pe);
  340. /* Put PE to the list */
  341. list_add_tail(&pe->list, &phb->ioda.pe_list);
  342. /* Account for one DMA PE if at least one DMA capable device exist
  343. * below the bridge
  344. */
  345. if (pe->dma_weight != 0) {
  346. phb->ioda.dma_weight += pe->dma_weight;
  347. phb->ioda.dma_pe_count++;
  348. }
  349. /* Link the PE */
  350. pnv_ioda_link_pe_by_weight(phb, pe);
  351. }
  352. static void pnv_ioda_setup_PEs(struct pci_bus *bus)
  353. {
  354. struct pci_dev *dev;
  355. pnv_ioda_setup_bus_PE(bus, 0);
  356. list_for_each_entry(dev, &bus->devices, bus_list) {
  357. if (dev->subordinate) {
  358. if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
  359. pnv_ioda_setup_bus_PE(dev->subordinate, 1);
  360. else
  361. pnv_ioda_setup_PEs(dev->subordinate);
  362. }
  363. }
  364. }
  365. /*
  366. * Configure PEs so that the downstream PCI buses and devices
  367. * could have their associated PE#. Unfortunately, we didn't
  368. * figure out the way to identify the PLX bridge yet. So we
  369. * simply put the PCI bus and the subordinate behind the root
  370. * port to PE# here. The game rule here is expected to be changed
  371. * as soon as we can detected PLX bridge correctly.
  372. */
  373. static void pnv_pci_ioda_setup_PEs(void)
  374. {
  375. struct pci_controller *hose, *tmp;
  376. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  377. pnv_ioda_setup_PEs(hose->bus);
  378. }
  379. }
  380. static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *dev)
  381. {
  382. /* We delay DMA setup after we have assigned all PE# */
  383. }
  384. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
  385. {
  386. struct pci_dev *dev;
  387. list_for_each_entry(dev, &bus->devices, bus_list) {
  388. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  389. if (dev->subordinate)
  390. pnv_ioda_setup_bus_dma(pe, dev->subordinate);
  391. }
  392. }
  393. static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
  394. u64 *startp, u64 *endp)
  395. {
  396. u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
  397. unsigned long start, end, inc;
  398. start = __pa(startp);
  399. end = __pa(endp);
  400. /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
  401. if (tbl->it_busno) {
  402. start <<= 12;
  403. end <<= 12;
  404. inc = 128 << 12;
  405. start |= tbl->it_busno;
  406. end |= tbl->it_busno;
  407. } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
  408. /* p7ioc-style invalidation, 2 TCEs per write */
  409. start |= (1ull << 63);
  410. end |= (1ull << 63);
  411. inc = 16;
  412. } else {
  413. /* Default (older HW) */
  414. inc = 128;
  415. }
  416. end |= inc - 1; /* round up end to be different than start */
  417. mb(); /* Ensure above stores are visible */
  418. while (start <= end) {
  419. __raw_writeq(start, invalidate);
  420. start += inc;
  421. }
  422. /*
  423. * The iommu layer will do another mb() for us on build()
  424. * and we don't care on free()
  425. */
  426. }
  427. static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
  428. struct iommu_table *tbl,
  429. u64 *startp, u64 *endp)
  430. {
  431. unsigned long start, end, inc;
  432. u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
  433. /* We'll invalidate DMA address in PE scope */
  434. start = 0x2ul << 60;
  435. start |= (pe->pe_number & 0xFF);
  436. end = start;
  437. /* Figure out the start, end and step */
  438. inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
  439. start |= (inc << 12);
  440. inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
  441. end |= (inc << 12);
  442. inc = (0x1ul << 12);
  443. mb();
  444. while (start <= end) {
  445. __raw_writeq(start, invalidate);
  446. start += inc;
  447. }
  448. }
  449. void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
  450. u64 *startp, u64 *endp)
  451. {
  452. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  453. tce32_table);
  454. struct pnv_phb *phb = pe->phb;
  455. if (phb->type == PNV_PHB_IODA1)
  456. pnv_pci_ioda1_tce_invalidate(tbl, startp, endp);
  457. else
  458. pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp);
  459. }
  460. static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  461. struct pnv_ioda_pe *pe, unsigned int base,
  462. unsigned int segs)
  463. {
  464. struct page *tce_mem = NULL;
  465. const __be64 *swinvp;
  466. struct iommu_table *tbl;
  467. unsigned int i;
  468. int64_t rc;
  469. void *addr;
  470. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  471. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  472. /* XXX FIXME: Handle 64-bit only DMA devices */
  473. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  474. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  475. /* We shouldn't already have a 32-bit DMA associated */
  476. if (WARN_ON(pe->tce32_seg >= 0))
  477. return;
  478. /* Grab a 32-bit TCE table */
  479. pe->tce32_seg = base;
  480. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  481. (base << 28), ((base + segs) << 28) - 1);
  482. /* XXX Currently, we allocate one big contiguous table for the
  483. * TCEs. We only really need one chunk per 256M of TCE space
  484. * (ie per segment) but that's an optimization for later, it
  485. * requires some added smarts with our get/put_tce implementation
  486. */
  487. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  488. get_order(TCE32_TABLE_SIZE * segs));
  489. if (!tce_mem) {
  490. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  491. goto fail;
  492. }
  493. addr = page_address(tce_mem);
  494. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  495. /* Configure HW */
  496. for (i = 0; i < segs; i++) {
  497. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  498. pe->pe_number,
  499. base + i, 1,
  500. __pa(addr) + TCE32_TABLE_SIZE * i,
  501. TCE32_TABLE_SIZE, 0x1000);
  502. if (rc) {
  503. pe_err(pe, " Failed to configure 32-bit TCE table,"
  504. " err %ld\n", rc);
  505. goto fail;
  506. }
  507. }
  508. /* Setup linux iommu table */
  509. tbl = &pe->tce32_table;
  510. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  511. base << 28);
  512. /* OPAL variant of P7IOC SW invalidated TCEs */
  513. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  514. if (swinvp) {
  515. /* We need a couple more fields -- an address and a data
  516. * to or. Since the bus is only printed out on table free
  517. * errors, and on the first pass the data will be a relative
  518. * bus number, print that out instead.
  519. */
  520. tbl->it_busno = 0;
  521. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  522. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE |
  523. TCE_PCI_SWINV_PAIR;
  524. }
  525. iommu_init_table(tbl, phb->hose->node);
  526. if (pe->pdev)
  527. set_iommu_table_base(&pe->pdev->dev, tbl);
  528. else
  529. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  530. return;
  531. fail:
  532. /* XXX Failure: Try to fallback to 64-bit only ? */
  533. if (pe->tce32_seg >= 0)
  534. pe->tce32_seg = -1;
  535. if (tce_mem)
  536. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  537. }
  538. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  539. struct pnv_ioda_pe *pe)
  540. {
  541. struct page *tce_mem = NULL;
  542. void *addr;
  543. const __be64 *swinvp;
  544. struct iommu_table *tbl;
  545. unsigned int tce_table_size, end;
  546. int64_t rc;
  547. /* We shouldn't already have a 32-bit DMA associated */
  548. if (WARN_ON(pe->tce32_seg >= 0))
  549. return;
  550. /* The PE will reserve all possible 32-bits space */
  551. pe->tce32_seg = 0;
  552. end = (1 << ilog2(phb->ioda.m32_pci_base));
  553. tce_table_size = (end / 0x1000) * 8;
  554. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  555. end);
  556. /* Allocate TCE table */
  557. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  558. get_order(tce_table_size));
  559. if (!tce_mem) {
  560. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  561. goto fail;
  562. }
  563. addr = page_address(tce_mem);
  564. memset(addr, 0, tce_table_size);
  565. /*
  566. * Map TCE table through TVT. The TVE index is the PE number
  567. * shifted by 1 bit for 32-bits DMA space.
  568. */
  569. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  570. pe->pe_number << 1, 1, __pa(addr),
  571. tce_table_size, 0x1000);
  572. if (rc) {
  573. pe_err(pe, "Failed to configure 32-bit TCE table,"
  574. " err %ld\n", rc);
  575. goto fail;
  576. }
  577. /* Setup linux iommu table */
  578. tbl = &pe->tce32_table;
  579. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0);
  580. /* OPAL variant of PHB3 invalidated TCEs */
  581. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  582. if (swinvp) {
  583. /* We need a couple more fields -- an address and a data
  584. * to or. Since the bus is only printed out on table free
  585. * errors, and on the first pass the data will be a relative
  586. * bus number, print that out instead.
  587. */
  588. tbl->it_busno = 0;
  589. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  590. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  591. }
  592. iommu_init_table(tbl, phb->hose->node);
  593. if (pe->pdev)
  594. set_iommu_table_base(&pe->pdev->dev, tbl);
  595. else
  596. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  597. return;
  598. fail:
  599. if (pe->tce32_seg >= 0)
  600. pe->tce32_seg = -1;
  601. if (tce_mem)
  602. __free_pages(tce_mem, get_order(tce_table_size));
  603. }
  604. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  605. {
  606. struct pci_controller *hose = phb->hose;
  607. unsigned int residual, remaining, segs, tw, base;
  608. struct pnv_ioda_pe *pe;
  609. /* If we have more PE# than segments available, hand out one
  610. * per PE until we run out and let the rest fail. If not,
  611. * then we assign at least one segment per PE, plus more based
  612. * on the amount of devices under that PE
  613. */
  614. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  615. residual = 0;
  616. else
  617. residual = phb->ioda.tce32_count -
  618. phb->ioda.dma_pe_count;
  619. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  620. hose->global_number, phb->ioda.tce32_count);
  621. pr_info("PCI: %d PE# for a total weight of %d\n",
  622. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  623. /* Walk our PE list and configure their DMA segments, hand them
  624. * out one base segment plus any residual segments based on
  625. * weight
  626. */
  627. remaining = phb->ioda.tce32_count;
  628. tw = phb->ioda.dma_weight;
  629. base = 0;
  630. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  631. if (!pe->dma_weight)
  632. continue;
  633. if (!remaining) {
  634. pe_warn(pe, "No DMA32 resources available\n");
  635. continue;
  636. }
  637. segs = 1;
  638. if (residual) {
  639. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  640. if (segs > remaining)
  641. segs = remaining;
  642. }
  643. /*
  644. * For IODA2 compliant PHB3, we needn't care about the weight.
  645. * The all available 32-bits DMA space will be assigned to
  646. * the specific PE.
  647. */
  648. if (phb->type == PNV_PHB_IODA1) {
  649. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  650. pe->dma_weight, segs);
  651. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  652. } else {
  653. pe_info(pe, "Assign DMA32 space\n");
  654. segs = 0;
  655. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  656. }
  657. remaining -= segs;
  658. base += segs;
  659. }
  660. }
  661. #ifdef CONFIG_PCI_MSI
  662. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  663. {
  664. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  665. struct irq_chip *chip = irq_data_get_irq_chip(d);
  666. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  667. ioda.irq_chip);
  668. int64_t rc;
  669. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  670. WARN_ON_ONCE(rc);
  671. icp_native_eoi(d);
  672. }
  673. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  674. unsigned int hwirq, unsigned int virq,
  675. unsigned int is_64, struct msi_msg *msg)
  676. {
  677. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  678. struct irq_data *idata;
  679. struct irq_chip *ichip;
  680. unsigned int xive_num = hwirq - phb->msi_base;
  681. uint64_t addr64;
  682. uint32_t addr32, data;
  683. int rc;
  684. /* No PE assigned ? bail out ... no MSI for you ! */
  685. if (pe == NULL)
  686. return -ENXIO;
  687. /* Check if we have an MVE */
  688. if (pe->mve_number < 0)
  689. return -ENXIO;
  690. /* Assign XIVE to PE */
  691. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  692. if (rc) {
  693. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  694. pci_name(dev), rc, xive_num);
  695. return -EIO;
  696. }
  697. if (is_64) {
  698. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  699. &addr64, &data);
  700. if (rc) {
  701. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  702. pci_name(dev), rc);
  703. return -EIO;
  704. }
  705. msg->address_hi = addr64 >> 32;
  706. msg->address_lo = addr64 & 0xfffffffful;
  707. } else {
  708. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  709. &addr32, &data);
  710. if (rc) {
  711. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  712. pci_name(dev), rc);
  713. return -EIO;
  714. }
  715. msg->address_hi = 0;
  716. msg->address_lo = addr32;
  717. }
  718. msg->data = data;
  719. /*
  720. * Change the IRQ chip for the MSI interrupts on PHB3.
  721. * The corresponding IRQ chip should be populated for
  722. * the first time.
  723. */
  724. if (phb->type == PNV_PHB_IODA2) {
  725. if (!phb->ioda.irq_chip_init) {
  726. idata = irq_get_irq_data(virq);
  727. ichip = irq_data_get_irq_chip(idata);
  728. phb->ioda.irq_chip_init = 1;
  729. phb->ioda.irq_chip = *ichip;
  730. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  731. }
  732. irq_set_chip(virq, &phb->ioda.irq_chip);
  733. }
  734. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  735. " address=%x_%08x data=%x PE# %d\n",
  736. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  737. msg->address_hi, msg->address_lo, data, pe->pe_number);
  738. return 0;
  739. }
  740. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  741. {
  742. unsigned int count;
  743. const __be32 *prop = of_get_property(phb->hose->dn,
  744. "ibm,opal-msi-ranges", NULL);
  745. if (!prop) {
  746. /* BML Fallback */
  747. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  748. }
  749. if (!prop)
  750. return;
  751. phb->msi_base = be32_to_cpup(prop);
  752. count = be32_to_cpup(prop + 1);
  753. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  754. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  755. phb->hose->global_number);
  756. return;
  757. }
  758. phb->msi_setup = pnv_pci_ioda_msi_setup;
  759. phb->msi32_support = 1;
  760. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  761. count, phb->msi_base);
  762. }
  763. #else
  764. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  765. #endif /* CONFIG_PCI_MSI */
  766. /*
  767. * This function is supposed to be called on basis of PE from top
  768. * to bottom style. So the the I/O or MMIO segment assigned to
  769. * parent PE could be overrided by its child PEs if necessary.
  770. */
  771. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  772. struct pnv_ioda_pe *pe)
  773. {
  774. struct pnv_phb *phb = hose->private_data;
  775. struct pci_bus_region region;
  776. struct resource *res;
  777. int i, index;
  778. int rc;
  779. /*
  780. * NOTE: We only care PCI bus based PE for now. For PCI
  781. * device based PE, for example SRIOV sensitive VF should
  782. * be figured out later.
  783. */
  784. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  785. pci_bus_for_each_resource(pe->pbus, res, i) {
  786. if (!res || !res->flags ||
  787. res->start > res->end)
  788. continue;
  789. if (res->flags & IORESOURCE_IO) {
  790. region.start = res->start - phb->ioda.io_pci_base;
  791. region.end = res->end - phb->ioda.io_pci_base;
  792. index = region.start / phb->ioda.io_segsize;
  793. while (index < phb->ioda.total_pe &&
  794. region.start <= region.end) {
  795. phb->ioda.io_segmap[index] = pe->pe_number;
  796. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  797. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  798. if (rc != OPAL_SUCCESS) {
  799. pr_err("%s: OPAL error %d when mapping IO "
  800. "segment #%d to PE#%d\n",
  801. __func__, rc, index, pe->pe_number);
  802. break;
  803. }
  804. region.start += phb->ioda.io_segsize;
  805. index++;
  806. }
  807. } else if (res->flags & IORESOURCE_MEM) {
  808. region.start = res->start -
  809. hose->pci_mem_offset -
  810. phb->ioda.m32_pci_base;
  811. region.end = res->end -
  812. hose->pci_mem_offset -
  813. phb->ioda.m32_pci_base;
  814. index = region.start / phb->ioda.m32_segsize;
  815. while (index < phb->ioda.total_pe &&
  816. region.start <= region.end) {
  817. phb->ioda.m32_segmap[index] = pe->pe_number;
  818. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  819. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  820. if (rc != OPAL_SUCCESS) {
  821. pr_err("%s: OPAL error %d when mapping M32 "
  822. "segment#%d to PE#%d",
  823. __func__, rc, index, pe->pe_number);
  824. break;
  825. }
  826. region.start += phb->ioda.m32_segsize;
  827. index++;
  828. }
  829. }
  830. }
  831. }
  832. static void pnv_pci_ioda_setup_seg(void)
  833. {
  834. struct pci_controller *tmp, *hose;
  835. struct pnv_phb *phb;
  836. struct pnv_ioda_pe *pe;
  837. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  838. phb = hose->private_data;
  839. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  840. pnv_ioda_setup_pe_seg(hose, pe);
  841. }
  842. }
  843. }
  844. static void pnv_pci_ioda_setup_DMA(void)
  845. {
  846. struct pci_controller *hose, *tmp;
  847. struct pnv_phb *phb;
  848. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  849. pnv_ioda_setup_dma(hose->private_data);
  850. /* Mark the PHB initialization done */
  851. phb = hose->private_data;
  852. phb->initialized = 1;
  853. }
  854. }
  855. static void pnv_pci_ioda_fixup(void)
  856. {
  857. pnv_pci_ioda_setup_PEs();
  858. pnv_pci_ioda_setup_seg();
  859. pnv_pci_ioda_setup_DMA();
  860. }
  861. /*
  862. * Returns the alignment for I/O or memory windows for P2P
  863. * bridges. That actually depends on how PEs are segmented.
  864. * For now, we return I/O or M32 segment size for PE sensitive
  865. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  866. * 1MiB for memory) will be returned.
  867. *
  868. * The current PCI bus might be put into one PE, which was
  869. * create against the parent PCI bridge. For that case, we
  870. * needn't enlarge the alignment so that we can save some
  871. * resources.
  872. */
  873. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  874. unsigned long type)
  875. {
  876. struct pci_dev *bridge;
  877. struct pci_controller *hose = pci_bus_to_host(bus);
  878. struct pnv_phb *phb = hose->private_data;
  879. int num_pci_bridges = 0;
  880. bridge = bus->self;
  881. while (bridge) {
  882. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  883. num_pci_bridges++;
  884. if (num_pci_bridges >= 2)
  885. return 1;
  886. }
  887. bridge = bridge->bus->self;
  888. }
  889. /* We need support prefetchable memory window later */
  890. if (type & IORESOURCE_MEM)
  891. return phb->ioda.m32_segsize;
  892. return phb->ioda.io_segsize;
  893. }
  894. /* Prevent enabling devices for which we couldn't properly
  895. * assign a PE
  896. */
  897. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  898. {
  899. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  900. struct pnv_phb *phb = hose->private_data;
  901. struct pci_dn *pdn;
  902. /* The function is probably called while the PEs have
  903. * not be created yet. For example, resource reassignment
  904. * during PCI probe period. We just skip the check if
  905. * PEs isn't ready.
  906. */
  907. if (!phb->initialized)
  908. return 0;
  909. pdn = pnv_ioda_get_pdn(dev);
  910. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  911. return -EINVAL;
  912. return 0;
  913. }
  914. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  915. u32 devfn)
  916. {
  917. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  918. }
  919. void __init pnv_pci_init_ioda_phb(struct device_node *np, int ioda_type)
  920. {
  921. struct pci_controller *hose;
  922. static int primary = 1;
  923. struct pnv_phb *phb;
  924. unsigned long size, m32map_off, iomap_off, pemap_off;
  925. const u64 *prop64;
  926. const u32 *prop32;
  927. u64 phb_id;
  928. void *aux;
  929. long rc;
  930. pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  931. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  932. if (!prop64) {
  933. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  934. return;
  935. }
  936. phb_id = be64_to_cpup(prop64);
  937. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  938. phb = alloc_bootmem(sizeof(struct pnv_phb));
  939. if (phb) {
  940. memset(phb, 0, sizeof(struct pnv_phb));
  941. phb->hose = hose = pcibios_alloc_controller(np);
  942. }
  943. if (!phb || !phb->hose) {
  944. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  945. np->full_name);
  946. return;
  947. }
  948. spin_lock_init(&phb->lock);
  949. /* XXX Use device-tree */
  950. hose->first_busno = 0;
  951. hose->last_busno = 0xff;
  952. hose->private_data = phb;
  953. phb->opal_id = phb_id;
  954. phb->type = ioda_type;
  955. /* Detect specific models for error handling */
  956. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  957. phb->model = PNV_PHB_MODEL_P7IOC;
  958. else if (of_device_is_compatible(np, "ibm,p8-pciex"))
  959. phb->model = PNV_PHB_MODEL_PHB3;
  960. else
  961. phb->model = PNV_PHB_MODEL_UNKNOWN;
  962. /* Parse 32-bit and IO ranges (if any) */
  963. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  964. primary = 0;
  965. /* Get registers */
  966. phb->regs = of_iomap(np, 0);
  967. if (phb->regs == NULL)
  968. pr_err(" Failed to map registers !\n");
  969. /* Initialize more IODA stuff */
  970. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  971. if (!prop32)
  972. phb->ioda.total_pe = 1;
  973. else
  974. phb->ioda.total_pe = *prop32;
  975. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  976. /* FW Has already off top 64k of M32 space (MSI space) */
  977. phb->ioda.m32_size += 0x10000;
  978. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  979. phb->ioda.m32_pci_base = hose->mem_resources[0].start -
  980. hose->pci_mem_offset;
  981. phb->ioda.io_size = hose->pci_io_size;
  982. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  983. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  984. /* Allocate aux data & arrays
  985. *
  986. * XXX TODO: Don't allocate io segmap on PHB3
  987. */
  988. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  989. m32map_off = size;
  990. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  991. iomap_off = size;
  992. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  993. pemap_off = size;
  994. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  995. aux = alloc_bootmem(size);
  996. memset(aux, 0, size);
  997. phb->ioda.pe_alloc = aux;
  998. phb->ioda.m32_segmap = aux + m32map_off;
  999. phb->ioda.io_segmap = aux + iomap_off;
  1000. phb->ioda.pe_array = aux + pemap_off;
  1001. set_bit(0, phb->ioda.pe_alloc);
  1002. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1003. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1004. /* Calculate how many 32-bit TCE segments we have */
  1005. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1006. /* Clear unusable m64 */
  1007. hose->mem_resources[1].flags = 0;
  1008. hose->mem_resources[1].start = 0;
  1009. hose->mem_resources[1].end = 0;
  1010. hose->mem_resources[2].flags = 0;
  1011. hose->mem_resources[2].start = 0;
  1012. hose->mem_resources[2].end = 0;
  1013. #if 0 /* We should really do that ... */
  1014. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1015. window_type,
  1016. window_num,
  1017. starting_real_address,
  1018. starting_pci_address,
  1019. segment_size);
  1020. #endif
  1021. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  1022. phb->ioda.total_pe,
  1023. phb->ioda.m32_size, phb->ioda.m32_segsize,
  1024. phb->ioda.io_size, phb->ioda.io_segsize);
  1025. phb->hose->ops = &pnv_pci_ops;
  1026. /* Setup RID -> PE mapping function */
  1027. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1028. /* Setup TCEs */
  1029. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1030. /* Setup MSI support */
  1031. pnv_pci_init_ioda_msis(phb);
  1032. /*
  1033. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1034. * to let the PCI core do resource assignment. It's supposed
  1035. * that the PCI core will do correct I/O and MMIO alignment
  1036. * for the P2P bridge bars so that each PCI bus (excluding
  1037. * the child P2P bridges) can form individual PE.
  1038. */
  1039. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1040. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1041. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1042. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1043. /* Reset IODA tables to a clean state */
  1044. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1045. if (rc)
  1046. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1047. /*
  1048. * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
  1049. * has cleared the RTT which has the same effect
  1050. */
  1051. if (ioda_type == PNV_PHB_IODA1)
  1052. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  1053. }
  1054. void pnv_pci_init_ioda2_phb(struct device_node *np)
  1055. {
  1056. pnv_pci_init_ioda_phb(np, PNV_PHB_IODA2);
  1057. }
  1058. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1059. {
  1060. struct device_node *phbn;
  1061. const u64 *prop64;
  1062. u64 hub_id;
  1063. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1064. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1065. if (!prop64) {
  1066. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1067. return;
  1068. }
  1069. hub_id = be64_to_cpup(prop64);
  1070. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1071. /* Count child PHBs */
  1072. for_each_child_of_node(np, phbn) {
  1073. /* Look for IODA1 PHBs */
  1074. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1075. pnv_pci_init_ioda_phb(phbn, PNV_PHB_IODA1);
  1076. }
  1077. }