1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * PowerNV nvram code.
4 *
5 * Copyright 2011 IBM Corp.
6 */
7
8#define DEBUG
9
10#include <linux/delay.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/of.h>
14
15#include <asm/opal.h>
16#include <asm/nvram.h>
17#include <asm/machdep.h>
18
19static unsigned int nvram_size;
20
21static ssize_t opal_nvram_size(void)
22{
23	return nvram_size;
24}
25
26static ssize_t opal_nvram_read(char *buf, size_t count, loff_t *index)
27{
28	s64 rc;
29	int off;
30
31	if (*index >= nvram_size)
32		return 0;
33	off = *index;
34	if ((off + count) > nvram_size)
35		count = nvram_size - off;
36	rc = opal_read_nvram(__pa(buf), count, off);
37	if (rc != OPAL_SUCCESS)
38		return -EIO;
39	*index += count;
40	return count;
41}
42
43/*
44 * This can be called in the panic path with interrupts off, so use
45 * mdelay in that case.
46 */
47static ssize_t opal_nvram_write(char *buf, size_t count, loff_t *index)
48{
49	s64 rc = OPAL_BUSY;
50	int off;
51
52	if (*index >= nvram_size)
53		return 0;
54	off = *index;
55	if ((off + count) > nvram_size)
56		count = nvram_size - off;
57
58	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
59		rc = opal_write_nvram(__pa(buf), count, off);
60		if (rc == OPAL_BUSY_EVENT) {
61			if (in_interrupt() || irqs_disabled())
62				mdelay(OPAL_BUSY_DELAY_MS);
63			else
64				msleep(OPAL_BUSY_DELAY_MS);
65			opal_poll_events(NULL);
66		} else if (rc == OPAL_BUSY) {
67			if (in_interrupt() || irqs_disabled())
68				mdelay(OPAL_BUSY_DELAY_MS);
69			else
70				msleep(OPAL_BUSY_DELAY_MS);
71		}
72	}
73
74	if (rc)
75		return -EIO;
76
77	*index += count;
78	return count;
79}
80
81static int __init opal_nvram_init_log_partitions(void)
82{
83	/* Scan nvram for partitions */
84	nvram_scan_partitions();
85	nvram_init_oops_partition(0);
86	return 0;
87}
88machine_arch_initcall(powernv, opal_nvram_init_log_partitions);
89
90void __init opal_nvram_init(void)
91{
92	struct device_node *np;
93	const __be32 *nbytes_p;
94
95	np = of_find_compatible_node(NULL, NULL, "ibm,opal-nvram");
96	if (np == NULL)
97		return;
98
99	nbytes_p = of_get_property(np, "#bytes", NULL);
100	if (!nbytes_p) {
101		of_node_put(np);
102		return;
103	}
104	nvram_size = be32_to_cpup(nbytes_p);
105
106	pr_info("OPAL nvram setup, %u bytes\n", nvram_size);
107	of_node_put(np);
108
109	ppc_md.nvram_read = opal_nvram_read;
110	ppc_md.nvram_write = opal_nvram_write;
111	ppc_md.nvram_size = opal_nvram_size;
112}
113
114