Home | History | Annotate | Line # | Download | only in apbus
apbus.c revision 1.23
      1 /*	$NetBSD: apbus.c,v 1.23 2018/09/30 06:14:23 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.23 2018/09/30 06:14:23 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 	}
    207 	return nintr;
    208 }
    209 
    210 /*
    211  * register device interrupt routine
    212  */
    213 void *
    214 apbus_intr_establish(int level, int mask, int priority, int (*func)(void *),
    215     void *arg, const char *name, int ctlno)
    216 {
    217 	struct newsmips_intr *ip;
    218 	struct newsmips_intrhand *ih, *curih;
    219 	volatile uint32_t *inten0, *inten1;
    220 
    221 	ip = &apintr_tab[level];
    222 
    223 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    224 	if (ih == NULL)
    225 		panic("%s: can't malloc handler info", __func__);
    226 	ih->ih_mask = mask;
    227 	ih->ih_priority = priority;
    228 	ih->ih_func = func;
    229 	ih->ih_arg = arg;
    230 
    231 	if (LIST_EMPTY(&ip->intr_q)) {
    232 		LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
    233 		goto done;
    234 	}
    235 
    236 	for (curih = LIST_FIRST(&ip->intr_q);
    237 	    LIST_NEXT(curih, ih_q) != NULL;
    238 	    curih = LIST_NEXT(curih, ih_q)) {
    239 		if (ih->ih_priority > curih->ih_priority) {
    240 			LIST_INSERT_BEFORE(curih, ih, ih_q);
    241 			goto done;
    242 		}
    243 	}
    244 
    245 	LIST_INSERT_AFTER(curih, ih, ih_q);
    246 
    247  done:
    248 	switch (level) {
    249 	case 0:
    250 		inten0 = (volatile uint32_t *)NEWS5000_INTEN0;
    251 		*inten0 |= mask;
    252 		break;
    253 	case 1:
    254 		inten1 = (volatile uint32_t *)NEWS5000_INTEN1;
    255 		*inten1 |= mask;
    256 		break;
    257 	}
    258 
    259 	return (void *)ih;
    260 }
    261 
    262 static void
    263 apbus_dma_unmapped(bus_dma_tag_t t, bus_dmamap_t map)
    264 {
    265 	int seg;
    266 
    267 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    268 		/*
    269 		 * set MSB to indicate unmapped DMA.
    270 		 * also need bit 30 for memory over 256MB.
    271 		 */
    272 		if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
    273 			map->dm_segs[seg].ds_addr |= 0x80000000;
    274 		else
    275 			map->dm_segs[seg].ds_addr |= 0xc0000000;
    276 	}
    277 }
    278 
    279 #define	APBUS_NDMAMAP	(NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
    280 #define	APBUS_MAPTBL(n, v)	\
    281 			(*(volatile uint32_t *)(NEWS5000_APBUS_DMAMAP + \
    282 			 NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
    283 static uint8_t apbus_dma_maptbl[APBUS_NDMAMAP];
    284 
    285 static int
    286 apbus_dma_mapalloc(bus_dma_tag_t t, bus_dmamap_t map, int flags)
    287 {
    288 	int i, j, cnt;
    289 
    290 	cnt = round_page(map->_dm_size) / PAGE_SIZE;
    291 
    292  again:
    293 	for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
    294 		for (j = 0; j < cnt; j++) {
    295 			if (apbus_dma_maptbl[i + j])
    296 				break;
    297 		}
    298 		if (j == cnt) {
    299 			for (j = 0; j < cnt; j++)
    300 				apbus_dma_maptbl[i + j] = 1;
    301 			map->_dm_maptbl = i;
    302 			map->_dm_maptblcnt = cnt;
    303 			return 0;
    304 		}
    305 	}
    306 	if ((flags & BUS_DMA_NOWAIT) == 0) {
    307 		tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
    308 		goto again;
    309 	}
    310 	return ENOMEM;
    311 }
    312 
    313 static void
    314 apbus_dma_mapfree(bus_dma_tag_t t, bus_dmamap_t map)
    315 {
    316 	int i, n;
    317 
    318 	if (map->_dm_maptblcnt > 0) {
    319 		n = map->_dm_maptbl;
    320 		for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
    321 #ifdef DIAGNOSTIC
    322 			if (apbus_dma_maptbl[n] == 0)
    323 				panic("freeing free DMA map");
    324 			APBUS_MAPTBL(n, 0xffffffff);	/* causes DMA error */
    325 #endif
    326 			apbus_dma_maptbl[n] = 0;
    327 		}
    328 		wakeup(&apbus_dma_maptbl);
    329 		map->_dm_maptblcnt = 0;
    330 	}
    331 }
    332 
    333 static void
    334 apbus_dma_mapset(bus_dma_tag_t t, bus_dmamap_t map)
    335 {
    336 	int i;
    337 	bus_addr_t addr, eaddr;
    338 	int seg;
    339 	bus_dma_segment_t *segs;
    340 
    341 	i = 0;
    342 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    343 		segs = &map->dm_segs[seg];
    344 		for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
    345 		    addr < eaddr; addr += PAGE_SIZE, i++) {
    346 #ifdef DIAGNOSTIC
    347 			if (i >= map->_dm_maptblcnt)
    348 				panic("DMA map table overflow");
    349 #endif
    350 			APBUS_MAPTBL(map->_dm_maptbl + i,
    351 				NEWS5000_APBUS_MAP_VALID |
    352 				NEWS5000_APBUS_MAP_COHERENT |
    353 				(addr >> PGSHIFT));
    354 		}
    355 	}
    356 	map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
    357 	map->dm_segs[0].ds_len = map->dm_mapsize;
    358 	map->dm_nsegs = 1;
    359 }
    360 
    361 static int
    362 apbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    363     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    364 {
    365 	int error;
    366 
    367 	if (flags & NEWSMIPS_DMAMAP_MAPTBL)
    368 		nsegments = round_page(size) / PAGE_SIZE;
    369 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    370 	    flags, dmamp);
    371 	if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
    372 		error = apbus_dma_mapalloc(t, *dmamp, flags);
    373 		if (error) {
    374 			_bus_dmamap_destroy(t, *dmamp);
    375 			*dmamp = NULL;
    376 		}
    377 	}
    378 	return error;
    379 }
    380 
    381 static void
    382 apbus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    383 {
    384 
    385 	if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    386 		apbus_dma_mapfree(t, map);
    387 	_bus_dmamap_destroy(t, map);
    388 }
    389 
    390 static int
    391 apbus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    392     bus_size_t buflen, struct proc *p, int flags)
    393 {
    394 	int error;
    395 
    396 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    397 	if (error == 0) {
    398 		if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    399 			apbus_dma_mapset(t, map);
    400 		else
    401 			apbus_dma_unmapped(t, map);
    402 	}
    403 	return error;
    404 }
    405 
    406 static int
    407 apbus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    408     int flags)
    409 {
    410 	int error;
    411 
    412 	error = _bus_dmamap_load_mbuf(t, map, m0, 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_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    424     int flags)
    425 {
    426 	int error;
    427 
    428 	error = _bus_dmamap_load_uio(t, map, uio, 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_raw(bus_dma_tag_t t, bus_dmamap_t map,
    440     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    441 {
    442 	int error;
    443 
    444 	error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, 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 void
    455 apbus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    456     bus_size_t len, int ops)
    457 {
    458 
    459 	/*
    460 	 * Flush DMA cache by issuing IO read for the AProm of specified slot.
    461 	 */
    462 	bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
    463 
    464 	bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
    465 }
    466 
    467 struct newsmips_bus_dma_tag apbus_dma_tag = {
    468 	apbus_dmamap_create,
    469 	apbus_dmamap_destroy,
    470 	apbus_dmamap_load,
    471 	apbus_dmamap_load_mbuf,
    472 	apbus_dmamap_load_uio,
    473 	apbus_dmamap_load_raw,
    474 	_bus_dmamap_unload,
    475 	apbus_dmamap_sync,
    476 	_bus_dmamem_alloc,
    477 	_bus_dmamem_free,
    478 	_bus_dmamem_map,
    479 	_bus_dmamem_unmap,
    480 	_bus_dmamem_mmap,
    481 };
    482 
    483 struct newsmips_bus_dma_tag *
    484 apbus_dmatag_init(struct apbus_attach_args *apa)
    485 {
    486 	struct newsmips_bus_dma_tag *dmat;
    487 
    488 	dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
    489 	if (dmat != NULL) {
    490 		memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
    491 		dmat->_slotno = apa->apa_slotno;
    492 		dmat->_slotbaset = 0;
    493 		dmat->_slotbaseh = apa->apa_hwbase;
    494 	}
    495 	return dmat;
    496 }
    497