1 /* $NetBSD: bus_space_large.c,v 1.9 2023/12/20 06:36:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * For large sparse bus space
35 * (e.g. NEC RISCstation 2250 PCI memory space: 432MB)
36 *
37 * This bus_space uses either wired TLB or normal KSEG2 mapping
38 * for each bus space region.
39 *
40 * Wired TLB will be used on the following cases:
41 * - If requested region is already mapped by wired TLBs.
42 * This is needed for some critical resources which are used
43 * on kernel early stage (e.g. console device resources)
44 * - If requested region size >= ARC_THRESHOLD_TO_USE_WIRED_TLB,
45 * and enough wired TLBs are still free.
46 * In this case, the size of wired TLBs becomes always
47 * MIPS3_WIRED_SIZE (i.e. 16MB). (See wired_map_machdep.c for detail.)
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: bus_space_large.c,v 1.9 2023/12/20 06:36:02 thorpej Exp $");
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55
56 #include <uvm/uvm_extern.h>
57
58 #include <sys/bus.h>
59 #include <machine/wired_map.h>
60
61 static int arc_large_bus_space_compose_handle(bus_space_tag_t, bus_addr_t,
62 bus_size_t, int, bus_space_handle_t *);
63 static int arc_large_bus_space_dispose_handle(bus_space_tag_t,
64 bus_space_handle_t, bus_size_t);
65 static int arc_large_bus_space_paddr(bus_space_tag_t, bus_space_handle_t,
66 paddr_t *);
67
68 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)69 arc_large_bus_space_init(bus_space_tag_t bst, const char *name, paddr_t paddr,
70 bus_addr_t start, bus_size_t size)
71 {
72
73 arc_sparse_bus_space_init(bst, name, paddr, start, size);
74 bst->bs_compose_handle = arc_large_bus_space_compose_handle;
75 bst->bs_dispose_handle = arc_large_bus_space_dispose_handle;
76 bst->bs_paddr = arc_large_bus_space_paddr;
77 }
78
79 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)80 arc_large_bus_space_compose_handle(bus_space_tag_t bst, bus_addr_t addr,
81 bus_size_t size, int flags, bus_space_handle_t *bshp)
82 {
83 paddr_t pa;
84 vaddr_t va;
85
86 pa = bst->bs_pbase + (addr - bst->bs_start);
87 /*
88 * Check whether the physical address is already wired mapped
89 * or not. If not mapped but requested region is large enough,
90 * try to use wired TLB.
91 */
92 if ((va = arc_contiguously_wired_mapped(pa, size)) != 0 ||
93 (/* requested region is large enough? */
94 size >= ARC_THRESHOLD_TO_USE_WIRED_TLB &&
95 /* enough wired TLBs are still available? */
96 (va = arc_map_wired(pa, size)) != 0)) {
97 #ifdef DIAGNOSTIC
98 /*
99 * If wired TLB is used,
100 * we will not make this bus_space region cacheable,
101 * since other bus_space might share this wired TLB.
102 */
103 if (flags & BUS_SPACE_MAP_CACHEABLE)
104 printf("arc_large_bus_space_compose_handle: "
105 "ignore cacheable 0x%llx..0x%llx\n",
106 pa, pa + size - 1);
107 #endif
108 *bshp = va;
109 return 0;
110 }
111 return arc_sparse_bus_space_compose_handle(bst, addr, size, flags,
112 bshp);
113 }
114
115 static int
arc_large_bus_space_dispose_handle(bus_space_tag_t bst,bus_space_handle_t bsh,bus_size_t size)116 arc_large_bus_space_dispose_handle(bus_space_tag_t bst, bus_space_handle_t bsh,
117 bus_size_t size)
118 {
119 paddr_t pa;
120
121 /*
122 * We never free already wired TLB entries,
123 * since the TLBs might be used for other bus_space region.
124 */
125 if (arc_wired_map_extract(bsh, &pa))
126 return 0;
127 return arc_sparse_bus_space_dispose_handle(bst, bsh, size);
128 }
129
130 static int
arc_large_bus_space_paddr(bus_space_tag_t bst,bus_space_handle_t bsh,paddr_t * pap)131 arc_large_bus_space_paddr(bus_space_tag_t bst, bus_space_handle_t bsh,
132 paddr_t *pap)
133 {
134
135 if (arc_wired_map_extract(bsh, pap))
136 return 0;
137 return arc_sparse_bus_space_paddr(bst, bsh, pap);
138 }
139