]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/platforms/powernv/opal.c
Merge remote-tracking branch 'dt-rh/for-next'
[karo-tx-linux.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 <asm/opal.h>
22 #include <asm/firmware.h>
23
24 #include "powernv.h"
25
26 struct opal {
27         u64 base;
28         u64 entry;
29 } opal;
30
31 static struct device_node *opal_node;
32 static DEFINE_SPINLOCK(opal_write_lock);
33 extern u64 opal_mc_secondary_handler[];
34 static unsigned int *opal_irqs;
35 static unsigned int opal_irq_count;
36 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
37 static DEFINE_SPINLOCK(opal_notifier_lock);
38 static uint64_t last_notified_mask = 0x0ul;
39 static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
40
41 int __init early_init_dt_scan_opal(unsigned long node,
42                                    const char *uname, int depth, void *data)
43 {
44         const void *basep, *entryp;
45         unsigned long basesz, entrysz;
46
47         if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
48                 return 0;
49
50         basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
51         entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
52
53         if (!basep || !entryp)
54                 return 1;
55
56         opal.base = of_read_number(basep, basesz/4);
57         opal.entry = of_read_number(entryp, entrysz/4);
58
59         pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
60                  opal.base, basep, basesz);
61         pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
62                  opal.entry, entryp, entrysz);
63
64         powerpc_firmware_features |= FW_FEATURE_OPAL;
65         if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
66                 powerpc_firmware_features |= FW_FEATURE_OPALv2;
67                 powerpc_firmware_features |= FW_FEATURE_OPALv3;
68                 printk("OPAL V3 detected !\n");
69         } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
70                 powerpc_firmware_features |= FW_FEATURE_OPALv2;
71                 printk("OPAL V2 detected !\n");
72         } else {
73                 printk("OPAL V1 detected !\n");
74         }
75
76         return 1;
77 }
78
79 static int __init opal_register_exception_handlers(void)
80 {
81 #ifdef __BIG_ENDIAN__
82         u64 glue;
83
84         if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
85                 return -ENODEV;
86
87         /* Hookup some exception handlers. We use the fwnmi area at 0x7000
88          * to provide the glue space to OPAL
89          */
90         glue = 0x7000;
91         opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
92                                         __pa(opal_mc_secondary_handler[0]),
93                                         glue);
94         glue += 128;
95         opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
96                                         0, glue);
97         glue += 128;
98         opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
99 #endif
100
101         return 0;
102 }
103
104 early_initcall(opal_register_exception_handlers);
105
106 int opal_notifier_register(struct notifier_block *nb)
107 {
108         if (!nb) {
109                 pr_warning("%s: Invalid argument (%p)\n",
110                            __func__, nb);
111                 return -EINVAL;
112         }
113
114         atomic_notifier_chain_register(&opal_notifier_head, nb);
115         return 0;
116 }
117
118 static void opal_do_notifier(uint64_t events)
119 {
120         unsigned long flags;
121         uint64_t changed_mask;
122
123         if (atomic_read(&opal_notifier_hold))
124                 return;
125
126         spin_lock_irqsave(&opal_notifier_lock, flags);
127         changed_mask = last_notified_mask ^ events;
128         last_notified_mask = events;
129         spin_unlock_irqrestore(&opal_notifier_lock, flags);
130
131         /*
132          * We feed with the event bits and changed bits for
133          * enough information to the callback.
134          */
135         atomic_notifier_call_chain(&opal_notifier_head,
136                                    events, (void *)changed_mask);
137 }
138
139 void opal_notifier_update_evt(uint64_t evt_mask,
140                               uint64_t evt_val)
141 {
142         unsigned long flags;
143
144         spin_lock_irqsave(&opal_notifier_lock, flags);
145         last_notified_mask &= ~evt_mask;
146         last_notified_mask |= evt_val;
147         spin_unlock_irqrestore(&opal_notifier_lock, flags);
148 }
149
150 void opal_notifier_enable(void)
151 {
152         int64_t rc;
153         uint64_t evt = 0;
154
155         atomic_set(&opal_notifier_hold, 0);
156
157         /* Process pending events */
158         rc = opal_poll_events(&evt);
159         if (rc == OPAL_SUCCESS && evt)
160                 opal_do_notifier(evt);
161 }
162
163 void opal_notifier_disable(void)
164 {
165         atomic_set(&opal_notifier_hold, 1);
166 }
167
168 int opal_get_chars(uint32_t vtermno, char *buf, int count)
169 {
170         s64 rc;
171         __be64 evt, len;
172
173         if (!opal.entry)
174                 return -ENODEV;
175         opal_poll_events(&evt);
176         if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
177                 return 0;
178         len = cpu_to_be64(count);
179         rc = opal_console_read(vtermno, &len, buf);     
180         if (rc == OPAL_SUCCESS)
181                 return be64_to_cpu(len);
182         return 0;
183 }
184
185 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
186 {
187         int written = 0;
188         __be64 olen;
189         s64 len, rc;
190         unsigned long flags;
191         __be64 evt;
192
193         if (!opal.entry)
194                 return -ENODEV;
195
196         /* We want put_chars to be atomic to avoid mangling of hvsi
197          * packets. To do that, we first test for room and return
198          * -EAGAIN if there isn't enough.
199          *
200          * Unfortunately, opal_console_write_buffer_space() doesn't
201          * appear to work on opal v1, so we just assume there is
202          * enough room and be done with it
203          */
204         spin_lock_irqsave(&opal_write_lock, flags);
205         if (firmware_has_feature(FW_FEATURE_OPALv2)) {
206                 rc = opal_console_write_buffer_space(vtermno, &olen);
207                 len = be64_to_cpu(olen);
208                 if (rc || len < total_len) {
209                         spin_unlock_irqrestore(&opal_write_lock, flags);
210                         /* Closed -> drop characters */
211                         if (rc)
212                                 return total_len;
213                         opal_poll_events(NULL);
214                         return -EAGAIN;
215                 }
216         }
217
218         /* We still try to handle partial completions, though they
219          * should no longer happen.
220          */
221         rc = OPAL_BUSY;
222         while(total_len > 0 && (rc == OPAL_BUSY ||
223                                 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
224                 olen = cpu_to_be64(total_len);
225                 rc = opal_console_write(vtermno, &olen, data);
226                 len = be64_to_cpu(olen);
227
228                 /* Closed or other error drop */
229                 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
230                     rc != OPAL_BUSY_EVENT) {
231                         written = total_len;
232                         break;
233                 }
234                 if (rc == OPAL_SUCCESS) {
235                         total_len -= len;
236                         data += len;
237                         written += len;
238                 }
239                 /* This is a bit nasty but we need that for the console to
240                  * flush when there aren't any interrupts. We will clean
241                  * things a bit later to limit that to synchronous path
242                  * such as the kernel console and xmon/udbg
243                  */
244                 do
245                         opal_poll_events(&evt);
246                 while(rc == OPAL_SUCCESS &&
247                         (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
248         }
249         spin_unlock_irqrestore(&opal_write_lock, flags);
250         return written;
251 }
252
253 int opal_machine_check(struct pt_regs *regs)
254 {
255         struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
256         struct opal_machine_check_event evt;
257         const char *level, *sevstr, *subtype;
258         static const char *opal_mc_ue_types[] = {
259                 "Indeterminate",
260                 "Instruction fetch",
261                 "Page table walk ifetch",
262                 "Load/Store",
263                 "Page table walk Load/Store",
264         };
265         static const char *opal_mc_slb_types[] = {
266                 "Indeterminate",
267                 "Parity",
268                 "Multihit",
269         };
270         static const char *opal_mc_erat_types[] = {
271                 "Indeterminate",
272                 "Parity",
273                 "Multihit",
274         };
275         static const char *opal_mc_tlb_types[] = {
276                 "Indeterminate",
277                 "Parity",
278                 "Multihit",
279         };
280
281         /* Copy the event structure and release the original */
282         evt = *opal_evt;
283         opal_evt->in_use = 0;
284
285         /* Print things out */
286         if (evt.version != OpalMCE_V1) {
287                 pr_err("Machine Check Exception, Unknown event version %d !\n",
288                        evt.version);
289                 return 0;
290         }
291         switch(evt.severity) {
292         case OpalMCE_SEV_NO_ERROR:
293                 level = KERN_INFO;
294                 sevstr = "Harmless";
295                 break;
296         case OpalMCE_SEV_WARNING:
297                 level = KERN_WARNING;
298                 sevstr = "";
299                 break;
300         case OpalMCE_SEV_ERROR_SYNC:
301                 level = KERN_ERR;
302                 sevstr = "Severe";
303                 break;
304         case OpalMCE_SEV_FATAL:
305         default:
306                 level = KERN_ERR;
307                 sevstr = "Fatal";
308                 break;
309         }
310
311         printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
312                evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
313                "Recovered" : "[Not recovered");
314         printk("%s  Initiator: %s\n", level,
315                evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
316         switch(evt.error_type) {
317         case OpalMCE_ERROR_TYPE_UE:
318                 subtype = evt.u.ue_error.ue_error_type <
319                         ARRAY_SIZE(opal_mc_ue_types) ?
320                         opal_mc_ue_types[evt.u.ue_error.ue_error_type]
321                         : "Unknown";
322                 printk("%s  Error type: UE [%s]\n", level, subtype);
323                 if (evt.u.ue_error.effective_address_provided)
324                         printk("%s    Effective address: %016llx\n",
325                                level, evt.u.ue_error.effective_address);
326                 if (evt.u.ue_error.physical_address_provided)
327                         printk("%s      Physial address: %016llx\n",
328                                level, evt.u.ue_error.physical_address);
329                 break;
330         case OpalMCE_ERROR_TYPE_SLB:
331                 subtype = evt.u.slb_error.slb_error_type <
332                         ARRAY_SIZE(opal_mc_slb_types) ?
333                         opal_mc_slb_types[evt.u.slb_error.slb_error_type]
334                         : "Unknown";
335                 printk("%s  Error type: SLB [%s]\n", level, subtype);
336                 if (evt.u.slb_error.effective_address_provided)
337                         printk("%s    Effective address: %016llx\n",
338                                level, evt.u.slb_error.effective_address);
339                 break;
340         case OpalMCE_ERROR_TYPE_ERAT:
341                 subtype = evt.u.erat_error.erat_error_type <
342                         ARRAY_SIZE(opal_mc_erat_types) ?
343                         opal_mc_erat_types[evt.u.erat_error.erat_error_type]
344                         : "Unknown";
345                 printk("%s  Error type: ERAT [%s]\n", level, subtype);
346                 if (evt.u.erat_error.effective_address_provided)
347                         printk("%s    Effective address: %016llx\n",
348                                level, evt.u.erat_error.effective_address);
349                 break;
350         case OpalMCE_ERROR_TYPE_TLB:
351                 subtype = evt.u.tlb_error.tlb_error_type <
352                         ARRAY_SIZE(opal_mc_tlb_types) ?
353                         opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
354                         : "Unknown";
355                 printk("%s  Error type: TLB [%s]\n", level, subtype);
356                 if (evt.u.tlb_error.effective_address_provided)
357                         printk("%s    Effective address: %016llx\n",
358                                level, evt.u.tlb_error.effective_address);
359                 break;
360         default:
361         case OpalMCE_ERROR_TYPE_UNKNOWN:
362                 printk("%s  Error type: Unknown\n", level);
363                 break;
364         }
365         return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
366 }
367
368 static irqreturn_t opal_interrupt(int irq, void *data)
369 {
370         __be64 events;
371
372         opal_handle_interrupt(virq_to_hw(irq), &events);
373
374         opal_do_notifier(events);
375
376         return IRQ_HANDLED;
377 }
378
379 static int __init opal_init(void)
380 {
381         struct device_node *np, *consoles;
382         const __be32 *irqs;
383         int rc, i, irqlen;
384
385         opal_node = of_find_node_by_path("/ibm,opal");
386         if (!opal_node) {
387                 pr_warn("opal: Node not found\n");
388                 return -ENODEV;
389         }
390
391         /* Register OPAL consoles if any ports */
392         if (firmware_has_feature(FW_FEATURE_OPALv2))
393                 consoles = of_find_node_by_path("/ibm,opal/consoles");
394         else
395                 consoles = of_node_get(opal_node);
396         if (consoles) {
397                 for_each_child_of_node(consoles, np) {
398                         if (strcmp(np->name, "serial"))
399                                 continue;
400                         of_platform_device_create(np, NULL, NULL);
401                 }
402                 of_node_put(consoles);
403         }
404
405         /* Find all OPAL interrupts and request them */
406         irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
407         pr_debug("opal: Found %d interrupts reserved for OPAL\n",
408                  irqs ? (irqlen / 4) : 0);
409         opal_irq_count = irqlen / 4;
410         opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
411         for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
412                 unsigned int hwirq = be32_to_cpup(irqs);
413                 unsigned int irq = irq_create_mapping(NULL, hwirq);
414                 if (irq == NO_IRQ) {
415                         pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
416                         continue;
417                 }
418                 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
419                 if (rc)
420                         pr_warning("opal: Error %d requesting irq %d"
421                                    " (0x%x)\n", rc, irq, hwirq);
422                 opal_irqs[i] = irq;
423         }
424         return 0;
425 }
426 subsys_initcall(opal_init);
427
428 void opal_shutdown(void)
429 {
430         unsigned int i;
431
432         for (i = 0; i < opal_irq_count; i++) {
433                 if (opal_irqs[i])
434                         free_irq(opal_irqs[i], NULL);
435                 opal_irqs[i] = 0;
436         }
437 }