1 1.52 jmcneill /* $NetBSD: isa_machdep.c,v 1.52 2022/04/15 17:53:44 jmcneill Exp $ */ 2 1.1 fvdl 3 1.1 fvdl /*- 4 1.1 fvdl * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 5 1.1 fvdl * All rights reserved. 6 1.1 fvdl * 7 1.1 fvdl * This code is derived from software contributed to The NetBSD Foundation 8 1.1 fvdl * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9 1.1 fvdl * Simulation Facility, NASA Ames Research Center. 10 1.1 fvdl * 11 1.1 fvdl * Redistribution and use in source and binary forms, with or without 12 1.1 fvdl * modification, are permitted provided that the following conditions 13 1.1 fvdl * are met: 14 1.1 fvdl * 1. Redistributions of source code must retain the above copyright 15 1.1 fvdl * notice, this list of conditions and the following disclaimer. 16 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 fvdl * notice, this list of conditions and the following disclaimer in the 18 1.1 fvdl * documentation and/or other materials provided with the distribution. 19 1.1 fvdl * 20 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 fvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 fvdl * POSSIBILITY OF SUCH DAMAGE. 31 1.1 fvdl */ 32 1.1 fvdl 33 1.1 fvdl /*- 34 1.1 fvdl * Copyright (c) 1991 The Regents of the University of California. 35 1.1 fvdl * All rights reserved. 36 1.1 fvdl * 37 1.1 fvdl * This code is derived from software contributed to Berkeley by 38 1.1 fvdl * William Jolitz. 39 1.1 fvdl * 40 1.1 fvdl * Redistribution and use in source and binary forms, with or without 41 1.1 fvdl * modification, are permitted provided that the following conditions 42 1.1 fvdl * are met: 43 1.1 fvdl * 1. Redistributions of source code must retain the above copyright 44 1.1 fvdl * notice, this list of conditions and the following disclaimer. 45 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright 46 1.1 fvdl * notice, this list of conditions and the following disclaimer in the 47 1.1 fvdl * documentation and/or other materials provided with the distribution. 48 1.8 agc * 3. Neither the name of the University nor the names of its contributors 49 1.1 fvdl * may be used to endorse or promote products derived from this software 50 1.1 fvdl * without specific prior written permission. 51 1.1 fvdl * 52 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 1.1 fvdl * SUCH DAMAGE. 63 1.1 fvdl * 64 1.1 fvdl * @(#)isa.c 7.2 (Berkeley) 5/13/91 65 1.1 fvdl */ 66 1.1 fvdl 67 1.1 fvdl #include <sys/cdefs.h> 68 1.52 jmcneill __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.52 2022/04/15 17:53:44 jmcneill Exp $"); 69 1.1 fvdl 70 1.1 fvdl #include <sys/param.h> 71 1.1 fvdl #include <sys/systm.h> 72 1.1 fvdl #include <sys/kernel.h> 73 1.1 fvdl #include <sys/syslog.h> 74 1.1 fvdl #include <sys/device.h> 75 1.1 fvdl #include <sys/proc.h> 76 1.26 ad #include <sys/bus.h> 77 1.26 ad #include <sys/cpu.h> 78 1.1 fvdl 79 1.12 yamt #include <machine/bus_private.h> 80 1.1 fvdl #include <machine/pio.h> 81 1.1 fvdl #include <machine/cpufunc.h> 82 1.31 dyoung #include <machine/autoconf.h> 83 1.31 dyoung #include <machine/bootinfo.h> 84 1.1 fvdl 85 1.1 fvdl #include <dev/isa/isareg.h> 86 1.1 fvdl #include <dev/isa/isavar.h> 87 1.1 fvdl 88 1.1 fvdl #include <uvm/uvm_extern.h> 89 1.1 fvdl 90 1.31 dyoung #include "acpica.h" 91 1.31 dyoung #include "opt_acpi.h" 92 1.1 fvdl #include "ioapic.h" 93 1.1 fvdl 94 1.1 fvdl #if NIOAPIC > 0 95 1.1 fvdl #include <machine/i82093var.h> 96 1.1 fvdl #include <machine/mpbiosvar.h> 97 1.2 fvdl #endif 98 1.1 fvdl 99 1.48 jmcneill #if NACPICA > 0 100 1.48 jmcneill #include <dev/acpi/acpivar.h> 101 1.48 jmcneill #endif 102 1.48 jmcneill 103 1.7 fvdl static int _isa_dma_may_bounce(bus_dma_tag_t, bus_dmamap_t, int, int *); 104 1.1 fvdl 105 1.1 fvdl struct x86_bus_dma_tag isa_bus_dma_tag = { 106 1.29 christos ._tag_needs_free = 0, 107 1.29 christos ._bounce_thresh = ISA_DMA_BOUNCE_THRESHOLD, 108 1.29 christos ._bounce_alloc_lo = 0, 109 1.29 christos ._bounce_alloc_hi = ISA_DMA_BOUNCE_THRESHOLD, 110 1.29 christos ._may_bounce = _isa_dma_may_bounce, 111 1.1 fvdl }; 112 1.1 fvdl 113 1.7 fvdl #define IDTVEC(name) __CONCAT(X,name) 114 1.25 dsl typedef void (vector)(void); 115 1.7 fvdl extern vector *IDTVEC(intr)[]; 116 1.7 fvdl 117 1.1 fvdl #define LEGAL_IRQ(x) ((x) >= 0 && (x) < NUM_LEGACY_IRQS && (x) != 2) 118 1.1 fvdl 119 1.1 fvdl int 120 1.15 christos isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq) 121 1.1 fvdl { 122 1.1 fvdl int i, tmp, bestirq, count; 123 1.1 fvdl struct intrhand **p, *q; 124 1.1 fvdl struct intrsource *isp; 125 1.1 fvdl struct cpu_info *ci; 126 1.1 fvdl 127 1.1 fvdl if (type == IST_NONE) 128 1.1 fvdl panic("intr_alloc: bogus type"); 129 1.1 fvdl 130 1.1 fvdl ci = &cpu_info_primary; 131 1.1 fvdl 132 1.1 fvdl bestirq = -1; 133 1.1 fvdl count = -1; 134 1.1 fvdl 135 1.1 fvdl /* some interrupts should never be dynamically allocated */ 136 1.1 fvdl mask &= 0xdef8; 137 1.1 fvdl 138 1.1 fvdl /* 139 1.1 fvdl * XXX some interrupts will be used later (6 for fdc, 12 for pms). 140 1.1 fvdl * the right answer is to do "breadth-first" searching of devices. 141 1.1 fvdl */ 142 1.1 fvdl mask &= 0xefbf; 143 1.1 fvdl 144 1.26 ad mutex_enter(&cpu_lock); 145 1.1 fvdl 146 1.1 fvdl for (i = 0; i < NUM_LEGACY_IRQS; i++) { 147 1.1 fvdl if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0) 148 1.1 fvdl continue; 149 1.1 fvdl isp = ci->ci_isources[i]; 150 1.1 fvdl if (isp == NULL) { 151 1.32 mbalmer /* if nothing's using the irq, just return it */ 152 1.1 fvdl *irq = i; 153 1.26 ad mutex_exit(&cpu_lock); 154 1.32 mbalmer return 0; 155 1.1 fvdl } 156 1.1 fvdl 157 1.1 fvdl switch(isp->is_type) { 158 1.1 fvdl case IST_EDGE: 159 1.1 fvdl case IST_LEVEL: 160 1.1 fvdl if (type != isp->is_type) 161 1.1 fvdl continue; 162 1.1 fvdl /* 163 1.1 fvdl * if the irq is shareable, count the number of other 164 1.1 fvdl * handlers, and if it's smaller than the last irq like 165 1.1 fvdl * this, remember it 166 1.1 fvdl * 167 1.1 fvdl * XXX We should probably also consider the 168 1.1 fvdl * interrupt level and stick IPL_TTY with other 169 1.1 fvdl * IPL_TTY, etc. 170 1.1 fvdl */ 171 1.1 fvdl for (p = &isp->is_handlers, tmp = 0; (q = *p) != NULL; 172 1.1 fvdl p = &q->ih_next, tmp++) 173 1.1 fvdl ; 174 1.1 fvdl if ((bestirq == -1) || (count > tmp)) { 175 1.1 fvdl bestirq = i; 176 1.1 fvdl count = tmp; 177 1.1 fvdl } 178 1.1 fvdl break; 179 1.1 fvdl case IST_PULSE: 180 1.1 fvdl /* this just isn't shareable */ 181 1.1 fvdl continue; 182 1.1 fvdl } 183 1.1 fvdl } 184 1.1 fvdl 185 1.26 ad mutex_exit(&cpu_lock); 186 1.1 fvdl 187 1.1 fvdl if (bestirq == -1) 188 1.32 mbalmer return 1; 189 1.1 fvdl 190 1.1 fvdl *irq = bestirq; 191 1.1 fvdl 192 1.32 mbalmer return 0; 193 1.1 fvdl } 194 1.1 fvdl 195 1.1 fvdl const struct evcnt * 196 1.15 christos isa_intr_evcnt(isa_chipset_tag_t ic, int irq) 197 1.1 fvdl { 198 1.1 fvdl /* XXX for now, no evcnt parent reported */ 199 1.1 fvdl return NULL; 200 1.1 fvdl } 201 1.1 fvdl 202 1.1 fvdl void * 203 1.32 mbalmer isa_intr_establish(isa_chipset_tag_t ic, int irq, int type, int level, 204 1.32 mbalmer int (*ih_fun)(void *), void *ih_arg) 205 1.1 fvdl { 206 1.34 jdolecek return isa_intr_establish_xname(ic, irq, type, level, 207 1.34 jdolecek ih_fun, ih_arg, "unknown"); 208 1.34 jdolecek } 209 1.34 jdolecek 210 1.34 jdolecek void * 211 1.34 jdolecek isa_intr_establish_xname(isa_chipset_tag_t ic, int irq, int type, int level, 212 1.34 jdolecek int (*ih_fun)(void *), void *ih_arg, const char *xname) 213 1.34 jdolecek { 214 1.1 fvdl struct pic *pic; 215 1.1 fvdl int pin; 216 1.41 cherry #if NIOAPIC > 0 217 1.38 bouyer intr_handle_t mpih = 0; 218 1.35 cherry struct ioapic_softc *ioapic = NULL; 219 1.1 fvdl #endif 220 1.1 fvdl 221 1.1 fvdl pin = irq; 222 1.1 fvdl pic = &i8259_pic; 223 1.1 fvdl 224 1.1 fvdl #if NIOAPIC > 0 225 1.1 fvdl if (mp_busses != NULL) { 226 1.10 fvdl if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 || 227 1.10 fvdl intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) { 228 1.1 fvdl if (!APIC_IRQ_ISLEGACY(mpih)) { 229 1.1 fvdl pin = APIC_IRQ_PIN(mpih); 230 1.23 drochner ioapic = ioapic_find(APIC_IRQ_APIC(mpih)); 231 1.23 drochner if (ioapic == NULL) { 232 1.1 fvdl printf("isa_intr_establish: " 233 1.1 fvdl "unknown apic %d\n", 234 1.1 fvdl APIC_IRQ_APIC(mpih)); 235 1.1 fvdl return NULL; 236 1.1 fvdl } 237 1.23 drochner pic = &ioapic->sc_pic; 238 1.1 fvdl } 239 1.1 fvdl } else 240 1.1 fvdl printf("isa_intr_establish: no MP mapping found\n"); 241 1.1 fvdl } 242 1.1 fvdl #endif 243 1.34 jdolecek return intr_establish_xname(irq, pic, pin, type, level, ih_fun, ih_arg, 244 1.34 jdolecek false, xname); 245 1.1 fvdl } 246 1.1 fvdl 247 1.32 mbalmer /* Deregister an interrupt handler. */ 248 1.1 fvdl void 249 1.15 christos isa_intr_disestablish(isa_chipset_tag_t ic, void *arg) 250 1.1 fvdl { 251 1.44 cherry #if !defined(XENPV) 252 1.1 fvdl struct intrhand *ih = arg; 253 1.1 fvdl 254 1.1 fvdl if (!LEGAL_IRQ(ih->ih_pin)) 255 1.1 fvdl panic("intr_disestablish: bogus irq"); 256 1.1 fvdl 257 1.1 fvdl intr_disestablish(ih); 258 1.50 skrll #endif 259 1.1 fvdl } 260 1.1 fvdl 261 1.1 fvdl void 262 1.32 mbalmer isa_attach_hook(device_t parent, device_t self, struct isabus_attach_args *iba) 263 1.1 fvdl { 264 1.4 fvdl extern struct x86_isa_chipset x86_isa_chipset; 265 1.1 fvdl extern int isa_has_been_seen; 266 1.1 fvdl 267 1.1 fvdl /* 268 1.1 fvdl * Notify others that might need to know that the ISA bus 269 1.1 fvdl * has now been attached. 270 1.1 fvdl */ 271 1.1 fvdl if (isa_has_been_seen) 272 1.1 fvdl panic("isaattach: ISA bus already seen!"); 273 1.1 fvdl isa_has_been_seen = 1; 274 1.1 fvdl 275 1.1 fvdl /* 276 1.1 fvdl * Since we can only have one ISA bus, we just use a single 277 1.1 fvdl * statically allocated ISA chipset structure. Pass it up 278 1.1 fvdl * now. 279 1.1 fvdl */ 280 1.4 fvdl iba->iba_ic = &x86_isa_chipset; 281 1.1 fvdl } 282 1.1 fvdl 283 1.27 dyoung void 284 1.28 dyoung isa_detach_hook(isa_chipset_tag_t ic, device_t self) 285 1.27 dyoung { 286 1.27 dyoung extern int isa_has_been_seen; 287 1.27 dyoung 288 1.27 dyoung isa_has_been_seen = 0; 289 1.27 dyoung } 290 1.27 dyoung 291 1.1 fvdl int 292 1.21 cegger isa_mem_alloc(bus_space_tag_t t, bus_size_t size, bus_size_t align, 293 1.32 mbalmer bus_addr_t boundary, int flags, bus_addr_t *addrp, bus_space_handle_t *bshp) 294 1.1 fvdl { 295 1.32 mbalmer /* Allocate physical address space in the ISA hole. */ 296 1.32 mbalmer return bus_space_alloc(t, IOM_BEGIN, IOM_END - 1, size, align, 297 1.32 mbalmer boundary, flags, addrp, bshp); 298 1.1 fvdl } 299 1.1 fvdl 300 1.1 fvdl void 301 1.21 cegger isa_mem_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size) 302 1.1 fvdl { 303 1.1 fvdl bus_space_free(t, bsh, size); 304 1.1 fvdl } 305 1.1 fvdl 306 1.1 fvdl /* 307 1.7 fvdl * ISA only has 24-bits of address space. This means 308 1.7 fvdl * we can't DMA to pages over 16M. In order to DMA to 309 1.7 fvdl * arbitrary buffers, we use "bounce buffers" - pages 310 1.7 fvdl * in memory below the 16M boundary. On DMA reads, 311 1.7 fvdl * DMA happens to the bounce buffers, and is copied into 312 1.7 fvdl * the caller's buffer. On writes, data is copied into 313 1.51 skrll * the bounce buffer, and the DMA happens from those 314 1.7 fvdl * pages. To software using the DMA mapping interface, 315 1.7 fvdl * this looks simply like a data cache. 316 1.7 fvdl * 317 1.7 fvdl * If we have more than 16M of RAM in the system, we may 318 1.7 fvdl * need bounce buffers. We check and remember that here. 319 1.7 fvdl * 320 1.7 fvdl * There are exceptions, however. VLB devices can do 321 1.7 fvdl * 32-bit DMA, and indicate that here. 322 1.7 fvdl * 323 1.7 fvdl * ...or, there is an opposite case. The most segments 324 1.7 fvdl * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If 325 1.7 fvdl * the caller can't handle that many segments (e.g. the 326 1.7 fvdl * ISA DMA controller), we may have to bounce it as well. 327 1.7 fvdl */ 328 1.7 fvdl static int 329 1.15 christos _isa_dma_may_bounce(bus_dma_tag_t t, bus_dmamap_t map, int flags, 330 1.14 christos int *cookieflagsp) 331 1.1 fvdl { 332 1.7 fvdl if ((flags & ISABUS_DMA_32BIT) != 0) 333 1.1 fvdl map->_dm_bounce_thresh = 0; 334 1.1 fvdl 335 1.7 fvdl if (((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) 336 1.11 thorpej *cookieflagsp |= X86_DMA_MIGHT_NEED_BOUNCE; 337 1.7 fvdl return 0; 338 1.1 fvdl } 339 1.31 dyoung 340 1.31 dyoung device_t 341 1.31 dyoung device_isa_register(device_t dev, void *aux) 342 1.31 dyoung { 343 1.31 dyoung /* 344 1.31 dyoung * Handle network interfaces here, the attachment information is 345 1.31 dyoung * not available driver-independently later. 346 1.31 dyoung * 347 1.31 dyoung * For disks, there is nothing useful available at attach time. 348 1.31 dyoung */ 349 1.31 dyoung if (device_class(dev) == DV_IFNET) { 350 1.31 dyoung struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF); 351 1.31 dyoung if (bin == NULL) 352 1.31 dyoung return NULL; 353 1.31 dyoung 354 1.31 dyoung /* 355 1.31 dyoung * We don't check the driver name against the device name 356 1.31 dyoung * passed by the boot ROM. The ROM should stay usable if 357 1.31 dyoung * the driver becomes obsolete. The physical attachment 358 1.31 dyoung * information (checked below) must be sufficient to 359 1.32 mbalmer * identify the device. 360 1.31 dyoung */ 361 1.31 dyoung if (bin->bus == BI_BUS_ISA && 362 1.31 dyoung device_is_a(device_parent(dev), "isa")) { 363 1.31 dyoung struct isa_attach_args *iaa = aux; 364 1.31 dyoung 365 1.31 dyoung /* Compare IO base address */ 366 1.31 dyoung /* XXXJRT What about multiple IO addrs? */ 367 1.31 dyoung if (iaa->ia_nio > 0 && 368 1.31 dyoung bin->addr.iobase == iaa->ia_io[0].ir_addr) 369 1.31 dyoung return dev; 370 1.31 dyoung } 371 1.31 dyoung } 372 1.46 bouyer if (vm_guest == VM_GUEST_XENPVH) 373 1.46 bouyer prop_dictionary_set_bool(device_properties(dev), 374 1.46 bouyer "no-legacy-devices", true); 375 1.31 dyoung #if NACPICA > 0 376 1.52 jmcneill #if notyet 377 1.52 jmcneill /* 378 1.52 jmcneill * The following code block is technically correct, but unfortunately 379 1.52 jmcneill * it breaks things like being able to use lm(4) on platforms that 380 1.52 jmcneill * have no other means of exposing temperature, fan, and voltage 381 1.52 jmcneill * sensors. 382 1.52 jmcneill */ 383 1.31 dyoung if (device_is_a(dev, "isa") && acpi_active) { 384 1.47 jmcneill /* 385 1.47 jmcneill * For FACP >= 2, the LEGACY_DEVICES flag indicates that 386 1.47 jmcneill * the motherboard supports user-visible devices on the LPC 387 1.47 jmcneill * or ISA bus. If clear, assume that no such devices are 388 1.47 jmcneill * present and we can enumerate everything we need using 389 1.47 jmcneill * ACPI tables. 390 1.47 jmcneill */ 391 1.47 jmcneill if (AcpiGbl_FADT.Header.Revision >= 2 && 392 1.47 jmcneill !(AcpiGbl_FADT.BootFlags & ACPI_FADT_LEGACY_DEVICES)) { 393 1.31 dyoung prop_dictionary_set_bool(device_properties(dev), 394 1.31 dyoung "no-legacy-devices", true); 395 1.47 jmcneill } 396 1.31 dyoung } 397 1.52 jmcneill #endif 398 1.52 jmcneill 399 1.49 jmcneill if (vm_guest == VM_GUEST_VMWARE && 400 1.49 jmcneill device_is_a(dev, "isa") && acpi_active) { 401 1.49 jmcneill prop_dictionary_set_bool(device_properties(dev), 402 1.49 jmcneill "no-legacy-devices", true); 403 1.49 jmcneill } 404 1.31 dyoung #endif /* NACPICA > 0 */ 405 1.31 dyoung return NULL; 406 1.31 dyoung } 407