tile PCI RC: make default consistent DMA mask 32-bit
[cascardo/linux.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 /* The PCI I/O space size in each PCI domain. */
81 #define IO_SPACE_SIZE           0x10000
82
83 /* Provide shorter versions of some very long constant names. */
84 #define AUTO_CONFIG_RC  \
85         TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC
86 #define AUTO_CONFIG_RC_G1       \
87         TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC_G1
88 #define AUTO_CONFIG_EP  \
89         TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT
90 #define AUTO_CONFIG_EP_G1       \
91         TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT_G1
92
93 /* Array of the PCIe ports configuration info obtained from the BIB. */
94 struct pcie_trio_ports_property pcie_ports[TILEGX_NUM_TRIO];
95
96 /* Number of configured TRIO instances. */
97 int num_trio_shims;
98
99 /* All drivers share the TRIO contexts defined here. */
100 gxio_trio_context_t trio_contexts[TILEGX_NUM_TRIO];
101
102 /* Pointer to an array of PCIe RC controllers. */
103 struct pci_controller pci_controllers[TILEGX_NUM_TRIO * TILEGX_TRIO_PCIES];
104 int num_rc_controllers;
105
106 static struct pci_ops tile_cfg_ops;
107
108 /* Mask of CPUs that should receive PCIe interrupts. */
109 static struct cpumask intr_cpus_map;
110
111 /* We don't need to worry about the alignment of resources. */
112 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
113                                        resource_size_t size,
114                                        resource_size_t align)
115 {
116         return res->start;
117 }
118 EXPORT_SYMBOL(pcibios_align_resource);
119
120 /*
121  * Pick a CPU to receive and handle the PCIe interrupts, based on the IRQ #.
122  * For now, we simply send interrupts to non-dataplane CPUs.
123  * We may implement methods to allow user to specify the target CPUs,
124  * e.g. via boot arguments.
125  */
126 static int tile_irq_cpu(int irq)
127 {
128         unsigned int count;
129         int i = 0;
130         int cpu;
131
132         count = cpumask_weight(&intr_cpus_map);
133         if (unlikely(count == 0)) {
134                 pr_warning("intr_cpus_map empty, interrupts will be"
135                            " delievered to dataplane tiles\n");
136                 return irq % (smp_height * smp_width);
137         }
138
139         count = irq % count;
140         for_each_cpu(cpu, &intr_cpus_map) {
141                 if (i++ == count)
142                         break;
143         }
144         return cpu;
145 }
146
147 /* Open a file descriptor to the TRIO shim. */
148 static int tile_pcie_open(int trio_index)
149 {
150         gxio_trio_context_t *context = &trio_contexts[trio_index];
151         int ret;
152         int mac;
153
154         /* This opens a file descriptor to the TRIO shim. */
155         ret = gxio_trio_init(context, trio_index);
156         if (ret < 0)
157                 goto gxio_trio_init_failure;
158
159         /* Allocate an ASID for the kernel. */
160         ret = gxio_trio_alloc_asids(context, 1, 0, 0);
161         if (ret < 0) {
162                 pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
163                         trio_index);
164                 goto asid_alloc_failure;
165         }
166
167         context->asid = ret;
168
169 #ifdef USE_SHARED_PCIE_CONFIG_REGION
170         /*
171          * Alloc a PIO region for config access, shared by all MACs per TRIO.
172          * This shouldn't fail since the kernel is supposed to the first
173          * client of the TRIO's PIO regions.
174          */
175         ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
176         if (ret < 0) {
177                 pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
178                         trio_index);
179                 goto pio_alloc_failure;
180         }
181
182         context->pio_cfg_index = ret;
183
184         /*
185          * For PIO CFG, the bus_address_hi parameter is 0. The mac parameter
186          * is also 0 because it is specified in PIO_REGION_SETUP_CFG_ADDR.
187          */
188         ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
189                 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
190         if (ret < 0) {
191                 pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
192                         trio_index);
193                 goto pio_alloc_failure;
194         }
195 #endif
196
197         /* Get the properties of the PCIe ports on this TRIO instance. */
198         ret = gxio_trio_get_port_property(context, &pcie_ports[trio_index]);
199         if (ret < 0) {
200                 pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d,"
201                        " on TRIO %d\n", ret, trio_index);
202                 goto get_port_property_failure;
203         }
204
205         context->mmio_base_mac =
206                 iorpc_ioremap(context->fd, 0, HV_TRIO_CONFIG_IOREMAP_SIZE);
207         if (context->mmio_base_mac == NULL) {
208                 pr_err("PCI: TRIO config space mapping failure, error %d,"
209                        " on TRIO %d\n", ret, trio_index);
210                 ret = -ENOMEM;
211
212                 goto trio_mmio_mapping_failure;
213         }
214
215         /* Check the port strap state which will override the BIB setting. */
216         for (mac = 0; mac < TILEGX_TRIO_PCIES; mac++) {
217                 TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
218                 unsigned int reg_offset;
219
220                 /* Ignore ports that are not specified in the BIB. */
221                 if (!pcie_ports[trio_index].ports[mac].allow_rc &&
222                     !pcie_ports[trio_index].ports[mac].allow_ep)
223                         continue;
224
225                 reg_offset =
226                         (TRIO_PCIE_INTFC_PORT_CONFIG <<
227                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
228                         (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
229                                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
230                         (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
231
232                 port_config.word =
233                         __gxio_mmio_read(context->mmio_base_mac + reg_offset);
234
235                 if (port_config.strap_state != AUTO_CONFIG_RC &&
236                     port_config.strap_state != AUTO_CONFIG_RC_G1) {
237                         /*
238                          * If this is really intended to be an EP port, record
239                          * it so that the endpoint driver will know about it.
240                          */
241                         if (port_config.strap_state == AUTO_CONFIG_EP ||
242                             port_config.strap_state == AUTO_CONFIG_EP_G1)
243                                 pcie_ports[trio_index].ports[mac].allow_ep = 1;
244                 }
245         }
246
247         return ret;
248
249 trio_mmio_mapping_failure:
250 get_port_property_failure:
251 asid_alloc_failure:
252 #ifdef USE_SHARED_PCIE_CONFIG_REGION
253 pio_alloc_failure:
254 #endif
255         hv_dev_close(context->fd);
256 gxio_trio_init_failure:
257         context->fd = -1;
258
259         return ret;
260 }
261
262 static int __init tile_trio_init(void)
263 {
264         int i;
265
266         /* We loop over all the TRIO shims. */
267         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
268                 if (tile_pcie_open(i) < 0)
269                         continue;
270                 num_trio_shims++;
271         }
272
273         return 0;
274 }
275 postcore_initcall(tile_trio_init);
276
277 static void tilegx_legacy_irq_ack(struct irq_data *d)
278 {
279         __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
280 }
281
282 static void tilegx_legacy_irq_mask(struct irq_data *d)
283 {
284         __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
285 }
286
287 static void tilegx_legacy_irq_unmask(struct irq_data *d)
288 {
289         __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
290 }
291
292 static struct irq_chip tilegx_legacy_irq_chip = {
293         .name                   = "tilegx_legacy_irq",
294         .irq_ack                = tilegx_legacy_irq_ack,
295         .irq_mask               = tilegx_legacy_irq_mask,
296         .irq_unmask             = tilegx_legacy_irq_unmask,
297
298         /* TBD: support set_affinity. */
299 };
300
301 /*
302  * This is a wrapper function of the kernel level-trigger interrupt
303  * handler handle_level_irq() for PCI legacy interrupts. The TRIO
304  * is configured such that only INTx Assert interrupts are proxied
305  * to Linux which just calls handle_level_irq() after clearing the
306  * MAC INTx Assert status bit associated with this interrupt.
307  */
308 static void trio_handle_level_irq(unsigned int irq, struct irq_desc *desc)
309 {
310         struct pci_controller *controller = irq_desc_get_handler_data(desc);
311         gxio_trio_context_t *trio_context = controller->trio;
312         uint64_t intx = (uint64_t)irq_desc_get_chip_data(desc);
313         int mac = controller->mac;
314         unsigned int reg_offset;
315         uint64_t level_mask;
316
317         handle_level_irq(irq, desc);
318
319         /*
320          * Clear the INTx Level status, otherwise future interrupts are
321          * not sent.
322          */
323         reg_offset = (TRIO_PCIE_INTFC_MAC_INT_STS <<
324                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
325                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
326                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
327                 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
328
329         level_mask = TRIO_PCIE_INTFC_MAC_INT_STS__INT_LEVEL_MASK << intx;
330
331         __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset, level_mask);
332 }
333
334 /*
335  * Create kernel irqs and set up the handlers for the legacy interrupts.
336  * Also some minimum initialization for the MSI support.
337  */
338 static int tile_init_irqs(struct pci_controller *controller)
339 {
340         int i;
341         int j;
342         int irq;
343         int result;
344
345         cpumask_copy(&intr_cpus_map, cpu_online_mask);
346
347
348         for (i = 0; i < 4; i++) {
349                 gxio_trio_context_t *context = controller->trio;
350                 int cpu;
351
352                 /* Ask the kernel to allocate an IRQ. */
353                 irq = create_irq();
354                 if (irq < 0) {
355                         pr_err("PCI: no free irq vectors, failed for %d\n", i);
356
357                         goto free_irqs;
358                 }
359                 controller->irq_intx_table[i] = irq;
360
361                 /* Distribute the 4 IRQs to different tiles. */
362                 cpu = tile_irq_cpu(irq);
363
364                 /* Configure the TRIO intr binding for this IRQ. */
365                 result = gxio_trio_config_legacy_intr(context, cpu_x(cpu),
366                                                       cpu_y(cpu), KERNEL_PL,
367                                                       irq, controller->mac, i);
368                 if (result < 0) {
369                         pr_err("PCI: MAC intx config failed for %d\n", i);
370
371                         goto free_irqs;
372                 }
373
374                 /* Register the IRQ handler with the kernel. */
375                 irq_set_chip_and_handler(irq, &tilegx_legacy_irq_chip,
376                                         trio_handle_level_irq);
377                 irq_set_chip_data(irq, (void *)(uint64_t)i);
378                 irq_set_handler_data(irq, controller);
379         }
380
381         return 0;
382
383 free_irqs:
384         for (j = 0; j < i; j++)
385                 destroy_irq(controller->irq_intx_table[j]);
386
387         return -1;
388 }
389
390 /*
391  * Return 1 if the port is strapped to operate in RC mode.
392  */
393 static int
394 strapped_for_rc(gxio_trio_context_t *trio_context, int mac)
395 {
396         TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
397         unsigned int reg_offset;
398
399         /* Check the port configuration. */
400         reg_offset =
401                 (TRIO_PCIE_INTFC_PORT_CONFIG <<
402                         TRIO_CFG_REGION_ADDR__REG_SHIFT) |
403                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
404                         TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
405                 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
406         port_config.word =
407                 __gxio_mmio_read(trio_context->mmio_base_mac + reg_offset);
408
409         if (port_config.strap_state == AUTO_CONFIG_RC ||
410             port_config.strap_state == AUTO_CONFIG_RC_G1)
411                 return 1;
412         else
413                 return 0;
414 }
415
416 /*
417  * Find valid controllers and fill in pci_controller structs for each
418  * of them.
419  *
420  * Return the number of controllers discovered.
421  */
422 int __init tile_pci_init(void)
423 {
424         int ctl_index = 0;
425         int i, j;
426
427         if (!pci_probe) {
428                 pr_info("PCI: disabled by boot argument\n");
429                 return 0;
430         }
431
432         pr_info("PCI: Searching for controllers...\n");
433
434         if (num_trio_shims == 0 || sim_is_simulator())
435                 return 0;
436
437         /*
438          * Now determine which PCIe ports are configured to operate in RC
439          * mode. There is a differece in the port configuration capability
440          * between the Gx36 and Gx72 devices.
441          *
442          * The Gx36 has configuration capability for each of the 3 PCIe
443          * interfaces (disable, auto endpoint, auto RC, etc.).
444          * On the Gx72, you can only select one of the 3 PCIe interfaces per
445          * TRIO to train automatically. Further, the allowable training modes
446          * are reduced to four options (auto endpoint, auto RC, stream x1,
447          * stream x4).
448          *
449          * For Gx36 ports, it must be allowed to be in RC mode by the
450          * Board Information Block, and the hardware strapping pins must be
451          * set to RC mode.
452          *
453          * For Gx72 ports, the port will operate in RC mode if either of the
454          * following is true:
455          * 1. It is allowed to be in RC mode by the Board Information Block,
456          *    and the BIB doesn't allow the EP mode.
457          * 2. It is allowed to be in either the RC or the EP mode by the BIB,
458          *    and the hardware strapping pin is set to RC mode.
459          */
460         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
461                 gxio_trio_context_t *context = &trio_contexts[i];
462
463                 if (context->fd < 0)
464                         continue;
465
466                 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
467                         int is_rc = 0;
468
469                         if (pcie_ports[i].is_gx72 &&
470                             pcie_ports[i].ports[j].allow_rc) {
471                                 if (!pcie_ports[i].ports[j].allow_ep ||
472                                     strapped_for_rc(context, j))
473                                         is_rc = 1;
474                         } else if (pcie_ports[i].ports[j].allow_rc &&
475                                    strapped_for_rc(context, j)) {
476                                 is_rc = 1;
477                         }
478                         if (is_rc) {
479                                 pcie_rc[i][j] = 1;
480                                 num_rc_controllers++;
481                         }
482                 }
483         }
484
485         /* Return if no PCIe ports are configured to operate in RC mode. */
486         if (num_rc_controllers == 0)
487                 return 0;
488
489         /* Set the TRIO pointer and MAC index for each PCIe RC port. */
490         for (i = 0; i < TILEGX_NUM_TRIO; i++) {
491                 for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
492                         if (pcie_rc[i][j]) {
493                                 pci_controllers[ctl_index].trio =
494                                         &trio_contexts[i];
495                                 pci_controllers[ctl_index].mac = j;
496                                 pci_controllers[ctl_index].trio_index = i;
497                                 ctl_index++;
498                                 if (ctl_index == num_rc_controllers)
499                                         goto out;
500                         }
501                 }
502         }
503
504 out:
505         /* Configure each PCIe RC port. */
506         for (i = 0; i < num_rc_controllers; i++) {
507
508                 /* Configure the PCIe MAC to run in RC mode. */
509                 struct pci_controller *controller = &pci_controllers[i];
510
511                 controller->index = i;
512                 controller->ops = &tile_cfg_ops;
513
514                 controller->io_space.start = PCIBIOS_MIN_IO +
515                         (i * IO_SPACE_SIZE);
516                 controller->io_space.end = controller->io_space.start +
517                         IO_SPACE_SIZE - 1;
518                 BUG_ON(controller->io_space.end > IO_SPACE_LIMIT);
519                 controller->io_space.flags = IORESOURCE_IO;
520                 snprintf(controller->io_space_name,
521                          sizeof(controller->io_space_name),
522                          "PCI I/O domain %d", i);
523                 controller->io_space.name = controller->io_space_name;
524
525                 /*
526                  * The PCI memory resource is located above the PA space.
527                  * For every host bridge, the BAR window or the MMIO aperture
528                  * is in range [3GB, 4GB - 1] of a 4GB space beyond the
529                  * PA space.
530                  */
531                 controller->mem_offset = TILE_PCI_MEM_START +
532                         (i * TILE_PCI_BAR_WINDOW_TOP);
533                 controller->mem_space.start = controller->mem_offset +
534                         TILE_PCI_BAR_WINDOW_TOP - TILE_PCI_BAR_WINDOW_SIZE;
535                 controller->mem_space.end = controller->mem_offset +
536                         TILE_PCI_BAR_WINDOW_TOP - 1;
537                 controller->mem_space.flags = IORESOURCE_MEM;
538                 snprintf(controller->mem_space_name,
539                          sizeof(controller->mem_space_name),
540                          "PCI mem domain %d", i);
541                 controller->mem_space.name = controller->mem_space_name;
542         }
543
544         return num_rc_controllers;
545 }
546
547 /*
548  * (pin - 1) converts from the PCI standard's [1:4] convention to
549  * a normal [0:3] range.
550  */
551 static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
552 {
553         struct pci_controller *controller =
554                 (struct pci_controller *)dev->sysdata;
555         return controller->irq_intx_table[pin - 1];
556 }
557
558 static void fixup_read_and_payload_sizes(struct pci_controller *controller)
559 {
560         gxio_trio_context_t *trio_context = controller->trio;
561         struct pci_bus *root_bus = controller->root_bus;
562         TRIO_PCIE_RC_DEVICE_CONTROL_t dev_control;
563         TRIO_PCIE_RC_DEVICE_CAP_t rc_dev_cap;
564         unsigned int reg_offset;
565         struct pci_bus *child;
566         int mac;
567         int err;
568
569         mac = controller->mac;
570
571         /* Set our max read request size to be 4KB. */
572         reg_offset =
573                 (TRIO_PCIE_RC_DEVICE_CONTROL <<
574                         TRIO_CFG_REGION_ADDR__REG_SHIFT) |
575                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
576                         TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
577                 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
578
579         dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
580                                               reg_offset);
581         dev_control.max_read_req_sz = 5;
582         __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
583                             dev_control.word);
584
585         /*
586          * Set the max payload size supported by this Gx PCIe MAC.
587          * Though Gx PCIe supports Max Payload Size of up to 1024 bytes,
588          * experiments have shown that setting MPS to 256 yields the
589          * best performance.
590          */
591         reg_offset =
592                 (TRIO_PCIE_RC_DEVICE_CAP <<
593                         TRIO_CFG_REGION_ADDR__REG_SHIFT) |
594                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
595                         TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
596                 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
597
598         rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
599                                              reg_offset);
600         rc_dev_cap.mps_sup = 1;
601         __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
602                             rc_dev_cap.word);
603
604         /* Configure PCI Express MPS setting. */
605         list_for_each_entry(child, &root_bus->children, node) {
606                 struct pci_dev *self = child->self;
607                 if (!self)
608                         continue;
609
610                 pcie_bus_configure_settings(child, self->pcie_mpss);
611         }
612
613         /*
614          * Set the mac_config register in trio based on the MPS/MRS of the link.
615          */
616         reg_offset =
617                 (TRIO_PCIE_RC_DEVICE_CONTROL <<
618                         TRIO_CFG_REGION_ADDR__REG_SHIFT) |
619                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
620                         TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
621                 (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
622
623         dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
624                                                 reg_offset);
625
626         err = gxio_trio_set_mps_mrs(trio_context,
627                                     dev_control.max_payload_size,
628                                     dev_control.max_read_req_sz,
629                                     mac);
630         if (err < 0) {
631                 pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, "
632                         "MAC %d on TRIO %d\n",
633                         mac, controller->trio_index);
634         }
635 }
636
637 static int setup_pcie_rc_delay(char *str)
638 {
639         unsigned long delay = 0;
640         unsigned long trio_index;
641         unsigned long mac;
642
643         if (str == NULL || !isdigit(*str))
644                 return -EINVAL;
645         trio_index = simple_strtoul(str, (char **)&str, 10);
646         if (trio_index >= TILEGX_NUM_TRIO)
647                 return -EINVAL;
648
649         if (*str != ',')
650                 return -EINVAL;
651
652         str++;
653         if (!isdigit(*str))
654                 return -EINVAL;
655         mac = simple_strtoul(str, (char **)&str, 10);
656         if (mac >= TILEGX_TRIO_PCIES)
657                 return -EINVAL;
658
659         if (*str != '\0') {
660                 if (*str != ',')
661                         return -EINVAL;
662
663                 str++;
664                 if (!isdigit(*str))
665                         return -EINVAL;
666                 delay = simple_strtoul(str, (char **)&str, 10);
667         }
668
669         rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
670         return 0;
671 }
672 early_param("pcie_rc_delay", setup_pcie_rc_delay);
673
674 /* PCI initialization entry point, called by subsys_initcall. */
675 int __init pcibios_init(void)
676 {
677         resource_size_t offset;
678         LIST_HEAD(resources);
679         int next_busno;
680         int i;
681
682         tile_pci_init();
683
684         if (num_rc_controllers == 0)
685                 return 0;
686
687         /*
688          * Delay a bit in case devices aren't ready.  Some devices are
689          * known to require at least 20ms here, but we use a more
690          * conservative value.
691          */
692         msleep(250);
693
694         /* Scan all of the recorded PCI controllers.  */
695         for (next_busno = 0, i = 0; i < num_rc_controllers; i++) {
696                 struct pci_controller *controller = &pci_controllers[i];
697                 gxio_trio_context_t *trio_context = controller->trio;
698                 TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
699                 TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
700                 struct pci_bus *bus;
701                 unsigned int reg_offset;
702                 unsigned int class_code_revision;
703                 int trio_index;
704                 int mac;
705                 int ret;
706
707                 if (trio_context->fd < 0)
708                         continue;
709
710                 trio_index = controller->trio_index;
711                 mac = controller->mac;
712
713                 /*
714                  * Check for PCIe link-up status to decide if we need
715                  * to force the link to come up.
716                  */
717                 reg_offset =
718                         (TRIO_PCIE_INTFC_PORT_STATUS <<
719                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
720                         (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
721                                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
722                         (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
723
724                 port_status.word =
725                         __gxio_mmio_read(trio_context->mmio_base_mac +
726                                          reg_offset);
727                 if (!port_status.dl_up) {
728                         if (rc_delay[trio_index][mac]) {
729                                 pr_info("Delaying PCIe RC TRIO init %d sec"
730                                         " on MAC %d on TRIO %d\n",
731                                         rc_delay[trio_index][mac], mac,
732                                         trio_index);
733                                 msleep(rc_delay[trio_index][mac] * 1000);
734                         }
735                         ret = gxio_trio_force_rc_link_up(trio_context, mac);
736                         if (ret < 0)
737                                 pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
738                                         "MAC %d on TRIO %d\n", mac, trio_index);
739                 }
740
741                 pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
742                         trio_index, controller->mac);
743
744                 /* Delay the bus probe if needed. */
745                 if (rc_delay[trio_index][mac]) {
746                         pr_info("Delaying PCIe RC bus enumerating %d sec"
747                                 " on MAC %d on TRIO %d\n",
748                                 rc_delay[trio_index][mac], mac,
749                                 trio_index);
750                         msleep(rc_delay[trio_index][mac] * 1000);
751                 } else {
752                         /*
753                          * Wait a bit here because some EP devices
754                          * take longer to come up.
755                          */
756                         msleep(1000);
757                 }
758
759                 /* Check for PCIe link-up status again. */
760                 port_status.word =
761                         __gxio_mmio_read(trio_context->mmio_base_mac +
762                                          reg_offset);
763                 if (!port_status.dl_up) {
764                         if (pcie_ports[trio_index].ports[mac].removable) {
765                                 pr_info("PCI: link is down, MAC %d on TRIO %d\n",
766                                         mac, trio_index);
767                                 pr_info("This is expected if no PCIe card"
768                                         " is connected to this link\n");
769                         } else
770                                 pr_err("PCI: link is down, MAC %d on TRIO %d\n",
771                                         mac, trio_index);
772                         continue;
773                 }
774
775                 /*
776                  * Ensure that the link can come out of L1 power down state.
777                  * Strictly speaking, this is needed only in the case of
778                  * heavy RC-initiated DMAs.
779                  */
780                 reg_offset =
781                         (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
782                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
783                         (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
784                                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
785                         (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
786                 tx_fifo_ctl.word =
787                         __gxio_mmio_read(trio_context->mmio_base_mac +
788                                          reg_offset);
789                 tx_fifo_ctl.min_p_credits = 0;
790                 __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
791                                   tx_fifo_ctl.word);
792
793                 /*
794                  * Change the device ID so that Linux bus crawl doesn't confuse
795                  * the internal bridge with any Tilera endpoints.
796                  */
797                 reg_offset =
798                         (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
799                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
800                         (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
801                                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
802                         (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
803
804                 __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
805                                     (TILERA_GX36_RC_DEV_ID <<
806                                     TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
807                                     TILERA_VENDOR_ID);
808
809                 /* Set the internal P2P bridge class code. */
810                 reg_offset =
811                         (TRIO_PCIE_RC_REVISION_ID <<
812                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
813                         (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
814                                 TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
815                         (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
816
817                 class_code_revision =
818                         __gxio_mmio_read32(trio_context->mmio_base_mac +
819                                            reg_offset);
820                 class_code_revision = (class_code_revision & 0xff) |
821                         (PCI_CLASS_BRIDGE_PCI << 16);
822
823                 __gxio_mmio_write32(trio_context->mmio_base_mac +
824                                     reg_offset, class_code_revision);
825
826 #ifdef USE_SHARED_PCIE_CONFIG_REGION
827
828                 /* Map in the MMIO space for the PIO region. */
829                 offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
830                         (((unsigned long long)mac) <<
831                         TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
832
833 #else
834
835                 /* Alloc a PIO region for PCI config access per MAC. */
836                 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
837                 if (ret < 0) {
838                         pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
839                                 "on TRIO %d, give up\n", mac, trio_index);
840
841                         continue;
842                 }
843
844                 trio_context->pio_cfg_index[mac] = ret;
845
846                 /* For PIO CFG, the bus_address_hi parameter is 0. */
847                 ret = gxio_trio_init_pio_region_aux(trio_context,
848                         trio_context->pio_cfg_index[mac],
849                         mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
850                 if (ret < 0) {
851                         pr_err("PCI: PCI CFG PIO init failure for mac %d "
852                                 "on TRIO %d, give up\n", mac, trio_index);
853
854                         continue;
855                 }
856
857                 offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
858                         (((unsigned long long)mac) <<
859                         TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
860
861 #endif
862
863                 /*
864                  * To save VMALLOC space, we take advantage of the fact that
865                  * bit 29 in the PIO CFG address format is reserved 0. With
866                  * TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT being 30,
867                  * this cuts VMALLOC space usage from 1GB to 512MB per mac.
868                  */
869                 trio_context->mmio_base_pio_cfg[mac] =
870                         iorpc_ioremap(trio_context->fd, offset, (1UL <<
871                         (TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT - 1)));
872                 if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
873                         pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
874                                 mac, trio_index);
875
876                         continue;
877                 }
878
879                 /* Initialize the PCIe interrupts. */
880                 if (tile_init_irqs(controller)) {
881                         pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
882                                 mac, trio_index);
883
884                         continue;
885                 }
886
887                 /*
888                  * The PCI memory resource is located above the PA space.
889                  * The memory range for the PCI root bus should not overlap
890                  * with the physical RAM.
891                  */
892                 pci_add_resource_offset(&resources, &controller->mem_space,
893                                         controller->mem_offset);
894                 pci_add_resource(&resources, &controller->io_space);
895                 controller->first_busno = next_busno;
896                 bus = pci_scan_root_bus(NULL, next_busno, controller->ops,
897                                         controller, &resources);
898                 controller->root_bus = bus;
899                 next_busno = bus->busn_res.end + 1;
900         }
901
902         /* Do machine dependent PCI interrupt routing */
903         pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
904
905         /*
906          * This comes from the generic Linux PCI driver.
907          *
908          * It allocates all of the resources (I/O memory, etc)
909          * associated with the devices read in above.
910          */
911         pci_assign_unassigned_resources();
912
913         /* Record the I/O resources in the PCI controller structure. */
914         for (i = 0; i < num_rc_controllers; i++) {
915                 struct pci_controller *controller = &pci_controllers[i];
916                 gxio_trio_context_t *trio_context = controller->trio;
917                 struct pci_bus *root_bus = pci_controllers[i].root_bus;
918                 int ret;
919                 int j;
920
921                 /*
922                  * Skip controllers that are not properly initialized or
923                  * have down links.
924                  */
925                 if (root_bus == NULL)
926                         continue;
927
928                 /* Configure the max_payload_size values for this domain. */
929                 fixup_read_and_payload_sizes(controller);
930
931                 /* Alloc a PIO region for PCI memory access for each RC port. */
932                 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
933                 if (ret < 0) {
934                         pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
935                                "give up\n", controller->trio_index,
936                                controller->mac);
937
938                         continue;
939                 }
940
941                 controller->pio_mem_index = ret;
942
943                 /*
944                  * For PIO MEM, the bus_address_hi parameter is hard-coded 0
945                  * because we always assign 32-bit PCI bus BAR ranges.
946                  */
947                 ret = gxio_trio_init_pio_region_aux(trio_context,
948                                                     controller->pio_mem_index,
949                                                     controller->mac,
950                                                     0,
951                                                     0);
952                 if (ret < 0) {
953                         pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
954                                "give up\n", controller->trio_index,
955                                controller->mac);
956
957                         continue;
958                 }
959
960 #ifdef CONFIG_TILE_PCI_IO
961                 /*
962                  * Alloc a PIO region for PCI I/O space access for each RC port.
963                  */
964                 ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
965                 if (ret < 0) {
966                         pr_err("PCI: I/O PIO alloc failure on TRIO %d mac %d, "
967                                "give up\n", controller->trio_index,
968                                controller->mac);
969
970                         continue;
971                 }
972
973                 controller->pio_io_index = ret;
974
975                 /*
976                  * For PIO IO, the bus_address_hi parameter is hard-coded 0
977                  * because PCI I/O address space is 32-bit.
978                  */
979                 ret = gxio_trio_init_pio_region_aux(trio_context,
980                                                     controller->pio_io_index,
981                                                     controller->mac,
982                                                     0,
983                                                     HV_TRIO_PIO_FLAG_IO_SPACE);
984                 if (ret < 0) {
985                         pr_err("PCI: I/O PIO init failure on TRIO %d mac %d, "
986                                "give up\n", controller->trio_index,
987                                controller->mac);
988
989                         continue;
990                 }
991 #endif
992
993                 /*
994                  * Configure a Mem-Map region for each memory controller so
995                  * that Linux can map all of its PA space to the PCI bus.
996                  * Use the IOMMU to handle hash-for-home memory.
997                  */
998                 for_each_online_node(j) {
999                         unsigned long start_pfn = node_start_pfn[j];
1000                         unsigned long end_pfn = node_end_pfn[j];
1001                         unsigned long nr_pages = end_pfn - start_pfn;
1002
1003                         ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
1004                                                           0);
1005                         if (ret < 0) {
1006                                 pr_err("PCI: Mem-Map alloc failure on TRIO %d "
1007                                        "mac %d for MC %d, give up\n",
1008                                        controller->trio_index,
1009                                        controller->mac, j);
1010
1011                                 goto alloc_mem_map_failed;
1012                         }
1013
1014                         controller->mem_maps[j] = ret;
1015
1016                         /*
1017                          * Initialize the Mem-Map and the I/O MMU so that all
1018                          * the physical memory can be accessed by the endpoint
1019                          * devices. The base bus address is set to the base CPA
1020                          * of this memory controller plus an offset (see pci.h).
1021                          * The region's base VA is set to the base CPA. The
1022                          * I/O MMU table essentially translates the CPA to
1023                          * the real PA. Implicitly, for node 0, we create
1024                          * a separate Mem-Map region that serves as the inbound
1025                          * window for legacy 32-bit devices. This is a direct
1026                          * map of the low 4GB CPA space.
1027                          */
1028                         ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
1029                                 controller->mem_maps[j],
1030                                 start_pfn << PAGE_SHIFT,
1031                                 nr_pages << PAGE_SHIFT,
1032                                 trio_context->asid,
1033                                 controller->mac,
1034                                 (start_pfn << PAGE_SHIFT) +
1035                                 TILE_PCI_MEM_MAP_BASE_OFFSET,
1036                                 j,
1037                                 GXIO_TRIO_ORDER_MODE_UNORDERED);
1038                         if (ret < 0) {
1039                                 pr_err("PCI: Mem-Map init failure on TRIO %d "
1040                                        "mac %d for MC %d, give up\n",
1041                                        controller->trio_index,
1042                                        controller->mac, j);
1043
1044                                 goto alloc_mem_map_failed;
1045                         }
1046                         continue;
1047
1048 alloc_mem_map_failed:
1049                         break;
1050                 }
1051         }
1052
1053         return 0;
1054 }
1055 subsys_initcall(pcibios_init);
1056
1057 /* No bus fixups needed. */
1058 void pcibios_fixup_bus(struct pci_bus *bus)
1059 {
1060 }
1061
1062 /* Process any "pci=" kernel boot arguments. */
1063 char *__init pcibios_setup(char *str)
1064 {
1065         if (!strcmp(str, "off")) {
1066                 pci_probe = 0;
1067                 return NULL;
1068         }
1069         return str;
1070 }
1071
1072 /*
1073  * Enable memory address decoding, as appropriate, for the
1074  * device described by the 'dev' struct.
1075  *
1076  * This is called from the generic PCI layer, and can be called
1077  * for bridges or endpoints.
1078  */
1079 int pcibios_enable_device(struct pci_dev *dev, int mask)
1080 {
1081         return pci_enable_resources(dev, mask);
1082 }
1083
1084 /*
1085  * Called for each device after PCI setup is done.
1086  * We initialize the PCI device capabilities conservatively, assuming that
1087  * all devices can only address the 32-bit DMA space. The exception here is
1088  * that the device dma_offset is set to the value that matches the 64-bit
1089  * capable devices. This is OK because dma_offset is not used by legacy
1090  * dma_ops, nor by the hybrid dma_ops's streaming DMAs, which are 64-bit ops.
1091  * This implementation matches the kernel design of setting PCI devices'
1092  * coherent_dma_mask to 0xffffffffull by default, allowing the device drivers
1093  * to skip calling pci_set_consistent_dma_mask(DMA_BIT_MASK(32)).
1094  */
1095 static void pcibios_fixup_final(struct pci_dev *pdev)
1096 {
1097         set_dma_ops(&pdev->dev, gx_legacy_pci_dma_map_ops);
1098         set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
1099         pdev->dev.archdata.max_direct_dma_addr =
1100                 TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
1101         pdev->dev.coherent_dma_mask = TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
1102 }
1103 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
1104
1105 /* Map a PCI MMIO bus address into VA space. */
1106 void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
1107 {
1108         struct pci_controller *controller = NULL;
1109         resource_size_t bar_start;
1110         resource_size_t bar_end;
1111         resource_size_t offset;
1112         resource_size_t start;
1113         resource_size_t end;
1114         int trio_fd;
1115         int i;
1116
1117         start = phys_addr;
1118         end = phys_addr + size - 1;
1119
1120         /*
1121          * By searching phys_addr in each controller's mem_space, we can
1122          * determine the controller that should accept the PCI memory access.
1123          */
1124         for (i = 0; i < num_rc_controllers; i++) {
1125                 /*
1126                  * Skip controllers that are not properly initialized or
1127                  * have down links.
1128                  */
1129                 if (pci_controllers[i].root_bus == NULL)
1130                         continue;
1131
1132                 bar_start = pci_controllers[i].mem_space.start;
1133                 bar_end = pci_controllers[i].mem_space.end;
1134
1135                 if ((start >= bar_start) && (end <= bar_end)) {
1136                         controller = &pci_controllers[i];
1137                         break;
1138                 }
1139         }
1140
1141         if (controller == NULL)
1142                 return NULL;
1143
1144         trio_fd = controller->trio->fd;
1145
1146         /* Convert the resource start to the bus address offset. */
1147         start = phys_addr - controller->mem_offset;
1148
1149         offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
1150
1151         /* We need to keep the PCI bus address's in-page offset in the VA. */
1152         return iorpc_ioremap(trio_fd, offset, size) +
1153                 (start & (PAGE_SIZE - 1));
1154 }
1155 EXPORT_SYMBOL(ioremap);
1156
1157 #ifdef CONFIG_TILE_PCI_IO
1158 /* Map a PCI I/O address into VA space. */
1159 void __iomem *ioport_map(unsigned long port, unsigned int size)
1160 {
1161         struct pci_controller *controller = NULL;
1162         resource_size_t bar_start;
1163         resource_size_t bar_end;
1164         resource_size_t offset;
1165         resource_size_t start;
1166         resource_size_t end;
1167         int trio_fd;
1168         int i;
1169
1170         start = port;
1171         end = port + size - 1;
1172
1173         /*
1174          * By searching the port in each controller's io_space, we can
1175          * determine the controller that should accept the PCI I/O access.
1176          */
1177         for (i = 0; i < num_rc_controllers; i++) {
1178                 /*
1179                  * Skip controllers that are not properly initialized or
1180                  * have down links.
1181                  */
1182                 if (pci_controllers[i].root_bus == NULL)
1183                         continue;
1184
1185                 bar_start = pci_controllers[i].io_space.start;
1186                 bar_end = pci_controllers[i].io_space.end;
1187
1188                 if ((start >= bar_start) && (end <= bar_end)) {
1189                         controller = &pci_controllers[i];
1190                         break;
1191                 }
1192         }
1193
1194         if (controller == NULL)
1195                 return NULL;
1196
1197         trio_fd = controller->trio->fd;
1198
1199         /* Convert the resource start to the bus address offset. */
1200         port -= controller->io_space.start;
1201
1202         offset = HV_TRIO_PIO_OFFSET(controller->pio_io_index) + port;
1203
1204         /* We need to keep the PCI bus address's in-page offset in the VA. */
1205         return iorpc_ioremap(trio_fd, offset, size) + (port & (PAGE_SIZE - 1));
1206 }
1207 EXPORT_SYMBOL(ioport_map);
1208
1209 void ioport_unmap(void __iomem *addr)
1210 {
1211         iounmap(addr);
1212 }
1213 EXPORT_SYMBOL(ioport_unmap);
1214 #endif
1215
1216 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1217 {
1218         iounmap(addr);
1219 }
1220 EXPORT_SYMBOL(pci_iounmap);
1221
1222 /****************************************************************
1223  *
1224  * Tile PCI config space read/write routines
1225  *
1226  ****************************************************************/
1227
1228 /*
1229  * These are the normal read and write ops
1230  * These are expanded with macros from  pci_bus_read_config_byte() etc.
1231  *
1232  * devfn is the combined PCI device & function.
1233  *
1234  * offset is in bytes, from the start of config space for the
1235  * specified bus & device.
1236  */
1237 static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
1238                          int size, u32 *val)
1239 {
1240         struct pci_controller *controller = bus->sysdata;
1241         gxio_trio_context_t *trio_context = controller->trio;
1242         int busnum = bus->number & 0xff;
1243         int device = PCI_SLOT(devfn);
1244         int function = PCI_FUNC(devfn);
1245         int config_type = 1;
1246         TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
1247         void *mmio_addr;
1248
1249         /*
1250          * Map all accesses to the local device on root bus into the
1251          * MMIO space of the MAC. Accesses to the downstream devices
1252          * go to the PIO space.
1253          */
1254         if (pci_is_root_bus(bus)) {
1255                 if (device == 0) {
1256                         /*
1257                          * This is the internal downstream P2P bridge,
1258                          * access directly.
1259                          */
1260                         unsigned int reg_offset;
1261
1262                         reg_offset = ((offset & 0xFFF) <<
1263                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
1264                                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
1265                                 << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
1266                                 (controller->mac <<
1267                                         TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1268
1269                         mmio_addr = trio_context->mmio_base_mac + reg_offset;
1270
1271                         goto valid_device;
1272
1273                 } else {
1274                         /*
1275                          * We fake an empty device for (device > 0),
1276                          * since there is only one device on bus 0.
1277                          */
1278                         goto invalid_device;
1279                 }
1280         }
1281
1282         /*
1283          * Accesses to the directly attached device have to be
1284          * sent as type-0 configs.
1285          */
1286         if (busnum == (controller->first_busno + 1)) {
1287                 /*
1288                  * There is only one device off of our built-in P2P bridge.
1289                  */
1290                 if (device != 0)
1291                         goto invalid_device;
1292
1293                 config_type = 0;
1294         }
1295
1296         cfg_addr.word = 0;
1297         cfg_addr.reg_addr = (offset & 0xFFF);
1298         cfg_addr.fn = function;
1299         cfg_addr.dev = device;
1300         cfg_addr.bus = busnum;
1301         cfg_addr.type = config_type;
1302
1303         /*
1304          * Note that we don't set the mac field in cfg_addr because the
1305          * mapping is per port.
1306          */
1307         mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1308                 cfg_addr.word;
1309
1310 valid_device:
1311
1312         switch (size) {
1313         case 4:
1314                 *val = __gxio_mmio_read32(mmio_addr);
1315                 break;
1316
1317         case 2:
1318                 *val = __gxio_mmio_read16(mmio_addr);
1319                 break;
1320
1321         case 1:
1322                 *val = __gxio_mmio_read8(mmio_addr);
1323                 break;
1324
1325         default:
1326                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1327         }
1328
1329         TRACE_CFG_RD(size, *val, busnum, device, function, offset);
1330
1331         return 0;
1332
1333 invalid_device:
1334
1335         switch (size) {
1336         case 4:
1337                 *val = 0xFFFFFFFF;
1338                 break;
1339
1340         case 2:
1341                 *val = 0xFFFF;
1342                 break;
1343
1344         case 1:
1345                 *val = 0xFF;
1346                 break;
1347
1348         default:
1349                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1350         }
1351
1352         return 0;
1353 }
1354
1355
1356 /*
1357  * See tile_cfg_read() for relevent comments.
1358  * Note that "val" is the value to write, not a pointer to that value.
1359  */
1360 static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
1361                           int size, u32 val)
1362 {
1363         struct pci_controller *controller = bus->sysdata;
1364         gxio_trio_context_t *trio_context = controller->trio;
1365         int busnum = bus->number & 0xff;
1366         int device = PCI_SLOT(devfn);
1367         int function = PCI_FUNC(devfn);
1368         int config_type = 1;
1369         TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
1370         void *mmio_addr;
1371         u32 val_32 = (u32)val;
1372         u16 val_16 = (u16)val;
1373         u8 val_8 = (u8)val;
1374
1375         /*
1376          * Map all accesses to the local device on root bus into the
1377          * MMIO space of the MAC. Accesses to the downstream devices
1378          * go to the PIO space.
1379          */
1380         if (pci_is_root_bus(bus)) {
1381                 if (device == 0) {
1382                         /*
1383                          * This is the internal downstream P2P bridge,
1384                          * access directly.
1385                          */
1386                         unsigned int reg_offset;
1387
1388                         reg_offset = ((offset & 0xFFF) <<
1389                                 TRIO_CFG_REGION_ADDR__REG_SHIFT) |
1390                                 (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
1391                                 << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
1392                                 (controller->mac <<
1393                                         TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
1394
1395                         mmio_addr = trio_context->mmio_base_mac + reg_offset;
1396
1397                         goto valid_device;
1398
1399                 } else {
1400                         /*
1401                          * We fake an empty device for (device > 0),
1402                          * since there is only one device on bus 0.
1403                          */
1404                         goto invalid_device;
1405                 }
1406         }
1407
1408         /*
1409          * Accesses to the directly attached device have to be
1410          * sent as type-0 configs.
1411          */
1412         if (busnum == (controller->first_busno + 1)) {
1413                 /*
1414                  * There is only one device off of our built-in P2P bridge.
1415                  */
1416                 if (device != 0)
1417                         goto invalid_device;
1418
1419                 config_type = 0;
1420         }
1421
1422         cfg_addr.word = 0;
1423         cfg_addr.reg_addr = (offset & 0xFFF);
1424         cfg_addr.fn = function;
1425         cfg_addr.dev = device;
1426         cfg_addr.bus = busnum;
1427         cfg_addr.type = config_type;
1428
1429         /*
1430          * Note that we don't set the mac field in cfg_addr because the
1431          * mapping is per port.
1432          */
1433         mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
1434                         cfg_addr.word;
1435
1436 valid_device:
1437
1438         switch (size) {
1439         case 4:
1440                 __gxio_mmio_write32(mmio_addr, val_32);
1441                 TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
1442                 break;
1443
1444         case 2:
1445                 __gxio_mmio_write16(mmio_addr, val_16);
1446                 TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
1447                 break;
1448
1449         case 1:
1450                 __gxio_mmio_write8(mmio_addr, val_8);
1451                 TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
1452                 break;
1453
1454         default:
1455                 return PCIBIOS_FUNC_NOT_SUPPORTED;
1456         }
1457
1458 invalid_device:
1459
1460         return 0;
1461 }
1462
1463
1464 static struct pci_ops tile_cfg_ops = {
1465         .read =         tile_cfg_read,
1466         .write =        tile_cfg_write,
1467 };
1468
1469
1470 /* MSI support starts here. */
1471 static unsigned int tilegx_msi_startup(struct irq_data *d)
1472 {
1473         if (d->msi_desc)
1474                 unmask_msi_irq(d);
1475
1476         return 0;
1477 }
1478
1479 static void tilegx_msi_ack(struct irq_data *d)
1480 {
1481         __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
1482 }
1483
1484 static void tilegx_msi_mask(struct irq_data *d)
1485 {
1486         mask_msi_irq(d);
1487         __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
1488 }
1489
1490 static void tilegx_msi_unmask(struct irq_data *d)
1491 {
1492         __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
1493         unmask_msi_irq(d);
1494 }
1495
1496 static struct irq_chip tilegx_msi_chip = {
1497         .name                   = "tilegx_msi",
1498         .irq_startup            = tilegx_msi_startup,
1499         .irq_ack                = tilegx_msi_ack,
1500         .irq_mask               = tilegx_msi_mask,
1501         .irq_unmask             = tilegx_msi_unmask,
1502
1503         /* TBD: support set_affinity. */
1504 };
1505
1506 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1507 {
1508         struct pci_controller *controller;
1509         gxio_trio_context_t *trio_context;
1510         struct msi_msg msg;
1511         int default_irq;
1512         uint64_t mem_map_base;
1513         uint64_t mem_map_limit;
1514         u64 msi_addr;
1515         int mem_map;
1516         int cpu;
1517         int irq;
1518         int ret;
1519
1520         irq = create_irq();
1521         if (irq < 0)
1522                 return irq;
1523
1524         /*
1525          * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
1526          * devices that are not capable of generating a 64-bit message address.
1527          * These devices will fall back to using the legacy interrupts.
1528          * Most PCIe endpoint devices do support 64-bit message addressing.
1529          */
1530         if (desc->msi_attrib.is_64 == 0) {
1531                 dev_printk(KERN_INFO, &pdev->dev,
1532                         "64-bit MSI message address not supported, "
1533                         "falling back to legacy interrupts.\n");
1534
1535                 ret = -ENOMEM;
1536                 goto is_64_failure;
1537         }
1538
1539         default_irq = desc->msi_attrib.default_irq;
1540         controller = irq_get_handler_data(default_irq);
1541
1542         BUG_ON(!controller);
1543
1544         trio_context = controller->trio;
1545
1546         /*
1547          * Allocate a scatter-queue that will accept the MSI write and
1548          * trigger the TILE-side interrupts. We use the scatter-queue regions
1549          * before the mem map regions, because the latter are needed by more
1550          * applications.
1551          */
1552         mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
1553         if (mem_map >= 0) {
1554                 TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
1555                         .pop = 0,
1556                         .doorbell = 1,
1557                 }};
1558
1559                 mem_map += TRIO_NUM_MAP_MEM_REGIONS;
1560                 mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
1561                         mem_map * MEM_MAP_INTR_REGION_SIZE;
1562                 mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
1563
1564                 msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
1565                 msg.data = (unsigned int)doorbell_template.word;
1566         } else {
1567                 /* SQ regions are out, allocate from map mem regions. */
1568                 mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
1569                 if (mem_map < 0) {
1570                         dev_printk(KERN_INFO, &pdev->dev,
1571                                 "%s Mem-Map alloc failure. "
1572                                 "Failed to initialize MSI interrupts. "
1573                                 "Falling back to legacy interrupts.\n",
1574                                 desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
1575                         ret = -ENOMEM;
1576                         goto msi_mem_map_alloc_failure;
1577                 }
1578
1579                 mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
1580                         mem_map * MEM_MAP_INTR_REGION_SIZE;
1581                 mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
1582
1583                 msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
1584                         TRIO_MAP_MEM_REG_INT0;
1585
1586                 msg.data = mem_map;
1587         }
1588
1589         /* We try to distribute different IRQs to different tiles. */
1590         cpu = tile_irq_cpu(irq);
1591
1592         /*
1593          * Now call up to the HV to configure the MSI interrupt and
1594          * set up the IPI binding.
1595          */
1596         ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
1597                                         KERNEL_PL, irq, controller->mac,
1598                                         mem_map, mem_map_base, mem_map_limit,
1599                                         trio_context->asid);
1600         if (ret < 0) {
1601                 dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
1602
1603                 goto hv_msi_config_failure;
1604         }
1605
1606         irq_set_msi_desc(irq, desc);
1607
1608         msg.address_hi = msi_addr >> 32;
1609         msg.address_lo = msi_addr & 0xffffffff;
1610
1611         write_msi_msg(irq, &msg);
1612         irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
1613         irq_set_handler_data(irq, controller);
1614
1615         return 0;
1616
1617 hv_msi_config_failure:
1618         /* Free mem-map */
1619 msi_mem_map_alloc_failure:
1620 is_64_failure:
1621         destroy_irq(irq);
1622         return ret;
1623 }
1624
1625 void arch_teardown_msi_irq(unsigned int irq)
1626 {
1627         destroy_irq(irq);
1628 }