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