2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
7 * This driver is derived from the Linux sym53c8xx driver.
8 * Copyright (C) 1998-2000 Gerard Roudier
10 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
11 * a port of the FreeBSD ncr driver to Linux-1.2.13.
13 * The original ncr driver has been written for 386bsd and FreeBSD by
14 * Wolfgang Stanglmeier <wolf@cologne.de>
15 * Stefan Esser <se@mi.Uni-Koeln.de>
16 * Copyright (C) 1994 Wolfgang Stanglmeier
18 * Other major contributions:
20 * NVRAM detection and reading.
21 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23 *-----------------------------------------------------------------------------
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 #include <dev/sym/sym_glue.h>
47 * Simple power of two buddy-like generic allocator.
48 * Provides naturally aligned memory chunks.
50 * This simple code is not intended to be fast, but to
51 * provide power of 2 aligned memory allocations.
52 * Since the SCRIPTS processor only supplies 8 bit arithmetic,
53 * this allocator allows simple and fast address calculations
54 * from the SCRIPTS code. In addition, cache line alignment
55 * is guaranteed for power of 2 cache line size.
57 * This allocator has been developped for the Linux sym53c8xx
58 * driver, since this O/S does not provide naturally aligned
60 * It has the advantage of allowing the driver to use private
61 * pages of memory that will be useful if we ever need to deal
62 * with IO MMUs for PCI.
64 static void *___sym_malloc(m_pool_p mp, int size)
67 int s = (1 << SYM_MEM_SHIFT);
72 if (size > SYM_MEM_CLUSTER_SIZE)
82 if (s == SYM_MEM_CLUSTER_SIZE) {
83 h[j].next = (m_link_p) M_GET_MEM_CLUSTER();
85 h[j].next->next = NULL;
91 a = (m_addr_t) h[j].next;
93 h[j].next = h[j].next->next;
97 h[j].next = (m_link_p) (a+s);
98 h[j].next->next = NULL;
102 printf("___sym_malloc(%d) = %p\n", size, (void *) a);
108 * Counter-part of the generic allocator.
110 static void ___sym_mfree(m_pool_p mp, void *ptr, int size)
113 int s = (1 << SYM_MEM_SHIFT);
119 printf("___sym_mfree(%p, %d)\n", ptr, size);
122 if (size > SYM_MEM_CLUSTER_SIZE)
133 if (s == SYM_MEM_CLUSTER_SIZE) {
134 #ifdef SYM_MEM_FREE_UNUSED
135 M_FREE_MEM_CLUSTER(a);
137 ((m_link_p) a)->next = h[i].next;
138 h[i].next = (m_link_p) a;
144 while (q->next && q->next != (m_link_p) b) {
148 ((m_link_p) a)->next = h[i].next;
149 h[i].next = (m_link_p) a;
152 q->next = q->next->next;
160 * Verbose and zeroing allocator that wrapps to the generic allocator.
162 static void *__sym_calloc2(m_pool_p mp, int size, char *name, int uflags)
166 p = ___sym_malloc(mp, size);
168 if (DEBUG_FLAGS & DEBUG_ALLOC) {
169 printf ("new %-10s[%4d] @%p.\n", name, size, p);
174 else if (uflags & SYM_MEM_WARN)
175 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name, size);
178 #define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, SYM_MEM_WARN)
183 static void __sym_mfree(m_pool_p mp, void *ptr, int size, char *name)
185 if (DEBUG_FLAGS & DEBUG_ALLOC)
186 printf ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
188 ___sym_mfree(mp, ptr, size);
192 * Default memory pool we donnot need to involve in DMA.
194 * With DMA abstraction, we use functions (methods), to
195 * distinguish between non DMAable memory and DMAable memory.
197 static m_addr_t ___mp0_get_mem_cluster(m_pool_p mp)
199 m_addr_t m = (m_addr_t) sym_get_mem_cluster();
205 #ifdef SYM_MEM_FREE_UNUSED
206 static void ___mp0_free_mem_cluster(m_pool_p mp, m_addr_t m)
208 sym_free_mem_cluster(m);
213 #ifdef SYM_MEM_FREE_UNUSED
214 static struct sym_m_pool mp0 =
215 {NULL, ___mp0_get_mem_cluster, ___mp0_free_mem_cluster};
217 static struct sym_m_pool mp0 =
218 {NULL, ___mp0_get_mem_cluster};
222 * Actual memory allocation routine for non-DMAed memory.
224 void *sym_calloc_unlocked(int size, char *name)
227 m = __sym_calloc(&mp0, size, name);
234 void sym_mfree_unlocked(void *ptr, int size, char *name)
236 __sym_mfree(&mp0, ptr, size, name);
240 * Methods that maintains DMAable pools according to user allocations.
241 * New pools are created on the fly when a new pool id is provided.
242 * They are deleted on the fly when they get emptied.
244 /* Get a memory cluster that matches the DMA contraints of a given pool */
245 static m_addr_t ___get_dma_mem_cluster(m_pool_p mp)
250 vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB");
254 vaddr = sym_m_get_dma_mem_cluster(mp, vbp);
256 int hc = VTOB_HASH_CODE(vaddr);
257 vbp->next = mp->vtob[hc];
260 return (m_addr_t) vaddr;
267 #ifdef SYM_MEM_FREE_UNUSED
268 /* Free a memory cluster and associated resources for DMA */
269 static void ___free_dma_mem_cluster(m_pool_p mp, m_addr_t m)
272 int hc = VTOB_HASH_CODE(m);
274 vbpp = &mp->vtob[hc];
275 while (*vbpp && (*vbpp)->vaddr != m)
276 vbpp = &(*vbpp)->next;
279 *vbpp = (*vbpp)->next;
280 sym_m_free_dma_mem_cluster(mp, vbp);
281 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
287 /* Fetch the memory pool for a given pool id (i.e. DMA constraints) */
288 static __inline m_pool_p ___get_dma_pool(m_pool_ident_t dev_dmat)
292 mp && !sym_m_pool_match(mp->dev_dmat, dev_dmat);
297 /* Create a new memory DMAable pool (when fetch failed) */
298 static m_pool_p ___cre_dma_pool(m_pool_ident_t dev_dmat)
302 mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL");
304 mp->dev_dmat = dev_dmat;
305 if (!sym_m_create_dma_mem_tag(mp)) {
306 mp->get_mem_cluster = ___get_dma_mem_cluster;
307 #ifdef SYM_MEM_FREE_UNUSED
308 mp->free_mem_cluster = ___free_dma_mem_cluster;
316 __sym_mfree(&mp0, mp, sizeof(*mp), "MPOOL");
320 #ifdef SYM_MEM_FREE_UNUSED
321 /* Destroy a DMAable memory pool (when got emptied) */
322 static void ___del_dma_pool(m_pool_p p)
324 m_pool_p *pp = &mp0.next;
326 while (*pp && *pp != p)
330 sym_m_delete_dma_mem_tag(p);
331 __sym_mfree(&mp0, p, sizeof(*p), "MPOOL");
337 * Actual allocator for DMAable memory.
339 void *__sym_calloc_dma_unlocked(m_pool_ident_t dev_dmat, int size, char *name)
344 mp = ___get_dma_pool(dev_dmat);
346 mp = ___cre_dma_pool(dev_dmat);
348 m = __sym_calloc(mp, size, name);
349 #ifdef SYM_MEM_FREE_UNUSED
361 __sym_mfree_dma_unlocked(m_pool_ident_t dev_dmat, void *m, int size, char *name)
365 mp = ___get_dma_pool(dev_dmat);
367 __sym_mfree(mp, m, size, name);
368 #ifdef SYM_MEM_FREE_UNUSED
375 * Actual virtual to bus physical address translator
376 * for 32 bit addressable DMAable memory.
378 u32 __vtobus_unlocked(m_pool_ident_t dev_dmat, void *m)
381 int hc = VTOB_HASH_CODE(m);
383 m_addr_t a = ((m_addr_t) m) & ~SYM_MEM_CLUSTER_MASK;
385 mp = ___get_dma_pool(dev_dmat);
388 while (vp && (m_addr_t) vp->vaddr != a)
392 panic("sym: VTOBUS FAILED!\n");
393 return (u32)(vp ? vp->baddr + (((m_addr_t) m) - a) : 0);