Merge branch 'work.whack-a-mole' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / tty / hvc / hvc_tile.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2010 Tilera Corporation. All Rights Reserved.
4  *
5  * Tilera TILE Processor hypervisor console
6  */
7
8 #include <linux/console.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/moduleparam.h>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
17
18 #include <asm/setup.h>
19 #include <arch/sim_def.h>
20
21 #include <hv/hypervisor.h>
22
23 #include "hvc_console.h"
24
25 static int use_sim_console;
26 static int __init sim_console(char *str)
27 {
28         use_sim_console = 1;
29         return 0;
30 }
31 early_param("sim_console", sim_console);
32
33 int tile_console_write(const char *buf, int count)
34 {
35         if (unlikely(use_sim_console)) {
36                 int i;
37                 for (i = 0; i < count; ++i)
38                         __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
39                                      (buf[i] << _SIM_CONTROL_OPERATOR_BITS));
40                 __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
41                              (SIM_PUTC_FLUSH_BINARY <<
42                               _SIM_CONTROL_OPERATOR_BITS));
43                 return 0;
44         } else {
45                 /* Translate 0 bytes written to EAGAIN for hvc_console_print. */
46                 return hv_console_write((HV_VirtAddr)buf, count) ?: -EAGAIN;
47         }
48 }
49
50 static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
51 {
52         return tile_console_write(buf, count);
53 }
54
55 static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
56 {
57         int i, c;
58
59         for (i = 0; i < count; ++i) {
60                 c = hv_console_read_if_ready();
61                 if (c < 0)
62                         break;
63                 buf[i] = c;
64         }
65
66         return i;
67 }
68
69 #ifdef __tilegx__
70 /*
71  * IRQ based callbacks.
72  */
73 static int hvc_tile_notifier_add_irq(struct hvc_struct *hp, int irq)
74 {
75         int rc;
76         int cpu = raw_smp_processor_id();  /* Choose an arbitrary cpu */
77         HV_Coord coord = { .x = cpu_x(cpu), .y = cpu_y(cpu) };
78
79         rc = notifier_add_irq(hp, irq);
80         if (rc)
81                 return rc;
82
83         /*
84          * Request that the hypervisor start sending us interrupts.
85          * If the hypervisor returns an error, we still return 0, so that
86          * we can fall back to polling.
87          */
88         if (hv_console_set_ipi(KERNEL_PL, irq, coord) < 0)
89                 notifier_del_irq(hp, irq);
90
91         return 0;
92 }
93
94 static void hvc_tile_notifier_del_irq(struct hvc_struct *hp, int irq)
95 {
96         HV_Coord coord = { 0, 0 };
97
98         /* Tell the hypervisor to stop sending us interrupts. */
99         hv_console_set_ipi(KERNEL_PL, -1, coord);
100
101         notifier_del_irq(hp, irq);
102 }
103
104 static void hvc_tile_notifier_hangup_irq(struct hvc_struct *hp, int irq)
105 {
106         hvc_tile_notifier_del_irq(hp, irq);
107 }
108 #endif
109
110 static const struct hv_ops hvc_tile_get_put_ops = {
111         .get_chars = hvc_tile_get_chars,
112         .put_chars = hvc_tile_put_chars,
113 #ifdef __tilegx__
114         .notifier_add = hvc_tile_notifier_add_irq,
115         .notifier_del = hvc_tile_notifier_del_irq,
116         .notifier_hangup = hvc_tile_notifier_hangup_irq,
117 #endif
118 };
119
120
121 #ifdef __tilegx__
122 static int hvc_tile_probe(struct platform_device *pdev)
123 {
124         struct hvc_struct *hp;
125         int tile_hvc_irq;
126
127         /* Create our IRQ and register it. */
128         tile_hvc_irq = irq_alloc_hwirq(-1);
129         if (!tile_hvc_irq)
130                 return -ENXIO;
131
132         tile_irq_activate(tile_hvc_irq, TILE_IRQ_PERCPU);
133         hp = hvc_alloc(0, tile_hvc_irq, &hvc_tile_get_put_ops, 128);
134         if (IS_ERR(hp)) {
135                 irq_free_hwirq(tile_hvc_irq);
136                 return PTR_ERR(hp);
137         }
138         dev_set_drvdata(&pdev->dev, hp);
139
140         return 0;
141 }
142
143 static int hvc_tile_remove(struct platform_device *pdev)
144 {
145         int rc;
146         struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
147
148         rc = hvc_remove(hp);
149         if (rc == 0)
150                 irq_free_hwirq(hp->data);
151
152         return rc;
153 }
154
155 static void hvc_tile_shutdown(struct platform_device *pdev)
156 {
157         struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
158
159         hvc_tile_notifier_del_irq(hp, hp->data);
160 }
161
162 static struct platform_device hvc_tile_pdev = {
163         .name           = "hvc-tile",
164         .id             = 0,
165 };
166
167 static struct platform_driver hvc_tile_driver = {
168         .probe          = hvc_tile_probe,
169         .remove         = hvc_tile_remove,
170         .shutdown       = hvc_tile_shutdown,
171         .driver         = {
172                 .name   = "hvc-tile",
173         }
174 };
175 #endif
176
177 static int __init hvc_tile_console_init(void)
178 {
179         hvc_instantiate(0, 0, &hvc_tile_get_put_ops);
180         add_preferred_console("hvc", 0, NULL);
181         return 0;
182 }
183 console_initcall(hvc_tile_console_init);
184
185 static int __init hvc_tile_init(void)
186 {
187 #ifndef __tilegx__
188         struct hvc_struct *hp;
189         hp = hvc_alloc(0, 0, &hvc_tile_get_put_ops, 128);
190         return PTR_ERR_OR_ZERO(hp);
191 #else
192         platform_device_register(&hvc_tile_pdev);
193         return platform_driver_register(&hvc_tile_driver);
194 #endif
195 }
196 device_initcall(hvc_tile_init);