xref: /freebsd/sys/powerpc/powernv/opal_dev.c (revision 8b959dd6a3921c35395bef4a6d7ad2426a3bd88e)
1 /*-
2  * Copyright (c) 2015 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/clock.h>
36 #include <sys/cpu.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/endian.h>
43 
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/ofw/openfirm.h>
50 
51 #include "clock_if.h"
52 #include "opal.h"
53 
54 static int	opaldev_probe(device_t);
55 static int	opaldev_attach(device_t);
56 /* clock interface */
57 static int	opal_gettime(device_t dev, struct timespec *ts);
58 static int	opal_settime(device_t dev, struct timespec *ts);
59 /* ofw bus interface */
60 static const struct ofw_bus_devinfo *opaldev_get_devinfo(device_t dev,
61     device_t child);
62 
63 static void	opal_shutdown(void *arg, int howto);
64 static void	opal_handle_shutdown_message(void *unused,
65     struct opal_msg *msg);
66 static void	opal_intr(void *);
67 
68 static device_method_t  opaldev_methods[] = {
69 	/* Device interface */
70 	DEVMETHOD(device_probe,		opaldev_probe),
71 	DEVMETHOD(device_attach,	opaldev_attach),
72 
73 	/* clock interface */
74 	DEVMETHOD(clock_gettime,	opal_gettime),
75 	DEVMETHOD(clock_settime,	opal_settime),
76 
77 	/* Bus interface */
78 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
79 
80         /* ofw_bus interface */
81 	DEVMETHOD(ofw_bus_get_devinfo,	opaldev_get_devinfo),
82 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
83 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
84 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
85 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
86 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
87 
88 	DEVMETHOD_END
89 };
90 
91 static driver_t opaldev_driver = {
92 	"opal",
93 	opaldev_methods,
94 	0
95 };
96 
97 static devclass_t opaldev_devclass;
98 
99 EARLY_DRIVER_MODULE(opaldev, ofwbus, opaldev_driver, opaldev_devclass, 0, 0,
100     BUS_PASS_BUS);
101 
102 static void opal_heartbeat(void);
103 static void opal_handle_messages(void);
104 
105 static struct proc *opal_hb_proc;
106 static struct kproc_desc opal_heartbeat_kp = {
107 	"opal_heartbeat",
108 	opal_heartbeat,
109 	&opal_hb_proc
110 };
111 
112 SYSINIT(opal_heartbeat_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start,
113     &opal_heartbeat_kp);
114 
115 static int opal_heartbeat_ms;
116 EVENTHANDLER_LIST_DEFINE(OPAL_ASYNC_COMP);
117 EVENTHANDLER_LIST_DEFINE(OPAL_EPOW);
118 EVENTHANDLER_LIST_DEFINE(OPAL_SHUTDOWN);
119 EVENTHANDLER_LIST_DEFINE(OPAL_HMI_EVT);
120 EVENTHANDLER_LIST_DEFINE(OPAL_DPO);
121 EVENTHANDLER_LIST_DEFINE(OPAL_OCC);
122 
123 #define	OPAL_SOFT_OFF		0
124 #define	OPAL_SOFT_REBOOT	1
125 
126 static void
127 opal_heartbeat(void)
128 {
129 	uint64_t events;
130 
131 	if (opal_heartbeat_ms == 0)
132 		kproc_exit(0);
133 
134 	while (1) {
135 		events = 0;
136 		/* Turn the OPAL state crank */
137 		opal_call(OPAL_POLL_EVENTS, vtophys(&events));
138 		if (be64toh(events) & OPAL_EVENT_MSG_PENDING)
139 			opal_handle_messages();
140 		tsleep(opal_hb_proc, 0, "opal",
141 		    MSEC_2_TICKS(opal_heartbeat_ms));
142 	}
143 }
144 
145 static int
146 opaldev_probe(device_t dev)
147 {
148 	phandle_t iparent;
149 	pcell_t *irqs;
150 	int i, n_irqs;
151 
152 	if (!ofw_bus_is_compatible(dev, "ibm,opal-v3"))
153 		return (ENXIO);
154 	if (opal_check() != 0)
155 		return (ENXIO);
156 
157 	device_set_desc(dev, "OPAL Abstraction Firmware");
158 
159 	/* Manually add IRQs before attaching */
160 	if (OF_hasprop(ofw_bus_get_node(dev), "opal-interrupts")) {
161 		iparent = OF_finddevice("/interrupt-controller@0");
162 		iparent = OF_xref_from_node(iparent);
163 
164 		n_irqs = OF_getproplen(ofw_bus_get_node(dev),
165                     "opal-interrupts") / sizeof(*irqs);
166 		irqs = malloc(n_irqs * sizeof(*irqs), M_DEVBUF, M_WAITOK);
167 		OF_getencprop(ofw_bus_get_node(dev), "opal-interrupts", irqs,
168 		    n_irqs * sizeof(*irqs));
169 		for (i = 0; i < n_irqs; i++)
170 			bus_set_resource(dev, SYS_RES_IRQ, i,
171 			    ofw_bus_map_intr(dev, iparent, 1, &irqs[i]), 1);
172 		free(irqs, M_DEVBUF);
173 	}
174 
175 	return (BUS_PROBE_SPECIFIC);
176 }
177 
178 static int
179 opaldev_attach(device_t dev)
180 {
181 	phandle_t child;
182 	device_t cdev;
183 	uint64_t junk;
184 	int i, rv;
185 	uint32_t async_count;
186 	struct ofw_bus_devinfo *dinfo;
187 	struct resource *irq;
188 
189 	/* Test for RTC support and register clock if it works */
190 	rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
191 	do {
192 		rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
193 		if (rv == OPAL_BUSY_EVENT)
194 			rv = opal_call(OPAL_POLL_EVENTS, 0);
195 	} while (rv == OPAL_BUSY_EVENT);
196 
197 	if (rv == OPAL_SUCCESS)
198 		clock_register(dev, 2000);
199 
200 	EVENTHANDLER_REGISTER(OPAL_SHUTDOWN, opal_handle_shutdown_message,
201 	    NULL, EVENTHANDLER_PRI_ANY);
202 	EVENTHANDLER_REGISTER(shutdown_final, opal_shutdown, NULL,
203 	    SHUTDOWN_PRI_LAST);
204 
205 	OF_getencprop(ofw_bus_get_node(dev), "ibm,heartbeat-ms",
206 	    &opal_heartbeat_ms, sizeof(opal_heartbeat_ms));
207 	/* Bind to interrupts */
208 	for (i = 0; (irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
209 	    RF_ACTIVE)) != NULL; i++)
210 		bus_setup_intr(dev, irq, INTR_TYPE_TTY | INTR_MPSAFE |
211 		    INTR_ENTROPY, NULL, opal_intr, (void *)rman_get_start(irq),
212 		    NULL);
213 
214 	OF_getencprop(ofw_bus_get_node(dev), "opal-msg-async-num",
215 	    &async_count, sizeof(async_count));
216 	opal_init_async_tokens(async_count);
217 
218 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
219 	    child = OF_peer(child)) {
220 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
221 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
222 			free(dinfo, M_DEVBUF);
223 			continue;
224 		}
225 		cdev = device_add_child(dev, NULL, -1);
226 		if (cdev == NULL) {
227 			device_printf(dev, "<%s>: device_add_child failed\n",
228 			    dinfo->obd_name);
229 			ofw_bus_gen_destroy_devinfo(dinfo);
230 			free(dinfo, M_DEVBUF);
231 			continue;
232 		}
233 		device_set_ivars(cdev, dinfo);
234 	}
235 
236 	return (bus_generic_attach(dev));
237 }
238 
239 static int
240 bcd2bin32(int bcd)
241 {
242 	int out = 0;
243 
244 	out += bcd2bin(bcd & 0xff);
245 	out += 100*bcd2bin((bcd & 0x0000ff00) >> 8);
246 	out += 10000*bcd2bin((bcd & 0x00ff0000) >> 16);
247 	out += 1000000*bcd2bin((bcd & 0xffff0000) >> 24);
248 
249 	return (out);
250 }
251 
252 static int
253 bin2bcd32(int bin)
254 {
255 	int out = 0;
256 	int tmp;
257 
258 	tmp = bin % 100;
259 	out += bin2bcd(tmp) * 0x1;
260 	bin = bin / 100;
261 
262 	tmp = bin % 100;
263 	out += bin2bcd(tmp) * 0x100;
264 	bin = bin / 100;
265 
266 	tmp = bin % 100;
267 	out += bin2bcd(tmp) * 0x10000;
268 
269 	return (out);
270 }
271 
272 static int
273 opal_gettime(device_t dev, struct timespec *ts)
274 {
275 	int rv;
276 	struct clocktime ct;
277 	uint32_t ymd;
278 	uint64_t hmsm;
279 
280 	rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
281 	while (rv == OPAL_BUSY_EVENT)  {
282 		opal_call(OPAL_POLL_EVENTS, 0);
283 		pause("opalrtc", 1);
284 		rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
285 	}
286 
287 	if (rv != OPAL_SUCCESS)
288 		return (ENXIO);
289 
290 	hmsm = be64toh(hmsm);
291 	ymd = be32toh(ymd);
292 
293 	ct.nsec	= bcd2bin32((hmsm & 0x000000ffffff0000) >> 16) * 1000;
294 	ct.sec	= bcd2bin((hmsm & 0x0000ff0000000000) >> 40);
295 	ct.min	= bcd2bin((hmsm & 0x00ff000000000000) >> 48);
296 	ct.hour	= bcd2bin((hmsm & 0xff00000000000000) >> 56);
297 
298 	ct.day	= bcd2bin((ymd & 0x000000ff) >> 0);
299 	ct.mon	= bcd2bin((ymd & 0x0000ff00) >> 8);
300 	ct.year = bcd2bin32((ymd & 0xffff0000) >> 16);
301 
302 	return (clock_ct_to_ts(&ct, ts));
303 }
304 
305 static int
306 opal_settime(device_t dev, struct timespec *ts)
307 {
308 	int rv;
309 	struct clocktime ct;
310 	uint32_t ymd = 0;
311 	uint64_t hmsm = 0;
312 
313 	clock_ts_to_ct(ts, &ct);
314 
315 	ymd |= (uint32_t)bin2bcd(ct.day);
316 	ymd |= ((uint32_t)bin2bcd(ct.mon) << 8);
317 	ymd |= ((uint32_t)bin2bcd32(ct.year) << 16);
318 
319 	hmsm |= ((uint64_t)bin2bcd32(ct.nsec/1000) << 16);
320 	hmsm |= ((uint64_t)bin2bcd(ct.sec) << 40);
321 	hmsm |= ((uint64_t)bin2bcd(ct.min) << 48);
322 	hmsm |= ((uint64_t)bin2bcd(ct.hour) << 56);
323 
324 	/*
325 	 * We do NOT swap endian here, because the values are being sent
326 	 * via registers instead of indirect via memory.
327 	 */
328 	do {
329 		rv = opal_call(OPAL_RTC_WRITE, ymd, hmsm);
330 		if (rv == OPAL_BUSY_EVENT) {
331 			rv = opal_call(OPAL_POLL_EVENTS, 0);
332 			pause("opalrtc", 1);
333 		}
334 	} while (rv == OPAL_BUSY_EVENT);
335 
336 	if (rv != OPAL_SUCCESS)
337 		return (ENXIO);
338 
339 	return (0);
340 }
341 
342 static const struct ofw_bus_devinfo *
343 opaldev_get_devinfo(device_t dev, device_t child)
344 {
345 	return (device_get_ivars(child));
346 }
347 
348 static void
349 opal_shutdown(void *arg, int howto)
350 {
351 
352 	if (howto & RB_HALT)
353 		opal_call(OPAL_CEC_POWER_DOWN, 0 /* Normal power off */);
354 	else
355 		opal_call(OPAL_CEC_REBOOT);
356 
357 	opal_call(OPAL_RETURN_CPU);
358 }
359 
360 static void
361 opal_handle_shutdown_message(void *unused, struct opal_msg *msg)
362 {
363 	int howto;
364 
365 	switch (be64toh(msg->params[0])) {
366 	case OPAL_SOFT_OFF:
367 		howto = RB_POWEROFF;
368 		break;
369 	case OPAL_SOFT_REBOOT:
370 		howto = RB_REROOT;
371 		break;
372 	}
373 	shutdown_nice(howto);
374 }
375 
376 static void
377 opal_handle_messages(void)
378 {
379 	static struct opal_msg msg;
380 	uint64_t rv;
381 	uint32_t type;
382 
383 	rv = opal_call(OPAL_GET_MSG, vtophys(&msg), sizeof(msg));
384 
385 	switch (rv) {
386 	case OPAL_SUCCESS:
387 		break;
388 	case OPAL_RESOURCE:
389 		/* no available messages - return */
390 		return;
391 	case OPAL_PARAMETER:
392 		printf("%s error: invalid buffer. Please file a bug report.\n", __func__);
393 		return;
394 	case OPAL_PARTIAL:
395 		printf("%s error: buffer is too small and messages was discarded. Please file a bug report.\n", __func__);
396 		return;
397 	default:
398 		printf("%s opal_call returned unknown result <%lu>\n", __func__, rv);
399 		return;
400 	}
401 
402 	type = be32toh(msg.msg_type);
403 	switch (type) {
404 	case OPAL_MSG_ASYNC_COMP:
405 		EVENTHANDLER_DIRECT_INVOKE(OPAL_ASYNC_COMP, &msg);
406 		break;
407 	case OPAL_MSG_EPOW:
408 		EVENTHANDLER_DIRECT_INVOKE(OPAL_EPOW, &msg);
409 		break;
410 	case OPAL_MSG_SHUTDOWN:
411 		EVENTHANDLER_DIRECT_INVOKE(OPAL_SHUTDOWN, &msg);
412 		break;
413 	case OPAL_MSG_HMI_EVT:
414 		EVENTHANDLER_DIRECT_INVOKE(OPAL_HMI_EVT, &msg);
415 		break;
416 	case OPAL_MSG_DPO:
417 		EVENTHANDLER_DIRECT_INVOKE(OPAL_DPO, &msg);
418 		break;
419 	case OPAL_MSG_OCC:
420 		EVENTHANDLER_DIRECT_INVOKE(OPAL_OCC, &msg);
421 		break;
422 	default:
423 		printf("%s Unknown OPAL message type %d\n", __func__, type);
424 	}
425 }
426 
427 static void
428 opal_intr(void *xintr)
429 {
430 	uint64_t events = 0;
431 
432 	opal_call(OPAL_HANDLE_INTERRUPT, (uint32_t)(uint64_t)xintr,
433 	    vtophys(&events));
434 	/* Wake up the heartbeat, if it's been setup. */
435 	if (be64toh(events) != 0 && opal_hb_proc != NULL)
436 		wakeup(opal_hb_proc);
437 
438 }
439