nexus.c revision 205723
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 205723 2010-03-27 03:15:34Z 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/clock.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/module.h>
50#include <machine/bus.h>
51#include <sys/rman.h>
52#include <sys/interrupt.h>
53
54#include <vm/vm.h>
55#include <vm/pmap.h>
56
57#include <machine/efi.h>
58#include <machine/intr.h>
59#include <machine/pmap.h>
60#include <machine/resource.h>
61#include <machine/vmparam.h>
62
63#include <contrib/dev/acpica/include/acpi.h>
64
65#include <dev/acpica/acpivar.h>
66
67#include <isa/isareg.h>
68#include <sys/rtprio.h>
69
70#include "clock_if.h"
71
72static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
73struct nexus_device {
74	struct resource_list	nx_resources;
75};
76
77#define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
78
79static struct rman irq_rman, port_rman, mem_rman;
80
81static	int nexus_probe(device_t);
82static	int nexus_attach(device_t);
83static	int nexus_print_child(device_t, device_t);
84static device_t nexus_add_child(device_t bus, int order, const char *name,
85				int unit);
86static	struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
87					      u_long, u_long, u_long, u_int);
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			     driver_filter_t filter, void (*)(void *), void *,
96			     void **);
97static	int nexus_teardown_intr(device_t, device_t, struct resource *,
98				void *);
99static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
100static	int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
101static	int nexus_get_resource(device_t, device_t, int, int, u_long *,
102			       u_long *);
103static void nexus_delete_resource(device_t, device_t, int, int);
104static	int nexus_config_intr(device_t, int, enum intr_trigger,
105			      enum intr_polarity);
106
107static int nexus_gettime(device_t, struct timespec *);
108static int nexus_settime(device_t, struct timespec *);
109
110static device_method_t nexus_methods[] = {
111	/* Device interface */
112	DEVMETHOD(device_probe,		nexus_probe),
113	DEVMETHOD(device_attach,	nexus_attach),
114	DEVMETHOD(device_detach,	bus_generic_detach),
115	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
116	DEVMETHOD(device_suspend,	bus_generic_suspend),
117	DEVMETHOD(device_resume,	bus_generic_resume),
118
119	/* Bus interface */
120	DEVMETHOD(bus_print_child,	nexus_print_child),
121	DEVMETHOD(bus_add_child,	nexus_add_child),
122	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
123	DEVMETHOD(bus_release_resource,	nexus_release_resource),
124	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
125	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
126	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
127	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
128	DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
129	DEVMETHOD(bus_set_resource,	nexus_set_resource),
130	DEVMETHOD(bus_get_resource,	nexus_get_resource),
131	DEVMETHOD(bus_delete_resource,	nexus_delete_resource),
132	DEVMETHOD(bus_config_intr,	nexus_config_intr),
133
134	/* Clock interface */
135	DEVMETHOD(clock_gettime,	nexus_gettime),
136	DEVMETHOD(clock_settime,	nexus_settime),
137
138	{ 0, 0 }
139};
140
141static driver_t nexus_driver = {
142	"nexus",
143	nexus_methods,
144	1,			/* no softc */
145};
146static devclass_t nexus_devclass;
147
148DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
149
150static int
151nexus_probe(device_t dev)
152{
153
154	device_quiet(dev);	/* suppress attach message for neatness */
155
156	irq_rman.rm_type = RMAN_ARRAY;
157	irq_rman.rm_descr = "Interrupt request lines";
158	irq_rman.rm_start = 0;
159	irq_rman.rm_end = IA64_NXIVS - 1;
160	if (rman_init(&irq_rman)
161	    || rman_manage_region(&irq_rman,
162				  irq_rman.rm_start, irq_rman.rm_end))
163		panic("nexus_probe irq_rman");
164
165	port_rman.rm_start = 0;
166	port_rman.rm_end = 0xffff;
167	port_rman.rm_type = RMAN_ARRAY;
168	port_rman.rm_descr = "I/O ports";
169	if (rman_init(&port_rman)
170	    || rman_manage_region(&port_rman, 0, 0xffff))
171		panic("nexus_probe port_rman");
172
173	mem_rman.rm_start = 0;
174	mem_rman.rm_end = ~0u;
175	mem_rman.rm_type = RMAN_ARRAY;
176	mem_rman.rm_descr = "I/O memory addresses";
177	if (rman_init(&mem_rman)
178	    || rman_manage_region(&mem_rman, 0, ~0))
179		panic("nexus_probe mem_rman");
180
181	return bus_generic_probe(dev);
182}
183
184static int
185nexus_attach(device_t dev)
186{
187
188	/*
189	 * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
190	 */
191	outb(IO_ICU1+1, 0xff);
192	outb(IO_ICU2+1, 0xff);
193
194	if (acpi_identify() == 0)
195		BUS_ADD_CHILD(dev, 10, "acpi", 0);
196	clock_register(dev, 1000);
197	bus_generic_attach(dev);
198	return 0;
199}
200
201static int
202nexus_print_child(device_t bus, device_t child)
203{
204	struct nexus_device *ndev = DEVTONX(child);
205	struct resource_list *rl = &ndev->nx_resources;
206	int retval = 0;
207
208	retval += bus_print_child_header(bus, child);
209	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
210	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
211	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
212	if (device_get_flags(child))
213		retval += printf(" flags %#x", device_get_flags(child));
214	retval += printf(" on motherboard\n");	/* XXX "motherboard", ick */
215
216	return (retval);
217}
218
219static device_t
220nexus_add_child(device_t bus, int order, const char *name, int unit)
221{
222	device_t		child;
223	struct nexus_device	*ndev;
224
225	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
226	if (!ndev)
227		return(0);
228	resource_list_init(&ndev->nx_resources);
229
230	child = device_add_child_ordered(bus, order, name, unit);
231
232	/* should we free this in nexus_child_detached? */
233	device_set_ivars(child, ndev);
234
235	return(child);
236}
237
238
239/*
240 * Allocate a resource on behalf of child.  NB: child is usually going to be a
241 * child of one of our descendants, not a direct child of nexus0.
242 * (Exceptions include npx.)
243 */
244static struct resource *
245nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
246		     u_long start, u_long end, u_long count, u_int flags)
247{
248	struct nexus_device *ndev = DEVTONX(child);
249	struct	resource *rv;
250	struct resource_list_entry *rle;
251	struct	rman *rm;
252	int needactivate = flags & RF_ACTIVE;
253
254	/*
255	 * If this is an allocation of the "default" range for a given RID, and
256	 * we know what the resources for this device are (ie. they aren't maintained
257	 * by a child bus), then work out the start/end values.
258	 */
259	if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
260		if (ndev == NULL)
261			return(NULL);
262		rle = resource_list_find(&ndev->nx_resources, type, *rid);
263		if (rle == NULL)
264			return(NULL);
265		start = rle->start;
266		end = rle->end;
267		count = rle->count;
268	}
269
270	flags &= ~RF_ACTIVE;
271
272	switch (type) {
273	case SYS_RES_IRQ:
274		rm = &irq_rman;
275		break;
276
277	case SYS_RES_IOPORT:
278		rm = &port_rman;
279		break;
280
281	case SYS_RES_MEMORY:
282		rm = &mem_rman;
283		break;
284
285	default:
286		return 0;
287	}
288
289	rv = rman_reserve_resource(rm, start, end, count, flags, child);
290	if (rv == 0)
291		return 0;
292	rman_set_rid(rv, *rid);
293
294	if (needactivate) {
295		if (bus_activate_resource(child, type, *rid, rv)) {
296			rman_release_resource(rv);
297			return 0;
298		}
299	}
300
301	return rv;
302}
303
304static int
305nexus_activate_resource(device_t bus, device_t child, int type, int rid,
306    struct resource *r)
307{
308	vm_paddr_t paddr;
309	void *vaddr;
310
311	paddr = rman_get_start(r);
312
313	switch (type) {
314	case SYS_RES_IOPORT:
315		rman_set_bustag(r, IA64_BUS_SPACE_IO);
316		rman_set_bushandle(r, paddr);
317		break;
318	case SYS_RES_MEMORY:
319		vaddr = pmap_mapdev(paddr, rman_get_size(r));
320		rman_set_bustag(r, IA64_BUS_SPACE_MEM);
321		rman_set_bushandle(r, (bus_space_handle_t) vaddr);
322		rman_set_virtual(r, vaddr);
323		break;
324	}
325	return (rman_activate_resource(r));
326}
327
328static int
329nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
330			  struct resource *r)
331{
332
333	return (rman_deactivate_resource(r));
334}
335
336static int
337nexus_release_resource(device_t bus, device_t child, int type, int rid,
338		       struct resource *r)
339{
340	if (rman_get_flags(r) & RF_ACTIVE) {
341		int error = bus_deactivate_resource(child, type, rid, r);
342		if (error)
343			return error;
344	}
345	return (rman_release_resource(r));
346}
347
348/*
349 * Currently this uses the really grody interface from kern/kern_intr.c
350 * (which really doesn't belong in kern/anything.c).  Eventually, all of
351 * the code in kern_intr.c and machdep_intr.c should get moved here, since
352 * this is going to be the official interface.
353 */
354static int
355nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
356		 int flags, driver_filter_t filter, void (*ihand)(void *),
357		 void *arg, void **cookiep)
358{
359	driver_t	*driver;
360	int		error;
361
362	/* somebody tried to setup an irq that failed to allocate! */
363	if (irq == NULL)
364		panic("nexus_setup_intr: NULL irq resource!");
365
366	*cookiep = 0;
367	if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
368		flags |= INTR_EXCL;
369
370	driver = device_get_driver(child);
371
372	/*
373	 * We depend here on rman_activate_resource() being idempotent.
374	 */
375	error = rman_activate_resource(irq);
376	if (error)
377		return (error);
378
379	error = ia64_setup_intr(device_get_nameunit(child),
380	    rman_get_start(irq), filter, ihand, arg, flags, cookiep);
381
382	return (error);
383}
384
385static int
386nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
387    void *cookie)
388{
389
390	return (ia64_teardown_intr(cookie));
391}
392
393static struct resource_list *
394nexus_get_reslist(device_t dev, device_t child)
395{
396	struct nexus_device *ndev = DEVTONX(child);
397
398	return (&ndev->nx_resources);
399}
400
401static int
402nexus_set_resource(device_t dev, device_t child, int type, int rid,
403    u_long start, u_long count)
404{
405	struct nexus_device	*ndev = DEVTONX(child);
406	struct resource_list	*rl = &ndev->nx_resources;
407
408	if (type == SYS_RES_IOPORT && start > (0x10000 - count)) {
409		/*
410		 * Work around a firmware bug in the HP rx2660, where in ACPI
411		 * an I/O port is really a memory mapped I/O address. The bug
412		 * is in the GAS that describes the address and in particular
413		 * the SpaceId field. The field should not say the address is
414		 * an I/O port when it is in fact an I/O memory address.
415		 */
416		if (bootverbose)
417			printf("%s: invalid port range (%#lx-%#lx); "
418			    "assuming I/O memory range.\n", __func__, start,
419			    start + count - 1);
420		type = SYS_RES_MEMORY;
421	}
422
423	/* XXX this should return a success/failure indicator */
424	resource_list_add(rl, type, rid, start, start + count - 1, count);
425	return(0);
426}
427
428static int
429nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
430{
431	struct nexus_device	*ndev = DEVTONX(child);
432	struct resource_list	*rl = &ndev->nx_resources;
433	struct resource_list_entry *rle;
434
435	rle = resource_list_find(rl, type, rid);
436	device_printf(child, "type %d  rid %d  startp %p  countp %p - got %p\n",
437		      type, rid, startp, countp, rle);
438	if (!rle)
439		return(ENOENT);
440	if (startp)
441		*startp = rle->start;
442	if (countp)
443		*countp = rle->count;
444	return(0);
445}
446
447static void
448nexus_delete_resource(device_t dev, device_t child, int type, int rid)
449{
450	struct nexus_device	*ndev = DEVTONX(child);
451	struct resource_list	*rl = &ndev->nx_resources;
452
453	resource_list_delete(rl, type, rid);
454}
455
456static int
457nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
458    enum intr_polarity pol)
459{
460
461	return (sapic_config_intr(irq, trig, pol));
462}
463
464static int
465nexus_gettime(device_t dev, struct timespec *ts)
466{
467	struct clocktime ct;
468	struct efi_tm tm;
469
470	efi_get_time(&tm);
471
472	/*
473	 * This code was written in 2005, so logically EFI cannot return
474	 * a year smaller than that. Assume the EFI clock is out of whack
475	 * in that case and reset the EFI clock.
476	 */
477	if (tm.tm_year < 2005)
478		return (EINVAL);
479
480	ct.nsec = tm.tm_nsec;
481	ct.sec = tm.tm_sec;
482	ct.min = tm.tm_min;
483	ct.hour = tm.tm_hour;
484	ct.day = tm.tm_mday;
485	ct.mon = tm.tm_mon;
486	ct.year = tm.tm_year;
487	ct.dow = -1;
488	return (clock_ct_to_ts(&ct, ts));
489}
490
491static int
492nexus_settime(device_t dev, struct timespec *ts)
493{
494	struct clocktime ct;
495	struct efi_tm tm;
496
497	efi_get_time(&tm);
498
499	clock_ts_to_ct(ts, &ct);
500	tm.tm_nsec = ts->tv_nsec;
501	tm.tm_sec = ct.sec;
502	tm.tm_min = ct.min;
503	tm.tm_hour = ct.hour;
504	tm.tm_year = ct.year;
505	tm.tm_mon = ct.mon;
506	tm.tm_mday = ct.day;
507	return (efi_set_time(&tm));
508}
509