opal.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * PowerNV OPAL high level interfaces
 *
 * Copyright 2011 IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#undef DEBUG

#include <linux/types.h>
#include <linux/of.h>
R
Rob Herring 已提交
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/kobject.h>
22 23
#include <asm/opal.h>
#include <asm/firmware.h>
24
#include <asm/mce.h>
25 26 27

#include "powernv.h"

28 29 30
/* /sys/firmware/opal */
struct kobject *opal_kobj;

31 32 33 34 35 36 37
struct opal {
	u64 base;
	u64 entry;
} opal;

static struct device_node *opal_node;
static DEFINE_SPINLOCK(opal_write_lock);
38
extern u64 opal_mc_secondary_handler[];
39 40
static unsigned int *opal_irqs;
static unsigned int opal_irq_count;
41 42 43 44
static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
static DEFINE_SPINLOCK(opal_notifier_lock);
static uint64_t last_notified_mask = 0x0ul;
static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

int __init early_init_dt_scan_opal(unsigned long node,
				   const char *uname, int depth, void *data)
{
	const void *basep, *entryp;
	unsigned long basesz, entrysz;

	if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
		return 0;

	basep  = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
	entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);

	if (!basep || !entryp)
		return 1;

	opal.base = of_read_number(basep, basesz/4);
	opal.entry = of_read_number(entryp, entrysz/4);

	pr_debug("OPAL Base  = 0x%llx (basep=%p basesz=%ld)\n",
		 opal.base, basep, basesz);
	pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
		 opal.entry, entryp, entrysz);

	powerpc_firmware_features |= FW_FEATURE_OPAL;
70 71 72 73 74
	if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
		powerpc_firmware_features |= FW_FEATURE_OPALv2;
		powerpc_firmware_features |= FW_FEATURE_OPALv3;
		printk("OPAL V3 detected !\n");
	} else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
75 76 77 78 79 80
		powerpc_firmware_features |= FW_FEATURE_OPALv2;
		printk("OPAL V2 detected !\n");
	} else {
		printk("OPAL V1 detected !\n");
	}

81 82 83 84 85
	return 1;
}

static int __init opal_register_exception_handlers(void)
{
86
#ifdef __BIG_ENDIAN__
87 88 89 90 91
	u64 glue;

	if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
		return -ENODEV;

92 93 94 95 96 97 98 99 100 101 102 103
	/* Hookup some exception handlers. We use the fwnmi area at 0x7000
	 * to provide the glue space to OPAL
	 */
	glue = 0x7000;
	opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
					__pa(opal_mc_secondary_handler[0]),
					glue);
	glue += 128;
	opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
					0, glue);
	glue += 128;
	opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
104
#endif
105

106
	return 0;
107 108
}

109 110
early_initcall(opal_register_exception_handlers);

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
int opal_notifier_register(struct notifier_block *nb)
{
	if (!nb) {
		pr_warning("%s: Invalid argument (%p)\n",
			   __func__, nb);
		return -EINVAL;
	}

	atomic_notifier_chain_register(&opal_notifier_head, nb);
	return 0;
}

static void opal_do_notifier(uint64_t events)
{
	unsigned long flags;
	uint64_t changed_mask;

	if (atomic_read(&opal_notifier_hold))
		return;

	spin_lock_irqsave(&opal_notifier_lock, flags);
	changed_mask = last_notified_mask ^ events;
	last_notified_mask = events;
	spin_unlock_irqrestore(&opal_notifier_lock, flags);

	/*
	 * We feed with the event bits and changed bits for
	 * enough information to the callback.
	 */
	atomic_notifier_call_chain(&opal_notifier_head,
				   events, (void *)changed_mask);
}

void opal_notifier_update_evt(uint64_t evt_mask,
			      uint64_t evt_val)
{
	unsigned long flags;

	spin_lock_irqsave(&opal_notifier_lock, flags);
	last_notified_mask &= ~evt_mask;
	last_notified_mask |= evt_val;
	spin_unlock_irqrestore(&opal_notifier_lock, flags);
}

void opal_notifier_enable(void)
{
	int64_t rc;
	uint64_t evt = 0;

	atomic_set(&opal_notifier_hold, 0);

	/* Process pending events */
	rc = opal_poll_events(&evt);
	if (rc == OPAL_SUCCESS && evt)
		opal_do_notifier(evt);
}

void opal_notifier_disable(void)
{
	atomic_set(&opal_notifier_hold, 1);
}

173 174
int opal_get_chars(uint32_t vtermno, char *buf, int count)
{
175 176
	s64 rc;
	__be64 evt, len;
177 178

	if (!opal.entry)
179
		return -ENODEV;
180
	opal_poll_events(&evt);
181
	if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
182
		return 0;
183 184
	len = cpu_to_be64(count);
	rc = opal_console_read(vtermno, &len, buf);	
185
	if (rc == OPAL_SUCCESS)
186
		return be64_to_cpu(len);
187 188 189 190 191 192
	return 0;
}

