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