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