powerpc/powernv: Fix endian issues with OPAL async code
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / powernv / opal.c
1 /*
2  * PowerNV OPAL high level interfaces
3  *
4  * Copyright 2011 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #undef DEBUG
13
14 #include <linux/types.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_platform.h>
18 #include <linux/interrupt.h>
19 #include <linux/notifier.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/kobject.h>
23 #include <linux/delay.h>
24 #include <linux/memblock.h>
25 #include <asm/opal.h>
26 #include <asm/firmware.h>
27 #include <asm/mce.h>
28
29 #include "powernv.h"
30
31 /* /sys/firmware/opal */
32 struct kobject *opal_kobj;
33
34 struct opal {
35         u64 base;
36         u64 entry;
37         u64 size;
38 } opal;
39
40 struct mcheck_recoverable_range {
41         u64 start_addr;
42         u64 end_addr;
43         u64 recover_addr;
44 };
45
46 static struct mcheck_recoverable_range *mc_recoverable_range;
47 static int mc_recoverable_range_len;
48
49 static struct device_node *opal_node;
50 static DEFINE_SPINLOCK(opal_write_lock);
51 extern u64 opal_mc_secondary_handler[];
52 static unsigned int *opal_irqs;
53 static unsigned int opal_irq_count;
54 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
55 static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
56 static DEFINE_SPINLOCK(opal_notifier_lock);
57 static uint64_t last_notified_mask = 0x0ul;
58 static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
59
60 int __init early_init_dt_scan_opal(unsigned long node,
61                                    const char *uname, int depth, void *data)
62 {
63         const void *basep, *entryp, *sizep;
64         unsigned long basesz, entrysz, runtimesz;
65
66         if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
67                 return 0;
68
69         basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
70         entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
71         sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
72
73         if (!basep || !entryp || !sizep)
74                 return 1;
75
76         opal.base = of_read_number(basep, basesz/4);
77         opal.entry = of_read_number(entryp, entrysz/4);
78         opal.size = of_read_number(sizep, runtimesz/4);
79
80         pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
81                  opal.base, basep, basesz);
82         pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
83                  opal.entry, entryp, entrysz);
84         pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%ld)\n",
85                  opal.size, sizep, runtimesz);
86
87         powerpc_firmware_features |= FW_FEATURE_OPAL;
88         if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
89                 powerpc_firmware_features |= FW_FEATURE_OPALv2;
90                 powerpc_firmware_features |= FW_FEATURE_OPALv3;
91                 printk("OPAL V3 detected !\n");
92         } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
93                 powerpc_firmware_features |= FW_FEATURE_OPALv2;
94                 printk("OPAL V2 detected !\n");
95         } else {
96                 printk("OPAL V1 detected !\n");
97         }
98
99         return 1;
100 }
101
102 int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
103                                    const char *uname, int depth, void *data)
104 {
105         unsigned long i, size;
106         const __be32 *prop;
107
108         if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
109                 return 0;
110
111         prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &size);
112
113         if (!prop)
114                 return 1;
115
116         pr_debug("Found machine check recoverable ranges.\n");
117
118         /*
119          * Allocate a buffer to hold the MC recoverable ranges. We would be
120          * accessing them in real mode, hence it needs to be within
121          * RMO region.
122          */
123         mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
124                                                         ppc64_rma_size));
125         memset(mc_recoverable_range, 0, size);
126
127         /*
128          * Each recoverable address entry is an (start address,len,
129          * recover address) pair, * 2 cells each, totalling 4 cells per entry.
130          */
131         for (i = 0; i < size / (sizeof(*prop) * 5); i++) {
132                 mc_recoverable_range[i].start_addr =
133                                         of_read_number(prop + (i * 5) + 0, 2);
134                 mc_recoverable_range[i].end_addr =
135                                         mc_recoverable_range[i].start_addr +
136                                         of_read_number(prop + (i * 5) + 2, 1);
137                 mc_recoverable_range[i].recover_addr =
138                                         of_read_number(prop + (i * 5) + 3, 2);
139
140                 pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
141                                 mc_recoverable_range[i].start_addr,
142                                 mc_recoverable_range[i].end_addr,
143                                 mc_recoverable_range[i].recover_addr);
144         }
145         mc_recoverable_range_len = i;
146         return 1;
147 }
148
149 static int __init opal_register_exception_handlers(void)
150 {
151 #ifdef __BIG_ENDIAN__
152         u64 glue;
153
154         if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
155                 return -ENODEV;
156
157         /* Hookup some exception handlers except machine check. We use the
158          * fwnmi area at 0x7000 to provide the glue space to OPAL
159          */
160         glue = 0x7000;
161         opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
162                                         0, glue);
163         glue += 128;
164         opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
165 #endif
166
167         return 0;
168 }
169
170 early_initcall(opal_register_exception_handlers);
171
172 int opal_notifier_register(struct notifier_block *nb)
173 {
174         if (!nb) {
175                 pr_warning("%s: Invalid argument (%p)\n",
176                            __func__, nb);
177                 return -EINVAL;
178         }
179
180         atomic_notifier_chain_register(&opal_notifier_head, nb);
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(opal_notifier_register);
184
185 int opal_notifier_unregister(struct notifier_block *nb)
186 {
187         if (!nb) {
188                 pr_warning("%s: Invalid argument (%p)\n",
189                            __func__, nb);
190                 return -EINVAL;
191         }
192
193         atomic_notifier_chain_unregister(&opal_notifier_head, nb);
194         return 0;
195 }
196 EXPORT_SYMBOL_GPL(opal_notifier_unregister);
197
198 static void opal_do_notifier(uint64_t events)
199 {
200         unsigned long flags;
201         uint64_t changed_mask;
202
203         if (atomic_read(&opal_notifier_hold))
204                 return;
205
206         spin_lock_irqsave(&opal_notifier_lock, flags);
207         changed_mask = last_notified_mask ^ events;
208         last_notified_mask = events;
209         spin_unlock_irqrestore(&opal_notifier_lock, flags);
210
211         /*
212          * We feed with the event bits and changed bits for
213          * enough information to the callback.
214          */
215         atomic_notifier_call_chain(&opal_notifier_head,
216                                    events, (void *)changed_mask);
217 }
218
219 void opal_notifier_update_evt(uint64_t evt_mask,
220                               uint64_t evt_val)
221 {
222         unsigned long flags;
223
224         spin_lock_irqsave(&opal_notifier_lock, flags);
225         last_notified_mask &= ~evt_mask;
226         last_notified_mask |= evt_val;
227         spin_unlock_irqrestore(&opal_notifier_lock, flags);
228 }
229
230 void opal_notifier_enable(void)
231 {
232         int64_t rc;
233         uint64_t evt = 0;
234
235         atomic_set(&opal_notifier_hold, 0);
236
237         /* Process pending events */
238         rc = opal_poll_events(&evt);
239         if (rc == OPAL_SUCCESS && evt)
240                 opal_do_notifier(evt);
241 }
242
243 void opal_notifier_disable(void)
244 {
245         atomic_set(&opal_notifier_hold, 1);
246 }
247
248 /*
249  * Opal message notifier based on message type. Allow subscribers to get
250  * notified for specific messgae type.
251  */
252 int opal_message_notifier_register(enum OpalMessageType msg_type,
253                                         struct notifier_block *nb)
254 {
255         if (!nb) {
256                 pr_warning("%s: Invalid argument (%p)\n",
257                            __func__, nb);
258                 return -EINVAL;
259         }
260         if (msg_type > OPAL_MSG_TYPE_MAX) {
261                 pr_warning("%s: Invalid message type argument (%d)\n",
262                            __func__, msg_type);
263                 return -EINVAL;
264         }
265         return atomic_notifier_chain_register(
266                                 &opal_msg_notifier_head[msg_type], nb);
267 }
268
269 static void opal_message_do_notify(uint32_t msg_type, void *msg)
270 {
271         /* notify subscribers */
272         atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
273                                         msg_type, msg);
274 }
275
276 static void opal_handle_message(void)
277 {
278         s64 ret;
279         /*
280          * TODO: pre-allocate a message buffer depending on opal-msg-size
281          * value in /proc/device-tree.
282          */
283         static struct opal_msg msg;
284         u32 type;
285
286         ret = opal_get_msg(__pa(&msg), sizeof(msg));
287         /* No opal message pending. */
288         if (ret == OPAL_RESOURCE)
289                 return;
290
291         /* check for errors. */
292         if (ret) {
293                 pr_warning("%s: Failed to retrive opal message, err=%lld\n",
294                                 __func__, ret);
295                 return;
296         }
297
298         type = be32_to_cpu(msg.msg_type);
299
300         /* Sanity check */
301         if (type > OPAL_MSG_TYPE_MAX) {
302                 pr_warning("%s: Unknown message type: %u\n", __func__, type);
303                 return;
304         }
305         opal_message_do_notify(type, (void *)&msg);
306 }
307
308 static int opal_message_notify(struct notifier_block *nb,
309                           unsigned long events, void *change)
310 {
311         if (events & OPAL_EVENT_MSG_PENDING)
312                 opal_handle_message();
313         return 0;
314 }
315
316 static struct notifier_block opal_message_nb = {
317         .notifier_call  = opal_message_notify,
318         .next           = NULL,
319         .priority       = 0,
320 };
321
322 static int __init opal_message_init(void)
323 {
324         int ret, i;
325
326         for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
327                 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
328
329         ret = opal_notifier_register(&opal_message_nb);
330         if (ret) {
331                 pr_err("%s: Can't register OPAL event notifier (%d)\n",
332                        __func__, ret);
333                 return ret;
334         }
335         return 0;
336 }
337 early_initcall(opal_message_init);
338
339 int opal_get_chars(uint32_t vtermno, char *buf, int count)
340 {
341         s64 rc;
342         __be64 evt, len;
343
344         if (!opal.entry)
345                 return -ENODEV;
346         opal_poll_events(&evt);
347         if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
348                 return 0;
349         len = cpu_to_be64(count);
350         rc = opal_console_read(vtermno, &len, buf);     
351         if (rc == OPAL_SUCCESS)
352                 return be64_to_cpu(len);
353         return 0;
354 }
355
356 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
357 {
358         int written = 0;
359         __be64 olen;
360         s64 len, rc;
361         unsigned long flags;
362         __be64 evt;
363
364         if (!opal.entry)
365                 return -ENODEV;
366
367         /* We want put_chars to be atomic to avoid mangling of hvsi
368          * packets. To do that, we first test for room and return
369          * -EAGAIN if there isn't enough.
370          *
371          * Unfortunately, opal_console_write_buffer_space() doesn't
372          * appear to work on opal v1, so we just assume there is
373          * enough room and be done with it
374          */
375         spin_lock_irqsave(&opal_write_lock, flags);
376         if (firmware_has_feature(FW_FEATURE_OPALv2)) {
377                 rc = opal_console_write_buffer_space(vtermno, &olen);
378                 len = be64_to_cpu(olen);
379                 if (rc || len < total_len) {
380                         spin_unlock_irqrestore(&opal_write_lock, flags);
381                         /* Closed -> drop characters */
382                         if (rc)
383                                 return total_len;
384                         opal_poll_events(NULL);
385                         return -EAGAIN;
386                 }
387         }
388
389         /* We still try to handle partial completions, though they
390          * should no longer happen.
391          */
392         rc = OPAL_BUSY;
393         while(total_len > 0 && (rc == OPAL_BUSY ||
394                                 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
395                 olen = cpu_to_be64(total_len);
396                 rc = opal_console_write(vtermno, &olen, data);
397                 len = be64_to_cpu(olen);
398
399                 /* Closed or other error drop */
400                 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
401                     rc != OPAL_BUSY_EVENT) {
402                         written = total_len;
403                         break;
404                 }
405                 if (rc == OPAL_SUCCESS) {
406                         total_len -= len;
407                         data += len;
408                         written += len;
409                 }
410                 /* This is a bit nasty but we need that for the console to
411                  * flush when there aren't any interrupts. We will clean
412                  * things a bit later to limit that to synchronous path
413                  * such as the kernel console and xmon/udbg
414                  */
415                 do
416                         opal_poll_events(&evt);
417                 while(rc == OPAL_SUCCESS &&
418                         (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
419         }
420         spin_unlock_irqrestore(&opal_write_lock, flags);
421         return written;
422 }
423
424 static int opal_recover_mce(struct pt_regs *regs,
425                                         struct machine_check_event *evt)
426 {
427         int recovered = 0;
428         uint64_t ea = get_mce_fault_addr(evt);
429
430         if (!(regs->msr & MSR_RI)) {
431                 /* If MSR_RI isn't set, we cannot recover */
432                 recovered = 0;
433         } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
434                 /* Platform corrected itself */
435                 recovered = 1;
436         } else if (ea && !is_kernel_addr(ea)) {
437                 /*
438                  * Faulting address is not in kernel text. We should be fine.
439                  * We need to find which process uses this address.
440                  * For now, kill the task if we have received exception when
441                  * in userspace.
442                  *
443                  * TODO: Queue up this address for hwpoisioning later.
444                  */
445                 if (user_mode(regs) && !is_global_init(current)) {
446                         _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
447                         recovered = 1;
448                 } else
449                         recovered = 0;
450         } else if (user_mode(regs) && !is_global_init(current) &&
451                 evt->severity == MCE_SEV_ERROR_SYNC) {
452                 /*
453                  * If we have received a synchronous error when in userspace
454                  * kill the task.
455                  */
456                 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
457                 recovered = 1;
458         }
459         return recovered;
460 }
461
462 int opal_machine_check(struct pt_regs *regs)
463 {
464         struct machine_check_event evt;
465
466         if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
467                 return 0;
468
469         /* Print things out */
470         if (evt.version != MCE_V1) {
471                 pr_err("Machine Check Exception, Unknown event version %d !\n",
472                        evt.version);
473                 return 0;
474         }
475         machine_check_print_event_info(&evt);
476
477         if (opal_recover_mce(regs, &evt))
478                 return 1;
479         return 0;
480 }
481
482 static uint64_t find_recovery_address(uint64_t nip)
483 {
484         int i;
485
486         for (i = 0; i < mc_recoverable_range_len; i++)
487                 if ((nip >= mc_recoverable_range[i].start_addr) &&
488                     (nip < mc_recoverable_range[i].end_addr))
489                     return mc_recoverable_range[i].recover_addr;
490         return 0;
491 }
492
493 bool opal_mce_check_early_recovery(struct pt_regs *regs)
494 {
495         uint64_t recover_addr = 0;
496
497         if (!opal.base || !opal.size)
498                 goto out;
499
500         if ((regs->nip >= opal.base) &&
501                         (regs->nip <= (opal.base + opal.size)))
502                 recover_addr = find_recovery_address(regs->nip);
503
504         /*
505          * Setup regs->nip to rfi into fixup address.
506          */
507         if (recover_addr)
508                 regs->nip = recover_addr;
509
510 out:
511         return !!recover_addr;
512 }
513
514 static irqreturn_t opal_interrupt(int irq, void *data)
515 {
516         __be64 events;
517
518         opal_handle_interrupt(virq_to_hw(irq), &events);
519
520         opal_do_notifier(events);
521
522         return IRQ_HANDLED;
523 }
524
525 static int opal_sysfs_init(void)
526 {
527         opal_kobj = kobject_create_and_add("opal", firmware_kobj);
528         if (!opal_kobj) {
529                 pr_warn("kobject_create_and_add opal failed\n");
530                 return -ENOMEM;
531         }
532
533         return 0;
534 }
535
536 static int __init opal_init(void)
537 {
538         struct device_node *np, *consoles;
539         const __be32 *irqs;
540         int rc, i, irqlen;
541
542         opal_node = of_find_node_by_path("/ibm,opal");
543         if (!opal_node) {
544                 pr_warn("opal: Node not found\n");
545                 return -ENODEV;
546         }
547
548         /* Register OPAL consoles if any ports */
549         if (firmware_has_feature(FW_FEATURE_OPALv2))
550                 consoles = of_find_node_by_path("/ibm,opal/consoles");
551         else
552                 consoles = of_node_get(opal_node);
553         if (consoles) {
554                 for_each_child_of_node(consoles, np) {
555                         if (strcmp(np->name, "serial"))
556                                 continue;
557                         of_platform_device_create(np, NULL, NULL);
558                 }
559                 of_node_put(consoles);
560         }
561
562         /* Find all OPAL interrupts and request them */
563         irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
564         pr_debug("opal: Found %d interrupts reserved for OPAL\n",
565                  irqs ? (irqlen / 4) : 0);
566         opal_irq_count = irqlen / 4;
567         opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
568         for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
569                 unsigned int hwirq = be32_to_cpup(irqs);
570                 unsigned int irq = irq_create_mapping(NULL, hwirq);
571                 if (irq == NO_IRQ) {
572                         pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
573                         continue;
574                 }
575                 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
576                 if (rc)
577                         pr_warning("opal: Error %d requesting irq %d"
578                                    " (0x%x)\n", rc, irq, hwirq);
579                 opal_irqs[i] = irq;
580         }
581
582         /* Create "opal" kobject under /sys/firmware */
583         rc = opal_sysfs_init();
584         if (rc == 0) {
585                 /* Setup error log interface */
586                 rc = opal_elog_init();
587                 /* Setup code update interface */
588                 opal_flash_init();
589                 /* Setup platform dump extract interface */
590                 opal_platform_dump_init();
591                 /* Setup system parameters interface */
592                 opal_sys_param_init();
593         }
594
595         return 0;
596 }
597 subsys_initcall(opal_init);
598
599 void opal_shutdown(void)
600 {
601         unsigned int i;
602         long rc = OPAL_BUSY;
603
604         /* First free interrupts, which will also mask them */
605         for (i = 0; i < opal_irq_count; i++) {
606                 if (opal_irqs[i])
607                         free_irq(opal_irqs[i], NULL);
608                 opal_irqs[i] = 0;
609         }
610
611         /*
612          * Then sync with OPAL which ensure anything that can
613          * potentially write to our memory has completed such
614          * as an ongoing dump retrieval
615          */
616         while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
617                 rc = opal_sync_host_reboot();
618                 if (rc == OPAL_BUSY)
619                         opal_poll_events(NULL);
620                 else
621                         mdelay(10);
622         }
623 }