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