pci-ioda.c 33 KB

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