xref: /netbsd-src/sys/arch/sun3/sun3x/bus_subr.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: bus_subr.c,v 1.25 2003/07/15 03:36:19 lukem 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/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: bus_subr.c,v 1.25 2003/07/15 03:36:19 lukem Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #include <machine/autoconf.h>
54 #include <machine/cpu.h>
55 #include <machine/mon.h>
56 #include <machine/pmap.h>
57 #include <machine/pte.h>
58 
59 #include <sun3/sun3/machdep.h>
60 #include <sun3/sun3x/vme.h>
61 
62 label_t *nofault;
63 
64 /* These are defined in pmap.c */
65 extern vaddr_t tmp_vpages[];
66 extern int tmp_vpages_inuse;
67 
68 static const struct {
69 	long base;
70 	long mask;
71 } bus_info[BUS__NTYPES] = {
72 	{ /* OBIO  */ 0, ~0 },
73 	{ /* OBMEM */ 0, ~0 },
74 	/* VME A16 */
75 	{ VME16D16_BASE, VME16_MASK },
76 	{ VME16D32_BASE, VME16_MASK },
77 	/* VME A24 */
78 	{ VME24D16_BASE, VME24_MASK },
79 	{ VME24D32_BASE, VME24_MASK },
80 	/* VME A32 */
81 	{ VME32D16_BASE, VME32_MASK },
82 	{ VME32D32_BASE, VME32_MASK },
83 };
84 
85 /*
86  * Create a temporary, one-page mapping for a device.
87  * This is used by some device probe routines that
88  * need to do peek/write/read tricks.
89  */
90 void *
91 bus_tmapin(bustype, pa)
92 	int bustype, pa;
93 {
94 	vaddr_t pgva;
95 	int off, s;
96 
97 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
98 		panic("bus_tmapin: bustype");
99 
100 	off = pa & PGOFSET;
101 	pa -= off;
102 
103 	pa &= bus_info[bustype].mask;
104 	pa |= bus_info[bustype].base;
105 	pa |= PMAP_NC;
106 
107 	s = splvm();
108 	if (tmp_vpages_inuse)
109 		panic("bus_tmapin: tmp_vpages_inuse");
110 	tmp_vpages_inuse++;
111 
112 	pgva = tmp_vpages[1];
113 	pmap_kenter_pa(pgva, pa, VM_PROT_READ | VM_PROT_WRITE);
114 	pmap_update(pmap_kernel());
115 	splx(s);
116 
117 	return ((void *)(pgva + off));
118 }
119 
120 void bus_tmapout(vp)
121 	void *vp;
122 {
123 	vaddr_t pgva;
124 	int s;
125 
126 	pgva = m68k_trunc_page(vp);
127 	if (pgva != tmp_vpages[1])
128 		return;
129 
130 	s = splvm();
131 	pmap_kremove(pgva, PAGE_SIZE);
132 	pmap_update(pmap_kernel());
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 	vaddr_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 = (vaddr_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 	vaddr_t va;
184 	int off;
185 
186 	va = (vaddr_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