From patchwork Tue May 21 13:11:50 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: 13669442 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 600FDC25B74 for ; Tue, 21 May 2024 13:13:46 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9PHj-0007wm-Ls; Tue, 21 May 2024 09:12:07 -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 1s9PHd-0007mh-3a for qemu-devel@nongnu.org; Tue, 21 May 2024 09:12:02 -0400 Received: from smarthost3.eviden.com ([80.78.11.84]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9PHZ-0001ZU-1F for qemu-devel@nongnu.org; Tue, 21 May 2024 09:12:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1716297117; x=1747833117; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=rG+Bw3MmmndtbJbVWq6083592qHj2h+NjpwqB0a2mhM=; b=GYnSZSWur6WHft/WuKn8U5jNAjo/54NBmSelwQidvq/DwKdh/l/IMNj6 4Tod3iTQ8bNOgn8Vz3Bf8mFL0+c+ePFKGzgf9kRPrhex4OucIc6aEqjOz 161X3xOu0B4QCv45+E2KQo9ClacEdH5UFTHbGwddkO0ovT/RYqLs+tW0g t99TBtebPCMpX3/nK/oShFcgmWZOLZAR3gx9mGGf2rJOLUkLErLcYehJi 9+JKYwFUqJ3U1vZFuwr2Oiwasr3n/kRsVtUU/Gelg68ni5NcXzUuG5ZES HnZr2GglXUI6Zh5q4U2/SbaD1+C8c12dC/nw8QIQUKegdGl0yobt+KNhH A==; X-IronPort-AV: E=Sophos;i="6.08,177,1712613600"; d="scan'208";a="13008012" X-MGA-submission: MDG48rgx94wLZo/3EO67I5dIktNJtDeikG8/t7MYO9KNuD3beOhD3Y+L9dEmZ2kaeBkKVsBAiwRKxWB53w7SJJAqx4hMCbvuI0vtq/8ttuLlhL01PF/2B5W7MzkNRwdOGaIjPUlz0eeMdrIq62t1CooIwVYEgSwhGsYZ4K/9R4pCVA== Received: from mail-dbaeur03lp2169.outbound.protection.outlook.com (HELO EUR03-DBA-obe.outbound.protection.outlook.com) ([104.47.51.169]) by smarthost3.eviden.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 15:11:51 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oBaTJS7cxUZsWe2m/MiymtJhz6azvBZucI2LaVABQePzJklbGPHAT7wb4+QSl25hkeb5dPvXkoqPC2d1fRGqH7V1q/zMfL3G9bNt7UnojfI7AVkRVBWSuiBxmL5/HZ2/xRRMIreuQWLPWKxnUz8NUNGAXwBTxiXu1Wm3kxBbbx5rHH7xTDDoeBhRw4di/gDUjhHfsZk1fWOhPhcr5QY0M6/OSPqqbsXic5rI5koNVjVImA0Cm0f88+q7MVccVDHA7bK6p3LzIcmxUEzgNyAwAbetSM/igmZtxOj0ROaggNBmmyvbOd80JWF1N+IT56M2hiDkOd55H7Xp16F9+jPX3w== 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=rG+Bw3MmmndtbJbVWq6083592qHj2h+NjpwqB0a2mhM=; b=Q9deFib3E+KnbcAVdPfWnKIfBKrueTAyTgMnGtgjWvw3RP00eRmhwLAiaADmfE0yLtBMjlphHWo6rok3K4ZJEz+2+6ZmRzIVD6UxZ8fycZeqUhAmhRQh0iEY+5Z/ZI6S0V5+CZz5Vfmbgvg0cFrNHO1xUdVs+z9aSW5MrwgxNQSAgdA5X1Mw5nigfsNXqIlzQvAiwE4vYzrnjmWyyO3zFk2fQpXmIOrzXvZmJzXy9RdfpV+uSV8jTXXSTqHtukNBM8RHdLPq4evC9jQ4YMyFgV5cRdw0397nB93tmJmJodrSqai1D3NJKFCJdXh4iENecV6M1kAR1hNx4mhmYxiqPA== 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 Received: from AM8PR07MB7602.eurprd07.prod.outlook.com (2603:10a6:20b:24b::7) by DB8PR07MB6249.eurprd07.prod.outlook.com (2603:10a6:10:140::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.27; Tue, 21 May 2024 13:11:50 +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.7587.035; Tue, 21 May 2024 13:11:50 +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" , CLEMENT MATHIEU--DRIF Subject: [PATCH ats_vtd 17/22] atc: generic ATC that can be used by PCIe devices that support SVM Thread-Topic: [PATCH ats_vtd 17/22] atc: generic ATC that can be used by PCIe devices that support SVM Thread-Index: AQHaq4BxILxnhaztEUGv0ZSpe4izZQ== Date: Tue, 21 May 2024 13:11:50 +0000 Message-ID: <20240521130946.117849-18-clement.mathieu--drif@eviden.com> References: <20240521130946.117849-1-clement.mathieu--drif@eviden.com> In-Reply-To: <20240521130946.117849-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_|DB8PR07MB6249:EE_ x-ms-office365-filtering-correlation-id: e4b58842-519f-45ba-e76b-08dc79979399 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230031|366007|1800799015|376005|38070700009; x-microsoft-antispam-message-info: =?utf-8?q?L0IqHn3frpkqFLRxkubcpFJhAazgsVs?= =?utf-8?q?Hejmy4G6Jb0QSOKActXC3H0I+AUZRlIYzAteHZ3RXhEXaEQACec34/U0ht874VG+r?= =?utf-8?q?R+BuoBDfbHr1t4+j1CyCvG00Ne4Pjd0aMnYzVbGTtBfy25yQXaswMjqV8nM6UvOJq?= =?utf-8?q?vS/hcgL3iNeuIhO1V80GqiXVHlCVO1s2XTbsiGheWBmIDxW5iepTK4f3BW9aKRkSN?= =?utf-8?q?iEZk+6eEA7P4f6ICSuqqL/UIDxmOZNddBmy7/TMwlK1mD6mxMuxJ7DeiS/ojWGOGI?= =?utf-8?q?1+zXuAhdIDkiFwBp+qHNnE8IZCMzWze7lAxHhw9Xl+mp+1AAONzmd494FfFaNRx/R?= =?utf-8?q?TlXh/4BnvR/mZJnI+vBrgaFoEQWQG3A9zQvif2JNxIK7cuO/JYEWYRq6S33VEdhbW?= =?utf-8?q?tkOu2lhDiWovydmMHPLTZHgb2kpipDnAd3Dcc+Byx24vzNPkxRASJFV3/rbyvXVkA?= =?utf-8?q?Ao0mWXasMoRkU9wsU+uTI7mxE7IctaYm7ct2kn8/0HmyfHcieDrdLfMTMXBguip08?= =?utf-8?q?fz34yjQYuspbsZW9suvGUzUELlTnmxRFBtso3Diiv84v1JavdU4eoRGc6vQHMbBYW?= =?utf-8?q?6aoa2rlxu7mB1CBmx4gsaz94KRD/2Bcrov/Zv5qSktqq7Zgp5tth07M/RzTLsYbnI?= =?utf-8?q?cGiKdqqhpUyAfB0podVpYS4VlkEPywEmghk0ZwoSfpxI05B1rmaNu5W8ypNVhjMtv?= =?utf-8?q?0kUUk8Xssbf56ZQsYmagVdkYmQfdhwQCxXXcwrKmZm95JT1kk+YtctyJF3Vig93Mb?= =?utf-8?q?6nvcVn0CIz6olZ1zLwLAF1APUHbRid4g3DwIvqiizPQXfKnKLRo+r+IAvB6nfg8H7?= =?utf-8?q?NwxegzDhWJxx41Bwz5Z0FR8iL8K/psznNXC82rVMwE6nxz/t7zMv5iVV1K+3BUmFz?= =?utf-8?q?yXDOdtPHA/nlnwPv7fkgTK3V6YYMRVc9QcxApQWqSYwpNA9qamxfoKZZEyyxzMuzo?= =?utf-8?q?0rQ6RSJ06FS3EEsWFdZE+55W0XjrebJtydGfHLvyB12r1TGVR61unj0IgHJUn4bZL?= =?utf-8?q?OQJj1RvDwH24AoHuB+XVGV2pV1To71OpFXrcDNRncoZMCeNWAZMqO24Wmgnvtd++4?= =?utf-8?q?xnvASH61tDZEvrBaFP0gVR+dcD0/E3JGOqtsjfTEugGJQf+43BhDMqUmezg5dkhBK?= =?utf-8?q?gYFzgdm3d7HC8xaaNWcZPTZGrLWRsJnl3kIDykWnSaM2upir6DuqBX98O3p6ysCfp?= =?utf-8?q?siqtjZ2Pt+ExB5EGvpZoFljDPiIFXl74jHbg=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)(366007)(1800799015)(376005)(38070700009); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?BACUy0+ZJQDt1wxjzAq5pdNAxP1j?= =?utf-8?q?3BnBV9Vb50xGJepz+S1rIKXL/KcgpLsBlDPy9Ga9UzpSYYFdEnNCac43oirso2XyF?= =?utf-8?q?51BPlcKh3ypvUsEfHjBBC6RrHGRLQMNadH2lo2UDajDfuS91aQTSWfgR5P/ZyLS3W?= =?utf-8?q?KGzHQgCGSvb7D4TjyNyG2dU5qfClhYqUAxQXOHWeVJKa/iJzTIygcgMvJS8oIgpNs?= =?utf-8?q?nkHJCpVs3rUDIfpyTJb+hwuOgM3/42yP6cpA+5L+OCtLmrVCQGyECSiWEULGuik7d?= =?utf-8?q?qBadgZUxcPnBeZRx19olqR9Km1tGmBINO762ezR/qp1JpndiIjavAZGvKBTWnVeYG?= =?utf-8?q?zV4rM8UU3rTUapiSX1Axyb5Q4nUw2Qjo8HwGfDPs6SEcE3LhAFMZF3iXeuCJUEq3h?= =?utf-8?q?g8E0pn47VGg79eaUamGw0iQ3xJTFu0qd2KggBOoHlwL2kKibmYy+qXe1jeQX7vtmi?= =?utf-8?q?j6Ma9+JpyYQpSYrbSwBlSBTSJBdWvEcraEmRQ3bY81UH6iIo9cNI7ygXXa+budP+7?= =?utf-8?q?DKE/07zLjwePF5Pwf9d88YYjDuofy7uVvQJvsgdSEhYCX7807/SLcnQZj2Wa0xVLt?= =?utf-8?q?tF95kc9K+6Y9skyuEwkdAwXR9GyEdc5gW7AuzjF7tbSPHw4k0X0NcXrCo6ebpP05K?= =?utf-8?q?SJXFQcEFhtVYI5laSDzeUvuLM0TG5kSHZZkGwjYWOdvj6YNUOxju9zimAPEemxJYA?= =?utf-8?q?PvkQNnrCRxjuabmvm+t3J7TPSsKDzHm+jamnEUqqOw+YnlG+sN5eFWVwylYM/VT1f?= =?utf-8?q?w8lOXaFl0K4gRmDDb+QO7R5oKDMMwvvM1PB5aHgR2f2Oagmnx7wtbMI7UcDHaD2wm?= =?utf-8?q?jeqeflN/0awj4kIFJ4SNqlEE2WpGZwXjPouAUQYEg36gCC/rcXe6QDcef1G+7z+Dc?= =?utf-8?q?KJdmQ1VF5sltcShMnllITO+QalyFrvVRHDnDN88R0onHHsOo3FT8z2vdHnkQhnloE?= =?utf-8?q?MdcCoJ9sJBZIQu2tqNyl1fv9/Gp9e1nKBc5aTs1vXNyO94Nw6iU5bLqgsUx2e/Z4/?= =?utf-8?q?2qna0L4Gf7b8y32qGqa88eoH1m/pascPX67CTdsUVkbDXklSf2mXfAY/2RsR/09H+?= =?utf-8?q?NvsjO/AIvpCpS7k6Es7xEwGfzhf8fLdgL1luv+ZjeFr9/MCxlJ6YgS1FxghR8l1tZ?= =?utf-8?q?FGZAbOGp5JbiZ69b4esvVr36YlAyPbeYEif6GWUZwyLd8jB1oVqUFxAMdicSAivL5?= =?utf-8?q?ckaeXStuI27dw92zAxZ67BlvheoQg06SE9zLmy0uzPIVPdIS1R0a6dGuWyMJko0Q3?= =?utf-8?q?j5mOuoWavZNplw7rJUpBiuGivOSBqMUBFp5zHKG4KD4I8P090k5d2cDwnU/Kbw9K4?= =?utf-8?q?N/m0HRnHZk9QN2wKY3is/gnwIW+oZ2nRRDih8K/l3vBQnXzpDPhmKNX3GOHj+TUQr?= =?utf-8?q?obfKKAq6iV/um0v5sBGGFlnk2MTfSOw3z8U/+nrm0lOMFMrdkZ6vxq75TPtcrxY3J?= =?utf-8?q?1F9mPZ530MTkb7K6rch9X4rFC/jeqmASyh8/4Zl30gnEBLxp7v40hRd5ntqhd/WbH?= =?utf-8?q?sWYiaoM57g3W1bWpOXlSkohK/0HV3Il/d6YD79L9kVL95g38n4X55uc=3D?= Content-ID: <4E4E46386A911F43BE9B36D2683FD9E3@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: e4b58842-519f-45ba-e76b-08dc79979399 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 May 2024 13:11:50.2827 (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: vAb5aDYq1VbZxOft3Z1+hzcowAIl3dzK79EfJkJGrNbuzhtOWwOaTXce5JkuLn7BQ8dPh1QBYBCjR7L6K2MZBV3cM0XX33Jw7qosvHGy/m2sq7d6HmEMHmvT9E6Lbkdm X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR07MB6249 Received-SPF: pass client-ip=80.78.11.84; envelope-from=clement.mathieu--drif@eviden.com; helo=smarthost3.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 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'))