From patchwork Fri Mar 18 09:13:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baptiste Reynal X-Patchwork-Id: 8617431 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EE755C0553 for ; Fri, 18 Mar 2016 09:17:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B13FF2034B for ; Fri, 18 Mar 2016 09:17:39 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7041320377 for ; Fri, 18 Mar 2016 09:17:38 +0000 (UTC) Received: from localhost ([::1]:42394 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agqXJ-0000JM-P3 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 18 Mar 2016 05:17:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42183) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agqTU-0002rQ-S3 for qemu-devel@nongnu.org; Fri, 18 Mar 2016 05:13:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agqTR-0003nH-D5 for qemu-devel@nongnu.org; Fri, 18 Mar 2016 05:13:40 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:36522) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agqTR-0003n3-27 for qemu-devel@nongnu.org; Fri, 18 Mar 2016 05:13:37 -0400 Received: by mail-wm0-x243.google.com with SMTP id l68so4443324wml.3 for ; Fri, 18 Mar 2016 02:13:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtualopensystems-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aGOCACXfMzurapB6siZR4Zl3GMDhVLTwBdult751PtI=; b=M+rxHknXQOsaJvucbvSVUuZyF2POknUY5ErqYPMJdfrAVRlFCIswuhyMaWSOQy2aeq INtM/GTZQQ7UlBeGZk+LhNOXe/dy8zEA3ybK1FzMHWZ+BdDEroxevdQgtFYJtw3gX9gU yq5MzCZWs8Oe6s+93olzpbLumSweI6Mq0S2ZK7LP5aV31IfRnV0qLEgn8nJ5tUW38ygd UqHozYS9ztcYPE5FJN22vHL0BgtrFUTyP3TPlM/1/RBPlAkEIB0HmRtF/H+DtbPxxD++ LkupN3xrocvJ2rXtmxDbaeQIPvWOEOUzv+eEBu5TNLPLLnmKn4Cm7PXrMGgGuwaUI44z nITA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=aGOCACXfMzurapB6siZR4Zl3GMDhVLTwBdult751PtI=; b=abKaz+/9GskrgtKOeh9+oQHKlM7xHyX4TRxLrUSz2eYo4+9iTT36WxW0n1YXK3/ckx Tz3xZki2TGGPph9eU5zbPPHQf/UH6E8mWihsXOogYsR7shX3dSJaKBOB0sQglguAsIVV nh2fyfgOB8HEwA8lgDKiObWrGEfq1QwbpRqoi6ouzZ1tv6outIRXXs9L5xggd/OR9wL0 GntieeF8fuAQ+6RVc4wxugvKqsABwNlEHxWoYGThOYhOul71+0hktI1K+8T02fN7qevW V5nrrp0k+sDrQcMg4PX5KUjUZrqz2aeIU32bPZ/hTIQXJV5o4o9hl+Eh9jLFd1sbszTw H+vQ== X-Gm-Message-State: AD7BkJLYWRjEi+8pJzGYcExIyFnH4qdT4XnkWacFK3TIl7zqlUg3drsl8efmm2dv8HDiIQ== X-Received: by 10.194.63.242 with SMTP id j18mr16663778wjs.114.1458292416528; Fri, 18 Mar 2016 02:13:36 -0700 (PDT) Received: from localhost (LMontsouris-657-1-4-224.w90-63.abo.wanadoo.fr. [90.63.239.224]) by smtp.gmail.com with ESMTPSA id t7sm11329155wjf.39.2016.03.18.02.13.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Mar 2016 02:13:35 -0700 (PDT) From: Baptiste Reynal To: qemu-devel@nongnu.org Date: Fri, 18 Mar 2016 10:13:03 +0100 Message-Id: <1458292385-13802-5-git-send-email-b.reynal@virtualopensystems.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458292385-13802-1-git-send-email-b.reynal@virtualopensystems.com> References: <1458292385-13802-1-git-send-email-b.reynal@virtualopensystems.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Cc: b.reynal@virtualopensystems.com, Jani.Kokkonen@huawei.com, tech@virtualopensystems.com, Claudio.Fontana@huawei.com, Christian Pinto Subject: [Qemu-devel] [RFC v2 4/6] hw/misc: sdm virtio device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is the virtio implementation for an SDM device. Parameters are: comm=[sdm_communication_id] specifies the communication channel master=[true/false] - configure the SDM device as master or slave num_slaves=[slave_number] - if master is true, specifies the number of slaves len-signals=[signals_number] - specifies the number of signals signals[x]=[signal_id] - add a signal to the device, with the ID x Signed-off-by: Christian Pinto Signed-off-by: Baptiste Reynal --- hw/virtio/Makefile.objs | 1 + hw/virtio/virtio-sdm.c | 262 +++++++++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-sdm.h | 60 +++++++++ linux-headers/linux/virtio_sdm.h | 52 ++++++++ 4 files changed, 375 insertions(+) create mode 100644 hw/virtio/virtio-sdm.c create mode 100644 include/hw/virtio/virtio-sdm.h create mode 100644 linux-headers/linux/virtio_sdm.h diff --git a/hw/virtio/Makefile.objs b/hw/virtio/Makefile.objs index 19b224a..7e44bc4 100644 --- a/hw/virtio/Makefile.objs +++ b/hw/virtio/Makefile.objs @@ -2,6 +2,7 @@ common-obj-y += virtio-rng.o common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o common-obj-y += virtio-bus.o common-obj-y += virtio-mmio.o +obj-$(CONFIG_SDM) += virtio-sdm.o obj-$(CONFIG_VIRTIO) += dataplane/ obj-y += virtio.o virtio-balloon.o diff --git a/hw/virtio/virtio-sdm.c b/hw/virtio/virtio-sdm.c new file mode 100644 index 0000000..66986c7 --- /dev/null +++ b/hw/virtio/virtio-sdm.c @@ -0,0 +1,262 @@ +/* + * Copyright (C) 2016 - Virtual Open Systems + * Authors: Christian Pinto + * Baptiste Reynal + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include + +#include "qemu/iov.h" +#include "qemu/error-report.h" +#include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-sdm.h" + +typedef void (*cmd_handler)(VirtIOSdm *sdm, union sdm_cmd_params); +/** + * Command handler for signal type commands. + * Invoked when the command is received from the guest + */ +static void handler_signal(VirtIOSdm *sdm, union sdm_cmd_params params) +{ + SDMSignalData signal; + DEBUG_PRINT("handler_signal: SIGNAL %d, SLAVE_ID %d\n", params.sig.type, + params.sig.slave_id); + + signal.type = params.sig.type; + signal.slave = params.sig.slave_id; + memcpy(&signal.payload, ¶ms.sig.payload, 2 * sizeof(uint32_t)); + + sdm_communication_signal(sdm->sdmc, SDM_DEVICE(sdm), &signal); +} + +static cmd_handler sdm_cmd_handlers[] = { + [SDM_SIGNAL_CMD] = handler_signal, +}; + +/** + * Send a command to the guest virtio-driver + */ +static void send_cmd_to_guest(VirtIOSdm *sdm, + struct sdm_cmd cmd) +{ + VirtQueueElement elem; + VirtIODevice *vdev = VIRTIO_DEVICE(sdm); + + if (!virtqueue_pop(sdm->hg_vq, &elem)) { + return; + } + + if (iov_from_buf(elem.in_sg, elem.in_num, 0, &cmd, sizeof(cmd))) { + virtqueue_push(sdm->hg_vq, &elem, sizeof(cmd)); + virtio_notify(vdev, sdm->hg_vq); + DEBUG_PRINT("Sending command %d of type %d\n", + cmd.cmd, cmd.params.sig.type); + } else { + error_report("virtio-idm - Failed to issue command id 0x%x\n", cmd.cmd); + } +} + +/** + * guest -> host virtqueue handler. + * Calls the handler assoociated to the command received over the virtqueue + */ +static void handle_guest_host_queue(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtQueueElement elem; + struct sdm_cmd cmd; + + if (!virtqueue_pop(vq, &elem)) { + return; + } + + if (!iov_to_buf(elem.out_sg, elem.out_num, 0, &cmd, sizeof(cmd))) { + return; + } + + DEBUG_PRINT("Command received 0x%x\n", cmd.cmd); + + if (cmd.cmd >= ARRAY_SIZE(sdm_cmd_handlers) || + !sdm_cmd_handlers[cmd.params.sig.type]) { + error_report("virtio-idm - unrecognized command %x\n", cmd.cmd); + return; + } + + sdm_cmd_handlers[cmd.params.sig.type](VIRTIO_SDM(vdev), cmd.params); +} + +/** + * Guest sends commands over this virtqueue only to add an empty + * buffer to be later used by the host. + */ +static void handle_host_guest_queue(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOSdm *sdm = VIRTIO_SDM(vdev); + + sdm->busy = false; +} + +static uint64_t get_features(VirtIODevice *vdev, uint64_t requested_features, + Error **errp) +{ + return requested_features; +} + +static void virtio_sdm_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSdm *sdm = VIRTIO_SDM(dev); + int i; + + virtio_init(vdev, "virtio-sdm", VIRTIO_ID_SDM, 0); + + sdm->hg_vq = virtio_add_queue(vdev, 128, handle_host_guest_queue); + sdm->gh_vq = virtio_add_queue(vdev, 128, handle_guest_host_queue); + + sdm->signals = calloc(sdm->num_signals, sizeof(SDMSignal)); + for (i = 0; i < sdm->num_signals; i++) { + sdm->signals[i] = (SDMSignal *) object_resolve_path_type( + sdm->signals_name[i], + TYPE_SDM_SIGNAL, false); + if (!sdm->signals[i]) { + error_report("virtio-sdm: Cannot find signal %s", + sdm->signals_name[i]);; + } + } + + sdm_communication_connect(sdm->sdmc, SDM_DEVICE(sdm)); +} + +static void virtio_sdm_device_unrealize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + virtio_cleanup(vdev); +} + +static int virtio_sdm_accept(SDMDevice *sdm) +{ + VirtIOSdm *sdmv = VIRTIO_SDM(sdm); + + if (sdmv->last_slave >= sdmv->num_slaves) { + return -1; + } + + return ++sdmv->last_slave; +} + +static int virtio_sdm_notify(SDMDevice *sdm, SDMSignalData *signal) +{ + VirtIOSdm *sdmv = VIRTIO_SDM(sdm); + SDMSignal *hw_signal ; + struct sdm_cmd cmd; + + if (signal->type > sdmv->num_signals) { + return 0; + } + + hw_signal = sdmv->signals[signal->type]; + + if (!hw_signal) { + return 0; + } + + sdm_signal_hw_ops(hw_signal, signal); + + if (!sdm_signal_hw_only(hw_signal)) { + if (sdmv->busy) { + return -1; + } + + sdmv->busy = true; + + cmd.cmd = SDM_SIGNAL_CMD; + cmd.params.sig.type = signal->type; + cmd.params.sig.slave_id = signal->slave; + memcpy(cmd.params.sig.payload, signal->payload, 2 * sizeof(uint32_t)); + + printf("Forward %d %d\n", cmd.params.sig.type, cmd.params.sig.slave_id); + send_cmd_to_guest(sdmv, cmd); + } + + return 0; +} + +static int virtio_sdm_get_num_slaves(SDMDevice *sdm) +{ + VirtIOSdm *sdmv = VIRTIO_SDM(sdm); + + return sdmv->num_slaves; +} + +static bool virtio_sdm_is_master(SDMDevice *sdm) +{ + VirtIOSdm *sdmv = VIRTIO_SDM(sdm); + + return sdmv->master; +} + +static Property sdm_virtio_properties[] = { + DEFINE_PROP_UINT32("num-slaves", VirtIOSdm, num_slaves, 1), + DEFINE_PROP_BOOL("master", VirtIOSdm, master, false), + DEFINE_PROP_ARRAY("signals", VirtIOSdm, num_signals, signals_name, + qdev_prop_string, char *), + DEFINE_PROP_END_OF_LIST(), +}; + +static void virtio_sdm_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + SDMDeviceClass *sdmc = SDM_DEVICE_CLASS(klass); + + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + vdc->realize = virtio_sdm_device_realize; + vdc->unrealize = virtio_sdm_device_unrealize; + vdc->get_features = get_features; + + sdmc->accept = virtio_sdm_accept; + sdmc->notify = virtio_sdm_notify; + sdmc->get_num_slaves = virtio_sdm_get_num_slaves; + sdmc->is_master = virtio_sdm_is_master; + + dc->props = sdm_virtio_properties; +} + +static void virtio_sdm_init(Object *obj) +{ + VirtIOSdm *sdm = VIRTIO_SDM(obj); + + object_property_add_link(obj, "comm", TYPE_SDM_COMMUNICATION, + (Object **)&sdm->sdmc, + qdev_prop_allow_set_link_before_realize, + OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort); + + sdm->busy = false; +} + +static const TypeInfo virtio_sdm_info = { + .name = TYPE_VIRTIO_SDM, + .parent = TYPE_VIRTIO_DEVICE, + .instance_size = sizeof(VirtIOSdm), + .class_init = virtio_sdm_class_init, + .instance_init = virtio_sdm_init, + .interfaces = (InterfaceInfo[]) { + { TYPE_SDM_DEVICE }, + { } + } +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_sdm_info); +} + +type_init(virtio_register_types) diff --git a/include/hw/virtio/virtio-sdm.h b/include/hw/virtio/virtio-sdm.h new file mode 100644 index 0000000..601e083 --- /dev/null +++ b/include/hw/virtio/virtio-sdm.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2016 - Virtual Open Systems + * Author: Christian Pinto + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _QEMU_VIRTIO_SDM_H +#define _QEMU_VIRTIO_SDM_H + +#include "hw/virtio/virtio.h" +#include "hw/misc/sdm-device.h" +#include "hw/misc/sdm-communication.h" + +#define DEBUG +#ifdef DEBUG +#define DEBUG_PRINT(fmt, ...) \ + do {printf("virtio-idm - " fmt, ## __VA_ARGS__); } while (0) +#else +#define DEBUG_PRINT(fmt, ...) +#endif + +#define TYPE_VIRTIO_SDM "virtio-sdm-device" +#define VIRTIO_SDM(obj) \ + OBJECT_CHECK(VirtIOSdm, (obj), TYPE_VIRTIO_SDM) +#define VIRTIO_SDM_GET_PARENT_CLASS(obj) \ + OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_SDM) + +/* The Virtio ID for the virtio SDM device */ +#define VIRTIO_ID_SDM 65000 + +typedef struct VirtIOSdm VirtIOSdm; + +struct VirtIOSdm { + VirtIODevice parent_obj; + + SDMCommunication *sdmc; + + bool master; + bool busy; + + uint32_t num_slaves; + uint32_t last_slave; + + uint32_t num_signals; + char **signals_name; + SDMSignal **signals; + + VirtQueue *hg_vq; + VirtQueue *gh_vq; +}; + +#endif /*_QEMU_VIRTIO_SDM_H*/ diff --git a/linux-headers/linux/virtio_sdm.h b/linux-headers/linux/virtio_sdm.h new file mode 100644 index 0000000..db786b0 --- /dev/null +++ b/linux-headers/linux/virtio_sdm.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2016 - Virtual Open Systems + * Author: Christian Pinto + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _LINUX_VIRTIO_SDM_H +#define _LINUX_VIRTIO_SDM_H +#include +#include +#include +/* the commands the host can issue to a virtio-sdm guest */ +enum sdm_cmd_type { + SDM_SIGNAL_CMD, +}; + +enum sdm_signal_type { + SDM_IRQ, + SDM_BOOT, +}; + +union sdm_cmd_params { + /** + * This command is used to communicate signals between + * guest and host. + * The slave_id to which signal triggering/reception is + * associated, the type and signal and 2 32-bits registers + * for payload are transmitted. + */ + struct { + __u16 type; + __u16 slave_id; + + __u32 payload[2]; + } sig; + __u32 padding[4]; +}; + +struct sdm_cmd { + enum sdm_cmd_type cmd; + union sdm_cmd_params params; +}; + +#endif /* _LINUX_VIRTIO_SDM_H */