From patchwork Mon Jun 3 05:59:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: CLEMENT MATHIEU--DRIF X-Patchwork-Id: 13683270 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 93348C27C4F for ; Mon, 3 Jun 2024 06:01:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sE0js-0006ri-KG; Mon, 03 Jun 2024 02:00:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sE0jl-0006nM-Nf for qemu-devel@nongnu.org; Mon, 03 Jun 2024 02:00:05 -0400 Received: from smarthost2.eviden.com ([80.78.11.83]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sE0jf-000731-69 for qemu-devel@nongnu.org; Mon, 03 Jun 2024 02:00:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1717394399; x=1748930399; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=89kz2uZWAOgE5eTvYbbmfDZ+DOtI2Rb/PyJY5T5dams=; b=eE6NLiXShQbYKTcjWrepu0P+HSLv58AjZo9igwC/ONh8HlmF2Uhazx0J pB/9Mn9HRfC1zMckoO4VAOR/KVsPNwgctRLUjfDHb/zAegjCArq5WlIXq 2TV82A9cIMfZnUY4fXgrmdunOW2Wz3DGombqyK79OXWmx6qu/nu6+BeGV ohRjZ5CB8gNPtvW+ugIt+Ws44QTD0uA1EMWsfCAotwB5uvzz4jXSUptd8 z8mZ5Xx8pEe4RPOmFU/kKkmQH6CUVYsKompl/qho8M0E9JcRWjiqXdbiU bxX3CulNEZqv1qnH2myJDyC7PMoUSsPEU4n9EKkoO7L5C4c0KejhTqn8Q g==; X-IronPort-AV: E=Sophos;i="6.08,210,1712613600"; d="scan'208";a="13763251" X-MGA-submission: MDEhRueoPZ2SneOXDWaF/qVCmIZ0XWUWVw1hUB5jdhvWYDvS0uPLOPBUo4PH2MRNk5hX4umsRpb++0oVsS1qxFGCuPHke7BzkraSCcvPV04U28mH+Pok2QZKLnuxAP9jissimZ2f1T9fecroIBsKkTpcYW86i9AslOVvbVjkrYxXig== Received: from mail-he1eur01lp2051.outbound.protection.outlook.com (HELO EUR01-HE1-obe.outbound.protection.outlook.com) ([104.47.0.51]) by smarthost2.eviden.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2024 07:59:55 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=j443M289D2aWD46oV8TJcANoImizt/UgWRlOpdOfGqApYYi3UYiPvBn5Z7R34/Y1JJooQTyOVVfkb7QH16wc4NPx2XrDacT+Ju0QB9a+Veje/GfiJdFGPh3+XysbcfiYEx+pZG9KGMLCIGhCd2PuR+NUx1+6WcMF6lQ0wl0bFE1dh1PRYoz17wE5GxA62zP4goE4RC+RvQhomoFnSbaFSpMA+ndXcnF9NzT1P4LNHcYSOZaNssTg3cjrFgvPxdZkRnCisMpOEcgfrlbRpj2eZ79BohI24W2j1lfX/jGcAtD/KEb4o2b7n4H3xJ5egiQ/P4twchaPWrooJ33vRWbpdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=89kz2uZWAOgE5eTvYbbmfDZ+DOtI2Rb/PyJY5T5dams=; b=LNzkzm3oLnQSrNPGIRKwnf905za1e/RKw3TW6RMWusRq4weL4UkYHuzwbF66FPCh5DfyBkA7K+fLVfw8CL1e0/BzVWjDc9XFr9PoRBfe3HncAgcIaKod9uXr+HvvtDQt+2FIiavlm1DLYfBbOeETuHxMnxItQi19cQUrqlHOLZ16GcQVnvlfaaE/Gd3aLu5T60sfDMjVhNzgHNfj3cm2ayqcjAgSn5D+iaqX8XhLv44CQML6dcKkNz8kSmdI/uxs1Hhz4gUS/D19XO6KmRuJazgs5K2Osey6gk/pxekd0KkawSE/YJl0K1sbc24lDu5/6ZWbro+/xjpRWrdVJe0DYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=eviden.com; dmarc=pass action=none header.from=eviden.com; dkim=pass header.d=eviden.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Eviden.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=89kz2uZWAOgE5eTvYbbmfDZ+DOtI2Rb/PyJY5T5dams=; b=q+6A0GylRHReocXIdZuG+m6pX7uk75h6aJnRK3rtnHNvlVwRvwkkY8VuZeWbL/i3+HPnWHdQqLZUFAOknnjzCffXdbilfosOZU2VDPwYKWAnIIzjsHpI+tDDVCZVECFRVNbWOj/HXbppyAtHRuX2m24zOkZI6R7GZcIyTkcP5+BUc2b4ZshJttrXKcPbvAhj63g/tk9b9tkn+Ilqk3OEhZhHj+vDRLkCdcyTQiQJKWwWT+1vluEOJMU6Sq+KBW6Na8M1KnQXePMVNL6xPkOS/QvAJ2/5yM1btOU+WZXzVqmce9WvlcmbxeXu5uwND0vBQCp5QwTRd5X6kDW05UG6Xg== Received: from AM8PR07MB7602.eurprd07.prod.outlook.com (2603:10a6:20b:24b::7) by VI1PR07MB6238.eurprd07.prod.outlook.com (2603:10a6:800:136::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 05:59:51 +0000 Received: from AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d]) by AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d%5]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 05:59:51 +0000 From: CLEMENT MATHIEU--DRIF To: "qemu-devel@nongnu.org" CC: "jasowang@redhat.com" , "zhenzhong.duan@intel.com" , "kevin.tian@intel.com" , "yi.l.liu@intel.com" , "joao.m.martins@oracle.com" , "peterx@redhat.com" , "mst@redhat.com" , CLEMENT MATHIEU--DRIF Subject: [PATCH ats_vtd v5 17/22] atc: generic ATC that can be used by PCIe devices that support SVM Thread-Topic: [PATCH ats_vtd v5 17/22] atc: generic ATC that can be used by PCIe devices that support SVM Thread-Index: AQHatXs/4cl8uzc3D06D64WXq8wIGg== Date: Mon, 3 Jun 2024 05:59:51 +0000 Message-ID: <20240603055917.18735-18-clement.mathieu--drif@eviden.com> References: <20240603055917.18735-1-clement.mathieu--drif@eviden.com> In-Reply-To: <20240603055917.18735-1-clement.mathieu--drif@eviden.com> Accept-Language: en-GB, fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=eviden.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: AM8PR07MB7602:EE_|VI1PR07MB6238:EE_ x-ms-office365-filtering-correlation-id: fbb2d89d-1f7b-4ec4-9b67-08dc83926250 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230031|376005|366007|1800799015|38070700009; x-microsoft-antispam-message-info: =?utf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L0O6PCT4al5hdR7aEPcbWh7XE?= =?utf-8?q?zywxspkBkUBsluWiPCHCyzvaBVb/RO2HdY/1pws3F0NOSoNUFHD3P92Iegw0KvEhS?= =?utf-8?q?rSS2sEnX1r5C268CzjFdwn4wcVo3vVzrq8arTl4RGjNkBBm9y1jdr1IGxznwIE/6P?= =?utf-8?q?XgcdUquIAhEXAunsGqwLbkNL1wa5cCZZnkQw9qiQHAljrx5pj/x776w7i5WirNOuc?= =?utf-8?q?gSZnqy/7gzpFvYBFCYEa6ki5zXOkB/h6H0N9jfoit0dhIxjAExnAM3sGfSZh9kr3/?= =?utf-8?q?2UakOGMCQ+Cjs4swmf0ind1os6lEu46SP52ONVq90oYfQ4Yr4E2blMF92ij1+grnu?= =?utf-8?q?y+5dXUpd/KsC5+mcFfvS9/2VEOBCLzx6sgj1PdQip+0zf/0i41LHoOMMSunOP2QnX?= =?utf-8?q?9TIYTqr6aDu2Z3sVEXbHKPHzaazkLRSufNQ=3D=3D?= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM8PR07MB7602.eurprd07.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376005)(366007)(1800799015)(38070700009); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?nQhH0xqa542qTvuIL+jCOnTbAjjt?= =?utf-8?q?c3VSTFAdP4suTq681uDEccBBi53+rIMcnsy9/pc/sc0P67j/78PygsDBhKlBIS0VC?= =?utf-8?q?VSgV+svMDWFvuemEcq3KyyNqYTDklimiZY7utMmaF0U++YLj7eVmI/+Fzj2le+7b1?= =?utf-8?q?72MP3E/QrJWnRYuMP+VQlh1TDoslw2n54wBQChZz1arPwD5m+Oo4k/4aSXDtju7fz?= =?utf-8?q?2cOFLYc8Ar30+qslGA/jvVimvoTos10EpzurGQeb7E0vzLJLhQ9GYO9TuvCnfBgQa?= =?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W/u0xNNx3XXRi4cvnWQwkSk0dtEa9A6BqtcnadbUyO3ICzlTujU4JN/89qow6q?= =?utf-8?q?UQ81i6qsJzB8Pn6p4kEEKOHnGYlPvlcaW/A+NmDstH4FPSjzNggrY9HoIyt1vnGfx?= =?utf-8?q?PxUGLMAw6gIVDAmenReIintrbMB74whffmwojYehz4QfNL/rVu287oztQsXSxkUd2?= =?utf-8?q?Vhopio/dX9a7E2HbC7L9nHySC9tGYT/WNb0sy3tO1tIxzArlX3soWdfK52GSVw1Th?= =?utf-8?q?CYah1GiRDV5flc5X3mEQzjrA+B4gLMMgZsoPylA+Qu/g8mg7OVzRPX1fYSaIGOrsx?= =?utf-8?q?kHaR58TEC7iGsUDT3fUDTpAylqOsRawrXXh2Z6zp4sk7lCwP/sRpp7mxmOXkzVn80?= =?utf-8?q?dH+Zn/cW5xlJnbeTp/i97CJc84XI23ORW9/UEy4ero30/B9qgy6M/jM=3D?= Content-ID: <5374C1BC3E978640B7878E39204C3407@eurprd07.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: eviden.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: AM8PR07MB7602.eurprd07.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: fbb2d89d-1f7b-4ec4-9b67-08dc83926250 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Jun 2024 05:59:51.7273 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 7d1c7785-2d8a-437d-b842-1ed5d8fbe00a X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: HFbuudPL7muZupEHCkkhNpiOAjAdELDiiiHsgKJla74lTSmTDq9daS3qwD0SNepGGz2m48bIVpYaxyrGEsOACCTgWFs9n/QxnJjqQSQeJPzMbPCHvcGgSkxicu8NlWAC X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR07MB6238 Received-SPF: pass client-ip=80.78.11.83; envelope-from=clement.mathieu--drif@eviden.com; helo=smarthost2.eviden.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Clément Mathieu--Drif As the SVM-capable devices will need to cache translations, we provide an first implementation. This cache uses a two-level design based on hash tables. The first level is indexed by a PASID and the second by a virtual addresse. Signed-off-by: Clément Mathieu--Drif --- util/atc.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++ util/atc.h | 117 ++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 329 insertions(+) create mode 100644 util/atc.c create mode 100644 util/atc.h diff --git a/util/atc.c b/util/atc.c new file mode 100644 index 0000000000..584ce045db --- /dev/null +++ b/util/atc.c @@ -0,0 +1,211 @@ +/* + * QEMU emulation of an ATC + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "util/atc.h" + + +#define PAGE_TABLE_ENTRY_SIZE 8 + +/* a pasid is hashed using the identity function */ +static guint atc_pasid_key_hash(gconstpointer v) +{ + return (guint)(uintptr_t)v; /* pasid */ +} + +/* pasid equality */ +static gboolean atc_pasid_key_equal(gconstpointer v1, gconstpointer v2) +{ + return v1 == v2; +} + +/* Hash function for IOTLB entries */ +static guint atc_addr_key_hash(gconstpointer v) +{ + hwaddr addr = (hwaddr)v; + return (guint)((addr >> 32) ^ (addr & 0xffffffffU)); +} + +/* Equality test for IOTLB entries */ +static gboolean atc_addr_key_equal(gconstpointer v1, gconstpointer v2) +{ + return (hwaddr)v1 == (hwaddr)v2; +} + +static void atc_address_space_free(void *as) +{ + g_hash_table_unref(as); +} + +/* return log2(val), or UINT8_MAX if val is not a power of 2 */ +static uint8_t ilog2(uint64_t val) +{ + uint8_t result = 0; + while (val != 1) { + if (val & 1) { + return UINT8_MAX; + } + + val >>= 1; + result += 1; + } + return result; +} + +ATC *atc_new(uint64_t page_size, uint8_t address_width) +{ + ATC *atc; + uint8_t log_page_size = ilog2(page_size); + /* number of bits each used to store all the intermediate indexes */ + uint64_t addr_lookup_indexes_size; + + if (log_page_size == UINT8_MAX) { + return NULL; + } + /* + * We only support page table entries of 8 (PAGE_TABLE_ENTRY_SIZE) bytes + * log2(page_size / 8) = log2(page_size) - 3 + * is the level offset + */ + if (log_page_size <= 3) { + return NULL; + } + + atc = g_new0(ATC, 1); + atc->address_spaces = g_hash_table_new_full(atc_pasid_key_hash, + atc_pasid_key_equal, + NULL, atc_address_space_free); + atc->level_offset = log_page_size - 3; + /* at this point, we know that page_size is a power of 2 */ + atc->min_addr_mask = page_size - 1; + addr_lookup_indexes_size = address_width - log_page_size; + if ((addr_lookup_indexes_size % atc->level_offset) != 0) { + goto error; + } + atc->levels = addr_lookup_indexes_size / atc->level_offset; + atc->page_size = page_size; + return atc; + +error: + g_free(atc); + return NULL; +} + +static inline GHashTable *atc_get_address_space_cache(ATC *atc, uint32_t pasid) +{ + return g_hash_table_lookup(atc->address_spaces, + (gconstpointer)(uintptr_t)pasid); +} + +void atc_create_address_space_cache(ATC *atc, uint32_t pasid) +{ + GHashTable *as_cache; + + as_cache = atc_get_address_space_cache(atc, pasid); + if (!as_cache) { + as_cache = g_hash_table_new_full(atc_addr_key_hash, + atc_addr_key_equal, + NULL, g_free); + g_hash_table_replace(atc->address_spaces, + (gpointer)(uintptr_t)pasid, as_cache); + } +} + +void atc_delete_address_space_cache(ATC *atc, uint32_t pasid) +{ + g_hash_table_remove(atc->address_spaces, (gpointer)(uintptr_t)pasid); +} + +int atc_update(ATC *atc, IOMMUTLBEntry *entry) +{ + IOMMUTLBEntry *value; + GHashTable *as_cache = atc_get_address_space_cache(atc, entry->pasid); + if (!as_cache) { + return -ENODEV; + } + value = g_memdup2(entry, sizeof(*value)); + g_hash_table_replace(as_cache, (gpointer)(entry->iova), value); + return 0; +} + +IOMMUTLBEntry *atc_lookup(ATC *atc, uint32_t pasid, hwaddr addr) +{ + IOMMUTLBEntry *entry; + hwaddr mask = atc->min_addr_mask; + hwaddr key = addr & (~mask); + GHashTable *as_cache = atc_get_address_space_cache(atc, pasid); + + if (!as_cache) { + return NULL; + } + + /* + * Iterate over the possible page sizes and try to find a hit + */ + for (uint8_t level = 0; level < atc->levels; ++level) { + entry = g_hash_table_lookup(as_cache, (gconstpointer)key); + if (entry && (mask == entry->addr_mask)) { + return entry; + } + mask = (mask << atc->level_offset) | ((1 << atc->level_offset) - 1); + key = addr & (~mask); + } + + return NULL; +} + +static gboolean atc_invalidate_entry_predicate(gpointer key, gpointer value, + gpointer user_data) +{ + IOMMUTLBEntry *entry = (IOMMUTLBEntry *)value; + IOMMUTLBEntry *target = (IOMMUTLBEntry *)user_data; + hwaddr target_mask = ~target->addr_mask; + hwaddr entry_mask = ~entry->addr_mask; + return ((target->iova & target_mask) == (entry->iova & target_mask)) || + ((target->iova & entry_mask) == (entry->iova & entry_mask)); +} + +void atc_invalidate(ATC *atc, IOMMUTLBEntry *entry) +{ + GHashTable *as_cache = atc_get_address_space_cache(atc, entry->pasid); + if (!as_cache) { + return; + } + g_hash_table_foreach_remove(as_cache, + atc_invalidate_entry_predicate, + entry); +} + +void atc_destroy(ATC *atc) +{ + g_hash_table_unref(atc->address_spaces); +} + +size_t atc_get_max_number_of_pages(ATC *atc, hwaddr addr, size_t length) +{ + hwaddr page_mask = ~(atc->min_addr_mask); + size_t result = (length / atc->page_size); + if ((((addr & page_mask) + length - 1) & page_mask) != + ((addr + length - 1) & page_mask)) { + result += 1; + } + return result + (length % atc->page_size != 0 ? 1 : 0); +} + +void atc_reset(ATC *atc) +{ + g_hash_table_remove_all(atc->address_spaces); +} diff --git a/util/atc.h b/util/atc.h new file mode 100644 index 0000000000..8be95f5cca --- /dev/null +++ b/util/atc.h @@ -0,0 +1,117 @@ +/* + * QEMU emulation of an ATC + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef UTIL_ATC_H +#define UTIL_ATC_H + +#include "qemu/osdep.h" +#include "exec/memory.h" + +typedef struct ATC { + GHashTable *address_spaces; /* Key : pasid, value : GHashTable */ + hwaddr min_addr_mask; + uint64_t page_size; + uint8_t levels; + uint8_t level_offset; +} ATC; + +/* + * atc_new: Create an ATC. + * + * Return an ATC or NULL if the creation failed + * + * @page_size: #PCIDevice doing the memory access + * @address_width: width of the virtual addresses used by the IOMMU (in bits) + */ +ATC *atc_new(uint64_t page_size, uint8_t address_width); + +/* + * atc_update: Insert or update an entry in the cache + * + * Return 0 if the operation succeeds, a negative error code otherwise + * + * The insertion will fail if the address space associated with this pasid + * has not been created with atc_create_address_space_cache + * + * @atc: the ATC to update + * @entry: the tlb entry to insert into the cache + */ +int atc_update(ATC *atc, IOMMUTLBEntry *entry); + +/* + * atc_create_address_space_cache: delare a new address space + * identified by a PASID + * + * @atc: the ATC to update + * @pasid: the pasid of the address space to be created + */ +void atc_create_address_space_cache(ATC *atc, uint32_t pasid); + +/* + * atc_delete_address_space_cache: delete an address space + * identified by a PASID + * + * @atc: the ATC to update + * @pasid: the pasid of the address space to be deleted + */ +void atc_delete_address_space_cache(ATC *atc, uint32_t pasid); + +/* + * atc_lookup: query the cache in a given address space + * + * @atc: the ATC to query + * @pasid: the pasid of the address space to query + * @addr: the virtual address to translate + */ +IOMMUTLBEntry *atc_lookup(ATC *atc, uint32_t pasid, hwaddr addr); + +/* + * atc_invalidate: invalidate an entry in the cache + * + * @atc: the ATC to update + * @entry: the entry to invalidate + */ +void atc_invalidate(ATC *atc, IOMMUTLBEntry *entry); + +/* + * atc_destroy: delete an ATC + * + * @atc: the cache to be deleted + */ +void atc_destroy(ATC *atc); + +/* + * atc_get_max_number_of_pages: get the number of pages a memory operation + * will access if all the pages concerned have the minimum size. + * + * This function can be used to determine the size of the result array to be + * allocated when issuing an ATS request. + * + * @atc: the cache + * @addr: start address + * @length: number of bytes accessed from addr + */ +size_t atc_get_max_number_of_pages(ATC *atc, hwaddr addr, size_t length); + +/* + * atc_reset: invalidates all the entries stored in the ATC + * + * @atc: the cache + */ +void atc_reset(ATC *atc); + +#endif diff --git a/util/meson.build b/util/meson.build index 2ad57b10ba..e6ca37a4b1 100644 --- a/util/meson.build +++ b/util/meson.build @@ -93,6 +93,7 @@ if have_block util_ss.add(files('hbitmap.c')) util_ss.add(files('hexdump.c')) util_ss.add(files('iova-tree.c')) + util_ss.add(files('atc.c')) util_ss.add(files('iov.c', 'uri.c')) util_ss.add(files('nvdimm-utils.c')) util_ss.add(files('block-helpers.c'))