blob: cf85ba86bf5eb8c727e2de88b56cdeabc64086d1 [file] [log] [blame]
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +00001/*
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>
Rob Herring26a20562013-09-26 07:40:04 -050016#include <linux/of_fdt.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000017#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000018#include <linux/interrupt.h>
Gavin Shan1bc98de2013-06-20 18:13:22 +080019#include <linux/notifier.h>
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100020#include <linux/slab.h>
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +053021#include <linux/sched.h>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053022#include <linux/kobject.h>
Vasant Hegdef7d98d12014-01-15 17:02:04 +110023#include <linux/delay.h>
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053024#include <linux/memblock.h>
Michael Ellermanb14726c2014-07-15 22:22:24 +100025
26#include <asm/machdep.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000027#include <asm/opal.h>
28#include <asm/firmware.h>
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +053029#include <asm/mce.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000030
31#include "powernv.h"
32
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053033/* /sys/firmware/opal */
34struct kobject *opal_kobj;
35
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000036struct opal {
37 u64 base;
38 u64 entry;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053039 u64 size;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000040} opal;
41
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053042struct mcheck_recoverable_range {
43 u64 start_addr;
44 u64 end_addr;
45 u64 recover_addr;
46};
47
48static struct mcheck_recoverable_range *mc_recoverable_range;
49static int mc_recoverable_range_len;
50
Joel Stanleybfc36892014-04-01 14:28:19 +103051struct device_node *opal_node;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000052static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000053extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100054static unsigned int *opal_irqs;
55static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080056static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +053057static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
Gavin Shan1bc98de2013-06-20 18:13:22 +080058static DEFINE_SPINLOCK(opal_notifier_lock);
59static uint64_t last_notified_mask = 0x0ul;
60static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000061
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +100062static void opal_reinit_cores(void)
63{
64 /* Do the actual re-init, This will clobber all FPRs, VRs, etc...
65 *
66 * It will preserve non volatile GPRs and HSPRG0/1. It will
67 * also restore HIDs and other SPRs to their original value
68 * but it might clobber a bunch.
69 */
70#ifdef __BIG_ENDIAN__
71 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
72#else
73 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_LE);
74#endif
75}
76
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000077int __init early_init_dt_scan_opal(unsigned long node,
78 const char *uname, int depth, void *data)
79{
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053080 const void *basep, *entryp, *sizep;
Rob Herring9d0c4df2014-04-01 23:49:03 -050081 int basesz, entrysz, runtimesz;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000082
83 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
84 return 0;
85
86 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
87 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053088 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000089
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053090 if (!basep || !entryp || !sizep)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000091 return 1;
92
93 opal.base = of_read_number(basep, basesz/4);
94 opal.entry = of_read_number(entryp, entrysz/4);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053095 opal.size = of_read_number(sizep, runtimesz/4);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000096
Rob Herring9d0c4df2014-04-01 23:49:03 -050097 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000098 opal.base, basep, basesz);
Rob Herring9d0c4df2014-04-01 23:49:03 -050099 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000100 opal.entry, entryp, entrysz);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500101 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530102 opal.size, sizep, runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000103
104 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000105 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
106 powerpc_firmware_features |= FW_FEATURE_OPALv2;
107 powerpc_firmware_features |= FW_FEATURE_OPALv3;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000108 pr_info("OPAL V3 detected !\n");
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000109 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000110 powerpc_firmware_features |= FW_FEATURE_OPALv2;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000111 pr_info("OPAL V2 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000112 } else {
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000113 pr_info("OPAL V1 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000114 }
115
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +1000116 /* Reinit all cores with the right endian */
117 opal_reinit_cores();
118
119 /* Restore some bits */
120 if (cur_cpu_spec->cpu_restore)
121 cur_cpu_spec->cpu_restore();
122
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000123 return 1;
124}
125
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530126int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
127 const char *uname, int depth, void *data)
128{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500129 int i, psize, size;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530130 const __be32 *prop;
131
132 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
133 return 0;
134
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530135 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530136
137 if (!prop)
138 return 1;
139
140 pr_debug("Found machine check recoverable ranges.\n");
141
142 /*
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530143 * Calculate number of available entries.
144 *
145 * Each recoverable address range entry is (start address, len,
146 * recovery address), 2 cells each for start and recovery address,
147 * 1 cell for len, totalling 5 cells per entry.
148 */
149 mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
150
151 /* Sanity check */
152 if (!mc_recoverable_range_len)
153 return 1;
154
155 /* Size required to hold all the entries. */
156 size = mc_recoverable_range_len *
157 sizeof(struct mcheck_recoverable_range);
158
159 /*
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530160 * Allocate a buffer to hold the MC recoverable ranges. We would be
161 * accessing them in real mode, hence it needs to be within
162 * RMO region.
163 */
164 mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
165 ppc64_rma_size));
166 memset(mc_recoverable_range, 0, size);
167
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530168 for (i = 0; i < mc_recoverable_range_len; i++) {
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530169 mc_recoverable_range[i].start_addr =
170 of_read_number(prop + (i * 5) + 0, 2);
171 mc_recoverable_range[i].end_addr =
172 mc_recoverable_range[i].start_addr +
173 of_read_number(prop + (i * 5) + 2, 1);
174 mc_recoverable_range[i].recover_addr =
175 of_read_number(prop + (i * 5) + 3, 2);
176
177 pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
178 mc_recoverable_range[i].start_addr,
179 mc_recoverable_range[i].end_addr,
180 mc_recoverable_range[i].recover_addr);
181 }
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530182 return 1;
183}
184
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000185static int __init opal_register_exception_handlers(void)
186{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000187#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000188 u64 glue;
189
190 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
191 return -ENODEV;
192
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +0530193 /* Hookup some exception handlers except machine check. We use the
194 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000195 */
196 glue = 0x7000;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000197 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000198#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000199
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000200 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000201}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000202machine_early_initcall(powernv, opal_register_exception_handlers);
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000203
Gavin Shan1bc98de2013-06-20 18:13:22 +0800204int opal_notifier_register(struct notifier_block *nb)
205{
206 if (!nb) {
207 pr_warning("%s: Invalid argument (%p)\n",
208 __func__, nb);
209 return -EINVAL;
210 }
211
212 atomic_notifier_chain_register(&opal_notifier_head, nb);
213 return 0;
214}
Benjamin Herrenschmidt798af002014-03-28 13:36:31 +1100215EXPORT_SYMBOL_GPL(opal_notifier_register);
216
217int opal_notifier_unregister(struct notifier_block *nb)
218{
219 if (!nb) {
220 pr_warning("%s: Invalid argument (%p)\n",
221 __func__, nb);
222 return -EINVAL;
223 }
224
225 atomic_notifier_chain_unregister(&opal_notifier_head, nb);
226 return 0;
227}
228EXPORT_SYMBOL_GPL(opal_notifier_unregister);
Gavin Shan1bc98de2013-06-20 18:13:22 +0800229
230static void opal_do_notifier(uint64_t events)
231{
232 unsigned long flags;
233 uint64_t changed_mask;
234
235 if (atomic_read(&opal_notifier_hold))
236 return;
237
238 spin_lock_irqsave(&opal_notifier_lock, flags);
239 changed_mask = last_notified_mask ^ events;
240 last_notified_mask = events;
241 spin_unlock_irqrestore(&opal_notifier_lock, flags);
242
243 /*
244 * We feed with the event bits and changed bits for
245 * enough information to the callback.
246 */
247 atomic_notifier_call_chain(&opal_notifier_head,
248 events, (void *)changed_mask);
249}
250
251void opal_notifier_update_evt(uint64_t evt_mask,
252 uint64_t evt_val)
253{
254 unsigned long flags;
255
256 spin_lock_irqsave(&opal_notifier_lock, flags);
257 last_notified_mask &= ~evt_mask;
258 last_notified_mask |= evt_val;
259 spin_unlock_irqrestore(&opal_notifier_lock, flags);
260}
261
262void opal_notifier_enable(void)
263{
264 int64_t rc;
Anton Blanchard56b4c992014-04-22 15:01:24 +1000265 __be64 evt = 0;
Gavin Shan1bc98de2013-06-20 18:13:22 +0800266
267 atomic_set(&opal_notifier_hold, 0);
268
269 /* Process pending events */
270 rc = opal_poll_events(&evt);
271 if (rc == OPAL_SUCCESS && evt)
Anton Blanchard56b4c992014-04-22 15:01:24 +1000272 opal_do_notifier(be64_to_cpu(evt));
Gavin Shan1bc98de2013-06-20 18:13:22 +0800273}
274
275void opal_notifier_disable(void)
276{
277 atomic_set(&opal_notifier_hold, 1);
278}
279
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530280/*
281 * Opal message notifier based on message type. Allow subscribers to get
282 * notified for specific messgae type.
283 */
284int opal_message_notifier_register(enum OpalMessageType msg_type,
285 struct notifier_block *nb)
286{
287 if (!nb) {
288 pr_warning("%s: Invalid argument (%p)\n",
289 __func__, nb);
290 return -EINVAL;
291 }
292 if (msg_type > OPAL_MSG_TYPE_MAX) {
293 pr_warning("%s: Invalid message type argument (%d)\n",
294 __func__, msg_type);
295 return -EINVAL;
296 }
297 return atomic_notifier_chain_register(
298 &opal_msg_notifier_head[msg_type], nb);
299}
300
301static void opal_message_do_notify(uint32_t msg_type, void *msg)
302{
303 /* notify subscribers */
304 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
305 msg_type, msg);
306}
307
308static void opal_handle_message(void)
309{
310 s64 ret;
311 /*
312 * TODO: pre-allocate a message buffer depending on opal-msg-size
313 * value in /proc/device-tree.
314 */
315 static struct opal_msg msg;
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100316 u32 type;
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530317
318 ret = opal_get_msg(__pa(&msg), sizeof(msg));
319 /* No opal message pending. */
320 if (ret == OPAL_RESOURCE)
321 return;
322
323 /* check for errors. */
324 if (ret) {
325 pr_warning("%s: Failed to retrive opal message, err=%lld\n",
326 __func__, ret);
327 return;
328 }
329
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100330 type = be32_to_cpu(msg.msg_type);
331
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530332 /* Sanity check */
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100333 if (type > OPAL_MSG_TYPE_MAX) {
334 pr_warning("%s: Unknown message type: %u\n", __func__, type);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530335 return;
336 }
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100337 opal_message_do_notify(type, (void *)&msg);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530338}
339
340static int opal_message_notify(struct notifier_block *nb,
341 unsigned long events, void *change)
342{
343 if (events & OPAL_EVENT_MSG_PENDING)
344 opal_handle_message();
345 return 0;
346}
347
348static struct notifier_block opal_message_nb = {
349 .notifier_call = opal_message_notify,
350 .next = NULL,
351 .priority = 0,
352};
353
354static int __init opal_message_init(void)
355{
356 int ret, i;
357
358 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
359 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
360
361 ret = opal_notifier_register(&opal_message_nb);
362 if (ret) {
363 pr_err("%s: Can't register OPAL event notifier (%d)\n",
364 __func__, ret);
365 return ret;
366 }
367 return 0;
368}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000369machine_early_initcall(powernv, opal_message_init);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530370
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000371int opal_get_chars(uint32_t vtermno, char *buf, int count)
372{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000373 s64 rc;
374 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000375
376 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000377 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000378 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000379 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000380 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000381 len = cpu_to_be64(count);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500382 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000383 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000384 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000385 return 0;
386}
387
388int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
389{
390 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000391 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000392 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000393 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000394 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000395
396 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000397 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000398
399 /* We want put_chars to be atomic to avoid mangling of hvsi
400 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000401 * -EAGAIN if there isn't enough.
402 *
403 * Unfortunately, opal_console_write_buffer_space() doesn't
404 * appear to work on opal v1, so we just assume there is
405 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000406 */
407 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000408 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000409 rc = opal_console_write_buffer_space(vtermno, &olen);
410 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000411 if (rc || len < total_len) {
412 spin_unlock_irqrestore(&opal_write_lock, flags);
413 /* Closed -> drop characters */
414 if (rc)
415 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000416 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000417 return -EAGAIN;
418 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000419 }
420
421 /* We still try to handle partial completions, though they
422 * should no longer happen.
423 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000424 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000425 while(total_len > 0 && (rc == OPAL_BUSY ||
426 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000427 olen = cpu_to_be64(total_len);
428 rc = opal_console_write(vtermno, &olen, data);
429 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000430
431 /* Closed or other error drop */
432 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
433 rc != OPAL_BUSY_EVENT) {
434 written = total_len;
435 break;
436 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000437 if (rc == OPAL_SUCCESS) {
438 total_len -= len;
439 data += len;
440 written += len;
441 }
442 /* This is a bit nasty but we need that for the console to
443 * flush when there aren't any interrupts. We will clean
444 * things a bit later to limit that to synchronous path
445 * such as the kernel console and xmon/udbg
446 */
447 do
448 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000449 while(rc == OPAL_SUCCESS &&
450 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000451 }
452 spin_unlock_irqrestore(&opal_write_lock, flags);
453 return written;
454}
455
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530456static int opal_recover_mce(struct pt_regs *regs,
457 struct machine_check_event *evt)
458{
459 int recovered = 0;
460 uint64_t ea = get_mce_fault_addr(evt);
461
462 if (!(regs->msr & MSR_RI)) {
463 /* If MSR_RI isn't set, we cannot recover */
464 recovered = 0;
465 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
466 /* Platform corrected itself */
467 recovered = 1;
468 } else if (ea && !is_kernel_addr(ea)) {
469 /*
470 * Faulting address is not in kernel text. We should be fine.
471 * We need to find which process uses this address.
472 * For now, kill the task if we have received exception when
473 * in userspace.
474 *
475 * TODO: Queue up this address for hwpoisioning later.
476 */
477 if (user_mode(regs) && !is_global_init(current)) {
478 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
479 recovered = 1;
480 } else
481 recovered = 0;
482 } else if (user_mode(regs) && !is_global_init(current) &&
483 evt->severity == MCE_SEV_ERROR_SYNC) {
484 /*
485 * If we have received a synchronous error when in userspace
486 * kill the task.
487 */
488 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
489 recovered = 1;
490 }
491 return recovered;
492}
493
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000494int opal_machine_check(struct pt_regs *regs)
495{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530496 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000497
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530498 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
499 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000500
501 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530502 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000503 pr_err("Machine Check Exception, Unknown event version %d !\n",
504 evt.version);
505 return 0;
506 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530507 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000508
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530509 if (opal_recover_mce(regs, &evt))
510 return 1;
511 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000512}
513
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530514/* Early hmi handler called in real mode. */
515int opal_hmi_exception_early(struct pt_regs *regs)
516{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530517 s64 rc;
518
519 /*
520 * call opal hmi handler. Pass paca address as token.
521 * The return value OPAL_SUCCESS is an indication that there is
522 * an HMI event generated waiting to pull by Linux.
523 */
524 rc = opal_handle_hmi();
525 if (rc == OPAL_SUCCESS) {
526 local_paca->hmi_event_available = 1;
527 return 1;
528 }
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530529 return 0;
530}
531
532/* HMI exception handler called in virtual mode during check_irq_replay. */
533int opal_handle_hmi_exception(struct pt_regs *regs)
534{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530535 s64 rc;
536 __be64 evt = 0;
537
538 /*
539 * Check if HMI event is available.
540 * if Yes, then call opal_poll_events to pull opal messages and
541 * process them.
542 */
543 if (!local_paca->hmi_event_available)
544 return 0;
545
546 local_paca->hmi_event_available = 0;
547 rc = opal_poll_events(&evt);
548 if (rc == OPAL_SUCCESS && evt)
549 opal_do_notifier(be64_to_cpu(evt));
550
551 return 1;
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530552}
553
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530554static uint64_t find_recovery_address(uint64_t nip)
555{
556 int i;
557
558 for (i = 0; i < mc_recoverable_range_len; i++)
559 if ((nip >= mc_recoverable_range[i].start_addr) &&
560 (nip < mc_recoverable_range[i].end_addr))
561 return mc_recoverable_range[i].recover_addr;
562 return 0;
563}
564
565bool opal_mce_check_early_recovery(struct pt_regs *regs)
566{
567 uint64_t recover_addr = 0;
568
569 if (!opal.base || !opal.size)
570 goto out;
571
572 if ((regs->nip >= opal.base) &&
573 (regs->nip <= (opal.base + opal.size)))
574 recover_addr = find_recovery_address(regs->nip);
575
576 /*
577 * Setup regs->nip to rfi into fixup address.
578 */
579 if (recover_addr)
580 regs->nip = recover_addr;
581
582out:
583 return !!recover_addr;
584}
585
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000586static irqreturn_t opal_interrupt(int irq, void *data)
587{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000588 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000589
590 opal_handle_interrupt(virq_to_hw(irq), &events);
591
Anton Blanchard56b4c992014-04-22 15:01:24 +1000592 opal_do_notifier(be64_to_cpu(events));
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000593
594 return IRQ_HANDLED;
595}
596
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530597static int opal_sysfs_init(void)
598{
599 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
600 if (!opal_kobj) {
601 pr_warn("kobject_create_and_add opal failed\n");
602 return -ENOMEM;
603 }
604
605 return 0;
606}
607
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530608static void __init opal_dump_region_init(void)
609{
610 void *addr;
611 uint64_t size;
612 int rc;
613
614 /* Register kernel log buffer */
615 addr = log_buf_addr_get();
616 size = log_buf_len_get();
617 rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
618 __pa(addr), size);
619 /* Don't warn if this is just an older OPAL that doesn't
620 * know about that call
621 */
622 if (rc && rc != OPAL_UNSUPPORTED)
623 pr_warn("DUMP: Failed to register kernel log buffer. "
624 "rc = %d\n", rc);
625}
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000626static int __init opal_init(void)
627{
628 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000629 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000630 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000631
632 opal_node = of_find_node_by_path("/ibm,opal");
633 if (!opal_node) {
634 pr_warn("opal: Node not found\n");
635 return -ENODEV;
636 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000637
638 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000639 if (firmware_has_feature(FW_FEATURE_OPALv2))
640 consoles = of_find_node_by_path("/ibm,opal/consoles");
641 else
642 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000643 if (consoles) {
644 for_each_child_of_node(consoles, np) {
645 if (strcmp(np->name, "serial"))
646 continue;
647 of_platform_device_create(np, NULL, NULL);
648 }
649 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000650 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000651
652 /* Find all OPAL interrupts and request them */
653 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
654 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
655 irqs ? (irqlen / 4) : 0);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000656 opal_irq_count = irqlen / 4;
657 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000658 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
659 unsigned int hwirq = be32_to_cpup(irqs);
660 unsigned int irq = irq_create_mapping(NULL, hwirq);
661 if (irq == NO_IRQ) {
662 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
663 continue;
664 }
665 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
666 if (rc)
667 pr_warning("opal: Error %d requesting irq %d"
668 " (0x%x)\n", rc, irq, hwirq);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000669 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000670 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530671
672 /* Create "opal" kobject under /sys/firmware */
673 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530674 if (rc == 0) {
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530675 /* Setup dump region interface */
676 opal_dump_region_init();
Stewart Smith774fea12014-02-28 11:58:32 +1100677 /* Setup error log interface */
678 rc = opal_elog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530679 /* Setup code update interface */
680 opal_flash_init();
Stewart Smithc7e64b92014-03-03 10:25:42 +1100681 /* Setup platform dump extract interface */
682 opal_platform_dump_init();
Neelesh Gupta4029cd62014-03-07 11:02:09 +0530683 /* Setup system parameters interface */
684 opal_sys_param_init();
Joel Stanleybfc36892014-04-01 14:28:19 +1030685 /* Setup message log interface. */
686 opal_msglog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530687 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530688
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000689 return 0;
690}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000691machine_subsys_initcall(powernv, opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000692
693void opal_shutdown(void)
694{
695 unsigned int i;
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100696 long rc = OPAL_BUSY;
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000697
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100698 /* First free interrupts, which will also mask them */
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000699 for (i = 0; i < opal_irq_count; i++) {
700 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000701 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000702 opal_irqs[i] = 0;
703 }
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100704
705 /*
706 * Then sync with OPAL which ensure anything that can
707 * potentially write to our memory has completed such
708 * as an ongoing dump retrieval
709 */
710 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
711 rc = opal_sync_host_reboot();
712 if (rc == OPAL_BUSY)
713 opal_poll_events(NULL);
714 else
715 mdelay(10);
716 }
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530717
718 /* Unregister memory dump region */
719 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000720}
Joel Stanleye28b05e2014-04-01 14:28:20 +1030721
722/* Export this so that test modules can use it */
723EXPORT_SYMBOL_GPL(opal_invalid_call);
Anton Blanchard3441f042014-04-22 15:01:26 +1000724
725/* Convert a region of vmalloc memory to an opal sg list */
726struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
727 unsigned long vmalloc_size)
728{
729 struct opal_sg_list *sg, *first = NULL;
730 unsigned long i = 0;
731
732 sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
733 if (!sg)
734 goto nomem;
735
736 first = sg;
737
738 while (vmalloc_size > 0) {
739 uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
740 uint64_t length = min(vmalloc_size, PAGE_SIZE);
741
742 sg->entry[i].data = cpu_to_be64(data);
743 sg->entry[i].length = cpu_to_be64(length);
744 i++;
745
746 if (i >= SG_ENTRIES_PER_NODE) {
747 struct opal_sg_list *next;
748
749 next = kzalloc(PAGE_SIZE, GFP_KERNEL);
750 if (!next)
751 goto nomem;
752
753 sg->length = cpu_to_be64(
754 i * sizeof(struct opal_sg_entry) + 16);
755 i = 0;
756 sg->next = cpu_to_be64(__pa(next));
757 sg = next;
758 }
759
760 vmalloc_addr += length;
761 vmalloc_size -= length;
762 }
763
764 sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
765
766 return first;
767
768nomem:
769 pr_err("%s : Failed to allocate memory\n", __func__);
770 opal_free_sg_list(first);
771 return NULL;
772}
773
774void opal_free_sg_list(struct opal_sg_list *sg)
775{
776 while (sg) {
777 uint64_t next = be64_to_cpu(sg->next);
778
779 kfree(sg);
780
781 if (next)
782 sg = __va(next);
783 else
784 sg = NULL;
785 }
786}