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