xref: /netbsd/sys/arch/sun3/sun3/bus_subr.c (revision bf9ec67e)
1 /*	$NetBSD: bus_subr.c,v 1.10 2001/09/05 13:21:09 tsutsui 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, Sun3-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 <uvm/uvm_extern.h>
49 
50 #include <machine/autoconf.h>
51 #include <machine/cpu.h>
52 #include <machine/mon.h>
53 #include <machine/pmap.h>
54 #include <machine/pte.h>
55 
56 #include <sun3/sun3/control.h>
57 #include <sun3/sun3/machdep.h>
58 #include <sun3/sun3/vme.h>
59 
60 label_t *nofault;
61 
62 /* These are defined in pmap.c */
63 extern vaddr_t tmp_vpages[];
64 extern int tmp_vpages_inuse;
65 
66 #define OBIO_MASK 0xFFffff
67 #define PMAP_OBMEM 0
68 
69 static const struct {
70 	int  type;
71 	long base;
72 	long mask;
73 } bus_info[BUS__NTYPES] = {
74 	{ PMAP_OBIO,  0, OBIO_MASK },
75 	{ PMAP_OBMEM, 0, ~0 },
76 	/* VME A16 */
77 	{ PMAP_VME16, VME16_BASE, VME16_MASK },
78 	{ PMAP_VME32, VME16_BASE, VME16_MASK },
79 	/* VME A24 */
80 	{ PMAP_VME16, VME24_BASE, VME24_MASK },
81 	{ PMAP_VME32, VME24_BASE, VME24_MASK },
82 	/* VME A32 */
83 	{ PMAP_VME16, VME32_BASE, VME32_MASK },
84 	{ PMAP_VME32, VME32_BASE, VME32_MASK },
85 };
86 
87 /*
88  * Create a temporary, one-page mapping for a device.
89  * This is used by some device probe routines that
90  * need to do peek/write/read tricks.
91  */
92 void *
93 bus_tmapin(bustype, pa)
94 	int bustype, pa;
95 {
96 	vaddr_t pgva;
97 	int off, pte;
98 
99 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
100 		panic("bus_tmapin: bustype");
101 
102 	off = pa & PGOFSET;
103 	pa -= off;
104 
105 	pa &= bus_info[bustype].mask;
106 	pa |= bus_info[bustype].base;
107 
108 	pte = PA_PGNUM(pa);
109 	pte |= (bus_info[bustype].type << PG_MOD_SHIFT);
110 	pte |= (PG_VALID | PG_WRITE | PG_SYSTEM | PG_NC);
111 
112 	if (tmp_vpages_inuse)
113 		panic("bus_tmapin: tmp_vpages_inuse");
114 	tmp_vpages_inuse++;
115 
116 	pgva = tmp_vpages[1];
117 	set_pte(pgva, pte);
118 
119 	return ((void *)(pgva + off));
120 }
121 
122 void bus_tmapout(vp)
123 	void *vp;
124 {
125 	vaddr_t pgva;
126 
127 	pgva = m68k_trunc_page(vp);
128 	if (pgva != tmp_vpages[1])
129 		return;
130 
131 	set_pte(pgva, PG_INVAL);
132 	--tmp_vpages_inuse;
133 }
134 
135 /*
136  * Make a permanent mapping for a device.
137  */
138 void *
139 bus_mapin(bustype, pa, sz)
140 	int bustype, pa, sz;
141 {
142 	vaddr_t va;
143 	int off;
144 
145 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
146 		panic("bus_mapin: bustype");
147 
148 	off = pa & PGOFSET;
149 	pa -= off;
150 	sz += off;
151 	sz = m68k_round_page(sz);
152 
153 	/* Borrow PROM mappings if we can. */
154 	if (bustype == BUS_OBIO) {
155 		va = (vaddr_t) obio_find_mapping(pa, sz);
156 		if (va != 0)
157 			goto done;
158 	}
159 
160 	pa &= bus_info[bustype].mask;
161 	pa |= bus_info[bustype].base;
162 	pa |= bus_info[bustype].type;
163 	pa |= PMAP_NC;	/* non-cached */
164 
165 	/* Get some kernel virtual address space. */
166 	va = uvm_km_valloc_wait(kernel_map, sz);
167 	if (va == 0)
168 		panic("bus_mapin");
169 
170 	/* Map it to the specified bus. */
171 	pmap_map(va, pa, pa + sz, VM_PROT_ALL);
172 
173 done:
174 	return ((void*)(va + off));
175 }
176 
177 void
178 bus_mapout(ptr, sz)
179 	void *ptr;
180 	int sz;
181 {
182 	vaddr_t va;
183 	int off;
184 
185 	va = (vaddr_t)ptr;
186 
187 	/* If it was a PROM mapping, do NOT free it! */
188 	if ((va >= SUN3_MONSTART) && (va < SUN3_MONEND))
189 		return;
190 
191 	off = va & PGOFSET;
192 	va -= off;
193 	sz += off;
194 	sz = m68k_round_page(sz);
195 
196 	uvm_km_free_wakeup(kernel_map, va, sz);
197 }
198