1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2003 Christoph Hellwig (hch@lst.de)
4 * Copyright (C) 1999, 2000, 04 Ralf Baechle (ralf@linux-mips.org)
5 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
7 #include <linux/kernel.h>
8 #include <linux/export.h>
10 #include <linux/smp.h>
11 #include <linux/dma-direct.h>
12 #include <linux/platform_device.h>
13 #include <linux/platform_data/xtalk-bridge.h>
15 #include <asm/pci/bridge.h>
16 #include <asm/paccess.h>
17 #include <asm/sn/irq_alloc.h>
20 * Most of the IOC3 PCI config register aren't present
21 * we emulate what is needed for a normal PCI enumeration
23 static u32
emulate_ioc3_cfg(int where
, int size
)
25 if (size
== 1 && where
== 0x3d)
27 else if (size
== 2 && where
== 0x3c)
29 else if (size
== 4 && where
== 0x3c)
35 static void bridge_disable_swapping(struct pci_dev
*dev
)
37 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(dev
->bus
);
38 int slot
= PCI_SLOT(dev
->devfn
);
40 /* Turn off byte swapping */
41 bridge_clr(bc
, b_device
[slot
].reg
, BRIDGE_DEV_SWAP_DIR
);
42 bridge_read(bc
, b_widget
.w_tflush
); /* Flush */
45 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SGI
, PCI_DEVICE_ID_SGI_IOC3
,
46 bridge_disable_swapping
);
50 * The Bridge ASIC supports both type 0 and type 1 access. Type 1 is
51 * not really documented, so right now I can't write code which uses it.
52 * Therefore we use type 0 accesses for now even though they won't work
53 * correctly for PCI-to-PCI bridges.
55 * The function is complicated by the ultimate brokenness of the IOC3 chip
56 * which is used in SGI systems. The IOC3 can only handle 32-bit PCI
57 * accesses and does only decode parts of it's address space.
59 static int pci_conf0_read_config(struct pci_bus
*bus
, unsigned int devfn
,
60 int where
, int size
, u32
*value
)
62 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(bus
);
63 struct bridge_regs
*bridge
= bc
->base
;
64 int slot
= PCI_SLOT(devfn
);
65 int fn
= PCI_FUNC(devfn
);
70 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].c
[PCI_VENDOR_ID
];
71 if (get_dbe(cf
, (u32
*)addr
))
72 return PCIBIOS_DEVICE_NOT_FOUND
;
75 * IOC3 is broken beyond belief ... Don't even give the
76 * generic PCI code a chance to look at it for real ...
78 if (cf
== (PCI_VENDOR_ID_SGI
| (PCI_DEVICE_ID_SGI_IOC3
<< 16)))
81 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].c
[where
^ (4 - size
)];
84 res
= get_dbe(*value
, (u8
*)addr
);
86 res
= get_dbe(*value
, (u16
*)addr
);
88 res
= get_dbe(*value
, (u32
*)addr
);
90 return res
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
95 * IOC3 special handling
97 if ((where
>= 0x14 && where
< 0x40) || (where
>= 0x48)) {
98 *value
= emulate_ioc3_cfg(where
, size
);
99 return PCIBIOS_SUCCESSFUL
;
102 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].l
[where
>> 2];
103 if (get_dbe(cf
, (u32
*)addr
))
104 return PCIBIOS_DEVICE_NOT_FOUND
;
106 shift
= ((where
& 3) << 3);
107 mask
= (0xffffffffU
>> ((4 - size
) << 3));
108 *value
= (cf
>> shift
) & mask
;
110 return PCIBIOS_SUCCESSFUL
;
113 static int pci_conf1_read_config(struct pci_bus
*bus
, unsigned int devfn
,
114 int where
, int size
, u32
*value
)
116 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(bus
);
117 struct bridge_regs
*bridge
= bc
->base
;
118 int busno
= bus
->number
;
119 int slot
= PCI_SLOT(devfn
);
120 int fn
= PCI_FUNC(devfn
);
125 bridge_write(bc
, b_pci_cfg
, (busno
<< 16) | (slot
<< 11));
126 addr
= &bridge
->b_type1_cfg
.c
[(fn
<< 8) | PCI_VENDOR_ID
];
127 if (get_dbe(cf
, (u32
*)addr
))
128 return PCIBIOS_DEVICE_NOT_FOUND
;
131 * IOC3 is broken beyond belief ... Don't even give the
132 * generic PCI code a chance to look at it for real ...
134 if (cf
== (PCI_VENDOR_ID_SGI
| (PCI_DEVICE_ID_SGI_IOC3
<< 16)))
137 addr
= &bridge
->b_type1_cfg
.c
[(fn
<< 8) | (where
^ (4 - size
))];
140 res
= get_dbe(*value
, (u8
*)addr
);
142 res
= get_dbe(*value
, (u16
*)addr
);
144 res
= get_dbe(*value
, (u32
*)addr
);
146 return res
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
151 * IOC3 special handling
153 if ((where
>= 0x14 && where
< 0x40) || (where
>= 0x48)) {
154 *value
= emulate_ioc3_cfg(where
, size
);
155 return PCIBIOS_SUCCESSFUL
;
158 addr
= &bridge
->b_type1_cfg
.c
[(fn
<< 8) | where
];
159 if (get_dbe(cf
, (u32
*)addr
))
160 return PCIBIOS_DEVICE_NOT_FOUND
;
162 shift
= ((where
& 3) << 3);
163 mask
= (0xffffffffU
>> ((4 - size
) << 3));
164 *value
= (cf
>> shift
) & mask
;
166 return PCIBIOS_SUCCESSFUL
;
169 static int pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
170 int where
, int size
, u32
*value
)
172 if (!pci_is_root_bus(bus
))
173 return pci_conf1_read_config(bus
, devfn
, where
, size
, value
);
175 return pci_conf0_read_config(bus
, devfn
, where
, size
, value
);
178 static int pci_conf0_write_config(struct pci_bus
*bus
, unsigned int devfn
,
179 int where
, int size
, u32 value
)
181 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(bus
);
182 struct bridge_regs
*bridge
= bc
->base
;
183 int slot
= PCI_SLOT(devfn
);
184 int fn
= PCI_FUNC(devfn
);
186 u32 cf
, shift
, mask
, smask
;
189 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].c
[PCI_VENDOR_ID
];
190 if (get_dbe(cf
, (u32
*)addr
))
191 return PCIBIOS_DEVICE_NOT_FOUND
;
194 * IOC3 is broken beyond belief ... Don't even give the
195 * generic PCI code a chance to look at it for real ...
197 if (cf
== (PCI_VENDOR_ID_SGI
| (PCI_DEVICE_ID_SGI_IOC3
<< 16)))
200 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].c
[where
^ (4 - size
)];
203 res
= put_dbe(value
, (u8
*)addr
);
205 res
= put_dbe(value
, (u16
*)addr
);
207 res
= put_dbe(value
, (u32
*)addr
);
210 return PCIBIOS_DEVICE_NOT_FOUND
;
212 return PCIBIOS_SUCCESSFUL
;
217 * IOC3 special handling
219 if ((where
>= 0x14 && where
< 0x40) || (where
>= 0x48))
220 return PCIBIOS_SUCCESSFUL
;
222 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].l
[where
>> 2];
224 if (get_dbe(cf
, (u32
*)addr
))
225 return PCIBIOS_DEVICE_NOT_FOUND
;
227 shift
= ((where
& 3) << 3);
228 mask
= (0xffffffffU
>> ((4 - size
) << 3));
229 smask
= mask
<< shift
;
231 cf
= (cf
& ~smask
) | ((value
& mask
) << shift
);
232 if (put_dbe(cf
, (u32
*)addr
))
233 return PCIBIOS_DEVICE_NOT_FOUND
;
235 return PCIBIOS_SUCCESSFUL
;
238 static int pci_conf1_write_config(struct pci_bus
*bus
, unsigned int devfn
,
239 int where
, int size
, u32 value
)
241 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(bus
);
242 struct bridge_regs
*bridge
= bc
->base
;
243 int slot
= PCI_SLOT(devfn
);
244 int fn
= PCI_FUNC(devfn
);
245 int busno
= bus
->number
;
247 u32 cf
, shift
, mask
, smask
;
250 bridge_write(bc
, b_pci_cfg
, (busno
<< 16) | (slot
<< 11));
251 addr
= &bridge
->b_type1_cfg
.c
[(fn
<< 8) | PCI_VENDOR_ID
];
252 if (get_dbe(cf
, (u32
*)addr
))
253 return PCIBIOS_DEVICE_NOT_FOUND
;
256 * IOC3 is broken beyond belief ... Don't even give the
257 * generic PCI code a chance to look at it for real ...
259 if (cf
== (PCI_VENDOR_ID_SGI
| (PCI_DEVICE_ID_SGI_IOC3
<< 16)))
262 addr
= &bridge
->b_type1_cfg
.c
[(fn
<< 8) | (where
^ (4 - size
))];
265 res
= put_dbe(value
, (u8
*)addr
);
267 res
= put_dbe(value
, (u16
*)addr
);
269 res
= put_dbe(value
, (u32
*)addr
);
272 return PCIBIOS_DEVICE_NOT_FOUND
;
274 return PCIBIOS_SUCCESSFUL
;
279 * IOC3 special handling
281 if ((where
>= 0x14 && where
< 0x40) || (where
>= 0x48))
282 return PCIBIOS_SUCCESSFUL
;
284 addr
= &bridge
->b_type0_cfg_dev
[slot
].f
[fn
].l
[where
>> 2];
285 if (get_dbe(cf
, (u32
*)addr
))
286 return PCIBIOS_DEVICE_NOT_FOUND
;
288 shift
= ((where
& 3) << 3);
289 mask
= (0xffffffffU
>> ((4 - size
) << 3));
290 smask
= mask
<< shift
;
292 cf
= (cf
& ~smask
) | ((value
& mask
) << shift
);
293 if (put_dbe(cf
, (u32
*)addr
))
294 return PCIBIOS_DEVICE_NOT_FOUND
;
296 return PCIBIOS_SUCCESSFUL
;
299 static int pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
300 int where
, int size
, u32 value
)
302 if (!pci_is_root_bus(bus
))
303 return pci_conf1_write_config(bus
, devfn
, where
, size
, value
);
305 return pci_conf0_write_config(bus
, devfn
, where
, size
, value
);
308 static struct pci_ops bridge_pci_ops
= {
309 .read
= pci_read_config
,
310 .write
= pci_write_config
,
313 struct bridge_irq_chip_data
{
314 struct bridge_controller
*bc
;
318 static int bridge_set_affinity(struct irq_data
*d
, const struct cpumask
*mask
,
322 struct bridge_irq_chip_data
*data
= d
->chip_data
;
323 int bit
= d
->parent_data
->hwirq
;
328 ret
= irq_chip_set_affinity_parent(d
, mask
, force
);
330 cpu
= cpumask_first_and(mask
, cpu_online_mask
);
331 nasid
= COMPACT_TO_NASID_NODEID(cpu_to_node(cpu
));
332 bridge_write(data
->bc
, b_int_addr
[pin
].addr
,
333 (((data
->bc
->intr_addr
>> 30) & 0x30000) |
334 bit
| (nasid
<< 8)));
335 bridge_read(data
->bc
, b_wid_tflush
);
339 return irq_chip_set_affinity_parent(d
, mask
, force
);
343 struct irq_chip bridge_irq_chip
= {
345 .irq_mask
= irq_chip_mask_parent
,
346 .irq_unmask
= irq_chip_unmask_parent
,
347 .irq_set_affinity
= bridge_set_affinity
350 static int bridge_domain_alloc(struct irq_domain
*domain
, unsigned int virq
,
351 unsigned int nr_irqs
, void *arg
)
353 struct bridge_irq_chip_data
*data
;
354 struct irq_alloc_info
*info
= arg
;
357 if (nr_irqs
> 1 || !info
)
360 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
364 ret
= irq_domain_alloc_irqs_parent(domain
, virq
, nr_irqs
, arg
);
366 data
->bc
= info
->ctrl
;
367 data
->nasid
= info
->nasid
;
368 irq_domain_set_info(domain
, virq
, info
->pin
, &bridge_irq_chip
,
369 data
, handle_level_irq
, NULL
, NULL
);
377 static void bridge_domain_free(struct irq_domain
*domain
, unsigned int virq
,
378 unsigned int nr_irqs
)
380 struct irq_data
*irqd
= irq_domain_get_irq_data(domain
, virq
);
385 kfree(irqd
->chip_data
);
386 irq_domain_free_irqs_top(domain
, virq
, nr_irqs
);
389 static int bridge_domain_activate(struct irq_domain
*domain
,
390 struct irq_data
*irqd
, bool reserve
)
392 struct bridge_irq_chip_data
*data
= irqd
->chip_data
;
393 struct bridge_controller
*bc
= data
->bc
;
394 int bit
= irqd
->parent_data
->hwirq
;
395 int pin
= irqd
->hwirq
;
398 bridge_write(bc
, b_int_addr
[pin
].addr
,
399 (((bc
->intr_addr
>> 30) & 0x30000) |
400 bit
| (data
->nasid
<< 8)));
401 bridge_set(bc
, b_int_enable
, (1 << pin
));
402 bridge_set(bc
, b_int_enable
, 0x7ffffe00); /* more stuff in int_enable */
405 * Enable sending of an interrupt clear packt to the hub on a high to
406 * low transition of the interrupt pin.
408 * IRIX sets additional bits in the address which are documented as
409 * reserved in the bridge docs.
411 bridge_set(bc
, b_int_mode
, (1UL << pin
));
414 * We assume the bridge to have a 1:1 mapping between devices
415 * (slots) and intr pins.
417 device
= bridge_read(bc
, b_int_device
);
418 device
&= ~(7 << (pin
*3));
419 device
|= (pin
<< (pin
*3));
420 bridge_write(bc
, b_int_device
, device
);
422 bridge_read(bc
, b_wid_tflush
);
426 static void bridge_domain_deactivate(struct irq_domain
*domain
,
427 struct irq_data
*irqd
)
429 struct bridge_irq_chip_data
*data
= irqd
->chip_data
;
431 bridge_clr(data
->bc
, b_int_enable
, (1 << irqd
->hwirq
));
432 bridge_read(data
->bc
, b_wid_tflush
);
435 static const struct irq_domain_ops bridge_domain_ops
= {
436 .alloc
= bridge_domain_alloc
,
437 .free
= bridge_domain_free
,
438 .activate
= bridge_domain_activate
,
439 .deactivate
= bridge_domain_deactivate
443 * All observed requests have pin == 1. We could have a global here, that
444 * gets incremented and returned every time - unfortunately, pci_map_irq
445 * may be called on the same device over and over, and need to return the
446 * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
448 * A given PCI device, in general, should be able to intr any of the cpus
449 * on any one of the hubs connected to its xbow.
451 static int bridge_map_irq(const struct pci_dev
*dev
, u8 slot
, u8 pin
)
453 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(dev
->bus
);
454 struct irq_alloc_info info
;
457 irq
= bc
->pci_int
[slot
];
460 info
.nasid
= bc
->nasid
;
463 irq
= irq_domain_alloc_irqs(bc
->domain
, 1, bc
->nasid
, &info
);
467 bc
->pci_int
[slot
] = irq
;
472 static int bridge_probe(struct platform_device
*pdev
)
474 struct xtalk_bridge_platform_data
*bd
= dev_get_platdata(&pdev
->dev
);
475 struct device
*dev
= &pdev
->dev
;
476 struct bridge_controller
*bc
;
477 struct pci_host_bridge
*host
;
478 struct irq_domain
*domain
, *parent
;
479 struct fwnode_handle
*fn
;
483 parent
= irq_get_default_host();
486 fn
= irq_domain_alloc_named_fwnode("BRIDGE");
489 domain
= irq_domain_create_hierarchy(parent
, 0, 8, fn
,
490 &bridge_domain_ops
, NULL
);
491 irq_domain_free_fwnode(fn
);
495 pci_set_flags(PCI_PROBE_ONLY
);
497 host
= devm_pci_alloc_host_bridge(dev
, sizeof(*bc
));
500 goto err_remove_domain
;
503 bc
= pci_host_bridge_priv(host
);
505 bc
->busn
.name
= "Bridge PCI busn";
508 bc
->busn
.flags
= IORESOURCE_BUS
;
512 pci_add_resource_offset(&host
->windows
, &bd
->mem
, bd
->mem_offset
);
513 pci_add_resource_offset(&host
->windows
, &bd
->io
, bd
->io_offset
);
514 pci_add_resource(&host
->windows
, &bc
->busn
);
516 err
= devm_request_pci_bus_resources(dev
, &host
->windows
);
518 goto err_free_resource
;
520 bc
->nasid
= bd
->nasid
;
522 bc
->baddr
= (u64
)bd
->masterwid
<< 60 | PCI64_ATTR_BAR
;
523 bc
->base
= (struct bridge_regs
*)bd
->bridge_addr
;
524 bc
->intr_addr
= bd
->intr_addr
;
527 * Clear all pending interrupts.
529 bridge_write(bc
, b_int_rst_stat
, BRIDGE_IRR_ALL_CLR
);
532 * Until otherwise set up, assume all interrupts are from slot 0
534 bridge_write(bc
, b_int_device
, 0x0);
537 * disable swapping for big windows
539 bridge_clr(bc
, b_wid_control
,
540 BRIDGE_CTRL_IO_SWAP
| BRIDGE_CTRL_MEM_SWAP
);
541 #ifdef CONFIG_PAGE_SIZE_4KB
542 bridge_clr(bc
, b_wid_control
, BRIDGE_CTRL_PAGE_SIZE
);
543 #else /* 16kB or larger */
544 bridge_set(bc
, b_wid_control
, BRIDGE_CTRL_PAGE_SIZE
);
548 * Hmm... IRIX sets additional bits in the address which
549 * are documented as reserved in the bridge docs.
551 bridge_write(bc
, b_wid_int_upper
,
552 ((bc
->intr_addr
>> 32) & 0xffff) | (bd
->masterwid
<< 16));
553 bridge_write(bc
, b_wid_int_lower
, bc
->intr_addr
& 0xffffffff);
554 bridge_write(bc
, b_dir_map
, (bd
->masterwid
<< 20)); /* DMA */
555 bridge_write(bc
, b_int_enable
, 0);
557 for (slot
= 0; slot
< 8; slot
++) {
558 bridge_set(bc
, b_device
[slot
].reg
, BRIDGE_DEV_SWAP_DIR
);
559 bc
->pci_int
[slot
] = -1;
561 bridge_read(bc
, b_wid_tflush
); /* wait until Bridge PIO complete */
563 host
->dev
.parent
= dev
;
566 host
->ops
= &bridge_pci_ops
;
567 host
->map_irq
= bridge_map_irq
;
568 host
->swizzle_irq
= pci_common_swizzle
;
570 err
= pci_scan_root_bus_bridge(host
);
572 goto err_free_resource
;
574 pci_bus_claim_resources(host
->bus
);
575 pci_bus_add_devices(host
->bus
);
577 platform_set_drvdata(pdev
, host
->bus
);
582 pci_free_resource_list(&host
->windows
);
584 irq_domain_remove(domain
);
588 static int bridge_remove(struct platform_device
*pdev
)
590 struct pci_bus
*bus
= platform_get_drvdata(pdev
);
591 struct bridge_controller
*bc
= BRIDGE_CONTROLLER(bus
);
593 irq_domain_remove(bc
->domain
);
594 pci_lock_rescan_remove();
595 pci_stop_root_bus(bus
);
596 pci_remove_root_bus(bus
);
597 pci_unlock_rescan_remove();
602 static struct platform_driver bridge_driver
= {
603 .probe
= bridge_probe
,
604 .remove
= bridge_remove
,
606 .name
= "xtalk-bridge",
610 builtin_platform_driver(bridge_driver
);