From nobody Mon Nov 17 03:41:21 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1615585498; cv=none; d=zohomail.com; s=zohoarc; b=A063ZqWGenDohJJiStyfUBczOdzW5TOMv1SUpxo/0a4gqInHNnzwJPfC/OJAbDxd1nM5taQr/MZ3zNPTDIOOXBNqsitPHj+wVqf0n99YpUuW3L7r0r7cRKIGv/7eaHZlChAlOE0PNIrMMEzuuTW4yW2LNdB7DWq5MyJu8RiwFkU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615585498; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mmnx16cMUUEWSd4MNbcqQ6JCpwhw2dk6vwiXQptWY7I=; b=aGqX35E7rpmY1qg+gJkvdew3xbHMLZ0q9N9BFnH3v9kU4hO7Hh5PmHk1i2Cyz1xl6rp0N9UgQ88rQLjlOQw2Tu8oHC7S2X1LYrjugJlOtfRPhdXrZM/lp1lhpZMfJS2hWu3uaUeW8H5ckDZsQj+ZjcWqFbz4ndfUr44GaFBxv68= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161558549808957.175679586960655; Fri, 12 Mar 2021 13:44:58 -0800 (PST) Received: from localhost ([::1]:60358 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKpaa-00083n-Sd for importer@patchew.org; Fri, 12 Mar 2021 16:44:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41414) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKpXn-0003py-TQ for qemu-devel@nongnu.org; Fri, 12 Mar 2021 16:42:03 -0500 Received: from mout.kundenserver.de ([217.72.192.75]:34831) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKpXk-0008CA-Jx for qemu-devel@nongnu.org; Fri, 12 Mar 2021 16:42:03 -0500 Received: from localhost.localdomain ([82.142.6.26]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MVvCl-1lCl5P3vRF-00Rp3a; Fri, 12 Mar 2021 22:41:51 +0100 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PATCH v8 1/5] char: add goldfish-tty Date: Fri, 12 Mar 2021 22:41:41 +0100 Message-Id: <20210312214145.2936082-2-laurent@vivier.eu> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210312214145.2936082-1-laurent@vivier.eu> References: <20210312214145.2936082-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:FV0ikCxsF3YxOFOq7uBThKPE08XT/sklbaHWVFZLLlAW1+H92fB VFV6vDojNnDGoxRP0SKaLLIRKBLJnkWHnQkqnqf4m7VgkCEg0x41IRxvk8ulO4pK3hKTnNt zr5Uyzdy1bhVwijBNoGcxLdGMAeyp9Lfl5y4aJMaCJH+P8+6NDEVwjBud9xw21Ng7IJtg6Y IlobbyPn53dZ233MeNZSA== X-UI-Out-Filterresults: notjunk:1;V03:K0:0Y/yLY70zJo=:j3vV11pBumlQxiqGmIa4na SCNCRXVvgK0wcKQ2i8bkYRkQ4oE8Owt+9nvUYxy1yyFn4d7grm5rIOSyvkLlkmL7CHkTf9frB nt1OmX5fsHG4idd7vIINp1YmgAR4RGc4Gv0N1AM6O53SlP9+YHi22lVyGIBlWSFmsItYzYWlr pQuQJS9mdmuxN0yB2HVZMW/kM/PcZ3dV2GV+KAcvFGq+eQ3toX38qTcB9EMldvU8cQO4iSfoE SHhVlUleyVpGLjbP56nMLPHFRzR4piMJWj5+ee652bCyWx4K1N/j7mwtV+0KkpmMsD/KVWFba /dh9zUu1GXmKmHh3TZtXlxmgxuyqIq4JiBs3RhFV8gwgU+3tQnb94zT/fj94FOMG6v1Jl002R zy2vu41anBldqQUm//gur7lFmOJoTRar8b4BucZJS/A+aPCvbHjMGtET2DWJPTkFasBIodMBM mCFHiDYqvQ== Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=217.72.192.75; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Richard Henderson , Laurent Vivier , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Implement the goldfish tty device as defined in https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDF= ISH-VIRTUAL-HARDWARE.TXT and based on the kernel driver code: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/dri= vers/tty/goldfish.c Signed-off-by: Laurent Vivier Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 --- include/hw/char/goldfish_tty.h | 35 ++++ hw/char/goldfish_tty.c | 285 +++++++++++++++++++++++++++++++++ hw/char/Kconfig | 3 + hw/char/meson.build | 2 + hw/char/trace-events | 10 ++ 5 files changed, 335 insertions(+) create mode 100644 include/hw/char/goldfish_tty.h create mode 100644 hw/char/goldfish_tty.c diff --git a/include/hw/char/goldfish_tty.h b/include/hw/char/goldfish_tty.h new file mode 100644 index 000000000000..b9dd67362a68 --- /dev/null +++ b/include/hw/char/goldfish_tty.h @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifer: GPL-2.0-or-later + * + * Goldfish TTY + * + * (c) 2020 Laurent Vivier + * + */ + +#ifndef HW_CHAR_GOLDFISH_TTY_H +#define HW_CHAR_GOLDFISH_TTY_H + +#include "qemu/fifo8.h" +#include "chardev/char-fe.h" + +#define TYPE_GOLDFISH_TTY "goldfish_tty" +OBJECT_DECLARE_SIMPLE_TYPE(GoldfishTTYState, GOLDFISH_TTY) + +#define GOLFISH_TTY_BUFFER_SIZE 128 + +struct GoldfishTTYState { + SysBusDevice parent_obj; + + MemoryRegion iomem; + qemu_irq irq; + CharBackend chr; + + uint32_t data_len; + uint64_t data_ptr; + bool int_enabled; + + Fifo8 rx_fifo; +}; + +#endif diff --git a/hw/char/goldfish_tty.c b/hw/char/goldfish_tty.c new file mode 100644 index 000000000000..8365a1876145 --- /dev/null +++ b/hw/char/goldfish_tty.c @@ -0,0 +1,285 @@ +/* + * SPDX-License-Identifer: GPL-2.0-or-later + * + * Goldfish TTY + * + * (c) 2020 Laurent Vivier + * + */ + +#include "qemu/osdep.h" +#include "hw/irq.h" +#include "hw/qdev-properties-system.h" +#include "hw/sysbus.h" +#include "migration/vmstate.h" +#include "chardev/char-fe.h" +#include "qemu/log.h" +#include "trace.h" +#include "exec/address-spaces.h" +#include "hw/char/goldfish_tty.h" + +#define GOLDFISH_TTY_VERSION 1 + +/* registers */ + +enum { + REG_PUT_CHAR =3D 0x00, + REG_BYTES_READY =3D 0x04, + REG_CMD =3D 0x08, + REG_DATA_PTR =3D 0x10, + REG_DATA_LEN =3D 0x14, + REG_DATA_PTR_HIGH =3D 0x18, + REG_VERSION =3D 0x20, +}; + +/* commands */ + +enum { + CMD_INT_DISABLE =3D 0x00, + CMD_INT_ENABLE =3D 0x01, + CMD_WRITE_BUFFER =3D 0x02, + CMD_READ_BUFFER =3D 0x03, +}; + +static uint64_t goldfish_tty_read(void *opaque, hwaddr addr, + unsigned size) +{ + GoldfishTTYState *s =3D opaque; + uint64_t value =3D 0; + + switch (addr) { + case REG_BYTES_READY: + value =3D fifo8_num_used(&s->rx_fifo); + break; + case REG_VERSION: + value =3D GOLDFISH_TTY_VERSION; + break; + default: + qemu_log_mask(LOG_UNIMP, + "%s: unimplemented register read 0x%02"HWADDR_PRIx"\= n", + __func__, addr); + break; + } + + trace_goldfish_tty_read(s, addr, size, value); + + return value; +} + +static void goldfish_tty_cmd(GoldfishTTYState *s, uint32_t cmd) +{ + uint32_t to_copy; + uint8_t *buf; + uint8_t data_out[GOLFISH_TTY_BUFFER_SIZE]; + int len; + uint64_t ptr; + + switch (cmd) { + case CMD_INT_DISABLE: + if (s->int_enabled) { + if (!fifo8_is_empty(&s->rx_fifo)) { + qemu_set_irq(s->irq, 0); + } + s->int_enabled =3D false; + } + break; + case CMD_INT_ENABLE: + if (!s->int_enabled) { + if (!fifo8_is_empty(&s->rx_fifo)) { + qemu_set_irq(s->irq, 1); + } + s->int_enabled =3D true; + } + break; + case CMD_WRITE_BUFFER: + len =3D s->data_len; + ptr =3D s->data_ptr; + while (len) { + to_copy =3D MIN(GOLFISH_TTY_BUFFER_SIZE, len); + + address_space_rw(&address_space_memory, ptr, + MEMTXATTRS_UNSPECIFIED, data_out, to_copy, 0); + qemu_chr_fe_write_all(&s->chr, data_out, to_copy); + + len -=3D to_copy; + ptr +=3D to_copy; + } + break; + case CMD_READ_BUFFER: + len =3D s->data_len; + ptr =3D s->data_ptr; + while (len && !fifo8_is_empty(&s->rx_fifo)) { + buf =3D (uint8_t *)fifo8_pop_buf(&s->rx_fifo, len, &to_copy); + address_space_rw(&address_space_memory, ptr, + MEMTXATTRS_UNSPECIFIED, buf, to_copy, 1); + + len -=3D to_copy; + ptr +=3D to_copy; + } + if (s->int_enabled && fifo8_is_empty(&s->rx_fifo)) { + qemu_set_irq(s->irq, 0); + } + break; + } +} + +static void goldfish_tty_write(void *opaque, hwaddr addr, + uint64_t value, unsigned size) +{ + GoldfishTTYState *s =3D opaque; + unsigned char c; + + trace_goldfish_tty_write(s, addr, size, value); + + switch (addr) { + case REG_PUT_CHAR: + c =3D value; + qemu_chr_fe_write_all(&s->chr, &c, sizeof(c)); + break; + case REG_CMD: + goldfish_tty_cmd(s, value); + break; + case REG_DATA_PTR: + s->data_ptr =3D value; + break; + case REG_DATA_PTR_HIGH: + s->data_ptr =3D deposit64(s->data_ptr, 32, 32, value); + break; + case REG_DATA_LEN: + s->data_len =3D value; + break; + default: + qemu_log_mask(LOG_UNIMP, + "%s: unimplemented register write 0x%02"HWADDR_PRIx"= \n", + __func__, addr); + break; + } +} + +static const MemoryRegionOps goldfish_tty_ops =3D { + .read =3D goldfish_tty_read, + .write =3D goldfish_tty_write, + .endianness =3D DEVICE_NATIVE_ENDIAN, + .valid.max_access_size =3D 4, + .impl.max_access_size =3D 4, + .impl.min_access_size =3D 4, +}; + +static int goldfish_tty_can_receive(void *opaque) +{ + GoldfishTTYState *s =3D opaque; + int available =3D fifo8_num_free(&s->rx_fifo); + + trace_goldfish_tty_can_receive(s, available); + + return available; +} + +static void goldfish_tty_receive(void *opaque, const uint8_t *buffer, int = size) +{ + GoldfishTTYState *s =3D opaque; + + trace_goldfish_tty_receive(s, size); + + g_assert(size <=3D fifo8_num_free(&s->rx_fifo)); + + fifo8_push_all(&s->rx_fifo, buffer, size); + + if (s->int_enabled && !fifo8_is_empty(&s->rx_fifo)) { + qemu_set_irq(s->irq, 1); + } +} + +static void goldfish_tty_reset(DeviceState *dev) +{ + GoldfishTTYState *s =3D GOLDFISH_TTY(dev); + + trace_goldfish_tty_reset(s); + + fifo8_reset(&s->rx_fifo); + s->int_enabled =3D false; + s->data_ptr =3D 0; + s->data_len =3D 0; +} + +static void goldfish_tty_realize(DeviceState *dev, Error **errp) +{ + GoldfishTTYState *s =3D GOLDFISH_TTY(dev); + + trace_goldfish_tty_realize(s); + + fifo8_create(&s->rx_fifo, GOLFISH_TTY_BUFFER_SIZE); + memory_region_init_io(&s->iomem, OBJECT(s), &goldfish_tty_ops, s, + "goldfish_tty", 0x24); + + if (qemu_chr_fe_backend_connected(&s->chr)) { + qemu_chr_fe_set_handlers(&s->chr, goldfish_tty_can_receive, + goldfish_tty_receive, NULL, NULL, + s, NULL, true); + } +} + +static void goldfish_tty_unrealize(DeviceState *dev) +{ + GoldfishTTYState *s =3D GOLDFISH_TTY(dev); + + trace_goldfish_tty_unrealize(s); + + fifo8_destroy(&s->rx_fifo); +} + +static const VMStateDescription vmstate_goldfish_tty =3D { + .name =3D "goldfish_tty", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(data_len, GoldfishTTYState), + VMSTATE_UINT64(data_ptr, GoldfishTTYState), + VMSTATE_BOOL(int_enabled, GoldfishTTYState), + VMSTATE_FIFO8(rx_fifo, GoldfishTTYState), + VMSTATE_END_OF_LIST() + } +}; + +static Property goldfish_tty_properties[] =3D { + DEFINE_PROP_CHR("chardev", GoldfishTTYState, chr), + DEFINE_PROP_END_OF_LIST(), +}; + +static void goldfish_tty_instance_init(Object *obj) +{ + SysBusDevice *dev =3D SYS_BUS_DEVICE(obj); + GoldfishTTYState *s =3D GOLDFISH_TTY(obj); + + trace_goldfish_tty_instance_init(s); + + sysbus_init_mmio(dev, &s->iomem); + sysbus_init_irq(dev, &s->irq); +} + +static void goldfish_tty_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + + device_class_set_props(dc, goldfish_tty_properties); + dc->reset =3D goldfish_tty_reset; + dc->realize =3D goldfish_tty_realize; + dc->unrealize =3D goldfish_tty_unrealize; + dc->vmsd =3D &vmstate_goldfish_tty; + set_bit(DEVICE_CATEGORY_INPUT, dc->categories); +} + +static const TypeInfo goldfish_tty_info =3D { + .name =3D TYPE_GOLDFISH_TTY, + .parent =3D TYPE_SYS_BUS_DEVICE, + .class_init =3D goldfish_tty_class_init, + .instance_init =3D goldfish_tty_instance_init, + .instance_size =3D sizeof(GoldfishTTYState), +}; + +static void goldfish_tty_register_types(void) +{ + type_register_static(&goldfish_tty_info); +} + +type_init(goldfish_tty_register_types) diff --git a/hw/char/Kconfig b/hw/char/Kconfig index f6f4fffd1b7c..4cf36ac637ba 100644 --- a/hw/char/Kconfig +++ b/hw/char/Kconfig @@ -64,3 +64,6 @@ config MCHP_PFSOC_MMUART =20 config SIFIVE_UART bool + +config GOLDFISH_TTY + bool diff --git a/hw/char/meson.build b/hw/char/meson.build index 7ba38dbd965f..da5bb8b762e0 100644 --- a/hw/char/meson.build +++ b/hw/char/meson.build @@ -39,3 +39,5 @@ specific_ss.add(when: 'CONFIG_HTIF', if_true: files('risc= v_htif.c')) specific_ss.add(when: 'CONFIG_TERMINAL3270', if_true: files('terminal3270.= c')) specific_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-serial-bus.c= ')) specific_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr_vty.c')) + +specific_ss.add(when: 'CONFIG_GOLDFISH_TTY', if_true: files('goldfish_tty.= c')) diff --git a/hw/char/trace-events b/hw/char/trace-events index 81026f661277..76d52938ead3 100644 --- a/hw/char/trace-events +++ b/hw/char/trace-events @@ -20,6 +20,16 @@ virtio_console_flush_buf(unsigned int port, size_t len, = ssize_t ret) "port %u, i virtio_console_chr_read(unsigned int port, int size) "port %u, size %d" virtio_console_chr_event(unsigned int port, int event) "port %u, event %d" =20 +# goldfish_tty.c +goldfish_tty_read(void *dev, unsigned int addr, unsigned int size, uint64_= t value) "tty: %p reg: 0x%02x size: %d value: 0x%"PRIx64 +goldfish_tty_write(void *dev, unsigned int addr, unsigned int size, uint64= _t value) "tty: %p reg: 0x%02x size: %d value: 0x%"PRIx64 +goldfish_tty_can_receive(void *dev, unsigned int available) "tty: %p avail= able: %u" +goldfish_tty_receive(void *dev, unsigned int size) "tty: %p size: %u" +goldfish_tty_reset(void *dev) "tty: %p" +goldfish_tty_realize(void *dev) "tty: %p" +goldfish_tty_unrealize(void *dev) "tty: %p" +goldfish_tty_instance_init(void *dev) "tty: %p" + # grlib_apbuart.c grlib_apbuart_event(int event) "event:%d" grlib_apbuart_writel_unknown(uint64_t addr, uint32_t value) "addr 0x%"PRIx= 64" value 0x%x" --=20 2.29.2