Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / arch / ia64 / kernel / sal.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * System Abstraction Layer (SAL) interface routines.
4  *
5  * Copyright (C) 1998, 1999, 2001, 2003 Hewlett-Packard Co
6  *      David Mosberger-Tang <davidm@hpl.hp.com>
7  * Copyright (C) 1999 VA Linux Systems
8  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/string.h>
16
17 #include <asm/delay.h>
18 #include <asm/page.h>
19 #include <asm/sal.h>
20 #include <asm/pal.h>
21
22  __cacheline_aligned DEFINE_SPINLOCK(sal_lock);
23 unsigned long sal_platform_features;
24
25 unsigned short sal_revision;
26 unsigned short sal_version;
27
28 #define SAL_MAJOR(x) ((x) >> 8)
29 #define SAL_MINOR(x) ((x) & 0xff)
30
31 static struct {
32         void *addr;     /* function entry point */
33         void *gpval;    /* gp value to use */
34 } pdesc;
35
36 static long
37 default_handler (void)
38 {
39         return -1;
40 }
41
42 ia64_sal_handler ia64_sal = (ia64_sal_handler) default_handler;
43 ia64_sal_desc_ptc_t *ia64_ptc_domain_info;
44
45 const char *
46 ia64_sal_strerror (long status)
47 {
48         const char *str;
49         switch (status) {
50               case 0: str = "Call completed without error"; break;
51               case 1: str = "Effect a warm boot of the system to complete "
52                               "the update"; break;
53               case -1: str = "Not implemented"; break;
54               case -2: str = "Invalid argument"; break;
55               case -3: str = "Call completed with error"; break;
56               case -4: str = "Virtual address not registered"; break;
57               case -5: str = "No information available"; break;
58               case -6: str = "Insufficient space to add the entry"; break;
59               case -7: str = "Invalid entry_addr value"; break;
60               case -8: str = "Invalid interrupt vector"; break;
61               case -9: str = "Requested memory not available"; break;
62               case -10: str = "Unable to write to the NVM device"; break;
63               case -11: str = "Invalid partition type specified"; break;
64               case -12: str = "Invalid NVM_Object id specified"; break;
65               case -13: str = "NVM_Object already has the maximum number "
66                                 "of partitions"; break;
67               case -14: str = "Insufficient space in partition for the "
68                                 "requested write sub-function"; break;
69               case -15: str = "Insufficient data buffer space for the "
70                                 "requested read record sub-function"; break;
71               case -16: str = "Scratch buffer required for the write/delete "
72                                 "sub-function"; break;
73               case -17: str = "Insufficient space in the NVM_Object for the "
74                                 "requested create sub-function"; break;
75               case -18: str = "Invalid value specified in the partition_rec "
76                                 "argument"; break;
77               case -19: str = "Record oriented I/O not supported for this "
78                                 "partition"; break;
79               case -20: str = "Bad format of record to be written or "
80                                 "required keyword variable not "
81                                 "specified"; break;
82               default: str = "Unknown SAL status code"; break;
83         }
84         return str;
85 }
86
87 void __init
88 ia64_sal_handler_init (void *entry_point, void *gpval)
89 {
90         /* fill in the SAL procedure descriptor and point ia64_sal to it: */
91         pdesc.addr = entry_point;
92         pdesc.gpval = gpval;
93         ia64_sal = (ia64_sal_handler) &pdesc;
94 }
95
96 static void __init
97 check_versions (struct ia64_sal_systab *systab)
98 {
99         sal_revision = (systab->sal_rev_major << 8) | systab->sal_rev_minor;
100         sal_version = (systab->sal_b_rev_major << 8) | systab->sal_b_rev_minor;
101
102         /* Check for broken firmware */
103         if ((sal_revision == SAL_VERSION_CODE(49, 29))
104             && (sal_version == SAL_VERSION_CODE(49, 29)))
105         {
106                 /*
107                  * Old firmware for zx2000 prototypes have this weird version number,
108                  * reset it to something sane.
109                  */
110                 sal_revision = SAL_VERSION_CODE(2, 8);
111                 sal_version = SAL_VERSION_CODE(0, 0);
112         }
113
114         if (ia64_platform_is("sn2") && (sal_revision == SAL_VERSION_CODE(2, 9)))
115                 /*
116                  * SGI Altix has hard-coded version 2.9 in their prom
117                  * but they actually implement 3.2, so let's fix it here.
118                  */
119                 sal_revision = SAL_VERSION_CODE(3, 2);
120 }
121
122 static void __init
123 sal_desc_entry_point (void *p)
124 {
125         struct ia64_sal_desc_entry_point *ep = p;
126         ia64_pal_handler_init(__va(ep->pal_proc));
127         ia64_sal_handler_init(__va(ep->sal_proc), __va(ep->gp));
128 }
129
130 #ifdef CONFIG_SMP
131 static void __init
132 set_smp_redirect (int flag)
133 {
134 #ifndef CONFIG_HOTPLUG_CPU
135         if (no_int_routing)
136                 smp_int_redirect &= ~flag;
137         else
138                 smp_int_redirect |= flag;
139 #else
140         /*
141          * For CPU Hotplug we dont want to do any chipset supported
142          * interrupt redirection. The reason is this would require that
143          * All interrupts be stopped and hard bind the irq to a cpu.
144          * Later when the interrupt is fired we need to set the redir hint
145          * on again in the vector. This is cumbersome for something that the
146          * user mode irq balancer will solve anyways.
147          */
148         no_int_routing=1;
149         smp_int_redirect &= ~flag;
150 #endif
151 }
152 #else
153 #define set_smp_redirect(flag)  do { } while (0)
154 #endif
155
156 static void __init
157 sal_desc_platform_feature (void *p)
158 {
159         struct ia64_sal_desc_platform_feature *pf = p;
160         sal_platform_features = pf->feature_mask;
161
162         printk(KERN_INFO "SAL Platform features:");
163         if (!sal_platform_features) {
164                 printk(" None\n");
165                 return;
166         }
167
168         if (sal_platform_features & IA64_SAL_PLATFORM_FEATURE_BUS_LOCK)
169                 printk(" BusLock");
170         if (sal_platform_features & IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT) {
171                 printk(" IRQ_Redirection");
172                 set_smp_redirect(SMP_IRQ_REDIRECTION);
173         }
174         if (sal_platform_features & IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT) {
175                 printk(" IPI_Redirection");
176                 set_smp_redirect(SMP_IPI_REDIRECTION);
177         }
178         if (sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)
179                 printk(" ITC_Drift");
180         printk("\n");
181 }
182
183 #ifdef CONFIG_SMP
184 static void __init
185 sal_desc_ap_wakeup (void *p)
186 {
187         struct ia64_sal_desc_ap_wakeup *ap = p;
188
189         switch (ap->mechanism) {
190         case IA64_SAL_AP_EXTERNAL_INT:
191                 ap_wakeup_vector = ap->vector;
192                 printk(KERN_INFO "SAL: AP wakeup using external interrupt "
193                                 "vector 0x%lx\n", ap_wakeup_vector);
194                 break;
195         default:
196                 printk(KERN_ERR "SAL: AP wakeup mechanism unsupported!\n");
197                 break;
198         }
199 }
200
201 static void __init
202 chk_nointroute_opt(void)
203 {
204         char *cp;
205
206         for (cp = boot_command_line; *cp; ) {
207                 if (memcmp(cp, "nointroute", 10) == 0) {
208                         no_int_routing = 1;
209                         printk ("no_int_routing on\n");
210                         break;
211                 } else {
212                         while (*cp != ' ' && *cp)
213                                 ++cp;
214                         while (*cp == ' ')
215                                 ++cp;
216                 }
217         }
218 }
219
220 #else
221 static void __init sal_desc_ap_wakeup(void *p) { }
222 #endif
223
224 /*
225  * HP rx5670 firmware polls for interrupts during SAL_CACHE_FLUSH by reading
226  * cr.ivr, but it never writes cr.eoi.  This leaves any interrupt marked as
227  * "in-service" and masks other interrupts of equal or lower priority.
228  *
229  * HP internal defect reports: F1859, F2775, F3031.
230  */
231 static int sal_cache_flush_drops_interrupts;
232
233 static int __init
234 force_pal_cache_flush(char *str)
235 {
236         sal_cache_flush_drops_interrupts = 1;
237         return 0;
238 }
239 early_param("force_pal_cache_flush", force_pal_cache_flush);
240
241 void __init
242 check_sal_cache_flush (void)
243 {
244         unsigned long flags;
245         int cpu;
246         u64 vector, cache_type = 3;
247         struct ia64_sal_retval isrv;
248
249         if (sal_cache_flush_drops_interrupts)
250                 return;
251
252         cpu = get_cpu();
253         local_irq_save(flags);
254
255         /*
256          * Send ourselves a timer interrupt, wait until it's reported, and see
257          * if SAL_CACHE_FLUSH drops it.
258          */
259         platform_send_ipi(cpu, IA64_TIMER_VECTOR, IA64_IPI_DM_INT, 0);
260
261         while (!ia64_get_irr(IA64_TIMER_VECTOR))
262                 cpu_relax();
263
264         SAL_CALL(isrv, SAL_CACHE_FLUSH, cache_type, 0, 0, 0, 0, 0, 0);
265
266         if (isrv.status)
267                 printk(KERN_ERR "SAL_CAL_FLUSH failed with %ld\n", isrv.status);
268
269         if (ia64_get_irr(IA64_TIMER_VECTOR)) {
270                 vector = ia64_get_ivr();
271                 ia64_eoi();
272                 WARN_ON(vector != IA64_TIMER_VECTOR);
273         } else {
274                 sal_cache_flush_drops_interrupts = 1;
275                 printk(KERN_ERR "SAL: SAL_CACHE_FLUSH drops interrupts; "
276                         "PAL_CACHE_FLUSH will be used instead\n");
277                 ia64_eoi();
278         }
279
280         local_irq_restore(flags);
281         put_cpu();
282 }
283
284 s64
285 ia64_sal_cache_flush (u64 cache_type)
286 {
287         struct ia64_sal_retval isrv;
288
289         if (sal_cache_flush_drops_interrupts) {
290                 unsigned long flags;
291                 u64 progress;
292                 s64 rc;
293
294                 progress = 0;
295                 local_irq_save(flags);
296                 rc = ia64_pal_cache_flush(cache_type,
297                         PAL_CACHE_FLUSH_INVALIDATE, &progress, NULL);
298                 local_irq_restore(flags);
299                 return rc;
300         }
301
302         SAL_CALL(isrv, SAL_CACHE_FLUSH, cache_type, 0, 0, 0, 0, 0, 0);
303         return isrv.status;
304 }
305 EXPORT_SYMBOL_GPL(ia64_sal_cache_flush);
306
307 void __init
308 ia64_sal_init (struct ia64_sal_systab *systab)
309 {
310         char *p;
311         int i;
312
313         if (!systab) {
314                 printk(KERN_WARNING "Hmm, no SAL System Table.\n");
315                 return;
316         }
317
318         if (strncmp(systab->signature, "SST_", 4) != 0)
319                 printk(KERN_ERR "bad signature in system table!");
320
321         check_versions(systab);
322 #ifdef CONFIG_SMP
323         chk_nointroute_opt();
324 #endif
325
326         /* revisions are coded in BCD, so %x does the job for us */
327         printk(KERN_INFO "SAL %x.%x: %.32s %.32s%sversion %x.%x\n",
328                         SAL_MAJOR(sal_revision), SAL_MINOR(sal_revision),
329                         systab->oem_id, systab->product_id,
330                         systab->product_id[0] ? " " : "",
331                         SAL_MAJOR(sal_version), SAL_MINOR(sal_version));
332
333         p = (char *) (systab + 1);
334         for (i = 0; i < systab->entry_count; i++) {
335                 /*
336                  * The first byte of each entry type contains the type
337                  * descriptor.
338                  */
339                 switch (*p) {
340                 case SAL_DESC_ENTRY_POINT:
341                         sal_desc_entry_point(p);
342                         break;
343                 case SAL_DESC_PLATFORM_FEATURE:
344                         sal_desc_platform_feature(p);
345                         break;
346                 case SAL_DESC_PTC:
347                         ia64_ptc_domain_info = (ia64_sal_desc_ptc_t *)p;
348                         break;
349                 case SAL_DESC_AP_WAKEUP:
350                         sal_desc_ap_wakeup(p);
351                         break;
352                 }
353                 p += SAL_DESC_SIZE(*p);
354         }
355
356 }
357
358 int
359 ia64_sal_oemcall(struct ia64_sal_retval *isrvp, u64 oemfunc, u64 arg1,
360                  u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7)
361 {
362         if (oemfunc < IA64_SAL_OEMFUNC_MIN || oemfunc > IA64_SAL_OEMFUNC_MAX)
363                 return -1;
364         SAL_CALL(*isrvp, oemfunc, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
365         return 0;
366 }
367 EXPORT_SYMBOL(ia64_sal_oemcall);
368
369 int
370 ia64_sal_oemcall_nolock(struct ia64_sal_retval *isrvp, u64 oemfunc, u64 arg1,
371                         u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6,
372                         u64 arg7)
373 {
374         if (oemfunc < IA64_SAL_OEMFUNC_MIN || oemfunc > IA64_SAL_OEMFUNC_MAX)
375                 return -1;
376         SAL_CALL_NOLOCK(*isrvp, oemfunc, arg1, arg2, arg3, arg4, arg5, arg6,
377                         arg7);
378         return 0;
379 }
380 EXPORT_SYMBOL(ia64_sal_oemcall_nolock);
381
382 int
383 ia64_sal_oemcall_reentrant(struct ia64_sal_retval *isrvp, u64 oemfunc,
384                            u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5,
385                            u64 arg6, u64 arg7)
386 {
387         if (oemfunc < IA64_SAL_OEMFUNC_MIN || oemfunc > IA64_SAL_OEMFUNC_MAX)
388                 return -1;
389         SAL_CALL_REENTRANT(*isrvp, oemfunc, arg1, arg2, arg3, arg4, arg5, arg6,
390                            arg7);
391         return 0;
392 }
393 EXPORT_SYMBOL(ia64_sal_oemcall_reentrant);
394
395 long
396 ia64_sal_freq_base (unsigned long which, unsigned long *ticks_per_second,
397                     unsigned long *drift_info)
398 {
399         struct ia64_sal_retval isrv;
400
401         SAL_CALL(isrv, SAL_FREQ_BASE, which, 0, 0, 0, 0, 0, 0);
402         *ticks_per_second = isrv.v0;
403         *drift_info = isrv.v1;
404         return isrv.status;
405 }
406 EXPORT_SYMBOL_GPL(ia64_sal_freq_base);