[SPARC64]: Use in-kernel OBP device tree for PCI controller probing.
[deliverable/linux.git] / arch / sparc64 / kernel / pci.c
1 /* $Id: pci.c,v 1.39 2002/01/05 01:13:43 davem Exp $
2 * pci.c: UltraSparc PCI controller support.
3 *
4 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
5 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
7 */
8
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/sched.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/smp_lock.h>
17 #include <linux/init.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/pbm.h>
21 #include <asm/pgtable.h>
22 #include <asm/irq.h>
23 #include <asm/ebus.h>
24 #include <asm/isa.h>
25 #include <asm/prom.h>
26
27 unsigned long pci_memspace_mask = 0xffffffffUL;
28
29 #ifndef CONFIG_PCI
30 /* A "nop" PCI implementation. */
31 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
32 unsigned long off, unsigned long len,
33 unsigned char *buf)
34 {
35 return 0;
36 }
37 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
38 unsigned long off, unsigned long len,
39 unsigned char *buf)
40 {
41 return 0;
42 }
43 #else
44
45 /* List of all PCI controllers found in the system. */
46 struct pci_controller_info *pci_controller_root = NULL;
47
48 /* Each PCI controller found gets a unique index. */
49 int pci_num_controllers = 0;
50
51 volatile int pci_poke_in_progress;
52 volatile int pci_poke_cpu = -1;
53 volatile int pci_poke_faulted;
54
55 static DEFINE_SPINLOCK(pci_poke_lock);
56
57 void pci_config_read8(u8 *addr, u8 *ret)
58 {
59 unsigned long flags;
60 u8 byte;
61
62 spin_lock_irqsave(&pci_poke_lock, flags);
63 pci_poke_cpu = smp_processor_id();
64 pci_poke_in_progress = 1;
65 pci_poke_faulted = 0;
66 __asm__ __volatile__("membar #Sync\n\t"
67 "lduba [%1] %2, %0\n\t"
68 "membar #Sync"
69 : "=r" (byte)
70 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
71 : "memory");
72 pci_poke_in_progress = 0;
73 pci_poke_cpu = -1;
74 if (!pci_poke_faulted)
75 *ret = byte;
76 spin_unlock_irqrestore(&pci_poke_lock, flags);
77 }
78
79 void pci_config_read16(u16 *addr, u16 *ret)
80 {
81 unsigned long flags;
82 u16 word;
83
84 spin_lock_irqsave(&pci_poke_lock, flags);
85 pci_poke_cpu = smp_processor_id();
86 pci_poke_in_progress = 1;
87 pci_poke_faulted = 0;
88 __asm__ __volatile__("membar #Sync\n\t"
89 "lduha [%1] %2, %0\n\t"
90 "membar #Sync"
91 : "=r" (word)
92 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
93 : "memory");
94 pci_poke_in_progress = 0;
95 pci_poke_cpu = -1;
96 if (!pci_poke_faulted)
97 *ret = word;
98 spin_unlock_irqrestore(&pci_poke_lock, flags);
99 }
100
101 void pci_config_read32(u32 *addr, u32 *ret)
102 {
103 unsigned long flags;
104 u32 dword;
105
106 spin_lock_irqsave(&pci_poke_lock, flags);
107 pci_poke_cpu = smp_processor_id();
108 pci_poke_in_progress = 1;
109 pci_poke_faulted = 0;
110 __asm__ __volatile__("membar #Sync\n\t"
111 "lduwa [%1] %2, %0\n\t"
112 "membar #Sync"
113 : "=r" (dword)
114 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
115 : "memory");
116 pci_poke_in_progress = 0;
117 pci_poke_cpu = -1;
118 if (!pci_poke_faulted)
119 *ret = dword;
120 spin_unlock_irqrestore(&pci_poke_lock, flags);
121 }
122
123 void pci_config_write8(u8 *addr, u8 val)
124 {
125 unsigned long flags;
126
127 spin_lock_irqsave(&pci_poke_lock, flags);
128 pci_poke_cpu = smp_processor_id();
129 pci_poke_in_progress = 1;
130 pci_poke_faulted = 0;
131 __asm__ __volatile__("membar #Sync\n\t"
132 "stba %0, [%1] %2\n\t"
133 "membar #Sync"
134 : /* no outputs */
135 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
136 : "memory");
137 pci_poke_in_progress = 0;
138 pci_poke_cpu = -1;
139 spin_unlock_irqrestore(&pci_poke_lock, flags);
140 }
141
142 void pci_config_write16(u16 *addr, u16 val)
143 {
144 unsigned long flags;
145
146 spin_lock_irqsave(&pci_poke_lock, flags);
147 pci_poke_cpu = smp_processor_id();
148 pci_poke_in_progress = 1;
149 pci_poke_faulted = 0;
150 __asm__ __volatile__("membar #Sync\n\t"
151 "stha %0, [%1] %2\n\t"
152 "membar #Sync"
153 : /* no outputs */
154 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
155 : "memory");
156 pci_poke_in_progress = 0;
157 pci_poke_cpu = -1;
158 spin_unlock_irqrestore(&pci_poke_lock, flags);
159 }
160
161 void pci_config_write32(u32 *addr, u32 val)
162 {
163 unsigned long flags;
164
165 spin_lock_irqsave(&pci_poke_lock, flags);
166 pci_poke_cpu = smp_processor_id();
167 pci_poke_in_progress = 1;
168 pci_poke_faulted = 0;
169 __asm__ __volatile__("membar #Sync\n\t"
170 "stwa %0, [%1] %2\n\t"
171 "membar #Sync"
172 : /* no outputs */
173 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
174 : "memory");
175 pci_poke_in_progress = 0;
176 pci_poke_cpu = -1;
177 spin_unlock_irqrestore(&pci_poke_lock, flags);
178 }
179
180 /* Probe for all PCI controllers in the system. */
181 extern void sabre_init(struct device_node *, const char *);
182 extern void psycho_init(struct device_node *, const char *);
183 extern void schizo_init(struct device_node *, const char *);
184 extern void schizo_plus_init(struct device_node *, const char *);
185 extern void tomatillo_init(struct device_node *, const char *);
186 extern void sun4v_pci_init(struct device_node *, const char *);
187
188 static struct {
189 char *model_name;
190 void (*init)(struct device_node *, const char *);
191 } pci_controller_table[] __initdata = {
192 { "SUNW,sabre", sabre_init },
193 { "pci108e,a000", sabre_init },
194 { "pci108e,a001", sabre_init },
195 { "SUNW,psycho", psycho_init },
196 { "pci108e,8000", psycho_init },
197 { "SUNW,schizo", schizo_init },
198 { "pci108e,8001", schizo_init },
199 { "SUNW,schizo+", schizo_plus_init },
200 { "pci108e,8002", schizo_plus_init },
201 { "SUNW,tomatillo", tomatillo_init },
202 { "pci108e,a801", tomatillo_init },
203 { "SUNW,sun4v-pci", sun4v_pci_init },
204 };
205 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
206 sizeof(pci_controller_table[0]))
207
208 static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
209 {
210 int i;
211
212 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
213 if (!strncmp(model_name,
214 pci_controller_table[i].model_name,
215 namelen)) {
216 pci_controller_table[i].init(dp, model_name);
217 return 1;
218 }
219 }
220
221 return 0;
222 }
223
224 static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
225 {
226 int i;
227
228 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
229 if (!strncmp(model_name,
230 pci_controller_table[i].model_name,
231 namelen)) {
232 return 1;
233 }
234 }
235 return 0;
236 }
237
238 static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
239 {
240 struct device_node *dp;
241 int count = 0;
242
243 for_each_node_by_name(dp, "pci") {
244 struct property *prop;
245 int len;
246
247 prop = of_find_property(dp, "model", &len);
248 if (!prop)
249 prop = of_find_property(dp, "compatible", &len);
250
251 if (prop) {
252 const char *model = prop->value;
253 int item_len = 0;
254
255 /* Our value may be a multi-valued string in the
256 * case of some compatible properties. For sanity,
257 * only try the first one.
258 */
259 while (model[item_len] && len) {
260 len--;
261 item_len++;
262 }
263
264 if (handler(model, item_len, dp))
265 count++;
266 }
267 }
268
269 return count;
270 }
271
272
273 /* Is there some PCI controller in the system? */
274 int __init pcic_present(void)
275 {
276 return pci_controller_scan(pci_is_controller);
277 }
278
279 struct pci_iommu_ops *pci_iommu_ops;
280 EXPORT_SYMBOL(pci_iommu_ops);
281
282 extern struct pci_iommu_ops pci_sun4u_iommu_ops,
283 pci_sun4v_iommu_ops;
284
285 /* Find each controller in the system, attach and initialize
286 * software state structure for each and link into the
287 * pci_controller_root. Setup the controller enough such
288 * that bus scanning can be done.
289 */
290 static void __init pci_controller_probe(void)
291 {
292 if (tlb_type == hypervisor)
293 pci_iommu_ops = &pci_sun4v_iommu_ops;
294 else
295 pci_iommu_ops = &pci_sun4u_iommu_ops;
296
297 printk("PCI: Probing for controllers.\n");
298
299 pci_controller_scan(pci_controller_init);
300 }
301
302 static void __init pci_scan_each_controller_bus(void)
303 {
304 struct pci_controller_info *p;
305
306 for (p = pci_controller_root; p; p = p->next)
307 p->scan_bus(p);
308 }
309
310 extern void clock_probe(void);
311 extern void power_init(void);
312
313 static int __init pcibios_init(void)
314 {
315 pci_controller_probe();
316 if (pci_controller_root == NULL)
317 return 0;
318
319 pci_scan_each_controller_bus();
320
321 isa_init();
322 ebus_init();
323 clock_probe();
324 power_init();
325
326 return 0;
327 }
328
329 subsys_initcall(pcibios_init);
330
331 void pcibios_fixup_bus(struct pci_bus *pbus)
332 {
333 struct pci_pbm_info *pbm = pbus->sysdata;
334
335 /* Generic PCI bus probing sets these to point at
336 * &io{port,mem}_resouce which is wrong for us.
337 */
338 pbus->resource[0] = &pbm->io_space;
339 pbus->resource[1] = &pbm->mem_space;
340 }
341
342 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
343 {
344 struct pci_pbm_info *pbm = pdev->bus->sysdata;
345 struct resource *root = NULL;
346
347 if (r->flags & IORESOURCE_IO)
348 root = &pbm->io_space;
349 if (r->flags & IORESOURCE_MEM)
350 root = &pbm->mem_space;
351
352 return root;
353 }
354
355 void pcibios_update_irq(struct pci_dev *pdev, int irq)
356 {
357 }
358
359 void pcibios_align_resource(void *data, struct resource *res,
360 unsigned long size, unsigned long align)
361 {
362 }
363
364 int pcibios_enable_device(struct pci_dev *pdev, int mask)
365 {
366 return 0;
367 }
368
369 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
370 struct resource *res)
371 {
372 struct pci_pbm_info *pbm = pdev->bus->sysdata;
373 struct resource zero_res, *root;
374
375 zero_res.start = 0;
376 zero_res.end = 0;
377 zero_res.flags = res->flags;
378
379 if (res->flags & IORESOURCE_IO)
380 root = &pbm->io_space;
381 else
382 root = &pbm->mem_space;
383
384 pbm->parent->resource_adjust(pdev, &zero_res, root);
385
386 region->start = res->start - zero_res.start;
387 region->end = res->end - zero_res.start;
388 }
389 EXPORT_SYMBOL(pcibios_resource_to_bus);
390
391 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
392 struct pci_bus_region *region)
393 {
394 struct pci_pbm_info *pbm = pdev->bus->sysdata;
395 struct resource *root;
396
397 res->start = region->start;
398 res->end = region->end;
399
400 if (res->flags & IORESOURCE_IO)
401 root = &pbm->io_space;
402 else
403 root = &pbm->mem_space;
404
405 pbm->parent->resource_adjust(pdev, res, root);
406 }
407 EXPORT_SYMBOL(pcibios_bus_to_resource);
408
409 char * __init pcibios_setup(char *str)
410 {
411 return str;
412 }
413
414 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
415
416 /* If the user uses a host-bridge as the PCI device, he may use
417 * this to perform a raw mmap() of the I/O or MEM space behind
418 * that controller.
419 *
420 * This can be useful for execution of x86 PCI bios initialization code
421 * on a PCI card, like the xfree86 int10 stuff does.
422 */
423 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
424 enum pci_mmap_state mmap_state)
425 {
426 struct pcidev_cookie *pcp = pdev->sysdata;
427 struct pci_pbm_info *pbm;
428 struct pci_controller_info *p;
429 unsigned long space_size, user_offset, user_size;
430
431 if (!pcp)
432 return -ENXIO;
433 pbm = pcp->pbm;
434 if (!pbm)
435 return -ENXIO;
436
437 p = pbm->parent;
438 if (p->pbms_same_domain) {
439 unsigned long lowest, highest;
440
441 lowest = ~0UL; highest = 0UL;
442 if (mmap_state == pci_mmap_io) {
443 if (p->pbm_A.io_space.flags) {
444 lowest = p->pbm_A.io_space.start;
445 highest = p->pbm_A.io_space.end + 1;
446 }
447 if (p->pbm_B.io_space.flags) {
448 if (lowest > p->pbm_B.io_space.start)
449 lowest = p->pbm_B.io_space.start;
450 if (highest < p->pbm_B.io_space.end + 1)
451 highest = p->pbm_B.io_space.end + 1;
452 }
453 space_size = highest - lowest;
454 } else {
455 if (p->pbm_A.mem_space.flags) {
456 lowest = p->pbm_A.mem_space.start;
457 highest = p->pbm_A.mem_space.end + 1;
458 }
459 if (p->pbm_B.mem_space.flags) {
460 if (lowest > p->pbm_B.mem_space.start)
461 lowest = p->pbm_B.mem_space.start;
462 if (highest < p->pbm_B.mem_space.end + 1)
463 highest = p->pbm_B.mem_space.end + 1;
464 }
465 space_size = highest - lowest;
466 }
467 } else {
468 if (mmap_state == pci_mmap_io) {
469 space_size = (pbm->io_space.end -
470 pbm->io_space.start) + 1;
471 } else {
472 space_size = (pbm->mem_space.end -
473 pbm->mem_space.start) + 1;
474 }
475 }
476
477 /* Make sure the request is in range. */
478 user_offset = vma->vm_pgoff << PAGE_SHIFT;
479 user_size = vma->vm_end - vma->vm_start;
480
481 if (user_offset >= space_size ||
482 (user_offset + user_size) > space_size)
483 return -EINVAL;
484
485 if (p->pbms_same_domain) {
486 unsigned long lowest = ~0UL;
487
488 if (mmap_state == pci_mmap_io) {
489 if (p->pbm_A.io_space.flags)
490 lowest = p->pbm_A.io_space.start;
491 if (p->pbm_B.io_space.flags &&
492 lowest > p->pbm_B.io_space.start)
493 lowest = p->pbm_B.io_space.start;
494 } else {
495 if (p->pbm_A.mem_space.flags)
496 lowest = p->pbm_A.mem_space.start;
497 if (p->pbm_B.mem_space.flags &&
498 lowest > p->pbm_B.mem_space.start)
499 lowest = p->pbm_B.mem_space.start;
500 }
501 vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
502 } else {
503 if (mmap_state == pci_mmap_io) {
504 vma->vm_pgoff = (pbm->io_space.start +
505 user_offset) >> PAGE_SHIFT;
506 } else {
507 vma->vm_pgoff = (pbm->mem_space.start +
508 user_offset) >> PAGE_SHIFT;
509 }
510 }
511
512 return 0;
513 }
514
515 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
516 * to the 32-bit pci bus offset for DEV requested by the user.
517 *
518 * Basically, the user finds the base address for his device which he wishes
519 * to mmap. They read the 32-bit value from the config space base register,
520 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
521 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
522 *
523 * Returns negative error code on failure, zero on success.
524 */
525 static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
526 enum pci_mmap_state mmap_state)
527 {
528 unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
529 unsigned long user32 = user_offset & pci_memspace_mask;
530 unsigned long largest_base, this_base, addr32;
531 int i;
532
533 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
534 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
535
536 /* Figure out which base address this is for. */
537 largest_base = 0UL;
538 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
539 struct resource *rp = &dev->resource[i];
540
541 /* Active? */
542 if (!rp->flags)
543 continue;
544
545 /* Same type? */
546 if (i == PCI_ROM_RESOURCE) {
547 if (mmap_state != pci_mmap_mem)
548 continue;
549 } else {
550 if ((mmap_state == pci_mmap_io &&
551 (rp->flags & IORESOURCE_IO) == 0) ||
552 (mmap_state == pci_mmap_mem &&
553 (rp->flags & IORESOURCE_MEM) == 0))
554 continue;
555 }
556
557 this_base = rp->start;
558
559 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
560
561 if (mmap_state == pci_mmap_io)
562 addr32 &= 0xffffff;
563
564 if (addr32 <= user32 && this_base > largest_base)
565 largest_base = this_base;
566 }
567
568 if (largest_base == 0UL)
569 return -EINVAL;
570
571 /* Now construct the final physical address. */
572 if (mmap_state == pci_mmap_io)
573 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
574 else
575 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
576
577 return 0;
578 }
579
580 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
581 * mapping.
582 */
583 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
584 enum pci_mmap_state mmap_state)
585 {
586 vma->vm_flags |= (VM_IO | VM_RESERVED);
587 }
588
589 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
590 * device mapping.
591 */
592 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
593 enum pci_mmap_state mmap_state)
594 {
595 /* Our io_remap_pfn_range takes care of this, do nothing. */
596 }
597
598 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
599 * for this architecture. The region in the process to map is described by vm_start
600 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
601 * The pci device structure is provided so that architectures may make mapping
602 * decisions on a per-device or per-bus basis.
603 *
604 * Returns a negative error code on failure, zero on success.
605 */
606 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
607 enum pci_mmap_state mmap_state,
608 int write_combine)
609 {
610 int ret;
611
612 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
613 if (ret < 0)
614 return ret;
615
616 __pci_mmap_set_flags(dev, vma, mmap_state);
617 __pci_mmap_set_pgprot(dev, vma, mmap_state);
618
619 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
620 ret = io_remap_pfn_range(vma, vma->vm_start,
621 vma->vm_pgoff,
622 vma->vm_end - vma->vm_start,
623 vma->vm_page_prot);
624 if (ret)
625 return ret;
626
627 return 0;
628 }
629
630 /* Return the domain nuber for this pci bus */
631
632 int pci_domain_nr(struct pci_bus *pbus)
633 {
634 struct pci_pbm_info *pbm = pbus->sysdata;
635 int ret;
636
637 if (pbm == NULL || pbm->parent == NULL) {
638 ret = -ENXIO;
639 } else {
640 struct pci_controller_info *p = pbm->parent;
641
642 ret = p->index;
643 if (p->pbms_same_domain == 0)
644 ret = ((ret << 1) +
645 ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
646 }
647
648 return ret;
649 }
650 EXPORT_SYMBOL(pci_domain_nr);
651
652 int pcibios_prep_mwi(struct pci_dev *dev)
653 {
654 /* We set correct PCI_CACHE_LINE_SIZE register values for every
655 * device probed on this platform. So there is nothing to check
656 * and this always succeeds.
657 */
658 return 0;
659 }
660
661 #endif /* !(CONFIG_PCI) */
This page took 0.046222 seconds and 5 git commands to generate.