1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/alpha/kernel/pci.c
5 * Extruded from code written by
6 * Dave Rusling (david.rusling@reo.mts.dec.com)
7 * David Mosberger (davidm@cs.arizona.edu)
10 /* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */
13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14 * PCI-PCI bridges cleanup
16 #include <linux/string.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/bootmem.h>
22 #include <linux/module.h>
23 #include <linux/cache.h>
24 #include <linux/slab.h>
25 #include <asm/machvec.h>
32 * Some string constants used by the various core logics.
35 const char *const pci_io_names
[] = {
36 "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
37 "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
40 const char *const pci_mem_names
[] = {
41 "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
42 "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
45 const char pci_hae0_name
[] = "HAE0";
48 * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
53 * The PCI controller list.
56 struct pci_controller
*hose_head
, **hose_tail
= &hose_head
;
57 struct pci_controller
*pci_isa_hose
;
63 static void quirk_isa_bridge(struct pci_dev
*dev
)
65 dev
->class = PCI_CLASS_BRIDGE_ISA
<< 8;
67 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82378
, quirk_isa_bridge
);
69 static void quirk_cypress(struct pci_dev
*dev
)
71 /* The Notorious Cy82C693 chip. */
73 /* The generic legacy mode IDE fixup in drivers/pci/probe.c
74 doesn't work correctly with the Cypress IDE controller as
75 it has non-standard register layout. Fix that. */
76 if (dev
->class >> 8 == PCI_CLASS_STORAGE_IDE
) {
77 dev
->resource
[2].start
= dev
->resource
[3].start
= 0;
78 dev
->resource
[2].end
= dev
->resource
[3].end
= 0;
79 dev
->resource
[2].flags
= dev
->resource
[3].flags
= 0;
80 if (PCI_FUNC(dev
->devfn
) == 2) {
81 dev
->resource
[0].start
= 0x170;
82 dev
->resource
[0].end
= 0x177;
83 dev
->resource
[1].start
= 0x376;
84 dev
->resource
[1].end
= 0x376;
88 /* The Cypress bridge responds on the PCI bus in the address range
89 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
90 way to turn this off. The bridge also supports several extended
91 BIOS ranges (disabled after power-up), and some consoles do turn
92 them on. So if we use a large direct-map window, or a large SG
93 window, we must avoid the entire 0xfff00000-0xffffffff region. */
94 if (dev
->class >> 8 == PCI_CLASS_BRIDGE_ISA
) {
95 if (__direct_map_base
+ __direct_map_size
>= 0xfff00000UL
)
96 __direct_map_size
= 0xfff00000UL
- __direct_map_base
;
98 struct pci_controller
*hose
= dev
->sysdata
;
99 struct pci_iommu_arena
*pci
= hose
->sg_pci
;
100 if (pci
&& pci
->dma_base
+ pci
->size
>= 0xfff00000UL
)
101 pci
->size
= 0xfff00000UL
- pci
->dma_base
;
105 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ
, PCI_DEVICE_ID_CONTAQ_82C693
, quirk_cypress
);
107 /* Called for each device after PCI setup is done. */
108 static void pcibios_fixup_final(struct pci_dev
*dev
)
110 unsigned int class = dev
->class >> 8;
112 if (class == PCI_CLASS_BRIDGE_ISA
|| class == PCI_CLASS_BRIDGE_EISA
) {
113 dev
->dma_mask
= MAX_ISA_DMA_ADDRESS
- 1;
117 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID
, PCI_ANY_ID
, pcibios_fixup_final
);
119 /* Just declaring that the power-of-ten prefixes are actually the
120 power-of-two ones doesn't make it true :) */
126 pcibios_align_resource(void *data
, const struct resource
*res
,
127 resource_size_t size
, resource_size_t align
)
129 struct pci_dev
*dev
= data
;
130 struct pci_controller
*hose
= dev
->sysdata
;
131 unsigned long alignto
;
132 resource_size_t start
= res
->start
;
134 if (res
->flags
& IORESOURCE_IO
) {
135 /* Make sure we start at our min on all hoses */
136 if (start
- hose
->io_space
->start
< PCIBIOS_MIN_IO
)
137 start
= PCIBIOS_MIN_IO
+ hose
->io_space
->start
;
140 * Put everything into 0x00-0xff region modulo 0x400
143 start
= (start
+ 0x3ff) & ~0x3ff;
145 else if (res
->flags
& IORESOURCE_MEM
) {
146 /* Make sure we start at our min on all hoses */
147 if (start
- hose
->mem_space
->start
< PCIBIOS_MIN_MEM
)
148 start
= PCIBIOS_MIN_MEM
+ hose
->mem_space
->start
;
151 * The following holds at least for the Low Cost
152 * Alpha implementation of the PCI interface:
154 * In sparse memory address space, the first
155 * octant (16MB) of every 128MB segment is
156 * aliased to the very first 16 MB of the
157 * address space (i.e., it aliases the ISA
158 * memory address space). Thus, we try to
159 * avoid allocating PCI devices in that range.
160 * Can be allocated in 2nd-7th octant only.
161 * Devices that need more than 112MB of
162 * address space must be accessed through
163 * dense memory space only!
166 /* Align to multiple of size of minimum base. */
167 alignto
= max_t(resource_size_t
, 0x1000, align
);
168 start
= ALIGN(start
, alignto
);
169 if (hose
->sparse_mem_base
&& size
<= 7 * 16*MB
) {
170 if (((start
/ (16*MB
)) & 0x7) == 0) {
171 start
&= ~(128*MB
- 1);
173 start
= ALIGN(start
, alignto
);
175 if (start
/(128*MB
) != (start
+ size
- 1)/(128*MB
)) {
176 start
&= ~(128*MB
- 1);
177 start
+= (128 + 16)*MB
;
178 start
= ALIGN(start
, alignto
);
192 if (alpha_mv
.init_pci
)
197 subsys_initcall(pcibios_init
);
199 #ifdef ALPHA_RESTORE_SRM_SETUP
200 /* Store PCI device configuration left by SRM here. */
201 struct pdev_srm_saved_conf
203 struct pdev_srm_saved_conf
*next
;
207 static struct pdev_srm_saved_conf
*srm_saved_configs
;
209 static void pdev_save_srm_config(struct pci_dev
*dev
)
211 struct pdev_srm_saved_conf
*tmp
;
212 static int printed
= 0;
214 if (!alpha_using_srm
|| pci_has_flag(PCI_PROBE_ONLY
))
218 printk(KERN_INFO
"pci: enabling save/restore of SRM state\n");
222 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
224 printk(KERN_ERR
"%s: kmalloc() failed!\n", __func__
);
227 tmp
->next
= srm_saved_configs
;
232 srm_saved_configs
= tmp
;
236 pci_restore_srm_config(void)
238 struct pdev_srm_saved_conf
*tmp
;
240 /* No need to restore if probed only. */
241 if (pci_has_flag(PCI_PROBE_ONLY
))
244 /* Restore SRM config. */
245 for (tmp
= srm_saved_configs
; tmp
; tmp
= tmp
->next
) {
246 pci_restore_state(tmp
->dev
);
250 #define pdev_save_srm_config(dev) do {} while (0)
253 void pcibios_fixup_bus(struct pci_bus
*bus
)
255 struct pci_dev
*dev
= bus
->self
;
257 if (pci_has_flag(PCI_PROBE_ONLY
) && dev
&&
258 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
259 pci_read_bridge_bases(bus
);
262 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
263 pdev_save_srm_config(dev
);
268 * If we set up a device for bus mastering, we need to check the latency
269 * timer as certain firmware forgets to set it properly, as seen
270 * on SX164 and LX164 with SRM.
273 pcibios_set_master(struct pci_dev
*dev
)
276 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
277 if (lat
>= 16) return;
278 printk("PCI: Setting latency timer of device %s to 64\n",
280 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 64);
284 pcibios_claim_one_bus(struct pci_bus
*b
)
287 struct pci_bus
*child_bus
;
289 list_for_each_entry(dev
, &b
->devices
, bus_list
) {
292 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
293 struct resource
*r
= &dev
->resource
[i
];
295 if (r
->parent
|| !r
->start
|| !r
->flags
)
297 if (pci_has_flag(PCI_PROBE_ONLY
) ||
298 (r
->flags
& IORESOURCE_PCI_FIXED
)) {
299 if (pci_claim_resource(dev
, i
) == 0)
302 pci_claim_bridge_resource(dev
, i
);
307 list_for_each_entry(child_bus
, &b
->children
, node
)
308 pcibios_claim_one_bus(child_bus
);
312 pcibios_claim_console_setup(void)
316 list_for_each_entry(b
, &pci_root_buses
, node
)
317 pcibios_claim_one_bus(b
);
321 common_init_pci(void)
323 struct pci_controller
*hose
;
324 struct list_head resources
;
325 struct pci_host_bridge
*bridge
;
328 int need_domain_info
= 0;
333 /* Scan all of the recorded PCI controllers. */
334 for (next_busno
= 0, hose
= hose_head
; hose
; hose
= hose
->next
) {
335 sg_base
= hose
->sg_pci
? hose
->sg_pci
->dma_base
: ~0;
337 /* Adjust hose mem_space limit to prevent PCI allocations
338 in the iommu windows. */
339 pci_mem_end
= min((u32
)__direct_map_base
, sg_base
) - 1;
340 end
= hose
->mem_space
->start
+ pci_mem_end
;
341 if (hose
->mem_space
->end
> end
)
342 hose
->mem_space
->end
= end
;
344 INIT_LIST_HEAD(&resources
);
345 pci_add_resource_offset(&resources
, hose
->io_space
,
346 hose
->io_space
->start
);
347 pci_add_resource_offset(&resources
, hose
->mem_space
,
348 hose
->mem_space
->start
);
350 bridge
= pci_alloc_host_bridge(0);
354 list_splice_init(&resources
, &bridge
->windows
);
355 bridge
->dev
.parent
= NULL
;
356 bridge
->sysdata
= hose
;
357 bridge
->busnr
= next_busno
;
358 bridge
->ops
= alpha_mv
.pci_ops
;
359 bridge
->swizzle_irq
= alpha_mv
.pci_swizzle
;
360 bridge
->map_irq
= alpha_mv
.pci_map_irq
;
362 ret
= pci_scan_root_bus_bridge(bridge
);
364 pci_free_host_bridge(bridge
);
368 bus
= hose
->bus
= bridge
->bus
;
369 hose
->need_domain_info
= need_domain_info
;
370 next_busno
= bus
->busn_res
.end
+ 1;
371 /* Don't allow 8-bit bus number overflow inside the hose -
372 reserve some space for bridges. */
373 if (next_busno
> 224) {
375 need_domain_info
= 1;
379 pcibios_claim_console_setup();
381 pci_assign_unassigned_resources();
382 for (hose
= hose_head
; hose
; hose
= hose
->next
) {
385 pci_bus_add_devices(bus
);
389 struct pci_controller
* __init
390 alloc_pci_controller(void)
392 struct pci_controller
*hose
;
394 hose
= alloc_bootmem(sizeof(*hose
));
397 hose_tail
= &hose
->next
;
402 struct resource
* __init
405 return alloc_bootmem(sizeof(struct resource
));
409 /* Provide information on locations of various I/O regions in physical
410 memory. Do this on a per-card basis so that we choose the right hose. */
413 sys_pciconfig_iobase(long which
, unsigned long bus
, unsigned long dfn
)
415 struct pci_controller
*hose
;
418 /* from hose or from bus.devfn */
419 if (which
& IOBASE_FROM_HOSE
) {
420 for(hose
= hose_head
; hose
; hose
= hose
->next
)
421 if (hose
->index
== bus
) break;
422 if (!hose
) return -ENODEV
;
424 /* Special hook for ISA access. */
425 if (bus
== 0 && dfn
== 0) {
428 dev
= pci_get_bus_and_slot(bus
, dfn
);
436 switch (which
& ~IOBASE_FROM_HOSE
) {
439 case IOBASE_SPARSE_MEM
:
440 return hose
->sparse_mem_base
;
441 case IOBASE_DENSE_MEM
:
442 return hose
->dense_mem_base
;
443 case IOBASE_SPARSE_IO
:
444 return hose
->sparse_io_base
;
445 case IOBASE_DENSE_IO
:
446 return hose
->dense_io_base
;
447 case IOBASE_ROOT_BUS
:
448 return hose
->bus
->number
;
454 /* Destroy an __iomem token. Not copied from lib/iomap.c. */
456 void pci_iounmap(struct pci_dev
*dev
, void __iomem
* addr
)
462 EXPORT_SYMBOL(pci_iounmap
);
464 /* FIXME: Some boxes have multiple ISA bridges! */
465 struct pci_dev
*isa_bridge
;
466 EXPORT_SYMBOL(isa_bridge
);