blob: 26090c736bcfe0f9ec7d0caa4e8a8899ac8f3feb [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);
58static struct xenbus_driver xencons_driver;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070059
60/* ------------------------------------------------------------------ */
61
Stefano Stabellini02e19f92012-01-30 16:02:31 +000062static struct xencons_info *vtermno_to_xencons(int vtermno)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070063{
Stefano Stabellini02e19f92012-01-30 16:02:31 +000064 struct xencons_info *entry, *n, *ret = NULL;
65
66 if (list_empty(&xenconsoles))
67 return NULL;
68
69 list_for_each_entry_safe(entry, n, &xenconsoles, list) {
70 if (entry->vtermno == vtermno) {
71 ret = entry;
72 break;
73 }
74 }
75
76 return ret;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070077}
78
Stefano Stabellini02e19f92012-01-30 16:02:31 +000079static inline int xenbus_devid_to_vtermno(int devid)
80{
81 return devid + HVC_COOKIE;
82}
83
84static inline void notify_daemon(struct xencons_info *cons)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070085{
86 /* Use evtchn: this is called early, before irq is set up. */
Stefano Stabellini02e19f92012-01-30 16:02:31 +000087 notify_remote_via_evtchn(cons->evtchn);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070088}
89
Stefano Stabellini02e19f92012-01-30 16:02:31 +000090static int __write_console(struct xencons_info *xencons,
91 const char *data, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070092{
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070093 XENCONS_RING_IDX cons, prod;
Stefano Stabellini02e19f92012-01-30 16:02:31 +000094 struct xencons_interface *intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070095 int sent = 0;
96
97 cons = intf->out_cons;
98 prod = intf->out_prod;
99 mb(); /* update queue values before going on */
100 BUG_ON((prod - cons) > sizeof(intf->out));
101
102 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
103 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
104
105 wmb(); /* write ring before updating pointer */
106 intf->out_prod = prod;
107
Jeremy Fitzhardinge403a85f2010-10-14 11:38:47 -0700108 if (sent)
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000109 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700110 return sent;
111}
112
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100113static int domU_write_console(uint32_t vtermno, const char *data, int len)
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900114{
115 int ret = len;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000116 struct xencons_info *cons = vtermno_to_xencons(vtermno);
117 if (cons == NULL)
118 return -EINVAL;
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900119
120 /*
121 * Make sure the whole buffer is emitted, polling if
122 * necessary. We don't ever want to rely on the hvc daemon
123 * because the most interesting console output is when the
124 * kernel is crippled.
125 */
126 while (len) {
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000127 int sent = __write_console(cons, data, len);
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900128
129 data += sent;
130 len -= sent;
131
132 if (unlikely(len))
133 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
134 }
135
136 return ret;
137}
138
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100139static int domU_read_console(uint32_t vtermno, char *buf, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700140{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000141 struct xencons_interface *intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700142 XENCONS_RING_IDX cons, prod;
143 int recv = 0;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000144 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
145 if (xencons == NULL)
146 return -EINVAL;
147 intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700148
149 cons = intf->in_cons;
150 prod = intf->in_prod;
151 mb(); /* get pointers before reading ring */
152 BUG_ON((prod - cons) > sizeof(intf->in));
153
154 while (cons != prod && recv < len)
155 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
156
157 mb(); /* read ring before consuming */
158 intf->in_cons = cons;
159
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000160 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700161 return recv;
162}
163
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100164static struct hv_ops domU_hvc_ops = {
165 .get_chars = domU_read_console,
166 .put_chars = domU_write_console,
Christian Borntraeger611e0972008-06-20 15:24:08 +0200167 .notifier_add = notifier_add_irq,
168 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000169 .notifier_hangup = notifier_hangup_irq,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700170};
171
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100172static int dom0_read_console(uint32_t vtermno, char *buf, int len)
173{
174 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
175}
176
177/*
178 * Either for a dom0 to write to the system console, or a domU with a
179 * debug version of Xen
180 */
181static int dom0_write_console(uint32_t vtermno, const char *str, int len)
182{
183 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
184 if (rc < 0)
185 return 0;
186
187 return len;
188}
189
190static struct hv_ops dom0_hvc_ops = {
191 .get_chars = dom0_read_console,
192 .put_chars = dom0_write_console,
193 .notifier_add = notifier_add_irq,
194 .notifier_del = notifier_del_irq,
195 .notifier_hangup = notifier_hangup_irq,
196};
197
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000198static int xen_hvm_console_init(void)
199{
200 int r;
201 uint64_t v = 0;
202 unsigned long mfn;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000203 struct xencons_info *info;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000204
205 if (!xen_hvm_domain())
206 return -ENODEV;
207
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000208 info = vtermno_to_xencons(HVC_COOKIE);
209 if (!info) {
210 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
211 if (!info)
212 return -ENOMEM;
213 }
214
215 /* already configured */
216 if (info->intf != NULL)
217 return 0;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000218
219 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000220 if (r < 0) {
221 kfree(info);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000222 return -ENODEV;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000223 }
224 info->evtchn = v;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000225 hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000226 if (r < 0) {
227 kfree(info);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000228 return -ENODEV;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000229 }
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000230 mfn = v;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000231 info->intf = ioremap(mfn << PAGE_SHIFT, PAGE_SIZE);
232 if (info->intf == NULL) {
233 kfree(info);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000234 return -ENODEV;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000235 }
236 info->vtermno = HVC_COOKIE;
237
238 spin_lock(&xencons_lock);
239 list_add_tail(&info->list, &xenconsoles);
240 spin_unlock(&xencons_lock);
241
242 return 0;
243}
244
245static int xen_pv_console_init(void)
246{
247 struct xencons_info *info;
248
249 if (!xen_pv_domain())
250 return -ENODEV;
251
252 if (!xen_start_info->console.domU.evtchn)
253 return -ENODEV;
254
255 info = vtermno_to_xencons(HVC_COOKIE);
256 if (!info) {
257 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
258 if (!info)
259 return -ENOMEM;
260 }
261
262 /* already configured */
263 if (info->intf != NULL)
264 return 0;
265
266 info->evtchn = xen_start_info->console.domU.evtchn;
267 info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
268 info->vtermno = HVC_COOKIE;
269
270 spin_lock(&xencons_lock);
271 list_add_tail(&info->list, &xenconsoles);
272 spin_unlock(&xencons_lock);
273
274 return 0;
275}
276
277static int xen_initial_domain_console_init(void)
278{
279 struct xencons_info *info;
280
281 if (!xen_initial_domain())
282 return -ENODEV;
283
284 info = vtermno_to_xencons(HVC_COOKIE);
285 if (!info) {
286 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
287 if (!info)
288 return -ENOMEM;
289 }
290
291 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
292 info->vtermno = HVC_COOKIE;
293
294 spin_lock(&xencons_lock);
295 list_add_tail(&info->list, &xenconsoles);
296 spin_unlock(&xencons_lock);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000297
298 return 0;
299}
300
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100301static int __init xen_hvc_init(void)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700302{
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000303 int r;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000304 struct xencons_info *info;
305 const struct hv_ops *ops;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700306
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000307 if (!xen_domain())
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100308 return -ENODEV;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700309
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100310 if (xen_initial_domain()) {
311 ops = &dom0_hvc_ops;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000312 r = xen_initial_domain_console_init();
313 if (r < 0)
314 return r;
315 info = vtermno_to_xencons(HVC_COOKIE);
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100316 } else {
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100317 ops = &domU_hvc_ops;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000318 if (xen_hvm_domain())
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000319 r = xen_hvm_console_init();
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000320 else
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000321 r = xen_pv_console_init();
322 if (r < 0)
323 return r;
324
325 info = vtermno_to_xencons(HVC_COOKIE);
326 info->irq = bind_evtchn_to_irq(info->evtchn);
327 }
328 if (info->irq < 0)
329 info->irq = 0; /* NO_IRQ */
330 else
331 irq_set_noprobe(info->irq);
332
333 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
334 if (IS_ERR(info->hvc)) {
335 r = PTR_ERR(info->hvc);
336 spin_lock(&xencons_lock);
337 list_del(&info->list);
338 spin_unlock(&xencons_lock);
339 if (info->irq)
340 unbind_from_irqhandler(info->irq, NULL);
341 kfree(info);
342 return r;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100343 }
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100344
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000345 return xenbus_register_frontend(&xencons_driver);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700346}
347
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100348void xen_console_resume(void)
349{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000350 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
351 if (info != NULL && info->irq)
352 rebind_evtchn_irq(info->evtchn, info->irq);
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100353}
354
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000355static void xencons_disconnect_backend(struct xencons_info *info)
356{
357 if (info->irq > 0)
358 unbind_from_irqhandler(info->irq, NULL);
359 info->irq = 0;
360 if (info->evtchn > 0)
361 xenbus_free_evtchn(info->xbdev, info->evtchn);
362 info->evtchn = 0;
363 if (info->gntref > 0)
364 gnttab_free_grant_references(info->gntref);
365 info->gntref = 0;
366 if (info->hvc != NULL)
367 hvc_remove(info->hvc);
368 info->hvc = NULL;
369}
370
371static void xencons_free(struct xencons_info *info)
372{
373 free_page((unsigned long)info->intf);
374 info->intf = NULL;
375 info->vtermno = 0;
376 kfree(info);
377}
378
379static int xen_console_remove(struct xencons_info *info)
380{
381 xencons_disconnect_backend(info);
382 spin_lock(&xencons_lock);
383 list_del(&info->list);
384 spin_unlock(&xencons_lock);
385 if (info->xbdev != NULL)
386 xencons_free(info);
387 else {
388 if (xen_hvm_domain())
389 iounmap(info->intf);
390 kfree(info);
391 }
392 return 0;
393}
394
395static int xencons_remove(struct xenbus_device *dev)
396{
397 return xen_console_remove(dev_get_drvdata(&dev->dev));
398}
399
400static int xencons_connect_backend(struct xenbus_device *dev,
401 struct xencons_info *info)
402{
403 int ret, evtchn, devid, ref, irq;
404 struct xenbus_transaction xbt;
405 grant_ref_t gref_head;
406 unsigned long mfn;
407
408 ret = xenbus_alloc_evtchn(dev, &evtchn);
409 if (ret)
410 return ret;
411 info->evtchn = evtchn;
412 irq = bind_evtchn_to_irq(evtchn);
413 if (irq < 0)
414 return irq;
415 info->irq = irq;
416 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
417 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
418 irq, &domU_hvc_ops, 256);
419 if (IS_ERR(info->hvc))
420 return PTR_ERR(info->hvc);
421 if (xen_pv_domain())
422 mfn = virt_to_mfn(info->intf);
423 else
424 mfn = __pa(info->intf) >> PAGE_SHIFT;
425 ret = gnttab_alloc_grant_references(1, &gref_head);
426 if (ret < 0)
427 return ret;
428 info->gntref = gref_head;
429 ref = gnttab_claim_grant_reference(&gref_head);
430 if (ref < 0)
431 return ref;
432 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
433 mfn, 0);
434
435 again:
436 ret = xenbus_transaction_start(&xbt);
437 if (ret) {
438 xenbus_dev_fatal(dev, ret, "starting transaction");
439 return ret;
440 }
441 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
442 if (ret)
443 goto error_xenbus;
444 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
445 evtchn);
446 if (ret)
447 goto error_xenbus;
448 ret = xenbus_printf(xbt, dev->nodename, "type", "ioemu");
449 if (ret)
450 goto error_xenbus;
451 ret = xenbus_transaction_end(xbt, 0);
452 if (ret) {
453 if (ret == -EAGAIN)
454 goto again;
455 xenbus_dev_fatal(dev, ret, "completing transaction");
456 return ret;
457 }
458
459 xenbus_switch_state(dev, XenbusStateInitialised);
460 return 0;
461
462 error_xenbus:
463 xenbus_transaction_end(xbt, 1);
464 xenbus_dev_fatal(dev, ret, "writing xenstore");
465 return ret;
466}
467
468static int __devinit xencons_probe(struct xenbus_device *dev,
469 const struct xenbus_device_id *id)
470{
471 int ret, devid;
472 struct xencons_info *info;
473
474 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
475 if (devid == 0)
476 return -ENODEV;
477
478 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
479 if (!info)
480 goto error_nomem;
481 dev_set_drvdata(&dev->dev, info);
482 info->xbdev = dev;
483 info->vtermno = xenbus_devid_to_vtermno(devid);
484 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
485 if (!info->intf)
486 goto error_nomem;
487
488 ret = xencons_connect_backend(dev, info);
489 if (ret < 0)
490 goto error;
491 spin_lock(&xencons_lock);
492 list_add_tail(&info->list, &xenconsoles);
493 spin_unlock(&xencons_lock);
494
495 return 0;
496
497 error_nomem:
498 ret = -ENOMEM;
499 xenbus_dev_fatal(dev, ret, "allocating device memory");
500 error:
501 xencons_disconnect_backend(info);
502 xencons_free(info);
503 return ret;
504}
505
506static int xencons_resume(struct xenbus_device *dev)
507{
508 struct xencons_info *info = dev_get_drvdata(&dev->dev);
509
510 xencons_disconnect_backend(info);
511 memset(info->intf, 0, PAGE_SIZE);
512 return xencons_connect_backend(dev, info);
513}
514
515static void xencons_backend_changed(struct xenbus_device *dev,
516 enum xenbus_state backend_state)
517{
518 switch (backend_state) {
519 case XenbusStateReconfiguring:
520 case XenbusStateReconfigured:
521 case XenbusStateInitialising:
522 case XenbusStateInitialised:
523 case XenbusStateUnknown:
524 case XenbusStateClosed:
525 break;
526
527 case XenbusStateInitWait:
528 break;
529
530 case XenbusStateConnected:
531 xenbus_switch_state(dev, XenbusStateConnected);
532 break;
533
534 case XenbusStateClosing:
535 xenbus_frontend_closed(dev);
536 break;
537 }
538}
539
540static const struct xenbus_device_id xencons_ids[] = {
541 { "console" },
542 { "" }
543};
544
545
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100546static void __exit xen_hvc_fini(void)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700547{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000548 struct xencons_info *entry, *next;
549
550 if (list_empty(&xenconsoles))
551 return;
552
553 list_for_each_entry_safe(entry, next, &xenconsoles, list) {
554 xen_console_remove(entry);
555 }
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700556}
557
558static int xen_cons_init(void)
559{
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000560 const struct hv_ops *ops;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100561
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000562 if (!xen_domain())
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700563 return 0;
564
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100565 if (xen_initial_domain())
566 ops = &dom0_hvc_ops;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000567 else {
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000568 int r;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100569 ops = &domU_hvc_ops;
570
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000571 if (xen_hvm_domain())
572 r = xen_hvm_console_init();
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000573 else
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000574 r = xen_pv_console_init();
575 if (r < 0)
576 return r;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000577 }
578
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100579 hvc_instantiate(HVC_COOKIE, 0, ops);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700580 return 0;
581}
582
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000583static DEFINE_XENBUS_DRIVER(xencons, "xenconsole",
584 .probe = xencons_probe,
585 .remove = xencons_remove,
586 .resume = xencons_resume,
587 .otherend_changed = xencons_backend_changed,
588);
589
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100590module_init(xen_hvc_init);
591module_exit(xen_hvc_fini);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700592console_initcall(xen_cons_init);
593
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100594#ifdef CONFIG_EARLY_PRINTK
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700595static void xenboot_write_console(struct console *console, const char *string,
596 unsigned len)
597{
598 unsigned int linelen, off = 0;
599 const char *pos;
600
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000601 if (!xen_pv_domain())
602 return;
603
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100604 dom0_write_console(0, string, len);
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100605
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100606 if (xen_initial_domain())
607 return;
608
609 domU_write_console(0, "(early) ", 8);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700610 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
611 linelen = pos-string+off;
612 if (off + linelen > len)
613 break;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100614 domU_write_console(0, string+off, linelen);
615 domU_write_console(0, "\r\n", 2);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700616 off += linelen + 1;
617 }
618 if (off < len)
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100619 domU_write_console(0, string+off, len-off);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700620}
621
622struct console xenboot_console = {
623 .name = "xenboot",
624 .write = xenboot_write_console,
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100625 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700626};
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100627#endif /* CONFIG_EARLY_PRINTK */
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100628
629void xen_raw_console_write(const char *str)
630{
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100631 dom0_write_console(0, str, strlen(str));
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100632}
633
634void xen_raw_printk(const char *fmt, ...)
635{
636 static char buf[512];
637 va_list ap;
638
639 va_start(ap, fmt);
640 vsnprintf(buf, sizeof(buf), fmt, ap);
641 va_end(ap);
642
643 xen_raw_console_write(buf);
644}