Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
587 giacomo 1
/*
2
 *      Low-Level PCI Access for i386 machines
3
 *
4
 * Copyright 1993, 1994 Drew Eckhardt
5
 *      Visionary Computing
6
 *      (Unix and Linux consulting and custom programming)
7
 *      Drew@Colorado.EDU
8
 *      +1 (303) 786-7975
9
 *
10
 * Drew's work was sponsored by:
11
 *      iX Multiuser Multitasking Magazine
12
 *      Hannover, Germany
13
 *      hm@ix.de
14
 *
15
 * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
16
 *
17
 * For more information, please consult the following manuals (look at
18
 * http://www.pcisig.com/ for how to get them):
19
 *
20
 * PCI BIOS Specification
21
 * PCI Local Bus Specification
22
 * PCI to PCI Bridge Specification
23
 * PCI System Design Guide
24
 *
25
 */
26
 
27
#include <linuxcomp.h>
28
 
29
#include <linux/types.h>
30
#include <linux/kernel.h>
31
#include <linux/pci.h>
32
#include <linux/init.h>
33
#include <linux/ioport.h>
34
#include <linux/errno.h>
35
 
36
#include "pci2.h"
37
 
38
/*
39
 * We need to avoid collisions with `mirrored' VGA ports
40
 * and other strange ISA hardware, so we always want the
41
 * addresses to be allocated in the 0x000-0x0ff region
42
 * modulo 0x400.
43
 *
44
 * Why? Because some silly external IO cards only decode
45
 * the low 10 bits of the IO address. The 0x00-0xff region
46
 * is reserved for motherboard devices that decode all 16
47
 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
48
 * but we want to try to avoid allocating at 0x2900-0x2bff
49
 * which might have be mirrored at 0x0100-0x03ff..
50
 */
51
void
52
pcibios_align_resource(void *data, struct resource *res,
53
                       unsigned long size, unsigned long align)
54
{
55
        if (res->flags & IORESOURCE_IO) {
56
                unsigned long start = res->start;
57
 
58
                if (start & 0x300) {
59
                        start = (start + 0x3ff) & ~0x3ff;
60
                        res->start = start;
61
                }
62
        }
63
}
64
 
65
 
66
/*
67
 *  Handle resources of PCI devices.  If the world were perfect, we could
68
 *  just allocate all the resource regions and do nothing more.  It isn't.
69
 *  On the other hand, we cannot just re-allocate all devices, as it would
70
 *  require us to know lots of host bridge internals.  So we attempt to
71
 *  keep as much of the original configuration as possible, but tweak it
72
 *  when it's found to be wrong.
73
 *
74
 *  Known BIOS problems we have to work around:
75
 *      - I/O or memory regions not configured
76
 *      - regions configured, but not enabled in the command register
77
 *      - bogus I/O addresses above 64K used
78
 *      - expansion ROMs left enabled (this may sound harmless, but given
79
 *        the fact the PCI specs explicitly allow address decoders to be
80
 *        shared between expansion ROMs and other resource regions, it's
81
 *        at least dangerous)
82
 *
83
 *  Our solution:
84
 *      (1) Allocate resources for all buses behind PCI-to-PCI bridges.
85
 *          This gives us fixed barriers on where we can allocate.
86
 *      (2) Allocate resources for all enabled devices.  If there is
87
 *          a collision, just mark the resource as unallocated. Also
88
 *          disable expansion ROMs during this step.
89
 *      (3) Try to allocate resources for disabled devices.  If the
90
 *          resources were assigned correctly, everything goes well,
91
 *          if they weren't, they won't disturb allocation of other
92
 *          resources.
93
 *      (4) Assign new addresses to resources which were either
94
 *          not configured at all or misconfigured.  If explicitly
95
 *          requested by the user, configure expansion ROM address
96
 *          as well.
97
 */
98
 
99
static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
100
{
101
        struct list_head *ln;
102
        struct pci_bus *bus;
103
        struct pci_dev *dev;
104
        int idx;
105
        struct resource *r, *pr;
106
 
107
        /* Depth-First Search on bus tree */
108
        for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
109
                bus = pci_bus_b(ln);
110
                if ((dev = bus->self)) {
111
                        for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
112
                                r = &dev->resource[idx];
113
                                if (!r->start)
114
                                        continue;
115
                                pr = pci_find_parent_resource(dev, r);
116
                                if (!pr || request_resource(pr, r) < 0)
117
                                        printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev));
118
                        }
119
                }
120
                pcibios_allocate_bus_resources(&bus->children);
121
        }
122
}
123
 
124
static void __init pcibios_allocate_resources(int pass)
125
{
126
        struct pci_dev *dev = NULL;
127
        int idx, disabled;
128
        u16 command;
129
        struct resource *r, *pr;
130
 
131
        while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
132
                pci_read_config_word(dev, PCI_COMMAND, &command);
133
                for(idx = 0; idx < 6; idx++) {
134
                        r = &dev->resource[idx];
135
                        if (r->parent)          /* Already allocated */
136
                                continue;
137
                        if (!r->start)          /* Address not assigned at all */
138
                                continue;
139
                        if (r->flags & IORESOURCE_IO)
140
                                disabled = !(command & PCI_COMMAND_IO);
141
                        else
142
                                disabled = !(command & PCI_COMMAND_MEMORY);
143
                        if (pass == disabled) {
144
                                DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
145
                                    r->start, r->end, r->flags, disabled, pass);
146
                                pr = pci_find_parent_resource(dev, r);
147
                                if (!pr || request_resource(pr, r) < 0) {
148
                                        printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev));
149
                                        /* We'll assign a new address later */
150
                                        r->end -= r->start;
151
                                        r->start = 0;
152
                                }
