typo fixes: occuring -> occurring
[linux-2.6] / 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 power_init(void);
311
312 static int __init pcibios_init(void)
313 {
314         pci_controller_probe();
315         if (pci_controller_root == NULL)
316                 return 0;
317
318         pci_scan_each_controller_bus();
319
320         isa_init();
321         ebus_init();
322         power_init();
323
324         return 0;
325 }
326
327 subsys_initcall(pcibios_init);
328
329 void pcibios_fixup_bus(struct pci_bus *pbus)
330 {
331         struct pci_pbm_info *pbm = pbus->sysdata;
332
333         /* Generic PCI bus probing sets these to point at
334          * &io{port,mem}_resouce which is wrong for us.
335          */
336         pbus->resource[0] = &pbm->io_space;
337         pbus->resource[1] = &pbm->mem_space;
338 }
339
340 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
341 {
342         struct pci_pbm_info *pbm = pdev->bus->sysdata;
343         struct resource *root = NULL;
344
345         if (r->flags & IORESOURCE_IO)
346                 root = &pbm->io_space;
347         if (r->flags & IORESOURCE_MEM)
348                 root = &pbm->mem_space;
349
350         return root;
351 }
352
353 void pcibios_update_irq(struct pci_dev *pdev, int irq)
354 {
355 }
356
357 void pcibios_align_resource(void *data, struct resource *res,
358                             resource_size_t size, resource_size_t align)
359 {
360 }
361
362 int pcibios_enable_device(struct pci_dev *pdev, int mask)
363 {
364         return 0;
365 }
366
367 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
368                              struct resource *res)
369 {
370         struct pci_pbm_info *pbm = pdev->bus->sysdata;
371         struct resource zero_res, *root;
372
373         zero_res.start = 0;
374         zero_res.end = 0;
375         zero_res.flags = res->flags;
376
377         if (res->flags & IORESOURCE_IO)
378                 root = &pbm->io_space;
379         else
380                 root = &pbm->mem_space;
381
382         pbm->parent->resource_adjust(pdev, &zero_res, root);
383
384         region->start = res->start - zero_res.start;
385         region->end = res->end - zero_res.start;
386 }
387 EXPORT_SYMBOL(pcibios_resource_to_bus);
388
389 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
390                              struct pci_bus_region *region)
391 {
392         struct pci_pbm_info *pbm = pdev->bus->sysdata;
393         struct resource *root;
394
395         res->start = region->start;
396         res->end = region->end;
397
398         if (res->flags & IORESOURCE_IO)
399                 root = &pbm->io_space;
400         else
401                 root = &pbm->mem_space;
402
403         pbm->parent->resource_adjust(pdev, res, root);
404 }
405 EXPORT_SYMBOL(pcibios_bus_to_resource);
406
407 char * __init pcibios_setup(char *str)
408 {
409         return str;
410 }
411
412 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
413
414 /* If the user uses a host-bridge as the PCI device, he may use
415  * this to perform a raw mmap() of the I/O or MEM space behind
416  * that controller.
417  *
418  * This can be useful for execution of x86 PCI bios initialization code
419  * on a PCI card, like the xfree86 int10 stuff does.
420  */
421 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
422                                       enum pci_mmap_state mmap_state)
423 {
424         struct pcidev_cookie *pcp = pdev->sysdata;
425         struct pci_pbm_info *pbm;
426         struct pci_controller_info *p;
427         unsigned long space_size, user_offset, user_size;
428
429         if (!pcp)
430                 return -ENXIO;
431         pbm = pcp->pbm;
432         if (!pbm)
433                 return -ENXIO;
434
435         p = pbm->parent;
436         if (p->pbms_same_domain) {
437                 unsigned long lowest, highest;
438
439                 lowest = ~0UL; highest = 0UL;
440                 if (mmap_state == pci_mmap_io) {
441                         if (p->pbm_A.io_space.flags) {
442                                 lowest = p->pbm_A.io_space.start;
443                                 highest = p->pbm_A.io_space.end + 1;
444                         }
445                         if (p->pbm_B.io_space.flags) {
446                                 if (lowest > p->pbm_B.io_space.start)
447                                         lowest = p->pbm_B.io_space.start;
448                                 if (highest < p->pbm_B.io_space.end + 1)
449                                         highest = p->pbm_B.io_space.end + 1;
450                         }
451                         space_size = highest - lowest;
452                 } else {
453                         if (p->pbm_A.mem_space.flags) {
454                                 lowest = p->pbm_A.mem_space.start;
455                                 highest = p->pbm_A.mem_space.end + 1;
456                         }
457                         if (p->pbm_B.mem_space.flags) {
458                                 if (lowest > p->pbm_B.mem_space.start)
459                                         lowest = p->pbm_B.mem_space.start;
460                                 if (highest < p->pbm_B.mem_space.end + 1)
461                                         highest = p->pbm_B.mem_space.end + 1;
462                         }
463                         space_size = highest - lowest;
464                 }
465         } else {
466                 if (mmap_state == pci_mmap_io) {
467                         space_size = (pbm->io_space.end -
468                                       pbm->io_space.start) + 1;
469                 } else {
470                         space_size = (pbm->mem_space.end -
471                                       pbm->mem_space.start) + 1;
472                 }
473         }
474
475         /* Make sure the request is in range. */
476         user_offset = vma->vm_pgoff << PAGE_SHIFT;
477         user_size = vma->vm_end - vma->vm_start;
478
479         if (user_offset >= space_size ||
480             (user_offset + user_size) > space_size)
481                 return -EINVAL;
482
483         if (p->pbms_same_domain) {
484                 unsigned long lowest = ~0UL;
485
486                 if (mmap_state == pci_mmap_io) {
487                         if (p->pbm_A.io_space.flags)
488                                 lowest = p->pbm_A.io_space.start;
489                         if (p->pbm_B.io_space.flags &&
490                             lowest > p->pbm_B.io_space.start)
491                                 lowest = p->pbm_B.io_space.start;
492                 } else {
493                         if (p->pbm_A.mem_space.flags)
494                                 lowest = p->pbm_A.mem_space.start;
495                         if (p->pbm_B.mem_space.flags &&
496                             lowest > p->pbm_B.mem_space.start)
497                                 lowest = p->pbm_B.mem_space.start;
498                 }
499                 vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
500         } else {
501                 if (mmap_state == pci_mmap_io) {
502                         vma->vm_pgoff = (pbm->io_space.start +
503                                          user_offset) >> PAGE_SHIFT;
504                 } else {
505                         vma->vm_pgoff = (pbm->mem_space.start +
506                                          user_offset) >> PAGE_SHIFT;
507                 }
508         }
509
510         return 0;
511 }
512
513 /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
514  * to the 32-bit pci bus offset for DEV requested by the user.
515  *
516  * Basically, the user finds the base address for his device which he wishes
517  * to mmap.  They read the 32-bit value from the config space base register,
518  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
519  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
520  *
521  * Returns negative error code on failure, zero on success.
522  */
523 static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
524                                   enum pci_mmap_state mmap_state)
525 {
526         unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
527         unsigned long user32 = user_offset & pci_memspace_mask;
528         unsigned long largest_base, this_base, addr32;
529         int i;
530
531         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
532                 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
533
534         /* Figure out which base address this is for. */
535         largest_base = 0UL;
536         for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
537                 struct resource *rp = &dev->resource[i];
538
539                 /* Active? */
540                 if (!rp->flags)
541                         continue;
542
543                 /* Same type? */
544                 if (i == PCI_ROM_RESOURCE) {
545                         if (mmap_state != pci_mmap_mem)
546                                 continue;
547                 } else {
548                         if ((mmap_state == pci_mmap_io &&
549                              (rp->flags & IORESOURCE_IO) == 0) ||
550                             (mmap_state == pci_mmap_mem &&
551                              (rp->flags & IORESOURCE_MEM) == 0))
552                                 continue;
553                 }
554
555                 this_base = rp->start;
556
557                 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
558
559                 if (mmap_state == pci_mmap_io)
560                         addr32 &= 0xffffff;
561
562                 if (addr32 <= user32 && this_base > largest_base)
563                         largest_base = this_base;
564         }
565
566         if (largest_base == 0UL)
567                 return -EINVAL;
568
569         /* Now construct the final physical address. */
570         if (mmap_state == pci_mmap_io)
571                 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
572         else
573                 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
574
575         return 0;
576 }
577
578 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
579  * mapping.
580  */
581 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
582                                             enum pci_mmap_state mmap_state)
583 {
584         vma->vm_flags |= (VM_IO | VM_RESERVED);
585 }
586
587 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
588  * device mapping.
589  */
590 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
591                                              enum pci_mmap_state mmap_state)
592 {
593         /* Our io_remap_pfn_range takes care of this, do nothing.  */
594 }
595
596 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
597  * for this architecture.  The region in the process to map is described by vm_start
598  * and vm_end members of VMA, the base physical address is found in vm_pgoff.
599  * The pci device structure is provided so that architectures may make mapping
600  * decisions on a per-device or per-bus basis.
601  *
602  * Returns a negative error code on failure, zero on success.
603  */
604 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
605                         enum pci_mmap_state mmap_state,
606                         int write_combine)
607 {
608         int ret;
609
610         ret = __pci_mmap_make_offset(dev, vma, mmap_state);
611         if (ret < 0)
612                 return ret;
613
614         __pci_mmap_set_flags(dev, vma, mmap_state);
615         __pci_mmap_set_pgprot(dev, vma, mmap_state);
616
617         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
618         ret = io_remap_pfn_range(vma, vma->vm_start,
619                                  vma->vm_pgoff,
620                                  vma->vm_end - vma->vm_start,
621                                  vma->vm_page_prot);
622         if (ret)
623                 return ret;
624
625         return 0;
626 }
627
628 /* Return the domain nuber for this pci bus */
629
630 int pci_domain_nr(struct pci_bus *pbus)
631 {
632         struct pci_pbm_info *pbm = pbus->sysdata;
633         int ret;
634
635         if (pbm == NULL || pbm->parent == NULL) {
636                 ret = -ENXIO;
637         } else {
638                 struct pci_controller_info *p = pbm->parent;
639
640                 ret = p->index;
641                 if (p->pbms_same_domain == 0)
642                         ret = ((ret << 1) +
643                                ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
644         }
645
646         return ret;
647 }
648 EXPORT_SYMBOL(pci_domain_nr);
649
650 int pcibios_prep_mwi(struct pci_dev *dev)
651 {
652         /* We set correct PCI_CACHE_LINE_SIZE register values for every
653          * device probed on this platform.  So there is nothing to check
654          * and this always succeeds.
655          */
656         return 0;
657 }
658
659 #endif /* !(CONFIG_PCI) */