1
2
3
4
5#include <linux/config.h>
6#include <linux/kernel.h>
7#include <linux/pci.h>
8#include <linux/delay.h>
9#include <linux/string.h>
10#include <linux/init.h>
11#include <linux/capability.h>
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <linux/bootmem.h>
15
16#include <asm/processor.h>
17#include <asm/io.h>
18#include <asm/prom.h>
19#include <asm/sections.h>
20#include <asm/pci-bridge.h>
21#include <asm/byteorder.h>
22#include <asm/irq.h>
23#include <asm/uaccess.h>
24
25#undef DEBUG
26
27#ifdef DEBUG
28#define DBG(x...) printk(x)
29#else
30#define DBG(x...)
31#endif
32
33unsigned long isa_io_base = 0;
34unsigned long isa_mem_base = 0;
35unsigned long pci_dram_offset = 0;
36
37void pcibios_make_OF_bus_map(void);
38
39static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
40static int probe_resource(struct pci_bus *parent, struct resource *pr,
41 struct resource *res, struct resource **conflict);
42static void update_bridge_base(struct pci_bus *bus, int i);
43static void pcibios_fixup_resources(struct pci_dev* dev);
44static void fixup_broken_pcnet32(struct pci_dev* dev);
45static int reparent_resources(struct resource *parent, struct resource *res);
46static void fixup_rev1_53c810(struct pci_dev* dev);
47static void fixup_cpc710_pci64(struct pci_dev* dev);
48#ifdef CONFIG_PPC_PMAC
49extern void pmac_pci_fixup_cardbus(struct pci_dev* dev);
50extern void pmac_pci_fixup_pciata(struct pci_dev* dev);
51extern void pmac_pci_fixup_k2_sata(struct pci_dev* dev);
52#endif
53#ifdef CONFIG_PPC_OF
54static u8* pci_to_OF_bus_map;
55#endif
56
57
58
59
60int pci_assign_all_busses;
61
62struct pci_controller* hose_head;
63struct pci_controller** hose_tail = &hose_head;
64
65static int pci_bus_count;
66
67struct pci_fixup pcibios_fixups[] = {
68 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32 },
69 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_NCR, PCI_DEVICE_ID_NCR_53C810, fixup_rev1_53c810 },
70 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64},
71 { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },
72#ifdef CONFIG_PPC_PMAC
73
74 { PCI_FIXUP_FINAL, PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus },
75 { PCI_FIXUP_FINAL, PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata },
76 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_SERVERWORKS, 0x0240, pmac_pci_fixup_k2_sata },
77#endif
78 { 0 }
79};
80
81static void
82fixup_rev1_53c810(struct pci_dev* dev)
83{
84
85
86
87
88 if ((dev->class == PCI_CLASS_NOT_DEFINED)) {
89 printk("NCR 53c810 rev 1 detected, setting PCI class.\n");
90 dev->class = PCI_CLASS_STORAGE_SCSI;
91 }
92}
93
94static void
95fixup_broken_pcnet32(struct pci_dev* dev)
96{
97 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
98 dev->vendor = PCI_VENDOR_ID_AMD;
99 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
100 pci_name_device(dev);
101 }
102}
103
104static void
105fixup_cpc710_pci64(struct pci_dev* dev)
106{
107
108
109
110 dev->resource[0].start = dev->resource[0].end = 0;
111 dev->resource[0].flags = 0;
112 dev->resource[1].start = dev->resource[1].end = 0;
113 dev->resource[1].flags = 0;
114}
115
116static void
117pcibios_fixup_resources(struct pci_dev *dev)
118{
119 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
120 int i;
121 unsigned long offset;
122
123 if (!hose) {
124 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
125 return;
126 }
127 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
128 struct resource *res = dev->resource + i;
129 if (!res->flags)
130 continue;
131 if (res->end == 0xffffffff) {
132 DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
133 pci_name(dev), i, res->start, res->end);
134 res->end -= res->start;
135 res->start = 0;
136 res->flags |= IORESOURCE_UNSET;
137 continue;
138 }
139 offset = 0;
140 if (res->flags & IORESOURCE_MEM) {
141 offset = hose->pci_mem_offset;
142 } else if (res->flags & IORESOURCE_IO) {
143 offset = (unsigned long) hose->io_base_virt
144 - isa_io_base;
145 }
146 if (offset != 0) {
147 res->start += offset;
148 res->end += offset;
149#ifdef DEBUG
150 printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
151 i, res->flags, pci_name(dev),
152 res->start - offset, res->start);
153#endif
154 }
155 }
156
157
158 if (ppc_md.pcibios_fixup_resources)
159 ppc_md.pcibios_fixup_resources(dev);
160}
161
162void
163pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
164 struct resource *res)
165{
166 unsigned long offset = 0;
167 struct pci_controller *hose = dev->sysdata;
168
169 if (hose && res->flags & IORESOURCE_IO)
170 offset = (unsigned long)hose->io_base_virt - isa_io_base;
171 else if (hose && res->flags & IORESOURCE_MEM)
172 offset = hose->pci_mem_offset;
173 region->start = res->start - offset;
174 region->end = res->end - offset;
175}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190void
191pcibios_align_resource(void *data, struct resource *res, unsigned long size,
192 unsigned long align)
193{
194 struct pci_dev *dev = data;
195
196 if (res->flags & IORESOURCE_IO) {
197 unsigned long start = res->start;
198
199 if (size > 0x100) {
200 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
201 " (%ld bytes)\n", pci_name(dev),
202 dev->resource - res, size);
203 }
204
205 if (start & 0x300) {
206 start = (start + 0x3ff) & ~0x3ff;
207 res->start = start;
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
246static void __init
247pcibios_allocate_bus_resources(struct list_head *bus_list)
248{
249 struct list_head *ln;
250 struct pci_bus *bus;
251 int i;
252 struct resource *res, *pr;
253
254
255 for (ln = bus_list->next; ln != bus_list; ln=ln->next) {
256 bus = pci_bus_b(ln);
257 for (i = 0; i < 4; ++i) {
258 if ((res = bus->resource[i]) == NULL || !res->flags
259 || res->start > res->end)
260 continue;
261 if (bus->parent == NULL)
262 pr = (res->flags & IORESOURCE_IO)?
263 &ioport_resource: &iomem_resource;
264 else {
265 pr = pci_find_parent_resource(bus->self, res);
266 if (pr == res) {
267
268
269
270
271 continue;
272 }
273 }
274
275 DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
276 res->start, res->end, res->flags, pr);
277 if (pr) {
278 if (request_resource(pr, res) == 0)
279 continue;
280
281
282
283
284
285 if (reparent_resources(pr, res) == 0)
286 continue;
287 }
288 printk(KERN_ERR "PCI: Cannot allocate resource region "
289 "%d of PCI bridge %d\n", i, bus->number);
290 if (pci_relocate_bridge_resource(bus, i))
291 bus->resource[i] = NULL;
292 }
293 pcibios_allocate_bus_resources(&bus->children);
294 }
295}
296
297
298
299
300
301static int __init
302reparent_resources(struct resource *parent, struct resource *res)
303{
304 struct resource *p, **pp;
305 struct resource **firstpp = NULL;
306
307 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
308 if (p->end < res->start)
309 continue;
310 if (res->end < p->start)
311 break;
312 if (p->start < res->start || p->end > res->end)
313 return -1;
314 if (firstpp == NULL)
315 firstpp = pp;
316 }
317 if (firstpp == NULL)
318 return -1;
319 res->parent = parent;
320 res->child = *firstpp;
321 res->sibling = *pp;
322 *firstpp = res;
323 *pp = NULL;
324 for (p = res->child; p != NULL; p = p->sibling) {
325 p->parent = res;
326 DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
327 p->name, p->start, p->end, res->name);
328 }
329 return 0;
330}
331
332
333
334
335
336static int __init
337pci_relocate_bridge_resource(struct pci_bus *bus, int i)
338{
339 struct resource *res, *pr, *conflict;
340 unsigned long try, size;
341 int j;
342 struct pci_bus *parent = bus->parent;
343
344 if (parent == NULL) {
345
346 printk(KERN_ERR "PCI: can't move host bridge resource\n");
347 return -1;
348 }
349 res = bus->resource[i];
350 if (res == NULL)
351 return -1;
352 pr = NULL;
353 for (j = 0; j < 4; j++) {
354 struct resource *r = parent->resource[j];
355 if (!r)
356 continue;
357 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
358 continue;
359 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
360 pr = r;
361 break;
362 }
363 if (res->flags & IORESOURCE_PREFETCH)
364 pr = r;
365 }
366 if (pr == NULL)
367 return -1;
368 size = res->end - res->start;
369 if (pr->start > pr->end || size > pr->end - pr->start)
370 return -1;
371 try = pr->end;
372 for (;;) {
373 res->start = try - size;
374 res->end = try;
375 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
376 break;
377 if (conflict->start <= pr->start + size)
378 return -1;
379 try = conflict->start - 1;
380 }
381 if (request_resource(pr, res)) {
382 DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
383 res->start, res->end);
384 return -1;
385 }
386 update_bridge_base(bus, i);
387 printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
388 bus->number, i, res->start, res->end);
389 return 0;
390}
391
392static int __init
393probe_resource(struct pci_bus *parent, struct resource *pr,
394 struct resource *res, struct resource **conflict)
395{
396 struct pci_bus *bus;
397 struct pci_dev *dev;
398 struct resource *r;
399 struct list_head *ln;
400 int i;
401
402 for (r = pr->child; r != NULL; r = r->sibling) {
403 if (r->end >= res->start && res->end >= r->start) {
404 *conflict = r;
405 return 1;
406 }
407 }
408 for (ln = parent->children.next; ln != &parent->children;
409 ln = ln->next) {
410 bus = pci_bus_b(ln);
411 for (i = 0; i < 4; ++i) {
412 if ((r = bus->resource[i]) == NULL)
413 continue;
414 if (!r->flags || r->start > r->end || r == res)
415 continue;
416 if (pci_find_parent_resource(bus->self, r) != pr)
417 continue;
418 if (r->end >= res->start && res->end >= r->start) {
419 *conflict = r;
420 return 1;
421 }
422 }
423 }
424 for (ln = parent->devices.next; ln != &parent->devices; ln=ln->next) {
425 dev = pci_dev_b(ln);
426 for (i = 0; i < 6; ++i) {
427 r = &dev->resource[i];
428 if (!r->flags || (r->flags & IORESOURCE_UNSET))
429 continue;
430 if (pci_find_parent_resource(bus->self, r) != pr)
431 continue;
432 if (r->end >= res->start && res->end >= r->start) {
433 *conflict = r;
434 return 1;
435 }
436 }
437 }
438 return 0;
439}
440
441static void __init
442update_bridge_base(struct pci_bus *bus, int i)
443{
444 struct resource *res = bus->resource[i];
445 u8 io_base_lo, io_limit_lo;
446 u16 mem_base, mem_limit;
447 u16 cmd;
448 unsigned long start, end, off;
449 struct pci_dev *dev = bus->self;
450 struct pci_controller *hose = dev->sysdata;
451
452 if (!hose) {
453 printk("update_bridge_base: no hose?\n");
454 return;
455 }
456 pci_read_config_word(dev, PCI_COMMAND, &cmd);
457 pci_write_config_word(dev, PCI_COMMAND,
458 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
459 if (res->flags & IORESOURCE_IO) {
460 off = (unsigned long) hose->io_base_virt - isa_io_base;
461 start = res->start - off;
462 end = res->end - off;
463 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
464 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
465 if (end > 0xffff) {
466 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
467 start >> 16);
468 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
469 end >> 16);
470 io_base_lo |= PCI_IO_RANGE_TYPE_32;
471 } else
472 io_base_lo |= PCI_IO_RANGE_TYPE_16;
473 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
474 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
475
476 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
477 == IORESOURCE_MEM) {
478 off = hose->pci_mem_offset;
479 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
480 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
481 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
482 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
483
484 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
485 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
486 off = hose->pci_mem_offset;
487 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
488 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
489 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
490 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
491
492 } else {
493 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
494 pci_name(dev), i, res->flags);
495 }
496 pci_write_config_word(dev, PCI_COMMAND, cmd);
497}
498
499static inline void alloc_resource(struct pci_dev *dev, int idx)
500{
501 struct resource *pr, *r = &dev->resource[idx];
502
503 DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
504 pci_name(dev), idx, r->start, r->end, r->flags);
505 pr = pci_find_parent_resource(dev, r);
506 if (!pr || request_resource(pr, r) < 0) {
507 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
508 " of device %s\n", idx, pci_name(dev));
509 if (pr)
510 DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
511 pr, pr->start, pr->end, pr->flags);
512
513 r->flags |= IORESOURCE_UNSET;
514 r->end -= r->start;
515 r->start = 0;
516 }
517}
518
519static void __init
520pcibios_allocate_resources(int pass)
521{
522 struct pci_dev *dev = NULL;
523 int idx, disabled;
524 u16 command;
525 struct resource *r;
526
527 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
528 pci_read_config_word(dev, PCI_COMMAND, &command);
529 for (idx = 0; idx < 6; idx++) {
530 r = &dev->resource[idx];
531 if (r->parent)
532 continue;
533 if (!r->flags || (r->flags & IORESOURCE_UNSET))
534 continue;
535 if (r->flags & IORESOURCE_IO)
536 disabled = !(command & PCI_COMMAND_IO);
537 else
538 disabled = !(command & PCI_COMMAND_MEMORY);
539 if (pass == disabled)
540 alloc_resource(dev, idx);
541 }
542 if (pass)
543 continue;
544 r = &dev->resource[PCI_ROM_RESOURCE];
545 if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
546
547 u32 reg;
548 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
549 r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
550 pci_read_config_dword(dev, dev->rom_base_reg, ®);
551 pci_write_config_dword(dev, dev->rom_base_reg,
552 reg & ~PCI_ROM_ADDRESS_ENABLE);
553 }
554 }
555}
556
557static void __init
558pcibios_assign_resources(void)
559{
560 struct pci_dev *dev = NULL;
561 int idx;
562 struct resource *r;
563
564 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
565 int class = dev->class >> 8;
566
567
568 if (!class || class == PCI_CLASS_BRIDGE_HOST)
569 continue;
570
571 for (idx = 0; idx < 6; idx++) {
572 r = &dev->resource[idx];
573
574
575
576
577
578
579
580 if ((r->flags & IORESOURCE_UNSET) && r->end &&
581 (!ppc_md.pcibios_enable_device_hook ||
582 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
583 r->flags &= ~IORESOURCE_UNSET;
584 pci_assign_resource(dev, idx);
585 }
586 }
587
588#if 0
589 r = &dev->resource[PCI_ROM_RESOURCE];
590 r->end -= r->start;
591 r->start = 0;
592 if (r->end)
593 pci_assign_resource(dev, PCI_ROM_RESOURCE);
594#endif
595 }
596}
597
598
599int
600pcibios_enable_resources(struct pci_dev *dev, int mask)
601{
602 u16 cmd, old_cmd;
603 int idx;
604 struct resource *r;
605
606 pci_read_config_word(dev, PCI_COMMAND, &cmd);
607 old_cmd = cmd;
608 for (idx=0; idx<6; idx++) {
609
610 if (!(mask & (1<<idx)))
611 continue;
612
613 r = &dev->resource[idx];
614 if (r->flags & IORESOURCE_UNSET) {
615 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
616 return -EINVAL;
617 }
618 if (r->flags & IORESOURCE_IO)
619 cmd |= PCI_COMMAND_IO;
620 if (r->flags & IORESOURCE_MEM)
621 cmd |= PCI_COMMAND_MEMORY;
622 }
623 if (dev->resource[PCI_ROM_RESOURCE].start)
624 cmd |= PCI_COMMAND_MEMORY;
625 if (cmd != old_cmd) {
626 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
627 pci_write_config_word(dev, PCI_COMMAND, cmd);
628 }
629 return 0;
630}
631
632static int next_controller_index;
633
634struct pci_controller * __init
635pcibios_alloc_controller(void)
636{
637 struct pci_controller *hose;
638
639 hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
640 memset(hose, 0, sizeof(struct pci_controller));
641
642 *hose_tail = hose;
643 hose_tail = &hose->next;
644
645 hose->index = next_controller_index++;
646
647 return hose;
648}
649
650#ifdef CONFIG_PPC_OF
651
652
653
654static void __openfirmware
655make_one_node_map(struct device_node* node, u8 pci_bus)
656{
657 int *bus_range;
658 int len;
659
660 if (pci_bus >= pci_bus_count)
661 return;
662 bus_range = (int *) get_property(node, "bus-range", &len);
663 if (bus_range == NULL || len < 2 * sizeof(int)) {
664 printk(KERN_WARNING "Can't get bus-range for %s, "
665 "assuming it starts at 0\n", node->full_name);
666 pci_to_OF_bus_map[pci_bus] = 0;
667 } else
668 pci_to_OF_bus_map[pci_bus] = bus_range[0];
669
670 for (node=node->child; node != 0;node = node->sibling) {
671 struct pci_dev* dev;
672 unsigned int *class_code, *reg;
673
674 class_code = (unsigned int *) get_property(node, "class-code", NULL);
675 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
676 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
677 continue;
678 reg = (unsigned int *)get_property(node, "reg", NULL);
679 if (!reg)
680 continue;
681 dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
682 if (!dev || !dev->subordinate)
683 continue;
684 make_one_node_map(node, dev->subordinate->number);
685 }
686}
687
688void __openfirmware
689pcibios_make_OF_bus_map(void)
690{
691 int i;
692 struct pci_controller* hose;
693 u8* of_prop_map;
694
695 pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
696 if (!pci_to_OF_bus_map) {
697 printk(KERN_ERR "Can't allocate OF bus map !\n");
698 return;
699 }
700
701
702
703
704 for (i=0; i<pci_bus_count; i++)
705 pci_to_OF_bus_map[i] = 0xff;
706
707
708 for(hose=hose_head; hose; hose=hose->next) {
709 struct device_node* node;
710 node = (struct device_node *)hose->arch_data;
711 if (!node)
712 continue;
713 make_one_node_map(node, hose->first_busno);
714 }
715 of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
716 if (of_prop_map)
717 memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
718#ifdef DEBUG
719 printk("PCI->OF bus map:\n");
720 for (i=0; i<pci_bus_count; i++) {
721 if (pci_to_OF_bus_map[i] == 0xff)
722 continue;
723 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
724 }
725#endif
726}
727
728typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
729
730static struct device_node* __openfirmware
731scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
732{
733 struct device_node* sub_node;
734
735 for (; node != 0;node = node->sibling) {
736 unsigned int *class_code;
737
738 if (filter(node, data))
739 return node;
740
741
742
743
744
745
746 class_code = (unsigned int *) get_property(node, "class-code", NULL);
747 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
748 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
749 strcmp(node->name, "multifunc-device"))
750 continue;
751 sub_node = scan_OF_pci_childs(node->child, filter, data);
752 if (sub_node)
753 return sub_node;
754 }
755 return NULL;
756}
757
758static int
759scan_OF_pci_childs_iterator(struct device_node* node, void* data)
760{
761 unsigned int *reg;
762 u8* fdata = (u8*)data;
763
764 reg = (unsigned int *) get_property(node, "reg", NULL);
765 if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
766 && ((reg[0] >> 16) & 0xff) == fdata[0])
767 return 1;
768 return 0;
769}
770
771static struct device_node* __openfirmware
772scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
773{
774 u8 filter_data[2] = {bus, dev_fn};
775
776 return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
777}
778
779
780
781
782struct device_node *
783pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
784{
785 struct pci_controller *hose;
786 struct device_node *node;
787 int busnr;
788
789 if (!have_of)
790 return NULL;
791
792
793 busnr = bus->number;
794 hose = pci_bus_to_hose(busnr);
795 if (!hose)
796 return NULL;
797
798
799 node = (struct device_node *) hose->arch_data;
800 if (!node)
801 return NULL;
802
803
804#ifdef CONFIG_PPC_PMAC
805
806
807
808
809
810
811 if (_machine == _MACH_Pmac && busnr >= 0xf0)
812 busnr -= 0xf0;
813 else
814#endif
815 if (pci_to_OF_bus_map)
816 busnr = pci_to_OF_bus_map[busnr];
817 if (busnr == 0xff)
818 return NULL;
819
820
821 return scan_OF_childs_for_device(node->child, busnr, devfn);
822}
823
824struct device_node*
825pci_device_to_OF_node(struct pci_dev *dev)
826{
827 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
828}
829
830
831
832
833
834
835
836
837struct pci_controller*
838pci_find_hose_for_OF_device(struct device_node* node)
839{
840 if (!have_of)
841 return NULL;
842 while(node) {
843 struct pci_controller* hose;
844 for (hose=hose_head;hose;hose=hose->next)
845 if (hose->arch_data == node)
846 return hose;
847 node=node->parent;
848 }
849 return NULL;
850}
851
852static int __openfirmware
853find_OF_pci_device_filter(struct device_node* node, void* data)
854{
855 return ((void *)node == data);
856}
857
858
859
860
861int
862pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
863{
864 unsigned int *reg;
865 struct pci_controller* hose;
866 struct pci_dev* dev = NULL;
867
868 if (!have_of)
869 return -ENODEV;
870
871 hose = pci_find_hose_for_OF_device(node);
872 if (!hose || !hose->arch_data)
873 return -ENODEV;
874 if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
875 find_OF_pci_device_filter, (void *)node))
876 return -ENODEV;
877 reg = (unsigned int *) get_property(node, "reg", NULL);
878 if (!reg)
879 return -ENODEV;
880 *bus = (reg[0] >> 16) & 0xff;
881 *devfn = ((reg[0] >> 8) & 0xff);
882
883
884
885
886
887
888 if (!pci_to_OF_bus_map)
889 return 0;
890 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
891 if (pci_to_OF_bus_map[dev->bus->number] != *bus)
892 continue;
893 if (dev->devfn != *devfn)
894 continue;
895 *bus = dev->bus->number;
896 return 0;
897 }
898 return -ENODEV;
899}
900
901void __init
902pci_process_bridge_OF_ranges(struct pci_controller *hose,
903 struct device_node *dev, int primary)
904{
905 static unsigned int static_lc_ranges[256] __initdata;
906 unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
907 unsigned int size;
908 int rlen = 0, orig_rlen;
909 int memno = 0;
910 struct resource *res;
911 int np, na = prom_n_addr_cells(dev);
912 np = na + 5;
913
914
915
916
917
918 dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
919 if (!dt_ranges)
920 return;
921
922 if (rlen > sizeof(static_lc_ranges)) {
923 printk(KERN_WARNING "OF ranges property too large !\n");
924 rlen = sizeof(static_lc_ranges);
925 }
926 lc_ranges = static_lc_ranges;
927 memcpy(lc_ranges, dt_ranges, rlen);
928 orig_rlen = rlen;
929
930
931
932
933 ranges = lc_ranges;
934 prev = NULL;
935 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
936 if (prev) {
937 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
938 (prev[2] + prev[na+4]) == ranges[2] &&
939 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
940 prev[na+4] += ranges[na+4];
941 ranges[0] = 0;
942 ranges += np;
943 continue;
944 }
945 }
946 prev = ranges;
947 ranges += np;
948 }
949
950
951
952
953
954
955
956
957
958 ranges = lc_ranges;
959 rlen = orig_rlen;
960 while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
961 res = NULL;
962 size = ranges[na+4];
963 switch (ranges[0] >> 24) {
964 case 1:
965 if (ranges[2] != 0)
966 break;
967 hose->io_base_phys = ranges[na+2];
968
969 if (size > 0x01000000)
970 size = 0x01000000;
971 hose->io_base_virt = ioremap(ranges[na+2], size);
972 if (primary)
973 isa_io_base = (unsigned long) hose->io_base_virt;
974 res = &hose->io_resource;
975 res->flags = IORESOURCE_IO;
976 res->start = ranges[2];
977 break;
978 case 2:
979 memno = 0;
980 if (ranges[1] == 0 && ranges[2] == 0
981 && ranges[na+4] <= (16 << 20)) {
982
983 if (primary)
984 isa_mem_base = ranges[na+2];
985 memno = 1;
986 }
987 while (memno < 3 && hose->mem_resources[memno].flags)
988 ++memno;
989 if (memno == 0)
990 hose->pci_mem_offset = ranges[na+2] - ranges[2];
991 if (memno < 3) {
992 res = &hose->mem_resources[memno];
993 res->flags = IORESOURCE_MEM;
994 res->start = ranges[na+2];
995 }
996 break;
997 }
998 if (res != NULL) {
999 res->name = dev->full_name;
1000 res->end = res->start + size - 1;
1001 res->parent = NULL;
1002 res->sibling = NULL;
1003 res->child = NULL;
1004 }
1005 ranges += np;
1006 }
1007}
1008
1009
1010
1011
1012void __init
1013pci_create_OF_bus_map(void)
1014{
1015 struct property* of_prop;
1016
1017 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
1018 if (of_prop && find_path_device("/")) {
1019 memset(of_prop, -1, sizeof(struct property) + 256);
1020 of_prop->name = "pci-OF-bus-map";
1021 of_prop->length = 256;
1022 of_prop->value = (unsigned char *)&of_prop[1];
1023 prom_add_property(find_path_device("/"), of_prop);
1024 }
1025}
1026
1027static ssize_t pci_show_devspec(struct device *dev, char *buf)
1028{
1029 struct pci_dev *pdev;
1030 struct device_node *np;
1031
1032 pdev = to_pci_dev (dev);
1033 np = pci_device_to_OF_node(pdev);
1034 if (np == NULL || np->full_name == NULL)
1035 return 0;
1036 return sprintf(buf, "%s", np->full_name);
1037}
1038static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
1039
1040#endif
1041
1042
1043void pcibios_add_platform_entries(struct pci_dev *pdev)
1044{
1045#ifdef CONFIG_PPC_OF
1046 device_create_file(&pdev->dev, &dev_attr_devspec);
1047#endif
1048}
1049
1050
1051#ifdef CONFIG_PPC_PMAC
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069static void __init
1070do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
1071{
1072 struct pci_dev *bridge = bus->self;
1073 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
1074 u32 l;
1075 u16 w;
1076 struct resource res;
1077
1078 if (bus->resource[0] == NULL)
1079 return;
1080 res = *(bus->resource[0]);
1081
1082 DBG("Remapping Bus %d, bridge: %s\n", bus->number, bridge->slot_name);
1083 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1084 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
1085 DBG(" IO window: %08lx-%08lx\n", res.start, res.end);
1086
1087
1088 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1089 l &= 0xffff000f;
1090 l |= (res.start >> 8) & 0x00f0;
1091 l |= res.end & 0xf000;
1092 pci_write_config_dword(bridge, PCI_IO_BASE, l);
1093
1094 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1095 l = (res.start >> 16) | (res.end & 0xffff0000);
1096 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1097 }
1098
1099 pci_read_config_word(bridge, PCI_COMMAND, &w);
1100 w |= PCI_COMMAND_IO;
1101 pci_write_config_word(bridge, PCI_COMMAND, w);
1102
1103#if 0
1104 if (enable_vga) {
1105 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1106 w |= PCI_BRIDGE_CTL_VGA;
1107 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1108 }
1109#endif
1110}
1111
1112
1113
1114
1115
1116
1117static int __init
1118check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1119{
1120 struct list_head *ln;
1121 int i;
1122 int rc = 0;
1123
1124#define push_end(res, size) do { unsigned long __sz = (size) ; \
1125 res->end = ((res->end + __sz) / (__sz + 1)) * (__sz + 1) + __sz; \
1126 } while (0)
1127
1128 for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
1129 struct pci_dev *dev = pci_dev_b(ln);
1130 u16 class = dev->class >> 8;
1131
1132 if (class == PCI_CLASS_DISPLAY_VGA ||
1133 class == PCI_CLASS_NOT_DEFINED_VGA)
1134 *found_vga = 1;
1135 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1136 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1137 if (class == PCI_CLASS_BRIDGE_CARDBUS)
1138 push_end(res, 0xfff);
1139
1140 for (i=0; i<PCI_NUM_RESOURCES; i++) {
1141 struct resource *r;
1142 unsigned long r_size;
1143
1144 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1145 && i >= PCI_BRIDGE_RESOURCES)
1146 continue;
1147 r = &dev->resource[i];
1148 r_size = r->end - r->start;
1149 if (r_size < 0xfff)
1150 r_size = 0xfff;
1151 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1152 rc = 1;
1153 push_end(res, r_size);
1154 }
1155 }
1156 }
1157
1158 return rc;
1159}
1160
1161
1162
1163
1164
1165
1166
1167static void __init
1168do_fixup_p2p_level(struct pci_bus *bus)
1169{
1170 struct list_head *ln;
1171 int i, parent_io;
1172 int has_vga = 0;
1173
1174 for (parent_io=0; parent_io<4; parent_io++)
1175 if (bus->resource[parent_io]
1176 && bus->resource[parent_io]->flags & IORESOURCE_IO)
1177 break;
1178 if (parent_io >= 4)
1179 return;
1180
1181 for (ln=bus->children.next; ln != &bus->children; ln=ln->next) {
1182 struct pci_bus *b = pci_bus_b(ln);
1183 struct pci_dev *d = b->self;
1184 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1185 struct resource *res = b->resource[0];
1186 struct resource tmp_res;
1187 unsigned long max;
1188 int found_vga = 0;
1189
1190 memset(&tmp_res, 0, sizeof(tmp_res));
1191 tmp_res.start = bus->resource[parent_io]->start;
1192
1193
1194
1195
1196 if (tmp_res.start == 0)
1197 tmp_res.start = 0x1000;
1198
1199 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1200 res != bus->resource[parent_io] &&
1201 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1202 check_for_io_childs(b, &tmp_res, &found_vga)) {
1203 u8 io_base_lo;
1204
1205 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1206
1207 if (found_vga) {
1208 if (has_vga) {
1209 printk(KERN_WARNING "Skipping VGA, already active"
1210 " on bus segment\n");
1211 found_vga = 0;
1212 } else
1213 has_vga = 1;
1214 }
1215 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1216
1217 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1218 max = ((unsigned long) hose->io_base_virt
1219 - isa_io_base) + 0xffffffff;
1220 else
1221 max = ((unsigned long) hose->io_base_virt
1222 - isa_io_base) + 0xffff;
1223
1224 *res = tmp_res;
1225 res->flags = IORESOURCE_IO;
1226 res->name = b->name;
1227
1228
1229 for (i = 0 ; i < 4; i++) {
1230 struct resource *r = bus->resource[i];
1231 if (!r)
1232 continue;
1233 if ((r->flags & IORESOURCE_IO) == 0)
1234 continue;
1235 DBG("Trying to allocate from %08lx, size %08lx from parent"
1236 " res %d: %08lx -> %08lx\n",
1237 res->start, res->end, i, r->start, r->end);
1238
1239 if (allocate_resource(r, res, res->end + 1, res->start, max,
1240 res->end + 1, NULL, NULL) < 0) {
1241 DBG("Failed !\n");
1242 continue;
1243 }
1244 do_update_p2p_io_resource(b, found_vga);
1245 break;
1246 }
1247 }
1248 do_fixup_p2p_level(b);
1249 }
1250}
1251
1252static void
1253pcibios_fixup_p2p_bridges(void)
1254{
1255 struct list_head *ln;
1256
1257 for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next) {
1258 struct pci_bus *b = pci_bus_b(ln);
1259 do_fixup_p2p_level(b);
1260 }
1261}
1262
1263#endif
1264
1265static int __init
1266pcibios_init(void)
1267{
1268 struct pci_controller *hose;
1269 struct pci_bus *bus;
1270 int next_busno;
1271
1272 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1273
1274
1275 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
1276 if (pci_assign_all_busses)
1277 hose->first_busno = next_busno;
1278 hose->last_busno = 0xff;
1279 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
1280 hose->last_busno = bus->subordinate;
1281 if (pci_assign_all_busses || next_busno <= hose->last_busno)
1282 next_busno = hose->last_busno+1;
1283 }
1284 pci_bus_count = next_busno;
1285
1286
1287
1288
1289
1290 if (pci_assign_all_busses && have_of)
1291 pcibios_make_OF_bus_map();
1292
1293
1294 if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
1295 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
1296
1297
1298 if (ppc_md.pcibios_fixup)
1299 ppc_md.pcibios_fixup();
1300
1301
1302 pcibios_allocate_bus_resources(&pci_root_buses);
1303 pcibios_allocate_resources(0);
1304 pcibios_allocate_resources(1);
1305#ifdef CONFIG_PPC_PMAC
1306 pcibios_fixup_p2p_bridges();
1307#endif
1308 pcibios_assign_resources();
1309
1310
1311 if (ppc_md.pcibios_after_init)
1312 ppc_md.pcibios_after_init();
1313
1314 return 0;
1315}
1316
1317subsys_initcall(pcibios_init);
1318
1319unsigned char __init
1320common_swizzle(struct pci_dev *dev, unsigned char *pinp)
1321{
1322 struct pci_controller *hose = dev->sysdata;
1323
1324 if (dev->bus->number != hose->first_busno) {
1325 u8 pin = *pinp;
1326 do {
1327 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
1328
1329 dev = dev->bus->self;
1330 } while (dev->bus->self);
1331 *pinp = pin;
1332
1333
1334 }
1335 return PCI_SLOT(dev->devfn);
1336}
1337
1338unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
1339 unsigned long start, unsigned long size)
1340{
1341 return start;
1342}
1343
1344void __init pcibios_fixup_bus(struct pci_bus *bus)
1345{
1346 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1347 unsigned long io_offset;
1348 struct resource *res;
1349 int i;
1350
1351 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1352 if (bus->parent == NULL) {
1353
1354 hose->bus = bus;
1355
1356 bus->resource[0] = res = &hose->io_resource;
1357 if (!res->flags) {
1358 if (io_offset)
1359 printk(KERN_ERR "I/O resource not set for host"
1360 " bridge %d\n", hose->index);
1361 res->start = 0;
1362 res->end = IO_SPACE_LIMIT;
1363 res->flags = IORESOURCE_IO;
1364 }
1365 res->start += io_offset;
1366 res->end += io_offset;
1367
1368 for (i = 0; i < 3; ++i) {
1369 res = &hose->mem_resources[i];
1370 if (!res->flags) {
1371 if (i > 0)
1372 continue;
1373 printk(KERN_ERR "Memory resource not set for "
1374 "host bridge %d\n", hose->index);
1375 res->start = hose->pci_mem_offset;
1376 res->end = ~0U;
1377 res->flags = IORESOURCE_MEM;
1378 }
1379 bus->resource[i+1] = res;
1380 }
1381 } else {
1382
1383 pci_read_bridge_bases(bus);
1384
1385 for (i = 0; i < 4; ++i) {
1386 if ((res = bus->resource[i]) == NULL)
1387 continue;
1388 if (!res->flags)
1389 continue;
1390 if (io_offset && (res->flags & IORESOURCE_IO)) {
1391 res->start += io_offset;
1392 res->end += io_offset;
1393 } else if (hose->pci_mem_offset
1394 && (res->flags & IORESOURCE_MEM)) {
1395 res->start += hose->pci_mem_offset;
1396 res->end += hose->pci_mem_offset;
1397 }
1398 }
1399 }
1400
1401 if (ppc_md.pcibios_fixup_bus)
1402 ppc_md.pcibios_fixup_bus(bus);
1403}
1404
1405char __init *pcibios_setup(char *str)
1406{
1407 return str;
1408}
1409
1410
1411void __init
1412pcibios_update_irq(struct pci_dev *dev, int irq)
1413{
1414 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1415
1416}
1417
1418int pcibios_enable_device(struct pci_dev *dev, int mask)
1419{
1420 u16 cmd, old_cmd;
1421 int idx;
1422 struct resource *r;
1423
1424 if (ppc_md.pcibios_enable_device_hook)
1425 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1426 return -EINVAL;
1427
1428 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1429 old_cmd = cmd;
1430 for (idx=0; idx<6; idx++) {
1431 r = &dev->resource[idx];
1432 if (r->flags & IORESOURCE_UNSET) {
1433 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1434 return -EINVAL;
1435 }
1436 if (r->flags & IORESOURCE_IO)
1437 cmd |= PCI_COMMAND_IO;
1438 if (r->flags & IORESOURCE_MEM)
1439 cmd |= PCI_COMMAND_MEMORY;
1440 }
1441 if (cmd != old_cmd) {
1442 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1443 pci_name(dev), old_cmd, cmd);
1444 pci_write_config_word(dev, PCI_COMMAND, cmd);
1445 }
1446 return 0;
1447}
1448
1449struct pci_controller*
1450pci_bus_to_hose(int bus)
1451{
1452 struct pci_controller* hose = hose_head;
1453
1454 for (; hose; hose = hose->next)
1455 if (bus >= hose->first_busno && bus <= hose->last_busno)
1456 return hose;
1457 return NULL;
1458}
1459
1460void*
1461pci_bus_io_base(unsigned int bus)
1462{
1463 struct pci_controller *hose;
1464
1465 hose = pci_bus_to_hose(bus);
1466 if (!hose)
1467 return NULL;
1468 return hose->io_base_virt;
1469}
1470
1471unsigned long
1472pci_bus_io_base_phys(unsigned int bus)
1473{
1474 struct pci_controller *hose;
1475
1476 hose = pci_bus_to_hose(bus);
1477 if (!hose)
1478 return 0;
1479 return hose->io_base_phys;
1480}
1481
1482unsigned long
1483pci_bus_mem_base_phys(unsigned int bus)
1484{
1485 struct pci_controller *hose;
1486
1487 hose = pci_bus_to_hose(bus);
1488 if (!hose)
1489 return 0;
1490 return hose->pci_mem_offset;
1491}
1492
1493unsigned long
1494pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
1495{
1496
1497
1498 struct pci_controller* hose =
1499 (struct pci_controller *)pdev->sysdata;
1500 if (hose && res->flags & IORESOURCE_MEM)
1501 return res->start - hose->pci_mem_offset;
1502
1503 return res->start;
1504}
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523static __inline__ int
1524__pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
1525 enum pci_mmap_state mmap_state)
1526{
1527 struct pci_controller *hose = (struct pci_controller *) dev->sysdata;
1528 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1529 unsigned long size = vma->vm_end - vma->vm_start;
1530 unsigned long base;
1531 struct resource *res;
1532 int i;
1533 int ret = -EINVAL;
1534
1535 if (hose == 0)
1536 return -EINVAL;
1537 if (offset + size <= offset)
1538 return -EINVAL;
1539
1540 if (mmap_state == pci_mmap_mem) {
1541
1542 base = hose->pci_mem_offset;
1543 for (i = 0; i < 3; ++i) {
1544 res = &hose->mem_resources[i];
1545 if (res->flags == 0)
1546 continue;
1547 if (offset >= res->start - base
1548 && offset + size - 1 <= res->end - base) {
1549 ret = 0;
1550 break;
1551 }
1552 }
1553 offset += hose->pci_mem_offset;
1554 } else {
1555
1556 base = (unsigned long)hose->io_base_virt - isa_io_base;
1557 res = &hose->io_resource;
1558 if (offset >= res->start - base
1559 && offset + size - 1 <= res->end - base)
1560 ret = 0;
1561 offset += hose->io_base_phys;
1562 }
1563
1564 vma->vm_pgoff = offset >> PAGE_SHIFT;
1565 return ret;
1566}
1567
1568
1569
1570
1571
1572static __inline__ void
1573__pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1574 enum pci_mmap_state mmap_state)
1575{
1576 vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
1577}
1578
1579
1580
1581
1582
1583static __inline__ void
1584__pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1585 enum pci_mmap_state mmap_state, int write_combine)
1586{
1587 int prot = pgprot_val(vma->vm_page_prot);
1588
1589
1590 prot |= _PAGE_NO_CACHE;
1591 if (!write_combine)
1592 prot |= _PAGE_GUARDED;
1593 vma->vm_page_prot = __pgprot(prot);
1594}
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1607 enum pci_mmap_state mmap_state,
1608 int write_combine)
1609{
1610 int ret;
1611
1612 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1613 if (ret < 0)
1614 return ret;
1615
1616 __pci_mmap_set_flags(dev, vma, mmap_state);
1617 __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
1618
1619 ret = remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
1620 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1621
1622 return ret;
1623}
1624
1625
1626
1627
1628unsigned long
1629phys_to_bus(unsigned long pa)
1630{
1631 struct pci_controller *hose;
1632 int i;
1633
1634 for (hose = hose_head; hose; hose = hose->next) {
1635 for (i = 0; i < 3; ++i) {
1636 if (pa >= hose->mem_resources[i].start
1637 && pa <= hose->mem_resources[i].end) {
1638
1639
1640
1641
1642
1643
1644 if (i == 0)
1645 pa -= hose->pci_mem_offset;
1646 return pa;
1647 }
1648 }
1649 }
1650
1651 return 0;
1652}
1653
1654unsigned long
1655pci_phys_to_bus(unsigned long pa, int busnr)
1656{
1657 struct pci_controller* hose = pci_bus_to_hose(busnr);
1658 if (!hose)
1659 return pa;
1660 return pa - hose->pci_mem_offset;
1661}
1662
1663unsigned long
1664pci_bus_to_phys(unsigned int ba, int busnr)
1665{
1666 struct pci_controller* hose = pci_bus_to_hose(busnr);
1667 if (!hose)
1668 return ba;
1669 return ba + hose->pci_mem_offset;
1670}
1671
1672
1673
1674
1675
1676
1677
1678long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1679{
1680 struct pci_controller* hose;
1681 long result = -EOPNOTSUPP;
1682
1683
1684
1685
1686
1687
1688#ifdef CONFIG_PPC_PMAC
1689 if (_machine == _MACH_Pmac && machine_is_compatible("MacRISC4"))
1690 if (bus == 0)
1691 bus = 0xf0;
1692#endif
1693
1694 hose = pci_bus_to_hose(bus);
1695 if (!hose)
1696 return -ENODEV;
1697
1698 switch (which) {
1699 case IOBASE_BRIDGE_NUMBER:
1700 return (long)hose->first_busno;
1701 case IOBASE_MEMORY:
1702 return (long)hose->pci_mem_offset;
1703 case IOBASE_IO:
1704 return (long)hose->io_base_phys;
1705 case IOBASE_ISA_IO:
1706 return (long)isa_io_base;
1707 case IOBASE_ISA_MEM:
1708 return (long)isa_mem_base;
1709 }
1710
1711 return result;
1712}
1713
1714void __init
1715pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1716 int flags, char *name)
1717{
1718 res->start = start;
1719 res->end = end;
1720 res->flags = flags;
1721 res->name = name;
1722 res->parent = NULL;
1723 res->sibling = NULL;
1724 res->child = NULL;
1725}
1726
1727
1728
1729
1730
1731#define NULL_PCI_OP(rw, size, type) \
1732static int \
1733null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1734{ \
1735 return PCIBIOS_DEVICE_NOT_FOUND; \
1736}
1737
1738static int
1739null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1740 int len, u32 *val)
1741{
1742 return PCIBIOS_DEVICE_NOT_FOUND;
1743}
1744
1745static int
1746null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1747 int len, u32 val)
1748{
1749 return PCIBIOS_DEVICE_NOT_FOUND;
1750}
1751
1752static struct pci_ops null_pci_ops =
1753{
1754 null_read_config,
1755 null_write_config
1756};
1757
1758
1759
1760
1761
1762static struct pci_bus *
1763fake_pci_bus(struct pci_controller *hose, int busnr)
1764{
1765 static struct pci_bus bus;
1766
1767 if (hose == 0) {
1768 hose = pci_bus_to_hose(busnr);
1769 if (hose == 0)
1770 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1771 }
1772 bus.number = busnr;
1773 bus.sysdata = hose;
1774 bus.ops = hose? hose->ops: &null_pci_ops;
1775 return &bus;
1776}
1777
1778#define EARLY_PCI_OP(rw, size, type) \
1779int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1780 int devfn, int offset, type value) \
1781{ \
1782 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1783 devfn, offset, value); \
1784}
1785
1786EARLY_PCI_OP(read, byte, u8 *)
1787EARLY_PCI_OP(read, word, u16 *)
1788EARLY_PCI_OP(read, dword, u32 *)
1789EARLY_PCI_OP(write, byte, u8)
1790EARLY_PCI_OP(write, word, u16)
1791EARLY_PCI_OP(write, dword, u32)
1792