1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/string.h>
17#include <linux/pci.h>
18#include <linux/init.h>
19#include <linux/ioport.h>
20#include <linux/kernel.h>
21#include <linux/bootmem.h>
22#include <linux/slab.h>
23#include <asm/machvec.h>
24
25#include "proto.h"
26#include "pci_impl.h"
27
28
29
30
31
32
33const char *const pci_io_names[] = {
34 "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
35 "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
36};
37
38const char *const pci_mem_names[] = {
39 "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
40 "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
41};
42
43const char pci_hae0_name[] = "HAE0";
44
45
46int pci_probe_only;
47
48
49
50
51
52struct pci_controller *hose_head, **hose_tail = &hose_head;
53struct pci_controller *pci_isa_hose;
54
55
56
57
58
59static void __init
60quirk_eisa_bridge(struct pci_dev *dev)
61{
62 dev->class = PCI_CLASS_BRIDGE_EISA << 8;
63}
64
65static void __init
66quirk_isa_bridge(struct pci_dev *dev)
67{
68 dev->class = PCI_CLASS_BRIDGE_ISA << 8;
69}
70
71static void __init
72quirk_cypress(struct pci_dev *dev)
73{
74
75
76
77
78
79
80
81 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
82 dev->resource[0].flags = 0;
83 dev->resource[1].flags = 0;
84 }
85
86
87
88
89
90
91
92 else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
93 if (__direct_map_base + __direct_map_size >= 0xfff00000)
94 __direct_map_size = 0xfff00000 - __direct_map_base;
95 else {
96 struct pci_controller *hose = dev->sysdata;
97 struct pci_iommu_arena *pci = hose->sg_pci;
98 if (pci && pci->dma_base + pci->size >= 0xfff00000)
99 pci->size = 0xfff00000 - pci->dma_base;
100 }
101 }
102}
103
104struct pci_fixup pcibios_fixups[] __initdata = {
105 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82375,
106 quirk_eisa_bridge },
107 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378,
108 quirk_isa_bridge },
109 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693,
110 quirk_cypress },
111 { 0 }
112};
113
114#define MAX(val1, val2) ((val1) > (val2) ? (val1) : (val2))
115#define ALIGN(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
116#define KB 1024
117#define MB (1024*KB)
118#define GB (1024*MB)
119
120void
121pcibios_align_resource(void *data, struct resource *res,
122 unsigned long size, unsigned long align)
123{
124 struct pci_dev *dev = data;
125 struct pci_controller *hose = dev->sysdata;
126 unsigned long alignto;
127 unsigned long start = res->start;
128
129 if (res->flags & IORESOURCE_IO) {
130
131 if (start - hose->io_space->start < PCIBIOS_MIN_IO)
132 start = PCIBIOS_MIN_IO + hose->io_space->start;
133
134
135
136
137 if (start & 0x300)
138 start = (start + 0x3ff) & ~0x3ff;
139 }
140 else if (res->flags & IORESOURCE_MEM) {
141
142 if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
143 start = PCIBIOS_MIN_MEM + hose->mem_space->start;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 alignto = MAX(0x1000, align);
163 start = ALIGN(start, alignto);
164 if (hose->sparse_mem_base && size <= 7 * 16*MB) {
165 if (((start / (16*MB)) & 0x7) == 0) {
166 start &= ~(128*MB - 1);
167 start += 16*MB;
168 start = ALIGN(start, alignto);
169 }
170 if (start/(128*MB) != (start + size - 1)/(128*MB)) {
171 start &= ~(128*MB - 1);
172 start += (128 + 16)*MB;
173 start = ALIGN(start, alignto);
174 }
175 }
176 }
177
178 res->start = start;
179}
180#undef MAX
181#undef ALIGN
182#undef KB
183#undef MB
184#undef GB
185
186void __init
187pcibios_init(void)
188{
189 if (!alpha_mv.init_pci)
190 return;
191 alpha_mv.init_pci();
192}
193
194char * __init
195pcibios_setup(char *str)
196{
197 return str;
198}
199
200#ifdef ALPHA_RESTORE_SRM_SETUP
201static struct pdev_srm_saved_conf *srm_saved_configs;
202
203void __init
204pdev_save_srm_config(struct pci_dev *dev)
205{
206 struct pdev_srm_saved_conf *tmp;
207 static int printed = 0;
208
209 if (!alpha_using_srm || pci_probe_only)
210 return;
211
212 if (!printed) {
213 printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
214 printed = 1;
215 }
216
217 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
218 if (!tmp) {
219 printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
220 return;
221 }
222 tmp->next = srm_saved_configs;
223 tmp->dev = dev;
224
225 pci_save_state(dev, tmp->regs);
226
227 srm_saved_configs = tmp;
228}
229
230void
231pci_restore_srm_config(void)
232{
233 struct pdev_srm_saved_conf *tmp;
234
235
236 if (pci_probe_only)
237 return;
238
239
240 for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
241 pci_restore_state(tmp->dev, tmp->regs);
242 }
243}
244#endif
245
246void __init
247pcibios_fixup_resource(struct resource *res, struct resource *root)
248{
249 res->start += root->start;
250 res->end += root->start;
251}
252
253void __init
254pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
255{
256
257 struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
258 int i;
259
260 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
261 if (!dev->resource[i].start)
262 continue;
263 if (dev->resource[i].flags & IORESOURCE_IO)
264 pcibios_fixup_resource(&dev->resource[i],
265 hose->io_space);
266 else if (dev->resource[i].flags & IORESOURCE_MEM)
267 pcibios_fixup_resource(&dev->resource[i],
268 hose->mem_space);
269 }
270}
271
272void __init
273pcibios_fixup_bus(struct pci_bus *bus)
274{
275
276
277 struct pci_controller *hose = bus->sysdata;
278 struct list_head *ln;
279 struct pci_dev *dev = bus->self;
280
281 if (!dev) {
282
283 u32 pci_mem_end;
284 u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
285 unsigned long end;
286
287 bus->resource[0] = hose->io_space;
288 bus->resource[1] = hose->mem_space;
289
290
291
292 pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
293 end = hose->mem_space->start + pci_mem_end;
294 if (hose->mem_space->end > end)
295 hose->mem_space->end = end;
296 } else if (pci_probe_only &&
297 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
298 pci_read_bridge_bases(bus);
299 pcibios_fixup_device_resources(dev, bus);
300 }
301
302 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
303 struct pci_dev *dev = pci_dev_b(ln);
304
305 pdev_save_srm_config(dev);
306 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
307 pcibios_fixup_device_resources(dev, bus);
308 }
309}
310
311void
312pcibios_update_resource(struct pci_dev *dev, struct resource *root,
313 struct resource *res, int resource)
314{
315 struct pci_controller *hose = dev->sysdata;
316 int where;
317 u32 reg;
318
319 if (resource < PCI_ROM_RESOURCE)
320 where = PCI_BASE_ADDRESS_0 + (resource * 4);
321 else if (resource == PCI_ROM_RESOURCE)
322 where = dev->rom_base_reg;
323 else {
324 return;
325 }
326
327
328 if (res->flags & IORESOURCE_IO)
329 root = hose->io_space;
330 if (res->flags & IORESOURCE_MEM)
331 root = hose->mem_space;
332
333 reg = (res->start - root->start) | (res->flags & 0xf);
334 pci_write_config_dword(dev, where, reg);
335 if ((res->flags & (PCI_BASE_ADDRESS_SPACE
336 | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
337 == (PCI_BASE_ADDRESS_SPACE_MEMORY
338 | PCI_BASE_ADDRESS_MEM_TYPE_64)) {
339 pci_write_config_dword(dev, where+4, 0);
340 printk(KERN_WARNING "PCI: dev %s type 64-bit\n", dev->name);
341 }
342
343
344}
345
346void __init
347pcibios_update_irq(struct pci_dev *dev, int irq)
348{
349 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
350}
351
352
353
354u8 __init
355common_swizzle(struct pci_dev *dev, u8 *pinp)
356{
357 struct pci_controller *hose = dev->sysdata;
358
359 if (dev->bus->number != hose->first_busno) {
360 u8 pin = *pinp;
361 do {
362 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
363
364 dev = dev->bus->self;
365 } while (dev->bus->parent);
366 *pinp = pin;
367
368
369 }
370
371 return PCI_SLOT(dev->devfn);
372}
373
374void __init
375pcibios_fixup_pbus_ranges(struct pci_bus * bus,
376 struct pbus_set_ranges_data * ranges)
377{
378 struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
379
380 ranges->io_start -= hose->io_space->start;
381 ranges->io_end -= hose->io_space->start;
382 ranges->mem_start -= hose->mem_space->start;
383 ranges->mem_end -= hose->mem_space->start;
384
385
386 ranges->prefetch_start -= hose->mem_space->start;
387 ranges->prefetch_end -= hose->mem_space->start;
388}
389
390int
391pcibios_enable_device(struct pci_dev *dev, int mask)
392{
393
394 return 0;
395}
396
397
398
399
400
401
402void
403pcibios_set_master(struct pci_dev *dev)
404{
405 u8 lat;
406 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
407 if (lat >= 16) return;
408 printk("PCI: Setting latency timer of device %s to 64\n",
409 dev->slot_name);
410 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
411}
412
413static void __init
414pcibios_claim_one_bus(struct pci_bus *b)
415{
416 struct list_head *ld;
417 struct pci_bus *child_bus;
418
419 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
420 struct pci_dev *dev = pci_dev_b(ld);
421 int i;
422
423 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
424 struct resource *r = &dev->resource[i];
425
426 if (r->parent || !r->start || !r->flags)
427 continue;
428 pci_claim_resource(dev, i);
429 }
430 }
431
432 list_for_each_entry(child_bus, &b->children, node)
433 pcibios_claim_one_bus(child_bus);
434}
435
436static void __init
437pcibios_claim_console_setup(void)
438{
439 struct list_head *lb;
440
441 for(lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
442 struct pci_bus *b = pci_bus_b(lb);
443 pcibios_claim_one_bus(b);
444 }
445}
446
447void __init
448common_init_pci(void)
449{
450 struct pci_controller *hose;
451 struct pci_bus *bus;
452 int next_busno;
453
454
455 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
456 hose->first_busno = next_busno;
457 hose->last_busno = 0xff;
458 bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
459 hose->bus = bus;
460 next_busno = hose->last_busno = bus->subordinate;
461 next_busno += 1;
462 }
463
464 if (pci_probe_only)
465 pcibios_claim_console_setup();
466 else
467
468
469 pci_assign_unassigned_resources();
470 pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
471}
472
473
474struct pci_controller * __init
475alloc_pci_controller(void)
476{
477 struct pci_controller *hose;
478
479 hose = alloc_bootmem(sizeof(*hose));
480
481 *hose_tail = hose;
482 hose_tail = &hose->next;
483
484 return hose;
485}
486
487struct resource * __init
488alloc_resource(void)
489{
490 struct resource *res;
491
492 res = alloc_bootmem(sizeof(*res));
493
494 return res;
495}
496
497
498
499
500
501asmlinkage long
502sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
503{
504 struct pci_controller *hose;
505 struct pci_dev *dev;
506
507
508 if (which & IOBASE_FROM_HOSE) {
509 for(hose = hose_head; hose; hose = hose->next)
510 if (hose->index == bus) break;
511 if (!hose) return -ENODEV;
512 } else {
513
514 if (bus == 0 && dfn == 0) {
515 hose = pci_isa_hose;
516 } else {
517 dev = pci_find_slot(bus, dfn);
518 if (!dev)
519 return -ENODEV;
520 hose = dev->sysdata;
521 }
522 }
523
524 switch (which & ~IOBASE_FROM_HOSE) {
525 case IOBASE_HOSE:
526 return hose->index;
527 case IOBASE_SPARSE_MEM:
528 return hose->sparse_mem_base;
529 case IOBASE_DENSE_MEM:
530 return hose->dense_mem_base;
531 case IOBASE_SPARSE_IO:
532 return hose->sparse_io_base;
533 case IOBASE_DENSE_IO:
534 return hose->dense_io_base;
535 case IOBASE_ROOT_BUS:
536 return hose->bus->number;
537 }
538
539 return -EOPNOTSUPP;
540}
541
542
543int
544pci_controller_num(struct pci_dev *pdev)
545{
546 struct pci_controller *hose = pdev->sysdata;
547 return (hose ? hose->index : -ENXIO);
548}
549