hvc_opal.c 9.9 KB

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