virtio_console.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/virtio.h>
  23. #include <linux/virtio_console.h>
  24. #include "hvc_console.h"
  25. /*
  26. * This is a global struct for storing common data for all the devices
  27. * this driver handles.
  28. *
  29. * Mainly, it has a linked list for all the consoles in one place so
  30. * that callbacks from hvc for get_chars(), put_chars() work properly
  31. * across multiple devices and multiple ports per device.
  32. */
  33. struct ports_driver_data {
  34. /*
  35. * This is used to keep track of the number of hvc consoles
  36. * spawned by this driver. This number is given as the first
  37. * argument to hvc_alloc(). To correctly map an initial
  38. * console spawned via hvc_instantiate to the console being
  39. * hooked up via hvc_alloc, we need to pass the same vtermno.
  40. *
  41. * We also just assume the first console being initialised was
  42. * the first one that got used as the initial console.
  43. */
  44. unsigned int next_vtermno;
  45. /* All the console devices handled by this driver */
  46. struct list_head consoles;
  47. };
  48. static struct ports_driver_data pdrvdata;
  49. DEFINE_SPINLOCK(pdrvdata_lock);
  50. /* This struct holds information that's relevant only for console ports */
  51. struct console {
  52. /* We'll place all consoles in a list in the pdrvdata struct */
  53. struct list_head list;
  54. /* The hvc device associated with this console port */
  55. struct hvc_struct *hvc;
  56. /*
  57. * This number identifies the number that we used to register
  58. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  59. * number passed on by the hvc callbacks to us to
  60. * differentiate between the other console ports handled by
  61. * this driver
  62. */
  63. u32 vtermno;
  64. };
  65. /*
  66. * This is a per-device struct that stores data common to all the
  67. * ports for that device (vdev->priv).
  68. */
  69. struct ports_device {
  70. /* Array of per-port IO virtqueues */
  71. struct virtqueue **in_vqs, **out_vqs;
  72. struct virtio_device *vdev;
  73. };
  74. struct port_buffer {
  75. char *buf;
  76. /* size of the buffer in *buf above */
  77. size_t size;
  78. /* used length of the buffer */
  79. size_t len;
  80. /* offset in the buf from which to consume data */
  81. size_t offset;
  82. };
  83. /* This struct holds the per-port data */
  84. struct port {
  85. /* Pointer to the parent virtio_console device */
  86. struct ports_device *portdev;
  87. /* The current buffer from which data has to be fed to readers */
  88. struct port_buffer *inbuf;
  89. /*
  90. * To protect the operations on the in_vq associated with this
  91. * port. Has to be a spinlock because it can be called from
  92. * interrupt context (get_char()).
  93. */
  94. spinlock_t inbuf_lock;
  95. /* The IO vqs for this port */
  96. struct virtqueue *in_vq, *out_vq;
  97. /*
  98. * The entries in this struct will be valid if this port is
  99. * hooked up to an hvc console
  100. */
  101. struct console cons;
  102. };
  103. /* This is the very early arch-specified put chars function. */
  104. static int (*early_put_chars)(u32, const char *, int);
  105. static struct port *find_port_by_vtermno(u32 vtermno)
  106. {
  107. struct port *port;
  108. struct console *cons;
  109. unsigned long flags;
  110. spin_lock_irqsave(&pdrvdata_lock, flags);
  111. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  112. if (cons->vtermno == vtermno) {
  113. port = container_of(cons, struct port, cons);
  114. goto out;
  115. }
  116. }
  117. port = NULL;
  118. out:
  119. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  120. return port;
  121. }
  122. static struct port *find_port_by_vq(struct ports_device *portdev,
  123. struct virtqueue *vq)
  124. {
  125. struct port *port;
  126. struct console *cons;
  127. unsigned long flags;
  128. spin_lock_irqsave(&pdrvdata_lock, flags);
  129. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  130. port = container_of(cons, struct port, cons);
  131. if (port->in_vq == vq || port->out_vq == vq)
  132. goto out;
  133. }
  134. port = NULL;
  135. out:
  136. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  137. return port;
  138. }
  139. static void free_buf(struct port_buffer *buf)
  140. {
  141. kfree(buf->buf);
  142. kfree(buf);
  143. }
  144. static struct port_buffer *alloc_buf(size_t buf_size)
  145. {
  146. struct port_buffer *buf;
  147. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  148. if (!buf)
  149. goto fail;
  150. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  151. if (!buf->buf)
  152. goto free_buf;
  153. buf->len = 0;
  154. buf->offset = 0;
  155. buf->size = buf_size;
  156. return buf;
  157. free_buf:
  158. kfree(buf);
  159. fail:
  160. return NULL;
  161. }
  162. /* Callers should take appropriate locks */
  163. static void *get_inbuf(struct port *port)
  164. {
  165. struct port_buffer *buf;
  166. struct virtqueue *vq;
  167. unsigned int len;
  168. vq = port->in_vq;
  169. buf = vq->vq_ops->get_buf(vq, &len);
  170. if (buf) {
  171. buf->len = len;
  172. buf->offset = 0;
  173. }
  174. return buf;
  175. }
  176. /*
  177. * Create a scatter-gather list representing our input buffer and put
  178. * it in the queue.
  179. *
  180. * Callers should take appropriate locks.
  181. */
  182. static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  183. {
  184. struct scatterlist sg[1];
  185. int ret;
  186. sg_init_one(sg, buf->buf, buf->size);
  187. ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
  188. vq->vq_ops->kick(vq);
  189. return ret;
  190. }
  191. static bool port_has_data(struct port *port)
  192. {
  193. unsigned long flags;
  194. bool ret;
  195. ret = false;
  196. spin_lock_irqsave(&port->inbuf_lock, flags);
  197. if (port->inbuf)
  198. ret = true;
  199. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  200. return ret;
  201. }
  202. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
  203. {
  204. struct scatterlist sg[1];
  205. struct virtqueue *out_vq;
  206. ssize_t ret;
  207. unsigned int len;
  208. out_vq = port->out_vq;
  209. sg_init_one(sg, in_buf, in_count);
  210. ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
  211. /* Tell Host to go! */
  212. out_vq->vq_ops->kick(out_vq);
  213. if (ret < 0) {
  214. len = 0;
  215. goto fail;
  216. }
  217. /*
  218. * Wait till the host acknowledges it pushed out the data we
  219. * sent. Also ensure we return to userspace the number of
  220. * bytes that were successfully consumed by the host.
  221. */
  222. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  223. cpu_relax();
  224. fail:
  225. /* We're expected to return the amount of data we wrote */
  226. return len;
  227. }
  228. /*
  229. * Give out the data that's requested from the buffer that we have
  230. * queued up.
  231. */
  232. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count)
  233. {
  234. struct port_buffer *buf;
  235. unsigned long flags;
  236. if (!out_count || !port_has_data(port))
  237. return 0;
  238. buf = port->inbuf;
  239. if (out_count > buf->len - buf->offset)
  240. out_count = buf->len - buf->offset;
  241. memcpy(out_buf, buf->buf + buf->offset, out_count);
  242. /* Return the number of bytes actually copied */
  243. buf->offset += out_count;
  244. if (buf->offset == buf->len) {
  245. /*
  246. * We're done using all the data in this buffer.
  247. * Re-queue so that the Host can send us more data.
  248. */
  249. spin_lock_irqsave(&port->inbuf_lock, flags);
  250. port->inbuf = NULL;
  251. if (add_inbuf(port->in_vq, buf) < 0)
  252. dev_warn(&port->portdev->vdev->dev, "failed add_buf\n");
  253. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  254. }
  255. return out_count;
  256. }
  257. /*
  258. * The put_chars() callback is pretty straightforward.
  259. *
  260. * We turn the characters into a scatter-gather list, add it to the
  261. * output queue and then kick the Host. Then we sit here waiting for
  262. * it to finish: inefficient in theory, but in practice
  263. * implementations will do it immediately (lguest's Launcher does).
  264. */
  265. static int put_chars(u32 vtermno, const char *buf, int count)
  266. {
  267. struct port *port;
  268. port = find_port_by_vtermno(vtermno);
  269. if (!port)
  270. return 0;
  271. if (unlikely(early_put_chars))
  272. return early_put_chars(vtermno, buf, count);
  273. return send_buf(port, (void *)buf, count);
  274. }
  275. /*
  276. * get_chars() is the callback from the hvc_console infrastructure
  277. * when an interrupt is received.
  278. *
  279. * We call out to fill_readbuf that gets us the required data from the
  280. * buffers that are queued up.
  281. */
  282. static int get_chars(u32 vtermno, char *buf, int count)
  283. {
  284. struct port *port;
  285. port = find_port_by_vtermno(vtermno);
  286. if (!port)
  287. return 0;
  288. /* If we don't have an input queue yet, we can't get input. */
  289. BUG_ON(!port->in_vq);
  290. return fill_readbuf(port, buf, count);
  291. }
  292. static void resize_console(struct port *port)
  293. {
  294. struct virtio_device *vdev;
  295. struct winsize ws;
  296. vdev = port->portdev->vdev;
  297. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  298. vdev->config->get(vdev,
  299. offsetof(struct virtio_console_config, cols),
  300. &ws.ws_col, sizeof(u16));
  301. vdev->config->get(vdev,
  302. offsetof(struct virtio_console_config, rows),
  303. &ws.ws_row, sizeof(u16));
  304. hvc_resize(port->cons.hvc, ws);
  305. }
  306. }
  307. static void virtcons_apply_config(struct virtio_device *vdev)
  308. {
  309. resize_console(find_port_by_vtermno(0));
  310. }
  311. /* We set the configuration at this point, since we now have a tty */
  312. static int notifier_add_vio(struct hvc_struct *hp, int data)
  313. {
  314. struct port *port;
  315. port = find_port_by_vtermno(hp->vtermno);
  316. if (!port)
  317. return -EINVAL;
  318. hp->irq_requested = 1;
  319. resize_console(port);
  320. return 0;
  321. }
  322. static void notifier_del_vio(struct hvc_struct *hp, int data)
  323. {
  324. hp->irq_requested = 0;
  325. }
  326. static void hvc_handle_input(struct virtqueue *vq)
  327. {
  328. struct port *port;
  329. unsigned long flags;
  330. port = find_port_by_vq(vq->vdev->priv, vq);
  331. if (!port)
  332. return;
  333. spin_lock_irqsave(&port->inbuf_lock, flags);
  334. port->inbuf = get_inbuf(port);
  335. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  336. if (hvc_poll(port->cons.hvc))
  337. hvc_kick();
  338. }
  339. /* The operations for the console. */
  340. static const struct hv_ops hv_ops = {
  341. .get_chars = get_chars,
  342. .put_chars = put_chars,
  343. .notifier_add = notifier_add_vio,
  344. .notifier_del = notifier_del_vio,
  345. .notifier_hangup = notifier_del_vio,
  346. };
  347. /*
  348. * Console drivers are initialized very early so boot messages can go
  349. * out, so we do things slightly differently from the generic virtio
  350. * initialization of the net and block drivers.
  351. *
  352. * At this stage, the console is output-only. It's too early to set
  353. * up a virtqueue, so we let the drivers do some boutique early-output
  354. * thing.
  355. */
  356. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  357. {
  358. early_put_chars = put_chars;
  359. return hvc_instantiate(0, 0, &hv_ops);
  360. }
  361. int __devinit init_port_console(struct port *port)
  362. {
  363. int ret;
  364. /*
  365. * The Host's telling us this port is a console port. Hook it
  366. * up with an hvc console.
  367. *
  368. * To set up and manage our virtual console, we call
  369. * hvc_alloc().
  370. *
  371. * The first argument of hvc_alloc() is the virtual console
  372. * number. The second argument is the parameter for the
  373. * notification mechanism (like irq number). We currently
  374. * leave this as zero, virtqueues have implicit notifications.
  375. *
  376. * The third argument is a "struct hv_ops" containing the
  377. * put_chars() get_chars(), notifier_add() and notifier_del()
  378. * pointers. The final argument is the output buffer size: we
  379. * can do any size, so we put PAGE_SIZE here.
  380. */
  381. port->cons.vtermno = pdrvdata.next_vtermno;
  382. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  383. if (IS_ERR(port->cons.hvc)) {
  384. ret = PTR_ERR(port->cons.hvc);
  385. port->cons.hvc = NULL;
  386. return ret;
  387. }
  388. spin_lock_irq(&pdrvdata_lock);
  389. pdrvdata.next_vtermno++;
  390. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  391. spin_unlock_irq(&pdrvdata_lock);
  392. return 0;
  393. }
  394. static int __devinit add_port(struct ports_device *portdev)
  395. {
  396. struct port *port;
  397. struct port_buffer *inbuf;
  398. int err;
  399. port = kmalloc(sizeof(*port), GFP_KERNEL);
  400. if (!port) {
  401. err = -ENOMEM;
  402. goto fail;
  403. }
  404. port->portdev = portdev;
  405. port->inbuf = NULL;
  406. port->in_vq = portdev->in_vqs[0];
  407. port->out_vq = portdev->out_vqs[0];
  408. spin_lock_init(&port->inbuf_lock);
  409. inbuf = alloc_buf(PAGE_SIZE);
  410. if (!inbuf) {
  411. err = -ENOMEM;
  412. goto free_port;
  413. }
  414. /* Register the input buffer the first time. */
  415. add_inbuf(port->in_vq, inbuf);
  416. err = init_port_console(port);
  417. if (err)
  418. goto free_inbuf;
  419. return 0;
  420. free_inbuf:
  421. free_buf(inbuf);
  422. free_port:
  423. kfree(port);
  424. fail:
  425. return err;
  426. }
  427. static int init_vqs(struct ports_device *portdev)
  428. {
  429. vq_callback_t **io_callbacks;
  430. char **io_names;
  431. struct virtqueue **vqs;
  432. u32 nr_ports, nr_queues;
  433. int err;
  434. /* We currently only have one port and two queues for that port */
  435. nr_ports = 1;
  436. nr_queues = 2;
  437. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  438. if (!vqs) {
  439. err = -ENOMEM;
  440. goto fail;
  441. }
  442. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  443. if (!io_callbacks) {
  444. err = -ENOMEM;
  445. goto free_vqs;
  446. }
  447. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  448. if (!io_names) {
  449. err = -ENOMEM;
  450. goto free_callbacks;
  451. }
  452. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  453. GFP_KERNEL);
  454. if (!portdev->in_vqs) {
  455. err = -ENOMEM;
  456. goto free_names;
  457. }
  458. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  459. GFP_KERNEL);
  460. if (!portdev->out_vqs) {
  461. err = -ENOMEM;
  462. goto free_invqs;
  463. }
  464. io_callbacks[0] = hvc_handle_input;
  465. io_callbacks[1] = NULL;
  466. io_names[0] = "input";
  467. io_names[1] = "output";
  468. /* Find the queues. */
  469. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  470. io_callbacks,
  471. (const char **)io_names);
  472. if (err)
  473. goto free_outvqs;
  474. portdev->in_vqs[0] = vqs[0];
  475. portdev->out_vqs[0] = vqs[1];
  476. kfree(io_callbacks);
  477. kfree(io_names);
  478. kfree(vqs);
  479. return 0;
  480. free_names:
  481. kfree(io_names);
  482. free_callbacks:
  483. kfree(io_callbacks);
  484. free_outvqs:
  485. kfree(portdev->out_vqs);
  486. free_invqs:
  487. kfree(portdev->in_vqs);
  488. free_vqs:
  489. kfree(vqs);
  490. fail:
  491. return err;
  492. }
  493. /*
  494. * Once we're further in boot, we get probed like any other virtio
  495. * device.
  496. */
  497. static int __devinit virtcons_probe(struct virtio_device *vdev)
  498. {
  499. struct ports_device *portdev;
  500. int err;
  501. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  502. if (!portdev) {
  503. err = -ENOMEM;
  504. goto fail;
  505. }
  506. /* Attach this portdev to this virtio_device, and vice-versa. */
  507. portdev->vdev = vdev;
  508. vdev->priv = portdev;
  509. err = init_vqs(portdev);
  510. if (err < 0) {
  511. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  512. goto free;
  513. }
  514. /* We only have one port. */
  515. err = add_port(portdev);
  516. if (err)
  517. goto free_vqs;
  518. /* Start using the new console output. */
  519. early_put_chars = NULL;
  520. return 0;
  521. free_vqs:
  522. vdev->config->del_vqs(vdev);
  523. kfree(portdev->in_vqs);
  524. kfree(portdev->out_vqs);
  525. free:
  526. kfree(portdev);
  527. fail:
  528. return err;
  529. }
  530. static struct virtio_device_id id_table[] = {
  531. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  532. { 0 },
  533. };
  534. static unsigned int features[] = {
  535. VIRTIO_CONSOLE_F_SIZE,
  536. };
  537. static struct virtio_driver virtio_console = {
  538. .feature_table = features,
  539. .feature_table_size = ARRAY_SIZE(features),
  540. .driver.name = KBUILD_MODNAME,
  541. .driver.owner = THIS_MODULE,
  542. .id_table = id_table,
  543. .probe = virtcons_probe,
  544. .config_changed = virtcons_apply_config,
  545. };
  546. static int __init init(void)
  547. {
  548. INIT_LIST_HEAD(&pdrvdata.consoles);
  549. return register_virtio_driver(&virtio_console);
  550. }
  551. module_init(init);
  552. MODULE_DEVICE_TABLE(virtio, id_table);
  553. MODULE_DESCRIPTION("Virtio console driver");
  554. MODULE_LICENSE("GPL");