From patchwork Thu Apr 1 12:12:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6633DC43461 for ; Thu, 1 Apr 2021 12:14:47 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EC514610EA for ; Thu, 1 Apr 2021 12:14:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EC514610EA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwDm-0001lL-3y for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:14:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47720) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCH-0000Vw-6N for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:13 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]:35686) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCC-0004N0-VM for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:12 -0400 Received: by mail-pl1-x62d.google.com with SMTP id e14so932354plj.2 for ; Thu, 01 Apr 2021 05:13:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Qpna+j/iGaaxi0QXVwjA+9BLwCSZkAAM2isUXvU5/SM=; b=RPTqm4gmEKBwNg5UZdc2f8DZKLN+49ch7L0+yU03gzKBfdFeFzG/Duzf/JW6uCzWHl nd9TC+mJzgPBANg1pm+KOH+gt+um+PzMKDz1l6ec+VHc3vGncMyw/toxu8KF1HvvFQMj lIXF1a9znryVEfsVoX95Z3QyEx+bLg5bckNyWIyqI8KYbPKFS7qeL2VFjHpqLm+W6ONC rzrzZ78ZG1l8NKiWOSSvWonG9izlMpjyKcXlPSwMbWTyDUKViMp880UlsL+0idWTQ6Js TwwmFPLie8luZAof0nInIFw+Nl6/lsH2m3y0SVg8I3JbnyK1dFcqDNFvA/JEQ3lHI26Q yLyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qpna+j/iGaaxi0QXVwjA+9BLwCSZkAAM2isUXvU5/SM=; b=HulQnT/NYa+bXdnzLcRmsHxsdEgVMKGV0q0nZj/74uhgIQ7D3SQK8ZlTG/iymk+WY0 A1HvtBEnTTCma8eawzwrgFv0PhegJZgUyUvVgTaryWY30/JPjUcKjLpDdsN4vhqKuXmk hDZ4byQH7X4fbSr2j+jQgXfEpJY5zvRP2QM9NsxDSwA9UPo64lbHhbJIz+tJ52s9KEze xjy/3u0fEeqTjX5DXOztfrExowcprxMnhLcaLYb71PA5D5Sa+0z8XsgZlWVjAH2XCbFY 9GAPKU1RST0PWAMGcirfQQTXi5M0kPuKL2BizB8xCFb4PmxBJXxftD8pAThPBhXUBL4F vyMA== X-Gm-Message-State: AOAM530UiF/72rlzUjfTbPelRHm/G/uSI060FxXtjcmii+GY0J1ZEn2+ wyG8nGPscL5DjUs2oRPZb+zzn4ZNHFkDNA== X-Google-Smtp-Source: ABdhPJwEY2bAHpmShPRBRupFxa2N6OCWSxf1QTXHWB8LKwq5YS7OfXLNXs4hzSHllZ4x8kmEPzFO3A== X-Received: by 2002:a17:903:4106:b029:e7:49bd:4266 with SMTP id r6-20020a1709034106b02900e749bd4266mr7745634pld.56.1617279187619; Thu, 01 Apr 2021 05:13:07 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id b3sm5535575pjg.41.2021.04.01.05.13.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:07 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org Subject: [PATCH V2 1/6] !Merge: Update virtio headers from kernel Date: Thu, 1 Apr 2021 17:42:30 +0530 Message-Id: X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=viresh.kumar@linaro.org; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Viresh Kumar , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" DO NOT MERGE Update Linux virtio headers with help of ./scripts/update-linux-headers.sh ~/lsrc/linux.git Signed-off-by: Viresh Kumar --- include/standard-headers/linux/virtio_i2c.h | 40 +++++++++++++++++++++ include/standard-headers/linux/virtio_ids.h | 1 + 2 files changed, 41 insertions(+) create mode 100644 include/standard-headers/linux/virtio_i2c.h diff --git a/include/standard-headers/linux/virtio_i2c.h b/include/standard-headers/linux/virtio_i2c.h new file mode 100644 index 000000000000..378d31c4a527 --- /dev/null +++ b/include/standard-headers/linux/virtio_i2c.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later WITH Linux-syscall-note */ +/* + * Definitions for virtio I2C Adpter + * + * Copyright (c) 2021 Intel Corporation. All rights reserved. + */ + +#ifndef _LINUX_VIRTIO_I2C_H +#define _LINUX_VIRTIO_I2C_H + +#include "standard-headers/linux/types.h" + +/* The bit 0 of the @virtio_i2c_out_hdr.@flags, used to group the requests */ +#define VIRTIO_I2C_FLAGS_FAIL_NEXT 0x00000001 + +/** + * struct virtio_i2c_out_hdr - the virtio I2C message OUT header + * @addr: the controlled device address + * @padding: used to pad to full dword + * @flags: used for feature extensibility + */ +struct virtio_i2c_out_hdr { + uint16_t addr; + uint16_t padding; + uint32_t flags; +}; + +/** + * struct virtio_i2c_in_hdr - the virtio I2C message IN header + * @status: the processing result from the backend + */ +struct virtio_i2c_in_hdr { + uint8_t status; +}; + +/* The final status written by the device */ +#define VIRTIO_I2C_MSG_OK 0 +#define VIRTIO_I2C_MSG_ERR 1 + +#endif /* _LINUX_VIRTIO_I2C_H */ diff --git a/include/standard-headers/linux/virtio_ids.h b/include/standard-headers/linux/virtio_ids.h index bc1c0621f5ed..b89391dff6c9 100644 --- a/include/standard-headers/linux/virtio_ids.h +++ b/include/standard-headers/linux/virtio_ids.h @@ -54,5 +54,6 @@ #define VIRTIO_ID_FS 26 /* virtio filesystem */ #define VIRTIO_ID_PMEM 27 /* virtio pmem */ #define VIRTIO_ID_MAC80211_HWSIM 29 /* virtio mac80211-hwsim */ +#define VIRTIO_ID_I2C_ADAPTER 34 /* virtio i2c adapter */ #endif /* _LINUX_VIRTIO_IDS_H */ From patchwork Thu Apr 1 12:12:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177971 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8658DC433B4 for ; Thu, 1 Apr 2021 12:21:02 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B04096112F for ; Thu, 1 Apr 2021 12:21:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B04096112F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:54376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwJo-0004wH-PO for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:21:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47756) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCL-0000ZS-7g for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:17 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]:41505) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCG-0004Nk-M6 for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:17 -0400 Received: by mail-pj1-x1030.google.com with SMTP id nh23-20020a17090b3657b02900c0d5e235a8so937764pjb.0 for ; Thu, 01 Apr 2021 05:13:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XhB43fJW+cSP+kG6C1kZ0/qtnB8FNNh4OQ05du9NT8Y=; b=w5BBS3XkgKwOY7lNBT5eKhDRnvo4MLZS1YmSJyw6L42FuxxkcVCiQ1HCg4EbrQ0n9k eMVKmoZUXcWkw0UEYBMAw7kwkiAFyKqGkSxgFxM/QCfg2BZ/vk3t/JJjhTSjAm6Tv4t0 iiJ9kllFh5f0+RTwXEav34lg6r4thx2XD0IBnw3/wG/7XMxmhwz4JA5NSopWzf8BXYX0 eW/wGLEkXGIYL+G4PsWfjFM9aDHk//DI2FyLFC84UZcq1VbsjlmmOznXDBktJLtHEdj5 4W+TVK3M45wnMM+hPsva46NjXPf4IB5F99a/bSr90J3hAeo9kXzOt0PEYzH5WPjp0xFC FOVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XhB43fJW+cSP+kG6C1kZ0/qtnB8FNNh4OQ05du9NT8Y=; b=F21WfqSN/hOE6/JfkG95IlzQEBUL/tjA1jSmQj/EUj1NB62S7NNXUpWxgHIMlNMS4D bKX8I5BfajM1KmJVL3ZqQzLpnuMcjzX+N3ZfxS5LAJJEIEVaFapZFpfLbLylLTwK2urF 73pJllkr/qBnjE+5d8q4Z5B7VwKIyhMwqkts51g2ktQWQMQ07lhUC4XMWfXDOluoESRq 48egq0Rq0d0JbqjRWefEJctfmyoJis5HhetmaxmXLYNfVUChTGc2eRrH2J1ssaaktvBz iJk5cDww5/S/x7ASvtHDgP21ipq1DpJvqHs53O/Q8MvlQh4abHZEnOfzCQxAHwINAiqT YT0Q== X-Gm-Message-State: AOAM530WW+9L2whe//pOaHIOU771r363Cb0xNHxoaZ8IwByDH4IKtUpx 5XF14VVepzKio48QUakvl9MB0W2cxEc5Dg== X-Google-Smtp-Source: ABdhPJw1cDbOlumKkxbH7swVxwx0/NBGnRaVsiMdV/JBB0LrAZgPSwcs3MDGUuBsDChol3IA/IQ8Fw== X-Received: by 2002:a17:90b:4c0a:: with SMTP id na10mr8923343pjb.227.1617279190389; Thu, 01 Apr 2021 05:13:10 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id i7sm5575648pfq.184.2021.04.01.05.13.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:09 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org, "Michael S. Tsirkin" , Viresh Kumar Subject: [PATCH V2 2/6] hw/virtio: add boilerplate for vhost-user-i2c device Date: Thu, 1 Apr 2021 17:42:31 +0530 Message-Id: <6dbe80dc7c82d9582db1b8fc4493d253cecc24b6.1617278395.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=viresh.kumar@linaro.org; helo=mail-pj1-x1030.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This creates the QEMU side of the vhost-user-i2c device which connects to the remote daemon. It is based of vhost-user-fs code. Signed-off-by: Viresh Kumar Reviewed-by: Alex Bennée --- hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c.c | 285 +++++++++++++++++++++++++++++ include/hw/virtio/vhost-user-i2c.h | 37 ++++ 4 files changed, 328 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c.c create mode 100644 include/hw/virtio/vhost-user-i2c.h diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index 0eda25c4e1bf..35ab45e2095c 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -58,3 +58,8 @@ config VIRTIO_MEM depends on LINUX depends on VIRTIO_MEM_SUPPORTED select MEM_DEVICE + +config VHOST_USER_I2C + bool + default y + depends on VIRTIO && VHOST_USER diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index fbff9bc9d4de..1a0d736a0db5 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -25,6 +25,7 @@ virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock. virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) +virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c')) diff --git a/hw/virtio/vhost-user-i2c.c b/hw/virtio/vhost-user-i2c.c new file mode 100644 index 000000000000..20a49bc0078b --- /dev/null +++ b/hw/virtio/vhost-user-i2c.c @@ -0,0 +1,285 @@ +/* + * Vhost-user i2c virtio device + * + * Copyright (c) 2021 Viresh Kumar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/virtio-bus.h" +#include "hw/virtio/vhost-user-i2c.h" +#include "qemu/error-report.h" +#include "standard-headers/linux/virtio_ids.h" + +static void vu_i2c_start(VirtIODevice *vdev) +{ + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); + int ret; + int i; + + if (!k->set_guest_notifiers) { + error_report("binding does not support guest notifiers"); + return; + } + + ret = vhost_dev_enable_notifiers(&i2c->vhost_dev, vdev); + if (ret < 0) { + error_report("Error enabling host notifiers: %d", -ret); + return; + } + + ret = k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, true); + if (ret < 0) { + error_report("Error binding guest notifier: %d", -ret); + goto err_host_notifiers; + } + + i2c->vhost_dev.acked_features = vdev->guest_features; + ret = vhost_dev_start(&i2c->vhost_dev, vdev); + if (ret < 0) { + error_report("Error starting vhost-user-i2c: %d", -ret); + goto err_guest_notifiers; + } + + /* + * guest_notifier_mask/pending not used yet, so just unmask + * everything here. virtio-pci will do the right thing by + * enabling/disabling irqfd. + */ + for (i = 0; i < i2c->vhost_dev.nvqs; i++) { + vhost_virtqueue_mask(&i2c->vhost_dev, vdev, i, false); + } + + return; + +err_guest_notifiers: + k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, false); +err_host_notifiers: + vhost_dev_disable_notifiers(&i2c->vhost_dev, vdev); +} + +static void vu_i2c_stop(VirtIODevice *vdev) +{ + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); + int ret; + + if (!k->set_guest_notifiers) { + return; + } + + vhost_dev_stop(&i2c->vhost_dev, vdev); + + ret = k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, false); + if (ret < 0) { + error_report("vhost guest notifier cleanup failed: %d", ret); + return; + } + + vhost_dev_disable_notifiers(&i2c->vhost_dev, vdev); +} + +static void vu_i2c_set_status(VirtIODevice *vdev, uint8_t status) +{ + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK; + + if (!vdev->vm_running) { + should_start = false; + } + + if (i2c->vhost_dev.started == should_start) { + return; + } + + if (should_start) { + vu_i2c_start(vdev); + } else { + vu_i2c_stop(vdev); + } +} + +static uint64_t vu_i2c_get_features(VirtIODevice *vdev, + uint64_t requested_features, Error **errp) +{ + /* No feature bits used yet */ + return requested_features; +} + +static void vu_i2c_handle_output(VirtIODevice *vdev, VirtQueue *vq) +{ + /* + * Not normally called; it's the daemon that handles the queue; + * however virtio's cleanup path can call this. + */ +} + +static void vu_i2c_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask) +{ + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + + vhost_virtqueue_mask(&i2c->vhost_dev, vdev, idx, mask); +} + +static bool vu_i2c_guest_notifier_pending(VirtIODevice *vdev, int idx) +{ + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + + return vhost_virtqueue_pending(&i2c->vhost_dev, idx); +} + +static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserI2C *i2c) +{ + vhost_user_cleanup(&i2c->vhost_user); + virtio_delete_queue(i2c->req_vq); + virtio_cleanup(vdev); + g_free(i2c->vhost_dev.vqs); + i2c->vhost_dev.vqs = NULL; +} + +static int vu_i2c_connect(DeviceState *dev) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + + if (i2c->connected) { + return 0; + } + i2c->connected = true; + + /* restore vhost state */ + if (virtio_device_started(vdev, vdev->status)) { + vu_i2c_start(vdev); + } + + return 0; +} + +static void vu_i2c_disconnect(DeviceState *dev) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + + if (!i2c->connected) { + return; + } + i2c->connected = false; + + if (i2c->vhost_dev.started) { + vu_i2c_stop(vdev); + } +} + +static void vu_i2c_event(void *opaque, QEMUChrEvent event) +{ + DeviceState *dev = opaque; + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostUserI2C *i2c = VHOST_USER_I2C(vdev); + + switch (event) { + case CHR_EVENT_OPENED: + if (vu_i2c_connect(dev) < 0) { + qemu_chr_fe_disconnect(&i2c->conf.chardev); + return; + } + break; + case CHR_EVENT_CLOSED: + vu_i2c_disconnect(dev); + break; + case CHR_EVENT_BREAK: + case CHR_EVENT_MUX_IN: + case CHR_EVENT_MUX_OUT: + /* Ignore */ + break; + } +} + +static void vu_i2c_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostUserI2C *i2c = VHOST_USER_I2C(dev); + int ret; + + if (!i2c->conf.chardev.chr) { + error_setg(errp, "missing chardev"); + return; + } + + if (!vhost_user_init(&i2c->vhost_user, &i2c->conf.chardev, errp)) { + return; + } + + virtio_init(vdev, "vhost-user-i2c", VIRTIO_ID_I2C_ADAPTER, 0); + + i2c->req_vq = virtio_add_queue(vdev, 4, vu_i2c_handle_output); + i2c->vhost_dev.nvqs = 1; + i2c->vhost_dev.vqs = g_new0(struct vhost_virtqueue, i2c->vhost_dev.nvqs); + ret = vhost_dev_init(&i2c->vhost_dev, &i2c->vhost_user, + VHOST_BACKEND_TYPE_USER, 0); + if (ret < 0) { + error_setg_errno(errp, -ret, "vhost_dev_init() failed"); + do_vhost_user_cleanup(vdev, i2c); + } + + qemu_chr_fe_set_handlers(&i2c->conf.chardev, NULL, NULL, vu_i2c_event, NULL, + dev, NULL, true); +} + +static void vu_i2c_device_unrealize(DeviceState *dev) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostUserI2C *i2c = VHOST_USER_I2C(dev); + + /* This will stop vhost backend if appropriate. */ + vu_i2c_set_status(vdev, 0); + + vhost_dev_cleanup(&i2c->vhost_dev); + + do_vhost_user_cleanup(vdev, i2c); +} + +static const VMStateDescription vu_i2c_vmstate = { + .name = "vhost-user-i2c", + .unmigratable = 1, +}; + +static Property vu_i2c_properties[] = { + DEFINE_PROP_CHR("chardev", VHostUserI2C, conf.chardev), + DEFINE_PROP_END_OF_LIST(), +}; + +static void vu_i2c_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + + device_class_set_props(dc, vu_i2c_properties); + dc->vmsd = &vu_i2c_vmstate; + set_bit(DEVICE_CATEGORY_INPUT, dc->categories); + vdc->realize = vu_i2c_device_realize; + vdc->unrealize = vu_i2c_device_unrealize; + vdc->get_features = vu_i2c_get_features; + vdc->set_status = vu_i2c_set_status; + vdc->guest_notifier_mask = vu_i2c_guest_notifier_mask; + vdc->guest_notifier_pending = vu_i2c_guest_notifier_pending; +} + +static const TypeInfo vu_i2c_info = { + .name = TYPE_VHOST_USER_I2C, + .parent = TYPE_VIRTIO_DEVICE, + .instance_size = sizeof(VHostUserI2C), + .class_init = vu_i2c_class_init, +}; + +static void vu_i2c_register_types(void) +{ + type_register_static(&vu_i2c_info); +} + +type_init(vu_i2c_register_types) diff --git a/include/hw/virtio/vhost-user-i2c.h b/include/hw/virtio/vhost-user-i2c.h new file mode 100644 index 000000000000..a5fffcb6096c --- /dev/null +++ b/include/hw/virtio/vhost-user-i2c.h @@ -0,0 +1,37 @@ +/* + * Vhost-user i2c virtio device + * + * Copyright (c) 2021 Viresh Kumar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef _QEMU_VHOST_USER_I2C_H +#define _QEMU_VHOST_USER_I2C_H + +#include "hw/virtio/virtio.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/vhost-user.h" +#include "chardev/char-fe.h" + +#define TYPE_VHOST_USER_I2C "vhost-user-i2c-device" +OBJECT_DECLARE_SIMPLE_TYPE(VHostUserI2C, VHOST_USER_I2C) + +typedef struct { + CharBackend chardev; +} VHostUserI2CConf; + +struct VHostUserI2C { + /*< private >*/ + VirtIODevice parent; + VHostUserI2CConf conf; + struct vhost_virtqueue *vhost_vq; + struct vhost_dev vhost_dev; + VhostUserState vhost_user; + VirtQueue *req_vq; + bool connected; + + /*< public >*/ +}; + +#endif /* _QEMU_VHOST_USER_I2C_H */ From patchwork Thu Apr 1 12:12:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFCACC433ED for ; Thu, 1 Apr 2021 12:14:50 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3CC36610E8 for ; Thu, 1 Apr 2021 12:14:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3CC36610E8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47868 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwDp-0001sY-5T for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:14:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47754) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCK-0000YK-L7 for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:16 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]:40794) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCI-0004PP-Ld for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:16 -0400 Received: by mail-pl1-x62b.google.com with SMTP id h8so918333plt.7 for ; Thu, 01 Apr 2021 05:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=R7dqDU5k5co2s2wETT/LvVbwqOE2ojRZvo5wYukiZUs=; b=q8uHP05Xn4/2tK15u03Rm0MjoBCftldQ771+CE1p8f/6phJUXbZq1nZzFtj6Nwy+q6 PCG3YvBvcXkgFpuUtPo2zTBGBvIXiO2NPgpBl+NREaqMgOWNSBbvmJBYHDygPmup5oZj b6tFpQoH1jZKpmVMkgUVM2BYg49CSZYIxJxOTcH1EqiCtfpf0fxAPByttQDtl8r1ZrPI LuXF7TO+XwiLV5rDLaSIAgAv7W3PAN+BktnKnf6ojpsv65uCBNKSSBrcfhwLE+BFqOts AL2NBjKNVeLkUkkA9nVGYq30hm2RkkhxSKXuEtIU9qh32cjFocSj+qYn/0HuUnvE/9DA 8QFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=R7dqDU5k5co2s2wETT/LvVbwqOE2ojRZvo5wYukiZUs=; b=iu+TqHwbAyfHhBfwW/EupWjGPptxzwuW2DlAWuX33eTeh4p8Ltv0DBDC9OCM299G0V JK08IXctKoDdfByW0OsVT1WrAzKVe0lf+XnfWb0LZw9ES8nXsXXkxibziDzGGsd7Yys4 hSyZCYbGmZKiNQCVfmDh+nQsk68JcQqe8tarebAf+WkRlSbFVRrxUuJxv8NrMKnNrd/x Ete94C6ffohKkI7yc3J+veGpVBAs5HMKvxjblqHxyzZVp0lB/iaagNPq6krcnQPwhANy frCtcuXozTH+ffcEmpvVeayJGFYSVJwZ97IEp1nDEg1KhOydpK+IkXcsCu+t4MuP1ENv sb5g== X-Gm-Message-State: AOAM5326OGHUFEpwS76aq3wxjYm9w3HaB29jOjha3ut5mYGmabYq0vWJ L4Eu+wTXzbM2roVYlJgCzpybs0HESBX8zw== X-Google-Smtp-Source: ABdhPJyF1GC+ezXKmF7+SUtaaW8vgRy64zKj5gQrGz2q0aESo+J8ICKU6CTfahKHKV+zJRmqyjrCQQ== X-Received: by 2002:a17:902:ed84:b029:e7:1f2b:1eb4 with SMTP id e4-20020a170902ed84b02900e71f2b1eb4mr7861307plj.74.1617279192923; Thu, 01 Apr 2021 05:13:12 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id b84sm5553113pfb.162.2021.04.01.05.13.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:12 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org, Viresh Kumar , "Michael S. Tsirkin" Subject: [PATCH V2 3/6] hw/virtio: add vhost-user-i2c-pci boilerplate Date: Thu, 1 Apr 2021 17:42:32 +0530 Message-Id: X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=viresh.kumar@linaro.org; helo=mail-pl1-x62b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This allows is to instantiate a vhost-user-i2c device as part of a PCI bus. It is mostly boilerplate which looks pretty similar to the vhost-user-fs-pci device. Reviewed-by: Alex Bennée Signed-off-by: Viresh Kumar --- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c-pci.c | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c-pci.c diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 1a0d736a0db5..bc352a600911 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -26,6 +26,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) +virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_true: files('vhost-user-i2c-pci.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c')) diff --git a/hw/virtio/vhost-user-i2c-pci.c b/hw/virtio/vhost-user-i2c-pci.c new file mode 100644 index 000000000000..70b7b65fd970 --- /dev/null +++ b/hw/virtio/vhost-user-i2c-pci.c @@ -0,0 +1,69 @@ +/* + * Vhost-user i2c virtio device PCI glue + * + * Copyright (c) 2021 Viresh Kumar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/vhost-user-i2c.h" +#include "virtio-pci.h" + +struct VHostUserI2CPCI { + VirtIOPCIProxy parent_obj; + VHostUserI2C vdev; +}; + +typedef struct VHostUserI2CPCI VHostUserI2CPCI; + +#define TYPE_VHOST_USER_I2C_PCI "vhost-user-i2c-pci-base" + +DECLARE_INSTANCE_CHECKER(VHostUserI2CPCI, VHOST_USER_I2C_PCI, + TYPE_VHOST_USER_I2C_PCI) + +static void vhost_user_i2c_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) +{ + VHostUserI2CPCI *dev = VHOST_USER_I2C_PCI(vpci_dev); + DeviceState *vdev = DEVICE(&dev->vdev); + + vpci_dev->nvectors = 1; + qdev_realize(vdev, BUS(&vpci_dev->bus), errp); +} + +static void vhost_user_i2c_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); + k->realize = vhost_user_i2c_pci_realize; + set_bit(DEVICE_CATEGORY_INPUT, dc->categories); + pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id = 0; /* Set by virtio-pci based on virtio id */ + pcidev_k->revision = 0x00; + pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; +} + +static void vhost_user_i2c_pci_instance_init(Object *obj) +{ + VHostUserI2CPCI *dev = VHOST_USER_I2C_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VHOST_USER_I2C); +} + +static const VirtioPCIDeviceTypeInfo vhost_user_i2c_pci_info = { + .base_name = TYPE_VHOST_USER_I2C_PCI, + .non_transitional_name = "vhost-user-i2c-pci", + .instance_size = sizeof(VHostUserI2CPCI), + .instance_init = vhost_user_i2c_pci_instance_init, + .class_init = vhost_user_i2c_pci_class_init, +}; + +static void vhost_user_i2c_pci_register(void) +{ + virtio_pci_types_register(&vhost_user_i2c_pci_info); +} + +type_init(vhost_user_i2c_pci_register); From patchwork Thu Apr 1 12:12:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A9BBC433ED for ; Thu, 1 Apr 2021 12:23:01 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2748061056 for ; Thu, 1 Apr 2021 12:23:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2748061056 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:56164 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwLk-0005p7-69 for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:23:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47784) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCO-0000b5-FV for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:20 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]:33464) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCL-0004T6-96 for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:20 -0400 Received: by mail-pf1-x436.google.com with SMTP id x26so1380805pfn.0 for ; Thu, 01 Apr 2021 05:13:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GyJKcAKDbAOi377ljHtaMrB/No31n5CLGnaNTI6+HOw=; b=YQ09B+cN9cSB6DiW1U+7LdPVeCLD4lkSGxZQXTyyiL2nYIko/gA96LmXRj5w9UlUro XAQ52TADDVc6YNw9uMA5GSA7mgNK4/pmX42RXSBS8xCumyEsD8tUMzUslSAp7+68amKp +U6CNhwXnfYLuXu+H/xf3TdGMTPBdwNUIkDR2RC7peT1WD+t3LutQOxijCxatyOCKaNB 4EYiHdOB7OxdSSRHKOy++Csv2yv4QoWA1hmAYT55cmN3+imrGB+lOm7I7LHfrU6bjTz/ lGz5GZuE0wTWrFr4KivI/0w5YXafp3njIb/ESht3h1Wpk+JAsPvoLYsnO8yCiwtbdEUG Z77w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GyJKcAKDbAOi377ljHtaMrB/No31n5CLGnaNTI6+HOw=; b=Zteeo4u6qADAHqViPyHQEWP3+kkemZQeNTmBiAFIjrDA2qN7tWNIDsi2yAba50VB+j eW9wPEgw9zjfQV9L0lhTObmcnjokH3y0Z8UfYHaPAiL2GzmBLQjY5RSaal1UbJuufbiu ODE/GgFcAAT+lJiNgUYnHBKekPgeFTQ3HJoXdW6rOKalBBnYd82zF8KxLLuQYy3ud1ut IyYgfwDx753QaUwapC2awh61Q+PEoAGodk3URqWRSKP3tnnkvJwq05g2HeQkZdFU0cpp 37dimqZpVg6wGDTWWtgaXcUmjUDri9kLQbrETyE+D8Zf8MA1k+uPLabFZbPaC3Cei8/L 4Umg== X-Gm-Message-State: AOAM5338Kb9Kl4WIIDlCLURewheZ9n8rbvdVWFA6eHF7Ue6ePyb1JgAN BJ96nyiCUQ2w4F5ADGQa7Ju7VX9MyR9pVw== X-Google-Smtp-Source: ABdhPJxhFmrYtfpMIIStk0a9QSzZL+2tkGvNq30Gyy9eorUgxNDA/Y5yngCmsS2ziHJNYFQPg7Lw+w== X-Received: by 2002:a05:6a00:2345:b029:20b:c007:f9a4 with SMTP id j5-20020a056a002345b029020bc007f9a4mr7310653pfj.42.1617279195719; Thu, 01 Apr 2021 05:13:15 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id n6sm1938141pfq.214.2021.04.01.05.13.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:15 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org, Viresh Kumar Subject: [PATCH V2 4/6] tools/vhost-user-i2c: Add backend driver Date: Thu, 1 Apr 2021 17:42:33 +0530 Message-Id: X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::436; envelope-from=viresh.kumar@linaro.org; helo=mail-pf1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This adds the vhost-user backend driver to support virtio based I2C and SMBUS devices. vhost-user-i2c --help Signed-off-by: Viresh Kumar --- tools/meson.build | 8 + tools/vhost-user-i2c/50-qemu-i2c.json.in | 5 + tools/vhost-user-i2c/main.c | 809 +++++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 4 files changed, 832 insertions(+) create mode 100644 tools/vhost-user-i2c/50-qemu-i2c.json.in create mode 100644 tools/vhost-user-i2c/main.c create mode 100644 tools/vhost-user-i2c/meson.build diff --git a/tools/meson.build b/tools/meson.build index 3e5a0abfa29f..8271e110978b 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -24,3 +24,11 @@ endif if have_virtiofsd subdir('virtiofsd') endif + +have_virtioi2c= (have_system and + have_tools and + 'CONFIG_LINUX' in config_host) + +if have_virtioi2c + subdir('vhost-user-i2c') +endif diff --git a/tools/vhost-user-i2c/50-qemu-i2c.json.in b/tools/vhost-user-i2c/50-qemu-i2c.json.in new file mode 100644 index 000000000000..dafd1337fa9c --- /dev/null +++ b/tools/vhost-user-i2c/50-qemu-i2c.json.in @@ -0,0 +1,5 @@ +{ + "description": "QEMU vhost-user-i2c", + "type": "bridge", + "binary": "@libexecdir@/vhost-user-i2c" +} diff --git a/tools/vhost-user-i2c/main.c b/tools/vhost-user-i2c/main.c new file mode 100644 index 000000000000..2e0a17dd6ed6 --- /dev/null +++ b/tools/vhost-user-i2c/main.c @@ -0,0 +1,809 @@ +/* + * VIRTIO I2C Emulation via vhost-user + * + * Copyright (c) 2021 Viresh Kumar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#define G_LOG_DOMAIN "vhost-user-i2c" +#define G_LOG_USE_STRUCTURED 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "qemu/compiler.h" +#include "qemu/cutils.h" +#include "standard-headers/linux/virtio_i2c.h" +#include "subprojects/libvhost-user/libvhost-user-glib.h" +#include "subprojects/libvhost-user/libvhost-user.h" + +#define VHOST_USER_I2C_MAX_QUEUES 1 + +/* vhost-user-i2c definitions */ + +#define MAX_I2C_VDEV (1 << 7) +#define MAX_I2C_ADAPTER 16 + +typedef struct { + int32_t fd; + int32_t bus; + bool smbus; + bool clients[MAX_I2C_VDEV]; +} VI2cAdapter; + +typedef struct { + VugDev dev; + GMainLoop *loop; + VI2cAdapter *adapter[MAX_I2C_ADAPTER]; + uint16_t adapter_map[MAX_I2C_VDEV]; + uint32_t adapter_num; +} VuI2c; + +static gboolean print_cap, verbose; +static gchar *socket_path, *device_list; +static gint socket_fd = -1; + +static GOptionEntry options[] = { + { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &socket_path, + "Location of vhost-user Unix domain socket, incompatible with --fd", + "PATH" }, + { "fd", 'f', 0, G_OPTION_ARG_INT, &socket_fd, + "Specify file-descriptor of the backend, don't use with --socket-path", + "FD" }, + { "device-list", 'l', 0, G_OPTION_ARG_STRING, &device_list, + "List of i2c-dev bus and attached devices", "I2C Devices" }, + { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &print_cap, + "Output to stdout the backend capabilities in JSON format and exit", + NULL}, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, + "Be more verbose in output", NULL}, + { NULL } +}; + + +/* I2c helpers */ +static void fmt_bytes(GString *s, uint8_t *bytes, int len) +{ + int32_t i; + for (i = 0; i < len; i++) { + if (i && i % 16 == 0) { + g_string_append_c(s, '\n'); + } + g_string_append_printf(s, "%x ", bytes[i]); + } +} + +static void vi2c_dump_msg(struct i2c_msg *msgs, size_t count) +{ + int32_t i; + + for (i = 0; i < count; i++) { + g_autoptr(GString) s = g_string_new("\nI2c request: "); + + g_string_append_printf(s, "addr: %x\n", msgs[i].addr); + g_string_append_printf(s, "transfer len: %x\n", msgs[i].len); + + g_string_append_printf(s, "%s: ", msgs[i].flags & I2C_M_RD ? + "Data read" : "Data Written"); + fmt_bytes(s, (uint8_t *)msgs[i].buf, msgs[i].len); + g_string_append_printf(s, "\n"); + + g_debug("%s: %s", __func__, s->str); + } +} + +static int vi2c_map_adapters(VuI2c *i2c) +{ + VI2cAdapter *adapter; + int32_t i, client_addr; + + /* + * Flatten the map for client address and adapter to the array: + * + * adapter_map[MAX_I2C_VDEV]: + * + * Adapter | adapter2 | none | adapter1 | adapter3 | none| (val) + * |----------|-------|----------|----------|-----| + * Slave Address | addr 1 | none | addr 2 | addr 3 | none| (idx) + * |<-----------------------MAX_I2C_VDEV---------------->| + */ + for (i = 0; i < i2c->adapter_num; i++) { + adapter = i2c->adapter[i]; + + for (client_addr = 0; client_addr < MAX_I2C_VDEV; client_addr++) { + if (adapter->clients[client_addr]) { + if (i2c->adapter_map[client_addr]) { + g_printerr("client addr %x repeated, not supported!\n", + client_addr); + return -1; + } + + /* The array is initialized to 0, + 1 for index */ + i2c->adapter_map[client_addr] = i + 1; + if (verbose) { + g_print("client: 0x%x -> i2c adapter: %d\n", client_addr, + adapter->bus); + } + } + } + } + return 0; +} + +static VI2cAdapter *vi2c_find_adapter(VuI2c *i2c, uint16_t addr) +{ + if (addr < MAX_I2C_VDEV && (i2c->adapter_map[addr] != 0)) { + return i2c->adapter[i2c->adapter_map[addr] - 1]; + } + + return NULL; +} + +static bool vi2c_set_client_addr(VI2cAdapter *adapter, uint16_t addr) +{ + if (ioctl(adapter->fd, I2C_SLAVE, addr) < 0) { + if (errno == EBUSY) { + g_printerr("client device %x is busy!\n", addr); + } else { + g_printerr("client device %d does not exist!\n", addr); + } + return false; + } + return true; +} + +static void vi2c_remove_adapters(VuI2c *i2c) +{ + VI2cAdapter *adapter; + int32_t i; + + for (i = 0; i < MAX_I2C_ADAPTER; i++) { + adapter = i2c->adapter[i]; + if (!adapter) { + break; + } + + if (adapter->fd > 0) { + close(adapter->fd); + } + + g_free(adapter); + i2c->adapter[i] = NULL; + } +} + +static VI2cAdapter *vi2c_create_adapter(int32_t bus, uint16_t client_addr[], + int32_t n_client) +{ + VI2cAdapter *adapter; + char path[20]; + uint64_t funcs; + int32_t fd, i; + + if (bus < 0) { + return NULL; + } + + adapter = g_malloc0(sizeof(*adapter)); + if (!adapter) { + g_printerr("failed to alloc adapter"); + return NULL; + } + + snprintf(path, sizeof(path), "/dev/i2c-%d", bus); + path[sizeof(path) - 1] = '\0'; + + fd = open(path, O_RDWR); + if (fd < 0) { + g_printerr("virtio_i2c: failed to open %s\n", path); + goto fail; + } + + adapter->fd = fd; + adapter->bus = bus; + + if (ioctl(fd, I2C_FUNCS, &funcs) < 0) { + g_printerr("virtio_i2c: failed to get functionality %s: %d\n", path, + errno); + goto close_fd; + } + + if (funcs & I2C_FUNC_I2C) { + adapter->smbus = false; + } else if (funcs & I2C_FUNC_SMBUS_WORD_DATA) { + adapter->smbus = true; + } else { + g_printerr("virtio_i2c: invalid functionality %lx\n", funcs); + goto close_fd; + } + + for (i = 0; i < n_client; i++) { + if (client_addr[i]) { + if (!vi2c_set_client_addr(adapter, client_addr[i])) { + goto close_fd; + } + + if (adapter->clients[client_addr[i]]) { + g_printerr("client addr 0x%x repeat, not allowed.\n", + client_addr[i]); + goto close_fd; + } + + adapter->clients[client_addr[i]] = true; + if (verbose) { + g_print("Added client 0x%x to bus %u\n", client_addr[i], bus); + } + } + } + + if (verbose) { + g_print("Added adapter: bus: %d, func %s\n", bus, + adapter->smbus ? "smbus" : "i2c"); + } + return adapter; + +close_fd: + close(fd); +fail: + g_free(adapter); + return NULL; +} + +/* + * Virtio I2C device list format. + * + * :[:], + * [:[:]] + * + * bus (dec): adatper bus number. + * e.g. 2 for /dev/i2c-2 + * client_addr (hex): address for client device + * e.g. 0x1C or 1C + * + * Example: --device-list="2:0x1c:0x20,3:0x10:0x2c" + * + * Note: client address can not repeat. + */ +static int32_t vi2c_parse(VuI2c *i2c) +{ + uint16_t client_addr[MAX_I2C_VDEV]; + int32_t n_adapter = 0, n_client; + int64_t addr, bus; + const char *cp, *t; + + while (device_list) { + /* Read :[:] entries one by one */ + cp = strsep(&device_list, ","); + + if (!cp || *cp == '\0') { + break; + } + + if (n_adapter == MAX_I2C_ADAPTER) { + g_printerr("too many adapter (%d), only support %d\n", n_adapter, + MAX_I2C_ADAPTER); + goto out; + } + + if (qemu_strtol(cp, &t, 10, &bus) || bus < 0) { + g_printerr("Invalid bus number %s\n", cp); + goto out; + } + + cp = t; + n_client = 0; + + /* Parse clients [:] entries one by one */ + while (cp != NULL && *cp != '\0') { + if (*cp == ':') { + cp++; + } + + if (n_client == MAX_I2C_VDEV) { + g_printerr("too many devices (%d), only support %d\n", + n_client, MAX_I2C_VDEV); + goto out; + } + + if (qemu_strtol(cp, &t, 16, &addr) || + addr < 0 || addr > MAX_I2C_VDEV) { + g_printerr("Invalid address %s : %lx\n", cp, addr); + goto out; + } + + client_addr[n_client++] = addr; + cp = t; + if (verbose) { + g_print("i2c adapter %ld:0x%lx\n", bus, addr); + } + } + + i2c->adapter[n_adapter] = vi2c_create_adapter(bus, client_addr, + n_client); + if (!i2c->adapter[n_adapter]) { + goto out; + } + + n_adapter++; + } + + if (!n_adapter) { + g_printerr("Failed to add any adapters\n"); + return -1; + } + + i2c->adapter_num = n_adapter; + + if (!vi2c_map_adapters(i2c)) { + return 0; + } + +out: + vi2c_remove_adapters(i2c); + return -1; +} + +static int32_t i2c_xfer(VI2cAdapter *adapter, struct i2c_msg *msgs, + size_t count) +{ + struct i2c_rdwr_ioctl_data data; + + data.nmsgs = count; + data.msgs = msgs; + + return ioctl(adapter->fd, I2C_RDWR, &data); +} + +/* + * Based on Linux's drivers/i2c/i2c-core-smbus.c:i2c_smbus_xfer_emulated(). This + * function tries to reverse what Linux does, only support basic modes (up to + * word transfer). + */ +static int32_t smbus_xfer(VI2cAdapter *adapter, struct i2c_msg *msgs, + size_t count) +{ + union i2c_smbus_data data; + struct i2c_smbus_ioctl_data smbus_data = { .data = &data }; + int32_t ret; + + smbus_data.command = msgs[0].buf[0]; + + switch (count) { + case 1: + if (msgs[0].flags & I2C_M_RD) { + if (msgs[0].len > 1) { + g_printerr("Incorrect message length for read operation: %d\n", + msgs[0].len); + return -1; + } + smbus_data.read_write = I2C_SMBUS_READ; + } else { + smbus_data.read_write = I2C_SMBUS_WRITE; + } + + if (!msgs[0].len) { + smbus_data.size = I2C_SMBUS_QUICK; + smbus_data.data = NULL; + } else if (msgs[0].len == 1) { + smbus_data.size = I2C_SMBUS_BYTE; + if (smbus_data.read_write == I2C_SMBUS_WRITE) { + smbus_data.data = NULL; + } + } else if (msgs[0].len == 2) { + smbus_data.size = I2C_SMBUS_BYTE_DATA; + data.byte = msgs[0].buf[1]; + } else if (msgs[0].len == 3) { + smbus_data.size = I2C_SMBUS_WORD_DATA; + data.word = msgs[0].buf[1] | (msgs[0].buf[2] << 8); + } else { + g_printerr("Message length not supported for write operation: %d\n", + msgs[0].len); + return -1; + } + + break; + case 2: + if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD) || + (msgs[0].len != 1) || (msgs[1].len > 2)) { + g_printerr("Expecting a valid read smbus transfer: %ld: %d: %d\n", + count, msgs[0].len, msgs[1].len); + return -1; + } + + smbus_data.read_write = I2C_SMBUS_READ; + smbus_data.size = msgs[1].len == 1 ? + I2C_SMBUS_BYTE_DATA : I2C_SMBUS_WORD_DATA; + break; + default: + g_printerr("Invalid number of messages for smbus xfer: %ld\n", count); + return -1; + } + + if (verbose) { + g_print("SMBUS command: %x: %x: %x\n", smbus_data.read_write, + smbus_data.command, smbus_data.size); + } + + ret = ioctl(adapter->fd, I2C_SMBUS, &smbus_data); + if (ret < 0) { + return ret; + } + + if (smbus_data.read_write == I2C_SMBUS_WRITE) { + return 0; + } + + switch (smbus_data.size) { + case I2C_SMBUS_BYTE: + msgs[0].buf[0] = data.byte; + break; + case I2C_SMBUS_BYTE_DATA: + msgs[1].buf[0] = data.byte; + break; + case I2C_SMBUS_WORD_DATA: + msgs[1].buf[0] = data.word & 0xff; + msgs[1].buf[1] = data.word >> 8; + break; + } + + return 0; +} + +static uint8_t vi2c_xfer(VuDev *dev, struct i2c_msg *msgs, size_t count) +{ + VuI2c *i2c = container_of(dev, VuI2c, dev.parent); + VI2cAdapter *adapter; + int8_t ret; + + /* Assume that adapter should be same for all messages sent together */ + adapter = vi2c_find_adapter(i2c, msgs[0].addr); + if (!adapter) { + g_printerr("Failed to find adapter for address: %x\n", msgs[0].addr); + return VIRTIO_I2C_MSG_ERR; + } + + /* Set client's address */ + if (!vi2c_set_client_addr(adapter, msgs[0].addr)) { + return VIRTIO_I2C_MSG_ERR; + } + + if (adapter->smbus) { + ret = smbus_xfer(adapter, msgs, count); + } else { + ret = i2c_xfer(adapter, msgs, count); + } + + if (ret < 0) { + g_printerr("Failed to transfer data to address %x : %d\n", + msgs[0].addr, errno); + return VIRTIO_I2C_MSG_ERR; + } + + if (verbose) { + vi2c_dump_msg(msgs, count); + } + + return VIRTIO_I2C_MSG_OK; +} + +/* Virtio helpers */ +static uint64_t vi2c_get_features(VuDev *dev) +{ + if (verbose) { + g_info("%s: replying", __func__); + } + return 0; +} + +static void vi2c_set_features(VuDev *dev, uint64_t features) +{ + if (verbose && features) { + g_autoptr(GString) s = g_string_new("Requested un-handled feature"); + g_string_append_printf(s, " 0x%" PRIx64 "", features); + g_info("%s: %s", __func__, s->str); + } +} + +static void vi2c_handle_ctrl(VuDev *dev, int qidx) +{ + VuVirtq *vq = vu_get_queue(dev, qidx); + struct virtio_i2c_out_hdr *out_hdr; + size_t i, count = 0, len, in_hdr_len; + struct i2c_msg *msgs; + VuVirtqElement *elem; + uint8_t status; + struct { + struct virtio_i2c_in_hdr *in_hdr; + VuVirtqElement *elem; + size_t size; + } *info; + + /* Count number of messages */ + do { + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + } while (elem && ++count); + + if (!count) { + if (verbose) { + g_printerr("Virtqueue can't have 0 elements\n"); + } + return; + } + + /* Rewind everything, now that we have counted the number of messages */ + vu_queue_rewind(dev, vq, count); + + if (verbose) { + g_print("Received %ld messages in virtqueue\n", count); + } + + /* Allocate memory for msgs and info */ + msgs = g_malloc0_n(count, sizeof(*msgs) + sizeof(*info)); + if (!msgs) { + g_printerr("failed to allocate space for messages\n"); + return; + } + info = (void *)(msgs + count); + + for (i = 0; i < count; i++) { + elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement)); + if (!elem) { + g_printerr("Failed to pop element: %ld : %ld\n", i, count); + goto out; + } + info[i].elem = elem; + info[i].size = sizeof(*info[i].in_hdr); + + g_debug("%s: got queue (in %d, out %d)", __func__, elem->in_num, + elem->out_num); + + /* Validate size of "out" header */ + if (elem->out_sg[0].iov_len != sizeof(*out_hdr)) { + g_warning("%s: Invalid out hdr %zu : %zu\n", __func__, + elem->out_sg[0].iov_len, sizeof(*out_hdr)); + goto out; + } + + out_hdr = elem->out_sg[0].iov_base; + + /* Bit 0 is reserved in virtio spec */ + msgs[i].addr = le16toh(out_hdr->addr) >> 1; + + /* Read Operation */ + if (elem->out_num == 1 && elem->in_num == 2) { + len = elem->in_sg[0].iov_len; + if (!len) { + g_warning("%s: Read buffer length can't be zero\n", __func__); + goto out; + } + + msgs[i].buf = elem->in_sg[0].iov_base; + msgs[i].flags = I2C_M_RD; + msgs[i].len = len; + + info[i].in_hdr = elem->in_sg[1].iov_base; + info[i].size += len; + in_hdr_len = elem->in_sg[1].iov_len; + } else if (elem->out_num == 2 && elem->in_num == 1) { + /* Write Operation */ + len = elem->out_sg[1].iov_len; + if (!len) { + g_warning("%s: Write buffer length can't be zero\n", __func__); + goto out; + } + + msgs[i].buf = elem->out_sg[1].iov_base; + msgs[i].flags = 0; + msgs[i].len = len; + + info[i].in_hdr = elem->in_sg[0].iov_base; + in_hdr_len = elem->in_sg[0].iov_len; + } else { + g_warning("%s: Transfer type not supported (in %d, out %d)\n", + __func__, elem->in_num, elem->out_num); + goto out; + } + + /* Validate size of "in" header */ + if (in_hdr_len != sizeof(*(info[i].in_hdr))) { + g_warning("%s: Invalid in hdr %zu : %zu\n", __func__, in_hdr_len, + sizeof(*(info[i].in_hdr))); + goto out; + } + } + + status = vi2c_xfer(dev, msgs, count); + + for (i = 0; i < count; i++) { + info[i].in_hdr->status = status; + vu_queue_push(dev, vq, info[i].elem, info[i].size); + } + + vu_queue_notify(dev, vq); + +out: + g_free(msgs); +} + +static void +vi2c_queue_set_started(VuDev *dev, int qidx, bool started) +{ + VuVirtq *vq = vu_get_queue(dev, qidx); + + g_debug("queue started %d:%d\n", qidx, started); + + if (!qidx) { + vu_set_queue_handler(dev, vq, started ? vi2c_handle_ctrl : NULL); + } +} + +/* + * vi2c_process_msg: process messages of vhost-user interface + * + * Any that are not handled here are processed by the libvhost library + * itself. + */ +static int vi2c_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply) +{ + VuI2c *i2c = container_of(dev, VuI2c, dev.parent); + + if (msg->request == VHOST_USER_NONE) { + g_main_loop_quit(i2c->loop); + return 1; + } + + return 0; +} + +static const VuDevIface vuiface = { + .set_features = vi2c_set_features, + .get_features = vi2c_get_features, + .queue_set_started = vi2c_queue_set_started, + .process_msg = vi2c_process_msg, +}; + +static gboolean hangup(gpointer user_data) +{ + GMainLoop *loop = (GMainLoop *) user_data; + g_info("%s: caught hangup/quit signal, quitting main loop", __func__); + g_main_loop_quit(loop); + return true; +} + +static void vi2c_panic(VuDev *dev, const char *msg) +{ + g_critical("%s\n", msg); + exit(EXIT_FAILURE); +} + +/* Print vhost-user.json backend program capabilities */ +static void print_capabilities(void) +{ + printf("{\n"); + printf(" \"type\": \"i2c\"\n"); + printf(" \"device-list\"\n"); + printf("}\n"); +} + +static void vi2c_destroy(VuI2c *i2c) +{ + vi2c_remove_adapters(i2c); + vug_deinit(&i2c->dev); + if (socket_path) { + unlink(socket_path); + } +} + +int main(int argc, char *argv[]) +{ + GError *error = NULL; + GOptionContext *context; + g_autoptr(GSocket) socket = NULL; + VuI2c i2c = {0}; + + context = g_option_context_new("vhost-user emulation of I2C device"); + g_option_context_add_main_entries(context, options, "vhost-user-i2c"); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + g_printerr("option parsing failed: %s\n", error->message); + exit(1); + } + + if (print_cap) { + print_capabilities(); + exit(0); + } + + if (!socket_path && socket_fd < 0) { + g_printerr("Please specify either --fd or --socket-path\n"); + exit(EXIT_FAILURE); + } + + if (verbose) { + g_log_set_handler(NULL, G_LOG_LEVEL_MASK, g_log_default_handler, NULL); + g_setenv("G_MESSAGES_DEBUG", "all", true); + } else { + g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | + G_LOG_LEVEL_ERROR, g_log_default_handler, NULL); + } + + /* + * Now create a vhost-user socket that we will receive messages + * on. Once we have our handler set up we can enter the glib main + * loop. + */ + if (socket_path) { + g_autoptr(GSocketAddress) addr = g_unix_socket_address_new(socket_path); + g_autoptr(GSocket) bind_socket = g_socket_new(G_SOCKET_FAMILY_UNIX, + G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); + + if (!g_socket_bind(bind_socket, addr, false, &error)) { + g_printerr("Failed to bind to socket at %s (%s).\n", + socket_path, error->message); + exit(EXIT_FAILURE); + } + if (!g_socket_listen(bind_socket, &error)) { + g_printerr("Failed to listen on socket %s (%s).\n", + socket_path, error->message); + } + g_message("awaiting connection to %s", socket_path); + socket = g_socket_accept(bind_socket, NULL, &error); + if (!socket) { + g_printerr("Failed to accept on socket %s (%s).\n", + socket_path, error->message); + } + } else { + socket = g_socket_new_from_fd(socket_fd, &error); + if (!socket) { + g_printerr("Failed to connect to FD %d (%s).\n", + socket_fd, error->message); + exit(EXIT_FAILURE); + } + } + + if (vi2c_parse(&i2c)) { + exit(EXIT_FAILURE); + } + + /* + * Create the main loop first so all the various sources can be + * added. As well as catching signals we need to ensure vug_init + * can add it's GSource watches. + */ + + i2c.loop = g_main_loop_new(NULL, FALSE); + /* catch exit signals */ + g_unix_signal_add(SIGHUP, hangup, i2c.loop); + g_unix_signal_add(SIGINT, hangup, i2c.loop); + + if (!vug_init(&i2c.dev, VHOST_USER_I2C_MAX_QUEUES, g_socket_get_fd(socket), + vi2c_panic, &vuiface)) { + g_printerr("Failed to initialize libvhost-user-glib.\n"); + exit(EXIT_FAILURE); + } + + + g_message("entering main loop, awaiting messages"); + g_main_loop_run(i2c.loop); + g_message("finished main loop, cleaning up"); + + g_main_loop_unref(i2c.loop); + vi2c_destroy(&i2c); +} diff --git a/tools/vhost-user-i2c/meson.build b/tools/vhost-user-i2c/meson.build new file mode 100644 index 000000000000..f71e9fec7df6 --- /dev/null +++ b/tools/vhost-user-i2c/meson.build @@ -0,0 +1,10 @@ +executable('vhost-user-i2c', files( + 'main.c'), + dependencies: [qemuutil, glib, gio], + install: true, + install_dir: get_option('libexecdir')) + +configure_file(input: '50-qemu-i2c.json.in', + output: '50-qemu-i2c.json', + configuration: config_host, + install_dir: qemu_datadir / 'vhost-user') From patchwork Thu Apr 1 12:12:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177989 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83C2EC433ED for ; Thu, 1 Apr 2021 12:25:49 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E8D436112D for ; Thu, 1 Apr 2021 12:25:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E8D436112D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:58910 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwOS-0007D7-1W for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:25:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47804) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCQ-0000c9-Cv for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:22 -0400 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]:50950) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCN-0004UX-LW for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:22 -0400 Received: by mail-pj1-x102c.google.com with SMTP id gb6so1070882pjb.0 for ; Thu, 01 Apr 2021 05:13:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ucPW6AwDArigoJGiD4YxTX7iziwkWQWTh6uKDGyTHHk=; b=u/dd/lf1oi99LYDvoCC7OKugmOXECYCn5xUYtlxUW5yqpbnI0kEnqSFRE5zmqoR/FC ac6oMTGJMWF9eFJ1vv/ge7ETwHrZnWGAUsRs8DpN2O6UlhU82utMZDi+GhKsKOgSTiQi JXEDf/wbYtLx/gh99zbr/v2DwWcO2kFuI1+G9jgbYm1ubU4I+JpxR4sMXIKbZHwjiFvP phOZuUcwPjQz+Elp6fHc2EHYJ1/2zwizmDL2rYrySP3SjgG6ioIOVP0rETN8OHhZ54+q f1qteeT1VVwMHNapt0VFIUWFTPjvErDSh6xZQavDE1W4kgW31kPs8z3QcePtN3AUoGb/ OY1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ucPW6AwDArigoJGiD4YxTX7iziwkWQWTh6uKDGyTHHk=; b=R0nf3TiskKpHEsBFiTu1JCVN+mLmhoUwol69beGDv8h6Bifyhhq7FRxRv1O4QLDfMw FVqSCbf7l5I5E+D9yrc0b44Z1rHe1+9lQguJr8eEbUx9yMY8V3P02UFEr6CoO+YWjeWK XDmJbIkv8bIliuG1qvQdusHRC/jDrnxy33L9GyS9WJwB+UN85mRMHpvIgqA0UERAz7/9 taNi2qropm6JE3JeB5anlm5Sn9bv5wTEZkuDirtOPbrISrodOSo86CjIDfjYPxqXhsj/ 4u0ozggjqR0p6XZGT9IoTPslL7Y9d52F2R8SQ5qVStXhgJVFKiC72sd0p6VoHHqE4HAo e7zg== X-Gm-Message-State: AOAM531quSTejpWJzI/v4w+suaQbbsnOay5I3gmfTmdiu5Wp90Zj3lWo lyn+lAzbliYunJql8HoAGP83ZBF7YpJ/pg== X-Google-Smtp-Source: ABdhPJw8gwRKWUZ8/UkJl1pvJ0lir8hONId0W5spn4seUioY/OII90kptgPTKjq2n3V+okCk62hwfQ== X-Received: by 2002:a17:90a:ff91:: with SMTP id hf17mr8954713pjb.34.1617279198360; Thu, 01 Apr 2021 05:13:18 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id ha8sm5311443pjb.6.2021.04.01.05.13.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:17 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org, Viresh Kumar Subject: [PATCH V2 5/6] docs: add a man page for vhost-user-i2c Date: Thu, 1 Apr 2021 17:42:34 +0530 Message-Id: <276f8f8825fc5eb084710b8265b9ee9097e0aa69.1617278395.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=viresh.kumar@linaro.org; helo=mail-pj1-x102c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Basic usage and example invocation. Signed-off-by: Viresh Kumar --- docs/tools/index.rst | 1 + docs/tools/vhost-user-i2c.rst | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 docs/tools/vhost-user-i2c.rst diff --git a/docs/tools/index.rst b/docs/tools/index.rst index 3a5829c17a54..af2519406ddf 100644 --- a/docs/tools/index.rst +++ b/docs/tools/index.rst @@ -17,3 +17,4 @@ QEMU Tools Guide qemu-trace-stap virtfs-proxy-helper virtiofsd + vhost-user-i2c diff --git a/docs/tools/vhost-user-i2c.rst b/docs/tools/vhost-user-i2c.rst new file mode 100644 index 000000000000..1f05c6473ad7 --- /dev/null +++ b/docs/tools/vhost-user-i2c.rst @@ -0,0 +1,75 @@ +QEMU vhost-user-i2c - I2C emulation backend +=========================================== + +Synopsis +-------- + +**vhost-user-i2c** [*OPTIONS*] + +Description +----------- + +This program is a vhost-user backend that emulates a VirtIO I2C bus. +This program takes the layout of the i2c bus and its devices on the host +OS and then talks to them via the /dev/i2c-X interface when a request +comes from the guest OS for an I2C or SMBUS device. + +This program is designed to work with QEMU's ``-device +vhost-user-i2c-pci`` but should work with any virtual machine monitor +(VMM) that supports vhost-user. See the Examples section below. + +Options +------- + +.. program:: vhost-user-i2c + +.. option:: -h, --help + + Print help. + +.. option:: -v, --verbose + + Increase verbosity of output + +.. option:: -s, --socket-path=PATH + + Listen on vhost-user UNIX domain socket at PATH. Incompatible with --fd. + +.. option:: -f, --fd=FDNUM + + Accept connections from vhost-user UNIX domain socket file descriptor FDNUM. + The file descriptor must already be listening for connections. + Incompatible with --socket-path. + +.. option:: -l, --device-list=I2C-DEVICES + + I2c device list at the host OS in the format: + :[:],[:[:]] + + Example: --device-list "2:1c:20,3:10:2c" + + Here, + bus (decimal): adatper bus number. e.g. 2 for /dev/i2c-2, 3 for /dev/i2c-3. + client_addr (hex): address for client device. e.g. 0x1C, 0x20, 0x10, 0x2C. + +Examples +-------- + +The daemon should be started first: + +:: + + host# vhost-user-i2c --socket-path=vi2c.sock --device-list 0:20 + +The QEMU invocation needs to create a chardev socket the device can +use to communicate as well as share the guests memory over a memfd. + +:: + + host# qemu-system \ + -chardev socket,path=vi2c.sock,id=vi2c \ + -device vhost-user-i2c-pci,chardev=vi2c,id=i2c \ + -m 4096 \ + -object memory-backend-file,id=mem,size=4G,mem-path=/dev/shm,share=on \ + -numa node,memdev=mem \ + ... From patchwork Thu Apr 1 12:12:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 12177991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F789C433ED for ; Thu, 1 Apr 2021 12:26:27 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 997436112D for ; Thu, 1 Apr 2021 12:26:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 997436112D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:60516 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRwP3-00080K-Oa for qemu-devel@archiver.kernel.org; Thu, 01 Apr 2021 08:26:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47828) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRwCR-0000g1-V6 for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:23 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:43791) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lRwCQ-0004X0-BE for qemu-devel@nongnu.org; Thu, 01 Apr 2021 08:13:23 -0400 Received: by mail-pj1-x1031.google.com with SMTP id x21-20020a17090a5315b029012c4a622e4aso921767pjh.2 for ; Thu, 01 Apr 2021 05:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=weqa3lPAgEacE8uK3uE+OTNHEhHG1VPxu0/697D38p8=; b=IM2AbVlAHGcniWNrJX3wPQ548M8EC5CqAcf8COZGr0gqqXZVC87AKbeR+h0kd8qcYx iOB00TTcpBEznt9mCRjiCTs9w77DQhbBMvgcNqL2LTMs70MojtuYefbj0lR8uO4sIaC7 FJjxhjoDg5pXpHco+f9Uxup5tbqo4UBiCT+hQViBP7c7kZTLzCv2qGYS28FxwtGHlp9Z BVX8bgw+f54AYoDipo6lzro5YHdxVfNewIGysjoQQgJ9I0nzgAP5gCXS2NK/WTo8Lmy5 BlBuUXAg0CJqUI+eA8lsZgPQEr1hQJ7JGGPkCXfGxd1qYFWOQ07jj+3IX3OfwGeVyLXT A7Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=weqa3lPAgEacE8uK3uE+OTNHEhHG1VPxu0/697D38p8=; b=jxT1MbmWW+YTgHMPfCKsalLbLHjhZx1/8DUtbbCIv/5Cc2R9m550tkoLUeO9wxL9rP RXZ8saHwZa7Y7e8JeD1cWvXbgj5qz4zfH3fu5ToIewwdYO3YzXDk0FP01NllHgAwyp1d iTaYKjXjD7Mf5VPkdide32Eo1avZnwsb3BmclpVkuquLVB12ux44BSuugGDslnYWhzDc 64+/HXE3ZelhuZqmzocWcKzZt/m/XVhwJJKWN9jHAxkoRc3YEdUTwI24mj40wok1ZsAD nB/p5oSXrzbHxyLb29YF6GfUKPl6Uelc4jR9aZfl32qgD2z/zrGSUJjbihdXdLchiO2K OO4g== X-Gm-Message-State: AOAM530WqQqju6GnNG6wTYqOCojiUotkmeXrUbwWk9YtDMgZIF/EUDTs hsItH04NaBZQbfxE12VPWsoqYbHRZZLdkA== X-Google-Smtp-Source: ABdhPJw160tp3wTLcCmShmfV8iy9UQhuACh0HIbq1SGktDuefijmXJO5BoHLSdDVgB4QYE/oFUmGxA== X-Received: by 2002:a17:90a:1502:: with SMTP id l2mr8397999pja.149.1617279201179; Thu, 01 Apr 2021 05:13:21 -0700 (PDT) Received: from localhost ([136.185.154.93]) by smtp.gmail.com with ESMTPSA id t205sm5260304pgb.37.2021.04.01.05.13.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Apr 2021 05:13:20 -0700 (PDT) From: Viresh Kumar To: qemu-devel@nongnu.org Subject: [PATCH V2 6/6] MAINTAINERS: Add entry for virtio-i2c Date: Thu, 1 Apr 2021 17:42:35 +0530 Message-Id: X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=viresh.kumar@linaro.org; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: Vincent Guittot , Jie Deng , Viresh Kumar , Bill Mills , Arnd Bergmann , Mike Holmes , =?utf-8?q?Alex_Benn=C3=A9e?= , stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This patch adds entry for virtio-i2c related files in MAINTAINERS. Signed-off-by: Viresh Kumar --- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 9147e9a429a0..34e1b26c985a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2136,6 +2136,15 @@ F: docs/interop/vhost-user-gpu.rst F: contrib/vhost-user-gpu F: hw/display/vhost-user-* +vhost-user-i2c +M: Viresh Kumar +S: Supported +F: docs/tools/vhost-user-i2c.rst +F: hw/virtio/vhost-user-i2c.c +F: hw/virtio/vhost-user-i2c-pci.c +F: include/hw/virtio/vhost-user-i2c.h +F: tools/vhost-user-i2c/* + Cirrus VGA M: Gerd Hoffmann S: Odd Fixes