pci-ioda.c 39 KB

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