153
                        }
154
                }
155
                if (!pass) {
156
                        r = &dev->resource[PCI_ROM_RESOURCE];
157
                        if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
158
                                /* Turn the ROM off, leave the resource region, but keep it unregistered. */
159
                                u32 reg;
160
                                DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
161
                                r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
162
                                pci_read_config_dword(dev, dev->rom_base_reg, &reg);
163
                                pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
164
                        }
165
                }
166
        }
167
}
168
 
169
static void __init pcibios_assign_resources(void)
170
{
171
        struct pci_dev *dev = NULL;
172
        int idx;
173
        struct resource *r;
174
 
175
        while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
176
                int class = dev->class >> 8;
177
 
178
                /* Don't touch classless devices and host bridges */
179
                if (!class || class == PCI_CLASS_BRIDGE_HOST)
180
                        continue;
181
 
182
                for(idx=0; idx<6; idx++) {
183
                        r = &dev->resource[idx];
184
 
185
                        /*
186
                         *  Don't touch IDE controllers and I/O ports of video cards!
187
                         */
188
                        if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
189
                            (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
190
                                continue;
191
 
192
                        /*
193
                         *  We shall assign a new address to this resource, either because
194
                         *  the BIOS forgot to do so or because we have decided the old
195
                         *  address was unusable for some reason.
196
                         */
197
                        if (!r->start && r->end)
198
                                pci_assign_resource(dev, idx);
199
                }
200
 
201
                if (pci_probe & PCI_ASSIGN_ROMS) {
202
                        r = &dev->resource[PCI_ROM_RESOURCE];
203
                        r->end -= r->start;
204
                        r->start = 0;
205
                        if (r->end)
206
                                pci_assign_resource(dev, PCI_ROM_RESOURCE);
207
                }
208
        }
209
}
210
 
211
void __init pcibios_resource_survey(void)
212
{
213
        DBG("PCI: Allocating resources\n");
214
        pcibios_allocate_bus_resources(&pci_root_buses);
215
        pcibios_allocate_resources(0);
216
        pcibios_allocate_resources(1);
217
        pcibios_assign_resources();
218
}
219
 
220
int pcibios_enable_resources(struct pci_dev *dev, int mask)
221
{
222
        u16 cmd, old_cmd;
223
        int idx;
224
        struct resource *r;
225
 
226
        pci_read_config_word(dev, PCI_COMMAND, &cmd);
227
        old_cmd = cmd;
228
        for(idx=0; idx<6; idx++) {
229
                /* Only set up the requested stuff */
230
                if (!(mask & (1<<idx)))
231
                        continue;
232
 
233
                r = &dev->resource[idx];
234
                if (!r->start && r->end) {
235
                        printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
236
                        return -EINVAL;
237
                }
238
                if (r->flags & IORESOURCE_IO)
239
                        cmd |= PCI_COMMAND_IO;
240
                if (r->flags & IORESOURCE_MEM)
241
                        cmd |= PCI_COMMAND_MEMORY;
242
        }
243
        if (dev->resource[PCI_ROM_RESOURCE].start)
244
                cmd |= PCI_COMMAND_MEMORY;
245
        if (cmd != old_cmd) {
246
                printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
247
                pci_write_config_word(dev, PCI_COMMAND, cmd);
248
        }
249
        return 0;
250
}
251
 
252
/*
253
 *  If we set up a device for bus mastering, we need to check the latency
254
 *  timer as certain crappy BIOSes forget to set it properly.
255
 */
256
unsigned int pcibios_max_latency = 255;
257
 
258
void pcibios_set_master(struct pci_dev *dev)
259
{
260
        u8 lat;
261
        pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
262
        if (lat < 16)
263
                lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
264
        else if (lat > pcibios_max_latency)
265
                lat = pcibios_max_latency;
266
        else
267
                return;
268
        printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
269
        pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
270
}
271
 
272
int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
273
                        enum pci_mmap_state mmap_state, int write_combine)
274
{
275
        unsigned long prot;
276
 
277
        /* I/O space cannot be accessed via normal processor loads and
278
         * stores on this platform.
279
         */
280
        if (mmap_state == pci_mmap_io)
281
                return -EINVAL;
282
 
283
        /* Leave vm_pgoff as-is, the PCI space address is the physical
284
         * address on this platform.
285
         */
286
        vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
287
 
288
        prot = pgprot_val(vma->vm_page_prot);
289
        prot |= _PAGE_PCD | _PAGE_PWT;
290
        vma->vm_page_prot = __pgprot(prot);
291
 
292
        /* Write-combine setting is ignored, it is changed via the mtrr
293
         * interfaces on this platform.
294
         */
295
        if (remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
296
                             vma->vm_end - vma->vm_start,
297
                             vma->vm_page_prot))
298
                return -EAGAIN;
299
 
300
        return 0;
301
}