int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
{
	int written = 0;
193
	__be64 olen;
194
	s64 len, rc;
195
	unsigned long flags;
196
	__be64 evt;
197 198

	if (!opal.entry)
199
		return -ENODEV;
200 201 202

	/* We want put_chars to be atomic to avoid mangling of hvsi
	 * packets. To do that, we first test for room and return
203 204 205 206 207
	 * -EAGAIN if there isn't enough.
	 *
	 * Unfortunately, opal_console_write_buffer_space() doesn't
	 * appear to work on opal v1, so we just assume there is
	 * enough room and be done with it
208 209
	 */
	spin_lock_irqsave(&opal_write_lock, flags);
210
	if (firmware_has_feature(FW_FEATURE_OPALv2)) {
211 212
		rc = opal_console_write_buffer_space(vtermno, &olen);
		len = be64_to_cpu(olen);
213 214 215 216 217
		if (rc || len < total_len) {
			spin_unlock_irqrestore(&opal_write_lock, flags);
			/* Closed -> drop characters */
			if (rc)
				return total_len;
218
			opal_poll_events(NULL);
219 220
			return -EAGAIN;
		}
221 222 223 224 225
	}

	/* We still try to handle partial completions, though they
	 * should no longer happen.
	 */
226
	rc = OPAL_BUSY;
227 228
	while(total_len > 0 && (rc == OPAL_BUSY ||
				rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
229 230 231
		olen = cpu_to_be64(total_len);
		rc = opal_console_write(vtermno, &olen, data);
		len = be64_to_cpu(olen);
232 233 234 235 236 237 238

		/* Closed or other error drop */
		if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
		    rc != OPAL_BUSY_EVENT) {
			written = total_len;
			break;
		}
239 240 241 242 243 244 245 246 247 248 249 250
		if (rc == OPAL_SUCCESS) {
			total_len -= len;
			data += len;
			written += len;
		}
		/* This is a bit nasty but we need that for the console to
		 * flush when there aren't any interrupts. We will clean
		 * things a bit later to limit that to synchronous path
		 * such as the kernel console and xmon/udbg
		 */
		do
			opal_poll_events(&evt);
251 252
		while(rc == OPAL_SUCCESS &&
			(be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
253 254 255 256 257
	}
	spin_unlock_irqrestore(&opal_write_lock, flags);
	return written;
}

258 259
int opal_machine_check(struct pt_regs *regs)
{
260
	struct machine_check_event evt;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	const char *level, *sevstr, *subtype;
	static const char *opal_mc_ue_types[] = {
		"Indeterminate",
		"Instruction fetch",
		"Page table walk ifetch",
		"Load/Store",
		"Page table walk Load/Store",
	};
	static const char *opal_mc_slb_types[] = {
		"Indeterminate",
		"Parity",
		"Multihit",
	};
	static const char *opal_mc_erat_types[] = {
		"Indeterminate",
		"Parity",
		"Multihit",
	};
	static const char *opal_mc_tlb_types[] = {
		"Indeterminate",
		"Parity",
		"Multihit",
	};

285 286
	if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
		return 0;
287 288

	/* Print things out */
289
	if (evt.version != MCE_V1) {
290 291 292 293 294
		pr_err("Machine Check Exception, Unknown event version %d !\n",
		       evt.version);
		return 0;
	}
	switch(evt.severity) {
295
	case MCE_SEV_NO_ERROR:
296 297 298
		level = KERN_INFO;
		sevstr = "Harmless";
		break;
299
	case MCE_SEV_WARNING:
300 301 302
		level = KERN_WARNING;
		sevstr = "";
		break;
303
	case MCE_SEV_ERROR_SYNC:
304 305 306
		level = KERN_ERR;
		sevstr = "Severe";
		break;
307
	case MCE_SEV_FATAL:
308 309 310 311 312 313 314
	default:
		level = KERN_ERR;
		sevstr = "Fatal";
		break;
	}

	printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
315
	       evt.disposition == MCE_DISPOSITION_RECOVERED ?
316 317
	       "Recovered" : "[Not recovered");
	printk("%s  Initiator: %s\n", level,
318
	       evt.initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown");
319
	switch(evt.error_type) {
320
	case MCE_ERROR_TYPE_UE:
321 322 323 324 325 326 327 328 329 330 331 332
		subtype = evt.u.ue_error.ue_error_type <
			ARRAY_SIZE(opal_mc_ue_types) ?
			opal_mc_ue_types[evt.u.ue_error.ue_error_type]
			: "Unknown";
		printk("%s  Error type: UE [%s]\n", level, subtype);
		if (evt.u.ue_error.effective_address_provided)
			printk("%s    Effective address: %016llx\n",
			       level, evt.u.ue_error.effective_address);
		if (evt.u.ue_error.physical_address_provided)
			printk("%s      Physial address: %016llx\n",
			       level, evt.u.ue_error.physical_address);
		break;
333
	case MCE_ERROR_TYPE_SLB:
334 335 336 337 338 339 340 341 342
		subtype = evt.u.slb_error.slb_error_type <
			ARRAY_SIZE(opal_mc_slb_types) ?
			opal_mc_slb_types[evt.u.slb_error.slb_error_type]
			: "Unknown";
		printk("%s  Error type: SLB [%s]\n", level, subtype);
		if (evt.u.slb_error.effective_address_provided)
			printk("%s    Effective address: %016llx\n",
			       level, evt.u.slb_error.effective_address);
		break;
343
	case MCE_ERROR_TYPE_ERAT:
344 345 346 347 348 349 350 351 352
		subtype = evt.u.erat_error.erat_error_type <
			ARRAY_SIZE(opal_mc_erat_types) ?
			opal_mc_erat_types[evt.u.erat_error.erat_error_type]
			: "Unknown";
		printk("%s  Error type: ERAT [%s]\n", level, subtype);
		if (evt.u.erat_error.effective_address_provided)
			printk("%s    Effective address: %016llx\n",
			       level, evt.u.erat_error.effective_address);
		break;
353
	case MCE_ERROR_TYPE_TLB:
354 355 356 357 358 359 360 361 362 363
		subtype = evt.u.tlb_error.tlb_error_type <
			ARRAY_SIZE(opal_mc_tlb_types) ?
			opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
			: "Unknown";
		printk("%s  Error type: TLB [%s]\n", level, subtype);
		if (evt.u.tlb_error.effective_address_provided)
			printk("%s    Effective address: %016llx\n",
			       level, evt.u.tlb_error.effective_address);
		break;
	default:
364
	case MCE_ERROR_TYPE_UNKNOWN:
365 366 367
		printk("%s  Error type: Unknown\n", level);
		break;
	}
368
	return evt.severity == MCE_SEV_FATAL ? 0 : 1;
369 370
}

371 372
static irqreturn_t opal_interrupt(int irq, void *data)
{
373
	__be64 events;
374 375 376

	opal_handle_interrupt(virq_to_hw(irq), &events);

377
	opal_do_notifier(events);
378 379 380 381

	return IRQ_HANDLED;
}

382 383 384 385 386 387 388 389 390 391 392
static int opal_sysfs_init(void)
{
	opal_kobj = kobject_create_and_add("opal", firmware_kobj);
	if (!opal_kobj) {
		pr_warn("kobject_create_and_add opal failed\n");
		return -ENOMEM;
	}

	return 0;
}

393 394 395
static int __init opal_init(void)
{
	struct device_node *np, *consoles;
396
	const __be32 *irqs;
397
	int rc, i, irqlen;
398 399 400 401 402 403

	opal_node = of_find_node_by_path("/ibm,opal");
	if (!opal_node) {
		pr_warn("opal: Node not found\n");
		return -ENODEV;
	}
404 405

	/* Register OPAL consoles if any ports */
406 407 408 409
	if (firmware_has_feature(FW_FEATURE_OPALv2))
		consoles = of_find_node_by_path("/ibm,opal/consoles");
	else
		consoles = of_node_get(opal_node);
410 411 412 413 414 415 416
	if (consoles) {
		for_each_child_of_node(consoles, np) {
			if (strcmp(np->name, "serial"))
				continue;
			of_platform_device_create(np, NULL, NULL);
		}
		of_node_put(consoles);
417
	}
418 419 420 421 422

	/* Find all OPAL interrupts and request them */
	irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
	pr_debug("opal: Found %d interrupts reserved for OPAL\n",
		 irqs ? (irqlen / 4) : 0);
423 424
	opal_irq_count = irqlen / 4;
	opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
425 426 427 428 429 430 431 432 433 434 435
	for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
		unsigned int hwirq = be32_to_cpup(irqs);
		unsigned int irq = irq_create_mapping(NULL, hwirq);
		if (irq == NO_IRQ) {
			pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
			continue;
		}
		rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
		if (rc)
			pr_warning("opal: Error %d requesting irq %d"
				   " (0x%x)\n", rc, irq, hwirq);
436
		opal_irqs[i] = irq;
437
	}
438 439 440

	/* Create "opal" kobject under /sys/firmware */
	rc = opal_sysfs_init();
441 442 443 444
	if (rc == 0) {
		/* Setup code update interface */
		opal_flash_init();
	}
445

446 447 448
	return 0;
}
subsys_initcall(opal_init);
449 450 451 452 453 454 455

void opal_shutdown(void)
{
	unsigned int i;

	for (i = 0; i < opal_irq_count; i++) {
		if (opal_irqs[i])
456
			free_irq(opal_irqs[i], NULL);
457 458 459
		opal_irqs[i] = 0;
	}
}