1
2
3
4
5
6
7
8
9
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 <asm/opal.h>
25#include <asm/firmware.h>
26#include <asm/mce.h>
27
28#include "powernv.h"
29
30
31struct kobject *opal_kobj;
32
33struct opal {
34 u64 base;
35 u64 entry;
36} opal;
37
38static struct device_node *opal_node;
39static DEFINE_SPINLOCK(opal_write_lock);
40extern u64 opal_mc_secondary_handler[];
41static unsigned int *opal_irqs;
42static unsigned int opal_irq_count;
43static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
44static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
45static DEFINE_SPINLOCK(opal_notifier_lock);
46static uint64_t last_notified_mask = 0x0ul;
47static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
48
49int __init early_init_dt_scan_opal(unsigned long node,
50 const char *uname, int depth, void *data)
51{
52 const void *basep, *entryp;
53 unsigned long basesz, entrysz;
54
55 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
56 return 0;
57
58 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
59 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
60
61 if (!basep || !entryp)
62 return 1;
63
64 opal.base = of_read_number(basep, basesz/4);
65 opal.entry = of_read_number(entryp, entrysz/4);
66
67 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
68 opal.base, basep, basesz);
69 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
70 opal.entry, entryp, entrysz);
71
72 powerpc_firmware_features |= FW_FEATURE_OPAL;
73 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
74 powerpc_firmware_features |= FW_FEATURE_OPALv2;
75 powerpc_firmware_features |= FW_FEATURE_OPALv3;
76 printk("OPAL V3 detected !\n");
77 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
78 powerpc_firmware_features |= FW_FEATURE_OPALv2;
79 printk("OPAL V2 detected !\n");
80 } else {
81 printk("OPAL V1 detected !\n");
82 }
83
84 return 1;
85}
86
87static int __init opal_register_exception_handlers(void)
88{
89#ifdef __BIG_ENDIAN__
90 u64 glue;
91
92 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
93 return -ENODEV;
94
95
96
97
98 glue = 0x7000;
99 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
100 0, glue);
101 glue += 128;
102 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
103#endif
104
105 return 0;
106}
107
108early_initcall(opal_register_exception_handlers);
109
110int opal_notifier_register(struct notifier_block *nb)
111{
112 if (!nb) {
113 pr_warning("%s: Invalid argument (%p)\n",
114 __func__, nb);
115 return -EINVAL;
116 }
117
118 atomic_notifier_chain_register(&opal_notifier_head, nb);
119 return 0;
120}
121
122static void opal_do_notifier(uint64_t events)
123{
124 unsigned long flags;
125 uint64_t changed_mask;
126
127 if (atomic_read(&opal_notifier_hold))
128 return;
129
130 spin_lock_irqsave(&opal_notifier_lock, flags);
131 changed_mask = last_notified_mask ^ events;
132 last_notified_mask = events;
133 spin_unlock_irqrestore(&opal_notifier_lock, flags);
134
135
136
137
138
139 atomic_notifier_call_chain(&opal_notifier_head,
140 events, (void *)changed_mask);
141}
142
143void opal_notifier_update_evt(uint64_t evt_mask,
144 uint64_t evt_val)
145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&opal_notifier_lock, flags);
149 last_notified_mask &= ~evt_mask;
150 last_notified_mask |= evt_val;
151 spin_unlock_irqrestore(&opal_notifier_lock, flags);
152}
153
154void opal_notifier_enable(void)
155{
156 int64_t rc;
157 uint64_t evt = 0;
158
159 atomic_set(&opal_notifier_hold, 0);
160
161
162 rc = opal_poll_events(&evt);
163 if (rc == OPAL_SUCCESS && evt)
164 opal_do_notifier(evt);
165}
166
167void opal_notifier_disable(void)
168{
169 atomic_set(&opal_notifier_hold, 1);
170}
171
172
173
174
175
176int opal_message_notifier_register(enum OpalMessageType msg_type,
177 struct notifier_block *nb)
178{
179 if (!nb) {
180 pr_warning("%s: Invalid argument (%p)\n",
181 __func__, nb);
182 return -EINVAL;
183 }
184 if (msg_type > OPAL_MSG_TYPE_MAX) {
185 pr_warning("%s: Invalid message type argument (%d)\n",
186 __func__, msg_type);
187 return -EINVAL;
188 }
189 return atomic_notifier_chain_register(
190 &opal_msg_notifier_head[msg_type], nb);
191}
192
193static void opal_message_do_notify(uint32_t msg_type, void *msg)
194{
195
196 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
197 msg_type, msg);
198}
199
200static void opal_handle_message(void)
201{
202 s64 ret;
203
204
205
206
207 static struct opal_msg msg;
208
209 ret = opal_get_msg(__pa(&msg), sizeof(msg));
210
211 if (ret == OPAL_RESOURCE)
212 return;
213
214
215 if (ret) {
216 pr_warning("%s: Failed to retrive opal message, err=%lld\n",
217 __func__, ret);
218 return;
219 }
220
221
222 if (msg.msg_type > OPAL_MSG_TYPE_MAX) {
223 pr_warning("%s: Unknown message type: %u\n",
224 __func__, msg.msg_type);
225 return;
226 }
227 opal_message_do_notify(msg.msg_type, (void *)&msg);
228}
229
230static int opal_message_notify(struct notifier_block *nb,
231 unsigned long events, void *change)
232{
233 if (events & OPAL_EVENT_MSG_PENDING)
234 opal_handle_message();
235 return 0;
236}
237
238static struct notifier_block opal_message_nb = {
239 .notifier_call = opal_message_notify,
240 .next = NULL,
241 .priority = 0,
242};
243
244static int __init opal_message_init(void)
245{
246 int ret, i;
247
248 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
249 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
250
251 ret = opal_notifier_register(&opal_message_nb);
252 if (ret) {
253 pr_err("%s: Can't register OPAL event notifier (%d)\n",
254 __func__, ret);
255 return ret;
256 }
257 return 0;
258}
259early_initcall(opal_message_init);
260
261int opal_get_chars(uint32_t vtermno, char *buf, int count)
262{
263 s64 rc;
264 __be64 evt, len;
265
266 if (!opal.entry)
267 return -ENODEV;
268 opal_poll_events(&evt);
269 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
270 return 0;
271 len = cpu_to_be64(count);
272 rc = opal_console_read(vtermno, &len, buf);
273 if (rc == OPAL_SUCCESS)
274 return be64_to_cpu(len);
275 return 0;
276}
277
278int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
279{
280 int written = 0;
281 __be64 olen;
282 s64 len, rc;
283 unsigned long flags;
284 __be64 evt;
285
286 if (!opal.entry)
287 return -ENODEV;
288
289
290
291
292
293
294
295
296
297 spin_lock_irqsave(&opal_write_lock, flags);
298 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
299 rc = opal_console_write_buffer_space(vtermno, &olen);
300 len = be64_to_cpu(olen);
301 if (rc || len < total_len) {
302 spin_unlock_irqrestore(&opal_write_lock, flags);
303
304 if (rc)
305 return total_len;
306 opal_poll_events(NULL);
307 return -EAGAIN;
308 }
309 }
310
311
312
313
314 rc = OPAL_BUSY;
315 while(total_len > 0 && (rc == OPAL_BUSY ||
316 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
317 olen = cpu_to_be64(total_len);
318 rc = opal_console_write(vtermno, &olen, data);
319 len = be64_to_cpu(olen);
320
321
322 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
323 rc != OPAL_BUSY_EVENT) {
324 written = total_len;
325 break;
326 }
327 if (rc == OPAL_SUCCESS) {
328 total_len -= len;
329 data += len;
330 written += len;
331 }
332
333
334
335
336
337 do
338 opal_poll_events(&evt);
339 while(rc == OPAL_SUCCESS &&
340 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
341 }
342 spin_unlock_irqrestore(&opal_write_lock, flags);
343 return written;
344}
345
346static int opal_recover_mce(struct pt_regs *regs,
347 struct machine_check_event *evt)
348{
349 int recovered = 0;
350 uint64_t ea = get_mce_fault_addr(evt);
351
352 if (!(regs->msr & MSR_RI)) {
353
354 recovered = 0;
355 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
356
357 recovered = 1;
358 } else if (ea && !is_kernel_addr(ea)) {
359
360
361
362
363
364
365
366
367 if (user_mode(regs) && !is_global_init(current)) {
368 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
369 recovered = 1;
370 } else
371 recovered = 0;
372 } else if (user_mode(regs) && !is_global_init(current) &&
373 evt->severity == MCE_SEV_ERROR_SYNC) {
374
375
376
377
378 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
379 recovered = 1;
380 }
381 return recovered;
382}
383
384int opal_machine_check(struct pt_regs *regs)
385{
386 struct machine_check_event evt;
387
388 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
389 return 0;
390
391
392 if (evt.version != MCE_V1) {
393 pr_err("Machine Check Exception, Unknown event version %d !\n",
394 evt.version);
395 return 0;
396 }
397 machine_check_print_event_info(&evt);
398
399 if (opal_recover_mce(regs, &evt))
400 return 1;
401 return 0;
402}
403
404static irqreturn_t opal_interrupt(int irq, void *data)
405{
406 __be64 events;
407
408 opal_handle_interrupt(virq_to_hw(irq), &events);
409
410 opal_do_notifier(events);
411
412 return IRQ_HANDLED;
413}
414
415static int opal_sysfs_init(void)
416{
417 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
418 if (!opal_kobj) {
419 pr_warn("kobject_create_and_add opal failed\n");
420 return -ENOMEM;
421 }
422
423 return 0;
424}
425
426static int __init opal_init(void)
427{
428 struct device_node *np, *consoles;
429 const __be32 *irqs;
430 int rc, i, irqlen;
431
432 opal_node = of_find_node_by_path("/ibm,opal");
433 if (!opal_node) {
434 pr_warn("opal: Node not found\n");
435 return -ENODEV;
436 }
437
438
439 if (firmware_has_feature(FW_FEATURE_OPALv2))
440 consoles = of_find_node_by_path("/ibm,opal/consoles");
441 else
442 consoles = of_node_get(opal_node);
443 if (consoles) {
444 for_each_child_of_node(consoles, np) {
445 if (strcmp(np->name, "serial"))
446 continue;
447 of_platform_device_create(np, NULL, NULL);
448 }
449 of_node_put(consoles);
450 }
451
452
453 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
454 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
455 irqs ? (irqlen / 4) : 0);
456 opal_irq_count = irqlen / 4;
457 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
458 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
459 unsigned int hwirq = be32_to_cpup(irqs);
460 unsigned int irq = irq_create_mapping(NULL, hwirq);
461 if (irq == NO_IRQ) {
462 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
463 continue;
464 }
465 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
466 if (rc)
467 pr_warning("opal: Error %d requesting irq %d"
468 " (0x%x)\n", rc, irq, hwirq);
469 opal_irqs[i] = irq;
470 }
471
472
473 rc = opal_sysfs_init();
474 if (rc == 0) {
475
476 opal_flash_init();
477 }
478
479 return 0;
480}
481subsys_initcall(opal_init);
482
483void opal_shutdown(void)
484{
485 unsigned int i;
486 long rc = OPAL_BUSY;
487
488
489 for (i = 0; i < opal_irq_count; i++) {
490 if (opal_irqs[i])
491 free_irq(opal_irqs[i], NULL);
492 opal_irqs[i] = 0;
493 }
494
495
496
497
498
499
500 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
501 rc = opal_sync_host_reboot();
502 if (rc == OPAL_BUSY)
503 opal_poll_events(NULL);
504 else
505 mdelay(10);
506 }
507}
508