2 * drivers/pci/setup-res.c
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 * David Miller (davem@redhat.com)
9 * Support routines for initializing a PCI subsystem.
12 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/cache.h>
25 #include <linux/slab.h>
28 #define DEBUG_CONFIG 1
30 # define DBGC(args) printk args
37 pci_claim_resource(struct pci_dev
*dev
, int resource
)
39 struct resource
*res
= &dev
->resource
[resource
];
40 struct resource
*root
= pci_find_parent_resource(dev
, res
);
45 err
= request_resource(root
, res
);
47 printk(KERN_ERR
"PCI: Address space collision on "
48 "region %d of device %s [%lx:%lx]\n",
49 resource
, dev
->name
, res
->start
, res
->end
);
52 printk(KERN_ERR
"PCI: No parent found for region %d "
53 "of device %s\n", resource
, dev
->name
);
60 * Given the PCI bus a device resides on, try to
61 * find an acceptable resource allocation for a
62 * specific device resource..
64 static int pci_assign_bus_resource(const struct pci_bus
*bus
,
69 unsigned int type_mask
,
74 type_mask
|= IORESOURCE_IO
| IORESOURCE_MEM
;
75 for (i
= 0 ; i
< 4; i
++) {
76 struct resource
*r
= bus
->resource
[i
];
80 /* type_mask must match */
81 if ((res
->flags
^ r
->flags
) & type_mask
)
84 /* We cannot allocate a non-prefetching resource from a pre-fetching area */
85 if ((r
->flags
& IORESOURCE_PREFETCH
) && !(res
->flags
& IORESOURCE_PREFETCH
))
88 /* Ok, try it out.. */
89 if (allocate_resource(r
, res
, size
, min
, -1, size
, pcibios_align_resource
, dev
) < 0)
92 /* Update PCI config space. */
93 pcibios_update_resource(dev
, r
, res
, resno
);
100 pci_assign_resource(struct pci_dev
*dev
, int i
)
102 const struct pci_bus
*bus
= dev
->bus
;
103 struct resource
*res
= dev
->resource
+ i
;
104 unsigned long size
, min
;
106 size
= res
->end
- res
->start
+ 1;
107 min
= (res
->flags
& IORESOURCE_IO
) ? PCIBIOS_MIN_IO
: PCIBIOS_MIN_MEM
;
109 /* First, try exact prefetching match.. */
110 if (pci_assign_bus_resource(bus
, dev
, res
, size
, min
, IORESOURCE_PREFETCH
, i
) < 0) {
114 * But a prefetching area can handle a non-prefetching
115 * window (it will just not perform as well).
117 if (!(res
->flags
& IORESOURCE_PREFETCH
) || pci_assign_bus_resource(bus
, dev
, res
, size
, min
, 0, i
) < 0) {
118 printk(KERN_ERR
"PCI: Failed to allocate resource %d for %s\n", i
, dev
->name
);
123 DBGC((" got res[%lx:%lx] for resource %d of %s\n", res
->start
,
124 res
->end
, i
, dev
->name
));
129 /* Sort resources of a given type by alignment */
131 pdev_sort_resources(struct pci_dev
*dev
,
132 struct resource_list
*head
, u32 type_mask
)
136 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
138 struct resource_list
*list
, *tmp
;
139 unsigned long r_size
;
141 /* PCI-PCI bridges may have I/O ports or
142 memory on the primary bus */
143 if (dev
->class >> 8 == PCI_CLASS_BRIDGE_PCI
&&
144 i
>= PCI_BRIDGE_RESOURCES
)
147 r
= &dev
->resource
[i
];
148 r_size
= r
->end
- r
->start
;
150 if (!(r
->flags
& type_mask
) || r
->parent
)
153 printk(KERN_WARNING
"PCI: Ignore bogus resource %d "
155 i
, r
->start
, r
->end
, dev
->name
);
158 for (list
= head
; ; list
= list
->next
) {
159 unsigned long size
= 0;
160 struct resource_list
*ln
= list
->next
;
163 size
= ln
->res
->end
- ln
->res
->start
;
165 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
177 pdev_enable_device(struct pci_dev
*dev
)
183 DBGC(("PCI enable device: (%s)\n", dev
->name
));
185 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
187 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
188 struct resource
*res
= &dev
->resource
[i
];
190 if (res
->flags
& IORESOURCE_IO
)
191 cmd
|= PCI_COMMAND_IO
;
192 else if (res
->flags
& IORESOURCE_MEM
)
193 cmd
|= PCI_COMMAND_MEMORY
;
196 /* Special case, disable the ROM. Several devices act funny
197 (ie. do not respond to memory space writes) when it is left
198 enabled. A good example are QlogicISP adapters. */
200 if (dev
->rom_base_reg
) {
201 pci_read_config_dword(dev
, dev
->rom_base_reg
, ®
);
202 reg
&= ~PCI_ROM_ADDRESS_ENABLE
;
203 pci_write_config_dword(dev
, dev
->rom_base_reg
, reg
);
204 dev
->resource
[PCI_ROM_RESOURCE
].flags
&= ~PCI_ROM_ADDRESS_ENABLE
;
207 /* All of these (may) have I/O scattered all around and may not
208 use I/O base address registers at all. So we just have to
209 always enable IO to these devices. */
210 if ((dev
->class >> 8) == PCI_CLASS_NOT_DEFINED
211 || (dev
->class >> 8) == PCI_CLASS_NOT_DEFINED_VGA
212 || (dev
->class >> 8) == PCI_CLASS_STORAGE_IDE
213 || (dev
->class >> 16) == PCI_BASE_CLASS_DISPLAY
) {
214 cmd
|= PCI_COMMAND_IO
;
217 /* ??? Always turn on bus mastering. If the device doesn't support
218 it, the bit will go into the bucket. */
219 cmd
|= PCI_COMMAND_MASTER
;
221 /* Set the cache line and default latency (32). */
222 pci_write_config_word(dev
, PCI_CACHE_LINE_SIZE
,
223 (32 << 8) | (L1_CACHE_BYTES
/ sizeof(u32
)));
225 /* Enable the appropriate bits in the PCI command register. */
226 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
228 DBGC((" cmd reg 0x%x\n", cmd
));