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