1 /* $NetBSD: bus_space.c,v 1.27 2026/11/16 19:19:14 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * Implementation of bus_space mapping for the hp300. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.27 2026/11/16 19:19:14 thorpej Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/vmem.h> 42 43 #include <machine/autoconf.h> 44 #include <machine/bus.h> 45 46 #include <uvm/uvm_extern.h> 47 48 int 49 bus_space_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags, 50 bus_space_handle_t *bshp) 51 { 52 vmem_addr_t kva; 53 vsize_t offset; 54 int error; 55 56 if (t->bustype == HP300_BUS_SPACE_INTIO) { 57 /* 58 * Intio space is direct-mapped in pmap_bootstrap(); just 59 * do the translation. 60 */ 61 *bshp = (bus_space_handle_t)IIOV(INTIOBASE + bpa); 62 return 0; 63 } 64 65 if (t->bustype != HP300_BUS_SPACE_DIO && 66 t->bustype != HP300_BUS_SPACE_SGC) 67 panic("%s: bad space tag", __func__); 68 69 /* 70 * Allocate virtual address space from the extio extent map. 71 */ 72 offset = m68k_page_offset(bpa); 73 size = m68k_round_page(offset + size); 74 error = vmem_alloc(extio_arena, size, VM_BESTFIT | VM_NOSLEEP, &kva); 75 if (error) { 76 return error; 77 } 78 79 vaddr_t va = kva; 80 vaddr_t eva = kva + size; 81 bpa = m68k_trunc_page(bpa); 82 while (va < eva) { 83 pmap_kenter_pa(va, bpa, VM_PROT_READ | VM_PROT_WRITE, 84 PMAP_NOCACHE); 85 va += PAGE_SIZE; 86 bpa += PAGE_SIZE; 87 } 88 pmap_update(pmap_kernel()); 89 90 /* 91 * All done. 92 */ 93 *bshp = (bus_space_handle_t)(kva + offset); 94 return 0; 95 } 96 97 int 98 bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart, bus_addr_t rend, 99 bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags, 100 bus_addr_t *bpap, bus_space_handle_t *bshp) 101 { 102 103 /* 104 * Not meaningful on any currently-supported hp300 bus. 105 */ 106 return EINVAL; 107 } 108 109 void 110 bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size) 111 { 112 113 /* 114 * Not meaningful on any currently-supported hp300 bus. 115 */ 116 panic("%s: shouldn't be here", __func__); 117 } 118 119 void 120 bus_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size) 121 { 122 vaddr_t kva; 123 vsize_t offset; 124 125 if (t->bustype == HP300_BUS_SPACE_INTIO) { 126 /* 127 * Intio space is direct-mapped in pmap_bootstrap(); nothing 128 * to do 129 */ 130 return; 131 } 132 133 if (t->bustype != HP300_BUS_SPACE_DIO && 134 t->bustype != HP300_BUS_SPACE_SGC) 135 panic("%s: bad space tag", __func__); 136 137 kva = m68k_trunc_page(bsh); 138 offset = m68k_page_offset(bsh); 139 size = m68k_round_page(offset + size); 140 141 KASSERT(bsh >= (vaddr_t)extiobase); 142 KASSERT(bsh < ((vaddr_t)extiobase + ptoa(EIOMAPSIZE))); 143 144 /* 145 * Unmap the range. 146 */ 147 pmap_kremove(kva, size); 148 pmap_update(pmap_kernel()); 149 150 /* 151 * Free it from the extio arena. 152 */ 153 vmem_free(extio_arena, kva, size); 154 } 155 156 int 157 bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh, 158 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp) 159 { 160 161 *nbshp = bsh + offset; 162 return 0; 163 } 164 165 int 166 hp300_bus_space_probe(bus_space_tag_t t, bus_space_handle_t bsh, 167 bus_size_t offset, int sz) 168 { 169 label_t faultbuf; 170 int i; 171 172 nofault = &faultbuf; 173 if (setjmp(nofault)) { 174 nofault = NULL; 175 return 0; 176 } 177 178 switch (sz) { 179 case 1: 180 i = bus_space_read_1(t, bsh, offset); 181 break; 182 183 case 2: 184 i = bus_space_read_2(t, bsh, offset); 185 break; 186 187 case 4: 188 i = bus_space_read_4(t, bsh, offset); 189 break; 190 191 default: 192 panic("%s: unupported data size %d", __func__, sz); 193 /* NOTREACHED */ 194 } 195 __USE(i); 196 nofault = NULL; 197 return 1; 198 } 199