xref: /netbsd-src/sys/arch/arc/arc/bus_space_large.c (revision 8ff6f65dafc58186614d149001c905dec4c934a2)
1*8ff6f65dSthorpej /*	$NetBSD: bus_space_large.c,v 1.9 2023/12/20 06:36:02 thorpej Exp $	*/
2861ce3dcSsoda 
3861ce3dcSsoda /*-
4861ce3dcSsoda  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5861ce3dcSsoda  * All rights reserved.
6861ce3dcSsoda  *
7861ce3dcSsoda  * This code is derived from software contributed to The NetBSD Foundation
8861ce3dcSsoda  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9861ce3dcSsoda  * Simulation Facility, NASA Ames Research Center.
10861ce3dcSsoda  *
11861ce3dcSsoda  * Redistribution and use in source and binary forms, with or without
12861ce3dcSsoda  * modification, are permitted provided that the following conditions
13861ce3dcSsoda  * are met:
14861ce3dcSsoda  * 1. Redistributions of source code must retain the above copyright
15861ce3dcSsoda  *    notice, this list of conditions and the following disclaimer.
16861ce3dcSsoda  * 2. Redistributions in binary form must reproduce the above copyright
17861ce3dcSsoda  *    notice, this list of conditions and the following disclaimer in the
18861ce3dcSsoda  *    documentation and/or other materials provided with the distribution.
19861ce3dcSsoda  *
20861ce3dcSsoda  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21861ce3dcSsoda  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22861ce3dcSsoda  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23861ce3dcSsoda  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24861ce3dcSsoda  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25861ce3dcSsoda  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26861ce3dcSsoda  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27861ce3dcSsoda  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28861ce3dcSsoda  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29861ce3dcSsoda  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30861ce3dcSsoda  * POSSIBILITY OF SUCH DAMAGE.
31861ce3dcSsoda  */
32861ce3dcSsoda 
33861ce3dcSsoda /*
34861ce3dcSsoda  * For large sparse bus space
35861ce3dcSsoda  * (e.g. NEC RISCstation 2250 PCI memory space: 432MB)
36861ce3dcSsoda  *
37861ce3dcSsoda  * This bus_space uses either wired TLB or normal KSEG2 mapping
38861ce3dcSsoda  * for each bus space region.
39861ce3dcSsoda  *
40861ce3dcSsoda  * Wired TLB will be used on the following cases:
41861ce3dcSsoda  * - If requested region is already mapped by wired TLBs.
42861ce3dcSsoda  *	This is needed for some critical resources which are used
43861ce3dcSsoda  *	on kernel early stage (e.g. console device resources)
44861ce3dcSsoda  * - If requested region size >= ARC_THRESHOLD_TO_USE_WIRED_TLB,
45861ce3dcSsoda  *   and enough wired TLBs are still free.
46861ce3dcSsoda  *	In this case, the size of wired TLBs becomes always
47fdb3b14bStsutsui  *	MIPS3_WIRED_SIZE (i.e. 16MB). (See wired_map_machdep.c for detail.)
48861ce3dcSsoda  */
49861ce3dcSsoda 
50a4183603Slukem #include <sys/cdefs.h>
51*8ff6f65dSthorpej __KERNEL_RCSID(0, "$NetBSD: bus_space_large.c,v 1.9 2023/12/20 06:36:02 thorpej Exp $");
52a4183603Slukem 
53861ce3dcSsoda #include <sys/param.h>
54861ce3dcSsoda #include <sys/systm.h>
5547ef8ee9Smrg 
5647ef8ee9Smrg #include <uvm/uvm_extern.h>
57861ce3dcSsoda 
58cf10107dSdyoung #include <sys/bus.h>
59fdb3b14bStsutsui #include <machine/wired_map.h>
60861ce3dcSsoda 
617fe2a5a0Stsutsui static int arc_large_bus_space_compose_handle(bus_space_tag_t, bus_addr_t,
627fe2a5a0Stsutsui     bus_size_t, int, bus_space_handle_t *);
637fe2a5a0Stsutsui static int arc_large_bus_space_dispose_handle(bus_space_tag_t,
647fe2a5a0Stsutsui     bus_space_handle_t, bus_size_t);
657fe2a5a0Stsutsui static int arc_large_bus_space_paddr(bus_space_tag_t, bus_space_handle_t,
667fe2a5a0Stsutsui     paddr_t *);
67861ce3dcSsoda 
68861ce3dcSsoda void
arc_large_bus_space_init(bus_space_tag_t bst,const char * name,paddr_t paddr,bus_addr_t start,bus_size_t size)697fe2a5a0Stsutsui arc_large_bus_space_init(bus_space_tag_t bst, const char *name, paddr_t paddr,
707fe2a5a0Stsutsui     bus_addr_t start, bus_size_t size)
71861ce3dcSsoda {
727fe2a5a0Stsutsui 
73861ce3dcSsoda 	arc_sparse_bus_space_init(bst, name, paddr, start, size);
74861ce3dcSsoda 	bst->bs_compose_handle = arc_large_bus_space_compose_handle;
75861ce3dcSsoda 	bst->bs_dispose_handle = arc_large_bus_space_dispose_handle;
76861ce3dcSsoda 	bst->bs_paddr = arc_large_bus_space_paddr;
77861ce3dcSsoda }
78861ce3dcSsoda 
79861ce3dcSsoda static int
arc_large_bus_space_compose_handle(bus_space_tag_t bst,bus_addr_t addr,bus_size_t size,int flags,bus_space_handle_t * bshp)807fe2a5a0Stsutsui arc_large_bus_space_compose_handle(bus_space_tag_t bst, bus_addr_t addr,
817fe2a5a0Stsutsui     bus_size_t size, int flags, bus_space_handle_t *bshp)
82861ce3dcSsoda {
83861ce3dcSsoda 	paddr_t pa;
84861ce3dcSsoda 	vaddr_t va;
85861ce3dcSsoda 
86861ce3dcSsoda 	pa = bst->bs_pbase + (addr - bst->bs_start);
87861ce3dcSsoda 	/*
88861ce3dcSsoda 	 * Check whether the physical address is already wired mapped
89861ce3dcSsoda 	 * or not.  If not mapped but requested region is large enough,
90861ce3dcSsoda 	 * try to use wired TLB.
91861ce3dcSsoda 	 */
92861ce3dcSsoda 	if ((va = arc_contiguously_wired_mapped(pa, size)) != 0 ||
93861ce3dcSsoda 	    (/* requested region is large enough? */
94861ce3dcSsoda 	     size >= ARC_THRESHOLD_TO_USE_WIRED_TLB &&
95861ce3dcSsoda 	     /* enough wired TLBs are still available? */
96861ce3dcSsoda 	     (va = arc_map_wired(pa, size)) != 0)) {
97861ce3dcSsoda #ifdef DIAGNOSTIC
98861ce3dcSsoda 		/*
99861ce3dcSsoda 		 * If wired TLB is used,
100861ce3dcSsoda 		 * we will not make this bus_space region cacheable,
101861ce3dcSsoda 		 * since other bus_space might share this wired TLB.
102861ce3dcSsoda 		 */
103861ce3dcSsoda 		if (flags & BUS_SPACE_MAP_CACHEABLE)
104861ce3dcSsoda 			printf("arc_large_bus_space_compose_handle: "
105861ce3dcSsoda 			    "ignore cacheable 0x%llx..0x%llx\n",
106861ce3dcSsoda 			    pa, pa + size - 1);
107861ce3dcSsoda #endif
108861ce3dcSsoda 		*bshp = va;
1097fe2a5a0Stsutsui 		return 0;
110861ce3dcSsoda 	}
1117fe2a5a0Stsutsui 	return arc_sparse_bus_space_compose_handle(bst, addr, size, flags,
1127fe2a5a0Stsutsui 	    bshp);
113861ce3dcSsoda }
114861ce3dcSsoda 
115861ce3dcSsoda static int
arc_large_bus_space_dispose_handle(bus_space_tag_t bst,bus_space_handle_t bsh,bus_size_t size)1167fe2a5a0Stsutsui arc_large_bus_space_dispose_handle(bus_space_tag_t bst, bus_space_handle_t bsh,
1177fe2a5a0Stsutsui     bus_size_t size)
118861ce3dcSsoda {
119861ce3dcSsoda 	paddr_t pa;
120861ce3dcSsoda 
121861ce3dcSsoda 	/*
122861ce3dcSsoda 	 * We never free already wired TLB entries,
123861ce3dcSsoda 	 * since the TLBs might be used for other bus_space region.
124861ce3dcSsoda 	 */
125861ce3dcSsoda 	if (arc_wired_map_extract(bsh, &pa))
1267fe2a5a0Stsutsui 		return 0;
1277fe2a5a0Stsutsui 	return arc_sparse_bus_space_dispose_handle(bst, bsh, size);
128861ce3dcSsoda }
129861ce3dcSsoda 
130861ce3dcSsoda static int
arc_large_bus_space_paddr(bus_space_tag_t bst,bus_space_handle_t bsh,paddr_t * pap)1317fe2a5a0Stsutsui arc_large_bus_space_paddr(bus_space_tag_t bst, bus_space_handle_t bsh,
1327fe2a5a0Stsutsui     paddr_t *pap)
133861ce3dcSsoda {
1347fe2a5a0Stsutsui 
135861ce3dcSsoda 	if (arc_wired_map_extract(bsh, pap))
1367fe2a5a0Stsutsui 		return 0;
1377fe2a5a0Stsutsui 	return arc_sparse_bus_space_paddr(bst, bsh, pap);
138861ce3dcSsoda }
139