pci-ioda.c 36 KB

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