pci-ioda.c 36 KB

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