1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/module.h>
29#include <linux/sched.h>
30#include <linux/kernel.h>
31#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/ioport.h>
34#include <linux/mm.h>
35#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/proc_fs.h>
38#include <linux/scatterlist.h>
39
40#include <asm/io.h>
41#include <asm/vaddrs.h>
42#include <asm/oplib.h>
43#include <asm/prom.h>
44#include <asm/of_device.h>
45#include <asm/sbus.h>
46#include <asm/page.h>
47#include <asm/pgalloc.h>
48#include <asm/dma.h>
49
50#define mmu_inval_dma_area(p, l)
51
52struct resource *_sparc_find_resource(struct resource *r, unsigned long);
53
54static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
55static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
56 unsigned long size, char *name);
57static void _sparc_free_io(struct resource *res);
58
59
60static struct resource _sparc_dvma = {
61 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
62};
63
64 struct resource sparc_iomap = {
65 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
66};
67
68
69
70
71
72
73
74#define XNMLN 15
75#define XNRES 10
76
77struct xresource {
78 struct resource xres;
79 int xflag;
80 char xname[XNMLN+1];
81};
82
83static struct xresource xresv[XNRES];
84
85static struct xresource *xres_alloc(void) {
86 struct xresource *xrp;
87 int n;
88
89 xrp = xresv;
90 for (n = 0; n < XNRES; n++) {
91 if (xrp->xflag == 0) {
92 xrp->xflag = 1;
93 return xrp;
94 }
95 xrp++;
96 }
97 return NULL;
98}
99
100static void xres_free(struct xresource *xrp) {
101 xrp->xflag = 0;
102}
103
104
105
106
107
108
109
110void __iomem *ioremap(unsigned long offset, unsigned long size)
111{
112 char name[14];
113
114 sprintf(name, "phys_%08x", (u32)offset);
115 return _sparc_alloc_io(0, offset, size, name);
116}
117
118
119
120
121void iounmap(volatile void __iomem *virtual)
122{
123 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
124 struct resource *res;
125
126 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
127 printk("free_io/iounmap: cannot free %lx\n", vaddr);
128 return;
129 }
130 _sparc_free_io(res);
131
132 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
133 xres_free((struct xresource *)res);
134 } else {
135 kfree(res);
136 }
137}
138
139
140
141void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
142 unsigned long size, char *name)
143{
144 return _sparc_alloc_io(phyres->flags & 0xF,
145 phyres->start + offset, size, name);
146}
147
148void __iomem *of_ioremap(struct resource *res, unsigned long offset,
149 unsigned long size, char *name)
150{
151 return _sparc_alloc_io(res->flags & 0xF,
152 res->start + offset,
153 size, name);
154}
155EXPORT_SYMBOL(of_ioremap);
156
157void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
158{
159 iounmap(base);
160}
161EXPORT_SYMBOL(of_iounmap);
162
163
164
165void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
166{
167 iounmap(addr);
168}
169
170
171
172
173static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
174 unsigned long size, char *name)
175{
176 static int printed_full;
177 struct xresource *xres;
178 struct resource *res;
179 char *tack;
180 int tlen;
181 void __iomem *va;
182
183 if (name == NULL) name = "???";
184
185 if ((xres = xres_alloc()) != 0) {
186 tack = xres->xname;
187 res = &xres->xres;
188 } else {
189 if (!printed_full) {
190 printk("ioremap: done with statics, switching to malloc\n");
191 printed_full = 1;
192 }
193 tlen = strlen(name);
194 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
195 if (tack == NULL) return NULL;
196 memset(tack, 0, sizeof(struct resource));
197 res = (struct resource *) tack;
198 tack += sizeof (struct resource);
199 }
200
201 strlcpy(tack, name, XNMLN+1);
202 res->name = tack;
203
204 va = _sparc_ioremap(res, busno, phys, size);
205
206 return va;
207}
208
209
210
211static void __iomem *
212_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
213{
214 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
215
216 if (allocate_resource(&sparc_iomap, res,
217 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
218 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
219
220 prom_printf("alloc_io_res(%s): cannot occupy\n",
221 (res->name != NULL)? res->name: "???");
222 prom_halt();
223 }
224
225 pa &= PAGE_MASK;
226 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
227
228 return (void __iomem *)(unsigned long)(res->start + offset);
229}
230
231
232
233
234static void _sparc_free_io(struct resource *res)
235{
236 unsigned long plen;
237
238 plen = res->end - res->start + 1;
239 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
240 sparc_unmapiorange(res->start, plen);
241 release_resource(res);
242}
243
244#ifdef CONFIG_SBUS
245
246void sbus_set_sbus64(struct sbus_dev *sdev, int x)
247{
248 printk("sbus_set_sbus64: unsupported\n");
249}
250
251extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
252void __init sbus_fill_device_irq(struct sbus_dev *sdev)
253{
254 struct linux_prom_irqs irqs[PROMINTR_MAX];
255 int len;
256
257 len = prom_getproperty(sdev->prom_node, "intr",
258 (char *)irqs, sizeof(irqs));
259 if (len != -1) {
260 sdev->num_irqs = len / 8;
261 if (sdev->num_irqs == 0) {
262 sdev->irqs[0] = 0;
263 } else if (sparc_cpu_model == sun4d) {
264 for (len = 0; len < sdev->num_irqs; len++)
265 sdev->irqs[len] =
266 sun4d_build_irq(sdev, irqs[len].pri);
267 } else {
268 for (len = 0; len < sdev->num_irqs; len++)
269 sdev->irqs[len] = irqs[len].pri;
270 }
271 } else {
272 int interrupts[PROMINTR_MAX];
273
274
275
276
277
278
279 len = prom_getproperty(sdev->prom_node, "interrupts",
280 (char *)interrupts, sizeof(interrupts));
281 if (len == -1) {
282 sdev->irqs[0] = 0;
283 sdev->num_irqs = 0;
284 } else {
285 sdev->num_irqs = len / sizeof(int);
286 for (len = 0; len < sdev->num_irqs; len++) {
287 sdev->irqs[len] =
288 sbint_to_irq(sdev, interrupts[len]);
289 }
290 }
291 }
292}
293
294
295
296
297
298
299
300
301void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
302{
303 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
304 unsigned long va;
305 struct resource *res;
306 int order;
307
308
309 if (len <= 0) {
310 return NULL;
311 }
312
313 if (len > 256*1024) {
314 return NULL;
315 }
316
317 order = get_order(len_total);
318 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
319 goto err_nopages;
320
321 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
322 goto err_nomem;
323
324 if (allocate_resource(&_sparc_dvma, res, len_total,
325 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
326 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
327 goto err_nova;
328 }
329 mmu_inval_dma_area(va, len_total);
330
331
332
333
334
335
336 if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
337 goto err_noiommu;
338
339
340 if (sdev) {
341 res->name = sdev->prom_name;
342 }
343
344 return (void *)(unsigned long)res->start;
345
346err_noiommu:
347 release_resource(res);
348err_nova:
349 free_pages(va, order);
350err_nomem:
351 kfree(res);
352err_nopages:
353 return NULL;
354}
355
356void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
357{
358 struct resource *res;
359 struct page *pgv;
360
361 if ((res = _sparc_find_resource(&_sparc_dvma,
362 (unsigned long)p)) == NULL) {
363 printk("sbus_free_consistent: cannot free %p\n", p);
364 return;
365 }
366
367 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
368 printk("sbus_free_consistent: unaligned va %p\n", p);
369 return;
370 }
371
372 n = (n + PAGE_SIZE-1) & PAGE_MASK;
373 if ((res->end-res->start)+1 != n) {
374 printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
375 (long)((res->end-res->start)+1), n);
376 return;
377 }
378
379 release_resource(res);
380 kfree(res);
381
382
383 pgv = mmu_translate_dvma(ba);
384 mmu_unmap_dma_area(ba, n);
385
386 __free_pages(pgv, get_order(n));
387}
388
389
390
391
392
393
394dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
395{
396
397 if (len <= 0) {
398 return 0;
399 }
400
401 if (len > 256*1024) {
402 return 0;
403 }
404 return mmu_get_scsi_one(va, len, sdev->bus);
405}
406
407void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
408{
409 mmu_release_scsi_one(ba, n, sdev->bus);
410}
411
412int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
413{
414 mmu_get_scsi_sgl(sg, n, sdev->bus);
415
416
417
418
419
420 return n;
421}
422
423void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
424{
425 mmu_release_scsi_sgl(sg, n, sdev->bus);
426}
427
428
429
430void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
431{
432#if 0
433 unsigned long va;
434 struct resource *res;
435
436
437 res = _sparc_find_resource(&_sparc_dvma, ba);
438 if (res == NULL)
439 panic("sbus_dma_sync_single: 0x%x\n", ba);
440
441 va = page_address(mmu_translate_dvma(ba));
442
443
444
445
446
447#endif
448}
449
450void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
451{
452#if 0
453 unsigned long va;
454 struct resource *res;
455
456
457 res = _sparc_find_resource(&_sparc_dvma, ba);
458 if (res == NULL)
459 panic("sbus_dma_sync_single: 0x%x\n", ba);
460
461 va = page_address(mmu_translate_dvma(ba));
462
463
464
465
466
467#endif
468}
469
470void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
471{
472 printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
473}
474
475void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
476{
477 printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
478}
479
480
481
482
483
484
485
486
487void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
488{
489 int parent_node = pn->node;
490
491 if (sparc_cpu_model == sun4d) {
492 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
493 int num_iounit_ranges, len;
494
495 len = prom_getproperty(parent_node, "ranges",
496 (char *) iounit_ranges,
497 sizeof (iounit_ranges));
498 if (len != -1) {
499 num_iounit_ranges =
500 (len / sizeof(struct linux_prom_ranges));
501 prom_adjust_ranges(sbus->sbus_ranges,
502 sbus->num_sbus_ranges,
503 iounit_ranges, num_iounit_ranges);
504 }
505 }
506}
507
508void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
509{
510#ifndef CONFIG_SUN4
511 struct device_node *parent = dp->parent;
512
513 if (sparc_cpu_model != sun4d &&
514 parent != NULL &&
515 !strcmp(parent->name, "iommu")) {
516 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
517
518 iommu_init(parent->node, sbus);
519 }
520
521 if (sparc_cpu_model == sun4d) {
522 extern void iounit_init(int sbi_node, int iounit_node,
523 struct sbus_bus *sbus);
524
525 iounit_init(dp->node, parent->node, sbus);
526 }
527#endif
528}
529
530void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
531{
532 if (sparc_cpu_model == sun4d) {
533 struct device_node *parent = dp->parent;
534
535 sbus->devid = of_getintprop_default(parent, "device-id", 0);
536 sbus->board = of_getintprop_default(parent, "board#", 0);
537 }
538}
539
540int __init sbus_arch_preinit(void)
541{
542 extern void register_proc_sparc_ioport(void);
543
544 register_proc_sparc_ioport();
545
546#ifdef CONFIG_SUN4
547 {
548 extern void sun4_dvma_init(void);
549 sun4_dvma_init();
550 }
551 return 1;
552#else
553 return 0;
554#endif
555}
556
557void __init sbus_arch_postinit(void)
558{
559 if (sparc_cpu_model == sun4d) {
560 extern void sun4d_init_sbi_irq(void);
561 sun4d_init_sbi_irq();
562 }
563}
564#endif
565
566#ifdef CONFIG_PCI
567
568
569
570
571void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
572{
573 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
574 unsigned long va;
575 struct resource *res;
576 int order;
577
578 if (len == 0) {
579 return NULL;
580 }
581 if (len > 256*1024) {
582 return NULL;
583 }
584
585 order = get_order(len_total);
586 va = __get_free_pages(GFP_KERNEL, order);
587 if (va == 0) {
588 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
589 return NULL;
590 }
591
592 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
593 free_pages(va, order);
594 printk("pci_alloc_consistent: no core\n");
595 return NULL;
596 }
597
598 if (allocate_resource(&_sparc_dvma, res, len_total,
599 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
600 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
601 free_pages(va, order);
602 kfree(res);
603 return NULL;
604 }
605 mmu_inval_dma_area(va, len_total);
606#if 0
607 printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
608 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
609#endif
610 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
611
612 *pba = virt_to_phys(va);
613 return (void *) res->start;
614}
615
616
617
618
619
620
621
622
623
624void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
625{
626 struct resource *res;
627 unsigned long pgp;
628
629 if ((res = _sparc_find_resource(&_sparc_dvma,
630 (unsigned long)p)) == NULL) {
631 printk("pci_free_consistent: cannot free %p\n", p);
632 return;
633 }
634
635 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
636 printk("pci_free_consistent: unaligned va %p\n", p);
637 return;
638 }
639
640 n = (n + PAGE_SIZE-1) & PAGE_MASK;
641 if ((res->end-res->start)+1 != n) {
642 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
643 (long)((res->end-res->start)+1), (long)n);
644 return;
645 }
646
647 pgp = (unsigned long) phys_to_virt(ba);
648 mmu_inval_dma_area(pgp, n);
649 sparc_unmapiorange((unsigned long)p, n);
650
651 release_resource(res);
652 kfree(res);
653
654 free_pages(pgp, get_order(n));
655}
656
657
658
659
660
661
662
663dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
664 int direction)
665{
666 BUG_ON(direction == PCI_DMA_NONE);
667
668 return virt_to_phys(ptr);
669}
670
671
672
673
674
675
676
677
678void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
679 int direction)
680{
681 BUG_ON(direction == PCI_DMA_NONE);
682 if (direction != PCI_DMA_TODEVICE) {
683 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
684 (size + PAGE_SIZE-1) & PAGE_MASK);
685 }
686}
687
688
689
690
691dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
692 unsigned long offset, size_t size, int direction)
693{
694 BUG_ON(direction == PCI_DMA_NONE);
695
696 return page_to_phys(page) + offset;
697}
698
699void pci_unmap_page(struct pci_dev *hwdev,
700 dma_addr_t dma_address, size_t size, int direction)
701{
702 BUG_ON(direction == PCI_DMA_NONE);
703
704}
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
722 int direction)
723{
724 struct scatterlist *sg;
725 int n;
726
727 BUG_ON(direction == PCI_DMA_NONE);
728
729 for_each_sg(sgl, sg, nents, n) {
730 BUG_ON(page_address(sg_page(sg)) == NULL);
731 sg->dvma_address = virt_to_phys(sg_virt(sg));
732 sg->dvma_length = sg->length;
733 }
734 return nents;
735}
736
737
738
739
740
741void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
742 int direction)
743{
744 struct scatterlist *sg;
745 int n;
746
747 BUG_ON(direction == PCI_DMA_NONE);
748 if (direction != PCI_DMA_TODEVICE) {
749 for_each_sg(sgl, sg, nents, n) {
750 BUG_ON(page_address(sg_page(sg)) == NULL);
751 mmu_inval_dma_area(
752 (unsigned long) page_address(sg_page(sg)),
753 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
754 }
755 }
756}
757
758
759
760
761
762
763
764
765
766
767
768void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
769{
770 BUG_ON(direction == PCI_DMA_NONE);
771 if (direction != PCI_DMA_TODEVICE) {
772 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
773 (size + PAGE_SIZE-1) & PAGE_MASK);
774 }
775}
776
777void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
778{
779 BUG_ON(direction == PCI_DMA_NONE);
780 if (direction != PCI_DMA_TODEVICE) {
781 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
782 (size + PAGE_SIZE-1) & PAGE_MASK);
783 }
784}
785
786
787
788
789
790
791
792void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
793{
794 struct scatterlist *sg;
795 int n;
796
797 BUG_ON(direction == PCI_DMA_NONE);
798 if (direction != PCI_DMA_TODEVICE) {
799 for_each_sg(sgl, sg, nents, n) {
800 BUG_ON(page_address(sg_page(sg)) == NULL);
801 mmu_inval_dma_area(
802 (unsigned long) page_address(sg_page(sg)),
803 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
804 }
805 }
806}
807
808void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
809{
810 struct scatterlist *sg;
811 int n;
812
813 BUG_ON(direction == PCI_DMA_NONE);
814 if (direction != PCI_DMA_TODEVICE) {
815 for_each_sg(sgl, sg, nents, n) {
816 BUG_ON(page_address(sg_page(sg)) == NULL);
817 mmu_inval_dma_area(
818 (unsigned long) page_address(sg_page(sg)),
819 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
820 }
821 }
822}
823#endif
824
825#ifdef CONFIG_PROC_FS
826
827static int
828_sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
829 void *data)
830{
831 char *p = buf, *e = buf + length;
832 struct resource *r;
833 const char *nm;
834
835 for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
836 if (p + 32 >= e)
837 break;
838 if ((nm = r->name) == 0) nm = "???";
839 p += sprintf(p, "%016llx-%016llx: %s\n",
840 (unsigned long long)r->start,
841 (unsigned long long)r->end, nm);
842 }
843
844 return p-buf;
845}
846
847#endif
848
849
850
851
852
853
854
855
856struct resource *
857_sparc_find_resource(struct resource *root, unsigned long hit)
858{
859 struct resource *tmp;
860
861 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
862 if (tmp->start <= hit && tmp->end >= hit)
863 return tmp;
864 }
865 return NULL;
866}
867
868void register_proc_sparc_ioport(void)
869{
870#ifdef CONFIG_PROC_FS
871 create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
872 create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
873#endif
874}
875