aboutsummaryrefslogtreecommitdiff
path: root/sys/powerpc/powernv/opal_nvram.c
blob: fe58bfacee284bb4e54480232ab0135e6715b179 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2019 Justin Hibbits
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/uio.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <machine/bus.h>
#include <machine/md_var.h>
#include <machine/pio.h>
#include <machine/resource.h>

#include "opal.h"

#include <sys/rman.h>

#include <vm/vm.h>
#include <vm/pmap.h>

#define	NVRAM_BUFSIZE	(65536)	/* 64k blocks */

struct opal_nvram_softc {
	device_t	 sc_dev;
	struct mtx	 sc_mtx;
	uint32_t	 sc_size;
	uint8_t		*sc_buf;
	vm_paddr_t	 sc_buf_phys;

	struct cdev 	*sc_cdev;
	int		 sc_isopen;
};

#define	NVRAM_LOCK(sc)		mtx_lock(&sc->sc_mtx)
#define	NVRAM_UNLOCK(sc)	mtx_unlock(&sc->sc_mtx)

/*
 * Device interface.
 */
static int		opal_nvram_probe(device_t);
static int		opal_nvram_attach(device_t);
static int		opal_nvram_detach(device_t);

/*
 * Driver methods.
 */
static device_method_t	opal_nvram_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		opal_nvram_probe),
	DEVMETHOD(device_attach,	opal_nvram_attach),
	DEVMETHOD(device_detach,	opal_nvram_detach),
	{ 0, 0 }
};

static driver_t	opal_nvram_driver = {
	"opal_nvram",
	opal_nvram_methods,
	sizeof(struct opal_nvram_softc)
};

static devclass_t opal_nvram_devclass;

DRIVER_MODULE(opal_nvram, opal, opal_nvram_driver, opal_nvram_devclass, 0, 0);

/*
 * Cdev methods.
 */

static	d_open_t	opal_nvram_open;
static	d_close_t	opal_nvram_close;
static	d_read_t	opal_nvram_read;
static	d_write_t	opal_nvram_write;
static	d_ioctl_t	opal_nvram_ioctl;

static struct cdevsw opal_nvram_cdevsw = {
	.d_version =	D_VERSION,
	.d_open =	opal_nvram_open,
	.d_close =	opal_nvram_close,
	.d_read =	opal_nvram_read,
	.d_write =	opal_nvram_write,
	.d_ioctl =	opal_nvram_ioctl,
	.d_name =	"nvram",
};

static int
opal_nvram_probe(device_t dev)
{

	if (!ofw_bus_is_compatible(dev, "ibm,opal-nvram"))
		return (ENXIO);

	device_set_desc(dev, "OPAL NVRAM");
	return (BUS_PROBE_DEFAULT);
}

static int
opal_nvram_attach(device_t dev)
{
	struct opal_nvram_softc *sc;
	phandle_t node;
	int err;

	node = ofw_bus_get_node(dev);
	sc = device_get_softc(dev);

	sc->sc_dev = dev;

	err = OF_getencprop(node, "#bytes", &sc->sc_size,
	    sizeof(sc->sc_size));

	if (err < 0)
		return (ENXIO);

	sc->sc_buf = contigmalloc(NVRAM_BUFSIZE, M_DEVBUF, M_WAITOK,
	    0, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
	if (sc->sc_buf == NULL) {
		device_printf(dev, "No memory for buffer.\n");
		return (ENXIO);
	}
	sc->sc_buf_phys = pmap_kextract((vm_offset_t)sc->sc_buf);
	sc->sc_cdev = make_dev(&opal_nvram_cdevsw, 0, 0, 0, 0600,
	    "nvram");
	sc->sc_cdev->si_drv1 = sc;

	mtx_init(&sc->sc_mtx, "opal_nvram", 0, MTX_DEF);

	return (0);
}

static int
opal_nvram_detach(device_t dev)
{
	struct opal_nvram_softc *sc;

	sc = device_get_softc(dev);

	if (sc->sc_cdev != NULL)
		destroy_dev(sc->sc_cdev);
	if (sc->sc_buf != NULL)
		contigfree(sc->sc_buf, NVRAM_BUFSIZE, M_DEVBUF);

	mtx_destroy(&sc->sc_mtx);

	return (0);
}

static int
opal_nvram_open(struct cdev *dev, int flags, int fmt, struct thread *td)
{
	struct opal_nvram_softc *sc = dev->si_drv1;
	int err;

	err = 0;

	NVRAM_LOCK(sc);
	if (sc->sc_isopen)
		err = EBUSY;
	else
		sc->sc_isopen = 1;
	NVRAM_UNLOCK(sc);

	return (err);
}

static int
opal_nvram_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
{
	struct opal_nvram_softc *sc = dev->si_drv1;

	NVRAM_LOCK(sc);
	sc->sc_isopen = 0;
	NVRAM_UNLOCK(sc);

	return (0);
}

static int
opal_nvram_read(struct cdev *dev, struct uio *uio, int ioflag)
{
	struct opal_nvram_softc *sc = dev->si_drv1;
	int rv, amnt;

	rv = 0;

	NVRAM_LOCK(sc);
	while (uio->uio_resid > 0) {
		amnt = MIN(uio->uio_resid, sc->sc_size - uio->uio_offset);
		amnt = MIN(amnt, NVRAM_BUFSIZE);
		if (amnt == 0)
			break;

		rv = opal_call(OPAL_READ_NVRAM, sc->sc_buf_phys,
		    amnt, uio->uio_offset);
		if (rv != OPAL_SUCCESS) {
			switch (rv) {
			case OPAL_HARDWARE:
				rv = EIO;
				break;
			case OPAL_PARAMETER:
				rv = EINVAL;
				break;
			}
			break;
		}
		rv = uiomove(sc->sc_buf, amnt, uio);
		if (rv != 0)
			break;
	}
	NVRAM_UNLOCK(sc);

	return (rv);
}

static int
opal_nvram_write(struct cdev *dev, struct uio *uio, int ioflag)
{
	off_t offset;
	int rv, amnt;
	struct opal_nvram_softc *sc = dev->si_drv1;

	rv = 0;

	NVRAM_LOCK(sc);
	while (uio->uio_resid > 0) {
		amnt = MIN(uio->uio_resid, sc->sc_size - uio->uio_offset);
		amnt = MIN(amnt, NVRAM_BUFSIZE);
		if (amnt == 0) {
			rv = ENOSPC;
			break;
		}
		offset = uio->uio_offset;
		rv = uiomove(sc->sc_buf, amnt, uio);
		if (rv != 0)
			break;
		rv = opal_call(OPAL_WRITE_NVRAM, sc->sc_buf_phys, amnt,
		    offset);
		if (rv != OPAL_SUCCESS) {
			switch (rv) {
			case OPAL_HARDWARE:
				rv = EIO;
				break;
			case OPAL_PARAMETER:
				rv = EINVAL;
				break;
			}
			break;
		}
	}

	NVRAM_UNLOCK(sc);

	return (rv);
}

static int
opal_nvram_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
    struct thread *td)
{
	struct opal_nvram_softc *sc = dev->si_drv1;

	switch (cmd) {
	case DIOCGMEDIASIZE:
		*(off_t *)data = sc->sc_size;
		return (0);
	}
	return (EINVAL);
}