1 /* $NetBSD: bus_subr.c,v 1.20 2026/11/15 15:16:25 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 * 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 * bus_xxx support functions, Sun3-specific part. 34 * The common stuff is in autoconf.c 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: bus_subr.c,v 1.20 2026/11/15 15:16:25 thorpej Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/device.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <machine/autoconf.h> 47 #include <machine/cpu.h> 48 #include <machine/mon.h> 49 #include <machine/pmap.h> 50 #include <machine/pte.h> 51 52 #include <sun3/sun3/control.h> 53 #include <sun3/sun3/machdep.h> 54 #include <sun3/sun3/vme.h> 55 56 #define OBIO_MASK 0xFFffff 57 58 static const struct { 59 int type; 60 long base; 61 long mask; 62 } bus_info[BUS__NTYPES] = { 63 { PMAP_OBIO, 0, OBIO_MASK }, 64 { PMAP_OBMEM, 0, ~0 }, 65 /* VME A16 */ 66 { PMAP_VME16, VME16_BASE, VME16_MASK }, 67 { PMAP_VME32, VME16_BASE, VME16_MASK }, 68 /* VME A24 */ 69 { PMAP_VME16, VME24_BASE, VME24_MASK }, 70 { PMAP_VME32, VME24_BASE, VME24_MASK }, 71 /* VME A32 */ 72 { PMAP_VME16, VME32_BASE, VME32_MASK }, 73 { PMAP_VME32, VME32_BASE, VME32_MASK }, 74 }; 75 76 /* 77 * Create a temporary, one-page mapping for a device. 78 * This is used by some device probe routines that 79 * need to do peek/write/read tricks. 80 */ 81 void * 82 bus_tmapin(int bustype, int pa) 83 { 84 vaddr_t pgva; 85 int off, pte; 86 87 if ((bustype < 0) || (bustype >= BUS__NTYPES)) 88 panic("bus_tmapin: bustype"); 89 90 off = pa & PGOFSET; 91 pa -= off; 92 93 pa &= bus_info[bustype].mask; 94 pa |= bus_info[bustype].base; 95 96 pte = PA_PGNUM(pa); 97 pte |= (bus_info[bustype].type << PG_MOD_SHIFT); 98 pte |= (PG_VALID | PG_WRITE | PG_SYSTEM | PG_NC); 99 100 pgva = curcpu()->ci_tmpva_vmmap; 101 KASSERT(pgva != 0); 102 set_pte(pgva, pte); 103 104 return ((void *)(pgva + off)); 105 } 106 107 void 108 bus_tmapout(void *vp) 109 { 110 vaddr_t pgva; 111 112 pgva = m68k_trunc_page(vp); 113 if (pgva != curcpu()->ci_tmpva_dst) 114 return; 115 116 set_pte(pgva, PG_INVAL); 117 } 118 119 /* 120 * Make a permanent mapping for a device. 121 */ 122 void * 123 bus_mapin(int bustype, int pa, int sz) 124 { 125 vaddr_t va; 126 int off; 127 128 if ((bustype < 0) || (bustype >= BUS__NTYPES)) 129 panic("bus_mapin: bustype"); 130 131 off = pa & PGOFSET; 132 pa -= off; 133 sz += off; 134 sz = m68k_round_page(sz); 135 136 /* Borrow PROM mappings if we can. */ 137 if (bustype == BUS_OBIO) { 138 if (find_prom_map(pa, PMAP_OBIO, sz, &va) == 0) 139 goto done; 140 } 141 142 pa &= bus_info[bustype].mask; 143 pa |= bus_info[bustype].base; 144 pa |= bus_info[bustype].type; 145 pa |= PMAP_NC; /* non-cached */ 146 147 /* Get some kernel virtual address space. */ 148 va = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); 149 if (va == 0) 150 panic("bus_mapin"); 151 152 /* Map it to the specified bus. */ 153 pmap_map(va, pa, pa + sz, VM_PROT_ALL); 154 155 done: 156 return ((void*)(va + off)); 157 } 158 159 void 160 bus_mapout(void *ptr, int sz) 161 { 162 vaddr_t va; 163 int off; 164 165 va = (vaddr_t)ptr; 166 167 /* If it was a PROM mapping, do NOT free it! */ 168 if ((va >= SUN3_MONSTART) && (va < SUN3_MONEND)) 169 return; 170 171 off = va & PGOFSET; 172 va -= off; 173 sz += off; 174 sz = m68k_round_page(sz); 175 176 pmap_remove(pmap_kernel(), va, va + sz); 177 pmap_update(pmap_kernel()); 178 uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY); 179 } 180