xref: /freebsd/sys/isa/isa_common.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND MIT
3  *
4  * Copyright (c) 1999 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * Modifications for Intel architecture by Garrett A. Wollman.
30  * Copyright 1998 Massachusetts Institute of Technology
31  *
32  * Permission to use, copy, modify, and distribute this software and
33  * its documentation for any purpose and without fee is hereby
34  * granted, provided that both the above copyright notice and this
35  * permission notice appear in all copies, that both the above
36  * copyright notice and this permission notice appear in all
37  * supporting documentation, and that the name of M.I.T. not be used
38  * in advertising or publicity pertaining to distribution of the
39  * software without specific, written prior permission.  M.I.T. makes
40  * no representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied
42  * warranty.
43  *
44  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
45  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
48  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Parts of the ISA bus implementation common to all architectures.
60  */
61 
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64 
65 #include "opt_isa.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/bus.h>
71 #include <sys/malloc.h>
72 #include <sys/module.h>
73 #include <machine/bus.h>
74 #include <sys/rman.h>
75 
76 #include <machine/resource.h>
77 
78 #include <isa/isavar.h>
79 #include <isa/isa_common.h>
80 
81 static int	isa_print_child(device_t bus, device_t dev);
82 
83 static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
84 
85 static int isa_running;
86 
87 /*
88  * At 'probe' time, we add all the devices which we know about to the
89  * bus.  The generic attach routine will probe and attach them if they
90  * are alive.
91  */
92 static int
93 isa_probe(device_t dev)
94 {
95 	device_set_desc(dev, "ISA bus");
96 	isa_init(dev);		/* Allow machdep code to initialise */
97 	return (0);
98 }
99 
100 extern device_t isa_bus_device;
101 
102 static int
103 isa_attach(device_t dev)
104 {
105 	/*
106 	 * Arrange for isa_probe_children(dev) to be called later. XXX
107 	 */
108 	isa_bus_device = dev;
109 	return (0);
110 }
111 
112 /*
113  * Find a working set of memory regions for a child using the ranges
114  * in *config  and return the regions in *result. Returns non-zero if
115  * a set of ranges was found.
116  */
117 static int
118 isa_find_memory(device_t child, struct isa_config *config,
119     struct isa_config *result)
120 {
121 	int success, i;
122 	struct resource *res[ISA_NMEM];
123 
124 	/*
125 	 * First clear out any existing resource definitions.
126 	 */
127 	for (i = 0; i < ISA_NMEM; i++) {
128 		bus_delete_resource(child, SYS_RES_MEMORY, i);
129 		res[i] = NULL;
130 	}
131 
132 	success = 1;
133 	result->ic_nmem = config->ic_nmem;
134 	for (i = 0; i < config->ic_nmem; i++) {
135 		uint32_t start, end, size, align;
136 
137 		size = config->ic_mem[i].ir_size;
138 
139 		/* the PnP device may have a null resource as filler */
140 		if (size == 0) {
141 			result->ic_mem[i].ir_start = 0;
142 			result->ic_mem[i].ir_end = 0;
143 			result->ic_mem[i].ir_size = 0;
144 			result->ic_mem[i].ir_align = 0;
145 			continue;
146 		}
147 
148 		for (start = config->ic_mem[i].ir_start,
149 			     end = config->ic_mem[i].ir_end,
150 			     align = config->ic_mem[i].ir_align;
151 		     start + size - 1 <= end && start + size > start;
152 		     start += MAX(align, 1)) {
153 			bus_set_resource(child, SYS_RES_MEMORY, i,
154 					 start, size);
155 			res[i] = bus_alloc_resource_any(child,
156 			    SYS_RES_MEMORY, &i,
157 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
158 			if (res[i]) {
159 				result->ic_mem[i].ir_start = start;
160 				result->ic_mem[i].ir_end = start + size - 1;
161 				result->ic_mem[i].ir_size = size;
162 				result->ic_mem[i].ir_align = align;
163 				break;
164 			}
165 		}
166 
167 		/*
168 		 * If we didn't find a place for memory range i, then
169 		 * give up now.
170 		 */
171 		if (!res[i]) {
172 			success = 0;
173 			break;
174 		}
175 	}
176 
177 	for (i = 0; i < ISA_NMEM; i++) {
178 		if (res[i])
179 			bus_release_resource(child, SYS_RES_MEMORY,
180 					     i, res[i]);
181 	}
182 
183 	return (success);
184 }
185 
186 /*
187  * Find a working set of port regions for a child using the ranges
188  * in *config  and return the regions in *result. Returns non-zero if
189  * a set of ranges was found.
190  */
191 static int
192 isa_find_port(device_t child, struct isa_config *config,
193     struct isa_config *result)
194 {
195 	int success, i;
196 	struct resource *res[ISA_NPORT];
197 
198 	/*
199 	 * First clear out any existing resource definitions.
200 	 */
201 	for (i = 0; i < ISA_NPORT; i++) {
202 		bus_delete_resource(child, SYS_RES_IOPORT, i);
203 		res[i] = NULL;
204 	}
205 
206 	success = 1;
207 	result->ic_nport = config->ic_nport;
208 	for (i = 0; i < config->ic_nport; i++) {
209 		uint32_t start, end, size, align;
210 
211 		size = config->ic_port[i].ir_size;
212 
213 		/* the PnP device may have a null resource as filler */
214 		if (size == 0) {
215 			result->ic_port[i].ir_start = 0;
216 			result->ic_port[i].ir_end = 0;
217 			result->ic_port[i].ir_size = 0;
218 			result->ic_port[i].ir_align = 0;
219 			continue;
220 		}
221 
222 		for (start = config->ic_port[i].ir_start,
223 			     end = config->ic_port[i].ir_end,
224 			     align = config->ic_port[i].ir_align;
225 		     start + size - 1 <= end;
226 		     start += align) {
227 			bus_set_resource(child, SYS_RES_IOPORT, i,
228 					 start, size);
229 			res[i] = bus_alloc_resource_any(child,
230 			    SYS_RES_IOPORT, &i,
231 			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
232 			if (res[i]) {
233 				result->ic_port[i].ir_start = start;
234 				result->ic_port[i].ir_end = start + size - 1;
235 				result->ic_port[i].ir_size = size;
236 				result->ic_port[i].ir_align = align;
237 				break;
238 			}
239 		}
240 
241 		/*
242 		 * If we didn't find a place for port range i, then
243 		 * give up now.
244 		 */
245 		if (!res[i]) {
246 			success = 0;
247 			break;
248 		}
249 	}
250 
251 	for (i = 0; i < ISA_NPORT; i++) {
252 		if (res[i])
253 			bus_release_resource(child, SYS_RES_IOPORT,
254 					     i, res[i]);
255 	}
256 
257 	return success;
258 }
259 
260 /*
261  * Return the index of the first bit in the mask (or -1 if mask is empty.
262  */
263 static int
264 find_first_bit(uint32_t mask)
265 {
266 	return (ffs(mask) - 1);
267 }
268 
269 /*
270  * Return the index of the next bit in the mask, or -1 if there are no more.
271  */
272 static int
273 find_next_bit(uint32_t mask, int bit)
274 {
275 	bit++;
276 	while (bit < 32 && !(mask & (1 << bit)))
277 		bit++;
278 	if (bit != 32)
279 		return (bit);
280 	return (-1);
281 }
282 
283 /*
284  * Find a working set of irqs for a child using the masks in *config
285  * and return the regions in *result. Returns non-zero if a set of
286  * irqs was found.
287  */
288 static int
289 isa_find_irq(device_t child, struct isa_config *config,
290     struct isa_config *result)
291 {
292 	int success, i;
293 	struct resource *res[ISA_NIRQ];
294 
295 	/*
296 	 * First clear out any existing resource definitions.
297 	 */
298 	for (i = 0; i < ISA_NIRQ; i++) {
299 		bus_delete_resource(child, SYS_RES_IRQ, i);
300 		res[i] = NULL;
301 	}
302 
303 	success = 1;
304 	result->ic_nirq = config->ic_nirq;
305 	for (i = 0; i < config->ic_nirq; i++) {
306 		uint32_t mask = config->ic_irqmask[i];
307 		int irq;
308 
309 		/* the PnP device may have a null resource as filler */
310 		if (mask == 0) {
311 			result->ic_irqmask[i] = 0;
312 			continue;
313 		}
314 
315 		for (irq = find_first_bit(mask);
316 		     irq != -1;
317 		     irq = find_next_bit(mask, irq)) {
318 			bus_set_resource(child, SYS_RES_IRQ, i,
319 					 irq, 1);
320 			res[i] = bus_alloc_resource_any(child,
321 							SYS_RES_IRQ, &i,
322 							0 /* !RF_ACTIVE */ );
323 			if (res[i]) {
324 				result->ic_irqmask[i] = (1 << irq);
325 				break;
326 			}
327 		}
328 
329 		/*
330 		 * If we didn't find a place for irq range i, then
331 		 * give up now.
332 		 */
333 		if (!res[i]) {
334 			success = 0;
335 			break;
336 		}
337 	}
338 
339 	for (i = 0; i < ISA_NIRQ; i++) {
340 		if (res[i])
341 			bus_release_resource(child, SYS_RES_IRQ,
342 					     i, res[i]);
343 	}
344 
345 	return (success);
346 }
347 
348 /*
349  * Find a working set of drqs for a child using the masks in *config
350  * and return the regions in *result. Returns non-zero if a set of
351  * drqs was found.
352  */
353 static int
354 isa_find_drq(device_t child, struct isa_config *config,
355     struct isa_config *result)
356 {
357 	int success, i;
358 	struct resource *res[ISA_NDRQ];
359 
360 	/*
361 	 * First clear out any existing resource definitions.
362 	 */
363 	for (i = 0; i < ISA_NDRQ; i++) {
364 		bus_delete_resource(child, SYS_RES_DRQ, i);
365 		res[i] = NULL;
366 	}
367 
368 	success = 1;
369 	result->ic_ndrq = config->ic_ndrq;
370 	for (i = 0; i < config->ic_ndrq; i++) {
371 		uint32_t mask = config->ic_drqmask[i];
372 		int drq;
373 
374 		/* the PnP device may have a null resource as filler */
375 		if (mask == 0) {
376 			result->ic_drqmask[i] = 0;
377 			continue;
378 		}
379 
380 		for (drq = find_first_bit(mask);
381 		     drq != -1;
382 		     drq = find_next_bit(mask, drq)) {
383 			bus_set_resource(child, SYS_RES_DRQ, i,
384 					 drq, 1);
385 			res[i] = bus_alloc_resource_any(child,
386 							SYS_RES_DRQ, &i,
387 							0 /* !RF_ACTIVE */);
388 			if (res[i]) {
389 				result->ic_drqmask[i] = (1 << drq);
390 				break;
391 			}
392 		}
393 
394 		/*
395 		 * If we didn't find a place for drq range i, then
396 		 * give up now.
397 		 */
398 		if (!res[i]) {
399 			success = 0;
400 			break;
401 		}
402 	}
403 
404 	for (i = 0; i < ISA_NDRQ; i++) {
405 		if (res[i])
406 			bus_release_resource(child, SYS_RES_DRQ,
407 					     i, res[i]);
408 	}
409 
410 	return (success);
411 }
412 
413 /*
414  * Attempt to find a working set of resources for a device. Return
415  * non-zero if a working configuration is found.
416  */
417 static int
418 isa_assign_resources(device_t child)
419 {
420 	struct isa_device *idev = DEVTOISA(child);
421 	struct isa_config_entry *ice;
422 	struct isa_config *cfg;
423 	const char *reason;
424 
425 	reason = "Empty ISA id_configs";
426 	cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO);
427 	if (cfg == NULL)
428 		return(0);
429 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
430 		reason = "memory";
431 		if (!isa_find_memory(child, &ice->ice_config, cfg))
432 			continue;
433 		reason = "port";
434 		if (!isa_find_port(child, &ice->ice_config, cfg))
435 			continue;
436 		reason = "irq";
437 		if (!isa_find_irq(child, &ice->ice_config, cfg))
438 			continue;
439 		reason = "drq";
440 		if (!isa_find_drq(child, &ice->ice_config, cfg))
441 			continue;
442 
443 		/*
444 		 * A working configuration was found enable the device
445 		 * with this configuration.
446 		 */
447 		reason = "no callback";
448 		if (idev->id_config_cb) {
449 			idev->id_config_cb(idev->id_config_arg,
450 					   cfg, 1);
451 			free(cfg, M_TEMP);
452 			return (1);
453 		}
454 	}
455 
456 	/*
457 	 * Disable the device.
458 	 */
459 	bus_print_child_header(device_get_parent(child), child);
460 	printf(" can't assign resources (%s)\n", reason);
461 	if (bootverbose)
462 		isa_print_child(device_get_parent(child), child);
463 	bzero(cfg, sizeof (*cfg));
464 	if (idev->id_config_cb)
465 		idev->id_config_cb(idev->id_config_arg, cfg, 0);
466 	device_disable(child);
467 
468 	free(cfg, M_TEMP);
469 	return (0);
470 }
471 
472 /*
473  * Claim any unallocated resources to keep other devices from using
474  * them.
475  */
476 static void
477 isa_claim_resources(device_t dev, device_t child)
478 {
479 	struct isa_device *idev = DEVTOISA(child);
480 	struct resource_list *rl = &idev->id_resources;
481 	struct resource_list_entry *rle;
482 	int rid;
483 
484 	STAILQ_FOREACH(rle, rl, link) {
485 		if (!rle->res) {
486 			rid = rle->rid;
487 			resource_list_alloc(rl, dev, child, rle->type, &rid,
488 			    0, ~0, 1, 0);
489 		}
490 	}
491 }
492 
493 /*
494  * Called after other devices have initialised to probe for isa devices.
495  */
496 void
497 isa_probe_children(device_t dev)
498 {
499 	struct isa_device *idev;
500 	device_t *children, child;
501 	struct isa_config *cfg;
502 	int nchildren, i;
503 
504 	/*
505 	 * Create all the non-hinted children by calling drivers'
506 	 * identify methods.
507 	 */
508 	bus_generic_probe(dev);
509 
510 	if (device_get_children(dev, &children, &nchildren))
511 		return;
512 
513 	/*
514 	 * First disable all pnp devices so that they don't get
515 	 * matched by legacy probes.
516 	 */
517 	if (bootverbose)
518 		printf("isa_probe_children: disabling PnP devices\n");
519 
520 	cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO);
521 	if (cfg == NULL) {
522 		free(children, M_TEMP);
523 		return;
524 	}
525 
526 	for (i = 0; i < nchildren; i++) {
527 		idev = DEVTOISA(children[i]);
528 
529 		bzero(cfg, sizeof(*cfg));
530 		if (idev->id_config_cb)
531 			idev->id_config_cb(idev->id_config_arg, cfg, 0);
532 	}
533 
534 	free(cfg, M_TEMP);
535 
536 	/*
537 	 * Next, probe all the PnP BIOS devices so they can subsume any
538 	 * hints.
539 	 */
540 	for (i = 0; i < nchildren; i++) {
541 		child = children[i];
542 		idev = DEVTOISA(child);
543 
544 		if (idev->id_order > ISA_ORDER_PNPBIOS)
545 			continue;
546 		if (!TAILQ_EMPTY(&idev->id_configs) &&
547 		    !isa_assign_resources(child))
548 			continue;
549 
550 		if (device_probe_and_attach(child) == 0)
551 			isa_claim_resources(dev, child);
552 	}
553 	free(children, M_TEMP);
554 
555 	/*
556 	 * Next, enumerate hinted devices and probe all non-pnp devices so
557 	 * that they claim their resources first.
558 	 */
559 	bus_enumerate_hinted_children(dev);
560 	if (device_get_children(dev, &children, &nchildren))
561 		return;
562 	if (bootverbose)
563 		printf("isa_probe_children: probing non-PnP devices\n");
564 	for (i = 0; i < nchildren; i++) {
565 		child = children[i];
566 		idev = DEVTOISA(child);
567 
568 		if (device_is_attached(child) ||
569 		    !TAILQ_EMPTY(&idev->id_configs))
570 			continue;
571 
572 		device_probe_and_attach(child);
573 	}
574 
575 	/*
576 	 * Finally assign resource to pnp devices and probe them.
577 	 */
578 	if (bootverbose)
579 		printf("isa_probe_children: probing PnP devices\n");
580 	for (i = 0; i < nchildren; i++) {
581 		child = children[i];
582 		idev = DEVTOISA(child);
583 
584 		if (device_is_attached(child) || TAILQ_EMPTY(&idev->id_configs))
585 			continue;
586 
587 		if (isa_assign_resources(child)) {
588 			device_probe_and_attach(child);
589 			isa_claim_resources(dev, child);
590 		}
591 	}
592 
593 	free(children, M_TEMP);
594 
595 	isa_running = 1;
596 }
597 
598 /*
599  * Add a new child with default ivars.
600  */
601 static device_t
602 isa_add_child(device_t dev, u_int order, const char *name, int unit)
603 {
604 	device_t child;
605 	struct	isa_device *idev;
606 
607 	child = device_add_child_ordered(dev, order, name, unit);
608 	if (child == NULL)
609 		return (child);
610 
611 	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO);
612 	if (!idev)
613 		return (0);
614 
615 	resource_list_init(&idev->id_resources);
616 	TAILQ_INIT(&idev->id_configs);
617 	idev->id_order = order;
618 
619 	device_set_ivars(child, idev);
620 
621 	return (child);
622 }
623 
624 static int
625 isa_print_all_resources(device_t dev)
626 {
627 	struct	isa_device *idev = DEVTOISA(dev);
628 	struct resource_list *rl = &idev->id_resources;
629 	int retval = 0;
630 
631 	if (STAILQ_FIRST(rl) || device_get_flags(dev))
632 		retval += printf(" at");
633 
634 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
635 	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
636 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
637 	retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
638 	if (device_get_flags(dev))
639 		retval += printf(" flags %#x", device_get_flags(dev));
640 #ifdef ISAPNP
641 	if (idev->id_vendorid)
642 		retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));
643 #endif
644 
645 	return (retval);
646 }
647 
648 static int
649 isa_print_child(device_t bus, device_t dev)
650 {
651 	int retval = 0;
652 
653 	retval += bus_print_child_header(bus, dev);
654 	retval += isa_print_all_resources(dev);
655 	retval += bus_print_child_footer(bus, dev);
656 
657 	return (retval);
658 }
659 
660 static void
661 isa_probe_nomatch(device_t dev, device_t child)
662 {
663 	if (bootverbose) {
664 		bus_print_child_header(dev, child);
665 		printf(" failed to probe");
666 		isa_print_all_resources(child);
667 		bus_print_child_footer(dev, child);
668 	}
669 
670 	return;
671 }
672 
673 static int
674 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
675 {
676 	struct isa_device* idev = DEVTOISA(dev);
677 	struct resource_list *rl = &idev->id_resources;
678 	struct resource_list_entry *rle;
679 
680 	switch (index) {
681 	case ISA_IVAR_PORT_0:
682 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
683 		if (rle)
684 			*result = rle->start;
685 		else
686 			*result = -1;
687 		break;
688 
689 	case ISA_IVAR_PORT_1:
690 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
691 		if (rle)
692 			*result = rle->start;
693 		else
694 			*result = -1;
695 		break;
696 
697 	case ISA_IVAR_PORTSIZE_0:
698 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
699 		if (rle)
700 			*result = rle->count;
701 		else
702 			*result = 0;
703 		break;
704 
705 	case ISA_IVAR_PORTSIZE_1:
706 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
707 		if (rle)
708 			*result = rle->count;
709 		else
710 			*result = 0;
711 		break;
712 
713 	case ISA_IVAR_MADDR_0:
714 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
715 		if (rle)
716 			*result = rle->start;
717 		else
718 			*result = -1;
719 		break;
720 
721 	case ISA_IVAR_MADDR_1:
722 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
723 		if (rle)
724 			*result = rle->start;
725 		else
726 			*result = -1;
727 		break;
728 
729 	case ISA_IVAR_MEMSIZE_0:
730 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
731 		if (rle)
732 			*result = rle->count;
733 		else
734 			*result = 0;
735 		break;
736 
737 	case ISA_IVAR_MEMSIZE_1:
738 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
739 		if (rle)
740 			*result = rle->count;
741 		else
742 			*result = 0;
743 		break;
744 
745 	case ISA_IVAR_IRQ_0:
746 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
747 		if (rle)
748 			*result = rle->start;
749 		else
750 			*result = -1;
751 		break;
752 
753 	case ISA_IVAR_IRQ_1:
754 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
755 		if (rle)
756 			*result = rle->start;
757 		else
758 			*result = -1;
759 		break;
760 
761 	case ISA_IVAR_DRQ_0:
762 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
763 		if (rle)
764 			*result = rle->start;
765 		else
766 			*result = -1;
767 		break;
768 
769 	case ISA_IVAR_DRQ_1:
770 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
771 		if (rle)
772 			*result = rle->start;
773 		else
774 			*result = -1;
775 		break;
776 
777 	case ISA_IVAR_VENDORID:
778 		*result = idev->id_vendorid;
779 		break;
780 
781 	case ISA_IVAR_SERIAL:
782 		*result = idev->id_serial;
783 		break;
784 
785 	case ISA_IVAR_LOGICALID:
786 		*result = idev->id_logicalid;
787 		break;
788 
789 	case ISA_IVAR_COMPATID:
790 		*result = idev->id_compatid;
791 		break;
792 
793 	case ISA_IVAR_CONFIGATTR:
794 		*result = idev->id_config_attr;
795 		break;
796 
797 	case ISA_IVAR_PNP_CSN:
798 		*result = idev->id_pnp_csn;
799 		break;
800 
801 	case ISA_IVAR_PNP_LDN:
802 		*result = idev->id_pnp_ldn;
803 		break;
804 
805 	case ISA_IVAR_PNPBIOS_HANDLE:
806 		*result = idev->id_pnpbios_handle;
807 		break;
808 
809 	default:
810 		return (ENOENT);
811 	}
812 
813 	return (0);
814 }
815 
816 static int
817 isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
818 {
819 	struct isa_device* idev = DEVTOISA(dev);
820 
821 	switch (index) {
822 	case ISA_IVAR_PORT_0:
823 	case ISA_IVAR_PORT_1:
824 	case ISA_IVAR_PORTSIZE_0:
825 	case ISA_IVAR_PORTSIZE_1:
826 	case ISA_IVAR_MADDR_0:
827 	case ISA_IVAR_MADDR_1:
828 	case ISA_IVAR_MEMSIZE_0:
829 	case ISA_IVAR_MEMSIZE_1:
830 	case ISA_IVAR_IRQ_0:
831 	case ISA_IVAR_IRQ_1:
832 	case ISA_IVAR_DRQ_0:
833 	case ISA_IVAR_DRQ_1:
834 		return (EINVAL);
835 
836 	case ISA_IVAR_VENDORID:
837 		idev->id_vendorid = value;
838 		break;
839 
840 	case ISA_IVAR_SERIAL:
841 		idev->id_serial = value;
842 		break;
843 
844 	case ISA_IVAR_LOGICALID:
845 		idev->id_logicalid = value;
846 		break;
847 
848 	case ISA_IVAR_COMPATID:
849 		idev->id_compatid = value;
850 		break;
851 
852 	case ISA_IVAR_CONFIGATTR:
853 		idev->id_config_attr = value;
854 		break;
855 
856 	default:
857 		return (ENOENT);
858 	}
859 
860 	return (0);
861 }
862 
863 /*
864  * Free any resources which the driver missed or which we were holding for
865  * it (see isa_probe_children).
866  */
867 static void
868 isa_child_detached(device_t dev, device_t child)
869 {
870 	struct isa_device* idev = DEVTOISA(child);
871 
872 	if (TAILQ_FIRST(&idev->id_configs))
873 		isa_claim_resources(dev, child);
874 }
875 
876 static void
877 isa_driver_added(device_t dev, driver_t *driver)
878 {
879 	device_t *children;
880 	int nchildren, i;
881 
882 	/*
883 	 * Don't do anything if drivers are dynamically
884 	 * added during autoconfiguration (cf. ymf724).
885 	 * since that would end up calling identify
886 	 * twice.
887 	 */
888 	if (!isa_running)
889 		return;
890 
891 	DEVICE_IDENTIFY(driver, dev);
892 	if (device_get_children(dev, &children, &nchildren))
893 		return;
894 
895 	for (i = 0; i < nchildren; i++) {
896 		device_t child = children[i];
897 		struct isa_device *idev = DEVTOISA(child);
898 		struct resource_list *rl = &idev->id_resources;
899 		struct resource_list_entry *rle;
900 
901 		if (device_get_state(child) != DS_NOTPRESENT)
902 			continue;
903 		if (!device_is_enabled(child))
904 			continue;
905 
906 		/*
907 		 * Free resources which we were holding on behalf of
908 		 * the device.
909 		 */
910 		STAILQ_FOREACH(rle, &idev->id_resources, link) {
911 			if (rle->res)
912 				resource_list_release(rl, dev, child,
913 						      rle->type,
914 						      rle->rid,
915 						      rle->res);
916 		}
917 
918 		if (TAILQ_FIRST(&idev->id_configs))
919 			if (!isa_assign_resources(child))
920 				continue;
921 
922 		device_probe_and_attach(child);
923 
924 		if (TAILQ_FIRST(&idev->id_configs))
925 			isa_claim_resources(dev, child);
926 	}
927 
928 	free(children, M_TEMP);
929 }
930 
931 static int
932 isa_set_resource(device_t dev, device_t child, int type, int rid,
933     rman_res_t start, rman_res_t count)
934 {
935 	struct isa_device* idev = DEVTOISA(child);
936 	struct resource_list *rl = &idev->id_resources;
937 
938 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
939 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
940 		return (EINVAL);
941 	if (rid < 0)
942 		return (EINVAL);
943 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
944 		return (EINVAL);
945 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
946 		return (EINVAL);
947 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
948 		return (EINVAL);
949 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
950 		return (EINVAL);
951 
952 	resource_list_add(rl, type, rid, start, start + count - 1, count);
953 
954 	return (0);
955 }
956 
957 static struct resource_list *
958 isa_get_resource_list (device_t dev, device_t child)
959 {
960 	struct isa_device* idev = DEVTOISA(child);
961 	struct resource_list *rl = &idev->id_resources;
962 
963 	if (!rl)
964 		return (NULL);
965 
966 	return (rl);
967 }
968 
969 static int
970 isa_add_config(device_t dev, device_t child, int priority,
971     struct isa_config *config)
972 {
973 	struct isa_device* idev = DEVTOISA(child);
974 	struct isa_config_entry *newice, *ice;
975 
976 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
977 	if (!newice)
978 		return (ENOMEM);
979 
980 	newice->ice_priority = priority;
981 	newice->ice_config = *config;
982 
983 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
984 		if (ice->ice_priority > priority)
985 			break;
986 	}
987 	if (ice)
988 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
989 	else
990 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
991 
992 	return (0);
993 }
994 
995 static void
996 isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
997     void *arg)
998 {
999 	struct isa_device* idev = DEVTOISA(child);
1000 
1001 	idev->id_config_cb = fn;
1002 	idev->id_config_arg = arg;
1003 }
1004 
1005 static int
1006 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
1007 {
1008 	struct isa_device* idev = DEVTOISA(child);
1009 
1010 	if (!idev->id_vendorid)
1011 		return (ENOENT);
1012 
1013 	while (ids && ids->ip_id) {
1014 		/*
1015 		 * Really ought to support >1 compat id per device.
1016 		 */
1017 		if (idev->id_logicalid == ids->ip_id
1018 		    || idev->id_compatid == ids->ip_id) {
1019 			if (ids->ip_desc)
1020 				device_set_desc(child, ids->ip_desc);
1021 			return (0);
1022 		}
1023 		ids++;
1024 	}
1025 
1026 	return (ENXIO);
1027 }
1028 
1029 static int
1030 isa_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1031     size_t buflen)
1032 {
1033 #ifdef ISAPNP
1034 	struct isa_device *idev = DEVTOISA(child);
1035 
1036 	if (idev->id_vendorid)
1037 		snprintf(buf, buflen, "pnpid=%s",
1038 		    pnp_eisaformat(idev->id_vendorid));
1039 #endif
1040 	return (0);
1041 }
1042 
1043 static int
1044 isa_child_location_str(device_t bus, device_t child, char *buf,
1045     size_t buflen)
1046 {
1047 #if 0
1048 	/* id_pnphandle isn't there yet */
1049 	struct isa_device *idev = DEVTOISA(child);
1050 
1051 	if (idev->id_vendorid)
1052 		snprintf(buf, buflen, "pnphandle=%d", idev->id_pnphandle);
1053 #endif
1054 	/* Nothing here yet */
1055 	*buf = '\0';
1056 	return (0);
1057 }
1058 
1059 static device_method_t isa_methods[] = {
1060 	/* Device interface */
1061 	DEVMETHOD(device_probe,		isa_probe),
1062 	DEVMETHOD(device_attach,	isa_attach),
1063 	DEVMETHOD(device_detach,	bus_generic_detach),
1064 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1065 	DEVMETHOD(device_suspend,	bus_generic_suspend),
1066 	DEVMETHOD(device_resume,	bus_generic_resume),
1067 
1068 	/* Bus interface */
1069 	DEVMETHOD(bus_add_child,	isa_add_child),
1070 	DEVMETHOD(bus_print_child,	isa_print_child),
1071 	DEVMETHOD(bus_probe_nomatch,	isa_probe_nomatch),
1072 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
1073 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
1074 	DEVMETHOD(bus_child_detached,	isa_child_detached),
1075 	DEVMETHOD(bus_driver_added,	isa_driver_added),
1076 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1077 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1078 
1079 	DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
1080 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
1081 	DEVMETHOD(bus_release_resource,	isa_release_resource),
1082 	DEVMETHOD(bus_set_resource,	isa_set_resource),
1083 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
1084 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
1085 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1086 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1087 	DEVMETHOD(bus_child_pnpinfo_str, isa_child_pnpinfo_str),
1088 	DEVMETHOD(bus_child_location_str, isa_child_location_str),
1089 	DEVMETHOD(bus_hinted_child,	isa_hinted_child),
1090 	DEVMETHOD(bus_hint_device_unit,	isa_hint_device_unit),
1091 
1092 	/* ISA interface */
1093 	DEVMETHOD(isa_add_config,	isa_add_config),
1094 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
1095 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
1096 
1097 	{ 0, 0 }
1098 };
1099 
1100 DEFINE_CLASS_0(isa, isa_driver, isa_methods, 0);
1101 
1102 devclass_t isa_devclass;
1103 
1104 /*
1105  * ISA can be attached to a PCI-ISA bridge, or other locations on some
1106  * platforms.
1107  */
1108 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
1109 DRIVER_MODULE(isa, eisab, isa_driver, isa_devclass, 0, 0);
1110 MODULE_VERSION(isa, 1);
1111 
1112 /*
1113  * Code common to ISA bridges.
1114  */
1115 
1116 devclass_t isab_devclass;
1117 
1118 int
1119 isab_attach(device_t dev)
1120 {
1121 	device_t child;
1122 
1123 	child = device_add_child(dev, "isa", 0);
1124 	if (child != NULL)
1125 		return (bus_generic_attach(dev));
1126 	return (ENXIO);
1127 }
1128