1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
5 * Tilera TILE Processor hypervisor console
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>
18 #include <asm/setup.h>
19 #include <arch/sim_def.h>
21 #include <hv/hypervisor.h>
23 #include "hvc_console.h"
25 static int use_sim_console
;
26 static int __init
sim_console(char *str
)
31 early_param("sim_console", sim_console
);
33 int tile_console_write(const char *buf
, int count
)
35 if (unlikely(use_sim_console
)) {
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
));
45 /* Translate 0 bytes written to EAGAIN for hvc_console_print. */
46 return hv_console_write((HV_VirtAddr
)buf
, count
) ?: -EAGAIN
;
50 static int hvc_tile_put_chars(uint32_t vt
, const char *buf
, int count
)
52 return tile_console_write(buf
, count
);
55 static int hvc_tile_get_chars(uint32_t vt
, char *buf
, int count
)
59 for (i
= 0; i
< count
; ++i
) {
60 c
= hv_console_read_if_ready();
71 * IRQ based callbacks.
73 static int hvc_tile_notifier_add_irq(struct hvc_struct
*hp
, int irq
)
76 int cpu
= raw_smp_processor_id(); /* Choose an arbitrary cpu */
77 HV_Coord coord
= { .x
= cpu_x(cpu
), .y
= cpu_y(cpu
) };
79 rc
= notifier_add_irq(hp
, irq
);
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.
88 if (hv_console_set_ipi(KERNEL_PL
, irq
, coord
) < 0)
89 notifier_del_irq(hp
, irq
);
94 static void hvc_tile_notifier_del_irq(struct hvc_struct
*hp
, int irq
)
96 HV_Coord coord
= { 0, 0 };
98 /* Tell the hypervisor to stop sending us interrupts. */
99 hv_console_set_ipi(KERNEL_PL
, -1, coord
);
101 notifier_del_irq(hp
, irq
);
104 static void hvc_tile_notifier_hangup_irq(struct hvc_struct
*hp
, int irq
)
106 hvc_tile_notifier_del_irq(hp
, irq
);
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
,
114 .notifier_add
= hvc_tile_notifier_add_irq
,
115 .notifier_del
= hvc_tile_notifier_del_irq
,
116 .notifier_hangup
= hvc_tile_notifier_hangup_irq
,
122 static int hvc_tile_probe(struct platform_device
*pdev
)
124 struct hvc_struct
*hp
;
127 /* Create our IRQ and register it. */
128 tile_hvc_irq
= irq_alloc_hwirq(-1);
132 tile_irq_activate(tile_hvc_irq
, TILE_IRQ_PERCPU
);
133 hp
= hvc_alloc(0, tile_hvc_irq
, &hvc_tile_get_put_ops
, 128);
135 irq_free_hwirq(tile_hvc_irq
);
138 dev_set_drvdata(&pdev
->dev
, hp
);
143 static int hvc_tile_remove(struct platform_device
*pdev
)
146 struct hvc_struct
*hp
= dev_get_drvdata(&pdev
->dev
);
150 irq_free_hwirq(hp
->data
);
155 static void hvc_tile_shutdown(struct platform_device
*pdev
)
157 struct hvc_struct
*hp
= dev_get_drvdata(&pdev
->dev
);
159 hvc_tile_notifier_del_irq(hp
, hp
->data
);
162 static struct platform_device hvc_tile_pdev
= {
167 static struct platform_driver hvc_tile_driver
= {
168 .probe
= hvc_tile_probe
,
169 .remove
= hvc_tile_remove
,
170 .shutdown
= hvc_tile_shutdown
,
177 static int __init
hvc_tile_console_init(void)
179 hvc_instantiate(0, 0, &hvc_tile_get_put_ops
);
180 add_preferred_console("hvc", 0, NULL
);
183 console_initcall(hvc_tile_console_init
);
185 static int __init
hvc_tile_init(void)
188 struct hvc_struct
*hp
;
189 hp
= hvc_alloc(0, 0, &hvc_tile_get_put_ops
, 128);
190 return PTR_ERR_OR_ZERO(hp
);
192 platform_device_register(&hvc_tile_pdev
);
193 return platform_driver_register(&hvc_tile_driver
);
196 device_initcall(hvc_tile_init
);