xref: /linux/drivers/tty/hvc/hvc_opal.c (revision 831e5c42fd922072a4c80d69e369dc5a60f38f25)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * opal driver interface to hvc_console.c
4  *
5  * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
6  */
7 
8 #undef DEBUG
9 
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/console.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/export.h>
18 #include <linux/interrupt.h>
19 
20 #include <asm/hvconsole.h>
21 #include <asm/firmware.h>
22 #include <asm/hvsi.h>
23 #include <asm/udbg.h>
24 #include <asm/opal.h>
25 
26 #include "hvc_console.h"
27 
28 static const char hvc_opal_name[] = "hvc_opal";
29 
30 static const struct of_device_id hvc_opal_match[] = {
31 	{ .name = "serial", .compatible = "ibm,opal-console-raw" },
32 	{ .name = "serial", .compatible = "ibm,opal-console-hvsi" },
33 	{ },
34 };
35 
36 typedef enum hv_protocol {
37 	HV_PROTOCOL_RAW,
38 	HV_PROTOCOL_HVSI
39 } hv_protocol_t;
40 
41 struct hvc_opal_priv {
42 	hv_protocol_t		proto;	/* Raw data or HVSI packets */
43 	struct hvsi_priv	hvsi;	/* HVSI specific data */
44 };
45 static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
46 
47 /* For early boot console */
48 static struct hvc_opal_priv hvc_opal_boot_priv;
49 static u32 hvc_opal_boot_termno;
50 
51 static const struct hv_ops hvc_opal_raw_ops = {
52 	.get_chars = opal_get_chars,
53 	.put_chars = opal_put_chars,
54 	.flush = opal_flush_chars,
55 	.notifier_add = notifier_add_irq,
56 	.notifier_del = notifier_del_irq,
57 	.notifier_hangup = notifier_hangup_irq,
58 };
59 
60 static ssize_t hvc_opal_hvsi_get_chars(uint32_t vtermno, u8 *buf, size_t count)
61 {
62 	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
63 
64 	if (WARN_ON(!pv))
65 		return -ENODEV;
66 
67 	return hvsilib_get_chars(&pv->hvsi, buf, count);
68 }
69 
70 static ssize_t hvc_opal_hvsi_put_chars(uint32_t vtermno, const u8 *buf,
71 				       size_t count)
72 {
73 	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
74 
75 	if (WARN_ON(!pv))
76 		return -ENODEV;
77 
78 	return hvsilib_put_chars(&pv->hvsi, buf, count);
79 }
80 
81 static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
82 {
83 	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
84 	int rc;
85 
86 	pr_devel("HVSI@%x: do open !\n", hp->vtermno);
87 
88 	rc = notifier_add_irq(hp, data);
89 	if (rc)
90 		return rc;
91 
92 	return hvsilib_open(&pv->hvsi, hp);
93 }
94 
95 static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
96 {
97 	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
98 
99 	pr_devel("HVSI@%x: do close !\n", hp->vtermno);
100 
101 	hvsilib_close(&pv->hvsi, hp);
102 
103 	notifier_del_irq(hp, data);
104 }
105 
106 static void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
107 {
108 	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
109 
110 	pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
111 
112 	hvsilib_close(&pv->hvsi, hp);
113 
114 	notifier_hangup_irq(hp, data);
115 }
116 
117 static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
118 {
119 	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
120 
121 	if (!pv)
122 		return -EINVAL;
123 	return pv->hvsi.mctrl;
124 }
125 
126 static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
127 				unsigned int clear)
128 {
129 	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
130 
131 	pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
132 		 hp->vtermno, set, clear);
133 
134 	if (set & TIOCM_DTR)
135 		hvsilib_write_mctrl(&pv->hvsi, 1);
136 	else if (clear & TIOCM_DTR)
137 		hvsilib_write_mctrl(&pv->hvsi, 0);
138 
139 	return 0;
140 }
141 
142 static const struct hv_ops hvc_opal_hvsi_ops = {
143 	.get_chars = hvc_opal_hvsi_get_chars,
144 	.put_chars = hvc_opal_hvsi_put_chars,
145 	.flush = opal_flush_chars,
146 	.notifier_add = hvc_opal_hvsi_open,
147 	.notifier_del = hvc_opal_hvsi_close,
148 	.notifier_hangup = hvc_opal_hvsi_hangup,
149 	.tiocmget = hvc_opal_hvsi_tiocmget,
150 	.tiocmset = hvc_opal_hvsi_tiocmset,
151 };
152 
153 static int hvc_opal_probe(struct platform_device *dev)
154 {
155 	const struct hv_ops *ops;
156 	struct hvc_struct *hp;
157 	struct hvc_opal_priv *pv;
158 	hv_protocol_t proto;
159 	unsigned int termno, boot = 0;
160 	const __be32 *reg;
161 	int irq;
162 
163 	if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
164 		proto = HV_PROTOCOL_RAW;
165 		ops = &hvc_opal_raw_ops;
166 	} else if (of_device_is_compatible(dev->dev.of_node,
167 					   "ibm,opal-console-hvsi")) {
168 		proto = HV_PROTOCOL_HVSI;
169 		ops = &hvc_opal_hvsi_ops;
170 	} else {
171 		pr_err("hvc_opal: Unknown protocol for %pOF\n",
172 		       dev->dev.of_node);
173 		return -ENXIO;
174 	}
175 
176 	reg = of_get_property(dev->dev.of_node, "reg", NULL);
177 	termno = reg ? be32_to_cpup(reg) : 0;
178 
179 	irq = platform_get_irq_optional(dev, 0);
180 	if (irq == -EPROBE_DEFER)
181 		return irq;
182 
183 	/* Is it our boot one ? */
184 	if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
185 		pv = hvc_opal_privs[termno];
186 		boot = 1;
187 	} else if (hvc_opal_privs[termno] == NULL) {
188 		pv = kzalloc_obj(struct hvc_opal_priv);
189 		if (!pv)
190 			return -ENOMEM;
191 		pv->proto = proto;
192 		hvc_opal_privs[termno] = pv;
193 		if (proto == HV_PROTOCOL_HVSI) {
194 			/*
195 			 * We want put_chars to be atomic to avoid mangling of
196 			 * hvsi packets.
197 			 */
198 			hvsilib_init(&pv->hvsi,
199 				     opal_get_chars, opal_put_chars_atomic,
200 				     termno, 0);
201 		}
202 
203 		/* Instanciate now to establish a mapping index==vtermno */
204 		hvc_instantiate(termno, termno, ops);
205 	} else {
206 		pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
207 		       dev->dev.of_node, termno);
208 		return -ENXIO;
209 	}
210 
211 	pr_info("hvc%d: %s protocol on %pOF%s\n", termno,
212 		proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
213 		dev->dev.of_node,
214 		boot ? " (boot console)" : "");
215 
216 	if (irq < 0) {
217 		pr_info("hvc%d: No interrupts property, using OPAL event\n",
218 				termno);
219 		irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
220 	}
221 
222 	if (!irq) {
223 		pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
224 			dev->dev.of_node);
225 		return irq;
226 	}
227 
228 	hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
229 	if (IS_ERR(hp))
230 		return PTR_ERR(hp);
231 
232 	/* hvc consoles on powernv may need to share a single irq */
233 	hp->flags = IRQF_SHARED;
234 	dev_set_drvdata(&dev->dev, hp);
235 
236 	return 0;
237 }
238 
239 static void hvc_opal_remove(struct platform_device *dev)
240 {
241 	struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
242 	int termno;
243 
244 	termno = hp->vtermno;
245 	hvc_remove(hp);
246 	if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
247 		kfree(hvc_opal_privs[termno]);
248 	hvc_opal_privs[termno] = NULL;
249 }
250 
251 static struct platform_driver hvc_opal_driver = {
252 	.probe		= hvc_opal_probe,
253 	.remove		= hvc_opal_remove,
254 	.driver		= {
255 		.name	= hvc_opal_name,
256 		.of_match_table	= hvc_opal_match,
257 	}
258 };
259 
260 static int __init hvc_opal_init(void)
261 {
262 	if (!firmware_has_feature(FW_FEATURE_OPAL))
263 		return -ENODEV;
264 
265 	/* Register as a vio device to receive callbacks */
266 	return platform_driver_register(&hvc_opal_driver);
267 }
268 device_initcall(hvc_opal_init);
269 
270 static void udbg_opal_putc(char c)
271 {
272 	unsigned int termno = hvc_opal_boot_termno;
273 	int count = -1;
274 
275 	if (c == '\n')
276 		udbg_opal_putc('\r');
277 
278 	do {
279 		switch(hvc_opal_boot_priv.proto) {
280 		case HV_PROTOCOL_RAW:
281 			count = opal_put_chars(termno, &c, 1);
282 			break;
283 		case HV_PROTOCOL_HVSI:
284 			count = hvc_opal_hvsi_put_chars(termno, &c, 1);
285 			break;
286 		}
287 
288 		/* This is needed for the cosole to flush
289 		 * when there aren't any interrupts.
290 		 */
291 		opal_flush_console(termno);
292 	} while(count == 0 || count == -EAGAIN);
293 }
294 
295 static int udbg_opal_getc_poll(void)
296 {
297 	unsigned int termno = hvc_opal_boot_termno;
298 	int rc = 0;
299 	char c;
300 
301 	switch(hvc_opal_boot_priv.proto) {
302 	case HV_PROTOCOL_RAW:
303 		rc = opal_get_chars(termno, &c, 1);
304 		break;
305 	case HV_PROTOCOL_HVSI:
306 		rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
307 		break;
308 	}
309 	if (!rc)
310 		return -1;
311 	return c;
312 }
313 
314 static int udbg_opal_getc(void)
315 {
316 	int ch;
317 	for (;;) {
318 		ch = udbg_opal_getc_poll();
319 		if (ch != -1)
320 			return ch;
321 	}
322 }
323 
324 static void udbg_init_opal_common(void)
325 {
326 	udbg_putc = udbg_opal_putc;
327 	udbg_getc = udbg_opal_getc;
328 	udbg_getc_poll = udbg_opal_getc_poll;
329 }
330 
331 void __init hvc_opal_init_early(void)
332 {
333 	struct device_node *stdout_node = of_node_get(of_stdout);
334 	const __be32 *termno;
335 	const struct hv_ops *ops;
336 	u32 index;
337 
338 	/* If the console wasn't in /chosen, try /ibm,opal */
339 	if (!stdout_node) {
340 		struct device_node *opal, *np;
341 
342 		/* Current OPAL takeover doesn't provide the stdout
343 		 * path, so we hard wire it
344 		 */
345 		opal = of_find_node_by_path("/ibm,opal/consoles");
346 		if (opal) {
347 			pr_devel("hvc_opal: Found consoles in new location\n");
348 		} else {
349 			opal = of_find_node_by_path("/ibm,opal");
350 			if (opal)
351 				pr_devel("hvc_opal: "
352 					 "Found consoles in old location\n");
353 		}
354 		if (!opal)
355 			return;
356 		for_each_child_of_node(opal, np) {
357 			if (of_node_name_eq(np, "serial")) {
358 				stdout_node = np;
359 				break;
360 			}
361 		}
362 		of_node_put(opal);
363 	}
364 	if (!stdout_node)
365 		return;
366 	termno = of_get_property(stdout_node, "reg", NULL);
367 	index = termno ? be32_to_cpup(termno) : 0;
368 	if (index >= MAX_NR_HVC_CONSOLES)
369 		return;
370 	hvc_opal_privs[index] = &hvc_opal_boot_priv;
371 
372 	/* Check the protocol */
373 	if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
374 		hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
375 		ops = &hvc_opal_raw_ops;
376 		pr_devel("hvc_opal: Found RAW console\n");
377 	}
378 	else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
379 		hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
380 		ops = &hvc_opal_hvsi_ops;
381 		hvsilib_init(&hvc_opal_boot_priv.hvsi,
382 			     opal_get_chars, opal_put_chars_atomic,
383 			     index, 1);
384 		/* HVSI, perform the handshake now */
385 		hvsilib_establish(&hvc_opal_boot_priv.hvsi);
386 		pr_devel("hvc_opal: Found HVSI console\n");
387 	} else
388 		goto out;
389 	hvc_opal_boot_termno = index;
390 	udbg_init_opal_common();
391 	add_preferred_console("hvc", index, NULL);
392 	hvc_instantiate(index, index, ops);
393 out:
394 	of_node_put(stdout_node);
395 }
396 
397 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
398 void __init udbg_init_debug_opal_raw(void)
399 {
400 	u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
401 	hvc_opal_privs[index] = &hvc_opal_boot_priv;
402 	hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
403 	hvc_opal_boot_termno = index;
404 	udbg_init_opal_common();
405 }
406 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
407 
408 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
409 void __init udbg_init_debug_opal_hvsi(void)
410 {
411 	u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
412 	hvc_opal_privs[index] = &hvc_opal_boot_priv;
413 	hvc_opal_boot_termno = index;
414 	udbg_init_opal_common();
415 	hvsilib_init(&hvc_opal_boot_priv.hvsi,
416 		     opal_get_chars, opal_put_chars_atomic,
417 		     index, 1);
418 	hvsilib_establish(&hvc_opal_boot_priv.hvsi);
419 }
420 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */
421