pci-ioda.c 36 KB

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