From patchwork Mon Jun 3 05:59:52 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: 1942657 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=eviden.com header.i=@eviden.com header.a=rsa-sha256 header.s=mail header.b=E4GXZFZA; dkim=pass (2048-bit key; unprotected) header.d=Eviden.com header.i=@Eviden.com header.a=rsa-sha256 header.s=selector1 header.b=YdJ4ccqj; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vt34w5844z20PW for ; Mon, 3 Jun 2024 16:01:20 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sE0jw-0006te-W2; Mon, 03 Jun 2024 02:00:17 -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 1sE0jm-0006oX-O5 for qemu-devel@nongnu.org; Mon, 03 Jun 2024 02:00:06 -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 1sE0jg-00072f-TR for qemu-devel@nongnu.org; Mon, 03 Jun 2024 02:00:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1717394401; x=1748930401; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=NDvmTaxDzYjsRZ3EF+zbL5RdaMiNQK17KHNCW8LJaoM=; b=E4GXZFZAv0nGhEHSEK0MUemfcjD+naWn5SuhAy748WOJqbcXBKSnAbEB TKzK7gac3X3nV8YdpWcC9hZUPzCsmfrf4RXkQWSUQ6x7H900HqD9rGTUk EEGaOM+V46hRDrySExDQg9pxGQ/8YwwxbMQ/6yRN9GDfplv7t5iGOHx8k cSAgPxJrDuxyTxErJJAvNoa8oJeJ5Zyyz+hPDBhxTEJYGiW9T2Gd5fRLV NE12PrdVBUpuQ1NhBise5r0HrmC/nASpUIeOsMvYVaIkaZ+BfM1C/HAvB pLV5tsWzHRWI8f1N3nmljzL6/2IeeGCDA/Mhy2xtxyn1ULJgQgoo58rvO Q==; X-IronPort-AV: E=Sophos;i="6.08,210,1712613600"; d="scan'208";a="13763252" X-MGA-submission: MDED4qULRP8Bq+zqwY8c4DqSEmxZ1bZhR7UUkn6AiyIx4SGelyFpxDANqd/CU+AFSE3rFDAOisfJHzsouF1QeJ0adTyRENZGDvkoQoXhU23ZdgkGU0zhUO9yRkhn2/qD/SM/gOzCT+dlwE0izf4+2NCIdZsHaJrMsh5ODwEC3woepw== 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:56 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hekcvG4Hmgz0f4WST6Y9R/fQOh+T3ddKlSWK12ejU1njpBG4tzAK7DbiOlwlWPlpR9NSqBDN/jMaFW45d0Xg0J7cg2wvBfEwIS5S01vCWVmei+8jmoRvnOlbOxCKXal1KOMUweL8iPoBJQqJwYXc/6aoB6YmfuDNHfd4T3bE63y0J5Ml9JCniSJuLwKCkv2XSeGR7uSgDOxS6Pwf1ZQbEe2Mkjf35m6eq9X22Huh13teeJwSzAxRyx0fc4oNxzx8T9biq5I+iu2lP5DUp9GDvvUrEFBvHyiXlgxv016Bj+NQAV39O1obGLu+2636GjaU/4giWUHd6p1TWw/feXxUGQ== 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=NDvmTaxDzYjsRZ3EF+zbL5RdaMiNQK17KHNCW8LJaoM=; b=U4Y3SXvx9gYpkVhdl/dNKvHFtooHBmRmFjtezFeGcVR4uiymUbO3axyJn3JXEFEH6/3+TFu1G9qe9nparGcoiFSw6JAWI8ENYbsRw8RDBb4lK6EiWd+rx1pB8qtnNeTJMdB/Vj71DK2K0fz/efYmfm91ksFV1RvRWTXDCE3DH3jO6XJWCJUdmZ73UTnOv2eQ+BeP2X2Xni/JB7zsfBbbR3ymxEdUFRPPbJbnHNvvifqtYSI3oX8qmyMkDyyGaeBeTrVBWX7w24XSmrLPc6v2WBWuYBF1BOucum7Hc/k1kB7EncKJnHIUyOQw6qOzovqANJdw2Y3krwjJIiC0saNIOQ== 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=NDvmTaxDzYjsRZ3EF+zbL5RdaMiNQK17KHNCW8LJaoM=; b=YdJ4ccqjcsR3HwdhyBxU1+wVOSbCGCe/F5bdVZWQSe16cvmZD/u0k6S8L9kg3GRFnN7UfAk2lLF2uL4yJ+GBMalbGFgI1FDtpLAEHbmjRaSRgGqVT89vTqD0HvH45ZrMvVBs/zYGUTqxOLN0GFBJS6DiHfXMrmvZdMj007QrHTwXaV7MG16KTIdoos1I9egWI6dj6wxM5Y/pcYX8W9uwmE7eEjO80gI9xg1ERdmg3KBNm0bc5cEWDBCxRluIr18fghawTVhvQtoM73REf4NM18hiPVFE39pNFz6X/G1j8W/X9tCRJgIZhE6tvDxY3p9xPRFoBdH1VYfScDMosDUzSg== 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:52 +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:52 +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 18/22] atc: add unit tests Thread-Topic: [PATCH ats_vtd v5 18/22] atc: add unit tests Thread-Index: AQHatXtAHjZRSkrQhE2gCr0Vvwwm6w== Date: Mon, 3 Jun 2024 05:59:52 +0000 Message-ID: <20240603055917.18735-19-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: 798fca15-90e4-4fac-789d-08dc839262b9 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-8?q?4SVzJajLEq29MUWp7rwrp340MZVoUzF?= =?utf-8?q?Mjy2xwJe+pd1dd0F3RBb/lbIkCcEflK1YCc0hG9R5buS03mFiOIN2EzUywWgeHywD?= =?utf-8?q?cvZwz1SWlWVi6kcPPdWiKqHEZDqK56j6vB15Q+j98NQ3el3K21oZPmp4ImY8PY9nP?= =?utf-8?q?Shx7CxtfEBtpKpkDy+uTOVhq7bbqs5oWe4jM7s0CXrC3Op7hMhy61urHyB8cgWZql?= =?utf-8?q?UCDRPLUQ6i+VB5hRp7bAI2hVGUt36wZnXivb3kEtxE8nxXHmo4BOXV5uWe8paQtOs?= =?utf-8?q?bPr9NDwL2e2SuweSxRd4w1WB+cKLP2egRY9/4Ofit/ckZpcglmSKIaWMjksu3P0VP?= =?utf-8?q?Kn5Sdzvfk6piJJWnGTYL069DtwS6pb5hVHEM39fxWyMS2/syTimgaI93j9ZeFa88d?= =?utf-8?q?UBp4ppER8OmgeJL7a7aO0a5XPGJbtMD7BW6VxR6w66zT/87nYn4tqYLDv5CapiIAG?= =?utf-8?q?nzLAvsHIT7ag0z4cz4EIqbCpFurM5f+BfSy9O2fefmvTrbhRTnLoWAjkvXyKwesKG?= =?utf-8?q?cC6TZ/5PhUbSEdYYJE5EFM3R6WfW2n/dMfLT6hP4wZ44/vRecRJbBq6G9XYxdOA2J?= =?utf-8?q?mMYCI03TNaytJ9SKBU2WrxS7NqfLAoUzSkRKQiyo1ovelCtGeYoSdjIttAY7pmOp5?= =?utf-8?q?FTgDSyEFFMV76OlR46CkjBvCf3LLulo7ZKhmQAnGALWII8VKV6wmtWiPu3Mlo1f3p?= =?utf-8?q?SSaSRie14PeFSD3uqvJkUYeKyY+orHE/7D48brCctVPwk9h26fvPcUtNXwFOC3R+K?= =?utf-8?q?TuafnZVWq5l1HBQmIn56lSEs0BUrqlSVdB1yGt9yeCzNyNk4xOUzvkLYGxDEtzVSo?= =?utf-8?q?bFeW+zSxZyFhG0dWlZJPPO8/w+JTFEGdb3mPWngFOnl2lr5KsCuScetYUIJdAVBGL?= =?utf-8?q?wk7UpN7WBZu0a2WnV9BKxjMmdNt9uUugcHy+5qNYWux1APTnNFrs1PAgB8cIjSLbo?= =?utf-8?q?vaFtGO8IJ5YrJIApqNaAoc6zvfV4FZEmkJ6/KaavZi1ogBfOTCR2W/K9LlOvf0MCE?= =?utf-8?q?/1Qq0NkyMIgOsL8crciC13Zv8M4KdrhjLzqHq2qxTAsz/0sKUAydwSOAlSEIzDITM?= =?utf-8?q?fKMZZA+gBPC1B+JktrnN5wTidILRC5l+uImHKd3p9jGvcLffrjuzhMldNTSnRPEO7?= =?utf-8?q?YW7tyUPZS7ZbB1r1fTd6cKDnqS8npkCcnLSs/1Wf7a5vMOE4+WryUffgHYYl92Fb6?= =?utf-8?q?su73JCFF12gHt7y1VZeNtVfpBBe+pOCGaZz0KcXHd0LcW0c0KgijXaSHeWYKHmbKW?= =?utf-8?q?I0oxvdk5pxtVveu6aN0iUrMgOdC+hLnugLQ=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?eMflOy+II6QZUOOkxipMlzRvmASW?= =?utf-8?q?Y8vEXwX9m/XRie/JizWV32+Qq36oBkIK9jHcNpXs7wZNaC4jTcYb7hE2EFXoH51Hf?= =?utf-8?q?GaEwwRqgSAuoAx54gKdRwsEm1NVQgp9KwfJkE7ZxovdIYC3CXURuq6ARdma09anBG?= =?utf-8?q?x7d9jQp15OuurSRL9FNvnxKTheovK2IQ+Oi+ZiivBwLokopRBuq0W1DOp5AgrLvOx?= =?utf-8?q?HEoUThhWGBgXNBka5Xm7JAdQc8xQpQ+VjhTHPlkfwdYVxAuaDfKq1p30OugCXNIqU?= =?utf-8?q?5/mYUilkQ5DuJvEE0maC55cLFYsACoY1cJbmkDNVkILS+KBsxTM5mCV0SCsaudGeP?= =?utf-8?q?p4E5a4VTI9SRIYyUsWdTz9A6+Yr6cxZlFtAGXFiaAIteth+W/1jGbHW/o3FpOlGoA?= =?utf-8?q?+Cdv9/JuFfb3SsASrazLSQtTgS9uW5qHUr07X3qfU4YxnMOaF2GUbyiKSd/GGUzrq?= =?utf-8?q?ZeDeHaY58m70vBaG5Dyj+rix56YZSlV1rUiTtTvddfAGWZaSxv88UoOdqFWyAldrK?= =?utf-8?q?NlFcEorZlTIydpYPM2xs7tBfgUkuUoL1DdFDKRRPo0L8Cml7sAjW2dxV00IW7gAih?= =?utf-8?q?/vl+KniY7BLPp7Ky9GluM7Et+aVVQ3b6vDyX4nx/aKYJ+fe+Nh8QpZMb1EJltan65?= =?utf-8?q?lXoTJ+GpS5EE92pntGFRinQp9foz1JjobIrFYvYQg5K8OY4gBFocA7uczrAGHRkyi?= =?utf-8?q?H9RiPbYekEAswf1E2ZzzUOIk7w5ozsCOwvArsxlfSlDEWcEOB3NgAVd9kKtHEzgqn?= =?utf-8?q?FQ5SJ1eGa9AN1LOJcGeQFbMTZSNhI8Yd+ljj0COW4QDp2A1rj3coE2vkEIGzyt/nn?= =?utf-8?q?HMunk6L1V1I9vTM3EKxhWzmvXX31GX38Wji9PfOUqsTq5HshSvLMLgv/WJE7S1dql?= =?utf-8?q?pmlM5ML8cfNQT52+UOVIVafSGWjegkXnghSMJ1pBwJ5WWr/Irm16K2+Nw8Cw2bD10?= =?utf-8?q?J2+mDwIPVNKOQQ0YsAtvcwOIb5rnIRZlfr6897rqwz9e9ON8fsQNuBGEI8+J8FoxH?= =?utf-8?q?WWCH3i+8ruxjdgqWA8B6IGpNf94mjAS2SDusq1KppID+1iTBPqERFEDvQj3Y+g7je?= =?utf-8?q?GIjAQzr7TpAwByST7hvCtV4gn0xPaFs5qmevt3UT4P5slsM6oxHLZSQPtVXZ6YEfh?= =?utf-8?q?EgArfmKQzDV5WQm/E6SaxiJt9wHJyvvDmgrM7KaNkRwtpLXNw3OpynA3e5Mo+JZM3?= =?utf-8?q?y/L+WQr1UTqq5Ow9pPERwrX7sdk0p0IArQlEPMsEZNLWyeEHMGJTS/y1fX7mQQmuf?= =?utf-8?q?y3CWFk8MA3z+H7ZWkqgrj8mFjAtprNl8YWQtqDfbOToL0O2JRSE9KYIR0oUJ+pTA8?= =?utf-8?q?bBdmIVtd+LYsefn8SgF/wdf2o/XEAIPdhTUFR7lfYHfQbjxuFgvBXVyeopE32hBQd?= =?utf-8?q?9yfJdS5EH6/e0cwlTQ0KxTsuOHkQIiFUJEUQgpIqBIvOmnQrZu1KBeVmyCawLyKnD?= =?utf-8?q?rilKO7pJj+gnxkvXaeUu4QDkTYI30eijp7yS3RVgZgd7sAAYSeSk0kJZXyi9Pz7BS?= =?utf-8?q?zWwyZ4jKXqMQxmNM7a+oXIBp41Tp5gE+6aMRTl2zrftPMuTgUiq3EwQ=3D?= Content-ID: <325765F4E6859F44889687663EBFD776@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: 798fca15-90e4-4fac-789d-08dc839262b9 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Jun 2024 05:59:52.4223 (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: tZd44VXWHtBQMYDQPIpF7QZzep/LPWTXQ6KZf98iuHkjSrQhGfEDZdlLFwJtzWiXhVj3FTI3IOXbaOHpfWJnC15VGj4rYXqYm1znO2OmAyl/UUIsoPapMXsCJsejQHBh 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Clément Mathieu--Drif 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(); +}