Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*-
 * Copyright (c) 2015 Nathan Whitehorn
 * All rights reserved.
 *
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/clock.h>
#include <sys/cpu.h>
#include <sys/eventhandler.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/reboot.h>
#include <sys/sysctl.h>
#include <sys/endian.h>

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

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

#include "clock_if.h"
#include "opal.h"

static int	opaldev_probe(device_t);
static int	opaldev_attach(device_t);
/* clock interface */
static int	opal_gettime(device_t dev, struct timespec *ts);
static int	opal_settime(device_t dev, struct timespec *ts);
/* ofw bus interface */
static const struct ofw_bus_devinfo *opaldev_get_devinfo(device_t dev,
    device_t child);

static void	opal_shutdown(void *arg, int howto);
static void	opal_handle_shutdown_message(void *unused,
    struct opal_msg *msg);
static void	opal_intr(void *);

static device_method_t  opaldev_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		opaldev_probe),
	DEVMETHOD(device_attach,	opaldev_attach),

	/* clock interface */
	DEVMETHOD(clock_gettime,	opal_gettime),
	DEVMETHOD(clock_settime,	opal_settime),

	/* Bus interface */
	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),

        /* ofw_bus interface */
	DEVMETHOD(ofw_bus_get_devinfo,	opaldev_get_devinfo),
	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),

	DEVMETHOD_END
};

static driver_t opaldev_driver = {
	"opal",
	opaldev_methods,
	0
};

static devclass_t opaldev_devclass;

EARLY_DRIVER_MODULE(opaldev, ofwbus, opaldev_driver, opaldev_devclass, 0, 0,
    BUS_PASS_BUS);

static void opal_heartbeat(void);
static void opal_handle_messages(void);

static struct proc *opal_hb_proc;
static struct kproc_desc opal_heartbeat_kp = {
	"opal_heartbeat",
	opal_heartbeat,
	&opal_hb_proc
};

SYSINIT(opal_heartbeat_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start,
    &opal_heartbeat_kp);

static int opal_heartbeat_ms;
EVENTHANDLER_LIST_DEFINE(OPAL_ASYNC_COMP);
EVENTHANDLER_LIST_DEFINE(OPAL_EPOW);
EVENTHANDLER_LIST_DEFINE(OPAL_SHUTDOWN);
EVENTHANDLER_LIST_DEFINE(OPAL_HMI_EVT);
EVENTHANDLER_LIST_DEFINE(OPAL_DPO);
EVENTHANDLER_LIST_DEFINE(OPAL_OCC);

#define	OPAL_SOFT_OFF		0
#define	OPAL_SOFT_REBOOT	1

static void
opal_heartbeat(void)
{
	uint64_t events;

	if (opal_heartbeat_ms == 0)
		kproc_exit(0);

	while (1) {
		events = 0;
		/* Turn the OPAL state crank */
		opal_call(OPAL_POLL_EVENTS, vtophys(&events));
		if (be64toh(events) & OPAL_EVENT_MSG_PENDING)
			opal_handle_messages();
		tsleep(opal_hb_proc, 0, "opal",
		    MSEC_2_TICKS(opal_heartbeat_ms));
	}
}

static int
opaldev_probe(device_t dev)
{
	phandle_t iparent;
	pcell_t *irqs;
	int i, n_irqs;

	if (!ofw_bus_is_compatible(dev, "ibm,opal-v3"))
		return (ENXIO);
	if (opal_check() != 0)
		return (ENXIO);

	device_set_desc(dev, "OPAL Abstraction Firmware");

	/* Manually add IRQs before attaching */
	if (OF_hasprop(ofw_bus_get_node(dev), "opal-interrupts")) {
		iparent = OF_finddevice("/interrupt-controller@0");
		iparent = OF_xref_from_node(iparent);

		n_irqs = OF_getproplen(ofw_bus_get_node(dev),
                    "opal-interrupts") / sizeof(*irqs);
		irqs = malloc(n_irqs * sizeof(*irqs), M_DEVBUF, M_WAITOK);
		OF_getencprop(ofw_bus_get_node(dev), "opal-interrupts", irqs,
		    n_irqs * sizeof(*irqs));
		for (i = 0; i < n_irqs; i++)
			bus_set_resource(dev, SYS_RES_IRQ, i,
			    ofw_bus_map_intr(dev, iparent, 1, &irqs[i]), 1);
		free(irqs, M_DEVBUF);
	}

	return (BUS_PROBE_SPECIFIC);
}

