pci-ioda.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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/delay.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/msi.h>
  21. #include <asm/sections.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/opal.h>
  28. #include <asm/iommu.h>
  29. #include <asm/tce.h>
  30. #include "powernv.h"
  31. #include "pci.h"
  32. static int __pe_printk(const char *level, const struct pnv_ioda_pe *pe,
  33. struct va_format *vaf)
  34. {
  35. char pfix[32];
  36. if (pe->pdev)
  37. strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  38. else
  39. sprintf(pfix, "%04x:%02x ",
  40. pci_domain_nr(pe->pbus), pe->pbus->number);
  41. return printk("pci %s%s: [PE# %.3d] %pV", level, pfix, pe->pe_number, vaf);
  42. }
  43. #define define_pe_printk_level(func, kern_level) \
  44. static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
  45. { \
  46. struct va_format vaf; \
  47. va_list args; \
  48. int r; \
  49. \
  50. va_start(args, fmt); \
  51. \
  52. vaf.fmt = fmt; \
  53. vaf.va = &args; \
  54. \
  55. r = __pe_printk(kern_level, pe, &vaf); \
  56. va_end(args); \
  57. \
  58. return r; \
  59. } \
  60. define_pe_printk_level(pe_err, KERN_ERR);
  61. define_pe_printk_level(pe_warn, KERN_WARNING);
  62. define_pe_printk_level(pe_info, KERN_INFO);
  63. static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
  64. {
  65. struct device_node *np;
  66. np = pci_device_to_OF_node(dev);
  67. if (!np)
  68. return NULL;
  69. return PCI_DN(np);
  70. }
  71. static int __devinit pnv_ioda_alloc_pe(struct pnv_phb *phb)
  72. {
  73. unsigned long pe;
  74. do {
  75. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  76. phb->ioda.total_pe, 0);
  77. if (pe >= phb->ioda.total_pe)
  78. return IODA_INVALID_PE;
  79. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  80. phb->ioda.pe_array[pe].pe_number = pe;
  81. return pe;
  82. }
  83. static void __devinit pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  84. {
  85. WARN_ON(phb->ioda.pe_array[pe].pdev);
  86. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  87. clear_bit(pe, phb->ioda.pe_alloc);
  88. }
  89. /* Currently those 2 are only used when MSIs are enabled, this will change
  90. * but in the meantime, we need to protect them to avoid warnings
  91. */
  92. #ifdef CONFIG_PCI_MSI
  93. static struct pnv_ioda_pe * __devinit pnv_ioda_get_pe(struct pci_dev *dev)
  94. {
  95. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  96. struct pnv_phb *phb = hose->private_data;
  97. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  98. if (!pdn)
  99. return NULL;
  100. if (pdn->pe_number == IODA_INVALID_PE)
  101. return NULL;
  102. return &phb->ioda.pe_array[pdn->pe_number];
  103. }
  104. #endif /* CONFIG_PCI_MSI */
  105. static int __devinit pnv_ioda_configure_pe(struct pnv_phb *phb,
  106. struct pnv_ioda_pe *pe)
  107. {
  108. struct pci_dev *parent;
  109. uint8_t bcomp, dcomp, fcomp;
  110. long rc, rid_end, rid;
  111. /* Bus validation ? */
  112. if (pe->pbus) {
  113. int count;
  114. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  115. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  116. parent = pe->pbus->self;
  117. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  118. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  119. else
  120. count = 1;
  121. switch(count) {
  122. case 1: bcomp = OpalPciBusAll; break;
  123. case 2: bcomp = OpalPciBus7Bits; break;
  124. case 4: bcomp = OpalPciBus6Bits; break;
  125. case 8: bcomp = OpalPciBus5Bits; break;
  126. case 16: bcomp = OpalPciBus4Bits; break;
  127. case 32: bcomp = OpalPciBus3Bits; break;
  128. default:
  129. pr_err("%s: Number of subordinate busses %d"
  130. " unsupported\n",
  131. pci_name(pe->pbus->self), count);
  132. /* Do an exact match only */
  133. bcomp = OpalPciBusAll;
  134. }
  135. rid_end = pe->rid + (count << 8);
  136. } else {
  137. parent = pe->pdev->bus->self;
  138. bcomp = OpalPciBusAll;
  139. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  140. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  141. rid_end = pe->rid + 1;
  142. }
  143. /* Associate PE in PELT */
  144. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  145. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  146. if (rc) {
  147. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  148. return -ENXIO;
  149. }
  150. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  151. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  152. /* Add to all parents PELT-V */
  153. while (parent) {
  154. struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
  155. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  156. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  157. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  158. /* XXX What to do in case of error ? */
  159. }
  160. parent = parent->bus->self;
  161. }
  162. /* Setup reverse map */
  163. for (rid = pe->rid; rid < rid_end; rid++)
  164. phb->ioda.pe_rmap[rid] = pe->pe_number;
  165. /* Setup one MVTs on IODA1 */
  166. if (phb->type == PNV_PHB_IODA1) {
  167. pe->mve_number = pe->pe_number;
  168. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
  169. pe->pe_number);
  170. if (rc) {
  171. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  172. rc, pe->mve_number);
  173. pe->mve_number = -1;
  174. } else {
  175. rc = opal_pci_set_mve_enable(phb->opal_id,
  176. pe->mve_number, OPAL_ENABLE_MVE);
  177. if (rc) {
  178. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  179. rc, pe->mve_number);
  180. pe->mve_number = -1;
  181. }
  182. }
  183. } else if (phb->type == PNV_PHB_IODA2)
  184. pe->mve_number = 0;
  185. return 0;
  186. }
  187. static void __devinit pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  188. struct pnv_ioda_pe *pe)
  189. {
  190. struct pnv_ioda_pe *lpe;
  191. list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
  192. if (lpe->dma_weight < pe->dma_weight) {
  193. list_add_tail(&pe->dma_link, &lpe->dma_link);
  194. return;
  195. }
  196. }
  197. list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
  198. }
  199. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  200. {
  201. /* This is quite simplistic. The "base" weight of a device
  202. * is 10. 0 means no DMA is to be accounted for it.
  203. */
  204. /* If it's a bridge, no DMA */
  205. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  206. return 0;
  207. /* Reduce the weight of slow USB controllers */
  208. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  209. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  210. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  211. return 3;
  212. /* Increase the weight of RAID (includes Obsidian) */
  213. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  214. return 15;
  215. /* Default */
  216. return 10;
  217. }
  218. #if 0
  219. static struct pnv_ioda_pe * __devinit pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  220. {
  221. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  222. struct pnv_phb *phb = hose->private_data;
  223. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  224. struct pnv_ioda_pe *pe;
  225. int pe_num;
  226. if (!pdn) {
  227. pr_err("%s: Device tree node not associated properly\n",
  228. pci_name(dev));
  229. return NULL;
  230. }
  231. if (pdn->pe_number != IODA_INVALID_PE)
  232. return NULL;
  233. /* PE#0 has been pre-set */
  234. if (dev->bus->number == 0)
  235. pe_num = 0;
  236. else
  237. pe_num = pnv_ioda_alloc_pe(phb);
  238. if (pe_num == IODA_INVALID_PE) {
  239. pr_warning("%s: Not enough PE# available, disabling device\n",
  240. pci_name(dev));
  241. return NULL;
  242. }
  243. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  244. * pointer in the PE data structure, both should be destroyed at the
  245. * same time. However, this needs to be looked at more closely again
  246. * once we actually start removing things (Hotplug, SR-IOV, ...)
  247. *
  248. * At some point we want to remove the PDN completely anyways
  249. */
  250. pe = &phb->ioda.pe_array[pe_num];
  251. pci_dev_get(dev);
  252. pdn->pcidev = dev;
  253. pdn->pe_number = pe_num;
  254. pe->pdev = dev;
  255. pe->pbus = NULL;
  256. pe->tce32_seg = -1;
  257. pe->mve_number = -1;
  258. pe->rid = dev->bus->number << 8 | pdn->devfn;
  259. pe_info(pe, "Associated device to PE\n");
  260. if (pnv_ioda_configure_pe(phb, pe)) {
  261. /* XXX What do we do here ? */
  262. if (pe_num)
  263. pnv_ioda_free_pe(phb, pe_num);
  264. pdn->pe_number = IODA_INVALID_PE;
  265. pe->pdev = NULL;
  266. pci_dev_put(dev);
  267. return NULL;
  268. }
  269. /* Assign a DMA weight to the device */
  270. pe->dma_weight = pnv_ioda_dma_weight(dev);
  271. if (pe->dma_weight != 0) {
  272. phb->ioda.dma_weight += pe->dma_weight;
  273. phb->ioda.dma_pe_count++;
  274. }
  275. /* Link the PE */
  276. pnv_ioda_link_pe_by_weight(phb, pe);
  277. return pe;
  278. }
  279. #endif /* Useful for SRIOV case */
  280. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  281. {
  282. struct pci_dev *dev;
  283. list_for_each_entry(dev, &bus->devices, bus_list) {
  284. struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
  285. if (pdn == NULL) {
  286. pr_warn("%s: No device node associated with device !\n",
  287. pci_name(dev));
  288. continue;
  289. }
  290. pci_dev_get(dev);
  291. pdn->pcidev = dev;
  292. pdn->pe_number = pe->pe_number;
  293. pe->dma_weight += pnv_ioda_dma_weight(dev);
  294. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  295. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  296. }
  297. }
  298. /*
  299. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  300. * single PCI bus. Another one that contains the primary PCI bus and its
  301. * subordinate PCI devices and buses. The second type of PE is normally
  302. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  303. */
  304. static void __devinit pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
  305. {
  306. struct pci_controller *hose = pci_bus_to_host(bus);
  307. struct pnv_phb *phb = hose->private_data;
  308. struct pnv_ioda_pe *pe;
  309. int pe_num;
  310. pe_num = pnv_ioda_alloc_pe(phb);
  311. if (pe_num == IODA_INVALID_PE) {
  312. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  313. __func__, pci_domain_nr(bus), bus->number);
  314. return;
  315. }
  316. pe = &phb->ioda.pe_array[pe_num];
  317. pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  318. pe->pbus = bus;
  319. pe->pdev = NULL;
  320. pe->tce32_seg = -1;
  321. pe->mve_number = -1;
  322. pe->rid = bus->busn_res.start << 8;
  323. pe->dma_weight = 0;
  324. if (all)
  325. pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
  326. bus->busn_res.start, bus->busn_res.end, pe_num);
  327. else
  328. pe_info(pe, "Secondary bus %d associated with PE#%d\n",
  329. bus->busn_res.start, pe_num);
  330. if (pnv_ioda_configure_pe(phb, pe)) {
  331. /* XXX What do we do here ? */
  332. if (pe_num)
  333. pnv_ioda_free_pe(phb, pe_num);
  334. pe->pbus = NULL;
  335. return;
  336. }
  337. /* Associate it with all child devices */
  338. pnv_ioda_setup_same_PE(bus, pe);
  339. /* Put PE to the list */
  340. list_add_tail(&pe->list, &phb->ioda.pe_list);
  341. /* Account for one DMA PE if at least one DMA capable device exist
  342. * below the bridge
  343. */
  344. if (pe->dma_weight != 0) {
  345. phb->ioda.dma_weight += pe->dma_weight;
  346. phb->ioda.dma_pe_count++;
  347. }
  348. /* Link the PE */
  349. pnv_ioda_link_pe_by_weight(phb, pe);
  350. }
  351. static void __devinit pnv_ioda_setup_PEs(struct pci_bus *bus)
  352. {
  353. struct pci_dev *dev;
  354. pnv_ioda_setup_bus_PE(bus, 0);
  355. list_for_each_entry(dev, &bus->devices, bus_list) {
  356. if (dev->subordinate) {
  357. if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
  358. pnv_ioda_setup_bus_PE(dev->subordinate, 1);
  359. else
  360. pnv_ioda_setup_PEs(dev->subordinate);
  361. }
  362. }
  363. }
  364. /*
  365. * Configure PEs so that the downstream PCI buses and devices
  366. * could have their associated PE#. Unfortunately, we didn't
  367. * figure out the way to identify the PLX bridge yet. So we
  368. * simply put the PCI bus and the subordinate behind the root
  369. * port to PE# here. The game rule here is expected to be changed
  370. * as soon as we can detected PLX bridge correctly.
  371. */
  372. static void __devinit pnv_pci_ioda_setup_PEs(void)
  373. {
  374. struct pci_controller *hose, *tmp;
  375. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  376. pnv_ioda_setup_PEs(hose->bus);
  377. }
  378. }
  379. static void __devinit pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb,
  380. struct pci_dev *dev)
  381. {
  382. /* We delay DMA setup after we have assigned all PE# */
  383. }
  384. static void __devinit pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  385. struct pci_bus *bus)
  386. {
  387. struct pci_dev *dev;
  388. list_for_each_entry(dev, &bus->devices, bus_list) {
  389. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  390. if (dev->subordinate)
  391. pnv_ioda_setup_bus_dma(pe, dev->subordinate);
  392. }
  393. }
  394. static void __devinit pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  395. struct pnv_ioda_pe *pe,
  396. unsigned int base,
  397. unsigned int segs)
  398. {
  399. struct page *tce_mem = NULL;
  400. const __be64 *swinvp;
  401. struct iommu_table *tbl;
  402. unsigned int i;
  403. int64_t rc;
  404. void *addr;
  405. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  406. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  407. /* XXX FIXME: Handle 64-bit only DMA devices */
  408. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  409. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  410. /* We shouldn't already have a 32-bit DMA associated */
  411. if (WARN_ON(pe->tce32_seg >= 0))
  412. return;
  413. /* Grab a 32-bit TCE table */
  414. pe->tce32_seg = base;
  415. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  416. (base << 28), ((base + segs) << 28) - 1);
  417. /* XXX Currently, we allocate one big contiguous table for the
  418. * TCEs. We only really need one chunk per 256M of TCE space
  419. * (ie per segment) but that's an optimization for later, it
  420. * requires some added smarts with our get/put_tce implementation
  421. */
  422. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  423. get_order(TCE32_TABLE_SIZE * segs));
  424. if (!tce_mem) {
  425. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  426. goto fail;
  427. }
  428. addr = page_address(tce_mem);
  429. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  430. /* Configure HW */
  431. for (i = 0; i < segs; i++) {
  432. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  433. pe->pe_number,
  434. base + i, 1,
  435. __pa(addr) + TCE32_TABLE_SIZE * i,
  436. TCE32_TABLE_SIZE, 0x1000);
  437. if (rc) {
  438. pe_err(pe, " Failed to configure 32-bit TCE table,"
  439. " err %ld\n", rc);
  440. goto fail;
  441. }
  442. }
  443. /* Setup linux iommu table */
  444. tbl = &pe->tce32_table;
  445. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  446. base << 28);
  447. /* OPAL variant of P7IOC SW invalidated TCEs */
  448. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  449. if (swinvp) {
  450. /* We need a couple more fields -- an address and a data
  451. * to or. Since the bus is only printed out on table free
  452. * errors, and on the first pass the data will be a relative
  453. * bus number, print that out instead.
  454. */
  455. tbl->it_busno = 0;
  456. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  457. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
  458. | TCE_PCI_SWINV_PAIR;
  459. }
  460. iommu_init_table(tbl, phb->hose->node);
  461. if (pe->pdev)
  462. set_iommu_table_base(&pe->pdev->dev, tbl);
  463. else
  464. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  465. return;
  466. fail:
  467. /* XXX Failure: Try to fallback to 64-bit only ? */
  468. if (pe->tce32_seg >= 0)
  469. pe->tce32_seg = -1;
  470. if (tce_mem)
  471. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  472. }
  473. static void __devinit pnv_ioda_setup_dma(struct pnv_phb *phb)
  474. {
  475. struct pci_controller *hose = phb->hose;
  476. unsigned int residual, remaining, segs, tw, base;
  477. struct pnv_ioda_pe *pe;
  478. /* If we have more PE# than segments available, hand out one
  479. * per PE until we run out and let the rest fail. If not,
  480. * then we assign at least one segment per PE, plus more based
  481. * on the amount of devices under that PE
  482. */
  483. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  484. residual = 0;
  485. else
  486. residual = phb->ioda.tce32_count -
  487. phb->ioda.dma_pe_count;
  488. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  489. hose->global_number, phb->ioda.tce32_count);
  490. pr_info("PCI: %d PE# for a total weight of %d\n",
  491. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  492. /* Walk our PE list and configure their DMA segments, hand them
  493. * out one base segment plus any residual segments based on
  494. * weight
  495. */
  496. remaining = phb->ioda.tce32_count;
  497. tw = phb->ioda.dma_weight;
  498. base = 0;
  499. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  500. if (!pe->dma_weight)
  501. continue;
  502. if (!remaining) {
  503. pe_warn(pe, "No DMA32 resources available\n");
  504. continue;
  505. }
  506. segs = 1;
  507. if (residual) {
  508. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  509. if (segs > remaining)
  510. segs = remaining;
  511. }
  512. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  513. pe->dma_weight, segs);
  514. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  515. remaining -= segs;
  516. base += segs;
  517. }
  518. }
  519. #ifdef CONFIG_PCI_MSI
  520. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  521. unsigned int hwirq, unsigned int is_64,
  522. struct msi_msg *msg)
  523. {
  524. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  525. unsigned int xive_num = hwirq - phb->msi_base;
  526. uint64_t addr64;
  527. uint32_t addr32, data;
  528. int rc;
  529. /* No PE assigned ? bail out ... no MSI for you ! */
  530. if (pe == NULL)
  531. return -ENXIO;
  532. /* Check if we have an MVE */
  533. if (pe->mve_number < 0)
  534. return -ENXIO;
  535. /* Assign XIVE to PE */
  536. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  537. if (rc) {
  538. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  539. pci_name(dev), rc, xive_num);
  540. return -EIO;
  541. }
  542. if (is_64) {
  543. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  544. &addr64, &data);
  545. if (rc) {
  546. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  547. pci_name(dev), rc);
  548. return -EIO;
  549. }
  550. msg->address_hi = addr64 >> 32;
  551. msg->address_lo = addr64 & 0xfffffffful;
  552. } else {
  553. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  554. &addr32, &data);
  555. if (rc) {
  556. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  557. pci_name(dev), rc);
  558. return -EIO;
  559. }
  560. msg->address_hi = 0;
  561. msg->address_lo = addr32;
  562. }
  563. msg->data = data;
  564. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  565. " address=%x_%08x data=%x PE# %d\n",
  566. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  567. msg->address_hi, msg->address_lo, data, pe->pe_number);
  568. return 0;
  569. }
  570. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  571. {
  572. unsigned int bmap_size;
  573. const __be32 *prop = of_get_property(phb->hose->dn,
  574. "ibm,opal-msi-ranges", NULL);
  575. if (!prop) {
  576. /* BML Fallback */
  577. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  578. }
  579. if (!prop)
  580. return;
  581. phb->msi_base = be32_to_cpup(prop);
  582. phb->msi_count = be32_to_cpup(prop + 1);
  583. bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
  584. phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
  585. if (!phb->msi_map) {
  586. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  587. phb->hose->global_number);
  588. return;
  589. }
  590. phb->msi_setup = pnv_pci_ioda_msi_setup;
  591. phb->msi32_support = 1;
  592. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  593. phb->msi_count, phb->msi_base);
  594. }
  595. #else
  596. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  597. #endif /* CONFIG_PCI_MSI */
  598. /*
  599. * This function is supposed to be called on basis of PE from top
  600. * to bottom style. So the the I/O or MMIO segment assigned to
  601. * parent PE could be overrided by its child PEs if necessary.
  602. */
  603. static void __devinit pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  604. struct pnv_ioda_pe *pe)
  605. {
  606. struct pnv_phb *phb = hose->private_data;
  607. struct pci_bus_region region;
  608. struct resource *res;
  609. int i, index;
  610. int rc;
  611. /*
  612. * NOTE: We only care PCI bus based PE for now. For PCI
  613. * device based PE, for example SRIOV sensitive VF should
  614. * be figured out later.
  615. */
  616. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  617. pci_bus_for_each_resource(pe->pbus, res, i) {
  618. if (!res || !res->flags ||
  619. res->start > res->end)
  620. continue;
  621. if (res->flags & IORESOURCE_IO) {
  622. region.start = res->start - phb->ioda.io_pci_base;
  623. region.end = res->end - phb->ioda.io_pci_base;
  624. index = region.start / phb->ioda.io_segsize;
  625. while (index < phb->ioda.total_pe &&
  626. region.start <= region.end) {
  627. phb->ioda.io_segmap[index] = pe->pe_number;
  628. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  629. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  630. if (rc != OPAL_SUCCESS) {
  631. pr_err("%s: OPAL error %d when mapping IO "
  632. "segment #%d to PE#%d\n",
  633. __func__, rc, index, pe->pe_number);
  634. break;
  635. }
  636. region.start += phb->ioda.io_segsize;
  637. index++;
  638. }
  639. } else if (res->flags & IORESOURCE_MEM) {
  640. region.start = res->start -
  641. hose->pci_mem_offset -
  642. phb->ioda.m32_pci_base;
  643. region.end = res->end -
  644. hose->pci_mem_offset -
  645. phb->ioda.m32_pci_base;
  646. index = region.start / phb->ioda.m32_segsize;
  647. while (index < phb->ioda.total_pe &&
  648. region.start <= region.end) {
  649. phb->ioda.m32_segmap[index] = pe->pe_number;
  650. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  651. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  652. if (rc != OPAL_SUCCESS) {
  653. pr_err("%s: OPAL error %d when mapping M32 "
  654. "segment#%d to PE#%d",
  655. __func__, rc, index, pe->pe_number);
  656. break;
  657. }
  658. region.start += phb->ioda.m32_segsize;
  659. index++;
  660. }
  661. }
  662. }
  663. }
  664. static void __devinit pnv_pci_ioda_setup_seg(void)
  665. {
  666. struct pci_controller *tmp, *hose;
  667. struct pnv_phb *phb;
  668. struct pnv_ioda_pe *pe;
  669. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  670. phb = hose->private_data;
  671. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  672. pnv_ioda_setup_pe_seg(hose, pe);
  673. }
  674. }
  675. }
  676. static void __devinit pnv_pci_ioda_setup_DMA(void)
  677. {
  678. struct pci_controller *hose, *tmp;
  679. struct pnv_phb *phb;
  680. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  681. pnv_ioda_setup_dma(hose->private_data);
  682. /* Mark the PHB initialization done */
  683. phb = hose->private_data;
  684. phb->initialized = 1;
  685. }
  686. }
  687. static void __devinit pnv_pci_ioda_fixup(void)
  688. {
  689. pnv_pci_ioda_setup_PEs();
  690. pnv_pci_ioda_setup_seg();
  691. pnv_pci_ioda_setup_DMA();
  692. }
  693. /*
  694. * Returns the alignment for I/O or memory windows for P2P
  695. * bridges. That actually depends on how PEs are segmented.
  696. * For now, we return I/O or M32 segment size for PE sensitive
  697. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  698. * 1MiB for memory) will be returned.
  699. *
  700. * The current PCI bus might be put into one PE, which was
  701. * create against the parent PCI bridge. For that case, we
  702. * needn't enlarge the alignment so that we can save some
  703. * resources.
  704. */
  705. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  706. unsigned long type)
  707. {
  708. struct pci_dev *bridge;
  709. struct pci_controller *hose = pci_bus_to_host(bus);
  710. struct pnv_phb *phb = hose->private_data;
  711. int num_pci_bridges = 0;
  712. bridge = bus->self;
  713. while (bridge) {
  714. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  715. num_pci_bridges++;
  716. if (num_pci_bridges >= 2)
  717. return 1;
  718. }
  719. bridge = bridge->bus->self;
  720. }
  721. /* We need support prefetchable memory window later */
  722. if (type & IORESOURCE_MEM)
  723. return phb->ioda.m32_segsize;
  724. return phb->ioda.io_segsize;
  725. }
  726. /* Prevent enabling devices for which we couldn't properly
  727. * assign a PE
  728. */
  729. static int __devinit pnv_pci_enable_device_hook(struct pci_dev *dev)
  730. {
  731. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  732. struct pnv_phb *phb = hose->private_data;
  733. struct pci_dn *pdn;
  734. /* The function is probably called while the PEs have
  735. * not be created yet. For example, resource reassignment
  736. * during PCI probe period. We just skip the check if
  737. * PEs isn't ready.
  738. */
  739. if (!phb->initialized)
  740. return 0;
  741. pdn = pnv_ioda_get_pdn(dev);
  742. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  743. return -EINVAL;
  744. return 0;
  745. }
  746. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  747. u32 devfn)
  748. {
  749. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  750. }
  751. void __init pnv_pci_init_ioda1_phb(struct device_node *np)
  752. {
  753. struct pci_controller *hose;
  754. static int primary = 1;
  755. struct pnv_phb *phb;
  756. unsigned long size, m32map_off, iomap_off, pemap_off;
  757. const u64 *prop64;
  758. u64 phb_id;
  759. void *aux;
  760. long rc;
  761. pr_info(" Initializing IODA OPAL PHB %s\n", np->full_name);
  762. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  763. if (!prop64) {
  764. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  765. return;
  766. }
  767. phb_id = be64_to_cpup(prop64);
  768. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  769. phb = alloc_bootmem(sizeof(struct pnv_phb));
  770. if (phb) {
  771. memset(phb, 0, sizeof(struct pnv_phb));
  772. phb->hose = hose = pcibios_alloc_controller(np);
  773. }
  774. if (!phb || !phb->hose) {
  775. pr_err("PCI: Failed to allocate PCI controller for %s\n",
  776. np->full_name);
  777. return;
  778. }
  779. spin_lock_init(&phb->lock);
  780. /* XXX Use device-tree */
  781. hose->first_busno = 0;
  782. hose->last_busno = 0xff;
  783. hose->private_data = phb;
  784. phb->opal_id = phb_id;
  785. phb->type = PNV_PHB_IODA1;
  786. /* Detect specific models for error handling */
  787. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  788. phb->model = PNV_PHB_MODEL_P7IOC;
  789. else
  790. phb->model = PNV_PHB_MODEL_UNKNOWN;
  791. /* We parse "ranges" now since we need to deduce the register base
  792. * from the IO base
  793. */
  794. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  795. primary = 0;
  796. /* Magic formula from Milton */
  797. phb->regs = of_iomap(np, 0);
  798. if (phb->regs == NULL)
  799. pr_err(" Failed to map registers !\n");
  800. /* XXX This is hack-a-thon. This needs to be changed so that:
  801. * - we obtain stuff like PE# etc... from device-tree
  802. * - we properly re-allocate M32 ourselves
  803. * (the OFW one isn't very good)
  804. */
  805. /* Initialize more IODA stuff */
  806. phb->ioda.total_pe = 128;
  807. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  808. /* OFW Has already off top 64k of M32 space (MSI space) */
  809. phb->ioda.m32_size += 0x10000;
  810. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  811. phb->ioda.m32_pci_base = hose->mem_resources[0].start -
  812. hose->pci_mem_offset;
  813. phb->ioda.io_size = hose->pci_io_size;
  814. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  815. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  816. /* Allocate aux data & arrays */
  817. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  818. m32map_off = size;
  819. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  820. iomap_off = size;
  821. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  822. pemap_off = size;
  823. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  824. aux = alloc_bootmem(size);
  825. memset(aux, 0, size);
  826. phb->ioda.pe_alloc = aux;
  827. phb->ioda.m32_segmap = aux + m32map_off;
  828. phb->ioda.io_segmap = aux + iomap_off;
  829. phb->ioda.pe_array = aux + pemap_off;
  830. set_bit(0, phb->ioda.pe_alloc);
  831. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  832. INIT_LIST_HEAD(&phb->ioda.pe_list);
  833. /* Calculate how many 32-bit TCE segments we have */
  834. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  835. /* Clear unusable m64 */
  836. hose->mem_resources[1].flags = 0;
  837. hose->mem_resources[1].start = 0;
  838. hose->mem_resources[1].end = 0;
  839. hose->mem_resources[2].flags = 0;
  840. hose->mem_resources[2].start = 0;
  841. hose->mem_resources[2].end = 0;
  842. #if 0
  843. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  844. window_type,
  845. window_num,
  846. starting_real_address,
  847. starting_pci_address,
  848. segment_size);
  849. #endif
  850. pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
  851. phb->ioda.total_pe,
  852. phb->ioda.m32_size, phb->ioda.m32_segsize,
  853. phb->ioda.io_size, phb->ioda.io_segsize);
  854. if (phb->regs) {
  855. pr_devel(" BUID = 0x%016llx\n", in_be64(phb->regs + 0x100));
  856. pr_devel(" PHB2_CR = 0x%016llx\n", in_be64(phb->regs + 0x160));
  857. pr_devel(" IO_BAR = 0x%016llx\n", in_be64(phb->regs + 0x170));
  858. pr_devel(" IO_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x178));
  859. pr_devel(" IO_SAR = 0x%016llx\n", in_be64(phb->regs + 0x180));
  860. pr_devel(" M32_BAR = 0x%016llx\n", in_be64(phb->regs + 0x190));
  861. pr_devel(" M32_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x198));
  862. pr_devel(" M32_SAR = 0x%016llx\n", in_be64(phb->regs + 0x1a0));
  863. }
  864. phb->hose->ops = &pnv_pci_ops;
  865. /* Setup RID -> PE mapping function */
  866. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  867. /* Setup TCEs */
  868. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  869. /* Setup MSI support */
  870. pnv_pci_init_ioda_msis(phb);
  871. /*
  872. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  873. * to let the PCI core do resource assignment. It's supposed
  874. * that the PCI core will do correct I/O and MMIO alignment
  875. * for the P2P bridge bars so that each PCI bus (excluding
  876. * the child P2P bridges) can form individual PE.
  877. */
  878. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  879. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  880. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  881. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  882. /* Reset IODA tables to a clean state */
  883. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  884. if (rc)
  885. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  886. opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
  887. }
  888. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  889. {
  890. struct device_node *phbn;
  891. const u64 *prop64;
  892. u64 hub_id;
  893. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  894. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  895. if (!prop64) {
  896. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  897. return;
  898. }
  899. hub_id = be64_to_cpup(prop64);
  900. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  901. /* Count child PHBs */
  902. for_each_child_of_node(np, phbn) {
  903. /* Look for IODA1 PHBs */
  904. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  905. pnv_pci_init_ioda1_phb(phbn);
  906. }
  907. }