pci-ioda.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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_probe_mode_set(EEH_PROBE_MODE_DEV);
  874. eeh_addr_cache_build();
  875. eeh_init();
  876. #endif
  877. }
  878. /*
  879. * Returns the alignment for I/O or memory windows for P2P
  880. * bridges. That actually depends on how PEs are segmented.
  881. * For now, we return I/O or M32 segment size for PE sensitive
  882. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  883. * 1MiB for memory) will be returned.
  884. *
  885. * The current PCI bus might be put into one PE, which was
  886. * create against the parent PCI bridge. For that case, we
  887. * needn't enlarge the alignment so that we can save some
  888. * resources.
  889. */
  890. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  891. unsigned long type)
  892. {
  893. struct pci_dev *bridge;
  894. struct pci_controller *hose = pci_bus_to_host(bus);
  895. struct pnv_phb *phb = hose->private_data;
  896. int num_pci_bridges = 0;
  897. bridge = bus->self;
  898. while (bridge) {
  899. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  900. num_pci_bridges++;
  901. if (num_pci_bridges >= 2)
  902. return 1;
  903. }
  904. bridge = bridge->bus->self;
  905. }
  906. /* We need support prefetchable memory window later */
  907. if (type & IORESOURCE_MEM)
  908. return phb->ioda.m32_segsize;
  909. return phb->ioda.io_segsize;
  910. }
  911. /* Prevent enabling devices for which we couldn't properly
  912. * assign a PE
  913. */
  914. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  915. {
  916. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  917. struct pnv_phb *phb = hose->private_data;
  918. struct pci_dn *pdn;
  919. /* The function is probably called while the PEs have
  920. * not be created yet. For example, resource reassignment
  921. * during PCI probe period. We just skip the check if
  922. * PEs isn't ready.
  923. */
  924. if (!phb->initialized)
  925. return 0;
  926. pdn = pci_get_pdn(dev);
  927. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  928. return -EINVAL;
  929. return 0;
  930. }
  931. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  932. u32 devfn)
  933. {
  934. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  935. }
  936. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  937. {
  938. opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
  939. OPAL_ASSERT_RESET);
  940. }
  941. void __init pnv_pci_init_ioda_phb(struct device_node *np,
  942. u64 hub_id, int ioda_type)
  943. {
  944. struct pci_controller *hose;
  945. static int primary = 1;
  946. struct pnv_phb *phb;
  947. unsigned long size, m32map_off, iomap_off, pemap_off;
  948. const u64 *prop64;
  949. const u32 *prop32;
  950. u64 phb_id;
  951. void *aux;
  952. long rc;
  953. pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  954. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  955. if (!prop64) {
  956. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  957. return;
  958. }
  959. phb_id = be64_to_cpup(prop64);
  960. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  961. phb = alloc_bootmem(sizeof(struct pnv_phb));
  962. if (phb) {
  963. memset(phb, 0, sizeof(struct pnv_phb));
  964. phb->hose = hose = pcibios_alloc_controller(np);
  965. }
  966. if (!phb || !phb->hose) {
  967. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  968. np->full_name);
  969. return;
  970. }
  971. spin_lock_init(&phb->lock);
  972. /* XXX Use device-tree */
  973. hose->first_busno = 0;
  974. hose->last_busno = 0xff;
  975. hose->private_data = phb;
  976. phb->hub_id = hub_id;
  977. phb->opal_id = phb_id;
  978. phb->type = ioda_type;
  979. /* Detect specific models for error handling */
  980. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  981. phb->model = PNV_PHB_MODEL_P7IOC;
  982. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  983. phb->model = PNV_PHB_MODEL_PHB3;
  984. else
  985. phb->model = PNV_PHB_MODEL_UNKNOWN;
  986. /* Parse 32-bit and IO ranges (if any) */
  987. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  988. primary = 0;
  989. /* Get registers */
  990. phb->regs = of_iomap(np, 0);
  991. if (phb->regs == NULL)
  992. pr_err(" Failed to map registers !\n");
  993. /* Initialize more IODA stuff */
  994. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  995. if (!prop32)
  996. phb->ioda.total_pe = 1;
  997. else
  998. phb->ioda.total_pe = *prop32;
  999. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  1000. /* FW Has already off top 64k of M32 space (MSI space) */
  1001. phb->ioda.m32_size += 0x10000;
  1002. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  1003. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  1004. phb->ioda.io_size = hose->pci_io_size;
  1005. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  1006. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  1007. /* Allocate aux data & arrays
  1008. *
  1009. * XXX TODO: Don't allocate io segmap on PHB3
  1010. */
  1011. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  1012. m32map_off = size;
  1013. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  1014. iomap_off = size;
  1015. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  1016. pemap_off = size;
  1017. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  1018. aux = alloc_bootmem(size);
  1019. memset(aux, 0, size);
  1020. phb->ioda.pe_alloc = aux;
  1021. phb->ioda.m32_segmap = aux + m32map_off;
  1022. phb->ioda.io_segmap = aux + iomap_off;
  1023. phb->ioda.pe_array = aux + pemap_off;
  1024. set_bit(0, phb->ioda.pe_alloc);
  1025. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1026. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1027. /* Calculate how many 32-bit TCE segments we have */
  1028. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1029. /* Clear unusable m64 */
  1030. hose->mem_resources[1].flags = 0;
  1031. hose->mem_resources[1].start = 0;
  1032. hose->mem_resources[1].end = 0;
  1033. hose->mem_resources[2].flags = 0;
  1034. hose->mem_resources[2].start = 0;
  1035. hose->mem_resources[2].end = 0;
  1036. #if 0 /* We should really do that ... */
  1037. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1038. window_type,
  1039. window_num,
  1040. starting_real_address,
  1041. starting_pci_address,
  1042. segment_size);
  1043. #endif
  1044. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  1045. phb->ioda.total_pe,
  1046. phb->ioda.m32_size, phb->ioda.m32_segsize,
  1047. phb->ioda.io_size, phb->ioda.io_segsize);
  1048. phb->hose->ops = &pnv_pci_ops;
  1049. #ifdef CONFIG_EEH
  1050. phb->eeh_ops = &ioda_eeh_ops;
  1051. #endif
  1052. /* Setup RID -> PE mapping function */
  1053. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1054. /* Setup TCEs */
  1055. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1056. /* Setup shutdown function for kexec */
  1057. phb->shutdown = pnv_pci_ioda_shutdown;
  1058. /* Setup MSI support */
  1059. pnv_pci_init_ioda_msis(phb);
  1060. /*
  1061. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1062. * to let the PCI core do resource assignment. It's supposed
  1063. * that the PCI core will do correct I/O and MMIO alignment
  1064. * for the P2P bridge bars so that each PCI bus (excluding
  1065. * the child P2P bridges) can form individual PE.
  1066. */
  1067. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1068. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1069. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1070. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1071. /* Reset IODA tables to a clean state */
  1072. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1073. if (rc)
  1074. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1075. /*
  1076. * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
  1077. * has cleared the RTT which has the same effect
  1078. */
  1079. if (ioda_type == PNV_PHB_IODA1)
  1080. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  1081. }
  1082. void pnv_pci_init_ioda2_phb(struct device_node *np)
  1083. {
  1084. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  1085. }
  1086. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1087. {
  1088. struct device_node *phbn;
  1089. const u64 *prop64;
  1090. u64 hub_id;
  1091. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1092. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1093. if (!prop64) {
  1094. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1095. return;
  1096. }
  1097. hub_id = be64_to_cpup(prop64);
  1098. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1099. /* Count child PHBs */
  1100. for_each_child_of_node(np, phbn) {
  1101. /* Look for IODA1 PHBs */
  1102. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1103. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  1104. }
  1105. }