2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
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.
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
15 #include <linux/kernel.h>
16 #include <linux/mmzone.h>
17 #include <linux/pci.h>
18 #include <linux/delay.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/capability.h>
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/irq.h>
25 #include <linux/msi.h>
27 #include <linux/uaccess.h>
28 #include <linux/ctype.h>
30 #include <asm/processor.h>
31 #include <asm/sections.h>
32 #include <asm/byteorder.h>
34 #include <gxio/iorpc_globals.h>
35 #include <gxio/kiorpc.h>
36 #include <gxio/trio.h>
37 #include <gxio/iorpc_trio.h>
38 #include <hv/drv_trio_intf.h>
43 * This file containes the routines to search for PCI buses,
44 * enumerate the buses, and configure any attached devices.
47 #define DEBUG_PCI_CFG 0
50 #define TRACE_CFG_WR(size, val, bus, dev, func, offset) \
51 pr_info("CFG WR %d-byte VAL %#x to bus %d dev %d func %d addr %u\n", \
52 size, val, bus, dev, func, offset & 0xFFF);
53 #define TRACE_CFG_RD(size, val, bus, dev, func, offset) \
54 pr_info("CFG RD %d-byte VAL %#x from bus %d dev %d func %d addr %u\n", \
55 size, val, bus, dev, func, offset & 0xFFF);
57 #define TRACE_CFG_WR(...)
58 #define TRACE_CFG_RD(...)
61 static int pci_probe = 1;
63 /* Information on the PCIe RC ports configuration. */
64 static int pcie_rc[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
67 * On some platforms with one or more Gx endpoint ports, we need to
68 * delay the PCIe RC port probe for a few seconds to work around
69 * a HW PCIe link-training bug. The exact delay is specified with
70 * a kernel boot argument in the form of "pcie_rc_delay=T,P,S",
71 * where T is the TRIO instance number, P is the port number and S is
72 * the delay in seconds. If the argument is specified, but the delay is
73 * not provided, the value will be DEFAULT_RC_DELAY.
75 static int rc_delay[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
77 /* Default number of seconds that the PCIe RC port probe can be delayed. */
78 #define DEFAULT_RC_DELAY 10
80 /* Array of the PCIe ports configuration info obtained from the BIB. */
81 struct pcie_port_property pcie_ports[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
83 /* All drivers share the TRIO contexts defined here. */
84 gxio_trio_context_t trio_contexts[TILEGX_NUM_TRIO];
86 /* Pointer to an array of PCIe RC controllers. */
87 struct pci_controller pci_controllers[TILEGX_NUM_TRIO * TILEGX_TRIO_PCIES];
88 int num_rc_controllers;
89 static int num_ep_controllers;
91 static struct pci_ops tile_cfg_ops;
93 /* Mask of CPUs that should receive PCIe interrupts. */
94 static struct cpumask intr_cpus_map;
97 * We don't need to worry about the alignment of resources.
99 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
100 resource_size_t size, resource_size_t align)
104 EXPORT_SYMBOL(pcibios_align_resource);
108 * Pick a CPU to receive and handle the PCIe interrupts, based on the IRQ #.
109 * For now, we simply send interrupts to non-dataplane CPUs.
110 * We may implement methods to allow user to specify the target CPUs,
111 * e.g. via boot arguments.
113 static int tile_irq_cpu(int irq)
119 count = cpumask_weight(&intr_cpus_map);
120 if (unlikely(count == 0)) {
121 pr_warning("intr_cpus_map empty, interrupts will be"
122 " delievered to dataplane tiles\n");
123 return irq % (smp_height * smp_width);
127 for_each_cpu(cpu, &intr_cpus_map) {
135 * Open a file descriptor to the TRIO shim.
137 static int tile_pcie_open(int trio_index)
139 gxio_trio_context_t *context = &trio_contexts[trio_index];
143 * This opens a file descriptor to the TRIO shim.
145 ret = gxio_trio_init(context, trio_index);
150 * Allocate an ASID for the kernel.
152 ret = gxio_trio_alloc_asids(context, 1, 0, 0);
154 pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
156 goto asid_alloc_failure;
161 #ifdef USE_SHARED_PCIE_CONFIG_REGION
163 * Alloc a PIO region for config access, shared by all MACs per TRIO.
164 * This shouldn't fail since the kernel is supposed to the first
165 * client of the TRIO's PIO regions.
167 ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
169 pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
171 goto pio_alloc_failure;
174 context->pio_cfg_index = ret;
177 * For PIO CFG, the bus_address_hi parameter is 0. The mac parameter
178 * is also 0 because it is specified in PIO_REGION_SETUP_CFG_ADDR.
180 ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
181 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
183 pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
185 goto pio_alloc_failure;
192 #ifdef USE_SHARED_PCIE_CONFIG_REGION
195 hv_dev_close(context->fd);
201 tilegx_legacy_irq_ack(struct irq_data *d)
203 __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
207 tilegx_legacy_irq_mask(struct irq_data *d)
209 __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
213 tilegx_legacy_irq_unmask(struct irq_data *d)
215 __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
218 static struct irq_chip tilegx_legacy_irq_chip = {
219 .name = "tilegx_legacy_irq",
220 .irq_ack = tilegx_legacy_irq_ack,
221 .irq_mask = tilegx_legacy_irq_mask,
222 .irq_unmask = tilegx_legacy_irq_unmask,
224 /* TBD: support set_affinity. */
228 * This is a wrapper function of the kernel level-trigger interrupt
229 * handler handle_level_irq() for PCI legacy interrupts. The TRIO
230 * is configured such that only INTx Assert interrupts are proxied
231 * to Linux which just calls handle_level_irq() after clearing the
232 * MAC INTx Assert status bit associated with this interrupt.
235 trio_handle_level_irq(unsigned int irq, struct irq_desc *desc)
237 struct pci_controller *controller = irq_desc_get_handler_data(desc);
238 gxio_trio_context_t *trio_context = controller->trio;
239 uint64_t intx = (uint64_t)irq_desc_get_chip_data(desc);
240 int mac = controller->mac;
241 unsigned int reg_offset;
244 handle_level_irq(irq, desc);
247 * Clear the INTx Level status, otherwise future interrupts are
250 reg_offset = (TRIO_PCIE_INTFC_MAC_INT_STS <<
251 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
252 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
253 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
254 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
256 level_mask = TRIO_PCIE_INTFC_MAC_INT_STS__INT_LEVEL_MASK << intx;
258 __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset, level_mask);
262 * Create kernel irqs and set up the handlers for the legacy interrupts.
263 * Also some minimum initialization for the MSI support.
265 static int tile_init_irqs(struct pci_controller *controller)
272 cpumask_copy(&intr_cpus_map, cpu_online_mask);
275 for (i = 0; i < 4; i++) {
276 gxio_trio_context_t *context = controller->trio;
279 /* Ask the kernel to allocate an IRQ. */
282 pr_err("PCI: no free irq vectors, failed for %d\n", i);
286 controller->irq_intx_table[i] = irq;
288 /* Distribute the 4 IRQs to different tiles. */
289 cpu = tile_irq_cpu(irq);
291 /* Configure the TRIO intr binding for this IRQ. */
292 result = gxio_trio_config_legacy_intr(context, cpu_x(cpu),
293 cpu_y(cpu), KERNEL_PL,
294 irq, controller->mac, i);
296 pr_err("PCI: MAC intx config failed for %d\n", i);
302 * Register the IRQ handler with the kernel.
304 irq_set_chip_and_handler(irq, &tilegx_legacy_irq_chip,
305 trio_handle_level_irq);
306 irq_set_chip_data(irq, (void *)(uint64_t)i);
307 irq_set_handler_data(irq, controller);
313 for (j = 0; j < i; j++)
314 destroy_irq(controller->irq_intx_table[j]);
320 * Find valid controllers and fill in pci_controller structs for each
323 * Returns the number of controllers discovered.
325 int __init tile_pci_init(void)
327 int num_trio_shims = 0;
332 pr_info("PCI: disabled by boot argument\n");
336 pr_info("PCI: Searching for controllers...\n");
339 * We loop over all the TRIO shims.
341 for (i = 0; i < TILEGX_NUM_TRIO; i++) {
344 ret = tile_pcie_open(i);
351 if (num_trio_shims == 0 || sim_is_simulator())
355 * Now determine which PCIe ports are configured to operate in RC mode.
356 * We look at the Board Information Block first and then see if there
357 * are any overriding configuration by the HW strapping pin.
359 for (i = 0; i < TILEGX_NUM_TRIO; i++) {
360 gxio_trio_context_t *context = &trio_contexts[i];
366 ret = hv_dev_pread(context->fd, 0,
367 (HV_VirtAddr)&pcie_ports[i][0],
368 sizeof(struct pcie_port_property) * TILEGX_TRIO_PCIES,
369 GXIO_TRIO_OP_GET_PORT_PROPERTY);
371 pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d,"
372 " on TRIO %d\n", ret, i);
376 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
377 if (pcie_ports[i][j].allow_rc) {
379 num_rc_controllers++;
381 else if (pcie_ports[i][j].allow_ep) {
382 num_ep_controllers++;
388 * Return if no PCIe ports are configured to operate in RC mode.
390 if (num_rc_controllers == 0)
394 * Set the TRIO pointer and MAC index for each PCIe RC port.
396 for (i = 0; i < TILEGX_NUM_TRIO; i++) {
397 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
399 pci_controllers[ctl_index].trio =
401 pci_controllers[ctl_index].mac = j;
402 pci_controllers[ctl_index].trio_index = i;
404 if (ctl_index == num_rc_controllers)
412 * Configure each PCIe RC port.
414 for (i = 0; i < num_rc_controllers; i++) {
416 * Configure the PCIe MAC to run in RC mode.
419 struct pci_controller *controller = &pci_controllers[i];
421 controller->index = i;
422 controller->ops = &tile_cfg_ops;
425 * The PCI memory resource is located above the PA space.
426 * For every host bridge, the BAR window or the MMIO aperture
427 * is in range [3GB, 4GB - 1] of a 4GB space beyond the
431 controller->mem_offset = TILE_PCI_MEM_START +
432 (i * TILE_PCI_BAR_WINDOW_TOP);
433 controller->mem_space.start = controller->mem_offset +
434 TILE_PCI_BAR_WINDOW_TOP - TILE_PCI_BAR_WINDOW_SIZE;
435 controller->mem_space.end = controller->mem_offset +
436 TILE_PCI_BAR_WINDOW_TOP - 1;
437 controller->mem_space.flags = IORESOURCE_MEM;
438 snprintf(controller->mem_space_name,
439 sizeof(controller->mem_space_name),
440 "PCI mem domain %d", i);
441 controller->mem_space.name = controller->mem_space_name;
444 return num_rc_controllers;
448 * (pin - 1) converts from the PCI standard's [1:4] convention to
449 * a normal [0:3] range.
451 static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
453 struct pci_controller *controller =
454 (struct pci_controller *)dev->sysdata;
455 return controller->irq_intx_table[pin - 1];
459 static void fixup_read_and_payload_sizes(struct pci_controller *controller)
461 gxio_trio_context_t *trio_context = controller->trio;
462 struct pci_bus *root_bus = controller->root_bus;
463 TRIO_PCIE_RC_DEVICE_CONTROL_t dev_control;
464 TRIO_PCIE_RC_DEVICE_CAP_t rc_dev_cap;
465 unsigned int reg_offset;
466 struct pci_bus *child;
470 mac = controller->mac;
473 * Set our max read request size to be 4KB.
476 (TRIO_PCIE_RC_DEVICE_CONTROL <<
477 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
478 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
479 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
480 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
482 dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
484 dev_control.max_read_req_sz = 5;
485 __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
489 * Set the max payload size supported by this Gx PCIe MAC.
490 * Though Gx PCIe supports Max Payload Size of up to 1024 bytes,
491 * experiments have shown that setting MPS to 256 yields the
495 (TRIO_PCIE_RC_DEVICE_CAP <<
496 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
497 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
498 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
499 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
501 rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
503 rc_dev_cap.mps_sup = 1;
504 __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
507 /* Configure PCI Express MPS setting. */
508 list_for_each_entry(child, &root_bus->children, node) {
509 struct pci_dev *self = child->self;
513 pcie_bus_configure_settings(child, self->pcie_mpss);
517 * Set the mac_config register in trio based on the MPS/MRS of the link.
520 (TRIO_PCIE_RC_DEVICE_CONTROL <<
521 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
522 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
523 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
524 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
526 dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
529 err = gxio_trio_set_mps_mrs(trio_context,
530 dev_control.max_payload_size,
531 dev_control.max_read_req_sz,
534 pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, "
535 "MAC %d on TRIO %d\n",
536 mac, controller->trio_index);
540 static int setup_pcie_rc_delay(char *str)
542 unsigned long delay = 0;
543 unsigned long trio_index;
546 if (str == NULL || !isdigit(*str))
548 trio_index = simple_strtoul(str, (char **)&str, 10);
549 if (trio_index >= TILEGX_NUM_TRIO)
558 mac = simple_strtoul(str, (char **)&str, 10);
559 if (mac >= TILEGX_TRIO_PCIES)
569 delay = simple_strtoul(str, (char **)&str, 10);
572 rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
575 early_param("pcie_rc_delay", setup_pcie_rc_delay);
578 * PCI initialization entry point, called by subsys_initcall.
580 int __init pcibios_init(void)
582 resource_size_t offset;
583 LIST_HEAD(resources);
589 if (num_rc_controllers == 0 && num_ep_controllers == 0)
593 * We loop over all the TRIO shims and set up the MMIO mappings.
595 for (i = 0; i < TILEGX_NUM_TRIO; i++) {
596 gxio_trio_context_t *context = &trio_contexts[i];
602 * Map in the MMIO space for the MAC.
605 context->mmio_base_mac =
606 iorpc_ioremap(context->fd, offset,
607 HV_TRIO_CONFIG_IOREMAP_SIZE);
608 if (context->mmio_base_mac == NULL) {
609 pr_err("PCI: MAC map failure on TRIO %d\n", i);
611 hv_dev_close(context->fd);
618 * Delay a bit in case devices aren't ready. Some devices are
619 * known to require at least 20ms here, but we use a more
620 * conservative value.
624 /* Scan all of the recorded PCI controllers. */
625 for (next_busno = 0, i = 0; i < num_rc_controllers; i++) {
626 struct pci_controller *controller = &pci_controllers[i];
627 gxio_trio_context_t *trio_context = controller->trio;
628 TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
629 TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
630 TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
632 unsigned int reg_offset;
633 unsigned int class_code_revision;
638 if (trio_context->fd < 0)
641 trio_index = controller->trio_index;
642 mac = controller->mac;
645 * Check the port strap state which will override the BIB
650 (TRIO_PCIE_INTFC_PORT_CONFIG <<
651 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
652 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
653 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
654 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
657 __gxio_mmio_read(trio_context->mmio_base_mac +
660 if ((port_config.strap_state !=
661 TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC) &&
662 (port_config.strap_state !=
663 TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC_G1)) {
665 * If this is really intended to be an EP port,
666 * record it so that the endpoint driver will know about it.
668 if (port_config.strap_state ==
669 TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT ||
670 port_config.strap_state ==
671 TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT_G1)
672 pcie_ports[trio_index][mac].allow_ep = 1;
678 * Check for PCIe link-up status to decide if we need
679 * to force the link to come up.
682 (TRIO_PCIE_INTFC_PORT_STATUS <<
683 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
684 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
685 TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
686 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
689 __gxio_mmio_read(trio_context->mmio_base_mac +
691 if (!port_status.dl_up) {
692 if (rc_delay[trio_index][mac]) {
693 pr_info("Delaying PCIe RC TRIO init %d sec"
694 " on MAC %d on TRIO %d\n",
695 rc_delay[trio_index][mac], mac,
697 msleep(rc_delay[trio_index][mac] * 1000);
699 ret = gxio_trio_force_rc_link_up(trio_context, mac);
701 pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
702 "MAC %d on TRIO %d\n", mac, trio_index);
705 pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
706 trio_index, controller->mac);
709 * Delay the bus probe if needed.
711 if (rc_delay[trio_index][mac]) {
712 pr_info("Delaying PCIe RC bus enumerating %d sec"
713 " on MAC %d on TRIO %d\n",
714 rc_delay[trio_index][mac], mac,
716 msleep(rc_delay[trio_index][mac] * 1000);
719 * Wait a bit here because some EP devices
720 * take longer to come up.
726 * Check for PCIe link-up status again.
729 __gxio_mmio_read(trio_context->mmio_base_mac +
731 if (!port_status.dl_up) {
732 pr_err("PCI: link is down, MAC %d on TRIO %d\n",
738 * Ensure that the link can come out of L1 power down state.
739 * Strictly speaking, this is needed only in the case of
740 * heavy RC-initiated DMAs.
743 (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
744 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
745 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
746 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
747 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
749 __gxio_mmio_read(trio_context->mmio_base_mac +
751 tx_fifo_ctl.min_p_credits = 0;
752 __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
756 * Change the device ID so that Linux bus crawl doesn't confuse
757 * the internal bridge with any Tilera endpoints.
761 (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
762 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
763 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
764 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
765 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
767 __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
768 (TILERA_GX36_RC_DEV_ID <<
769 TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
773 * Set the internal P2P bridge class code.
777 (TRIO_PCIE_RC_REVISION_ID <<
778 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
779 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
780 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
781 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
783 class_code_revision =
784 __gxio_mmio_read32(trio_context->mmio_base_mac +
786 class_code_revision = (class_code_revision & 0xff ) |
787 (PCI_CLASS_BRIDGE_PCI << 16);
789 __gxio_mmio_write32(trio_context->mmio_base_mac +
790 reg_offset, class_code_revision);
792 #ifdef USE_SHARED_PCIE_CONFIG_REGION
795 * Map in the MMIO space for the PIO region.
797 offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
798 (((unsigned long long)mac) <<
799 TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
804 * Alloc a PIO region for PCI config access per MAC.
806 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
808 pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
809 "on TRIO %d, give up\n", mac, trio_index);
814 trio_context->pio_cfg_index[mac] = ret;
817 * For PIO CFG, the bus_address_hi parameter is 0.
819 ret = gxio_trio_init_pio_region_aux(trio_context,
820 trio_context->pio_cfg_index[mac],
821 mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
823 pr_err("PCI: PCI CFG PIO init failure for mac %d "
824 "on TRIO %d, give up\n", mac, trio_index);
829 offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
830 (((unsigned long long)mac) <<
831 TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
835 trio_context->mmio_base_pio_cfg[mac] =
836 iorpc_ioremap(trio_context->fd, offset,
837 (1 << TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT));
838 if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
839 pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
846 * Initialize the PCIe interrupts.
848 if (tile_init_irqs(controller)) {
849 pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
856 * The PCI memory resource is located above the PA space.
857 * The memory range for the PCI root bus should not overlap
858 * with the physical RAM
860 pci_add_resource_offset(&resources, &controller->mem_space,
861 controller->mem_offset);
863 controller->first_busno = next_busno;
864 bus = pci_scan_root_bus(NULL, next_busno, controller->ops,
865 controller, &resources);
866 controller->root_bus = bus;
867 next_busno = bus->busn_res.end + 1;
871 /* Do machine dependent PCI interrupt routing */
872 pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
875 * This comes from the generic Linux PCI driver.
877 * It allocates all of the resources (I/O memory, etc)
878 * associated with the devices read in above.
881 pci_assign_unassigned_resources();
883 /* Record the I/O resources in the PCI controller structure. */
884 for (i = 0; i < num_rc_controllers; i++) {
885 struct pci_controller *controller = &pci_controllers[i];
886 gxio_trio_context_t *trio_context = controller->trio;
887 struct pci_bus *root_bus = pci_controllers[i].root_bus;
888 struct pci_bus *next_bus;
889 uint32_t bus_address_hi;
895 * Skip controllers that are not properly initialized or
898 if (root_bus == NULL)
901 /* Configure the max_payload_size values for this domain. */
902 fixup_read_and_payload_sizes(controller);
904 list_for_each_entry(dev, &root_bus->devices, bus_list) {
905 /* Find the PCI host controller, ie. the 1st bridge. */
906 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
907 (PCI_SLOT(dev->devfn) == 0)) {
908 next_bus = dev->subordinate;
909 pci_controllers[i].mem_resources[0] =
910 *next_bus->resource[0];
911 pci_controllers[i].mem_resources[1] =
912 *next_bus->resource[1];
913 pci_controllers[i].mem_resources[2] =
914 *next_bus->resource[2];
920 if (pci_controllers[i].mem_resources[1].flags & IORESOURCE_MEM)
922 pci_controllers[i].mem_resources[1].start >> 32;
923 else if (pci_controllers[i].mem_resources[2].flags & IORESOURCE_PREFETCH)
925 pci_controllers[i].mem_resources[2].start >> 32;
927 /* This is unlikely. */
928 pr_err("PCI: no memory resources on TRIO %d mac %d\n",
929 controller->trio_index, controller->mac);
934 * Alloc a PIO region for PCI memory access for each RC port.
936 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
938 pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
939 "give up\n", controller->trio_index,
945 controller->pio_mem_index = ret;
948 * For PIO MEM, the bus_address_hi parameter is hard-coded 0
949 * because we always assign 32-bit PCI bus BAR ranges.
951 ret = gxio_trio_init_pio_region_aux(trio_context,
952 controller->pio_mem_index,
957 pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
958 "give up\n", controller->trio_index,
965 * Configure a Mem-Map region for each memory controller so
966 * that Linux can map all of its PA space to the PCI bus.
967 * Use the IOMMU to handle hash-for-home memory.
969 for_each_online_node(j) {
970 unsigned long start_pfn = node_start_pfn[j];
971 unsigned long end_pfn = node_end_pfn[j];
972 unsigned long nr_pages = end_pfn - start_pfn;
974 ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
977 pr_err("PCI: Mem-Map alloc failure on TRIO %d "
978 "mac %d for MC %d, give up\n",
979 controller->trio_index,
982 goto alloc_mem_map_failed;
985 controller->mem_maps[j] = ret;
988 * Initialize the Mem-Map and the I/O MMU so that all
989 * the physical memory can be accessed by the endpoint
990 * devices. The base bus address is set to the base CPA
991 * of this memory controller plus an offset (see pci.h).
992 * The region's base VA is set to the base CPA. The
993 * I/O MMU table essentially translates the CPA to
994 * the real PA. Implicitly, for node 0, we create
995 * a separate Mem-Map region that serves as the inbound
996 * window for legacy 32-bit devices. This is a direct
997 * map of the low 4GB CPA space.
999 ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
1000 controller->mem_maps[j],
1001 start_pfn << PAGE_SHIFT,
1002 nr_pages << PAGE_SHIFT,
1005 (start_pfn << PAGE_SHIFT) +
1006 TILE_PCI_MEM_MAP_BASE_OFFSET,
1008 GXIO_TRIO_ORDER_MODE_UNORDERED);
1010 pr_err("PCI: Mem-Map init failure on TRIO %d "
1011 "mac %d for MC %d, give up\n",
1012 controller->trio_index,
1013 controller->mac, j);
1015 goto alloc_mem_map_failed;
1019 alloc_mem_map_failed:
1027 subsys_initcall(pcibios_init);
1029 /* Note: to be deleted after Linux 3.6 merge. */
1030 void pcibios_fixup_bus(struct pci_bus *bus)
1035 * This can be called from the generic PCI layer, but doesn't need to
1038 char *pcibios_setup(char *str)
1040 if (!strcmp(str, "off")) {
1048 * Enable memory address decoding, as appropriate, for the
1049 * device described by the 'dev' struct. The I/O decoding
1050 * is disabled, though the TILE-Gx supports I/O addressing.
1052 * This is called from the generic PCI layer, and can be called
1053 * for bridges or endpoints.
1055 int pcibios_enable_device(struct pci_dev *dev, int mask)
1057 return pci_enable_resources(dev, mask);
1060 /* Called for each device after PCI setup is done. */
1061 static void pcibios_fixup_final(struct pci_dev *pdev)
1063 set_dma_ops(&pdev->dev, gx_pci_dma_map_ops);
1064 set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
1065 pdev->dev.archdata.max_direct_dma_addr =
1066 TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
1068 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
1070 /* Map a PCI MMIO bus address into VA space. */
1071 void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
1073 struct pci_controller *controller = NULL;
1074 resource_size_t bar_start;
1075 resource_size_t bar_end;
1076 resource_size_t offset;
1077 resource_size_t start;
1078 resource_size_t end;
1083 end = phys_addr + size - 1;
1086 * In the following, each PCI controller's mem_resources[1]
1087 * represents its (non-prefetchable) PCI memory resource and
1088 * mem_resources[2] refers to its prefetchable PCI memory resource.
1089 * By searching phys_addr in each controller's mem_resources[], we can
1090 * determine the controller that should accept the PCI memory access.
1093 for (i = 0; i < num_rc_controllers; i++) {
1095 * Skip controllers that are not properly initialized or
1098 if (pci_controllers[i].root_bus == NULL)
1101 for (j = 1; j < 3; j++) {
1103 pci_controllers[i].mem_resources[j].start;
1105 pci_controllers[i].mem_resources[j].end;
1107 if ((start >= bar_start) && (end <= bar_end)) {
1109 controller = &pci_controllers[i];
1116 if (controller == NULL)
1120 trio_fd = controller->trio->fd;
1122 /* Convert the resource start to the bus address offset. */
1123 start = phys_addr - controller->mem_offset;
1125 offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
1128 * We need to keep the PCI bus address's in-page offset in the VA.
1130 return iorpc_ioremap(trio_fd, offset, size) +
1131 (phys_addr & (PAGE_SIZE - 1));
1133 EXPORT_SYMBOL(ioremap);
1135 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1139 EXPORT_SYMBOL(pci_iounmap);
1141 /****************************************************************
1143 * Tile PCI config space read/write routines
1145 ****************************************************************/
1148 * These are the normal read and write ops
1149 * These are expanded with macros from pci_bus_read_config_byte() etc.
1151 * devfn is the combined PCI device & function.
1153 * offset is in bytes, from the start of config space for the
1154 * specified bus & device.
1157 static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
1160 struct pci_controller *controller = bus->sysdata;
1161 gxio_trio_context_t *trio_context = controller->trio;
1162 int busnum = bus->number & 0xff;
1163 int device = PCI_SLOT(devfn);
1164 int function = PCI_FUNC(devfn);
1165 int config_type = 1;
1166 TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
1170 * Map all accesses to the local device on root bus into the
1171 * MMIO space of the MAC. Accesses to the downstream devices
1172 * go to the PIO space.
1174 if (pci_is_root_bus(bus)) {
1177 * This is the internal downstream P2P bridge,
1180 unsigned int reg_offset;
1182 reg_offset = ((offset & 0xFFF) <<
1183 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
1184 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
1185 << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
1187 TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1189 mmio_addr = trio_context->mmio_base_mac + reg_offset;
1195 * We fake an empty device for (device > 0),
1196 * since there is only one device on bus 0.
1198 goto invalid_device;
1203 * Accesses to the directly attached device have to be
1204 * sent as type-0 configs.
1207 if (busnum == (controller->first_busno + 1)) {
1209 * There is only one device off of our built-in P2P bridge.
1212 goto invalid_device;
1218 cfg_addr.reg_addr = (offset & 0xFFF);
1219 cfg_addr.fn = function;
1220 cfg_addr.dev = device;
1221 cfg_addr.bus = busnum;
1222 cfg_addr.type = config_type;
1225 * Note that we don't set the mac field in cfg_addr because the
1226 * mapping is per port.
1229 mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1236 *val = __gxio_mmio_read32(mmio_addr);
1240 *val = __gxio_mmio_read16(mmio_addr);
1244 *val = __gxio_mmio_read8(mmio_addr);
1248 return PCIBIOS_FUNC_NOT_SUPPORTED;
1251 TRACE_CFG_RD(size, *val, busnum, device, function, offset);
1271 return PCIBIOS_FUNC_NOT_SUPPORTED;
1279 * See tile_cfg_read() for relevent comments.
1280 * Note that "val" is the value to write, not a pointer to that value.
1282 static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
1285 struct pci_controller *controller = bus->sysdata;
1286 gxio_trio_context_t *trio_context = controller->trio;
1287 int busnum = bus->number & 0xff;
1288 int device = PCI_SLOT(devfn);
1289 int function = PCI_FUNC(devfn);
1290 int config_type = 1;
1291 TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
1293 u32 val_32 = (u32)val;
1294 u16 val_16 = (u16)val;
1298 * Map all accesses to the local device on root bus into the
1299 * MMIO space of the MAC. Accesses to the downstream devices
1300 * go to the PIO space.
1302 if (pci_is_root_bus(bus)) {
1305 * This is the internal downstream P2P bridge,
1308 unsigned int reg_offset;
1310 reg_offset = ((offset & 0xFFF) <<
1311 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
1312 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
1313 << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
1315 TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1317 mmio_addr = trio_context->mmio_base_mac + reg_offset;
1323 * We fake an empty device for (device > 0),
1324 * since there is only one device on bus 0.
1326 goto invalid_device;
1331 * Accesses to the directly attached device have to be
1332 * sent as type-0 configs.
1335 if (busnum == (controller->first_busno + 1)) {
1337 * There is only one device off of our built-in P2P bridge.
1340 goto invalid_device;
1346 cfg_addr.reg_addr = (offset & 0xFFF);
1347 cfg_addr.fn = function;
1348 cfg_addr.dev = device;
1349 cfg_addr.bus = busnum;
1350 cfg_addr.type = config_type;
1353 * Note that we don't set the mac field in cfg_addr because the
1354 * mapping is per port.
1357 mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1364 __gxio_mmio_write32(mmio_addr, val_32);
1365 TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
1369 __gxio_mmio_write16(mmio_addr, val_16);
1370 TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
1374 __gxio_mmio_write8(mmio_addr, val_8);
1375 TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
1379 return PCIBIOS_FUNC_NOT_SUPPORTED;
1388 static struct pci_ops tile_cfg_ops = {
1389 .read = tile_cfg_read,
1390 .write = tile_cfg_write,
1395 * MSI support starts here.
1398 tilegx_msi_startup(struct irq_data *d)
1407 tilegx_msi_ack(struct irq_data *d)
1409 __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
1413 tilegx_msi_mask(struct irq_data *d)
1416 __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
1420 tilegx_msi_unmask(struct irq_data *d)
1422 __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
1426 static struct irq_chip tilegx_msi_chip = {
1427 .name = "tilegx_msi",
1428 .irq_startup = tilegx_msi_startup,
1429 .irq_ack = tilegx_msi_ack,
1430 .irq_mask = tilegx_msi_mask,
1431 .irq_unmask = tilegx_msi_unmask,
1433 /* TBD: support set_affinity. */
1436 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1438 struct pci_controller *controller;
1439 gxio_trio_context_t *trio_context;
1442 uint64_t mem_map_base;
1443 uint64_t mem_map_limit;
1455 * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
1456 * devices that are not capable of generating a 64-bit message address.
1457 * These devices will fall back to using the legacy interrupts.
1458 * Most PCIe endpoint devices do support 64-bit message addressing.
1460 if (desc->msi_attrib.is_64 == 0) {
1461 dev_printk(KERN_INFO, &pdev->dev,
1462 "64-bit MSI message address not supported, "
1463 "falling back to legacy interrupts.\n");
1469 default_irq = desc->msi_attrib.default_irq;
1470 controller = irq_get_handler_data(default_irq);
1472 BUG_ON(!controller);
1474 trio_context = controller->trio;
1477 * Allocate a scatter-queue that will accept the MSI write and
1478 * trigger the TILE-side interrupts. We use the scatter-queue regions
1479 * before the mem map regions, because the latter are needed by more
1482 mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
1484 TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
1489 mem_map += TRIO_NUM_MAP_MEM_REGIONS;
1490 mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
1491 mem_map * MEM_MAP_INTR_REGION_SIZE;
1492 mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
1494 msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
1495 msg.data = (unsigned int)doorbell_template.word;
1497 /* SQ regions are out, allocate from map mem regions. */
1498 mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
1500 dev_printk(KERN_INFO, &pdev->dev,
1501 "%s Mem-Map alloc failure. "
1502 "Failed to initialize MSI interrupts. "
1503 "Falling back to legacy interrupts.\n",
1504 desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
1506 goto msi_mem_map_alloc_failure;
1509 mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
1510 mem_map * MEM_MAP_INTR_REGION_SIZE;
1511 mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
1513 msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
1514 TRIO_MAP_MEM_REG_INT0;
1519 /* We try to distribute different IRQs to different tiles. */
1520 cpu = tile_irq_cpu(irq);
1523 * Now call up to the HV to configure the MSI interrupt and
1524 * set up the IPI binding.
1526 ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
1527 KERNEL_PL, irq, controller->mac,
1528 mem_map, mem_map_base, mem_map_limit,
1529 trio_context->asid);
1531 dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
1533 goto hv_msi_config_failure;
1536 irq_set_msi_desc(irq, desc);
1538 msg.address_hi = msi_addr >> 32;
1539 msg.address_lo = msi_addr & 0xffffffff;
1541 write_msi_msg(irq, &msg);
1542 irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
1543 irq_set_handler_data(irq, controller);
1547 hv_msi_config_failure:
1549 msi_mem_map_alloc_failure:
1555 void arch_teardown_msi_irq(unsigned int irq)