virtio_console.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. /*
  203. * Give out the data that's requested from the buffer that we have
  204. * queued up.
  205. */
  206. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count)
  207. {
  208. struct port_buffer *buf;
  209. unsigned long flags;
  210. if (!out_count || !port_has_data(port))
  211. return 0;
  212. buf = port->inbuf;
  213. if (out_count > buf->len - buf->offset)
  214. out_count = buf->len - buf->offset;
  215. memcpy(out_buf, buf->buf + buf->offset, out_count);
  216. /* Return the number of bytes actually copied */
  217. buf->offset += out_count;
  218. if (buf->offset == buf->len) {
  219. /*
  220. * We're done using all the data in this buffer.
  221. * Re-queue so that the Host can send us more data.
  222. */
  223. spin_lock_irqsave(&port->inbuf_lock, flags);
  224. port->inbuf = NULL;
  225. if (add_inbuf(port->in_vq, buf) < 0)
  226. dev_warn(&port->portdev->vdev->dev, "failed add_buf\n");
  227. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  228. }
  229. return out_count;
  230. }
  231. /*
  232. * The put_chars() callback is pretty straightforward.
  233. *
  234. * We turn the characters into a scatter-gather list, add it to the
  235. * output queue and then kick the Host. Then we sit here waiting for
  236. * it to finish: inefficient in theory, but in practice
  237. * implementations will do it immediately (lguest's Launcher does).
  238. */
  239. static int put_chars(u32 vtermno, const char *buf, int count)
  240. {
  241. struct scatterlist sg[1];
  242. struct port *port;
  243. struct virtqueue *out_vq;
  244. unsigned int len;
  245. port = find_port_by_vtermno(vtermno);
  246. if (!port)
  247. return 0;
  248. if (unlikely(early_put_chars))
  249. return early_put_chars(vtermno, buf, count);
  250. out_vq = port->out_vq;
  251. /* This is a convenient routine to initialize a single-elem sg list */
  252. sg_init_one(sg, buf, count);
  253. /* This shouldn't fail: if it does, we lose chars. */
  254. if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) {
  255. /* Tell Host to go! */
  256. out_vq->vq_ops->kick(out_vq);
  257. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  258. cpu_relax();
  259. }
  260. /* We're expected to return the amount of data we wrote: all of it. */
  261. return count;
  262. }
  263. /*
  264. * get_chars() is the callback from the hvc_console infrastructure
  265. * when an interrupt is received.
  266. *
  267. * We call out to fill_readbuf that gets us the required data from the
  268. * buffers that are queued up.
  269. */
  270. static int get_chars(u32 vtermno, char *buf, int count)
  271. {
  272. struct port *port;
  273. port = find_port_by_vtermno(vtermno);
  274. if (!port)
  275. return 0;
  276. /* If we don't have an input queue yet, we can't get input. */
  277. BUG_ON(!port->in_vq);
  278. return fill_readbuf(port, buf, count);
  279. }
  280. static void resize_console(struct port *port)
  281. {
  282. struct virtio_device *vdev;
  283. struct winsize ws;
  284. vdev = port->portdev->vdev;
  285. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  286. vdev->config->get(vdev,
  287. offsetof(struct virtio_console_config, cols),
  288. &ws.ws_col, sizeof(u16));
  289. vdev->config->get(vdev,
  290. offsetof(struct virtio_console_config, rows),
  291. &ws.ws_row, sizeof(u16));
  292. hvc_resize(port->cons.hvc, ws);
  293. }
  294. }
  295. static void virtcons_apply_config(struct virtio_device *vdev)
  296. {
  297. resize_console(find_port_by_vtermno(0));
  298. }
  299. /* We set the configuration at this point, since we now have a tty */
  300. static int notifier_add_vio(struct hvc_struct *hp, int data)
  301. {
  302. struct port *port;
  303. port = find_port_by_vtermno(hp->vtermno);
  304. if (!port)
  305. return -EINVAL;
  306. hp->irq_requested = 1;
  307. resize_console(port);
  308. return 0;
  309. }
  310. static void notifier_del_vio(struct hvc_struct *hp, int data)
  311. {
  312. hp->irq_requested = 0;
  313. }
  314. static void hvc_handle_input(struct virtqueue *vq)
  315. {
  316. struct port *port;
  317. unsigned long flags;
  318. port = find_port_by_vq(vq->vdev->priv, vq);
  319. if (!port)
  320. return;
  321. spin_lock_irqsave(&port->inbuf_lock, flags);
  322. port->inbuf = get_inbuf(port);
  323. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  324. if (hvc_poll(port->cons.hvc))
  325. hvc_kick();
  326. }
  327. /* The operations for the console. */
  328. static const struct hv_ops hv_ops = {
  329. .get_chars = get_chars,
  330. .put_chars = put_chars,
  331. .notifier_add = notifier_add_vio,
  332. .notifier_del = notifier_del_vio,
  333. .notifier_hangup = notifier_del_vio,
  334. };
  335. /*
  336. * Console drivers are initialized very early so boot messages can go
  337. * out, so we do things slightly differently from the generic virtio
  338. * initialization of the net and block drivers.
  339. *
  340. * At this stage, the console is output-only. It's too early to set
  341. * up a virtqueue, so we let the drivers do some boutique early-output
  342. * thing.
  343. */
  344. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  345. {
  346. early_put_chars = put_chars;
  347. return hvc_instantiate(0, 0, &hv_ops);
  348. }
  349. int __devinit init_port_console(struct port *port)
  350. {
  351. int ret;
  352. /*
  353. * The Host's telling us this port is a console port. Hook it
  354. * up with an hvc console.
  355. *
  356. * To set up and manage our virtual console, we call
  357. * hvc_alloc().
  358. *
  359. * The first argument of hvc_alloc() is the virtual console
  360. * number. The second argument is the parameter for the
  361. * notification mechanism (like irq number). We currently
  362. * leave this as zero, virtqueues have implicit notifications.
  363. *
  364. * The third argument is a "struct hv_ops" containing the
  365. * put_chars() get_chars(), notifier_add() and notifier_del()
  366. * pointers. The final argument is the output buffer size: we
  367. * can do any size, so we put PAGE_SIZE here.
  368. */
  369. port->cons.vtermno = pdrvdata.next_vtermno;
  370. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  371. if (IS_ERR(port->cons.hvc)) {
  372. ret = PTR_ERR(port->cons.hvc);
  373. port->cons.hvc = NULL;
  374. return ret;
  375. }
  376. spin_lock_irq(&pdrvdata_lock);
  377. pdrvdata.next_vtermno++;
  378. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  379. spin_unlock_irq(&pdrvdata_lock);
  380. return 0;
  381. }
  382. static int __devinit add_port(struct ports_device *portdev)
  383. {
  384. struct port *port;
  385. struct port_buffer *inbuf;
  386. int err;
  387. port = kmalloc(sizeof(*port), GFP_KERNEL);
  388. if (!port) {
  389. err = -ENOMEM;
  390. goto fail;
  391. }
  392. port->portdev = portdev;
  393. port->inbuf = NULL;
  394. port->in_vq = portdev->in_vqs[0];
  395. port->out_vq = portdev->out_vqs[0];
  396. spin_lock_init(&port->inbuf_lock);
  397. inbuf = alloc_buf(PAGE_SIZE);
  398. if (!inbuf) {
  399. err = -ENOMEM;
  400. goto free_port;
  401. }
  402. /* Register the input buffer the first time. */
  403. add_inbuf(port->in_vq, inbuf);
  404. err = init_port_console(port);
  405. if (err)
  406. goto free_inbuf;
  407. return 0;
  408. free_inbuf:
  409. free_buf(inbuf);
  410. free_port:
  411. kfree(port);
  412. fail:
  413. return err;
  414. }
  415. static int init_vqs(struct ports_device *portdev)
  416. {
  417. vq_callback_t **io_callbacks;
  418. char **io_names;
  419. struct virtqueue **vqs;
  420. u32 nr_ports, nr_queues;
  421. int err;
  422. /* We currently only have one port and two queues for that port */
  423. nr_ports = 1;
  424. nr_queues = 2;
  425. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  426. if (!vqs) {
  427. err = -ENOMEM;
  428. goto fail;
  429. }
  430. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  431. if (!io_callbacks) {
  432. err = -ENOMEM;
  433. goto free_vqs;
  434. }
  435. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  436. if (!io_names) {
  437. err = -ENOMEM;
  438. goto free_callbacks;
  439. }
  440. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  441. GFP_KERNEL);
  442. if (!portdev->in_vqs) {
  443. err = -ENOMEM;
  444. goto free_names;
  445. }
  446. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  447. GFP_KERNEL);
  448. if (!portdev->out_vqs) {
  449. err = -ENOMEM;
  450. goto free_invqs;
  451. }
  452. io_callbacks[0] = hvc_handle_input;
  453. io_callbacks[1] = NULL;
  454. io_names[0] = "input";
  455. io_names[1] = "output";
  456. /* Find the queues. */
  457. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  458. io_callbacks,
  459. (const char **)io_names);
  460. if (err)
  461. goto free_outvqs;
  462. portdev->in_vqs[0] = vqs[0];
  463. portdev->out_vqs[0] = vqs[1];
  464. kfree(io_callbacks);
  465. kfree(io_names);
  466. kfree(vqs);
  467. return 0;
  468. free_names:
  469. kfree(io_names);
  470. free_callbacks:
  471. kfree(io_callbacks);
  472. free_outvqs:
  473. kfree(portdev->out_vqs);
  474. free_invqs:
  475. kfree(portdev->in_vqs);
  476. free_vqs:
  477. kfree(vqs);
  478. fail:
  479. return err;
  480. }
  481. /*
  482. * Once we're further in boot, we get probed like any other virtio
  483. * device.
  484. */
  485. static int __devinit virtcons_probe(struct virtio_device *vdev)
  486. {
  487. struct ports_device *portdev;
  488. int err;
  489. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  490. if (!portdev) {
  491. err = -ENOMEM;
  492. goto fail;
  493. }
  494. /* Attach this portdev to this virtio_device, and vice-versa. */
  495. portdev->vdev = vdev;
  496. vdev->priv = portdev;
  497. err = init_vqs(portdev);
  498. if (err < 0) {
  499. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  500. goto free;
  501. }
  502. /* We only have one port. */
  503. err = add_port(portdev);
  504. if (err)
  505. goto free_vqs;
  506. /* Start using the new console output. */
  507. early_put_chars = NULL;
  508. return 0;
  509. free_vqs:
  510. vdev->config->del_vqs(vdev);
  511. kfree(portdev->in_vqs);
  512. kfree(portdev->out_vqs);
  513. free:
  514. kfree(portdev);
  515. fail:
  516. return err;
  517. }
  518. static struct virtio_device_id id_table[] = {
  519. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  520. { 0 },
  521. };
  522. static unsigned int features[] = {
  523. VIRTIO_CONSOLE_F_SIZE,
  524. };
  525. static struct virtio_driver virtio_console = {
  526. .feature_table = features,
  527. .feature_table_size = ARRAY_SIZE(features),
  528. .driver.name = KBUILD_MODNAME,
  529. .driver.owner = THIS_MODULE,
  530. .id_table = id_table,
  531. .probe = virtcons_probe,
  532. .config_changed = virtcons_apply_config,
  533. };
  534. static int __init init(void)
  535. {
  536. INIT_LIST_HEAD(&pdrvdata.consoles);
  537. return register_virtio_driver(&virtio_console);
  538. }
  539. module_init(init);
  540. MODULE_DEVICE_TABLE(virtio, id_table);
  541. MODULE_DESCRIPTION("Virtio console driver");
  542. MODULE_LICENSE("GPL");