static int
opaldev_attach(device_t dev)
{
	phandle_t child;
	device_t cdev;
	uint64_t junk;
	int i, rv;
	uint32_t async_count;
	struct ofw_bus_devinfo *dinfo;
	struct resource *irq;

	/* Test for RTC support and register clock if it works */
	rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
	do {
		rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
		if (rv == OPAL_BUSY_EVENT)
			rv = opal_call(OPAL_POLL_EVENTS, 0);
	} while (rv == OPAL_BUSY_EVENT);

	if (rv == OPAL_SUCCESS)
		clock_register(dev, 2000);

	EVENTHANDLER_REGISTER(OPAL_SHUTDOWN, opal_handle_shutdown_message,
	    NULL, EVENTHANDLER_PRI_ANY);
	EVENTHANDLER_REGISTER(shutdown_final, opal_shutdown, NULL,
	    SHUTDOWN_PRI_LAST);

	OF_getencprop(ofw_bus_get_node(dev), "ibm,heartbeat-ms",
	    &opal_heartbeat_ms, sizeof(opal_heartbeat_ms));
	/* Bind to interrupts */
	for (i = 0; (irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
	    RF_ACTIVE)) != NULL; i++)
		bus_setup_intr(dev, irq, INTR_TYPE_TTY | INTR_MPSAFE |
		    INTR_ENTROPY, NULL, opal_intr, (void *)rman_get_start(irq),
		    NULL);

	OF_getencprop(ofw_bus_get_node(dev), "opal-msg-async-num",
	    &async_count, sizeof(async_count));
	opal_init_async_tokens(async_count);

	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
	    child = OF_peer(child)) {
		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
			free(dinfo, M_DEVBUF);
			continue;
		}
		cdev = device_add_child(dev, NULL, -1);
		if (cdev == NULL) {
			device_printf(dev, "<%s>: device_add_child failed\n",
			    dinfo->obd_name);
			ofw_bus_gen_destroy_devinfo(dinfo);
			free(dinfo, M_DEVBUF);
			continue;
		}
		device_set_ivars(cdev, dinfo);
	}

	return (bus_generic_attach(dev));
}

static int
bcd2bin32(int bcd)
{
	int out = 0;

	out += bcd2bin(bcd & 0xff);
	out += 100*bcd2bin((bcd & 0x0000ff00) >> 8);
	out += 10000*bcd2bin((bcd & 0x00ff0000) >> 16);
	out += 1000000*bcd2bin((bcd & 0xffff0000) >> 24);

	return (out);
}

static int
bin2bcd32(int bin)
{
	int out = 0;
	int tmp;

	tmp = bin % 100;
	out += bin2bcd(tmp) * 0x1;
	bin = bin / 100;

	tmp = bin % 100;
	out += bin2bcd(tmp) * 0x100;
	bin = bin / 100;

	tmp = bin % 100;
	out += bin2bcd(tmp) * 0x10000;

	return (out);
}

static int
opal_gettime(device_t dev, struct timespec *ts)
{
	int rv;
	struct clocktime ct;
	uint32_t ymd;
	uint64_t hmsm;

	rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
	while (rv == OPAL_BUSY_EVENT)  {
		opal_call(OPAL_POLL_EVENTS, 0);
		pause("opalrtc", 1);
		rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
	}

	if (rv != OPAL_SUCCESS)
		return (ENXIO);

	hmsm = be64toh(hmsm);
	ymd = be32toh(ymd);

	ct.nsec	= bcd2bin32((hmsm & 0x000000ffffff0000) >> 16) * 1000;
	ct.sec	= bcd2bin((hmsm & 0x0000ff0000000000) >> 40);
	ct.min	= bcd2bin((hmsm & 0x00ff000000000000) >> 48);
	ct.hour	= bcd2bin((hmsm & 0xff00000000000000) >> 56);

	ct.day	= bcd2bin((ymd & 0x000000ff) >> 0);
	ct.mon	= bcd2bin((ymd & 0x0000ff00) >> 8);
	ct.year = bcd2bin32((ymd & 0xffff0000) >> 16);

	return (clock_ct_to_ts(&ct, ts));
}

