hvc_opal.c 10.0 KB

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