pci-ioda.c 34 KB

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