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 #include "opt_isa.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bus.h>
49 #include <sys/kernel.h>
50 #include <sys/linker.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <machine/intr_machdep.h>
55 #include <sys/rman.h>
56 #include <sys/interrupt.h>
57
58 #include <machine/vmparam.h>
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <machine/pmap.h>
62
63 #include <machine/metadata.h>
64 #include <machine/resource.h>
65 #include <machine/pc/bios.h>
66
67 #include "pcib_if.h"
68
69 #ifdef DEV_ISA
70 #include <isa/isavar.h>
71 #include <amd64/isa/isa.h>
72 #endif
73 #include <sys/rtprio.h>
74
75 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
76 struct nexus_device {
77 struct resource_list nx_resources;
78 };
79
80 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
81
82 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
83
84 static int nexus_probe(device_t);
85 static int nexus_attach(device_t);
86 static int nexus_print_all_resources(device_t dev);
87 static int nexus_print_child(device_t, device_t);
88 static device_t nexus_add_child(device_t bus, int order, const char *name,
89 int unit);
90 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
91 u_long, u_long, u_long, u_int);
92 static int nexus_config_intr(device_t, int, enum intr_trigger,
93 enum intr_polarity);
94 static int nexus_activate_resource(device_t, device_t, int, int,
95 struct resource *);
96 static int nexus_deactivate_resource(device_t, device_t, int, int,
97 struct resource *);
98 static int nexus_release_resource(device_t, device_t, int, int,
99 struct resource *);
100 static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
101 driver_filter_t filter, void (*)(void *), void *,
102 void **);
103 static int nexus_teardown_intr(device_t, device_t, struct resource *,
104 void *);
105 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
106 static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
107 static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
108 static void nexus_delete_resource(device_t, device_t, int, int);
109 static int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
110 static int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
111 static int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
112 static int nexus_release_msix(device_t pcib, device_t dev, int irq);
113 static int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
114
115 static device_method_t nexus_methods[] = {
116 /* Device interface */
117 DEVMETHOD(device_probe, nexus_probe),
118 DEVMETHOD(device_attach, nexus_attach),
119 DEVMETHOD(device_detach, bus_generic_detach),
120 DEVMETHOD(device_shutdown, bus_generic_shutdown),
121 DEVMETHOD(device_suspend, bus_generic_suspend),
122 DEVMETHOD(device_resume, bus_generic_resume),
123
124 /* Bus interface */
125 DEVMETHOD(bus_print_child, nexus_print_child),
126 DEVMETHOD(bus_add_child, nexus_add_child),
127 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
128 DEVMETHOD(bus_release_resource, nexus_release_resource),
129 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
130 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
131 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
132 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
133 DEVMETHOD(bus_config_intr, nexus_config_intr),
134 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
135 DEVMETHOD(bus_set_resource, nexus_set_resource),
136 DEVMETHOD(bus_get_resource, nexus_get_resource),
137 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
138
139 /* pcib interface */
140 DEVMETHOD(pcib_alloc_msi, nexus_alloc_msi),
141 DEVMETHOD(pcib_release_msi, nexus_release_msi),
142 DEVMETHOD(pcib_alloc_msix, nexus_alloc_msix),
143 DEVMETHOD(pcib_release_msix, nexus_release_msix),
144 DEVMETHOD(pcib_map_msi, nexus_map_msi),
145
146 { 0, 0 }
147 };
148
149 static driver_t nexus_driver = {
150 "nexus",
151 nexus_methods,
152 1, /* no softc */
153 };
154 static devclass_t nexus_devclass;
155
156 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
157
158 static int
159 nexus_probe(device_t dev)
160 {
161 int irq;
162
163 device_quiet(dev); /* suppress attach message for neatness */
164
165 /*
166 * XXX working notes:
167 *
168 * - IRQ resource creation should be moved to the PIC/APIC driver.
169 * - DRQ resource creation should be moved to the DMAC driver.
170 * - The above should be sorted to probe earlier than any child busses.
171 *
172 * - Leave I/O and memory creation here, as child probes may need them.
173 * (especially eg. ACPI)
174 */
175
176 /*
177 * IRQ's are on the mainboard on old systems, but on the ISA part
178 * of PCI->ISA bridges. There would be multiple sets of IRQs on
179 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
180 * component, so in a way, PCI can be a partial child of an ISA bus(!).
181 * APIC interrupts are global though.
182 */
183 irq_rman.rm_start = 0;
184 irq_rman.rm_type = RMAN_ARRAY;
185 irq_rman.rm_descr = "Interrupt request lines";
186 irq_rman.rm_end = NUM_IO_INTS - 1;
187 if (rman_init(&irq_rman))
188 panic("nexus_probe irq_rman");
189
190 /*
191 * We search for regions of existing IRQs and add those to the IRQ
192 * resource manager.
193 */
194 for (irq = 0; irq < NUM_IO_INTS; irq++)
195 if (intr_lookup_source(irq) != NULL)
196 if (rman_manage_region(&irq_rman, irq, irq) != 0)
197 panic("nexus_probe irq_rman add");
198
199 /*
200 * ISA DMA on PCI systems is implemented in the ISA part of each
201 * PCI->ISA bridge and the channels can be duplicated if there are
202 * multiple bridges. (eg: laptops with docking stations)
203 */
204 drq_rman.rm_start = 0;
205 drq_rman.rm_end = 7;
206 drq_rman.rm_type = RMAN_ARRAY;
207 drq_rman.rm_descr = "DMA request lines";
208 /* XXX drq 0 not available on some machines */
209 if (rman_init(&drq_rman)
210 || rman_manage_region(&drq_rman,
211 drq_rman.rm_start, drq_rman.rm_end))
212 panic("nexus_probe drq_rman");
213
214 /*
215 * However, IO ports and Memory truely are global at this level,
216 * as are APIC interrupts (however many IO APICS there turn out
217 * to be on large systems..)
218 */
219 port_rman.rm_start = 0;
220 port_rman.rm_end = 0xffff;
221 port_rman.rm_type = RMAN_ARRAY;
222 port_rman.rm_descr = "I/O ports";
223 if (rman_init(&port_rman)
224 || rman_manage_region(&port_rman, 0, 0xffff))
225 panic("nexus_probe port_rman");
226
227 mem_rman.rm_start = 0;
228 mem_rman.rm_end = ~0u;
229 mem_rman.rm_type = RMAN_ARRAY;
230 mem_rman.rm_descr = "I/O memory addresses";
231 if (rman_init(&mem_rman)
232 || rman_manage_region(&mem_rman, 0, ~0))
233 panic("nexus_probe mem_rman");
234
235 return 0;
236 }
237
238 static int
239 nexus_attach(device_t dev)
240 {
241
242 bus_generic_probe(dev);
243 bus_generic_attach(dev);
244 return 0;
245 }
246
247 static int
248 nexus_print_all_resources(device_t dev)
249 {
250 struct nexus_device *ndev = DEVTONX(dev);
251 struct resource_list *rl = &ndev->nx_resources;
252 int retval = 0;
253
254 if (STAILQ_FIRST(rl))
255 retval += printf(" at");
256
257 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
258 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
259 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
260
261 return retval;
262 }
263
264 static int
265 nexus_print_child(device_t bus, device_t child)
266 {
267 int retval = 0;
268
269 retval += bus_print_child_header(bus, child);
270 retval += nexus_print_all_resources(child);
271 if (device_get_flags(child))
272 retval += printf(" flags %#x", device_get_flags(child));
273 retval += printf(" on motherboard\n"); /* XXX "motherboard", ick */
274
275 return (retval);
276 }
277
278 static device_t
279 nexus_add_child(device_t bus, int order, const char *name, int unit)
280 {
281 device_t child;
282 struct nexus_device *ndev;
283
284 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
285 if (!ndev)
286 return(0);
287 resource_list_init(&ndev->nx_resources);
288
289 child = device_add_child_ordered(bus, order, name, unit);
290
291 /* should we free this in nexus_child_detached? */
292 device_set_ivars(child, ndev);
293
294 return(child);
295 }
296
297 /*
298 * Allocate a resource on behalf of child. NB: child is usually going to be a
299 * child of one of our descendants, not a direct child of nexus0.
300 */
301 static struct resource *
302 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
303 u_long start, u_long end, u_long count, u_int flags)
304 {
305 struct nexus_device *ndev = DEVTONX(child);
306 struct resource *rv;
307 struct resource_list_entry *rle;
308 struct rman *rm;
309 int needactivate = flags & RF_ACTIVE;
310
311 /*
312 * If this is an allocation of the "default" range for a given RID, and
313 * we know what the resources for this device are (ie. they aren't maintained
314 * by a child bus), then work out the start/end values.
315 */
316 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
317 if (ndev == NULL)
318 return(NULL);
319 rle = resource_list_find(&ndev->nx_resources, type, *rid);
320 if (rle == NULL)
321 return(NULL);
322 start = rle->start;
323 end = rle->end;
324 count = rle->count;
325 }
326
327 flags &= ~RF_ACTIVE;
328
329 switch (type) {
330 case SYS_RES_IRQ:
331 rm = &irq_rman;
332 break;
333
334 case SYS_RES_DRQ:
335 rm = &drq_rman;
336 break;
337
338 case SYS_RES_IOPORT:
339 rm = &port_rman;
340 break;
341
342 case SYS_RES_MEMORY:
343 rm = &mem_rman;
344 break;
345
346 default:
347 return 0;
348 }
349
350 rv = rman_reserve_resource(rm, start, end, count, flags, child);
351 if (rv == 0)
352 return 0;
353 rman_set_rid(rv, *rid);
354
355 if (needactivate) {
356 if (bus_activate_resource(child, type, *rid, rv)) {
357 rman_release_resource(rv);
358 return 0;
359 }
360 }
361
362 return rv;
363 }
364
365 static int
366 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
367 struct resource *r)
368 {
369
370 /*
371 * If this is a memory resource, map it into the kernel.
372 */
373 if (type == SYS_RES_MEMORY) {
374 void *vaddr;
375
376 vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r));
377 rman_set_virtual(r, vaddr);
378 rman_set_bustag(r, AMD64_BUS_SPACE_MEM);
379 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
380 } else if (type == SYS_RES_IOPORT) {
381 rman_set_bustag(r, AMD64_BUS_SPACE_IO);
382 rman_set_bushandle(r, rman_get_start(r));
383 }
384 return (rman_activate_resource(r));
385 }
386
387 static int
388 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
389 struct resource *r)
390 {
391 /*
392 * If this is a memory resource, unmap it.
393 */
394 if (type == SYS_RES_MEMORY) {
395 pmap_unmapdev((vm_offset_t)rman_get_virtual(r),
396 rman_get_size(r));
397 }
398
399 return (rman_deactivate_resource(r));
400 }
401
402 static int
403 nexus_release_resource(device_t bus, device_t child, int type, int rid,
404 struct resource *r)
405 {
406 if (rman_get_flags(r) & RF_ACTIVE) {
407 int error = bus_deactivate_resource(child, type, rid, r);
408 if (error)
409 return error;
410 }
411 return (rman_release_resource(r));
412 }
413
414 /*
415 * Currently this uses the really grody interface from kern/kern_intr.c
416 * (which really doesn't belong in kern/anything.c). Eventually, all of
417 * the code in kern_intr.c and machdep_intr.c should get moved here, since
418 * this is going to be the official interface.
419 */
420 static int
421 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
422 int flags, driver_filter_t filter, void (*ihand)(void *),
423 void *arg, void **cookiep)
424 {
425 int error;
426
427 /* somebody tried to setup an irq that failed to allocate! */
428 if (irq == NULL)
429 panic("nexus_setup_intr: NULL irq resource!");
430
431 *cookiep = 0;
432 if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
433 flags |= INTR_EXCL;
434
435 /*
436 * We depend here on rman_activate_resource() being idempotent.
437 */
438 error = rman_activate_resource(irq);
439 if (error)
440 return (error);
441
442 error = intr_add_handler(device_get_nameunit(child),
443 rman_get_start(irq), filter, ihand, arg, flags, cookiep);
444
445 return (error);
446 }
447
448 static int
449 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
450 {
451 return (intr_remove_handler(ih));
452 }
453
454 static int
455 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
456 enum intr_polarity pol)
457 {
458 return (intr_config_intr(irq, trig, pol));
459 }
460
461 static struct resource_list *
462 nexus_get_reslist(device_t dev, device_t child)
463 {
464 struct nexus_device *ndev = DEVTONX(child);
465
466 return (&ndev->nx_resources);
467 }
468
469 static int
470 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
471 {
472 struct nexus_device *ndev = DEVTONX(child);
473 struct resource_list *rl = &ndev->nx_resources;
474
475 /* XXX this should return a success/failure indicator */
476 resource_list_add(rl, type, rid, start, start + count - 1, count);
477 return(0);
478 }
479
480 static int
481 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
482 {
483 struct nexus_device *ndev = DEVTONX(child);
484 struct resource_list *rl = &ndev->nx_resources;
485 struct resource_list_entry *rle;
486
487 rle = resource_list_find(rl, type, rid);
488 if (!rle)
489 return(ENOENT);
490 if (startp)
491 *startp = rle->start;
492 if (countp)
493 *countp = rle->count;
494 return(0);
495 }
496
497 static void
498 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
499 {
500 struct nexus_device *ndev = DEVTONX(child);
501 struct resource_list *rl = &ndev->nx_resources;
502
503 resource_list_delete(rl, type, rid);
504 }
505
506 /* Called from the MSI code to add new IRQs to the IRQ rman. */
507 void
508 nexus_add_irq(u_long irq)
509 {
510
511 if (rman_manage_region(&irq_rman, irq, irq) != 0)
512 panic("%s: failed", __func__);
513 }
514
515 static int
516 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
517 {
518
519 return (msix_alloc(dev, irq));
520 }
521
522 static int
523 nexus_release_msix(device_t pcib, device_t dev, int irq)
524 {
525
526 return (msix_release(irq));
527 }
528
529 static int
530 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
531 {
532
533 return (msi_alloc(dev, count, maxcount, irqs));
534 }
535
536 static int
537 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
538 {
539
540 return (msi_release(irqs, count));
541 }
542
543 static int
544 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
545 {
546
547 return (msi_map(irq, addr, data));
548 }
549
550 /* Placeholder for system RAM. */
551 static void
552 ram_identify(driver_t *driver, device_t parent)
553 {
554
555 if (resource_disabled("ram", 0))
556 return;
557 if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
558 panic("ram_identify");
559 }
560
561 static int
562 ram_probe(device_t dev)
563 {
564
565 device_quiet(dev);
566 device_set_desc(dev, "System RAM");
567 return (0);
568 }
569
570 static int
571 ram_attach(device_t dev)
572 {
573 struct bios_smap *smapbase, *smap, *smapend;
574 struct resource *res;
575 caddr_t kmdp;
576 uint32_t smapsize;
577 int error, rid;
578
579 /* Retrieve the system memory map from the loader. */
580 kmdp = preload_search_by_type("elf kernel");
581 if (kmdp == NULL)
582 kmdp = preload_search_by_type("elf64 kernel");
583 smapbase = (struct bios_smap *)preload_search_info(kmdp,
584 MODINFO_METADATA | MODINFOMD_SMAP);
585 smapsize = *((u_int32_t *)smapbase - 1);
586 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
587
588 rid = 0;
589 for (smap = smapbase; smap < smapend; smap++) {
590 if (smap->type != 0x01 || smap->length == 0)
591 continue;
592 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, smap->base,
593 smap->length);
594 if (error)
595 panic("ram_attach: resource %d failed set with %d", rid,
596 error);
597 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
598 if (res == NULL)
599 panic("ram_attach: resource %d failed to attach", rid);
600 rid++;
601 }
602 return (0);
603 }
604
605 static device_method_t ram_methods[] = {
606 /* Device interface */
607 DEVMETHOD(device_identify, ram_identify),
608 DEVMETHOD(device_probe, ram_probe),
609 DEVMETHOD(device_attach, ram_attach),
610 { 0, 0 }
611 };
612
613 static driver_t ram_driver = {
614 "ram",
615 ram_methods,
616 1, /* no softc */
617 };
618
619 static devclass_t ram_devclass;
620
621 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0);
622
623 #ifdef DEV_ISA
624 /*
625 * Placeholder which claims PnP 'devices' which describe system
626 * resources.
627 */
628 static struct isa_pnp_id sysresource_ids[] = {
629 { 0x010cd041 /* PNP0c01 */, "System Memory" },
630 { 0x020cd041 /* PNP0c02 */, "System Resource" },
631 { 0 }
632 };
633
634 static int
635 sysresource_probe(device_t dev)
636 {
637 int result;
638
639 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
640 device_quiet(dev);
641 }
642 return(result);
643 }
644
645 static int
646 sysresource_attach(device_t dev)
647 {
648 return(0);
649 }
650
651 static device_method_t sysresource_methods[] = {
652 /* Device interface */
653 DEVMETHOD(device_probe, sysresource_probe),
654 DEVMETHOD(device_attach, sysresource_attach),
655 DEVMETHOD(device_detach, bus_generic_detach),
656 DEVMETHOD(device_shutdown, bus_generic_shutdown),
657 DEVMETHOD(device_suspend, bus_generic_suspend),
658 DEVMETHOD(device_resume, bus_generic_resume),
659 { 0, 0 }
660 };
661
662 static driver_t sysresource_driver = {
663 "sysresource",
664 sysresource_methods,
665 1, /* no softc */
666 };
667
668 static devclass_t sysresource_devclass;
669
670 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
671 #endif /* DEV_ISA */
Cache object: aded6839a0e77f385577448dc8bfcc72
|