apbus.c revision 1.26 1 /* $NetBSD: apbus.c,v 1.26 2019/11/10 21:16:31 chs 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.26 2019/11/10 21:16:31 chs 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 volatile uint32_t *news_wbflush;
82
83 static int
84 apbusmatch(device_t parent, cfdata_t cf, void *aux)
85 {
86 struct confargs *ca = aux;
87
88 if (strcmp(ca->ca_name, "ap") != 0)
89 return 0;
90
91 return 1;
92 }
93
94
95 static void
96 apbusattach(device_t parent, device_t self, void *aux)
97 {
98 struct apbus_attach_args child;
99 struct apbus_dev *apdev;
100 struct apbus_ctl *apctl;
101 struct newsmips_intr *ip;
102 int i;
103
104 apbus_map_romwork();
105 mips_set_wbflush(apbus_wbflush);
106
107 if (systype == NEWS5000) {
108 *(volatile uint32_t *)(NEWS5000_APBUS_INTST) = 0xffffffff;
109 *(volatile uint32_t *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
110 *(volatile uint32_t *)(NEWS5000_APBUS_CTRL) = 0x00000004;
111 *(volatile uint32_t *)(NEWS5000_APBUS_DMA) = 0xffffffff;
112 }
113 if (systype == NEWS4000) {
114 *(volatile uint32_t *)0xb60000a4 = 0x1fffffff;
115 *(volatile uint32_t *)0xb6000070 = 0xffffffff;
116 *(volatile uint32_t *)0xb6000098 = 0xffffffff;
117 }
118
119 aprint_normal("\n");
120
121 for (i = 0; i < NLEVEL; i++) {
122 ip = &apintr_tab[i];
123 LIST_INIT(&ip->intr_q);
124 }
125
126 /*
127 * get first ap-device
128 */
129 apdev = apbus_lookupdev(NULL);
130
131 /*
132 * trace device chain
133 */
134 while (apdev) {
135 apctl = apdev->apbd_ctl;
136
137 /*
138 * probe physical device only
139 * (no pseudo device)
140 */
141 if (apctl && apctl->apbc_hwbase) {
142 /*
143 * ... and, all units
144 */
145 while (apctl) {
146 /* make apbus_attach_args for devices */
147 child.apa_name = apdev->apbd_name;
148 child.apa_ctlnum = apctl->apbc_ctlno;
149 child.apa_slotno = apctl->apbc_sl;
150 child.apa_hwbase = apctl->apbc_hwbase;
151
152 config_found(self, &child, apbusprint);
153
154 apctl = apctl->apbc_link;
155 }
156 }
157
158 apdev = apdev->apbd_link;
159 }
160 }
161
162 int
163 apbusprint(void *aux, const char *pnp)
164 {
165 struct apbus_attach_args *a = aux;
166
167 if (pnp)
168 aprint_normal("%s at %s slot%d addr 0x%lx",
169 a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
170
171 return UNCONF;
172 }
173
174 #if 0
175 void *
176 aptokseg0(void *va)
177 {
178 vaddr_t addr = (vaddr_t)va;
179
180 if (addr >= 0xfff00000) {
181 addr -= 0xfff00000;
182 addr += physmem << PGSHIFT;
183 addr += 0x80000000;
184 va = (void *)addr;
185 }
186 return va;
187 }
188 #endif
189
190 void
191 apbus_wbflush(void)
192 {
193
194 (*mips_locore_jumpvec.ljv_wbflush)();
195 (void)*news_wbflush;
196 }
197
198 /*
199 * called by hardware interrupt routine
200 */
201 int
202 apbus_intr_dispatch(int level, int stat)
203 {
204 struct newsmips_intr *ip;
205 struct newsmips_intrhand *ih;
206 int nintr;
207
208 ip = &apintr_tab[level];
209
210 nintr = 0;
211 LIST_FOREACH(ih, &ip->intr_q, ih_q) {
212 if (ih->ih_mask & stat) {
213 nintr += (*ih->ih_func)(ih->ih_arg);
214 ih->intr_count.ev_count++;
215 }
216 }
217 return nintr;
218 }
219
220 /*
221 * register device interrupt routine
222 */
223 void *
224 apbus_intr_establish(int level, int mask, int priority, int (*func)(void *),
225 void *arg, const char *name, int ctlno)
226 {
227 struct newsmips_intr *ip;
228 struct newsmips_intrhand *ih, *curih;
229 volatile uint32_t *inten0, *inten1;
230
231 ip = &apintr_tab[level];
232
233 ih = malloc(sizeof(*ih), M_DEVBUF, M_WAITOK);
234 ih->ih_mask = mask;
235 ih->ih_priority = priority;
236 ih->ih_func = func;
237 ih->ih_arg = arg;
238 evcnt_attach_dynamic(&ih->intr_count, EVCNT_TYPE_INTR,
239 NULL, "apbus", name);
240
241 if (LIST_EMPTY(&ip->intr_q)) {
242 LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
243 goto done;
244 }
245
246 for (curih = LIST_FIRST(&ip->intr_q);
247 LIST_NEXT(curih, ih_q) != NULL;
248 curih = LIST_NEXT(curih, ih_q)) {
249 if (ih->ih_priority > curih->ih_priority) {
250 LIST_INSERT_BEFORE(curih, ih, ih_q);
251 goto done;
252 }
253 }
254
255 LIST_INSERT_AFTER(curih, ih, ih_q);
256
257 done:
258 if (systype == NEWS5000) {
259 inten0 = (uint32_t *)NEWS5000_INTEN0;
260 inten1 = (uint32_t *)NEWS5000_INTEN1;
261 }
262 if (systype == NEWS4000) {
263 inten0 = (uint32_t *)NEWS4000_INTEN0;
264 inten1 = (uint32_t *)NEWS4000_INTEN1;
265 }
266 switch (level) {
267 case 0:
268 *inten0 |= mask;
269 break;
270 case 1:
271 *inten1 |= mask;
272 break;
273 }
274
275 return (void *)ih;
276 }
277
278 static void
279 apbus_dma_unmapped(bus_dma_tag_t t, bus_dmamap_t map)
280 {
281 int seg;
282
283 for (seg = 0; seg < map->dm_nsegs; seg++) {
284 /*
285 * set MSB to indicate unmapped DMA.
286 * also need bit 30 for memory over 256MB.
287 */
288 if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
289 map->dm_segs[seg].ds_addr |= 0x80000000;
290 else
291 map->dm_segs[seg].ds_addr |= 0xc0000000;
292 }
293 }
294
295 #define APBUS_NDMAMAP (NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
296 #define APBUS_MAPTBL(n, v) \
297 (*(volatile uint32_t *)(NEWS5000_APBUS_DMAMAP + \
298 NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
299 static uint8_t apbus_dma_maptbl[APBUS_NDMAMAP];
300
301 static int
302 apbus_dma_mapalloc(bus_dma_tag_t t, bus_dmamap_t map, int flags)
303 {
304 int i, j, cnt;
305
306 cnt = round_page(map->_dm_size) / PAGE_SIZE;
307
308 again:
309 for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
310 for (j = 0; j < cnt; j++) {
311 if (apbus_dma_maptbl[i + j])
312 break;
313 }
314 if (j == cnt) {
315 for (j = 0; j < cnt; j++)
316 apbus_dma_maptbl[i + j] = 1;
317 map->_dm_maptbl = i;
318 map->_dm_maptblcnt = cnt;
319 return 0;
320 }
321 }
322 if ((flags & BUS_DMA_NOWAIT) == 0) {
323 tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
324 goto again;
325 }
326 return ENOMEM;
327 }
328
329 static void
330 apbus_dma_mapfree(bus_dma_tag_t t, bus_dmamap_t map)
331 {
332 int i, n;
333
334 if (map->_dm_maptblcnt > 0) {
335 n = map->_dm_maptbl;
336 for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
337 #ifdef DIAGNOSTIC
338 if (apbus_dma_maptbl[n] == 0)
339 panic("freeing free DMA map");
340 APBUS_MAPTBL(n, 0xffffffff); /* causes DMA error */
341 #endif
342 apbus_dma_maptbl[n] = 0;
343 }
344 wakeup(&apbus_dma_maptbl);
345 map->_dm_maptblcnt = 0;
346 }
347 }
348
349 static void
350 apbus_dma_mapset(bus_dma_tag_t t, bus_dmamap_t map)
351 {
352 int i;
353 bus_addr_t addr, eaddr;
354 int seg;
355 bus_dma_segment_t *segs;
356
357 i = 0;
358 for (seg = 0; seg < map->dm_nsegs; seg++) {
359 segs = &map->dm_segs[seg];
360 for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
361 addr < eaddr; addr += PAGE_SIZE, i++) {
362 #ifdef DIAGNOSTIC
363 if (i >= map->_dm_maptblcnt)
364 panic("DMA map table overflow");
365 #endif
366 APBUS_MAPTBL(map->_dm_maptbl + i,
367 NEWS5000_APBUS_MAP_VALID |
368 NEWS5000_APBUS_MAP_COHERENT |
369 (addr >> PGSHIFT));
370 }
371 }
372 map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
373 map->dm_segs[0].ds_len = map->dm_mapsize;
374 map->dm_nsegs = 1;
375 }
376
377 static int
378 apbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
379 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
380 {
381 int error;
382
383 if (flags & NEWSMIPS_DMAMAP_MAPTBL)
384 nsegments = round_page(size) / PAGE_SIZE;
385 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
386 flags, dmamp);
387 if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
388 error = apbus_dma_mapalloc(t, *dmamp, flags);
389 if (error) {
390 _bus_dmamap_destroy(t, *dmamp);
391 *dmamp = NULL;
392 }
393 }
394 return error;
395 }
396
397 static void
398 apbus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
399 {
400
401 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
402 apbus_dma_mapfree(t, map);
403 _bus_dmamap_destroy(t, map);
404 }
405
406 static int
407 apbus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
408 bus_size_t buflen, struct proc *p, int flags)
409 {
410 int error;
411
412 error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
413 if (error == 0) {
414 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
415 apbus_dma_mapset(t, map);
416 else
417 apbus_dma_unmapped(t, map);
418 }
419 return error;
420 }
421
422 static int
423 apbus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
424 int flags)
425 {
426 int error;
427
428 error = _bus_dmamap_load_mbuf(t, map, m0, flags);
429 if (error == 0) {
430 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
431 apbus_dma_mapset(t, map);
432 else
433 apbus_dma_unmapped(t, map);
434 }
435 return error;
436 }
437
438 static int
439 apbus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
440 int flags)
441 {
442 int error;
443
444 error = _bus_dmamap_load_uio(t, map, uio, flags);
445 if (error == 0) {
446 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
447 apbus_dma_mapset(t, map);
448 else
449 apbus_dma_unmapped(t, map);
450 }
451 return error;
452 }
453
454 static int
455 apbus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
456 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
457 {
458 int error;
459
460 error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
461 if (error == 0) {
462 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
463 apbus_dma_mapset(t, map);
464 else
465 apbus_dma_unmapped(t, map);
466 }
467 return error;
468 }
469
470 static void
471 apbus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
472 bus_size_t len, int ops)
473 {
474
475 /*
476 * Flush DMA cache by issuing IO read for the AProm of specified slot.
477 */
478 bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
479
480 bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
481 }
482
483 struct newsmips_bus_dma_tag apbus_dma_tag = {
484 apbus_dmamap_create,
485 apbus_dmamap_destroy,
486 apbus_dmamap_load,
487 apbus_dmamap_load_mbuf,
488 apbus_dmamap_load_uio,
489 apbus_dmamap_load_raw,
490 _bus_dmamap_unload,
491 apbus_dmamap_sync,
492 _bus_dmamem_alloc,
493 _bus_dmamem_free,
494 _bus_dmamem_map,
495 _bus_dmamem_unmap,
496 _bus_dmamem_mmap,
497 };
498
499 struct newsmips_bus_dma_tag *
500 apbus_dmatag_init(struct apbus_attach_args *apa)
501 {
502 struct newsmips_bus_dma_tag *dmat;
503
504 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_WAITOK);
505 memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
506 dmat->_slotno = apa->apa_slotno;
507 dmat->_slotbaset = 0;
508 dmat->_slotbaseh = apa->apa_hwbase;
509 return dmat;
510 }
511