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