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