xref: /freebsd/sys/x86/x86/nexus.c (revision 9768746b)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * This code implements a `root nexus' for Intel Architecture
35  * machines.  The function of the root nexus is to serve as an
36  * attachment point for both processors and buses, and to manage
37  * resources which are common to all of them.  In particular,
38  * this code implements the core resource managers for interrupt
39  * requests, DMA requests (which rightfully should be a part of the
40  * ISA code but it's easier to do it here for now), I/O port addresses,
41  * and I/O memory address space.
42  */
43 
44 #ifdef __amd64__
45 #define	DEV_APIC
46 #else
47 #include "opt_apic.h"
48 #endif
49 #include "opt_isa.h"
50 #include "opt_pci.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/bus.h>
55 #include <sys/kernel.h>
56 #include <sys/linker.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <machine/bus.h>
60 #include <machine/intr_machdep.h>
61 #include <sys/rman.h>
62 #include <sys/interrupt.h>
63 
64 #include <machine/md_var.h>
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_phys.h>
69 #include <vm/vm_dumpset.h>
70 #include <vm/pmap.h>
71 
72 #include <machine/metadata.h>
73 #include <machine/nexusvar.h>
74 #include <machine/resource.h>
75 #include <machine/pc/bios.h>
76 
77 #ifdef DEV_APIC
78 #include "pcib_if.h"
79 #endif
80 
81 #ifdef DEV_ISA
82 #include <isa/isavar.h>
83 #include <isa/isareg.h>
84 #endif
85 #include <sys/rtprio.h>
86 
87 #define	ELF_KERN_STR	("elf"__XSTRING(__ELF_WORD_SIZE)" kernel")
88 
89 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
90 
91 #define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
92 
93 struct rman irq_rman, drq_rman, port_rman, mem_rman;
94 
95 static	int nexus_probe(device_t);
96 static	int nexus_attach(device_t);
97 static	int nexus_print_all_resources(device_t dev);
98 static	int nexus_print_child(device_t, device_t);
99 static device_t nexus_add_child(device_t bus, u_int order, const char *name,
100 				int unit);
101 static	struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
102 					      rman_res_t, rman_res_t, rman_res_t,
103 					      u_int);
104 static	int nexus_adjust_resource(device_t, device_t, int, struct resource *,
105 				  rman_res_t, rman_res_t);
106 #ifdef SMP
107 static	int nexus_bind_intr(device_t, device_t, struct resource *, int);
108 #endif
109 static	int nexus_config_intr(device_t, int, enum intr_trigger,
110 			      enum intr_polarity);
111 static	int nexus_describe_intr(device_t dev, device_t child,
112 				struct resource *irq, void *cookie,
113 				const char *descr);
114 static	int nexus_activate_resource(device_t, device_t, int, int,
115 				    struct resource *);
116 static	int nexus_deactivate_resource(device_t, device_t, int, int,
117 				      struct resource *);
118 static	int nexus_map_resource(device_t bus, device_t child, int type,
119     			       struct resource *r,
120 			       struct resource_map_request *argsp,
121 			       struct resource_map *map);
122 static	int nexus_unmap_resource(device_t bus, device_t child, int type,
123 				 struct resource *r, struct resource_map *map);
124 static	int nexus_release_resource(device_t, device_t, int, int,
125 				   struct resource *);
126 static	int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
127 			     driver_filter_t filter, void (*)(void *), void *,
128 			      void **);
129 static	int nexus_teardown_intr(device_t, device_t, struct resource *,
130 				void *);
131 static	int nexus_suspend_intr(device_t, device_t, struct resource *);
132 static	int nexus_resume_intr(device_t, device_t, struct resource *);
133 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
134 static	int nexus_set_resource(device_t, device_t, int, int,
135 			       rman_res_t, rman_res_t);
136 static	int nexus_get_resource(device_t, device_t, int, int,
137 			       rman_res_t *, rman_res_t *);
138 static void nexus_delete_resource(device_t, device_t, int, int);
139 static	int nexus_get_cpus(device_t, device_t, enum cpu_sets, size_t,
140 			   cpuset_t *);
141 #if defined(DEV_APIC) && defined(DEV_PCI)
142 static	int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
143 static	int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
144 static	int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
145 static	int nexus_release_msix(device_t pcib, device_t dev, int irq);
146 static	int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
147 #endif
148 
149 static device_method_t nexus_methods[] = {
150 	/* Device interface */
151 	DEVMETHOD(device_probe,		nexus_probe),
152 	DEVMETHOD(device_attach,	nexus_attach),
153 	DEVMETHOD(device_detach,	bus_generic_detach),
154 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
155 	DEVMETHOD(device_suspend,	bus_generic_suspend),
156 	DEVMETHOD(device_resume,	bus_generic_resume),
157 
158 	/* Bus interface */
159 	DEVMETHOD(bus_print_child,	nexus_print_child),
160 	DEVMETHOD(bus_add_child,	nexus_add_child),
161 	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
162 	DEVMETHOD(bus_adjust_resource,	nexus_adjust_resource),
163 	DEVMETHOD(bus_release_resource,	nexus_release_resource),
164 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
165 	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
166 	DEVMETHOD(bus_map_resource,	nexus_map_resource),
167 	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
168 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
169 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
170 	DEVMETHOD(bus_suspend_intr,	nexus_suspend_intr),
171 	DEVMETHOD(bus_resume_intr,	nexus_resume_intr),
172 #ifdef SMP
173 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
174 #endif
175 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
176 	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
177 	DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
178 	DEVMETHOD(bus_set_resource,	nexus_set_resource),
179 	DEVMETHOD(bus_get_resource,	nexus_get_resource),
180 	DEVMETHOD(bus_delete_resource,	nexus_delete_resource),
181 	DEVMETHOD(bus_get_cpus,		nexus_get_cpus),
182 
183 	/* pcib interface */
184 #if defined(DEV_APIC) && defined(DEV_PCI)
185 	DEVMETHOD(pcib_alloc_msi,	nexus_alloc_msi),
186 	DEVMETHOD(pcib_release_msi,	nexus_release_msi),
187 	DEVMETHOD(pcib_alloc_msix,	nexus_alloc_msix),
188 	DEVMETHOD(pcib_release_msix,	nexus_release_msix),
189 	DEVMETHOD(pcib_map_msi,		nexus_map_msi),
190 #endif
191 	{ 0, 0 }
192 };
193 
194 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
195 
196 DRIVER_MODULE(nexus, root, nexus_driver, 0, 0);
197 
198 static int
199 nexus_probe(device_t dev)
200 {
201 
202 	device_quiet(dev);	/* suppress attach message for neatness */
203 	return (BUS_PROBE_GENERIC);
204 }
205 
206 void
207 nexus_init_resources(void)
208 {
209 	int irq;
210 
211 	/*
212 	 * XXX working notes:
213 	 *
214 	 * - IRQ resource creation should be moved to the PIC/APIC driver.
215 	 * - DRQ resource creation should be moved to the DMAC driver.
216 	 * - The above should be sorted to probe earlier than any child buses.
217 	 *
218 	 * - Leave I/O and memory creation here, as child probes may need them.
219 	 *   (especially eg. ACPI)
220 	 */
221 
222 	/*
223 	 * IRQ's are on the mainboard on old systems, but on the ISA part
224 	 * of PCI->ISA bridges.  There would be multiple sets of IRQs on
225 	 * multi-ISA-bus systems.  PCI interrupts are routed to the ISA
226 	 * component, so in a way, PCI can be a partial child of an ISA bus(!).
227 	 * APIC interrupts are global though.
228 	 */
229 	irq_rman.rm_start = 0;
230 	irq_rman.rm_type = RMAN_ARRAY;
231 	irq_rman.rm_descr = "Interrupt request lines";
232 	irq_rman.rm_end = num_io_irqs - 1;
233 	if (rman_init(&irq_rman))
234 		panic("nexus_init_resources irq_rman");
235 
236 	/*
237 	 * We search for regions of existing IRQs and add those to the IRQ
238 	 * resource manager.
239 	 */
240 	for (irq = 0; irq < num_io_irqs; irq++)
241 		if (intr_lookup_source(irq) != NULL)
242 			if (rman_manage_region(&irq_rman, irq, irq) != 0)
243 				panic("nexus_init_resources irq_rman add");
244 
245 	/*
246 	 * ISA DMA on PCI systems is implemented in the ISA part of each
247 	 * PCI->ISA bridge and the channels can be duplicated if there are
248 	 * multiple bridges.  (eg: laptops with docking stations)
249 	 */
250 	drq_rman.rm_start = 0;
251 	drq_rman.rm_end = 7;
252 	drq_rman.rm_type = RMAN_ARRAY;
253 	drq_rman.rm_descr = "DMA request lines";
254 	/* XXX drq 0 not available on some machines */
255 	if (rman_init(&drq_rman)
256 	    || rman_manage_region(&drq_rman,
257 				  drq_rman.rm_start, drq_rman.rm_end))
258 		panic("nexus_init_resources drq_rman");
259 
260 	/*
261 	 * However, IO ports and Memory truely are global at this level,
262 	 * as are APIC interrupts (however many IO APICS there turn out
263 	 * to be on large systems..)
264 	 */
265 	port_rman.rm_start = 0;
266 	port_rman.rm_end = 0xffff;
267 	port_rman.rm_type = RMAN_ARRAY;
268 	port_rman.rm_descr = "I/O ports";
269 	if (rman_init(&port_rman)
270 	    || rman_manage_region(&port_rman, 0, 0xffff))
271 		panic("nexus_init_resources port_rman");
272 
273 	mem_rman.rm_start = 0;
274 	mem_rman.rm_end = cpu_getmaxphyaddr();
275 	mem_rman.rm_type = RMAN_ARRAY;
276 	mem_rman.rm_descr = "I/O memory addresses";
277 	if (rman_init(&mem_rman)
278 	    || rman_manage_region(&mem_rman, 0, mem_rman.rm_end))
279 		panic("nexus_init_resources mem_rman");
280 }
281 
282 static int
283 nexus_attach(device_t dev)
284 {
285 
286 	nexus_init_resources();
287 	bus_generic_probe(dev);
288 
289 	/*
290 	 * Explicitly add the legacy0 device here.  Other platform
291 	 * types (such as ACPI), use their own nexus(4) subclass
292 	 * driver to override this routine and add their own root bus.
293 	 */
294 	if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL)
295 		panic("legacy: could not attach");
296 	bus_generic_attach(dev);
297 	return 0;
298 }
299 
300 static int
301 nexus_print_all_resources(device_t dev)
302 {
303 	struct	nexus_device *ndev = DEVTONX(dev);
304 	struct resource_list *rl = &ndev->nx_resources;
305 	int retval = 0;
306 
307 	if (STAILQ_FIRST(rl))
308 		retval += printf(" at");
309 
310 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
311 	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
312 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
313 
314 	return retval;
315 }
316 
317 static int
318 nexus_print_child(device_t bus, device_t child)
319 {
320 	int retval = 0;
321 
322 	retval += bus_print_child_header(bus, child);
323 	retval += nexus_print_all_resources(child);
324 	if (device_get_flags(child))
325 		retval += printf(" flags %#x", device_get_flags(child));
326 	retval += printf("\n");
327 
328 	return (retval);
329 }
330 
331 static device_t
332 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
333 {
334 	device_t		child;
335 	struct nexus_device	*ndev;
336 
337 	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
338 	if (!ndev)
339 		return(0);
340 	resource_list_init(&ndev->nx_resources);
341 
342 	child = device_add_child_ordered(bus, order, name, unit);
343 
344 	/* should we free this in nexus_child_detached? */
345 	device_set_ivars(child, ndev);
346 
347 	return(child);
348 }
349 
350 static struct rman *
351 nexus_rman(int type)
352 {
353 	switch (type) {
354 	case SYS_RES_IRQ:
355 		return (&irq_rman);
356 	case SYS_RES_DRQ:
357 		return (&drq_rman);
358 	case SYS_RES_IOPORT:
359 		return (&port_rman);
360 	case SYS_RES_MEMORY:
361 		return (&mem_rman);
362 	default:
363 		return (NULL);
364 	}
365 }
366 
367 /*
368  * Allocate a resource on behalf of child.  NB: child is usually going to be a
369  * child of one of our descendants, not a direct child of nexus0.
370  * (Exceptions include npx.)
371  */
372 static struct resource *
373 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
374 		     rman_res_t start, rman_res_t end, rman_res_t count,
375 		     u_int flags)
376 {
377 	struct nexus_device *ndev = DEVTONX(child);
378 	struct	resource *rv;
379 	struct resource_list_entry *rle;
380 	struct	rman *rm;
381 	int needactivate = flags & RF_ACTIVE;
382 
383 	/*
384 	 * If this is an allocation of the "default" range for a given
385 	 * RID, and we know what the resources for this device are
386 	 * (ie. they aren't maintained by a child bus), then work out
387 	 * the start/end values.
388 	 */
389 	if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) {
390 		if (device_get_parent(child) != bus || ndev == NULL)
391 			return(NULL);
392 		rle = resource_list_find(&ndev->nx_resources, type, *rid);
393 		if (rle == NULL)
394 			return(NULL);
395 		start = rle->start;
396 		end = rle->end;
397 		count = rle->count;
398 	}
399 
400 	flags &= ~RF_ACTIVE;
401 	rm = nexus_rman(type);
402 	if (rm == NULL)
403 		return (NULL);
404 
405 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
406 	if (rv == NULL)
407 		return 0;
408 	rman_set_rid(rv, *rid);
409 
410 	if (needactivate) {
411 		if (bus_activate_resource(child, type, *rid, rv)) {
412 			rman_release_resource(rv);
413 			return 0;
414 		}
415 	}
416 
417 	return rv;
418 }
419 
420 static int
421 nexus_adjust_resource(device_t bus, device_t child, int type,
422     struct resource *r, rman_res_t start, rman_res_t end)
423 {
424 	struct rman *rm;
425 
426 	rm = nexus_rman(type);
427 	if (rm == NULL)
428 		return (ENXIO);
429 	if (!rman_is_region_manager(r, rm))
430 		return (EINVAL);
431 	return (rman_adjust_resource(r, start, end));
432 }
433 
434 static int
435 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
436 			struct resource *r)
437 {
438 	struct resource_map map;
439 	int error;
440 
441 	error = rman_activate_resource(r);
442 	if (error != 0)
443 		return (error);
444 
445 	if (!(rman_get_flags(r) & RF_UNMAPPED) &&
446 	    (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
447 		error = nexus_map_resource(bus, child, type, r, NULL, &map);
448 		if (error) {
449 			rman_deactivate_resource(r);
450 			return (error);
451 		}
452 
453 		rman_set_mapping(r,&map);
454 	}
455 	return (0);
456 }
457 
458 static int
459 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
460 			  struct resource *r)
461 {
462 	struct resource_map map;
463 	int error;
464 
465 	error = rman_deactivate_resource(r);
466 	if (error)
467 		return (error);
468 
469 	if (!(rman_get_flags(r) & RF_UNMAPPED) &&
470 	    (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
471 		rman_get_mapping(r, &map);
472 		nexus_unmap_resource(bus, child, type, r, &map);
473 	}
474 	return (0);
475 }
476 
477 static int
478 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
479     struct resource_map_request *argsp, struct resource_map *map)
480 {
481 	struct resource_map_request args;
482 	rman_res_t end, length, start;
483 
484 	/* Resources must be active to be mapped. */
485 	if (!(rman_get_flags(r) & RF_ACTIVE))
486 		return (ENXIO);
487 
488 	/* Mappings are only supported on I/O and memory resources. */
489 	switch (type) {
490 	case SYS_RES_IOPORT:
491 	case SYS_RES_MEMORY:
492 		break;
493 	default:
494 		return (EINVAL);
495 	}
496 
497 	resource_init_map_request(&args);
498 	if (argsp != NULL)
499 		bcopy(argsp, &args, imin(argsp->size, args.size));
500 	start = rman_get_start(r) + args.offset;
501 	if (args.length == 0)
502 		length = rman_get_size(r);
503 	else
504 		length = args.length;
505 	end = start + length - 1;
506 	if (start > rman_get_end(r) || start < rman_get_start(r))
507 		return (EINVAL);
508 	if (end > rman_get_end(r) || end < start)
509 		return (EINVAL);
510 
511 	/*
512 	 * If this is a memory resource, map it into the kernel.
513 	 */
514 	switch (type) {
515 	case SYS_RES_IOPORT:
516 		map->r_bushandle = start;
517 		map->r_bustag = X86_BUS_SPACE_IO;
518 		map->r_size = length;
519 		map->r_vaddr = NULL;
520 		break;
521 	case SYS_RES_MEMORY:
522 		map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
523 		map->r_bustag = X86_BUS_SPACE_MEM;
524 		map->r_size = length;
525 
526 		/*
527 		 * The handle is the virtual address.
528 		 */
529 		map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
530 		break;
531 	}
532 	return (0);
533 }
534 
535 static int
536 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
537     struct resource_map *map)
538 {
539 
540 	/*
541 	 * If this is a memory resource, unmap it.
542 	 */
543 	switch (type) {
544 	case SYS_RES_MEMORY:
545 		pmap_unmapdev(map->r_vaddr, map->r_size);
546 		/* FALLTHROUGH */
547 	case SYS_RES_IOPORT:
548 		break;
549 	default:
550 		return (EINVAL);
551 	}
552 	return (0);
553 }
554 
555 static int
556 nexus_release_resource(device_t bus, device_t child, int type, int rid,
557 		       struct resource *r)
558 {
559 
560 	if (rman_get_flags(r) & RF_ACTIVE) {
561 		int error = bus_deactivate_resource(child, type, rid, r);
562 		if (error)
563 			return error;
564 	}
565 	return (rman_release_resource(r));
566 }
567 
568 /*
569  * Currently this uses the really grody interface from kern/kern_intr.c
570  * (which really doesn't belong in kern/anything.c).  Eventually, all of
571  * the code in kern_intr.c and machdep_intr.c should get moved here, since
572  * this is going to be the official interface.
573  */
574 static int
575 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
576 		 int flags, driver_filter_t filter, void (*ihand)(void *),
577 		 void *arg, void **cookiep)
578 {
579 	int		error, domain;
580 
581 	/* somebody tried to setup an irq that failed to allocate! */
582 	if (irq == NULL)
583 		panic("nexus_setup_intr: NULL irq resource!");
584 
585 	*cookiep = NULL;
586 	if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
587 		flags |= INTR_EXCL;
588 
589 	/*
590 	 * We depend here on rman_activate_resource() being idempotent.
591 	 */
592 	error = rman_activate_resource(irq);
593 	if (error)
594 		return (error);
595 	if (bus_get_domain(child, &domain) != 0)
596 		domain = 0;
597 
598 	error = intr_add_handler(device_get_nameunit(child),
599 	    rman_get_start(irq), filter, ihand, arg, flags, cookiep, domain);
600 	if (error == 0)
601 		rman_set_irq_cookie(irq, *cookiep);
602 
603 	return (error);
604 }
605 
606 static int
607 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
608 {
609 	int error;
610 
611 	error = intr_remove_handler(ih);
612 	if (error == 0)
613 		rman_set_irq_cookie(r, NULL);
614 	return (error);
615 }
616 
617 static int
618 nexus_suspend_intr(device_t dev, device_t child, struct resource *irq)
619 {
620 	return (intr_event_suspend_handler(rman_get_irq_cookie(irq)));
621 }
622 
623 static int
624 nexus_resume_intr(device_t dev, device_t child, struct resource *irq)
625 {
626 	return (intr_event_resume_handler(rman_get_irq_cookie(irq)));
627 }
628 
629 #ifdef SMP
630 static int
631 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
632 {
633 	return (intr_bind(rman_get_start(irq), cpu));
634 }
635 #endif
636 
637 static int
638 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
639     enum intr_polarity pol)
640 {
641 	return (intr_config_intr(irq, trig, pol));
642 }
643 
644 static int
645 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
646     void *cookie, const char *descr)
647 {
648 
649 	return (intr_describe(rman_get_start(irq), cookie, descr));
650 }
651 
652 static struct resource_list *
653 nexus_get_reslist(device_t dev, device_t child)
654 {
655 	struct nexus_device *ndev = DEVTONX(child);
656 
657 	return (&ndev->nx_resources);
658 }
659 
660 static int
661 nexus_set_resource(device_t dev, device_t child, int type, int rid,
662     rman_res_t start, rman_res_t count)
663 {
664 	struct nexus_device	*ndev = DEVTONX(child);
665 	struct resource_list	*rl = &ndev->nx_resources;
666 
667 	/* XXX this should return a success/failure indicator */
668 	resource_list_add(rl, type, rid, start, start + count - 1, count);
669 	return(0);
670 }
671 
672 static int
673 nexus_get_resource(device_t dev, device_t child, int type, int rid,
674     rman_res_t *startp, rman_res_t *countp)
675 {
676 	struct nexus_device	*ndev = DEVTONX(child);
677 	struct resource_list	*rl = &ndev->nx_resources;
678 	struct resource_list_entry *rle;
679 
680 	rle = resource_list_find(rl, type, rid);
681 	if (!rle)
682 		return(ENOENT);
683 	if (startp)
684 		*startp = rle->start;
685 	if (countp)
686 		*countp = rle->count;
687 	return(0);
688 }
689 
690 static void
691 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
692 {
693 	struct nexus_device	*ndev = DEVTONX(child);
694 	struct resource_list	*rl = &ndev->nx_resources;
695 
696 	resource_list_delete(rl, type, rid);
697 }
698 
699 static int
700 nexus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
701     cpuset_t *cpuset)
702 {
703 
704 	switch (op) {
705 #ifdef SMP
706 	case INTR_CPUS:
707 		if (setsize != sizeof(cpuset_t))
708 			return (EINVAL);
709 		*cpuset = intr_cpus;
710 		return (0);
711 #endif
712 	default:
713 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
714 	}
715 }
716 
717 /* Called from the MSI code to add new IRQs to the IRQ rman. */
718 void
719 nexus_add_irq(u_long irq)
720 {
721 
722 	if (rman_manage_region(&irq_rman, irq, irq) != 0)
723 		panic("%s: failed", __func__);
724 }
725 
726 #if defined(DEV_APIC) && defined(DEV_PCI)
727 static int
728 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
729 {
730 
731 	return (msix_alloc(dev, irq));
732 }
733 
734 static int
735 nexus_release_msix(device_t pcib, device_t dev, int irq)
736 {
737 
738 	return (msix_release(irq));
739 }
740 
741 static int
742 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
743 {
744 
745 	return (msi_alloc(dev, count, maxcount, irqs));
746 }
747 
748 static int
749 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
750 {
751 
752 	return (msi_release(irqs, count));
753 }
754 
755 static int
756 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
757 {
758 
759 	return (msi_map(irq, addr, data));
760 }
761 #endif /* DEV_APIC && DEV_PCI */
762 
763 /* Placeholder for system RAM. */
764 static void
765 ram_identify(driver_t *driver, device_t parent)
766 {
767 
768 	if (resource_disabled("ram", 0))
769 		return;
770 	if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
771 		panic("ram_identify");
772 }
773 
774 static int
775 ram_probe(device_t dev)
776 {
777 
778 	device_quiet(dev);
779 	device_set_desc(dev, "System RAM");
780 	return (0);
781 }
782 
783 static int
784 ram_attach(device_t dev)
785 {
786 	struct bios_smap *smapbase, *smap, *smapend;
787 	struct resource *res;
788 	rman_res_t length;
789 	vm_paddr_t *p;
790 	caddr_t kmdp;
791 	uint32_t smapsize;
792 	int error, rid;
793 
794 	/* Retrieve the system memory map from the loader. */
795 	kmdp = preload_search_by_type("elf kernel");
796 	if (kmdp == NULL)
797 		kmdp = preload_search_by_type(ELF_KERN_STR);
798 	smapbase = (struct bios_smap *)preload_search_info(kmdp,
799 	    MODINFO_METADATA | MODINFOMD_SMAP);
800 	if (smapbase != NULL) {
801 		smapsize = *((u_int32_t *)smapbase - 1);
802 		smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
803 
804 		rid = 0;
805 		for (smap = smapbase; smap < smapend; smap++) {
806 			if (smap->type != SMAP_TYPE_MEMORY ||
807 			    smap->length == 0)
808 				continue;
809 			if (smap->base > mem_rman.rm_end)
810 				continue;
811 			length = smap->base + smap->length > mem_rman.rm_end ?
812 			    mem_rman.rm_end - smap->base : smap->length;
813 			error = bus_set_resource(dev, SYS_RES_MEMORY, rid,
814 			    smap->base, length);
815 			if (error)
816 				panic(
817 				    "ram_attach: resource %d failed set with %d",
818 				    rid, error);
819 			res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
820 			    0);
821 			if (res == NULL)
822 				panic("ram_attach: resource %d failed to attach",
823 				    rid);
824 			rid++;
825 		}
826 		return (0);
827 	}
828 
829 	/*
830 	 * If the system map is not available, fall back to using
831 	 * dump_avail[].  We use the dump_avail[] array rather than
832 	 * phys_avail[] for the memory map as phys_avail[] contains
833 	 * holes for kernel memory, page 0, the message buffer, and
834 	 * the dcons buffer.  We test the end address in the loop
835 	 * instead of the start since the start address for the first
836 	 * segment is 0.
837 	 */
838 	for (rid = 0, p = dump_avail; p[1] != 0; rid++, p += 2) {
839 		if (p[0] > mem_rman.rm_end)
840 			break;
841 		length = (p[1] > mem_rman.rm_end ? mem_rman.rm_end : p[1]) -
842 		    p[0];
843 		error = bus_set_resource(dev, SYS_RES_MEMORY, rid, p[0],
844 		    length);
845 		if (error)
846 			panic("ram_attach: resource %d failed set with %d", rid,
847 			    error);
848 		res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
849 		if (res == NULL)
850 			panic("ram_attach: resource %d failed to attach", rid);
851 	}
852 	return (0);
853 }
854 
855 static device_method_t ram_methods[] = {
856 	/* Device interface */
857 	DEVMETHOD(device_identify,	ram_identify),
858 	DEVMETHOD(device_probe,		ram_probe),
859 	DEVMETHOD(device_attach,	ram_attach),
860 	{ 0, 0 }
861 };
862 
863 static driver_t ram_driver = {
864 	"ram",
865 	ram_methods,
866 	1,		/* no softc */
867 };
868 
869 DRIVER_MODULE(ram, nexus, ram_driver, 0, 0);
870 
871 #ifdef DEV_ISA
872 /*
873  * Placeholder which claims PnP 'devices' which describe system
874  * resources.
875  */
876 static struct isa_pnp_id sysresource_ids[] = {
877 	{ 0x010cd041 /* PNP0c01 */, "System Memory" },
878 	{ 0x020cd041 /* PNP0c02 */, "System Resource" },
879 	{ 0 }
880 };
881 
882 static int
883 sysresource_probe(device_t dev)
884 {
885 	int	result;
886 
887 	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
888 		device_quiet(dev);
889 	}
890 	return(result);
891 }
892 
893 static int
894 sysresource_attach(device_t dev)
895 {
896 	return(0);
897 }
898 
899 static device_method_t sysresource_methods[] = {
900 	/* Device interface */
901 	DEVMETHOD(device_probe,		sysresource_probe),
902 	DEVMETHOD(device_attach,	sysresource_attach),
903 	DEVMETHOD(device_detach,	bus_generic_detach),
904 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
905 	DEVMETHOD(device_suspend,	bus_generic_suspend),
906 	DEVMETHOD(device_resume,	bus_generic_resume),
907 	{ 0, 0 }
908 };
909 
910 static driver_t sysresource_driver = {
911 	"sysresource",
912 	sysresource_methods,
913 	1,		/* no softc */
914 };
915 
916 DRIVER_MODULE(sysresource, isa, sysresource_driver, 0, 0);
917 ISA_PNP_INFO(sysresource_ids);
918 #endif /* DEV_ISA */
919