xref: /openbmc/linux/arch/m68k/sun3x/dvma.c (revision 239480ab)
1 /*
2  * Virtual DMA allocation
3  *
4  * (C) 1999 Thomas Bogendoerfer (tsbogend@alpha.franken.de)
5  *
6  * 11/26/2000 -- disabled the existing code because it didn't work for
7  * me in 2.4.  Replaced with a significantly more primitive version
8  * similar to the sun3 code.  the old functionality was probably more
9  * desirable, but....   -- Sam Creasey (sammy@oh.verio.com)
10  *
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/bitops.h>
16 #include <linux/mm.h>
17 #include <linux/bootmem.h>
18 #include <linux/vmalloc.h>
19 
20 #include <asm/sun3x.h>
21 #include <asm/dvma.h>
22 #include <asm/io.h>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/pgalloc.h>
26 
27 /* IOMMU support */
28 
29 #define IOMMU_ADDR_MASK            0x03ffe000
30 #define IOMMU_CACHE_INHIBIT        0x00000040
31 #define IOMMU_FULL_BLOCK           0x00000020
32 #define IOMMU_MODIFIED             0x00000010
33 #define IOMMU_USED                 0x00000008
34 #define IOMMU_WRITE_PROTECT        0x00000004
35 #define IOMMU_DT_MASK              0x00000003
36 #define IOMMU_DT_INVALID           0x00000000
37 #define IOMMU_DT_VALID             0x00000001
38 #define IOMMU_DT_BAD               0x00000002
39 
40 
41 static volatile unsigned long *iommu_pte = (unsigned long *)SUN3X_IOMMU;
42 
43 
44 #define dvma_entry_paddr(index)		(iommu_pte[index] & IOMMU_ADDR_MASK)
45 #define dvma_entry_vaddr(index,paddr)	((index << DVMA_PAGE_SHIFT) |  \
46 					 (paddr & (DVMA_PAGE_SIZE-1)))
47 #if 0
48 #define dvma_entry_set(index,addr)	(iommu_pte[index] =            \
49 					    (addr & IOMMU_ADDR_MASK) | \
50 				             IOMMU_DT_VALID | IOMMU_CACHE_INHIBIT)
51 #else
52 #define dvma_entry_set(index,addr)	(iommu_pte[index] =            \
53 					    (addr & IOMMU_ADDR_MASK) | \
54 				             IOMMU_DT_VALID)
55 #endif
56 #define dvma_entry_clr(index)		(iommu_pte[index] = IOMMU_DT_INVALID)
57 #define dvma_entry_hash(addr)		((addr >> DVMA_PAGE_SHIFT) ^ \
58 					 ((addr & 0x03c00000) >>     \
59 						(DVMA_PAGE_SHIFT+4)))
60 
61 #ifdef DEBUG
62 /* code to print out a dvma mapping for debugging purposes */
63 void dvma_print (unsigned long dvma_addr)
64 {
65 
66 	unsigned long index;
67 
68 	index = dvma_addr >> DVMA_PAGE_SHIFT;
69 
70 	pr_info("idx %lx dvma_addr %08lx paddr %08lx\n", index, dvma_addr,
71 		dvma_entry_paddr(index));
72 }
73 #endif
74 
75 
76 /* create a virtual mapping for a page assigned within the IOMMU
77    so that the cpu can reach it easily */
78 inline int dvma_map_cpu(unsigned long kaddr,
79 			       unsigned long vaddr, int len)
80 {
81 	pgd_t *pgd;
82 	unsigned long end;
83 	int ret = 0;
84 
85 	kaddr &= PAGE_MASK;
86 	vaddr &= PAGE_MASK;
87 
88 	end = PAGE_ALIGN(vaddr + len);
89 
90 	pr_debug("dvma: mapping kern %08lx to virt %08lx\n", kaddr, vaddr);
91 	pgd = pgd_offset_k(vaddr);
92 
93 	do {
94 		pmd_t *pmd;
95 		unsigned long end2;
96 
97 		if((pmd = pmd_alloc(&init_mm, pgd, vaddr)) == NULL) {
98 			ret = -ENOMEM;
99 			goto out;
100 		}
101 
102 		if((end & PGDIR_MASK) > (vaddr & PGDIR_MASK))
103 			end2 = (vaddr + (PGDIR_SIZE-1)) & PGDIR_MASK;
104 		else
105 			end2 = end;
106 
107 		do {
108 			pte_t *pte;
109 			unsigned long end3;
110 
111 			if((pte = pte_alloc_kernel(pmd, vaddr)) == NULL) {
112 				ret = -ENOMEM;
113 				goto out;
114 			}
115 
116 			if((end2 & PMD_MASK) > (vaddr & PMD_MASK))
117 				end3 = (vaddr + (PMD_SIZE-1)) & PMD_MASK;
118 			else
119 				end3 = end2;
120 
121 			do {
122 				pr_debug("mapping %08lx phys to %08lx\n",
123 					 __pa(kaddr), vaddr);
124 				set_pte(pte, pfn_pte(virt_to_pfn(kaddr),
125 						     PAGE_KERNEL));
126 				pte++;
127 				kaddr += PAGE_SIZE;
128 				vaddr += PAGE_SIZE;
129 			} while(vaddr < end3);
130 
131 		} while(vaddr < end2);
132 
133 	} while(vaddr < end);
134 
135 	flush_tlb_all();
136 
137  out:
138 	return ret;
139 }
140 
141 
142 inline int dvma_map_iommu(unsigned long kaddr, unsigned long baddr,
143 				 int len)
144 {
145 	unsigned long end, index;
146 
147 	index = baddr >> DVMA_PAGE_SHIFT;
148 	end = ((baddr+len) >> DVMA_PAGE_SHIFT);
149 
150 	if(len & ~DVMA_PAGE_MASK)
151 		end++;
152 
153 	for(; index < end ; index++) {
154 //		if(dvma_entry_use(index))
155 //			BUG();
156 //		pr_info("mapping pa %lx to ba %lx\n", __pa(kaddr),
157 //			index << DVMA_PAGE_SHIFT);
158 
159 		dvma_entry_set(index, __pa(kaddr));
160 
161 		iommu_pte[index] |= IOMMU_FULL_BLOCK;
162 //		dvma_entry_inc(index);
163 
164 		kaddr += DVMA_PAGE_SIZE;
165 	}
166 
167 #ifdef DEBUG
168 	for(index = (baddr >> DVMA_PAGE_SHIFT); index < end; index++)
169 		dvma_print(index << DVMA_PAGE_SHIFT);
170 #endif
171 	return 0;
172 
173 }
174 
175 void dvma_unmap_iommu(unsigned long baddr, int len)
176 {
177 
178 	int index, end;
179 
180 
181 	index = baddr >> DVMA_PAGE_SHIFT;
182 	end = (DVMA_PAGE_ALIGN(baddr+len) >> DVMA_PAGE_SHIFT);
183 
184 	for(; index < end ; index++) {
185 		pr_debug("freeing bus mapping %08x\n",
186 			 index << DVMA_PAGE_SHIFT);
187 #if 0
188 		if(!dvma_entry_use(index))
189 			pr_info("dvma_unmap freeing unused entry %04x\n",
190 				index);
191 		else
192 			dvma_entry_dec(index);
193 #endif
194 		dvma_entry_clr(index);
195 	}
196 
197 }
198 
199