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 1034CC64ED9 for ; Tue, 14 Feb 2023 21:27:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233106AbjBNV1v (ORCPT ); Tue, 14 Feb 2023 16:27:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233007AbjBNV1l (ORCPT ); Tue, 14 Feb 2023 16:27:41 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E084303D5; Tue, 14 Feb 2023 13:27:15 -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 31E8L90K016522; Tue, 14 Feb 2023 21:26: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=77sqya39xgyq0GNOrJXOch0bYFFw7TBtX9CjmVbNLUE=; b=lh457bHSD/J24VFSE3nJOJAr0pqDlDES4PAdnhrxLXfP6CXiwCtIb6TfTWgh88G8FxmF GYkOZl9uZtMHzLwLp33BeEiT5QtyJs0uQL5Jqej6qKgml6psXc0Q3bXQSVjFVLzLemI4 2OygWRz4OIJOe57BxgCylmrNW1cStU1aqqHqqgqIR1xRzrxPn4Q9s8pmk3HpoSftp6hk /oEDtSb7rC0sQYn0dRBuB3dNG/fVzh8uPNt/+g7GE+3tB7GGz6lnB4jowrCgIubByeKQ NfO4O4Epgvy/P9VpN1Pf+5tXfV3W7rHjcFJ4so1efED7t6SK3+ICnENumbqaipuYUtxo AQ== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3nr6ps1y4w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Feb 2023 21:26:01 +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 31ELQ00t001990 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Feb 2023 21:26: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.36; Tue, 14 Feb 2023 13:26:00 -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 , Catalin Marinas , Jassi Brar , , , , , Subject: [PATCH v10 21/26] virt: gunyah: Add IO handlers Date: Tue, 14 Feb 2023 13:25:52 -0800 Message-ID: <20230214212553.3325671-1-quic_eberman@quicinc.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230214211229.3239350-1-quic_eberman@quicinc.com> References: <20230214211229.3239350-1-quic_eberman@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) 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: vi7BTemolUhX0U4ceKva_g8s38S6Akuc X-Proofpoint-GUID: vi7BTemolUhX0U4ceKva_g8s38S6Akuc X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-14_15,2023-02-14_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 clxscore=1015 impostorscore=0 adultscore=0 mlxlogscore=979 malwarescore=0 priorityscore=1501 bulkscore=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302140184 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 5 ++ include/linux/gunyah_vm_mgr.h | 25 ++++++++++ 3 files changed, 124 insertions(+) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 7190107a6a0d..24829db17a0d 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -257,6 +257,100 @@ static void gh_vm_add_resource(struct gh_vm *ghvm, struct gunyah_resource *ghrsc mutex_unlock(&ghvm->resources_lock); } +static int _gh_vm_io_handler_compare(const struct rb_node *node, const struct rb_node *parent) +{ + struct gh_vm_io_handler *n = container_of(node, struct gh_vm_io_handler, node); + struct gh_vm_io_handler *p = container_of(parent, struct gh_vm_io_handler, 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 *node) +{ + const struct gh_vm_io_handler *k = 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 = { + .addr = addr, + .len = len, + .datamatch = data, + }; + struct rb_node *node; + + node = 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 = NULL; + int ret; + + down_read(&ghvm->mmio_handler_lock); + io_hdlr = gh_vm_mgr_find_io_hdlr(ghvm, addr, len, data); + if (!io_hdlr || !io_hdlr->ops || !io_hdlr->ops->write) { + ret = -ENODEV; + goto out; + } + + ret = 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_hdlr) +{ + 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 = 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 = data; diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h index 56ae97d752d6..70802cd7cbe1 100644 --- a/drivers/virt/gunyah/vm_mgr.h +++ b/drivers/virt/gunyah/vm_mgr.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -53,6 +54,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; }; int gh_vm_mem_alloc(struct gh_vm *ghvm, struct gh_userspace_memory_region *region); @@ -61,4 +64,6 @@ int gh_vm_mem_free(struct gh_vm *ghvm, u32 label); struct gh_vm_mem *gh_vm_mem_find(struct gh_vm *ghvm, u32 label); struct gh_vm_mem *gh_vm_mem_find_mapping(struct gh_vm *ghvm, u64 gpa, u32 size); +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 84579150d6bc..4d5bb0638d0d 100644 --- a/include/linux/gunyah_vm_mgr.h +++ b/include/linux/gunyah_vm_mgr.h @@ -91,4 +91,29 @@ struct gh_vm_resource_ticket { int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket); void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket); +/* + * 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 iodevice. + */ +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_dev); +void gh_vm_remove_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_dev); + #endif -- 2.39.1