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