1 /*        $NetBSD: bus_space.c,v 1.22 2023/04/21 23:01:59 tsutsui Exp $         */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Implementation of bus_space mapping for the hp300.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.22 2023/04/21 23:01:59 tsutsui Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/extent.h>
42 
43 #include <machine/autoconf.h>
44 #include <machine/bus.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 extern int *nofault;
49 
50 int
bus_space_map(bus_space_tag_t t,bus_addr_t bpa,bus_size_t size,int flags,bus_space_handle_t * bshp)51 bus_space_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags,
52     bus_space_handle_t *bshp)
53 {
54           vaddr_t kva;
55           vsize_t offset;
56           int error;
57 
58           if (t->bustype == HP300_BUS_SPACE_INTIO) {
59                     /*
60                      * Intio space is direct-mapped in pmap_bootstrap(); just
61                      * do the translation.
62                      */
63                     *bshp = (bus_space_handle_t)IIOV(INTIOBASE + bpa);
64                     return 0;
65           }
66 
67           if (t->bustype != HP300_BUS_SPACE_DIO &&
68               t->bustype != HP300_BUS_SPACE_SGC)
69                     panic("%s: bad space tag", __func__);
70 
71           /*
72            * Allocate virtual address space from the extio extent map.
73            */
74           offset = m68k_page_offset(bpa);
75           size = m68k_round_page(offset + size);
76           error = extent_alloc(extio_ex, size, PAGE_SIZE, 0,
77               EX_FAST | EX_NOWAIT | (extio_ex_malloc_safe ? EX_MALLOCOK : 0),
78               &kva);
79           if (error)
80                     return error;
81 
82           /*
83            * Map the range.  The range is always cache-inhibited on the hp300.
84            */
85           physaccess((void *)kva, (void *)bpa, size, PG_RW|PG_CI);
86 
87           /*
88            * All done.
89            */
90           *bshp = (bus_space_handle_t)(kva + offset);
91           return 0;
92 }
93 
94 int
bus_space_alloc(bus_space_tag_t t,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int flags,bus_addr_t * bpap,bus_space_handle_t * bshp)95 bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart, bus_addr_t rend,
96     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
97     bus_addr_t *bpap, bus_space_handle_t *bshp)
98 {
99 
100           /*
101            * Not meaningful on any currently-supported hp300 bus.
102            */
103           return EINVAL;
104 }
105 
106 void
bus_space_free(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t size)107 bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
108 {
109 
110           /*
111            * Not meaningful on any currently-supported hp300 bus.
112            */
113           panic("%s: shouldn't be here", __func__);
114 }
115 
116 void
bus_space_unmap(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t size)117 bus_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
118 {
119           vaddr_t kva;
120           vsize_t offset;
121 
122           if (t->bustype == HP300_BUS_SPACE_INTIO) {
123                     /*
124                      * Intio space is direct-mapped in pmap_bootstrap(); nothing
125                      * to do
126                      */
127                     return;
128           }
129 
130           if (t->bustype != HP300_BUS_SPACE_DIO &&
131               t->bustype != HP300_BUS_SPACE_SGC)
132                     panic("%s: bad space tag", __func__);
133 
134           kva = m68k_trunc_page(bsh);
135           offset = m68k_page_offset(bsh);
136           size = m68k_round_page(offset + size);
137 
138 #ifdef DIAGNOSTIC
139           if (bsh < (vaddr_t)extiobase ||
140               bsh >= ((vaddr_t)extiobase + ptoa(EIOMAPSIZE)))
141                     panic("%s: bad bus space handle", __func__);
142 #endif
143 
144           /*
145            * Unmap the range.
146            */
147           physunaccess((void *)kva, size);
148 
149           /*
150            * Free it from the extio extent map.
151            */
152           if (extent_free(extio_ex, kva, size,
153               EX_NOWAIT | (extio_ex_malloc_safe ? EX_MALLOCOK : 0)))
154                     printf("%s: kva 0x%lx size 0x%lx: "
155                         "can't free region\n", __func__, (u_long)bsh, size);
156 }
157 
158 int
bus_space_subregion(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)159 bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
160     bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
161 {
162 
163           *nbshp = bsh + offset;
164           return 0;
165 }
166 
167 int
hp300_bus_space_probe(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset,int sz)168 hp300_bus_space_probe(bus_space_tag_t t, bus_space_handle_t bsh,
169     bus_size_t offset, int sz)
170 {
171           label_t faultbuf;
172           int i;
173 
174           nofault = (int *)&faultbuf;
175           if (setjmp((label_t *)nofault)) {
176                     nofault = NULL;
177                     return 0;
178           }
179 
180           switch (sz) {
181           case 1:
182                     i = bus_space_read_1(t, bsh, offset);
183                     break;
184 
185           case 2:
186                     i = bus_space_read_2(t, bsh, offset);
187                     break;
188 
189           case 4:
190                     i = bus_space_read_4(t, bsh, offset);
191                     break;
192 
193           default:
194                     panic("%s: unupported data size %d", __func__, sz);
195                     /* NOTREACHED */
196           }
197           __USE(i);
198           nofault = NULL;
199           return 1;
200 }
201