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