pci-ioda.c 33 KB

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