From patchwork Mon Jan 20 17:41:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CLEMENT MATHIEU--DRIF X-Patchwork-Id: 13945443 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 E2836C02181 for ; Mon, 20 Jan 2025 17:45:02 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tZvmi-0004mC-3d; Mon, 20 Jan 2025 12:42:00 -0500 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 1tZvmg-0004it-98 for qemu-devel@nongnu.org; Mon, 20 Jan 2025 12:41:58 -0500 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 1tZvmd-0000aZ-E2 for qemu-devel@nongnu.org; Mon, 20 Jan 2025 12:41:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1737394915; x=1768930915; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=cfjC/wqFYSpLXoJOJ3oLFuGzwUlEFdalwLBOR1vc6bY=; b=tEqX6gUs21zeSZNcF+6vnFRVeXnBmr/xDbnz305JWH1FynQGV48R1g9i sKjWVR58RIDm9pLhxqgC+R1zX14tY8Erc7/YKjWPguDX2Mf6vZljX3zuS YEkjquNj8JZdlj18pyfmd8IW46PJEHUDD3ZBhAQRdzi8msrQyCEOmqbai uL6mXbVy9gMw6CsFpbMsfG6vi3BtPPS3+6quOGUuIz5dQD4me6WtTW3GA oqWuheydcojjcdESFy/MUfuabXL44NikyuxRhozYNgcRygTs+2RR7uDLq hzVAsmf/qV7sQVi8EMkuhcc6jsLvGGkgUfE9rjoOgJlElUxzhPvpVIqTU w==; X-CSE-ConnectionGUID: 5nRQC8sYRDSaF0bIaATAmw== X-CSE-MsgGUID: /C4lwRNYT8CuTWkNdgJIsA== X-IronPort-AV: E=Sophos;i="6.13,219,1732575600"; d="scan'208";a="29133403" X-MGA-submission: MDH6TmX8E4XUuBXgITKzvDkz7UUUuG7OGXDYzi7xRXLIsUT7D+m7D7sPQucnOLkmqG3TzNbY/7+xTuXZx7r1x01NOl8JPc2wJqf7nCB+l+7dHsCUmhXJS5Q8eo/YrFwidHLoFCOAgdxCIjdusGFGb1B+hkxoMPeUrsxGh665EkYNcg== Received: from mail-am6eur05lp2113.outbound.protection.outlook.com (HELO EUR05-AM6-obe.outbound.protection.outlook.com) ([104.47.18.113]) by smarthost1.eviden.com with ESMTP/TLS/TLS_AES_256_GCM_SHA384; 20 Jan 2025 18:41:54 +0100 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=aaIXhkgCKsswHvL/TQLRee7x7ZAN0RP8FyGB5INaHjksk7V2m8TafXGgqQJgc879zpeoHTW/4r98g9nTP8heY9I7muUK1wtZ+hkDdh4Djc0/z/Tsure7lW0m2QfmVKAEcMJVszl8/jtzzt10eXb5TsnenlipZvL/n7cI0U4fLoNj9HBOzEI25XyzvIh8vmli+oNKbB9KflK6tSYyaL9XyMAagqQ4vaPw7RTjeNl4pkdAxRFBL7xMO1w+E4bMCQRQL0FJWclNC+NsF3FAgyTYtuNW8gfEazGCaweBlFhZKBC0oRAdoYX45z2JHNwVIYH8doikNCEmoXVbPNxIMQBPGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=fRYAqsK3djNecieuq9gZMJbKslo9CcsSie6ejP5sSps=; b=M1f6z6hx+CBqc6DtDOfv0zdP172tk+L5ugpWUBu4921DqAB4frInLEDMbgn4XmiwTNQBr98K30vqxqeG0Z7zAe4xwunj5J5H+ThCd5Ka+3iauuSPlHsx7CVCssbCCr3v5SzT7wa7XBd4JkKi98BsLzUDAGP/2c5+UFcCXjHYk6sZv3JMNI632ExteqPf3zycp/siadJK6n145ndXRu8hHL447y3c0HYIWpM1wnqnvlfySZ7+KdtbpdzfXQ+IpbTaNrQ92zZjmoFkQYG2oWBr/pZ/JRAW9w0es22fIejcPqEX2dFzdQmwVV7fM7KfJzblnxV1sX8e+C6u5O5WLS7iXw== 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=fRYAqsK3djNecieuq9gZMJbKslo9CcsSie6ejP5sSps=; b=QZnlB8XO471mpRzeujeuPV+9tSuSxMx92CUtGr7uff1MYYYCpHbDJSWNhpd73OAwd4uAw+lMwsYp9joTDxeoHiO7GGs5MIzwzhSz1xFX2Z4nTmwuFDmZZKBN+LuoXQJ0U6+PEm0WOeQwP5h0+J2GZiVmQrJbqy9imPw2blCzBC3Xlh97s5t9sJt+NkzQuzysTMC6sKvJfEXZucEubiCifYg0FL1OogLdM/p0GH9ZifGiZUWTFMdNe0VnKa9+PNFwwmcTYm06vKcjKBe2z4p9jHEtxoGPL0RTvWgA99i8g3DDpG6xvY+1RkJKbgK9531LF9BzfZkBumD/hFbO5/neew== Received: from AM8PR07MB7602.eurprd07.prod.outlook.com (2603:10a6:20b:24b::7) by DBBPR07MB7548.eurprd07.prod.outlook.com (2603:10a6:10:1eb::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8356.21; Mon, 20 Jan 2025 17:41:53 +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.8356.020; Mon, 20 Jan 2025 17:41:53 +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" , "tjeznach@rivosinc.com" , "minwoo.im@samsung.com" , CLEMENT MATHIEU--DRIF Subject: [PATCH v2 14/19] atc: Add unit tests Thread-Topic: [PATCH v2 14/19] atc: Add unit tests Thread-Index: AQHba2KXke9thKhyt0qrO3dDNNVJOw== Date: Mon, 20 Jan 2025 17:41:53 +0000 Message-ID: <20250120174033.308518-15-clement.mathieu--drif@eviden.com> References: <20250120174033.308518-1-clement.mathieu--drif@eviden.com> In-Reply-To: <20250120174033.308518-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_|DBBPR07MB7548:EE_ x-ms-office365-filtering-correlation-id: ae6fac31-a220-4d1c-2d76-08dd3979ba08 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230040|1800799024|366016|7416014|376014|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?JU/CI/WiorAYeRzEnpBm9xh/w9?= =?iso-8859-1?q?F21L1lTFwvs6/KZEca8hBlkokcY/tD4BIVgIWWO56uEXQpmhOOFXSbP6DAKF?= =?iso-8859-1?q?i+H5tjHJdwqqC3LXKNLyu2Jn5cuejZ42T9u7aSqt2usT40lL3wewvgzG7Q3b?= =?iso-8859-1?q?4PL37lycCkm/b7PW7lB9lo8N5mYF6fDjDFb8GNKEYv2+lgoLPPuxJpIrqyom?= =?iso-8859-1?q?0hpOg83P4qZDVlY4Enk/t8iSq3MS/hiEIhRu7cND+KMr5tgxX59TrTzlTjfI?= =?iso-8859-1?q?x5XpUWJir4+JOWlbEwCL1U4yiR70gSpEwOmujUX9nB9dc1HxJ6fTxv54ypLA?= =?iso-8859-1?q?mBpPwKLcInNkPwNFr59jrhgCS6Z3UTZIsqeJgpg8TV2vavllahHFs+wTlaaK?= =?iso-8859-1?q?yWdEk5/DaxH0BNhe0zKNZw2hZtaUWqvpMhSKaFMsiFWZMiHstasMEPChZqvn?= =?iso-8859-1?q?ax0wuVe92gV8shDoWAleOjq7PwiDDumFYqCjf6hOhRvYRhPQtpDo5HzPf4/a?= =?iso-8859-1?q?DFuaGbggBvUVRfJrWoCegbx1vFWuKC+fBhz5iW7ncvfUcH3ectYus7VFpPbC?= =?iso-8859-1?q?wNIuFeoHIHE30BsHpF8h1c2muBwR4mI3MrsDnd4E//yJFV9btxDBusLYoCx+?= =?iso-8859-1?q?eM0l3Rc3Mp3gxA2QiAZniwOZAtb1gAI7IobDM1JaggFFdeRDQyjazRPibMz7?= =?iso-8859-1?q?07zrpbJDfZTym9ifk1vI5+w+swTHGp+FEf7mpfPFv7OMWjjB+mH58X8+8Wu/?= =?iso-8859-1?q?gQiqbByRQZvdiNnKF7Xeis2YQarR95ExhWIgIUvnDitCHrswEb1TOsQo/uVZ?= =?iso-8859-1?q?tPtDWd/fZz3JGdG/DELEUqWcdgNMsOWaMMpBn2ul+4C6Ld2uI0bz2DSEqrMR?= =?iso-8859-1?q?NeaUSDgXGC39RA00RGtAz21lqa0GxYYi04ZSHuL5RBtDR8/zsqWQZnB2HtBu?= =?iso-8859-1?q?8vMa/wnktjrZ+SiCl0D42PxO3w+/VuL99eoJiBhw/YfYZl5T6qTRKka2QmTF?= =?iso-8859-1?q?Bi4h2ZPUj3XfjHCqkBbNv53Y4rN8PyNvFTo/m71RJGDFhFXf5Npkz0ZXzOS/?= =?iso-8859-1?q?wxfE84ef3NAn1uqhEufV6Dk0mlyxOBSkiiN4S7KDbS/P/WWQxC/9rns62MUB?= =?iso-8859-1?q?JA3dP6jxtrC/QvmdPBT/LAiGgfPQZUPvcaRxMB7TrTPb0skepuYoM9rTKF8B?= =?iso-8859-1?q?bRNDtbypEUN/IrDxfRgqXPp58MXl+fB7RK4K90J8jOFM/gEF1uMn765e7bhp?= =?iso-8859-1?q?07Sq/TQUFqsCjmEBBVzT9ZwJRFEiKMamvLROlLkTWsdMkRYxZq/Ye/kLHMLM?= =?iso-8859-1?q?59Kr0piRqfLLCpEzfv/DrVIWHNF1m+PPcw3sZ7+lc1sV1Jc/ajPiOob4ZCcK?= =?iso-8859-1?q?VU+oDVgLEC//ttBeu4rKc45wHPvtnyS6H7UTPQhDkikmoC33MvS8E6l6269G?= =?iso-8859-1?q?gO1CQHYDfVsrVKgeTLfqf1SA=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:(13230040)(1800799024)(366016)(7416014)(376014)(38070700018); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?LamExWdQz8VFP/n5v6tw/4k?= =?iso-8859-1?q?m96bVd2tD5UVh0evAbT/GBDPpHs8xBtKfrqgrgxRz7DB7s+ceLtveXDjzHUL?= =?iso-8859-1?q?JVcRfWdlO5uo9bCoJR1jhjENaDrq1hFXT6uEjtqECyGOMUsIP5VrGvu71gO7?= =?iso-8859-1?q?FWrS9Ngir9muiiD5NVIxuzUS85ZZR0lHHOzyAh/waV9sh41P0InqY3bR9eci?= =?iso-8859-1?q?dkG7uk08tk6bA9cmJRG0WPRBNcsQQIedv9+0LBm/MezEo7bJQB2VeQYSFpMA?= =?iso-8859-1?q?3PrDZQF/WbFoGOdDET+/CX2RZS0LIlhfzFK9N/Ur9nIRGYrttrmm4wQB7sz5?= =?iso-8859-1?q?oPiUlpCPyzZ3X85b54dP6oc12NFz163n73+8k+3xWdIsW9STIuvQjzM2gBeM?= =?iso-8859-1?q?KhrRLVQ5Sj9r67LKyadLhjus8VAtkyedhOaVdDPjFOz+Wave4haunq+VlaF5?= =?iso-8859-1?q?o0QgMMdgfDNw3inamGCVh5g2V3OwoZcaYxoi/yx4Lwf8ualU/iKD2RIWgD08?= =?iso-8859-1?q?ArBI4JPknZ7Puu17drbl4m7YpZ7fblIlt0ooKrLpr1qf0BxsBFbF4SqFmOp8?= =?iso-8859-1?q?HmFD43TwhtOrHCI5xZ1PNgRb3BL4Wg82UnFBtLM7t2hUN/Vs7glT5Z52Hd7F?= =?iso-8859-1?q?9QooopvT6d1zEgC2S+iqsr8QKTXh/z+ZXTXEiBoCkPTqA5MVlrc5gR7LUPSX?= =?iso-8859-1?q?80yreChMAwW0pXey0zoKf0IWTEpHQsc46DLCPcJ/jvx98P/tAG2+/eFSU8nk?= =?iso-8859-1?q?EWRD26mnKFaNg6EnI1PoaN1Nz4TeTZagYDa80r54rbcXXwr7ZyxUtBkK5DUl?= =?iso-8859-1?q?07QSk8Rpq4NHiK+eIhDEyGXvuhMTX5ASP8Z5ZUw3yaZ0OPITr89oxsdFIkha?= =?iso-8859-1?q?bSVy9IxX2rZ+EOvwW73t4Ir7UYb/yzC2I0JbHOsXQd0kRObCm2v9CwAZXesA?= =?iso-8859-1?q?gYmyWP1V4CmG+3iiGqr/XpMpUIbP10oMRcMbKUjPkwk9mA/bTQoCm/0YzjSr?= =?iso-8859-1?q?o7oQTl5NoZCuhzjEwbKSlahecOR2Sc3thAI0SVQp5TOYFOX9YlG5pS7JZJJA?= =?iso-8859-1?q?nWNaJj/BNbnaN6A0GNO2LoE56gAbZxsTy0dKpqFzKvWjXYtjpCjRZ6X8HH/l?= =?iso-8859-1?q?q9d1XiBP4mdC8Vnbb/b20OFIN8Sp3WI+s/WBBb0aLy7G95Z6se8D4bs6nu7C?= =?iso-8859-1?q?NLDBeLDEylXrR796jIgzr1CG8lYdsDinfzGIPu/LSXAtiCxnVCLa+fKXbW7o?= =?iso-8859-1?q?Dulf2vdIgl7XPVP/BV+mkRPaG+9NFWT6PBMDcjyyWmfJXJuKqalhNI/LUZi8?= =?iso-8859-1?q?im3Nh+w4itaBhW7Ih8mu1gXYQ4dlhoQmVP6f1uTZ8mfEx9o5RrEJEykIS5ot?= =?iso-8859-1?q?e1OH5nSnm6bhgErPa8ziMa5/7SjTETyZ43L0NC11nE/4V0gIkEVqKnnaf/z/?= =?iso-8859-1?q?c4b5RESFkwE99BWTUQRDhfpipKumzZMTQRRZ3wUaDt84iQS1nAnqJi4Zm2p+?= =?iso-8859-1?q?RJ4TN9Y81UmYq25kFS21l5eZ6ZUSXD7eLu25jPkvq8JmlBx09o8HTiiwYkew?= =?iso-8859-1?q?xby5bqjFphhZQ90Ir16OMP39qqMM6OAO+SkdN8EVJWJum3tCTillW4usGy6a?= =?iso-8859-1?q?QydaVWs1Jw/bWD9ngHlyS7awcqRaxRxt3sjiQy3Ff0tC2P2NwgSCzuiXYmsk?= =?iso-8859-1?q?=3D?= 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: ae6fac31-a220-4d1c-2d76-08dd3979ba08 X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Jan 2025 17:41:53.1283 (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: 6Swpn8dlzyWwHC1V0Wa7Ikcl2Xw4NOPWk8V0tCP7Atb+TI1WBjTFhgSBgKlnjyxA/mUBizDSkDdQg7pzpkCjQJGLIz8pK9bIhr6s+/kQGJu0N5Iq9IRVJTNV2XGiiS1y X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR07MB7548 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, RCVD_IN_MSPIKE_H2=-0.036, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, 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: Clement Mathieu--Drif Signed-off-by: Clement 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 d5248ae51d..810197d5e1 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -48,6 +48,7 @@ tests = { 'test-qapi-util': [], 'test-interval-tree': [], 'test-fifo': [], + '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..0d1c1b7ca7 --- /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) { + g_assert(atc->levels == levels); + g_assert(atc->level_offset == level_offset); + + 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(); +}