From patchwork Tue May 21 13:11: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: 13669435 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 DDD84C25B7A for ; Tue, 21 May 2024 13:12:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9PHq-00083L-5H; Tue, 21 May 2024 09:12:14 -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 1s9PHZ-0007kV-H3 for qemu-devel@nongnu.org; Tue, 21 May 2024 09:11:58 -0400 Received: from smarthost1.eviden.com ([80.78.11.82]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s9PHV-0001Zt-PI for qemu-devel@nongnu.org; Tue, 21 May 2024 09:11:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1716297114; x=1747833114; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=VbrUOIfc8U2UXrs6qL7PQ/+HQEYub06zah43XKt6V9I=; b=rRkpe5Wk2iUPB4fu9PeYtRR7N7/1BrWpWcKPRpCmLzLY7AUmw2DxTxZd 2wTYn1p08oDqoGuEyESbasDhpj4VM7PuBbf8s+pnc261Ggh80O204qSpx z/ISdzZp9p0T9MSqG6mlGIIZ9wsPGICzN2LFH5stUZt2qGvYMgM1NvPjQ eszkkLUuRgx7zfrP7vhO+inUJjz4Whrqqyqa1qe9RRr0ipZKE4SZjxP+d tQkJZ9otdZvs7/RcK32bZPokRoKCLKwuLX/LKMFoN9hJweGGpbvj6A8GT iF1/6/6SoWg6Rre6vZAZt5Urgr1TczqM+KcoHbAPL0haUzeTj3Mt9SdsX Q==; X-IronPort-AV: E=Sophos;i="6.08,177,1712613600"; d="scan'208";a="13004109" X-MGA-submission: MDFxsB7xin/uDNOsIDkr3O5BEHMrcXBudKGn8VcsQNaXUcgBcrtbNb7arztuMVCcQh5CeeHUc6DFcjdQB1ZrrjwnnWsu9z4Jgyn4CWL4THOMPHSN311R57tWbajHV2+sv9K3rNZokqpY7Hk0YOxzOPgabKt1cSct1Vat90C6bhdtWQ== Received: from mail-dbaeur03lp2169.outbound.protection.outlook.com (HELO EUR03-DBA-obe.outbound.protection.outlook.com) ([104.47.51.169]) by smarthost1.eviden.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 15:11:52 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XWqMOzS9Ci0oBW8cecDfgp32CUjNt8LZrYvr6f5YoIJ8cmLKovp2XdktLzYjbOVexU8DJ/3wONKXVZGKU/cloOX+qaY9nTQ+HiuIeQb4E7PAxkpQ4k+jW0+iz+JB7SwjSE3iQLh5Ussups9c79A5pOqB/M60AB4w8uZQEEynLK+UB5qH9OXEdqwWmTHxkBONdsdT15rE/T1tFSSW5yuwGlMbeupwU3EJAZR/Rwtq8GkJV2BXIWNcQ/qlBfOQR+lCedlmAaacEynlxsnWeesu1QzpCLcBXNJC/Bqpta0e1Z23VDtb/SbwWoTbbBmY9Bzm3Tf5z6yxQg/5ABdx0VCuMw== 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=VbrUOIfc8U2UXrs6qL7PQ/+HQEYub06zah43XKt6V9I=; b=eyb5mSV/TOC+DGXEyrFxNu3+ZCxnsfOXK1SBKxQkP8Xhza6qO2FUWZgEXHvEYFrgrYly1Htv0AIUUk1Sa4Ow9JRYE1Jh706IqDuuUZo/qKZIZNBnoibFqxB99ZYjmy1bm+ZAyMDFAuqMX9t58J9aP3OPfyHhslGUtM/NuZ7UZ12HSRk4eLiwCaVACk3OrW9YMjuUA5gCoQGiIHq8XkaJtLWSFrCQEoGeGrGGZhBW0FPVxmJvnxKGvt0Gde1qbn5kd2UQlpQ0p5sMZSmUFfyFmYTEyJUdwQAAeoLsqRQtz6xYE90SwRmae0vsyqlAY9RVOX4MTU+F68pRULlcU7cidA== 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: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.7587.035; Tue, 21 May 2024 13:11: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" , CLEMENT MATHIEU--DRIF Subject: [PATCH ats_vtd 18/22] atc: add unit tests Thread-Topic: [PATCH ats_vtd 18/22] atc: add unit tests Thread-Index: AQHaq4BxokQ3ac59N0iT4f1OV0wt4w== Date: Tue, 21 May 2024 13:11:51 +0000 Message-ID: <20240521130946.117849-19-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: 76efda2a-dbfe-4acc-6faa-08dc7997941f 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?4tIAz3AUAjsClRDCJQ6WKwo2QXcWLaw?= =?utf-8?q?R2jlEaiPMeDhOj8cD0NBu3Rl4EfR5jzVhXbAD4yGDC4TEu1iZcqMhgUmWJAtgADi/?= =?utf-8?q?cl1ohOZAgx2CQneBL4vNucsR1LJka2SwAnUEVICF0e25+BCPnAxa20RwZ1cqSZRYK?= =?utf-8?q?tLB53XXtTTpgzLLSEpXwEGTwKFpj3B4Ygzc+tWr3lWjc/ldpfJupnZzfq8tTeCz1j?= =?utf-8?q?It4Vnpv3rKKU834eglBDPZLAkrfsttBa1ha2HqnCwTPGsl/pvAJ6DqeqZQmwWTFgw?= =?utf-8?q?56dDTPqDv+/1bmlLHUFiEagbl8liJNrdTGX3zBYrCTyF3jj7EvhVoRzle9+XiktCF?= =?utf-8?q?7BtZtutwXXmbA34u9zcAWY2AVDiw/YBNbfPMWv+pw4paEK2ni441i3CjrU/7DzcJp?= =?utf-8?q?8XzS5jX8lJTIldKD2uznJP4ac5T9WJbKeq0nKgCDepgAMWAjD0BvjdOjlfTZU+OxX?= =?utf-8?q?m6HBkheLkAJzyONHO0B1vSsP9Gy54+mXr7HrrkmiB6062u+aeE4GeB+lhlCXogoxv?= =?utf-8?q?StjfZAVGiXMDywNyoztKLK0njcb3GpSbe7ZlAOoq3hbPxltdPS24KuOBZzTxaWmLQ?= =?utf-8?q?HthkEijzyfynk54joz2tr1n/rWPo4fYJo/V6uaVhZmC36Ppp4e5k5iX7NsRBcvt3K?= =?utf-8?q?8ipj5D3jLAqyhXOphFEItrUPVzTklszKahPR+RhNdJ+cLaShlSU3ZEq5StGnIXE4T?= =?utf-8?q?Ji5JlXCzN/rRbNeYWH3jaCXRBWRo0di0sHubKyqfBucsD+g/rkJrxi6aIKx71imjs?= =?utf-8?q?CmD3izmhDFagynSyZxHnhYF14wpUHqW8flF/XgCrLHfCtTn/5HazfWgSYqdTE/ytt?= =?utf-8?q?BWxtOil3KvfNXu+wjxxWiKj4HJnAp2U+63IXBXDaRPNEn/dLcNrdJ2vkEi5TFRArC?= =?utf-8?q?9emcQq42VX6Se98xY9PDTlyHfQGiI8Uyzqq3jUU1wbsfF7x/7EqmEkFpwVzik3+D0?= =?utf-8?q?6HNb3tsxlGCEnmW6KUemznQb7wNj5NFbuX9hTwlK6elNMAfBdoWr+9wbLLFjqkeTW?= =?utf-8?q?wR+zfq2Mse7cC2ouY3gnjM1zRC5WHa+/hrEm6qACMpg9kGCzx1Lk0OHX8WFpf9SmU?= =?utf-8?q?MEugJYi2FzwCd5ciFdnHuo5ipRQ/Z7Uvd46jrb6ST5lEnXwQ681aH5YEyEJwJvIfa?= =?utf-8?q?wS1z6bhOnB7/UoSNz0PWodaR4spszN4sP+8M53aKy0XgeHxe4fuKbuYe8zpGInf8Y?= =?utf-8?q?zZSkPrfHoZoxVXgtQYNrtgtPeSeWBFGmFmTw=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?arsBq3AJ471ERjqyKM5P4ZHKiprF?= =?utf-8?q?h2ulJ/+yx8dTU7AqeyfRNO6LQQVhAktkvem/prq4Q2rPc9mVf83lDEHZXMqcLIQOE?= =?utf-8?q?RiVnLjk6oGs1wWzKlwmp55JnyszfQtRx58RYJCjV7nEIb6cU8+xbbg1HQxvo1jq5w?= =?utf-8?q?weZ8MBhApXigroKVTyXxeHBS0cj+DjGegJrM64Fvss9GeP8/KknWAH0IcmfKYRalI?= =?utf-8?q?dmooVMNHq8ChmjMs+rFZ0ZkugNvG09WSq0DThBysFrl3MVqY9Tjvh63D3rSYyMeGd?= =?utf-8?q?6uENa53WGUuo54g5BJqlq9M7aMLSptPJ5zLDVbr2hdq/9ZAryyvzyY8ZG15TEwZ78?= =?utf-8?q?sJoxXd0kJatj7hn+zg4WptWhmn3zoGNphv4ncp7VwtQzPONWNvjJ3F8tTtR0issYN?= =?utf-8?q?cOIFwRemDXKrbaj8WcJXNkpcgAzl2pBA01zLO/va+TpagyQ7uwqQoapyG0/obhgnH?= =?utf-8?q?25MXcVgULTN2sbp7e5svECJYQFbUnAnHckON2cmAHUtcE1T4YUstlkWvNxr3Or+c0?= =?utf-8?q?P3aSMrnnAippzKFN/kFUymE5WApXFZG7lQC2qCY1W6vgvSkgoPAFj7TXjhPOH7QGl?= =?utf-8?q?rKnNTSoDnAFMZUFCsqsq14BuuJulEFsYIKUWGWDa3wCQ7xGAbpogjjxHwzytpusS1?= =?utf-8?q?Y1CJLXPwHuk+KwKtMkgBT1GM5DIu7GPeYui8S3r3BnmExbNe1Wsb7ndnwCvJtjSm0?= =?utf-8?q?sH9Vmtcx/u4CBX/FqWqfLMezKbCvjmVBF3MlR2jNVOjBYxa3bJRDQgoMrBQonTj6p?= =?utf-8?q?e3zyFUUcfkAbwxdDFpF/BwPulpeolbwCky8VuyQiv40GkOy50mnfOtU6rdY8aC0tH?= =?utf-8?q?rK1Ep8y4L8qtqUOJuvRu8JSqvGBHtm+/5Ohq2Qh4b710reOImROWlA2Kyn4zuU65O?= =?utf-8?q?l48hcc/SQfH6EUfOFDHUVrCLZa4cWuFy2M4zpDcQncVNYI43OLdOPQpn/Dt30I5st?= =?utf-8?q?anTLdaDkuWtsSv52uNLX+GQ5LGXdoBLorPySC8RJUupiHXVLvAGEslgiVpWJYaWit?= =?utf-8?q?odOO/AmSc5gmew1PxQXihmKvPiXhA5cmwEsE9vUcWCjmpWLVxhRKYpdCaAXAp8vjP?= =?utf-8?q?lmKWqgXYAtGBNF+8UNY7nuyq4sPEhOfshv2k4KsFf/1wnqfX2i5GaMIPkQAghdGhu?= =?utf-8?q?RCTB8TesXi+Cfa8wHwGkjTDvJVWelBtXlLMN0DEWRu6fZVeTsSAIzwV3gYgqviK4Y?= =?utf-8?q?RdljI2L0I+Kkg8kFAR4yXUPwoinlMcYf5lUEF/iT8cBIXGFPSkKJJYdF0NgHw/T45?= =?utf-8?q?+joxstV7/6Q2h9HrQCGc0ZvmfM6wUs/+Vh12Vk1C7WTpa+khI/Hce19VbMn5aH726?= =?utf-8?q?b6XPtjKL+NawPTe02xD9WjBpaANOCzIo9LxXNTaO7nsRVKWZsG7+PWnqQCcxBbq2A?= =?utf-8?q?POwugkoQ4iGrtZuZ3Mgllc5dgZDNq8IQ9FTnOYhGaPTImpUoxlIf7v02X7JidSZ0J?= =?utf-8?q?f7JnKJZDXxZRuWBPIT7B3T4rhdrBQy3oGjozT7Tz286uzZg2nrgu+DUUFt77hKE2b?= =?utf-8?q?FCLV7RJ7m1ZXdzAW+dmtUC/iObacka3Ithrbi8F8K5V318a5SaTeRuo=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: 76efda2a-dbfe-4acc-6faa-08dc7997941f X-MS-Exchange-CrossTenant-originalarrivaltime: 21 May 2024 13:11:51.1322 (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: OEjyhf9cf6v6cVmB/H8dCH0h9LVrTxJm4a2nvOOP0y5bX84h0shyvD7EcFtj88ItnUs0msS9NJ6HOBZ/NAHWIAd8QBr5tSh77Ko3lmWE+47hOaix29VyA+vkxoNDj9my X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR07MB6249 Received-SPF: pass client-ip=80.78.11.82; envelope-from=clement.mathieu--drif@eviden.com; helo=smarthost1.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 Signed-off-by: Clément Mathieu--Drif --- tests/unit/meson.build | 1 + tests/unit/test-atc.c | 527 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 528 insertions(+) create mode 100644 tests/unit/test-atc.c diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 26c109c968..d6c6c574de 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -47,6 +47,7 @@ tests = { 'test-logging': [], 'test-qapi-util': [], 'test-interval-tree': [], + 'test-atc': [] } if have_system or have_tools diff --git a/tests/unit/test-atc.c b/tests/unit/test-atc.c new file mode 100644 index 0000000000..89378f7f63 --- /dev/null +++ b/tests/unit/test-atc.c @@ -0,0 +1,527 @@ +/* + * 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" + +static inline bool tlb_entry_equal(IOMMUTLBEntry *e1, IOMMUTLBEntry *e2) +{ + if (!e1 || !e2) { + return !e1 && !e2; + } + return e1->iova == e2->iova && + e1->addr_mask == e2->addr_mask && + e1->pasid == e2->pasid && + e1->perm == e2->perm && + e1->target_as == e2->target_as && + e1->translated_addr == e2->translated_addr; +} + +static void assert_lookup_equals(ATC *atc, IOMMUTLBEntry *target, + uint32_t pasid, hwaddr iova) +{ + IOMMUTLBEntry *result; + result = atc_lookup(atc, pasid, iova); + g_assert(tlb_entry_equal(result, target)); +} + +static void check_creation(uint64_t page_size, uint8_t address_width, + uint8_t levels, uint8_t level_offset, + bool should_work) { + ATC *atc = atc_new(page_size, address_width); + if (atc) { + if (atc->levels != levels || atc->level_offset != level_offset) { + g_assert(false); /* ATC created but invalid configuration : fail */ + } + atc_destroy(atc); + g_assert(should_work); + } else { + g_assert(!should_work); + } +} + +static void test_creation_parameters(void) +{ + check_creation(8, 39, 3, 9, false); + check_creation(4095, 39, 3, 9, false); + check_creation(4097, 39, 3, 9, false); + check_creation(8192, 48, 0, 0, false); + + check_creation(4096, 38, 0, 0, false); + check_creation(4096, 39, 3, 9, true); + check_creation(4096, 40, 0, 0, false); + check_creation(4096, 47, 0, 0, false); + check_creation(4096, 48, 4, 9, true); + check_creation(4096, 49, 0, 0, false); + check_creation(4096, 56, 0, 0, false); + check_creation(4096, 57, 5, 9, true); + check_creation(4096, 58, 0, 0, false); + + check_creation(16384, 35, 0, 0, false); + check_creation(16384, 36, 2, 11, true); + check_creation(16384, 37, 0, 0, false); + check_creation(16384, 46, 0, 0, false); + check_creation(16384, 47, 3, 11, true); + check_creation(16384, 48, 0, 0, false); + check_creation(16384, 57, 0, 0, false); + check_creation(16384, 58, 4, 11, true); + check_creation(16384, 59, 0, 0, false); +} + +static void test_single_entry(void) +{ + IOMMUTLBEntry entry = { + .iova = 0x123456789000ULL, + .addr_mask = 0xfffULL, + .pasid = 5, + .perm = IOMMU_RW, + .translated_addr = 0xdeadbeefULL, + }; + + ATC *atc = atc_new(4096, 48); + g_assert(atc); + + assert_lookup_equals(atc, NULL, entry.pasid, + entry.iova + (entry.addr_mask / 2)); + + atc_create_address_space_cache(atc, entry.pasid); + g_assert(atc_update(atc, &entry) == 0); + + assert_lookup_equals(atc, NULL, entry.pasid + 1, + entry.iova + (entry.addr_mask / 2)); + assert_lookup_equals(atc, &entry, entry.pasid, + entry.iova + (entry.addr_mask / 2)); + + atc_destroy(atc); +} + +static void test_single_entry_2(void) +{ + static uint64_t page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0xabcdef200000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eedULL, + }; + + ATC *atc = atc_new(page_size , 48); + atc_create_address_space_cache(atc, e1.pasid); + atc_update(atc, &e1); + + assert_lookup_equals(atc, NULL, e1.pasid, 0xabcdef201000ULL); + + atc_destroy(atc); +} + +static void test_page_boundaries(void) +{ + static const uint32_t pasid = 5; + static const hwaddr page_size = 4096; + + /* 2 consecutive entries */ + IOMMUTLBEntry e1 = { + .iova = 0x123456789000ULL, + .addr_mask = page_size - 1, + .pasid = pasid, + .perm = IOMMU_RW, + .translated_addr = 0xdeadbeefULL, + }; + IOMMUTLBEntry e2 = { + .iova = e1.iova + page_size, + .addr_mask = page_size - 1, + .pasid = pasid, + .perm = IOMMU_RW, + .translated_addr = 0x900df00dULL, + }; + + ATC *atc = atc_new(page_size, 48); + + atc_create_address_space_cache(atc, e1.pasid); + /* creating the address space twice should not be a problem */ + atc_create_address_space_cache(atc, e1.pasid); + + atc_update(atc, &e1); + atc_update(atc, &e2); + + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova - 1); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova + e1.addr_mask); + g_assert((e1.iova + e1.addr_mask + 1) == e2.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova + e2.addr_mask); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova + e2.addr_mask + 1); + + assert_lookup_equals(atc, NULL, e1.pasid + 10, e1.iova); + assert_lookup_equals(atc, NULL, e2.pasid + 10, e2.iova); + atc_destroy(atc); +} + +static void test_huge_page(void) +{ + static const uint32_t pasid = 5; + static const hwaddr page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0x123456600000ULL, + .addr_mask = 0x1fffffULL, + .pasid = pasid, + .perm = IOMMU_RW, + .translated_addr = 0xdeadbeefULL, + }; + hwaddr addr; + + ATC *atc = atc_new(page_size, 48); + + atc_create_address_space_cache(atc, e1.pasid); + atc_update(atc, &e1); + + for (addr = e1.iova; addr <= e1.iova + e1.addr_mask; addr += page_size) { + assert_lookup_equals(atc, &e1, e1.pasid, addr); + } + /* addr is now out of the huge page */ + assert_lookup_equals(atc, NULL, e1.pasid, addr); + atc_destroy(atc); +} + +static void test_pasid(void) +{ + hwaddr addr = 0xaaaaaaaaa000ULL; + IOMMUTLBEntry e1 = { + .iova = addr, + .addr_mask = 0xfffULL, + .pasid = 8, + .perm = IOMMU_RW, + .translated_addr = 0xdeadbeefULL, + }; + IOMMUTLBEntry e2 = { + .iova = addr, + .addr_mask = 0xfffULL, + .pasid = 2, + .perm = IOMMU_RW, + .translated_addr = 0xb001ULL, + }; + uint16_t i; + + ATC *atc = atc_new(4096, 48); + + atc_create_address_space_cache(atc, e1.pasid); + atc_create_address_space_cache(atc, e2.pasid); + atc_update(atc, &e1); + atc_update(atc, &e2); + + for (i = 0; i <= MAX(e1.pasid, e2.pasid) + 1; ++i) { + if (i == e1.pasid || i == e2.pasid) { + continue; + } + assert_lookup_equals(atc, NULL, i, addr); + } + assert_lookup_equals(atc, &e1, e1.pasid, addr); + assert_lookup_equals(atc, &e1, e1.pasid, addr); + atc_destroy(atc); +} + +static void test_large_address(void) +{ + IOMMUTLBEntry e1 = { + .iova = 0xaaaaaaaaa000ULL, + .addr_mask = 0xfffULL, + .pasid = 8, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + IOMMUTLBEntry e2 = { + .iova = 0x1f00baaaaabf000ULL, + .addr_mask = 0xfffULL, + .pasid = e1.pasid, + .perm = IOMMU_RW, + .translated_addr = 0xdeadbeefULL, + }; + + ATC *atc = atc_new(4096, 57); + + atc_create_address_space_cache(atc, e1.pasid); + atc_update(atc, &e1); + atc_update(atc, &e2); + + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + atc_destroy(atc); +} + +static void test_bigger_page(void) +{ + IOMMUTLBEntry e1 = { + .iova = 0xaabbccdde000ULL, + .addr_mask = 0x1fffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + hwaddr i; + + ATC *atc = atc_new(8192, 43); + + atc_create_address_space_cache(atc, e1.pasid); + atc_update(atc, &e1); + + i = e1.iova & (~e1.addr_mask); + assert_lookup_equals(atc, NULL, e1.pasid, i - 1); + while (i <= e1.iova + e1.addr_mask) { + assert_lookup_equals(atc, &e1, e1.pasid, i); + ++i; + } + assert_lookup_equals(atc, NULL, e1.pasid, i); + atc_destroy(atc); +} + +static void test_unknown_pasid(void) +{ + IOMMUTLBEntry e1 = { + .iova = 0xaabbccfff000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + + ATC *atc = atc_new(4096, 48); + g_assert(atc_update(atc, &e1) != 0); + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova); + atc_destroy(atc); +} + +static void test_invalidation(void) +{ + static uint64_t page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0xaabbccddf000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + IOMMUTLBEntry e2 = { + .iova = 0xffe00000ULL, + .addr_mask = 0x1fffffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0xb000001ULL, + }; + IOMMUTLBEntry e3; + + ATC *atc = atc_new(page_size , 48); + atc_create_address_space_cache(atc, e1.pasid); + + atc_update(atc, &e1); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + atc_invalidate(atc, &e1); + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova); + + atc_update(atc, &e1); + atc_update(atc, &e2); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + atc_invalidate(atc, &e2); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova); + + /* invalidate a huge page by invalidating a small region */ + for (hwaddr addr = e2.iova; addr <= (e2.iova + e2.addr_mask); + addr += page_size) { + atc_update(atc, &e2); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + e3 = (IOMMUTLBEntry){ + .iova = addr, + .addr_mask = page_size - 1, + .pasid = e2.pasid, + .perm = IOMMU_RW, + .translated_addr = 0, + }; + atc_invalidate(atc, &e3); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova); + } + atc_destroy(atc); +} + +static void test_delete_address_space_cache(void) +{ + static uint64_t page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0xaabbccddf000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + IOMMUTLBEntry e2 = { + .iova = e1.iova, + .addr_mask = 0xfffULL, + .pasid = 2, + .perm = IOMMU_RW, + .translated_addr = 0x5eeeeeedULL, + }; + + ATC *atc = atc_new(page_size , 48); + atc_create_address_space_cache(atc, e1.pasid); + + atc_update(atc, &e1); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + atc_invalidate(atc, &e2); /* unkown pasid : is a nop*/ + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + + atc_create_address_space_cache(atc, e2.pasid); + atc_update(atc, &e2); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + atc_invalidate(atc, &e1); + /* e1 has been removed but e2 is still there */ + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + + atc_update(atc, &e1); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + + atc_delete_address_space_cache(atc, e2.pasid); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova); + atc_destroy(atc); +} + +static void test_invalidate_entire_address_space(void) +{ + static uint64_t page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0x1000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eedULL, + }; + IOMMUTLBEntry e2 = { + .iova = 0xfffffffff000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0xbeefULL, + }; + IOMMUTLBEntry e3 = { + .iova = 0, + .addr_mask = 0xffffffffffffffffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0, + }; + + ATC *atc = atc_new(page_size , 48); + atc_create_address_space_cache(atc, e1.pasid); + + atc_update(atc, &e1); + atc_update(atc, &e2); + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + atc_invalidate(atc, &e3); + /* e1 has been removed but e2 is still there */ + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova); + + atc_destroy(atc); +} + +static void test_reset(void) +{ + static uint64_t page_size = 4096; + IOMMUTLBEntry e1 = { + .iova = 0x1000ULL, + .addr_mask = 0xfffULL, + .pasid = 1, + .perm = IOMMU_RW, + .translated_addr = 0x5eedULL, + }; + IOMMUTLBEntry e2 = { + .iova = 0xfffffffff000ULL, + .addr_mask = 0xfffULL, + .pasid = 2, + .perm = IOMMU_RW, + .translated_addr = 0xbeefULL, + }; + + ATC *atc = atc_new(page_size , 48); + atc_create_address_space_cache(atc, e1.pasid); + atc_create_address_space_cache(atc, e2.pasid); + atc_update(atc, &e1); + atc_update(atc, &e2); + + assert_lookup_equals(atc, &e1, e1.pasid, e1.iova); + assert_lookup_equals(atc, &e2, e2.pasid, e2.iova); + + atc_reset(atc); + + assert_lookup_equals(atc, NULL, e1.pasid, e1.iova); + assert_lookup_equals(atc, NULL, e2.pasid, e2.iova); + atc_destroy(atc); +} + +static void test_get_max_number_of_pages(void) +{ + static uint64_t page_size = 4096; + hwaddr base = 0xc0fee000; /* aligned */ + ATC *atc = atc_new(page_size , 48); + g_assert(atc_get_max_number_of_pages(atc, base, page_size / 2) == 1); + g_assert(atc_get_max_number_of_pages(atc, base, page_size) == 1); + g_assert(atc_get_max_number_of_pages(atc, base, page_size + 1) == 2); + + g_assert(atc_get_max_number_of_pages(atc, base + 10, 1) == 1); + g_assert(atc_get_max_number_of_pages(atc, base + 10, page_size - 10) == 1); + g_assert(atc_get_max_number_of_pages(atc, base + 10, + page_size - 10 + 1) == 2); + g_assert(atc_get_max_number_of_pages(atc, base + 10, + page_size - 10 + 2) == 2); + + g_assert(atc_get_max_number_of_pages(atc, base + page_size - 1, 1) == 1); + g_assert(atc_get_max_number_of_pages(atc, base + page_size - 1, 2) == 2); + g_assert(atc_get_max_number_of_pages(atc, base + page_size - 1, 3) == 2); + + g_assert(atc_get_max_number_of_pages(atc, base + 10, page_size * 20) == 21); + g_assert(atc_get_max_number_of_pages(atc, base + 10, + (page_size * 20) + (page_size - 10)) + == 21); + g_assert(atc_get_max_number_of_pages(atc, base + 10, + (page_size * 20) + + (page_size - 10 + 1)) == 22); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/atc/test_creation_parameters", test_creation_parameters); + g_test_add_func("/atc/test_single_entry", test_single_entry); + g_test_add_func("/atc/test_single_entry_2", test_single_entry_2); + g_test_add_func("/atc/test_page_boundaries", test_page_boundaries); + g_test_add_func("/atc/test_huge_page", test_huge_page); + g_test_add_func("/atc/test_pasid", test_pasid); + g_test_add_func("/atc/test_large_address", test_large_address); + g_test_add_func("/atc/test_bigger_page", test_bigger_page); + g_test_add_func("/atc/test_unknown_pasid", test_unknown_pasid); + g_test_add_func("/atc/test_invalidation", test_invalidation); + g_test_add_func("/atc/test_delete_address_space_cache", + test_delete_address_space_cache); + g_test_add_func("/atc/test_invalidate_entire_address_space", + test_invalidate_entire_address_space); + g_test_add_func("/atc/test_reset", test_reset); + g_test_add_func("/atc/test_get_max_number_of_pages", + test_get_max_number_of_pages); + return g_test_run(); +}