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;
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)
130 a = (unsigned long)ptr;
133 if (s == SYM_MEM_CLUSTER_SIZE) {
134 #ifdef SYM_MEM_FREE_UNUSED
135 M_FREE_MEM_CLUSTER((void *)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 void *___mp0_get_mem_cluster(m_pool_p mp)
199 void *m = sym_get_mem_cluster();
205 #ifdef SYM_MEM_FREE_UNUSED
206 static void ___mp0_free_mem_cluster(m_pool_p mp, void *m)
208 sym_free_mem_cluster(m);
212 #define ___mp0_free_mem_cluster NULL
215 static struct sym_m_pool mp0 = {
217 ___mp0_get_mem_cluster,
218 ___mp0_free_mem_cluster
222 * Methods that maintains DMAable pools according to user allocations.
223 * New pools are created on the fly when a new pool id is provided.
224 * They are deleted on the fly when they get emptied.
226 /* Get a memory cluster that matches the DMA constraints of a given pool */
227 static void * ___get_dma_mem_cluster(m_pool_p mp)
232 vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB");
236 vaddr = sym_m_get_dma_mem_cluster(mp, vbp);
238 int hc = VTOB_HASH_CODE(vaddr);
239 vbp->next = mp->vtob[hc];
248 #ifdef SYM_MEM_FREE_UNUSED
249 /* Free a memory cluster and associated resources for DMA */
250 static void ___free_dma_mem_cluster(m_pool_p mp, void *m)
253 int hc = VTOB_HASH_CODE(m);
255 vbpp = &mp->vtob[hc];
256 while (*vbpp && (*vbpp)->vaddr != m)
257 vbpp = &(*vbpp)->next;
260 *vbpp = (*vbpp)->next;
261 sym_m_free_dma_mem_cluster(mp, vbp);
262 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB");
268 /* Fetch the memory pool for a given pool id (i.e. DMA constraints) */
269 static __inline m_pool_p ___get_dma_pool(m_pool_ident_t dev_dmat)
273 mp && !sym_m_pool_match(mp->dev_dmat, dev_dmat);
278 /* Create a new memory DMAable pool (when fetch failed) */
279 static m_pool_p ___cre_dma_pool(m_pool_ident_t dev_dmat)
281 m_pool_p mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL");
283 mp->dev_dmat = dev_dmat;
284 mp->get_mem_cluster = ___get_dma_mem_cluster;
285 #ifdef SYM_MEM_FREE_UNUSED
286 mp->free_mem_cluster = ___free_dma_mem_cluster;
295 #ifdef SYM_MEM_FREE_UNUSED
296 /* Destroy a DMAable memory pool (when got emptied) */
297 static void ___del_dma_pool(m_pool_p p)
299 m_pool_p *pp = &mp0.next;
301 while (*pp && *pp != p)
305 __sym_mfree(&mp0, p, sizeof(*p), "MPOOL");
310 /* This lock protects only the memory allocation/free. */
311 static DEFINE_SPINLOCK(sym53c8xx_lock);
314 * Actual allocator for DMAable memory.
316 void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
322 spin_lock_irqsave(&sym53c8xx_lock, flags);
323 mp = ___get_dma_pool(dev_dmat);
325 mp = ___cre_dma_pool(dev_dmat);
328 m = __sym_calloc(mp, size, name);
329 #ifdef SYM_MEM_FREE_UNUSED
335 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
339 void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
344 spin_lock_irqsave(&sym53c8xx_lock, flags);
345 mp = ___get_dma_pool(dev_dmat);
348 __sym_mfree(mp, m, size, name);
349 #ifdef SYM_MEM_FREE_UNUSED
354 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
358 * Actual virtual to bus physical address translator
359 * for 32 bit addressable DMAable memory.
361 dma_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
365 int hc = VTOB_HASH_CODE(m);
367 void *a = (void *)((unsigned long)m & ~SYM_MEM_CLUSTER_MASK);
370 spin_lock_irqsave(&sym53c8xx_lock, flags);
371 mp = ___get_dma_pool(dev_dmat);
374 while (vp && vp->vaddr != a)
378 panic("sym: VTOBUS FAILED!\n");
379 b = vp->baddr + (m - a);
380 spin_unlock_irqrestore(&sym53c8xx_lock, flags);