virtio_console.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. /* The IO vqs for this port */
  90. struct virtqueue *in_vq, *out_vq;
  91. /*
  92. * The entries in this struct will be valid if this port is
  93. * hooked up to an hvc console
  94. */
  95. struct console cons;
  96. };
  97. /* This is the very early arch-specified put chars function. */
  98. static int (*early_put_chars)(u32, const char *, int);
  99. static struct port *find_port_by_vtermno(u32 vtermno)
  100. {
  101. struct port *port;
  102. struct console *cons;
  103. unsigned long flags;
  104. spin_lock_irqsave(&pdrvdata_lock, flags);
  105. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  106. if (cons->vtermno == vtermno) {
  107. port = container_of(cons, struct port, cons);
  108. goto out;
  109. }
  110. }
  111. port = NULL;
  112. out:
  113. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  114. return port;
  115. }
  116. static void free_buf(struct port_buffer *buf)
  117. {
  118. kfree(buf->buf);
  119. kfree(buf);
  120. }
  121. static struct port_buffer *alloc_buf(size_t buf_size)
  122. {
  123. struct port_buffer *buf;
  124. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  125. if (!buf)
  126. goto fail;
  127. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  128. if (!buf->buf)
  129. goto free_buf;
  130. buf->len = 0;
  131. buf->offset = 0;
  132. buf->size = buf_size;
  133. return buf;
  134. free_buf:
  135. kfree(buf);
  136. fail:
  137. return NULL;
  138. }
  139. /* Callers should take appropriate locks */
  140. static void *get_inbuf(struct port *port)
  141. {
  142. struct port_buffer *buf;
  143. struct virtqueue *vq;
  144. unsigned int len;
  145. vq = port->in_vq;
  146. buf = vq->vq_ops->get_buf(vq, &len);
  147. if (buf) {
  148. buf->len = len;
  149. buf->offset = 0;
  150. }
  151. return buf;
  152. }
  153. /*
  154. * Create a scatter-gather list representing our input buffer and put
  155. * it in the queue.
  156. *
  157. * Callers should take appropriate locks.
  158. */
  159. static void add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  160. {
  161. struct scatterlist sg[1];
  162. sg_init_one(sg, buf->buf, buf->size);
  163. if (vq->vq_ops->add_buf(vq, sg, 0, 1, buf) < 0)
  164. BUG();
  165. vq->vq_ops->kick(vq);
  166. }
  167. /*
  168. * The put_chars() callback is pretty straightforward.
  169. *
  170. * We turn the characters into a scatter-gather list, add it to the
  171. * output queue and then kick the Host. Then we sit here waiting for
  172. * it to finish: inefficient in theory, but in practice
  173. * implementations will do it immediately (lguest's Launcher does).
  174. */
  175. static int put_chars(u32 vtermno, const char *buf, int count)
  176. {
  177. struct scatterlist sg[1];
  178. struct port *port;
  179. struct virtqueue *out_vq;
  180. unsigned int len;
  181. port = find_port_by_vtermno(vtermno);
  182. if (!port)
  183. return 0;
  184. if (unlikely(early_put_chars))
  185. return early_put_chars(vtermno, buf, count);
  186. out_vq = port->out_vq;
  187. /* This is a convenient routine to initialize a single-elem sg list */
  188. sg_init_one(sg, buf, count);
  189. /* This shouldn't fail: if it does, we lose chars. */
  190. if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) {
  191. /* Tell Host to go! */
  192. out_vq->vq_ops->kick(out_vq);
  193. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  194. cpu_relax();
  195. }
  196. /* We're expected to return the amount of data we wrote: all of it. */
  197. return count;
  198. }
  199. /*
  200. * get_chars() is the callback from the hvc_console infrastructure
  201. * when an interrupt is received.
  202. *
  203. * Most of the code deals with the fact that the hvc_console()
  204. * infrastructure only asks us for 16 bytes at a time. We keep
  205. * in_offset and in_used fields for partially-filled buffers.
  206. */
  207. static int get_chars(u32 vtermno, char *buf, int count)
  208. {
  209. struct port *port;
  210. port = find_port_by_vtermno(vtermno);
  211. if (!port)
  212. return 0;
  213. /* If we don't have an input queue yet, we can't get input. */
  214. BUG_ON(!port->in_vq);
  215. /* No more in buffer? See if they've (re)used it. */
  216. if (port->inbuf->offset == port->inbuf->len) {
  217. if (!get_inbuf(port))
  218. return 0;
  219. }
  220. /* You want more than we have to give? Well, try wanting less! */
  221. if (port->inbuf->offset + count > port->inbuf->len)
  222. count = port->inbuf->len - port->inbuf->offset;
  223. /* Copy across to their buffer and increment offset. */
  224. memcpy(buf, port->inbuf->buf + port->inbuf->offset, count);
  225. port->inbuf->offset += count;
  226. /* Finished? Re-register buffer so Host will use it again. */
  227. if (port->inbuf->offset == port->inbuf->len)
  228. add_inbuf(port->in_vq, port->inbuf);
  229. return count;
  230. }
  231. static void resize_console(struct port *port)
  232. {
  233. struct virtio_device *vdev;
  234. struct winsize ws;
  235. vdev = port->portdev->vdev;
  236. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  237. vdev->config->get(vdev,
  238. offsetof(struct virtio_console_config, cols),
  239. &ws.ws_col, sizeof(u16));
  240. vdev->config->get(vdev,
  241. offsetof(struct virtio_console_config, rows),
  242. &ws.ws_row, sizeof(u16));
  243. hvc_resize(port->cons.hvc, ws);
  244. }
  245. }
  246. static void virtcons_apply_config(struct virtio_device *vdev)
  247. {
  248. resize_console(find_port_by_vtermno(0));
  249. }
  250. /* We set the configuration at this point, since we now have a tty */
  251. static int notifier_add_vio(struct hvc_struct *hp, int data)
  252. {
  253. struct port *port;
  254. port = find_port_by_vtermno(hp->vtermno);
  255. if (!port)
  256. return -EINVAL;
  257. hp->irq_requested = 1;
  258. resize_console(port);
  259. return 0;
  260. }
  261. static void notifier_del_vio(struct hvc_struct *hp, int data)
  262. {
  263. hp->irq_requested = 0;
  264. }
  265. static void hvc_handle_input(struct virtqueue *vq)
  266. {
  267. struct console *cons;
  268. bool activity = false;
  269. list_for_each_entry(cons, &pdrvdata.consoles, list)
  270. activity |= hvc_poll(cons->hvc);
  271. if (activity)
  272. hvc_kick();
  273. }
  274. /* The operations for the console. */
  275. static const struct hv_ops hv_ops = {
  276. .get_chars = get_chars,
  277. .put_chars = put_chars,
  278. .notifier_add = notifier_add_vio,
  279. .notifier_del = notifier_del_vio,
  280. .notifier_hangup = notifier_del_vio,
  281. };
  282. /*
  283. * Console drivers are initialized very early so boot messages can go
  284. * out, so we do things slightly differently from the generic virtio
  285. * initialization of the net and block drivers.
  286. *
  287. * At this stage, the console is output-only. It's too early to set
  288. * up a virtqueue, so we let the drivers do some boutique early-output
  289. * thing.
  290. */
  291. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  292. {
  293. early_put_chars = put_chars;
  294. return hvc_instantiate(0, 0, &hv_ops);
  295. }
  296. int __devinit init_port_console(struct port *port)
  297. {
  298. int ret;
  299. /*
  300. * The Host's telling us this port is a console port. Hook it
  301. * up with an hvc console.
  302. *
  303. * To set up and manage our virtual console, we call
  304. * hvc_alloc().
  305. *
  306. * The first argument of hvc_alloc() is the virtual console
  307. * number. The second argument is the parameter for the
  308. * notification mechanism (like irq number). We currently
  309. * leave this as zero, virtqueues have implicit notifications.
  310. *
  311. * The third argument is a "struct hv_ops" containing the
  312. * put_chars() get_chars(), notifier_add() and notifier_del()
  313. * pointers. The final argument is the output buffer size: we
  314. * can do any size, so we put PAGE_SIZE here.
  315. */
  316. port->cons.vtermno = pdrvdata.next_vtermno;
  317. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  318. if (IS_ERR(port->cons.hvc)) {
  319. ret = PTR_ERR(port->cons.hvc);
  320. port->cons.hvc = NULL;
  321. return ret;
  322. }
  323. spin_lock_irq(&pdrvdata_lock);
  324. pdrvdata.next_vtermno++;
  325. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  326. spin_unlock_irq(&pdrvdata_lock);
  327. return 0;
  328. }
  329. static int __devinit add_port(struct ports_device *portdev)
  330. {
  331. struct port *port;
  332. int err;
  333. port = kmalloc(sizeof(*port), GFP_KERNEL);
  334. if (!port) {
  335. err = -ENOMEM;
  336. goto fail;
  337. }
  338. port->portdev = portdev;
  339. port->in_vq = portdev->in_vqs[0];
  340. port->out_vq = portdev->out_vqs[0];
  341. port->inbuf = alloc_buf(PAGE_SIZE);
  342. if (!port->inbuf) {
  343. err = -ENOMEM;
  344. goto free_port;
  345. }
  346. err = init_port_console(port);
  347. if (err)
  348. goto free_inbuf;
  349. /* Register the input buffer the first time. */
  350. add_inbuf(port->in_vq, port->inbuf);
  351. return 0;
  352. free_inbuf:
  353. free_buf(port->inbuf);
  354. free_port:
  355. kfree(port);
  356. fail:
  357. return err;
  358. }
  359. static int init_vqs(struct ports_device *portdev)
  360. {
  361. vq_callback_t **io_callbacks;
  362. char **io_names;
  363. struct virtqueue **vqs;
  364. u32 nr_ports, nr_queues;
  365. int err;
  366. /* We currently only have one port and two queues for that port */
  367. nr_ports = 1;
  368. nr_queues = 2;
  369. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  370. if (!vqs) {
  371. err = -ENOMEM;
  372. goto fail;
  373. }
  374. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  375. if (!io_callbacks) {
  376. err = -ENOMEM;
  377. goto free_vqs;
  378. }
  379. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  380. if (!io_names) {
  381. err = -ENOMEM;
  382. goto free_callbacks;
  383. }
  384. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  385. GFP_KERNEL);
  386. if (!portdev->in_vqs) {
  387. err = -ENOMEM;
  388. goto free_names;
  389. }
  390. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  391. GFP_KERNEL);
  392. if (!portdev->out_vqs) {
  393. err = -ENOMEM;
  394. goto free_invqs;
  395. }
  396. io_callbacks[0] = hvc_handle_input;
  397. io_callbacks[1] = NULL;
  398. io_names[0] = "input";
  399. io_names[1] = "output";
  400. /* Find the queues. */
  401. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  402. io_callbacks,
  403. (const char **)io_names);
  404. if (err)
  405. goto free_outvqs;
  406. portdev->in_vqs[0] = vqs[0];
  407. portdev->out_vqs[0] = vqs[1];
  408. kfree(io_callbacks);
  409. kfree(io_names);
  410. kfree(vqs);
  411. return 0;
  412. free_names:
  413. kfree(io_names);
  414. free_callbacks:
  415. kfree(io_callbacks);
  416. free_outvqs:
  417. kfree(portdev->out_vqs);
  418. free_invqs:
  419. kfree(portdev->in_vqs);
  420. free_vqs:
  421. kfree(vqs);
  422. fail:
  423. return err;
  424. }
  425. /*
  426. * Once we're further in boot, we get probed like any other virtio
  427. * device.
  428. */
  429. static int __devinit virtcons_probe(struct virtio_device *vdev)
  430. {
  431. struct ports_device *portdev;
  432. int err;
  433. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  434. if (!portdev) {
  435. err = -ENOMEM;
  436. goto fail;
  437. }
  438. /* Attach this portdev to this virtio_device, and vice-versa. */
  439. portdev->vdev = vdev;
  440. vdev->priv = portdev;
  441. err = init_vqs(portdev);
  442. if (err < 0) {
  443. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  444. goto free;
  445. }
  446. /* We only have one port. */
  447. err = add_port(portdev);
  448. if (err)
  449. goto free_vqs;
  450. /* Start using the new console output. */
  451. early_put_chars = NULL;
  452. return 0;
  453. free_vqs:
  454. vdev->config->del_vqs(vdev);
  455. kfree(portdev->in_vqs);
  456. kfree(portdev->out_vqs);
  457. free:
  458. kfree(portdev);
  459. fail:
  460. return err;
  461. }
  462. static struct virtio_device_id id_table[] = {
  463. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  464. { 0 },
  465. };
  466. static unsigned int features[] = {
  467. VIRTIO_CONSOLE_F_SIZE,
  468. };
  469. static struct virtio_driver virtio_console = {
  470. .feature_table = features,
  471. .feature_table_size = ARRAY_SIZE(features),
  472. .driver.name = KBUILD_MODNAME,
  473. .driver.owner = THIS_MODULE,
  474. .id_table = id_table,
  475. .probe = virtcons_probe,
  476. .config_changed = virtcons_apply_config,
  477. };
  478. static int __init init(void)
  479. {
  480. INIT_LIST_HEAD(&pdrvdata.consoles);
  481. return register_virtio_driver(&virtio_console);
  482. }
  483. module_init(init);
  484. MODULE_DEVICE_TABLE(virtio, id_table);
  485. MODULE_DESCRIPTION("Virtio console driver");
  486. MODULE_LICENSE("GPL");