xref: /netbsd-src/sys/arch/sun3/sun3x/bus_subr.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: bus_subr.c,v 1.28 2005/12/11 12:19:27 christos 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.28 2005/12/11 12:19:27 christos 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(int bustype, int pa)
92 {
93 	vaddr_t pgva;
94 	int off, s;
95 
96 	if ((bustype < 0) || (bustype >= BUS__NTYPES))
97 		panic("bus_tmapin: bustype");
98 
99 	off = pa & PGOFSET;
100 	pa -= off;
101 
102 	pa &= bus_info[bustype].mask;
103 	pa |= bus_info[bustype].base;
104 	pa |= PMAP_NC;
105 
106 	s = splvm();
107 	if (tmp_vpages_inuse)
108 		panic("bus_tmapin: tmp_vpages_inuse");
109 	tmp_vpages_inuse++;
110 
111 	pgva = tmp_vpages[1];
112 	pmap_kenter_pa(pgva, pa, VM_PROT_READ | VM_PROT_WRITE);
113 	pmap_update(pmap_kernel());
114 	splx(s);
115 
116 	return ((void *)(pgva + off));
117 }
118 
119 void
120 bus_tmapout(void *vp)
121 {
122 	vaddr_t pgva;
123 	int s;
124 
125 	pgva = m68k_trunc_page(vp);
126 	if (pgva != tmp_vpages[1])
127 		return;
128 
129 	s = splvm();
130 	pmap_kremove(pgva, PAGE_SIZE);
131 	pmap_update(pmap_kernel());
132 	--tmp_vpages_inuse;
133 	splx(s);
134 }
135 
136 /*
137  * Make a permanent mapping for a device.
138  */
139 void *
140 bus_mapin(int bustype, int pa, int 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 |= PMAP_NC;	/* non-cached */
163 
164 	/* Get some kernel virtual address space. */
165 	va = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
166 	if (va == 0)
167 		panic("bus_mapin");
168 
169 	/* Map it to the specified bus. */
170 	pmap_map(va, pa, pa + sz, VM_PROT_ALL);
171 
172 done:
173 	return ((void*)(va + off));
174 }
175 
176 void
177 bus_mapout(void *ptr, int sz)
178 {
179 	vaddr_t va;
180 	int off;
181 
182 	va = (vaddr_t)ptr;
183 
184 	/* If it was a PROM mapping, do NOT free it! */
185 	if ((va >= SUN3X_MONSTART) && (va < SUN3X_MONEND))
186 		return;
187 
188 	off = va & PGOFSET;
189 	va -= off;
190 	sz += off;
191 	sz = m68k_round_page(sz);
192 
193 	uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
194 }
195