blob: 944eaeb8e0cff62b89ea735b0d98e167ddd806d0 [file] [log] [blame]
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -07001/*
2 * xen console driver interface to hvc_console.c
3 *
4 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/types.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000026#include <linux/list.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070027
Stefano Stabellinieb5ef072012-01-27 18:31:36 +000028#include <asm/io.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070029#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070030
31#include <xen/xen.h>
Stefano Stabellinieb5ef072012-01-27 18:31:36 +000032#include <xen/interface/xen.h>
33#include <xen/hvm.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000034#include <xen/grant_table.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070035#include <xen/page.h>
36#include <xen/events.h>
37#include <xen/interface/io/console.h>
38#include <xen/hvc-console.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000039#include <xen/xenbus.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070040
41#include "hvc_console.h"
42
43#define HVC_COOKIE 0x58656e /* "Xen" in hex */
44
Stefano Stabellini02e19f92012-01-30 16:02:31 +000045struct xencons_info {
46 struct list_head list;
47 struct xenbus_device *xbdev;
48 struct xencons_interface *intf;
49 unsigned int evtchn;
50 struct hvc_struct *hvc;
51 int irq;
52 int vtermno;
53 grant_ref_t gntref;
54};
55
56static LIST_HEAD(xenconsoles);
57static DEFINE_SPINLOCK(xencons_lock);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070058
59/* ------------------------------------------------------------------ */
60
Stefano Stabellini02e19f92012-01-30 16:02:31 +000061static struct xencons_info *vtermno_to_xencons(int vtermno)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070062{
Stefano Stabellini02e19f92012-01-30 16:02:31 +000063 struct xencons_info *entry, *n, *ret = NULL;
64
65 if (list_empty(&xenconsoles))
66 return NULL;
67
68 list_for_each_entry_safe(entry, n, &xenconsoles, list) {
69 if (entry->vtermno == vtermno) {
70 ret = entry;
71 break;
72 }
73 }
74
75 return ret;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070076}
77
Stefano Stabellini02e19f92012-01-30 16:02:31 +000078static inline int xenbus_devid_to_vtermno(int devid)
79{
80 return devid + HVC_COOKIE;
81}
82
83static inline void notify_daemon(struct xencons_info *cons)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070084{
85 /* Use evtchn: this is called early, before irq is set up. */
Stefano Stabellini02e19f92012-01-30 16:02:31 +000086 notify_remote_via_evtchn(cons->evtchn);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070087}
88
Stefano Stabellini02e19f92012-01-30 16:02:31 +000089static int __write_console(struct xencons_info *xencons,
90 const char *data, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070091{
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070092 XENCONS_RING_IDX cons, prod;
Stefano Stabellini02e19f92012-01-30 16:02:31 +000093 struct xencons_interface *intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070094 int sent = 0;
95
96 cons = intf->out_cons;
97 prod = intf->out_prod;
98 mb(); /* update queue values before going on */
99 BUG_ON((prod - cons) > sizeof(intf->out));
100
101 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
102 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
103
104 wmb(); /* write ring before updating pointer */
105 intf->out_prod = prod;
106
Jeremy Fitzhardinge403a85f2010-10-14 11:38:47 -0700107 if (sent)
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000108 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700109 return sent;
110}
111
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100112static int domU_write_console(uint32_t vtermno, const char *data, int len)
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900113{
114 int ret = len;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000115 struct xencons_info *cons = vtermno_to_xencons(vtermno);
116 if (cons == NULL)
117 return -EINVAL;
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900118
119 /*
120 * Make sure the whole buffer is emitted, polling if
121 * necessary. We don't ever want to rely on the hvc daemon
122 * because the most interesting console output is when the
123 * kernel is crippled.
124 */
125 while (len) {
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000126 int sent = __write_console(cons, data, len);
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900127
128 data += sent;
129 len -= sent;
130
131 if (unlikely(len))
132 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
133 }
134
135 return ret;
136}
137
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100138static int domU_read_console(uint32_t vtermno, char *buf, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700139{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000140 struct xencons_interface *intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700141 XENCONS_RING_IDX cons, prod;
142 int recv = 0;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000143 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
144 if (xencons == NULL)
145 return -EINVAL;
146 intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700147
148 cons = intf->in_cons;
149 prod = intf->in_prod;
150 mb(); /* get pointers before reading ring */
151 BUG_ON((prod - cons) > sizeof(intf->in));
152
153 while (cons != prod && recv < len)
154 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
155
156 mb(); /* read ring before consuming */
157 intf->in_cons = cons;
158
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000159 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700160 return recv;
161}
162
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100163static struct hv_ops domU_hvc_ops = {
164 .get_chars = domU_read_console,
165 .put_chars = domU_write_console,
Christian Borntraeger611e0972008-06-20 15:24:08 +0200166 .notifier_add = notifier_add_irq,
167 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000168 .notifier_hangup = notifier_hangup_irq,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700169};
170
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100171static int dom0_read_console(uint32_t vtermno, char *buf, int len)
172{
173 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
174}
175
176/*
177 * Either for a dom0 to write to the system console, or a domU with a
178 * debug version of Xen
179 */
180static int dom0_write_console(uint32_t vtermno, const char *str, int len)
181{
182 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
183 if (rc < 0)
184 return 0;
185
186 return len;
187}
188
189static struct hv_ops dom0_hvc_ops = {
190 .get_chars = dom0_read_console,
191 .put_chars = dom0_write_console,
192 .notifier_add = notifier_add_irq,
193 .notifier_del = notifier_del_irq,
194 .notifier_hangup = notifier_hangup_irq,
195};
196
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000197static int xen_hvm_console_init(void)
198{
199 int r;
200 uint64_t v = 0;
201 unsigned long mfn;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000202 struct xencons_info *info;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000203
204 if (!xen_hvm_domain())
205 return -ENODEV;
206
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000207 info = vtermno_to_xencons(HVC_COOKIE);
208 if (!info) {
209 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
210 if (!info)
211 return -ENOMEM;
212 }
213
214 /* already configured */
215 if (info->intf != NULL)
216 return 0;
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400217 /*
218 * If the toolstack (or the hypervisor) hasn't set these values, the
219 * default value is 0. Even though mfn = 0 and evtchn = 0 are
220 * theoretically correct values, in practice they never are and they
221 * mean that a legacy toolstack hasn't initialized the pv console correctly.
222 */
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000223 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400224 if (r < 0 || v == 0)
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400225 goto err;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000226 info->evtchn = v;
Konrad Rzeszutek Wilka32c88b2012-05-23 12:55:38 -0400227 v = 0;
228 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400229 if (r < 0 || v == 0)
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400230 goto err;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000231 mfn = v;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000232 info->intf = ioremap(mfn << PAGE_SHIFT, PAGE_SIZE);
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400233 if (info->intf == NULL)
234 goto err;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000235 info->vtermno = HVC_COOKIE;
236
237 spin_lock(&xencons_lock);
238 list_add_tail(&info->list, &xenconsoles);
239 spin_unlock(&xencons_lock);
240
241 return 0;
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400242err:
243 kfree(info);
244 return -ENODEV;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000245}
246
247static int xen_pv_console_init(void)
248{
249 struct xencons_info *info;
250
251 if (!xen_pv_domain())
252 return -ENODEV;
253
254 if (!xen_start_info->console.domU.evtchn)
255 return -ENODEV;
256
257 info = vtermno_to_xencons(HVC_COOKIE);
258 if (!info) {
259 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
260 if (!info)
261 return -ENOMEM;
262 }
263
264 /* already configured */
265 if (info->intf != NULL)
266 return 0;
267
268 info->evtchn = xen_start_info->console.domU.evtchn;
269 info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
270 info->vtermno = HVC_COOKIE;
271
272 spin_lock(&xencons_lock);
273 list_add_tail(&info->list, &xenconsoles);
274 spin_unlock(&xencons_lock);
275
276 return 0;
277}
278
279static int xen_initial_domain_console_init(void)
280{
281 struct xencons_info *info;
282
283 if (!xen_initial_domain())
284 return -ENODEV;
285
286 info = vtermno_to_xencons(HVC_COOKIE);
287 if (!info) {
288 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
289 if (!info)
290 return -ENOMEM;
291 }
292
293 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
294 info->vtermno = HVC_COOKIE;
295
296 spin_lock(&xencons_lock);
297 list_add_tail(&info->list, &xenconsoles);
298 spin_unlock(&xencons_lock);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000299
300 return 0;
301}
302
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100303void xen_console_resume(void)
304{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000305 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
306 if (info != NULL && info->irq)
307 rebind_evtchn_irq(info->evtchn, info->irq);
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100308}
309
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000310static void xencons_disconnect_backend(struct xencons_info *info)
311{
312 if (info->irq > 0)
313 unbind_from_irqhandler(info->irq, NULL);
314 info->irq = 0;
315 if (info->evtchn > 0)
316 xenbus_free_evtchn(info->xbdev, info->evtchn);
317 info->evtchn = 0;
318 if (info->gntref > 0)
319 gnttab_free_grant_references(info->gntref);
320 info->gntref = 0;
321 if (info->hvc != NULL)
322 hvc_remove(info->hvc);
323 info->hvc = NULL;
324}
325
326static void xencons_free(struct xencons_info *info)
327{
328 free_page((unsigned long)info->intf);
329 info->intf = NULL;
330 info->vtermno = 0;
331 kfree(info);
332}
333
334static int xen_console_remove(struct xencons_info *info)
335{
336 xencons_disconnect_backend(info);
337 spin_lock(&xencons_lock);
338 list_del(&info->list);
339 spin_unlock(&xencons_lock);
340 if (info->xbdev != NULL)
341 xencons_free(info);
342 else {
343 if (xen_hvm_domain())
344 iounmap(info->intf);
345 kfree(info);
346 }
347 return 0;
348}
349
Stefano Stabellinicf8e0192012-02-21 11:30:42 +0000350#ifdef CONFIG_HVC_XEN_FRONTEND
351static struct xenbus_driver xencons_driver;
352
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000353static int xencons_remove(struct xenbus_device *dev)
354{
355 return xen_console_remove(dev_get_drvdata(&dev->dev));
356}
357
358static int xencons_connect_backend(struct xenbus_device *dev,
359 struct xencons_info *info)
360{
361 int ret, evtchn, devid, ref, irq;
362 struct xenbus_transaction xbt;
363 grant_ref_t gref_head;
364 unsigned long mfn;
365
366 ret = xenbus_alloc_evtchn(dev, &evtchn);
367 if (ret)
368 return ret;
369 info->evtchn = evtchn;
370 irq = bind_evtchn_to_irq(evtchn);
371 if (irq < 0)
372 return irq;
373 info->irq = irq;
374 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
375 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
376 irq, &domU_hvc_ops, 256);
377 if (IS_ERR(info->hvc))
378 return PTR_ERR(info->hvc);
379 if (xen_pv_domain())
380 mfn = virt_to_mfn(info->intf);
381 else
382 mfn = __pa(info->intf) >> PAGE_SHIFT;
383 ret = gnttab_alloc_grant_references(1, &gref_head);
384 if (ret < 0)
385 return ret;
386 info->gntref = gref_head;
387 ref = gnttab_claim_grant_reference(&gref_head);
388 if (ref < 0)
389 return ref;
390 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
391 mfn, 0);
392
393 again:
394 ret = xenbus_transaction_start(&xbt);
395 if (ret) {
396 xenbus_dev_fatal(dev, ret, "starting transaction");
397 return ret;
398 }
399 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
400 if (ret)
401 goto error_xenbus;
402 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
403 evtchn);
404 if (ret)
405 goto error_xenbus;
406 ret = xenbus_printf(xbt, dev->nodename, "type", "ioemu");
407 if (ret)
408 goto error_xenbus;
409 ret = xenbus_transaction_end(xbt, 0);
410 if (ret) {
411 if (ret == -EAGAIN)
412 goto again;
413 xenbus_dev_fatal(dev, ret, "completing transaction");
414 return ret;
415 }
416
417 xenbus_switch_state(dev, XenbusStateInitialised);
418 return 0;
419
420 error_xenbus:
421 xenbus_transaction_end(xbt, 1);
422 xenbus_dev_fatal(dev, ret, "writing xenstore");
423 return ret;
424}
425
426static int __devinit xencons_probe(struct xenbus_device *dev,
427 const struct xenbus_device_id *id)
428{
429 int ret, devid;
430 struct xencons_info *info;
431
432 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
433 if (devid == 0)
434 return -ENODEV;
435
Dan Carpenter201a52b2012-05-15 11:47:47 +0300436 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000437 if (!info)
Dan Carpenter201a52b2012-05-15 11:47:47 +0300438 return -ENOMEM;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000439 dev_set_drvdata(&dev->dev, info);
440 info->xbdev = dev;
441 info->vtermno = xenbus_devid_to_vtermno(devid);
442 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
443 if (!info->intf)
444 goto error_nomem;
445
446 ret = xencons_connect_backend(dev, info);
447 if (ret < 0)
448 goto error;
449 spin_lock(&xencons_lock);
450 list_add_tail(&info->list, &xenconsoles);
451 spin_unlock(&xencons_lock);
452
453 return 0;
454
455 error_nomem:
456 ret = -ENOMEM;
457 xenbus_dev_fatal(dev, ret, "allocating device memory");
458 error:
459 xencons_disconnect_backend(info);
460 xencons_free(info);
461 return ret;
462}
463
464static int xencons_resume(struct xenbus_device *dev)
465{
466 struct xencons_info *info = dev_get_drvdata(&dev->dev);
467
468 xencons_disconnect_backend(info);
469 memset(info->intf, 0, PAGE_SIZE);
470 return xencons_connect_backend(dev, info);
471}
472
473static void xencons_backend_changed(struct xenbus_device *dev,
474 enum xenbus_state backend_state)
475{
476 switch (backend_state) {
477 case XenbusStateReconfiguring:
478 case XenbusStateReconfigured:
479 case XenbusStateInitialising:
480 case XenbusStateInitialised:
481 case XenbusStateUnknown:
482 case XenbusStateClosed:
483 break;
484
485 case XenbusStateInitWait:
486 break;
487
488 case XenbusStateConnected:
489 xenbus_switch_state(dev, XenbusStateConnected);
490 break;
491
492 case XenbusStateClosing:
493 xenbus_frontend_closed(dev);
494 break;
495 }
496}
497
498static const struct xenbus_device_id xencons_ids[] = {
499 { "console" },
500 { "" }
501};
502
503
Stefano Stabellinicf8e0192012-02-21 11:30:42 +0000504static DEFINE_XENBUS_DRIVER(xencons, "xenconsole",
505 .probe = xencons_probe,
506 .remove = xencons_remove,
507 .resume = xencons_resume,
508 .otherend_changed = xencons_backend_changed,
509);
510#endif /* CONFIG_HVC_XEN_FRONTEND */
511
512static int __init xen_hvc_init(void)
513{
514 int r;
515 struct xencons_info *info;
516 const struct hv_ops *ops;
517
518 if (!xen_domain())
519 return -ENODEV;
520
521 if (xen_initial_domain()) {
522 ops = &dom0_hvc_ops;
523 r = xen_initial_domain_console_init();
524 if (r < 0)
525 return r;
526 info = vtermno_to_xencons(HVC_COOKIE);
527 } else {
528 ops = &domU_hvc_ops;
529 if (xen_hvm_domain())
530 r = xen_hvm_console_init();
531 else
532 r = xen_pv_console_init();
533 if (r < 0)
534 return r;
535
536 info = vtermno_to_xencons(HVC_COOKIE);
537 info->irq = bind_evtchn_to_irq(info->evtchn);
538 }
539 if (info->irq < 0)
540 info->irq = 0; /* NO_IRQ */
541 else
542 irq_set_noprobe(info->irq);
543
544 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
545 if (IS_ERR(info->hvc)) {
546 r = PTR_ERR(info->hvc);
547 spin_lock(&xencons_lock);
548 list_del(&info->list);
549 spin_unlock(&xencons_lock);
550 if (info->irq)
551 unbind_from_irqhandler(info->irq, NULL);
552 kfree(info);
553 return r;
554 }
555
556 r = 0;
557#ifdef CONFIG_HVC_XEN_FRONTEND
558 r = xenbus_register_frontend(&xencons_driver);
559#endif
560 return r;
561}
562
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100563static void __exit xen_hvc_fini(void)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700564{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000565 struct xencons_info *entry, *next;
566
567 if (list_empty(&xenconsoles))
568 return;
569
570 list_for_each_entry_safe(entry, next, &xenconsoles, list) {
571 xen_console_remove(entry);
572 }
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700573}
574
575static int xen_cons_init(void)
576{
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000577 const struct hv_ops *ops;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100578
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000579 if (!xen_domain())
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700580 return 0;
581
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100582 if (xen_initial_domain())
583 ops = &dom0_hvc_ops;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000584 else {
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000585 int r;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100586 ops = &domU_hvc_ops;
587
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000588 if (xen_hvm_domain())
589 r = xen_hvm_console_init();
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000590 else
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000591 r = xen_pv_console_init();
592 if (r < 0)
593 return r;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000594 }
595
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100596 hvc_instantiate(HVC_COOKIE, 0, ops);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700597 return 0;
598}
599
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000600
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100601module_init(xen_hvc_init);
602module_exit(xen_hvc_fini);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700603console_initcall(xen_cons_init);
604
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100605#ifdef CONFIG_EARLY_PRINTK
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700606static void xenboot_write_console(struct console *console, const char *string,
607 unsigned len)
608{
609 unsigned int linelen, off = 0;
610 const char *pos;
611
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000612 if (!xen_pv_domain())
613 return;
614
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100615 dom0_write_console(0, string, len);
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100616
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100617 if (xen_initial_domain())
618 return;
619
620 domU_write_console(0, "(early) ", 8);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700621 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
622 linelen = pos-string+off;
623 if (off + linelen > len)
624 break;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100625 domU_write_console(0, string+off, linelen);
626 domU_write_console(0, "\r\n", 2);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700627 off += linelen + 1;
628 }
629 if (off < len)
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100630 domU_write_console(0, string+off, len-off);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700631}
632
633struct console xenboot_console = {
634 .name = "xenboot",
635 .write = xenboot_write_console,
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100636 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700637};
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100638#endif /* CONFIG_EARLY_PRINTK */
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100639
640void xen_raw_console_write(const char *str)
641{
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100642 dom0_write_console(0, str, strlen(str));
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100643}
644
645void xen_raw_printk(const char *fmt, ...)
646{
647 static char buf[512];
648 va_list ap;
649
650 va_start(ap, fmt);
651 vsnprintf(buf, sizeof(buf), fmt, ap);
652 va_end(ap);
653
654 xen_raw_console_write(buf);
655}