e0d666483e13a524f6e9978cdab81df9f78b8347
[firefly-linux-kernel-4.4.55.git] / arch / tile / kernel / pci_gx.c
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
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>
26 #include <linux/io.h>
27 #include <linux/uaccess.h>
28 #include <linux/ctype.h>
29
30 #include <asm/processor.h>
31 #include <asm/sections.h>
32 #include <asm/byteorder.h>
33
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>
39
40 #include <arch/sim.h>
41
42 /*
43  * This file containes the routines to search for PCI buses,
44  * enumerate the buses, and configure any attached devices.
45  */
46
47 #define DEBUG_PCI_CFG   0
48
49 #if DEBUG_PCI_CFG
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);
56 #else
57 #define TRACE_CFG_WR(...)
58 #define TRACE_CFG_RD(...)
59 #endif
60
61 static int pci_probe = 1;
62
63 /* Information on the PCIe RC ports configuration. */
64 static int pcie_rc[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
65
66 /*
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.
74  */
75 static int rc_delay[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
76
77 /* Default number of seconds that the PCIe RC port probe can be delayed. */
78 #define DEFAULT_RC_DELAY        10
79
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];
82
83 /* All drivers share the TRIO contexts defined here. */
84 gxio_trio_context_t trio_contexts[TILEGX_NUM_TRIO];
85
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;
90
91 static struct pci_ops tile_cfg_ops;
92
93 /* Mask of CPUs that should receive PCIe interrupts. */
94 static struct cpumask intr_cpus_map;
95
96 /*
97  * We don't need to worry about the alignment of resources.
98  */
99 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
100                                 resource_size_t size, resource_size_t align)
101 {
102         return res->start;
103 }
104 EXPORT_SYMBOL(pcibios_align_resource);
105
106
107 /*
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.
112  */
113 static int tile_irq_cpu(int irq)
114 {
115         unsigned int count;
116         int i = 0;
117         int cpu;
118
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);
124         }
125
126         count = irq % count;
127         for_each_cpu(cpu, &intr_cpus_map) {
128                 if (i++ == count)
129                         break;
130         }
131         return cpu;
132 }
133
134 /*
135  * Open a file descriptor to the TRIO shim.
136  */
137 static int tile_pcie_open(int trio_index)
138 {
139         gxio_trio_context_t *context = &trio_contexts[trio_index];
140         int ret;
141
142         /*
143          * This opens a file descriptor to the TRIO shim.
144          */
145         ret = gxio_trio_init(context, trio_index);
146         if (ret < 0)
147                 return ret;
148
149         /*
150          * Allocate an ASID for the kernel.
151          */
152         ret = gxio_trio_alloc_asids(context, 1, 0, 0);
153         if (ret < 0) {
154                 pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
155                         trio_index);
156                 goto asid_alloc_failure;
157         }
158
159         context->asid = ret;
160
161 #ifdef USE_SHARED_PCIE_CONFIG_REGION
162         /*
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.
166          */
167         ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
168         if (ret < 0) {
169                 pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
170                         trio_index);
171                 goto pio_alloc_failure;
172         }
173
174         context->pio_cfg_index = ret;
175
176         /*
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.
179          */
180         ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
181                 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
182         if (ret < 0) {
183                 pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
184                         trio_index);
185                 goto pio_alloc_failure;
186         }
187 #endif
188
189         return ret;
190
191 asid_alloc_failure:
192 #ifdef USE_SHARED_PCIE_CONFIG_REGION
193 pio_alloc_failure:
194 #endif
195         hv_dev_close(context->fd);
196
197         return ret;
198 }
199
200 static void
201 tilegx_legacy_irq_ack(struct irq_data *d)
202 {
203         __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
204 }
205
206 static void
207 tilegx_legacy_irq_mask(struct irq_data *d)
208 {
209         __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
210 }
211
212 static void
213 tilegx_legacy_irq_unmask(struct irq_data *d)
214 {
215         __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
216 }
217
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,
223
224         /* TBD: support set_affinity. */
225 };
226
227 /*
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.
233  */
234 static void
235 trio_handle_level_irq(unsigned int irq, struct irq_desc *desc)
236 {
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;
242         uint64_t level_mask;
243
244         handle_level_irq(irq, desc);
245
246         /*
247          * Clear the INTx Level status, otherwise future interrupts are
248          * not sent.
249          */
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);
255
256         level_mask = TRIO_PCIE_INTFC_MAC_INT_STS__INT_LEVEL_MASK << intx;
257
258         __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset, level_mask);
259 }
260
261 /*
262  * Create kernel irqs and set up the handlers for the legacy interrupts.
263  * Also some minimum initialization for the MSI support.
264  */
265 static int tile_init_irqs(struct pci_controller *controller)
266 {
267         int i;
268         int j;
269         int irq;
270         int result;
271
272         cpumask_copy(&intr_cpus_map, cpu_online_mask);
273
274
275         for (i = 0; i < 4; i++) {
276                 gxio_trio_context_t *context = controller->trio;
277                 int cpu;
278
279                 /* Ask the kernel to allocate an IRQ. */
280                 irq = create_irq();
281                 if (irq < 0) {
282                         pr_err("PCI: no free irq vectors, failed for %d\n", i);
283
284                         goto free_irqs;
285                 }
286                 controller->irq_intx_table[i] = irq;
287
288                 /* Distribute the 4 IRQs to different tiles. */
289                 cpu = tile_irq_cpu(irq);
290
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);
295                 if (result < 0) {
296                         pr_err("PCI: MAC intx config failed for %d\n", i);
297
298                         goto free_irqs;
299                 }
300
301                 /*
302                  * Register the IRQ handler with the kernel.
303                  */
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);
308         }
309
310         return 0;
311
312 free_irqs:
313         for (j = 0; j < i; j++)
314                 destroy_irq(controller->irq_intx_table[j]);
315
316         return -1;
317 }
318
319 /*
320  * Find valid controllers and fill in pci_controller structs for each
321  * of them.
322  *
323  * Returns the number of controllers discovered.
324  */
325 int __init tile_pci_init(void)
326 {
327         int num_trio_shims = 0;
328         int ctl_index = 0;
329         int i, j;
330
331         if (!pci_probe) {
332                 pr_info("PCI: disabled by boot argument\n");
333                 return 0;
334         }
335
336         pr_info("PCI: Searching for controllers...\n");
337
338         /*
339          * We loop over all the TRIO shims.
340          */
341         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
342                 int ret;
343
344                 ret = tile_pcie_open(i);
345                 if (ret < 0)
346                         continue;
347
348                 num_trio_shims++;
349         }
350
351         if (num_trio_shims == 0 || sim_is_simulator())
352                 return 0;
353
354         /*
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.
358          */
359         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
360                 gxio_trio_context_t *context = &trio_contexts[i];
361                 int ret;
362
363                 if (context->fd < 0)
364                         continue;
365
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);
370                 if (ret < 0) {
371                         pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d,"
372                                 " on TRIO %d\n", ret, i);
373                         continue;
374                 }
375
376                 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
377                         if (pcie_ports[i][j].allow_rc) {
378                                 pcie_rc[i][j] = 1;
379                                 num_rc_controllers++;
380                         }
381                         else if (pcie_ports[i][j].allow_ep) {
382                                 num_ep_controllers++;
383                         }
384                 }
385         }
386
387         /*
388          * Return if no PCIe ports are configured to operate in RC mode.
389          */
390         if (num_rc_controllers == 0)
391                 return 0;
392
393         /*
394          * Set the TRIO pointer and MAC index for each PCIe RC port.
395          */
396         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
397                 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
398                         if (pcie_rc[i][j]) {
399                                 pci_controllers[ctl_index].trio =
400                                         &trio_contexts[i];
401                                 pci_controllers[ctl_index].mac = j;
402                                 pci_controllers[ctl_index].trio_index = i;
403                                 ctl_index++;
404                                 if (ctl_index == num_rc_controllers)
405                                         goto out;
406                         }
407                 }
408         }
409
410 out:
411         /*
412          * Configure each PCIe RC port.
413          */
414         for (i = 0; i < num_rc_controllers; i++) {
415                 /*
416                  * Configure the PCIe MAC to run in RC mode.
417                  */
418
419                 struct pci_controller *controller = &pci_controllers[i];
420
421                 controller->index = i;
422                 controller->ops = &tile_cfg_ops;
423
424                 /*
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
428                  * PA space.
429                  */
430
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;
442         }
443
444         return num_rc_controllers;
445 }
446
447 /*
448  * (pin - 1) converts from the PCI standard's [1:4] convention to
449  * a normal [0:3] range.
450  */
451 static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
452 {
453         struct pci_controller *controller =
454                 (struct pci_controller *)dev->sysdata;
455         return controller->irq_intx_table[pin - 1];
456 }
457
458
459 static void fixup_read_and_payload_sizes(struct pci_controller *controller)
460 {
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;
467         int mac;
468         int err;
469
470         mac = controller->mac;
471
472         /*
473          * Set our max read request size to be 4KB.
474          */
475         reg_offset =
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);
481
482         dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
483                                                 reg_offset);
484         dev_control.max_read_req_sz = 5;
485         __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
486                                                 dev_control.word);
487
488         /*
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
492          * best performance.
493          */
494         reg_offset =
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);
500
501         rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
502                                                 reg_offset);
503         rc_dev_cap.mps_sup = 1;
504         __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
505                                                 rc_dev_cap.word);
506
507         /* Configure PCI Express MPS setting. */
508         list_for_each_entry(child, &root_bus->children, node) {
509                 struct pci_dev *self = child->self;
510                 if (!self)
511                         continue;
512
513                 pcie_bus_configure_settings(child, self->pcie_mpss);
514         }
515
516         /*
517          * Set the mac_config register in trio based on the MPS/MRS of the link.
518          */
519         reg_offset =
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);
525
526         dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
527                                                 reg_offset);
528
529         err = gxio_trio_set_mps_mrs(trio_context,
530                                     dev_control.max_payload_size,
531                                     dev_control.max_read_req_sz,
532                                     mac);
533         if (err < 0) {
534                 pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, "
535                         "MAC %d on TRIO %d\n",
536                         mac, controller->trio_index);
537         }
538 }
539
540 static int setup_pcie_rc_delay(char *str)
541 {
542         unsigned long delay = 0;
543         unsigned long trio_index;
544         unsigned long mac;
545
546         if (str == NULL || !isdigit(*str))
547                 return -EINVAL;
548         trio_index = simple_strtoul(str, (char **)&str, 10);
549         if (trio_index >= TILEGX_NUM_TRIO)
550                 return -EINVAL;
551
552         if (*str != ',')
553                 return -EINVAL;
554
555         str++;
556         if (!isdigit(*str))
557                 return -EINVAL;
558         mac = simple_strtoul(str, (char **)&str, 10);
559         if (mac >= TILEGX_TRIO_PCIES)
560                 return -EINVAL;
561
562         if (*str != '\0') {
563                 if (*str != ',')
564                         return -EINVAL;
565
566                 str++;
567                 if (!isdigit(*str))
568                         return -EINVAL;
569                 delay = simple_strtoul(str, (char **)&str, 10);
570         }
571
572         rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
573         return 0;
574 }
575 early_param("pcie_rc_delay", setup_pcie_rc_delay);
576
577 /*
578  * PCI initialization entry point, called by subsys_initcall.
579  */
580 int __init pcibios_init(void)
581 {
582         resource_size_t offset;
583         LIST_HEAD(resources);
584         int next_busno;
585         int i;
586
587         tile_pci_init();
588
589         if (num_rc_controllers == 0 && num_ep_controllers == 0)
590                 return 0;
591
592         /*
593          * We loop over all the TRIO shims and set up the MMIO mappings.
594          */
595         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
596                 gxio_trio_context_t *context = &trio_contexts[i];
597
598                 if (context->fd < 0)
599                         continue;
600
601                 /*
602                  * Map in the MMIO space for the MAC.
603                  */
604                 offset = 0;
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);
610
611                         hv_dev_close(context->fd);
612                         context->fd = -1;
613                         continue;
614                 }
615         }
616
617         /*
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.
621          */
622         msleep(250);
623
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;
631                 struct pci_bus *bus;
632                 unsigned int reg_offset;
633                 unsigned int class_code_revision;
634                 int trio_index;
635                 int mac;
636                 int ret;
637
638                 if (trio_context->fd < 0)
639                         continue;
640
641                 trio_index = controller->trio_index;
642                 mac = controller->mac;
643
644                 /*
645                  * Check the port strap state which will override the BIB
646                  * setting.
647                  */
648
649                 reg_offset =
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);
655
656                 port_config.word =
657                         __gxio_mmio_read(trio_context->mmio_base_mac +
658                                          reg_offset);
659
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)) {
664                         /*
665                          * If this is really intended to be an EP port,
666                          * record it so that the endpoint driver will know about it.
667                          */
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;
673
674                         continue;
675                 }
676
677                 /*
678                  * Check for PCIe link-up status to decide if we need
679                  * to force the link to come up.
680                  */
681                 reg_offset =
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);
687
688                 port_status.word =
689                         __gxio_mmio_read(trio_context->mmio_base_mac +
690                                          reg_offset);
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,
696                                         trio_index);
697                                 msleep(rc_delay[trio_index][mac] * 1000);
698                         }
699                         ret = gxio_trio_force_rc_link_up(trio_context, mac);
700                         if (ret < 0)
701                                 pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
702                                         "MAC %d on TRIO %d\n", mac, trio_index);
703                 }
704
705                 pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
706                         trio_index, controller->mac);
707
708                 /*
709                  * Delay the bus probe if needed.
710                  */
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,
715                                 trio_index);
716                         msleep(rc_delay[trio_index][mac] * 1000);
717                 } else {
718                         /*
719                          * Wait a bit here because some EP devices
720                          * take longer to come up.
721                          */
722                         msleep(1000);
723                 }
724
725                 /*
726                  * Check for PCIe link-up status again.
727                  */
728                 port_status.word =
729                         __gxio_mmio_read(trio_context->mmio_base_mac +
730                                          reg_offset);
731                 if (!port_status.dl_up) {
732                         pr_err("PCI: link is down, MAC %d on TRIO %d\n",
733                                 mac, trio_index);
734                         continue;
735                 }
736
737                 /*
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.
741                  */
742                 reg_offset =
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);
748                 tx_fifo_ctl.word =
749                         __gxio_mmio_read(trio_context->mmio_base_mac +
750                                          reg_offset);
751                 tx_fifo_ctl.min_p_credits = 0;
752                 __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
753                                   tx_fifo_ctl.word);
754
755                 /*
756                  * Change the device ID so that Linux bus crawl doesn't confuse
757                  * the internal bridge with any Tilera endpoints.
758                  */
759
760                 reg_offset =
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);
766
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) |
770                                     TILERA_VENDOR_ID);
771
772                 /*
773                  * Set the internal P2P bridge class code.
774                  */
775
776                 reg_offset =
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);
782
783                 class_code_revision =
784                         __gxio_mmio_read32(trio_context->mmio_base_mac +
785                                            reg_offset);
786                 class_code_revision = (class_code_revision & 0xff ) |
787                                         (PCI_CLASS_BRIDGE_PCI << 16);
788
789                 __gxio_mmio_write32(trio_context->mmio_base_mac +
790                                     reg_offset, class_code_revision);
791
792 #ifdef USE_SHARED_PCIE_CONFIG_REGION
793
794                 /*
795                  * Map in the MMIO space for the PIO region.
796                  */
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);
800
801 #else
802
803                 /*
804                  * Alloc a PIO region for PCI config access per MAC.
805                  */
806                 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
807                 if (ret < 0) {
808                         pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
809                                 "on TRIO %d, give up\n", mac, trio_index);
810
811                         continue;
812                 }
813
814                 trio_context->pio_cfg_index[mac] = ret;
815
816                 /*
817                  * For PIO CFG, the bus_address_hi parameter is 0.
818                  */
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);
822                 if (ret < 0) {
823                         pr_err("PCI: PCI CFG PIO init failure for mac %d "
824                                 "on TRIO %d, give up\n", mac, trio_index);
825
826                         continue;
827                 }
828
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);
832
833 #endif
834
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",
840                                 mac, trio_index);
841
842                         continue;
843                 }
844
845                 /*
846                  * Initialize the PCIe interrupts.
847                  */
848                 if (tile_init_irqs(controller)) {
849                         pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
850                                 mac, trio_index);
851
852                         continue;
853                 }
854
855                 /*
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
859                  */
860                 pci_add_resource_offset(&resources, &controller->mem_space,
861                                         controller->mem_offset);
862
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;
868
869         }
870
871         /* Do machine dependent PCI interrupt routing */
872         pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
873
874         /*
875          * This comes from the generic Linux PCI driver.
876          *
877          * It allocates all of the resources (I/O memory, etc)
878          * associated with the devices read in above.
879          */
880
881         pci_assign_unassigned_resources();
882
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;
890                 struct pci_dev *dev;
891                 int ret;
892                 int j;
893
894                 /*
895                  * Skip controllers that are not properly initialized or
896                  * have down links.
897                  */
898                 if (root_bus == NULL)
899                         continue;
900
901                 /* Configure the max_payload_size values for this domain. */
902                 fixup_read_and_payload_sizes(controller);
903
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];
915
916                                 break;
917                         }
918                 }
919
920                 if (pci_controllers[i].mem_resources[1].flags & IORESOURCE_MEM)
921                         bus_address_hi =
922                                 pci_controllers[i].mem_resources[1].start >> 32;
923                 else if (pci_controllers[i].mem_resources[2].flags & IORESOURCE_PREFETCH)
924                         bus_address_hi =
925                                 pci_controllers[i].mem_resources[2].start >> 32;
926                 else {
927                         /* This is unlikely. */
928                         pr_err("PCI: no memory resources on TRIO %d mac %d\n",
929                                 controller->trio_index, controller->mac);
930                         continue;
931                 }
932
933                 /*
934                  * Alloc a PIO region for PCI memory access for each RC port.
935                  */
936                 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
937                 if (ret < 0) {
938                         pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
939                                 "give up\n", controller->trio_index,
940                                 controller->mac);
941
942                         continue;
943                 }
944
945                 controller->pio_mem_index = ret;
946
947                 /*
948                  * For PIO MEM, the bus_address_hi parameter is hard-coded 0
949                  * because we always assign 32-bit PCI bus BAR ranges.
950                  */
951                 ret = gxio_trio_init_pio_region_aux(trio_context,
952                                                     controller->pio_mem_index,
953                                                     controller->mac,
954                                                     0,
955                                                     0);
956                 if (ret < 0) {
957                         pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
958                                 "give up\n", controller->trio_index,
959                                 controller->mac);
960
961                         continue;
962                 }
963
964                 /*
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.
968                  */
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;
973
974                         ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
975                                                           0);
976                         if (ret < 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,
980                                         controller->mac, j);
981
982                                 goto alloc_mem_map_failed;
983                         }
984
985                         controller->mem_maps[j] = ret;
986
987                         /*
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.
998                          */
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,
1003                                 trio_context->asid,
1004                                 controller->mac,
1005                                 (start_pfn << PAGE_SHIFT) +
1006                                 TILE_PCI_MEM_MAP_BASE_OFFSET,
1007                                 j,
1008                                 GXIO_TRIO_ORDER_MODE_UNORDERED);
1009                         if (ret < 0) {
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);
1014
1015                                 goto alloc_mem_map_failed;
1016                         }
1017                         continue;
1018
1019 alloc_mem_map_failed:
1020                         break;
1021                 }
1022
1023         }
1024
1025         return 0;
1026 }
1027 subsys_initcall(pcibios_init);
1028
1029 /* Note: to be deleted after Linux 3.6 merge. */
1030 void pcibios_fixup_bus(struct pci_bus *bus)
1031 {
1032 }
1033
1034 /*
1035  * This can be called from the generic PCI layer, but doesn't need to
1036  * do anything.
1037  */
1038 char *pcibios_setup(char *str)
1039 {
1040         if (!strcmp(str, "off")) {
1041                 pci_probe = 0;
1042                 return NULL;
1043         }
1044         return str;
1045 }
1046
1047 /*
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.
1051  *
1052  * This is called from the generic PCI layer, and can be called
1053  * for bridges or endpoints.
1054  */
1055 int pcibios_enable_device(struct pci_dev *dev, int mask)
1056 {
1057         return pci_enable_resources(dev, mask);
1058 }
1059
1060 /* Called for each device after PCI setup is done. */
1061 static void pcibios_fixup_final(struct pci_dev *pdev)
1062 {
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;
1067 }
1068 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
1069
1070 /* Map a PCI MMIO bus address into VA space. */
1071 void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
1072 {
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;
1079         int trio_fd;
1080         int i, j;
1081
1082         start = phys_addr;
1083         end = phys_addr + size - 1;
1084
1085         /*
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.
1091          */
1092
1093         for (i = 0; i < num_rc_controllers; i++) {
1094                 /*
1095                  * Skip controllers that are not properly initialized or
1096                  * have down links.
1097                  */
1098                 if (pci_controllers[i].root_bus == NULL)
1099                         continue;
1100
1101                 for (j = 1; j < 3; j++) {
1102                         bar_start =
1103                                 pci_controllers[i].mem_resources[j].start;
1104                         bar_end =
1105                                 pci_controllers[i].mem_resources[j].end;
1106
1107                         if ((start >= bar_start) && (end <= bar_end)) {
1108
1109                                 controller = &pci_controllers[i];
1110
1111                                 goto got_it;
1112                         }
1113                 }
1114         }
1115
1116         if (controller == NULL)
1117                 return NULL;
1118
1119 got_it:
1120         trio_fd = controller->trio->fd;
1121
1122         /* Convert the resource start to the bus address offset. */
1123         start = phys_addr - controller->mem_offset;
1124
1125         offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
1126
1127         /*
1128          * We need to keep the PCI bus address's in-page offset in the VA.
1129          */
1130         return iorpc_ioremap(trio_fd, offset, size) +
1131                 (phys_addr & (PAGE_SIZE - 1));
1132 }
1133 EXPORT_SYMBOL(ioremap);
1134
1135 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1136 {
1137         iounmap(addr);
1138 }
1139 EXPORT_SYMBOL(pci_iounmap);
1140
1141 /****************************************************************
1142  *
1143  * Tile PCI config space read/write routines
1144  *
1145  ****************************************************************/
1146
1147 /*
1148  * These are the normal read and write ops
1149  * These are expanded with macros from  pci_bus_read_config_byte() etc.
1150  *
1151  * devfn is the combined PCI device & function.
1152  *
1153  * offset is in bytes, from the start of config space for the
1154  * specified bus & device.
1155  */
1156
1157 static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
1158                          int size, u32 *val)
1159 {
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;
1167         void *mmio_addr;
1168
1169         /*
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.
1173          */
1174         if (pci_is_root_bus(bus)) {
1175                 if (device == 0) {
1176                         /*
1177                          * This is the internal downstream P2P bridge,
1178                          * access directly.
1179                          */
1180                         unsigned int reg_offset;
1181
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 ) |
1186                                 (controller->mac <<
1187                                         TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1188
1189                         mmio_addr = trio_context->mmio_base_mac + reg_offset;
1190
1191                         goto valid_device;
1192
1193                 } else {
1194                         /*
1195                          * We fake an empty device for (device > 0),
1196                          * since there is only one device on bus 0.
1197                          */
1198                         goto invalid_device;
1199                 }
1200         }
1201
1202         /*
1203          * Accesses to the directly attached device have to be
1204          * sent as type-0 configs.
1205          */
1206
1207         if (busnum == (controller->first_busno + 1)) {
1208                 /*
1209                  * There is only one device off of our built-in P2P bridge.
1210                  */
1211                 if (device != 0)
1212                         goto invalid_device;
1213
1214                 config_type = 0;
1215         }
1216
1217         cfg_addr.word = 0;
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;
1223
1224         /*
1225          * Note that we don't set the mac field in cfg_addr because the
1226          * mapping is per port.
1227          */
1228
1229         mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1230                         cfg_addr.word;
1231
1232 valid_device:
1233
1234         switch (size) {
1235         case 4:
1236                 *val = __gxio_mmio_read32(mmio_addr);
1237                 break;
1238
1239         case 2:
1240                 *val = __gxio_mmio_read16(mmio_addr);
1241                 break;
1242
1243         case 1:
1244                 *val = __gxio_mmio_read8(mmio_addr);
1245                 break;
1246
1247         default:
1248                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1249         }
1250
1251         TRACE_CFG_RD(size, *val, busnum, device, function, offset);
1252
1253         return 0;
1254
1255 invalid_device:
1256
1257         switch (size) {
1258         case 4:
1259                 *val = 0xFFFFFFFF;
1260                 break;
1261
1262         case 2:
1263                 *val = 0xFFFF;
1264                 break;
1265
1266         case 1:
1267                 *val = 0xFF;
1268                 break;
1269
1270         default:
1271                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1272         }
1273
1274         return 0;
1275 }
1276
1277
1278 /*
1279  * See tile_cfg_read() for relevent comments.
1280  * Note that "val" is the value to write, not a pointer to that value.
1281  */
1282 static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
1283                           int size, u32 val)
1284 {
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;
1292         void *mmio_addr;
1293         u32 val_32 = (u32)val;
1294         u16 val_16 = (u16)val;
1295         u8 val_8 = (u8)val;
1296
1297         /*
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.
1301          */
1302         if (pci_is_root_bus(bus)) {
1303                 if (device == 0) {
1304                         /*
1305                          * This is the internal downstream P2P bridge,
1306                          * access directly.
1307                          */
1308                         unsigned int reg_offset;
1309
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 ) |
1314                                 (controller->mac <<
1315                                         TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1316
1317                         mmio_addr = trio_context->mmio_base_mac + reg_offset;
1318
1319                         goto valid_device;
1320
1321                 } else {
1322                         /*
1323                          * We fake an empty device for (device > 0),
1324                          * since there is only one device on bus 0.
1325                          */
1326                         goto invalid_device;
1327                 }
1328         }
1329
1330         /*
1331          * Accesses to the directly attached device have to be
1332          * sent as type-0 configs.
1333          */
1334
1335         if (busnum == (controller->first_busno + 1)) {
1336                 /*
1337                  * There is only one device off of our built-in P2P bridge.
1338                  */
1339                 if (device != 0)
1340                         goto invalid_device;
1341
1342                 config_type = 0;
1343         }
1344
1345         cfg_addr.word = 0;
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;
1351
1352         /*
1353          * Note that we don't set the mac field in cfg_addr because the
1354          * mapping is per port.
1355          */
1356
1357         mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1358                         cfg_addr.word;
1359
1360 valid_device:
1361
1362         switch (size) {
1363         case 4:
1364                 __gxio_mmio_write32(mmio_addr, val_32);
1365                 TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
1366                 break;
1367
1368         case 2:
1369                 __gxio_mmio_write16(mmio_addr, val_16);
1370                 TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
1371                 break;
1372
1373         case 1:
1374                 __gxio_mmio_write8(mmio_addr, val_8);
1375                 TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
1376                 break;
1377
1378         default:
1379                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1380         }
1381
1382 invalid_device:
1383
1384         return 0;
1385 }
1386
1387
1388 static struct pci_ops tile_cfg_ops = {
1389         .read =         tile_cfg_read,
1390         .write =        tile_cfg_write,
1391 };
1392
1393
1394 /*
1395  * MSI support starts here.
1396  */
1397 static unsigned int
1398 tilegx_msi_startup(struct irq_data *d)
1399 {
1400         if (d->msi_desc)
1401                 unmask_msi_irq(d);
1402
1403         return 0;
1404 }
1405
1406 static void
1407 tilegx_msi_ack(struct irq_data *d)
1408 {
1409         __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
1410 }
1411
1412 static void
1413 tilegx_msi_mask(struct irq_data *d)
1414 {
1415         mask_msi_irq(d);
1416         __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
1417 }
1418
1419 static void
1420 tilegx_msi_unmask(struct irq_data *d)
1421 {
1422         __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
1423         unmask_msi_irq(d);
1424 }
1425
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,
1432
1433         /* TBD: support set_affinity. */
1434 };
1435
1436 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1437 {
1438         struct pci_controller *controller;
1439         gxio_trio_context_t *trio_context;
1440         struct msi_msg msg;
1441         int default_irq;
1442         uint64_t mem_map_base;
1443         uint64_t mem_map_limit;
1444         u64 msi_addr;
1445         int mem_map;
1446         int cpu;
1447         int irq;
1448         int ret;
1449
1450         irq = create_irq();
1451         if (irq < 0)
1452                 return irq;
1453
1454         /*
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.
1459          */
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");
1464
1465                 ret = -ENOMEM;
1466                 goto is_64_failure;
1467         }
1468
1469         default_irq = desc->msi_attrib.default_irq;
1470         controller = irq_get_handler_data(default_irq);
1471
1472         BUG_ON(!controller);
1473
1474         trio_context = controller->trio;
1475
1476         /*
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
1480          * applications.
1481          */
1482         mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
1483         if (mem_map >= 0) {
1484                 TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
1485                         .pop = 0,
1486                         .doorbell = 1,
1487                 }};
1488
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;
1493
1494                 msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
1495                 msg.data = (unsigned int)doorbell_template.word;
1496         } else {
1497                 /* SQ regions are out, allocate from map mem regions. */
1498                 mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
1499                 if (mem_map < 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");
1505                         ret = -ENOMEM;
1506                         goto msi_mem_map_alloc_failure;
1507                 }
1508
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;
1512
1513                 msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
1514                         TRIO_MAP_MEM_REG_INT0;
1515
1516                 msg.data = mem_map;
1517         }
1518
1519         /* We try to distribute different IRQs to different tiles. */
1520         cpu = tile_irq_cpu(irq);
1521
1522         /*
1523          * Now call up to the HV to configure the MSI interrupt and
1524          * set up the IPI binding.
1525          */
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);
1530         if (ret < 0) {
1531                 dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
1532
1533                 goto hv_msi_config_failure;
1534         }
1535
1536         irq_set_msi_desc(irq, desc);
1537
1538         msg.address_hi = msi_addr >> 32;
1539         msg.address_lo = msi_addr & 0xffffffff;
1540
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);
1544
1545         return 0;
1546
1547 hv_msi_config_failure:
1548         /* Free mem-map */
1549 msi_mem_map_alloc_failure:
1550 is_64_failure:
1551         destroy_irq(irq);
1552         return ret;
1553 }
1554
1555 void arch_teardown_msi_irq(unsigned int irq)
1556 {
1557         destroy_irq(irq);
1558 }