pci-ioda.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
  389. {
  390. struct pci_dev *dev;
  391. list_for_each_entry(dev, &bus->devices, bus_list) {
  392. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  393. if (dev->subordinate)
  394. pnv_ioda_setup_bus_dma(pe, dev->subordinate);
  395. }
  396. }
  397. static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
  398. u64 *startp, u64 *endp)
  399. {
  400. u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
  401. unsigned long start, end, inc;
  402. start = __pa(startp);
  403. end = __pa(endp);
  404. /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
  405. if (tbl->it_busno) {
  406. start <<= 12;
  407. end <<= 12;
  408. inc = 128 << 12;
  409. start |= tbl->it_busno;
  410. end |= tbl->it_busno;
  411. } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
  412. /* p7ioc-style invalidation, 2 TCEs per write */
  413. start |= (1ull << 63);
  414. end |= (1ull << 63);
  415. inc = 16;
  416. } else {
  417. /* Default (older HW) */
  418. inc = 128;
  419. }
  420. end |= inc - 1; /* round up end to be different than start */
  421. mb(); /* Ensure above stores are visible */
  422. while (start <= end) {
  423. __raw_writeq(start, invalidate);
  424. start += inc;
  425. }
  426. /*
  427. * The iommu layer will do another mb() for us on build()
  428. * and we don't care on free()
  429. */
  430. }
  431. static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
  432. struct iommu_table *tbl,
  433. u64 *startp, u64 *endp)
  434. {
  435. unsigned long start, end, inc;
  436. u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
  437. /* We'll invalidate DMA address in PE scope */
  438. start = 0x2ul << 60;
  439. start |= (pe->pe_number & 0xFF);
  440. end = start;
  441. /* Figure out the start, end and step */
  442. inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
  443. start |= (inc << 12);
  444. inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
  445. end |= (inc << 12);
  446. inc = (0x1ul << 12);
  447. mb();
  448. while (start <= end) {
  449. __raw_writeq(start, invalidate);
  450. start += inc;
  451. }
  452. }
  453. void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
  454. u64 *startp, u64 *endp)
  455. {
  456. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  457. tce32_table);
  458. struct pnv_phb *phb = pe->phb;
  459. if (phb->type == PNV_PHB_IODA1)
  460. pnv_pci_ioda1_tce_invalidate(tbl, startp, endp);
  461. else
  462. pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp);
  463. }
  464. static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  465. struct pnv_ioda_pe *pe, unsigned int base,
  466. unsigned int segs)
  467. {
  468. struct page *tce_mem = NULL;
  469. const __be64 *swinvp;
  470. struct iommu_table *tbl;
  471. unsigned int i;
  472. int64_t rc;
  473. void *addr;
  474. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  475. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  476. /* XXX FIXME: Handle 64-bit only DMA devices */
  477. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  478. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  479. /* We shouldn't already have a 32-bit DMA associated */
  480. if (WARN_ON(pe->tce32_seg >= 0))
  481. return;
  482. /* Grab a 32-bit TCE table */
  483. pe->tce32_seg = base;
  484. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  485. (base << 28), ((base + segs) << 28) - 1);
  486. /* XXX Currently, we allocate one big contiguous table for the
  487. * TCEs. We only really need one chunk per 256M of TCE space
  488. * (ie per segment) but that's an optimization for later, it
  489. * requires some added smarts with our get/put_tce implementation
  490. */
  491. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  492. get_order(TCE32_TABLE_SIZE * segs));
  493. if (!tce_mem) {
  494. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  495. goto fail;
  496. }
  497. addr = page_address(tce_mem);
  498. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  499. /* Configure HW */
  500. for (i = 0; i < segs; i++) {
  501. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  502. pe->pe_number,
  503. base + i, 1,
  504. __pa(addr) + TCE32_TABLE_SIZE * i,
  505. TCE32_TABLE_SIZE, 0x1000);
  506. if (rc) {
  507. pe_err(pe, " Failed to configure 32-bit TCE table,"
  508. " err %ld\n", rc);
  509. goto fail;
  510. }
  511. }
  512. /* Setup linux iommu table */
  513. tbl = &pe->tce32_table;
  514. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  515. base << 28);
  516. /* OPAL variant of P7IOC SW invalidated TCEs */
  517. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  518. if (swinvp) {
  519. /* We need a couple more fields -- an address and a data
  520. * to or. Since the bus is only printed out on table free
  521. * errors, and on the first pass the data will be a relative
  522. * bus number, print that out instead.
  523. */
  524. tbl->it_busno = 0;
  525. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  526. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE |
  527. TCE_PCI_SWINV_PAIR;
  528. }
  529. iommu_init_table(tbl, phb->hose->node);
  530. iommu_register_group(tbl, pci_domain_nr(pe->pbus), pe->pe_number);
  531. if (pe->pdev)
  532. set_iommu_table_base(&pe->pdev->dev, tbl);
  533. else
  534. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  535. return;
  536. fail:
  537. /* XXX Failure: Try to fallback to 64-bit only ? */
  538. if (pe->tce32_seg >= 0)
  539. pe->tce32_seg = -1;
  540. if (tce_mem)
  541. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  542. }
  543. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  544. struct pnv_ioda_pe *pe)
  545. {
  546. struct page *tce_mem = NULL;
  547. void *addr;
  548. const __be64 *swinvp;
  549. struct iommu_table *tbl;
  550. unsigned int tce_table_size, end;
  551. int64_t rc;
  552. /* We shouldn't already have a 32-bit DMA associated */
  553. if (WARN_ON(pe->tce32_seg >= 0))
  554. return;
  555. /* The PE will reserve all possible 32-bits space */
  556. pe->tce32_seg = 0;
  557. end = (1 << ilog2(phb->ioda.m32_pci_base));
  558. tce_table_size = (end / 0x1000) * 8;
  559. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  560. end);
  561. /* Allocate TCE table */
  562. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  563. get_order(tce_table_size));
  564. if (!tce_mem) {
  565. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  566. goto fail;
  567. }
  568. addr = page_address(tce_mem);
  569. memset(addr, 0, tce_table_size);
  570. /*
  571. * Map TCE table through TVT. The TVE index is the PE number
  572. * shifted by 1 bit for 32-bits DMA space.
  573. */
  574. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  575. pe->pe_number << 1, 1, __pa(addr),
  576. tce_table_size, 0x1000);
  577. if (rc) {
  578. pe_err(pe, "Failed to configure 32-bit TCE table,"
  579. " err %ld\n", rc);
  580. goto fail;
  581. }
  582. /* Setup linux iommu table */
  583. tbl = &pe->tce32_table;
  584. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0);
  585. /* OPAL variant of PHB3 invalidated TCEs */
  586. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  587. if (swinvp) {
  588. /* We need a couple more fields -- an address and a data
  589. * to or. Since the bus is only printed out on table free
  590. * errors, and on the first pass the data will be a relative
  591. * bus number, print that out instead.
  592. */
  593. tbl->it_busno = 0;
  594. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  595. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  596. }
  597. iommu_init_table(tbl, phb->hose->node);
  598. if (pe->pdev)
  599. set_iommu_table_base(&pe->pdev->dev, tbl);
  600. else
  601. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  602. return;
  603. fail:
  604. if (pe->tce32_seg >= 0)
  605. pe->tce32_seg = -1;
  606. if (tce_mem)
  607. __free_pages(tce_mem, get_order(tce_table_size));
  608. }
  609. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  610. {
  611. struct pci_controller *hose = phb->hose;
  612. unsigned int residual, remaining, segs, tw, base;
  613. struct pnv_ioda_pe *pe;
  614. /* If we have more PE# than segments available, hand out one
  615. * per PE until we run out and let the rest fail. If not,
  616. * then we assign at least one segment per PE, plus more based
  617. * on the amount of devices under that PE
  618. */
  619. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  620. residual = 0;
  621. else
  622. residual = phb->ioda.tce32_count -
  623. phb->ioda.dma_pe_count;
  624. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  625. hose->global_number, phb->ioda.tce32_count);
  626. pr_info("PCI: %d PE# for a total weight of %d\n",
  627. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  628. /* Walk our PE list and configure their DMA segments, hand them
  629. * out one base segment plus any residual segments based on
  630. * weight
  631. */
  632. remaining = phb->ioda.tce32_count;
  633. tw = phb->ioda.dma_weight;
  634. base = 0;
  635. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  636. if (!pe->dma_weight)
  637. continue;
  638. if (!remaining) {
  639. pe_warn(pe, "No DMA32 resources available\n");
  640. continue;
  641. }
  642. segs = 1;
  643. if (residual) {
  644. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  645. if (segs > remaining)
  646. segs = remaining;
  647. }
  648. /*
  649. * For IODA2 compliant PHB3, we needn't care about the weight.
  650. * The all available 32-bits DMA space will be assigned to
  651. * the specific PE.
  652. */
  653. if (phb->type == PNV_PHB_IODA1) {
  654. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  655. pe->dma_weight, segs);
  656. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  657. } else {
  658. pe_info(pe, "Assign DMA32 space\n");
  659. segs = 0;
  660. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  661. }
  662. remaining -= segs;
  663. base += segs;
  664. }
  665. }
  666. #ifdef CONFIG_PCI_MSI
  667. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  668. {
  669. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  670. struct irq_chip *chip = irq_data_get_irq_chip(d);
  671. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  672. ioda.irq_chip);
  673. int64_t rc;
  674. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  675. WARN_ON_ONCE(rc);
  676. icp_native_eoi(d);
  677. }
  678. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  679. unsigned int hwirq, unsigned int virq,
  680. unsigned int is_64, struct msi_msg *msg)
  681. {
  682. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  683. struct pci_dn *pdn = pci_get_pdn(dev);
  684. struct irq_data *idata;
  685. struct irq_chip *ichip;
  686. unsigned int xive_num = hwirq - phb->msi_base;
  687. uint64_t addr64;
  688. uint32_t addr32, data;
  689. int rc;
  690. /* No PE assigned ? bail out ... no MSI for you ! */
  691. if (pe == NULL)
  692. return -ENXIO;
  693. /* Check if we have an MVE */
  694. if (pe->mve_number < 0)
  695. return -ENXIO;
  696. /* Force 32-bit MSI on some broken devices */
  697. if (pdn && pdn->force_32bit_msi)
  698. is_64 = 0;
  699. /* Assign XIVE to PE */
  700. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  701. if (rc) {
  702. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  703. pci_name(dev), rc, xive_num);
  704. return -EIO;
  705. }
  706. if (is_64) {
  707. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  708. &addr64, &data);
  709. if (rc) {
  710. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  711. pci_name(dev), rc);
  712. return -EIO;
  713. }
  714. msg->address_hi = addr64 >> 32;
  715. msg->address_lo = addr64 & 0xfffffffful;
  716. } else {
  717. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  718. &addr32, &data);
  719. if (rc) {
  720. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  721. pci_name(dev), rc);
  722. return -EIO;
  723. }
  724. msg->address_hi = 0;
  725. msg->address_lo = addr32;
  726. }
  727. msg->data = data;
  728. /*
  729. * Change the IRQ chip for the MSI interrupts on PHB3.
  730. * The corresponding IRQ chip should be populated for
  731. * the first time.
  732. */
  733. if (phb->type == PNV_PHB_IODA2) {
  734. if (!phb->ioda.irq_chip_init) {
  735. idata = irq_get_irq_data(virq);
  736. ichip = irq_data_get_irq_chip(idata);
  737. phb->ioda.irq_chip_init = 1;
  738. phb->ioda.irq_chip = *ichip;
  739. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  740. }
  741. irq_set_chip(virq, &phb->ioda.irq_chip);
  742. }
  743. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  744. " address=%x_%08x data=%x PE# %d\n",
  745. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  746. msg->address_hi, msg->address_lo, data, pe->pe_number);
  747. return 0;
  748. }
  749. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  750. {
  751. unsigned int count;
  752. const __be32 *prop = of_get_property(phb->hose->dn,
  753. "ibm,opal-msi-ranges", NULL);
  754. if (!prop) {
  755. /* BML Fallback */
  756. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  757. }
  758. if (!prop)
  759. return;
  760. phb->msi_base = be32_to_cpup(prop);
  761. count = be32_to_cpup(prop + 1);
  762. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  763. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  764. phb->hose->global_number);
  765. return;
  766. }
  767. phb->msi_setup = pnv_pci_ioda_msi_setup;
  768. phb->msi32_support = 1;
  769. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  770. count, phb->msi_base);
  771. }
  772. #else
  773. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  774. #endif /* CONFIG_PCI_MSI */
  775. /*
  776. * This function is supposed to be called on basis of PE from top
  777. * to bottom style. So the the I/O or MMIO segment assigned to
  778. * parent PE could be overrided by its child PEs if necessary.
  779. */
  780. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  781. struct pnv_ioda_pe *pe)
  782. {
  783. struct pnv_phb *phb = hose->private_data;
  784. struct pci_bus_region region;
  785. struct resource *res;
  786. int i, index;
  787. int rc;
  788. /*
  789. * NOTE: We only care PCI bus based PE for now. For PCI
  790. * device based PE, for example SRIOV sensitive VF should
  791. * be figured out later.
  792. */
  793. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  794. pci_bus_for_each_resource(pe->pbus, res, i) {
  795. if (!res || !res->flags ||
  796. res->start > res->end)
  797. continue;
  798. if (res->flags & IORESOURCE_IO) {
  799. region.start = res->start - phb->ioda.io_pci_base;
  800. region.end = res->end - phb->ioda.io_pci_base;
  801. index = region.start / phb->ioda.io_segsize;
  802. while (index < phb->ioda.total_pe &&
  803. region.start <= region.end) {
  804. phb->ioda.io_segmap[index] = pe->pe_number;
  805. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  806. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  807. if (rc != OPAL_SUCCESS) {
  808. pr_err("%s: OPAL error %d when mapping IO "
  809. "segment #%d to PE#%d\n",
  810. __func__, rc, index, pe->pe_number);
  811. break;
  812. }
  813. region.start += phb->ioda.io_segsize;
  814. index++;
  815. }
  816. } else if (res->flags & IORESOURCE_MEM) {
  817. /* WARNING: Assumes M32 is mem region 0 in PHB. We need to
  818. * harden that algorithm when we start supporting M64
  819. */
  820. region.start = res->start -
  821. hose->mem_offset[0] -
  822. phb->ioda.m32_pci_base;
  823. region.end = res->end -
  824. hose->mem_offset[0] -
  825. phb->ioda.m32_pci_base;
  826. index = region.start / phb->ioda.m32_segsize;
  827. while (index < phb->ioda.total_pe &&
  828. region.start <= region.end) {
  829. phb->ioda.m32_segmap[index] = pe->pe_number;
  830. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  831. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  832. if (rc != OPAL_SUCCESS) {
  833. pr_err("%s: OPAL error %d when mapping M32 "
  834. "segment#%d to PE#%d",
  835. __func__, rc, index, pe->pe_number);
  836. break;
  837. }
  838. region.start += phb->ioda.m32_segsize;
  839. index++;
  840. }
  841. }
  842. }
  843. }
  844. static void pnv_pci_ioda_setup_seg(void)
  845. {
  846. struct pci_controller *tmp, *hose;
  847. struct pnv_phb *phb;
  848. struct pnv_ioda_pe *pe;
  849. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  850. phb = hose->private_data;
  851. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  852. pnv_ioda_setup_pe_seg(hose, pe);
  853. }
  854. }
  855. }
  856. static void pnv_pci_ioda_setup_DMA(void)
  857. {
  858. struct pci_controller *hose, *tmp;
  859. struct pnv_phb *phb;
  860. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  861. pnv_ioda_setup_dma(hose->private_data);
  862. /* Mark the PHB initialization done */
  863. phb = hose->private_data;
  864. phb->initialized = 1;
  865. }
  866. }
  867. static void pnv_pci_ioda_create_dbgfs(void)
  868. {
  869. #ifdef CONFIG_DEBUG_FS
  870. struct pci_controller *hose, *tmp;
  871. struct pnv_phb *phb;
  872. char name[16];
  873. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  874. phb = hose->private_data;
  875. sprintf(name, "PCI%04x", hose->global_number);
  876. phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
  877. if (!phb->dbgfs)
  878. pr_warning("%s: Error on creating debugfs on PHB#%x\n",
  879. __func__, hose->global_number);
  880. }
  881. #endif /* CONFIG_DEBUG_FS */
  882. }
  883. static void pnv_pci_ioda_fixup(void)
  884. {
  885. pnv_pci_ioda_setup_PEs();
  886. pnv_pci_ioda_setup_seg();
  887. pnv_pci_ioda_setup_DMA();
  888. pnv_pci_ioda_create_dbgfs();
  889. #ifdef CONFIG_EEH
  890. eeh_probe_mode_set(EEH_PROBE_MODE_DEV);
  891. eeh_addr_cache_build();
  892. eeh_init();
  893. #endif
  894. }
  895. /*
  896. * Returns the alignment for I/O or memory windows for P2P
  897. * bridges. That actually depends on how PEs are segmented.
  898. * For now, we return I/O or M32 segment size for PE sensitive
  899. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  900. * 1MiB for memory) will be returned.
  901. *
  902. * The current PCI bus might be put into one PE, which was
  903. * create against the parent PCI bridge. For that case, we
  904. * needn't enlarge the alignment so that we can save some
  905. * resources.
  906. */
  907. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  908. unsigned long type)
  909. {
  910. struct pci_dev *bridge;
  911. struct pci_controller *hose = pci_bus_to_host(bus);
  912. struct pnv_phb *phb = hose->private_data;
  913. int num_pci_bridges = 0;
  914. bridge = bus->self;
  915. while (bridge) {
  916. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  917. num_pci_bridges++;
  918. if (num_pci_bridges >= 2)
  919. return 1;
  920. }
  921. bridge = bridge->bus->self;
  922. }
  923. /* We need support prefetchable memory window later */
  924. if (type & IORESOURCE_MEM)
  925. return phb->ioda.m32_segsize;
  926. return phb->ioda.io_segsize;
  927. }
  928. /* Prevent enabling devices for which we couldn't properly
  929. * assign a PE
  930. */
  931. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  932. {
  933. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  934. struct pnv_phb *phb = hose->private_data;
  935. struct pci_dn *pdn;
  936. /* The function is probably called while the PEs have
  937. * not be created yet. For example, resource reassignment
  938. * during PCI probe period. We just skip the check if
  939. * PEs isn't ready.
  940. */
  941. if (!phb->initialized)
  942. return 0;
  943. pdn = pci_get_pdn(dev);
  944. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  945. return -EINVAL;
  946. return 0;
  947. }
  948. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  949. u32 devfn)
  950. {
  951. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  952. }
  953. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  954. {
  955. opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
  956. OPAL_ASSERT_RESET);
  957. }
  958. void __init pnv_pci_init_ioda_phb(struct device_node *np,
  959. u64 hub_id, int ioda_type)
  960. {
  961. struct pci_controller *hose;
  962. static int primary = 1;
  963. struct pnv_phb *phb;
  964. unsigned long size, m32map_off, iomap_off, pemap_off;
  965. const u64 *prop64;
  966. const u32 *prop32;
  967. u64 phb_id;
  968. void *aux;
  969. long rc;
  970. pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  971. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  972. if (!prop64) {
  973. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  974. return;
  975. }
  976. phb_id = be64_to_cpup(prop64);
  977. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  978. phb = alloc_bootmem(sizeof(struct pnv_phb));
  979. if (phb) {
  980. memset(phb, 0, sizeof(struct pnv_phb));
  981. phb->hose = hose = pcibios_alloc_controller(np);
  982. }
  983. if (!phb || !phb->hose) {
  984. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  985. np->full_name);
  986. return;
  987. }
  988. spin_lock_init(&phb->lock);
  989. /* XXX Use device-tree */
  990. hose->first_busno = 0;
  991. hose->last_busno = 0xff;
  992. hose->private_data = phb;
  993. phb->hub_id = hub_id;
  994. phb->opal_id = phb_id;
  995. phb->type = ioda_type;
  996. /* Detect specific models for error handling */
  997. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  998. phb->model = PNV_PHB_MODEL_P7IOC;
  999. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  1000. phb->model = PNV_PHB_MODEL_PHB3;
  1001. else
  1002. phb->model = PNV_PHB_MODEL_UNKNOWN;
  1003. /* Parse 32-bit and IO ranges (if any) */
  1004. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  1005. primary = 0;
  1006. /* Get registers */
  1007. phb->regs = of_iomap(np, 0);
  1008. if (phb->regs == NULL)
  1009. pr_err(" Failed to map registers !\n");
  1010. /* Initialize more IODA stuff */
  1011. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  1012. if (!prop32)
  1013. phb->ioda.total_pe = 1;
  1014. else
  1015. phb->ioda.total_pe = *prop32;
  1016. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  1017. /* FW Has already off top 64k of M32 space (MSI space) */
  1018. phb->ioda.m32_size += 0x10000;
  1019. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  1020. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  1021. phb->ioda.io_size = hose->pci_io_size;
  1022. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  1023. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  1024. /* Allocate aux data & arrays
  1025. *
  1026. * XXX TODO: Don't allocate io segmap on PHB3
  1027. */
  1028. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  1029. m32map_off = size;
  1030. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  1031. iomap_off = size;
  1032. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  1033. pemap_off = size;
  1034. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  1035. aux = alloc_bootmem(size);
  1036. memset(aux, 0, size);
  1037. phb->ioda.pe_alloc = aux;
  1038. phb->ioda.m32_segmap = aux + m32map_off;
  1039. phb->ioda.io_segmap = aux + iomap_off;
  1040. phb->ioda.pe_array = aux + pemap_off;
  1041. set_bit(0, phb->ioda.pe_alloc);
  1042. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1043. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1044. /* Calculate how many 32-bit TCE segments we have */
  1045. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1046. /* Clear unusable m64 */
  1047. hose->mem_resources[1].flags = 0;
  1048. hose->mem_resources[1].start = 0;
  1049. hose->mem_resources[1].end = 0;
  1050. hose->mem_resources[2].flags = 0;
  1051. hose->mem_resources[2].start = 0;
  1052. hose->mem_resources[2].end = 0;
  1053. #if 0 /* We should really do that ... */
  1054. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1055. window_type,
  1056. window_num,
  1057. starting_real_address,
  1058. starting_pci_address,
  1059. segment_size);
  1060. #endif
  1061. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  1062. phb->ioda.total_pe,
  1063. phb->ioda.m32_size, phb->ioda.m32_segsize,
  1064. phb->ioda.io_size, phb->ioda.io_segsize);
  1065. phb->hose->ops = &pnv_pci_ops;
  1066. #ifdef CONFIG_EEH
  1067. phb->eeh_ops = &ioda_eeh_ops;
  1068. #endif
  1069. /* Setup RID -> PE mapping function */
  1070. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1071. /* Setup TCEs */
  1072. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1073. /* Setup shutdown function for kexec */
  1074. phb->shutdown = pnv_pci_ioda_shutdown;
  1075. /* Setup MSI support */
  1076. pnv_pci_init_ioda_msis(phb);
  1077. /*
  1078. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1079. * to let the PCI core do resource assignment. It's supposed
  1080. * that the PCI core will do correct I/O and MMIO alignment
  1081. * for the P2P bridge bars so that each PCI bus (excluding
  1082. * the child P2P bridges) can form individual PE.
  1083. */
  1084. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1085. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1086. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1087. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1088. /* Reset IODA tables to a clean state */
  1089. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1090. if (rc)
  1091. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1092. /*
  1093. * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
  1094. * has cleared the RTT which has the same effect
  1095. */
  1096. if (ioda_type == PNV_PHB_IODA1)
  1097. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  1098. }
  1099. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  1100. {
  1101. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  1102. }
  1103. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1104. {
  1105. struct device_node *phbn;
  1106. const u64 *prop64;
  1107. u64 hub_id;
  1108. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1109. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1110. if (!prop64) {
  1111. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1112. return;
  1113. }
  1114. hub_id = be64_to_cpup(prop64);
  1115. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1116. /* Count child PHBs */
  1117. for_each_child_of_node(np, phbn) {
  1118. /* Look for IODA1 PHBs */
  1119. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1120. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  1121. }
  1122. }