hvc_xen.c revision 6a07cbc4ef9cea9f9eabc676fd6b8c03c11f51cc
1/* 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> 26#include <linux/list.h> 27 28#include <asm/io.h> 29#include <asm/xen/hypervisor.h> 30 31#include <xen/xen.h> 32#include <xen/interface/xen.h> 33#include <xen/hvm.h> 34#include <xen/grant_table.h> 35#include <xen/page.h> 36#include <xen/events.h> 37#include <xen/interface/io/console.h> 38#include <xen/hvc-console.h> 39#include <xen/xenbus.h> 40 41#include "hvc_console.h" 42 43#define HVC_COOKIE 0x58656e /* "Xen" in hex */ 44 45struct 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); 58 59/* ------------------------------------------------------------------ */ 60 61static struct xencons_info *vtermno_to_xencons(int vtermno) 62{ 63 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; 76} 77 78static 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) 84{ 85 /* Use evtchn: this is called early, before irq is set up. */ 86 notify_remote_via_evtchn(cons->evtchn); 87} 88 89static int __write_console(struct xencons_info *xencons, 90 const char *data, int len) 91{ 92 XENCONS_RING_IDX cons, prod; 93 struct xencons_interface *intf = xencons->intf; 94 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 107 if (sent) 108 notify_daemon(xencons); 109 return sent; 110} 111 112static int domU_write_console(uint32_t vtermno, const char *data, int len) 113{ 114 int ret = len; 115 struct xencons_info *cons = vtermno_to_xencons(vtermno); 116 if (cons == NULL) 117 return -EINVAL; 118 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) { 126 int sent = __write_console(cons, data, len); 127 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 138static int domU_read_console(uint32_t vtermno, char *buf, int len) 139{ 140 struct xencons_interface *intf; 141 XENCONS_RING_IDX cons, prod; 142 int recv = 0; 143 struct xencons_info *xencons = vtermno_to_xencons(vtermno); 144 if (xencons == NULL) 145 return -EINVAL; 146 intf = xencons->intf; 147 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 159 notify_daemon(xencons); 160 return recv; 161} 162 163static struct hv_ops domU_hvc_ops = { 164 .get_chars = domU_read_console, 165 .put_chars = domU_write_console, 166 .notifier_add = notifier_add_irq, 167 .notifier_del = notifier_del_irq, 168 .notifier_hangup = notifier_hangup_irq, 169}; 170 171static 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 197static int xen_hvm_console_init(void) 198{ 199 int r; 200 uint64_t v = 0; 201 unsigned long mfn; 202 struct xencons_info *info; 203 204 if (!xen_hvm_domain()) 205 return -ENODEV; 206 207 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; 217 218 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v); 219 if (r < 0) 220 goto err; 221 info->evtchn = v; 222 hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v); 223 if (r < 0) 224 goto err; 225 mfn = v; 226 info->intf = ioremap(mfn << PAGE_SHIFT, PAGE_SIZE); 227 if (info->intf == NULL) 228 goto err; 229 info->vtermno = HVC_COOKIE; 230 231 spin_lock(&xencons_lock); 232 list_add_tail(&info->list, &xenconsoles); 233 spin_unlock(&xencons_lock); 234 235 return 0; 236err: 237 kfree(info); 238 return -ENODEV; 239} 240 241static int xen_pv_console_init(void) 242{ 243 struct xencons_info *info; 244 245 if (!xen_pv_domain()) 246 return -ENODEV; 247 248 if (!xen_start_info->console.domU.evtchn) 249 return -ENODEV; 250 251 info = vtermno_to_xencons(HVC_COOKIE); 252 if (!info) { 253 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 254 if (!info) 255 return -ENOMEM; 256 } 257 258 /* already configured */ 259 if (info->intf != NULL) 260 return 0; 261 262 info->evtchn = xen_start_info->console.domU.evtchn; 263 info->intf = mfn_to_virt(xen_start_info->console.domU.mfn); 264 info->vtermno = HVC_COOKIE; 265 266 spin_lock(&xencons_lock); 267 list_add_tail(&info->list, &xenconsoles); 268 spin_unlock(&xencons_lock); 269 270 return 0; 271} 272 273static int xen_initial_domain_console_init(void) 274{ 275 struct xencons_info *info; 276 277 if (!xen_initial_domain()) 278 return -ENODEV; 279 280 info = vtermno_to_xencons(HVC_COOKIE); 281 if (!info) { 282 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 283 if (!info) 284 return -ENOMEM; 285 } 286 287 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0); 288 info->vtermno = HVC_COOKIE; 289 290 spin_lock(&xencons_lock); 291 list_add_tail(&info->list, &xenconsoles); 292 spin_unlock(&xencons_lock); 293 294 return 0; 295} 296 297void xen_console_resume(void) 298{ 299 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE); 300 if (info != NULL && info->irq) 301 rebind_evtchn_irq(info->evtchn, info->irq); 302} 303 304static void xencons_disconnect_backend(struct xencons_info *info) 305{ 306 if (info->irq > 0) 307 unbind_from_irqhandler(info->irq, NULL); 308 info->irq = 0; 309 if (info->evtchn > 0) 310 xenbus_free_evtchn(info->xbdev, info->evtchn); 311 info->evtchn = 0; 312 if (info->gntref > 0) 313 gnttab_free_grant_references(info->gntref); 314 info->gntref = 0; 315 if (info->hvc != NULL) 316 hvc_remove(info->hvc); 317 info->hvc = NULL; 318} 319 320static void xencons_free(struct xencons_info *info) 321{ 322 free_page((unsigned long)info->intf); 323 info->intf = NULL; 324 info->vtermno = 0; 325 kfree(info); 326} 327 328static int xen_console_remove(struct xencons_info *info) 329{ 330 xencons_disconnect_backend(info); 331 spin_lock(&xencons_lock); 332 list_del(&info->list); 333 spin_unlock(&xencons_lock); 334 if (info->xbdev != NULL) 335 xencons_free(info); 336 else { 337 if (xen_hvm_domain()) 338 iounmap(info->intf); 339 kfree(info); 340 } 341 return 0; 342} 343 344#ifdef CONFIG_HVC_XEN_FRONTEND 345static struct xenbus_driver xencons_driver; 346 347static int xencons_remove(struct xenbus_device *dev) 348{ 349 return xen_console_remove(dev_get_drvdata(&dev->dev)); 350} 351 352static int xencons_connect_backend(struct xenbus_device *dev, 353 struct xencons_info *info) 354{ 355 int ret, evtchn, devid, ref, irq; 356 struct xenbus_transaction xbt; 357 grant_ref_t gref_head; 358 unsigned long mfn; 359 360 ret = xenbus_alloc_evtchn(dev, &evtchn); 361 if (ret) 362 return ret; 363 info->evtchn = evtchn; 364 irq = bind_evtchn_to_irq(evtchn); 365 if (irq < 0) 366 return irq; 367 info->irq = irq; 368 devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; 369 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid), 370 irq, &domU_hvc_ops, 256); 371 if (IS_ERR(info->hvc)) 372 return PTR_ERR(info->hvc); 373 if (xen_pv_domain()) 374 mfn = virt_to_mfn(info->intf); 375 else 376 mfn = __pa(info->intf) >> PAGE_SHIFT; 377 ret = gnttab_alloc_grant_references(1, &gref_head); 378 if (ret < 0) 379 return ret; 380 info->gntref = gref_head; 381 ref = gnttab_claim_grant_reference(&gref_head); 382 if (ref < 0) 383 return ref; 384 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id, 385 mfn, 0); 386 387 again: 388 ret = xenbus_transaction_start(&xbt); 389 if (ret) { 390 xenbus_dev_fatal(dev, ret, "starting transaction"); 391 return ret; 392 } 393 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref); 394 if (ret) 395 goto error_xenbus; 396 ret = xenbus_printf(xbt, dev->nodename, "port", "%u", 397 evtchn); 398 if (ret) 399 goto error_xenbus; 400 ret = xenbus_printf(xbt, dev->nodename, "type", "ioemu"); 401 if (ret) 402 goto error_xenbus; 403 ret = xenbus_transaction_end(xbt, 0); 404 if (ret) { 405 if (ret == -EAGAIN) 406 goto again; 407 xenbus_dev_fatal(dev, ret, "completing transaction"); 408 return ret; 409 } 410 411 xenbus_switch_state(dev, XenbusStateInitialised); 412 return 0; 413 414 error_xenbus: 415 xenbus_transaction_end(xbt, 1); 416 xenbus_dev_fatal(dev, ret, "writing xenstore"); 417 return ret; 418} 419 420static int __devinit xencons_probe(struct xenbus_device *dev, 421 const struct xenbus_device_id *id) 422{ 423 int ret, devid; 424 struct xencons_info *info; 425 426 devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; 427 if (devid == 0) 428 return -ENODEV; 429 430 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); 431 if (!info) 432 return -ENOMEM; 433 dev_set_drvdata(&dev->dev, info); 434 info->xbdev = dev; 435 info->vtermno = xenbus_devid_to_vtermno(devid); 436 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); 437 if (!info->intf) 438 goto error_nomem; 439 440 ret = xencons_connect_backend(dev, info); 441 if (ret < 0) 442 goto error; 443 spin_lock(&xencons_lock); 444 list_add_tail(&info->list, &xenconsoles); 445 spin_unlock(&xencons_lock); 446 447 return 0; 448 449 error_nomem: 450 ret = -ENOMEM; 451 xenbus_dev_fatal(dev, ret, "allocating device memory"); 452 error: 453 xencons_disconnect_backend(info); 454 xencons_free(info); 455 return ret; 456} 457 458static int xencons_resume(struct xenbus_device *dev) 459{ 460 struct xencons_info *info = dev_get_drvdata(&dev->dev); 461 462 xencons_disconnect_backend(info); 463 memset(info->intf, 0, PAGE_SIZE); 464 return xencons_connect_backend(dev, info); 465} 466 467static void xencons_backend_changed(struct xenbus_device *dev, 468 enum xenbus_state backend_state) 469{ 470 switch (backend_state) { 471 case XenbusStateReconfiguring: 472 case XenbusStateReconfigured: 473 case XenbusStateInitialising: 474 case XenbusStateInitialised: 475 case XenbusStateUnknown: 476 case XenbusStateClosed: 477 break; 478 479 case XenbusStateInitWait: 480 break; 481 482 case XenbusStateConnected: 483 xenbus_switch_state(dev, XenbusStateConnected); 484 break; 485 486 case XenbusStateClosing: 487 xenbus_frontend_closed(dev); 488 break; 489 } 490} 491 492static const struct xenbus_device_id xencons_ids[] = { 493 { "console" }, 494 { "" } 495}; 496 497 498static DEFINE_XENBUS_DRIVER(xencons, "xenconsole", 499 .probe = xencons_probe, 500 .remove = xencons_remove, 501 .resume = xencons_resume, 502 .otherend_changed = xencons_backend_changed, 503); 504#endif /* CONFIG_HVC_XEN_FRONTEND */ 505 506static int __init xen_hvc_init(void) 507{ 508 int r; 509 struct xencons_info *info; 510 const struct hv_ops *ops; 511 512 if (!xen_domain()) 513 return -ENODEV; 514 515 if (xen_initial_domain()) { 516 ops = &dom0_hvc_ops; 517 r = xen_initial_domain_console_init(); 518 if (r < 0) 519 return r; 520 info = vtermno_to_xencons(HVC_COOKIE); 521 } else { 522 ops = &domU_hvc_ops; 523 if (xen_hvm_domain()) 524 r = xen_hvm_console_init(); 525 else 526 r = xen_pv_console_init(); 527 if (r < 0) 528 return r; 529 530 info = vtermno_to_xencons(HVC_COOKIE); 531 info->irq = bind_evtchn_to_irq(info->evtchn); 532 } 533 if (info->irq < 0) 534 info->irq = 0; /* NO_IRQ */ 535 else 536 irq_set_noprobe(info->irq); 537 538 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256); 539 if (IS_ERR(info->hvc)) { 540 r = PTR_ERR(info->hvc); 541 spin_lock(&xencons_lock); 542 list_del(&info->list); 543 spin_unlock(&xencons_lock); 544 if (info->irq) 545 unbind_from_irqhandler(info->irq, NULL); 546 kfree(info); 547 return r; 548 } 549 550 r = 0; 551#ifdef CONFIG_HVC_XEN_FRONTEND 552 r = xenbus_register_frontend(&xencons_driver); 553#endif 554 return r; 555} 556 557static void __exit xen_hvc_fini(void) 558{ 559 struct xencons_info *entry, *next; 560 561 if (list_empty(&xenconsoles)) 562 return; 563 564 list_for_each_entry_safe(entry, next, &xenconsoles, list) { 565 xen_console_remove(entry); 566 } 567} 568 569static int xen_cons_init(void) 570{ 571 const struct hv_ops *ops; 572 573 if (!xen_domain()) 574 return 0; 575 576 if (xen_initial_domain()) 577 ops = &dom0_hvc_ops; 578 else { 579 int r; 580 ops = &domU_hvc_ops; 581 582 if (xen_hvm_domain()) 583 r = xen_hvm_console_init(); 584 else 585 r = xen_pv_console_init(); 586 if (r < 0) 587 return r; 588 } 589 590 hvc_instantiate(HVC_COOKIE, 0, ops); 591 return 0; 592} 593 594 595module_init(xen_hvc_init); 596module_exit(xen_hvc_fini); 597console_initcall(xen_cons_init); 598 599#ifdef CONFIG_EARLY_PRINTK 600static void xenboot_write_console(struct console *console, const char *string, 601 unsigned len) 602{ 603 unsigned int linelen, off = 0; 604 const char *pos; 605 606 if (!xen_pv_domain()) 607 return; 608 609 dom0_write_console(0, string, len); 610 611 if (xen_initial_domain()) 612 return; 613 614 domU_write_console(0, "(early) ", 8); 615 while (off < len && NULL != (pos = strchr(string+off, '\n'))) { 616 linelen = pos-string+off; 617 if (off + linelen > len) 618 break; 619 domU_write_console(0, string+off, linelen); 620 domU_write_console(0, "\r\n", 2); 621 off += linelen + 1; 622 } 623 if (off < len) 624 domU_write_console(0, string+off, len-off); 625} 626 627struct console xenboot_console = { 628 .name = "xenboot", 629 .write = xenboot_write_console, 630 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, 631}; 632#endif /* CONFIG_EARLY_PRINTK */ 633 634void xen_raw_console_write(const char *str) 635{ 636 dom0_write_console(0, str, strlen(str)); 637} 638 639void xen_raw_printk(const char *fmt, ...) 640{ 641 static char buf[512]; 642 va_list ap; 643 644 va_start(ap, fmt); 645 vsnprintf(buf, sizeof(buf), fmt, ap); 646 va_end(ap); 647 648 xen_raw_console_write(buf); 649} 650