| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 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 |  | 
|  | 27 | #include <asm/xen/hypervisor.h> | 
|  | 28 | #include <xen/page.h> | 
|  | 29 | #include <xen/events.h> | 
|  | 30 | #include <xen/interface/io/console.h> | 
|  | 31 | #include <xen/hvc-console.h> | 
|  | 32 |  | 
|  | 33 | #include "hvc_console.h" | 
|  | 34 |  | 
|  | 35 | #define HVC_COOKIE   0x58656e /* "Xen" in hex */ | 
|  | 36 |  | 
|  | 37 | static struct hvc_struct *hvc; | 
|  | 38 | static int xencons_irq; | 
|  | 39 |  | 
|  | 40 | /* ------------------------------------------------------------------ */ | 
|  | 41 |  | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 42 | static unsigned long console_pfn = ~0ul; | 
|  | 43 |  | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 44 | static inline struct xencons_interface *xencons_interface(void) | 
|  | 45 | { | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 46 | if (console_pfn == ~0ul) | 
|  | 47 | return mfn_to_virt(xen_start_info->console.domU.mfn); | 
|  | 48 | else | 
|  | 49 | return __va(console_pfn << PAGE_SHIFT); | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | static inline void notify_daemon(void) | 
|  | 53 | { | 
|  | 54 | /* Use evtchn: this is called early, before irq is set up. */ | 
|  | 55 | notify_remote_via_evtchn(xen_start_info->console.domU.evtchn); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | static int write_console(uint32_t vtermno, const char *data, int len) | 
|  | 59 | { | 
|  | 60 | struct xencons_interface *intf = xencons_interface(); | 
|  | 61 | XENCONS_RING_IDX cons, prod; | 
|  | 62 | int sent = 0; | 
|  | 63 |  | 
|  | 64 | cons = intf->out_cons; | 
|  | 65 | prod = intf->out_prod; | 
|  | 66 | mb();			/* update queue values before going on */ | 
|  | 67 | BUG_ON((prod - cons) > sizeof(intf->out)); | 
|  | 68 |  | 
|  | 69 | while ((sent < len) && ((prod - cons) < sizeof(intf->out))) | 
|  | 70 | intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++]; | 
|  | 71 |  | 
|  | 72 | wmb();			/* write ring before updating pointer */ | 
|  | 73 | intf->out_prod = prod; | 
|  | 74 |  | 
|  | 75 | notify_daemon(); | 
|  | 76 | return sent; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | static int read_console(uint32_t vtermno, char *buf, int len) | 
|  | 80 | { | 
|  | 81 | struct xencons_interface *intf = xencons_interface(); | 
|  | 82 | XENCONS_RING_IDX cons, prod; | 
|  | 83 | int recv = 0; | 
|  | 84 |  | 
|  | 85 | cons = intf->in_cons; | 
|  | 86 | prod = intf->in_prod; | 
|  | 87 | mb();			/* get pointers before reading ring */ | 
|  | 88 | BUG_ON((prod - cons) > sizeof(intf->in)); | 
|  | 89 |  | 
|  | 90 | while (cons != prod && recv < len) | 
|  | 91 | buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)]; | 
|  | 92 |  | 
|  | 93 | mb();			/* read ring before consuming */ | 
|  | 94 | intf->in_cons = cons; | 
|  | 95 |  | 
|  | 96 | notify_daemon(); | 
|  | 97 | return recv; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static struct hv_ops hvc_ops = { | 
|  | 101 | .get_chars = read_console, | 
|  | 102 | .put_chars = write_console, | 
| Christian Borntraeger | 611e097 | 2008-06-20 15:24:08 +0200 | [diff] [blame] | 103 | .notifier_add = notifier_add_irq, | 
|  | 104 | .notifier_del = notifier_del_irq, | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 105 | }; | 
|  | 106 |  | 
|  | 107 | static int __init xen_init(void) | 
|  | 108 | { | 
|  | 109 | struct hvc_struct *hp; | 
|  | 110 |  | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 111 | if (!is_running_on_xen() || | 
|  | 112 | is_initial_xendomain() || | 
|  | 113 | !xen_start_info->console.domU.evtchn) | 
|  | 114 | return -ENODEV; | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn); | 
|  | 117 | if (xencons_irq < 0) | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 118 | xencons_irq = 0; /* NO_IRQ */ | 
|  | 119 |  | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 120 | hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256); | 
|  | 121 | if (IS_ERR(hp)) | 
|  | 122 | return PTR_ERR(hp); | 
|  | 123 |  | 
|  | 124 | hvc = hp; | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 125 |  | 
|  | 126 | console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn); | 
|  | 127 |  | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 128 | return 0; | 
|  | 129 | } | 
|  | 130 |  | 
| Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 131 | void xen_console_resume(void) | 
|  | 132 | { | 
|  | 133 | if (xencons_irq) | 
|  | 134 | rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq); | 
|  | 135 | } | 
|  | 136 |  | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 137 | static void __exit xen_fini(void) | 
|  | 138 | { | 
|  | 139 | if (hvc) | 
|  | 140 | hvc_remove(hvc); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | static int xen_cons_init(void) | 
|  | 144 | { | 
|  | 145 | if (!is_running_on_xen()) | 
|  | 146 | return 0; | 
|  | 147 |  | 
|  | 148 | hvc_instantiate(HVC_COOKIE, 0, &hvc_ops); | 
|  | 149 | return 0; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | module_init(xen_init); | 
|  | 153 | module_exit(xen_fini); | 
|  | 154 | console_initcall(xen_cons_init); | 
|  | 155 |  | 
| Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 156 | static void raw_console_write(const char *str, int len) | 
|  | 157 | { | 
|  | 158 | while(len > 0) { | 
|  | 159 | int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str); | 
|  | 160 | if (rc <= 0) | 
|  | 161 | break; | 
|  | 162 |  | 
|  | 163 | str += rc; | 
|  | 164 | len -= rc; | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | #ifdef CONFIG_EARLY_PRINTK | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 169 | static void xenboot_write_console(struct console *console, const char *string, | 
|  | 170 | unsigned len) | 
|  | 171 | { | 
|  | 172 | unsigned int linelen, off = 0; | 
|  | 173 | const char *pos; | 
|  | 174 |  | 
| Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 175 | raw_console_write(string, len); | 
|  | 176 |  | 
| Jeremy Fitzhardinge | 83abc70 | 2008-05-26 23:31:12 +0100 | [diff] [blame] | 177 | write_console(0, "(early) ", 8); | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 178 | while (off < len && NULL != (pos = strchr(string+off, '\n'))) { | 
|  | 179 | linelen = pos-string+off; | 
|  | 180 | if (off + linelen > len) | 
|  | 181 | break; | 
|  | 182 | write_console(0, string+off, linelen); | 
|  | 183 | write_console(0, "\r\n", 2); | 
|  | 184 | off += linelen + 1; | 
|  | 185 | } | 
|  | 186 | if (off < len) | 
|  | 187 | write_console(0, string+off, len-off); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | struct console xenboot_console = { | 
|  | 191 | .name		= "xenboot", | 
|  | 192 | .write		= xenboot_write_console, | 
| Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 193 | .flags		= CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, | 
| Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 194 | }; | 
| Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 195 | #endif	/* CONFIG_EARLY_PRINTK */ | 
| Jeremy Fitzhardinge | 0acf10d | 2008-05-26 23:30:59 +0100 | [diff] [blame] | 196 |  | 
|  | 197 | void xen_raw_console_write(const char *str) | 
|  | 198 | { | 
| Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 199 | raw_console_write(str, strlen(str)); | 
| Jeremy Fitzhardinge | 0acf10d | 2008-05-26 23:30:59 +0100 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
|  | 202 | void xen_raw_printk(const char *fmt, ...) | 
|  | 203 | { | 
|  | 204 | static char buf[512]; | 
|  | 205 | va_list ap; | 
|  | 206 |  | 
|  | 207 | va_start(ap, fmt); | 
|  | 208 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 209 | va_end(ap); | 
|  | 210 |  | 
|  | 211 | xen_raw_console_write(buf); | 
|  | 212 | } |