pci_gx.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mmzone.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/capability.h>
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/irq.h>
  24. #include <linux/msi.h>
  25. #include <linux/io.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/ctype.h>
  28. #include <asm/processor.h>
  29. #include <asm/sections.h>
  30. #include <asm/byteorder.h>
  31. #include <gxio/iorpc_globals.h>
  32. #include <gxio/kiorpc.h>
  33. #include <gxio/trio.h>
  34. #include <gxio/iorpc_trio.h>
  35. #include <hv/drv_trio_intf.h>
  36. #include <arch/sim.h>
  37. /*
  38. * This file containes the routines to search for PCI buses,
  39. * enumerate the buses, and configure any attached devices.
  40. */
  41. #define DEBUG_PCI_CFG 0
  42. #if DEBUG_PCI_CFG
  43. #define TRACE_CFG_WR(size, val, bus, dev, func, offset) \
  44. pr_info("CFG WR %d-byte VAL %#x to bus %d dev %d func %d addr %u\n", \
  45. size, val, bus, dev, func, offset & 0xFFF);
  46. #define TRACE_CFG_RD(size, val, bus, dev, func, offset) \
  47. pr_info("CFG RD %d-byte VAL %#x from bus %d dev %d func %d addr %u\n", \
  48. size, val, bus, dev, func, offset & 0xFFF);
  49. #else
  50. #define TRACE_CFG_WR(...)
  51. #define TRACE_CFG_RD(...)
  52. #endif
  53. static int __devinitdata pci_probe = 1;
  54. /* Information on the PCIe RC ports configuration. */
  55. static int __devinitdata pcie_rc[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
  56. /*
  57. * On some platforms with one or more Gx endpoint ports, we need to
  58. * delay the PCIe RC port probe for a few seconds to work around
  59. * a HW PCIe link-training bug. The exact delay is specified with
  60. * a kernel boot argument in the form of "pcie_rc_delay=T,P,S",
  61. * where T is the TRIO instance number, P is the port number and S is
  62. * the delay in seconds. If the delay is not provided, the value
  63. * will be DEFAULT_RC_DELAY.
  64. */
  65. static int __devinitdata rc_delay[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
  66. /* Default number of seconds that the PCIe RC port probe can be delayed. */
  67. #define DEFAULT_RC_DELAY 10
  68. /* Max number of seconds that the PCIe RC port probe can be delayed. */
  69. #define MAX_RC_DELAY 20
  70. /* Array of the PCIe ports configuration info obtained from the BIB. */
  71. struct pcie_port_property pcie_ports[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
  72. /* All drivers share the TRIO contexts defined here. */
  73. gxio_trio_context_t trio_contexts[TILEGX_NUM_TRIO];
  74. /* Pointer to an array of PCIe RC controllers. */
  75. struct pci_controller pci_controllers[TILEGX_NUM_TRIO * TILEGX_TRIO_PCIES];
  76. int num_rc_controllers;
  77. static int num_ep_controllers;
  78. static struct pci_ops tile_cfg_ops;
  79. /* Mask of CPUs that should receive PCIe interrupts. */
  80. static struct cpumask intr_cpus_map;
  81. /* PCI I/O space support is not implemented. */
  82. static struct resource pci_ioport_resource = {
  83. .name = "PCI IO",
  84. .start = 0,
  85. .end = 0,
  86. .flags = IORESOURCE_IO,
  87. };
  88. static struct resource pci_iomem_resource = {
  89. .name = "PCI mem",
  90. .start = TILE_PCI_MEM_START,
  91. .end = TILE_PCI_MEM_END,
  92. .flags = IORESOURCE_MEM,
  93. };
  94. /*
  95. * We don't need to worry about the alignment of resources.
  96. */
  97. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  98. resource_size_t size, resource_size_t align)
  99. {
  100. return res->start;
  101. }
  102. EXPORT_SYMBOL(pcibios_align_resource);
  103. /*
  104. * Pick a CPU to receive and handle the PCIe interrupts, based on the IRQ #.
  105. * For now, we simply send interrupts to non-dataplane CPUs.
  106. * We may implement methods to allow user to specify the target CPUs,
  107. * e.g. via boot arguments.
  108. */
  109. static int tile_irq_cpu(int irq)
  110. {
  111. unsigned int count;
  112. int i = 0;
  113. int cpu;
  114. count = cpumask_weight(&intr_cpus_map);
  115. if (unlikely(count == 0)) {
  116. pr_warning("intr_cpus_map empty, interrupts will be"
  117. " delievered to dataplane tiles\n");
  118. return irq % (smp_height * smp_width);
  119. }
  120. count = irq % count;
  121. for_each_cpu(cpu, &intr_cpus_map) {
  122. if (i++ == count)
  123. break;
  124. }
  125. return cpu;
  126. }
  127. /*
  128. * Open a file descriptor to the TRIO shim.
  129. */
  130. static int __devinit tile_pcie_open(int trio_index)
  131. {
  132. gxio_trio_context_t *context = &trio_contexts[trio_index];
  133. int ret;
  134. /*
  135. * This opens a file descriptor to the TRIO shim.
  136. */
  137. ret = gxio_trio_init(context, trio_index);
  138. if (ret < 0)
  139. return ret;
  140. /*
  141. * Allocate an ASID for the kernel.
  142. */
  143. ret = gxio_trio_alloc_asids(context, 1, 0, 0);
  144. if (ret < 0) {
  145. pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
  146. trio_index);
  147. goto asid_alloc_failure;
  148. }
  149. context->asid = ret;
  150. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  151. /*
  152. * Alloc a PIO region for config access, shared by all MACs per TRIO.
  153. * This shouldn't fail since the kernel is supposed to the first
  154. * client of the TRIO's PIO regions.
  155. */
  156. ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
  157. if (ret < 0) {
  158. pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
  159. trio_index);
  160. goto pio_alloc_failure;
  161. }
  162. context->pio_cfg_index = ret;
  163. /*
  164. * For PIO CFG, the bus_address_hi parameter is 0. The mac parameter
  165. * is also 0 because it is specified in PIO_REGION_SETUP_CFG_ADDR.
  166. */
  167. ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
  168. 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  169. if (ret < 0) {
  170. pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
  171. trio_index);
  172. goto pio_alloc_failure;
  173. }
  174. #endif
  175. return ret;
  176. asid_alloc_failure:
  177. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  178. pio_alloc_failure:
  179. #endif
  180. hv_dev_close(context->fd);
  181. return ret;
  182. }
  183. static void
  184. tilegx_legacy_irq_ack(struct irq_data *d)
  185. {
  186. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  187. }
  188. static void
  189. tilegx_legacy_irq_mask(struct irq_data *d)
  190. {
  191. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  192. }
  193. static void
  194. tilegx_legacy_irq_unmask(struct irq_data *d)
  195. {
  196. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  197. }
  198. static struct irq_chip tilegx_legacy_irq_chip = {
  199. .name = "tilegx_legacy_irq",
  200. .irq_ack = tilegx_legacy_irq_ack,
  201. .irq_mask = tilegx_legacy_irq_mask,
  202. .irq_unmask = tilegx_legacy_irq_unmask,
  203. /* TBD: support set_affinity. */
  204. };
  205. /*
  206. * This is a wrapper function of the kernel level-trigger interrupt
  207. * handler handle_level_irq() for PCI legacy interrupts. The TRIO
  208. * is configured such that only INTx Assert interrupts are proxied
  209. * to Linux which just calls handle_level_irq() after clearing the
  210. * MAC INTx Assert status bit associated with this interrupt.
  211. */
  212. static void
  213. trio_handle_level_irq(unsigned int irq, struct irq_desc *desc)
  214. {
  215. struct pci_controller *controller = irq_desc_get_handler_data(desc);
  216. gxio_trio_context_t *trio_context = controller->trio;
  217. uint64_t intx = (uint64_t)irq_desc_get_chip_data(desc);
  218. int mac = controller->mac;
  219. unsigned int reg_offset;
  220. uint64_t level_mask;
  221. handle_level_irq(irq, desc);
  222. /*
  223. * Clear the INTx Level status, otherwise future interrupts are
  224. * not sent.
  225. */
  226. reg_offset = (TRIO_PCIE_INTFC_MAC_INT_STS <<
  227. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  228. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  229. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  230. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  231. level_mask = TRIO_PCIE_INTFC_MAC_INT_STS__INT_LEVEL_MASK << intx;
  232. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset, level_mask);
  233. }
  234. /*
  235. * Create kernel irqs and set up the handlers for the legacy interrupts.
  236. * Also some minimum initialization for the MSI support.
  237. */
  238. static int __devinit tile_init_irqs(struct pci_controller *controller)
  239. {
  240. int i;
  241. int j;
  242. int irq;
  243. int result;
  244. cpumask_copy(&intr_cpus_map, cpu_online_mask);
  245. for (i = 0; i < 4; i++) {
  246. gxio_trio_context_t *context = controller->trio;
  247. int cpu;
  248. /* Ask the kernel to allocate an IRQ. */
  249. irq = create_irq();
  250. if (irq < 0) {
  251. pr_err("PCI: no free irq vectors, failed for %d\n", i);
  252. goto free_irqs;
  253. }
  254. controller->irq_intx_table[i] = irq;
  255. /* Distribute the 4 IRQs to different tiles. */
  256. cpu = tile_irq_cpu(irq);
  257. /* Configure the TRIO intr binding for this IRQ. */
  258. result = gxio_trio_config_legacy_intr(context, cpu_x(cpu),
  259. cpu_y(cpu), KERNEL_PL,
  260. irq, controller->mac, i);
  261. if (result < 0) {
  262. pr_err("PCI: MAC intx config failed for %d\n", i);
  263. goto free_irqs;
  264. }
  265. /*
  266. * Register the IRQ handler with the kernel.
  267. */
  268. irq_set_chip_and_handler(irq, &tilegx_legacy_irq_chip,
  269. trio_handle_level_irq);
  270. irq_set_chip_data(irq, (void *)(uint64_t)i);
  271. irq_set_handler_data(irq, controller);
  272. }
  273. return 0;
  274. free_irqs:
  275. for (j = 0; j < i; j++)
  276. destroy_irq(controller->irq_intx_table[j]);
  277. return -1;
  278. }
  279. /*
  280. * Find valid controllers and fill in pci_controller structs for each
  281. * of them.
  282. *
  283. * Returns the number of controllers discovered.
  284. */
  285. int __init tile_pci_init(void)
  286. {
  287. int num_trio_shims = 0;
  288. int ctl_index = 0;
  289. int i, j;
  290. if (!pci_probe) {
  291. pr_info("PCI: disabled by boot argument\n");
  292. return 0;
  293. }
  294. pr_info("PCI: Searching for controllers...\n");
  295. /*
  296. * We loop over all the TRIO shims.
  297. */
  298. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  299. int ret;
  300. ret = tile_pcie_open(i);
  301. if (ret < 0)
  302. continue;
  303. num_trio_shims++;
  304. }
  305. if (num_trio_shims == 0 || sim_is_simulator())
  306. return 0;
  307. /*
  308. * Now determine which PCIe ports are configured to operate in RC mode.
  309. * We look at the Board Information Block first and then see if there
  310. * are any overriding configuration by the HW strapping pin.
  311. */
  312. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  313. gxio_trio_context_t *context = &trio_contexts[i];
  314. int ret;
  315. if (context->fd < 0)
  316. continue;
  317. ret = hv_dev_pread(context->fd, 0,
  318. (HV_VirtAddr)&pcie_ports[i][0],
  319. sizeof(struct pcie_port_property) * TILEGX_TRIO_PCIES,
  320. GXIO_TRIO_OP_GET_PORT_PROPERTY);
  321. if (ret < 0) {
  322. pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d,"
  323. " on TRIO %d\n", ret, i);
  324. continue;
  325. }
  326. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  327. if (pcie_ports[i][j].allow_rc) {
  328. pcie_rc[i][j] = 1;
  329. num_rc_controllers++;
  330. }
  331. else if (pcie_ports[i][j].allow_ep) {
  332. num_ep_controllers++;
  333. }
  334. }
  335. }
  336. /*
  337. * Return if no PCIe ports are configured to operate in RC mode.
  338. */
  339. if (num_rc_controllers == 0)
  340. return 0;
  341. /*
  342. * Set the TRIO pointer and MAC index for each PCIe RC port.
  343. */
  344. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  345. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  346. if (pcie_rc[i][j]) {
  347. pci_controllers[ctl_index].trio =
  348. &trio_contexts[i];
  349. pci_controllers[ctl_index].mac = j;
  350. pci_controllers[ctl_index].trio_index = i;
  351. ctl_index++;
  352. if (ctl_index == num_rc_controllers)
  353. goto out;
  354. }
  355. }
  356. }
  357. out:
  358. /*
  359. * Configure each PCIe RC port.
  360. */
  361. for (i = 0; i < num_rc_controllers; i++) {
  362. /*
  363. * Configure the PCIe MAC to run in RC mode.
  364. */
  365. struct pci_controller *controller = &pci_controllers[i];
  366. controller->index = i;
  367. controller->last_busno = 0xff;
  368. controller->ops = &tile_cfg_ops;
  369. }
  370. return num_rc_controllers;
  371. }
  372. /*
  373. * (pin - 1) converts from the PCI standard's [1:4] convention to
  374. * a normal [0:3] range.
  375. */
  376. static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
  377. {
  378. struct pci_controller *controller =
  379. (struct pci_controller *)dev->sysdata;
  380. return controller->irq_intx_table[pin - 1];
  381. }
  382. static void __devinit fixup_read_and_payload_sizes(struct pci_controller *
  383. controller)
  384. {
  385. gxio_trio_context_t *trio_context = controller->trio;
  386. struct pci_bus *root_bus = controller->root_bus;
  387. TRIO_PCIE_RC_DEVICE_CONTROL_t dev_control;
  388. TRIO_PCIE_RC_DEVICE_CAP_t rc_dev_cap;
  389. unsigned int reg_offset;
  390. struct pci_bus *child;
  391. int mac;
  392. int err;
  393. mac = controller->mac;
  394. /*
  395. * Set our max read request size to be 4KB.
  396. */
  397. reg_offset =
  398. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  399. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  400. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  401. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  402. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  403. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  404. reg_offset);
  405. dev_control.max_read_req_sz = 5;
  406. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  407. dev_control.word);
  408. /*
  409. * Set the max payload size supported by this Gx PCIe MAC.
  410. * Though Gx PCIe supports Max Payload Size of up to 1024 bytes,
  411. * experiments have shown that setting MPS to 256 yields the
  412. * best performance.
  413. */
  414. reg_offset =
  415. (TRIO_PCIE_RC_DEVICE_CAP <<
  416. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  417. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  418. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  419. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  420. rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  421. reg_offset);
  422. rc_dev_cap.mps_sup = 1;
  423. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  424. rc_dev_cap.word);
  425. /* Configure PCI Express MPS setting. */
  426. list_for_each_entry(child, &root_bus->children, node) {
  427. struct pci_dev *self = child->self;
  428. if (!self)
  429. continue;
  430. pcie_bus_configure_settings(child, self->pcie_mpss);
  431. }
  432. /*
  433. * Set the mac_config register in trio based on the MPS/MRS of the link.
  434. */
  435. reg_offset =
  436. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  437. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  438. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  439. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  440. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  441. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  442. reg_offset);
  443. err = gxio_trio_set_mps_mrs(trio_context,
  444. dev_control.max_payload_size,
  445. dev_control.max_read_req_sz,
  446. mac);
  447. if (err < 0) {
  448. pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, "
  449. "MAC %d on TRIO %d\n",
  450. mac, controller->trio_index);
  451. }
  452. }
  453. static int __devinit setup_pcie_rc_delay(char *str)
  454. {
  455. unsigned long delay = 0;
  456. unsigned long trio_index;
  457. unsigned long mac;
  458. if (str == NULL || !isdigit(*str))
  459. return -EINVAL;
  460. trio_index = simple_strtoul(str, (char **)&str, 10);
  461. if (trio_index >= TILEGX_NUM_TRIO)
  462. return -EINVAL;
  463. if (*str != ',')
  464. return -EINVAL;
  465. str++;
  466. if (!isdigit(*str))
  467. return -EINVAL;
  468. mac = simple_strtoul(str, (char **)&str, 10);
  469. if (mac >= TILEGX_TRIO_PCIES)
  470. return -EINVAL;
  471. if (*str != '\0') {
  472. if (*str != ',')
  473. return -EINVAL;
  474. str++;
  475. if (!isdigit(*str))
  476. return -EINVAL;
  477. delay = simple_strtoul(str, (char **)&str, 10);
  478. if (delay > MAX_RC_DELAY)
  479. return -EINVAL;
  480. }
  481. rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
  482. pr_info("Delaying PCIe RC link training for %u sec"
  483. " on MAC %lu on TRIO %lu\n", rc_delay[trio_index][mac],
  484. mac, trio_index);
  485. return 0;
  486. }
  487. early_param("pcie_rc_delay", setup_pcie_rc_delay);
  488. /*
  489. * PCI initialization entry point, called by subsys_initcall.
  490. */
  491. int __init pcibios_init(void)
  492. {
  493. resource_size_t offset;
  494. LIST_HEAD(resources);
  495. int i;
  496. tile_pci_init();
  497. if (num_rc_controllers == 0 && num_ep_controllers == 0)
  498. return 0;
  499. /*
  500. * We loop over all the TRIO shims and set up the MMIO mappings.
  501. */
  502. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  503. gxio_trio_context_t *context = &trio_contexts[i];
  504. if (context->fd < 0)
  505. continue;
  506. /*
  507. * Map in the MMIO space for the MAC.
  508. */
  509. offset = 0;
  510. context->mmio_base_mac =
  511. iorpc_ioremap(context->fd, offset,
  512. HV_TRIO_CONFIG_IOREMAP_SIZE);
  513. if (context->mmio_base_mac == NULL) {
  514. pr_err("PCI: MAC map failure on TRIO %d\n", i);
  515. hv_dev_close(context->fd);
  516. context->fd = -1;
  517. continue;
  518. }
  519. }
  520. /*
  521. * Delay a bit in case devices aren't ready. Some devices are
  522. * known to require at least 20ms here, but we use a more
  523. * conservative value.
  524. */
  525. msleep(250);
  526. /* Scan all of the recorded PCI controllers. */
  527. for (i = 0; i < num_rc_controllers; i++) {
  528. struct pci_controller *controller = &pci_controllers[i];
  529. gxio_trio_context_t *trio_context = controller->trio;
  530. TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
  531. TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
  532. TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
  533. struct pci_bus *bus;
  534. unsigned int reg_offset;
  535. unsigned int class_code_revision;
  536. int trio_index;
  537. int mac;
  538. int ret;
  539. if (trio_context->fd < 0)
  540. continue;
  541. trio_index = controller->trio_index;
  542. mac = controller->mac;
  543. /*
  544. * Check the port strap state which will override the BIB
  545. * setting.
  546. */
  547. reg_offset =
  548. (TRIO_PCIE_INTFC_PORT_CONFIG <<
  549. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  550. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  551. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  552. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  553. port_config.word =
  554. __gxio_mmio_read(trio_context->mmio_base_mac +
  555. reg_offset);
  556. if ((port_config.strap_state !=
  557. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC) &&
  558. (port_config.strap_state !=
  559. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC_G1)) {
  560. /*
  561. * If this is really intended to be an EP port,
  562. * record it so that the endpoint driver will know about it.
  563. */
  564. if (port_config.strap_state ==
  565. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT ||
  566. port_config.strap_state ==
  567. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT_G1)
  568. pcie_ports[trio_index][mac].allow_ep = 1;
  569. continue;
  570. }
  571. /*
  572. * Delay the RC link training if needed.
  573. */
  574. if (rc_delay[trio_index][mac])
  575. msleep(rc_delay[trio_index][mac] * 1000);
  576. ret = gxio_trio_force_rc_link_up(trio_context, mac);
  577. if (ret < 0)
  578. pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
  579. "MAC %d on TRIO %d\n", mac, trio_index);
  580. pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
  581. trio_index, controller->mac);
  582. /*
  583. * Wait a bit here because some EP devices take longer
  584. * to come up.
  585. */
  586. msleep(1000);
  587. /*
  588. * Check for PCIe link-up status.
  589. */
  590. reg_offset =
  591. (TRIO_PCIE_INTFC_PORT_STATUS <<
  592. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  593. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  594. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  595. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  596. port_status.word =
  597. __gxio_mmio_read(trio_context->mmio_base_mac +
  598. reg_offset);
  599. if (!port_status.dl_up) {
  600. pr_err("PCI: link is down, MAC %d on TRIO %d\n",
  601. mac, trio_index);
  602. continue;
  603. }
  604. /*
  605. * Ensure that the link can come out of L1 power down state.
  606. * Strictly speaking, this is needed only in the case of
  607. * heavy RC-initiated DMAs.
  608. */
  609. reg_offset =
  610. (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
  611. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  612. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  613. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  614. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  615. tx_fifo_ctl.word =
  616. __gxio_mmio_read(trio_context->mmio_base_mac +
  617. reg_offset);
  618. tx_fifo_ctl.min_p_credits = 0;
  619. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
  620. tx_fifo_ctl.word);
  621. /*
  622. * Change the device ID so that Linux bus crawl doesn't confuse
  623. * the internal bridge with any Tilera endpoints.
  624. */
  625. reg_offset =
  626. (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
  627. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  628. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  629. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  630. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  631. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  632. (TILERA_GX36_RC_DEV_ID <<
  633. TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
  634. TILERA_VENDOR_ID);
  635. /*
  636. * Set the internal P2P bridge class code.
  637. */
  638. reg_offset =
  639. (TRIO_PCIE_RC_REVISION_ID <<
  640. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  641. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  642. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  643. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  644. class_code_revision =
  645. __gxio_mmio_read32(trio_context->mmio_base_mac +
  646. reg_offset);
  647. class_code_revision = (class_code_revision & 0xff ) |
  648. (PCI_CLASS_BRIDGE_PCI << 16);
  649. __gxio_mmio_write32(trio_context->mmio_base_mac +
  650. reg_offset, class_code_revision);
  651. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  652. /*
  653. * Map in the MMIO space for the PIO region.
  654. */
  655. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
  656. (((unsigned long long)mac) <<
  657. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  658. #else
  659. /*
  660. * Alloc a PIO region for PCI config access per MAC.
  661. */
  662. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  663. if (ret < 0) {
  664. pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
  665. "on TRIO %d, give up\n", mac, trio_index);
  666. continue;
  667. }
  668. trio_context->pio_cfg_index[mac] = ret;
  669. /*
  670. * For PIO CFG, the bus_address_hi parameter is 0.
  671. */
  672. ret = gxio_trio_init_pio_region_aux(trio_context,
  673. trio_context->pio_cfg_index[mac],
  674. mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  675. if (ret < 0) {
  676. pr_err("PCI: PCI CFG PIO init failure for mac %d "
  677. "on TRIO %d, give up\n", mac, trio_index);
  678. continue;
  679. }
  680. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
  681. (((unsigned long long)mac) <<
  682. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  683. #endif
  684. trio_context->mmio_base_pio_cfg[mac] =
  685. iorpc_ioremap(trio_context->fd, offset,
  686. (1 << TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT));
  687. if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
  688. pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
  689. mac, trio_index);
  690. continue;
  691. }
  692. /*
  693. * Initialize the PCIe interrupts.
  694. */
  695. if (tile_init_irqs(controller)) {
  696. pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
  697. mac, trio_index);
  698. continue;
  699. }
  700. /*
  701. * The PCI memory resource is located above the PA space.
  702. * The memory range for the PCI root bus should not overlap
  703. * with the physical RAM
  704. */
  705. pci_add_resource_offset(&resources, &iomem_resource,
  706. 1ULL << CHIP_PA_WIDTH());
  707. bus = pci_scan_root_bus(NULL, 0, controller->ops,
  708. controller, &resources);
  709. controller->root_bus = bus;
  710. controller->last_busno = bus->subordinate;
  711. }
  712. /* Do machine dependent PCI interrupt routing */
  713. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  714. /*
  715. * This comes from the generic Linux PCI driver.
  716. *
  717. * It allocates all of the resources (I/O memory, etc)
  718. * associated with the devices read in above.
  719. */
  720. pci_assign_unassigned_resources();
  721. /* Record the I/O resources in the PCI controller structure. */
  722. for (i = 0; i < num_rc_controllers; i++) {
  723. struct pci_controller *controller = &pci_controllers[i];
  724. gxio_trio_context_t *trio_context = controller->trio;
  725. struct pci_bus *root_bus = pci_controllers[i].root_bus;
  726. struct pci_bus *next_bus;
  727. uint32_t bus_address_hi;
  728. struct pci_dev *dev;
  729. int ret;
  730. int j;
  731. /*
  732. * Skip controllers that are not properly initialized or
  733. * have down links.
  734. */
  735. if (root_bus == NULL)
  736. continue;
  737. /* Configure the max_payload_size values for this domain. */
  738. fixup_read_and_payload_sizes(controller);
  739. list_for_each_entry(dev, &root_bus->devices, bus_list) {
  740. /* Find the PCI host controller, ie. the 1st bridge. */
  741. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  742. (PCI_SLOT(dev->devfn) == 0)) {
  743. next_bus = dev->subordinate;
  744. pci_controllers[i].mem_resources[0] =
  745. *next_bus->resource[0];
  746. pci_controllers[i].mem_resources[1] =
  747. *next_bus->resource[1];
  748. pci_controllers[i].mem_resources[2] =
  749. *next_bus->resource[2];
  750. break;
  751. }
  752. }
  753. if (pci_controllers[i].mem_resources[1].flags & IORESOURCE_MEM)
  754. bus_address_hi =
  755. pci_controllers[i].mem_resources[1].start >> 32;
  756. else if (pci_controllers[i].mem_resources[2].flags & IORESOURCE_PREFETCH)
  757. bus_address_hi =
  758. pci_controllers[i].mem_resources[2].start >> 32;
  759. else {
  760. /* This is unlikely. */
  761. pr_err("PCI: no memory resources on TRIO %d mac %d\n",
  762. controller->trio_index, controller->mac);
  763. continue;
  764. }
  765. /*
  766. * Alloc a PIO region for PCI memory access for each RC port.
  767. */
  768. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  769. if (ret < 0) {
  770. pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
  771. "give up\n", controller->trio_index,
  772. controller->mac);
  773. continue;
  774. }
  775. controller->pio_mem_index = ret;
  776. /*
  777. * For PIO MEM, the bus_address_hi parameter is hard-coded 0
  778. * because we always assign 32-bit PCI bus BAR ranges.
  779. */
  780. ret = gxio_trio_init_pio_region_aux(trio_context,
  781. controller->pio_mem_index,
  782. controller->mac,
  783. 0,
  784. 0);
  785. if (ret < 0) {
  786. pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
  787. "give up\n", controller->trio_index,
  788. controller->mac);
  789. continue;
  790. }
  791. /*
  792. * Configure a Mem-Map region for each memory controller so
  793. * that Linux can map all of its PA space to the PCI bus.
  794. * Use the IOMMU to handle hash-for-home memory.
  795. */
  796. for_each_online_node(j) {
  797. unsigned long start_pfn = node_start_pfn[j];
  798. unsigned long end_pfn = node_end_pfn[j];
  799. unsigned long nr_pages = end_pfn - start_pfn;
  800. ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
  801. 0);
  802. if (ret < 0) {
  803. pr_err("PCI: Mem-Map alloc failure on TRIO %d "
  804. "mac %d for MC %d, give up\n",
  805. controller->trio_index,
  806. controller->mac, j);
  807. goto alloc_mem_map_failed;
  808. }
  809. controller->mem_maps[j] = ret;
  810. /*
  811. * Initialize the Mem-Map and the I/O MMU so that all
  812. * the physical memory can be accessed by the endpoint
  813. * devices. The base bus address is set to the base CPA
  814. * of this memory controller plus an offset (see pci.h).
  815. * The region's base VA is set to the base CPA. The
  816. * I/O MMU table essentially translates the CPA to
  817. * the real PA. Implicitly, for node 0, we create
  818. * a separate Mem-Map region that serves as the inbound
  819. * window for legacy 32-bit devices. This is a direct
  820. * map of the low 4GB CPA space.
  821. */
  822. ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
  823. controller->mem_maps[j],
  824. start_pfn << PAGE_SHIFT,
  825. nr_pages << PAGE_SHIFT,
  826. trio_context->asid,
  827. controller->mac,
  828. (start_pfn << PAGE_SHIFT) +
  829. TILE_PCI_MEM_MAP_BASE_OFFSET,
  830. j,
  831. GXIO_TRIO_ORDER_MODE_UNORDERED);
  832. if (ret < 0) {
  833. pr_err("PCI: Mem-Map init failure on TRIO %d "
  834. "mac %d for MC %d, give up\n",
  835. controller->trio_index,
  836. controller->mac, j);
  837. goto alloc_mem_map_failed;
  838. }
  839. continue;
  840. alloc_mem_map_failed:
  841. break;
  842. }
  843. }
  844. return 0;
  845. }
  846. subsys_initcall(pcibios_init);
  847. /*
  848. * PCI scan code calls the arch specific pcibios_fixup_bus() each time it scans
  849. * a new bridge. Called after each bus is probed, but before its children are
  850. * examined.
  851. */
  852. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  853. {
  854. struct pci_dev *dev = bus->self;
  855. if (!dev) {
  856. /* This is the root bus. */
  857. bus->resource[0] = &pci_ioport_resource;
  858. bus->resource[1] = &pci_iomem_resource;
  859. }
  860. }
  861. /*
  862. * This can be called from the generic PCI layer, but doesn't need to
  863. * do anything.
  864. */
  865. char __devinit *pcibios_setup(char *str)
  866. {
  867. if (!strcmp(str, "off")) {
  868. pci_probe = 0;
  869. return NULL;
  870. }
  871. return str;
  872. }
  873. /*
  874. * This is called from the generic Linux layer.
  875. */
  876. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  877. {
  878. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  879. }
  880. /*
  881. * Enable memory address decoding, as appropriate, for the
  882. * device described by the 'dev' struct. The I/O decoding
  883. * is disabled, though the TILE-Gx supports I/O addressing.
  884. *
  885. * This is called from the generic PCI layer, and can be called
  886. * for bridges or endpoints.
  887. */
  888. int pcibios_enable_device(struct pci_dev *dev, int mask)
  889. {
  890. return pci_enable_resources(dev, mask);
  891. }
  892. /* Called for each device after PCI setup is done. */
  893. static void __init
  894. pcibios_fixup_final(struct pci_dev *pdev)
  895. {
  896. set_dma_ops(&pdev->dev, gx_pci_dma_map_ops);
  897. set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
  898. pdev->dev.archdata.max_direct_dma_addr =
  899. TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  900. }
  901. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  902. /* Map a PCI MMIO bus address into VA space. */
  903. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  904. {
  905. struct pci_controller *controller = NULL;
  906. resource_size_t bar_start;
  907. resource_size_t bar_end;
  908. resource_size_t offset;
  909. resource_size_t start;
  910. resource_size_t end;
  911. int trio_fd;
  912. int i, j;
  913. start = phys_addr;
  914. end = phys_addr + size - 1;
  915. /*
  916. * In the following, each PCI controller's mem_resources[1]
  917. * represents its (non-prefetchable) PCI memory resource and
  918. * mem_resources[2] refers to its prefetchable PCI memory resource.
  919. * By searching phys_addr in each controller's mem_resources[], we can
  920. * determine the controller that should accept the PCI memory access.
  921. */
  922. for (i = 0; i < num_rc_controllers; i++) {
  923. /*
  924. * Skip controllers that are not properly initialized or
  925. * have down links.
  926. */
  927. if (pci_controllers[i].root_bus == NULL)
  928. continue;
  929. for (j = 1; j < 3; j++) {
  930. bar_start =
  931. pci_controllers[i].mem_resources[j].start;
  932. bar_end =
  933. pci_controllers[i].mem_resources[j].end;
  934. if ((start >= bar_start) && (end <= bar_end)) {
  935. controller = &pci_controllers[i];
  936. goto got_it;
  937. }
  938. }
  939. }
  940. if (controller == NULL)
  941. return NULL;
  942. got_it:
  943. trio_fd = controller->trio->fd;
  944. offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + phys_addr;
  945. /*
  946. * We need to keep the PCI bus address's in-page offset in the VA.
  947. */
  948. return iorpc_ioremap(trio_fd, offset, size) +
  949. (phys_addr & (PAGE_SIZE - 1));
  950. }
  951. EXPORT_SYMBOL(ioremap);
  952. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  953. {
  954. iounmap(addr);
  955. }
  956. EXPORT_SYMBOL(pci_iounmap);
  957. /****************************************************************
  958. *
  959. * Tile PCI config space read/write routines
  960. *
  961. ****************************************************************/
  962. /*
  963. * These are the normal read and write ops
  964. * These are expanded with macros from pci_bus_read_config_byte() etc.
  965. *
  966. * devfn is the combined PCI device & function.
  967. *
  968. * offset is in bytes, from the start of config space for the
  969. * specified bus & device.
  970. */
  971. static int __devinit tile_cfg_read(struct pci_bus *bus,
  972. unsigned int devfn,
  973. int offset,
  974. int size,
  975. u32 *val)
  976. {
  977. struct pci_controller *controller = bus->sysdata;
  978. gxio_trio_context_t *trio_context = controller->trio;
  979. int busnum = bus->number & 0xff;
  980. int device = PCI_SLOT(devfn);
  981. int function = PCI_FUNC(devfn);
  982. int config_type = 1;
  983. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  984. void *mmio_addr;
  985. /*
  986. * Map all accesses to the local device (bus == 0) into the
  987. * MMIO space of the MAC. Accesses to the downstream devices
  988. * go to the PIO space.
  989. */
  990. if (busnum == 0) {
  991. if (device == 0) {
  992. /*
  993. * This is the internal downstream P2P bridge,
  994. * access directly.
  995. */
  996. unsigned int reg_offset;
  997. reg_offset = ((offset & 0xFFF) <<
  998. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  999. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1000. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1001. (controller->mac <<
  1002. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1003. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1004. goto valid_device;
  1005. } else {
  1006. /*
  1007. * We fake an empty device for (device > 0),
  1008. * since there is only one device on bus 0.
  1009. */
  1010. goto invalid_device;
  1011. }
  1012. }
  1013. /*
  1014. * Accesses to the directly attached device (bus == 1) have to be
  1015. * sent as type-0 configs.
  1016. */
  1017. if (busnum == 1) {
  1018. /*
  1019. * There is only one device off of our built-in P2P bridge.
  1020. */
  1021. if (device != 0)
  1022. goto invalid_device;
  1023. config_type = 0;
  1024. }
  1025. cfg_addr.word = 0;
  1026. cfg_addr.reg_addr = (offset & 0xFFF);
  1027. cfg_addr.fn = function;
  1028. cfg_addr.dev = device;
  1029. cfg_addr.bus = busnum;
  1030. cfg_addr.type = config_type;
  1031. /*
  1032. * Note that we don't set the mac field in cfg_addr because the
  1033. * mapping is per port.
  1034. */
  1035. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1036. cfg_addr.word;
  1037. valid_device:
  1038. switch (size) {
  1039. case 4:
  1040. *val = __gxio_mmio_read32(mmio_addr);
  1041. break;
  1042. case 2:
  1043. *val = __gxio_mmio_read16(mmio_addr);
  1044. break;
  1045. case 1:
  1046. *val = __gxio_mmio_read8(mmio_addr);
  1047. break;
  1048. default:
  1049. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1050. }
  1051. TRACE_CFG_RD(size, *val, busnum, device, function, offset);
  1052. return 0;
  1053. invalid_device:
  1054. switch (size) {
  1055. case 4:
  1056. *val = 0xFFFFFFFF;
  1057. break;
  1058. case 2:
  1059. *val = 0xFFFF;
  1060. break;
  1061. case 1:
  1062. *val = 0xFF;
  1063. break;
  1064. default:
  1065. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1066. }
  1067. return 0;
  1068. }
  1069. /*
  1070. * See tile_cfg_read() for relevent comments.
  1071. * Note that "val" is the value to write, not a pointer to that value.
  1072. */
  1073. static int __devinit tile_cfg_write(struct pci_bus *bus,
  1074. unsigned int devfn,
  1075. int offset,
  1076. int size,
  1077. u32 val)
  1078. {
  1079. struct pci_controller *controller = bus->sysdata;
  1080. gxio_trio_context_t *trio_context = controller->trio;
  1081. int busnum = bus->number & 0xff;
  1082. int device = PCI_SLOT(devfn);
  1083. int function = PCI_FUNC(devfn);
  1084. int config_type = 1;
  1085. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1086. void *mmio_addr;
  1087. u32 val_32 = (u32)val;
  1088. u16 val_16 = (u16)val;
  1089. u8 val_8 = (u8)val;
  1090. /*
  1091. * Map all accesses to the local device (bus == 0) into the
  1092. * MMIO space of the MAC. Accesses to the downstream devices
  1093. * go to the PIO space.
  1094. */
  1095. if (busnum == 0) {
  1096. if (device == 0) {
  1097. /*
  1098. * This is the internal downstream P2P bridge,
  1099. * access directly.
  1100. */
  1101. unsigned int reg_offset;
  1102. reg_offset = ((offset & 0xFFF) <<
  1103. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1104. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1105. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1106. (controller->mac <<
  1107. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1108. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1109. goto valid_device;
  1110. } else {
  1111. /*
  1112. * We fake an empty device for (device > 0),
  1113. * since there is only one device on bus 0.
  1114. */
  1115. goto invalid_device;
  1116. }
  1117. }
  1118. /*
  1119. * Accesses to the directly attached device (bus == 1) have to be
  1120. * sent as type-0 configs.
  1121. */
  1122. if (busnum == 1) {
  1123. /*
  1124. * There is only one device off of our built-in P2P bridge.
  1125. */
  1126. if (device != 0)
  1127. goto invalid_device;
  1128. config_type = 0;
  1129. }
  1130. cfg_addr.word = 0;
  1131. cfg_addr.reg_addr = (offset & 0xFFF);
  1132. cfg_addr.fn = function;
  1133. cfg_addr.dev = device;
  1134. cfg_addr.bus = busnum;
  1135. cfg_addr.type = config_type;
  1136. /*
  1137. * Note that we don't set the mac field in cfg_addr because the
  1138. * mapping is per port.
  1139. */
  1140. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1141. cfg_addr.word;
  1142. valid_device:
  1143. switch (size) {
  1144. case 4:
  1145. __gxio_mmio_write32(mmio_addr, val_32);
  1146. TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
  1147. break;
  1148. case 2:
  1149. __gxio_mmio_write16(mmio_addr, val_16);
  1150. TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
  1151. break;
  1152. case 1:
  1153. __gxio_mmio_write8(mmio_addr, val_8);
  1154. TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
  1155. break;
  1156. default:
  1157. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1158. }
  1159. invalid_device:
  1160. return 0;
  1161. }
  1162. static struct pci_ops tile_cfg_ops = {
  1163. .read = tile_cfg_read,
  1164. .write = tile_cfg_write,
  1165. };
  1166. /*
  1167. * MSI support starts here.
  1168. */
  1169. static unsigned int
  1170. tilegx_msi_startup(struct irq_data *d)
  1171. {
  1172. if (d->msi_desc)
  1173. unmask_msi_irq(d);
  1174. return 0;
  1175. }
  1176. static void
  1177. tilegx_msi_ack(struct irq_data *d)
  1178. {
  1179. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  1180. }
  1181. static void
  1182. tilegx_msi_mask(struct irq_data *d)
  1183. {
  1184. mask_msi_irq(d);
  1185. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  1186. }
  1187. static void
  1188. tilegx_msi_unmask(struct irq_data *d)
  1189. {
  1190. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  1191. unmask_msi_irq(d);
  1192. }
  1193. static struct irq_chip tilegx_msi_chip = {
  1194. .name = "tilegx_msi",
  1195. .irq_startup = tilegx_msi_startup,
  1196. .irq_ack = tilegx_msi_ack,
  1197. .irq_mask = tilegx_msi_mask,
  1198. .irq_unmask = tilegx_msi_unmask,
  1199. /* TBD: support set_affinity. */
  1200. };
  1201. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  1202. {
  1203. struct pci_controller *controller;
  1204. gxio_trio_context_t *trio_context;
  1205. struct msi_msg msg;
  1206. int default_irq;
  1207. uint64_t mem_map_base;
  1208. uint64_t mem_map_limit;
  1209. u64 msi_addr;
  1210. int mem_map;
  1211. int cpu;
  1212. int irq;
  1213. int ret;
  1214. irq = create_irq();
  1215. if (irq < 0)
  1216. return irq;
  1217. /*
  1218. * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
  1219. * devices that are not capable of generating a 64-bit message address.
  1220. * These devices will fall back to using the legacy interrupts.
  1221. * Most PCIe endpoint devices do support 64-bit message addressing.
  1222. */
  1223. if (desc->msi_attrib.is_64 == 0) {
  1224. dev_printk(KERN_INFO, &pdev->dev,
  1225. "64-bit MSI message address not supported, "
  1226. "falling back to legacy interrupts.\n");
  1227. ret = -ENOMEM;
  1228. goto is_64_failure;
  1229. }
  1230. default_irq = desc->msi_attrib.default_irq;
  1231. controller = irq_get_handler_data(default_irq);
  1232. BUG_ON(!controller);
  1233. trio_context = controller->trio;
  1234. /*
  1235. * Allocate the Mem-Map that will accept the MSI write and
  1236. * trigger the TILE-side interrupts.
  1237. */
  1238. mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
  1239. if (mem_map < 0) {
  1240. dev_printk(KERN_INFO, &pdev->dev,
  1241. "%s Mem-Map alloc failure. "
  1242. "Failed to initialize MSI interrupts. "
  1243. "Falling back to legacy interrupts.\n",
  1244. desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
  1245. ret = -ENOMEM;
  1246. goto msi_mem_map_alloc_failure;
  1247. }
  1248. /* We try to distribute different IRQs to different tiles. */
  1249. cpu = tile_irq_cpu(irq);
  1250. /*
  1251. * Now call up to the HV to configure the Mem-Map interrupt and
  1252. * set up the IPI binding.
  1253. */
  1254. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1255. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1256. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1257. ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
  1258. KERNEL_PL, irq, controller->mac,
  1259. mem_map, mem_map_base, mem_map_limit,
  1260. trio_context->asid);
  1261. if (ret < 0) {
  1262. dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
  1263. goto hv_msi_config_failure;
  1264. }
  1265. irq_set_msi_desc(irq, desc);
  1266. msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 - TRIO_MAP_MEM_REG_INT0;
  1267. msg.address_hi = msi_addr >> 32;
  1268. msg.address_lo = msi_addr & 0xffffffff;
  1269. msg.data = mem_map;
  1270. write_msi_msg(irq, &msg);
  1271. irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
  1272. irq_set_handler_data(irq, controller);
  1273. return 0;
  1274. hv_msi_config_failure:
  1275. /* Free mem-map */
  1276. msi_mem_map_alloc_failure:
  1277. is_64_failure:
  1278. destroy_irq(irq);
  1279. return ret;
  1280. }
  1281. void arch_teardown_msi_irq(unsigned int irq)
  1282. {
  1283. destroy_irq(irq);
  1284. }