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