mcadma_machdep.c revision 1.1 1 1.1 garbled /* $NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $ */
2 1.1 garbled
3 1.1 garbled /*-
4 1.1 garbled * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 garbled * All rights reserved.
6 1.1 garbled *
7 1.1 garbled * This code is derived from software contributed to The NetBSD Foundation
8 1.1 garbled * by Tim Rightnour
9 1.1 garbled *
10 1.1 garbled * Redistribution and use in source and binary forms, with or without
11 1.1 garbled * modification, are permitted provided that the following conditions
12 1.1 garbled * are met:
13 1.1 garbled * 1. Redistributions of source code must retain the above copyright
14 1.1 garbled * notice, this list of conditions and the following disclaimer.
15 1.1 garbled * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 garbled * notice, this list of conditions and the following disclaimer in the
17 1.1 garbled * documentation and/or other materials provided with the distribution.
18 1.1 garbled * 3. All advertising materials mentioning features or use of this software
19 1.1 garbled * must display the following acknowledgement:
20 1.1 garbled * This product includes software developed by the NetBSD
21 1.1 garbled * Foundation, Inc. and its contributors.
22 1.1 garbled * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 garbled * contributors may be used to endorse or promote products derived
24 1.1 garbled * from this software without specific prior written permission.
25 1.1 garbled *
26 1.1 garbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 garbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 garbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 garbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 garbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 garbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 garbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 garbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 garbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 garbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 garbled * POSSIBILITY OF SUCH DAMAGE.
37 1.1 garbled */
38 1.1 garbled
39 1.1 garbled #include <sys/cdefs.h>
40 1.1 garbled __KERNEL_RCSID(0, "$NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $");
41 1.1 garbled
42 1.1 garbled #include <sys/param.h>
43 1.1 garbled #include <sys/systm.h>
44 1.1 garbled #include <sys/syslog.h>
45 1.1 garbled #include <sys/device.h>
46 1.1 garbled #include <sys/malloc.h>
47 1.1 garbled #include <sys/proc.h>
48 1.1 garbled #include <sys/mbuf.h>
49 1.1 garbled
50 1.1 garbled #define _POWERPC_BUS_DMA_PRIVATE
51 1.1 garbled #include <machine/bus.h>
52 1.1 garbled
53 1.1 garbled #include <machine/pio.h>
54 1.1 garbled
55 1.1 garbled #include <dev/mca/mcavar.h>
56 1.1 garbled #include <dev/mca/mcareg.h>
57 1.1 garbled
58 1.1 garbled struct rs6000_dma_cookie {
59 1.1 garbled int id_flags;
60 1.1 garbled uint32_t bid; /* the BID of this IOCC */
61 1.1 garbled uint32_t tce_off; /* offset of the TCE */
62 1.1 garbled uint32_t nroftce;
63 1.1 garbled uint32_t nrofslaves; /* nrof slave channels */
64 1.1 garbled uint32_t slave_regs; /* allocation bitmask */
65 1.1 garbled uint32_t dma_regs; /* allocation bitmask */
66 1.1 garbled };
67 1.1 garbled
68 1.1 garbled /*
69 1.1 garbled * Entry points for MCA DMA. These are mostly wrappers around
70 1.1 garbled * the generic functions that understand how to deal with bounce
71 1.1 garbled * buffers, if necessary.
72 1.1 garbled */
73 1.1 garbled
74 1.1 garbled struct powerpc_bus_dma_tag mca_bus_dma_tag = {
75 1.1 garbled 0, /* _bounce_thresh */
76 1.1 garbled _mca_bus_dmamap_create,
77 1.1 garbled _bus_dmamap_destroy,
78 1.1 garbled _bus_dmamap_load,
79 1.1 garbled _bus_dmamap_load_mbuf,
80 1.1 garbled _bus_dmamap_load_uio,
81 1.1 garbled _bus_dmamap_load_raw,
82 1.1 garbled _bus_dmamap_unload,
83 1.1 garbled NULL, /* _dmamap_sync */
84 1.1 garbled _bus_dmamem_alloc,
85 1.1 garbled _bus_dmamem_free,
86 1.1 garbled _bus_dmamem_map,
87 1.1 garbled _bus_dmamem_unmap,
88 1.1 garbled _bus_dmamem_mmap,
89 1.1 garbled };
90 1.1 garbled
91 1.1 garbled /*
92 1.1 garbled * Allocate a DMA map, and set up DMA channel.
93 1.1 garbled */
94 1.1 garbled static int
95 1.1 garbled _mca_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int flags,
96 1.1 garbled bus_dmamap_t *dmamp, int dmach)
97 1.1 garbled {
98 1.1 garbled int error;
99 1.1 garbled struct rs6000_dma_cookie *cookie;
100 1.1 garbled
101 1.1 garbled #ifdef DEBUG
102 1.1 garbled /* Sanity check */
103 1.1 garbled if (dmach < 0 || dmach >= MAX_DMA_CHANNELS) {
104 1.1 garbled printf("mcadma_create: invalid DMA channel %d\n", dmach);
105 1.1 garbled return EINVAL;
106 1.1 garbled }
107 1.1 garbled
108 1.1 garbled if (size > 65536) {
109 1.1 garbled panic("mca_dmamap_create: dmamap sz %ld > 65536", (long) size);
110 1.1 garbled }
111 1.1 garbled #endif
112 1.1 garbled
113 1.1 garbled /*
114 1.1 garbled * MCA DMA transfer can be maximum 65536 bytes long and must
115 1.1 garbled * be in one chunk. No specific boundary constraints are present.
116 1.1 garbled */
117 1.1 garbled if ((error = _bus_dmamap_create(t, size, 1, 65536, 0, flags, dmamp)))
118 1.1 garbled return error;
119 1.1 garbled
120 1.1 garbled cookie = (struct rs6000_dma_cookie *) (*dmamp)->_dm_cookie;
121 1.1 garbled
122 1.1 garbled if (cookie == NULL) {
123 1.1 garbled /*
124 1.1 garbled * Allocate our cookie if not yet done.
125 1.1 garbled */
126 1.1 garbled cookie = malloc(sizeof(struct rs6000_dma_cookie), M_DMAMAP,
127 1.1 garbled ((flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK) | M_ZERO);
128 1.1 garbled if (cookie == NULL) {
129 1.1 garbled
130 1.1 garbled return ENOMEM;
131 1.1 garbled }
132 1.1 garbled (*dmamp)->_dm_cookie = cookie;
133 1.1 garbled }
134 1.1 garbled
135 1.1 garbled /* Encode DMA channel */
136 1.1 garbled cookie->id_flags &= 0x0f;
137 1.1 garbled cookie->id_flags |= dmach << 4;
138 1.1 garbled
139 1.1 garbled /* Mark the dmamap as using DMA controller. Some devices
140 1.1 garbled * drive DMA themselves, and don't need the MCA DMA controller.
141 1.1 garbled * To distinguish the two, use a flag for dmamaps which use the DMA
142 1.1 garbled * controller.
143 1.1 garbled */
144 1.1 garbled (*dmamp)->_dm_flags |= _MCABUS_DMA_USEDMACTRL;
145 1.1 garbled return 0;
146 1.1 garbled }
147 1.1 garbled
148 1.1 garbled static void
149 1.1 garbled _mca_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
150 1.1 garbled {
151 1.1 garbled struct rs6000_dma_cookie *cookie = map->_dm_cookie;
152 1.1 garbled
153 1.1 garbled free(cookie, M_DMAMAP);
154 1.1 garbled _bus_dmamap_destroy(t, map);
155 1.1 garbled }
156 1.1 garbled
157 1.1 garbled /*
158 1.1 garbled * Load an MCA DMA map with a linear buffer.
159 1.1 garbled */
160 1.1 garbled static int
161 1.1 garbled _mca_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
162 1.1 garbled bus_size_t buflen, struct proc *p, int flags)
163 1.1 garbled {
164 1.1 garbled struct rs6000_dma_cookie *cookie = map->_dm_cookie;
165 1.1 garbled int error;
166 1.1 garbled
167 1.1 garbled /* Make sure that on error condition we return "no valid mappings." */
168 1.1 garbled map->dm_mapsize = 0;
169 1.1 garbled map->dm_nsegs = 0;
170 1.1 garbled
171 1.1 garbled error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
172 1.1 garbled return error;
173 1.1 garbled }
174 1.1 garbled
175 1.1 garbled /*
176 1.1 garbled * Like _mca_bus_dmamap_load(), but for mbufs.
177 1.1 garbled */
178 1.1 garbled static int
179 1.1 garbled _mca_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
180 1.1 garbled int flags)
181 1.1 garbled {
182 1.1 garbled struct rs6000_dma_cookie *cookie = map->_dm_cookie;
183 1.1 garbled int error;
184 1.1 garbled
185 1.1 garbled /* Make sure that on error condition we return "no valid mappings." */
186 1.1 garbled map->dm_mapsize = 0;
187 1.1 garbled map->dm_nsegs = 0;
188 1.1 garbled
189 1.1 garbled #ifdef DIAGNOSTIC
190 1.1 garbled if ((m0->m_flags & M_PKTHDR) == 0)
191 1.1 garbled panic("_mca_bus_dmamap_load_mbuf: no packet header");
192 1.1 garbled #endif
193 1.1 garbled
194 1.1 garbled if (m0->m_pkthdr.len > map->_dm_size)
195 1.1 garbled return EINVAL;
196 1.1 garbled
197 1.1 garbled error = _bus_dmamap_load_mbuf(t, map, m0, flags);
198 1.1 garbled return error;
199 1.1 garbled }
200 1.1 garbled
201 1.1 garbled /*
202 1.1 garbled * Like _mca_bus_dmamap_load(), but for uios.
203 1.1 garbled */
204 1.1 garbled static int
205 1.1 garbled _mca_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
206 1.1 garbled int flags)
207 1.1 garbled {
208 1.1 garbled
209 1.1 garbled panic("_mca_bus_dmamap_load_uio: not implemented");
210 1.1 garbled }
211 1.1 garbled
212 1.1 garbled /*
213 1.1 garbled * Like _mca_bus_dmamap_load(), but for raw memory allocated with
214 1.1 garbled * bus_dmamem_alloc().
215 1.1 garbled */
216 1.1 garbled static int
217 1.1 garbled _mca_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
218 1.1 garbled bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
219 1.1 garbled {
220 1.1 garbled
221 1.1 garbled panic("_mca_bus_dmamap_load_raw: not implemented");
222 1.1 garbled }
223 1.1 garbled
224 1.1 garbled /*
225 1.1 garbled * Unload an MCA DMA map.
226 1.1 garbled */
227 1.1 garbled static void
228 1.1 garbled _mca_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
229 1.1 garbled {
230 1.1 garbled
231 1.1 garbled _bus_dmamap_unload(t, map);
232 1.1 garbled }
233 1.1 garbled
234 1.1 garbled /*
235 1.1 garbled * Allocate memory safe for MCA DMA.
236 1.1 garbled */
237 1.1 garbled static int
238 1.1 garbled _mca_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
239 1.1 garbled bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
240 1.1 garbled int flags)
241 1.1 garbled {
242 1.1 garbled paddr_t high;
243 1.1 garbled
244 1.1 garbled high = trunc_page(avail_end);
245 1.1 garbled
246 1.1 garbled return _bus_dmamem_alloc_range(t, size, alignment, boundary,
247 1.1 garbled segs, nsegs, rsegs, flags, 0, high);
248 1.1 garbled }
249