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