jazzdmatlb.c revision 1.9 1 /* $NetBSD: jazzdmatlb.c,v 1.9 2003/07/15 00:04:49 lukem Exp $ */
2 /* $OpenBSD: dma.c,v 1.5 1998/03/01 16:49:57 niklas Exp $ */
3
4 /*-
5 * Copyright (C) 2000 Shuichiro URATA. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Jazz derived system dma driver. Handles resource allocation and
32 * logical (virtual) address remaping.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: jazzdmatlb.c,v 1.9 2003/07/15 00:04:49 lukem Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/extent.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <machine/autoconf.h>
48 #include <machine/bus.h>
49
50 #include <arc/jazz/jazzdmatlbreg.h>
51 #include <arc/jazz/jazzdmatlbvar.h>
52
53 #include <mips/cache.h>
54
55 extern paddr_t kvtophys __P((vaddr_t)); /* XXX */
56
57 /*
58 * Currently, only NET and BIO devices use DMA, and splnet > splbio.
59 */
60 #define spldma() splnet()
61
62 #define NDMATLB (JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t))
63
64 static bus_space_tag_t dmatlb_iot;
65 static bus_space_handle_t dmatlb_ioh;
66
67 static struct extent *dmatlbmap;
68 static jazz_dma_pte_t *dma_tlb;
69
70 /*
71 * Initialize the dma mapping register area and pool.
72 */
73 void
74 jazz_dmatlb_init(iot, ioaddr)
75 bus_space_tag_t iot;
76 bus_addr_t ioaddr;
77 {
78 int err;
79
80 dmatlb_iot = iot;
81 err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh);
82 if (err != 0)
83 panic("jazz_dmatlb_init: cannot map 0x%lx", ioaddr);
84
85 dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE;
86
87 mips_dcache_wbinv_all();/* Make sure no map entries are cached */
88 bzero((char *)dma_tlb, JAZZ_DMATLB_SIZE);
89
90 dmatlbmap = extent_create("dmatlb", 0, NDMATLB, M_DEVBUF, NULL, NULL,
91 EX_NOWAIT);
92 if (dmatlbmap == NULL)
93 panic("jazz_dmatlb_init: cannot create extent map");
94
95 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_MAP,
96 MIPS_KSEG1_TO_PHYS(dma_tlb));
97 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_LIMIT,
98 JAZZ_DMATLB_SIZE);
99 jazz_dmatlb_flush();
100 }
101
102 /*
103 * Allocate an array of 'size' DMA PTEs.
104 * Return address to first pte.
105 */
106 jazz_dma_pte_t *
107 jazz_dmatlb_alloc(npte, boundary, flags, addr)
108 int npte;
109 bus_size_t boundary;
110 int flags;
111 bus_addr_t *addr;
112 {
113 u_long start;
114 int err;
115 int s;
116
117 s = spldma();
118 err = extent_alloc(dmatlbmap, npte, 1, boundary / JAZZ_DMA_PAGE_SIZE,
119 (flags & BUS_DMA_WAITOK) ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT,
120 &start);
121 splx(s);
122
123 if (err)
124 return (NULL);
125
126 *addr = start * JAZZ_DMA_PAGE_SIZE;
127
128 return (dma_tlb + start);
129 }
130
131 /*
132 * Free an array of DMA PTEs.
133 */
134 void
135 jazz_dmatlb_free(addr, npte)
136 bus_addr_t addr;
137 int npte;
138 {
139 u_long start;
140 int s;
141
142 start = addr / JAZZ_DMA_PAGE_SIZE;
143 s = spldma();
144 extent_free(dmatlbmap, start, npte, EX_NOWAIT);
145 splx(s);
146 }
147
148 /*
149 * Map up a virtual address space in dma space given by
150 * the dma control structure.
151 */
152 void
153 jazz_dmatlb_map_va(p, va, size, dma_pte)
154 struct proc *p;
155 vaddr_t va;
156 vsize_t size;
157 jazz_dma_pte_t *dma_pte;
158 {
159 paddr_t pa;
160
161 size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
162 va &= JAZZ_DMA_PAGE_NUM;
163 while (size > 0) {
164 if (p != NULL)
165 (void)pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
166 else
167 pa = kvtophys(va);
168
169 pa &= JAZZ_DMA_PAGE_NUM;
170 dma_pte->lo_addr = pa;
171 dma_pte->hi_addr = 0;
172 dma_pte++;
173 va += JAZZ_DMA_PAGE_SIZE;
174 size -= JAZZ_DMA_PAGE_SIZE;
175 }
176 }
177
178 /*
179 * Map up a physical address space in dma space given by
180 * the dma control structure.
181 */
182 void
183 jazz_dmatlb_map_pa(pa, size, dma_pte)
184 paddr_t pa;
185 psize_t size;
186 jazz_dma_pte_t *dma_pte;
187 {
188 size = jazz_dma_page_round(size + jazz_dma_page_offs(pa));
189 pa &= JAZZ_DMA_PAGE_NUM;
190 while (size > 0) {
191 dma_pte->lo_addr = pa;
192 dma_pte->hi_addr = 0;
193 dma_pte++;
194 pa += JAZZ_DMA_PAGE_SIZE;
195 size -= JAZZ_DMA_PAGE_SIZE;
196 }
197 }
198
199 /*
200 * Prepare for new dma by flushing
201 */
202 void
203 jazz_dmatlb_flush()
204 {
205 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0);
206 }
207