uba.c revision 1.58 1 /* $NetBSD: uba.c,v 1.58 2001/11/13 07:11:25 lukem Exp $ */
2 /*
3 * Copyright (c) 1996 Jonathan Stone.
4 * Copyright (c) 1994, 1996 Ludd, University of Lule}, Sweden.
5 * Copyright (c) 1982, 1986 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)uba.c 7.10 (Berkeley) 12/16/90
37 * @(#)autoconf.c 7.20 (Berkeley) 5/9/91
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: uba.c,v 1.58 2001/11/13 07:11:25 lukem Exp $");
42
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/systm.h>
47 #include <sys/map.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/conf.h>
52 #include <sys/dkstat.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <machine/bus.h>
60 #include <machine/scb.h>
61 #include <machine/cpu.h>
62
63 #include <dev/qbus/ubareg.h>
64 #include <dev/qbus/ubavar.h>
65
66 #include "ioconf.h"
67
68 static int ubasearch (struct device *, struct cfdata *, void *);
69 static int ubaprint (void *, const char *);
70
71 /*
72 * If we failed to allocate uba resources, put us on a queue to wait
73 * until there is available resources. Resources to compete about
74 * are map registers and BDPs. This is normally only a problem on
75 * Unibus systems, Qbus systems have more map registers than usable.
76 */
77 void
78 uba_enqueue(struct uba_unit *uu)
79 {
80 struct uba_softc *uh;
81 int s;
82
83 uh = (void *)((struct device *)(uu->uu_softc))->dv_parent;
84
85 s = spluba();
86 SIMPLEQ_INSERT_TAIL(&uh->uh_resq, uu, uu_resq);
87 splx(s);
88 }
89
90 /*
91 * When a routine that uses resources is finished, the next device
92 * in queue for map registers etc is called. If it succeeds to get
93 * resources, call next, and next, and next...
94 * This routine must be called at spluba.
95 */
96 void
97 uba_done(struct uba_softc *uh)
98 {
99 struct uba_unit *uu;
100
101 while ((uu = SIMPLEQ_FIRST(&uh->uh_resq))) {
102 SIMPLEQ_REMOVE_HEAD(&uh->uh_resq, uu, uu_resq);
103 if ((*uu->uu_ready)(uu) == 0) {
104 SIMPLEQ_INSERT_HEAD(&uh->uh_resq, uu, uu_resq);
105 break;
106 }
107 }
108 }
109
110 /*
111 * Each device that needs some handling if an ubareset occurs must
112 * register for reset first through this routine.
113 */
114 void
115 uba_reset_establish(void (*reset)(struct device *), struct device *dev)
116 {
117 struct uba_softc *uh = (void *)dev->dv_parent;
118 struct uba_reset *ur;
119
120 ur = malloc(sizeof(struct uba_reset), M_DEVBUF, M_NOWAIT);
121 if (ur == NULL)
122 panic("uba_reset_establish");
123 ur->ur_dev = dev;
124 ur->ur_reset = reset;
125
126 SIMPLEQ_INSERT_TAIL(&uh->uh_resetq, ur, ur_resetq);
127 }
128
129 /*
130 * Allocate a bunch of map registers and map them to the given address.
131 */
132 int
133 uballoc(struct uba_softc *uh, struct ubinfo *ui, int flags)
134 {
135 int waitok = (flags & UBA_CANTWAIT) == 0;
136 int error;
137
138 if ((error = bus_dmamap_create(uh->uh_dmat, ui->ui_size, 1,
139 ui->ui_size, 0, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT),
140 &ui->ui_dmam)))
141 return error;
142
143 if ((error = bus_dmamap_load(uh->uh_dmat, ui->ui_dmam, ui->ui_vaddr,
144 ui->ui_size, NULL, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT)))) {
145 bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
146 return error;
147 }
148 ui->ui_baddr = ui->ui_dmam->dm_segs[0].ds_addr;
149 return 0;
150 }
151
152 /*
153 * Allocate DMA-able memory and map it on the unibus.
154 */
155 int
156 ubmemalloc(struct uba_softc *uh, struct ubinfo *ui, int flags)
157 {
158 int waitok = (flags & UBA_CANTWAIT ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
159 int error;
160
161 if ((error = bus_dmamem_alloc(uh->uh_dmat, ui->ui_size, NBPG, 0,
162 &ui->ui_seg, 1, &ui->ui_rseg, waitok)))
163 return error;
164 if ((error = bus_dmamem_map(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg,
165 ui->ui_size, &ui->ui_vaddr, waitok|BUS_DMA_COHERENT))) {
166 bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
167 return error;
168 }
169 if ((error = uballoc(uh, ui, flags))) {
170 bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
171 bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
172 }
173 return error;
174 }
175
176 void
177 ubfree(struct uba_softc *uh, struct ubinfo *ui)
178 {
179 bus_dmamap_unload(uh->uh_dmat, ui->ui_dmam);
180 bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
181 }
182
183 void
184 ubmemfree(struct uba_softc *uh, struct ubinfo *ui)
185 {
186 bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
187 bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
188 ubfree(uh, ui);
189 }
190
191 /*
192 * Generate a reset on uba number uban. Then
193 * call each device that asked to be called during attach,
194 * giving it a chance to clean up so as to be able to continue.
195 */
196 void
197 ubareset(struct uba_softc *uh)
198 {
199 struct uba_reset *ur;
200 int s;
201
202 s = spluba();
203 SIMPLEQ_INIT(&uh->uh_resq);
204 printf("%s: reset", uh->uh_dev.dv_xname);
205 (*uh->uh_ubainit)(uh);
206
207 ur = SIMPLEQ_FIRST(&uh->uh_resetq);
208 if (ur) do {
209 printf(" %s", ur->ur_dev->dv_xname);
210 (*ur->ur_reset)(ur->ur_dev);
211 } while ((ur = SIMPLEQ_NEXT(ur, ur_resetq)));
212
213 printf("\n");
214 splx(s);
215 }
216
217 /*
218 * The common attach routine:
219 * Calls the scan routine to search for uba devices.
220 */
221 void
222 uba_attach(struct uba_softc *sc, paddr_t iopagephys)
223 {
224
225 /*
226 * Set last free interrupt vector for devices with
227 * programmable interrupt vectors. Use is to decrement
228 * this number and use result as interrupt vector.
229 */
230 sc->uh_lastiv = 0x200;
231 SIMPLEQ_INIT(&sc->uh_resq);
232 SIMPLEQ_INIT(&sc->uh_resetq);
233
234 /*
235 * Allocate place for unibus I/O space in virtual space.
236 */
237 if (bus_space_map(sc->uh_iot, iopagephys, UBAIOSIZE, 0, &sc->uh_ioh))
238 return;
239
240 if (sc->uh_beforescan)
241 (*sc->uh_beforescan)(sc);
242 /*
243 * Now start searching for devices.
244 */
245 config_search(ubasearch,(struct device *)sc, NULL);
246
247 if (sc->uh_afterscan)
248 (*sc->uh_afterscan)(sc);
249 }
250
251 int
252 ubasearch(struct device *parent, struct cfdata *cf, void *aux)
253 {
254 struct uba_softc *sc = (struct uba_softc *)parent;
255 struct uba_attach_args ua;
256 int i, vec, br;
257
258 ua.ua_ioh = ubdevreg(cf->cf_loc[0]) + sc->uh_ioh;
259 ua.ua_iot = sc->uh_iot;
260 ua.ua_dmat = sc->uh_dmat;
261
262 if (badaddr((caddr_t)ua.ua_ioh, 2) ||
263 (sc->uh_errchk ? (*sc->uh_errchk)(sc):0))
264 goto forgetit;
265
266 scb_vecref(0, 0); /* Clear vector ref */
267 i = (*cf->cf_attach->ca_match) (parent, cf, &ua);
268
269 if (sc->uh_errchk)
270 if ((*sc->uh_errchk)(sc))
271 goto forgetit;
272 if (i == 0)
273 goto forgetit;
274
275 i = scb_vecref(&vec, &br);
276 if (i == 0)
277 goto fail;
278 if (vec == 0)
279 goto fail;
280
281 ua.ua_br = br;
282 ua.ua_cvec = vec;
283 ua.ua_iaddr = cf->cf_loc[0];
284 ua.ua_evcnt = NULL;
285
286 config_attach(parent, cf, &ua, ubaprint);
287 return 0;
288
289 fail:
290 printf("%s%d at %s csr %o %s\n",
291 cf->cf_driver->cd_name, cf->cf_unit, parent->dv_xname,
292 cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
293
294 forgetit:
295 return 0;
296 }
297
298 /*
299 * Print out some interesting info common to all unibus devices.
300 */
301 int
302 ubaprint(void *aux, const char *uba)
303 {
304 struct uba_attach_args *ua = aux;
305
306 printf(" csr %o vec %o ipl %x", ua->ua_iaddr,
307 ua->ua_cvec & 511, ua->ua_br);
308 return UNCONF;
309 }
310
311 /*
312 * Move to machdep eventually
313 */
314 void
315 uba_intr_establish(void *icookie, int vec, void (*ifunc)(void *iarg),
316 void *iarg, struct evcnt *ev)
317 {
318 scb_vecalloc(vec, ifunc, iarg, SCB_ISTACK, ev);
319 }
320