pci-ioda.c 28 KB

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