blob: 5bab1c684bb11024c2e5a7ee143c18aefc54349b [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>
Stefano Stabellini4d9310e2012-08-06 15:27:09 +010024#include <linux/irq.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070025#include <linux/init.h>
26#include <linux/types.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000027#include <linux/list.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070028
Stefano Stabellinieb5ef072012-01-27 18:31:36 +000029#include <asm/io.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070030#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070031
32#include <xen/xen.h>
Stefano Stabellinieb5ef072012-01-27 18:31:36 +000033#include <xen/interface/xen.h>
34#include <xen/hvm.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000035#include <xen/grant_table.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070036#include <xen/page.h>
37#include <xen/events.h>
38#include <xen/interface/io/console.h>
Stefano Stabellini4d9310e2012-08-06 15:27:09 +010039#include <xen/interface/sched.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070040#include <xen/hvc-console.h>
Stefano Stabellini02e19f92012-01-30 16:02:31 +000041#include <xen/xenbus.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070042
43#include "hvc_console.h"
44
45#define HVC_COOKIE 0x58656e /* "Xen" in hex */
46
Stefano Stabellini02e19f92012-01-30 16:02:31 +000047struct 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
58static LIST_HEAD(xenconsoles);
59static DEFINE_SPINLOCK(xencons_lock);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070060
61/* ------------------------------------------------------------------ */
62
Stefano Stabellini02e19f92012-01-30 16:02:31 +000063static struct xencons_info *vtermno_to_xencons(int vtermno)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070064{
Stefano Stabellini02e19f92012-01-30 16:02:31 +000065 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 Fitzhardingeb536b4b2007-07-17 18:37:06 -070078}
79
Stefano Stabellini02e19f92012-01-30 16:02:31 +000080static inline int xenbus_devid_to_vtermno(int devid)
81{
82 return devid + HVC_COOKIE;
83}
84
85static inline void notify_daemon(struct xencons_info *cons)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070086{
87 /* Use evtchn: this is called early, before irq is set up. */
Stefano Stabellini02e19f92012-01-30 16:02:31 +000088 notify_remote_via_evtchn(cons->evtchn);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070089}
90
Stefano Stabellini02e19f92012-01-30 16:02:31 +000091static int __write_console(struct xencons_info *xencons,
92 const char *data, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070093{
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070094 XENCONS_RING_IDX cons, prod;
Stefano Stabellini02e19f92012-01-30 16:02:31 +000095 struct xencons_interface *intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070096 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 Fitzhardinge403a85f2010-10-14 11:38:47 -0700109 if (sent)
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000110 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700111 return sent;
112}
113
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100114static int domU_write_console(uint32_t vtermno, const char *data, int len)
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900115{
116 int ret = len;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000117 struct xencons_info *cons = vtermno_to_xencons(vtermno);
118 if (cons == NULL)
119 return -EINVAL;
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900120
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 Stabellini02e19f92012-01-30 16:02:31 +0000128 int sent = __write_console(cons, data, len);
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +0900129
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 Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100140static int domU_read_console(uint32_t vtermno, char *buf, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700141{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000142 struct xencons_interface *intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700143 XENCONS_RING_IDX cons, prod;
144 int recv = 0;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000145 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
146 if (xencons == NULL)
147 return -EINVAL;
148 intf = xencons->intf;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700149
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 Stabellini02e19f92012-01-30 16:02:31 +0000161 notify_daemon(xencons);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700162 return recv;
163}
164
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100165static struct hv_ops domU_hvc_ops = {
166 .get_chars = domU_read_console,
167 .put_chars = domU_write_console,
Christian Borntraeger611e0972008-06-20 15:24:08 +0200168 .notifier_add = notifier_add_irq,
169 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000170 .notifier_hangup = notifier_hangup_irq,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700171};
172
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100173static 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 */
182static 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 Wilk04b772d2013-09-27 17:18:13 -0400186 return rc;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100187
188 return len;
189}
190
191static 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 Stabellinieb5ef072012-01-27 18:31:36 +0000199static int xen_hvm_console_init(void)
200{
201 int r;
202 uint64_t v = 0;
203 unsigned long mfn;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000204 struct xencons_info *info;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000205
206 if (!xen_hvm_domain())
207 return -ENODEV;
208
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000209 info = vtermno_to_xencons(HVC_COOKIE);
210 if (!info) {
Joe Perches2d1d3f32013-08-29 14:08:18 -0700211 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000212 if (!info)
213 return -ENOMEM;
Konrad Rzeszutek Wilk37a80bf2012-06-26 09:30:51 -0400214 } else if (info->intf != NULL) {
215 /* already configured */
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000216 return 0;
Konrad Rzeszutek Wilk37a80bf2012-06-26 09:30:51 -0400217 }
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400218 /*
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 Stabellinieb5ef072012-01-27 18:31:36 +0000224 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400225 if (r < 0 || v == 0)
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400226 goto err;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000227 info->evtchn = v;
Konrad Rzeszutek Wilka32c88b2012-05-23 12:55:38 -0400228 v = 0;
229 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
Konrad Rzeszutek Wilk5842f5762012-05-23 12:56:59 -0400230 if (r < 0 || v == 0)
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400231 goto err;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000232 mfn = v;
Stefano Stabellini3216dce2013-02-19 13:59:19 +0000233 info->intf = xen_remap(mfn << PAGE_SHIFT, PAGE_SIZE);
Konrad Rzeszutek Wilk2e5ad6b2012-05-23 12:53:11 -0400234 if (info->intf == NULL)
235 goto err;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000236 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 Wilk2e5ad6b2012-05-23 12:53:11 -0400243err:
244 kfree(info);
245 return -ENODEV;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000246}
247
248static 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 Perches2d1d3f32013-08-29 14:08:18 -0700260 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000261 if (!info)
262 return -ENOMEM;
Konrad Rzeszutek Wilk37a80bf2012-06-26 09:30:51 -0400263 } else if (info->intf != NULL) {
264 /* already configured */
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000265 return 0;
Konrad Rzeszutek Wilk37a80bf2012-06-26 09:30:51 -0400266 }
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000267 info->evtchn = xen_start_info->console.domU.evtchn;
268 info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
269 info->vtermno = HVC_COOKIE;
270
271 spin_lock(&xencons_lock);
272 list_add_tail(&info->list, &xenconsoles);
273 spin_unlock(&xencons_lock);
274
275 return 0;
276}
277
278static int xen_initial_domain_console_init(void)
279{
280 struct xencons_info *info;
281
282 if (!xen_initial_domain())
283 return -ENODEV;
284
285 info = vtermno_to_xencons(HVC_COOKIE);
286 if (!info) {
Joe Perches2d1d3f32013-08-29 14:08:18 -0700287 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000288 if (!info)
289 return -ENOMEM;
290 }
291
292 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
293 info->vtermno = HVC_COOKIE;
294
295 spin_lock(&xencons_lock);
296 list_add_tail(&info->list, &xenconsoles);
297 spin_unlock(&xencons_lock);
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000298
299 return 0;
300}
301
Boris Ostrovskyb9d934f2015-04-29 17:10:14 -0400302static void xen_console_update_evtchn(struct xencons_info *info)
303{
304 if (xen_hvm_domain()) {
305 uint64_t v;
306 int err;
307
308 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
309 if (!err && v)
310 info->evtchn = v;
311 } else
312 info->evtchn = xen_start_info->console.domU.evtchn;
313}
314
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100315void xen_console_resume(void)
316{
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000317 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
Boris Ostrovskyb9d934f2015-04-29 17:10:14 -0400318 if (info != NULL && info->irq) {
319 if (!xen_initial_domain())
320 xen_console_update_evtchn(info);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000321 rebind_evtchn_irq(info->evtchn, info->irq);
Boris Ostrovskyb9d934f2015-04-29 17:10:14 -0400322 }
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100323}
324
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000325static void xencons_disconnect_backend(struct xencons_info *info)
326{
327 if (info->irq > 0)
328 unbind_from_irqhandler(info->irq, NULL);
329 info->irq = 0;
330 if (info->evtchn > 0)
331 xenbus_free_evtchn(info->xbdev, info->evtchn);
332 info->evtchn = 0;
333 if (info->gntref > 0)
334 gnttab_free_grant_references(info->gntref);
335 info->gntref = 0;
336 if (info->hvc != NULL)
337 hvc_remove(info->hvc);
338 info->hvc = NULL;
339}
340
341static void xencons_free(struct xencons_info *info)
342{
343 free_page((unsigned long)info->intf);
344 info->intf = NULL;
345 info->vtermno = 0;
346 kfree(info);
347}
348
349static int xen_console_remove(struct xencons_info *info)
350{
351 xencons_disconnect_backend(info);
352 spin_lock(&xencons_lock);
353 list_del(&info->list);
354 spin_unlock(&xencons_lock);
355 if (info->xbdev != NULL)
356 xencons_free(info);
357 else {
358 if (xen_hvm_domain())
359 iounmap(info->intf);
360 kfree(info);
361 }
362 return 0;
363}
364
Stefano Stabellinicf8e0192012-02-21 11:30:42 +0000365#ifdef CONFIG_HVC_XEN_FRONTEND
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000366static int xencons_remove(struct xenbus_device *dev)
367{
368 return xen_console_remove(dev_get_drvdata(&dev->dev));
369}
370
371static int xencons_connect_backend(struct xenbus_device *dev,
372 struct xencons_info *info)
373{
374 int ret, evtchn, devid, ref, irq;
375 struct xenbus_transaction xbt;
376 grant_ref_t gref_head;
377 unsigned long mfn;
378
379 ret = xenbus_alloc_evtchn(dev, &evtchn);
380 if (ret)
381 return ret;
382 info->evtchn = evtchn;
383 irq = bind_evtchn_to_irq(evtchn);
384 if (irq < 0)
385 return irq;
386 info->irq = irq;
387 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
388 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
389 irq, &domU_hvc_ops, 256);
390 if (IS_ERR(info->hvc))
391 return PTR_ERR(info->hvc);
392 if (xen_pv_domain())
393 mfn = virt_to_mfn(info->intf);
394 else
395 mfn = __pa(info->intf) >> PAGE_SHIFT;
396 ret = gnttab_alloc_grant_references(1, &gref_head);
397 if (ret < 0)
398 return ret;
399 info->gntref = gref_head;
400 ref = gnttab_claim_grant_reference(&gref_head);
401 if (ref < 0)
402 return ref;
403 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
404 mfn, 0);
405
406 again:
407 ret = xenbus_transaction_start(&xbt);
408 if (ret) {
409 xenbus_dev_fatal(dev, ret, "starting transaction");
410 return ret;
411 }
412 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
413 if (ret)
414 goto error_xenbus;
415 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
416 evtchn);
417 if (ret)
418 goto error_xenbus;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000419 ret = xenbus_transaction_end(xbt, 0);
420 if (ret) {
421 if (ret == -EAGAIN)
422 goto again;
423 xenbus_dev_fatal(dev, ret, "completing transaction");
424 return ret;
425 }
426
427 xenbus_switch_state(dev, XenbusStateInitialised);
428 return 0;
429
430 error_xenbus:
431 xenbus_transaction_end(xbt, 1);
432 xenbus_dev_fatal(dev, ret, "writing xenstore");
433 return ret;
434}
435
Bill Pemberton9671f092012-11-19 13:21:50 -0500436static int xencons_probe(struct xenbus_device *dev,
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000437 const struct xenbus_device_id *id)
438{
439 int ret, devid;
440 struct xencons_info *info;
441
442 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
443 if (devid == 0)
444 return -ENODEV;
445
Dan Carpenter201a52b2012-05-15 11:47:47 +0300446 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000447 if (!info)
Dan Carpenter201a52b2012-05-15 11:47:47 +0300448 return -ENOMEM;
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000449 dev_set_drvdata(&dev->dev, info);
450 info->xbdev = dev;
451 info->vtermno = xenbus_devid_to_vtermno(devid);
452 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
453 if (!info->intf)
454 goto error_nomem;
455
456 ret = xencons_connect_backend(dev, info);
457 if (ret < 0)
458 goto error;
459 spin_lock(&xencons_lock);
460 list_add_tail(&info->list, &xenconsoles);
461 spin_unlock(&xencons_lock);
462
463 return 0;
464
465 error_nomem:
466 ret = -ENOMEM;
467 xenbus_dev_fatal(dev, ret, "allocating device memory");
468 error:
469 xencons_disconnect_backend(info);
470 xencons_free(info);
471 return ret;
472}
473
474static int xencons_resume(struct xenbus_device *dev)
475{
476 struct xencons_info *info = dev_get_drvdata(&dev->dev);
477
478 xencons_disconnect_backend(info);
479 memset(info->intf, 0, PAGE_SIZE);
480 return xencons_connect_backend(dev, info);
481}
482
483static void xencons_backend_changed(struct xenbus_device *dev,
484 enum xenbus_state backend_state)
485{
486 switch (backend_state) {
487 case XenbusStateReconfiguring:
488 case XenbusStateReconfigured:
489 case XenbusStateInitialising:
490 case XenbusStateInitialised:
491 case XenbusStateUnknown:
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000492 break;
493
494 case XenbusStateInitWait:
495 break;
496
497 case XenbusStateConnected:
498 xenbus_switch_state(dev, XenbusStateConnected);
499 break;
500
David Vrabel9b6934a2012-09-21 17:04:24 +0100501 case XenbusStateClosed:
502 if (dev->state == XenbusStateClosed)
503 break;
504 /* Missed the backend's CLOSING state -- fallthrough */
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000505 case XenbusStateClosing:
506 xenbus_frontend_closed(dev);
507 break;
508 }
509}
510
511static const struct xenbus_device_id xencons_ids[] = {
512 { "console" },
513 { "" }
514};
515
David Vrabel95afae42014-09-08 17:30:41 +0100516static struct xenbus_driver xencons_driver = {
517 .name = "xenconsole",
518 .ids = xencons_ids,
Stefano Stabellinicf8e0192012-02-21 11:30:42 +0000519 .probe = xencons_probe,
520 .remove = xencons_remove,
521 .resume = xencons_resume,
522 .otherend_changed = xencons_backend_changed,
David Vrabel95afae42014-09-08 17:30:41 +0100523};
Stefano Stabellinicf8e0192012-02-21 11:30:42 +0000524#endif /* CONFIG_HVC_XEN_FRONTEND */
525
526static int __init xen_hvc_init(void)
527{
528 int r;
529 struct xencons_info *info;
530 const struct hv_ops *ops;
531
532 if (!xen_domain())
533 return -ENODEV;
534
535 if (xen_initial_domain()) {
536 ops = &dom0_hvc_ops;
537 r = xen_initial_domain_console_init();
538 if (r < 0)
539 return r;
540 info = vtermno_to_xencons(HVC_COOKIE);
541 } else {
542 ops = &domU_hvc_ops;
543 if (xen_hvm_domain())
544 r = xen_hvm_console_init();
545 else
546 r = xen_pv_console_init();
547 if (r < 0)
548 return r;
549
550 info = vtermno_to_xencons(HVC_COOKIE);
551 info->irq = bind_evtchn_to_irq(info->evtchn);
552 }
553 if (info->irq < 0)
554 info->irq = 0; /* NO_IRQ */
555 else
556 irq_set_noprobe(info->irq);
557
558 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
559 if (IS_ERR(info->hvc)) {
560 r = PTR_ERR(info->hvc);
561 spin_lock(&xencons_lock);
562 list_del(&info->list);
563 spin_unlock(&xencons_lock);
564 if (info->irq)
565 unbind_from_irqhandler(info->irq, NULL);
566 kfree(info);
567 return r;
568 }
569
570 r = 0;
571#ifdef CONFIG_HVC_XEN_FRONTEND
572 r = xenbus_register_frontend(&xencons_driver);
573#endif
574 return r;
575}
Paul Gortmaker4fedd0b2014-01-15 16:35:43 -0500576device_initcall(xen_hvc_init);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700577
578static int xen_cons_init(void)
579{
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000580 const struct hv_ops *ops;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100581
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000582 if (!xen_domain())
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700583 return 0;
584
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100585 if (xen_initial_domain())
586 ops = &dom0_hvc_ops;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000587 else {
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000588 int r;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100589 ops = &domU_hvc_ops;
590
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000591 if (xen_hvm_domain())
592 r = xen_hvm_console_init();
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000593 else
Stefano Stabellini02e19f92012-01-30 16:02:31 +0000594 r = xen_pv_console_init();
595 if (r < 0)
596 return r;
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000597 }
598
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100599 hvc_instantiate(HVC_COOKIE, 0, ops);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700600 return 0;
601}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700602console_initcall(xen_cons_init);
603
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100604#ifdef CONFIG_EARLY_PRINTK
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700605static void xenboot_write_console(struct console *console, const char *string,
606 unsigned len)
607{
608 unsigned int linelen, off = 0;
609 const char *pos;
610
Stefano Stabellinieb5ef072012-01-27 18:31:36 +0000611 if (!xen_pv_domain())
612 return;
613
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100614 dom0_write_console(0, string, len);
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100615
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100616 if (xen_initial_domain())
617 return;
618
619 domU_write_console(0, "(early) ", 8);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700620 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
621 linelen = pos-string+off;
622 if (off + linelen > len)
623 break;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100624 domU_write_console(0, string+off, linelen);
625 domU_write_console(0, "\r\n", 2);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700626 off += linelen + 1;
627 }
628 if (off < len)
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100629 domU_write_console(0, string+off, len-off);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700630}
631
632struct console xenboot_console = {
633 .name = "xenboot",
634 .write = xenboot_write_console,
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100635 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
David Vrabela9fbf4d2013-10-01 19:00:49 +0100636 .index = -1,
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{
Konrad Rzeszutek Wilk04b772d2013-09-27 17:18:13 -0400642 ssize_t len = strlen(str);
643 int rc = 0;
644
645 if (xen_domain()) {
646 rc = dom0_write_console(0, str, len);
647#ifdef CONFIG_X86
648 if (rc == -ENOSYS && xen_hvm_domain())
649 goto outb_print;
650
651 } else if (xen_cpuid_base()) {
652 int i;
653outb_print:
654 for (i = 0; i < len; i++)
655 outb(str[i], 0xe9);
656#endif
657 }
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100658}
659
660void xen_raw_printk(const char *fmt, ...)
661{
662 static char buf[512];
663 va_list ap;
664
665 va_start(ap, fmt);
666 vsnprintf(buf, sizeof(buf), fmt, ap);
667 va_end(ap);
668
669 xen_raw_console_write(buf);
670}