pci-ioda.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. iommu_register_group(tbl, pci_domain_nr(pe->pbus), pe->pe_number);
  520. return;
  521. fail:
  522. /* XXX Failure: Try to fallback to 64-bit only ? */
  523. if (pe->tce32_seg >= 0)
  524. pe->tce32_seg = -1;
  525. if (tce_mem)
  526. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  527. }
  528. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  529. struct pnv_ioda_pe *pe)
  530. {
  531. struct page *tce_mem = NULL;
  532. void *addr;
  533. const __be64 *swinvp;
  534. struct iommu_table *tbl;
  535. unsigned int tce_table_size, end;
  536. int64_t rc;
  537. /* We shouldn't already have a 32-bit DMA associated */
  538. if (WARN_ON(pe->tce32_seg >= 0))
  539. return;
  540. /* The PE will reserve all possible 32-bits space */
  541. pe->tce32_seg = 0;
  542. end = (1 << ilog2(phb->ioda.m32_pci_base));
  543. tce_table_size = (end / 0x1000) * 8;
  544. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  545. end);
  546. /* Allocate TCE table */
  547. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  548. get_order(tce_table_size));
  549. if (!tce_mem) {
  550. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  551. goto fail;
  552. }
  553. addr = page_address(tce_mem);
  554. memset(addr, 0, tce_table_size);
  555. /*
  556. * Map TCE table through TVT. The TVE index is the PE number
  557. * shifted by 1 bit for 32-bits DMA space.
  558. */
  559. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  560. pe->pe_number << 1, 1, __pa(addr),
  561. tce_table_size, 0x1000);
  562. if (rc) {
  563. pe_err(pe, "Failed to configure 32-bit TCE table,"
  564. " err %ld\n", rc);
  565. goto fail;
  566. }
  567. /* Setup linux iommu table */
  568. tbl = &pe->tce32_table;
  569. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0);
  570. /* OPAL variant of PHB3 invalidated TCEs */
  571. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  572. if (swinvp) {
  573. /* We need a couple more fields -- an address and a data
  574. * to or. Since the bus is only printed out on table free
  575. * errors, and on the first pass the data will be a relative
  576. * bus number, print that out instead.
  577. */
  578. tbl->it_busno = 0;
  579. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  580. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  581. }
  582. iommu_init_table(tbl, phb->hose->node);
  583. return;
  584. fail:
  585. if (pe->tce32_seg >= 0)
  586. pe->tce32_seg = -1;
  587. if (tce_mem)
  588. __free_pages(tce_mem, get_order(tce_table_size));
  589. }
  590. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  591. {
  592. struct pci_controller *hose = phb->hose;
  593. unsigned int residual, remaining, segs, tw, base;
  594. struct pnv_ioda_pe *pe;
  595. /* If we have more PE# than segments available, hand out one
  596. * per PE until we run out and let the rest fail. If not,
  597. * then we assign at least one segment per PE, plus more based
  598. * on the amount of devices under that PE
  599. */
  600. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  601. residual = 0;
  602. else
  603. residual = phb->ioda.tce32_count -
  604. phb->ioda.dma_pe_count;
  605. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  606. hose->global_number, phb->ioda.tce32_count);
  607. pr_info("PCI: %d PE# for a total weight of %d\n",
  608. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  609. /* Walk our PE list and configure their DMA segments, hand them
  610. * out one base segment plus any residual segments based on
  611. * weight
  612. */
  613. remaining = phb->ioda.tce32_count;
  614. tw = phb->ioda.dma_weight;
  615. base = 0;
  616. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  617. if (!pe->dma_weight)
  618. continue;
  619. if (!remaining) {
  620. pe_warn(pe, "No DMA32 resources available\n");
  621. continue;
  622. }
  623. segs = 1;
  624. if (residual) {
  625. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  626. if (segs > remaining)
  627. segs = remaining;
  628. }
  629. /*
  630. * For IODA2 compliant PHB3, we needn't care about the weight.
  631. * The all available 32-bits DMA space will be assigned to
  632. * the specific PE.
  633. */
  634. if (phb->type == PNV_PHB_IODA1) {
  635. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  636. pe->dma_weight, segs);
  637. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  638. } else {
  639. pe_info(pe, "Assign DMA32 space\n");
  640. segs = 0;
  641. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  642. }
  643. remaining -= segs;
  644. base += segs;
  645. }
  646. }
  647. #ifdef CONFIG_PCI_MSI
  648. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  649. {
  650. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  651. struct irq_chip *chip = irq_data_get_irq_chip(d);
  652. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  653. ioda.irq_chip);
  654. int64_t rc;
  655. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  656. WARN_ON_ONCE(rc);
  657. icp_native_eoi(d);
  658. }
  659. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  660. unsigned int hwirq, unsigned int virq,
  661. unsigned int is_64, struct msi_msg *msg)
  662. {
  663. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  664. struct pci_dn *pdn = pci_get_pdn(dev);
  665. struct irq_data *idata;
  666. struct irq_chip *ichip;
  667. unsigned int xive_num = hwirq - phb->msi_base;
  668. uint64_t addr64;
  669. uint32_t addr32, data;
  670. int rc;
  671. /* No PE assigned ? bail out ... no MSI for you ! */
  672. if (pe == NULL)
  673. return -ENXIO;
  674. /* Check if we have an MVE */
  675. if (pe->mve_number < 0)
  676. return -ENXIO;
  677. /* Force 32-bit MSI on some broken devices */
  678. if (pdn && pdn->force_32bit_msi)
  679. is_64 = 0;
  680. /* Assign XIVE to PE */
  681. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  682. if (rc) {
  683. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  684. pci_name(dev), rc, xive_num);
  685. return -EIO;
  686. }
  687. if (is_64) {
  688. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  689. &addr64, &data);
  690. if (rc) {
  691. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  692. pci_name(dev), rc);
  693. return -EIO;
  694. }
  695. msg->address_hi = addr64 >> 32;
  696. msg->address_lo = addr64 & 0xfffffffful;
  697. } else {
  698. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  699. &addr32, &data);
  700. if (rc) {
  701. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  702. pci_name(dev), rc);
  703. return -EIO;
  704. }
  705. msg->address_hi = 0;
  706. msg->address_lo = addr32;
  707. }
  708. msg->data = data;
  709. /*
  710. * Change the IRQ chip for the MSI interrupts on PHB3.
  711. * The corresponding IRQ chip should be populated for
  712. * the first time.
  713. */
  714. if (phb->type == PNV_PHB_IODA2) {
  715. if (!phb->ioda.irq_chip_init) {
  716. idata = irq_get_irq_data(virq);
  717. ichip = irq_data_get_irq_chip(idata);
  718. phb->ioda.irq_chip_init = 1;
  719. phb->ioda.irq_chip = *ichip;
  720. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  721. }
  722. irq_set_chip(virq, &phb->ioda.irq_chip);
  723. }
  724. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  725. " address=%x_%08x data=%x PE# %d\n",
  726. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  727. msg->address_hi, msg->address_lo, data, pe->pe_number);
  728. return 0;
  729. }
  730. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  731. {
  732. unsigned int count;
  733. const __be32 *prop = of_get_property(phb->hose->dn,
  734. "ibm,opal-msi-ranges", NULL);
  735. if (!prop) {
  736. /* BML Fallback */
  737. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  738. }
  739. if (!prop)
  740. return;
  741. phb->msi_base = be32_to_cpup(prop);
  742. count = be32_to_cpup(prop + 1);
  743. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  744. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  745. phb->hose->global_number);
  746. return;
  747. }
  748. phb->msi_setup = pnv_pci_ioda_msi_setup;
  749. phb->msi32_support = 1;
  750. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  751. count, phb->msi_base);
  752. }
  753. #else
  754. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  755. #endif /* CONFIG_PCI_MSI */
  756. /*
  757. * This function is supposed to be called on basis of PE from top
  758. * to bottom style. So the the I/O or MMIO segment assigned to
  759. * parent PE could be overrided by its child PEs if necessary.
  760. */
  761. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  762. struct pnv_ioda_pe *pe)
  763. {
  764. struct pnv_phb *phb = hose->private_data;
  765. struct pci_bus_region region;
  766. struct resource *res;
  767. int i, index;
  768. int rc;
  769. /*
  770. * NOTE: We only care PCI bus based PE for now. For PCI
  771. * device based PE, for example SRIOV sensitive VF should
  772. * be figured out later.
  773. */
  774. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  775. pci_bus_for_each_resource(pe->pbus, res, i) {
  776. if (!res || !res->flags ||
  777. res->start > res->end)
  778. continue;
  779. if (res->flags & IORESOURCE_IO) {
  780. region.start = res->start - phb->ioda.io_pci_base;
  781. region.end = res->end - phb->ioda.io_pci_base;
  782. index = region.start / phb->ioda.io_segsize;
  783. while (index < phb->ioda.total_pe &&
  784. region.start <= region.end) {
  785. phb->ioda.io_segmap[index] = pe->pe_number;
  786. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  787. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  788. if (rc != OPAL_SUCCESS) {
  789. pr_err("%s: OPAL error %d when mapping IO "
  790. "segment #%d to PE#%d\n",
  791. __func__, rc, index, pe->pe_number);
  792. break;
  793. }
  794. region.start += phb->ioda.io_segsize;
  795. index++;
  796. }
  797. } else if (res->flags & IORESOURCE_MEM) {
  798. /* WARNING: Assumes M32 is mem region 0 in PHB. We need to
  799. * harden that algorithm when we start supporting M64
  800. */
  801. region.start = res->start -
  802. hose->mem_offset[0] -
  803. phb->ioda.m32_pci_base;
  804. region.end = res->end -
  805. hose->mem_offset[0] -
  806. phb->ioda.m32_pci_base;
  807. index = region.start / phb->ioda.m32_segsize;
  808. while (index < phb->ioda.total_pe &&
  809. region.start <= region.end) {
  810. phb->ioda.m32_segmap[index] = pe->pe_number;
  811. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  812. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  813. if (rc != OPAL_SUCCESS) {
  814. pr_err("%s: OPAL error %d when mapping M32 "
  815. "segment#%d to PE#%d",
  816. __func__, rc, index, pe->pe_number);
  817. break;
  818. }
  819. region.start += phb->ioda.m32_segsize;
  820. index++;
  821. }
  822. }
  823. }
  824. }
  825. static void pnv_pci_ioda_setup_seg(void)
  826. {
  827. struct pci_controller *tmp, *hose;
  828. struct pnv_phb *phb;
  829. struct pnv_ioda_pe *pe;
  830. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  831. phb = hose->private_data;
  832. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  833. pnv_ioda_setup_pe_seg(hose, pe);
  834. }
  835. }
  836. }
  837. static void pnv_pci_ioda_setup_DMA(void)
  838. {
  839. struct pci_controller *hose, *tmp;
  840. struct pnv_phb *phb;
  841. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  842. pnv_ioda_setup_dma(hose->private_data);
  843. /* Mark the PHB initialization done */
  844. phb = hose->private_data;
  845. phb->initialized = 1;
  846. }
  847. }
  848. static void pnv_pci_ioda_fixup(void)
  849. {
  850. pnv_pci_ioda_setup_PEs();
  851. pnv_pci_ioda_setup_seg();
  852. pnv_pci_ioda_setup_DMA();
  853. }
  854. /*
  855. * Returns the alignment for I/O or memory windows for P2P
  856. * bridges. That actually depends on how PEs are segmented.
  857. * For now, we return I/O or M32 segment size for PE sensitive
  858. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  859. * 1MiB for memory) will be returned.
  860. *
  861. * The current PCI bus might be put into one PE, which was
  862. * create against the parent PCI bridge. For that case, we
  863. * needn't enlarge the alignment so that we can save some
  864. * resources.
  865. */
  866. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  867. unsigned long type)
  868. {
  869. struct pci_dev *bridge;
  870. struct pci_controller *hose = pci_bus_to_host(bus);
  871. struct pnv_phb *phb = hose->private_data;
  872. int num_pci_bridges = 0;
  873. bridge = bus->self;
  874. while (bridge) {
  875. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  876. num_pci_bridges++;
  877. if (num_pci_bridges >= 2)
  878. return 1;
  879. }
  880. bridge = bridge->bus->self;
  881. }
  882. /* We need support prefetchable memory window later */
  883. if (type & IORESOURCE_MEM)
  884. return phb->ioda.m32_segsize;
  885. return phb->ioda.io_segsize;
  886. }
  887. /* Prevent enabling devices for which we couldn't properly
  888. * assign a PE
  889. */
  890. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  891. {
  892. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  893. struct pnv_phb *phb = hose->private_data;
  894. struct pci_dn *pdn;
  895. /* The function is probably called while the PEs have
  896. * not be created yet. For example, resource reassignment
  897. * during PCI probe period. We just skip the check if
  898. * PEs isn't ready.
  899. */
  900. if (!phb->initialized)
  901. return 0;
  902. pdn = pci_get_pdn(dev);
  903. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  904. return -EINVAL;
  905. return 0;
  906. }
  907. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  908. u32 devfn)
  909. {
  910. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  911. }
  912. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  913. {
  914. opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
  915. OPAL_ASSERT_RESET);
  916. }
  917. void __init pnv_pci_init_ioda_phb(struct device_node *np, int ioda_type)
  918. {
  919. struct pci_controller *hose;
  920. static int primary = 1;
  921. struct pnv_phb *phb;
  922. unsigned long size, m32map_off, iomap_off, pemap_off;
  923. const u64 *prop64;
  924. const u32 *prop32;
  925. u64 phb_id;
  926. void *aux;
  927. long rc;
  928. pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  929. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  930. if (!prop64) {
  931. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  932. return;
  933. }
  934. phb_id = be64_to_cpup(prop64);
  935. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  936. phb = alloc_bootmem(sizeof(struct pnv_phb));
  937. if (phb) {
  938. memset(phb, 0, sizeof(struct pnv_phb));
  939. phb->hose = hose = pcibios_alloc_controller(np);
  940. }
  941. if (!phb || !phb->hose) {
  942. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  943. np->full_name);
  944. return;
  945. }
  946. spin_lock_init(&phb->lock);
  947. /* XXX Use device-tree */
  948. hose->first_busno = 0;
  949. hose->last_busno = 0xff;
  950. hose->private_data = phb;
  951. phb->opal_id = phb_id;
  952. phb->type = ioda_type;
  953. /* Detect specific models for error handling */
  954. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  955. phb->model = PNV_PHB_MODEL_P7IOC;
  956. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  957. phb->model = PNV_PHB_MODEL_PHB3;
  958. else
  959. phb->model = PNV_PHB_MODEL_UNKNOWN;
  960. /* Parse 32-bit and IO ranges (if any) */
  961. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  962. primary = 0;
  963. /* Get registers */
  964. phb->regs = of_iomap(np, 0);
  965. if (phb->regs == NULL)
  966. pr_err(" Failed to map registers !\n");
  967. /* Initialize more IODA stuff */
  968. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  969. if (!prop32)
  970. phb->ioda.total_pe = 1;
  971. else
  972. phb->ioda.total_pe = *prop32;
  973. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  974. /* FW Has already off top 64k of M32 space (MSI space) */
  975. phb->ioda.m32_size += 0x10000;
  976. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  977. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  978. phb->ioda.io_size = hose->pci_io_size;
  979. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  980. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  981. /* Allocate aux data & arrays
  982. *
  983. * XXX TODO: Don't allocate io segmap on PHB3
  984. */
  985. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  986. m32map_off = size;
  987. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  988. iomap_off = size;
  989. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  990. pemap_off = size;
  991. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  992. aux = alloc_bootmem(size);
  993. memset(aux, 0, size);
  994. phb->ioda.pe_alloc = aux;
  995. phb->ioda.m32_segmap = aux + m32map_off;
  996. phb->ioda.io_segmap = aux + iomap_off;
  997. phb->ioda.pe_array = aux + pemap_off;
  998. set_bit(0, phb->ioda.pe_alloc);
  999. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1000. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1001. /* Calculate how many 32-bit TCE segments we have */
  1002. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1003. /* Clear unusable m64 */
  1004. hose->mem_resources[1].flags = 0;
  1005. hose->mem_resources[1].start = 0;
  1006. hose->mem_resources[1].end = 0;
  1007. hose->mem_resources[2].flags = 0;
  1008. hose->mem_resources[2].start = 0;
  1009. hose->mem_resources[2].end = 0;
  1010. #if 0 /* We should really do that ... */
  1011. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1012. window_type,
  1013. window_num,
  1014. starting_real_address,
  1015. starting_pci_address,
  1016. segment_size);
  1017. #endif
  1018. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  1019. phb->ioda.total_pe,
  1020. phb->ioda.m32_size, phb->ioda.m32_segsize,
  1021. phb->ioda.io_size, phb->ioda.io_segsize);
  1022. phb->hose->ops = &pnv_pci_ops;
  1023. /* Setup RID -> PE mapping function */
  1024. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1025. /* Setup TCEs */
  1026. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1027. /* Setup shutdown function for kexec */
  1028. phb->shutdown = pnv_pci_ioda_shutdown;
  1029. /* Setup MSI support */
  1030. pnv_pci_init_ioda_msis(phb);
  1031. /*
  1032. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1033. * to let the PCI core do resource assignment. It's supposed
  1034. * that the PCI core will do correct I/O and MMIO alignment
  1035. * for the P2P bridge bars so that each PCI bus (excluding
  1036. * the child P2P bridges) can form individual PE.
  1037. */
  1038. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1039. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1040. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1041. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1042. /* Reset IODA tables to a clean state */
  1043. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1044. if (rc)
  1045. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1046. /*
  1047. * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
  1048. * has cleared the RTT which has the same effect
  1049. */
  1050. if (ioda_type == PNV_PHB_IODA1)
  1051. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  1052. }
  1053. void pnv_pci_init_ioda2_phb(struct device_node *np)
  1054. {
  1055. pnv_pci_init_ioda_phb(np, PNV_PHB_IODA2);
  1056. }
  1057. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1058. {
  1059. struct device_node *phbn;
  1060. const u64 *prop64;
  1061. u64 hub_id;
  1062. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1063. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1064. if (!prop64) {
  1065. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1066. return;
  1067. }
  1068. hub_id = be64_to_cpup(prop64);
  1069. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1070. /* Count child PHBs */
  1071. for_each_child_of_node(np, phbn) {
  1072. /* Look for IODA1 PHBs */
  1073. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1074. pnv_pci_init_ioda_phb(phbn, PNV_PHB_IODA1);
  1075. }
  1076. }