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
29
30
31
32
33
34
35
36
37
38
39
40#include "sym_glue.h"
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60static void *___sym_malloc(m_pool_p mp, int size)
61{
62 int i = 0;
63 int s = (1 << SYM_MEM_SHIFT);
64 int j;
65 void *a;
66 m_link_p h = mp->h;
67
68 if (size > SYM_MEM_CLUSTER_SIZE)
69 return NULL;
70
71 while (size > s) {
72 s <<= 1;
73 ++i;
74 }
75
76 j = i;
77 while (!h[j].next) {
78 if (s == SYM_MEM_CLUSTER_SIZE) {
79 h[j].next = (m_link_p) M_GET_MEM_CLUSTER();
80 if (h[j].next)
81 h[j].next->next = NULL;
82 break;
83 }
84 ++j;
85 s <<= 1;
86 }
87 a = h[j].next;
88 if (a) {
89 h[j].next = h[j].next->next;
90 while (j > i) {
91 j -= 1;
92 s >>= 1;
93 h[j].next = (m_link_p) (a+s);
94 h[j].next->next = NULL;
95 }
96 }
97#ifdef DEBUG
98 printf("___sym_malloc(%d) = %p\n", size, (void *) a);
99#endif
100 return a;
101}
102
103
104
105
106static void ___sym_mfree(m_pool_p mp, void *ptr, int size)
107{
108 int i = 0;
109 int s = (1 << SYM_MEM_SHIFT);
110 m_link_p q;
111 unsigned long a, b;
112 m_link_p h = mp->h;
113
114#ifdef DEBUG
115 printf("___sym_mfree(%p, %d)\n", ptr, size);
116#endif
117
118 if (size > SYM_MEM_CLUSTER_SIZE)
119 return;
120
121 while (size > s) {
122 s <<= 1;
123 ++i;
124 }
125
126 a = (unsigned long)ptr;
127
128 while (1) {
129 if (s == SYM_MEM_CLUSTER_SIZE) {
130#ifdef SYM_MEM_FREE_UNUSED
131 M_FREE_MEM_CLUSTER((void *)a);
132#else
133 ((m_link_p) a)->next = h[i].next;
134 h[i].next = (m_link_p) a;
135#endif
136 break;
137 }
138 b = a ^ s;
139 q = &h[i];
140 while (q->next && q->next != (m_link_p) b) {
141 q = q->next;
142 }
143 if (!q->next) {
144 ((m_link_p) a)->next = h[i].next;
145 h[i].next = (m_link_p) a;
146 break;
147 }
148 q->next = q->next->next;
149 a = a & b;
150 s <<= 1;
151 ++i;
152 }
153}
154
155
156
157
158static void *__sym_calloc2(m_pool_p mp, int size, char *name, int uflags)
159{
160 void *p;
161
162 p = ___sym_malloc(mp, size);
163
164 if (DEBUG_FLAGS & DEBUG_ALLOC) {
165 printf ("new %-10s[%4d] @%p.\n", name, size, p);
166 }
167
168 if (p)
169 memset(p, 0, size);
170 else if (uflags & SYM_MEM_WARN)
171 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name, size);
172 return p;
173}
174#define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, SYM_MEM_WARN)
175
176
177
178
179static void __sym_mfree(m_pool_p mp, void *ptr, int size, char *name)
180{
181 if (DEBUG_FLAGS & DEBUG_ALLOC)
182 printf ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
183
184 ___sym_mfree(mp, ptr, size);
185}
186
187
188
189
190
191
192
193static void *___mp0_get_mem_cluster(m_pool_p mp)
194{
195 void *m = sym_get_mem_cluster();
196 if (m)
197 ++mp->nump;
198 return m;
199}
200
201#ifdef SYM_MEM_FREE_UNUSED
202static void ___mp0_free_mem_cluster(m_pool_p mp, void *m)
203{
204 sym_free_mem_cluster(m);
205 --mp->nump;
206}
207#else
208#define ___mp0_free_mem_cluster NULL
209#endif
210
211static struct sym_m_pool mp0 = {
212 NULL,
213 ___mp0_get_mem_cluster,
214 ___mp0_free_mem_cluster
215};
216
217
218
219
220
221
222
223static void * ___get_dma_mem_cluster(m_pool_p mp)
224{
225 m_vtob_p vbp;
226 void *vaddr;
227
228 vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB");
229 if (!vbp)
230 goto out_err;
231
232 vaddr = sym_m_get_dma_mem_cluster(mp, vbp);
233 if (vaddr) {
234 int hc = VTOB_HASH_CODE(vaddr);
235 vbp->next = mp->vtob[hc];
236 mp->vtob[hc] = vbp;
237 ++mp->nump;
238 }
239 return vaddr;
240out_err:
241 return NULL;
242}
243
244#ifdef SYM_MEM_FREE_UNUSED
245
246static void ___free_dma_mem_cluster(m_pool_p mp, void *m)
247{
248 m_vtob_p *vbpp, vbp;
249 int hc = VTOB_HASH_CODE(m);
250
251 vbpp = &mp->vtob[hc];
252 while (*vbpp && (*vbpp)->vaddr != m)
253 vbpp = &(*vbpp)->next;
254 if (*vbpp) {
255 vbp = *vbpp;
256 *vbpp = (*vbpp)->next;
257 sym_m_free_dma_mem_cluster(mp, vbp);
258 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
259 --mp->nump;
260 }
261}
262#endif
263
264
265static inline m_pool_p ___get_dma_pool(m_pool_ident_t dev_dmat)
266{
267 m_pool_p mp;
268 for (mp = mp0.next;
269 mp && !sym_m_pool_match(mp->dev_dmat, dev_dmat);
270 mp = mp->next);
271 return mp;
272}
273
274
275static m_pool_p ___cre_dma_pool(m_pool_ident_t dev_dmat)
276{
277 m_pool_p mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL");
278 if (mp) {
279 mp->dev_dmat = dev_dmat;
280 mp->get_mem_cluster = ___get_dma_mem_cluster;
281#ifdef SYM_MEM_FREE_UNUSED
282 mp->free_mem_cluster = ___free_dma_mem_cluster;
283#endif
284 mp->next = mp0.next;
285 mp0.next = mp;
286 return mp;
287 }
288 return NULL;
289}
290
291#ifdef SYM_MEM_FREE_UNUSED
292
293static void ___del_dma_pool(m_pool_p p)
294{
295 m_pool_p *pp = &mp0.next;
296
297 while (*pp && *pp != p)
298 pp = &(*pp)->next;
299 if (*pp) {
300 *pp = (*pp)->next;
301 __sym_mfree(&mp0, p, sizeof(*p), "MPOOL");
302 }
303}
304#endif
305
306
307static DEFINE_SPINLOCK(sym53c8xx_lock);
308
309
310
311
312void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
313{
314 unsigned long flags;
315 m_pool_p mp;
316 void *m = NULL;
317
318 spin_lock_irqsave(&sym53c8xx_lock, flags);
319 mp = ___get_dma_pool(dev_dmat);
320 if (!mp)
321 mp = ___cre_dma_pool(dev_dmat);
322 if (!mp)
323 goto out;
324 m = __sym_calloc(mp, size, name);
325#ifdef SYM_MEM_FREE_UNUSED
326 if (!mp->nump)
327 ___del_dma_pool(mp);
328#endif
329
330 out:
331 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
332 return m;
333}
334
335void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
336{
337 unsigned long flags;
338 m_pool_p mp;
339
340 spin_lock_irqsave(&sym53c8xx_lock, flags);
341 mp = ___get_dma_pool(dev_dmat);
342 if (!mp)
343 goto out;
344 __sym_mfree(mp, m, size, name);
345#ifdef SYM_MEM_FREE_UNUSED
346 if (!mp->nump)
347 ___del_dma_pool(mp);
348#endif
349 out:
350 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
351}
352
353
354
355
356
357dma_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
358{
359 unsigned long flags;
360 m_pool_p mp;
361 int hc = VTOB_HASH_CODE(m);
362 m_vtob_p vp = NULL;
363 void *a = (void *)((unsigned long)m & ~SYM_MEM_CLUSTER_MASK);
364 dma_addr_t b;
365
366 spin_lock_irqsave(&sym53c8xx_lock, flags);
367 mp = ___get_dma_pool(dev_dmat);
368 if (mp) {
369 vp = mp->vtob[hc];
370 while (vp && vp->vaddr != a)
371 vp = vp->next;
372 }
373 if (!vp)
374 panic("sym: VTOBUS FAILED!\n");
375 b = vp->baddr + (m - a);
376 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
377 return b;
378}
379