From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73A79C6FD18 for ; Sat, 4 Mar 2023 01:07:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229704AbjCDBHG (ORCPT ); Fri, 3 Mar 2023 20:07:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjCDBHE (ORCPT ); Fri, 3 Mar 2023 20:07:04 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B61E160D7F; Fri, 3 Mar 2023 17:07:02 -0800 (PST) Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240NXi0014103; Sat, 4 Mar 2023 01:06:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=iML/v5HLLCz4wucUBuZyvoaXNwlF3x8DbYIpKB1O4/E=; b=e2kHhr613e+P6KST4vAAeO283KOAtZ0VHcmQVMpPiarLq0LQoDhrU6TddrH03LbHO6RE HRjVpg8UXi1znxkyC4pyponOiL7o5mlE/BaGtqX8vWjxd3kTmfP01enrwGBm6jndcCWa 6ECnRwVArfgatk8dWidm4QMAtof2JIpTK6wn7y+Y+SqjjTKVuHhN0TvN/QcbOwWRPE+k +71xUYKpgHhJYI9dBOQi8BPBfWm5DQiJuTAY6PjchEoZG8TJeWARJIAlLFjZYIPoCk+G 8jhLT7d5oih9ewI+lC3sQBVFw+A/+8tl2/dM9AkJJR5oppXXjcipcfFLlzpx68ceOH2l iQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3dpxjjb9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:47 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416jGj028205 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06: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.986.41; Fri, 3 Mar 2023 17:06:44 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 01/26] docs: gunyah: Introduce Gunyah Hypervisor Date: Fri, 3 Mar 2023 17:06:07 -0800 Message-ID: <20230304010632.2127470-2-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: xxl7lSxgZXADjYx9DqNgzXnRifAw-xZq X-Proofpoint-GUID: xxl7lSxgZXADjYx9DqNgzXnRifAw-xZq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 impostorscore=0 phishscore=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah is an open-source Type-1 hypervisor developed by Qualcomm. It does not depend on any lower-privileged OS/kernel code for its core functionality. This increases its security and can support a smaller trusted computing based when compared to Type-2 hypervisors. Add documentation describing the Gunyah hypervisor and the main components of the Gunyah hypervisor which are of interest to Linux virtualization development. Reviewed-by: Bagas Sanjaya Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/index.rst | 113 ++++++++++++++++++++ Documentation/virt/gunyah/message-queue.rst | 63 +++++++++++ Documentation/virt/index.rst | 1 + 3 files changed, 177 insertions(+) create mode 100644 Documentation/virt/gunyah/index.rst create mode 100644 Documentation/virt/gunyah/message-queue.rst diff --git a/Documentation/virt/gunyah/index.rst b/Documentation/virt/gunya= h/index.rst new file mode 100644 index 000000000000..74aa345e0a14 --- /dev/null +++ b/Documentation/virt/gunyah/index.rst @@ -0,0 +1,113 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Gunyah Hypervisor +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. toctree:: + :maxdepth: 1 + + message-queue + +Gunyah is a Type-1 hypervisor which is independent of any OS kernel, and r= uns in +a higher CPU privilege level. It does not depend on any lower-privileged o= perating system +for its core functionality. This increases its security and can support a = much smaller +trusted computing base than a Type-2 hypervisor. + +Gunyah is an open source hypervisor. The source repo is available at +https://github.com/quic/gunyah-hypervisor. + +Gunyah provides these following features. + +- Scheduling: + + A scheduler for virtual CPUs (vCPUs) on physical CPUs enables time-shari= ng + of the CPUs. Gunyah supports two models of scheduling: + + 1. "Behind the back" scheduling in which Gunyah hypervisor schedules v= CPUS on its own. + 2. "Proxy" scheduling in which a delegated VM can donate part of one o= f its vCPU slice + to another VM's vCPU via a hypercall. + +- Memory Management: + + APIs handling memory, abstracted as objects, limiting direct use of phys= ical + addresses. Memory ownership and usage tracking of all memory under its c= ontrol. + Memory partitioning between VMs is a fundamental security feature. + +- Interrupt Virtualization: + + Uses CPU hardware interrupt virtualization capabilities. Interrupts are = handled + in the hypervisor and routed to the assigned VM. + +- Inter-VM Communication: + + There are several different mechanisms provided for communicating betwee= n VMs. + +- Virtual platform: + + Architectural devices such as interrupt controllers and CPU timers are d= irectly provided + by the hypervisor as well as core virtual platform devices and system AP= Is such as ARM PSCI. + +- Device Virtualization: + + Para-virtualization of devices is supported using inter-VM communication. + +Architectures supported +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +AArch64 with a GIC + +Resources and Capabilities +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +Some services or resources provided by the Gunyah hypervisor are described= to a virtual machine by +capability IDs. For instance, inter-VM communication is performed with doo= rbells and message queues. +Gunyah allows access to manipulate that doorbell via the capability ID. Th= ese resources are +described in Linux as a struct gh_resource. + +High level management of these resources is performed by the resource mana= ger VM. RM informs a +guest VM about resources it can access through either the device tree or v= ia guest-initiated RPC. + +For each virtual machine, Gunyah maintains a table of resources which can = be accessed by that VM. +An entry in this table is called a "capability" and VMs can only access re= sources via this +capability table. Hence, virtual Gunyah resources are referenced by a "cap= ability IDs" and not +"resource IDs". If 2 VMs have access to the same resource, they might not = be using the same +capability ID to access that resource since the capability tables are inde= pendent per VM. + +Resource Manager +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The resource manager (RM) is a privileged application VM supporting the Gu= nyah Hypervisor. +It provides policy enforcement aspects of the virtualization system. The r= esource manager can +be treated as an extension of the Hypervisor but is separated to its own p= artition to ensure +that the hypervisor layer itself remains small and secure and to maintain = a separation of policy +and mechanism in the platform. RM runs at arm64 NS-EL1 similar to other vi= rtual machines. + +Communication with the resource manager from each guest VM happens with me= ssage-queue.rst. Details +about the specific messages can be found in drivers/virt/gunyah/rsc_mgr.c + +:: + + +-------+ +--------+ +--------+ + | RM | | VM_A | | VM_B | + +-.-.-.-+ +---.----+ +---.----+ + | | | | + +-.-.-----------.------------.----+ + | | \=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D/ | | + | \=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D/ | + | Gunyah | + +---------------------------------+ + +The source for the resource manager is available at https://github.com/qui= c/gunyah-resource-manager. + +The resource manager provides the following features: + +- VM lifecycle management: allocating a VM, starting VMs, destruction of V= Ms +- VM access control policy, including memory sharing and lending +- Interrupt routing configuration +- Forwarding of system-level events (e.g. VM shutdown) to owner VM + +When booting a virtual machine which uses a devicetree such as Linux, reso= urce manager overlays a +/hypervisor node. This node can let Linux know it is running as a Gunyah g= uest VM, +how to communicate with resource manager, and basic description and capabi= lities of +this VM. See Documentation/devicetree/bindings/firmware/gunyah-hypervisor.= yaml for a description +of this node. diff --git a/Documentation/virt/gunyah/message-queue.rst b/Documentation/vi= rt/gunyah/message-queue.rst new file mode 100644 index 000000000000..b352918ae54b --- /dev/null +++ b/Documentation/virt/gunyah/message-queue.rst @@ -0,0 +1,63 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Message Queues +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Message queue is a simple low-capacity IPC channel between two VMs. It is +intended for sending small control and configuration messages. Each message +queue is unidirectional, so a full-duplex IPC channel requires a pair of q= ueues. + +Messages can be up to 240 bytes in length. Longer messages require a furth= er +protocol on top of the message queue messages themselves. For instance, co= mmunication +with the resource manager adds a header field for sending longer messages = via multiple +message fragments. + +The diagram below shows how message queue works. A typical configuration i= nvolves +2 message queues. Message queue 1 allows VM_A to send messages to VM_B. Me= ssage +queue 2 allows VM_B to send messages to VM_A. + +1. VM_A sends a message of up to 240 bytes in length. It raises a hypercall + with the message to inform the hypervisor to add the message to + message queue 1's queue. The hypervisor copies memory into the internal + message queue representation; the memory doesn't need to be shared betw= een + VM_A and VM_B. + +2. Gunyah raises the corresponding interrupt for VM_B (Rx vIRQ) when any of + these happens: + + a. gh_msgq_send() has PUSH flag. Queue is immediately flushed. This is = the typical case. + b. Explicility with gh_msgq_push command from VM_A. + c. Message queue has reached a threshold depth. + +3. VM_B calls gh_msgq_recv() and Gunyah copies message to requested buffer. + +4. Gunyah buffers messages in the queue. If the queue became full when VM_= A added a message, + the return values for gh_msgq_send() include a flag that indicates the = queue is full. + Once VM_B receives the message and, thus, there is space in the queue, = Gunyah + will raise the Tx vIRQ on VM_A to indicate it can continue sending mess= ages. + +For VM_B to send a message to VM_A, the process is identical, except that = hypercalls +reference message queue 2's capability ID. Each message queue has its own = independent +vIRQ: two TX message queues will have two vIRQs (and two capability IDs). + +:: + + +---------------+ +-----------------+ +-------------= --+ + | VM_A | |Gunyah hypervisor| | VM_B = | + | | | | | = | + | | | | | = | + | | Tx | | | = | + | |-------->| | Rx vIRQ | = | + |gh_msgq_send() | Tx vIRQ |Message queue 1 |-------->|gh_msgq_recv(= ) | + | |<------- | | | = | + | | | | | = | + | Message Queue | | | | Message Queu= e | + | driver | | | | driver = | + | | | | | = | + | | | | | = | + | | | | Tx | = | + | | Rx vIRQ | |<--------| = | + |gh_msgq_recv() |<--------|Message queue 2 | Tx vIRQ |gh_msgq_send(= ) | + | | | |-------->| = | + | | | | | = | + | | | | | = | + +---------------+ +-----------------+ +-------------= --+ diff --git a/Documentation/virt/index.rst b/Documentation/virt/index.rst index 7fb55ae08598..15869ee059b3 100644 --- a/Documentation/virt/index.rst +++ b/Documentation/virt/index.rst @@ -16,6 +16,7 @@ Virtualization Support coco/sev-guest coco/tdx-guest hyperv/index + gunyah/index =20 .. only:: html and subproject =20 --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DAF2C6FA99 for ; Sat, 4 Mar 2023 01:07:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229812AbjCDBHK (ORCPT ); Fri, 3 Mar 2023 20:07:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229596AbjCDBHE (ORCPT ); Fri, 3 Mar 2023 20:07:04 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9AB161519; Fri, 3 Mar 2023 17:07:02 -0800 (PST) Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240hP7Q014377; Sat, 4 Mar 2023 01:06:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=lBgQ8sQd2ooWTyKgD+qhk0eX+e4plIATV17Lqcn5aqk=; b=dTZvV3FpctO2eCIguz4nqGEKuFNLz75gAuLhcESZv6ZQWZPAesNDQLGt19PBMtCBE65d qsHac/3AES2K5DTmUAhkdNcKSHEsK6hYusJemnuhTQiU3ByaasBDwb9PFI4vEg9C38PI JhY3dLMs/lAH8FerTs3vkED4w2Xo6YdW365ay3I1O+AS5DZtPiwT4j6gbKig5ylqn0bF yF+aV4uhjW3lrvgdF3VgLBK6AI5kKn8cNjDiNbeGCfQKg2q3xoKf3lRZXeH4vivpjdwK Wj+SqtPt72EBwexJSI5ct4Cj8gxvPBr7EdcsSI9BLK5VkBB2PxM0zFOACVmw5sCTbTv1 Dg== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3rse8by9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:48 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416lmW025739 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:47 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.986.41; Fri, 3 Mar 2023 17:06:46 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Rob Herring , Krzysztof Kozlowski CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Jonathan Corbet , Bagas Sanjaya , Will Deacon , "Andy Gross" , Catalin Marinas , "Jassi Brar" , , , , , , "Rob Herring" Subject: [PATCH v11 02/26] dt-bindings: Add binding for gunyah hypervisor Date: Fri, 3 Mar 2023 17:06:08 -0800 Message-ID: <20230304010632.2127470-3-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: 01EecaefkqRu4PEyg--kavSIItjnS7NB X-Proofpoint-ORIG-GUID: 01EecaefkqRu4PEyg--kavSIItjnS7NB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 malwarescore=0 suspectscore=0 priorityscore=1501 impostorscore=0 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When Linux is booted as a guest under the Gunyah hypervisor, the Gunyah Resource Manager applies a devicetree overlay describing the virtual platform configuration of the guest VM, such as the message queue capability IDs for communicating with the Resource Manager. This information is not otherwise discoverable by a VM: the Gunyah hypervisor core does not provide a direct interface to discover capability IDs nor a way to communicate with RM without having already known the corresponding message queue capability ID. Add the DT bindings that Gunyah adheres for the hypervisor node and message queues. Reviewed-by: Rob Herring Signed-off-by: Elliot Berman --- .../bindings/firmware/gunyah-hypervisor.yaml | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Documentation/devicetree/bindings/firmware/gunyah-hyper= visor.yaml diff --git a/Documentation/devicetree/bindings/firmware/gunyah-hypervisor.y= aml b/Documentation/devicetree/bindings/firmware/gunyah-hypervisor.yaml new file mode 100644 index 000000000000..3fc0b043ac3c --- /dev/null +++ b/Documentation/devicetree/bindings/firmware/gunyah-hypervisor.yaml @@ -0,0 +1,82 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/firmware/gunyah-hypervisor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Gunyah Hypervisor + +maintainers: + - Prakruthi Deepak Heragu + - Elliot Berman + +description: |+ + Gunyah virtual machines use this information to determine the capability= IDs + of the message queues used to communicate with the Gunyah Resource Manag= er. + See also: https://github.com/quic/gunyah-resource-manager/blob/develop/s= rc/vm_creation/dto_construct.c + +properties: + compatible: + const: gunyah-hypervisor + + "#address-cells": + description: Number of cells needed to represent 64-bit capability IDs. + const: 2 + + "#size-cells": + description: must be 0, because capability IDs are not memory address + ranges and do not have a size. + const: 0 + +patternProperties: + "^gunyah-resource-mgr(@.*)?": + type: object + description: + Resource Manager node which is required to communicate to Resource + Manager VM using Gunyah Message Queues. + + properties: + compatible: + const: gunyah-resource-manager + + reg: + items: + - description: Gunyah capability ID of the TX message queue + - description: Gunyah capability ID of the RX message queue + + interrupts: + items: + - description: Interrupt for the TX message queue + - description: Interrupt for the RX message queue + + additionalProperties: false + + required: + - compatible + - reg + - interrupts + +additionalProperties: false + +required: + - compatible + - "#address-cells" + - "#size-cells" + +examples: + - | + #include + + hypervisor { + #address-cells =3D <2>; + #size-cells =3D <0>; + compatible =3D "gunyah-hypervisor"; + + gunyah-resource-mgr@0 { + compatible =3D "gunyah-resource-manager"; + interrupts =3D , /* TX full IR= Q */ + ; /* RX empty IRQ= */ + reg =3D <0x00000000 0x00000000>, <0x00000000 0x00000001>; + /* TX, RX cap ids */ + }; + }; --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19A6AC61DA3 for ; Sat, 4 Mar 2023 01:07:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229836AbjCDBHW (ORCPT ); Fri, 3 Mar 2023 20:07:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229661AbjCDBHG (ORCPT ); Fri, 3 Mar 2023 20:07:06 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1713F60D7F; Fri, 3 Mar 2023 17:07:05 -0800 (PST) Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32400LVG003536; Sat, 4 Mar 2023 01:06:49 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=jEyk4HliMxKi4wE8CsySZn3iDFHDSO6CVbaXJWg6y1Y=; b=LhklqIu95JDioJYSYlEdh5A+06GuAWOC+YwCHMoTddsFxGk8NCKPxEJbsBex/NuK7xcm ZSjIi46OPCkIxqTDabDagYlTlHqboQcP3oiwsGdqJzDp+usGu8zyiRSO+do6sQYJkWAd ktZuVxAEn5edX2J8q63ZWu4GQsQHbtpsDJYBr1srOXEnQ7oUY4E9VogvyRsLkKl7HyIr 9B/Dz5K6FYJNbECMJ+AODL+oIQl1aBu4Cd1OwmPj7XTnWXe14bQH0Ch7t5IUs/MS4svz uD8yMruyq6e42/TRABxgbr0LjTH06dR5ulpdzY6GyMUpiGnP3j1rS6Ax89r2MhX2++g8 5Q== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3dfrjf3r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:48 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416mvx018575 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:48 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.986.41; Fri, 3 Mar 2023 17:06:47 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 03/26] gunyah: Common types and error codes for Gunyah hypercalls Date: Fri, 3 Mar 2023 17:06:09 -0800 Message-ID: <20230304010632.2127470-4-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: j_boKn3Ets6Sh33sceCLbPTuf9hMuIC6 X-Proofpoint-ORIG-GUID: j_boKn3Ets6Sh33sceCLbPTuf9hMuIC6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 phishscore=0 adultscore=0 impostorscore=0 bulkscore=0 mlxlogscore=719 clxscore=1015 priorityscore=1501 malwarescore=0 lowpriorityscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add architecture-independent standard error codes, types, and macros for Gunyah hypercalls. Reviewed-by: Dmitry Baryshkov Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- include/linux/gunyah.h | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/linux/gunyah.h diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h new file mode 100644 index 000000000000..54b4be71caf7 --- /dev/null +++ b/include/linux/gunyah.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#ifndef _LINUX_GUNYAH_H +#define _LINUX_GUNYAH_H + +#include +#include + +/*************************************************************************= *****/ +/* Common arch-independent definitions for Gunyah hypercalls = */ +#define GH_CAPID_INVAL U64_MAX +#define GH_VMID_ROOT_VM 0xff + +enum gh_error { + GH_ERROR_OK =3D 0, + GH_ERROR_UNIMPLEMENTED =3D -1, + GH_ERROR_RETRY =3D -2, + + GH_ERROR_ARG_INVAL =3D 1, + GH_ERROR_ARG_SIZE =3D 2, + GH_ERROR_ARG_ALIGN =3D 3, + + GH_ERROR_NOMEM =3D 10, + + GH_ERROR_ADDR_OVFL =3D 20, + GH_ERROR_ADDR_UNFL =3D 21, + GH_ERROR_ADDR_INVAL =3D 22, + + GH_ERROR_DENIED =3D 30, + GH_ERROR_BUSY =3D 31, + GH_ERROR_IDLE =3D 32, + + GH_ERROR_IRQ_BOUND =3D 40, + GH_ERROR_IRQ_UNBOUND =3D 41, + + GH_ERROR_CSPACE_CAP_NULL =3D 50, + GH_ERROR_CSPACE_CAP_REVOKED =3D 51, + GH_ERROR_CSPACE_WRONG_OBJ_TYPE =3D 52, + GH_ERROR_CSPACE_INSUF_RIGHTS =3D 53, + GH_ERROR_CSPACE_FULL =3D 54, + + GH_ERROR_MSGQUEUE_EMPTY =3D 60, + GH_ERROR_MSGQUEUE_FULL =3D 61, +}; + +/** + * gh_remap_error() - Remap Gunyah hypervisor errors into a Linux error co= de + * @gh_error: Gunyah hypercall return value + */ +static inline int gh_remap_error(enum gh_error gh_error) +{ + switch (gh_error) { + case GH_ERROR_OK: + return 0; + case GH_ERROR_NOMEM: + return -ENOMEM; + case GH_ERROR_DENIED: + case GH_ERROR_CSPACE_CAP_NULL: + case GH_ERROR_CSPACE_CAP_REVOKED: + case GH_ERROR_CSPACE_WRONG_OBJ_TYPE: + case GH_ERROR_CSPACE_INSUF_RIGHTS: + case GH_ERROR_CSPACE_FULL: + return -EACCES; + case GH_ERROR_BUSY: + case GH_ERROR_IDLE: + return -EBUSY; + case GH_ERROR_IRQ_BOUND: + case GH_ERROR_IRQ_UNBOUND: + case GH_ERROR_MSGQUEUE_FULL: + case GH_ERROR_MSGQUEUE_EMPTY: + return -EIO; + case GH_ERROR_UNIMPLEMENTED: + case GH_ERROR_RETRY: + return -EOPNOTSUPP; + default: + return -EINVAL; + } +} + +#endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C6BBC61DA3 for ; Sat, 4 Mar 2023 01:07:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229624AbjCDBHZ (ORCPT ); Fri, 3 Mar 2023 20:07:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229726AbjCDBHG (ORCPT ); Fri, 3 Mar 2023 20:07:06 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 248D461519; Fri, 3 Mar 2023 17:07:05 -0800 (PST) Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323NdMls007258; Sat, 4 Mar 2023 01:06:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=o2JDe5b6Wiz4S6AA16kU7uhlG3xm9wXNfhPV1JlcWH4=; b=bmd5R0sDPIClK5xNO5hGlwhM3IDrvWDhi13chPeqw1EBB3As0eH5LFz0cjgg7gXXDRAs nfryOLM2HzNcUU9KjwyI6/w8Vk8pIv+NGP/nhvGm/y2JbI0KJyIEdEgagGpC9BaiaGn+ 9zigYiLQPr+KLZvPSZ65Fwwqt6kZHADTaUTnmhSzzF8olSYFFK3/Jsigpe/ICMMzlY4u 7ou8mE5k8+rtjKp60003gOSmlf863AG6/aokJfE4C03W0AfHVBEgkTxmVWV3gFLTolaX JlQlWx7LoL1CI1niHFwip2H356GS/8yNvwHV6yqxMqet60+q8bVoFlURavjYoZGHN8wp nA== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3pu48m9y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:50 +0000 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 32416n5n019152 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:49 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.986.41; Fri, 3 Mar 2023 17:06:48 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Catalin Marinas , Will Deacon , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Andy Gross , Jassi Brar , , , , , Subject: [PATCH v11 04/26] virt: gunyah: Add hypercalls to identify Gunyah Date: Fri, 3 Mar 2023 17:06:10 -0800 Message-ID: <20230304010632.2127470-5-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: Mxvi9m7JJDyAC_3IHXowe5bRvfzbRTYc X-Proofpoint-ORIG-GUID: Mxvi9m7JJDyAC_3IHXowe5bRvfzbRTYc X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 priorityscore=1501 suspectscore=0 phishscore=0 malwarescore=0 bulkscore=0 spamscore=0 impostorscore=0 mlxscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add hypercalls to identify when Linux is running a virtual machine under Gunyah. There are two calls to help identify Gunyah: 1. gh_hypercall_get_uid() returns a UID when running under a Gunyah hypervisor. 2. gh_hypercall_hyp_identify() returns build information and a set of feature flags that are supported by Gunyah. Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- arch/arm64/Kbuild | 1 + arch/arm64/gunyah/Makefile | 3 ++ arch/arm64/gunyah/gunyah_hypercall.c | 64 ++++++++++++++++++++++++++++ drivers/virt/Kconfig | 2 + drivers/virt/gunyah/Kconfig | 13 ++++++ include/linux/gunyah.h | 28 ++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 arch/arm64/gunyah/Makefile create mode 100644 arch/arm64/gunyah/gunyah_hypercall.c create mode 100644 drivers/virt/gunyah/Kconfig diff --git a/arch/arm64/Kbuild b/arch/arm64/Kbuild index 5bfbf7d79c99..e4847ba0e3c9 100644 --- a/arch/arm64/Kbuild +++ b/arch/arm64/Kbuild @@ -3,6 +3,7 @@ obj-y +=3D kernel/ mm/ net/ obj-$(CONFIG_KVM) +=3D kvm/ obj-$(CONFIG_XEN) +=3D xen/ obj-$(subst m,y,$(CONFIG_HYPERV)) +=3D hyperv/ +obj-$(CONFIG_GUNYAH) +=3D gunyah/ obj-$(CONFIG_CRYPTO) +=3D crypto/ =20 # for cleaning diff --git a/arch/arm64/gunyah/Makefile b/arch/arm64/gunyah/Makefile new file mode 100644 index 000000000000..84f1e38cafb1 --- /dev/null +++ b/arch/arm64/gunyah/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_GUNYAH) +=3D gunyah_hypercall.o diff --git a/arch/arm64/gunyah/gunyah_hypercall.c b/arch/arm64/gunyah/gunya= h_hypercall.c new file mode 100644 index 000000000000..0d14e767e2c8 --- /dev/null +++ b/arch/arm64/gunyah/gunyah_hypercall.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include + +static const uuid_t gh_known_uuids[] =3D { + /* Qualcomm's version of Gunyah {19bd54bd-0b37-571b-946f-609b54539de6} */ + UUID_INIT(0x19bd54bd, 0x0b37, 0x571b, 0x94, 0x6f, 0x60, 0x9b, 0x54, 0x53,= 0x9d, 0xe6), + /* Standard version of Gunyah {c1d58fcd-a453-5fdb-9265-ce36673d5f14} */ + UUID_INIT(0xc1d58fcd, 0xa453, 0x5fdb, 0x92, 0x65, 0xce, 0x36, 0x67, 0x3d,= 0x5f, 0x14), +}; + +bool arch_is_gh_guest(void) +{ + struct arm_smccc_res res; + uuid_t uuid; + int i; + + arm_smccc_1_1_hvc(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res); + + ((u32 *)&uuid.b[0])[0] =3D lower_32_bits(res.a0); + ((u32 *)&uuid.b[0])[1] =3D lower_32_bits(res.a1); + ((u32 *)&uuid.b[0])[2] =3D lower_32_bits(res.a2); + ((u32 *)&uuid.b[0])[3] =3D lower_32_bits(res.a3); + + for (i =3D 0; i < ARRAY_SIZE(gh_known_uuids); i++) + if (uuid_equal(&uuid, &gh_known_uuids[i])) + return true; + + return false; +} +EXPORT_SYMBOL_GPL(arch_is_gh_guest); + +#define GH_HYPERCALL(fn) ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC= _SMC_64, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + fn) + +#define GH_HYPERCALL_HYP_IDENTIFY GH_HYPERCALL(0x8000) + +/** + * gh_hypercall_hyp_identify() - Returns build information and feature fla= gs + * supported by Gunyah. + * @hyp_identity: filled by the hypercall with the API info and feature fl= ags. + */ +void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_identify_resp *hyp_= identity) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_hvc(GH_HYPERCALL_HYP_IDENTIFY, &res); + + hyp_identity->api_info =3D res.a0; + hyp_identity->flags[0] =3D res.a1; + hyp_identity->flags[1] =3D res.a2; + hyp_identity->flags[2] =3D res.a3; +} +EXPORT_SYMBOL_GPL(gh_hypercall_hyp_identify); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Gunyah Hypervisor Hypercalls"); diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig index f79ab13a5c28..85bd6626ffc9 100644 --- a/drivers/virt/Kconfig +++ b/drivers/virt/Kconfig @@ -54,4 +54,6 @@ source "drivers/virt/coco/sev-guest/Kconfig" =20 source "drivers/virt/coco/tdx-guest/Kconfig" =20 +source "drivers/virt/gunyah/Kconfig" + endif diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig new file mode 100644 index 000000000000..1a737694c333 --- /dev/null +++ b/drivers/virt/gunyah/Kconfig @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config GUNYAH + tristate "Gunyah Virtualization drivers" + depends on ARM64 + depends on MAILBOX + help + The Gunyah drivers are the helper interfaces that run in a guest VM + such as basic inter-VM IPC and signaling mechanisms, and higher level + services such as memory/device sharing, IRQ sharing, and so on. + + Say Y/M here to enable the drivers needed to interact in a Gunyah + virtual environment. diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 54b4be71caf7..bd080e3a6fc9 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -6,8 +6,10 @@ #ifndef _LINUX_GUNYAH_H #define _LINUX_GUNYAH_H =20 +#include #include #include +#include =20 /*************************************************************************= *****/ /* Common arch-independent definitions for Gunyah hypercalls = */ @@ -80,4 +82,30 @@ static inline int gh_remap_error(enum gh_error gh_error) } } =20 +enum gh_api_feature { + GH_FEATURE_DOORBELL =3D 1, + GH_FEATURE_MSGQUEUE =3D 2, + GH_FEATURE_VCPU =3D 5, + GH_FEATURE_MEMEXTENT =3D 6, +}; + +bool arch_is_gh_guest(void); + +u16 gh_api_version(void); +bool gh_api_has_feature(enum gh_api_feature feature); + +#define GH_API_V1 1 + +#define GH_API_INFO_API_VERSION_MASK GENMASK_ULL(13, 0) +#define GH_API_INFO_BIG_ENDIAN BIT_ULL(14) +#define GH_API_INFO_IS_64BIT BIT_ULL(15) +#define GH_API_INFO_VARIANT_MASK GENMASK_ULL(63, 56) + +struct gh_hypercall_hyp_identify_resp { + u64 api_info; + u64 flags[3]; +}; + +void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_identify_resp *hyp_= identity); + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1ED36C61DA3 for ; Sat, 4 Mar 2023 01:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229612AbjCDBHO (ORCPT ); Fri, 3 Mar 2023 20:07:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229614AbjCDBHF (ORCPT ); Fri, 3 Mar 2023 20:07:05 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79EBC60D71; Fri, 3 Mar 2023 17:07:04 -0800 (PST) Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323LUSmM024354; Sat, 4 Mar 2023 01:06:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=c+R7Cm2h9YH+WF+IGm5QH89rs8g8xWEdt60LkDWmLUY=; b=RTYYnWXMQdpSdp2TJL09Jpcz5BgY+a1j30FLL4KKMVBZk9IOF26t1OornNM9vExCtekC XzFm31zjOGzFcnk1OnhaQN/cU6tCaob6FNFvWRBQCY05SAjosE/u0f3DXiWz8R5lmf1Z KzqBW27aURXgWF6+YF1OnL/1veqGxCsqxU5QVok0XYRIQCTPcmEsRTEmQcHg+ISsriQB SQsoqQJGkb2FXQfRzXMzp3MONOCGWwjWGqqlgJ2XK9hs0cO3/y48so82gnaeKu4f0CXV 9BTPHeyBx/2JMPar0VU73q1ZIPwf8W8K0OTFWiUnLhRZIIAflgjG2Jf+jlAP9NSEI5Gz Yw== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3c8htrbd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:52 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416pYk031659 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:51 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.986.41; Fri, 3 Mar 2023 17:06:50 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 05/26] virt: gunyah: Identify hypervisor version Date: Fri, 3 Mar 2023 17:06:11 -0800 Message-ID: <20230304010632.2127470-6-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: dwX26VFNCPBxoep2hDYNa8iXyxQZpPOD X-Proofpoint-ORIG-GUID: dwX26VFNCPBxoep2hDYNa8iXyxQZpPOD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 phishscore=0 malwarescore=0 mlxlogscore=999 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 suspectscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Export the version of Gunyah which is reported via the hyp_identify hypercall. Increments of the major API version indicate possibly backwards incompatible changes. Export the hypervisor identity so that Gunyah drivers can act according to the major API version. Signed-off-by: Elliot Berman Reviewed-by: Alex Elder Reviewed-by: Srinivas Kandagatla --- drivers/virt/Makefile | 1 + drivers/virt/gunyah/Makefile | 3 ++ drivers/virt/gunyah/gunyah.c | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 drivers/virt/gunyah/Makefile create mode 100644 drivers/virt/gunyah/gunyah.c diff --git a/drivers/virt/Makefile b/drivers/virt/Makefile index e9aa6fc96fab..a5817e2d7d71 100644 --- a/drivers/virt/Makefile +++ b/drivers/virt/Makefile @@ -12,3 +12,4 @@ obj-$(CONFIG_ACRN_HSM) +=3D acrn/ obj-$(CONFIG_EFI_SECRET) +=3D coco/efi_secret/ obj-$(CONFIG_SEV_GUEST) +=3D coco/sev-guest/ obj-$(CONFIG_INTEL_TDX_GUEST) +=3D coco/tdx-guest/ +obj-y +=3D gunyah/ diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile new file mode 100644 index 000000000000..34f32110faf9 --- /dev/null +++ b/drivers/virt/gunyah/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_GUNYAH) +=3D gunyah.o diff --git a/drivers/virt/gunyah/gunyah.c b/drivers/virt/gunyah/gunyah.c new file mode 100644 index 000000000000..4b7e6f3edaff --- /dev/null +++ b/drivers/virt/gunyah/gunyah.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#define pr_fmt(fmt) "gunyah: " fmt + +#include +#include +#include +#include + +static struct gh_hypercall_hyp_identify_resp gh_api; + +u16 gh_api_version(void) +{ + return FIELD_GET(GH_API_INFO_API_VERSION_MASK, gh_api.api_info); +} +EXPORT_SYMBOL_GPL(gh_api_version); + +bool gh_api_has_feature(enum gh_api_feature feature) +{ + switch (feature) { + case GH_FEATURE_DOORBELL: + case GH_FEATURE_MSGQUEUE: + case GH_FEATURE_VCPU: + case GH_FEATURE_MEMEXTENT: + return !!(gh_api.flags[0] & BIT_ULL(feature)); + default: + return false; + } +} +EXPORT_SYMBOL_GPL(gh_api_has_feature); + +static int __init gh_init(void) +{ + if (!arch_is_gh_guest()) + return -ENODEV; + + gh_hypercall_hyp_identify(&gh_api); + + pr_info("Running under Gunyah hypervisor %llx/v%u\n", + FIELD_GET(GH_API_INFO_VARIANT_MASK, gh_api.api_info), + gh_api_version()); + + /* We might move this out to individual drivers if there's ever an API ve= rsion bump */ + if (gh_api_version() !=3D GH_API_V1) { + pr_info("Unsupported Gunyah version: %u\n", gh_api_version()); + return -ENODEV; + } + + return 0; +} +arch_initcall(gh_init); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Gunyah Hypervisor Driver"); --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B716DC61DA4 for ; Sat, 4 Mar 2023 01:07:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229502AbjCDBH2 (ORCPT ); Fri, 3 Mar 2023 20:07:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229723AbjCDBHG (ORCPT ); Fri, 3 Mar 2023 20:07:06 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C79A760D71; Fri, 3 Mar 2023 17:07:05 -0800 (PST) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240NDCo010557; Sat, 4 Mar 2023 01:06:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=YsGuT96YuV6TmpZAGW8EVXUgQ910oBDQs5Ss5XxgyFk=; b=QccQw1y9PYbK9t9AXUCSaMzx9hY/iKnowqV8QwKTp6jMQ1ROXiSeExx4RlWqg6TpfKGl LZCM5Qu+M0w9euY4AXJWFgLnszE71Vlqc1PUh1pf21wKSr2ALdcRxjacKagpO/TPD+Jr YW1dFVSFGxcdoP4+1EUqfwLh3wdHbFxX58ofVR7HH2aF4co+zxPT8x9TDs/1tgCMwNGN IiQ7VAod78Dqo6ohBF4q1KxcQtF1bwl5RvfTxw6oSz612aTxRUXKQtfJtjy2FphK56Sk 0bM2SrFF+8w3iAIpNZAmv2fmd9IaxHiDyzodGjDMaBQ+2AgG0NRiNE3L883310l9nqIq 7g== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3ph4rpde-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:53 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416qbp018614 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:52 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.986.41; Fri, 3 Mar 2023 17:06:51 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Catalin Marinas , Will Deacon CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Andy Gross , Jassi Brar , , , , , Subject: [PATCH v11 06/26] virt: gunyah: msgq: Add hypercalls to send and receive messages Date: Fri, 3 Mar 2023 17:06:12 -0800 Message-ID: <20230304010632.2127470-7-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: 19W5F3kn0fDshEJNBOuMQ_lADrjVT29I X-Proofpoint-GUID: 19W5F3kn0fDshEJNBOuMQ_lADrjVT29I X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 suspectscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add hypercalls to send and receive messages on a Gunyah message queue. Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- arch/arm64/gunyah/gunyah_hypercall.c | 31 ++++++++++++++++++++++++++++ include/linux/gunyah.h | 6 ++++++ 2 files changed, 37 insertions(+) diff --git a/arch/arm64/gunyah/gunyah_hypercall.c b/arch/arm64/gunyah/gunya= h_hypercall.c index 0d14e767e2c8..3420d8f286a9 100644 --- a/arch/arm64/gunyah/gunyah_hypercall.c +++ b/arch/arm64/gunyah/gunyah_hypercall.c @@ -41,6 +41,8 @@ EXPORT_SYMBOL_GPL(arch_is_gh_guest); fn) =20 #define GH_HYPERCALL_HYP_IDENTIFY GH_HYPERCALL(0x8000) +#define GH_HYPERCALL_MSGQ_SEND GH_HYPERCALL(0x801B) +#define GH_HYPERCALL_MSGQ_RECV GH_HYPERCALL(0x801C) =20 /** * gh_hypercall_hyp_identify() - Returns build information and feature fla= gs @@ -60,5 +62,34 @@ void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_i= dentify_resp *hyp_identi } EXPORT_SYMBOL_GPL(gh_hypercall_hyp_identify); =20 +enum gh_error gh_hypercall_msgq_send(u64 capid, size_t size, void *buff, i= nt tx_flags, bool *ready) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_hvc(GH_HYPERCALL_MSGQ_SEND, capid, size, (uintptr_t)buff, t= x_flags, 0, &res); + + if (res.a0 =3D=3D GH_ERROR_OK) + *ready =3D !!res.a1; + + return res.a0; +} +EXPORT_SYMBOL_GPL(gh_hypercall_msgq_send); + +enum gh_error gh_hypercall_msgq_recv(u64 capid, void *buff, size_t size, s= ize_t *recv_size, + bool *ready) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_hvc(GH_HYPERCALL_MSGQ_RECV, capid, (uintptr_t)buff, size, 0= , &res); + + if (res.a0 =3D=3D GH_ERROR_OK) { + *recv_size =3D res.a1; + *ready =3D !!res.a2; + } + + return res.a0; +} +EXPORT_SYMBOL_GPL(gh_hypercall_msgq_recv); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Gunyah Hypervisor Hypercalls"); diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index bd080e3a6fc9..18cfbf5ee48b 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -108,4 +108,10 @@ struct gh_hypercall_hyp_identify_resp { =20 void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_identify_resp *hyp_= identity); =20 +#define GH_HYPERCALL_MSGQ_TX_FLAGS_PUSH BIT(0) + +enum gh_error gh_hypercall_msgq_send(u64 capid, size_t size, void *buff, i= nt tx_flags, bool *ready); +enum gh_error gh_hypercall_msgq_recv(u64 capid, void *buff, size_t size, s= ize_t *recv_size, + bool *ready); + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBB65C77B62 for ; Sat, 4 Mar 2023 01:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229994AbjCDBIL (ORCPT ); Fri, 3 Mar 2023 20:08:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229773AbjCDBHI (ORCPT ); Fri, 3 Mar 2023 20:07:08 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1645164845; Fri, 3 Mar 2023 17:07:07 -0800 (PST) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323LnDpO001386; Sat, 4 Mar 2023 01:06:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=v5WxD+pXNX8A+Tb8ukT62l45HFswGYt1bFwdiMWNRJ4=; b=CIXofuuNR5GFOWB4Rgz7KT0QAYrNWcXAhBj1o7Fg0ng78tUnZmbyO31DePHHRCYFmcCr rbideDlOW8L4tYZqoHAcFHoS0MHk8ZAnjGjVQywACQ/FRMEaEV0FF6IBB9u2NY8EYm1v 7US/VprIAUNMwCuRHKXpN/YIkvuosl+v260r3xbTAyq+yd7WLY6Ifsj+vtdUdCJJtL+t sBkRgNKW1v7FgwGW81JsY7XiZkSfHbjXsf4AugnuuDYQ8YgKm5BkYfvMnzAkx2VI/BCs 8sLUN/Twc2kZxlSLVRlgBaynwqCNNrF5Bs1gNgTAMmFkXUEVnSHnTxIOOEIIZDwZbJ5x dQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3ph4rpdf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:54 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416rK6028274 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:53 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.986.41; Fri, 3 Mar 2023 17:06:53 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet , Jassi Brar CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , , , , , Subject: [PATCH v11 07/26] mailbox: Add Gunyah message queue mailbox Date: Fri, 3 Mar 2023 17:06:13 -0800 Message-ID: <20230304010632.2127470-8-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: sRJNgBc4IS8v8KtpOpvsyV11yUvDhvGU X-Proofpoint-GUID: sRJNgBc4IS8v8KtpOpvsyV11yUvDhvGU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 suspectscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 mlxlogscore=363 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah message queues are a unidirectional inter-VM pipe for messages up to 1024 bytes. This driver supports pairing a receiver message queue and a transmitter message queue to expose a single mailbox channel. Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- Documentation/virt/gunyah/message-queue.rst | 8 + drivers/mailbox/Makefile | 2 + drivers/mailbox/gunyah-msgq.c | 209 ++++++++++++++++++++ include/linux/gunyah.h | 57 ++++++ 4 files changed, 276 insertions(+) create mode 100644 drivers/mailbox/gunyah-msgq.c diff --git a/Documentation/virt/gunyah/message-queue.rst b/Documentation/vi= rt/gunyah/message-queue.rst index b352918ae54b..70d82a4ef32d 100644 --- a/Documentation/virt/gunyah/message-queue.rst +++ b/Documentation/virt/gunyah/message-queue.rst @@ -61,3 +61,11 @@ vIRQ: two TX message queues will have two vIRQs (and two= capability IDs). | | | | | = | | | | | | = | +---------------+ +-----------------+ +-------------= --+ + +Gunyah message queues are exposed as mailboxes. To create the mailbox, cre= ate +a mbox_client and call `gh_msgq_init()`. On receipt of the RX_READY interr= upt, +all messages in the RX message queue are read and pushed via the `rx_callb= ack` +of the registered mbox_client. + +.. kernel-doc:: drivers/mailbox/gunyah-msgq.c + :identifiers: gh_msgq_init diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile index fc9376117111..5f929bb55e9a 100644 --- a/drivers/mailbox/Makefile +++ b/drivers/mailbox/Makefile @@ -55,6 +55,8 @@ obj-$(CONFIG_MTK_CMDQ_MBOX) +=3D mtk-cmdq-mailbox.o =20 obj-$(CONFIG_ZYNQMP_IPI_MBOX) +=3D zynqmp-ipi-mailbox.o =20 +obj-$(CONFIG_GUNYAH) +=3D gunyah-msgq.o + obj-$(CONFIG_SUN6I_MSGBOX) +=3D sun6i-msgbox.o =20 obj-$(CONFIG_SPRD_MBOX) +=3D sprd-mailbox.o diff --git a/drivers/mailbox/gunyah-msgq.c b/drivers/mailbox/gunyah-msgq.c new file mode 100644 index 000000000000..1989298653f9 --- /dev/null +++ b/drivers/mailbox/gunyah-msgq.c @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define mbox_chan_to_msgq(chan) (container_of(chan->mbox, struct gh_msgq, = mbox)) + +static irqreturn_t gh_msgq_rx_irq_handler(int irq, void *data) +{ + struct gh_msgq *msgq =3D data; + struct gh_msgq_rx_data rx_data; + enum gh_error gh_error; + bool ready =3D true; + + while (ready) { + gh_error =3D gh_hypercall_msgq_recv(msgq->rx_ghrsc->capid, + &rx_data.data, sizeof(rx_data.data), + &rx_data.length, &ready); + if (gh_error !=3D GH_ERROR_OK) { + if (gh_error !=3D GH_ERROR_MSGQUEUE_EMPTY) + dev_warn(msgq->mbox.dev, "Failed to receive data: %d\n", gh_error); + break; + } + mbox_chan_received_data(gh_msgq_chan(msgq), &rx_data); + } + + return IRQ_HANDLED; +} + +/* Fired when message queue transitions from "full" to "space available" t= o send messages */ +static irqreturn_t gh_msgq_tx_irq_handler(int irq, void *data) +{ + struct gh_msgq *msgq =3D data; + + mbox_chan_txdone(gh_msgq_chan(msgq), 0); + + return IRQ_HANDLED; +} + +/* Fired after sending message and hypercall told us there was more space = available. */ +static void gh_msgq_txdone_tasklet(struct tasklet_struct *tasklet) +{ + struct gh_msgq *msgq =3D container_of(tasklet, struct gh_msgq, txdone_tas= klet); + + mbox_chan_txdone(gh_msgq_chan(msgq), msgq->last_ret); +} + +static int gh_msgq_send_data(struct mbox_chan *chan, void *data) +{ + struct gh_msgq *msgq =3D mbox_chan_to_msgq(chan); + struct gh_msgq_tx_data *msgq_data =3D data; + u64 tx_flags =3D 0; + enum gh_error gh_error; + bool ready; + + if (msgq_data->push) + tx_flags |=3D GH_HYPERCALL_MSGQ_TX_FLAGS_PUSH; + + gh_error =3D gh_hypercall_msgq_send(msgq->tx_ghrsc->capid, msgq_data->len= gth, msgq_data->data, + tx_flags, &ready); + + /** + * unlikely because Linux tracks state of msgq and should not try to + * send message when msgq is full. + */ + if (unlikely(gh_error =3D=3D GH_ERROR_MSGQUEUE_FULL)) + return -EAGAIN; + + /** + * Propagate all other errors to client. If we return error to mailbox + * framework, then no other messages can be sent and nobody will know + * to retry this message. + */ + msgq->last_ret =3D gh_remap_error(gh_error); + + /** + * This message was successfully sent, but message queue isn't ready to + * accept more messages because it's now full. Mailbox framework + * requires that we only report that message was transmitted when + * we're ready to transmit another message. We'll get that in the form + * of tx IRQ once the other side starts to drain the msgq. + */ + if (gh_error =3D=3D GH_ERROR_OK) { + if (!ready) + return 0; + } else + dev_err(msgq->mbox.dev, "Failed to send data: %d (%d)\n", gh_error, msgq= ->last_ret); + + /** + * We can send more messages. Mailbox framework requires that tx done + * happens asynchronously to sending the message. Gunyah message queues + * tell us right away on the hypercall return whether we can send more + * messages. To work around this, defer the txdone to a tasklet. + */ + tasklet_schedule(&msgq->txdone_tasklet); + + return 0; +} + +static struct mbox_chan_ops gh_msgq_ops =3D { + .send_data =3D gh_msgq_send_data, +}; + +/** + * gh_msgq_init() - Initialize a Gunyah message queue with an mbox_client + * @parent: optional, device parent used for the mailbox controller + * @msgq: Pointer to the gh_msgq to initialize + * @cl: A mailbox client to bind to the mailbox channel that the message q= ueue creates + * @tx_ghrsc: optional, the transmission side of the message queue + * @rx_ghrsc: optional, the receiving side of the message queue + * + * At least one of tx_ghrsc and rx_ghrsc must be not NULL. Most message qu= eue use cases come with + * a pair of message queues to facilitate bidirectional communication. Whe= n tx_ghrsc is set, + * the client can send messages with mbox_send_message(gh_msgq_chan(msgq),= msg). When rx_ghrsc + * is set, the mbox_client must register an .rx_callback() and the message= queue driver will + * deliver all available messages upon receiving the RX ready interrupt. T= he messages should be + * consumed or copied by the client right away as the gh_msgq_rx_data will= be replaced/destroyed + * after the callback. + * + * Returns - 0 on success, negative otherwise + */ +int gh_msgq_init(struct device *parent, struct gh_msgq *msgq, struct mbox_= client *cl, + struct gh_resource *tx_ghrsc, struct gh_resource *rx_ghrsc) +{ + int ret; + + /* Must have at least a tx_ghrsc or rx_ghrsc and that they are the right = device types */ + if ((!tx_ghrsc && !rx_ghrsc) || + (tx_ghrsc && tx_ghrsc->type !=3D GH_RESOURCE_TYPE_MSGQ_TX) || + (rx_ghrsc && rx_ghrsc->type !=3D GH_RESOURCE_TYPE_MSGQ_RX)) + return -EINVAL; + + if (!gh_api_has_feature(GH_FEATURE_MSGQUEUE)) + return -EOPNOTSUPP; + + msgq->tx_ghrsc =3D tx_ghrsc; + msgq->rx_ghrsc =3D rx_ghrsc; + + msgq->mbox.dev =3D parent; + msgq->mbox.ops =3D &gh_msgq_ops; + msgq->mbox.num_chans =3D 1; + msgq->mbox.txdone_irq =3D true; + msgq->mbox.chans =3D &msgq->mbox_chan; + + if (msgq->tx_ghrsc) { + ret =3D request_irq(msgq->tx_ghrsc->irq, gh_msgq_tx_irq_handler, 0, "gh_= msgq_tx", + msgq); + if (ret) + goto err_chans; + } + + if (msgq->rx_ghrsc) { + ret =3D request_threaded_irq(msgq->rx_ghrsc->irq, NULL, gh_msgq_rx_irq_h= andler, + IRQF_ONESHOT, "gh_msgq_rx", msgq); + if (ret) + goto err_tx_irq; + } + + tasklet_setup(&msgq->txdone_tasklet, gh_msgq_txdone_tasklet); + + ret =3D mbox_controller_register(&msgq->mbox); + if (ret) + goto err_rx_irq; + + ret =3D mbox_bind_client(gh_msgq_chan(msgq), cl); + if (ret) + goto err_mbox; + + return 0; +err_mbox: + mbox_controller_unregister(&msgq->mbox); +err_rx_irq: + if (msgq->rx_ghrsc) + free_irq(msgq->rx_ghrsc->irq, msgq); +err_tx_irq: + if (msgq->tx_ghrsc) + free_irq(msgq->tx_ghrsc->irq, msgq); +err_chans: + kfree(msgq->mbox.chans); + return ret; +} +EXPORT_SYMBOL_GPL(gh_msgq_init); + +void gh_msgq_remove(struct gh_msgq *msgq) +{ + tasklet_kill(&msgq->txdone_tasklet); + mbox_controller_unregister(&msgq->mbox); + + if (msgq->rx_ghrsc) + free_irq(msgq->rx_ghrsc->irq, msgq); + + if (msgq->tx_ghrsc) + free_irq(msgq->tx_ghrsc->irq, msgq); + + kfree(msgq->mbox.chans); +} +EXPORT_SYMBOL_GPL(gh_msgq_remove); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Gunyah Message Queue Driver"); diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 18cfbf5ee48b..378bec0f2ce1 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -8,11 +8,68 @@ =20 #include #include +#include #include +#include +#include #include =20 +/* Follows resource manager's resource types for VM_GET_HYP_RESOURCES */ +enum gh_resource_type { + GH_RESOURCE_TYPE_BELL_TX =3D 0, + GH_RESOURCE_TYPE_BELL_RX =3D 1, + GH_RESOURCE_TYPE_MSGQ_TX =3D 2, + GH_RESOURCE_TYPE_MSGQ_RX =3D 3, + GH_RESOURCE_TYPE_VCPU =3D 4, +}; + +struct gh_resource { + enum gh_resource_type type; + u64 capid; + unsigned int irq; +}; + +/** + * Gunyah Message Queues + */ + +#define GH_MSGQ_MAX_MSG_SIZE 240 + +struct gh_msgq_tx_data { + size_t length; + bool push; + char data[]; +}; + +struct gh_msgq_rx_data { + size_t length; + char data[GH_MSGQ_MAX_MSG_SIZE]; +}; + +struct gh_msgq { + struct gh_resource *tx_ghrsc; + struct gh_resource *rx_ghrsc; + + /* msgq private */ + int last_ret; /* Linux error, not GH_STATUS_* */ + struct mbox_chan mbox_chan; + struct mbox_controller mbox; + struct tasklet_struct txdone_tasklet; +}; + + +int gh_msgq_init(struct device *parent, struct gh_msgq *msgq, struct mbox_= client *cl, + struct gh_resource *tx_ghrsc, struct gh_resource *rx_ghrsc); +void gh_msgq_remove(struct gh_msgq *msgq); + +static inline struct mbox_chan *gh_msgq_chan(struct gh_msgq *msgq) +{ + return &msgq->mbox.chans[0]; +} + /*************************************************************************= *****/ /* Common arch-independent definitions for Gunyah hypercalls = */ + #define GH_CAPID_INVAL U64_MAX #define GH_VMID_ROOT_VM 0xff =20 --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72E6AC77B6D for ; Sat, 4 Mar 2023 01:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230079AbjCDBIW (ORCPT ); Fri, 3 Mar 2023 20:08:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229885AbjCDBH3 (ORCPT ); Fri, 3 Mar 2023 20:07:29 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 889606547E; Fri, 3 Mar 2023 17:07:11 -0800 (PST) Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240h7nU014118; Sat, 4 Mar 2023 01:06:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=9Az7kssYSbGSJfbfBT4ukVvlyUcsH7RXzy7un8kyHRc=; b=RKwCRe2nwIXW8ljtlhU+TpA2BPAYnkFEyJilA7b3rO5RYUWWwnfrwtJPBmO4KFXEA5Wi xrkrQzFcyHEbam+96DfYUsy27/4iVyJyh6Ag2k4VEu5QoekjM/830CbjjhzKZ6nNdSBD Tr3Vaa1Md6B5h5iyGhZbbPq4HQyH6uGHOEM5NnPBbAYJrNeGxXYuatADaML4mchHQo7/ GUAC5ZIrpqIHjcD9rGUCxwaWko86iKSCKei8ZyEJ7bf7Q3lwRmzqUK12yrYs2Wqw3QTY 6C4GEt2k0q/UBNtPdIuI/n7y2IQacTdM0Xn/wNyQlfX3PgE9VuLzPj/p0i2BN3ZfthqA Bg== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p32ty42y4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:56 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416tLh018647 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:55 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.986.41; Fri, 3 Mar 2023 17:06:54 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 08/26] gunyah: rsc_mgr: Add resource manager RPC core Date: Fri, 3 Mar 2023 17:06:14 -0800 Message-ID: <20230304010632.2127470-9-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: njLynAGyY091NuuYSnjN_6JTzDs9LqyK X-Proofpoint-ORIG-GUID: njLynAGyY091NuuYSnjN_6JTzDs9LqyK X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 mlxlogscore=999 lowpriorityscore=0 suspectscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 adultscore=0 impostorscore=0 phishscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The resource manager is a special virtual machine which is always running on a Gunyah system. It provides APIs for creating and destroying VMs, secure memory management, sharing/lending of memory between VMs, and setup of inter-VM communication. Calls to the resource manager are made via message queues. This patch implements the basic probing and RPC mechanism to make those API calls. Request/response calls can be made with gh_rm_call. Drivers can also register to notifications pushed by RM via gh_rm_register_notifier Specific API calls that resource manager supports will be implemented in subsequent patches. Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Makefile | 3 + drivers/virt/gunyah/rsc_mgr.c | 688 +++++++++++++++++++++++++++++++++ drivers/virt/gunyah/rsc_mgr.h | 16 + include/linux/gunyah_rsc_mgr.h | 21 + 4 files changed, 728 insertions(+) create mode 100644 drivers/virt/gunyah/rsc_mgr.c create mode 100644 drivers/virt/gunyah/rsc_mgr.h create mode 100644 include/linux/gunyah_rsc_mgr.h diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index 34f32110faf9..cc864ff5abbb 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -1,3 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 =20 obj-$(CONFIG_GUNYAH) +=3D gunyah.o + +gunyah_rsc_mgr-y +=3D rsc_mgr.o +obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o diff --git a/drivers/virt/gunyah/rsc_mgr.c b/drivers/virt/gunyah/rsc_mgr.c new file mode 100644 index 000000000000..67813c9a52db --- /dev/null +++ b/drivers/virt/gunyah/rsc_mgr.c @@ -0,0 +1,688 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rsc_mgr.h" + +#define RM_RPC_API_VERSION_MASK GENMASK(3, 0) +#define RM_RPC_HEADER_WORDS_MASK GENMASK(7, 4) +#define RM_RPC_API_VERSION FIELD_PREP(RM_RPC_API_VERSION_MASK, 1) +#define RM_RPC_HEADER_WORDS FIELD_PREP(RM_RPC_HEADER_WORDS_MASK, \ + (sizeof(struct gh_rm_rpc_hdr) / sizeof(u32))) +#define RM_RPC_API (RM_RPC_API_VERSION | RM_RPC_HEADER_WORDS) + +#define RM_RPC_TYPE_CONTINUATION 0x0 +#define RM_RPC_TYPE_REQUEST 0x1 +#define RM_RPC_TYPE_REPLY 0x2 +#define RM_RPC_TYPE_NOTIF 0x3 +#define RM_RPC_TYPE_MASK GENMASK(1, 0) + +#define GH_RM_MAX_NUM_FRAGMENTS 62 +#define RM_RPC_FRAGMENTS_MASK GENMASK(7, 2) + +struct gh_rm_rpc_hdr { + u8 api; + u8 type; + __le16 seq; + __le32 msg_id; +} __packed; + +struct gh_rm_rpc_reply_hdr { + struct gh_rm_rpc_hdr hdr; + __le32 err_code; /* GH_RM_ERROR_* */ +} __packed; + +#define GH_RM_MAX_MSG_SIZE (GH_MSGQ_MAX_MSG_SIZE - sizeof(struct gh_rm_rpc= _hdr)) + +/* RM Error codes */ +enum gh_rm_error { + GH_RM_ERROR_OK =3D 0x0, + GH_RM_ERROR_UNIMPLEMENTED =3D 0xFFFFFFFF, + GH_RM_ERROR_NOMEM =3D 0x1, + GH_RM_ERROR_NORESOURCE =3D 0x2, + GH_RM_ERROR_DENIED =3D 0x3, + GH_RM_ERROR_INVALID =3D 0x4, + GH_RM_ERROR_BUSY =3D 0x5, + GH_RM_ERROR_ARGUMENT_INVALID =3D 0x6, + GH_RM_ERROR_HANDLE_INVALID =3D 0x7, + GH_RM_ERROR_VALIDATE_FAILED =3D 0x8, + GH_RM_ERROR_MAP_FAILED =3D 0x9, + GH_RM_ERROR_MEM_INVALID =3D 0xA, + GH_RM_ERROR_MEM_INUSE =3D 0xB, + GH_RM_ERROR_MEM_RELEASED =3D 0xC, + GH_RM_ERROR_VMID_INVALID =3D 0xD, + GH_RM_ERROR_LOOKUP_FAILED =3D 0xE, + GH_RM_ERROR_IRQ_INVALID =3D 0xF, + GH_RM_ERROR_IRQ_INUSE =3D 0x10, + GH_RM_ERROR_IRQ_RELEASED =3D 0x11, +}; + +/** + * struct gh_rm_connection - Represents a complete message from resource m= anager + * @payload: Combined payload of all the fragments (msg headers stripped o= ff). + * @size: Size of the payload received so far. + * @msg_id: Message ID from the header. + * @type: RM_RPC_TYPE_REPLY or RM_RPC_TYPE_NOTIF. + * @num_fragments: total number of fragments expected to be received. + * @fragments_received: fragments received so far. + * @reply: Fields used for request/reply sequences + * @notification: Fields used for notifiations + */ +struct gh_rm_connection { + void *payload; + size_t size; + __le32 msg_id; + u8 type; + + u8 num_fragments; + u8 fragments_received; + + union { + /** + * @ret: Linux return code, there was an error processing connection + * @seq: Sequence ID for the main message. + * @rm_error: For request/reply sequences with standard replies + * @seq_done: Signals caller that the RM reply has been received + */ + struct { + int ret; + u16 seq; + enum gh_rm_error rm_error; + struct completion seq_done; + } reply; + + /** + * @rm: Pointer to the RM that launched the connection + * @work: Triggered when all fragments of a notification received + */ + struct { + struct gh_rm *rm; + struct work_struct work; + } notification; + }; +}; + +/** + * struct gh_rm - private data for communicating w/Gunyah resource manager + * @dev: pointer to device + * @tx_ghrsc: message queue resource to TX to RM + * @rx_ghrsc: message queue resource to RX from RM + * @msgq: mailbox instance of above + * @active_rx_connection: ongoing gh_rm_connection for which we're receivi= ng fragments + * @last_tx_ret: return value of last mailbox tx + * @call_xarray: xarray to allocate & lookup sequence IDs for Request/Resp= onse flows + * @next_seq: next ID to allocate (for xa_alloc_cyclic) + * @cache: cache for allocating Tx messages + * @send_lock: synchronization to allow only one request to be sent at a t= ime + * @nh: notifier chain for clients interested in RM notification messages + */ +struct gh_rm { + struct device *dev; + struct gh_resource tx_ghrsc; + struct gh_resource rx_ghrsc; + struct gh_msgq msgq; + struct mbox_client msgq_client; + struct gh_rm_connection *active_rx_connection; + int last_tx_ret; + + struct xarray call_xarray; + u32 next_seq; + + struct kmem_cache *cache; + struct mutex send_lock; + struct blocking_notifier_head nh; +}; + +/** + * gh_rm_remap_error() - Remap Gunyah resource manager errors into a Linux= error code + * @gh_error: "Standard" return value from Gunyah resource manager + */ +static inline int gh_rm_remap_error(enum gh_rm_error rm_error) +{ + switch (rm_error) { + case GH_RM_ERROR_OK: + return 0; + case GH_RM_ERROR_UNIMPLEMENTED: + return -EOPNOTSUPP; + case GH_RM_ERROR_NOMEM: + return -ENOMEM; + case GH_RM_ERROR_NORESOURCE: + return -ENODEV; + case GH_RM_ERROR_DENIED: + return -EPERM; + case GH_RM_ERROR_BUSY: + return -EBUSY; + case GH_RM_ERROR_INVALID: + case GH_RM_ERROR_ARGUMENT_INVALID: + case GH_RM_ERROR_HANDLE_INVALID: + case GH_RM_ERROR_VALIDATE_FAILED: + case GH_RM_ERROR_MAP_FAILED: + case GH_RM_ERROR_MEM_INVALID: + case GH_RM_ERROR_MEM_INUSE: + case GH_RM_ERROR_MEM_RELEASED: + case GH_RM_ERROR_VMID_INVALID: + case GH_RM_ERROR_LOOKUP_FAILED: + case GH_RM_ERROR_IRQ_INVALID: + case GH_RM_ERROR_IRQ_INUSE: + case GH_RM_ERROR_IRQ_RELEASED: + return -EINVAL; + default: + return -EBADMSG; + } +} + +static int gh_rm_init_connection_payload(struct gh_rm_connection *connecti= on, void *msg, + size_t hdr_size, size_t msg_size) +{ + size_t max_buf_size, payload_size; + struct gh_rm_rpc_hdr *hdr =3D msg; + + if (msg_size < hdr_size) + return -EINVAL; + + payload_size =3D msg_size - hdr_size; + + connection->num_fragments =3D FIELD_GET(RM_RPC_FRAGMENTS_MASK, hdr->type); + connection->fragments_received =3D 0; + + /* There's not going to be any payload, no need to allocate buffer. */ + if (!payload_size && !connection->num_fragments) + return 0; + + if (connection->num_fragments > GH_RM_MAX_NUM_FRAGMENTS) + return -EINVAL; + + max_buf_size =3D payload_size + (connection->num_fragments * GH_RM_MAX_MS= G_SIZE); + + connection->payload =3D kzalloc(max_buf_size, GFP_KERNEL); + if (!connection->payload) + return -ENOMEM; + + memcpy(connection->payload, msg + hdr_size, payload_size); + connection->size =3D payload_size; + return 0; +} + +static void gh_rm_abort_connection(struct gh_rm *rm) +{ + switch (rm->active_rx_connection->type) { + case RM_RPC_TYPE_REPLY: + rm->active_rx_connection->reply.ret =3D -EIO; + complete(&rm->active_rx_connection->reply.seq_done); + break; + case RM_RPC_TYPE_NOTIF: + fallthrough; + default: + kfree(rm->active_rx_connection->payload); + kfree(rm->active_rx_connection); + } + + rm->active_rx_connection =3D NULL; +} + +static void gh_rm_notif_work(struct work_struct *work) +{ + struct gh_rm_connection *connection =3D container_of(work, struct gh_rm_c= onnection, + notification.work); + struct gh_rm *rm =3D connection->notification.rm; + + blocking_notifier_call_chain(&rm->nh, connection->msg_id, connection->pay= load); + + gh_rm_put(rm); + kfree(connection->payload); + kfree(connection); +} + +static void gh_rm_process_notif(struct gh_rm *rm, void *msg, size_t msg_si= ze) +{ + struct gh_rm_connection *connection; + struct gh_rm_rpc_hdr *hdr =3D msg; + int ret; + + if (rm->active_rx_connection) + gh_rm_abort_connection(rm); + + connection =3D kzalloc(sizeof(*connection), GFP_KERNEL); + if (!connection) + return; + + connection->type =3D RM_RPC_TYPE_NOTIF; + connection->msg_id =3D hdr->msg_id; + + gh_rm_get(rm); + connection->notification.rm =3D rm; + INIT_WORK(&connection->notification.work, gh_rm_notif_work); + + ret =3D gh_rm_init_connection_payload(connection, msg, sizeof(*hdr), msg_= size); + if (ret) { + dev_err(rm->dev, "Failed to initialize connection for notification: %d\n= ", ret); + gh_rm_put(rm); + kfree(connection); + return; + } + + rm->active_rx_connection =3D connection; +} + +static void gh_rm_process_rply(struct gh_rm *rm, void *msg, size_t msg_siz= e) +{ + struct gh_rm_rpc_reply_hdr *reply_hdr =3D msg; + struct gh_rm_connection *connection; + u16 seq_id; + + seq_id =3D le16_to_cpu(reply_hdr->hdr.seq); + connection =3D xa_load(&rm->call_xarray, seq_id); + + if (!connection || connection->msg_id !=3D reply_hdr->hdr.msg_id) + return; + + if (rm->active_rx_connection) + gh_rm_abort_connection(rm); + + if (gh_rm_init_connection_payload(connection, msg, sizeof(*reply_hdr), ms= g_size)) { + dev_err(rm->dev, "Failed to alloc connection buffer for sequence %d\n", = seq_id); + /* Send connection complete and error the client. */ + connection->reply.ret =3D -ENOMEM; + complete(&connection->reply.seq_done); + return; + } + + connection->reply.rm_error =3D le32_to_cpu(reply_hdr->err_code); + rm->active_rx_connection =3D connection; +} + +static void gh_rm_process_cont(struct gh_rm *rm, struct gh_rm_connection *= connection, + void *msg, size_t msg_size) +{ + struct gh_rm_rpc_hdr *hdr =3D msg; + size_t payload_size =3D msg_size - sizeof(*hdr); + + if (!rm->active_rx_connection) + return; + + /* + * hdr->fragments and hdr->msg_id preserves the value from first reply + * or notif message. To detect mishandling, check it's still intact. + */ + if (connection->msg_id !=3D hdr->msg_id || + connection->num_fragments !=3D FIELD_GET(RM_RPC_FRAGMENTS_MASK, hdr->typ= e)) { + gh_rm_abort_connection(rm); + return; + } + + memcpy(connection->payload + connection->size, msg + sizeof(*hdr), payloa= d_size); + connection->size +=3D payload_size; + connection->fragments_received++; +} + +static void gh_rm_try_complete_connection(struct gh_rm *rm) +{ + struct gh_rm_connection *connection =3D rm->active_rx_connection; + + if (!connection || connection->fragments_received !=3D connection->num_fr= agments) + return; + + switch (connection->type) { + case RM_RPC_TYPE_REPLY: + complete(&connection->reply.seq_done); + break; + case RM_RPC_TYPE_NOTIF: + schedule_work(&connection->notification.work); + break; + default: + dev_err_ratelimited(rm->dev, "Invalid message type (%d) received\n", + connection->type); + gh_rm_abort_connection(rm); + break; + } + + rm->active_rx_connection =3D NULL; +} + +static void gh_rm_msgq_rx_data(struct mbox_client *cl, void *mssg) +{ + struct gh_rm *rm =3D container_of(cl, struct gh_rm, msgq_client); + struct gh_msgq_rx_data *rx_data =3D mssg; + size_t msg_size =3D rx_data->length; + void *msg =3D rx_data->data; + struct gh_rm_rpc_hdr *hdr; + + if (msg_size < sizeof(*hdr) || msg_size > GH_MSGQ_MAX_MSG_SIZE) + return; + + hdr =3D msg; + if (hdr->api !=3D RM_RPC_API) { + dev_err(rm->dev, "Unknown RM RPC API version: %x\n", hdr->api); + return; + } + + switch (FIELD_GET(RM_RPC_TYPE_MASK, hdr->type)) { + case RM_RPC_TYPE_NOTIF: + gh_rm_process_notif(rm, msg, msg_size); + break; + case RM_RPC_TYPE_REPLY: + gh_rm_process_rply(rm, msg, msg_size); + break; + case RM_RPC_TYPE_CONTINUATION: + gh_rm_process_cont(rm, rm->active_rx_connection, msg, msg_size); + break; + default: + dev_err(rm->dev, "Invalid message type (%lu) received\n", + FIELD_GET(RM_RPC_TYPE_MASK, hdr->type)); + return; + } + + gh_rm_try_complete_connection(rm); +} + +static void gh_rm_msgq_tx_done(struct mbox_client *cl, void *mssg, int r) +{ + struct gh_rm *rm =3D container_of(cl, struct gh_rm, msgq_client); + + kmem_cache_free(rm->cache, mssg); + rm->last_tx_ret =3D r; +} + +static int gh_rm_send_request(struct gh_rm *rm, u32 message_id, + const void *req_buff, size_t req_buf_size, + struct gh_rm_connection *connection) +{ + size_t buf_size_remaining =3D req_buf_size; + const void *req_buf_curr =3D req_buff; + struct gh_msgq_tx_data *msg; + struct gh_rm_rpc_hdr *hdr, hdr_template; + u32 cont_fragments =3D 0; + size_t payload_size; + void *payload; + int ret; + + if (req_buf_size > GH_RM_MAX_NUM_FRAGMENTS * GH_RM_MAX_MSG_SIZE) { + dev_warn(rm->dev, "Limit exceeded for the number of fragments: %u\n", + cont_fragments); + dump_stack(); + return -E2BIG; + } + + if (req_buf_size) + cont_fragments =3D (req_buf_size - 1) / GH_RM_MAX_MSG_SIZE; + + hdr_template.api =3D RM_RPC_API; + hdr_template.type =3D FIELD_PREP(RM_RPC_TYPE_MASK, RM_RPC_TYPE_REQUEST) | + FIELD_PREP(RM_RPC_FRAGMENTS_MASK, cont_fragments); + hdr_template.seq =3D cpu_to_le16(connection->reply.seq); + hdr_template.msg_id =3D cpu_to_le32(message_id); + + ret =3D mutex_lock_interruptible(&rm->send_lock); + if (ret) + return ret; + + /* Consider also the 'request' packet for the loop count */ + do { + msg =3D kmem_cache_zalloc(rm->cache, GFP_KERNEL); + if (!msg) { + ret =3D -ENOMEM; + goto out; + } + + /* Fill header */ + hdr =3D (struct gh_rm_rpc_hdr *)msg->data; + *hdr =3D hdr_template; + + /* Copy payload */ + payload =3D hdr + 1; + payload_size =3D min(buf_size_remaining, GH_RM_MAX_MSG_SIZE); + memcpy(payload, req_buf_curr, payload_size); + req_buf_curr +=3D payload_size; + buf_size_remaining -=3D payload_size; + + /* Force the last fragment to immediately alert the receiver */ + msg->push =3D !buf_size_remaining; + msg->length =3D sizeof(*hdr) + payload_size; + + ret =3D mbox_send_message(gh_msgq_chan(&rm->msgq), msg); + if (ret < 0) { + kmem_cache_free(rm->cache, msg); + break; + } + + if (rm->last_tx_ret) { + ret =3D rm->last_tx_ret; + break; + } + + hdr_template.type =3D FIELD_PREP(RM_RPC_TYPE_MASK, RM_RPC_TYPE_CONTINUAT= ION) | + FIELD_PREP(RM_RPC_FRAGMENTS_MASK, cont_fragments); + } while (buf_size_remaining); + +out: + mutex_unlock(&rm->send_lock); + return ret < 0 ? ret : 0; +} + +/** + * gh_rm_call: Achieve request-response type communication with RPC + * @rm: Pointer to Gunyah resource manager internal data + * @message_id: The RM RPC message-id + * @req_buff: Request buffer that contains the payload + * @req_buf_size: Total size of the payload + * @resp_buf: Pointer to a response buffer + * @resp_buf_size: Size of the response buffer + * + * Make a request to the RM-VM and wait for reply back. For a successful + * response, the function returns the payload. The size of the payload is = set in + * resp_buf_size. The resp_buf should be freed by the caller when 0 is ret= urned + * and resp_buf_size !=3D 0. + * + * req_buff should be not NULL for req_buf_size >0. If req_buf_size =3D=3D= 0, + * req_buff *can* be NULL and no additional payload is sent. + * + * Context: Process context. Will sleep waiting for reply. + * Return: 0 on success. <0 if error. + */ +int gh_rm_call(struct gh_rm *rm, u32 message_id, void *req_buff, size_t re= q_buf_size, + void **resp_buf, size_t *resp_buf_size) +{ + struct gh_rm_connection *connection; + u32 seq_id; + int ret; + + /* message_id 0 is reserved. req_buf_size implies req_buf is not NULL */ + if (!message_id || (!req_buff && req_buf_size) || !rm) + return -EINVAL; + + + connection =3D kzalloc(sizeof(*connection), GFP_KERNEL); + if (!connection) + return -ENOMEM; + + connection->type =3D RM_RPC_TYPE_REPLY; + connection->msg_id =3D cpu_to_le32(message_id); + + init_completion(&connection->reply.seq_done); + + /* Allocate a new seq number for this connection */ + ret =3D xa_alloc_cyclic(&rm->call_xarray, &seq_id, connection, xa_limit_1= 6b, &rm->next_seq, + GFP_KERNEL); + if (ret < 0) + goto free; + connection->reply.seq =3D lower_16_bits(seq_id); + + /* Send the request to the Resource Manager */ + ret =3D gh_rm_send_request(rm, message_id, req_buff, req_buf_size, connec= tion); + if (ret < 0) + goto out; + + /* Wait for response */ + ret =3D wait_for_completion_interruptible(&connection->reply.seq_done); + if (ret) + goto out; + + /* Check for internal (kernel) error waiting for the response */ + if (connection->reply.ret) { + ret =3D connection->reply.ret; + if (ret !=3D -ENOMEM) + kfree(connection->payload); + goto out; + } + + /* Got a response, did resource manager give us an error? */ + if (connection->reply.rm_error !=3D GH_RM_ERROR_OK) { + dev_warn(rm->dev, "RM rejected message %08x. Error: %d\n", message_id, + connection->reply.rm_error); + dump_stack(); + ret =3D gh_rm_remap_error(connection->reply.rm_error); + kfree(connection->payload); + goto out; + } + + /* Everything looks good, return the payload */ + if (resp_buf_size) + *resp_buf_size =3D connection->size; + if (connection->size && resp_buf) + *resp_buf =3D connection->payload; + else { + /* kfree in case RM sent us multiple fragments but never any data in + * those fragments. We would've allocated memory for it, but connection-= >size =3D=3D 0 + */ + kfree(connection->payload); + } + +out: + xa_erase(&rm->call_xarray, connection->reply.seq); +free: + kfree(connection); + return ret; +} + + +int gh_rm_notifier_register(struct gh_rm *rm, struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&rm->nh, nb); +} +EXPORT_SYMBOL_GPL(gh_rm_notifier_register); + +int gh_rm_notifier_unregister(struct gh_rm *rm, struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&rm->nh, nb); +} +EXPORT_SYMBOL_GPL(gh_rm_notifier_unregister); + +struct device *gh_rm_get(struct gh_rm *rm) +{ + return get_device(rm->miscdev.this_device); +} +EXPORT_SYMBOL_GPL(gh_rm_get); + +void gh_rm_put(struct gh_rm *rm) +{ + put_device(rm->miscdev.this_device); +} +EXPORT_SYMBOL_GPL(gh_rm_put); + +static int gh_msgq_platform_probe_direction(struct platform_device *pdev, = bool tx, + struct gh_resource *ghrsc) +{ + struct device_node *node =3D pdev->dev.of_node; + int ret; + int idx =3D tx ? 0 : 1; + + ghrsc->type =3D tx ? GH_RESOURCE_TYPE_MSGQ_TX : GH_RESOURCE_TYPE_MSGQ_RX; + + ghrsc->irq =3D platform_get_irq(pdev, idx); + if (ghrsc->irq < 0) { + dev_err(&pdev->dev, "Failed to get irq%d: %d\n", idx, ghrsc->irq); + return ghrsc->irq; + } + + ret =3D of_property_read_u64_index(node, "reg", idx, &ghrsc->capid); + if (ret) { + dev_err(&pdev->dev, "Failed to get capid%d: %d\n", idx, ret); + return ret; + } + + return 0; +} + +static int gh_rm_drv_probe(struct platform_device *pdev) +{ + struct gh_msgq_tx_data *msg; + struct gh_rm *rm; + int ret; + + rm =3D devm_kzalloc(&pdev->dev, sizeof(*rm), GFP_KERNEL); + if (!rm) + return -ENOMEM; + + platform_set_drvdata(pdev, rm); + rm->dev =3D &pdev->dev; + + mutex_init(&rm->send_lock); + BLOCKING_INIT_NOTIFIER_HEAD(&rm->nh); + xa_init_flags(&rm->call_xarray, XA_FLAGS_ALLOC); + rm->cache =3D kmem_cache_create("gh_rm", struct_size(msg, data, GH_MSGQ_M= AX_MSG_SIZE), 0, + SLAB_HWCACHE_ALIGN, NULL); + if (!rm->cache) + return -ENOMEM; + + ret =3D gh_msgq_platform_probe_direction(pdev, true, &rm->tx_ghrsc); + if (ret) + goto err_cache; + + ret =3D gh_msgq_platform_probe_direction(pdev, false, &rm->rx_ghrsc); + if (ret) + goto err_cache; + + rm->msgq_client.dev =3D &pdev->dev; + rm->msgq_client.tx_block =3D true; + rm->msgq_client.rx_callback =3D gh_rm_msgq_rx_data; + rm->msgq_client.tx_done =3D gh_rm_msgq_tx_done; + + return gh_msgq_init(&pdev->dev, &rm->msgq, &rm->msgq_client, &rm->tx_ghrs= c, &rm->rx_ghrsc); +err_cache: + kmem_cache_destroy(rm->cache); + return ret; +} + +static int gh_rm_drv_remove(struct platform_device *pdev) +{ + struct gh_rm *rm =3D platform_get_drvdata(pdev); + + mbox_free_channel(gh_msgq_chan(&rm->msgq)); + gh_msgq_remove(&rm->msgq); + kmem_cache_destroy(rm->cache); + + return 0; +} + +static const struct of_device_id gh_rm_of_match[] =3D { + { .compatible =3D "gunyah-resource-manager" }, + {} +}; +MODULE_DEVICE_TABLE(of, gh_rm_of_match); + +static struct platform_driver gh_rm_driver =3D { + .probe =3D gh_rm_drv_probe, + .remove =3D gh_rm_drv_remove, + .driver =3D { + .name =3D "gh_rsc_mgr", + .of_match_table =3D gh_rm_of_match, + }, +}; +module_platform_driver(gh_rm_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Gunyah Resource Manager Driver"); diff --git a/drivers/virt/gunyah/rsc_mgr.h b/drivers/virt/gunyah/rsc_mgr.h new file mode 100644 index 000000000000..3665ebc7b020 --- /dev/null +++ b/drivers/virt/gunyah/rsc_mgr.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ +#ifndef __GH_RSC_MGR_PRIV_H +#define __GH_RSC_MGR_PRIV_H + +#include +#include +#include + +struct gh_rm; +int gh_rm_call(struct gh_rm *rsc_mgr, u32 message_id, void *req_buff, size= _t req_buf_size, + void **resp_buf, size_t *resp_buf_size); + +#endif diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h new file mode 100644 index 000000000000..deca9b3da541 --- /dev/null +++ b/include/linux/gunyah_rsc_mgr.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#ifndef _GUNYAH_RSC_MGR_H +#define _GUNYAH_RSC_MGR_H + +#include +#include +#include + +#define GH_VMID_INVAL U16_MAX + +struct gh_rm; +int gh_rm_notifier_register(struct gh_rm *rm, struct notifier_block *nb); +int gh_rm_notifier_unregister(struct gh_rm *rm, struct notifier_block *nb); +struct device *gh_rm_get(struct gh_rm *rm); +void gh_rm_put(struct gh_rm *rm); + +#endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45011C61DA4 for ; Sat, 4 Mar 2023 01:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230057AbjCDBIR (ORCPT ); Fri, 3 Mar 2023 20:08:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229874AbjCDBH2 (ORCPT ); Fri, 3 Mar 2023 20:07:28 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5ED365054; Fri, 3 Mar 2023 17:07:10 -0800 (PST) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323MXBj2013044; Sat, 4 Mar 2023 01:06:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=JlyHqXP7Soq3BVBkMamSVK9ujf73myqb4KE+XUy1blU=; b=crhKsJznVTFjHCT2VrtO0DCDFRxWf09Ra70xdA5b48qlWbmkG14JJ1NXTeYzTliyGDc4 6rLICaEIDhW+07in3jznrL75rGnxXS2MOAZBgeBvHp/GBO2pBFGu/ow/0iornqdQsblg 3se9Hvzyyr7yTBVMSpygcR/4xi6I3SdgNaQPClvqbmfUPetKS5YQ+bZ0SKxpJWgmI4Jc HQduplzZQiDJE7po6FvBMfLGU27eP7miTuKDHNcyxntu6cWNUnrsjyWXYOm7LQg9Z67a 11pOgie64KXW0vjZmz+rdFTX4h+GrviTSDk1lgTz7gdBVe5Chz43UdwDe51EFrj3erJ0 qg== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3ph4rpdg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:58 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416vTp031701 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:57 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.986.41; Fri, 3 Mar 2023 17:06:56 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 09/26] gunyah: rsc_mgr: Add VM lifecycle RPC Date: Fri, 3 Mar 2023 17:06:15 -0800 Message-ID: <20230304010632.2127470-10-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: BhzwMmc3EXhrPh6P32A8rJDh1iAv9dCF X-Proofpoint-GUID: BhzwMmc3EXhrPh6P32A8rJDh1iAv9dCF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 suspectscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add Gunyah Resource Manager RPC to launch an unauthenticated VM. Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Makefile | 2 +- drivers/virt/gunyah/rsc_mgr_rpc.c | 260 ++++++++++++++++++++++++++++++ include/linux/gunyah_rsc_mgr.h | 73 +++++++++ 3 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/gunyah/rsc_mgr_rpc.c diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index cc864ff5abbb..de29769f2f3f 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -2,5 +2,5 @@ =20 obj-$(CONFIG_GUNYAH) +=3D gunyah.o =20 -gunyah_rsc_mgr-y +=3D rsc_mgr.o +gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o diff --git a/drivers/virt/gunyah/rsc_mgr_rpc.c b/drivers/virt/gunyah/rsc_mg= r_rpc.c new file mode 100644 index 000000000000..ffcb861a31b5 --- /dev/null +++ b/drivers/virt/gunyah/rsc_mgr_rpc.c @@ -0,0 +1,260 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include "rsc_mgr.h" + +/* Message IDs: VM Management */ +#define GH_RM_RPC_VM_ALLOC_VMID 0x56000001 +#define GH_RM_RPC_VM_DEALLOC_VMID 0x56000002 +#define GH_RM_RPC_VM_START 0x56000004 +#define GH_RM_RPC_VM_STOP 0x56000005 +#define GH_RM_RPC_VM_RESET 0x56000006 +#define GH_RM_RPC_VM_CONFIG_IMAGE 0x56000009 +#define GH_RM_RPC_VM_INIT 0x5600000B +#define GH_RM_RPC_VM_GET_HYP_RESOURCES 0x56000020 +#define GH_RM_RPC_VM_GET_VMID 0x56000024 + +struct gh_rm_vm_common_vmid_req { + __le16 vmid; + __le16 _padding; +} __packed; + +/* Call: VM_ALLOC */ +struct gh_rm_vm_alloc_vmid_resp { + __le16 vmid; + __le16 _padding; +} __packed; + +/* Call: VM_STOP */ +#define GH_RM_VM_STOP_FLAG_FORCE_STOP BIT(0) + +#define GH_RM_VM_STOP_REASON_FORCE_STOP 3 + +struct gh_rm_vm_stop_req { + __le16 vmid; + u8 flags; + u8 _padding; + __le32 stop_reason; +} __packed; + +/* Call: VM_CONFIG_IMAGE */ +struct gh_rm_vm_config_image_req { + __le16 vmid; + __le16 auth_mech; + __le32 mem_handle; + __le64 image_offset; + __le64 image_size; + __le64 dtb_offset; + __le64 dtb_size; +} __packed; + +/* + * Several RM calls take only a VMID as a parameter and give only standard + * response back. Deduplicate boilerplate code by using this common call. + */ +static int gh_rm_common_vmid_call(struct gh_rm *rm, u32 message_id, u16 vm= id) +{ + struct gh_rm_vm_common_vmid_req req_payload =3D { + .vmid =3D cpu_to_le16(vmid), + }; + + return gh_rm_call(rm, message_id, &req_payload, sizeof(req_payload), NULL= , NULL); +} + +/** + * gh_rm_alloc_vmid() - Allocate a new VM in Gunyah. Returns the VM identi= fier. + * @rm: Handle to a Gunyah resource manager + * @vmid: Use 0 to dynamically allocate a VM. A reserved VMID can be suppl= ied + * to request allocation of a platform-defined VM. + * + * Returns - the allocated VMID or negative value on error + */ +int gh_rm_alloc_vmid(struct gh_rm *rm, u16 vmid) +{ + struct gh_rm_vm_common_vmid_req req_payload =3D { + .vmid =3D vmid, + }; + struct gh_rm_vm_alloc_vmid_resp *resp_payload; + size_t resp_size; + void *resp; + int ret; + + ret =3D gh_rm_call(rm, GH_RM_RPC_VM_ALLOC_VMID, &req_payload, sizeof(req_= payload), &resp, + &resp_size); + if (ret) + return ret; + + if (!vmid) { + resp_payload =3D resp; + ret =3D le16_to_cpu(resp_payload->vmid); + kfree(resp); + } + + return ret; +} + +/** + * gh_rm_dealloc_vmid() - Dispose the VMID + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier allocated with gh_rm_alloc_vmid + */ +int gh_rm_dealloc_vmid(struct gh_rm *rm, u16 vmid) +{ + return gh_rm_common_vmid_call(rm, GH_RM_RPC_VM_DEALLOC_VMID, vmid); +} + +/** + * gh_rm_vm_reset() - Reset the VM's resources + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier allocated with gh_rm_alloc_vmid + * + * While tearing down the VM, request RM to clean up all the VM resources + * associated with the VM. Only after this, Linux can clean up all the + * references it maintains to resources. + */ +int gh_rm_vm_reset(struct gh_rm *rm, u16 vmid) +{ + return gh_rm_common_vmid_call(rm, GH_RM_RPC_VM_RESET, vmid); +} + +/** + * gh_rm_vm_start() - Move the VM into "ready to run" state + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier allocated with gh_rm_alloc_vmid + * + * On VMs which use proxy scheduling, vcpu_run is needed to actually run t= he VM. + * On VMs which use Gunyah's scheduling, the vCPUs start executing in acco= rdance with Gunyah + * scheduling policies. + */ +int gh_rm_vm_start(struct gh_rm *rm, u16 vmid) +{ + return gh_rm_common_vmid_call(rm, GH_RM_RPC_VM_START, vmid); +} + +/** + * gh_rm_vm_stop() - Send a request to Resource Manager VM to forcibly sto= p a VM. + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier allocated with gh_rm_alloc_vmid + */ +int gh_rm_vm_stop(struct gh_rm *rm, u16 vmid) +{ + struct gh_rm_vm_stop_req req_payload =3D { + .vmid =3D cpu_to_le16(vmid), + .flags =3D GH_RM_VM_STOP_FLAG_FORCE_STOP, + .stop_reason =3D cpu_to_le32(GH_RM_VM_STOP_REASON_FORCE_STOP), + }; + + return gh_rm_call(rm, GH_RM_RPC_VM_STOP, &req_payload, sizeof(req_payload= ), NULL, NULL); +} + +/** + * gh_rm_vm_configure() - Prepare a VM to start and provide the common + * configuration needed by RM to configure a VM + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier allocated with gh_rm_alloc_vmid + * @auth_mechanism: Authentication mechanism used by resource manager to v= erify + * the virtual machine + * @mem_handle: Handle to a previously shared memparcel that contains all = parts + * of the VM image subject to authentication. + * @image_offset: Start address of VM image, relative to the start of memp= arcel + * @image_size: Size of the VM image + * @dtb_offset: Start address of the devicetree binary with VM configurati= on, + * relative to start of memparcel. + * @dtb_size: Maximum size of devicetree binary. Resource manager applies + * an overlay to the DTB and dtb_size should include room for + * the overlay. + */ +int gh_rm_vm_configure(struct gh_rm *rm, u16 vmid, enum gh_rm_vm_auth_mech= anism auth_mechanism, + u32 mem_handle, u64 image_offset, u64 image_size, u64 dtb_offset, u64 dt= b_size) +{ + struct gh_rm_vm_config_image_req req_payload =3D { + .vmid =3D cpu_to_le16(vmid), + .auth_mech =3D cpu_to_le16(auth_mechanism), + .mem_handle =3D cpu_to_le32(mem_handle), + .image_offset =3D cpu_to_le64(image_offset), + .image_size =3D cpu_to_le64(image_size), + .dtb_offset =3D cpu_to_le64(dtb_offset), + .dtb_size =3D cpu_to_le64(dtb_size), + }; + + return gh_rm_call(rm, GH_RM_RPC_VM_CONFIG_IMAGE, &req_payload, sizeof(req= _payload), + NULL, NULL); +} + +/** + * gh_rm_vm_init() - Move the VM to initialized state. + * @rm: Handle to a Gunyah resource manager + * @vmid: VM identifier + * + * RM will allocate needed resources for the VM. + */ +int gh_rm_vm_init(struct gh_rm *rm, u16 vmid) +{ + return gh_rm_common_vmid_call(rm, GH_RM_RPC_VM_INIT, vmid); +} + +/** + * gh_rm_get_hyp_resources() - Retrieve hypervisor resources (capabilities= ) associated with a VM + * @rm: Handle to a Gunyah resource manager + * @vmid: VMID of the other VM to get the resources of + * @resources: Set by gh_rm_get_hyp_resources and contains the returned hy= pervisor resources. + */ +int gh_rm_get_hyp_resources(struct gh_rm *rm, u16 vmid, + struct gh_rm_hyp_resources **resources) +{ + struct gh_rm_vm_common_vmid_req req_payload =3D { + .vmid =3D cpu_to_le16(vmid), + }; + struct gh_rm_hyp_resources *resp; + size_t resp_size; + int ret; + + ret =3D gh_rm_call(rm, GH_RM_RPC_VM_GET_HYP_RESOURCES, + &req_payload, sizeof(req_payload), + (void **)&resp, &resp_size); + if (ret) + return ret; + + if (!resp_size) + return -EBADMSG; + + if (resp_size < struct_size(resp, entries, 0) || + resp_size !=3D struct_size(resp, entries, le32_to_cpu(resp->n_entries)))= { + kfree(resp); + return -EBADMSG; + } + + *resources =3D resp; + return 0; +} + +/** + * gh_rm_get_vmid() - Retrieve VMID of this virtual machine + * @rm: Handle to a Gunyah resource manager + * @vmid: Filled with the VMID of this VM + */ +int gh_rm_get_vmid(struct gh_rm *rm, u16 *vmid) +{ + static u16 cached_vmid =3D GH_VMID_INVAL; + size_t resp_size; + __le32 *resp; + int ret; + + if (cached_vmid !=3D GH_VMID_INVAL) { + *vmid =3D cached_vmid; + return 0; + } + + ret =3D gh_rm_call(rm, GH_RM_RPC_VM_GET_VMID, NULL, 0, (void **)&resp, &r= esp_size); + if (ret) + return ret; + + *vmid =3D cached_vmid =3D lower_16_bits(le32_to_cpu(*resp)); + kfree(resp); + + return ret; +} +EXPORT_SYMBOL_GPL(gh_rm_get_vmid); diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index deca9b3da541..6a2f434e67f7 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -18,4 +18,77 @@ int gh_rm_notifier_unregister(struct gh_rm *rm, struct n= otifier_block *nb); struct device *gh_rm_get(struct gh_rm *rm); void gh_rm_put(struct gh_rm *rm); =20 +struct gh_rm_vm_exited_payload { + __le16 vmid; + __le16 exit_type; + __le32 exit_reason_size; + u8 exit_reason[]; +} __packed; + +#define GH_RM_NOTIFICATION_VM_EXITED 0x56100001 + +enum gh_rm_vm_status { + GH_RM_VM_STATUS_NO_STATE =3D 0, + GH_RM_VM_STATUS_INIT =3D 1, + GH_RM_VM_STATUS_READY =3D 2, + GH_RM_VM_STATUS_RUNNING =3D 3, + GH_RM_VM_STATUS_PAUSED =3D 4, + GH_RM_VM_STATUS_LOAD =3D 5, + GH_RM_VM_STATUS_AUTH =3D 6, + GH_RM_VM_STATUS_INIT_FAILED =3D 8, + GH_RM_VM_STATUS_EXITED =3D 9, + GH_RM_VM_STATUS_RESETTING =3D 10, + GH_RM_VM_STATUS_RESET =3D 11, +}; + +struct gh_rm_vm_status_payload { + __le16 vmid; + u16 reserved; + u8 vm_status; + u8 os_status; + __le16 app_status; +} __packed; + +#define GH_RM_NOTIFICATION_VM_STATUS 0x56100008 + +/* RPC Calls */ +int gh_rm_alloc_vmid(struct gh_rm *rm, u16 vmid); +int gh_rm_dealloc_vmid(struct gh_rm *rm, u16 vmid); +int gh_rm_vm_reset(struct gh_rm *rm, u16 vmid); +int gh_rm_vm_start(struct gh_rm *rm, u16 vmid); +int gh_rm_vm_stop(struct gh_rm *rm, u16 vmid); + +enum gh_rm_vm_auth_mechanism { + GH_RM_VM_AUTH_NONE =3D 0, + GH_RM_VM_AUTH_QCOM_PIL_ELF =3D 1, + GH_RM_VM_AUTH_QCOM_ANDROID_PVM =3D 2, +}; + +int gh_rm_vm_configure(struct gh_rm *rm, u16 vmid, enum gh_rm_vm_auth_mech= anism auth_mechanism, + u32 mem_handle, u64 image_offset, u64 image_size, + u64 dtb_offset, u64 dtb_size); +int gh_rm_vm_init(struct gh_rm *rm, u16 vmid); + +struct gh_rm_hyp_resource { + u8 type; + u8 reserved; + __le16 partner_vmid; + __le32 resource_handle; + __le32 resource_label; + __le64 cap_id; + __le32 virq_handle; + __le32 virq; + __le64 base; + __le64 size; +} __packed; + +struct gh_rm_hyp_resources { + __le32 n_entries; + struct gh_rm_hyp_resource entries[]; +} __packed; + +int gh_rm_get_hyp_resources(struct gh_rm *rm, u16 vmid, + struct gh_rm_hyp_resources **resources); +int gh_rm_get_vmid(struct gh_rm *rm, u16 *vmid); + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD8C7C77B61 for ; Sat, 4 Mar 2023 01:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230024AbjCDBIM (ORCPT ); Fri, 3 Mar 2023 20:08:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbjCDBH3 (ORCPT ); Fri, 3 Mar 2023 20:07:29 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14A5967803; Fri, 3 Mar 2023 17:07:14 -0800 (PST) Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32412l9X029856; Sat, 4 Mar 2023 01:06:59 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=Sm2Y3RjsWU4qjbU43RtEqS9/lvsRgOvN0fRS0+JIr+0=; b=bO/Z0MSeTY7o0PRRi3P8BOFNzIE2cHcgzvWzAiTSF+GLm7cCfIAWCEyD6Y4bZ1Tx0v2D bacguc+GJymir3BTgO9lZhOGbgL4PMWZSY7v8bJGvp/M0MlgGMLietZbgiinFR2h2CTC y/J3cMGhBvwXtUJaJOcwwCmThcrMiXmmS1GwoIrhHKwvqJu3TyZ/cQPNtk+S6lyLZOF/ TPa1bzgcw5T+flgrkWKLEoNcBjYp87HqCg5Kh2D0N/WwRYXJHdtzD3atvZMw/l8k2Jyi chbjHM7uHpgCP3UCVPmhMV0bUBFgtg817MxQUDo3JFJF1c+22hkjd1vjL3lAHsYj86Oi 7w== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3pu48ma2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:06:59 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32416we8018666 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:06:59 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.986.41; Fri, 3 Mar 2023 17:06:58 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Jonathan Corbet , "Elliot Berman" , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 10/26] gunyah: vm_mgr: Introduce basic VM Manager Date: Fri, 3 Mar 2023 17:06:16 -0800 Message-ID: <20230304010632.2127470-11-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: iS9tx9OEElwnFj0GyCT77BDXvGOSUsno X-Proofpoint-ORIG-GUID: iS9tx9OEElwnFj0GyCT77BDXvGOSUsno X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 priorityscore=1501 suspectscore=0 phishscore=0 malwarescore=0 bulkscore=0 spamscore=0 impostorscore=0 mlxscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah VM manager is a kernel moduel which exposes an interface to Gunyah userspace to load, run, and interact with other Gunyah virtual machines. The interface is a character device at /dev/gunyah. Add a basic VM manager driver. Upcoming patches will add more ioctls into this driver. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/virt/gunyah/Makefile | 2 +- drivers/virt/gunyah/rsc_mgr.c | 38 +++++- drivers/virt/gunyah/vm_mgr.c | 116 ++++++++++++++++++ drivers/virt/gunyah/vm_mgr.h | 23 ++++ include/uapi/linux/gunyah.h | 23 ++++ 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 drivers/virt/gunyah/vm_mgr.c create mode 100644 drivers/virt/gunyah/vm_mgr.h create mode 100644 include/uapi/linux/gunyah.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index 0a1882e296ae..2513324ae7be 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -137,6 +137,7 @@ Code Seq# Include File = Comments 'F' DD video/sstfb.h confl= ict! 'G' 00-3F drivers/misc/sgi-gru/grulib.h confl= ict! 'G' 00-0F xen/gntalloc.h, xen/gntdev.h confl= ict! +'G' 00-0f linux/gunyah.h confl= ict! 'H' 00-7F linux/hiddev.h confl= ict! 'H' 00-0F linux/hidraw.h confl= ict! 'H' 01 linux/mei.h confl= ict! diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index de29769f2f3f..03951cf82023 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -2,5 +2,5 @@ =20 obj-$(CONFIG_GUNYAH) +=3D gunyah.o =20 -gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o +gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o diff --git a/drivers/virt/gunyah/rsc_mgr.c b/drivers/virt/gunyah/rsc_mgr.c index 67813c9a52db..d7ce692d0067 100644 --- a/drivers/virt/gunyah/rsc_mgr.c +++ b/drivers/virt/gunyah/rsc_mgr.c @@ -15,8 +15,10 @@ #include #include #include +#include =20 #include "rsc_mgr.h" +#include "vm_mgr.h" =20 #define RM_RPC_API_VERSION_MASK GENMASK(3, 0) #define RM_RPC_HEADER_WORDS_MASK GENMASK(7, 4) @@ -129,6 +131,7 @@ struct gh_rm_connection { * @cache: cache for allocating Tx messages * @send_lock: synchronization to allow only one request to be sent at a t= ime * @nh: notifier chain for clients interested in RM notification messages + * @miscdev: /dev/gunyah */ struct gh_rm { struct device *dev; @@ -145,6 +148,8 @@ struct gh_rm { struct kmem_cache *cache; struct mutex send_lock; struct blocking_notifier_head nh; + + struct miscdevice miscdev; }; =20 /** @@ -593,6 +598,21 @@ void gh_rm_put(struct gh_rm *rm) } EXPORT_SYMBOL_GPL(gh_rm_put); =20 +static long gh_dev_ioctl(struct file *filp, unsigned int cmd, unsigned lon= g arg) +{ + struct miscdevice *miscdev =3D filp->private_data; + struct gh_rm *rm =3D container_of(miscdev, struct gh_rm, miscdev); + + return gh_dev_vm_mgr_ioctl(rm, cmd, arg); +} + +static const struct file_operations gh_dev_fops =3D { + .owner =3D THIS_MODULE, + .unlocked_ioctl =3D gh_dev_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, + .llseek =3D noop_llseek, +}; + static int gh_msgq_platform_probe_direction(struct platform_device *pdev, = bool tx, struct gh_resource *ghrsc) { @@ -651,7 +671,22 @@ static int gh_rm_drv_probe(struct platform_device *pde= v) rm->msgq_client.rx_callback =3D gh_rm_msgq_rx_data; rm->msgq_client.tx_done =3D gh_rm_msgq_tx_done; =20 - return gh_msgq_init(&pdev->dev, &rm->msgq, &rm->msgq_client, &rm->tx_ghrs= c, &rm->rx_ghrsc); + ret =3D gh_msgq_init(&pdev->dev, &rm->msgq, &rm->msgq_client, &rm->tx_ghr= sc, &rm->rx_ghrsc); + if (ret) + goto err_cache; + + rm->miscdev.name =3D "gunyah"; + rm->miscdev.minor =3D MISC_DYNAMIC_MINOR; + rm->miscdev.fops =3D &gh_dev_fops; + + ret =3D misc_register(&rm->miscdev); + if (ret) + goto err_msgq; + + return 0; +err_msgq: + mbox_free_channel(gh_msgq_chan(&rm->msgq)); + gh_msgq_remove(&rm->msgq); err_cache: kmem_cache_destroy(rm->cache); return ret; @@ -661,6 +696,7 @@ static int gh_rm_drv_remove(struct platform_device *pde= v) { struct gh_rm *rm =3D platform_get_drvdata(pdev); =20 + misc_deregister(&rm->miscdev); mbox_free_channel(gh_msgq_chan(&rm->msgq)); gh_msgq_remove(&rm->msgq); kmem_cache_destroy(rm->cache); diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c new file mode 100644 index 000000000000..dbacf36af72d --- /dev/null +++ b/drivers/virt/gunyah/vm_mgr.c @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#define pr_fmt(fmt) "gh_vm_mgr: " fmt + +#include +#include +#include +#include +#include + +#include + +#include "vm_mgr.h" + +static void gh_vm_free(struct work_struct *work) +{ + struct gh_vm *ghvm =3D container_of(work, struct gh_vm, free_work); + int ret; + + ret =3D gh_rm_dealloc_vmid(ghvm->rm, ghvm->vmid); + if (ret) + pr_warn("Failed to deallocate vmid: %d\n", ret); + + put_gh_rm(ghvm->rm); + kfree(ghvm); +} + +static __must_check struct gh_vm *gh_vm_alloc(struct gh_rm *rm) +{ + struct gh_vm *ghvm; + int vmid; + + vmid =3D gh_rm_alloc_vmid(rm, 0); + if (vmid < 0) + return ERR_PTR(vmid); + + ghvm =3D kzalloc(sizeof(*ghvm), GFP_KERNEL); + if (!ghvm) { + gh_rm_dealloc_vmid(rm, vmid); + return ERR_PTR(-ENOMEM); + } + + ghvm->parent =3D gh_rm_get(rm); + ghvm->vmid =3D vmid; + ghvm->rm =3D rm; + + INIT_WORK(&ghvm->free_work, gh_vm_free); + + return ghvm; +} + +static int gh_vm_release(struct inode *inode, struct file *filp) +{ + struct gh_vm *ghvm =3D filp->private_data; + + /* VM will be reset and make RM calls which can interruptible sleep. + * Defer to a work so this thread can receive signal. + */ + schedule_work(&ghvm->free_work); + return 0; +} + +static const struct file_operations gh_vm_fops =3D { + .release =3D gh_vm_release, + .llseek =3D noop_llseek, +}; + +static long gh_dev_ioctl_create_vm(struct gh_rm *rm, unsigned long arg) +{ + struct gh_vm *ghvm; + struct file *file; + int fd, err; + + /* arg reserved for future use. */ + if (arg) + return -EINVAL; + + ghvm =3D gh_vm_alloc(rm); + if (IS_ERR(ghvm)) + return PTR_ERR(ghvm); + + fd =3D get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) { + err =3D fd; + goto err_destroy_vm; + } + + file =3D anon_inode_getfile("gunyah-vm", &gh_vm_fops, ghvm, O_RDWR); + if (IS_ERR(file)) { + err =3D PTR_ERR(file); + goto err_put_fd; + } + + fd_install(fd, file); + + return fd; + +err_put_fd: + put_unused_fd(fd); +err_destroy_vm: + gh_vm_free(&ghvm->free_work); + return err; +} + +long gh_dev_vm_mgr_ioctl(struct gh_rm *rm, unsigned int cmd, unsigned long= arg) +{ + switch (cmd) { + case GH_CREATE_VM: + return gh_dev_ioctl_create_vm(rm, arg); + default: + return -ENOIOCTLCMD; + } +} diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h new file mode 100644 index 000000000000..4b22fbcac91c --- /dev/null +++ b/drivers/virt/gunyah/vm_mgr.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#ifndef _GH_PRIV_VM_MGR_H +#define _GH_PRIV_VM_MGR_H + +#include + +#include + +long gh_dev_vm_mgr_ioctl(struct gh_rm *rm, unsigned int cmd, unsigned long= arg); + +struct gh_vm { + u16 vmid; + struct gh_rm *rm; + struct device *parent; + + struct work_struct free_work; +}; + +#endif diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h new file mode 100644 index 000000000000..10ba32d2b0a6 --- /dev/null +++ b/include/uapi/linux/gunyah.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#ifndef _UAPI_LINUX_GUNYAH +#define _UAPI_LINUX_GUNYAH + +/* + * Userspace interface for /dev/gunyah - gunyah based virtual machine + */ + +#include +#include + +#define GH_IOCTL_TYPE 'G' + +/* + * ioctls for /dev/gunyah fds: + */ +#define GH_CREATE_VM _IO(GH_IOCTL_TYPE, 0x0) /* Returns a Gunyah VM fd */ + +#endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF5E3C77B6C for ; Sat, 4 Mar 2023 01:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230045AbjCDBIP (ORCPT ); Fri, 3 Mar 2023 20:08:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbjCDBHa (ORCPT ); Fri, 3 Mar 2023 20:07:30 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BD736512D; Fri, 3 Mar 2023 17:07:15 -0800 (PST) Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240SGcd014291; Sat, 4 Mar 2023 01:07:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=N0zG4KTkSsLM5Hix5XNnz/Kya03Ez1XXKcQnMY4TbrU=; b=PWj7L920dDK3/VNrfu1Rfc0J4k8uuK4UsgpbWAxqjhrqAnwjYxNQ1LQOBc8OzTzSic98 OYt/l6Yeqqdfx1yfTHdepYDi2nzhHouFDlcJMp+cvwjoDA+VIdTS0NN0c3/9xHcPSbbW 8eQB7iuMjZ2vnVeUlsbFaCthzjRI0lDWzymF032eO48CiV1Xirxkv8SSfOWJ708nIYhA eeUQ+e1C5cTxHCpfblKQxGRN8MNvDjX2SbgYbTrVmq6L6xQHDUTTg2DToJDoDLe0bnYK CeUAF7EUKOQHYuI82+7LZFtNvpcLzpiyhc7LOgnnUEv5qytoiQzpTbwRbcTIlsVCVf3d vQ== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3dfrjf3v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:01 +0000 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 324170ij019226 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:00 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.986.41; Fri, 3 Mar 2023 17:06:59 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 11/26] gunyah: rsc_mgr: Add RPC for sharing memory Date: Fri, 3 Mar 2023 17:06:17 -0800 Message-ID: <20230304010632.2127470-12-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: sJ_CFPNmgghs5D7b7GCobCLD_MVYUquT X-Proofpoint-ORIG-GUID: sJ_CFPNmgghs5D7b7GCobCLD_MVYUquT X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 phishscore=0 adultscore=0 impostorscore=0 bulkscore=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 malwarescore=0 lowpriorityscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah resource manager provides API to manipulate stage 2 page tables. Manipulations are represented as a memory parcel. Memory parcels describe a list of memory regions (intermediate physical address and size), a list of new permissions for VMs, and the memory type (DDR or MMIO). Memory parcels are uniquely identified by a handle allocated by Gunyah. There are a few types of memory parcel sharing which Gunyah supports: - Sharing: the guest and host VM both have access - Lending: only the guest has access; host VM loses access - Donating: Permanently lent (not reclaimed even if guest shuts down) Memory parcels that have been shared or lent can be reclaimed by the host via an additional call. The reclaim operation restores the original access the host VM had to the memory parcel and removes the access to other VM. One point to note that memory parcels don't describe where in the guest VM the memory parcel should reside. The guest VM must accept the memory parcel either explicitly via a "gh_rm_mem_accept" call (not introduced here) or be configured to accept it automatically at boot. As the guest VM accepts the memory parcel, it also mentions the IPA it wants to place memory parcel. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/rsc_mgr_rpc.c | 223 ++++++++++++++++++++++++++++++ include/linux/gunyah_rsc_mgr.h | 48 +++++++ 2 files changed, 271 insertions(+) diff --git a/drivers/virt/gunyah/rsc_mgr_rpc.c b/drivers/virt/gunyah/rsc_mg= r_rpc.c index ffcb861a31b5..3df15ad5b97d 100644 --- a/drivers/virt/gunyah/rsc_mgr_rpc.c +++ b/drivers/virt/gunyah/rsc_mgr_rpc.c @@ -6,6 +6,12 @@ #include #include "rsc_mgr.h" =20 +/* Message IDs: Memory Management */ +#define GH_RM_RPC_MEM_LEND 0x51000012 +#define GH_RM_RPC_MEM_SHARE 0x51000013 +#define GH_RM_RPC_MEM_RECLAIM 0x51000015 +#define GH_RM_RPC_MEM_APPEND 0x51000018 + /* Message IDs: VM Management */ #define GH_RM_RPC_VM_ALLOC_VMID 0x56000001 #define GH_RM_RPC_VM_DEALLOC_VMID 0x56000002 @@ -22,6 +28,46 @@ struct gh_rm_vm_common_vmid_req { __le16 _padding; } __packed; =20 +/* Call: MEM_LEND, MEM_SHARE */ +#define GH_MEM_SHARE_REQ_FLAGS_APPEND BIT(1) + +struct gh_rm_mem_share_req_header { + u8 mem_type; + u8 _padding0; + u8 flags; + u8 _padding1; + __le32 label; +} __packed; + +struct gh_rm_mem_share_req_acl_section { + __le32 n_entries; + struct gh_rm_mem_acl_entry entries[]; +}; + +struct gh_rm_mem_share_req_mem_section { + __le16 n_entries; + __le16 _padding; + struct gh_rm_mem_entry entries[]; +}; + +/* Call: MEM_RELEASE */ +struct gh_rm_mem_release_req { + __le32 mem_handle; + u8 flags; /* currently not used */ + u8 _padding0; + __le16 _padding1; +} __packed; + +/* Call: MEM_APPEND */ +#define GH_MEM_APPEND_REQ_FLAGS_END BIT(0) + +struct gh_rm_mem_append_req_header { + __le32 mem_handle; + u8 flags; + u8 _padding0; + __le16 _padding1; +} __packed; + /* Call: VM_ALLOC */ struct gh_rm_vm_alloc_vmid_resp { __le16 vmid; @@ -51,6 +97,8 @@ struct gh_rm_vm_config_image_req { __le64 dtb_size; } __packed; =20 +#define GH_RM_MAX_MEM_ENTRIES 512 + /* * Several RM calls take only a VMID as a parameter and give only standard * response back. Deduplicate boilerplate code by using this common call. @@ -64,6 +112,181 @@ static int gh_rm_common_vmid_call(struct gh_rm *rm, u3= 2 message_id, u16 vmid) return gh_rm_call(rm, message_id, &req_payload, sizeof(req_payload), NULL= , NULL); } =20 +static int _gh_rm_mem_append(struct gh_rm *rm, u32 mem_handle, bool end_ap= pend, + struct gh_rm_mem_entry *mem_entries, size_t n_mem_entries) +{ + struct gh_rm_mem_share_req_mem_section *mem_section; + struct gh_rm_mem_append_req_header *req_header; + size_t msg_size =3D 0; + void *msg; + int ret; + + msg_size +=3D sizeof(struct gh_rm_mem_append_req_header); + msg_size +=3D struct_size(mem_section, entries, n_mem_entries); + + msg =3D kzalloc(msg_size, GFP_KERNEL); + if (!msg) + return -ENOMEM; + + req_header =3D msg; + mem_section =3D (void *)req_header + sizeof(struct gh_rm_mem_append_req_h= eader); + + req_header->mem_handle =3D cpu_to_le32(mem_handle); + if (end_append) + req_header->flags |=3D GH_MEM_APPEND_REQ_FLAGS_END; + + mem_section->n_entries =3D cpu_to_le16(n_mem_entries); + memcpy(mem_section->entries, mem_entries, sizeof(*mem_entries) * n_mem_en= tries); + + ret =3D gh_rm_call(rm, GH_RM_RPC_MEM_APPEND, msg, msg_size, NULL, NULL); + kfree(msg); + + return ret; +} + +static int gh_rm_mem_append(struct gh_rm *rm, u32 mem_handle, + struct gh_rm_mem_entry *mem_entries, size_t n_mem_entries) +{ + bool end_append; + int ret =3D 0; + size_t n; + + while (n_mem_entries) { + if (n_mem_entries > GH_RM_MAX_MEM_ENTRIES) { + end_append =3D false; + n =3D GH_RM_MAX_MEM_ENTRIES; + } else { + end_append =3D true; + n =3D n_mem_entries; + } + + ret =3D _gh_rm_mem_append(rm, mem_handle, end_append, mem_entries, n); + if (ret) + break; + + mem_entries +=3D n; + n_mem_entries -=3D n; + } + + return ret; +} + +static int gh_rm_mem_lend_common(struct gh_rm *rm, u32 message_id, struct = gh_rm_mem_parcel *p) +{ + size_t msg_size =3D 0, initial_mem_entries =3D p->n_mem_entries, resp_siz= e; + struct gh_rm_mem_share_req_acl_section *acl_section; + struct gh_rm_mem_share_req_mem_section *mem_section; + struct gh_rm_mem_share_req_header *req_header; + u32 *attr_section; + __le32 *resp; + void *msg; + int ret; + + if (!p->acl_entries || !p->n_acl_entries || !p->mem_entries || !p->n_mem_= entries || + p->n_acl_entries > U8_MAX || p->mem_handle !=3D GH_MEM_HANDLE_INVAL) + return -EINVAL; + + if (initial_mem_entries > GH_RM_MAX_MEM_ENTRIES) + initial_mem_entries =3D GH_RM_MAX_MEM_ENTRIES; + + /* The format of the message goes: + * request header + * ACL entries (which VMs get what kind of access to this memory parcel) + * Memory entries (list of memory regions to share) + * Memory attributes (currently unused, we'll hard-code the size to 0) + */ + msg_size +=3D sizeof(struct gh_rm_mem_share_req_header); + msg_size +=3D struct_size(acl_section, entries, p->n_acl_entries); + msg_size +=3D struct_size(mem_section, entries, initial_mem_entries); + msg_size +=3D sizeof(u32); /* for memory attributes, currently unused */ + + msg =3D kzalloc(msg_size, GFP_KERNEL); + if (!msg) + return -ENOMEM; + + req_header =3D msg; + acl_section =3D (void *)req_header + sizeof(*req_header); + mem_section =3D (void *)acl_section + struct_size(acl_section, entries, p= ->n_acl_entries); + attr_section =3D (void *)mem_section + struct_size(mem_section, entries, = initial_mem_entries); + + req_header->mem_type =3D p->mem_type; + if (initial_mem_entries !=3D p->n_mem_entries) + req_header->flags |=3D GH_MEM_SHARE_REQ_FLAGS_APPEND; + req_header->label =3D cpu_to_le32(p->label); + + acl_section->n_entries =3D cpu_to_le32(p->n_acl_entries); + memcpy(acl_section->entries, p->acl_entries, sizeof(*(p->acl_entries)) * = p->n_acl_entries); + + mem_section->n_entries =3D cpu_to_le16(initial_mem_entries); + memcpy(mem_section->entries, p->mem_entries, + sizeof(*(p->mem_entries)) * initial_mem_entries); + + /* Set n_entries for memory attribute section to 0 */ + *attr_section =3D 0; + + ret =3D gh_rm_call(rm, message_id, msg, msg_size, (void **)&resp, &resp_s= ize); + kfree(msg); + + if (ret) + return ret; + + p->mem_handle =3D le32_to_cpu(*resp); + + if (initial_mem_entries !=3D p->n_mem_entries) { + ret =3D gh_rm_mem_append(rm, p->mem_handle, + &p->mem_entries[initial_mem_entries], + p->n_mem_entries - initial_mem_entries); + if (ret) { + gh_rm_mem_reclaim(rm, p); + p->mem_handle =3D GH_MEM_HANDLE_INVAL; + } + } + + kfree(resp); + return ret; +} + +/** + * gh_rm_mem_lend() - Lend memory to other virtual machines. + * @rm: Handle to a Gunyah resource manager + * @parcel: Package the memory information of the memory to be lent. + * + * Lending removes Linux's access to the memory while the memory parcel is= lent. + */ +int gh_rm_mem_lend(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel) +{ + return gh_rm_mem_lend_common(rm, GH_RM_RPC_MEM_LEND, parcel); +} + + +/** + * gh_rm_mem_share() - Share memory with other virtual machines. + * @rm: Handle to a Gunyah resource manager + * @parcel: Package the memory information of the memory to be shared. + * + * Sharing keeps Linux's access to the memory while the memory parcel is s= hared. + */ +int gh_rm_mem_share(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel) +{ + return gh_rm_mem_lend_common(rm, GH_RM_RPC_MEM_SHARE, parcel); +} + +/** + * gh_rm_mem_reclaim() - Reclaim a memory parcel + * @rm: Handle to a Gunyah resource manager + * @parcel: Package the memory information of the memory to be reclaimed. + * + * RM maps the associated memory back into the stage-2 page tables of the = owner VM. + */ +int gh_rm_mem_reclaim(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel) +{ + struct gh_rm_mem_release_req req =3D { + .mem_handle =3D cpu_to_le32(parcel->mem_handle), + }; + + return gh_rm_call(rm, GH_RM_RPC_MEM_RECLAIM, &req, sizeof(req), NULL, NUL= L); +} + /** * gh_rm_alloc_vmid() - Allocate a new VM in Gunyah. Returns the VM identi= fier. * @rm: Handle to a Gunyah resource manager diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index 6a2f434e67f7..88a429dad09e 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -11,6 +11,7 @@ #include =20 #define GH_VMID_INVAL U16_MAX +#define GH_MEM_HANDLE_INVAL U32_MAX =20 struct gh_rm; int gh_rm_notifier_register(struct gh_rm *rm, struct notifier_block *nb); @@ -51,7 +52,54 @@ struct gh_rm_vm_status_payload { =20 #define GH_RM_NOTIFICATION_VM_STATUS 0x56100008 =20 +#define GH_RM_ACL_X BIT(0) +#define GH_RM_ACL_W BIT(1) +#define GH_RM_ACL_R BIT(2) + +struct gh_rm_mem_acl_entry { + __le16 vmid; + u8 perms; + u8 reserved; +} __packed; + +struct gh_rm_mem_entry { + __le64 ipa_base; + __le64 size; +} __packed; + +enum gh_rm_mem_type { + GH_RM_MEM_TYPE_NORMAL =3D 0, + GH_RM_MEM_TYPE_IO =3D 1, +}; + +/* + * struct gh_rm_mem_parcel - Package info about memory to be lent/shared/d= onated/reclaimed + * @mem_type: The type of memory: normal (DDR) or IO + * @label: An client-specified identifier which can be used by the other V= Ms to identify the purpose + * of the memory parcel. + * @acl_entries: An array of access control entries. Each entry specifies = a VM and what access + * is allowed for the memory parcel. + * @n_acl_entries: Count of the number of entries in the `acl_entries` arr= ay. + * @mem_entries: An list of regions to be associated with the memory parce= l. Addresses should be + * (intermediate) physical addresses from Linux's perspectiv= e. + * @n_mem_entries: Count of the number of entries in the `mem_entries` arr= ay. + * @mem_handle: On success, filled with memory handle that RM allocates fo= r this memory parcel + */ +struct gh_rm_mem_parcel { + enum gh_rm_mem_type mem_type; + u32 label; + size_t n_acl_entries; + struct gh_rm_mem_acl_entry *acl_entries; + size_t n_mem_entries; + struct gh_rm_mem_entry *mem_entries; + u32 mem_handle; +}; + /* RPC Calls */ +int gh_rm_mem_lend(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel); +int gh_rm_mem_share(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel); +int gh_rm_mem_reclaim(struct gh_rm *rm, struct gh_rm_mem_parcel *parcel); + int gh_rm_alloc_vmid(struct gh_rm *rm, u16 vmid); int gh_rm_dealloc_vmid(struct gh_rm *rm, u16 vmid); int gh_rm_vm_reset(struct gh_rm *rm, u16 vmid); --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CBDEC76196 for ; Sat, 4 Mar 2023 01:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230118AbjCDBIF (ORCPT ); Fri, 3 Mar 2023 20:08:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230015AbjCDBHd (ORCPT ); Fri, 3 Mar 2023 20:07:33 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D6C069070; Fri, 3 Mar 2023 17:07:17 -0800 (PST) Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 324124Uf012689; Sat, 4 Mar 2023 01:07:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=5voBfN4UfwS1Tsnhw5B4L1qKGGC+z5LiXjYwrWjVKeI=; b=WioLy2UQAy8mMYu9+kYmzuGpVczWdLcqVgkHRQUfVZikmaCx4Q+oycctmYi4T+a0fVYZ pa8984ufIWpFIAAUrHfs+JxQXsVLsitEBV+P+x/6k2CKmEQb6MylwxaF5FUiQbnOrdR4 H6B23dl2b1TW4XNy8BIXR2Klnq6HWcJDYvgSd3kNQhiDUnc8CH9yEfngZYZb4jrVE31+ +ZcTrEImQA998rZZGzxQKLYO335LiRkcezRbSg/FpDS/bwsrSt+1cFjJP9oNN515OYCW QuuW1ALuvU0ITnKb0mM+xxnQJSX1vifK6o/2e3p7jgQHGJUR6aEQGu7WI0KewSItIHlf vw== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3bsbtreh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:02 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 324172SD025881 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:02 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.986.41; Fri, 3 Mar 2023 17:07:01 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 12/26] gunyah: vm_mgr: Add/remove user memory regions Date: Fri, 3 Mar 2023 17:06:18 -0800 Message-ID: <20230304010632.2127470-13-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: 623eFvYYF9RxhFnsOCc_wPZ3WEzvhNPz X-Proofpoint-ORIG-GUID: 623eFvYYF9RxhFnsOCc_wPZ3WEzvhNPz X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 impostorscore=0 suspectscore=0 bulkscore=0 clxscore=1015 adultscore=0 priorityscore=1501 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When launching a virtual machine, Gunyah userspace allocates memory for the guest and informs Gunyah about these memory regions through SET_USER_MEMORY_REGION ioctl. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Makefile | 2 +- drivers/virt/gunyah/vm_mgr.c | 44 ++++++ drivers/virt/gunyah/vm_mgr.h | 25 ++++ drivers/virt/gunyah/vm_mgr_mm.c | 229 ++++++++++++++++++++++++++++++++ include/uapi/linux/gunyah.h | 29 ++++ 5 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/gunyah/vm_mgr_mm.c diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index 03951cf82023..ff8bc4925392 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -2,5 +2,5 @@ =20 obj-$(CONFIG_GUNYAH) +=3D gunyah.o =20 -gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o +gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_mgr_mm.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index dbacf36af72d..e950274c6a53 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -18,8 +18,16 @@ static void gh_vm_free(struct work_struct *work) { struct gh_vm *ghvm =3D container_of(work, struct gh_vm, free_work); + struct gh_vm_mem *mapping, *tmp; int ret; =20 + mutex_lock(&ghvm->mm_lock); + list_for_each_entry_safe(mapping, tmp, &ghvm->memory_mappings, list) { + gh_vm_mem_reclaim(ghvm, mapping); + kfree(mapping); + } + mutex_unlock(&ghvm->mm_lock); + ret =3D gh_rm_dealloc_vmid(ghvm->rm, ghvm->vmid); if (ret) pr_warn("Failed to deallocate vmid: %d\n", ret); @@ -47,11 +55,44 @@ static __must_check struct gh_vm *gh_vm_alloc(struct gh= _rm *rm) ghvm->vmid =3D vmid; ghvm->rm =3D rm; =20 + mutex_init(&ghvm->mm_lock); + INIT_LIST_HEAD(&ghvm->memory_mappings); INIT_WORK(&ghvm->free_work, gh_vm_free); =20 return ghvm; } =20 +static long gh_vm_ioctl(struct file *filp, unsigned int cmd, unsigned long= arg) +{ + struct gh_vm *ghvm =3D filp->private_data; + void __user *argp =3D (void __user *)arg; + long r; + + switch (cmd) { + case GH_VM_SET_USER_MEM_REGION: { + struct gh_userspace_memory_region region; + + if (!gh_api_has_feature(GH_FEATURE_MEMEXTENT)) + return -EOPNOTSUPP; + + if (copy_from_user(®ion, argp, sizeof(region))) + return -EFAULT; + + /* All other flag bits are reserved for future use */ + if (region.flags & ~(GH_MEM_ALLOW_READ | GH_MEM_ALLOW_WRITE | GH_MEM_ALL= OW_EXEC)) + return -EINVAL; + + r =3D gh_vm_mem_alloc(ghvm, ®ion); + break; + } + default: + r =3D -ENOTTY; + break; + } + + return r; +} + static int gh_vm_release(struct inode *inode, struct file *filp) { struct gh_vm *ghvm =3D filp->private_data; @@ -64,6 +105,9 @@ static int gh_vm_release(struct inode *inode, struct fil= e *filp) } =20 static const struct file_operations gh_vm_fops =3D { + .owner =3D THIS_MODULE, + .unlocked_ioctl =3D gh_vm_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, .release =3D gh_vm_release, .llseek =3D noop_llseek, }; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 4b22fbcac91c..c9f6fa5478ed 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -7,17 +7,42 @@ #define _GH_PRIV_VM_MGR_H =20 #include +#include +#include +#include =20 #include =20 long gh_dev_vm_mgr_ioctl(struct gh_rm *rm, unsigned int cmd, unsigned long= arg); =20 +enum gh_vm_mem_share_type { + VM_MEM_SHARE, + VM_MEM_LEND, +}; + +struct gh_vm_mem { + struct list_head list; + enum gh_vm_mem_share_type share_type; + struct gh_rm_mem_parcel parcel; + + __u64 guest_phys_addr; + struct page **pages; + unsigned long npages; +}; + struct gh_vm { u16 vmid; struct gh_rm *rm; struct device *parent; =20 struct work_struct free_work; + struct mutex mm_lock; + struct list_head memory_mappings; }; =20 +int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region = *region); +void gh_vm_mem_reclaim(struct gh_vm *ghvm, struct gh_vm_mem *mapping); +int gh_vm_mem_free(struct gh_vm *ghvm, u32 label); +struct gh_vm_mem *gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32 label); + #endif diff --git a/drivers/virt/gunyah/vm_mgr_mm.c b/drivers/virt/gunyah/vm_mgr_m= m.c new file mode 100644 index 000000000000..db6f55cef37f --- /dev/null +++ b/drivers/virt/gunyah/vm_mgr_mm.c @@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#define pr_fmt(fmt) "gh_vm_mgr: " fmt + +#include +#include + +#include + +#include "vm_mgr.h" + +static struct gh_vm_mem *__gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32= label) + __must_hold(&ghvm->mm_lock) +{ + struct gh_vm_mem *mapping; + + list_for_each_entry(mapping, &ghvm->memory_mappings, list) + if (mapping->parcel.label =3D=3D label) + return mapping; + + return NULL; +} + +void gh_vm_mem_reclaim(struct gh_vm *ghvm, struct gh_vm_mem *mapping) + __must_hold(&ghvm->mm_lock) +{ + int i, ret =3D 0; + + if (mapping->parcel.mem_handle !=3D GH_MEM_HANDLE_INVAL) { + ret =3D gh_rm_mem_reclaim(ghvm->rm, &mapping->parcel); + if (ret) + pr_warn("Failed to reclaim memory parcel for label %d: %d\n", + mapping->parcel.label, ret); + } + + if (!ret) + for (i =3D 0; i < mapping->npages; i++) + unpin_user_page(mapping->pages[i]); + + kfree(mapping->pages); + kfree(mapping->parcel.acl_entries); + kfree(mapping->parcel.mem_entries); + + list_del(&mapping->list); +} + +struct gh_vm_mem *gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32 label) +{ + struct gh_vm_mem *mapping; + int ret; + + ret =3D mutex_lock_interruptible(&ghvm->mm_lock); + if (ret) + return ERR_PTR(ret); + + mapping =3D __gh_vm_mem_find_by_label(ghvm, label); + mutex_unlock(&ghvm->mm_lock); + + return mapping ? : ERR_PTR(-ENODEV); +} + +int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region = *region) +{ + struct gh_vm_mem *mapping, *tmp_mapping; + struct gh_rm_mem_entry *mem_entries; + phys_addr_t curr_page, prev_page; + struct gh_rm_mem_parcel *parcel; + int i, j, pinned, ret =3D 0; + size_t entry_size; + u16 vmid; + + if (!region->memory_size || !PAGE_ALIGNED(region->memory_size) || + !PAGE_ALIGNED(region->userspace_addr) || !PAGE_ALIGNED(region->guest_phy= s_addr)) + return -EINVAL; + + if (region->guest_phys_addr + region->memory_size < region->guest_phys_ad= dr) + return -EOVERFLOW; + + ret =3D mutex_lock_interruptible(&ghvm->mm_lock); + if (ret) + return ret; + + mapping =3D __gh_vm_mem_find_by_label(ghvm, region->label); + if (mapping) { + mutex_unlock(&ghvm->mm_lock); + return -EEXIST; + } + + mapping =3D kzalloc(sizeof(*mapping), GFP_KERNEL); + if (!mapping) { + mutex_unlock(&ghvm->mm_lock); + return -ENOMEM; + } + + mapping->parcel.label =3D region->label; + mapping->guest_phys_addr =3D region->guest_phys_addr; + mapping->npages =3D region->memory_size >> PAGE_SHIFT; + parcel =3D &mapping->parcel; + parcel->mem_handle =3D GH_MEM_HANDLE_INVAL; /* to be filled later by mem_= share/mem_lend */ + parcel->mem_type =3D GH_RM_MEM_TYPE_NORMAL; + + /* Check for overlap */ + list_for_each_entry(tmp_mapping, &ghvm->memory_mappings, list) { + if (!((mapping->guest_phys_addr + (mapping->npages << PAGE_SHIFT) <=3D + tmp_mapping->guest_phys_addr) || + (mapping->guest_phys_addr >=3D + tmp_mapping->guest_phys_addr + (tmp_mapping->npages << PAGE_SHIFT)))) { + ret =3D -EEXIST; + goto free_mapping; + } + } + + list_add(&mapping->list, &ghvm->memory_mappings); + + mapping->pages =3D kcalloc(mapping->npages, sizeof(*mapping->pages), GFP_= KERNEL); + if (!mapping->pages) { + ret =3D -ENOMEM; + mapping->npages =3D 0; /* update npages for reclaim */ + goto reclaim; + } + + pinned =3D pin_user_pages_fast(region->userspace_addr, mapping->npages, + FOLL_WRITE | FOLL_LONGTERM, mapping->pages); + if (pinned < 0) { + ret =3D pinned; + mapping->npages =3D 0; /* update npages for reclaim */ + goto reclaim; + } else if (pinned !=3D mapping->npages) { + ret =3D -EFAULT; + mapping->npages =3D pinned; /* update npages for reclaim */ + goto reclaim; + } + + parcel->n_acl_entries =3D 2; + mapping->share_type =3D VM_MEM_SHARE; + parcel->acl_entries =3D kcalloc(parcel->n_acl_entries, sizeof(*parcel->ac= l_entries), + GFP_KERNEL); + if (!parcel->acl_entries) { + ret =3D -ENOMEM; + goto reclaim; + } + + parcel->acl_entries[0].vmid =3D cpu_to_le16(ghvm->vmid); + + if (region->flags & GH_MEM_ALLOW_READ) + parcel->acl_entries[0].perms |=3D GH_RM_ACL_R; + if (region->flags & GH_MEM_ALLOW_WRITE) + parcel->acl_entries[0].perms |=3D GH_RM_ACL_W; + if (region->flags & GH_MEM_ALLOW_EXEC) + parcel->acl_entries[0].perms |=3D GH_RM_ACL_X; + + if (mapping->share_type =3D=3D VM_MEM_SHARE) { + ret =3D gh_rm_get_vmid(ghvm->rm, &vmid); + if (ret) + goto reclaim; + + parcel->acl_entries[1].vmid =3D cpu_to_le16(vmid); + /* Host assumed to have all these permissions. Gunyah will not + * grant new permissions if host actually had less than RWX + */ + parcel->acl_entries[1].perms |=3D GH_RM_ACL_R | GH_RM_ACL_W | GH_RM_ACL_= X; + } + + mem_entries =3D kcalloc(mapping->npages, sizeof(*mem_entries), GFP_KERNEL= ); + if (!mem_entries) { + ret =3D -ENOMEM; + goto reclaim; + } + + /* reduce number of entries by combining contiguous pages into single mem= ory entry */ + prev_page =3D page_to_phys(mapping->pages[0]); + mem_entries[0].ipa_base =3D cpu_to_le64(prev_page); + entry_size =3D PAGE_SIZE; + for (i =3D 1, j =3D 0; i < mapping->npages; i++) { + curr_page =3D page_to_phys(mapping->pages[i]); + if (curr_page - prev_page =3D=3D PAGE_SIZE) { + entry_size +=3D PAGE_SIZE; + } else { + mem_entries[j].size =3D cpu_to_le64(entry_size); + j++; + mem_entries[j].ipa_base =3D cpu_to_le64(curr_page); + entry_size =3D PAGE_SIZE; + } + + prev_page =3D curr_page; + } + mem_entries[j].size =3D cpu_to_le64(entry_size); + + parcel->n_mem_entries =3D j + 1; + parcel->mem_entries =3D kmemdup(mem_entries, sizeof(*mem_entries) * parce= l->n_mem_entries, + GFP_KERNEL); + kfree(mem_entries); + if (!parcel->mem_entries) { + ret =3D -ENOMEM; + goto reclaim; + } + + mutex_unlock(&ghvm->mm_lock); + return 0; +reclaim: + gh_vm_mem_reclaim(ghvm, mapping); +free_mapping: + kfree(mapping); + mutex_unlock(&ghvm->mm_lock); + return ret; +} + +int gh_vm_mem_free(struct gh_vm *ghvm, u32 label) +{ + struct gh_vm_mem *mapping; + int ret; + + ret =3D mutex_lock_interruptible(&ghvm->mm_lock); + if (ret) + return ret; + + mapping =3D __gh_vm_mem_find_by_label(ghvm, label); + if (!mapping) + goto out; + + gh_vm_mem_reclaim(ghvm, mapping); + kfree(mapping); +out: + mutex_unlock(&ghvm->mm_lock); + return ret; +} diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index 10ba32d2b0a6..a19207e3e065 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -20,4 +20,33 @@ */ #define GH_CREATE_VM _IO(GH_IOCTL_TYPE, 0x0) /* Returns a Gunyah VM fd */ =20 +/* + * ioctls for VM fds + */ + +#define GH_MEM_ALLOW_READ (1UL << 0) +#define GH_MEM_ALLOW_WRITE (1UL << 1) +#define GH_MEM_ALLOW_EXEC (1UL << 2) + +/** + * struct gh_userspace_memory_region - Userspace memory descripion for GH_= VM_SET_USER_MEM_REGION + * @label: Unique identifer to the region. + * @flags: Flags for memory parcel behavior + * @guest_phys_addr: Location of the memory region in guest's memory space= (page-aligned) + * @memory_size: Size of the region (page-aligned) + * @userspace_addr: Location of the memory region in caller (userspace)'s = memory + * + * See Documentation/virt/gunyah/vm-manager.rst for further details. + */ +struct gh_userspace_memory_region { + __u32 label; + __u32 flags; + __u64 guest_phys_addr; + __u64 memory_size; + __u64 userspace_addr; +}; + +#define GH_VM_SET_USER_MEM_REGION _IOW(GH_IOCTL_TYPE, 0x1, \ + struct gh_userspace_memory_region) + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8551CC77B70 for ; Sat, 4 Mar 2023 01:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230135AbjCDBIX (ORCPT ); Fri, 3 Mar 2023 20:08:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230006AbjCDBHd (ORCPT ); Fri, 3 Mar 2023 20:07:33 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E46069078; Fri, 3 Mar 2023 17:07:17 -0800 (PST) Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323NdeqR017178; Sat, 4 Mar 2023 01:07:04 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=MaVCwpT5GoBYhUfIWvF2v8THe49xZ8C+mZ8RNreqWGo=; b=K+BJ448nZkgGEv3RV5JB+oTGgLaKJgl0i+CVQZm/cUafjLlYjjKUbVwbLcFe7Rg9CvSm vuC/EWqrhjDeMnruajyDfU5CwaFEZ5QggmW4Veq2HhUdqzPnsGCOPuq9vrMMHNGliJnl eqKDEzAQm9UxUgGZQxmMwlAfoFmvqyjGCYOR+pANF29o/duwNWJgIO8lA1FWAWFqaNPd mK7E8pqVnBYKp9t/6oI0iWfa7HjezPp2SCbVMwr5DrhmiSV+kzXXXPsUHI5oGm/M/oo1 xceMrsssSyC1KANhODpnWmQIZbsz2N5OCMtlGAnBMO2QMS2cKBIE43Swg1nrEbw347er XA== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3dfrjf3x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:04 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 324173gY018726 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:03 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.986.41; Fri, 3 Mar 2023 17:07:02 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 13/26] gunyah: vm_mgr: Add ioctls to support basic non-proxy VM boot Date: Fri, 3 Mar 2023 17:06:19 -0800 Message-ID: <20230304010632.2127470-14-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: ZTwRCB7bU_EhQRaXBPnsYGMVGFJAzsIV X-Proofpoint-ORIG-GUID: ZTwRCB7bU_EhQRaXBPnsYGMVGFJAzsIV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 phishscore=0 adultscore=0 impostorscore=0 bulkscore=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 malwarescore=0 lowpriorityscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add remaining ioctls to support non-proxy VM boot: - Gunyah Resource Manager uses the VM's devicetree to configure the virtual machine. The location of the devicetree in the guest's virtual memory can be declared via the SET_DTB_CONFIG ioctl. - Trigger start of the virtual machine with VM_START ioctl. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman Reviewed-by: Srinivas Kandagatla --- drivers/virt/gunyah/vm_mgr.c | 243 ++++++++++++++++++++++++++++++-- drivers/virt/gunyah/vm_mgr.h | 10 ++ drivers/virt/gunyah/vm_mgr_mm.c | 23 +++ include/linux/gunyah_rsc_mgr.h | 6 + include/uapi/linux/gunyah.h | 13 ++ 5 files changed, 282 insertions(+), 13 deletions(-) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index e950274c6a53..299b9bb81edc 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -9,37 +9,118 @@ #include #include #include +#include #include =20 #include =20 #include "vm_mgr.h" =20 +static int gh_vm_rm_notification_status(struct gh_vm *ghvm, void *data) +{ + struct gh_rm_vm_status_payload *payload =3D data; + + if (payload->vmid !=3D ghvm->vmid) + return NOTIFY_OK; + + /* All other state transitions are synchronous to a corresponding RM call= */ + if (payload->vm_status =3D=3D GH_RM_VM_STATUS_RESET) { + down_write(&ghvm->status_lock); + ghvm->vm_status =3D payload->vm_status; + up_write(&ghvm->status_lock); + wake_up(&ghvm->vm_status_wait); + } + + return NOTIFY_DONE; +} + +static int gh_vm_rm_notification_exited(struct gh_vm *ghvm, void *data) +{ + struct gh_rm_vm_exited_payload *payload =3D data; + + if (payload->vmid !=3D ghvm->vmid) + return NOTIFY_OK; + + down_write(&ghvm->status_lock); + ghvm->vm_status =3D GH_RM_VM_STATUS_EXITED; + up_write(&ghvm->status_lock); + + return NOTIFY_DONE; +} + +static int gh_vm_rm_notification(struct notifier_block *nb, unsigned long = action, void *data) +{ + struct gh_vm *ghvm =3D container_of(nb, struct gh_vm, nb); + + switch (action) { + case GH_RM_NOTIFICATION_VM_STATUS: + return gh_vm_rm_notification_status(ghvm, data); + case GH_RM_NOTIFICATION_VM_EXITED: + return gh_vm_rm_notification_exited(ghvm, data); + default: + return NOTIFY_OK; + } +} + +static void gh_vm_stop(struct gh_vm *ghvm) +{ + int ret; + + down_write(&ghvm->status_lock); + if (ghvm->vm_status =3D=3D GH_RM_VM_STATUS_RUNNING) { + ret =3D gh_rm_vm_stop(ghvm->rm, ghvm->vmid); + if (ret) + dev_warn(ghvm->parent, "Failed to stop VM: %d\n", ret); + } + + ghvm->vm_status =3D GH_RM_VM_STATUS_EXITED; + up_write(&ghvm->status_lock); +} + static void gh_vm_free(struct work_struct *work) { struct gh_vm *ghvm =3D container_of(work, struct gh_vm, free_work); struct gh_vm_mem *mapping, *tmp; int ret; =20 - mutex_lock(&ghvm->mm_lock); - list_for_each_entry_safe(mapping, tmp, &ghvm->memory_mappings, list) { - gh_vm_mem_reclaim(ghvm, mapping); - kfree(mapping); - } - mutex_unlock(&ghvm->mm_lock); - - ret =3D gh_rm_dealloc_vmid(ghvm->rm, ghvm->vmid); - if (ret) - pr_warn("Failed to deallocate vmid: %d\n", ret); + switch (ghvm->vm_status) { + case GH_RM_VM_STATUS_RUNNING: + gh_vm_stop(ghvm); + fallthrough; + case GH_RM_VM_STATUS_INIT_FAILED: + case GH_RM_VM_STATUS_LOAD: + case GH_RM_VM_STATUS_EXITED: + mutex_lock(&ghvm->mm_lock); + list_for_each_entry_safe(mapping, tmp, &ghvm->memory_mappings, list) { + gh_vm_mem_reclaim(ghvm, mapping); + kfree(mapping); + } + mutex_unlock(&ghvm->mm_lock); + fallthrough; + case GH_RM_VM_STATUS_NO_STATE: + ret =3D gh_rm_dealloc_vmid(ghvm->rm, ghvm->vmid); + if (ret) + dev_warn(ghvm->parent, "Failed to deallocate vmid: %d\n", ret); + + gh_rm_notifier_unregister(ghvm->rm, &ghvm->nb); + gh_rm_put(ghvm->rm); + kfree(ghvm); + break; + default: + dev_err(ghvm->parent, "VM is unknown state: %d. VM will not be cleaned u= p.\n", + ghvm->vm_status); =20 - put_gh_rm(ghvm->rm); - kfree(ghvm); + gh_rm_notifier_unregister(ghvm->rm, &ghvm->nb); + gh_rm_put(ghvm->rm); + kfree(ghvm); + break; + } } =20 static __must_check struct gh_vm *gh_vm_alloc(struct gh_rm *rm) { struct gh_vm *ghvm; - int vmid; + int vmid, ret; =20 vmid =3D gh_rm_alloc_vmid(rm, 0); if (vmid < 0) @@ -55,13 +136,130 @@ static __must_check struct gh_vm *gh_vm_alloc(struct = gh_rm *rm) ghvm->vmid =3D vmid; ghvm->rm =3D rm; =20 + init_waitqueue_head(&ghvm->vm_status_wait); + ghvm->nb.notifier_call =3D gh_vm_rm_notification; + ret =3D gh_rm_notifier_register(rm, &ghvm->nb); + if (ret) { + gh_rm_put(rm); + gh_rm_dealloc_vmid(rm, vmid); + kfree(ghvm); + return ERR_PTR(ret); + } + mutex_init(&ghvm->mm_lock); INIT_LIST_HEAD(&ghvm->memory_mappings); + init_rwsem(&ghvm->status_lock); INIT_WORK(&ghvm->free_work, gh_vm_free); + ghvm->vm_status =3D GH_RM_VM_STATUS_LOAD; =20 return ghvm; } =20 +static int gh_vm_start(struct gh_vm *ghvm) +{ + struct gh_vm_mem *mapping; + u64 dtb_offset; + u32 mem_handle; + int ret; + + down_write(&ghvm->status_lock); + if (ghvm->vm_status !=3D GH_RM_VM_STATUS_LOAD) { + up_write(&ghvm->status_lock); + return 0; + } + + ghvm->vm_status =3D GH_RM_VM_STATUS_RESET; + + mutex_lock(&ghvm->mm_lock); + list_for_each_entry(mapping, &ghvm->memory_mappings, list) { + switch (mapping->share_type) { + case VM_MEM_LEND: + ret =3D gh_rm_mem_lend(ghvm->rm, &mapping->parcel); + break; + case VM_MEM_SHARE: + ret =3D gh_rm_mem_share(ghvm->rm, &mapping->parcel); + break; + } + if (ret) { + dev_warn(ghvm->parent, "Failed to %s parcel %d: %d\n", + mapping->share_type =3D=3D VM_MEM_LEND ? "lend" : "share", + mapping->parcel.label, + ret); + goto err; + } + } + mutex_unlock(&ghvm->mm_lock); + + mapping =3D gh_vm_mem_find_by_addr(ghvm, ghvm->dtb_config.guest_phys_addr, + ghvm->dtb_config.size); + if (!mapping) { + dev_warn(ghvm->parent, "Failed to find the memory_handle for DTB\n"); + ret =3D -EINVAL; + goto err; + } + + mem_handle =3D mapping->parcel.mem_handle; + dtb_offset =3D ghvm->dtb_config.guest_phys_addr - mapping->guest_phys_add= r; + + ret =3D gh_rm_vm_configure(ghvm->rm, ghvm->vmid, ghvm->auth, mem_handle, + 0, 0, dtb_offset, ghvm->dtb_config.size); + if (ret) { + dev_warn(ghvm->parent, "Failed to configure VM: %d\n", ret); + goto err; + } + + ret =3D gh_rm_vm_init(ghvm->rm, ghvm->vmid); + if (ret) { + dev_warn(ghvm->parent, "Failed to initialize VM: %d\n", ret); + goto err; + } + + ret =3D gh_rm_vm_start(ghvm->rm, ghvm->vmid); + if (ret) { + dev_warn(ghvm->parent, "Failed to start VM: %d\n", ret); + goto err; + } + + ghvm->vm_status =3D GH_RM_VM_STATUS_RUNNING; + up_write(&ghvm->status_lock); + return ret; +err: + ghvm->vm_status =3D GH_RM_VM_STATUS_INIT_FAILED; + /* gh_vm_free will handle releasing resources and reclaiming memory */ + up_write(&ghvm->status_lock); + return ret; +} + +static int gh_vm_ensure_started(struct gh_vm *ghvm) +{ + int ret; + + ret =3D down_read_interruptible(&ghvm->status_lock); + if (ret) + return ret; + + /* Unlikely because VM is typically started */ + if (unlikely(ghvm->vm_status =3D=3D GH_RM_VM_STATUS_LOAD)) { + up_read(&ghvm->status_lock); + ret =3D gh_vm_start(ghvm); + if (ret) + goto out; + /** gh_vm_start() is guaranteed to bring status out of + * GH_RM_VM_STATUS_LOAD, thus inifitely recursive call is not + * possible + */ + return gh_vm_ensure_started(ghvm); + } + + /* Unlikely because VM is typically running */ + if (unlikely(ghvm->vm_status !=3D GH_RM_VM_STATUS_RUNNING)) + ret =3D -ENODEV; + +out: + up_read(&ghvm->status_lock); + return ret; +} + static long gh_vm_ioctl(struct file *filp, unsigned int cmd, unsigned long= arg) { struct gh_vm *ghvm =3D filp->private_data; @@ -85,6 +283,25 @@ static long gh_vm_ioctl(struct file *filp, unsigned int= cmd, unsigned long arg) r =3D gh_vm_mem_alloc(ghvm, ®ion); break; } + case GH_VM_SET_DTB_CONFIG: { + struct gh_vm_dtb_config dtb_config; + + if (copy_from_user(&dtb_config, argp, sizeof(dtb_config))) + return -EFAULT; + + dtb_config.size =3D PAGE_ALIGN(dtb_config.size); + if (dtb_config.guest_phys_addr + dtb_config.size < dtb_config.guest_phys= _addr) + return -EOVERFLOW; + + ghvm->dtb_config =3D dtb_config; + + r =3D 0; + break; + } + case GH_VM_START: { + r =3D gh_vm_ensure_started(ghvm); + break; + } default: r =3D -ENOTTY; break; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index c9f6fa5478ed..26bcc2ae4478 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -10,6 +10,8 @@ #include #include #include +#include +#include =20 #include =20 @@ -34,6 +36,13 @@ struct gh_vm { u16 vmid; struct gh_rm *rm; struct device *parent; + enum gh_rm_vm_auth_mechanism auth; + struct gh_vm_dtb_config dtb_config; + + struct notifier_block nb; + enum gh_rm_vm_status vm_status; + wait_queue_head_t vm_status_wait; + struct rw_semaphore status_lock; =20 struct work_struct free_work; struct mutex mm_lock; @@ -44,5 +53,6 @@ int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspa= ce_memory_region *regio void gh_vm_mem_reclaim(struct gh_vm *ghvm, struct gh_vm_mem *mapping); int gh_vm_mem_free(struct gh_vm *ghvm, u32 label); struct gh_vm_mem *gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32 label); +struct gh_vm_mem *gh_vm_mem_find_by_addr(struct gh_vm *ghvm, u64 guest_phy= s_addr, u32 size); =20 #endif diff --git a/drivers/virt/gunyah/vm_mgr_mm.c b/drivers/virt/gunyah/vm_mgr_m= m.c index db6f55cef37f..6e1d2e8bddb7 100644 --- a/drivers/virt/gunyah/vm_mgr_mm.c +++ b/drivers/virt/gunyah/vm_mgr_mm.c @@ -47,6 +47,29 @@ void gh_vm_mem_reclaim(struct gh_vm *ghvm, struct gh_vm_= mem *mapping) list_del(&mapping->list); } =20 +struct gh_vm_mem *gh_vm_mem_find_by_addr(struct gh_vm *ghvm, u64 guest_phy= s_addr, u32 size) +{ + struct gh_vm_mem *mapping =3D NULL; + int ret; + + ret =3D mutex_lock_interruptible(&ghvm->mm_lock); + if (ret) + return ERR_PTR(ret); + + list_for_each_entry(mapping, &ghvm->memory_mappings, list) { + if (guest_phys_addr >=3D mapping->guest_phys_addr && + (guest_phys_addr + size <=3D mapping->guest_phys_addr + + (mapping->npages << PAGE_SHIFT))) { + goto unlock; + } + } + + mapping =3D NULL; +unlock: + mutex_unlock(&ghvm->mm_lock); + return mapping; +} + struct gh_vm_mem *gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32 label) { struct gh_vm_mem *mapping; diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index 88a429dad09e..8b0b46f28e39 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -29,6 +29,12 @@ struct gh_rm_vm_exited_payload { #define GH_RM_NOTIFICATION_VM_EXITED 0x56100001 =20 enum gh_rm_vm_status { + /** + * RM doesn't have a state where load partially failed because + * only Linux + */ + GH_RM_VM_STATUS_LOAD_FAILED =3D -1, + GH_RM_VM_STATUS_NO_STATE =3D 0, GH_RM_VM_STATUS_INIT =3D 1, GH_RM_VM_STATUS_READY =3D 2, diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index a19207e3e065..d6abd8605a2e 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -49,4 +49,17 @@ struct gh_userspace_memory_region { #define GH_VM_SET_USER_MEM_REGION _IOW(GH_IOCTL_TYPE, 0x1, \ struct gh_userspace_memory_region) =20 +/** + * struct gh_vm_dtb_config - Set the location of the VM's devicetree blob + * @guest_phys_addr: Address of the VM's devicetree in guest memory. + * @size: Maximum size of the devicetree. + */ +struct gh_vm_dtb_config { + __u64 guest_phys_addr; + __u64 size; +}; +#define GH_VM_SET_DTB_CONFIG _IOW(GH_IOCTL_TYPE, 0x2, struct gh_vm_dtb_con= fig) + +#define GH_VM_START _IO(GH_IOCTL_TYPE, 0x3) + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77696C6FD20 for ; Sat, 4 Mar 2023 01:08:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230023AbjCDBIB (ORCPT ); Fri, 3 Mar 2023 20:08:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230040AbjCDBHg (ORCPT ); Fri, 3 Mar 2023 20:07:36 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 576886B31A; Fri, 3 Mar 2023 17:07:19 -0800 (PST) Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323NjDqh003547; Sat, 4 Mar 2023 01:07:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=Y5//UadlEdV0a+5zcX8TQ4LlO7IHHl8LDnZbN/15rkM=; b=DmMtkSJXpmDNIw3XKIRtnqA7C4mY7tEaPFGnoGc2JbevtDcmwt+dR5n7OhWpLSyHLMEz ORzm8Y8uYUqkpmdw30s2hIRLOemp25S1BFHyBro6Lx6OKwuIHUns5TVYCi1aBszHj7FC P7QUd82FmaZY0TDIoOp1znhEx/TRuzBeD9/oX0ulSLi5Evjd9NbVQu6vw5Wk0QTiBum5 f4xOu2hrSz5d32y7kTPKRbcLyV3q+LaFj508xDd5rlwL5qaOM9PCu7wFRubKdTL26WNT CUYXQokUKNS9hiCZeiJFomNv2hVE5jjM3gh8BnIkxoyG5kE1z9vmkHKvz/UELG2oRlyH pw== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p36mybghm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:06 +0000 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 324175Jt019307 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:05 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.986.41; Fri, 3 Mar 2023 17:07:04 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 14/26] samples: Add sample userspace Gunyah VM Manager Date: Fri, 3 Mar 2023 17:06:20 -0800 Message-ID: <20230304010632.2127470-15-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: 4aFFUlhMHjADqvlSjcmx3j-iaZqyyyzq X-Proofpoint-ORIG-GUID: 4aFFUlhMHjADqvlSjcmx3j-iaZqyyyzq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 adultscore=0 clxscore=1015 mlxscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 priorityscore=1501 spamscore=0 malwarescore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a sample Gunyah VMM capable of launching a non-proxy scheduled VM. Signed-off-by: Elliot Berman --- samples/Kconfig | 10 ++ samples/Makefile | 1 + samples/gunyah/.gitignore | 2 + samples/gunyah/Makefile | 6 + samples/gunyah/gunyah_vmm.c | 270 +++++++++++++++++++++++++++++++++++ samples/gunyah/sample_vm.dts | 68 +++++++++ 6 files changed, 357 insertions(+) create mode 100644 samples/gunyah/.gitignore create mode 100644 samples/gunyah/Makefile create mode 100644 samples/gunyah/gunyah_vmm.c create mode 100644 samples/gunyah/sample_vm.dts diff --git a/samples/Kconfig b/samples/Kconfig index 30ef8bd48ba3..11070bf02bd7 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -273,6 +273,16 @@ config SAMPLE_CORESIGHT_SYSCFG This demonstrates how a user may create their own CoreSight configurations and easily load them into the system at runtime. =20 +config SAMPLE_GUNYAH + bool "Build example Gunyah Virtual Machine Manager" + depends on CC_CAN_LINK && HEADERS_INSTALL + depends on GUNYAH + help + Build an example Gunyah VMM userspace program capable of launching + a basic virtual machine under the Gunyah hypervisor. + This demonstrates how to create a virtual machine under the Gunyah + hypervisor. + source "samples/rust/Kconfig" =20 endif # SAMPLES diff --git a/samples/Makefile b/samples/Makefile index 7cb632ef88ee..a65555802642 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -37,3 +37,4 @@ obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) +=3D kmemleak/ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) +=3D coresight/ obj-$(CONFIG_SAMPLE_FPROBE) +=3D fprobe/ obj-$(CONFIG_SAMPLES_RUST) +=3D rust/ +obj-$(CONFIG_SAMPLE_GUNYAH) +=3D gunyah/ diff --git a/samples/gunyah/.gitignore b/samples/gunyah/.gitignore new file mode 100644 index 000000000000..adc7d1589fde --- /dev/null +++ b/samples/gunyah/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +/gunyah_vmm diff --git a/samples/gunyah/Makefile b/samples/gunyah/Makefile new file mode 100644 index 000000000000..faf14f9bb337 --- /dev/null +++ b/samples/gunyah/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only + +userprogs-always-y +=3D gunyah_vmm +dtb-y +=3D sample_vm.dtb + +userccflags +=3D -I usr/include diff --git a/samples/gunyah/gunyah_vmm.c b/samples/gunyah/gunyah_vmm.c new file mode 100644 index 000000000000..d0ba9c20cb13 --- /dev/null +++ b/samples/gunyah/gunyah_vmm.c @@ -0,0 +1,270 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __USE_GNU +#include + +#include + +struct vm_config { + int image_fd; + int dtb_fd; + int ramdisk_fd; + + uint64_t guest_base; + uint64_t guest_size; + + uint64_t image_offset; + off_t image_size; + uint64_t dtb_offset; + off_t dtb_size; + uint64_t ramdisk_offset; + off_t ramdisk_size; +}; + +static struct option options[] =3D { + { "help", no_argument, NULL, 'h' }, + { "image", required_argument, NULL, 'i' }, + { "dtb", required_argument, NULL, 'd' }, + { "ramdisk", optional_argument, NULL, 'r' }, + { "base", optional_argument, NULL, 'B' }, + { "size", optional_argument, NULL, 'S' }, + { "image_offset", optional_argument, NULL, 'I' }, + { "dtb_offset", optional_argument, NULL, 'D' }, + { "ramdisk_offset", optional_argument, NULL, 'R' }, + { } +}; + +static void print_help(char *cmd) +{ + printf("gunyah_vmm, a sample tool to launch Gunyah VMs\n" + "Usage: %s \n" + " --help, -h this menu\n" + " --image, -i VM image file to load (e.g. a kernel= Image) [Required]\n" + " --dtb, -d Devicetree to load [Required]\n" + " --ramdisk, -r Ramdisk to load\n" + " --base, -B
Set the base address of guest's m= emory [Default: 0x80000000]\n" + " --size, -S The number of bytes large to make= the guest's memory [Default: 0x6400000 (100 MB)]\n" + " --image_offset, -I Offset into guest memory to l= oad the VM image file [Default: 0x10000]\n" + " --dtb_offset, -D Offset into guest memory to l= oad the DTB [Default: 0]\n" + " --ramdisk_offset, -R Offset into guest memory t= o load a ramdisk [Default: 0x4600000]\n" + , cmd); +} + +int main(int argc, char **argv) +{ + int gunyah_fd, vm_fd, guest_fd; + struct gh_userspace_memory_region guest_mem_desc =3D { 0 }; + struct gh_vm_dtb_config dtb_config =3D { 0 }; + char *guest_mem; + struct vm_config config =3D { + /* Defaults good enough to boot static kernel and a basic ramdisk */ + .ramdisk_fd =3D -1, + .guest_base =3D 0x80000000, + .guest_size =3D 0x6400000, /* 100 MB */ + .image_offset =3D 0, + .dtb_offset =3D 0x45f0000, + .ramdisk_offset =3D 0x4600000, /* put at +70MB (30MB for ramdisk) */ + }; + struct stat st; + int opt, optidx, ret =3D 0; + long l; + + while ((opt =3D getopt_long(argc, argv, "hi:d:r:B:S:I:D:R:c:", options, &= optidx)) !=3D -1) { + switch (opt) { + case 'i': + config.image_fd =3D open(optarg, O_RDONLY | O_CLOEXEC); + if (config.image_fd < 0) { + perror("Failed to open image"); + return -1; + } + if (stat(optarg, &st) < 0) { + perror("Failed to stat image"); + return -1; + } + config.image_size =3D st.st_size; + break; + case 'd': + config.dtb_fd =3D open(optarg, O_RDONLY | O_CLOEXEC); + if (config.dtb_fd < 0) { + perror("Failed to open dtb"); + return -1; + } + if (stat(optarg, &st) < 0) { + perror("Failed to stat dtb"); + return -1; + } + config.dtb_size =3D st.st_size; + break; + case 'r': + config.ramdisk_fd =3D open(optarg, O_RDONLY | O_CLOEXEC); + if (config.ramdisk_fd < 0) { + perror("Failed to open ramdisk"); + return -1; + } + if (stat(optarg, &st) < 0) { + perror("Failed to stat ramdisk"); + return -1; + } + config.ramdisk_size =3D st.st_size; + break; + case 'B': + l =3D strtol(optarg, NULL, 0); + if (l =3D=3D LONG_MIN) { + perror("Failed to parse base address"); + return -1; + } + config.guest_base =3D l; + break; + case 'S': + l =3D strtol(optarg, NULL, 0); + if (l =3D=3D LONG_MIN) { + perror("Failed to parse memory size"); + return -1; + } + config.guest_size =3D l; + break; + case 'I': + l =3D strtol(optarg, NULL, 0); + if (l =3D=3D LONG_MIN) { + perror("Failed to parse image offset"); + return -1; + } + config.image_offset =3D l; + break; + case 'D': + l =3D strtol(optarg, NULL, 0); + if (l =3D=3D LONG_MIN) { + perror("Failed to parse dtb offset"); + return -1; + } + config.dtb_offset =3D l; + break; + case 'R': + l =3D strtol(optarg, NULL, 0); + if (l =3D=3D LONG_MIN) { + perror("Failed to parse ramdisk offset"); + return -1; + } + config.ramdisk_offset =3D l; + break; + case 'h': + print_help(argv[0]); + return 0; + default: + print_help(argv[0]); + return -1; + } + } + + if (!config.image_fd || !config.dtb_fd) { + print_help(argv[0]); + return -1; + } + + if (config.image_offset + config.image_size > config.guest_size) { + fprintf(stderr, "Image offset and size puts it outside guest memory. Mak= e image smaller or increase guest memory size.\n"); + return -1; + } + + if (config.dtb_offset + config.dtb_size > config.guest_size) { + fprintf(stderr, "DTB offset and size puts it outside guest memory. Make = dtb smaller or increase guest memory size.\n"); + return -1; + } + + if (config.ramdisk_fd =3D=3D -1 && + config.ramdisk_offset + config.ramdisk_size > config.guest_size) { + fprintf(stderr, "Ramdisk offset and size puts it outside guest memory. M= ake ramdisk smaller or increase guest memory size.\n"); + return -1; + } + + gunyah_fd =3D open("/dev/gunyah", O_RDWR | O_CLOEXEC); + if (gunyah_fd < 0) { + perror("Failed to open /dev/gunyah"); + return -1; + } + + vm_fd =3D ioctl(gunyah_fd, GH_CREATE_VM, 0); + if (vm_fd < 0) { + perror("Failed to create vm"); + return -1; + } + + guest_fd =3D memfd_create("guest_memory", MFD_CLOEXEC); + if (guest_fd < 0) { + perror("Failed to create guest memfd"); + return -1; + } + + if (ftruncate(guest_fd, config.guest_size) < 0) { + perror("Failed to grow guest memory"); + return -1; + } + + guest_mem =3D mmap(NULL, config.guest_size, PROT_READ | PROT_WRITE, MAP_S= HARED, guest_fd, 0); + if (guest_mem =3D=3D MAP_FAILED) { + perror("Not enough memory"); + return -1; + } + + if (read(config.image_fd, guest_mem + config.image_offset, config.image_s= ize) < 0) { + perror("Failed to read image into guest memory"); + return -1; + } + + if (read(config.dtb_fd, guest_mem + config.dtb_offset, config.dtb_size) <= 0) { + perror("Failed to read dtb into guest memory"); + return -1; + } + + if (config.ramdisk_fd > 0 && + read(config.ramdisk_fd, guest_mem + config.ramdisk_offset, + config.ramdisk_size) < 0) { + perror("Failed to read ramdisk into guest memory"); + return -1; + } + + guest_mem_desc.label =3D 0; + guest_mem_desc.flags =3D GH_MEM_ALLOW_READ | GH_MEM_ALLOW_WRITE | GH_MEM_= ALLOW_EXEC; + guest_mem_desc.guest_phys_addr =3D config.guest_base; + guest_mem_desc.memory_size =3D config.guest_size; + guest_mem_desc.userspace_addr =3D (__u64)guest_mem; + + if (ioctl(vm_fd, GH_VM_SET_USER_MEM_REGION, &guest_mem_desc) < 0) { + perror("Failed to register guest memory with VM"); + return -1; + } + + dtb_config.guest_phys_addr =3D config.guest_base + config.dtb_offset; + dtb_config.size =3D config.dtb_size; + if (ioctl(vm_fd, GH_VM_SET_DTB_CONFIG, &dtb_config) < 0) { + perror("Failed to set DTB configuration for VM"); + return -1; + } + + ret =3D ioctl(vm_fd, GH_VM_START); + if (ret) { + perror("GH_VM_START failed"); + return -1; + } + + while (1) + sleep(10); + + return 0; +} diff --git a/samples/gunyah/sample_vm.dts b/samples/gunyah/sample_vm.dts new file mode 100644 index 000000000000..293bbc0469c8 --- /dev/null +++ b/samples/gunyah/sample_vm.dts @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +/dts-v1/; + +/ { + #address-cells =3D <2>; + #size-cells =3D <2>; + interrupt-parent =3D <&intc>; + + chosen { + bootargs =3D "nokaslr"; + }; + + cpus { + #address-cells =3D <0x2>; + #size-cells =3D <0>; + + cpu@0 { + device_type =3D "cpu"; + compatible =3D "arm,armv8"; + reg =3D <0 0>; + }; + }; + + intc: interrupt-controller@3FFF0000 { + compatible =3D "arm,gic-v3"; + #interrupt-cells =3D <3>; + #address-cells =3D <2>; + #size-cells =3D <2>; + interrupt-controller; + reg =3D <0 0x3FFF0000 0 0x10000>, + <0 0x3FFD0000 0 0x20000>; + }; + + timer { + compatible =3D "arm,armv8-timer"; + always-on; + interrupts =3D <1 13 0x108>, + <1 14 0x108>, + <1 11 0x108>, + <1 10 0x108>; + clock-frequency =3D <19200000>; + }; + + gunyah-vm-config { + image-name =3D "linux_vm_0"; + + memory { + #address-cells =3D <2>; + #size-cells =3D <2>; + + base-address =3D <0 0x80000000>; + }; + + interrupts { + config =3D <&intc>; + }; + + vcpus { + affinity-map =3D < 0 >; + sched-priority =3D < (-1) >; + sched-timeslice =3D < 2000 >; + }; + }; +}; --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C14BC77B6E for ; Sat, 4 Mar 2023 01:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230072AbjCDBIU (ORCPT ); Fri, 3 Mar 2023 20:08:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230056AbjCDBHk (ORCPT ); Fri, 3 Mar 2023 20:07:40 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34F4767020; Fri, 3 Mar 2023 17:07:21 -0800 (PST) Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240Eb6e026084; Sat, 4 Mar 2023 01:07:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=MSHoHRblBk+A2PA/5L47JwVNoTORkbJ4zRXam65SI/o=; b=Xb422BGDRExs2GZl6TZnHT9aYNIN1ZX7bOWUdE4Vy6DfPI0eSEMMbhWhZiTNkfCG9teM 5KMmsT5IDnSB573n/0XhEGexyJ2VWSo/B6CgXLdSNrvXIDHvaD0YEH1MQFUXVVitnHkM fXem2TEiWqwzVkIzQKUwMlWuMFHkqF9PtLul1wkyNH2dm+x0bFQlXfwyBCzsqs3QE9hB z+bm9BMhssmKzX2YsoVIp7Nx6sETGZIoL5TWZbRSEpCrLM9EILyYCYPHyQ57iqKsh7jE PLyUTvScwjeAd+3K8actP5I5LUkARnDVBNXXK0Bb3Sb3ajvnk4eTlCLt2lIgR1+pkDOZ Yw== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3801ua8e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:07 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 324177s1032254 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:07 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.986.41; Fri, 3 Mar 2023 17:07:06 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 15/26] gunyah: rsc_mgr: Add platform ops on mem_lend/mem_reclaim Date: Fri, 3 Mar 2023 17:06:21 -0800 Message-ID: <20230304010632.2127470-16-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: mAznWiOQxfPYSXVBgJknRpPV-4N9wcKo X-Proofpoint-GUID: mAznWiOQxfPYSXVBgJknRpPV-4N9wcKo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 lowpriorityscore=0 priorityscore=1501 phishscore=0 adultscore=0 spamscore=0 bulkscore=0 mlxscore=0 suspectscore=0 clxscore=1015 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Qualcomm platforms, there is a firmware entity which controls access to physical pages. In order to share memory with another VM, this entity needs to be informed that the guest VM should have access to the memory. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Kconfig | 4 ++ drivers/virt/gunyah/Makefile | 1 + drivers/virt/gunyah/gunyah_platform_hooks.c | 80 +++++++++++++++++++++ drivers/virt/gunyah/rsc_mgr.h | 3 + drivers/virt/gunyah/rsc_mgr_rpc.c | 18 ++++- include/linux/gunyah_rsc_mgr.h | 17 +++++ 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 drivers/virt/gunyah/gunyah_platform_hooks.c diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index 1a737694c333..de815189dab6 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -4,6 +4,7 @@ config GUNYAH tristate "Gunyah Virtualization drivers" depends on ARM64 depends on MAILBOX + select GUNYAH_PLATFORM_HOOKS help The Gunyah drivers are the helper interfaces that run in a guest VM such as basic inter-VM IPC and signaling mechanisms, and higher level @@ -11,3 +12,6 @@ config GUNYAH =20 Say Y/M here to enable the drivers needed to interact in a Gunyah virtual environment. + +config GUNYAH_PLATFORM_HOOKS + tristate diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index ff8bc4925392..6b8f84dbfe0d 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 =20 obj-$(CONFIG_GUNYAH) +=3D gunyah.o +obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) +=3D gunyah_platform_hooks.o =20 gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_mgr_mm.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o diff --git a/drivers/virt/gunyah/gunyah_platform_hooks.c b/drivers/virt/gun= yah/gunyah_platform_hooks.c new file mode 100644 index 000000000000..60da0e154e98 --- /dev/null +++ b/drivers/virt/gunyah/gunyah_platform_hooks.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include + +#include "rsc_mgr.h" + +static struct gh_rm_platform_ops *rm_platform_ops; +static DECLARE_RWSEM(rm_platform_ops_lock); + +int gh_rm_platform_pre_mem_share(struct gh_rm *rm, struct gh_rm_mem_parcel= *mem_parcel) +{ + int ret =3D 0; + + down_read(&rm_platform_ops_lock); + if (rm_platform_ops && rm_platform_ops->pre_mem_share) + ret =3D rm_platform_ops->pre_mem_share(rm, mem_parcel); + up_read(&rm_platform_ops_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gh_rm_platform_pre_mem_share); + +int gh_rm_platform_post_mem_reclaim(struct gh_rm *rm, struct gh_rm_mem_par= cel *mem_parcel) +{ + int ret =3D 0; + + down_read(&rm_platform_ops_lock); + if (rm_platform_ops && rm_platform_ops->post_mem_reclaim) + ret =3D rm_platform_ops->post_mem_reclaim(rm, mem_parcel); + up_read(&rm_platform_ops_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gh_rm_platform_post_mem_reclaim); + +int gh_rm_register_platform_ops(struct gh_rm_platform_ops *platform_ops) +{ + int ret =3D 0; + + down_write(&rm_platform_ops_lock); + if (!rm_platform_ops) + rm_platform_ops =3D platform_ops; + else + ret =3D -EEXIST; + up_write(&rm_platform_ops_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gh_rm_register_platform_ops); + +void gh_rm_unregister_platform_ops(struct gh_rm_platform_ops *platform_ops) +{ + down_write(&rm_platform_ops_lock); + if (rm_platform_ops =3D=3D platform_ops) + rm_platform_ops =3D NULL; + up_write(&rm_platform_ops_lock); +} +EXPORT_SYMBOL_GPL(gh_rm_unregister_platform_ops); + +static void _devm_gh_rm_unregister_platform_ops(void *data) +{ + gh_rm_unregister_platform_ops(data); +} + +int devm_gh_rm_register_platform_ops(struct device *dev, struct gh_rm_plat= form_ops *ops) +{ + int ret; + + ret =3D gh_rm_register_platform_ops(ops); + if (ret) + return ret; + + return devm_add_action(dev, _devm_gh_rm_unregister_platform_ops, ops); +} +EXPORT_SYMBOL_GPL(devm_gh_rm_register_platform_ops); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Gunyah Platform Hooks"); diff --git a/drivers/virt/gunyah/rsc_mgr.h b/drivers/virt/gunyah/rsc_mgr.h index 3665ebc7b020..6838e736f361 100644 --- a/drivers/virt/gunyah/rsc_mgr.h +++ b/drivers/virt/gunyah/rsc_mgr.h @@ -13,4 +13,7 @@ struct gh_rm; int gh_rm_call(struct gh_rm *rsc_mgr, u32 message_id, void *req_buff, size= _t req_buf_size, void **resp_buf, size_t *resp_buf_size); =20 +int gh_rm_platform_pre_mem_share(struct gh_rm *rm, struct gh_rm_mem_parcel= *mem_parcel); +int gh_rm_platform_post_mem_reclaim(struct gh_rm *rm, struct gh_rm_mem_par= cel *mem_parcel); + #endif diff --git a/drivers/virt/gunyah/rsc_mgr_rpc.c b/drivers/virt/gunyah/rsc_mg= r_rpc.c index 3df15ad5b97d..733be4dc8dd2 100644 --- a/drivers/virt/gunyah/rsc_mgr_rpc.c +++ b/drivers/virt/gunyah/rsc_mgr_rpc.c @@ -204,6 +204,12 @@ static int gh_rm_mem_lend_common(struct gh_rm *rm, u32= message_id, struct gh_rm_ if (!msg) return -ENOMEM; =20 + ret =3D gh_rm_platform_pre_mem_share(rm, p); + if (ret) { + kfree(msg); + return ret; + } + req_header =3D msg; acl_section =3D (void *)req_header + sizeof(*req_header); mem_section =3D (void *)acl_section + struct_size(acl_section, entries, p= ->n_acl_entries); @@ -227,8 +233,10 @@ static int gh_rm_mem_lend_common(struct gh_rm *rm, u32= message_id, struct gh_rm_ ret =3D gh_rm_call(rm, message_id, msg, msg_size, (void **)&resp, &resp_s= ize); kfree(msg); =20 - if (ret) + if (ret) { + gh_rm_platform_post_mem_reclaim(rm, p); return ret; + } =20 p->mem_handle =3D le32_to_cpu(*resp); =20 @@ -283,8 +291,14 @@ int gh_rm_mem_reclaim(struct gh_rm *rm, struct gh_rm_m= em_parcel *parcel) struct gh_rm_mem_release_req req =3D { .mem_handle =3D cpu_to_le32(parcel->mem_handle), }; + int ret; + + ret =3D gh_rm_call(rm, GH_RM_RPC_MEM_RECLAIM, &req, sizeof(req), NULL, NU= LL); + /* Do not call platform mem reclaim hooks: the reclaim didn't happen*/ + if (ret) + return ret; =20 - return gh_rm_call(rm, GH_RM_RPC_MEM_RECLAIM, &req, sizeof(req), NULL, NUL= L); + return gh_rm_platform_post_mem_reclaim(rm, parcel); } =20 /** diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index 8b0b46f28e39..515087931a2b 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -145,4 +145,21 @@ int gh_rm_get_hyp_resources(struct gh_rm *rm, u16 vmid, struct gh_rm_hyp_resources **resources); int gh_rm_get_vmid(struct gh_rm *rm, u16 *vmid); =20 +struct gunyah_rm_platform_ops { + int (*pre_mem_share)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_parce= l); + int (*post_mem_reclaim)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_pa= rcel); +}; + +#if IS_ENABLED(CONFIG_GUNYAH_PLATFORM_HOOKS) +int gh_rm_register_platform_ops(struct gh_rm_platform_ops *platform_ops); +void gh_rm_unregister_platform_ops(struct gh_rm_platform_ops *platform_ops= ); +int devm_gh_rm_register_platform_ops(struct device *dev, struct gh_rm_plat= form_ops *ops); +#else +static inline int gh_rm_register_platform_ops(struct gh_rm_platform_ops *p= latform_ops) + { return 0; } +static inline void gh_rm_unregister_platform_ops(struct gh_rm_platform_ops= *platform_ops) { } +static inline int devm_gh_rm_register_platform_ops(struct device *dev, + struct gh_rm_platform_ops *ops) { return 0; } +#endif + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B759AC77B60 for ; Sat, 4 Mar 2023 01:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229907AbjCDBII (ORCPT ); Fri, 3 Mar 2023 20:08:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230054AbjCDBHk (ORCPT ); Fri, 3 Mar 2023 20:07:40 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD4F46B320; Fri, 3 Mar 2023 17:07:20 -0800 (PST) Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323NuXtr009343; Sat, 4 Mar 2023 01:07:09 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=eWmxb3IZ5TQifCieDXFnzJeJsGPL7l3OYuz4phDHOj4=; b=bert6cQlkYJYzjchLumj2/6tAkBNhylJ+3auqkwZ1QhfB0WPJkhfBd9XidBZH+HI0FSQ 0xM5bNYJaMno0xCe0Ff3G2HjbvEnNeBc4OJUhFlN3laO31j+1f0ZkDUgskVPulEzpVH1 21b13/gW9RfQbu21wfOYug2AgAh4xFWU/iuTG5SiICWV9DiTFEg7n8TXNLtz8azGHYAJ zUsYel+F+NuaMhXfXJKJcSFrJEtRtkbzn+gi4DpqiwOpBjnISRg0wWZkcghjhAh/tlE9 f5Hy3tRKalIwC0HicrGBKKwDchM/LYA5CINNBAH4hYnlSrqp1IqJPtamBdDOhfhzkGsx lg== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3c8htrc2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:09 +0000 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 324178xc019344 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:08 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.986.41; Fri, 3 Mar 2023 17:07:07 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Andy Gross , "Bjorn Andersson" , Konrad Dybcio , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 16/26] firmware: qcom_scm: Register Gunyah platform ops Date: Fri, 3 Mar 2023 17:06:22 -0800 Message-ID: <20230304010632.2127470-17-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: VuMJC0YlIYTeKEJ9x10zbLd46YuWVSWY X-Proofpoint-ORIG-GUID: VuMJC0YlIYTeKEJ9x10zbLd46YuWVSWY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 phishscore=0 malwarescore=0 mlxlogscore=999 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 suspectscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Qualcomm platforms have a firmware entity which performs access control to physical pages. Dynamically started Gunyah virtual machines use the QCOM_SCM_RM_MANAGED_VMID for access. Linux thus needs to assign access to the memory used by guest VMs. Gunyah doesn't do this operation for us since it is the current VM (typically VMID_HLOS) delegating the access and not Gunyah itself. Use the Gunyah platform ops to achieve this so that only Qualcomm platforms attempt to make the needed SCM calls. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/firmware/Kconfig | 2 + drivers/firmware/qcom_scm.c | 100 +++++++++++++++++++++++++++++++++ include/linux/gunyah_rsc_mgr.h | 2 +- 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig index b59e3041fd62..b888068ff6f2 100644 --- a/drivers/firmware/Kconfig +++ b/drivers/firmware/Kconfig @@ -214,6 +214,8 @@ config MTK_ADSP_IPC =20 config QCOM_SCM tristate + select VIRT_DRIVERS + select GUNYAH_PLATFORM_HOOKS =20 config QCOM_SCM_DOWNLOAD_MODE_DEFAULT bool "Qualcomm download mode enabled by default" diff --git a/drivers/firmware/qcom_scm.c b/drivers/firmware/qcom_scm.c index b95616b35bff..89a261a9e021 100644 --- a/drivers/firmware/qcom_scm.c +++ b/drivers/firmware/qcom_scm.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include "qcom_scm.h" =20 @@ -30,6 +31,9 @@ module_param(download_mode, bool, 0); #define SCM_HAS_IFACE_CLK BIT(1) #define SCM_HAS_BUS_CLK BIT(2) =20 +#define QCOM_SCM_RM_MANAGED_VMID 0x3A +#define QCOM_SCM_MAX_MANAGED_VMID 0x3F + struct qcom_scm { struct device *dev; struct clk *core_clk; @@ -1299,6 +1303,99 @@ int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_r= eg, u32 payload_val, } EXPORT_SYMBOL(qcom_scm_lmh_dcvsh); =20 +static int qcom_scm_gh_rm_pre_mem_share(struct gh_rm *rm, struct gh_rm_mem= _parcel *mem_parcel) +{ + struct qcom_scm_vmperm *new_perms; + u64 src, src_cpy; + int ret =3D 0, i, n; + u16 vmid; + + new_perms =3D kcalloc(mem_parcel->n_acl_entries, sizeof(*new_perms), GFP_= KERNEL); + if (!new_perms) + return -ENOMEM; + + for (n =3D 0; n < mem_parcel->n_acl_entries; n++) { + vmid =3D le16_to_cpu(mem_parcel->acl_entries[n].vmid); + if (vmid <=3D QCOM_SCM_MAX_MANAGED_VMID) + new_perms[n].vmid =3D vmid; + else + new_perms[n].vmid =3D QCOM_SCM_RM_MANAGED_VMID; + if (mem_parcel->acl_entries[n].perms & GH_RM_ACL_X) + new_perms[n].perm |=3D QCOM_SCM_PERM_EXEC; + if (mem_parcel->acl_entries[n].perms & GH_RM_ACL_W) + new_perms[n].perm |=3D QCOM_SCM_PERM_WRITE; + if (mem_parcel->acl_entries[n].perms & GH_RM_ACL_R) + new_perms[n].perm |=3D QCOM_SCM_PERM_READ; + } + + src =3D (1ull << QCOM_SCM_VMID_HLOS); + + for (i =3D 0; i < mem_parcel->n_mem_entries; i++) { + src_cpy =3D src; + ret =3D qcom_scm_assign_mem(le64_to_cpu(mem_parcel->mem_entries[i].ipa_b= ase), + le64_to_cpu(mem_parcel->mem_entries[i].size), + &src_cpy, new_perms, mem_parcel->n_acl_entries); + if (ret) { + src =3D 0; + for (n =3D 0; n < mem_parcel->n_acl_entries; n++) { + vmid =3D le16_to_cpu(mem_parcel->acl_entries[n].vmid); + if (vmid <=3D QCOM_SCM_MAX_MANAGED_VMID) + src |=3D (1ull << vmid); + else + src |=3D (1ull << QCOM_SCM_RM_MANAGED_VMID); + } + + new_perms[0].vmid =3D QCOM_SCM_VMID_HLOS; + + for (i--; i >=3D 0; i--) { + src_cpy =3D src; + WARN_ON_ONCE(qcom_scm_assign_mem( + le64_to_cpu(mem_parcel->mem_entries[i].ipa_base), + le64_to_cpu(mem_parcel->mem_entries[i].size), + &src_cpy, new_perms, 1)); + } + break; + } + } + + kfree(new_perms); + return ret; +} + +static int qcom_scm_gh_rm_post_mem_reclaim(struct gh_rm *rm, struct gh_rm_= mem_parcel *mem_parcel) +{ + struct qcom_scm_vmperm new_perms; + u64 src =3D 0, src_cpy; + int ret =3D 0, i, n; + u16 vmid; + + new_perms.vmid =3D QCOM_SCM_VMID_HLOS; + new_perms.perm =3D QCOM_SCM_PERM_EXEC | QCOM_SCM_PERM_WRITE | QCOM_SCM_PE= RM_READ; + + for (n =3D 0; n < mem_parcel->n_acl_entries; n++) { + vmid =3D le16_to_cpu(mem_parcel->acl_entries[n].vmid); + if (vmid <=3D QCOM_SCM_MAX_MANAGED_VMID) + src |=3D (1ull << vmid); + else + src |=3D (1ull << QCOM_SCM_RM_MANAGED_VMID); + } + + for (i =3D 0; i < mem_parcel->n_mem_entries; i++) { + src_cpy =3D src; + ret =3D qcom_scm_assign_mem(le64_to_cpu(mem_parcel->mem_entries[i].ipa_b= ase), + le64_to_cpu(mem_parcel->mem_entries[i].size), + &src_cpy, &new_perms, 1); + WARN_ON_ONCE(ret); + } + + return ret; +} + +static struct gh_rm_platform_ops qcom_scm_gh_rm_platform_ops =3D { + .pre_mem_share =3D qcom_scm_gh_rm_pre_mem_share, + .post_mem_reclaim =3D qcom_scm_gh_rm_post_mem_reclaim, +}; + static int qcom_scm_find_dload_address(struct device *dev, u64 *addr) { struct device_node *tcsr; @@ -1502,6 +1599,9 @@ static int qcom_scm_probe(struct platform_device *pde= v) if (download_mode) qcom_scm_set_download_mode(true); =20 + if (devm_gh_rm_register_platform_ops(&pdev->dev, &qcom_scm_gh_rm_platform= _ops)) + dev_warn(__scm->dev, "Gunyah RM platform ops were already registered\n"); + return 0; } =20 diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index 515087931a2b..acf8c1545a6c 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -145,7 +145,7 @@ int gh_rm_get_hyp_resources(struct gh_rm *rm, u16 vmid, struct gh_rm_hyp_resources **resources); int gh_rm_get_vmid(struct gh_rm *rm, u16 *vmid); =20 -struct gunyah_rm_platform_ops { +struct gh_rm_platform_ops { int (*pre_mem_share)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_parce= l); int (*post_mem_reclaim)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_pa= rcel); }; --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4062C678DB for ; Sat, 4 Mar 2023 01:10:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230149AbjCDBKO (ORCPT ); Fri, 3 Mar 2023 20:10:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229673AbjCDBJq (ORCPT ); Fri, 3 Mar 2023 20:09:46 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 905426B31A; Fri, 3 Mar 2023 17:08:49 -0800 (PST) Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323Ndesj017428; Sat, 4 Mar 2023 01:07:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=tu6+qvW/Zhi1XKBb+7JrOFesQl2cFoGev4zTJaArKOc=; b=AmOLrPdxeAja25DoJ4/dxS/kaRzh6+EHsJd9KeAoH6e3k1oAHbdMwllnnssA9X6xvJ+k rdKuEnMYK/gVpxaVL7uTNitSfPSURVskAARSivNqGHrykDb3Dj4JtWdGBURValEyI92N X0MFW52WcI5fWtbQ+L3D6pbsrn31bsh8SmIEmSDoYiP5JGdIjKgKqCV3KibY868k5lz3 6YnqV+m1nR4Z8p5gKy92FydMj9vHpXoLHyC8up+VitR2UlDYPVzFfZGpOGe1DLs8vg9j cRKXDizu8iItb6gW4IgqIGtJpXLo5k5pPH62G89oFSaAwl61/NaNYX3oXhLBATJU0FgE CQ== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3bsbtrew-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:10 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 324179Il032297 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:09 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.986.41; Fri, 3 Mar 2023 17:07:08 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 17/26] docs: gunyah: Document Gunyah VM Manager Date: Fri, 3 Mar 2023 17:06:23 -0800 Message-ID: <20230304010632.2127470-18-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: -Min-bMluKjxBJzwV2OMQ6tBhsnC1kRV X-Proofpoint-ORIG-GUID: -Min-bMluKjxBJzwV2OMQ6tBhsnC1kRV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 impostorscore=0 suspectscore=0 bulkscore=0 clxscore=1015 adultscore=0 priorityscore=1501 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Document the ioctls and usage of Gunyah VM Manager driver. Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/index.rst | 1 + Documentation/virt/gunyah/vm-manager.rst | 91 ++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 Documentation/virt/gunyah/vm-manager.rst diff --git a/Documentation/virt/gunyah/index.rst b/Documentation/virt/gunya= h/index.rst index 74aa345e0a14..7058249825b1 100644 --- a/Documentation/virt/gunyah/index.rst +++ b/Documentation/virt/gunyah/index.rst @@ -7,6 +7,7 @@ Gunyah Hypervisor .. toctree:: :maxdepth: 1 =20 + vm-manager message-queue =20 Gunyah is a Type-1 hypervisor which is independent of any OS kernel, and r= uns in diff --git a/Documentation/virt/gunyah/vm-manager.rst b/Documentation/virt/= gunyah/vm-manager.rst new file mode 100644 index 000000000000..1b4aa18670a3 --- /dev/null +++ b/Documentation/virt/gunyah/vm-manager.rst @@ -0,0 +1,91 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Virtual Machine Manager +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The Gunyah Virtual Machine Manager is a Linux driver to support launching +virtual machines using Gunyah. It presently supports launching non-proxy +scheduled Linux-like virtual machines. + +Except for some basic information about the location of initial binaries, +most of the configuration about a Gunyah virtual machine is described in t= he +VM's devicetree. The devicetree is generated by userspace. Interacting wit= h the +virtual machine is still done via the kernel and VM configuration requires= some +of the corresponding functionality to be set up in the kernel. For instanc= e, +sharing userspace memory with a VM is done via the GH_VM_SET_USER_MEM_REGI= ON +ioctl. The VM itself is configured to use the memory region via the +devicetree. + +Sample Userspace VMM +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +A sample userspace VMM is included in samples/gunyah/ along with a minimal +devicetree that can be used to launch a VM. To build this sample, enable +CONFIG_SAMPLE_GUNYAH. + +IOCTLs and userspace VMM flows +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D + +The kernel exposes a char device interface at /dev/gunyah. + +To create a VM, use the GH_CREATE_VM ioctl. A successful call will return a +"Gunyah VM" file descriptor. + +/dev/gunyah API Descriptions +---------------------------- + +GH_CREATE_VM +~~~~~~~~~~~~ + +Creates a Gunyah VM. The argument is reserved for future use and must be 0. + +Gunyah VM API Descriptions +-------------------------- + +GH_VM_SET_USER_MEM_REGION +~~~~~~~~~~~~~~~~~~~~~~~~~ + +This ioctl allows the user to create or delete a memory parcel for a guest +virtual machine. Each memory region is uniquely identified by a label; +attempting to create two regions with the same label is not allowed. Label= s are +unique per virtual machine. + +While VMM is guest-agnostic and allows runtime addition of memory regions, +Linux guest virtual machines do not support accepting memory regions at ru= ntime. +Thus, memory regions should be provided before starting the VM and the VM = must +be configured to accept these at boot-up. + +The guest physical address is used by Linux kernel to check that the reque= sted +user regions do not overlap and to help find the corresponding memory regi= on +for calls like GH_VM_SET_DTB_CONFIG. It must be page aligned. + +memory_size and userspace_addr must be page-aligned. + +The flags field of gh_userspace_memory_region accepts the following bits. = All +other bits must be 0 and are reserved for future use. The ioctl will return +-EINVAL if an unsupported bit is detected. + + - GH_MEM_ALLOW_READ/GH_MEM_ALLOW_WRITE/GH_MEM_ALLOW_EXEC sets read/write= /exec + permissions for the guest, respectively. + +To add a memory region, call GH_VM_SET_USER_MEM_REGION with fields set as +described above. + +.. kernel-doc:: include/uapi/linux/gunyah.h + :identifiers: gh_userspace_memory_region + +GH_VM_SET_DTB_CONFIG +~~~~~~~~~~~~~~~~~~~~ + +This ioctl sets the location of the VM's devicetree blob and is used by Gu= nyah +Resource Manager to allocate resources. The guest physical memory should b= e part +of the primary memory parcel provided to the VM prior to GH_VM_START. + +.. kernel-doc:: include/uapi/linux/gunyah.h + :identifiers: gh_vm_dtb_config + +GH_VM_START +~~~~~~~~~~~ + +This ioctl starts the VM. --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6589BC678DB for ; Sat, 4 Mar 2023 01:10:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229988AbjCDBKK (ORCPT ); Fri, 3 Mar 2023 20:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229962AbjCDBJo (ORCPT ); Fri, 3 Mar 2023 20:09:44 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 424466A9D4; Fri, 3 Mar 2023 17:08:43 -0800 (PST) Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323MX3Op016506; Sat, 4 Mar 2023 01:07:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=4LDvdikPShkYJfoovbBxUCUrmxXPxK5fDTIedpcNh2w=; b=X1RZrKEAV3MOCWKWk5QEuTXUVQMwOPLSCW8K0eoNK1How+F99f2GtfjEG+aTyixF7sYI wcabRRV7B8r5re9GDpxxVmvPZhvfHU/clou2gGn4bye0goOYvhwJ7INM+ABfOsRZhRoI OsuDpQO4myrjcFbW97DWN6LdNRI+PVxGgXgTqba3GSYHXxdFiqeKrkDPrdi3ID2fKks1 rT+H1nTjjUXbancyEVlubAE0P4uNVuZVyri4y0nAlDlD7kq9AAG05QMdo5DhS4fjZdMy tQHTZyYu7H31TSRTXWMLhyuSjWbeiH/gBoovPaizxvzTMDzo74pue9tlBlP5+4amNKBB HQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p32ty42ys-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:12 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417BtI028892 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:11 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.986.41; Fri, 3 Mar 2023 17:07:10 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Catalin Marinas , Will Deacon , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Andy Gross , Jassi Brar , , , , , Subject: [PATCH v11 18/26] virt: gunyah: Translate gh_rm_hyp_resource into gunyah_resource Date: Fri, 3 Mar 2023 17:06:24 -0800 Message-ID: <20230304010632.2127470-19-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: S8R5dXIZOPJ3DxwUl3PXFAAYbjOuHNFw X-Proofpoint-ORIG-GUID: S8R5dXIZOPJ3DxwUl3PXFAAYbjOuHNFw X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 mlxlogscore=975 lowpriorityscore=0 suspectscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 adultscore=0 impostorscore=0 phishscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When booting a Gunyah virtual machine, the host VM may gain capabilities to interact with resources for the guest virtual machine. Examples of such resources are vCPUs or message queues. To use those resources, we need to translate the RM response into a gunyah_resource structure which are useful to Linux drivers. Presently, Linux drivers need only to know the type of resource, the capability ID, and an interrupt. On ARM64 systems, the interrupt reported by Gunyah is the GIC interrupt ID number and always a SPI. Signed-off-by: Elliot Berman --- arch/arm64/include/asm/gunyah.h | 23 +++++ drivers/virt/gunyah/rsc_mgr.c | 163 +++++++++++++++++++++++++++++++- include/linux/gunyah.h | 4 + include/linux/gunyah_rsc_mgr.h | 3 + 4 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/gunyah.h diff --git a/arch/arm64/include/asm/gunyah.h b/arch/arm64/include/asm/gunya= h.h new file mode 100644 index 000000000000..64cfb964efee --- /dev/null +++ b/arch/arm64/include/asm/gunyah.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. + */ +#ifndef __ASM_GUNYAH_H_ +#define __ASM_GUNYAH_H_ + +#include +#include + +static inline int arch_gh_fill_irq_fwspec_params(u32 virq, struct irq_fwsp= ec *fwspec) +{ + if (virq < 32 || virq > 1019) + return -EINVAL; + + fwspec->param_count =3D 3; + fwspec->param[0] =3D GIC_SPI; + fwspec->param[1] =3D virq - 32; + fwspec->param[2] =3D IRQ_TYPE_EDGE_RISING; + return 0; +} + +#endif diff --git a/drivers/virt/gunyah/rsc_mgr.c b/drivers/virt/gunyah/rsc_mgr.c index d7ce692d0067..383be5ac0f44 100644 --- a/drivers/virt/gunyah/rsc_mgr.c +++ b/drivers/virt/gunyah/rsc_mgr.c @@ -17,6 +17,8 @@ #include #include =20 +#include + #include "rsc_mgr.h" #include "vm_mgr.h" =20 @@ -132,6 +134,7 @@ struct gh_rm_connection { * @send_lock: synchronization to allow only one request to be sent at a t= ime * @nh: notifier chain for clients interested in RM notification messages * @miscdev: /dev/gunyah + * @irq_domain: Domain to translate Gunyah hwirqs to Linux irqs */ struct gh_rm { struct device *dev; @@ -150,6 +153,7 @@ struct gh_rm { struct blocking_notifier_head nh; =20 struct miscdevice miscdev; + struct irq_domain *irq_domain; }; =20 /** @@ -190,6 +194,134 @@ static inline int gh_rm_remap_error(enum gh_rm_error = rm_error) } } =20 +struct gh_irq_chip_data { + u32 gh_virq; +}; + +static struct irq_chip gh_rm_irq_chip =3D { + .name =3D "Gunyah", + .irq_enable =3D irq_chip_enable_parent, + .irq_disable =3D irq_chip_disable_parent, + .irq_ack =3D irq_chip_ack_parent, + .irq_mask =3D irq_chip_mask_parent, + .irq_mask_ack =3D irq_chip_mask_ack_parent, + .irq_unmask =3D irq_chip_unmask_parent, + .irq_eoi =3D irq_chip_eoi_parent, + .irq_set_affinity =3D irq_chip_set_affinity_parent, + .irq_set_type =3D irq_chip_set_type_parent, + .irq_set_wake =3D irq_chip_set_wake_parent, + .irq_set_vcpu_affinity =3D irq_chip_set_vcpu_affinity_parent, + .irq_retrigger =3D irq_chip_retrigger_hierarchy, + .irq_get_irqchip_state =3D irq_chip_get_parent_state, + .irq_set_irqchip_state =3D irq_chip_set_parent_state, + .flags =3D IRQCHIP_SET_TYPE_MASKED | + IRQCHIP_SKIP_SET_WAKE | + IRQCHIP_MASK_ON_SUSPEND, +}; + +static int gh_rm_irq_domain_alloc(struct irq_domain *d, unsigned int virq,= unsigned int nr_irqs, + void *arg) +{ + struct gh_irq_chip_data *chip_data, *spec =3D arg; + struct irq_fwspec parent_fwspec; + struct gh_rm *rm =3D d->host_data; + u32 gh_virq =3D spec->gh_virq; + int ret; + + if (nr_irqs !=3D 1 || gh_virq =3D=3D U32_MAX) + return -EINVAL; + + chip_data =3D kzalloc(sizeof(*chip_data), GFP_KERNEL); + if (!chip_data) + return -ENOMEM; + + chip_data->gh_virq =3D gh_virq; + + ret =3D irq_domain_set_hwirq_and_chip(d, virq, chip_data->gh_virq, &gh_rm= _irq_chip, + chip_data); + if (ret) + goto err_free_irq_data; + + parent_fwspec.fwnode =3D d->parent->fwnode; + ret =3D arch_gh_fill_irq_fwspec_params(chip_data->gh_virq, &parent_fwspec= ); + if (ret) { + dev_err(rm->dev, "virq translation failed %u: %d\n", chip_data->gh_virq,= ret); + goto err_free_irq_data; + } + + ret =3D irq_domain_alloc_irqs_parent(d, virq, nr_irqs, &parent_fwspec); + if (ret) + goto err_free_irq_data; + + return ret; +err_free_irq_data: + kfree(chip_data); + return ret; +} + +static void gh_rm_irq_domain_free_single(struct irq_domain *d, unsigned in= t virq) +{ + struct gh_irq_chip_data *chip_data; + struct irq_data *irq_data; + + irq_data =3D irq_domain_get_irq_data(d, virq); + if (!irq_data) + return; + + chip_data =3D irq_data->chip_data; + + kfree(chip_data); + irq_data->chip_data =3D NULL; +} + +static void gh_rm_irq_domain_free(struct irq_domain *d, unsigned int virq,= unsigned int nr_irqs) +{ + unsigned int i; + + for (i =3D 0; i < nr_irqs; i++) + gh_rm_irq_domain_free_single(d, virq); +} + +static const struct irq_domain_ops gh_rm_irq_domain_ops =3D { + .alloc =3D gh_rm_irq_domain_alloc, + .free =3D gh_rm_irq_domain_free, +}; + +struct gh_resource *gh_rm_alloc_resource(struct gh_rm *rm, struct gh_rm_hy= p_resource *hyp_resource) +{ + struct gh_resource *ghrsc; + + ghrsc =3D kzalloc(sizeof(*ghrsc), GFP_KERNEL); + if (!ghrsc) + return NULL; + + ghrsc->type =3D hyp_resource->type; + ghrsc->capid =3D le64_to_cpu(hyp_resource->cap_id); + ghrsc->irq =3D IRQ_NOTCONNECTED; + ghrsc->rm_label =3D le32_to_cpu(hyp_resource->resource_label); + if (hyp_resource->virq && le32_to_cpu(hyp_resource->virq) !=3D U32_MAX) { + struct gh_irq_chip_data irq_data =3D { + .gh_virq =3D le32_to_cpu(hyp_resource->virq), + }; + + ghrsc->irq =3D irq_domain_alloc_irqs(rm->irq_domain, 1, NUMA_NO_NODE, &i= rq_data); + if (ghrsc->irq < 0) { + dev_err(rm->dev, + "Failed to allocate interrupt for resource %d label: %d: %d\n", + ghrsc->type, ghrsc->rm_label, ghrsc->irq); + ghrsc->irq =3D IRQ_NOTCONNECTED; + } + } + + return ghrsc; +} + +void gh_rm_free_resource(struct gh_resource *ghrsc) +{ + irq_dispose_mapping(ghrsc->irq); + kfree(ghrsc); +} + static int gh_rm_init_connection_payload(struct gh_rm_connection *connecti= on, void *msg, size_t hdr_size, size_t msg_size) { @@ -639,6 +771,8 @@ static int gh_msgq_platform_probe_direction(struct plat= form_device *pdev, bool t =20 static int gh_rm_drv_probe(struct platform_device *pdev) { + struct irq_domain *parent_irq_domain; + struct device_node *parent_irq_node; struct gh_msgq_tx_data *msg; struct gh_rm *rm; int ret; @@ -675,15 +809,41 @@ static int gh_rm_drv_probe(struct platform_device *pd= ev) if (ret) goto err_cache; =20 + parent_irq_node =3D of_irq_find_parent(pdev->dev.of_node); + if (!parent_irq_node) { + dev_err(&pdev->dev, "Failed to find interrupt parent of resource manager= \n"); + ret =3D -ENODEV; + goto err_msgq; + } + + parent_irq_domain =3D irq_find_host(parent_irq_node); + if (!parent_irq_domain) { + dev_err(&pdev->dev, "Failed to find interrupt parent domain of resource = manager\n"); + ret =3D -ENODEV; + goto err_msgq; + } + + rm->irq_domain =3D irq_domain_add_hierarchy(parent_irq_domain, 0, 0, pdev= ->dev.of_node, + &gh_rm_irq_domain_ops, NULL); + if (!rm->irq_domain) { + dev_err(&pdev->dev, "Failed to add irq domain\n"); + ret =3D -ENODEV; + goto err_msgq; + } + rm->irq_domain->host_data =3D rm; + + rm->miscdev.parent =3D &pdev->dev; rm->miscdev.name =3D "gunyah"; rm->miscdev.minor =3D MISC_DYNAMIC_MINOR; rm->miscdev.fops =3D &gh_dev_fops; =20 ret =3D misc_register(&rm->miscdev); if (ret) - goto err_msgq; + goto err_irq_domain; =20 return 0; +err_irq_domain: + irq_domain_remove(rm->irq_domain); err_msgq: mbox_free_channel(gh_msgq_chan(&rm->msgq)); gh_msgq_remove(&rm->msgq); @@ -697,6 +857,7 @@ static int gh_rm_drv_remove(struct platform_device *pde= v) struct gh_rm *rm =3D platform_get_drvdata(pdev); =20 misc_deregister(&rm->miscdev); + irq_domain_remove(rm->irq_domain); mbox_free_channel(gh_msgq_chan(&rm->msgq)); gh_msgq_remove(&rm->msgq); kmem_cache_destroy(rm->cache); diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 378bec0f2ce1..3e706b59d2c0 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -27,6 +27,10 @@ struct gh_resource { enum gh_resource_type type; u64 capid; unsigned int irq; + + /* To help allocator in vm manager */ + struct list_head list; + u32 rm_label; }; =20 /** diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h index acf8c1545a6c..58693c27cf1a 100644 --- a/include/linux/gunyah_rsc_mgr.h +++ b/include/linux/gunyah_rsc_mgr.h @@ -145,6 +145,9 @@ int gh_rm_get_hyp_resources(struct gh_rm *rm, u16 vmid, struct gh_rm_hyp_resources **resources); int gh_rm_get_vmid(struct gh_rm *rm, u16 *vmid); =20 +struct gh_resource *gh_rm_alloc_resource(struct gh_rm *rm, struct gh_rm_hy= p_resource *hyp_resource); +void gh_rm_free_resource(struct gh_resource *ghrsc); + struct gh_rm_platform_ops { int (*pre_mem_share)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_parce= l); int (*post_mem_reclaim)(struct gh_rm *rm, struct gh_rm_mem_parcel *mem_pa= rcel); --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD42EC77B75 for ; Sat, 4 Mar 2023 01:08:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229852AbjCDBIf (ORCPT ); Fri, 3 Mar 2023 20:08:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229937AbjCDBIJ (ORCPT ); Fri, 3 Mar 2023 20:08:09 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99D5A6700A; Fri, 3 Mar 2023 17:07:33 -0800 (PST) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240pX0k012032; Sat, 4 Mar 2023 01:07:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=2rsud1CHoQ3liASEh3joZIv3MgrCroRtN/O/D6/6mWc=; b=VTe76Qn/phMWeHDWkZ+j+RiuYqxFD8N3M6qvz/EOoPednycXWaKYORt8wRpqPJuSYNtp B7cV71EC7Av4lBp37dPIgS0QWnEJ9t6kbQ3yek+mYFYGdaoBhT0tP/vl0S6HrT/2xI1N iTwjcJWZStPrKLBXT7YbGykWe1x46aEz0SSxJqgc4/n7GdTappBuUvh/m4c7QmM9YI6Z ag+AwSZks1KkODYaTJUBw5pLFmPszD/pa1pDPJrmGDFptp+jLtIZ3qqhEwCnvl/0wy4W a8TrBRzfc2oQR7kz0xHyay4cvMbKygbnBbSSgh7CgdQQ2b13549LXgMFbf8N6aGzaRFF HQ== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3ph4rpeq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:14 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417D9Y019272 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:13 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.986.41; Fri, 3 Mar 2023 17:07:12 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 19/26] gunyah: vm_mgr: Add framework to add VM Functions Date: Fri, 3 Mar 2023 17:06:25 -0800 Message-ID: <20230304010632.2127470-20-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: Hd55z_8pbCtlMIn4DSD8BFKqy0L9jT5A X-Proofpoint-GUID: Hd55z_8pbCtlMIn4DSD8BFKqy0L9jT5A X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 suspectscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce a framework for Gunyah userspace to install VM functions. VM functions are optional interfaces to the virtual machine. vCPUs, ioeventfs, and irqfds are examples of such VM functions and are implemented in subsequent patches. A generic framework is implemented instead of individual ioctls to create vCPUs, irqfds, etc., in order to simplify the VM manager core implementation and allow dynamic loading of VM function modules. Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/vm-manager.rst | 18 ++ drivers/virt/gunyah/vm_mgr.c | 208 ++++++++++++++++++++++- drivers/virt/gunyah/vm_mgr.h | 4 + include/linux/gunyah_vm_mgr.h | 73 ++++++++ include/uapi/linux/gunyah.h | 17 ++ 5 files changed, 316 insertions(+), 4 deletions(-) create mode 100644 include/linux/gunyah_vm_mgr.h diff --git a/Documentation/virt/gunyah/vm-manager.rst b/Documentation/virt/= gunyah/vm-manager.rst index 1b4aa18670a3..af8ad88a88ab 100644 --- a/Documentation/virt/gunyah/vm-manager.rst +++ b/Documentation/virt/gunyah/vm-manager.rst @@ -17,6 +17,24 @@ sharing userspace memory with a VM is done via the GH_VM= _SET_USER_MEM_REGION ioctl. The VM itself is configured to use the memory region via the devicetree. =20 +Gunyah Functions +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Components of a Gunyah VM's configuration that need kernel configuration a= re +called "functions" and are built on top of a framework. Functions are iden= tified +by a string and have some argument(s) to configure them. They are typically +created by the `GH_VM_ADD_FUNCTION` ioctl. + +Functions typically will always do at least one of these operations: + +1. Create resource ticket(s). Resource tickets allow a function to register + itself as the client for a Gunyah resource (e.g. doorbell or vCPU) and + the function is given the pointer to the `struct gh_resource` when the + VM is starting. + +2. Register IO handler(s). IO handlers allow a function to handle stage-2 = faults + from the virtual machine. + Sample Userspace VMM =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 299b9bb81edc..88db011395ec 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -6,16 +6,165 @@ #define pr_fmt(fmt) "gh_vm_mgr: " fmt =20 #include +#include #include #include +#include #include #include #include +#include =20 #include =20 #include "vm_mgr.h" =20 +static DEFINE_XARRAY(functions); + +int gh_vm_function_register(struct gh_vm_function *fn) +{ + if (!fn->bind || !fn->unbind) + return -EINVAL; + + return xa_err(xa_store(&functions, fn->type, fn, GFP_KERNEL)); +} +EXPORT_SYMBOL_GPL(gh_vm_function_register); + +static void gh_vm_remove_function_instance(struct gh_vm_function_instance = *inst) + __must_hold(&inst->ghvm->fn_lock) +{ + inst->fn->unbind(inst); + list_del(&inst->vm_list); + module_put(inst->fn->mod); + kfree(inst->argp); + kfree(inst); +} + +void gh_vm_function_unregister(struct gh_vm_function *fn) +{ + /* Expecting unregister to only come when unloading a module */ + WARN_ON(fn->mod && module_refcount(fn->mod)); + xa_erase(&functions, fn->type); +} +EXPORT_SYMBOL_GPL(gh_vm_function_unregister); + +static struct gh_vm_function *gh_vm_get_function(u32 type) +{ + struct gh_vm_function *fn; + int r; + + fn =3D xa_load(&functions, type); + if (!fn) { + r =3D request_module("ghfunc:%d", type); + if (r) + return ERR_PTR(r); + + fn =3D xa_load(&functions, type); + } + + if (!fn || !try_module_get(fn->mod)) + fn =3D ERR_PTR(-ENOENT); + + return fn; +} + +static long gh_vm_add_function(struct gh_vm *ghvm, struct gh_fn_desc *f) +{ + struct gh_vm_function_instance *inst; + void __user *argp; + long r =3D 0; + + if (f->arg_size > GH_FN_MAX_ARG_SIZE) { + dev_err(ghvm->parent, "%s: arg_size > %d\n", __func__, GH_FN_MAX_ARG_SIZ= E); + return -EINVAL; + } + + inst =3D kzalloc(sizeof(*inst), GFP_KERNEL); + if (!inst) + return -ENOMEM; + + inst->arg_size =3D f->arg_size; + if (inst->arg_size) { + inst->argp =3D kzalloc(inst->arg_size, GFP_KERNEL); + if (!inst->argp) { + r =3D -ENOMEM; + goto free; + } + + argp =3D u64_to_user_ptr(f->arg); + if (copy_from_user(inst->argp, argp, f->arg_size)) { + r =3D -EFAULT; + goto free_arg; + } + } + + inst->fn =3D gh_vm_get_function(f->type); + if (IS_ERR(inst->fn)) { + r =3D PTR_ERR(inst->fn); + goto free_arg; + } + + inst->ghvm =3D ghvm; + inst->rm =3D ghvm->rm; + + mutex_lock(&ghvm->fn_lock); + r =3D inst->fn->bind(inst); + if (r < 0) { + module_put(inst->fn->mod); + goto free_arg; + } + + list_add(&inst->vm_list, &ghvm->functions); + mutex_unlock(&ghvm->fn_lock); + + return r; +free_arg: + kfree(inst->argp); +free: + kfree(inst); + return r; +} + +static long gh_vm_rm_function(struct gh_vm *ghvm, struct gh_fn_desc *f) +{ + struct gh_vm_function_instance *inst, *iter; + void __user *user_argp; + void *argp; + long r =3D 0; + + r =3D mutex_lock_interruptible(&ghvm->fn_lock); + if (r) + return r; + + if (f->arg_size) { + argp =3D kzalloc(f->arg_size, GFP_KERNEL); + if (!argp) { + r =3D -ENOMEM; + goto out; + } + + user_argp =3D u64_to_user_ptr(f->arg); + if (copy_from_user(argp, user_argp, f->arg_size)) { + r =3D -EFAULT; + kfree(argp); + goto out; + } + + list_for_each_entry_safe(inst, iter, &ghvm->functions, vm_list) { + if (inst->fn->type =3D=3D f->type && + f->arg_size =3D=3D inst->arg_size && + !memcmp(argp, inst->argp, f->arg_size)) + gh_vm_remove_function_instance(inst); + } + + kfree(argp); + } + +out: + mutex_unlock(&ghvm->fn_lock); + return r; +} + static int gh_vm_rm_notification_status(struct gh_vm *ghvm, void *data) { struct gh_rm_vm_status_payload *payload =3D data; @@ -80,6 +229,7 @@ static void gh_vm_stop(struct gh_vm *ghvm) static void gh_vm_free(struct work_struct *work) { struct gh_vm *ghvm =3D container_of(work, struct gh_vm, free_work); + struct gh_vm_function_instance *inst, *iiter; struct gh_vm_mem *mapping, *tmp; int ret; =20 @@ -90,6 +240,12 @@ static void gh_vm_free(struct work_struct *work) case GH_RM_VM_STATUS_INIT_FAILED: case GH_RM_VM_STATUS_LOAD: case GH_RM_VM_STATUS_EXITED: + mutex_lock(&ghvm->fn_lock); + list_for_each_entry_safe(inst, iiter, &ghvm->functions, vm_list) { + gh_vm_remove_function_instance(inst); + } + mutex_unlock(&ghvm->fn_lock); + mutex_lock(&ghvm->mm_lock); list_for_each_entry_safe(mapping, tmp, &ghvm->memory_mappings, list) { gh_vm_mem_reclaim(ghvm, mapping); @@ -117,6 +273,28 @@ static void gh_vm_free(struct work_struct *work) } } =20 +static void _gh_vm_put(struct kref *kref) +{ + struct gh_vm *ghvm =3D container_of(kref, struct gh_vm, kref); + + /* VM will be reset and make RM calls which can interruptible sleep. + * Defer to a work so this thread can receive signal. + */ + schedule_work(&ghvm->free_work); +} + +int __must_check gh_vm_get(struct gh_vm *ghvm) +{ + return kref_get_unless_zero(&ghvm->kref); +} +EXPORT_SYMBOL_GPL(gh_vm_get); + +void gh_vm_put(struct gh_vm *ghvm) +{ + kref_put(&ghvm->kref, _gh_vm_put); +} +EXPORT_SYMBOL_GPL(gh_vm_put); + static __must_check struct gh_vm *gh_vm_alloc(struct gh_rm *rm) { struct gh_vm *ghvm; @@ -150,6 +328,8 @@ static __must_check struct gh_vm *gh_vm_alloc(struct gh= _rm *rm) INIT_LIST_HEAD(&ghvm->memory_mappings); init_rwsem(&ghvm->status_lock); INIT_WORK(&ghvm->free_work, gh_vm_free); + kref_init(&ghvm->kref); + INIT_LIST_HEAD(&ghvm->functions); ghvm->vm_status =3D GH_RM_VM_STATUS_LOAD; =20 return ghvm; @@ -302,6 +482,29 @@ static long gh_vm_ioctl(struct file *filp, unsigned in= t cmd, unsigned long arg) r =3D gh_vm_ensure_started(ghvm); break; } + case GH_VM_ADD_FUNCTION: { + struct gh_fn_desc f; + + if (copy_from_user(&f, argp, sizeof(f))) + return -EFAULT; + + r =3D gh_vm_add_function(ghvm, &f); + break; + } + case GH_VM_REMOVE_FUNCTION: { + struct gh_fn_desc *f; + + f =3D kzalloc(sizeof(*f), GFP_KERNEL); + if (!f) + return -ENOMEM; + + if (copy_from_user(f, argp, sizeof(*f))) + return -EFAULT; + + r =3D gh_vm_rm_function(ghvm, f); + kfree(f); + break; + } default: r =3D -ENOTTY; break; @@ -314,10 +517,7 @@ static int gh_vm_release(struct inode *inode, struct f= ile *filp) { struct gh_vm *ghvm =3D filp->private_data; =20 - /* VM will be reset and make RM calls which can interruptible sleep. - * Defer to a work so this thread can receive signal. - */ - schedule_work(&ghvm->free_work); + gh_vm_put(ghvm); return 0; } =20 diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 26bcc2ae4478..7bd271bad721 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -8,6 +8,7 @@ =20 #include #include +#include #include #include #include @@ -45,8 +46,11 @@ struct gh_vm { struct rw_semaphore status_lock; =20 struct work_struct free_work; + struct kref kref; struct mutex mm_lock; struct list_head memory_mappings; + struct mutex fn_lock; + struct list_head functions; }; =20 int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region = *region); diff --git a/include/linux/gunyah_vm_mgr.h b/include/linux/gunyah_vm_mgr.h new file mode 100644 index 000000000000..3825c951790a --- /dev/null +++ b/include/linux/gunyah_vm_mgr.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#ifndef _GUNYAH_VM_MGR_H +#define _GUNYAH_VM_MGR_H + +#include +#include +#include +#include +#include +#include + +#include + +struct gh_vm; + +int __must_check gh_vm_get(struct gh_vm *ghvm); +void gh_vm_put(struct gh_vm *ghvm); + +struct gh_vm_function_instance; +struct gh_vm_function { + u32 type; + const char *name; + struct module *mod; + long (*bind)(struct gh_vm_function_instance *f); + void (*unbind)(struct gh_vm_function_instance *f); +}; + +/** + * struct gh_vm_function_instance - Represents one function instance + * @arg_size: size of user argument + * @argp: pointer to user argument + * @ghvm: Pointer to VM instance + * @rm: Pointer to resource manager for the VM instance + * @fn: The ops for the function + * @data: Private data for function + * @vm_list: for gh_vm's functions list + * @fn_list: for gh_vm_function's instances list + */ +struct gh_vm_function_instance { + size_t arg_size; + void *argp; + struct gh_vm *ghvm; + struct gh_rm *rm; + struct gh_vm_function *fn; + void *data; + struct list_head vm_list; +}; + +int gh_vm_function_register(struct gh_vm_function *f); +void gh_vm_function_unregister(struct gh_vm_function *f); + +#define DECLARE_GH_VM_FUNCTION(_name, _type, _bind, _unbind) \ + static struct gh_vm_function _name =3D { \ + .type =3D _type, \ + .name =3D __stringify(_name), \ + .mod =3D THIS_MODULE, \ + .bind =3D _bind, \ + .unbind =3D _unbind, \ + }; \ + MODULE_ALIAS("ghfunc:"__stringify(_type)) + +#define module_gh_vm_function(__gf) \ + module_driver(__gf, gh_vm_function_register, gh_vm_function_unregister) + +#define DECLARE_GH_VM_FUNCTION_INIT(_name, _type, _bind, _unbind) \ + DECLARE_GH_VM_FUNCTION(_name, _type, _bind, _unbind); \ + module_gh_vm_function(_name) + +#endif diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index d6abd8605a2e..caeb3b3a3e9a 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -62,4 +62,21 @@ struct gh_vm_dtb_config { =20 #define GH_VM_START _IO(GH_IOCTL_TYPE, 0x3) =20 +#define GH_FN_MAX_ARG_SIZE 256 + +/** + * struct gh_fn_desc - Arguments to create a VM function + * @type: Type of the function. See GH_FN_* macro for supported types + * @arg_size: Size of argument to pass to the function. arg_size <=3D GH_F= N_MAX_ARG_SIZE + * @arg: Value or pointer to argument given to the function + */ +struct gh_fn_desc { + __u32 type; + __u32 arg_size; + __u64 arg; +}; + +#define GH_VM_ADD_FUNCTION _IOW(GH_IOCTL_TYPE, 0x4, struct gh_fn_desc) +#define GH_VM_REMOVE_FUNCTION _IOW(GH_IOCTL_TYPE, 0x7, struct gh_fn_desc) + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31127C6FD1C for ; Sat, 4 Mar 2023 01:08:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbjCDBIm (ORCPT ); Fri, 3 Mar 2023 20:08:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjCDBIM (ORCPT ); Fri, 3 Mar 2023 20:08:12 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DE5967803; Fri, 3 Mar 2023 17:07:42 -0800 (PST) Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3238mIJu007928; Sat, 4 Mar 2023 01:07:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=/VCX+yqz/ho1pAOqIOYfJGpNKwtPtomBUY+Aoh/O+TI=; b=UnEHWhCGXHoSs7qcIhC36RmNXdsseY0gRIJDjDz0Tc0NXEZr0sNwGpwqFIXCLqgT/dBn YsHVr6QMe9ZQWwAmlvXCXD8jOCxG0UyrN2fobJyLIPeZNQD671I+ANODqIxMs9KW9nJx oZYXwJjPzHbfQ+9zpDn3lSJTw4Xm0wG2JRQ4OBqMwXakvt2tkKl53AX5B11m9m31pBGM HeAuYpi9sLV1RWx64lGpg8AMwmS0o7UlZnbULPbg+3TOC9ZSd14MEBRKx/OL85/N6bFg Dou3v5oEsZ7WZ2B0OaOFkaQY0kWu2W9MksOR/AGt961cS5O5HTmd/Be/1NZOlgosE3Sq EA== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3dpxjjcb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:15 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417EnF028975 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:14 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.986.41; Fri, 3 Mar 2023 17:07:13 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 20/26] virt: gunyah: Add resource tickets Date: Fri, 3 Mar 2023 17:06:26 -0800 Message-ID: <20230304010632.2127470-21-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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-ORIG-GUID: eyLziE9DYf1WbGWtvtPkVm5QrPjY-rYr X-Proofpoint-GUID: eyLziE9DYf1WbGWtvtPkVm5QrPjY-rYr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 impostorscore=0 phishscore=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Some VM functions need to acquire Gunyah resources. For instance, Gunyah vCPUs are exposed to the host as a resource. The Gunyah vCPU function will register a resource ticket and be able to interact with the hypervisor once the resource ticket is filled. Resource tickets are the mechanism for functions to acquire ownership of Gunyah resources. Gunyah functions can be created before the VM's resources are created and made available to Linux. A resource ticket identifies a type of resource and a label of a resource which the ticket holder is interested in. Resources are created by Gunyah as configured in the VM's devicetree configuration. Gunyah doesn't process the label and that makes it possible for userspace to create multiple resources with the same label. Resource ticket owners need to be prepared for populate to be called multiple times if userspace created multiple resources with the same label. Signed-off-by: Elliot Berman --- drivers/virt/gunyah/vm_mgr.c | 112 +++++++++++++++++++++++++++++++++- drivers/virt/gunyah/vm_mgr.h | 4 ++ include/linux/gunyah_vm_mgr.h | 14 +++++ 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 88db011395ec..0269bcdaf692 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -165,6 +165,74 @@ static long gh_vm_rm_function(struct gh_vm *ghvm, stru= ct gh_fn_desc *f) return r; } =20 +int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ti= cket *ticket) +{ + struct gh_vm_resource_ticket *iter; + struct gh_resource *ghrsc; + int ret =3D 0; + + mutex_lock(&ghvm->resources_lock); + list_for_each_entry(iter, &ghvm->resource_tickets, list) { + if (iter->resource_type =3D=3D ticket->resource_type && iter->label =3D= =3D ticket->label) { + ret =3D -EEXIST; + goto out; + } + } + + if (!try_module_get(ticket->owner)) { + ret =3D -ENODEV; + goto out; + } + + list_add(&ticket->list, &ghvm->resource_tickets); + INIT_LIST_HEAD(&ticket->resources); + + list_for_each_entry(ghrsc, &ghvm->resources, list) { + if (ghrsc->type =3D=3D ticket->resource_type && ghrsc->rm_label =3D=3D t= icket->label) { + if (!ticket->populate(ticket, ghrsc)) + list_move(&ghrsc->list, &ticket->resources); + } + } +out: + mutex_unlock(&ghvm->resources_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gh_vm_add_resource_ticket); + +void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resourc= e_ticket *ticket) +{ + struct gh_resource *ghrsc, *iter; + + mutex_lock(&ghvm->resources_lock); + list_for_each_entry_safe(ghrsc, iter, &ticket->resources, list) { + ticket->unpopulate(ticket, ghrsc); + list_move(&ghrsc->list, &ghvm->resources); + } + + module_put(ticket->owner); + list_del(&ticket->list); + mutex_unlock(&ghvm->resources_lock); +} +EXPORT_SYMBOL_GPL(gh_vm_remove_resource_ticket); + +static void gh_vm_add_resource(struct gh_vm *ghvm, struct gh_resource *ghr= sc) +{ + struct gh_vm_resource_ticket *ticket; + + mutex_lock(&ghvm->resources_lock); + list_for_each_entry(ticket, &ghvm->resource_tickets, list) { + if (ghrsc->type =3D=3D ticket->resource_type && ghrsc->rm_label =3D=3D t= icket->label) { + if (!ticket->populate(ticket, ghrsc)) { + list_add(&ghrsc->list, &ticket->resources); + goto found; + } + } + } + list_add(&ghrsc->list, &ghvm->resources); +found: + mutex_unlock(&ghvm->resources_lock); +} + static int gh_vm_rm_notification_status(struct gh_vm *ghvm, void *data) { struct gh_rm_vm_status_payload *payload =3D data; @@ -230,6 +298,8 @@ static void gh_vm_free(struct work_struct *work) { struct gh_vm *ghvm =3D container_of(work, struct gh_vm, free_work); struct gh_vm_function_instance *inst, *iiter; + struct gh_vm_resource_ticket *ticket, *titer; + struct gh_resource *ghrsc, *riter; struct gh_vm_mem *mapping, *tmp; int ret; =20 @@ -246,6 +316,25 @@ static void gh_vm_free(struct work_struct *work) } mutex_unlock(&ghvm->fn_lock); =20 + mutex_lock(&ghvm->resources_lock); + if (!list_empty(&ghvm->resource_tickets)) { + dev_warn(ghvm->parent, "Dangling resource tickets:\n"); + list_for_each_entry_safe(ticket, titer, &ghvm->resource_tickets, list) { + dev_warn(ghvm->parent, " %pS\n", ticket->populate); + gh_vm_remove_resource_ticket(ghvm, ticket); + } + } + + list_for_each_entry_safe(ghrsc, riter, &ghvm->resources, list) { + gh_rm_free_resource(ghrsc); + } + mutex_unlock(&ghvm->resources_lock); + + ret =3D gh_rm_vm_reset(ghvm->rm, ghvm->vmid); + if (ret) + dev_err(ghvm->parent, "Failed to reset the vm: %d\n", ret); + wait_event(ghvm->vm_status_wait, ghvm->vm_status =3D=3D GH_RM_VM_STATUS_= RESET); + mutex_lock(&ghvm->mm_lock); list_for_each_entry_safe(mapping, tmp, &ghvm->memory_mappings, list) { gh_vm_mem_reclaim(ghvm, mapping); @@ -329,6 +418,9 @@ static __must_check struct gh_vm *gh_vm_alloc(struct gh= _rm *rm) init_rwsem(&ghvm->status_lock); INIT_WORK(&ghvm->free_work, gh_vm_free); kref_init(&ghvm->kref); + mutex_init(&ghvm->resources_lock); + INIT_LIST_HEAD(&ghvm->resources); + INIT_LIST_HEAD(&ghvm->resource_tickets); INIT_LIST_HEAD(&ghvm->functions); ghvm->vm_status =3D GH_RM_VM_STATUS_LOAD; =20 @@ -338,9 +430,11 @@ static __must_check struct gh_vm *gh_vm_alloc(struct g= h_rm *rm) static int gh_vm_start(struct gh_vm *ghvm) { struct gh_vm_mem *mapping; + struct gh_rm_hyp_resources *resources; + struct gh_resource *ghrsc; u64 dtb_offset; u32 mem_handle; - int ret; + int ret, i, n; =20 down_write(&ghvm->status_lock); if (ghvm->vm_status !=3D GH_RM_VM_STATUS_LOAD) { @@ -394,6 +488,22 @@ static int gh_vm_start(struct gh_vm *ghvm) goto err; } =20 + ret =3D gh_rm_get_hyp_resources(ghvm->rm, ghvm->vmid, &resources); + if (ret) { + dev_warn(ghvm->parent, "Failed to get hypervisor resources for VM: %d\n"= , ret); + goto err; + } + + for (i =3D 0, n =3D le32_to_cpu(resources->n_entries); i < n; i++) { + ghrsc =3D gh_rm_alloc_resource(ghvm->rm, &resources->entries[i]); + if (!ghrsc) { + ret =3D -ENOMEM; + goto err; + } + + gh_vm_add_resource(ghvm, ghrsc); + } + ret =3D gh_rm_vm_start(ghvm->rm, ghvm->vmid); if (ret) { dev_warn(ghvm->parent, "Failed to start VM: %d\n", ret); diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 7bd271bad721..18d0e1effd25 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -7,6 +7,7 @@ #define _GH_PRIV_VM_MGR_H =20 #include +#include #include #include #include @@ -51,6 +52,9 @@ struct gh_vm { struct list_head memory_mappings; struct mutex fn_lock; struct list_head functions; + struct mutex resources_lock; + struct list_head resources; + struct list_head resource_tickets; }; =20 int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region = *region); diff --git a/include/linux/gunyah_vm_mgr.h b/include/linux/gunyah_vm_mgr.h index 3825c951790a..01b1761b5923 100644 --- a/include/linux/gunyah_vm_mgr.h +++ b/include/linux/gunyah_vm_mgr.h @@ -70,4 +70,18 @@ void gh_vm_function_unregister(struct gh_vm_function *f); DECLARE_GH_VM_FUNCTION(_name, _type, _bind, _unbind); \ module_gh_vm_function(_name) =20 +struct gh_vm_resource_ticket { + struct list_head list; /* for gh_vm's resources list */ + struct list_head resources; /* for gh_resources's list */ + enum gh_resource_type resource_type; + u32 label; + + struct module *owner; + int (*populate)(struct gh_vm_resource_ticket *ticket, struct gh_resource = *ghrsc); + void (*unpopulate)(struct gh_vm_resource_ticket *ticket, struct gh_resour= ce *ghrsc); +}; + +int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ti= cket *ticket); +void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resourc= e_ticket *ticket); + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28BA7C6FD20 for ; Sat, 4 Mar 2023 01:08:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229551AbjCDBIq (ORCPT ); Fri, 3 Mar 2023 20:08:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230000AbjCDBIL (ORCPT ); Fri, 3 Mar 2023 20:08:11 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 788DC6703B; Fri, 3 Mar 2023 17:07:41 -0800 (PST) Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 323DbL0b005648; Sat, 4 Mar 2023 01:07:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=sKx1pQwXN+n8lQP0AxQlXqUj8TK/UDv2m1PeuSDMcC0=; b=mAtvKZUAGk6VlBK01Axi1w2e3r3/puqL/THBXs0xjhuXeoJka1fERIcS0/IWXjBQm4Nr 73cZZ8OLFm/jdNyxV/U9kKoju+EgwVoCaGiI7ilnVPb0OnYJeb+K8AlUji0k6ziH+uEn n3DqgfdlWOXhiLw1ijoM7hlGgdcIpX0MVsE1GYTzydnm0lmv6tJJn8mTLKGxJHbGKn9l Xxa6jnx94yTmRVXXGScxwCPb7G5dWSXvOVXF8yiJxD7YPU+5r2DI2MPaRvil8+4QyJUh 718FbW0cBk9bcORDLvN6TiCr/O4O5teJj94at4+Hl5U7T07ywbcPxQ8Y81VUfjSG4+if 7g== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p32ty4302-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:16 +0000 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 32417FGj019427 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:15 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.986.41; Fri, 3 Mar 2023 17:07:15 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 21/26] virt: gunyah: Add IO handlers Date: Fri, 3 Mar 2023 17:06:27 -0800 Message-ID: <20230304010632.2127470-22-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: EivT7yLvgbvfel0ymLONl2e5kq-2VPFh X-Proofpoint-ORIG-GUID: EivT7yLvgbvfel0ymLONl2e5kq-2VPFh X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 mlxlogscore=999 lowpriorityscore=0 suspectscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 adultscore=0 impostorscore=0 phishscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add framework for VM functions to handle stage-2 write faults from Gunyah guest virtual machines. IO handlers have a range of addresses which they apply to. Optionally, they may apply to only when the value written matches the IO handler's value. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/vm_mgr.c | 94 +++++++++++++++++++++++++++++++++++ drivers/virt/gunyah/vm_mgr.h | 4 ++ include/linux/gunyah_vm_mgr.h | 25 ++++++++++ 3 files changed, 123 insertions(+) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 0269bcdaf692..b31fac15ff45 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -233,6 +233,100 @@ static void gh_vm_add_resource(struct gh_vm *ghvm, st= ruct gh_resource *ghrsc) mutex_unlock(&ghvm->resources_lock); } =20 +static int _gh_vm_io_handler_compare(const struct rb_node *node, const str= uct rb_node *parent) +{ + struct gh_vm_io_handler *n =3D container_of(node, struct gh_vm_io_handler= , node); + struct gh_vm_io_handler *p =3D container_of(parent, struct gh_vm_io_handl= er, node); + + if (n->addr < p->addr) + return -1; + if (n->addr > p->addr) + return 1; + if ((n->len && !p->len) || (!n->len && p->len)) + return 0; + if (n->len < p->len) + return -1; + if (n->len > p->len) + return 1; + if (n->datamatch < p->datamatch) + return -1; + if (n->datamatch > p->datamatch) + return 1; + return 0; +} + +static int gh_vm_io_handler_compare(struct rb_node *node, const struct rb_= node *parent) +{ + return _gh_vm_io_handler_compare(node, parent); +} + +static int gh_vm_io_handler_find(const void *key, const struct rb_node *no= de) +{ + const struct gh_vm_io_handler *k =3D key; + + return _gh_vm_io_handler_compare(&k->node, node); +} + +static struct gh_vm_io_handler *gh_vm_mgr_find_io_hdlr(struct gh_vm *ghvm,= u64 addr, + u64 len, u64 data) +{ + struct gh_vm_io_handler key =3D { + .addr =3D addr, + .len =3D len, + .datamatch =3D data, + }; + struct rb_node *node; + + node =3D rb_find(&key, &ghvm->mmio_handler_root, gh_vm_io_handler_find); + if (!node) + return NULL; + + return container_of(node, struct gh_vm_io_handler, node); +} + +int gh_vm_mmio_write(struct gh_vm *ghvm, u64 addr, u32 len, u64 data) +{ + struct gh_vm_io_handler *io_hdlr =3D NULL; + int ret; + + down_read(&ghvm->mmio_handler_lock); + io_hdlr =3D gh_vm_mgr_find_io_hdlr(ghvm, addr, len, data); + if (!io_hdlr || !io_hdlr->ops || !io_hdlr->ops->write) { + ret =3D -ENODEV; + goto out; + } + + ret =3D io_hdlr->ops->write(io_hdlr, addr, len, data); + +out: + up_read(&ghvm->mmio_handler_lock); + return ret; +} +EXPORT_SYMBOL_GPL(gh_vm_mmio_write); + +int gh_vm_add_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_h= dlr) +{ + struct rb_node *found; + + if (io_hdlr->datamatch && (!io_hdlr->len || io_hdlr->len > sizeof(io_hdlr= ->data))) + return -EINVAL; + + down_write(&ghvm->mmio_handler_lock); + found =3D rb_find_add(&io_hdlr->node, &ghvm->mmio_handler_root, gh_vm_io_= handler_compare); + up_write(&ghvm->mmio_handler_lock); + + return found ? -EEXIST : 0; +} +EXPORT_SYMBOL_GPL(gh_vm_add_io_handler); + +void gh_vm_remove_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *= io_hdlr) +{ + down_write(&ghvm->mmio_handler_lock); + rb_erase(&io_hdlr->node, &ghvm->mmio_handler_root); + up_write(&ghvm->mmio_handler_lock); +} +EXPORT_SYMBOL_GPL(gh_vm_remove_io_handler); + static int gh_vm_rm_notification_status(struct gh_vm *ghvm, void *data) { struct gh_rm_vm_status_payload *payload =3D data; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 18d0e1effd25..9c1046af80ed 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -55,6 +55,8 @@ struct gh_vm { struct mutex resources_lock; struct list_head resources; struct list_head resource_tickets; + struct rb_root mmio_handler_root; + struct rw_semaphore mmio_handler_lock; }; =20 int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region = *region); @@ -63,4 +65,6 @@ int gh_vm_mem_free(struct gh_vm *ghvm, u32 label); struct gh_vm_mem *gh_vm_mem_find_by_label(struct gh_vm *ghvm, u32 label); struct gh_vm_mem *gh_vm_mem_find_by_addr(struct gh_vm *ghvm, u64 guest_phy= s_addr, u32 size); =20 +int gh_vm_mmio_write(struct gh_vm *ghvm, u64 addr, u32 len, u64 data); + #endif diff --git a/include/linux/gunyah_vm_mgr.h b/include/linux/gunyah_vm_mgr.h index 01b1761b5923..2dbf5e5f4037 100644 --- a/include/linux/gunyah_vm_mgr.h +++ b/include/linux/gunyah_vm_mgr.h @@ -84,4 +84,29 @@ struct gh_vm_resource_ticket { int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ti= cket *ticket); void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resourc= e_ticket *ticket); =20 +/* + * gh_vm_io_handler contains the info about an io device and its associated + * addr and the ops associated with the io device. + */ +struct gh_vm_io_handler { + struct rb_node node; + u64 addr; + + bool datamatch; + u8 len; + u64 data; + struct gh_vm_io_handler_ops *ops; +}; + +/* + * gh_vm_io_handler_ops contains function pointers associated with an iode= vice. + */ +struct gh_vm_io_handler_ops { + int (*read)(struct gh_vm_io_handler *io_dev, u64 addr, u32 len, u64 data); + int (*write)(struct gh_vm_io_handler *io_dev, u64 addr, u32 len, u64 data= ); +}; + +int gh_vm_add_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_d= ev); +void gh_vm_remove_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *= io_dev); + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 304DFC61DA4 for ; Sat, 4 Mar 2023 01:08:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229579AbjCDBIv (ORCPT ); Fri, 3 Mar 2023 20:08:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbjCDBI3 (ORCPT ); Fri, 3 Mar 2023 20:08:29 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7D102723; Fri, 3 Mar 2023 17:07:56 -0800 (PST) Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32406TJg000822; Sat, 4 Mar 2023 01:07:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=VNmft5VIMrS64krCnda9AAzC4KmvuUdZnUkIt5xWHSE=; b=DQu0mFlYyd0AZ3HbHWanGDfqKbpVunapYFN+Rat/4mK7m5cZT8fh/t4hCFObXev5/pjh S0l7H3FIrNhan6XjMt+YIsFNxUDkUXXBSx/GPaNtTyx+xdEMn3DW9OjlFPKchXeWghDs HbFmo2nVIlWFoFykJ6+2xRdxStvFE3yhgiHzt9TdjYhkyRHkmKyj7SabtEfMFONBhi/R omdoL5t9X4mSVhFsKfCuHWLRl3HzefojvrQuxCeUpdKPCPZ+SOTMJjZOxpg4AD1cf9jB P2EuMSr3gM86zcOyPHplBKnIvflBpPT+Z5b+2974kMKfnrxgMGoBOKJU01h8uFyrgmDh uA== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3c8htrd0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:19 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417ItC029034 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:18 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.986.41; Fri, 3 Mar 2023 17:07:17 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet , Catalin Marinas , Will Deacon CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Andy Gross , Jassi Brar , , , , , Subject: [PATCH v11 22/26] virt: gunyah: Add proxy-scheduled vCPUs Date: Fri, 3 Mar 2023 17:06:28 -0800 Message-ID: <20230304010632.2127470-23-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: F8_d_NOxzm12TsK26Vq0ByqCG9AJGz6t X-Proofpoint-ORIG-GUID: F8_d_NOxzm12TsK26Vq0ByqCG9AJGz6t X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 phishscore=0 malwarescore=0 mlxlogscore=999 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 suspectscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah allows host virtual machines to schedule guest virtual machines and handle their MMIO accesses. vCPUs are presented to the host as a Gunyah resource and represented to userspace as a Gunyah VM function. Creating the vcpu VM function will create a file descriptor that: - can run an ioctl: GH_VCPU_RUN to schedule the guest vCPU until the next interrupt occurs on the host or when the guest vCPU can no longer be run. - can be mmap'd to share a gh_vcpu_run structure which can look up the reason why GH_VCPU_RUN returned and provide return values for MMIO access. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/vm-manager.rst | 46 ++- arch/arm64/gunyah/gunyah_hypercall.c | 28 ++ drivers/virt/gunyah/Kconfig | 11 + drivers/virt/gunyah/Makefile | 2 + drivers/virt/gunyah/gunyah_vcpu.c | 465 +++++++++++++++++++++++ drivers/virt/gunyah/vm_mgr.c | 4 + drivers/virt/gunyah/vm_mgr.h | 1 + include/linux/gunyah.h | 8 + include/uapi/linux/gunyah.h | 108 ++++++ 9 files changed, 671 insertions(+), 2 deletions(-) create mode 100644 drivers/virt/gunyah/gunyah_vcpu.c diff --git a/Documentation/virt/gunyah/vm-manager.rst b/Documentation/virt/= gunyah/vm-manager.rst index af8ad88a88ab..83d326b0d11f 100644 --- a/Documentation/virt/gunyah/vm-manager.rst +++ b/Documentation/virt/gunyah/vm-manager.rst @@ -5,8 +5,7 @@ Virtual Machine Manager =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 The Gunyah Virtual Machine Manager is a Linux driver to support launching -virtual machines using Gunyah. It presently supports launching non-proxy -scheduled Linux-like virtual machines. +virtual machines using Gunyah. =20 Except for some basic information about the location of initial binaries, most of the configuration about a Gunyah virtual machine is described in t= he @@ -107,3 +106,46 @@ GH_VM_START ~~~~~~~~~~~ =20 This ioctl starts the VM. + +GH_VM_ADD_FUNCTION +~~~~~~~~~~~~~~~~~~ + +This ioctl registers a Gunyah VM function with the VM manager. The VM func= tion +is described with a `type` string and some arguments for that type. Typica= lly, +the function is added before the VM starts, but the function doesn't "oper= ate" +until the VM starts with GH_VM_START: e.g. vCPU ioclts will all return an = error +until the VM starts because the vCPUs don't exist until the VM is started.= This +allows the VMM to set up all the kernel functionality needed for the VM *b= efore* +the VM starts. + +.. kernel-doc:: include/uapi/linux/gunyah.h + :identifiers: gh_fn_desc + +The possible types are documented below: + +.. kernel-doc:: include/uapi/linux/gunyah.h + :identifiers: GH_FN_VCPU gh_fn_vcpu_arg + +Gunyah VCPU API Descriptions +---------------------------- + +A vCPU file descriptor is created after calling `GH_VM_ADD_FUNCTION` with = the type `GH_FN_VCPU`. + +GH_VCPU_RUN +~~~~~~~~~~~ + +This ioctl is used to run a guest virtual cpu. While there are no +explicit parameters, there is an implicit parameter block that can be +obtained by mmap()ing the vcpu fd at offset 0, with the size given by +GH_VCPU_MMAP_SIZE. The parameter block is formatted as a 'struct +gh_vcpu_run' (see below). + +GH_VCPU_MMAP_SIZE +~~~~~~~~~~~~~~~~~ + +The GH_VCPU_RUN ioctl communicates with userspace via a shared +memory region. This ioctl returns the size of that region. See the +GH_VCPU_RUN documentation for details. + +.. kernel-doc:: include/uapi/linux/gunyah.h + :identifiers: gh_vcpu_run gh_vm_exit_info diff --git a/arch/arm64/gunyah/gunyah_hypercall.c b/arch/arm64/gunyah/gunya= h_hypercall.c index 3420d8f286a9..f01f5cec4d23 100644 --- a/arch/arm64/gunyah/gunyah_hypercall.c +++ b/arch/arm64/gunyah/gunyah_hypercall.c @@ -43,6 +43,7 @@ EXPORT_SYMBOL_GPL(arch_is_gh_guest); #define GH_HYPERCALL_HYP_IDENTIFY GH_HYPERCALL(0x8000) #define GH_HYPERCALL_MSGQ_SEND GH_HYPERCALL(0x801B) #define GH_HYPERCALL_MSGQ_RECV GH_HYPERCALL(0x801C) +#define GH_HYPERCALL_VCPU_RUN GH_HYPERCALL(0x8065) =20 /** * gh_hypercall_hyp_identify() - Returns build information and feature fla= gs @@ -91,5 +92,32 @@ enum gh_error gh_hypercall_msgq_recv(u64 capid, void *bu= ff, size_t size, size_t } EXPORT_SYMBOL_GPL(gh_hypercall_msgq_recv); =20 +enum gh_error gh_hypercall_vcpu_run(u64 capid, u64 *resume_data, + struct gh_hypercall_vcpu_run_resp *resp) +{ + struct arm_smccc_1_2_regs args =3D { + .a0 =3D GH_HYPERCALL_VCPU_RUN, + .a1 =3D capid, + .a2 =3D resume_data[0], + .a3 =3D resume_data[1], + .a4 =3D resume_data[2], + /* C language says this will be implictly zero. Gunyah requires 0, so be= explicit */ + .a5 =3D 0, + }; + struct arm_smccc_1_2_regs res; + + arm_smccc_1_2_hvc(&args, &res); + + if (res.a0 =3D=3D GH_ERROR_OK) { + resp->state =3D res.a1; + resp->state_data[0] =3D res.a2; + resp->state_data[1] =3D res.a3; + resp->state_data[2] =3D res.a4; + } + + return res.a0; +} +EXPORT_SYMBOL_GPL(gh_hypercall_vcpu_run); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Gunyah Hypervisor Hypercalls"); diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index de815189dab6..4c1c6110b50e 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -15,3 +15,14 @@ config GUNYAH =20 config GUNYAH_PLATFORM_HOOKS tristate + +config GUNYAH_VCPU + tristate "Runnable Gunyah vCPUs" + depends on GUNYAH + help + Enable kernel support for host-scheduled vCPUs running under Gunyah. + When selecting this option, userspace virtual machine managers (VMM) + can schedule the guest VM's vCPUs instead of using Gunyah's scheduler. + VMMs can also handle stage 2 faults of the vCPUs. + + 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 6b8f84dbfe0d..2d1b604a7b03 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -5,3 +5,5 @@ obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) +=3D gunyah_platform_ho= oks.o =20 gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_mgr_mm.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o + +obj-$(CONFIG_GUNYAH_VCPU) +=3D gunyah_vcpu.o diff --git a/drivers/virt/gunyah/gunyah_vcpu.c b/drivers/virt/gunyah/gunyah= _vcpu.c new file mode 100644 index 000000000000..870e471a11df --- /dev/null +++ b/drivers/virt/gunyah/gunyah_vcpu.c @@ -0,0 +1,465 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vm_mgr.h" + +#include + +#define MAX_VCPU_NAME 20 /* gh-vcpu:u32_max+NUL */ + +struct gh_vcpu { + struct gh_vm_function_instance *f; + struct gh_resource *rsc; + struct mutex run_lock; + /* Track why vcpu_run left last time around. */ + enum { + GH_VCPU_UNKNOWN =3D 0, + GH_VCPU_READY, + GH_VCPU_MMIO_READ, + GH_VCPU_SYSTEM_DOWN, + } state; + u8 mmio_read_len; + struct gh_vcpu_run *vcpu_run; + struct completion ready; + struct gh_vm *ghvm; + + struct notifier_block nb; + struct gh_vm_resource_ticket ticket; + struct kref kref; +}; + +/* VCPU is ready to run */ +#define GH_VCPU_STATE_READY 0 +/* VCPU is sleeping until an interrupt arrives */ +#define GH_VCPU_STATE_EXPECTS_WAKEUP 1 +/* VCPU is powered off */ +#define GH_VCPU_STATE_POWERED_OFF 2 +/* VCPU is blocked in EL2 for unspecified reason */ +#define GH_VCPU_STATE_BLOCKED 3 +/* VCPU has returned for MMIO READ */ +#define GH_VCPU_ADDRSPACE_VMMIO_READ 4 +/* VCPU has returned for MMIO WRITE */ +#define GH_VCPU_ADDRSPACE_VMMIO_WRITE 5 + +static void vcpu_release(struct kref *kref) +{ + struct gh_vcpu *vcpu =3D container_of(kref, struct gh_vcpu, kref); + + free_page((unsigned long)vcpu->vcpu_run); + kfree(vcpu); +} + +/* + * When hypervisor allows us to schedule vCPU again, it gives us an interr= upt + */ +static irqreturn_t gh_vcpu_irq_handler(int irq, void *data) +{ + struct gh_vcpu *vcpu =3D data; + + complete(&vcpu->ready); + return IRQ_HANDLED; +} + +static bool gh_handle_mmio(struct gh_vcpu *vcpu, + struct gh_hypercall_vcpu_run_resp *vcpu_run_resp) +{ + int ret =3D 0; + u64 addr =3D vcpu_run_resp->state_data[0], + len =3D vcpu_run_resp->state_data[1], + data =3D vcpu_run_resp->state_data[2]; + + if (vcpu_run_resp->state =3D=3D GH_VCPU_ADDRSPACE_VMMIO_READ) { + vcpu->vcpu_run->mmio.is_write =3D 0; + /* Record that we need to give vCPU user's supplied value next gh_vcpu_r= un() */ + vcpu->state =3D GH_VCPU_MMIO_READ; + vcpu->mmio_read_len =3D len; + } else { /* GH_VCPU_ADDRSPACE_VMMIO_WRITE */ + /* Try internal handlers first */ + ret =3D gh_vm_mmio_write(vcpu->f->ghvm, addr, len, data); + if (!ret) + return true; + + /* Give userspace the info */ + vcpu->vcpu_run->mmio.is_write =3D 1; + memcpy(vcpu->vcpu_run->mmio.data, &data, len); + } + + vcpu->vcpu_run->mmio.phys_addr =3D addr; + vcpu->vcpu_run->mmio.len =3D len; + vcpu->vcpu_run->exit_reason =3D GH_VCPU_EXIT_MMIO; + + return false; +} + +static int gh_vcpu_rm_notification(struct notifier_block *nb, unsigned lon= g action, void *data) +{ + struct gh_vcpu *vcpu =3D container_of(nb, struct gh_vcpu, nb); + struct gh_rm_vm_exited_payload *exit_payload =3D data; + + if (action =3D=3D GH_RM_NOTIFICATION_VM_EXITED && + le16_to_cpu(exit_payload->vmid) =3D=3D vcpu->ghvm->vmid) + complete(&vcpu->ready); + + return NOTIFY_OK; +} + +static inline enum gh_vm_status remap_vm_status(enum gh_rm_vm_status rm_st= atus) +{ + switch (rm_status) { + case GH_RM_VM_STATUS_INIT_FAILED: + return GH_VM_STATUS_LOAD_FAILED; + case GH_RM_VM_STATUS_EXITED: + return GH_VM_STATUS_EXITED; + default: + return GH_VM_STATUS_CRASHED; + } +} + +/** + * gh_vcpu_check_system() - Check whether VM as a whole is running + * @vcpu: Pointer to gh_vcpu + * + * Returns true if the VM is alive. + * Returns false if the vCPU is the VM is not alive (can only be that VM i= s shutting down). + */ +static bool gh_vcpu_check_system(struct gh_vcpu *vcpu) + __must_hold(&vcpu->run_lock) +{ + bool ret =3D true; + + down_read(&vcpu->ghvm->status_lock); + if (likely(vcpu->ghvm->vm_status =3D=3D GH_RM_VM_STATUS_RUNNING)) + goto out; + + vcpu->vcpu_run->status.status =3D remap_vm_status(vcpu->ghvm->vm_status); + vcpu->vcpu_run->status.exit_info =3D vcpu->ghvm->exit_info; + vcpu->vcpu_run->exit_reason =3D GH_VCPU_EXIT_STATUS; + vcpu->state =3D GH_VCPU_SYSTEM_DOWN; + ret =3D false; +out: + up_read(&vcpu->ghvm->status_lock); + return ret; +} + +/** + * gh_vcpu_run() - Request Gunyah to begin scheduling this vCPU. + * @vcpu: The client descriptor that was obtained via gh_vcpu_alloc() + */ +static int gh_vcpu_run(struct gh_vcpu *vcpu) +{ + struct gh_hypercall_vcpu_run_resp vcpu_run_resp; + u64 state_data[3] =3D { 0 }; + enum gh_error gh_error; + int ret =3D 0; + + if (!vcpu->f) + return -ENODEV; + + if (mutex_lock_interruptible(&vcpu->run_lock)) + return -ERESTARTSYS; + + if (!vcpu->rsc) { + ret =3D -ENODEV; + goto out; + } + + switch (vcpu->state) { + case GH_VCPU_UNKNOWN: + if (vcpu->ghvm->vm_status !=3D GH_RM_VM_STATUS_RUNNING) { + /* Check if VM is up. If VM is starting, will block until VM is fully up + * since that thread does down_write. + */ + if (!gh_vcpu_check_system(vcpu)) + goto out; + } + vcpu->state =3D GH_VCPU_READY; + break; + case GH_VCPU_MMIO_READ: + memcpy(&state_data[0], vcpu->vcpu_run->mmio.data, vcpu->mmio_read_len); + vcpu->state =3D GH_VCPU_READY; + break; + case GH_VCPU_SYSTEM_DOWN: + goto out; + default: + break; + } + + while (!ret && !signal_pending(current)) { + if (vcpu->vcpu_run->immediate_exit) { + ret =3D -EINTR; + goto out; + } + + gh_error =3D gh_hypercall_vcpu_run(vcpu->rsc->capid, state_data, &vcpu_r= un_resp); + if (gh_error =3D=3D GH_ERROR_OK) { + ret =3D 0; + switch (vcpu_run_resp.state) { + case GH_VCPU_STATE_READY: + if (need_resched()) + schedule(); + break; + case GH_VCPU_STATE_POWERED_OFF: + /* vcpu might be off because the VM is shut down. + * If so, it won't ever run again: exit back to user + */ + if (!gh_vcpu_check_system(vcpu)) + goto out; + /* Otherwise, another vcpu will turn it on (e.g. by PSCI) + * and hyp sends an interrupt to wake Linux up. + */ + fallthrough; + case GH_VCPU_STATE_EXPECTS_WAKEUP: + ret =3D wait_for_completion_interruptible(&vcpu->ready); + /* reinitialize completion before next hypercall. If we reinitialize + * after the hypercall, interrupt may have already come before + * re-initializing the completion and then end up waiting for + * event that already happened. + */ + reinit_completion(&vcpu->ready); + /* Check system status again. Completion might've + * come from gh_vcpu_rm_notification + */ + if (!ret && !gh_vcpu_check_system(vcpu)) + goto out; + break; + case GH_VCPU_STATE_BLOCKED: + schedule(); + break; + case GH_VCPU_ADDRSPACE_VMMIO_READ: + case GH_VCPU_ADDRSPACE_VMMIO_WRITE: + if (!gh_handle_mmio(vcpu, &vcpu_run_resp)) + goto out; + break; + default: + pr_warn_ratelimited("Unknown vCPU state: %llx\n", + vcpu_run_resp.state); + schedule(); + break; + } + } else if (gh_error =3D=3D GH_ERROR_RETRY) { + schedule(); + ret =3D 0; + } else + ret =3D gh_remap_error(gh_error); + } + +out: + mutex_unlock(&vcpu->run_lock); + + if (signal_pending(current)) + return -ERESTARTSYS; + + return ret; +} + +static long gh_vcpu_ioctl(struct file *filp, unsigned int cmd, unsigned lo= ng arg) +{ + struct gh_vcpu *vcpu =3D filp->private_data; + long ret =3D -EINVAL; + + switch (cmd) { + case GH_VCPU_RUN: + ret =3D gh_vcpu_run(vcpu); + break; + case GH_VCPU_MMAP_SIZE: + ret =3D PAGE_SIZE; + break; + default: + break; + } + return ret; +} + +static int gh_vcpu_release(struct inode *inode, struct file *filp) +{ + struct gh_vcpu *vcpu =3D filp->private_data; + + gh_vm_put(vcpu->ghvm); + kref_put(&vcpu->kref, vcpu_release); + return 0; +} + +static vm_fault_t gh_vcpu_fault(struct vm_fault *vmf) +{ + struct gh_vcpu *vcpu =3D vmf->vma->vm_file->private_data; + struct page *page =3D NULL; + + if (vmf->pgoff =3D=3D 0) + page =3D virt_to_page(vcpu->vcpu_run); + + get_page(page); + vmf->page =3D page; + return 0; +} + +static const struct vm_operations_struct gh_vcpu_ops =3D { + .fault =3D gh_vcpu_fault, +}; + +static int gh_vcpu_mmap(struct file *file, struct vm_area_struct *vma) +{ + vma->vm_ops =3D &gh_vcpu_ops; + return 0; +} + +static const struct file_operations gh_vcpu_fops =3D { + .owner =3D THIS_MODULE, + .unlocked_ioctl =3D gh_vcpu_ioctl, + .release =3D gh_vcpu_release, + .llseek =3D noop_llseek, + .mmap =3D gh_vcpu_mmap, +}; + +static int gh_vcpu_populate(struct gh_vm_resource_ticket *ticket, struct g= h_resource *ghrsc) +{ + struct gh_vcpu *vcpu =3D container_of(ticket, struct gh_vcpu, ticket); + int ret; + + mutex_lock(&vcpu->run_lock); + if (vcpu->rsc) { + ret =3D -1; + goto out; + } + + vcpu->rsc =3D ghrsc; + init_completion(&vcpu->ready); + + ret =3D request_irq(vcpu->rsc->irq, gh_vcpu_irq_handler, IRQF_TRIGGER_RIS= ING, "gh_vcpu", + vcpu); + if (ret) + pr_warn("Failed to request vcpu irq %d: %d", vcpu->rsc->irq, ret); + +out: + mutex_unlock(&vcpu->run_lock); + return ret; +} + +static void gh_vcpu_unpopulate(struct gh_vm_resource_ticket *ticket, + struct gh_resource *ghrsc) +{ + struct gh_vcpu *vcpu =3D container_of(ticket, struct gh_vcpu, ticket); + + vcpu->vcpu_run->immediate_exit =3D true; + complete_all(&vcpu->ready); + mutex_lock(&vcpu->run_lock); + free_irq(vcpu->rsc->irq, vcpu); + vcpu->rsc =3D NULL; + mutex_unlock(&vcpu->run_lock); +} + +static long gh_vcpu_bind(struct gh_vm_function_instance *f) +{ + struct gh_fn_vcpu_arg *arg =3D f->argp; + struct gh_vcpu *vcpu; + char name[MAX_VCPU_NAME]; + struct file *file; + struct page *page; + int fd; + long r; + + if (!gh_api_has_feature(GH_FEATURE_VCPU)) + return -EOPNOTSUPP; + + if (f->arg_size !=3D sizeof(*arg)) + return -EINVAL; + + vcpu =3D kzalloc(sizeof(*vcpu), GFP_KERNEL); + if (!vcpu) + return -ENOMEM; + + vcpu->f =3D f; + f->data =3D vcpu; + mutex_init(&vcpu->run_lock); + kref_init(&vcpu->kref); + + page =3D alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!page) { + r =3D -ENOMEM; + goto err_destroy_vcpu; + } + vcpu->vcpu_run =3D page_address(page); + + vcpu->ticket.resource_type =3D GH_RESOURCE_TYPE_VCPU; + vcpu->ticket.label =3D arg->id; + vcpu->ticket.owner =3D THIS_MODULE; + vcpu->ticket.populate =3D gh_vcpu_populate; + vcpu->ticket.unpopulate =3D gh_vcpu_unpopulate; + + r =3D gh_vm_add_resource_ticket(f->ghvm, &vcpu->ticket); + if (r) + goto err_destroy_page; + + fd =3D get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) { + r =3D fd; + goto err_remove_vcpu; + } + + if (!gh_vm_get(f->ghvm)) { + r =3D -ENODEV; + goto err_put_fd; + } + vcpu->ghvm =3D f->ghvm; + + vcpu->nb.notifier_call =3D gh_vcpu_rm_notification; + /* Ensure we run after the vm_mgr handles the notification and does + * any necessary state changes. We wake up to check the new state. + */ + vcpu->nb.priority =3D -1; + r =3D gh_rm_notifier_register(f->rm, &vcpu->nb); + if (r) + goto err_put_gh_vm; + + kref_get(&vcpu->kref); + snprintf(name, sizeof(name), "gh-vcpu:%d", vcpu->ticket.label); + file =3D anon_inode_getfile(name, &gh_vcpu_fops, vcpu, O_RDWR); + if (IS_ERR(file)) { + r =3D PTR_ERR(file); + goto err_notifier; + } + + fd_install(fd, file); + + return fd; +err_notifier: + gh_rm_notifier_unregister(f->rm, &vcpu->nb); +err_put_gh_vm: + gh_vm_put(vcpu->ghvm); +err_put_fd: + put_unused_fd(fd); +err_remove_vcpu: + gh_vm_remove_resource_ticket(f->ghvm, &vcpu->ticket); +err_destroy_page: + free_page((unsigned long)vcpu->vcpu_run); +err_destroy_vcpu: + kfree(vcpu); + return r; +} + +static void gh_vcpu_unbind(struct gh_vm_function_instance *f) +{ + struct gh_vcpu *vcpu =3D f->data; + + gh_rm_notifier_unregister(f->rm, &vcpu->nb); + gh_vm_remove_resource_ticket(vcpu->f->ghvm, &vcpu->ticket); + vcpu->f =3D NULL; + + kref_put(&vcpu->kref, vcpu_release); +} + +DECLARE_GH_VM_FUNCTION_INIT(vcpu, GH_FN_VCPU, gh_vcpu_bind, gh_vcpu_unbind= ); +MODULE_DESCRIPTION("Gunyah vCPU Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index b31fac15ff45..d453d902847e 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -354,6 +354,10 @@ static int gh_vm_rm_notification_exited(struct gh_vm *= ghvm, void *data) =20 down_write(&ghvm->status_lock); ghvm->vm_status =3D GH_RM_VM_STATUS_EXITED; + ghvm->exit_info.type =3D le16_to_cpu(payload->exit_type); + ghvm->exit_info.reason_size =3D le32_to_cpu(payload->exit_reason_size); + memcpy(&ghvm->exit_info.reason, payload->exit_reason, + min(GH_VM_MAX_EXIT_REASON_SIZE, ghvm->exit_info.reason_size)); up_write(&ghvm->status_lock); =20 return NOTIFY_DONE; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 9c1046af80ed..df78756639b6 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -45,6 +45,7 @@ struct gh_vm { enum gh_rm_vm_status vm_status; wait_queue_head_t vm_status_wait; struct rw_semaphore status_lock; + struct gh_vm_exit_info exit_info; =20 struct work_struct free_work; struct kref kref; diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 3e706b59d2c0..37f1e2c822ce 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -175,4 +175,12 @@ enum gh_error gh_hypercall_msgq_send(u64 capid, size_t= size, void *buff, int tx_ enum gh_error gh_hypercall_msgq_recv(u64 capid, void *buff, size_t size, s= ize_t *recv_size, bool *ready); =20 +struct gh_hypercall_vcpu_run_resp { + u64 state; + u64 state_data[3]; +}; + +enum gh_error gh_hypercall_vcpu_run(u64 capid, u64 *resume_data, + struct gh_hypercall_vcpu_run_resp *resp); + #endif diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index caeb3b3a3e9a..e52265fa5715 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -62,8 +62,32 @@ struct gh_vm_dtb_config { =20 #define GH_VM_START _IO(GH_IOCTL_TYPE, 0x3) =20 +/** + * GH_FN_VCPU - create a vCPU instance to control a vCPU + * + * gh_fn_desc is filled with &struct gh_fn_vcpu_arg + * + * The vcpu type will register with the VM Manager to expect to control + * vCPU number `vcpu_id`. It returns a file descriptor allowing interactio= n with + * the vCPU. See the Gunyah vCPU API description sections for interacting = with + * the Gunyah vCPU file descriptors. + * + * Return: file descriptor to manipulate the vcpu. See GH_VCPU_* ioctls + */ +#define GH_FN_VCPU 1 + #define GH_FN_MAX_ARG_SIZE 256 =20 +/** + * struct gh_fn_vcpu_arg - Arguments to create a vCPU + * @id: vcpu id + */ +struct gh_fn_vcpu_arg { + __u32 id; +}; + +#define GH_IRQFD_LEVEL (1UL << 0) + /** * struct gh_fn_desc - Arguments to create a VM function * @type: Type of the function. See GH_FN_* macro for supported types @@ -79,4 +103,88 @@ struct gh_fn_desc { #define GH_VM_ADD_FUNCTION _IOW(GH_IOCTL_TYPE, 0x4, struct gh_fn_desc) #define GH_VM_REMOVE_FUNCTION _IOW(GH_IOCTL_TYPE, 0x7, struct gh_fn_desc) =20 +enum gh_vm_status { + GH_VM_STATUS_LOAD_FAILED =3D 1, +#define GH_VM_STATUS_LOAD_FAILED GH_VM_STATUS_LOAD_FAILED + GH_VM_STATUS_EXITED =3D 2, +#define GH_VM_STATUS_EXITED GH_VM_STATUS_EXITED + GH_VM_STATUS_CRASHED =3D 3, +#define GH_VM_STATUS_CRASHED GH_VM_STATUS_CRASHED +}; + +/* + * Gunyah presently sends max 4 bytes of exit_reason. + * If that changes, this macro can be safely increased without breaking + * userspace so long as struct gh_vcpu_run < PAGE_SIZE. + */ +#define GH_VM_MAX_EXIT_REASON_SIZE 8u + +/** + * struct gh_vm_exit_info - Reason for VM exit as reported by Gunyah + * See Gunyah documentation for values. + * @type: Describes how VM exited + * @padding: padding bytes + * @reason_size: Number of bytes valid for `reason` + * @reason: See Gunyah documentation for interpretation. Note: these value= s are + * not interpreted by Linux and need to be converted from little-= endian + * as applicable. + */ +struct gh_vm_exit_info { + __u16 type; + __u16 padding; + __u32 reason_size; + __u8 reason[GH_VM_MAX_EXIT_REASON_SIZE]; +}; + +#define GH_VCPU_EXIT_UNKNOWN 0 +#define GH_VCPU_EXIT_MMIO 1 +#define GH_VCPU_EXIT_STATUS 2 + +/** + * struct gh_vcpu_run - Application code obtains a pointer to the gh_vcpu_= run + * structure by mmap()ing a vcpu fd. + * @immediate_exit: polled when scheduling the vcpu. If set, immediately r= eturns -EINTR. + * @padding: padding bytes + * @exit_reason: Set when GH_VCPU_RUN returns successfully and gives reaso= n why + * GH_VCPU_RUN has stopped running the vCPU. + * @mmio: Used when exit_reason =3D=3D GH_VCPU_EXIT_MMIO + * The guest has faulted on an memory-mapped I/O instruction that + * couldn't be satisfied by gunyah. + * @mmio.phys_addr: Address guest tried to access + * @mmio.data: the value that was written if `is_write =3D=3D 1`. Filled by + * user for reads (`is_wite =3D=3D 0`). + * @mmio.len: Length of write. Only the first `len` bytes of `data` + * are considered by Gunyah. + * @mmio.is_write: 1 if VM tried to perform a write, 0 for a read + * @status: Used when exit_reason =3D=3D GH_VCPU_EXIT_STATUS. + * The guest VM is no longer runnable. This struct informs why. + * @status.status: See `enum gh_vm_status` for possible values + * @status.exit_info: Used when status =3D=3D GH_VM_STATUS_EXITED + */ +struct gh_vcpu_run { + /* in */ + __u8 immediate_exit; + __u8 padding[7]; + + /* out */ + __u32 exit_reason; + + union { + struct { + __u64 phys_addr; + __u8 data[8]; + __u32 len; + __u8 is_write; + } mmio; + + struct { + enum gh_vm_status status; + struct gh_vm_exit_info exit_info; + } status; + }; +}; + +#define GH_VCPU_RUN _IO(GH_IOCTL_TYPE, 0x5) +#define GH_VCPU_MMAP_SIZE _IO(GH_IOCTL_TYPE, 0x6) + #endif --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A23AC6FD18 for ; Sat, 4 Mar 2023 01:08:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229871AbjCDBIj (ORCPT ); Fri, 3 Mar 2023 20:08:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229951AbjCDBIT (ORCPT ); Fri, 3 Mar 2023 20:08:19 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4445C6781A; Fri, 3 Mar 2023 17:07:50 -0800 (PST) Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240F8Vi019324; Sat, 4 Mar 2023 01:07:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=DfRy3GvvyBk78GUTsKcTklgcl7kKoNmh7DRjvVFD040=; b=S79rfEXV4pdWh98610HCh6N3yO9svZkP15hDoFKYqLShwEjFgleT0rUk/YfrQmgXReSq 0VrqkIpN/s8iUmMG0VLwvs2CmMTkfP9Y3BTbRtihotXil4iU5Pdic786+yFOxu15FDSA nfTNKZVwHaIjHk1To1K7PxMlnJZZbej6zCnC5aqK/q5Pw7ZRKDJQ968IKk3GIDJ/UQlr 3RI3y+5/ktS36mI65kTqADynbIeegD8KFcFJJHiD9SHWKyQzXlv4KUxjGtcxfySqkmNG hVovQ+2WP5I0TksM2g852i4ctoIWB98iSHWRt6aEIb7DORdgKq9sOGVTty1vGQL0yCa4 ow== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3c8htrd2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:20 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417JOT029048 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:19 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.986.41; Fri, 3 Mar 2023 17:07:18 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Catalin Marinas , Will Deacon CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , Bagas Sanjaya , Andy Gross , Jassi Brar , , , , , Subject: [PATCH v11 23/26] virt: gunyah: Add hypercalls for sending doorbell Date: Fri, 3 Mar 2023 17:06:29 -0800 Message-ID: <20230304010632.2127470-24-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: IiboxbtkbH4HJrmtSGSyH0Ln5QlJ8zUO X-Proofpoint-ORIG-GUID: IiboxbtkbH4HJrmtSGSyH0Ln5QlJ8zUO X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 phishscore=0 malwarescore=0 mlxlogscore=766 priorityscore=1501 impostorscore=0 mlxscore=0 bulkscore=0 suspectscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Gunyah doorbells allow two virtual machines to signal each other using interrupts. Add the hypercalls needed to assert the interrupt. Signed-off-by: Elliot Berman --- arch/arm64/gunyah/gunyah_hypercall.c | 25 +++++++++++++++++++++++++ include/linux/gunyah.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/arch/arm64/gunyah/gunyah_hypercall.c b/arch/arm64/gunyah/gunya= h_hypercall.c index f01f5cec4d23..0f1cdb706e91 100644 --- a/arch/arm64/gunyah/gunyah_hypercall.c +++ b/arch/arm64/gunyah/gunyah_hypercall.c @@ -41,6 +41,8 @@ EXPORT_SYMBOL_GPL(arch_is_gh_guest); fn) =20 #define GH_HYPERCALL_HYP_IDENTIFY GH_HYPERCALL(0x8000) +#define GH_HYPERCALL_BELL_SEND GH_HYPERCALL(0x8012) +#define GH_HYPERCALL_BELL_SET_MASK GH_HYPERCALL(0x8015) #define GH_HYPERCALL_MSGQ_SEND GH_HYPERCALL(0x801B) #define GH_HYPERCALL_MSGQ_RECV GH_HYPERCALL(0x801C) #define GH_HYPERCALL_VCPU_RUN GH_HYPERCALL(0x8065) @@ -63,6 +65,29 @@ void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_i= dentify_resp *hyp_identi } EXPORT_SYMBOL_GPL(gh_hypercall_hyp_identify); =20 +enum gh_error gh_hypercall_bell_send(u64 capid, u64 new_flags, u64 *old_fl= ags) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_hvc(GH_HYPERCALL_BELL_SEND, capid, new_flags, 0, &res); + + if (res.a0 =3D=3D GH_ERROR_OK) + *old_flags =3D res.a1; + + return res.a0; +} +EXPORT_SYMBOL_GPL(gh_hypercall_bell_send); + +enum gh_error gh_hypercall_bell_set_mask(u64 capid, u64 enable_mask, u64 a= ck_mask) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_hvc(GH_HYPERCALL_BELL_SET_MASK, capid, enable_mask, ack_mas= k, 0, &res); + + return res.a0; +} +EXPORT_SYMBOL_GPL(gh_hypercall_bell_set_mask); + enum gh_error gh_hypercall_msgq_send(u64 capid, size_t size, void *buff, i= nt tx_flags, bool *ready) { struct arm_smccc_res res; diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 37f1e2c822ce..63395dacc1a8 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -169,6 +169,9 @@ struct gh_hypercall_hyp_identify_resp { =20 void gh_hypercall_hyp_identify(struct gh_hypercall_hyp_identify_resp *hyp_= identity); =20 +enum gh_error gh_hypercall_bell_send(u64 capid, u64 new_flags, u64 *old_fl= ags); +enum gh_error gh_hypercall_bell_set_mask(u64 capid, u64 enable_mask, u64 a= ck_mask); + #define GH_HYPERCALL_MSGQ_TX_FLAGS_PUSH BIT(0) =20 enum gh_error gh_hypercall_msgq_send(u64 capid, size_t size, void *buff, i= nt tx_flags, bool *ready); --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CF76C678DB for ; Sat, 4 Mar 2023 01:09:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229887AbjCDBJg (ORCPT ); Fri, 3 Mar 2023 20:09:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbjCDBJ3 (ORCPT ); Fri, 3 Mar 2023 20:09:29 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3B7ABB9C; Fri, 3 Mar 2023 17:08:02 -0800 (PST) Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240J06l028516; Sat, 4 Mar 2023 01:07:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=vpOwzZQLrpmHaihmYhdcY2zbPs5StQEqOQFTEOCreb8=; b=R5DuSNfZHSZwnUGbomtO0mkgWDuzJsQnYZX1REN5qfI7uKLAFyeff6ZmFxiprfmakHwy u7YTUqKs+qdluMTXDrs0p5ifaus9Y7lxUJzRbS8o+fsfbLG3pliXGm4QGRVG0DFLZhJ8 6nhNqizl/9eM+PHRTm42iUXZy8L1I25ss5DbTllaRsziYRdp71IQMECMLssutgZIu/AM qiYAcakUWQ3DLF9hER3a+TM03blzsf3RLWVbqwV8EcqT62gAKC/Q59gKS288msxjHATP 8SSIemUIBOYRs7QwYpqV8vAPAKROUKd7XmuavXTMU1ftBxAhvEnSlXF/jI3bg8mvAJEt ig== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p36mybgjf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:21 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417LVl026133 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:21 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.986.41; Fri, 3 Mar 2023 17:07:20 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 24/26] virt: gunyah: Add irqfd interface Date: Fri, 3 Mar 2023 17:06:30 -0800 Message-ID: <20230304010632.2127470-25-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: unE253sIrpOs0k8Rbcml-qsvAYKgF2b7 X-Proofpoint-ORIG-GUID: unE253sIrpOs0k8Rbcml-qsvAYKgF2b7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 adultscore=0 clxscore=1015 mlxscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 priorityscore=1501 spamscore=0 malwarescore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/vm-manager.rst | 2 +- drivers/virt/gunyah/Kconfig | 9 ++ drivers/virt/gunyah/Makefile | 1 + drivers/virt/gunyah/gunyah_irqfd.c | 164 +++++++++++++++++++++++ include/linux/gunyah.h | 5 + include/uapi/linux/gunyah.h | 30 +++++ 6 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/gunyah/gunyah_irqfd.c diff --git a/Documentation/virt/gunyah/vm-manager.rst b/Documentation/virt/= gunyah/vm-manager.rst index 83d326b0d11f..a1dd70f0cbf6 100644 --- a/Documentation/virt/gunyah/vm-manager.rst +++ b/Documentation/virt/gunyah/vm-manager.rst @@ -124,7 +124,7 @@ the VM starts. The possible types are documented below: =20 .. kernel-doc:: include/uapi/linux/gunyah.h - :identifiers: GH_FN_VCPU gh_fn_vcpu_arg + :identifiers: GH_FN_VCPU gh_fn_vcpu_arg GH_FN_IRQFD gh_fn_irqfd_arg =20 Gunyah VCPU API Descriptions ---------------------------- diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index 4c1c6110b50e..2cde24d429d1 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -26,3 +26,12 @@ config GUNYAH_VCPU VMMs can also handle stage 2 faults of the vCPUs. =20 Say Y/M here if unsure and you want to support Gunyah VMMs. + +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 2d1b604a7b03..6cf756bfa3c2 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -7,3 +7,4 @@ gunyah_rsc_mgr-y +=3D rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_m= gr_mm.o obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o =20 obj-$(CONFIG_GUNYAH_VCPU) +=3D gunyah_vcpu.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 000000000000..38e5fe266b00 --- /dev/null +++ b/drivers/virt/gunyah/gunyah_irqfd.c @@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct gh_irqfd { + struct gh_resource *ghrsc; + struct gh_vm_resource_ticket ticket; + struct gh_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 gh_irqfd *irqfd =3D container_of(wait, struct gh_irqfd, wait); + __poll_t flags =3D key_to_poll(key); + u64 enable_mask =3D GH_BELL_NONBLOCK; + u64 old_flags; + int ret =3D 0; + + if (flags & EPOLLIN) { + if (irqfd->ghrsc) { + ret =3D gh_hypercall_bell_send(irqfd->ghrsc->capid, enable_mask, &old_f= lags); + 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 gh_irqfd *irq_ctx =3D container_of(pt, struct gh_irqfd, pt); + + add_wait_queue(wqh, &irq_ctx->wait); +} + +static int gh_irqfd_populate(struct gh_vm_resource_ticket *ticket, struct = gh_resource *ghrsc) +{ + struct gh_irqfd *irqfd =3D container_of(ticket, struct gh_irqfd, ticket); + u64 enable_mask =3D GH_BELL_NONBLOCK; + u64 ack_mask =3D ~0; + int ret =3D 0; + + 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 -1; + } + + irqfd->ghrsc =3D ghrsc; + if (irqfd->level) { + ret =3D gh_hypercall_bell_set_mask(irqfd->ghrsc->capid, enable_mask, ack= _mask); + 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 0; +} + +static void gh_irqfd_unpopulate(struct gh_vm_resource_ticket *ticket, stru= ct gh_resource *ghrsc) +{ + struct gh_irqfd *irqfd =3D container_of(ticket, struct gh_irqfd, ticket); + u64 cnt; + + eventfd_ctx_remove_wait_queue(irqfd->ctx, &irqfd->wait, &cnt); +} + +static long gh_irqfd_bind(struct gh_vm_function_instance *f) +{ + struct gh_fn_irqfd_arg *args =3D f->argp; + struct gh_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 & ~GH_IRQFD_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 & GH_IRQFD_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 GH_RESOURCE_TYPE_BELL_TX; + irqfd->ticket.label =3D args->label; + irqfd->ticket.owner =3D THIS_MODULE; + irqfd->ticket.populate =3D gh_irqfd_populate; + irqfd->ticket.unpopulate =3D gh_irqfd_unpopulate; + + r =3D gh_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 gh_irqfd_unbind(struct gh_vm_function_instance *f) +{ + struct gh_irqfd *irqfd =3D f->data; + + gh_vm_remove_resource_ticket(irqfd->f->ghvm, &irqfd->ticket); + eventfd_ctx_put(irqfd->ctx); + kfree(irqfd); +} + +DECLARE_GH_VM_FUNCTION_INIT(irqfd, GH_FN_IRQFD, gh_irqfd_bind, gh_irqfd_un= bind); +MODULE_DESCRIPTION("Gunyah irqfds"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h index 63395dacc1a8..0344b6988cfa 100644 --- a/include/linux/gunyah.h +++ b/include/linux/gunyah.h @@ -33,6 +33,11 @@ struct gh_resource { u32 rm_label; }; =20 +/** + * Gunyah Doorbells + */ +#define GH_BELL_NONBLOCK BIT(32) + /** * Gunyah Message Queues */ diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index e52265fa5715..5617dadc1c7b 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -76,6 +76,19 @@ struct gh_vm_dtb_config { */ #define GH_FN_VCPU 1 =20 +/** + * GH_FN_IRQFD - register eventfd to assert a Gunyah doorbell + * + * gh_fn_desc is filled with gh_fn_irqfd_arg + * + * 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. + * + * Return: 0 + */ +#define GH_FN_IRQFD 2 + #define GH_FN_MAX_ARG_SIZE 256 =20 /** @@ -88,6 +101,23 @@ struct gh_fn_vcpu_arg { =20 #define GH_IRQFD_LEVEL (1UL << 0) =20 +/** + * struct gh_fn_irqfd_arg - Arguments to create an irqfd function + * @fd: an eventfd which when written to will raise a doorbell + * @label: Label of the doorbell created on the guest VM + * @flags: GH_IRQFD_LEVEL configures the corresponding doorbell to behave + * like a level triggered interrupt. + * @padding: padding bytes + */ +struct gh_fn_irqfd_arg { + __u32 fd; + __u32 label; + __u32 flags; + __u32 padding; +}; + +#define GH_IOEVENTFD_DATAMATCH (1UL << 0) + /** * struct gh_fn_desc - Arguments to create a VM function * @type: Type of the function. See GH_FN_* macro for supported types --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AE86C678DB for ; Sat, 4 Mar 2023 01:35:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229773AbjCDBfw (ORCPT ); Fri, 3 Mar 2023 20:35:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbjCDBft (ORCPT ); Fri, 3 Mar 2023 20:35:49 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA6E364A88; Fri, 3 Mar 2023 17:35:47 -0800 (PST) Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240r4ow007584; Sat, 4 Mar 2023 01:07:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=wvhDtInPmC0leqk/Kbs8lbSLrxQCbpRgDn//zaKP+3A=; b=N4QIX8FVeV//H/Tk6LCPslaXvPIyP/w97rv2LggkAF+fdV9KtVgsNEeMFK5VCWd+wx5U aJAt8n7ppAp3cAEUXdJ4O+wl7AVblYvqdHA5R7ivr5utiCGoLHSrU8nifzhybmWxrsxW iuIV5gB6gSI3iq46OXwcdp9KQlS5rT6OciIqjViCqaHcXoE+K/ixRyLy5Cr2xbhfG8md oLHR2J93mFuJHsm6O6sz6tAJe5xqtO+JBsy6Rny40zBLeTn+PBzDkUv/V0+E6Zl0q+2F QtZB6tDmhM8kBI7J6BYtf+vGv4vZbJy3II/qdS4H05jOqU5n3546zPprMqBdey53d4Iv Ow== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p32ty430h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:23 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 32417Mmd019390 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:22 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.986.41; Fri, 3 Mar 2023 17:07:22 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla , Elliot Berman , Prakruthi Deepak Heragu , Jonathan Corbet CC: Murali Nalajala , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Dmitry Baryshkov , Bjorn Andersson , "Konrad Dybcio" , Arnd Bergmann , "Greg Kroah-Hartman" , Rob Herring , Krzysztof Kozlowski , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 25/26] virt: gunyah: Add ioeventfd Date: Fri, 3 Mar 2023 17:06:31 -0800 Message-ID: <20230304010632.2127470-26-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: 0ezja0ChU0czpuzouca54EhuQjAyN310 X-Proofpoint-ORIG-GUID: 0ezja0ChU0czpuzouca54EhuQjAyN310 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 spamscore=0 mlxlogscore=999 lowpriorityscore=0 suspectscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 adultscore=0 impostorscore=0 phishscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040004 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Allow userspace to attach an ioeventfd to an mmio address within the guest. Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- Documentation/virt/gunyah/vm-manager.rst | 2 +- drivers/virt/gunyah/Kconfig | 9 ++ drivers/virt/gunyah/Makefile | 1 + drivers/virt/gunyah/gunyah_ioeventfd.c | 117 +++++++++++++++++++++++ include/uapi/linux/gunyah.h | 37 +++++++ 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/gunyah/gunyah_ioeventfd.c diff --git a/Documentation/virt/gunyah/vm-manager.rst b/Documentation/virt/= gunyah/vm-manager.rst index a1dd70f0cbf6..cd41a705849f 100644 --- a/Documentation/virt/gunyah/vm-manager.rst +++ b/Documentation/virt/gunyah/vm-manager.rst @@ -124,7 +124,7 @@ the VM starts. The possible types are documented below: =20 .. kernel-doc:: include/uapi/linux/gunyah.h - :identifiers: GH_FN_VCPU gh_fn_vcpu_arg GH_FN_IRQFD gh_fn_irqfd_arg + :identifiers: GH_FN_VCPU gh_fn_vcpu_arg GH_FN_IRQFD gh_fn_irqfd_arg GH_= FN_IOEVENTFD gh_fn_ioeventfd_arg =20 Gunyah VCPU API Descriptions ---------------------------- diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index 2cde24d429d1..bd8e31184962 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -35,3 +35,12 @@ config GUNYAH_IRQFD on Gunyah virtual machine. =20 Say Y/M here if unsure and you want to support Gunyah VMMs. + +config GUNYAH_IOEVENTFD + tristate "Gunyah ioeventfd interface" + depends on GUNYAH + help + Enable kernel support for creating ioeventfds which can alert userspace + when a Gunyah virtual machine accesses a memory address. + + 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 6cf756bfa3c2..7347b1470491 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -8,3 +8,4 @@ obj-$(CONFIG_GUNYAH) +=3D gunyah_rsc_mgr.o =20 obj-$(CONFIG_GUNYAH_VCPU) +=3D gunyah_vcpu.o obj-$(CONFIG_GUNYAH_IRQFD) +=3D gunyah_irqfd.o +obj-$(CONFIG_GUNYAH_IOEVENTFD) +=3D gunyah_ioeventfd.o diff --git a/drivers/virt/gunyah/gunyah_ioeventfd.c b/drivers/virt/gunyah/g= unyah_ioeventfd.c new file mode 100644 index 000000000000..517f55706ed9 --- /dev/null +++ b/drivers/virt/gunyah/gunyah_ioeventfd.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights res= erved. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +struct gh_ioeventfd { + struct gh_vm_function_instance *f; + struct gh_vm_io_handler io_handler; + + struct eventfd_ctx *ctx; +}; + +static int gh_write_ioeventfd(struct gh_vm_io_handler *io_dev, u64 addr, u= 32 len, u64 data) +{ + struct gh_ioeventfd *iofd =3D container_of(io_dev, struct gh_ioeventfd, i= o_handler); + + eventfd_signal(iofd->ctx, 1); + return 0; +} + +static struct gh_vm_io_handler_ops io_ops =3D { + .write =3D gh_write_ioeventfd, +}; + +static long gh_ioeventfd_bind(struct gh_vm_function_instance *f) +{ + const struct gh_fn_ioeventfd_arg *args =3D f->argp; + struct eventfd_ctx *ctx =3D NULL; + struct gh_ioeventfd *iofd; + int ret; + + if (f->arg_size !=3D sizeof(*args)) + return -EINVAL; + + /* must be natural-word sized, or 0 to ignore length */ + switch (args->len) { + case 0: + case 1: + case 2: + case 4: + case 8: + break; + default: + return -EINVAL; + } + + /* check for range overflow */ + if (args->addr + args->len < args->addr) + return -EINVAL; + + /* ioeventfd with no length can't be combined with DATAMATCH */ + if (!args->len && (args->flags & GH_IOEVENTFD_DATAMATCH)) + return -EINVAL; + + /* All other flag bits are reserved for future use */ + if (args->flags & ~GH_IOEVENTFD_DATAMATCH) + return -EINVAL; + + ctx =3D eventfd_ctx_fdget(args->fd); + if (IS_ERR(ctx)) + return PTR_ERR(ctx); + + iofd =3D kzalloc(sizeof(*iofd), GFP_KERNEL); + if (!iofd) { + ret =3D -ENOMEM; + goto err_eventfd; + } + + f->data =3D iofd; + iofd->f =3D f; + + iofd->ctx =3D ctx; + + if (args->flags & GH_IOEVENTFD_DATAMATCH) { + iofd->io_handler.datamatch =3D true; + iofd->io_handler.len =3D args->len; + iofd->io_handler.data =3D args->datamatch; + } + iofd->io_handler.addr =3D args->addr; + iofd->io_handler.ops =3D &io_ops; + + ret =3D gh_vm_add_io_handler(f->ghvm, &iofd->io_handler); + if (ret) + goto err_io_dev_add; + + return 0; + +err_io_dev_add: + kfree(iofd); +err_eventfd: + eventfd_ctx_put(ctx); + return ret; +} + +static void gh_ioevent_unbind(struct gh_vm_function_instance *f) +{ + struct gh_ioeventfd *iofd =3D f->data; + + eventfd_ctx_put(iofd->ctx); + gh_vm_remove_io_handler(iofd->f->ghvm, &iofd->io_handler); + kfree(iofd); +} + +DECLARE_GH_VM_FUNCTION_INIT(ioeventfd, GH_FN_IOEVENTFD, + gh_ioeventfd_bind, gh_ioevent_unbind); +MODULE_DESCRIPTION("Gunyah ioeventfds"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index 5617dadc1c7b..f8482ff4cc55 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -89,6 +89,23 @@ struct gh_vm_dtb_config { */ #define GH_FN_IRQFD 2 =20 +/** + * GH_FN_IOEVENTFD - register ioeventfd to trigger when VM faults on param= eter + * + * gh_fn_desc is filled with gh_fn_ioeventfd_arg + * + * Attaches an ioeventfd to a legal mmio address within the guest. A guest= write + * in the registered address will signal the provided event instead of tri= ggering + * an exit on the GH_VCPU_RUN ioctl. + * + * If GH_IOEVENTFD_DATAMATCH flag is set, the event will be signaled only = if the + * written value to the registered address is equal to datamatch in + * struct gh_fn_ioeventfd_arg. + * + * Return: 0 + */ +#define GH_FN_IOEVENTFD 3 + #define GH_FN_MAX_ARG_SIZE 256 =20 /** @@ -118,6 +135,26 @@ struct gh_fn_irqfd_arg { =20 #define GH_IOEVENTFD_DATAMATCH (1UL << 0) =20 +/** + * struct gh_fn_ioeventfd_arg - Arguments to create an ioeventfd function + * @datamatch: data used when GH_IOEVENTFD_DATAMATCH is set + * @addr: Address in guest memory + * @len: Length of access + * @fd: When ioeventfd is matched, this eventfd is written + * @flags: If GH_IOEVENTFD_DATAMATCH flag is set, the event will be signal= ed + * only if the written value to the registered address is equal to + * @datamatch + * @padding: padding bytes + */ +struct gh_fn_ioeventfd_arg { + __u64 datamatch; + __u64 addr; /* legal mmio address */ + __u32 len; /* 1, 2, 4, or 8 bytes; or 0 to ignore length */ + __s32 fd; + __u32 flags; + __u32 padding; +}; + /** * struct gh_fn_desc - Arguments to create a VM function * @type: Type of the function. See GH_FN_* macro for supported types --=20 2.39.2 From nobody Tue May 21 01:00:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4531FC6FA99 for ; Sat, 4 Mar 2023 01:46:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229552AbjCDBqk (ORCPT ); Fri, 3 Mar 2023 20:46:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbjCDBqi (ORCPT ); Fri, 3 Mar 2023 20:46:38 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 587B46A06A; Fri, 3 Mar 2023 17:46:36 -0800 (PST) Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3240GUiE006914; Sat, 4 Mar 2023 01:07:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=7qJRx+Bc0RxDhyPOVZD28FbA36S3iDuE8wdALiNyenI=; b=J8NQZwKWcYur1s1m5NYeBFjWzz67QB/EHDBVT/F3spr6hb/J8h8v78NZB58g7xzkl6i3 xdGjqV81ROM37+HIP5qSjkfRUvm0wKTN2sIRYYo7d3xq+Q3rWBG6legbaj8l5na9F+M/ u6TCMlax+lj0ParH+o7OlFJLJK/8mtCYF6yB/lWv75EKS7Dkit1adUSWilws77QsFI1e nbezafR1WKsq3vxA7xcfD9DSI+2acnf02jHClK8EJNfipVyfqk4B8GsnP95WBB6ktuP0 F68PbjlZZVpqI5bNpTAKOQQd+Tu5gHB/HfKxS5Gdgab/HE5a6708R7ywVMgRwNP80+Ki lQ== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3p3pu48mb8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 04 Mar 2023 01:07:24 +0000 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 32417NMO019526 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 4 Mar 2023 01:07:23 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.986.41; Fri, 3 Mar 2023 17:07:23 -0800 From: Elliot Berman To: Alex Elder , Srinivas Kandagatla CC: Elliot Berman , Murali Nalajala , Trilok Soni , "Srivatsa Vaddagiri" , Carl van Schaik , Prakruthi Deepak Heragu , Dmitry Baryshkov , Bjorn Andersson , Konrad Dybcio , Arnd Bergmann , Greg Kroah-Hartman , Rob Herring , "Krzysztof Kozlowski" , Jonathan Corbet , Bagas Sanjaya , Will Deacon , Andy Gross , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v11 26/26] MAINTAINERS: Add Gunyah hypervisor drivers section Date: Fri, 3 Mar 2023 17:06:32 -0800 Message-ID: <20230304010632.2127470-27-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230304010632.2127470-1-quic_eberman@quicinc.com> References: <20230304010632.2127470-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.49.16.6] 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: QDQdUgSvbm-CZtNLBf4mE_tACS6T5Faa X-Proofpoint-ORIG-GUID: QDQdUgSvbm-CZtNLBf4mE_tACS6T5Faa X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-03_07,2023-03-03_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=916 priorityscore=1501 suspectscore=0 phishscore=0 malwarescore=0 bulkscore=0 spamscore=0 impostorscore=0 mlxscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303040005 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add myself and Prakruthi as maintainers of Gunyah hypervisor drivers. Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- MAINTAINERS | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index b0db911207ba..26ba59610276 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8916,6 +8916,19 @@ L: linux-efi@vger.kernel.org S: Maintained F: block/partitions/efi.* =20 +GUNYAH HYPERVISOR DRIVER +M: Elliot Berman +M: Prakruthi Deepak Heragu +L: linux-arm-msm@vger.kernel.org +S: Supported +F: Documentation/devicetree/bindings/firmware/gunyah-hypervisor.yaml +F: Documentation/virt/gunyah/ +F: arch/arm64/gunyah/ +F: drivers/mailbox/gunyah-msgq.c +F: drivers/virt/gunyah/ +F: include/linux/gunyah*.h +F: samples/gunyah/ + HABANALABS PCI DRIVER M: Oded Gabbay L: dri-devel@lists.freedesktop.org --=20 2.39.2