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