From patchwork Tue Jul 2 05:52:43 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: 13718997 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 1D472C41513 for ; Tue, 2 Jul 2024 05:55:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOWRf-00081a-PP; Tue, 02 Jul 2024 01:52:51 -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 1sOWRd-0007zm-CR for qemu-devel@nongnu.org; Tue, 02 Jul 2024 01:52:49 -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 1sOWRZ-0006Un-5C for qemu-devel@nongnu.org; Tue, 02 Jul 2024 01:52:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1719899565; x=1751435565; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=aaciKa6xrwSW2NtKsj36kO/MEVhisO6XpupR7gvFUH0=; b=NT+6ChqCq3RO1Y4T3lL3ArDXr29bUtBiDzv1JvnDDd2Psujx2u4MB/mh 1QkbrtjyYKDIcy/OfCIEdqYrieA0VximXzNVP6JLS/Kje3+dkjsNnLTfY MThPLhQPHV/TGn8lo6hjCZQa0NNoIuY3+m2DxJYhYpe290iIwGx778Rgv 4fmB4neZoMlAxE9FRhhQ8HDbPqTJYXqTtdfoDxnH8nfe19TkdDgxyOVCG 3MFnXbPDnfFtxNe01LoxbXjfaOi0j3zrzOLztpp3JYpUqUWzWzTsDpjs6 YrbT/Dx1mA0ssECZTcVHICSKoQiO4/QxFoOVSdW2xqsXcIyLaxWGHZJJj w==; X-IronPort-AV: E=Sophos;i="6.09,178,1716242400"; d="scan'208";a="15721524" X-MGA-submission: MDFdYXZNQMo2OED/j5/Bxa2wB2WbI6frAHbL2u5lX8arh7JObDvePlN4pGdDkrXUFheqjsOB0KV6zF2vtkaF2UNT65zihjz9/qSIiOr9MYab155/Pw2r82x17t+BhBk7jxrGoXOOLRbRTE0V0OylT3HSQg5a9GRE3tumJVanXftITQ== Received: from mail-am6eur05lp2113.outbound.protection.outlook.com (HELO EUR05-AM6-obe.outbound.protection.outlook.com) ([104.47.18.113]) by smarthost2.eviden.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jul 2024 07:52:44 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EkuQ7QZVygErYSEeIU0AY9bgAQbuGa0Z/E+D7gnFSYepCaT2EL0j3TPGWYPgwktQzugswlQFhXRxpf+6KSvJyFmWVpf1BDdA/t5J9r4yjG7pesFBSU4XjciNSs73TeThkpL5OvPEtKJDypKusei3lFNQFRvpQtIW534d4xQyShUtL/3vXX+i04CZxw8K6GMRdgvIRvW8WSRlXaMIcWe+QJ2aEwXmSvbcNdoMcpXFBH5caBmA0mJ8KaFBeICMk2gg58uMU0GZs8DA0w1uE49DwIDBVS3eQDQCkaoAyoJ59GKD1lYlo4xJrrIh1LH1w25WdwmvVn8rqYXK4hpWD1lc3g== 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=aaciKa6xrwSW2NtKsj36kO/MEVhisO6XpupR7gvFUH0=; b=XRG2Mzac4RXdU10DjCPz8KnmmS8UQyNQfOcMjuHsklftBGknozcctFl6ohjHkx2JLAhRIAoH/d4eQRmVreJiMbKuRghGeFuPQ/uZn7X6TkKL1c99+ZUY6gJIkP/SF307TdOyGQCTS8x0uv6MYXdwK/wEUj8L3qfkxbzLAD+jjG1554ayx5yea7XQlQK480SiACPpiatUVXMgOMpNMfD+RI9MtVJM+eH0ez/WAx151I9nxFEsaXi0rxWmaQ5prEY/mh2inwCNhNIxJvt0YJXsIHWK9Ixj1AplqUcVafNZFML2k9TCukU+HXa1anB47z2hECT8jVY7sOV316d3jPlvgw== 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=aaciKa6xrwSW2NtKsj36kO/MEVhisO6XpupR7gvFUH0=; b=heDAWprl/FuBFHP1clQgmwKvDlYaf6ITaysfi3JkqH1eR3aBqnHOMw3u06By/QtU8J+RTLMwBawn9pThJ68N2E4XmHUIx/Gp5ZFi7mganLY9R6L7ak9xqZ2cwI1fC/6xj7MwgWWLw6ktEbxPr/cPe5Nsy4y4c+tVObPpIDIFP5F9fvAmykTtVchUJvCqynWAHSntwkakSIYqwj3NbrjjrvKUQJVTyGIX14Zkc+t+wdjs7Y8NdhjZX1Ha1bSybYRJ2N3tg5U4aRXDPqUstHYJwfbhF/wqMNwm0yXx78rcSvVxf8CBu11CNJxp0VDpTT/zm8YoEvlCZWLniQBNXHjgKw== Received: from AM8PR07MB7602.eurprd07.prod.outlook.com (2603:10a6:20b:24b::7) by AS8PR07MB7080.eurprd07.prod.outlook.com (2603:10a6:20b:239::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.21; Tue, 2 Jul 2024 05:52:43 +0000 Received: from AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d]) by AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d%4]) with mapi id 15.20.7741.017; Tue, 2 Jul 2024 05:52:43 +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: AQHazEQOlP+g0o1Bb0i4sVdTaZApDg== Date: Tue, 2 Jul 2024 05:52:43 +0000 Message-ID: <20240702055221.1337035-18-clement.mathieu--drif@eviden.com> References: <20240702055221.1337035-1-clement.mathieu--drif@eviden.com> In-Reply-To: <20240702055221.1337035-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_|AS8PR07MB7080:EE_ x-ms-office365-filtering-correlation-id: 9c4a2b03-732a-4633-5896-08dc9a5b30ca x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230040|1800799024|366016|376014|38070700018; x-microsoft-antispam-message-info: =?utf-8?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C/oGut9naclTn/Ogo+2mJCXZ6Naimy65vbST?= =?utf-8?q?w4UgZBh9mBH87JLSm6AQHsIMoVf13Pl/oCBoGFVImPCzI43F1qruD8+v886Ji+hqF?= =?utf-8?q?DlrZIFBIn3r0ozANIai82MTRummjPuvG1d2xNaqehoOtDiCxrmsVpO/YBwrlh2ltY?= =?utf-8?q?ioc15hdzAfQLgYUNazSUdfdcaJBsECKrR9F+TJUI4y2BNgUwu5eiGBW5kT0SQK1X5?= =?utf-8?q?eHCLLijuIDS5L2JmJBKAjjGqAVS+Rvj/mW7avF+OouF0k5T0+W80QbflBUt1LumLL?= =?utf-8?q?i5YRbsM30fpeqIIRo1AAWtfjsdw0eWJ90WAPqbwoQZ/3Bnv7v+bRki+2jW8KW2zJU?= =?utf-8?q?HCoUjxIVDow8IOiyXxLhEVgPCTK/6jEUGRdSCZe3AaqyuXPh/n/SMUSTCS9y9iDsa?= =?utf-8?q?nfbI1YjzRDkxKLKhY7pTwWHpVk6h58bB4hAMtEPwq4SRiXtHFtu0K8Bo=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:(13230040)(1800799024)(366016)(376014)(38070700018); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?hnk7qbXIlZvyX4ZtKOsnOnY3YcTO?= =?utf-8?q?CG/hoXVcR4Jo7pQMK0dA+i+k6b+NB/5jOC9F7GBDmxCyNewqFaL5U+5uR1oh3XNxa?= =?utf-8?q?1o9MNe/1GXMzoPPT5uwBz43UP6Vc09UrUzHYwn+vArwuz38HaN/ll/AEdQtNpu6pm?= =?utf-8?q?jJtXydXDDAwq8QDalLETYiuRwgd9aTAxeFBe+h/Eg+0ElDliR6qtTF/qekyJfmb9d?= =?utf-8?q?Bvu4jZI4ZIvnPnFW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8Aegn9jTtlOEjkxozvHoP7fWNwg9wNFJ5wxbQ?= =?utf-8?q?dBP0RYntINPRo8r6mHiszCXDkqXTd5BVBSTTiErnyDuMmKNY2tEGDMaf0Dzndll+3?= =?utf-8?q?DuEd9uF0vnCvEBcLYRosdbX7m1UtZ1CYg+N+neEHWSyD8gN8U5Q2jbn6dOiumVEF0?= =?utf-8?q?jGWK3IS8eyPiN3cMY26f5uHY8/jymrrRu5sel5ifj6v+zlvA1tMOUjimNzf7pjiMf?= =?utf-8?q?RkXT0v2g2K6vCzsFWfWSjMdRodQ+8mXbrN5FO0cHXIz38+ONK1VyvRG2AAGOCmPKv?= =?utf-8?q?VTOsTG968w0O4wCgD1STa+sMMCmvzo5PqjmV6CuriKDYRr/mT7vuK5CbRo+uwsmxV?= =?utf-8?q?ER7gq/TUubJgaEIeLV4QTn/r5cE8XVL1twsABKEQSZjzIQp/7zd/37HVIYfOfvlvh?= =?utf-8?q?9hlsNst8XGJkbNH6WZ6IUoRCwyO1jXJBb2n8j6EMvHtFOl0Vod0actc=3D?= Content-ID: 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: 9c4a2b03-732a-4633-5896-08dc9a5b30ca X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jul 2024 05:52:43.0905 (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: pFQPKPCHQX9ChxQNNrfuPLbfrzHJ4NWz07nT5zLGI8+a/veW0IIqri/+gNsKQOGVMwraLBeDc3lUuz7iJuZYdxUltJvjTj1mnvOOblBgenozZL8INn4+yO1UgucL6+yS X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR07MB7080 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 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 72b505df11..2273f8176a 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')) util_ss.add(files('nvdimm-utils.c')) util_ss.add(files('block-helpers.c'))