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