pci_gx.c 38 KB

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