blob: cd5718b74d7ccde4bc6657309857e88fc3c6b730 [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
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +110012#define pr_fmt(fmt) "opal: " fmt
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000013
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +110014#include <linux/printk.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000015#include <linux/types.h>
16#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050017#include <linux/of_fdt.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000018#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000019#include <linux/interrupt.h>
Gavin Shan1bc98de2013-06-20 18:13:22 +080020#include <linux/notifier.h>
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100021#include <linux/slab.h>
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +053022#include <linux/sched.h>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053023#include <linux/kobject.h>
Vasant Hegdef7d98d12014-01-15 17:02:04 +110024#include <linux/delay.h>
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053025#include <linux/memblock.h>
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +110026#include <linux/kthread.h>
27#include <linux/freezer.h>
Michael Ellermanb14726c2014-07-15 22:22:24 +100028
29#include <asm/machdep.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000030#include <asm/opal.h>
31#include <asm/firmware.h>
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +053032#include <asm/mce.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000033
34#include "powernv.h"
35
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053036/* /sys/firmware/opal */
37struct kobject *opal_kobj;
38
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000039struct opal {
40 u64 base;
41 u64 entry;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053042 u64 size;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000043} opal;
44
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053045struct mcheck_recoverable_range {
46 u64 start_addr;
47 u64 end_addr;
48 u64 recover_addr;
49};
50
51static struct mcheck_recoverable_range *mc_recoverable_range;
52static int mc_recoverable_range_len;
53
Joel Stanleybfc36892014-04-01 14:28:19 +103054struct device_node *opal_node;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000055static DEFINE_SPINLOCK(opal_write_lock);
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 Herrenschmidt3bf57562014-11-14 16:13:50 +110061static uint32_t opal_heartbeat;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000062
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +100063static void opal_reinit_cores(void)
64{
65 /* Do the actual re-init, This will clobber all FPRs, VRs, etc...
66 *
67 * It will preserve non volatile GPRs and HSPRG0/1. It will
68 * also restore HIDs and other SPRs to their original value
69 * but it might clobber a bunch.
70 */
71#ifdef __BIG_ENDIAN__
72 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
73#else
74 opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_LE);
75#endif
76}
77
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000078int __init early_init_dt_scan_opal(unsigned long node,
79 const char *uname, int depth, void *data)
80{
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053081 const void *basep, *entryp, *sizep;
Rob Herring9d0c4df2014-04-01 23:49:03 -050082 int basesz, entrysz, runtimesz;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000083
84 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
85 return 0;
86
87 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
88 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053089 sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000090
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053091 if (!basep || !entryp || !sizep)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000092 return 1;
93
94 opal.base = of_read_number(basep, basesz/4);
95 opal.entry = of_read_number(entryp, entrysz/4);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +053096 opal.size = of_read_number(sizep, runtimesz/4);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000097
Rob Herring9d0c4df2014-04-01 23:49:03 -050098 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000099 opal.base, basep, basesz);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500100 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000101 opal.entry, entryp, entrysz);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500102 pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530103 opal.size, sizep, runtimesz);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000104
105 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000106 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
107 powerpc_firmware_features |= FW_FEATURE_OPALv2;
108 powerpc_firmware_features |= FW_FEATURE_OPALv3;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000109 pr_info("OPAL V3 detected !\n");
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +1000110 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000111 powerpc_firmware_features |= FW_FEATURE_OPALv2;
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000112 pr_info("OPAL V2 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000113 } else {
Anton Blanchard9a4f5cd2014-09-17 14:39:38 +1000114 pr_info("OPAL V1 detected !\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000115 }
116
Benjamin Herrenschmidt49266162014-05-20 11:01:28 +1000117 /* Reinit all cores with the right endian */
118 opal_reinit_cores();
119
120 /* Restore some bits */
121 if (cur_cpu_spec->cpu_restore)
122 cur_cpu_spec->cpu_restore();
123
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000124 return 1;
125}
126
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530127int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
128 const char *uname, int depth, void *data)
129{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500130 int i, psize, size;
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530131 const __be32 *prop;
132
133 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
134 return 0;
135
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530136 prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530137
138 if (!prop)
139 return 1;
140
141 pr_debug("Found machine check recoverable ranges.\n");
142
143 /*
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530144 * Calculate number of available entries.
145 *
146 * Each recoverable address range entry is (start address, len,
147 * recovery address), 2 cells each for start and recovery address,
148 * 1 cell for len, totalling 5 cells per entry.
149 */
150 mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
151
152 /* Sanity check */
153 if (!mc_recoverable_range_len)
154 return 1;
155
156 /* Size required to hold all the entries. */
157 size = mc_recoverable_range_len *
158 sizeof(struct mcheck_recoverable_range);
159
160 /*
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530161 * Allocate a buffer to hold the MC recoverable ranges. We would be
162 * accessing them in real mode, hence it needs to be within
163 * RMO region.
164 */
165 mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
166 ppc64_rma_size));
167 memset(mc_recoverable_range, 0, size);
168
Mahesh Salgaonkar6e556b42014-03-30 11:03:23 +0530169 for (i = 0; i < mc_recoverable_range_len; i++) {
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530170 mc_recoverable_range[i].start_addr =
171 of_read_number(prop + (i * 5) + 0, 2);
172 mc_recoverable_range[i].end_addr =
173 mc_recoverable_range[i].start_addr +
174 of_read_number(prop + (i * 5) + 2, 1);
175 mc_recoverable_range[i].recover_addr =
176 of_read_number(prop + (i * 5) + 3, 2);
177
178 pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
179 mc_recoverable_range[i].start_addr,
180 mc_recoverable_range[i].end_addr,
181 mc_recoverable_range[i].recover_addr);
182 }
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530183 return 1;
184}
185
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000186static int __init opal_register_exception_handlers(void)
187{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000188#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000189 u64 glue;
190
191 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
192 return -ENODEV;
193
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +0530194 /* Hookup some exception handlers except machine check. We use the
195 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000196 */
197 glue = 0x7000;
Mahesh Salgaonkar6507955c2014-10-10 21:28:26 +0530198
199 /*
200 * Check if we are running on newer firmware that exports
201 * OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to patch
202 * the HMI interrupt and we catch it directly in Linux.
203 *
204 * For older firmware (i.e currently released POWER8 System Firmware
205 * as of today <= SV810_087), we fallback to old behavior and let OPAL
206 * patch the HMI vector and handle it inside OPAL firmware.
207 *
208 * For newer firmware (in development/yet to be released) we will
209 * start catching/handling HMI directly in Linux.
210 */
211 if (!opal_check_token(OPAL_HANDLE_HMI)) {
Michael Ellerman08135132015-01-28 15:10:33 +1100212 pr_info("Old firmware detected, OPAL handles HMIs.\n");
Mahesh Salgaonkar6507955c2014-10-10 21:28:26 +0530213 opal_register_exception_handler(
214 OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
215 0, glue);
216 glue += 128;
217 }
218
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000219 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000220#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000221
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000222 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000223}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000224machine_early_initcall(powernv, opal_register_exception_handlers);
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000225
Gavin Shan1bc98de2013-06-20 18:13:22 +0800226int opal_notifier_register(struct notifier_block *nb)
227{
228 if (!nb) {
229 pr_warning("%s: Invalid argument (%p)\n",
230 __func__, nb);
231 return -EINVAL;
232 }
233
234 atomic_notifier_chain_register(&opal_notifier_head, nb);
235 return 0;
236}
Benjamin Herrenschmidt798af002014-03-28 13:36:31 +1100237EXPORT_SYMBOL_GPL(opal_notifier_register);
238
239int opal_notifier_unregister(struct notifier_block *nb)
240{
241 if (!nb) {
242 pr_warning("%s: Invalid argument (%p)\n",
243 __func__, nb);
244 return -EINVAL;
245 }
246
247 atomic_notifier_chain_unregister(&opal_notifier_head, nb);
248 return 0;
249}
250EXPORT_SYMBOL_GPL(opal_notifier_unregister);
Gavin Shan1bc98de2013-06-20 18:13:22 +0800251
Alistair Popple9f0fd042015-05-15 14:06:37 +1000252void opal_do_notifier(uint64_t events)
Gavin Shan1bc98de2013-06-20 18:13:22 +0800253{
254 unsigned long flags;
255 uint64_t changed_mask;
256
257 if (atomic_read(&opal_notifier_hold))
258 return;
259
260 spin_lock_irqsave(&opal_notifier_lock, flags);
261 changed_mask = last_notified_mask ^ events;
262 last_notified_mask = events;
263 spin_unlock_irqrestore(&opal_notifier_lock, flags);
264
265 /*
266 * We feed with the event bits and changed bits for
267 * enough information to the callback.
268 */
269 atomic_notifier_call_chain(&opal_notifier_head,
270 events, (void *)changed_mask);
271}
272
273void opal_notifier_update_evt(uint64_t evt_mask,
274 uint64_t evt_val)
275{
276 unsigned long flags;
277
278 spin_lock_irqsave(&opal_notifier_lock, flags);
279 last_notified_mask &= ~evt_mask;
280 last_notified_mask |= evt_val;
281 spin_unlock_irqrestore(&opal_notifier_lock, flags);
282}
283
284void opal_notifier_enable(void)
285{
286 int64_t rc;
Anton Blanchard56b4c992014-04-22 15:01:24 +1000287 __be64 evt = 0;
Gavin Shan1bc98de2013-06-20 18:13:22 +0800288
289 atomic_set(&opal_notifier_hold, 0);
290
291 /* Process pending events */
292 rc = opal_poll_events(&evt);
293 if (rc == OPAL_SUCCESS && evt)
Anton Blanchard56b4c992014-04-22 15:01:24 +1000294 opal_do_notifier(be64_to_cpu(evt));
Gavin Shan1bc98de2013-06-20 18:13:22 +0800295}
296
297void opal_notifier_disable(void)
298{
299 atomic_set(&opal_notifier_hold, 1);
300}
301
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530302/*
303 * Opal message notifier based on message type. Allow subscribers to get
304 * notified for specific messgae type.
305 */
Michael Ellermand7cf83f2015-02-17 20:01:54 +1100306int opal_message_notifier_register(enum opal_msg_type msg_type,
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530307 struct notifier_block *nb)
308{
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530309 if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
310 pr_warning("%s: Invalid arguments, msg_type:%d\n",
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530311 __func__, msg_type);
312 return -EINVAL;
313 }
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530314
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530315 return atomic_notifier_chain_register(
316 &opal_msg_notifier_head[msg_type], nb);
317}
318
Michael Ellermandf60f572015-03-26 20:03:16 +1100319int opal_message_notifier_unregister(enum opal_msg_type msg_type,
Neelesh Guptab921e902015-02-11 11:57:23 +0530320 struct notifier_block *nb)
321{
322 return atomic_notifier_chain_unregister(
323 &opal_msg_notifier_head[msg_type], nb);
324}
325
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530326static void opal_message_do_notify(uint32_t msg_type, void *msg)
327{
328 /* notify subscribers */
329 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
330 msg_type, msg);
331}
332
333static void opal_handle_message(void)
334{
335 s64 ret;
336 /*
337 * TODO: pre-allocate a message buffer depending on opal-msg-size
338 * value in /proc/device-tree.
339 */
340 static struct opal_msg msg;
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100341 u32 type;
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530342
343 ret = opal_get_msg(__pa(&msg), sizeof(msg));
344 /* No opal message pending. */
345 if (ret == OPAL_RESOURCE)
346 return;
347
348 /* check for errors. */
349 if (ret) {
Masanari Iida1a84db52014-08-29 23:37:33 +0900350 pr_warning("%s: Failed to retrieve opal message, err=%lld\n",
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530351 __func__, ret);
352 return;
353 }
354
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100355 type = be32_to_cpu(msg.msg_type);
356
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530357 /* Sanity check */
Neelesh Gupta792f96e2015-02-11 11:57:06 +0530358 if (type >= OPAL_MSG_TYPE_MAX) {
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100359 pr_warning("%s: Unknown message type: %u\n", __func__, type);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530360 return;
361 }
Anton Blanchardbb4398e2014-03-28 16:33:33 +1100362 opal_message_do_notify(type, (void *)&msg);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530363}
364
Alistair Popplea295af22015-05-15 14:06:41 +1000365static irqreturn_t opal_message_notify(int irq, void *data)
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530366{
Alistair Popplea295af22015-05-15 14:06:41 +1000367 opal_handle_message();
368 return IRQ_HANDLED;
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530369}
370
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530371static int __init opal_message_init(void)
372{
Alistair Popplea295af22015-05-15 14:06:41 +1000373 int ret, i, irq;
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530374
375 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
376 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
377
Alistair Popplea295af22015-05-15 14:06:41 +1000378 irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING));
379 if (!irq) {
380 pr_err("%s: Can't register OPAL event irq (%d)\n",
381 __func__, irq);
382 return irq;
383 }
384
385 ret = request_irq(irq, opal_message_notify,
386 IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530387 if (ret) {
Alistair Popplea295af22015-05-15 14:06:41 +1000388 pr_err("%s: Can't request OPAL event irq (%d)\n",
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530389 __func__, ret);
390 return ret;
391 }
Alistair Popplea295af22015-05-15 14:06:41 +1000392
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530393 return 0;
394}
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530395
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000396int opal_get_chars(uint32_t vtermno, char *buf, int count)
397{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000398 s64 rc;
399 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000400
401 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000402 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000403 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000404 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000405 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000406 len = cpu_to_be64(count);
Rob Herring9d0c4df2014-04-01 23:49:03 -0500407 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000408 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000409 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000410 return 0;
411}
412
413int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
414{
415 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000416 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000417 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000418 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000419 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000420
421 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000422 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000423
424 /* We want put_chars to be atomic to avoid mangling of hvsi
425 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000426 * -EAGAIN if there isn't enough.
427 *
428 * Unfortunately, opal_console_write_buffer_space() doesn't
429 * appear to work on opal v1, so we just assume there is
430 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000431 */
432 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000433 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000434 rc = opal_console_write_buffer_space(vtermno, &olen);
435 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000436 if (rc || len < total_len) {
437 spin_unlock_irqrestore(&opal_write_lock, flags);
438 /* Closed -> drop characters */
439 if (rc)
440 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000441 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000442 return -EAGAIN;
443 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000444 }
445
446 /* We still try to handle partial completions, though they
447 * should no longer happen.
448 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000449 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000450 while(total_len > 0 && (rc == OPAL_BUSY ||
451 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000452 olen = cpu_to_be64(total_len);
453 rc = opal_console_write(vtermno, &olen, data);
454 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000455
456 /* Closed or other error drop */
457 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
458 rc != OPAL_BUSY_EVENT) {
459 written = total_len;
460 break;
461 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000462 if (rc == OPAL_SUCCESS) {
463 total_len -= len;
464 data += len;
465 written += len;
466 }
467 /* This is a bit nasty but we need that for the console to
468 * flush when there aren't any interrupts. We will clean
469 * things a bit later to limit that to synchronous path
470 * such as the kernel console and xmon/udbg
471 */
472 do
473 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000474 while(rc == OPAL_SUCCESS &&
475 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000476 }
477 spin_unlock_irqrestore(&opal_write_lock, flags);
478 return written;
479}
480
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530481static int opal_recover_mce(struct pt_regs *regs,
482 struct machine_check_event *evt)
483{
484 int recovered = 0;
485 uint64_t ea = get_mce_fault_addr(evt);
486
487 if (!(regs->msr & MSR_RI)) {
488 /* If MSR_RI isn't set, we cannot recover */
489 recovered = 0;
490 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
491 /* Platform corrected itself */
492 recovered = 1;
493 } else if (ea && !is_kernel_addr(ea)) {
494 /*
495 * Faulting address is not in kernel text. We should be fine.
496 * We need to find which process uses this address.
497 * For now, kill the task if we have received exception when
498 * in userspace.
499 *
500 * TODO: Queue up this address for hwpoisioning later.
501 */
502 if (user_mode(regs) && !is_global_init(current)) {
503 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
504 recovered = 1;
505 } else
506 recovered = 0;
507 } else if (user_mode(regs) && !is_global_init(current) &&
508 evt->severity == MCE_SEV_ERROR_SYNC) {
509 /*
510 * If we have received a synchronous error when in userspace
511 * kill the task.
512 */
513 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
514 recovered = 1;
515 }
516 return recovered;
517}
518
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000519int opal_machine_check(struct pt_regs *regs)
520{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530521 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000522
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530523 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
524 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000525
526 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530527 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000528 pr_err("Machine Check Exception, Unknown event version %d !\n",
529 evt.version);
530 return 0;
531 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530532 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000533
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530534 if (opal_recover_mce(regs, &evt))
535 return 1;
536 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000537}
538
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530539/* Early hmi handler called in real mode. */
540int opal_hmi_exception_early(struct pt_regs *regs)
541{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530542 s64 rc;
543
544 /*
545 * call opal hmi handler. Pass paca address as token.
546 * The return value OPAL_SUCCESS is an indication that there is
547 * an HMI event generated waiting to pull by Linux.
548 */
549 rc = opal_handle_hmi();
550 if (rc == OPAL_SUCCESS) {
551 local_paca->hmi_event_available = 1;
552 return 1;
553 }
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530554 return 0;
555}
556
557/* HMI exception handler called in virtual mode during check_irq_replay. */
558int opal_handle_hmi_exception(struct pt_regs *regs)
559{
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530560 s64 rc;
561 __be64 evt = 0;
562
563 /*
564 * Check if HMI event is available.
565 * if Yes, then call opal_poll_events to pull opal messages and
566 * process them.
567 */
568 if (!local_paca->hmi_event_available)
569 return 0;
570
571 local_paca->hmi_event_available = 0;
572 rc = opal_poll_events(&evt);
Alistair Popple9f0fd042015-05-15 14:06:37 +1000573 if (rc == OPAL_SUCCESS && evt) {
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530574 opal_do_notifier(be64_to_cpu(evt));
Alistair Popple9f0fd042015-05-15 14:06:37 +1000575 opal_handle_events(be64_to_cpu(evt));
576 }
Mahesh Salgaonkar0ef95b42014-07-29 18:40:07 +0530577
578 return 1;
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530579}
580
Mahesh Salgaonkar55672ec2013-12-16 10:46:24 +0530581static uint64_t find_recovery_address(uint64_t nip)
582{
583 int i;
584
585 for (i = 0; i < mc_recoverable_range_len; i++)
586 if ((nip >= mc_recoverable_range[i].start_addr) &&
587 (nip < mc_recoverable_range[i].end_addr))
588 return mc_recoverable_range[i].recover_addr;
589 return 0;
590}
591
592bool opal_mce_check_early_recovery(struct pt_regs *regs)
593{
594 uint64_t recover_addr = 0;
595
596 if (!opal.base || !opal.size)
597 goto out;
598
599 if ((regs->nip >= opal.base) &&
600 (regs->nip <= (opal.base + opal.size)))
601 recover_addr = find_recovery_address(regs->nip);
602
603 /*
604 * Setup regs->nip to rfi into fixup address.
605 */
606 if (recover_addr)
607 regs->nip = recover_addr;
608
609out:
610 return !!recover_addr;
611}
612
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530613static int opal_sysfs_init(void)
614{
615 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
616 if (!opal_kobj) {
617 pr_warn("kobject_create_and_add opal failed\n");
618 return -ENOMEM;
619 }
620
621 return 0;
622}
623
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +1100624static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
625 struct bin_attribute *bin_attr,
626 char *buf, loff_t off, size_t count)
627{
628 return memory_read_from_buffer(buf, count, &off, bin_attr->private,
629 bin_attr->size);
630}
631
632static BIN_ATTR_RO(symbol_map, 0);
633
634static void opal_export_symmap(void)
635{
636 const __be64 *syms;
637 unsigned int size;
638 struct device_node *fw;
639 int rc;
640
641 fw = of_find_node_by_path("/ibm,opal/firmware");
642 if (!fw)
643 return;
644 syms = of_get_property(fw, "symbol-map", &size);
645 if (!syms || size != 2 * sizeof(__be64))
646 return;
647
648 /* Setup attributes */
649 bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0]));
650 bin_attr_symbol_map.size = be64_to_cpu(syms[1]);
651
652 rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map);
653 if (rc)
654 pr_warn("Error %d creating OPAL symbols file\n", rc);
655}
656
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530657static void __init opal_dump_region_init(void)
658{
659 void *addr;
660 uint64_t size;
661 int rc;
662
Stewart Smithb962f5a2015-02-12 16:25:27 +1100663 if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
664 return;
665
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530666 /* Register kernel log buffer */
667 addr = log_buf_addr_get();
Pranith Kumar6501ab5e32015-01-21 21:26:09 -0500668 if (addr == NULL)
669 return;
670
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530671 size = log_buf_len_get();
Pranith Kumar6501ab5e32015-01-21 21:26:09 -0500672 if (size == 0)
673 return;
674
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530675 rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
676 __pa(addr), size);
677 /* Don't warn if this is just an older OPAL that doesn't
678 * know about that call
679 */
680 if (rc && rc != OPAL_UNSUPPORTED)
681 pr_warn("DUMP: Failed to register kernel log buffer. "
682 "rc = %d\n", rc);
683}
Jeremy Kerr608b2862014-11-06 11:38:27 +0800684
Cyril Bured591902015-04-01 14:05:30 +0800685static void opal_flash_init(struct device_node *opal_node)
686{
687 struct device_node *np;
688
689 for_each_child_of_node(opal_node, np)
690 if (of_device_is_compatible(np, "ibm,opal-flash"))
691 of_platform_device_create(np, NULL, NULL);
692}
693
Jeremy Kerr608b2862014-11-06 11:38:27 +0800694static void opal_ipmi_init(struct device_node *opal_node)
695{
696 struct device_node *np;
697
698 for_each_child_of_node(opal_node, np)
699 if (of_device_is_compatible(np, "ibm,opal-ipmi"))
700 of_platform_device_create(np, NULL, NULL);
701}
702
Neelesh Gupta47083452014-12-13 23:31:05 +0530703static void opal_i2c_create_devs(void)
704{
705 struct device_node *np;
706
707 for_each_compatible_node(np, NULL, "ibm,opal-i2c")
708 of_platform_device_create(np, NULL, NULL);
709}
710
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100711static int kopald(void *unused)
712{
Alistair Popple9f0fd042015-05-15 14:06:37 +1000713 __be64 events;
714
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100715 set_freezable();
716 do {
717 try_to_freeze();
Alistair Popple9f0fd042015-05-15 14:06:37 +1000718 opal_poll_events(&events);
719 opal_handle_events(be64_to_cpu(events));
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100720 msleep_interruptible(opal_heartbeat);
721 } while (!kthread_should_stop());
722
723 return 0;
724}
725
726static void opal_init_heartbeat(void)
727{
728 /* Old firwmware, we assume the HVC heartbeat is sufficient */
729 if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
730 &opal_heartbeat) != 0)
731 opal_heartbeat = 0;
732
733 if (opal_heartbeat)
734 kthread_run(kopald, NULL, "kopald");
735}
736
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000737static int __init opal_init(void)
738{
739 struct device_node *np, *consoles;
Gavin Shanc1c3a522015-01-23 14:25:05 +1100740 int rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000741
742 opal_node = of_find_node_by_path("/ibm,opal");
743 if (!opal_node) {
Michael Ellerman08135132015-01-28 15:10:33 +1100744 pr_warn("Device node not found\n");
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000745 return -ENODEV;
746 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000747
Alistair Popple9f0fd042015-05-15 14:06:37 +1000748 /* Initialise OPAL events */
749 opal_event_init();
750
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000751 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000752 if (firmware_has_feature(FW_FEATURE_OPALv2))
753 consoles = of_find_node_by_path("/ibm,opal/consoles");
754 else
755 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000756 if (consoles) {
757 for_each_child_of_node(consoles, np) {
758 if (strcmp(np->name, "serial"))
759 continue;
760 of_platform_device_create(np, NULL, NULL);
761 }
762 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000763 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000764
Alistair Popple96e023e2015-05-15 14:06:36 +1000765 /* Initialise OPAL messaging system */
766 opal_message_init();
767
768 /* Initialise OPAL asynchronous completion interface */
769 opal_async_comp_init();
770
771 /* Initialise OPAL sensor interface */
772 opal_sensor_init();
773
774 /* Initialise OPAL hypervisor maintainence interrupt handling */
775 opal_hmi_handler_init();
776
Neelesh Gupta47083452014-12-13 23:31:05 +0530777 /* Create i2c platform devices */
778 opal_i2c_create_devs();
779
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100780 /* Setup a heatbeat thread if requested by OPAL */
781 opal_init_heartbeat();
782
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530783 /* Create "opal" kobject under /sys/firmware */
784 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530785 if (rc == 0) {
Benjamin Herrenschmidtc8742f82014-12-11 12:39:49 +1100786 /* Export symbol map to userspace */
787 opal_export_symmap();
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530788 /* Setup dump region interface */
789 opal_dump_region_init();
Stewart Smith774fea12014-02-28 11:58:32 +1100790 /* Setup error log interface */
791 rc = opal_elog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530792 /* Setup code update interface */
Cyril Bured591902015-04-01 14:05:30 +0800793 opal_flash_update_init();
Stewart Smithc7e64b92014-03-03 10:25:42 +1100794 /* Setup platform dump extract interface */
795 opal_platform_dump_init();
Neelesh Gupta4029cd62014-03-07 11:02:09 +0530796 /* Setup system parameters interface */
797 opal_sys_param_init();
Joel Stanleybfc36892014-04-01 14:28:19 +1030798 /* Setup message log interface. */
799 opal_msglog_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530800 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530801
Benjamin Herrenschmidt3bf57562014-11-14 16:13:50 +1100802 /* Initialize OPAL IPMI backend */
Jeremy Kerr608b2862014-11-06 11:38:27 +0800803 opal_ipmi_init(opal_node);
804
Cyril Bured591902015-04-01 14:05:30 +0800805 opal_flash_init(opal_node);
806
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000807 return 0;
808}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000809machine_subsys_initcall(powernv, opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000810
811void opal_shutdown(void)
812{
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100813 long rc = OPAL_BUSY;
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000814
Alistair Popple9f0fd042015-05-15 14:06:37 +1000815 opal_event_shutdown();
Vasant Hegdef7d98d12014-01-15 17:02:04 +1100816
817 /*
818 * Then sync with OPAL which ensure anything that can
819 * potentially write to our memory has completed such
820 * as an ongoing dump retrieval
821 */
822 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
823 rc = opal_sync_host_reboot();
824 if (rc == OPAL_BUSY)
825 opal_poll_events(NULL);
826 else
827 mdelay(10);
828 }
Vasant Hegdeb09c2ec2014-08-09 11:15:45 +0530829
830 /* Unregister memory dump region */
Stewart Smithb962f5a2015-02-12 16:25:27 +1100831 if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
832 opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000833}
Joel Stanleye28b05e2014-04-01 14:28:20 +1030834
835/* Export this so that test modules can use it */
836EXPORT_SYMBOL_GPL(opal_invalid_call);
Jeremy Kerr608b2862014-11-06 11:38:27 +0800837EXPORT_SYMBOL_GPL(opal_ipmi_send);
838EXPORT_SYMBOL_GPL(opal_ipmi_recv);
Cyril Bured591902015-04-01 14:05:30 +0800839EXPORT_SYMBOL_GPL(opal_flash_read);
840EXPORT_SYMBOL_GPL(opal_flash_write);
841EXPORT_SYMBOL_GPL(opal_flash_erase);
Anton Blanchard3441f042014-04-22 15:01:26 +1000842
843/* Convert a region of vmalloc memory to an opal sg list */
844struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
845 unsigned long vmalloc_size)
846{
847 struct opal_sg_list *sg, *first = NULL;
848 unsigned long i = 0;
849
850 sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
851 if (!sg)
852 goto nomem;
853
854 first = sg;
855
856 while (vmalloc_size > 0) {
857 uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
858 uint64_t length = min(vmalloc_size, PAGE_SIZE);
859
860 sg->entry[i].data = cpu_to_be64(data);
861 sg->entry[i].length = cpu_to_be64(length);
862 i++;
863
864 if (i >= SG_ENTRIES_PER_NODE) {
865 struct opal_sg_list *next;
866
867 next = kzalloc(PAGE_SIZE, GFP_KERNEL);
868 if (!next)
869 goto nomem;
870
871 sg->length = cpu_to_be64(
872 i * sizeof(struct opal_sg_entry) + 16);
873 i = 0;
874 sg->next = cpu_to_be64(__pa(next));
875 sg = next;
876 }
877
878 vmalloc_addr += length;
879 vmalloc_size -= length;
880 }
881
882 sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
883
884 return first;
885
886nomem:
887 pr_err("%s : Failed to allocate memory\n", __func__);
888 opal_free_sg_list(first);
889 return NULL;
890}
891
892void opal_free_sg_list(struct opal_sg_list *sg)
893{
894 while (sg) {
895 uint64_t next = be64_to_cpu(sg->next);
896
897 kfree(sg);
898
899 if (next)
900 sg = __va(next);
901 else
902 sg = NULL;
903 }
904}
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530905
Cédric Le Goatere3c5c2e2015-03-30 12:06:09 +0200906int opal_error_code(int rc)
907{
908 switch (rc) {
909 case OPAL_SUCCESS: return 0;
910
911 case OPAL_PARAMETER: return -EINVAL;
912 case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
913 case OPAL_BUSY_EVENT: return -EBUSY;
914 case OPAL_NO_MEM: return -ENOMEM;
915
916 case OPAL_UNSUPPORTED: return -EIO;
917 case OPAL_HARDWARE: return -EIO;
918 case OPAL_INTERNAL_ERROR: return -EIO;
919 default:
920 pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
921 return -EIO;
922 }
923}
924
Neelesh Gupta16b1d262014-10-14 14:08:36 +0530925EXPORT_SYMBOL_GPL(opal_poll_events);
926EXPORT_SYMBOL_GPL(opal_rtc_read);
927EXPORT_SYMBOL_GPL(opal_rtc_write);
928EXPORT_SYMBOL_GPL(opal_tpo_read);
929EXPORT_SYMBOL_GPL(opal_tpo_write);
Neelesh Gupta47083452014-12-13 23:31:05 +0530930EXPORT_SYMBOL_GPL(opal_i2c_request);