blob: feeccbaec4382c55c1583fcad49cf73264a407ce [file] [log] [blame]
Rusty Russelle2c97842007-07-26 10:41:03 -07001/*D:300
2 * The Guest console driver
Rusty Russell3f8c4d32007-07-19 01:49:27 -07003 *
Rusty Russelle2c97842007-07-26 10:41:03 -07004 * This is a trivial console driver: we use lguest's DMA mechanism to send
5 * bytes out, and register a DMA buffer to receive bytes in. It is assumed to
6 * be present and available from the very beginning of boot.
7 *
8 * Writing console drivers is one of the few remaining Dark Arts in Linux.
9 * Fortunately for us, the path of virtual consoles has been well-trodden by
10 * the PowerPC folks, who wrote "hvc_console.c" to generically support any
11 * virtual console. We use that infrastructure which only requires us to write
12 * the basic put_chars and get_chars functions and call the right register
13 * functions.
14 :*/
15
Rusty Russellf56a3842007-07-26 10:41:05 -070016/*M:002 The console can be flooded: while the Guest is processing input the
17 * Host can send more. Buffering in the Host could alleviate this, but it is a
18 * difficult problem in general. :*/
Rusty Russelle2c97842007-07-26 10:41:03 -070019/* Copyright (C) 2006 Rusty Russell, IBM Corporation
Rusty Russell3f8c4d32007-07-19 01:49:27 -070020 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 */
35#include <linux/err.h>
36#include <linux/init.h>
37#include <linux/lguest_bus.h>
38#include "hvc_console.h"
39
Rusty Russelle2c97842007-07-26 10:41:03 -070040/*D:340 This is our single console input buffer, with associated "struct
41 * lguest_dma" referring to it. Note the 0-terminated length array, and the
42 * use of physical address for the buffer itself. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070043static char inbuf[256];
44static struct lguest_dma cons_input = { .used_len = 0,
45 .addr[0] = __pa(inbuf),
46 .len[0] = sizeof(inbuf),
47 .len[1] = 0 };
48
Rusty Russelle2c97842007-07-26 10:41:03 -070049/*D:310 The put_chars() callback is pretty straightforward.
50 *
51 * First we put the pointer and length in a "struct lguest_dma": we only have
52 * one pointer, so we set the second length to 0. Then we use SEND_DMA to send
53 * the data to (Host) buffers attached to the console key. Usually a device's
54 * key is a physical address within the device's memory, but because the
55 * console device doesn't have any associated physical memory, we use the
56 * LGUEST_CONSOLE_DMA_KEY constant (aka 0). */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070057static int put_chars(u32 vtermno, const char *buf, int count)
58{
59 struct lguest_dma dma;
60
Rusty Russelle2c97842007-07-26 10:41:03 -070061 /* FIXME: DMA buffers in a "struct lguest_dma" are not allowed
62 * to go over page boundaries. This never seems to happen,
63 * but if it did we'd need to fix this code. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070064 dma.len[0] = count;
65 dma.len[1] = 0;
66 dma.addr[0] = __pa(buf);
67
68 lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
Rusty Russelle2c97842007-07-26 10:41:03 -070069 /* We're expected to return the amount of data we wrote: all of it. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070070 return count;
71}
72
Rusty Russelle2c97842007-07-26 10:41:03 -070073/*D:350 get_chars() is the callback from the hvc_console infrastructure when
74 * an interrupt is received.
75 *
76 * Firstly we see if our buffer has been filled: if not, we return. The rest
77 * of the code deals with the fact that the hvc_console() infrastructure only
78 * asks us for 16 bytes at a time. We keep a "cons_offset" variable for
79 * partially-read buffers. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070080static int get_chars(u32 vtermno, char *buf, int count)
81{
82 static int cons_offset;
83
Rusty Russelle2c97842007-07-26 10:41:03 -070084 /* Nothing left to see here... */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070085 if (!cons_input.used_len)
86 return 0;
87
Rusty Russelle2c97842007-07-26 10:41:03 -070088 /* You want more than we have to give? Well, try wanting less! */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070089 if (cons_input.used_len - cons_offset < count)
90 count = cons_input.used_len - cons_offset;
91
Rusty Russelle2c97842007-07-26 10:41:03 -070092 /* Copy across to their buffer and increment offset. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070093 memcpy(buf, inbuf + cons_offset, count);
94 cons_offset += count;
Rusty Russelle2c97842007-07-26 10:41:03 -070095
96 /* Finished? Zero offset, and reset cons_input so Host will use it
97 * again. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -070098 if (cons_offset == cons_input.used_len) {
99 cons_offset = 0;
100 cons_input.used_len = 0;
101 }
102 return count;
103}
Rusty Russelle2c97842007-07-26 10:41:03 -0700104/*:*/
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700105
106static struct hv_ops lguest_cons = {
107 .get_chars = get_chars,
108 .put_chars = put_chars,
109};
110
Rusty Russelle2c97842007-07-26 10:41:03 -0700111/*D:320 Console drivers are initialized very early so boot messages can go
112 * out. At this stage, the console is output-only. Our driver checks we're a
113 * Guest, and if so hands hvc_instantiate() the console number (0), priority
114 * (0), and the struct hv_ops containing the put_chars() function. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700115static int __init cons_init(void)
116{
117 if (strcmp(paravirt_ops.name, "lguest") != 0)
118 return 0;
119
120 return hvc_instantiate(0, 0, &lguest_cons);
121}
122console_initcall(cons_init);
123
Rusty Russelle2c97842007-07-26 10:41:03 -0700124/*D:370 To set up and manage our virtual console, we call hvc_alloc() and
125 * stash the result in the private pointer of the "struct lguest_device".
126 * Since we never remove the console device we never need this pointer again,
127 * but using ->private is considered good form, and you never know who's going
128 * to copy your driver.
129 *
130 * Once the console is set up, we bind our input buffer ready for input. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700131static int lguestcons_probe(struct lguest_device *lgdev)
132{
133 int err;
134
Rusty Russelle2c97842007-07-26 10:41:03 -0700135 /* The first argument of hvc_alloc() is the virtual console number, so
136 * we use zero. The second argument is the interrupt number.
137 *
138 * The third argument is a "struct hv_ops" containing the put_chars()
139 * and get_chars() pointers. The final argument is the output buffer
140 * size: we use 256 and expect the Host to have room for us to send
141 * that much. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700142 lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
143 if (IS_ERR(lgdev->private))
144 return PTR_ERR(lgdev->private);
145
Rusty Russelle2c97842007-07-26 10:41:03 -0700146 /* We bind a single DMA buffer at key LGUEST_CONSOLE_DMA_KEY.
147 * "cons_input" is that statically-initialized global DMA buffer we saw
148 * above, and we also give the interrupt we want. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700149 err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
150 lgdev_irq(lgdev));
151 if (err)
152 printk("lguest console: failed to bind buffer.\n");
153 return err;
154}
Rusty Russelle2c97842007-07-26 10:41:03 -0700155/* Note the use of lgdev_irq() for the interrupt number. We tell hvc_alloc()
156 * to expect input when this interrupt is triggered, and then tell
157 * lguest_bind_dma() that is the interrupt to send us when input comes in. */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700158
Rusty Russelle2c97842007-07-26 10:41:03 -0700159/*D:360 From now on the console driver follows standard Guest driver form:
160 * register_lguest_driver() registers the device type and probe function, and
161 * the probe function sets up the device.
162 *
163 * The standard "struct lguest_driver": */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700164static struct lguest_driver lguestcons_drv = {
165 .name = "lguestcons",
166 .owner = THIS_MODULE,
167 .device_type = LGUEST_DEVICE_T_CONSOLE,
168 .probe = lguestcons_probe,
169};
170
Rusty Russelle2c97842007-07-26 10:41:03 -0700171/* The standard init function */
Rusty Russell3f8c4d32007-07-19 01:49:27 -0700172static int __init hvc_lguest_init(void)
173{
174 return register_lguest_driver(&lguestcons_drv);
175}
176module_init(hvc_lguest_init);