1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* drivers/pci/setup.c
*
* Extruded from code written by
* Dave Rusling (david.rusling@reo.mts.dec.com)
* David Mosberger (davidm@cs.arizona.edu)
* David Miller (davem@redhat.com)
*
* Support routines for initializing a PCI subsystem.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <asm/cache.h>
#include <asm/pci.h>
#define DEBUG_CONFIG 0
#if DEBUG_CONFIG
# define DBGC(args) printk args
#else
# define DBGC(args)
#endif
int __init
pci_claim_resource(struct pci_dev *dev, int resource)
{
struct resource *res = &dev->resource[resource];
struct resource *root = pci_find_parent_resource(dev, res);
int err;
err = -EINVAL;
if (root != NULL) {
/* If `dev' is on a secondary pci bus, `root' may not be
at the origin. In that case, adjust the resource into
range. */
res->start += root->start;
res->end += root->start;
err = request_resource(root, res);
}
if (err) {
printk(KERN_ERR "PCI: Address space collision on region %d "
"of device %s\n", resource, dev->name);
}
return err;
}
static void
pdev_assign_unassigned_resources(struct pci_dev *dev, u32 min_io, u32 min_mem)
{
u32 reg;
u16 cmd;
int i;
DBGC(("PCI assign unassigned: (%s)\n", dev->name));
pci_read_config_word(dev, PCI_COMMAND, &cmd);
for (i = 0; i < PCI_NUM_RESOURCES; i++) {
struct resource *root, *res;
unsigned long size, min;
res = &dev->resource[i];
if (res->flags & IORESOURCE_IO)
cmd |= PCI_COMMAND_IO;
else if (res->flags & IORESOURCE_MEM)
cmd |= PCI_COMMAND_MEMORY;
/* If it is already assigned or the resource does
not exist, there is nothing to do. */
if (res->parent != NULL || res->flags == 0)
continue;
/* Determine the root we allocate from. */
root = pci_find_parent_resource(dev, res);
if (root == NULL)
continue;
min = (res->flags & IORESOURCE_IO ? min_io : min_mem);
min += root->start;
size = res->end - res->start + 1;
DBGC((" for root[%lx:%lx] min[%lx] size[%lx]\n",
root->start, root->end, min, size));
if (allocate_resource(root, res, size, min, -1, size) < 0) {
printk(KERN_ERR
"PCI: Failed to allocate resource %d for %s\n",
i, dev->name);
continue;
}
DBGC((" got res[%lx:%lx] for resource %d\n",
res->start, res->end, i));
/* Update PCI config space. */
pcibios_update_resource(dev, root, res, i);
}
/* Special case, disable the ROM. Several devices act funny
(ie. do not respond to memory space writes) when it is left
enabled. A good example are QlogicISP adapters. */
pci_read_config_dword(dev, PCI_ROM_ADDRESS, ®);
reg &= ~PCI_ROM_ADDRESS_ENABLE;
pci_write_config_dword(dev, PCI_ROM_ADDRESS, reg);
/* All of these (may) have I/O scattered all around and may not
use I/O base address registers at all. So we just have to
always enable IO to these devices. */
if ((dev->class >> 8) == PCI_CLASS_NOT_DEFINED
|| (dev->class >> 8) == PCI_CLASS_NOT_DEFINED_VGA
|| (dev->class >> 8) == PCI_CLASS_STORAGE_IDE
|| (dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
cmd |= PCI_COMMAND_IO;
}
/* ??? Always turn on bus mastering. If the device doesn't support
it, the bit will go into the bucket. */
cmd |= PCI_COMMAND_MASTER;
/* Enable the appropriate bits in the PCI command register. */
pci_write_config_word(dev, PCI_COMMAND, cmd);
DBGC((" cmd reg 0x%x\n", cmd));
/* If this is a PCI bridge, set the cache line correctly. */
if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
(L1_CACHE_BYTES / sizeof(u32)));
}
}
void __init
pci_assign_unassigned_resources(u32 min_io, u32 min_mem)
{
struct pci_dev *dev;
for (dev = pci_devices; dev; dev = dev->next)
pdev_assign_unassigned_resources(dev, min_io, min_mem);
}
struct pbus_set_ranges_data
{
int found_vga;
unsigned int io_start, io_end;
unsigned int mem_start, mem_end;
};
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
static void __init
pbus_set_ranges(struct pci_bus *bus, struct pbus_set_ranges_data *outer)
{
struct pbus_set_ranges_data inner;
struct pci_bus *child;
struct pci_dev *dev;
inner.found_vga = 0;
inner.mem_start = inner.io_start = ~0;
inner.mem_end = inner.io_end = 0;
/* Collect information about how our direct children are layed out. */
for (dev = bus->devices; dev; dev = dev->sibling) {
int i;
for (i = 0; i < PCI_NUM_RESOURCES; i++) {
struct resource *res = &dev->resource[i];
if (res->flags & IORESOURCE_IO) {
if (res->start < inner.io_start)
inner.io_start = res->start;
if (res->end > inner.io_end)
inner.io_end = res->end;
} else if (res->flags & IORESOURCE_MEM) {
if (res->start < inner.mem_start)
inner.mem_start = res->start;
if (res->end > inner.mem_end)
inner.mem_end = res->end;
}
}
if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
inner.found_vga = 1;
}
/* And for all of the sub-busses. */
for (child = bus->children; child; child = child->next)
pbus_set_ranges(child, &inner);
/* Align the values. */
inner.io_start = ROUND_DOWN(inner.io_start, 4*1024);
inner.io_end = ROUND_UP(inner.io_end, 4*1024);
inner.mem_start = ROUND_DOWN(inner.mem_start, 1*1024*1024);
inner.mem_end = ROUND_UP(inner.mem_end, 1*1024*1024);
/* Configure the bridge, if possible. */
if (bus->self) {
struct pci_dev *bridge = bus->self;
u32 l;
/* Set up the top and bottom of the PCI I/O segment
for this bus. */
pci_read_config_dword(bridge, PCI_IO_BASE, &l);
l &= 0xffff0000;
l |= (inner.io_start >> 8) & 0x00f0;
l |= (inner.io_end - 1) & 0xf000;
pci_write_config_dword(bridge, PCI_IO_BASE, l);
/*
* Clear out the upper 16 bits of IO base/limit.
* Clear out the upper 32 bits of PREF base/limit.
*/
pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0);
pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0);
pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
/* Set up the top and bottom of the PCI Memory segment
for this bus. */
l = (inner.mem_start & 0xfff00000) >> 16;
l |= (inner.mem_end - 1) & 0xfff00000;
pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
/*
* Turn off downstream PF memory address range, unless
* there is a VGA behind this bridge, in which case, we
* enable the PREFETCH range to include BIOS ROM at C0000.
*
* NOTE: this is a bit of a hack, done with PREFETCH for
* simplicity, rather than having to add it into the above
* non-PREFETCH range, which could then be bigger than we want.
* We might assume that we could relocate the BIOS ROM, but
* that would depend on having it found by those who need it
* (the DEC BIOS emulator would find it, but I do not know
* about the Xservers). So, we do it this way for now... ;-)
*/
l = (inner.found_vga) ? 0 : 0x0000ffff;
pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
/*
* Tell bridge that there is an ISA bus in the system,
* and (possibly) a VGA as well.
*/
l = (inner.found_vga) ? 0x0c : 0x04;
pci_write_config_byte(bridge, PCI_BRIDGE_CONTROL, l);
/*
* Clear status bits,
* turn on I/O enable (for downstream I/O),
* turn on memory enable (for downstream memory),
* turn on master enable (for upstream memory and I/O).
*/
pci_write_config_dword(bridge, PCI_COMMAND, 0xffff0007);
}
if (outer) {
outer->found_vga |= inner.found_vga;
if (inner.io_start < outer->io_start)
outer->io_start = inner.io_start;
if (inner.io_end > outer->io_end)
outer->io_end = inner.io_end;
if (inner.mem_start < outer->mem_start)
outer->mem_start = inner.mem_start;
if (inner.mem_end > outer->mem_end)
outer->mem_end = inner.mem_end;
}
}
void __init
pci_set_bus_ranges(void)
{
struct pci_bus *bus;
for (bus = pci_root; bus; bus = bus->next)
pbus_set_ranges(bus, NULL);
}
static void
pdev_fixup_irq(struct pci_dev *dev,
u8 (*swizzle)(struct pci_dev *, u8 *),
int (*map_irq)(struct pci_dev *, u8, u8))
{
u8 pin, slot;
int irq;
/* If this device is not on the primary bus, we need to figure out
which interrupt pin it will come in on. We know which slot it
will come in on 'cos that slot is where the bridge is. Each
time the interrupt line passes through a PCI-PCI bridge we must
apply the swizzle function. */
pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
/* Cope with 0 and illegal. */
if (pin == 0 || pin > 4)
pin = 1;
/* Follow the chain of bridges, swizzling as we go. */
slot = (*swizzle)(dev, &pin);
irq = (*map_irq)(dev, slot, pin);
if (irq == -1)
irq = 0;
dev->irq = irq;
DBGC(("PCI fixup irq: (%s) got %d\n", dev->name, dev->irq));
/* Always tell the device, so the driver knows what is
the real IRQ to use; the device does not use it. */
pcibios_update_irq(dev, irq);
}
void __init
pci_fixup_irqs(u8 (*swizzle)(struct pci_dev *, u8 *),
int (*map_irq)(struct pci_dev *, u8, u8))
{
struct pci_dev *dev;
for (dev = pci_devices; dev; dev = dev->next)
pdev_fixup_irq(dev, swizzle, map_irq);
}
|