xref: /freebsd/sys/isa/isa_common.c (revision c8fa54882226d89e987fb26b94979aed85a3bd9a)
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 void
isa_child_deleted(device_t dev,device_t child)626 isa_child_deleted(device_t dev, device_t child)
627 {
628 	free(device_get_ivars(child), M_ISADEV);
629 }
630 
631 static int
isa_print_all_resources(device_t dev)632 isa_print_all_resources(device_t dev)
633 {
634 	struct	isa_device *idev = DEVTOISA(dev);
635 	struct resource_list *rl = &idev->id_resources;
636 	int retval = 0;
637 
638 	if (STAILQ_FIRST(rl) || device_get_flags(dev))
639 		retval += printf(" at");
640 
641 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
642 	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
643 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
644 	retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
645 	if (device_get_flags(dev))
646 		retval += printf(" flags %#x", device_get_flags(dev));
647 	if (idev->id_vendorid)
648 		retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));
649 
650 	return (retval);
651 }
652 
653 static int
isa_print_child(device_t bus,device_t dev)654 isa_print_child(device_t bus, device_t dev)
655 {
656 	int retval = 0;
657 
658 	retval += bus_print_child_header(bus, dev);
659 	retval += isa_print_all_resources(dev);
660 	retval += bus_print_child_footer(bus, dev);
661 
662 	return (retval);
663 }
664 
665 static void
isa_probe_nomatch(device_t dev,device_t child)666 isa_probe_nomatch(device_t dev, device_t child)
667 {
668 	if (bootverbose) {
669 		bus_print_child_header(dev, child);
670 		printf(" failed to probe");
671 		isa_print_all_resources(child);
672 		bus_print_child_footer(dev, child);
673 	}
674 
675 	return;
676 }
677 
678 static int
isa_read_ivar(device_t bus,device_t dev,int index,uintptr_t * result)679 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
680 {
681 	struct isa_device* idev = DEVTOISA(dev);
682 	struct resource_list *rl = &idev->id_resources;
683 	struct resource_list_entry *rle;
684 
685 	switch (index) {
686 	case ISA_IVAR_PORT_0:
687 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
688 		if (rle)
689 			*result = rle->start;
690 		else
691 			*result = -1;
692 		break;
693 
694 	case ISA_IVAR_PORT_1:
695 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
696 		if (rle)
697 			*result = rle->start;
698 		else
699 			*result = -1;
700 		break;
701 
702 	case ISA_IVAR_PORTSIZE_0:
703 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
704 		if (rle)
705 			*result = rle->count;
706 		else
707 			*result = 0;
708 		break;
709 
710 	case ISA_IVAR_PORTSIZE_1:
711 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
712 		if (rle)
713 			*result = rle->count;
714 		else
715 			*result = 0;
716 		break;
717 
718 	case ISA_IVAR_MADDR_0:
719 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
720 		if (rle)
721 			*result = rle->start;
722 		else
723 			*result = -1;
724 		break;
725 
726 	case ISA_IVAR_MADDR_1:
727 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
728 		if (rle)
729 			*result = rle->start;
730 		else
731 			*result = -1;
732 		break;
733 
734 	case ISA_IVAR_MEMSIZE_0:
735 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
736 		if (rle)
737 			*result = rle->count;
738 		else
739 			*result = 0;
740 		break;
741 
742 	case ISA_IVAR_MEMSIZE_1:
743 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
744 		if (rle)
745 			*result = rle->count;
746 		else
747 			*result = 0;
748 		break;
749 
750 	case ISA_IVAR_IRQ_0:
751 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
752 		if (rle)
753 			*result = rle->start;
754 		else
755 			*result = -1;
756 		break;
757 
758 	case ISA_IVAR_IRQ_1:
759 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
760 		if (rle)
761 			*result = rle->start;
762 		else
763 			*result = -1;
764 		break;
765 
766 	case ISA_IVAR_DRQ_0:
767 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
768 		if (rle)
769 			*result = rle->start;
770 		else
771 			*result = -1;
772 		break;
773 
774 	case ISA_IVAR_DRQ_1:
775 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
776 		if (rle)
777 			*result = rle->start;
778 		else
779 			*result = -1;
780 		break;
781 
782 	case ISA_IVAR_VENDORID:
783 		*result = idev->id_vendorid;
784 		break;
785 
786 	case ISA_IVAR_SERIAL:
787 		*result = idev->id_serial;
788 		break;
789 
790 	case ISA_IVAR_LOGICALID:
791 		*result = idev->id_logicalid;
792 		break;
793 
794 	case ISA_IVAR_COMPATID:
795 		*result = idev->id_compatid;
796 		break;
797 
798 	case ISA_IVAR_CONFIGATTR:
799 		*result = idev->id_config_attr;
800 		break;
801 
802 	case ISA_IVAR_PNP_CSN:
803 		*result = idev->id_pnp_csn;
804 		break;
805 
806 	case ISA_IVAR_PNP_LDN:
807 		*result = idev->id_pnp_ldn;
808 		break;
809 
810 	case ISA_IVAR_PNPBIOS_HANDLE:
811 		*result = idev->id_pnpbios_handle;
812 		break;
813 
814 	default:
815 		return (ENOENT);
816 	}
817 
818 	return (0);
819 }
820 
821 static int
isa_write_ivar(device_t bus,device_t dev,int index,uintptr_t value)822 isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
823 {
824 	struct isa_device* idev = DEVTOISA(dev);
825 
826 	switch (index) {
827 	case ISA_IVAR_PORT_0:
828 	case ISA_IVAR_PORT_1:
829 	case ISA_IVAR_PORTSIZE_0:
830 	case ISA_IVAR_PORTSIZE_1:
831 	case ISA_IVAR_MADDR_0:
832 	case ISA_IVAR_MADDR_1:
833 	case ISA_IVAR_MEMSIZE_0:
834 	case ISA_IVAR_MEMSIZE_1:
835 	case ISA_IVAR_IRQ_0:
836 	case ISA_IVAR_IRQ_1:
837 	case ISA_IVAR_DRQ_0:
838 	case ISA_IVAR_DRQ_1:
839 		return (EINVAL);
840 
841 	case ISA_IVAR_VENDORID:
842 		idev->id_vendorid = value;
843 		break;
844 
845 	case ISA_IVAR_SERIAL:
846 		idev->id_serial = value;
847 		break;
848 
849 	case ISA_IVAR_LOGICALID:
850 		idev->id_logicalid = value;
851 		break;
852 
853 	case ISA_IVAR_COMPATID:
854 		idev->id_compatid = value;
855 		break;
856 
857 	case ISA_IVAR_CONFIGATTR:
858 		idev->id_config_attr = value;
859 		break;
860 
861 	default:
862 		return (ENOENT);
863 	}
864 
865 	return (0);
866 }
867 
868 /*
869  * Free any resources which the driver missed or which we were holding for
870  * it (see isa_probe_children).
871  */
872 static void
isa_child_detached(device_t dev,device_t child)873 isa_child_detached(device_t dev, device_t child)
874 {
875 	struct isa_device* idev = DEVTOISA(child);
876 
877 	if (TAILQ_FIRST(&idev->id_configs))
878 		isa_claim_resources(dev, child);
879 }
880 
881 static void
isa_driver_added(device_t dev,driver_t * driver)882 isa_driver_added(device_t dev, driver_t *driver)
883 {
884 	device_t *children;
885 	int nchildren, i;
886 
887 	/*
888 	 * Don't do anything if drivers are dynamically
889 	 * added during autoconfiguration (cf. ymf724).
890 	 * since that would end up calling identify
891 	 * twice.
892 	 */
893 	if (!isa_running)
894 		return;
895 
896 	DEVICE_IDENTIFY(driver, dev);
897 	if (device_get_children(dev, &children, &nchildren))
898 		return;
899 
900 	for (i = 0; i < nchildren; i++) {
901 		device_t child = children[i];
902 		struct isa_device *idev = DEVTOISA(child);
903 		struct resource_list *rl = &idev->id_resources;
904 		struct resource_list_entry *rle;
905 
906 		if (device_get_state(child) != DS_NOTPRESENT)
907 			continue;
908 		if (!device_is_enabled(child))
909 			continue;
910 
911 		/*
912 		 * Free resources which we were holding on behalf of
913 		 * the device.
914 		 */
915 		STAILQ_FOREACH(rle, &idev->id_resources, link) {
916 			if (rle->res)
917 				resource_list_release(rl, dev, child,
918 						      rle->res);
919 		}
920 
921 		if (TAILQ_FIRST(&idev->id_configs))
922 			if (!isa_assign_resources(child))
923 				continue;
924 
925 		device_probe_and_attach(child);
926 
927 		if (TAILQ_FIRST(&idev->id_configs))
928 			isa_claim_resources(dev, child);
929 	}
930 
931 	free(children, M_TEMP);
932 }
933 
934 static int
isa_set_resource(device_t dev,device_t child,int type,int rid,rman_res_t start,rman_res_t count)935 isa_set_resource(device_t dev, device_t child, int type, int rid,
936     rman_res_t start, rman_res_t count)
937 {
938 	struct isa_device* idev = DEVTOISA(child);
939 	struct resource_list *rl = &idev->id_resources;
940 
941 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
942 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
943 		return (EINVAL);
944 	if (rid < 0)
945 		return (EINVAL);
946 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
947 		return (EINVAL);
948 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
949 		return (EINVAL);
950 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
951 		return (EINVAL);
952 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
953 		return (EINVAL);
954 
955 	resource_list_add(rl, type, rid, start, start + count - 1, count);
956 
957 	return (0);
958 }
959 
960 static struct resource_list *
isa_get_resource_list(device_t dev,device_t child)961 isa_get_resource_list (device_t dev, device_t child)
962 {
963 	struct isa_device* idev = DEVTOISA(child);
964 	struct resource_list *rl = &idev->id_resources;
965 
966 	if (!rl)
967 		return (NULL);
968 
969 	return (rl);
970 }
971 
972 static int
isa_add_config(device_t dev,device_t child,int priority,struct isa_config * config)973 isa_add_config(device_t dev, device_t child, int priority,
974     struct isa_config *config)
975 {
976 	struct isa_device* idev = DEVTOISA(child);
977 	struct isa_config_entry *newice, *ice;
978 
979 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
980 	if (!newice)
981 		return (ENOMEM);
982 
983 	newice->ice_priority = priority;
984 	newice->ice_config = *config;
985 
986 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
987 		if (ice->ice_priority > priority)
988 			break;
989 	}
990 	if (ice)
991 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
992 	else
993 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
994 
995 	return (0);
996 }
997 
998 static void
isa_set_config_callback(device_t dev,device_t child,isa_config_cb * fn,void * arg)999 isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
1000     void *arg)
1001 {
1002 	struct isa_device* idev = DEVTOISA(child);
1003 
1004 	idev->id_config_cb = fn;
1005 	idev->id_config_arg = arg;
1006 }
1007 
1008 static int
isa_pnp_probe(device_t dev,device_t child,struct isa_pnp_id * ids)1009 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
1010 {
1011 	struct isa_device* idev = DEVTOISA(child);
1012 
1013 	if (!idev->id_vendorid)
1014 		return (ENOENT);
1015 
1016 	while (ids && ids->ip_id) {
1017 		/*
1018 		 * Really ought to support >1 compat id per device.
1019 		 */
1020 		if (idev->id_logicalid == ids->ip_id
1021 		    || idev->id_compatid == ids->ip_id) {
1022 			if (ids->ip_desc)
1023 				device_set_desc(child, ids->ip_desc);
1024 			return (0);
1025 		}
1026 		ids++;
1027 	}
1028 
1029 	return (ENXIO);
1030 }
1031 
1032 static int
isa_child_pnpinfo(device_t bus,device_t child,struct sbuf * sb)1033 isa_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb)
1034 {
1035 	struct isa_device *idev = DEVTOISA(child);
1036 
1037 	if (idev->id_vendorid)
1038 		sbuf_printf(sb, "pnpid=%s",
1039 		    pnp_eisaformat(idev->id_vendorid));
1040 	return (0);
1041 }
1042 
1043 static int
isa_child_location(device_t bus,device_t child,struct sbuf * sb)1044 isa_child_location(device_t bus, device_t child, struct sbuf *sb)
1045 {
1046 #if 0
1047 	/* id_pnphandle isn't there yet */
1048 	struct isa_device *idev = DEVTOISA(child);
1049 
1050 	if (idev->id_vendorid)
1051 		sbuf_printf(sbuf, "pnphandle=%d", idev->id_pnphandle);
1052 #endif
1053 	return (0);
1054 }
1055 
1056 static device_method_t isa_methods[] = {
1057 	/* Device interface */
1058 	DEVMETHOD(device_probe,		isa_probe),
1059 	DEVMETHOD(device_attach,	isa_attach),
1060 	DEVMETHOD(device_detach,	bus_generic_detach),
1061 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1062 	DEVMETHOD(device_suspend,	bus_generic_suspend),
1063 	DEVMETHOD(device_resume,	bus_generic_resume),
1064 
1065 	/* Bus interface */
1066 	DEVMETHOD(bus_add_child,	isa_add_child),
1067 	DEVMETHOD(bus_child_deleted,	isa_child_deleted),
1068 	DEVMETHOD(bus_print_child,	isa_print_child),
1069 	DEVMETHOD(bus_probe_nomatch,	isa_probe_nomatch),
1070 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
1071 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
1072 	DEVMETHOD(bus_child_detached,	isa_child_detached),
1073 	DEVMETHOD(bus_driver_added,	isa_driver_added),
1074 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1075 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1076 
1077 	DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
1078 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
1079 	DEVMETHOD(bus_release_resource,	isa_release_resource),
1080 	DEVMETHOD(bus_set_resource,	isa_set_resource),
1081 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
1082 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
1083 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1084 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1085 	DEVMETHOD(bus_child_pnpinfo,	isa_child_pnpinfo),
1086 	DEVMETHOD(bus_child_location,	isa_child_location),
1087 	DEVMETHOD(bus_hinted_child,	isa_hinted_child),
1088 	DEVMETHOD(bus_hint_device_unit,	isa_hint_device_unit),
1089 
1090 	/* ISA interface */
1091 	DEVMETHOD(isa_add_config,	isa_add_config),
1092 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
1093 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
1094 
1095 	{ 0, 0 }
1096 };
1097 
1098 DEFINE_CLASS_0(isa, isa_driver, isa_methods, 0);
1099 
1100 /*
1101  * ISA can be attached to a PCI-ISA bridge, or other locations on some
1102  * platforms.
1103  */
1104 DRIVER_MODULE(isa, isab, isa_driver, 0, 0);
1105 DRIVER_MODULE(isa, eisab, isa_driver, 0, 0);
1106 MODULE_VERSION(isa, 1);
1107 
1108 /*
1109  * Code common to ISA bridges.
1110  */
1111 
1112 int
isab_attach(device_t dev)1113 isab_attach(device_t dev)
1114 {
1115 	device_t child;
1116 
1117 	child = device_add_child(dev, "isa", 0);
1118 	if (child != NULL)
1119 		return (bus_generic_attach(dev));
1120 	return (ENXIO);
1121 }
1122 
1123 char *
pnp_eisaformat(uint32_t id)1124 pnp_eisaformat(uint32_t id)
1125 {
1126 	uint8_t *data;
1127 	static char idbuf[8];
1128 	const char  hextoascii[] = "0123456789abcdef";
1129 
1130 	id = htole32(id);
1131 	data = (uint8_t *)&id;
1132 	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
1133 	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
1134 	idbuf[2] = '@' + (data[1] & 0x1f);
1135 	idbuf[3] = hextoascii[(data[2] >> 4)];
1136 	idbuf[4] = hextoascii[(data[2] & 0xf)];
1137 	idbuf[5] = hextoascii[(data[3] >> 4)];
1138 	idbuf[6] = hextoascii[(data[3] & 0xf)];
1139 	idbuf[7] = 0;
1140 	return(idbuf);
1141 }
1142