Re: [Qemu-devel] [PATCH v6 12/23] RISC-V HTIF Console

2018-02-23 Thread Richard Henderson
On 02/22/2018 04:11 PM, Michael Clark wrote:
> HTIF (Host Target Interface) provides console emulation for QEMU. HTIF
> allows identical copies of BBL (Berkeley Boot Loader) and linux to run
> on both Spike and QEMU. BBL provides HTIF console access via the
> SBI (Supervisor Binary Interface) and the linux kernel SBI console.
> 
> The HTIT chardev implements the pre qom legacy interface consistent
> with the 16550a UART in 'hw/char/serial.c'.
> 
> Signed-off-by: Michael Clark 
> ---
>  hw/riscv/riscv_htif.c | 263 
> ++
>  include/hw/riscv/riscv_htif.h |  64 ++
>  2 files changed, 327 insertions(+)
>  create mode 100644 hw/riscv/riscv_htif.c
>  create mode 100644 include/hw/riscv/riscv_htif.h

Reviewed-by: Richard Henderson 


r~



[Qemu-devel] [PATCH v6 12/23] RISC-V HTIF Console

2018-02-22 Thread Michael Clark
HTIF (Host Target Interface) provides console emulation for QEMU. HTIF
allows identical copies of BBL (Berkeley Boot Loader) and linux to run
on both Spike and QEMU. BBL provides HTIF console access via the
SBI (Supervisor Binary Interface) and the linux kernel SBI console.

The HTIT chardev implements the pre qom legacy interface consistent
with the 16550a UART in 'hw/char/serial.c'.

Signed-off-by: Michael Clark 
---
 hw/riscv/riscv_htif.c | 263 ++
 include/hw/riscv/riscv_htif.h |  64 ++
 2 files changed, 327 insertions(+)
 create mode 100644 hw/riscv/riscv_htif.c
 create mode 100644 include/hw/riscv/riscv_htif.h

diff --git a/hw/riscv/riscv_htif.c b/hw/riscv/riscv_htif.c
new file mode 100644
index 000..e1087da
--- /dev/null
+++ b/hw/riscv/riscv_htif.c
@@ -0,0 +1,263 @@
+/*
+ * QEMU RISC-V Host Target Interface (HTIF) Emulation
+ *
+ * Author: Sagar Karandikar, sag...@eecs.berkeley.edu
+ *
+ * This provides HTIF device emulation for QEMU. At the moment this allows
+ * for identical copies of bbl/linux to run on both spike and QEMU.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/log.h"
+#include "hw/sysbus.h"
+#include "hw/char/serial.h"
+#include "chardev/char.h"
+#include "chardev/char-fe.h"
+#include "hw/riscv/riscv_htif.h"
+#include "qemu/timer.h"
+#include "exec/address-spaces.h"
+#include "qemu/error-report.h"
+
+#define RISCV_DEBUG_HTIF 0
+#define HTIF_DEBUG(fmt, ...)   
\
+do {   
\
+if (RISCV_DEBUG_HTIF) {
\
+qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, 
##__VA_ARGS__);\
+}  
\
+} while (0)
+
+static uint64_t fromhost_addr, tohost_addr;
+
+void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
+uint64_t st_size)
+{
+if (strcmp("fromhost", st_name) == 0) {
+fromhost_addr = st_value;
+if (st_size != 8) {
+error_report("HTIF fromhost must be 8 bytes");
+exit(1);
+}
+} else if (strcmp("tohost", st_name) == 0) {
+tohost_addr = st_value;
+if (st_size != 8) {
+error_report("HTIF tohost must be 8 bytes");
+exit(1);
+}
+}
+}
+
+/*
+ * Called by the char dev to see if HTIF is ready to accept input.
+ */
+static int htif_can_recv(void *opaque)
+{
+return 1;
+}
+
+/*
+ * Called by the char dev to supply input to HTIF console.
+ * We assume that we will receive one character at a time.
+ */
+static void htif_recv(void *opaque, const uint8_t *buf, int size)
+{
+HTIFState *htifstate = opaque;
+
+if (size != 1) {
+return;
+}
+
+/* TODO - we need to check whether mfromhost is zero which indicates
+  the device is ready to receive. The current implementation
+  will drop characters */
+
+uint64_t val_written = htifstate->pending_read;
+uint64_t resp = 0x100 | *buf;
+
+htifstate->env->mfromhost = (val_written >> 48 << 48) | (resp << 16 >> 16);
+}
+
+/*
+ * Called by the char dev to supply special events to the HTIF console.
+ * Not used for HTIF.
+ */
+static void htif_event(void *opaque, int event)
+{
+
+}
+
+static int htif_be_change(void *opaque)
+{
+HTIFState *s = opaque;
+
+qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
+htif_be_change, s, NULL, true);
+
+return 0;
+}
+
+static void htif_handle_tohost_write(HTIFState *htifstate, uint64_t 
val_written)
+{
+uint8_t device = val_written >> 56;
+uint8_t cmd = val_written >> 48;
+uint64_t payload = val_written & 0xULL;
+int resp = 0;
+
+HTIF_DEBUG("mtohost write: de