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