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#include <linux/types.h>
28#include <linux/kernel.h>
29#include <linux/pci.h>
30#include <linux/init.h>
31#include <linux/ioport.h>
32#include <linux/errno.h>
33
34#include "pci.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49void
50pcibios_align_resource(void *data, struct resource *res,
51 resource_size_t size, resource_size_t align)
52{
53 if (res->flags & IORESOURCE_IO) {
54 resource_size_t start = res->start;
55
56 if (start & 0x300) {
57 start = (start + 0x3ff) & ~0x3ff;
58 res->start = start;
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
97static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
98{
99 struct pci_bus *bus;
100 struct pci_dev *dev;
101 int idx;
102 struct resource *r, *pr;
103
104
105 list_for_each_entry(bus, bus_list, node) {
106 if ((dev = bus->self)) {
107 for (idx = PCI_BRIDGE_RESOURCES;
108 idx < PCI_NUM_RESOURCES; idx++) {
109 r = &dev->resource[idx];
110 if (!r->flags)
111 continue;
112 pr = pci_find_parent_resource(dev, r);
113 if (!r->start || !pr ||
114 request_resource(pr, r) < 0) {
115 printk(KERN_ERR "PCI: Cannot allocate "
116 "resource region %d "
117 "of bridge %s\n",
118 idx, pci_name(dev));
119
120
121
122
123
124
125 r->flags = 0;
126 }
127 }
128 }
129 pcibios_allocate_bus_resources(&bus->children);
130 }
131}
132
133static void __init pcibios_allocate_resources(int pass)
134{
135 struct pci_dev *dev = NULL;
136 int idx, disabled;
137 u16 command;
138 struct resource *r, *pr;
139
140 for_each_pci_dev(dev) {
141 pci_read_config_word(dev, PCI_COMMAND, &command);
142 for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
143 r = &dev->resource[idx];
144 if (r->parent)
145 continue;
146 if (!r->start)
147 continue;
148 if (r->flags & IORESOURCE_IO)
149 disabled = !(command & PCI_COMMAND_IO);
150 else
151 disabled = !(command & PCI_COMMAND_MEMORY);
152 if (pass == disabled) {
153 DBG("PCI: Resource %08lx-%08lx "
154 "(f=%lx, d=%d, p=%d)\n",
155 r->start, r->end, r->flags, disabled, pass);
156 pr = pci_find_parent_resource(dev, r);
157 if (!pr || request_resource(pr, r) < 0) {
158 printk(KERN_ERR "PCI: Cannot allocate "
159 "resource region %d "
160 "of device %s\n",
161 idx, pci_name(dev));
162
163 r->end -= r->start;
164 r->start = 0;
165 }
166 }
167 }
168 if (!pass) {
169 r = &dev->resource[PCI_ROM_RESOURCE];
170 if (r->flags & IORESOURCE_ROM_ENABLE) {
171
172
173 u32 reg;
174 DBG("PCI: Switching off ROM of %s\n",
175 pci_name(dev));
176 r->flags &= ~IORESOURCE_ROM_ENABLE;
177 pci_read_config_dword(dev,
178 dev->rom_base_reg, ®);
179 pci_write_config_dword(dev, dev->rom_base_reg,
180 reg & ~PCI_ROM_ADDRESS_ENABLE);
181 }
182 }
183 }
184}
185
186static int __init pcibios_assign_resources(void)
187{
188 struct pci_dev *dev = NULL;
189 struct resource *r, *pr;
190
191 if (!(pci_probe & PCI_ASSIGN_ROMS)) {
192
193
194
195
196
197 for_each_pci_dev(dev) {
198 r = &dev->resource[PCI_ROM_RESOURCE];
199 if (!r->flags || !r->start)
200 continue;
201 pr = pci_find_parent_resource(dev, r);
202 if (!pr || request_resource(pr, r) < 0) {
203 r->end -= r->start;
204 r->start = 0;
205 }
206 }
207 }
208
209 pci_assign_unassigned_resources();
210
211 return 0;
212}
213
214void __init pcibios_resource_survey(void)
215{
216 DBG("PCI: Allocating resources\n");
217 pcibios_allocate_bus_resources(&pci_root_buses);
218 pcibios_allocate_resources(0);
219 pcibios_allocate_resources(1);
220}
221
222
223
224
225
226fs_initcall(pcibios_assign_resources);
227
228int pcibios_enable_resources(struct pci_dev *dev, int mask)
229{
230 u16 cmd, old_cmd;
231 int idx;
232 struct resource *r;
233
234 pci_read_config_word(dev, PCI_COMMAND, &cmd);
235 old_cmd = cmd;
236 for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) {
237
238 if (!(mask & (1 << idx)))
239 continue;
240
241 r = &dev->resource[idx];
242 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
243 continue;
244 if ((idx == PCI_ROM_RESOURCE) &&
245 (!(r->flags & IORESOURCE_ROM_ENABLE)))
246 continue;
247 if (!r->start && r->end) {
248 printk(KERN_ERR "PCI: Device %s not available "
249 "because of resource %d collisions\n",
250 pci_name(dev), idx);
251 return -EINVAL;
252 }
253 if (r->flags & IORESOURCE_IO)
254 cmd |= PCI_COMMAND_IO;
255 if (r->flags & IORESOURCE_MEM)
256 cmd |= PCI_COMMAND_MEMORY;
257 }
258 if (cmd != old_cmd) {
259 printk("PCI: Enabling device %s (%04x -> %04x)\n",
260 pci_name(dev), old_cmd, cmd);
261 pci_write_config_word(dev, PCI_COMMAND, cmd);
262 }
263 return 0;
264}
265
266
267
268
269
270unsigned int pcibios_max_latency = 255;
271
272void pcibios_set_master(struct pci_dev *dev)
273{
274 u8 lat;
275 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
276 if (lat < 16)
277 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
278 else if (lat > pcibios_max_latency)
279 lat = pcibios_max_latency;
280 else
281 return;
282 printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
283 pci_name(dev), lat);
284 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
285}
286
287int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
288 enum pci_mmap_state mmap_state, int write_combine)
289{
290 unsigned long prot;
291
292
293
294
295 if (mmap_state == pci_mmap_io)
296 return -EINVAL;
297
298
299
300
301 prot = pgprot_val(vma->vm_page_prot);
302 if (boot_cpu_data.x86 > 3)
303 prot |= _PAGE_PCD | _PAGE_PWT;
304 vma->vm_page_prot = __pgprot(prot);
305
306
307
308
309 if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
310 vma->vm_end - vma->vm_start,
311 vma->vm_page_prot))
312 return -EAGAIN;
313
314 return 0;
315}
316