xref: /openbsd-src/sys/arch/alpha/dev/sgmap_common.c (revision 5be190757ae4fdbd85f768bf239e80e316cb615b)
1*5be19075Smpi /* $OpenBSD: sgmap_common.c,v 1.15 2019/05/13 21:27:59 mpi Exp $ */
28f164632Sart /* $NetBSD: sgmap_common.c,v 1.13 2000/06/29 09:02:57 mrg Exp $ */
38f164632Sart 
48f164632Sart /*-
58f164632Sart  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
68f164632Sart  * All rights reserved.
78f164632Sart  *
88f164632Sart  * This code is derived from software contributed to The NetBSD Foundation
98f164632Sart  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
108f164632Sart  * NASA Ames Research Center.
118f164632Sart  *
128f164632Sart  * Redistribution and use in source and binary forms, with or without
138f164632Sart  * modification, are permitted provided that the following conditions
148f164632Sart  * are met:
158f164632Sart  * 1. Redistributions of source code must retain the above copyright
168f164632Sart  *    notice, this list of conditions and the following disclaimer.
178f164632Sart  * 2. Redistributions in binary form must reproduce the above copyright
188f164632Sart  *    notice, this list of conditions and the following disclaimer in the
198f164632Sart  *    documentation and/or other materials provided with the distribution.
208f164632Sart  *
218f164632Sart  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
228f164632Sart  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
238f164632Sart  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
248f164632Sart  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
258f164632Sart  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
268f164632Sart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
278f164632Sart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
288f164632Sart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
298f164632Sart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
308f164632Sart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
318f164632Sart  * POSSIBILITY OF SUCH DAMAGE.
328f164632Sart  */
338f164632Sart 
34c87b4400Smartin #define _ALPHA_BUS_DMA_PRIVATE
35c87b4400Smartin 
368f164632Sart #include <sys/param.h>
378f164632Sart #include <sys/systm.h>
388f164632Sart #include <sys/kernel.h>
398f164632Sart #include <sys/malloc.h>
408f164632Sart #include <sys/proc.h>
418f164632Sart 
428f164632Sart #include <uvm/uvm_extern.h>
438f164632Sart 
448f164632Sart #include <machine/bus.h>
458f164632Sart 
468f164632Sart #include <alpha/dev/sgmapvar.h>
478f164632Sart 
488f164632Sart /*
498f164632Sart  * Some systems will prefetch the next page during a memory -> device DMA.
508f164632Sart  * This can cause machine checks if there is not a spill page after the
518f164632Sart  * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE).
528f164632Sart  */
538f164632Sart vaddr_t		alpha_sgmap_prefetch_spill_page_va;
548f164632Sart bus_addr_t	alpha_sgmap_prefetch_spill_page_pa;
558f164632Sart 
568f164632Sart void
alpha_sgmap_init(t,sgmap,name,wbase,sgvabase,sgvasize,ptesize,ptva,minptalign)578f164632Sart alpha_sgmap_init(t, sgmap, name, wbase, sgvabase, sgvasize, ptesize, ptva,
588f164632Sart     minptalign)
598f164632Sart 	bus_dma_tag_t t;
608f164632Sart 	struct alpha_sgmap *sgmap;
618f164632Sart 	const char *name;
628f164632Sart 	bus_addr_t wbase;
638f164632Sart 	bus_addr_t sgvabase;
648f164632Sart 	bus_size_t sgvasize;
658f164632Sart 	size_t ptesize;
668f164632Sart 	void *ptva;
678f164632Sart 	bus_size_t minptalign;
688f164632Sart {
698f164632Sart 	bus_dma_segment_t seg;
708f164632Sart 	size_t ptsize;
718f164632Sart 	int rseg;
728f164632Sart 
738f164632Sart 	if (sgvasize & PGOFSET) {
748f164632Sart 		printf("size botch for sgmap `%s'\n", name);
758f164632Sart 		goto die;
768f164632Sart 	}
778f164632Sart 
788f164632Sart 	sgmap->aps_wbase = wbase;
798f164632Sart 	sgmap->aps_sgvabase = sgvabase;
808f164632Sart 	sgmap->aps_sgvasize = sgvasize;
818f164632Sart 
828f164632Sart 	if (ptva != NULL) {
838f164632Sart 		/*
848f164632Sart 		 * We already have a page table; this may be a system
858f164632Sart 		 * where the page table resides in bridge-resident SRAM.
868f164632Sart 		 */
878f164632Sart 		sgmap->aps_pt = ptva;
888f164632Sart 		sgmap->aps_ptpa = 0;
898f164632Sart 	} else {
908f164632Sart 		/*
918f164632Sart 		 * Compute the page table size and allocate it.  At minimum,
928f164632Sart 		 * this must be aligned to the page table size.  However,
93a347b448Sjmc 		 * some platforms have more strict alignment requirements.
948f164632Sart 		 */
95194dd68bSbrad 		ptsize = (sgvasize / PAGE_SIZE) * ptesize;
968f164632Sart 		if (minptalign != 0) {
978f164632Sart 			if (minptalign < ptsize)
988f164632Sart 				minptalign = ptsize;
998f164632Sart 		} else
1008f164632Sart 			minptalign = ptsize;
1018f164632Sart 		if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
1028f164632Sart 		    BUS_DMA_NOWAIT)) {
103be10522bSmiod 			panic("unable to allocate page table for sgmap `%s'",
1048f164632Sart 			    name);
1058f164632Sart 			goto die;
1068f164632Sart 		}
1078f164632Sart 		sgmap->aps_ptpa = seg.ds_addr;
1088f164632Sart 		sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
1098f164632Sart 	}
1108f164632Sart 
1118f164632Sart 	/*
1128f164632Sart 	 * Create the extent map used to manage the virtual address
1138f164632Sart 	 * space.
1148f164632Sart 	 */
1158f164632Sart 	sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
1168f164632Sart 	    M_DEVBUF, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
1178f164632Sart 	if (sgmap->aps_ex == NULL) {
1188f164632Sart 		printf("unable to create extent map for sgmap `%s'\n",
1198f164632Sart 		    name);
1208f164632Sart 		goto die;
1218f164632Sart 	}
122ef2fb739Skettenis 	mtx_init(&sgmap->aps_mtx, IPL_HIGH);
1238f164632Sart 
1248f164632Sart 	/*
1258f164632Sart 	 * Allocate a spill page if that hasn't already been done.
1268f164632Sart 	 */
1278f164632Sart 	if (alpha_sgmap_prefetch_spill_page_va == 0) {
128194dd68bSbrad 		if (bus_dmamem_alloc(t, PAGE_SIZE, 0, 0, &seg, 1, &rseg,
1298f164632Sart 		    BUS_DMA_NOWAIT)) {
1308f164632Sart 			printf("unable to allocate spill page for sgmap `%s'\n",
1318f164632Sart 			    name);
1328f164632Sart 			goto die;
1338f164632Sart 		}
1348f164632Sart 		alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
1358f164632Sart 		alpha_sgmap_prefetch_spill_page_va =
1368f164632Sart 		    ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
137194dd68bSbrad 		bzero((caddr_t)alpha_sgmap_prefetch_spill_page_va, PAGE_SIZE);
1388f164632Sart 	}
1398f164632Sart 
1408f164632Sart 	return;
1418f164632Sart  die:
1428f164632Sart 	panic("alpha_sgmap_init");
1438f164632Sart }
1448f164632Sart 
1458f164632Sart int
alpha_sgmap_dmamap_setup(map,nsegments,flags)14689a84585Sjmatthew alpha_sgmap_dmamap_setup(map, nsegments, flags)
14789a84585Sjmatthew 	bus_dmamap_t map;
14889a84585Sjmatthew 	int nsegments;
14989a84585Sjmatthew 	int flags;
15089a84585Sjmatthew {
1515714159aSdoug 	map->_dm_cookie = mallocarray(nsegments, sizeof(struct extent_region),
15289a84585Sjmatthew 	    M_DEVBUF, (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
153*5be19075Smpi 	if (map->_dm_cookie != NULL)
154*5be19075Smpi 		map->_dm_cookiesize = nsegments * sizeof(struct extent_region);
15589a84585Sjmatthew 	return (map->_dm_cookie == NULL);
15689a84585Sjmatthew }
15789a84585Sjmatthew 
15889a84585Sjmatthew int
alpha_sgmap_dmamap_create(t,size,nsegments,maxsegsz,boundary,flags,dmamp)159c87b4400Smartin alpha_sgmap_dmamap_create(t, size, nsegments, maxsegsz, boundary,
160c87b4400Smartin     flags, dmamp)
161c87b4400Smartin 	bus_dma_tag_t t;
162c87b4400Smartin 	bus_size_t size;
163c87b4400Smartin 	int nsegments;
164c87b4400Smartin 	bus_size_t maxsegsz;
165c87b4400Smartin 	bus_size_t boundary;
166c87b4400Smartin 	int flags;
167c87b4400Smartin 	bus_dmamap_t *dmamp;
168c87b4400Smartin {
169c87b4400Smartin 	bus_dmamap_t map;
170c87b4400Smartin 	int error;
171c87b4400Smartin 
172c87b4400Smartin 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
173c87b4400Smartin 	    boundary, flags, dmamp);
174c87b4400Smartin 	if (error)
175c87b4400Smartin 		return (error);
176c87b4400Smartin 
177c87b4400Smartin 	map = *dmamp;
17889a84585Sjmatthew 	if (alpha_sgmap_dmamap_setup(map, nsegments, flags)) {
179ef2fb739Skettenis 		_bus_dmamap_destroy(t, map);
180ef2fb739Skettenis 		return (ENOMEM);
181ef2fb739Skettenis 	}
182c87b4400Smartin 
18304eae15dSbrad 	/* XXX BUS_DMA_ALLOCNOW */
184c87b4400Smartin 
185ef2fb739Skettenis 	return (0);
186c87b4400Smartin }
187c87b4400Smartin 
188c87b4400Smartin void
alpha_sgmap_dmamap_teardown(map)18989a84585Sjmatthew alpha_sgmap_dmamap_teardown(map)
19089a84585Sjmatthew 	bus_dmamap_t map;
19189a84585Sjmatthew {
192*5be19075Smpi 	free(map->_dm_cookie, M_DEVBUF, map->_dm_cookiesize);
19389a84585Sjmatthew }
19489a84585Sjmatthew 
19589a84585Sjmatthew void
alpha_sgmap_dmamap_destroy(t,map)196c87b4400Smartin alpha_sgmap_dmamap_destroy(t, map)
197c87b4400Smartin 	bus_dma_tag_t t;
198c87b4400Smartin 	bus_dmamap_t map;
199c87b4400Smartin {
20004eae15dSbrad 	KASSERT(map->dm_mapsize == 0);
201c87b4400Smartin 
20289a84585Sjmatthew 	alpha_sgmap_dmamap_teardown(map);
203c87b4400Smartin 	_bus_dmamap_destroy(t, map);
204c87b4400Smartin }
205