GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / tty / hvc / hvc_xen.c
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/irq.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/serial_core.h>
29
30 #include <asm/io.h>
31 #include <asm/xen/hypervisor.h>
32
33 #include <xen/xen.h>
34 #include <xen/interface/xen.h>
35 #include <xen/hvm.h>
36 #include <xen/grant_table.h>
37 #include <xen/page.h>
38 #include <xen/events.h>
39 #include <xen/interface/io/console.h>
40 #include <xen/interface/sched.h>
41 #include <xen/hvc-console.h>
42 #include <xen/xenbus.h>
43
44 #include "hvc_console.h"
45
46 #define HVC_COOKIE   0x58656e /* "Xen" in hex */
47
48 struct xencons_info {
49         struct list_head list;
50         struct xenbus_device *xbdev;
51         struct xencons_interface *intf;
52         unsigned int evtchn;
53         XENCONS_RING_IDX out_cons;
54         unsigned int out_cons_same;
55         struct hvc_struct *hvc;
56         int irq;
57         int vtermno;
58         grant_ref_t gntref;
59 };
60
61 static LIST_HEAD(xenconsoles);
62 static DEFINE_SPINLOCK(xencons_lock);
63
64 /* ------------------------------------------------------------------ */
65
66 static struct xencons_info *vtermno_to_xencons(int vtermno)
67 {
68         struct xencons_info *entry, *n, *ret = NULL;
69
70         if (list_empty(&xenconsoles))
71                         return NULL;
72
73         list_for_each_entry_safe(entry, n, &xenconsoles, list) {
74                 if (entry->vtermno == vtermno) {
75                         ret  = entry;
76                         break;
77                 }
78         }
79
80         return ret;
81 }
82
83 static inline int xenbus_devid_to_vtermno(int devid)
84 {
85         return devid + HVC_COOKIE;
86 }
87
88 static inline void notify_daemon(struct xencons_info *cons)
89 {
90         /* Use evtchn: this is called early, before irq is set up. */
91         notify_remote_via_evtchn(cons->evtchn);
92 }
93
94 static int __write_console(struct xencons_info *xencons,
95                 const char *data, int len)
96 {
97         XENCONS_RING_IDX cons, prod;
98         struct xencons_interface *intf = xencons->intf;
99         int sent = 0;
100
101         cons = intf->out_cons;
102         prod = intf->out_prod;
103         mb();                   /* update queue values before going on */
104
105         if ((prod - cons) > sizeof(intf->out)) {
106                 pr_err_once("xencons: Illegal ring page indices");
107                 return -EINVAL;
108         }
109
110         while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
111                 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
112
113         wmb();                  /* write ring before updating pointer */
114         intf->out_prod = prod;
115
116         if (sent)
117                 notify_daemon(xencons);
118         return sent;
119 }
120
121 static int domU_write_console(uint32_t vtermno, const char *data, int len)
122 {
123         int ret = len;
124         struct xencons_info *cons = vtermno_to_xencons(vtermno);
125         if (cons == NULL)
126                 return -EINVAL;
127
128         /*
129          * Make sure the whole buffer is emitted, polling if
130          * necessary.  We don't ever want to rely on the hvc daemon
131          * because the most interesting console output is when the
132          * kernel is crippled.
133          */
134         while (len) {
135                 int sent = __write_console(cons, data, len);
136
137                 if (sent < 0)
138                         return sent;
139
140                 data += sent;
141                 len -= sent;
142
143                 if (unlikely(len))
144                         HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
145         }
146
147         return ret;
148 }
149
150 static int domU_read_console(uint32_t vtermno, char *buf, int len)
151 {
152         struct xencons_interface *intf;
153         XENCONS_RING_IDX cons, prod;
154         int recv = 0;
155         struct xencons_info *xencons = vtermno_to_xencons(vtermno);
156         unsigned int eoiflag = 0;
157
158         if (xencons == NULL)
159                 return -EINVAL;
160         intf = xencons->intf;
161
162         cons = intf->in_cons;
163         prod = intf->in_prod;
164         mb();                   /* get pointers before reading ring */
165
166         if ((prod - cons) > sizeof(intf->in)) {
167                 pr_err_once("xencons: Illegal ring page indices");
168                 return -EINVAL;
169         }
170
171         while (cons != prod && recv < len)
172                 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
173
174         mb();                   /* read ring before consuming */
175         intf->in_cons = cons;
176
177         /*
178          * When to mark interrupt having been spurious:
179          * - there was no new data to be read, and
180          * - the backend did not consume some output bytes, and
181          * - the previous round with no read data didn't see consumed bytes
182          *   (we might have a race with an interrupt being in flight while
183          *   updating xencons->out_cons, so account for that by allowing one
184          *   round without any visible reason)
185          */
186         if (intf->out_cons != xencons->out_cons) {
187                 xencons->out_cons = intf->out_cons;
188                 xencons->out_cons_same = 0;
189         }
190         if (recv) {
191                 notify_daemon(xencons);
192         } else if (xencons->out_cons_same++ > 1) {
193                 eoiflag = XEN_EOI_FLAG_SPURIOUS;
194         }
195
196         xen_irq_lateeoi(xencons->irq, eoiflag);
197
198         return recv;
199 }
200
201 static const struct hv_ops domU_hvc_ops = {
202         .get_chars = domU_read_console,
203         .put_chars = domU_write_console,
204         .notifier_add = notifier_add_irq,
205         .notifier_del = notifier_del_irq,
206         .notifier_hangup = notifier_hangup_irq,
207 };
208
209 static int dom0_read_console(uint32_t vtermno, char *buf, int len)
210 {
211         return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
212 }
213
214 /*
215  * Either for a dom0 to write to the system console, or a domU with a
216  * debug version of Xen
217  */
218 static int dom0_write_console(uint32_t vtermno, const char *str, int len)
219 {
220         int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
221         if (rc < 0)
222                 return rc;
223
224         return len;
225 }
226
227 static const struct hv_ops dom0_hvc_ops = {
228         .get_chars = dom0_read_console,
229         .put_chars = dom0_write_console,
230         .notifier_add = notifier_add_irq,
231         .notifier_del = notifier_del_irq,
232         .notifier_hangup = notifier_hangup_irq,
233 };
234
235 static int xen_hvm_console_init(void)
236 {
237         int r;
238         uint64_t v = 0;
239         unsigned long gfn;
240         struct xencons_info *info;
241
242         if (!xen_hvm_domain())
243                 return -ENODEV;
244
245         info = vtermno_to_xencons(HVC_COOKIE);
246         if (!info) {
247                 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
248                 if (!info)
249                         return -ENOMEM;
250         } else if (info->intf != NULL) {
251                 /* already configured */
252                 return 0;
253         }
254         /*
255          * If the toolstack (or the hypervisor) hasn't set these values, the
256          * default value is 0. Even though gfn = 0 and evtchn = 0 are
257          * theoretically correct values, in practice they never are and they
258          * mean that a legacy toolstack hasn't initialized the pv console correctly.
259          */
260         r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
261         if (r < 0 || v == 0)
262                 goto err;
263         info->evtchn = v;
264         v = 0;
265         r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
266         if (r < 0 || v == 0)
267                 goto err;
268         gfn = v;
269         info->intf = xen_remap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE);
270         if (info->intf == NULL)
271                 goto err;
272         info->vtermno = HVC_COOKIE;
273
274         spin_lock(&xencons_lock);
275         list_add_tail(&info->list, &xenconsoles);
276         spin_unlock(&xencons_lock);
277
278         return 0;
279 err:
280         kfree(info);
281         return -ENODEV;
282 }
283
284 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
285 {
286         info->evtchn = xen_start_info->console.domU.evtchn;
287         /* GFN == MFN for PV guest */
288         info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
289         info->vtermno = vtermno;
290
291         list_add_tail(&info->list, &xenconsoles);
292
293         return 0;
294 }
295
296 static int xen_pv_console_init(void)
297 {
298         struct xencons_info *info;
299
300         if (!xen_pv_domain())
301                 return -ENODEV;
302
303         if (!xen_start_info->console.domU.evtchn)
304                 return -ENODEV;
305
306         info = vtermno_to_xencons(HVC_COOKIE);
307         if (!info) {
308                 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
309                 if (!info)
310                         return -ENOMEM;
311         } else if (info->intf != NULL) {
312                 /* already configured */
313                 return 0;
314         }
315         spin_lock(&xencons_lock);
316         xencons_info_pv_init(info, HVC_COOKIE);
317         spin_unlock(&xencons_lock);
318
319         return 0;
320 }
321
322 static int xen_initial_domain_console_init(void)
323 {
324         struct xencons_info *info;
325
326         if (!xen_initial_domain())
327                 return -ENODEV;
328
329         info = vtermno_to_xencons(HVC_COOKIE);
330         if (!info) {
331                 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
332                 if (!info)
333                         return -ENOMEM;
334         }
335
336         info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
337         info->vtermno = HVC_COOKIE;
338
339         spin_lock(&xencons_lock);
340         list_add_tail(&info->list, &xenconsoles);
341         spin_unlock(&xencons_lock);
342
343         return 0;
344 }
345
346 static void xen_console_update_evtchn(struct xencons_info *info)
347 {
348         if (xen_hvm_domain()) {
349                 uint64_t v = 0;
350                 int err;
351
352                 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
353                 if (!err && v)
354                         info->evtchn = v;
355         } else
356                 info->evtchn = xen_start_info->console.domU.evtchn;
357 }
358
359 void xen_console_resume(void)
360 {
361         struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
362         if (info != NULL && info->irq) {
363                 if (!xen_initial_domain())
364                         xen_console_update_evtchn(info);
365                 rebind_evtchn_irq(info->evtchn, info->irq);
366         }
367 }
368
369 #ifdef CONFIG_HVC_XEN_FRONTEND
370 static void xencons_disconnect_backend(struct xencons_info *info)
371 {
372         if (info->irq > 0)
373                 unbind_from_irqhandler(info->irq, NULL);
374         info->irq = 0;
375         if (info->evtchn > 0)
376                 xenbus_free_evtchn(info->xbdev, info->evtchn);
377         info->evtchn = 0;
378         if (info->gntref > 0)
379                 gnttab_free_grant_references(info->gntref);
380         info->gntref = 0;
381         if (info->hvc != NULL)
382                 hvc_remove(info->hvc);
383         info->hvc = NULL;
384 }
385
386 static void xencons_free(struct xencons_info *info)
387 {
388         free_page((unsigned long)info->intf);
389         info->intf = NULL;
390         info->vtermno = 0;
391         kfree(info);
392 }
393
394 static int xen_console_remove(struct xencons_info *info)
395 {
396         xencons_disconnect_backend(info);
397         spin_lock(&xencons_lock);
398         list_del(&info->list);
399         spin_unlock(&xencons_lock);
400         if (info->xbdev != NULL)
401                 xencons_free(info);
402         else {
403                 if (xen_hvm_domain())
404                         iounmap(info->intf);
405                 kfree(info);
406         }
407         return 0;
408 }
409
410 static int xencons_remove(struct xenbus_device *dev)
411 {
412         return xen_console_remove(dev_get_drvdata(&dev->dev));
413 }
414
415 static int xencons_connect_backend(struct xenbus_device *dev,
416                                   struct xencons_info *info)
417 {
418         int ret, evtchn, devid, ref, irq;
419         struct xenbus_transaction xbt;
420         grant_ref_t gref_head;
421
422         ret = xenbus_alloc_evtchn(dev, &evtchn);
423         if (ret)
424                 return ret;
425         info->evtchn = evtchn;
426         irq = bind_interdomain_evtchn_to_irq_lateeoi(dev->otherend_id, evtchn);
427         if (irq < 0)
428                 return irq;
429         info->irq = irq;
430         devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
431         info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
432                         irq, &domU_hvc_ops, 256);
433         if (IS_ERR(info->hvc))
434                 return PTR_ERR(info->hvc);
435         ret = gnttab_alloc_grant_references(1, &gref_head);
436         if (ret < 0)
437                 return ret;
438         info->gntref = gref_head;
439         ref = gnttab_claim_grant_reference(&gref_head);
440         if (ref < 0)
441                 return ref;
442         gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
443                                         virt_to_gfn(info->intf), 0);
444
445  again:
446         ret = xenbus_transaction_start(&xbt);
447         if (ret) {
448                 xenbus_dev_fatal(dev, ret, "starting transaction");
449                 return ret;
450         }
451         ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
452         if (ret)
453                 goto error_xenbus;
454         ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
455                             evtchn);
456         if (ret)
457                 goto error_xenbus;
458         ret = xenbus_transaction_end(xbt, 0);
459         if (ret) {
460                 if (ret == -EAGAIN)
461                         goto again;
462                 xenbus_dev_fatal(dev, ret, "completing transaction");
463                 return ret;
464         }
465
466         xenbus_switch_state(dev, XenbusStateInitialised);
467         return 0;
468
469  error_xenbus:
470         xenbus_transaction_end(xbt, 1);
471         xenbus_dev_fatal(dev, ret, "writing xenstore");
472         return ret;
473 }
474
475 static int xencons_probe(struct xenbus_device *dev,
476                                   const struct xenbus_device_id *id)
477 {
478         int ret, devid;
479         struct xencons_info *info;
480
481         devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
482         if (devid == 0)
483                 return -ENODEV;
484
485         info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
486         if (!info)
487                 return -ENOMEM;
488         dev_set_drvdata(&dev->dev, info);
489         info->xbdev = dev;
490         info->vtermno = xenbus_devid_to_vtermno(devid);
491         info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
492         if (!info->intf)
493                 goto error_nomem;
494
495         ret = xencons_connect_backend(dev, info);
496         if (ret < 0)
497                 goto error;
498         spin_lock(&xencons_lock);
499         list_add_tail(&info->list, &xenconsoles);
500         spin_unlock(&xencons_lock);
501
502         return 0;
503
504  error_nomem:
505         ret = -ENOMEM;
506         xenbus_dev_fatal(dev, ret, "allocating device memory");
507  error:
508         xencons_disconnect_backend(info);
509         xencons_free(info);
510         return ret;
511 }
512
513 static int xencons_resume(struct xenbus_device *dev)
514 {
515         struct xencons_info *info = dev_get_drvdata(&dev->dev);
516
517         xencons_disconnect_backend(info);
518         memset(info->intf, 0, XEN_PAGE_SIZE);
519         return xencons_connect_backend(dev, info);
520 }
521
522 static void xencons_backend_changed(struct xenbus_device *dev,
523                                    enum xenbus_state backend_state)
524 {
525         switch (backend_state) {
526         case XenbusStateReconfiguring:
527         case XenbusStateReconfigured:
528         case XenbusStateInitialising:
529         case XenbusStateInitialised:
530         case XenbusStateUnknown:
531                 break;
532
533         case XenbusStateInitWait:
534                 break;
535
536         case XenbusStateConnected:
537                 xenbus_switch_state(dev, XenbusStateConnected);
538                 break;
539
540         case XenbusStateClosed:
541                 if (dev->state == XenbusStateClosed)
542                         break;
543                 /* Missed the backend's CLOSING state -- fallthrough */
544         case XenbusStateClosing:
545                 xenbus_frontend_closed(dev);
546                 break;
547         }
548 }
549
550 static const struct xenbus_device_id xencons_ids[] = {
551         { "console" },
552         { "" }
553 };
554
555 static struct xenbus_driver xencons_driver = {
556         .name = "xenconsole",
557         .ids = xencons_ids,
558         .probe = xencons_probe,
559         .remove = xencons_remove,
560         .resume = xencons_resume,
561         .otherend_changed = xencons_backend_changed,
562 };
563 #endif /* CONFIG_HVC_XEN_FRONTEND */
564
565 static int __init xen_hvc_init(void)
566 {
567         int r;
568         struct xencons_info *info;
569         const struct hv_ops *ops;
570
571         if (!xen_domain())
572                 return -ENODEV;
573
574         if (xen_initial_domain()) {
575                 ops = &dom0_hvc_ops;
576                 r = xen_initial_domain_console_init();
577                 if (r < 0)
578                         return r;
579                 info = vtermno_to_xencons(HVC_COOKIE);
580         } else {
581                 ops = &domU_hvc_ops;
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                 info = vtermno_to_xencons(HVC_COOKIE);
590                 info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn);
591         }
592         if (info->irq < 0)
593                 info->irq = 0; /* NO_IRQ */
594         else
595                 irq_set_noprobe(info->irq);
596
597         info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
598         if (IS_ERR(info->hvc)) {
599                 r = PTR_ERR(info->hvc);
600                 spin_lock(&xencons_lock);
601                 list_del(&info->list);
602                 spin_unlock(&xencons_lock);
603                 if (info->irq)
604                         unbind_from_irqhandler(info->irq, NULL);
605                 kfree(info);
606                 return r;
607         }
608
609         r = 0;
610 #ifdef CONFIG_HVC_XEN_FRONTEND
611         r = xenbus_register_frontend(&xencons_driver);
612 #endif
613         return r;
614 }
615 device_initcall(xen_hvc_init);
616
617 static int xen_cons_init(void)
618 {
619         const struct hv_ops *ops;
620
621         if (!xen_domain())
622                 return 0;
623
624         if (xen_initial_domain())
625                 ops = &dom0_hvc_ops;
626         else {
627                 int r;
628                 ops = &domU_hvc_ops;
629
630                 if (xen_hvm_domain())
631                         r = xen_hvm_console_init();
632                 else
633                         r = xen_pv_console_init();
634                 if (r < 0)
635                         return r;
636         }
637
638         hvc_instantiate(HVC_COOKIE, 0, ops);
639         return 0;
640 }
641 console_initcall(xen_cons_init);
642
643 #ifdef CONFIG_X86
644 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
645 {
646         if (xen_cpuid_base())
647                 outsb(0xe9, str, len);
648 }
649 #else
650 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
651 #endif
652
653 #ifdef CONFIG_EARLY_PRINTK
654 static int __init xenboot_setup_console(struct console *console, char *string)
655 {
656         static struct xencons_info xenboot;
657
658         if (xen_initial_domain())
659                 return 0;
660         if (!xen_pv_domain())
661                 return -ENODEV;
662
663         return xencons_info_pv_init(&xenboot, 0);
664 }
665
666 static void xenboot_write_console(struct console *console, const char *string,
667                                   unsigned len)
668 {
669         unsigned int linelen, off = 0;
670         const char *pos;
671
672         if (!xen_pv_domain()) {
673                 xen_hvm_early_write(0, string, len);
674                 return;
675         }
676
677         dom0_write_console(0, string, len);
678
679         if (xen_initial_domain())
680                 return;
681
682         domU_write_console(0, "(early) ", 8);
683         while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
684                 linelen = pos-string+off;
685                 if (off + linelen > len)
686                         break;
687                 domU_write_console(0, string+off, linelen);
688                 domU_write_console(0, "\r\n", 2);
689                 off += linelen + 1;
690         }
691         if (off < len)
692                 domU_write_console(0, string+off, len-off);
693 }
694
695 struct console xenboot_console = {
696         .name           = "xenboot",
697         .write          = xenboot_write_console,
698         .setup          = xenboot_setup_console,
699         .flags          = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
700         .index          = -1,
701 };
702 #endif  /* CONFIG_EARLY_PRINTK */
703
704 void xen_raw_console_write(const char *str)
705 {
706         ssize_t len = strlen(str);
707         int rc = 0;
708
709         if (xen_domain()) {
710                 rc = dom0_write_console(0, str, len);
711                 if (rc != -ENOSYS || !xen_hvm_domain())
712                         return;
713         }
714         xen_hvm_early_write(0, str, len);
715 }
716
717 void xen_raw_printk(const char *fmt, ...)
718 {
719         static char buf[512];
720         va_list ap;
721
722         va_start(ap, fmt);
723         vsnprintf(buf, sizeof(buf), fmt, ap);
724         va_end(ap);
725
726         xen_raw_console_write(buf);
727 }
728
729 static void xenboot_earlycon_write(struct console *console,
730                                   const char *string,
731                                   unsigned len)
732 {
733         dom0_write_console(0, string, len);
734 }
735
736 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
737                                             const char *opt)
738 {
739         device->con->write = xenboot_earlycon_write;
740         return 0;
741 }
742 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);