static int
opal_settime(device_t dev, struct timespec *ts)
{
	int rv;
	struct clocktime ct;
	uint32_t ymd = 0;
	uint64_t hmsm = 0;

	clock_ts_to_ct(ts, &ct);

	ymd |= (uint32_t)bin2bcd(ct.day);
	ymd |= ((uint32_t)bin2bcd(ct.mon) << 8);
	ymd |= ((uint32_t)bin2bcd32(ct.year) << 16);

	hmsm |= ((uint64_t)bin2bcd32(ct.nsec/1000) << 16);
	hmsm |= ((uint64_t)bin2bcd(ct.sec) << 40);
	hmsm |= ((uint64_t)bin2bcd(ct.min) << 48);
	hmsm |= ((uint64_t)bin2bcd(ct.hour) << 56);

	/*
	 * We do NOT swap endian here, because the values are being sent
	 * via registers instead of indirect via memory.
	 */
	do {
		rv = opal_call(OPAL_RTC_WRITE, ymd, hmsm);
		if (rv == OPAL_BUSY_EVENT) {
			rv = opal_call(OPAL_POLL_EVENTS, 0);
			pause("opalrtc", 1);
		}
	} while (rv == OPAL_BUSY_EVENT);

	if (rv != OPAL_SUCCESS)
		return (ENXIO);

	return (0);
}

static const struct ofw_bus_devinfo *
opaldev_get_devinfo(device_t dev, device_t child)
{
	return (device_get_ivars(child));
}

static void
opal_shutdown(void *arg, int howto)
{

	if (howto & RB_HALT)
		opal_call(OPAL_CEC_POWER_DOWN, 0 /* Normal power off */);
	else
		opal_call(OPAL_CEC_REBOOT);

	opal_call(OPAL_RETURN_CPU);
}

static void
opal_handle_shutdown_message(void *unused, struct opal_msg *msg)
{
	int howto;

	switch (be64toh(msg->params[0])) {
	case OPAL_SOFT_OFF:
		howto = RB_POWEROFF;
		break;
	case OPAL_SOFT_REBOOT:
		howto = RB_REROOT;
		break;
	}
	shutdown_nice(howto);
}

static void
opal_handle_messages(void)
{
	static struct opal_msg msg;
	uint64_t rv;
	uint32_t type;

	rv = opal_call(OPAL_GET_MSG, vtophys(&msg), sizeof(msg));

	if (rv != OPAL_SUCCESS)
		return;

	type = be32toh(msg.msg_type);
	switch (type) {
	case OPAL_MSG_ASYNC_COMP:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_ASYNC_COMP, &msg);
		break;
	case OPAL_MSG_EPOW:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_EPOW, &msg);
		break;
	case OPAL_MSG_SHUTDOWN:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_SHUTDOWN, &msg);
		break;
	case OPAL_MSG_HMI_EVT:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_HMI_EVT, &msg);
		break;
	case OPAL_MSG_DPO:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_DPO, &msg);
		break;
	case OPAL_MSG_OCC:
		EVENTHANDLER_DIRECT_INVOKE(OPAL_OCC, &msg);
		break;
	default:
		printf("Unknown OPAL message type %d\n", type);
	}
}

static void
opal_intr(void *xintr)
{
	uint64_t events = 0;

	opal_call(OPAL_HANDLE_INTERRUPT, (uint32_t)(uint64_t)xintr,
	    vtophys(&events));
	/* Wake up the heartbeat, if it's been setup. */
	if (events != 0 && opal_hb_proc != NULL)
		wakeup(opal_hb_proc);

}