From patchwork Mon Dec 19 22:58:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 34823 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2662784wrn; Mon, 19 Dec 2022 15:05:57 -0800 (PST) X-Google-Smtp-Source: AA0mqf7e0Loo58vPnCKGEMxYPCxee9bJ4qA7BEAxIniBQFGQLDWMgwvFU7mCbjV6HY3d7wGVc30o X-Received: by 2002:a17:903:481:b0:189:acbb:5871 with SMTP id jj1-20020a170903048100b00189acbb5871mr42550720plb.68.1671491157452; Mon, 19 Dec 2022 15:05:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671491157; cv=none; d=google.com; s=arc-20160816; b=G1sovTMktw8622xc8L6xx2H3QN4XSFISi514UWg9F8jvYRpCUSzg8tg4KuMuZcBBfg Hm2vplztuhfLueZpIh16OYL13SfJS7mhtI0gTh0/YE4VE6WSAHUvfoO+q3fKGtuB99+p JdBqh+PzRUmx9SqXEeXv7ztvwWAssr1FnSPInq1K5EOlQkdeYt3dJWBlm3zLV9dJCCxQ /yjCSv5nn+q9daJ2XLt5ZZZkGz5w7SdY9DGIwa/HopCaQ/cI8/gxIAkZzOWWsKE32o2o fbjKh1P2PaCv750SISADwAJ9L6Cg4+Cxi6iqxCZqzfdnXqffOHvccLaxRPuXLF320p6A Pupw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8DBfpRw1P55kghMA1skoq1VZPxEn4zlEz457ZcB/ftk=; b=xBz+o0IQuregx/Y+gOOZbE3v192vkUgCzCTl3nZlYmfxpkvOFC9lamRxITU26yy/bN 8RLOEqzYpWmtNWftPi4U6vOhSOsMQP6OvS8M/vqQGTK+NfKZyRk/eV7IAVdtCPbDx2XI 2/VzuDACyLeH1S0+qN3QxnX5yPuhT1vYqYNEws74uZVwyZvIihOJAVHNB/E1Y+llIzbt yI+Tzt8xhDFQ+1jTn+8jueshk1tBohWdiLzxVscOH4gxZ/CPMcgYFih1+2w5Wsl4N675 +PfWiNtYvA1mH1NF/2a0TiMz1g/bKGsWx7VH/oBlH5LDYsUrpo7W25SdQvTCbF53MyIO 9AvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=hCAU7T9R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m8-20020a170902db0800b00176e4ac448esi7176717plx.224.2022.12.19.15.05.44; Mon, 19 Dec 2022 15:05:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=hCAU7T9R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232995AbiLSXF2 (ORCPT + 99 others); Mon, 19 Dec 2022 18:05:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233086AbiLSXD3 (ORCPT ); Mon, 19 Dec 2022 18:03:29 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AFBB15807; Mon, 19 Dec 2022 15:01:09 -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 2BJMvhas000408; Mon, 19 Dec 2022 23:00: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=8DBfpRw1P55kghMA1skoq1VZPxEn4zlEz457ZcB/ftk=; b=hCAU7T9RFl8J9xpxEVKp89JkjYx9olGDDgbTeo3WtVGJLIeh6lUxYXERCeqwHcRizbZj 70mY7avvqVZCBlOd+pv4EVCEvu2ijGEb0dc9uljfTyrLcuqupe3hEL7c7oCPnHdgoOSm Wcg3OS5HJVArgQXGV0wUnu6b0Qvv1SZ7qkWLeen/irfS1ZlHpG46btS/YplTCt6XTvah e5vNUyWcJN++vi0n4MBf+Pj2Y0RdPTPRmLPAX0gm2DQn7AAI3MVX2p8BAEl/CwLBf5Kj uHo1Lt9EMqdxyK7u43zulEb+Su6QESmjbVMrtI1IyjGv03PP0k/4SbmXHYOrGtjXMNBY Pg== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3mh4sedk88-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Dec 2022 23:00: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 2BJN0ow7007956 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 19 Dec 2022 23:00:50 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.36; Mon, 19 Dec 2022 15:00:50 -0800 From: Elliot Berman To: Bjorn Andersson , Elliot Berman , Murali Nalajala CC: Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Prakruthi Deepak Heragu , Arnd Bergmann , Greg Kroah-Hartman , Rob Herring , Krzysztof Kozlowski , Jonathan Corbet , "Bagas Sanjaya" , Catalin Marinas , Will Deacon , Jassi Brar , "Sudeep Holla" , Mark Rutland , "Lorenzo Pieralisi" , Dmitry Baryshkov , , , , , , Subject: [PATCH v8 24/28] virt: gunyah: Add IO handlers Date: Mon, 19 Dec 2022 14:58:45 -0800 Message-ID: <20221219225850.2397345-25-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221219225850.2397345-1-quic_eberman@quicinc.com> References: <20221219225850.2397345-1-quic_eberman@quicinc.com> MIME-Version: 1.0 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: 4oHTluD6IInIRKW9dxLPspFOq9X51blc X-Proofpoint-ORIG-GUID: 4oHTluD6IInIRKW9dxLPspFOq9X51blc X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-12-19_01,2022-12-15_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxlogscore=999 mlxscore=0 suspectscore=0 bulkscore=0 spamscore=0 phishscore=0 impostorscore=0 priorityscore=1501 adultscore=0 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2212190202 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752685511570181079?= X-GMAIL-MSGID: =?utf-8?q?1752685511570181079?= 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 | 85 +++++++++++++++++++++++++++++++++++ drivers/virt/gunyah/vm_mgr.h | 4 ++ include/linux/gunyah_vm_mgr.h | 25 +++++++++++ 3 files changed, 114 insertions(+) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 3328b84fa681..dcca255bc11f 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -165,6 +165,91 @@ void ghvm_remove_resource_ticket(struct gunyah_vm *ghvm, struct gunyah_vm_resour } EXPORT_SYMBOL_GPL(ghvm_remove_resource_ticket); +static inline bool gh_vm_io_handler_matches(struct gunyah_vm_io_handler *io_hdlr, u64 addr, + u64 len, u64 data) +{ + u64 mask = BIT_ULL(io_hdlr->len * BITS_PER_BYTE) - 1; + + if (io_hdlr->addr != addr) + return false; + + if (!io_hdlr->datamatch) + return true; + + if (io_hdlr->len != len) + return false; + + return (data & mask) == (io_hdlr->data & mask); +} + +static struct gunyah_vm_io_handler *gh_vm_mgr_find_io_hdlr(struct gunyah_vm *ghvm, u64 addr, + u64 len, u64 data) +{ + struct gunyah_vm_io_handler *io_hdlr = NULL; + struct rb_node *root = NULL; + + root = ghvm->mmio_handler_root.rb_node; + while (root) { + io_hdlr = rb_entry(root, struct gunyah_vm_io_handler, node); + if (addr < io_hdlr->addr) + root = root->rb_left; + else if (addr > io_hdlr->addr) + root = root->rb_right; + else if (gh_vm_io_handler_matches(io_hdlr, addr, len, data)) + return io_hdlr; + } + return NULL; +} + +int gh_vm_mgr_mmio_write(struct gunyah_vm *ghvm, u64 addr, u32 len, u64 data) +{ + struct gunyah_vm_io_handler *io_hdlr = NULL; + + io_hdlr = gh_vm_mgr_find_io_hdlr(ghvm, addr, len, data); + if (!io_hdlr) + return -ENODEV; + + if (io_hdlr->ops && io_hdlr->ops->write) + return io_hdlr->ops->write(io_hdlr, addr, len, data); + + return -ENODEV; +} +EXPORT_SYMBOL_GPL(gh_vm_mgr_mmio_write); + +int gh_vm_mgr_add_io_handler(struct gunyah_vm *ghvm, struct gunyah_vm_io_handler *io_hdlr) +{ + struct rb_node **root, *parent = NULL; + + if (io_hdlr->datamatch && + (!io_hdlr->len || io_hdlr->len > (sizeof(io_hdlr->data) * BITS_PER_BYTE))) + return -EINVAL; + + root = &ghvm->mmio_handler_root.rb_node; + while (*root) { + struct gunyah_vm_io_handler *curr = rb_entry(*root, struct gunyah_vm_io_handler, + node); + + parent = *root; + if (io_hdlr->addr < curr->addr) + root = &((*root)->rb_left); + else if (io_hdlr->addr > curr->addr) + root = &((*root)->rb_right); + else + return -EEXIST; + } + + rb_link_node(&io_hdlr->node, parent, root); + rb_insert_color(&io_hdlr->node, &ghvm->mmio_handler_root); + return 0; +} +EXPORT_SYMBOL_GPL(gh_vm_mgr_add_io_handler); + +void gh_vm_mgr_remove_io_handler(struct gunyah_vm *ghvm, struct gunyah_vm_io_handler *io_hdlr) +{ + rb_erase(&io_hdlr->node, &ghvm->mmio_handler_root); +} +EXPORT_SYMBOL_GPL(gh_vm_mgr_remove_io_handler); + static void ghvm_put(struct kref *kref) { struct gunyah_vm *ghvm = container_of(kref, struct gunyah_vm, kref); diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 8a71f97960bc..46ba2e530be2 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -58,6 +58,8 @@ struct gunyah_vm { struct list_head resources; struct list_head resource_tickets; + struct rb_root mmio_handler_root; + struct list_head functions; }; @@ -68,4 +70,6 @@ struct gunyah_vm_memory_mapping *gh_vm_mem_mapping_find(struct gunyah_vm *ghvm, struct gunyah_vm_memory_mapping *gh_vm_mem_mapping_find_mapping(struct gunyah_vm *ghvm, u64 gpa, u32 size); +int gh_vm_mgr_mmio_write(struct gunyah_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 3e6e6e0ab7f0..5b17cb54ba01 100644 --- a/include/linux/gunyah_vm_mgr.h +++ b/include/linux/gunyah_vm_mgr.h @@ -79,4 +79,29 @@ struct gunyah_vm_resource_ticket { int ghvm_add_resource_ticket(struct gunyah_vm *ghvm, struct gunyah_vm_resource_ticket *ticket); void ghvm_remove_resource_ticket(struct gunyah_vm *ghvm, struct gunyah_vm_resource_ticket *ticket); +/* + * gunyah_vm_io_handler contains the info about an io device and its associated + * addr and the ops associated with the io device. + */ +struct gunyah_vm_io_handler { + struct rb_node node; + u64 addr; + + bool datamatch; + u8 len; + u64 data; + struct gunyah_vm_io_handler_ops *ops; +}; + +/* + * gunyah_vm_io_handler_ops contains function pointers associated with an iodevice. + */ +struct gunyah_vm_io_handler_ops { + int (*read)(struct gunyah_vm_io_handler *io_dev, u64 addr, u32 len, u64 data); + int (*write)(struct gunyah_vm_io_handler *io_dev, u64 addr, u32 len, u64 data); +}; + +int gh_vm_mgr_add_io_handler(struct gunyah_vm *ghvm, struct gunyah_vm_io_handler *io_dev); +void gh_vm_mgr_remove_io_handler(struct gunyah_vm *ghvm, struct gunyah_vm_io_handler *io_dev); + #endif