apbus.c revision 1.24 1 /* $NetBSD: apbus.c,v 1.24 2018/09/30 14:09:35 tsutsui Exp $ */
2
3 /*-
4 * Copyright (C) 1999 SHIMIZU Ryo. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.24 2018/09/30 14:09:35 tsutsui Exp $");
31
32 #define __INTR_PRIVATE
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/device.h>
38 #include <sys/proc.h>
39 #include <sys/intr.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <machine/adrsmap.h>
44 #include <machine/autoconf.h>
45 #define _NEWSMIPS_BUS_DMA_PRIVATE
46 #include <machine/bus.h>
47 #include <newsmips/apbus/apbusvar.h>
48
49 static int apbusmatch(device_t, cfdata_t, void *);
50 static void apbusattach(device_t, device_t, void *);
51 static int apbusprint(void *, const char *);
52 #if 0
53 static void *aptokseg0 (void *);
54 #endif
55 static void apbus_dma_unmapped(bus_dma_tag_t, bus_dmamap_t);
56 static int apbus_dma_mapalloc(bus_dma_tag_t, bus_dmamap_t, int);
57 static void apbus_dma_mapfree(bus_dma_tag_t, bus_dmamap_t);
58 static void apbus_dma_mapset(bus_dma_tag_t, bus_dmamap_t);
59 static int apbus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
60 bus_size_t, int, bus_dmamap_t *);
61 static void apbus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
62 static int apbus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
63 struct proc *, int);
64 static int apbus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, struct mbuf *,
65 int);
66 static int apbus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, struct uio *,
67 int);
68 static int apbus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
69 bus_dma_segment_t *, int, bus_size_t, int);
70 static void apbus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
71 bus_size_t, int);
72
73 #define MAXAPDEVNUM 32
74
75 CFATTACH_DECL_NEW(ap, 0,
76 apbusmatch, apbusattach, NULL, NULL);
77
78 #define NLEVEL 2
79 static struct newsmips_intr apintr_tab[NLEVEL];
80
81 static int
82 apbusmatch(device_t parent, cfdata_t cf, void *aux)
83 {
84 struct confargs *ca = aux;
85
86 if (strcmp(ca->ca_name, "ap") != 0)
87 return 0;
88
89 return 1;
90 }
91
92
93 static void
94 apbusattach(device_t parent, device_t self, void *aux)
95 {
96 struct apbus_attach_args child;
97 struct apbus_dev *apdev;
98 struct apbus_ctl *apctl;
99 struct newsmips_intr *ip;
100 int i;
101
102 apbus_map_romwork();
103 mips_set_wbflush(apbus_wbflush);
104
105 *(volatile uint32_t *)(NEWS5000_APBUS_INTST) = 0xffffffff;
106 *(volatile uint32_t *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
107 *(volatile uint32_t *)(NEWS5000_APBUS_CTRL) = 0x00000004;
108 *(volatile uint32_t *)(NEWS5000_APBUS_DMA) = 0xffffffff;
109
110 aprint_normal("\n");
111
112 for (i = 0; i < NLEVEL; i++) {
113 ip = &apintr_tab[i];
114 LIST_INIT(&ip->intr_q);
115 }
116
117 /*
118 * get first ap-device
119 */
120 apdev = apbus_lookupdev(NULL);
121
122 /*
123 * trace device chain
124 */
125 while (apdev) {
126 apctl = apdev->apbd_ctl;
127
128 /*
129 * probe physical device only
130 * (no pseudo device)
131 */
132 if (apctl && apctl->apbc_hwbase) {
133 /*
134 * ... and, all units
135 */
136 while (apctl) {
137 /* make apbus_attach_args for devices */
138 child.apa_name = apdev->apbd_name;
139 child.apa_ctlnum = apctl->apbc_ctlno;
140 child.apa_slotno = apctl->apbc_sl;
141 child.apa_hwbase = apctl->apbc_hwbase;
142
143 config_found(self, &child, apbusprint);
144
145 apctl = apctl->apbc_link;
146 }
147 }
148
149 apdev = apdev->apbd_link;
150 }
151 }
152
153 int
154 apbusprint(void *aux, const char *pnp)
155 {
156 struct apbus_attach_args *a = aux;
157
158 if (pnp)
159 aprint_normal("%s at %s slot%d addr 0x%lx",
160 a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
161
162 return UNCONF;
163 }
164
165 #if 0
166 void *
167 aptokseg0(void *va)
168 {
169 vaddr_t addr = (vaddr_t)va;
170
171 if (addr >= 0xfff00000) {
172 addr -= 0xfff00000;
173 addr += physmem << PGSHIFT;
174 addr += 0x80000000;
175 va = (void *)addr;
176 }
177 return va;
178 }
179 #endif
180
181 void
182 apbus_wbflush(void)
183 {
184 volatile int32_t * const our_wbflush = (int32_t *)NEWS5000_WBFLUSH;
185
186 (*mips_locore_jumpvec.ljv_wbflush)();
187 (void)*our_wbflush;
188 }
189
190 /*
191 * called by hardware interrupt routine
192 */
193 int
194 apbus_intr_dispatch(int level, int stat)
195 {
196 struct newsmips_intr *ip;
197 struct newsmips_intrhand *ih;
198 int nintr;
199
200 ip = &apintr_tab[level];
201
202 nintr = 0;
203 LIST_FOREACH(ih, &ip->intr_q, ih_q) {
204 if (ih->ih_mask & stat) {
205 nintr += (*ih->ih_func)(ih->ih_arg);
206 ih->intr_count.ev_count++;
207 }
208 }
209 return nintr;
210 }
211
212 /*
213 * register device interrupt routine
214 */
215 void *
216 apbus_intr_establish(int level, int mask, int priority, int (*func)(void *),
217 void *arg, const char *name, int ctlno)
218 {
219 struct newsmips_intr *ip;
220 struct newsmips_intrhand *ih, *curih;
221 volatile uint32_t *inten0, *inten1;
222
223 ip = &apintr_tab[level];
224
225 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
226 if (ih == NULL)
227 panic("%s: can't malloc handler info", __func__);
228 ih->ih_mask = mask;
229 ih->ih_priority = priority;
230 ih->ih_func = func;
231 ih->ih_arg = arg;
232 evcnt_attach_dynamic(&ih->intr_count, EVCNT_TYPE_INTR,
233 NULL, "apbus", name);
234
235 if (LIST_EMPTY(&ip->intr_q)) {
236 LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
237 goto done;
238 }
239
240 for (curih = LIST_FIRST(&ip->intr_q);
241 LIST_NEXT(curih, ih_q) != NULL;
242 curih = LIST_NEXT(curih, ih_q)) {
243 if (ih->ih_priority > curih->ih_priority) {
244 LIST_INSERT_BEFORE(curih, ih, ih_q);
245 goto done;
246 }
247 }
248
249 LIST_INSERT_AFTER(curih, ih, ih_q);
250
251 done:
252 switch (level) {
253 case 0:
254 inten0 = (volatile uint32_t *)NEWS5000_INTEN0;
255 *inten0 |= mask;
256 break;
257 case 1:
258 inten1 = (volatile uint32_t *)NEWS5000_INTEN1;
259 *inten1 |= mask;
260 break;
261 }
262
263 return (void *)ih;
264 }
265
266 static void
267 apbus_dma_unmapped(bus_dma_tag_t t, bus_dmamap_t map)
268 {
269 int seg;
270
271 for (seg = 0; seg < map->dm_nsegs; seg++) {
272 /*
273 * set MSB to indicate unmapped DMA.
274 * also need bit 30 for memory over 256MB.
275 */
276 if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
277 map->dm_segs[seg].ds_addr |= 0x80000000;
278 else
279 map->dm_segs[seg].ds_addr |= 0xc0000000;
280 }
281 }
282
283 #define APBUS_NDMAMAP (NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
284 #define APBUS_MAPTBL(n, v) \
285 (*(volatile uint32_t *)(NEWS5000_APBUS_DMAMAP + \
286 NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
287 static uint8_t apbus_dma_maptbl[APBUS_NDMAMAP];
288
289 static int
290 apbus_dma_mapalloc(bus_dma_tag_t t, bus_dmamap_t map, int flags)
291 {
292 int i, j, cnt;
293
294 cnt = round_page(map->_dm_size) / PAGE_SIZE;
295
296 again:
297 for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
298 for (j = 0; j < cnt; j++) {
299 if (apbus_dma_maptbl[i + j])
300 break;
301 }
302 if (j == cnt) {
303 for (j = 0; j < cnt; j++)
304 apbus_dma_maptbl[i + j] = 1;
305 map->_dm_maptbl = i;
306 map->_dm_maptblcnt = cnt;
307 return 0;
308 }
309 }
310 if ((flags & BUS_DMA_NOWAIT) == 0) {
311 tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
312 goto again;
313 }
314 return ENOMEM;
315 }
316
317 static void
318 apbus_dma_mapfree(bus_dma_tag_t t, bus_dmamap_t map)
319 {
320 int i, n;
321
322 if (map->_dm_maptblcnt > 0) {
323 n = map->_dm_maptbl;
324 for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
325 #ifdef DIAGNOSTIC
326 if (apbus_dma_maptbl[n] == 0)
327 panic("freeing free DMA map");
328 APBUS_MAPTBL(n, 0xffffffff); /* causes DMA error */
329 #endif
330 apbus_dma_maptbl[n] = 0;
331 }
332 wakeup(&apbus_dma_maptbl);
333 map->_dm_maptblcnt = 0;
334 }
335 }
336
337 static void
338 apbus_dma_mapset(bus_dma_tag_t t, bus_dmamap_t map)
339 {
340 int i;
341 bus_addr_t addr, eaddr;
342 int seg;
343 bus_dma_segment_t *segs;
344
345 i = 0;
346 for (seg = 0; seg < map->dm_nsegs; seg++) {
347 segs = &map->dm_segs[seg];
348 for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
349 addr < eaddr; addr += PAGE_SIZE, i++) {
350 #ifdef DIAGNOSTIC
351 if (i >= map->_dm_maptblcnt)
352 panic("DMA map table overflow");
353 #endif
354 APBUS_MAPTBL(map->_dm_maptbl + i,
355 NEWS5000_APBUS_MAP_VALID |
356 NEWS5000_APBUS_MAP_COHERENT |
357 (addr >> PGSHIFT));
358 }
359 }
360 map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
361 map->dm_segs[0].ds_len = map->dm_mapsize;
362 map->dm_nsegs = 1;
363 }
364
365 static int
366 apbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
367 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
368 {
369 int error;
370
371 if (flags & NEWSMIPS_DMAMAP_MAPTBL)
372 nsegments = round_page(size) / PAGE_SIZE;
373 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
374 flags, dmamp);
375 if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
376 error = apbus_dma_mapalloc(t, *dmamp, flags);
377 if (error) {
378 _bus_dmamap_destroy(t, *dmamp);
379 *dmamp = NULL;
380 }
381 }
382 return error;
383 }
384
385 static void
386 apbus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
387 {
388
389 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
390 apbus_dma_mapfree(t, map);
391 _bus_dmamap_destroy(t, map);
392 }
393
394 static int
395 apbus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
396 bus_size_t buflen, struct proc *p, int flags)
397 {
398 int error;
399
400 error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
401 if (error == 0) {
402 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
403 apbus_dma_mapset(t, map);
404 else
405 apbus_dma_unmapped(t, map);
406 }
407 return error;
408 }
409
410 static int
411 apbus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
412 int flags)
413 {
414 int error;
415
416 error = _bus_dmamap_load_mbuf(t, map, m0, flags);
417 if (error == 0) {
418 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
419 apbus_dma_mapset(t, map);
420 else
421 apbus_dma_unmapped(t, map);
422 }
423 return error;
424 }
425
426 static int
427 apbus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
428 int flags)
429 {
430 int error;
431
432 error = _bus_dmamap_load_uio(t, map, uio, flags);
433 if (error == 0) {
434 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
435 apbus_dma_mapset(t, map);
436 else
437 apbus_dma_unmapped(t, map);
438 }
439 return error;
440 }
441
442 static int
443 apbus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
444 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
445 {
446 int error;
447
448 error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
449 if (error == 0) {
450 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
451 apbus_dma_mapset(t, map);
452 else
453 apbus_dma_unmapped(t, map);
454 }
455 return error;
456 }
457
458 static void
459 apbus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
460 bus_size_t len, int ops)
461 {
462
463 /*
464 * Flush DMA cache by issuing IO read for the AProm of specified slot.
465 */
466 bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
467
468 bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
469 }
470
471 struct newsmips_bus_dma_tag apbus_dma_tag = {
472 apbus_dmamap_create,
473 apbus_dmamap_destroy,
474 apbus_dmamap_load,
475 apbus_dmamap_load_mbuf,
476 apbus_dmamap_load_uio,
477 apbus_dmamap_load_raw,
478 _bus_dmamap_unload,
479 apbus_dmamap_sync,
480 _bus_dmamem_alloc,
481 _bus_dmamem_free,
482 _bus_dmamem_map,
483 _bus_dmamem_unmap,
484 _bus_dmamem_mmap,
485 };
486
487 struct newsmips_bus_dma_tag *
488 apbus_dmatag_init(struct apbus_attach_args *apa)
489 {
490 struct newsmips_bus_dma_tag *dmat;
491
492 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
493 if (dmat != NULL) {
494 memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
495 dmat->_slotno = apa->apa_slotno;
496 dmat->_slotbaset = 0;
497 dmat->_slotbaseh = apa->apa_hwbase;
498 }
499 return dmat;
500 }
501