From nobody Sun Dec 22 20:15:15 2024 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC1391B277; Thu, 22 Feb 2024 23:17:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708643830; cv=none; b=SXMq4WsOqzTe2dZpq76P7J8AN0k8JrMRULdmR64/04EIImNdo5v/HPrEa3UCP+t1jADiJOqchff7BtrwDLQ5FGO9+QYEh+QyCjJy5s12d+0WjpRPd0o9Plw46h1B6SZE0wVPXES8q/ieHy5RNCUmb6cryzVtFPjm37JGJyopDqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708643830; c=relaxed/simple; bh=0SDf3C3yfT7s6Nm5OfG+fg0wBWPZL/8Mzhz2XJExiYY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=um0QAzDTZHAt8IwG6bruFpdJOHi5jnHxp1YaB5c52cJF00VY0rn8BImM5MJqR1dwd5F7Nem5vva1OTA3zq2pBiu8fgBNBvjDwNLmXZ1FjF4sFlJQ/LXPYDmosKByYioQB+Ln4+Z8B19/mvxbBtg6QM9IFZt/aeYdGH/+8d214eQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=H/wcZInY; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="H/wcZInY" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41MLxFWt026010; Thu, 22 Feb 2024 23:16:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:date:subject:mime-version:content-type :content-transfer-encoding:message-id:references:in-reply-to:to :cc; s=qcppdkim1; bh=TYpwdT9rGpGU+Krn2HEFIYzazdkjNtusHFYwxcfsZAg =; b=H/wcZInYw9DqA1TKF4h0M7BlAGWNbQ/KfRny3iN9QUXCd6MjSpbRIVxfHfV lpWPKSJJGOieAjX02AuK0oeVlSf+DVbKSnX0/8hdRsnj0WjcRFHQrC7hrV6mp3a0 g+SSrCh2RhbLPB7o+ArRjNDoPVOTP7BkLo6ZnF1Nmy7hsRv6kHbZW3UZVHk32BCy cDyBcSE0AW4zE/H+XUDLf8Y0RPzRQUjb5yh3exulGyN+mZ8MM574OiXn4S1CFvzA fKUsURJwYneSWnFIsN8Tj3lYAZey8o4mTjPFu3nu+3bp99l+PH808lPkEZ250J7n IEMyWYcLRTaUBLVgL4+67118wHg== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3we24ajumj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:46 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 41MNGj5M028219 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:45 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 22 Feb 2024 15:16:44 -0800 From: Elliot Berman Date: Thu, 22 Feb 2024 15:16:55 -0800 Subject: [PATCH v17 32/35] virt: gunyah: Add irqfd interface Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID: <20240222-gunyah-v17-32-1e9da6763d38@quicinc.com> References: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> In-Reply-To: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> To: Alex Elder , Srinivas Kandagatla , Murali Nalajal , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Philip Derrin , Prakruthi Deepak Heragu , Jonathan Corbet , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Catalin Marinas , Will Deacon , Konrad Dybcio , Bjorn Andersson , Dmitry Baryshkov , "Fuad Tabba" , Sean Christopherson , "Andrew Morton" CC: , , , , , , Elliot Berman X-Mailer: b4 0.12.4 X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: 4M5Sctox9093hyrqv1fhYVdp-QAefRVI X-Proofpoint-ORIG-GUID: 4M5Sctox9093hyrqv1fhYVdp-QAefRVI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-22_15,2024-02-22_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 clxscore=1015 lowpriorityscore=0 priorityscore=1501 adultscore=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 bulkscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2402120000 definitions=main-2402220179 Enable support for creating irqfds which can raise an interrupt on a Gunyah virtual machine. irqfds are exposed to userspace as a Gunyah VM function with the name "irqfd". If the VM devicetree is not configured to create a doorbell with the corresponding label, userspace will still be able to assert the eventfd but no interrupt will be raised on the guest. Acked-by: Alex Elder Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Kconfig | 9 ++ drivers/virt/gunyah/Makefile | 1 + drivers/virt/gunyah/gunyah_irqfd.c | 187 +++++++++++++++++++++++++++++++++= ++++ include/uapi/linux/gunyah.h | 35 +++++++ 4 files changed, 232 insertions(+) diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index fe2823dc48bac..1685b75fb77a0 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -27,3 +27,12 @@ config GUNYAH_QCOM_PLATFORM extra platform-specific support. =20 Say Y/M here to use Gunyah on Qualcomm platforms. + +config GUNYAH_IRQFD + tristate "Gunyah irqfd interface" + depends on GUNYAH + help + Enable kernel support for creating irqfds which can raise an interrupt + on Gunyah virtual machine. + + Say Y/M here if unsure and you want to support Gunyah VMMs. diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index c4505fce177dd..b41b02792921c 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -5,3 +5,4 @@ gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_m= gr_mem.o guest_memfd.o obj-$(CONFIG_GUNYAH) +=3D gunyah.o gunyah_rsc_mgr.o gunyah_vcpu.o obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) +=3D gunyah_platform_hooks.o obj-$(CONFIG_GUNYAH_QCOM_PLATFORM) +=3D gunyah_qcom.o +obj-$(CONFIG_GUNYAH_IRQFD) +=3D gunyah_irqfd.o diff --git a/drivers/virt/gunyah/gunyah_irqfd.c b/drivers/virt/gunyah/gunya= h_irqfd.c new file mode 100644 index 0000000000000..951da6e8d321e --- /dev/null +++ b/drivers/virt/gunyah/gunyah_irqfd.c @@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct gunyah_irqfd { + struct gunyah_resource *ghrsc; + struct gunyah_vm_resource_ticket ticket; + struct gunyah_vm_function_instance *f; + + bool level; + + struct eventfd_ctx *ctx; + wait_queue_entry_t wait; + poll_table pt; +}; + +static int irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode, int s= ync, + void *key) +{ + struct gunyah_irqfd *irqfd =3D + container_of(wait, struct gunyah_irqfd, wait); + __poll_t flags =3D key_to_poll(key); + int ret =3D 0; + + if (flags & EPOLLIN) { + if (irqfd->ghrsc) { + ret =3D gunyah_hypercall_bell_send(irqfd->ghrsc->capid, 1, + NULL); + if (ret) + pr_err_ratelimited( + "Failed to inject interrupt %d: %d\n", + irqfd->ticket.label, ret); + } else + pr_err_ratelimited( + "Premature injection of interrupt\n"); + } + + return 0; +} + +static void irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *= wqh, + poll_table *pt) +{ + struct gunyah_irqfd *irq_ctx =3D + container_of(pt, struct gunyah_irqfd, pt); + + add_wait_queue(wqh, &irq_ctx->wait); +} + +static bool gunyah_irqfd_populate(struct gunyah_vm_resource_ticket *ticket, + struct gunyah_resource *ghrsc) +{ + struct gunyah_irqfd *irqfd =3D + container_of(ticket, struct gunyah_irqfd, ticket); + int ret; + + if (irqfd->ghrsc) { + pr_warn("irqfd%d already got a Gunyah resource. Check if multiple resour= ces with same label were configured.\n", + irqfd->ticket.label); + return false; + } + + irqfd->ghrsc =3D ghrsc; + if (irqfd->level) { + /* Configure the bell to trigger when bit 0 is asserted (see + * irq_wakeup) and for bell to automatically clear bit 0 once + * received by the VM (ack_mask). need to make sure bit 0 is cleared ri= ght away, + * otherwise the line will never be deasserted. Emulating edge + * trigger interrupt does not need to set either mask + * because irq is listed only once per gunyah_hypercall_bell_send + */ + ret =3D gunyah_hypercall_bell_set_mask(irqfd->ghrsc->capid, 1, 1); + if (ret) + pr_warn("irq %d couldn't be set as level triggered. Might cause IRQ sto= rm if asserted\n", + irqfd->ticket.label); + } + + return true; +} + +static void gunyah_irqfd_unpopulate(struct gunyah_vm_resource_ticket *tick= et, + struct gunyah_resource *ghrsc) +{ +} + +static long gunyah_irqfd_bind(struct gunyah_vm_function_instance *f) +{ + struct gunyah_fn_irqfd_arg *args =3D f->argp; + struct gunyah_irqfd *irqfd; + __poll_t events; + struct fd fd; + long r; + + if (f->arg_size !=3D sizeof(*args)) + return -EINVAL; + + /* All other flag bits are reserved for future use */ + if (args->flags & ~GUNYAH_IRQFD_FLAGS_LEVEL) + return -EINVAL; + + irqfd =3D kzalloc(sizeof(*irqfd), GFP_KERNEL); + if (!irqfd) + return -ENOMEM; + + irqfd->f =3D f; + f->data =3D irqfd; + + fd =3D fdget(args->fd); + if (!fd.file) { + kfree(irqfd); + return -EBADF; + } + + irqfd->ctx =3D eventfd_ctx_fileget(fd.file); + if (IS_ERR(irqfd->ctx)) { + r =3D PTR_ERR(irqfd->ctx); + goto err_fdput; + } + + if (args->flags & GUNYAH_IRQFD_FLAGS_LEVEL) + irqfd->level =3D true; + + init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); + init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); + + irqfd->ticket.resource_type =3D GUNYAH_RESOURCE_TYPE_BELL_TX; + irqfd->ticket.label =3D args->label; + irqfd->ticket.owner =3D THIS_MODULE; + irqfd->ticket.populate =3D gunyah_irqfd_populate; + irqfd->ticket.unpopulate =3D gunyah_irqfd_unpopulate; + + r =3D gunyah_vm_add_resource_ticket(f->ghvm, &irqfd->ticket); + if (r) + goto err_ctx; + + events =3D vfs_poll(fd.file, &irqfd->pt); + if (events & EPOLLIN) + pr_warn("Premature injection of interrupt\n"); + fdput(fd); + + return 0; +err_ctx: + eventfd_ctx_put(irqfd->ctx); +err_fdput: + fdput(fd); + kfree(irqfd); + return r; +} + +static void gunyah_irqfd_unbind(struct gunyah_vm_function_instance *f) +{ + struct gunyah_irqfd *irqfd =3D f->data; + u64 cnt; + + eventfd_ctx_remove_wait_queue(irqfd->ctx, &irqfd->wait, &cnt); + gunyah_vm_remove_resource_ticket(irqfd->f->ghvm, &irqfd->ticket); + eventfd_ctx_put(irqfd->ctx); + kfree(irqfd); +} + +static bool gunyah_irqfd_compare(const struct gunyah_vm_function_instance = *f, + const void *arg, size_t size) +{ + const struct gunyah_fn_irqfd_arg *instance =3D f->argp, *other =3D arg; + + if (sizeof(*other) !=3D size) + return false; + + return instance->label =3D=3D other->label; +} + +DECLARE_GUNYAH_VM_FUNCTION_INIT(irqfd, GUNYAH_FN_IRQFD, 2, gunyah_irqfd_bi= nd, + gunyah_irqfd_unbind, gunyah_irqfd_compare); +MODULE_DESCRIPTION("Gunyah irqfd VM Function"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index 574116f544722..cb7b0bb9bef38 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -63,9 +63,12 @@ struct gunyah_vm_dtb_config { * @GUNYAH_FN_VCPU: create a vCPU instance to control a vCPU * &struct gunyah_fn_desc.arg is a pointer to &struct gunyah_= fn_vcpu_arg * Return: file descriptor to manipulate the vcpu. + * @GUNYAH_FN_IRQFD: register eventfd to assert a Gunyah doorbell + * &struct gunyah_fn_desc.arg is a pointer to &struct gunyah= _fn_irqfd_arg */ enum gunyah_fn_type { GUNYAH_FN_VCPU =3D 1, + GUNYAH_FN_IRQFD, }; =20 #define GUNYAH_FN_MAX_ARG_SIZE 256 @@ -85,6 +88,38 @@ struct gunyah_fn_vcpu_arg { __u32 id; }; =20 +/** + * enum gunyah_irqfd_flags - flags for use in gunyah_fn_irqfd_arg + * @GUNYAH_IRQFD_FLAGS_LEVEL: make the interrupt operate like a level trig= gered + * interrupt on guest side. Triggering IRQFD before + * guest handles the interrupt causes interrupt to + * stay asserted. + */ +enum gunyah_irqfd_flags { + GUNYAH_IRQFD_FLAGS_LEVEL =3D 1UL << 0, +}; + +/** + * struct gunyah_fn_irqfd_arg - Arguments to create an irqfd function. + * + * Create this function with &GUNYAH_VM_ADD_FUNCTION using type &GUNYAH_FN= _IRQFD. + * + * Allows setting an eventfd to directly trigger a guest interrupt. + * irqfd.fd specifies the file descriptor to use as the eventfd. + * irqfd.label corresponds to the doorbell label used in the guest VM's de= vicetree. + * + * @fd: an eventfd which when written to will raise a doorbell + * @label: Label of the doorbell created on the guest VM + * @flags: see &enum gunyah_irqfd_flags + * @padding: padding bytes + */ +struct gunyah_fn_irqfd_arg { + __u32 fd; + __u32 label; + __u32 flags; + __u32 padding; +}; + /** * struct gunyah_fn_desc - Arguments to create a VM function * @type: Type of the function. See &enum gunyah_fn_type. --=20 2.34.1