1/*-
2 * Copyright (c) 2001 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <vm/vm.h>
36#include <vm/vm_kern.h>
37#include <machine/efi.h>
38#include <machine/intr.h>
39#include <machine/md_var.h>
40#include <machine/sal.h>
41#include <machine/smp.h>
42
43int ia64_ipi_wakeup;
44
45static struct ia64_fdesc sal_fdesc;
46static sal_entry_t	fake_sal;
47
48extern u_int64_t	ia64_pal_entry;
49sal_entry_t		*ia64_sal_entry = fake_sal;
50
51static struct uuid sal_table = EFI_TABLE_SAL;
52static struct sal_system_table *sal_systbl;
53
54static struct ia64_sal_result
55fake_sal(u_int64_t a1, u_int64_t a2, u_int64_t a3, u_int64_t a4,
56	 u_int64_t a5, u_int64_t a6, u_int64_t a7, u_int64_t a8)
57{
58	struct ia64_sal_result res;
59	res.sal_status = -3;
60	res.sal_result[0] = 0;
61	res.sal_result[1] = 0;
62	res.sal_result[2] = 0;
63	return res;
64}
65
66void
67ia64_sal_init(void)
68{
69	static int sizes[6] = {
70		48, 32, 16, 32, 16, 16
71	};
72	u_int8_t *p;
73	int error, i;
74
75	sal_systbl = efi_get_table(&sal_table);
76	if (sal_systbl == NULL)
77		return;
78
79	if (bcmp(sal_systbl->sal_signature, SAL_SIGNATURE, 4)) {
80		printf("Bad signature for SAL System Table\n");
81		return;
82	}
83
84	p = (u_int8_t *) (sal_systbl + 1);
85	for (i = 0; i < sal_systbl->sal_entry_count; i++) {
86		switch (*p) {
87		case 0: {
88			struct sal_entrypoint_descriptor *dp;
89
90			dp = (struct sal_entrypoint_descriptor*)p;
91			ia64_pal_entry = IA64_PHYS_TO_RR7(dp->sale_pal_proc);
92			if (bootverbose)
93				printf("PAL Proc at 0x%lx\n", ia64_pal_entry);
94			sal_fdesc.func = IA64_PHYS_TO_RR7(dp->sale_sal_proc);
95			sal_fdesc.gp = IA64_PHYS_TO_RR7(dp->sale_sal_gp);
96			if (bootverbose)
97				printf("SAL Proc at 0x%lx, GP at 0x%lx\n",
98				    sal_fdesc.func, sal_fdesc.gp);
99			ia64_sal_entry = (sal_entry_t *) &sal_fdesc;
100			break;
101		}
102		case 5: {
103			struct sal_ap_wakeup_descriptor *dp;
104
105			dp = (struct sal_ap_wakeup_descriptor*)p;
106			if (dp->sale_mechanism != 0) {
107				printf("SAL: unsupported AP wake-up mechanism "
108				    "(%d)\n", dp->sale_mechanism);
109				break;
110			}
111
112			/* Reserve the XIV so that we won't use it. */
113			error = ia64_xiv_reserve(dp->sale_vector,
114			    IA64_XIV_PLAT, NULL);
115			if (error) {
116				printf("SAL: invalid AP wake-up XIV (%#lx)\n",
117				    dp->sale_vector);
118				break;
119			}
120
121			ia64_ipi_wakeup = dp->sale_vector;
122			if (bootverbose)
123				printf("SAL: AP wake-up XIV: %#x\n",
124				    ia64_ipi_wakeup);
125			break;
126		}
127		}
128		p += sizes[*p];
129	}
130}
131