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> |
Stefano Stabellini | 4d9310e | 2012-08-06 15:27:09 +0100 | [diff] [blame] | 24 | #include <linux/irq.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/types.h> |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 27 | #include <linux/list.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 28 | |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 29 | #include <asm/io.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 30 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 31 | |
| 32 | #include <xen/xen.h> |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 33 | #include <xen/interface/xen.h> |
| 34 | #include <xen/hvm.h> |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 35 | #include <xen/grant_table.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 36 | #include <xen/page.h> |
| 37 | #include <xen/events.h> |
| 38 | #include <xen/interface/io/console.h> |
Stefano Stabellini | 4d9310e | 2012-08-06 15:27:09 +0100 | [diff] [blame] | 39 | #include <xen/interface/sched.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 40 | #include <xen/hvc-console.h> |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 41 | #include <xen/xenbus.h> |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 42 | |
| 43 | #include "hvc_console.h" |
| 44 | |
| 45 | #define HVC_COOKIE 0x58656e /* "Xen" in hex */ |
| 46 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 47 | struct xencons_info { |
| 48 | struct list_head list; |
| 49 | struct xenbus_device *xbdev; |
| 50 | struct xencons_interface *intf; |
| 51 | unsigned int evtchn; |
| 52 | struct hvc_struct *hvc; |
| 53 | int irq; |
| 54 | int vtermno; |
| 55 | grant_ref_t gntref; |
| 56 | }; |
| 57 | |
| 58 | static LIST_HEAD(xenconsoles); |
| 59 | static DEFINE_SPINLOCK(xencons_lock); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 60 | |
| 61 | /* ------------------------------------------------------------------ */ |
| 62 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 63 | static struct xencons_info *vtermno_to_xencons(int vtermno) |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 64 | { |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 65 | struct xencons_info *entry, *n, *ret = NULL; |
| 66 | |
| 67 | if (list_empty(&xenconsoles)) |
| 68 | return NULL; |
| 69 | |
| 70 | list_for_each_entry_safe(entry, n, &xenconsoles, list) { |
| 71 | if (entry->vtermno == vtermno) { |
| 72 | ret = entry; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return ret; |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 80 | static inline int xenbus_devid_to_vtermno(int devid) |
| 81 | { |
| 82 | return devid + HVC_COOKIE; |
| 83 | } |
| 84 | |
| 85 | static inline void notify_daemon(struct xencons_info *cons) |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 86 | { |
| 87 | /* Use evtchn: this is called early, before irq is set up. */ |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 88 | notify_remote_via_evtchn(cons->evtchn); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 91 | static int __write_console(struct xencons_info *xencons, |
| 92 | const char *data, int len) |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 93 | { |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 94 | XENCONS_RING_IDX cons, prod; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 95 | struct xencons_interface *intf = xencons->intf; |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 96 | int sent = 0; |
| 97 | |
| 98 | cons = intf->out_cons; |
| 99 | prod = intf->out_prod; |
| 100 | mb(); /* update queue values before going on */ |
| 101 | BUG_ON((prod - cons) > sizeof(intf->out)); |
| 102 | |
| 103 | while ((sent < len) && ((prod - cons) < sizeof(intf->out))) |
| 104 | intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++]; |
| 105 | |
| 106 | wmb(); /* write ring before updating pointer */ |
| 107 | intf->out_prod = prod; |
| 108 | |
Jeremy Fitzhardinge | 403a85f | 2010-10-14 11:38:47 -0700 | [diff] [blame] | 109 | if (sent) |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 110 | notify_daemon(xencons); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 111 | return sent; |
| 112 | } |
| 113 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 114 | static int domU_write_console(uint32_t vtermno, const char *data, int len) |
Jeremy Fitzhardinge | 7825cf1 | 2009-10-20 15:28:21 +0900 | [diff] [blame] | 115 | { |
| 116 | int ret = len; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 117 | struct xencons_info *cons = vtermno_to_xencons(vtermno); |
| 118 | if (cons == NULL) |
| 119 | return -EINVAL; |
Jeremy Fitzhardinge | 7825cf1 | 2009-10-20 15:28:21 +0900 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Make sure the whole buffer is emitted, polling if |
| 123 | * necessary. We don't ever want to rely on the hvc daemon |
| 124 | * because the most interesting console output is when the |
| 125 | * kernel is crippled. |
| 126 | */ |
| 127 | while (len) { |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 128 | int sent = __write_console(cons, data, len); |
Jeremy Fitzhardinge | 7825cf1 | 2009-10-20 15:28:21 +0900 | [diff] [blame] | 129 | |
| 130 | data += sent; |
| 131 | len -= sent; |
| 132 | |
| 133 | if (unlikely(len)) |
| 134 | HYPERVISOR_sched_op(SCHEDOP_yield, NULL); |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 140 | static int domU_read_console(uint32_t vtermno, char *buf, int len) |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 141 | { |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 142 | struct xencons_interface *intf; |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 143 | XENCONS_RING_IDX cons, prod; |
| 144 | int recv = 0; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 145 | struct xencons_info *xencons = vtermno_to_xencons(vtermno); |
| 146 | if (xencons == NULL) |
| 147 | return -EINVAL; |
| 148 | intf = xencons->intf; |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 149 | |
| 150 | cons = intf->in_cons; |
| 151 | prod = intf->in_prod; |
| 152 | mb(); /* get pointers before reading ring */ |
| 153 | BUG_ON((prod - cons) > sizeof(intf->in)); |
| 154 | |
| 155 | while (cons != prod && recv < len) |
| 156 | buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)]; |
| 157 | |
| 158 | mb(); /* read ring before consuming */ |
| 159 | intf->in_cons = cons; |
| 160 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 161 | notify_daemon(xencons); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 162 | return recv; |
| 163 | } |
| 164 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 165 | static struct hv_ops domU_hvc_ops = { |
| 166 | .get_chars = domU_read_console, |
| 167 | .put_chars = domU_write_console, |
Christian Borntraeger | 611e097 | 2008-06-20 15:24:08 +0200 | [diff] [blame] | 168 | .notifier_add = notifier_add_irq, |
| 169 | .notifier_del = notifier_del_irq, |
Hendrik Brueckner | fc362e2 | 2008-10-13 23:12:48 +0000 | [diff] [blame] | 170 | .notifier_hangup = notifier_hangup_irq, |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 173 | static int dom0_read_console(uint32_t vtermno, char *buf, int len) |
| 174 | { |
| 175 | return HYPERVISOR_console_io(CONSOLEIO_read, len, buf); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Either for a dom0 to write to the system console, or a domU with a |
| 180 | * debug version of Xen |
| 181 | */ |
| 182 | static int dom0_write_console(uint32_t vtermno, const char *str, int len) |
| 183 | { |
| 184 | int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str); |
| 185 | if (rc < 0) |
Konrad Rzeszutek Wilk | 04b772d | 2013-09-27 17:18:13 -0400 | [diff] [blame] | 186 | return rc; |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 187 | |
| 188 | return len; |
| 189 | } |
| 190 | |
| 191 | static struct hv_ops dom0_hvc_ops = { |
| 192 | .get_chars = dom0_read_console, |
| 193 | .put_chars = dom0_write_console, |
| 194 | .notifier_add = notifier_add_irq, |
| 195 | .notifier_del = notifier_del_irq, |
| 196 | .notifier_hangup = notifier_hangup_irq, |
| 197 | }; |
| 198 | |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 199 | static int xen_hvm_console_init(void) |
| 200 | { |
| 201 | int r; |
| 202 | uint64_t v = 0; |
| 203 | unsigned long mfn; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 204 | struct xencons_info *info; |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 205 | |
| 206 | if (!xen_hvm_domain()) |
| 207 | return -ENODEV; |
| 208 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 209 | info = vtermno_to_xencons(HVC_COOKIE); |
| 210 | if (!info) { |
Joe Perches | 2d1d3f3 | 2013-08-29 14:08:18 -0700 | [diff] [blame] | 211 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 212 | if (!info) |
| 213 | return -ENOMEM; |
Konrad Rzeszutek Wilk | 37a80bf | 2012-06-26 09:30:51 -0400 | [diff] [blame] | 214 | } else if (info->intf != NULL) { |
| 215 | /* already configured */ |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 216 | return 0; |
Konrad Rzeszutek Wilk | 37a80bf | 2012-06-26 09:30:51 -0400 | [diff] [blame] | 217 | } |
Konrad Rzeszutek Wilk | 5842f576 | 2012-05-23 12:56:59 -0400 | [diff] [blame] | 218 | /* |
| 219 | * If the toolstack (or the hypervisor) hasn't set these values, the |
| 220 | * default value is 0. Even though mfn = 0 and evtchn = 0 are |
| 221 | * theoretically correct values, in practice they never are and they |
| 222 | * mean that a legacy toolstack hasn't initialized the pv console correctly. |
| 223 | */ |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 224 | r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v); |
Konrad Rzeszutek Wilk | 5842f576 | 2012-05-23 12:56:59 -0400 | [diff] [blame] | 225 | if (r < 0 || v == 0) |
Konrad Rzeszutek Wilk | 2e5ad6b | 2012-05-23 12:53:11 -0400 | [diff] [blame] | 226 | goto err; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 227 | info->evtchn = v; |
Konrad Rzeszutek Wilk | a32c88b | 2012-05-23 12:55:38 -0400 | [diff] [blame] | 228 | v = 0; |
| 229 | r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v); |
Konrad Rzeszutek Wilk | 5842f576 | 2012-05-23 12:56:59 -0400 | [diff] [blame] | 230 | if (r < 0 || v == 0) |
Konrad Rzeszutek Wilk | 2e5ad6b | 2012-05-23 12:53:11 -0400 | [diff] [blame] | 231 | goto err; |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 232 | mfn = v; |
Stefano Stabellini | 3216dce | 2013-02-19 13:59:19 +0000 | [diff] [blame] | 233 | info->intf = xen_remap(mfn << PAGE_SHIFT, PAGE_SIZE); |
Konrad Rzeszutek Wilk | 2e5ad6b | 2012-05-23 12:53:11 -0400 | [diff] [blame] | 234 | if (info->intf == NULL) |
| 235 | goto err; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 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; |
Konrad Rzeszutek Wilk | 2e5ad6b | 2012-05-23 12:53:11 -0400 | [diff] [blame] | 243 | err: |
| 244 | kfree(info); |
| 245 | return -ENODEV; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int xen_pv_console_init(void) |
| 249 | { |
| 250 | struct xencons_info *info; |
| 251 | |
| 252 | if (!xen_pv_domain()) |
| 253 | return -ENODEV; |
| 254 | |
| 255 | if (!xen_start_info->console.domU.evtchn) |
| 256 | return -ENODEV; |
| 257 | |
| 258 | info = vtermno_to_xencons(HVC_COOKIE); |
| 259 | if (!info) { |
Joe Perches | 2d1d3f3 | 2013-08-29 14:08:18 -0700 | [diff] [blame] | 260 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 261 | if (!info) |
| 262 | return -ENOMEM; |
Konrad Rzeszutek Wilk | 37a80bf | 2012-06-26 09:30:51 -0400 | [diff] [blame] | 263 | } else if (info->intf != NULL) { |
| 264 | /* already configured */ |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 265 | return 0; |
Konrad Rzeszutek Wilk | 37a80bf | 2012-06-26 09:30:51 -0400 | [diff] [blame] | 266 | } |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 267 | info->evtchn = xen_start_info->console.domU.evtchn; |
Julien Grall | 0df4f26 | 2015-08-07 17:34:37 +0100 | [diff] [blame] | 268 | /* GFN == MFN for PV guest */ |
| 269 | info->intf = gfn_to_virt(xen_start_info->console.domU.mfn); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 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 | |
| 279 | static 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) { |
Joe Perches | 2d1d3f3 | 2013-08-29 14:08:18 -0700 | [diff] [blame] | 288 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 289 | if (!info) |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
David Vrabel | 77bb3df | 2015-05-19 18:40:49 +0100 | [diff] [blame] | 293 | info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 294 | info->vtermno = HVC_COOKIE; |
| 295 | |
| 296 | spin_lock(&xencons_lock); |
| 297 | list_add_tail(&info->list, &xenconsoles); |
| 298 | spin_unlock(&xencons_lock); |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Boris Ostrovsky | b9d934f | 2015-04-29 17:10:14 -0400 | [diff] [blame] | 303 | static void xen_console_update_evtchn(struct xencons_info *info) |
| 304 | { |
| 305 | if (xen_hvm_domain()) { |
Jan Beulich | c4ace5d | 2015-05-28 09:28:22 +0100 | [diff] [blame] | 306 | uint64_t v = 0; |
Boris Ostrovsky | b9d934f | 2015-04-29 17:10:14 -0400 | [diff] [blame] | 307 | int err; |
| 308 | |
| 309 | err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v); |
| 310 | if (!err && v) |
| 311 | info->evtchn = v; |
| 312 | } else |
| 313 | info->evtchn = xen_start_info->console.domU.evtchn; |
| 314 | } |
| 315 | |
Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 316 | void xen_console_resume(void) |
| 317 | { |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 318 | struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE); |
Boris Ostrovsky | b9d934f | 2015-04-29 17:10:14 -0400 | [diff] [blame] | 319 | if (info != NULL && info->irq) { |
| 320 | if (!xen_initial_domain()) |
| 321 | xen_console_update_evtchn(info); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 322 | rebind_evtchn_irq(info->evtchn, info->irq); |
Boris Ostrovsky | b9d934f | 2015-04-29 17:10:14 -0400 | [diff] [blame] | 323 | } |
Jeremy Fitzhardinge | 6b9b732 | 2008-05-26 23:31:25 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 326 | static void xencons_disconnect_backend(struct xencons_info *info) |
| 327 | { |
| 328 | if (info->irq > 0) |
| 329 | unbind_from_irqhandler(info->irq, NULL); |
| 330 | info->irq = 0; |
| 331 | if (info->evtchn > 0) |
| 332 | xenbus_free_evtchn(info->xbdev, info->evtchn); |
| 333 | info->evtchn = 0; |
| 334 | if (info->gntref > 0) |
| 335 | gnttab_free_grant_references(info->gntref); |
| 336 | info->gntref = 0; |
| 337 | if (info->hvc != NULL) |
| 338 | hvc_remove(info->hvc); |
| 339 | info->hvc = NULL; |
| 340 | } |
| 341 | |
| 342 | static void xencons_free(struct xencons_info *info) |
| 343 | { |
| 344 | free_page((unsigned long)info->intf); |
| 345 | info->intf = NULL; |
| 346 | info->vtermno = 0; |
| 347 | kfree(info); |
| 348 | } |
| 349 | |
| 350 | static int xen_console_remove(struct xencons_info *info) |
| 351 | { |
| 352 | xencons_disconnect_backend(info); |
| 353 | spin_lock(&xencons_lock); |
| 354 | list_del(&info->list); |
| 355 | spin_unlock(&xencons_lock); |
| 356 | if (info->xbdev != NULL) |
| 357 | xencons_free(info); |
| 358 | else { |
| 359 | if (xen_hvm_domain()) |
| 360 | iounmap(info->intf); |
| 361 | kfree(info); |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
Stefano Stabellini | cf8e019 | 2012-02-21 11:30:42 +0000 | [diff] [blame] | 366 | #ifdef CONFIG_HVC_XEN_FRONTEND |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 367 | static int xencons_remove(struct xenbus_device *dev) |
| 368 | { |
| 369 | return xen_console_remove(dev_get_drvdata(&dev->dev)); |
| 370 | } |
| 371 | |
| 372 | static int xencons_connect_backend(struct xenbus_device *dev, |
| 373 | struct xencons_info *info) |
| 374 | { |
| 375 | int ret, evtchn, devid, ref, irq; |
| 376 | struct xenbus_transaction xbt; |
| 377 | grant_ref_t gref_head; |
| 378 | unsigned long mfn; |
| 379 | |
| 380 | ret = xenbus_alloc_evtchn(dev, &evtchn); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | info->evtchn = evtchn; |
| 384 | irq = bind_evtchn_to_irq(evtchn); |
| 385 | if (irq < 0) |
| 386 | return irq; |
| 387 | info->irq = irq; |
| 388 | devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; |
| 389 | info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid), |
| 390 | irq, &domU_hvc_ops, 256); |
| 391 | if (IS_ERR(info->hvc)) |
| 392 | return PTR_ERR(info->hvc); |
| 393 | if (xen_pv_domain()) |
Julien Grall | 0df4f26 | 2015-08-07 17:34:37 +0100 | [diff] [blame] | 394 | mfn = virt_to_gfn(info->intf); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 395 | else |
| 396 | mfn = __pa(info->intf) >> PAGE_SHIFT; |
| 397 | ret = gnttab_alloc_grant_references(1, &gref_head); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | info->gntref = gref_head; |
| 401 | ref = gnttab_claim_grant_reference(&gref_head); |
| 402 | if (ref < 0) |
| 403 | return ref; |
| 404 | gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id, |
| 405 | mfn, 0); |
| 406 | |
| 407 | again: |
| 408 | ret = xenbus_transaction_start(&xbt); |
| 409 | if (ret) { |
| 410 | xenbus_dev_fatal(dev, ret, "starting transaction"); |
| 411 | return ret; |
| 412 | } |
| 413 | ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref); |
| 414 | if (ret) |
| 415 | goto error_xenbus; |
| 416 | ret = xenbus_printf(xbt, dev->nodename, "port", "%u", |
| 417 | evtchn); |
| 418 | if (ret) |
| 419 | goto error_xenbus; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 420 | ret = xenbus_transaction_end(xbt, 0); |
| 421 | if (ret) { |
| 422 | if (ret == -EAGAIN) |
| 423 | goto again; |
| 424 | xenbus_dev_fatal(dev, ret, "completing transaction"); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | xenbus_switch_state(dev, XenbusStateInitialised); |
| 429 | return 0; |
| 430 | |
| 431 | error_xenbus: |
| 432 | xenbus_transaction_end(xbt, 1); |
| 433 | xenbus_dev_fatal(dev, ret, "writing xenstore"); |
| 434 | return ret; |
| 435 | } |
| 436 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 437 | static int xencons_probe(struct xenbus_device *dev, |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 438 | const struct xenbus_device_id *id) |
| 439 | { |
| 440 | int ret, devid; |
| 441 | struct xencons_info *info; |
| 442 | |
| 443 | devid = dev->nodename[strlen(dev->nodename) - 1] - '0'; |
| 444 | if (devid == 0) |
| 445 | return -ENODEV; |
| 446 | |
Dan Carpenter | 201a52b | 2012-05-15 11:47:47 +0300 | [diff] [blame] | 447 | info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL); |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 448 | if (!info) |
Dan Carpenter | 201a52b | 2012-05-15 11:47:47 +0300 | [diff] [blame] | 449 | return -ENOMEM; |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 450 | dev_set_drvdata(&dev->dev, info); |
| 451 | info->xbdev = dev; |
| 452 | info->vtermno = xenbus_devid_to_vtermno(devid); |
| 453 | info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); |
| 454 | if (!info->intf) |
| 455 | goto error_nomem; |
| 456 | |
| 457 | ret = xencons_connect_backend(dev, info); |
| 458 | if (ret < 0) |
| 459 | goto error; |
| 460 | spin_lock(&xencons_lock); |
| 461 | list_add_tail(&info->list, &xenconsoles); |
| 462 | spin_unlock(&xencons_lock); |
| 463 | |
| 464 | return 0; |
| 465 | |
| 466 | error_nomem: |
| 467 | ret = -ENOMEM; |
| 468 | xenbus_dev_fatal(dev, ret, "allocating device memory"); |
| 469 | error: |
| 470 | xencons_disconnect_backend(info); |
| 471 | xencons_free(info); |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static int xencons_resume(struct xenbus_device *dev) |
| 476 | { |
| 477 | struct xencons_info *info = dev_get_drvdata(&dev->dev); |
| 478 | |
| 479 | xencons_disconnect_backend(info); |
| 480 | memset(info->intf, 0, PAGE_SIZE); |
| 481 | return xencons_connect_backend(dev, info); |
| 482 | } |
| 483 | |
| 484 | static void xencons_backend_changed(struct xenbus_device *dev, |
| 485 | enum xenbus_state backend_state) |
| 486 | { |
| 487 | switch (backend_state) { |
| 488 | case XenbusStateReconfiguring: |
| 489 | case XenbusStateReconfigured: |
| 490 | case XenbusStateInitialising: |
| 491 | case XenbusStateInitialised: |
| 492 | case XenbusStateUnknown: |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 493 | break; |
| 494 | |
| 495 | case XenbusStateInitWait: |
| 496 | break; |
| 497 | |
| 498 | case XenbusStateConnected: |
| 499 | xenbus_switch_state(dev, XenbusStateConnected); |
| 500 | break; |
| 501 | |
David Vrabel | 9b6934a | 2012-09-21 17:04:24 +0100 | [diff] [blame] | 502 | case XenbusStateClosed: |
| 503 | if (dev->state == XenbusStateClosed) |
| 504 | break; |
| 505 | /* Missed the backend's CLOSING state -- fallthrough */ |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 506 | case XenbusStateClosing: |
| 507 | xenbus_frontend_closed(dev); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static const struct xenbus_device_id xencons_ids[] = { |
| 513 | { "console" }, |
| 514 | { "" } |
| 515 | }; |
| 516 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 517 | static struct xenbus_driver xencons_driver = { |
| 518 | .name = "xenconsole", |
| 519 | .ids = xencons_ids, |
Stefano Stabellini | cf8e019 | 2012-02-21 11:30:42 +0000 | [diff] [blame] | 520 | .probe = xencons_probe, |
| 521 | .remove = xencons_remove, |
| 522 | .resume = xencons_resume, |
| 523 | .otherend_changed = xencons_backend_changed, |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 524 | }; |
Stefano Stabellini | cf8e019 | 2012-02-21 11:30:42 +0000 | [diff] [blame] | 525 | #endif /* CONFIG_HVC_XEN_FRONTEND */ |
| 526 | |
| 527 | static int __init xen_hvc_init(void) |
| 528 | { |
| 529 | int r; |
| 530 | struct xencons_info *info; |
| 531 | const struct hv_ops *ops; |
| 532 | |
| 533 | if (!xen_domain()) |
| 534 | return -ENODEV; |
| 535 | |
| 536 | if (xen_initial_domain()) { |
| 537 | ops = &dom0_hvc_ops; |
| 538 | r = xen_initial_domain_console_init(); |
| 539 | if (r < 0) |
| 540 | return r; |
| 541 | info = vtermno_to_xencons(HVC_COOKIE); |
| 542 | } else { |
| 543 | ops = &domU_hvc_ops; |
| 544 | if (xen_hvm_domain()) |
| 545 | r = xen_hvm_console_init(); |
| 546 | else |
| 547 | r = xen_pv_console_init(); |
| 548 | if (r < 0) |
| 549 | return r; |
| 550 | |
| 551 | info = vtermno_to_xencons(HVC_COOKIE); |
| 552 | info->irq = bind_evtchn_to_irq(info->evtchn); |
| 553 | } |
| 554 | if (info->irq < 0) |
| 555 | info->irq = 0; /* NO_IRQ */ |
| 556 | else |
| 557 | irq_set_noprobe(info->irq); |
| 558 | |
| 559 | info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256); |
| 560 | if (IS_ERR(info->hvc)) { |
| 561 | r = PTR_ERR(info->hvc); |
| 562 | spin_lock(&xencons_lock); |
| 563 | list_del(&info->list); |
| 564 | spin_unlock(&xencons_lock); |
| 565 | if (info->irq) |
| 566 | unbind_from_irqhandler(info->irq, NULL); |
| 567 | kfree(info); |
| 568 | return r; |
| 569 | } |
| 570 | |
| 571 | r = 0; |
| 572 | #ifdef CONFIG_HVC_XEN_FRONTEND |
| 573 | r = xenbus_register_frontend(&xencons_driver); |
| 574 | #endif |
| 575 | return r; |
| 576 | } |
Paul Gortmaker | 4fedd0b | 2014-01-15 16:35:43 -0500 | [diff] [blame] | 577 | device_initcall(xen_hvc_init); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 578 | |
| 579 | static int xen_cons_init(void) |
| 580 | { |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 581 | const struct hv_ops *ops; |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 582 | |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 583 | if (!xen_domain()) |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 584 | return 0; |
| 585 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 586 | if (xen_initial_domain()) |
| 587 | ops = &dom0_hvc_ops; |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 588 | else { |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 589 | int r; |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 590 | ops = &domU_hvc_ops; |
| 591 | |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 592 | if (xen_hvm_domain()) |
| 593 | r = xen_hvm_console_init(); |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 594 | else |
Stefano Stabellini | 02e19f9 | 2012-01-30 16:02:31 +0000 | [diff] [blame] | 595 | r = xen_pv_console_init(); |
| 596 | if (r < 0) |
| 597 | return r; |
Stefano Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 600 | hvc_instantiate(HVC_COOKIE, 0, ops); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 603 | console_initcall(xen_cons_init); |
| 604 | |
Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 605 | #ifdef CONFIG_EARLY_PRINTK |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 606 | static 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 Stabellini | eb5ef07 | 2012-01-27 18:31:36 +0000 | [diff] [blame] | 612 | if (!xen_pv_domain()) |
| 613 | return; |
| 614 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 615 | dom0_write_console(0, string, len); |
Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 616 | |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 617 | if (xen_initial_domain()) |
| 618 | return; |
| 619 | |
| 620 | domU_write_console(0, "(early) ", 8); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 621 | while (off < len && NULL != (pos = strchr(string+off, '\n'))) { |
| 622 | linelen = pos-string+off; |
| 623 | if (off + linelen > len) |
| 624 | break; |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 625 | domU_write_console(0, string+off, linelen); |
| 626 | domU_write_console(0, "\r\n", 2); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 627 | off += linelen + 1; |
| 628 | } |
| 629 | if (off < len) |
Jeremy Fitzhardinge | 4fe7d5a | 2010-09-02 16:17:06 +0100 | [diff] [blame] | 630 | domU_write_console(0, string+off, len-off); |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | struct console xenboot_console = { |
| 634 | .name = "xenboot", |
| 635 | .write = xenboot_write_console, |
Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 636 | .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, |
David Vrabel | a9fbf4d | 2013-10-01 19:00:49 +0100 | [diff] [blame] | 637 | .index = -1, |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 638 | }; |
Jeremy Fitzhardinge | 0922abd | 2008-05-26 23:31:00 +0100 | [diff] [blame] | 639 | #endif /* CONFIG_EARLY_PRINTK */ |
Jeremy Fitzhardinge | 0acf10d | 2008-05-26 23:30:59 +0100 | [diff] [blame] | 640 | |
| 641 | void xen_raw_console_write(const char *str) |
| 642 | { |
Konrad Rzeszutek Wilk | 04b772d | 2013-09-27 17:18:13 -0400 | [diff] [blame] | 643 | ssize_t len = strlen(str); |
| 644 | int rc = 0; |
| 645 | |
| 646 | if (xen_domain()) { |
| 647 | rc = dom0_write_console(0, str, len); |
| 648 | #ifdef CONFIG_X86 |
| 649 | if (rc == -ENOSYS && xen_hvm_domain()) |
| 650 | goto outb_print; |
| 651 | |
| 652 | } else if (xen_cpuid_base()) { |
| 653 | int i; |
| 654 | outb_print: |
| 655 | for (i = 0; i < len; i++) |
| 656 | outb(str[i], 0xe9); |
| 657 | #endif |
| 658 | } |
Jeremy Fitzhardinge | 0acf10d | 2008-05-26 23:30:59 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void xen_raw_printk(const char *fmt, ...) |
| 662 | { |
| 663 | static char buf[512]; |
| 664 | va_list ap; |
| 665 | |
| 666 | va_start(ap, fmt); |
| 667 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 668 | va_end(ap); |
| 669 | |
| 670 | xen_raw_console_write(buf); |
| 671 | } |