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