]>
Commit | Line | Data |
---|---|---|
31610434 RR |
1 | /*D:300 |
2 | * The Guest console driver | |
3 | * | |
4 | * Writing console drivers is one of the few remaining Dark Arts in Linux. | |
5 | * Fortunately for us, the path of virtual consoles has been well-trodden by | |
6 | * the PowerPC folks, who wrote "hvc_console.c" to generically support any | |
7 | * virtual console. We use that infrastructure which only requires us to write | |
8 | * the basic put_chars and get_chars functions and call the right register | |
9 | * functions. | |
10 | :*/ | |
11 | ||
12 | /*M:002 The console can be flooded: while the Guest is processing input the | |
13 | * Host can send more. Buffering in the Host could alleviate this, but it is a | |
14 | * difficult problem in general. :*/ | |
15 | /* Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation; either version 2 of the License, or | |
20 | * (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with this program; if not, write to the Free Software | |
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
30 | */ | |
31 | #include <linux/err.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/virtio.h> | |
34 | #include <linux/virtio_console.h> | |
35 | #include "hvc_console.h" | |
36 | ||
37 | /*D:340 These represent our input and output console queues, and the virtio | |
38 | * operations for them. */ | |
39 | static struct virtqueue *in_vq, *out_vq; | |
40 | static struct virtio_device *vdev; | |
41 | ||
42 | /* This is our input buffer, and how much data is left in it. */ | |
43 | static unsigned int in_len; | |
44 | static char *in, *inbuf; | |
45 | ||
46 | /* The operations for our console. */ | |
47 | static struct hv_ops virtio_cons; | |
48 | ||
91fcad19 CB |
49 | /* The hvc device */ |
50 | static struct hvc_struct *hvc; | |
51 | ||
31610434 RR |
52 | /*D:310 The put_chars() callback is pretty straightforward. |
53 | * | |
54 | * We turn the characters into a scatter-gather list, add it to the output | |
55 | * queue and then kick the Host. Then we sit here waiting for it to finish: | |
56 | * inefficient in theory, but in practice implementations will do it | |
57 | * immediately (lguest's Launcher does). */ | |
58 | static int put_chars(u32 vtermno, const char *buf, int count) | |
59 | { | |
60 | struct scatterlist sg[1]; | |
61 | unsigned int len; | |
62 | ||
63 | /* This is a convenient routine to initialize a single-elem sg list */ | |
64 | sg_init_one(sg, buf, count); | |
65 | ||
66 | /* add_buf wants a token to identify this buffer: we hand it any | |
67 | * non-NULL pointer, since there's only ever one buffer. */ | |
68 | if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) { | |
69 | /* Tell Host to go! */ | |
70 | out_vq->vq_ops->kick(out_vq); | |
71 | /* Chill out until it's done with the buffer. */ | |
72 | while (!out_vq->vq_ops->get_buf(out_vq, &len)) | |
73 | cpu_relax(); | |
74 | } | |
75 | ||
76 | /* We're expected to return the amount of data we wrote: all of it. */ | |
77 | return count; | |
78 | } | |
79 | ||
80 | /* Create a scatter-gather list representing our input buffer and put it in the | |
81 | * queue. */ | |
82 | static void add_inbuf(void) | |
83 | { | |
84 | struct scatterlist sg[1]; | |
85 | sg_init_one(sg, inbuf, PAGE_SIZE); | |
86 | ||
87 | /* We should always be able to add one buffer to an empty queue. */ | |
88 | if (in_vq->vq_ops->add_buf(in_vq, sg, 0, 1, inbuf) != 0) | |
89 | BUG(); | |
90 | in_vq->vq_ops->kick(in_vq); | |
91 | } | |
92 | ||
93 | /*D:350 get_chars() is the callback from the hvc_console infrastructure when | |
94 | * an interrupt is received. | |
95 | * | |
96 | * Most of the code deals with the fact that the hvc_console() infrastructure | |
97 | * only asks us for 16 bytes at a time. We keep in_offset and in_used fields | |
98 | * for partially-filled buffers. */ | |
99 | static int get_chars(u32 vtermno, char *buf, int count) | |
100 | { | |
101 | /* If we don't have an input queue yet, we can't get input. */ | |
102 | BUG_ON(!in_vq); | |
103 | ||
104 | /* No buffer? Try to get one. */ | |
105 | if (!in_len) { | |
106 | in = in_vq->vq_ops->get_buf(in_vq, &in_len); | |
107 | if (!in) | |
108 | return 0; | |
109 | } | |
110 | ||
111 | /* You want more than we have to give? Well, try wanting less! */ | |
112 | if (in_len < count) | |
113 | count = in_len; | |
114 | ||
115 | /* Copy across to their buffer and increment offset. */ | |
116 | memcpy(buf, in, count); | |
117 | in += count; | |
118 | in_len -= count; | |
119 | ||
120 | /* Finished? Re-register buffer so Host will use it again. */ | |
121 | if (in_len == 0) | |
122 | add_inbuf(); | |
123 | ||
124 | return count; | |
125 | } | |
126 | /*:*/ | |
127 | ||
128 | /*D:320 Console drivers are initialized very early so boot messages can go out, | |
129 | * so we do things slightly differently from the generic virtio initialization | |
130 | * of the net and block drivers. | |
131 | * | |
132 | * At this stage, the console is output-only. It's too early to set up a | |
133 | * virtqueue, so we let the drivers do some boutique early-output thing. */ | |
134 | int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int)) | |
135 | { | |
136 | virtio_cons.put_chars = put_chars; | |
137 | return hvc_instantiate(0, 0, &virtio_cons); | |
138 | } | |
139 | ||
c2983458 CB |
140 | /* |
141 | * virtio console configuration. This supports: | |
142 | * - console resize | |
143 | */ | |
144 | static void virtcons_apply_config(struct virtio_device *dev) | |
145 | { | |
146 | struct winsize ws; | |
147 | ||
148 | if (virtio_has_feature(dev, VIRTIO_CONSOLE_F_SIZE)) { | |
149 | dev->config->get(dev, | |
150 | offsetof(struct virtio_console_config, cols), | |
151 | &ws.ws_col, sizeof(u16)); | |
152 | dev->config->get(dev, | |
153 | offsetof(struct virtio_console_config, rows), | |
154 | &ws.ws_row, sizeof(u16)); | |
155 | hvc_resize(hvc, ws); | |
156 | } | |
157 | } | |
158 | ||
91fcad19 CB |
159 | /* |
160 | * we support only one console, the hvc struct is a global var | |
c2983458 | 161 | * We set the configuration at this point, since we now have a tty |
91fcad19 CB |
162 | */ |
163 | static int notifier_add_vio(struct hvc_struct *hp, int data) | |
164 | { | |
165 | hp->irq_requested = 1; | |
c2983458 CB |
166 | virtcons_apply_config(vdev); |
167 | ||
91fcad19 CB |
168 | return 0; |
169 | } | |
170 | ||
171 | static void notifier_del_vio(struct hvc_struct *hp, int data) | |
172 | { | |
173 | hp->irq_requested = 0; | |
174 | } | |
175 | ||
176 | static void hvc_handle_input(struct virtqueue *vq) | |
177 | { | |
178 | if (hvc_poll(hvc)) | |
179 | hvc_kick(); | |
180 | } | |
181 | ||
31610434 RR |
182 | /*D:370 Once we're further in boot, we get probed like any other virtio device. |
183 | * At this stage we set up the output virtqueue. | |
184 | * | |
185 | * To set up and manage our virtual console, we call hvc_alloc(). Since we | |
186 | * never remove the console device we never need this pointer again. | |
187 | * | |
188 | * Finally we put our input buffer in the input queue, ready to receive. */ | |
139b8298 | 189 | static int __devinit virtcons_probe(struct virtio_device *dev) |
31610434 RR |
190 | { |
191 | int err; | |
31610434 RR |
192 | |
193 | vdev = dev; | |
194 | ||
195 | /* This is the scratch page we use to receive console input */ | |
196 | inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
197 | if (!inbuf) { | |
198 | err = -ENOMEM; | |
199 | goto fail; | |
200 | } | |
201 | ||
202 | /* Find the input queue. */ | |
203 | /* FIXME: This is why we want to wean off hvc: we do nothing | |
204 | * when input comes in. */ | |
9499f5e7 | 205 | in_vq = vdev->config->find_vq(vdev, 0, hvc_handle_input, "input"); |
31610434 RR |
206 | if (IS_ERR(in_vq)) { |
207 | err = PTR_ERR(in_vq); | |
208 | goto free; | |
209 | } | |
210 | ||
9499f5e7 | 211 | out_vq = vdev->config->find_vq(vdev, 1, NULL, "output"); |
31610434 RR |
212 | if (IS_ERR(out_vq)) { |
213 | err = PTR_ERR(out_vq); | |
214 | goto free_in_vq; | |
215 | } | |
216 | ||
217 | /* Start using the new console output. */ | |
218 | virtio_cons.get_chars = get_chars; | |
219 | virtio_cons.put_chars = put_chars; | |
91fcad19 CB |
220 | virtio_cons.notifier_add = notifier_add_vio; |
221 | virtio_cons.notifier_del = notifier_del_vio; | |
fc362e2e | 222 | virtio_cons.notifier_hangup = notifier_del_vio; |
31610434 RR |
223 | |
224 | /* The first argument of hvc_alloc() is the virtual console number, so | |
91fcad19 CB |
225 | * we use zero. The second argument is the parameter for the |
226 | * notification mechanism (like irq number). We currently leave this | |
227 | * as zero, virtqueues have implicit notifications. | |
31610434 RR |
228 | * |
229 | * The third argument is a "struct hv_ops" containing the put_chars() | |
91fcad19 CB |
230 | * get_chars(), notifier_add() and notifier_del() pointers. |
231 | * The final argument is the output buffer size: we can do any size, | |
232 | * so we put PAGE_SIZE here. */ | |
31610434 RR |
233 | hvc = hvc_alloc(0, 0, &virtio_cons, PAGE_SIZE); |
234 | if (IS_ERR(hvc)) { | |
235 | err = PTR_ERR(hvc); | |
236 | goto free_out_vq; | |
237 | } | |
238 | ||
239 | /* Register the input buffer the first time. */ | |
240 | add_inbuf(); | |
241 | return 0; | |
242 | ||
243 | free_out_vq: | |
244 | vdev->config->del_vq(out_vq); | |
245 | free_in_vq: | |
246 | vdev->config->del_vq(in_vq); | |
247 | free: | |
248 | kfree(inbuf); | |
249 | fail: | |
250 | return err; | |
251 | } | |
252 | ||
253 | static struct virtio_device_id id_table[] = { | |
254 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, | |
255 | { 0 }, | |
256 | }; | |
257 | ||
c2983458 CB |
258 | static unsigned int features[] = { |
259 | VIRTIO_CONSOLE_F_SIZE, | |
260 | }; | |
261 | ||
31610434 | 262 | static struct virtio_driver virtio_console = { |
c2983458 CB |
263 | .feature_table = features, |
264 | .feature_table_size = ARRAY_SIZE(features), | |
31610434 RR |
265 | .driver.name = KBUILD_MODNAME, |
266 | .driver.owner = THIS_MODULE, | |
267 | .id_table = id_table, | |
268 | .probe = virtcons_probe, | |
c2983458 | 269 | .config_changed = virtcons_apply_config, |
31610434 RR |
270 | }; |
271 | ||
272 | static int __init init(void) | |
273 | { | |
274 | return register_virtio_driver(&virtio_console); | |
275 | } | |
276 | module_init(init); | |
277 | ||
278 | MODULE_DEVICE_TABLE(virtio, id_table); | |
279 | MODULE_DESCRIPTION("Virtio console driver"); | |
280 | MODULE_LICENSE("GPL"); |