pci-ioda.c 39 KB

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