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>
29 void pci_update_resource(struct pci_dev
*dev
, struct resource
*res
, int resno
)
31 struct pci_bus_region region
;
36 * Ignore resources for unimplemented BARs and unused resource slots
43 * Ignore non-moveable resources. This might be legacy resources for
44 * which no functional BAR register exists or another important
45 * system resource we shouldn't move around.
47 if (res
->flags
& IORESOURCE_PCI_FIXED
)
50 pcibios_resource_to_bus(dev
, ®ion
, res
);
52 dev_dbg(&dev
->dev
, "BAR %d: got res [%#llx-%#llx] bus [%#llx-%#llx] "
53 "flags %#lx\n", resno
,
54 (unsigned long long)res
->start
,
55 (unsigned long long)res
->end
,
56 (unsigned long long)region
.start
,
57 (unsigned long long)region
.end
,
58 (unsigned long)res
->flags
);
60 new = region
.start
| (res
->flags
& PCI_REGION_FLAG_MASK
);
61 if (res
->flags
& IORESOURCE_IO
)
62 mask
= (u32
)PCI_BASE_ADDRESS_IO_MASK
;
64 mask
= (u32
)PCI_BASE_ADDRESS_MEM_MASK
;
67 reg
= PCI_BASE_ADDRESS_0
+ 4 * resno
;
68 } else if (resno
== PCI_ROM_RESOURCE
) {
69 if (!(res
->flags
& IORESOURCE_ROM_ENABLE
))
71 new |= PCI_ROM_ADDRESS_ENABLE
;
72 reg
= dev
->rom_base_reg
;
74 /* Hmm, non-standard resource. */
76 return; /* kill uninitialised var warning */
79 pci_write_config_dword(dev
, reg
, new);
80 pci_read_config_dword(dev
, reg
, &check
);
82 if ((new ^ check
) & mask
) {
83 dev_err(&dev
->dev
, "BAR %d: error updating (%#08x != %#08x)\n",
87 if ((new & (PCI_BASE_ADDRESS_SPACE
|PCI_BASE_ADDRESS_MEM_TYPE_MASK
)) ==
88 (PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
)) {
89 new = region
.start
>> 16 >> 16;
90 pci_write_config_dword(dev
, reg
+ 4, new);
91 pci_read_config_dword(dev
, reg
+ 4, &check
);
93 dev_err(&dev
->dev
, "BAR %d: error updating "
94 "(high %#08x != %#08x)\n", resno
, new, check
);
97 res
->flags
&= ~IORESOURCE_UNSET
;
98 dev_dbg(&dev
->dev
, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
99 resno
, (unsigned long long)region
.start
,
100 (unsigned long long)region
.end
, res
->flags
);
103 int pci_claim_resource(struct pci_dev
*dev
, int resource
)
105 struct resource
*res
= &dev
->resource
[resource
];
106 struct resource
*root
= NULL
;
107 char *dtype
= resource
< PCI_BRIDGE_RESOURCES
? "device" : "bridge";
110 root
= pcibios_select_root(dev
, res
);
114 err
= insert_resource(root
, res
);
117 dev_err(&dev
->dev
, "BAR %d: %s of %s [%#llx-%#llx]\n",
119 root
? "address space collision on" :
120 "no parent found for",
122 (unsigned long long)res
->start
,
123 (unsigned long long)res
->end
);
129 int pci_assign_resource(struct pci_dev
*dev
, int resno
)
131 struct pci_bus
*bus
= dev
->bus
;
132 struct resource
*res
= dev
->resource
+ resno
;
133 resource_size_t size
, min
, align
;
136 size
= res
->end
- res
->start
+ 1;
137 min
= (res
->flags
& IORESOURCE_IO
) ? PCIBIOS_MIN_IO
: PCIBIOS_MIN_MEM
;
139 align
= resource_alignment(res
);
141 dev_err(&dev
->dev
, "BAR %d: can't allocate resource (bogus "
142 "alignment) [%#llx-%#llx] flags %#lx\n",
143 resno
, (unsigned long long)res
->start
,
144 (unsigned long long)res
->end
, res
->flags
);
148 /* First, try exact prefetching match.. */
149 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
,
151 pcibios_align_resource
, dev
);
153 if (ret
< 0 && (res
->flags
& IORESOURCE_PREFETCH
)) {
157 * But a prefetching area can handle a non-prefetching
158 * window (it will just not perform as well).
160 ret
= pci_bus_alloc_resource(bus
, res
, size
, align
, min
, 0,
161 pcibios_align_resource
, dev
);
165 dev_err(&dev
->dev
, "BAR %d: can't allocate %s resource "
166 "[%#llx-%#llx]\n", resno
,
167 res
->flags
& IORESOURCE_IO
? "I/O" : "mem",
168 (unsigned long long)res
->start
,
169 (unsigned long long)res
->end
);
171 res
->flags
&= ~IORESOURCE_STARTALIGN
;
172 if (resno
< PCI_BRIDGE_RESOURCES
)
173 pci_update_resource(dev
, res
, resno
);
180 int pci_assign_resource_fixed(struct pci_dev
*dev
, int resno
)
182 struct pci_bus
*bus
= dev
->bus
;
183 struct resource
*res
= dev
->resource
+ resno
;
184 unsigned int type_mask
;
187 type_mask
= IORESOURCE_IO
| IORESOURCE_MEM
| IORESOURCE_PREFETCH
;
189 for (i
= 0; i
< PCI_BUS_NUM_RESOURCES
; i
++) {
190 struct resource
*r
= bus
->resource
[i
];
194 /* type_mask must match */
195 if ((res
->flags
^ r
->flags
) & type_mask
)
198 ret
= request_resource(r
, res
);
205 dev_err(&dev
->dev
, "BAR %d: can't allocate %s resource "
206 "[%#llx-%#llx\n]", resno
,
207 res
->flags
& IORESOURCE_IO
? "I/O" : "mem",
208 (unsigned long long)res
->start
,
209 (unsigned long long)res
->end
);
210 } else if (resno
< PCI_BRIDGE_RESOURCES
) {
211 pci_update_resource(dev
, res
, resno
);
216 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed
);
219 /* Sort resources by alignment */
220 void pdev_sort_resources(struct pci_dev
*dev
, struct resource_list
*head
)
224 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
226 struct resource_list
*list
, *tmp
;
227 resource_size_t r_align
;
229 r
= &dev
->resource
[i
];
231 if (r
->flags
& IORESOURCE_PCI_FIXED
)
234 if (!(r
->flags
) || r
->parent
)
237 r_align
= resource_alignment(r
);
239 dev_warn(&dev
->dev
, "BAR %d: bogus alignment "
240 "[%#llx-%#llx] flags %#lx\n",
241 i
, (unsigned long long)r
->start
,
242 (unsigned long long)r
->end
, r
->flags
);
245 for (list
= head
; ; list
= list
->next
) {
246 resource_size_t align
= 0;
247 struct resource_list
*ln
= list
->next
;
250 align
= resource_alignment(ln
->res
);
252 if (r_align
> align
) {
253 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
255 panic("pdev_sort_resources(): "
256 "kmalloc() failed!\n");
267 int pci_enable_resources(struct pci_dev
*dev
, int mask
)
273 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
276 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
277 if (!(mask
& (1 << i
)))
280 r
= &dev
->resource
[i
];
282 if (!(r
->flags
& (IORESOURCE_IO
| IORESOURCE_MEM
)))
284 if ((i
== PCI_ROM_RESOURCE
) &&
285 (!(r
->flags
& IORESOURCE_ROM_ENABLE
)))
289 dev_err(&dev
->dev
, "device not available because of "
290 "BAR %d [%#llx-%#llx] collisions\n", i
,
291 (unsigned long long) r
->start
,
292 (unsigned long long) r
->end
);
296 if (r
->flags
& IORESOURCE_IO
)
297 cmd
|= PCI_COMMAND_IO
;
298 if (r
->flags
& IORESOURCE_MEM
)
299 cmd
|= PCI_COMMAND_MEMORY
;
302 if (cmd
!= old_cmd
) {
303 dev_info(&dev
->dev
, "enabling device (%04x -> %04x)\n",
305 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);