jazzdmatlb.c revision 1.3.2.4 1 /* $NetBSD: jazzdmatlb.c,v 1.3.2.4 2001/01/05 17:33:58 bouyer 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/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/extent.h>
39 #include <sys/device.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <machine/autoconf.h>
44 #include <machine/bus.h>
45
46 #include <arc/jazz/jazzdmatlbreg.h>
47 #include <arc/jazz/jazzdmatlbvar.h>
48
49 extern paddr_t kvtophys __P((vaddr_t)); /* XXX */
50
51 /*
52 * Currently, only NET and BIO devices use DMA, and splnet > splbio.
53 */
54 #define spldma() splnet()
55
56 #define NDMATLB (JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t))
57
58 static bus_space_tag_t dmatlb_iot;
59 static bus_space_handle_t dmatlb_ioh;
60
61 static struct extent *dmatlbmap;
62 static jazz_dma_pte_t *dma_tlb;
63
64 /*
65 * Initialize the dma mapping register area and pool.
66 */
67 void
68 jazz_dmatlb_init(iot, ioaddr)
69 bus_space_tag_t iot;
70 bus_addr_t ioaddr;
71 {
72 int err;
73
74 dmatlb_iot = iot;
75 err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh);
76 if (err != 0)
77 panic("jazz_dmatlb_init: cannot map 0x%lx\n", ioaddr);
78
79 dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE;
80
81 mips3_FlushCache(); /* Make sure no map entries are cached */
82 bzero((char *)dma_tlb, JAZZ_DMATLB_SIZE);
83
84 dmatlbmap = extent_create("dmatlb", 0, NDMATLB, M_DEVBUF, NULL, NULL,
85 EX_NOWAIT);
86 if (dmatlbmap == NULL)
87 panic("jazz_dmatlb_init: cannot create extent map");
88
89 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_MAP,
90 MIPS_KSEG1_TO_PHYS(dma_tlb));
91 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_LIMIT,
92 JAZZ_DMATLB_SIZE);
93 jazz_dmatlb_flush();
94 }
95
96 /*
97 * Allocate an array of 'size' DMA PTEs.
98 * Return address to first pte.
99 */
100 jazz_dma_pte_t *
101 jazz_dmatlb_alloc(npte, boundary, flags, addr)
102 int npte;
103 bus_size_t boundary;
104 int flags;
105 bus_addr_t *addr;
106 {
107 u_long start;
108 int err;
109 int s;
110
111 s = spldma();
112 err = extent_alloc(dmatlbmap, npte, 1, boundary / JAZZ_DMA_PAGE_SIZE,
113 (flags & BUS_DMA_WAITOK) ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT,
114 &start);
115 splx(s);
116
117 if (err)
118 return (NULL);
119
120 *addr = start * JAZZ_DMA_PAGE_SIZE;
121
122 return (dma_tlb + start);
123 }
124
125 /*
126 * Free an array of DMA PTEs.
127 */
128 void
129 jazz_dmatlb_free(addr, npte)
130 bus_addr_t addr;
131 int npte;
132 {
133 u_long start;
134 int s;
135
136 start = addr / JAZZ_DMA_PAGE_SIZE;
137 s = spldma();
138 extent_free(dmatlbmap, start, npte, EX_NOWAIT);
139 splx(s);
140 }
141
142 /*
143 * Map up a virtual address space in dma space given by
144 * the dma control structure.
145 */
146 void
147 jazz_dmatlb_map_va(p, va, size, dma_pte)
148 struct proc *p;
149 vaddr_t va;
150 vsize_t size;
151 jazz_dma_pte_t *dma_pte;
152 {
153 paddr_t pa;
154
155 size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
156 va &= JAZZ_DMA_PAGE_NUM;
157 while (size > 0) {
158 if (p != NULL)
159 (void)pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
160 else
161 pa = kvtophys(va);
162
163 pa &= JAZZ_DMA_PAGE_NUM;
164 dma_pte->lo_addr = pa;
165 dma_pte->hi_addr = 0;
166 dma_pte++;
167 va += JAZZ_DMA_PAGE_SIZE;
168 size -= JAZZ_DMA_PAGE_SIZE;
169 }
170 }
171
172 /*
173 * Map up a physical address space in dma space given by
174 * the dma control structure.
175 */
176 void
177 jazz_dmatlb_map_pa(pa, size, dma_pte)
178 paddr_t pa;
179 psize_t size;
180 jazz_dma_pte_t *dma_pte;
181 {
182 size = jazz_dma_page_round(size + jazz_dma_page_offs(pa));
183 pa &= JAZZ_DMA_PAGE_NUM;
184 while (size > 0) {
185 dma_pte->lo_addr = pa;
186 dma_pte->hi_addr = 0;
187 dma_pte++;
188 pa += JAZZ_DMA_PAGE_SIZE;
189 size -= JAZZ_DMA_PAGE_SIZE;
190 }
191 }
192
193 /*
194 * Prepare for new dma by flushing
195 */
196 void
197 jazz_dmatlb_flush()
198 {
199 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0);
200 }
201