xref: /netbsd-src/sys/arch/sun3/sun3x/bus_subr.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: bus_subr.c,v 1.16 1999/11/13 00:32:19 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass and Gordon W. Ross.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * bus_xxx support functions, Sun3X-specific part.
41  * The common stuff is in autoconf.c
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_map.h>
51 
52 #include <uvm/uvm_extern.h>
53 
54 #include <machine/autoconf.h>
55 #include <machine/cpu.h>
56 #include <machine/mon.h>
57 #include <machine/pmap.h>
58 #include <machine/pte.h>
59 
60 #include <sun3/sun3/machdep.h>
61 #include <sun3/sun3x/vme.h>
62 
63 label_t *nofault;
64 
65 /* These are defined in pmap.c */
66 extern vm_offset_t tmp_vpages[];
67 extern int tmp_vpages_inuse;
68 
69 static const struct {
70 	long base;
71 	long mask;
72 } bus_info[BUS__NTYPES] = {
73 	{ /* OBIO  */ 0, ~0 },
74 	{ /* OBMEM */ 0, ~0 },
75 	/* VME A16 */
76 	{ VME16D16_BASE, VME16_MASK },
77 	{ VME16D32_BASE, VME16_MASK },
78 	/* VME A24 */
79 	{ VME24D16_BASE, VME24_MASK },
80 	{ VME24D32_BASE, VME24_MASK },
81 	/* VME A32 */
82 	{ VME32D16_BASE, VME32_MASK },
83 	{ VME32D32_BASE, VME32_MASK },
84 };
85 
86 /*
87  * Create a temporary, one-page mapping for a device.
88  * This is used by some device probe routines that
89  * need to do peek/write/read tricks.
90  */
91 void *
92 bus_tmapin(bustype, pa)
93 	int bustype, pa;
94 {
95 	vm_offset_t pgva;
96 	int off, s;
97 
98 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
99 		panic("bus_tmapin: bustype");
100 
101 	off = pa & PGOFSET;
102 	pa -= off;
103 
104 	pa &= bus_info[bustype].mask;
105 	pa |= bus_info[bustype].base;
106 	pa |= PMAP_NC;
107 
108 	s = splimp();
109 	if (tmp_vpages_inuse)
110 		panic("bus_tmapin: tmp_vpages_inuse");
111 	tmp_vpages_inuse++;
112 
113 	pgva = tmp_vpages[1];
114 	pmap_enter(pmap_kernel(), pgva, pa,
115 	           (VM_PROT_READ|VM_PROT_WRITE), PMAP_WIRED);
116 	splx(s);
117 
118 	return ((void *)(pgva + off));
119 }
120 
121 void bus_tmapout(vp)
122 	void *vp;
123 {
124 	vm_offset_t pgva;
125 	int s;
126 
127 	pgva = m68k_trunc_page(vp);
128 	if (pgva != tmp_vpages[1])
129 		return;
130 
131 	s = splimp();
132 	pmap_remove(pmap_kernel(), pgva, pgva + NBPG);
133 	--tmp_vpages_inuse;
134 	splx(s);
135 }
136 
137 /*
138  * Make a permanent mapping for a device.
139  */
140 void *
141 bus_mapin(bustype, pa, sz)
142 	int bustype, pa, sz;
143 {
144 	vm_offset_t va;
145 	int off;
146 
147 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
148 		panic("bus_mapin: bustype");
149 
150 	off = pa & PGOFSET;
151 	pa -= off;
152 	sz += off;
153 	sz = m68k_round_page(sz);
154 
155 	/* Borrow PROM mappings if we can. */
156 	if (bustype == BUS_OBIO) {
157 		va = (vm_offset_t) obio_find_mapping(pa, sz);
158 		if (va != 0)
159 			goto done;
160 	}
161 
162 	pa &= bus_info[bustype].mask;
163 	pa |= bus_info[bustype].base;
164 	pa |= PMAP_NC;	/* non-cached */
165 
166 	/* Get some kernel virtual address space. */
167 	va = uvm_km_valloc_wait(kernel_map, sz);
168 	if (va == 0)
169 		panic("bus_mapin");
170 
171 	/* Map it to the specified bus. */
172 	pmap_map(va, pa, pa + sz, VM_PROT_ALL);
173 
174 done:
175 	return ((void*)(va + off));
176 }
177 
178 void
179 bus_mapout(ptr, sz)
180 	void *ptr;
181 	int sz;
182 {
183 	vm_offset_t va;
184 	int off;
185 
186 	va = (vm_offset_t)ptr;
187 
188 	/* If it was a PROM mapping, do NOT free it! */
189 	if ((va >= SUN3X_MONSTART) && (va < SUN3X_MONEND))
190 		return;
191 
192 	off = va & PGOFSET;
193 	va -= off;
194 	sz += off;
195 	sz = m68k_round_page(sz);
196 
197 	uvm_km_free_wakeup(kernel_map, va, sz);
198 }
199