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