xfs: increase number of ACL entries for V5 superblocks
[linux-2.6-microblaze.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_platform.h>
17 #include <linux/interrupt.h>
18 #include <asm/opal.h>
19 #include <asm/firmware.h>
20
21 #include "powernv.h"
22
23 struct opal {
24         u64 base;
25         u64 entry;
26 } opal;
27
28 static struct device_node *opal_node;
29 static DEFINE_SPINLOCK(opal_write_lock);
30 extern u64 opal_mc_secondary_handler[];
31
32 int __init early_init_dt_scan_opal(unsigned long node,
33                                    const char *uname, int depth, void *data)
34 {
35         const void *basep, *entryp;
36         unsigned long basesz, entrysz;
37
38         if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
39                 return 0;
40
41         basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
42         entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
43
44         if (!basep || !entryp)
45                 return 1;
46
47         opal.base = of_read_number(basep, basesz/4);
48         opal.entry = of_read_number(entryp, entrysz/4);
49
50         pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
51                  opal.base, basep, basesz);
52         pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
53                  opal.entry, entryp, entrysz);
54
55         powerpc_firmware_features |= FW_FEATURE_OPAL;
56         if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
57                 powerpc_firmware_features |= FW_FEATURE_OPALv2;
58                 printk("OPAL V2 detected !\n");
59         } else {
60                 printk("OPAL V1 detected !\n");
61         }
62
63         return 1;
64 }
65
66 static int __init opal_register_exception_handlers(void)
67 {
68         u64 glue;
69
70         if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
71                 return -ENODEV;
72
73         /* Hookup some exception handlers. We use the fwnmi area at 0x7000
74          * to provide the glue space to OPAL
75          */
76         glue = 0x7000;
77         opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
78                                         __pa(opal_mc_secondary_handler[0]),
79                                         glue);
80         glue += 128;
81         opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
82                                         0, glue);
83         glue += 128;
84         opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
85
86         return 0;
87 }
88
89 early_initcall(opal_register_exception_handlers);
90
91 int opal_get_chars(uint32_t vtermno, char *buf, int count)
92 {
93         s64 len, rc;
94         u64 evt;
95
96         if (!opal.entry)
97                 return -ENODEV;
98         opal_poll_events(&evt);
99         if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
100                 return 0;
101         len = count;
102         rc = opal_console_read(vtermno, &len, buf);
103         if (rc == OPAL_SUCCESS)
104                 return len;
105         return 0;
106 }
107
108 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
109 {
110         int written = 0;
111         s64 len, rc;
112         unsigned long flags;
113         u64 evt;
114
115         if (!opal.entry)
116                 return -ENODEV;
117
118         /* We want put_chars to be atomic to avoid mangling of hvsi
119          * packets. To do that, we first test for room and return
120          * -EAGAIN if there isn't enough.
121          *
122          * Unfortunately, opal_console_write_buffer_space() doesn't
123          * appear to work on opal v1, so we just assume there is
124          * enough room and be done with it
125          */
126         spin_lock_irqsave(&opal_write_lock, flags);
127         if (firmware_has_feature(FW_FEATURE_OPALv2)) {
128                 rc = opal_console_write_buffer_space(vtermno, &len);
129                 if (rc || len < total_len) {
130                         spin_unlock_irqrestore(&opal_write_lock, flags);
131                         /* Closed -> drop characters */
132                         if (rc)
133                                 return total_len;
134                         opal_poll_events(&evt);
135                         return -EAGAIN;
136                 }
137         }
138
139         /* We still try to handle partial completions, though they
140          * should no longer happen.
141          */
142         rc = OPAL_BUSY;
143         while(total_len > 0 && (rc == OPAL_BUSY ||
144                                 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
145                 len = total_len;
146                 rc = opal_console_write(vtermno, &len, data);
147                 if (rc == OPAL_SUCCESS) {
148                         total_len -= len;
149                         data += len;
150                         written += len;
151                 }
152                 /* This is a bit nasty but we need that for the console to
153                  * flush when there aren't any interrupts. We will clean
154                  * things a bit later to limit that to synchronous path
155                  * such as the kernel console and xmon/udbg
156                  */
157                 do
158                         opal_poll_events(&evt);
159                 while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
160         }
161         spin_unlock_irqrestore(&opal_write_lock, flags);
162         return written;
163 }
164
165 int opal_machine_check(struct pt_regs *regs)
166 {
167         struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
168         struct opal_machine_check_event evt;
169         const char *level, *sevstr, *subtype;
170         static const char *opal_mc_ue_types[] = {
171                 "Indeterminate",
172                 "Instruction fetch",
173                 "Page table walk ifetch",
174                 "Load/Store",
175                 "Page table walk Load/Store",
176         };
177         static const char *opal_mc_slb_types[] = {
178                 "Indeterminate",
179                 "Parity",
180                 "Multihit",
181         };
182         static const char *opal_mc_erat_types[] = {
183                 "Indeterminate",
184                 "Parity",
185                 "Multihit",
186         };
187         static const char *opal_mc_tlb_types[] = {
188                 "Indeterminate",
189                 "Parity",
190                 "Multihit",
191         };
192
193         /* Copy the event structure and release the original */
194         evt = *opal_evt;
195         opal_evt->in_use = 0;
196
197         /* Print things out */
198         if (evt.version != OpalMCE_V1) {
199                 pr_err("Machine Check Exception, Unknown event version %d !\n",
200                        evt.version);
201                 return 0;
202         }
203         switch(evt.severity) {
204         case OpalMCE_SEV_NO_ERROR:
205                 level = KERN_INFO;
206                 sevstr = "Harmless";
207                 break;
208         case OpalMCE_SEV_WARNING:
209                 level = KERN_WARNING;
210                 sevstr = "";
211                 break;
212         case OpalMCE_SEV_ERROR_SYNC:
213                 level = KERN_ERR;
214                 sevstr = "Severe";
215                 break;
216         case OpalMCE_SEV_FATAL:
217         default:
218                 level = KERN_ERR;
219                 sevstr = "Fatal";
220                 break;
221         }
222
223         printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
224                evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
225                "Recovered" : "[Not recovered");
226         printk("%s  Initiator: %s\n", level,
227                evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
228         switch(evt.error_type) {
229         case OpalMCE_ERROR_TYPE_UE:
230                 subtype = evt.u.ue_error.ue_error_type <
231                         ARRAY_SIZE(opal_mc_ue_types) ?
232                         opal_mc_ue_types[evt.u.ue_error.ue_error_type]
233                         : "Unknown";
234                 printk("%s  Error type: UE [%s]\n", level, subtype);
235                 if (evt.u.ue_error.effective_address_provided)
236                         printk("%s    Effective address: %016llx\n",
237                                level, evt.u.ue_error.effective_address);
238                 if (evt.u.ue_error.physical_address_provided)
239                         printk("%s      Physial address: %016llx\n",
240                                level, evt.u.ue_error.physical_address);
241                 break;
242         case OpalMCE_ERROR_TYPE_SLB:
243                 subtype = evt.u.slb_error.slb_error_type <
244                         ARRAY_SIZE(opal_mc_slb_types) ?
245                         opal_mc_slb_types[evt.u.slb_error.slb_error_type]
246                         : "Unknown";
247                 printk("%s  Error type: SLB [%s]\n", level, subtype);
248                 if (evt.u.slb_error.effective_address_provided)
249                         printk("%s    Effective address: %016llx\n",
250                                level, evt.u.slb_error.effective_address);
251                 break;
252         case OpalMCE_ERROR_TYPE_ERAT:
253                 subtype = evt.u.erat_error.erat_error_type <
254                         ARRAY_SIZE(opal_mc_erat_types) ?
255                         opal_mc_erat_types[evt.u.erat_error.erat_error_type]
256                         : "Unknown";
257                 printk("%s  Error type: ERAT [%s]\n", level, subtype);
258                 if (evt.u.erat_error.effective_address_provided)
259                         printk("%s    Effective address: %016llx\n",
260                                level, evt.u.erat_error.effective_address);
261                 break;
262         case OpalMCE_ERROR_TYPE_TLB:
263                 subtype = evt.u.tlb_error.tlb_error_type <
264                         ARRAY_SIZE(opal_mc_tlb_types) ?
265                         opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
266                         : "Unknown";
267                 printk("%s  Error type: TLB [%s]\n", level, subtype);
268                 if (evt.u.tlb_error.effective_address_provided)
269                         printk("%s    Effective address: %016llx\n",
270                                level, evt.u.tlb_error.effective_address);
271                 break;
272         default:
273         case OpalMCE_ERROR_TYPE_UNKNOWN:
274                 printk("%s  Error type: Unknown\n", level);
275                 break;
276         }
277         return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
278 }
279
280 static irqreturn_t opal_interrupt(int irq, void *data)
281 {
282         uint64_t events;
283
284         opal_handle_interrupt(virq_to_hw(irq), &events);
285
286         /* XXX TODO: Do something with the events */
287
288         return IRQ_HANDLED;
289 }
290
291 static int __init opal_init(void)
292 {
293         struct device_node *np, *consoles;
294         const u32 *irqs;
295         int rc, i, irqlen;
296
297         opal_node = of_find_node_by_path("/ibm,opal");
298         if (!opal_node) {
299                 pr_warn("opal: Node not found\n");
300                 return -ENODEV;
301         }
302         if (firmware_has_feature(FW_FEATURE_OPALv2))
303                 consoles = of_find_node_by_path("/ibm,opal/consoles");
304         else
305                 consoles = of_node_get(opal_node);
306
307         /* Register serial ports */
308         for_each_child_of_node(consoles, np) {
309                 if (strcmp(np->name, "serial"))
310                         continue;
311                 of_platform_device_create(np, NULL, NULL);
312         }
313         of_node_put(consoles);
314
315         /* Find all OPAL interrupts and request them */
316         irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
317         pr_debug("opal: Found %d interrupts reserved for OPAL\n",
318                  irqs ? (irqlen / 4) : 0);
319         for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
320                 unsigned int hwirq = be32_to_cpup(irqs);
321                 unsigned int irq = irq_create_mapping(NULL, hwirq);
322                 if (irq == NO_IRQ) {
323                         pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
324                         continue;
325                 }
326                 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
327                 if (rc)
328                         pr_warning("opal: Error %d requesting irq %d"
329                                    " (0x%x)\n", rc, irq, hwirq);
330         }
331         return 0;
332 }
333 subsys_initcall(opal_init);