1 /*        $NetBSD: sal.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $      */
2 
3 /*-
4  * Copyright (c) 2001 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_multiprocessor.h"
30 
31 #include <sys/cdefs.h>
32 
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 
38 #include <machine/efi.h>
39 #include <machine/vmparam.h>
40 #include <machine/md_var.h>
41 #include <machine/sal.h>
42 #include <machine/smp.h>
43 
44 /*
45  * IPIs are used more genericly than only
46  * for inter-processor interrupts. Don't
47  * make it a SMP specific thing...
48  */
49 int ipi_vector[IPI_COUNT];
50 
51 static struct ia64_fdesc sal_fdesc;
52 static sal_entry_t  fake_sal;
53 
54 extern u_int64_t    ia64_pal_entry;
55 sal_entry_t                   *ia64_sal_entry = fake_sal;
56 
57 static struct uuid sal_table = EFI_TABLE_SAL;
58 static struct sal_system_table *sal_systbl;
59 
60 static struct ia64_sal_result
fake_sal(u_int64_t a1,u_int64_t a2,u_int64_t a3,u_int64_t a4,u_int64_t a5,u_int64_t a6,u_int64_t a7,u_int64_t a8)61 fake_sal(u_int64_t a1, u_int64_t a2, u_int64_t a3, u_int64_t a4,
62            u_int64_t a5, u_int64_t a6, u_int64_t a7, u_int64_t a8)
63 {
64           struct ia64_sal_result res;
65           res.sal_status = -3;
66           res.sal_result[0] = 0;
67           res.sal_result[1] = 0;
68           res.sal_result[2] = 0;
69           return res;
70 }
71 
72 static void
setup_ipi_vectors(int ceil)73 setup_ipi_vectors(int ceil)
74 {
75           int ipi;
76 
77           ipi_vector[IPI_MCA_RENDEZ] = ceil - 0x10;
78           ipi_vector[IPI_MCA_CMCV] = ceil - 0x30;
79           ipi_vector[IPI_TEST] = ceil - 0x30 + 1;
80 
81           ipi = IPI_AST;                /* First generic IPI. */
82           ceil -= 0x20;                 /* First vector in group. */
83           while (ipi < IPI_COUNT)
84                     ipi_vector[ipi++] = ceil++;
85 }
86 
87 void
ia64_sal_init(void)88 ia64_sal_init(void)
89 {
90           static int sizes[6] = {
91                     48, 32, 16, 32, 16, 16
92           };
93           u_int8_t *p;
94           int i;
95 
96           sal_systbl = efi_get_table(&sal_table);
97           if (sal_systbl == NULL)
98                     return;
99 
100           if (memcmp(sal_systbl->sal_signature, SAL_SIGNATURE, 4)) {
101                     printf("Bad signature for SAL System Table\n");
102                     return;
103           }
104 
105           p = (u_int8_t *) (sal_systbl + 1);
106           for (i = 0; i < sal_systbl->sal_entry_count; i++) {
107                     switch (*p) {
108                     case 0: {
109                               struct sal_entrypoint_descriptor *dp;
110 
111                               dp = (struct sal_entrypoint_descriptor*)p;
112                               ia64_pal_entry = IA64_PHYS_TO_RR7(dp->sale_pal_proc);
113                               if (bootverbose)
114                                         printf("PAL Proc at 0x%lx\n", ia64_pal_entry);
115                               sal_fdesc.func = IA64_PHYS_TO_RR7(dp->sale_sal_proc);
116                               sal_fdesc.gp = IA64_PHYS_TO_RR7(dp->sale_sal_gp);
117                               if (bootverbose)
118                                         printf("SAL Proc at 0x%lx, GP at 0x%lx\n",
119                                             sal_fdesc.func, sal_fdesc.gp);
120                               ia64_sal_entry = (sal_entry_t *) &sal_fdesc;
121                               break;
122                     }
123                     case 5: {
124                               struct sal_ap_wakeup_descriptor *dp;
125 #ifdef MULTIPROCESSOR
126                               struct ia64_sal_result result;
127                               struct ia64_fdesc *fd;
128 #endif
129 
130                               dp = (struct sal_ap_wakeup_descriptor*)p;
131                               if (dp->sale_mechanism != 0) {
132                                         printf("SAL: unsupported AP wake-up mechanism "
133                                             "(%d)\n", dp->sale_mechanism);
134                                         break;
135                               }
136 
137                               if (dp->sale_vector < 0x10 || dp->sale_vector > 0xff) {
138                                         printf("SAL: invalid AP wake-up vector "
139                                             "(0x%lx)\n", dp->sale_vector);
140                                         break;
141                               }
142 
143                               /*
144                                * SAL documents that the wake-up vector should be
145                                * high (close to 255). The MCA rendezvous vector
146                                * should be less than the wake-up vector, but still
147                                * "high". We use the following priority assignment:
148                                *        Wake-up:  priority of the sale_vector
149                                *        Rendezvous:         priority-1
150                                *        Generic IPIs:       priority-2
151                                *        Special IPIs:       priority-3
152                                * Consequently, the wake-up priority should be at
153                                * least 4 (ie vector >= 0x40).
154                                */
155                               if (dp->sale_vector < 0x40) {
156                                         printf("SAL: AP wake-up vector too low "
157                                             "(0x%lx)\n", dp->sale_vector);
158                                         break;
159                               }
160 
161                               if (bootverbose)
162                                         printf("SAL: AP wake-up vector: 0x%lx\n",
163                                             dp->sale_vector);
164 
165                               ipi_vector[IPI_AP_WAKEUP] = dp->sale_vector;
166                               setup_ipi_vectors(dp->sale_vector & 0xf0);
167 
168 #ifdef MULTIPROCESSOR
169                               fd = (struct ia64_fdesc *) os_boot_rendez;
170                               result = ia64_sal_entry(SAL_SET_VECTORS,
171                                   SAL_OS_BOOT_RENDEZ, ia64_tpa(fd->func),
172                                   ia64_tpa(fd->gp), 0, 0, 0, 0);
173 #endif
174 
175                               break;
176                     }
177                     }
178                     p += sizes[*p];
179           }
180 
181           if (ipi_vector[IPI_AP_WAKEUP] == 0)
182                     setup_ipi_vectors(0xf0);
183 }
184