From patchwork Fri Feb 21 08:07:40 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: 13984960 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 8720AC021AA for ; Fri, 21 Feb 2025 08:09:17 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tlO4e-0004bg-4u; Fri, 21 Feb 2025 03:07:52 -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 1tlO4a-0004a7-JX for qemu-devel@nongnu.org; Fri, 21 Feb 2025 03:07:48 -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 1tlO4X-0005ko-O1 for qemu-devel@nongnu.org; Fri, 21 Feb 2025 03:07:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eviden.com; i=@eviden.com; q=dns/txt; s=mail; t=1740125266; x=1771661266; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=oA3KYmbSWy/K+XMLDvDgQtpvoImsgYm3m1tPp4EH2n0=; b=QZHvsRpUfW1ZNbCSIRY5tL8xWQu7tOrQQHNv6DIg8VYofmjlo17kQ88V YeNGSoNfLPhPZ7OIpWASPr9Ia+VpNBpylPZtvCeDEuoV4IIgR/n+4kHQj 86pP6hVZhLyZ08d+PkjQne0DsV6CEh+y1a2e09JUx7DGsyq4jr/r1Ww0Z g49BFpCVoHZ8VuTH+TKy7UeUq8F4PctgXvvt4q17arMnrGQJb4mo+KVUS FHBUQENN/qioRv4kGfTLg941LO21NRJsRQY9jAnNlH/AMIGPRtcNnmKTK KsyAOUMbKd0ELkKmAJv3oVi48hb47ijyK3sWv1C8jeKpdEUG95WtT995V Q==; X-CSE-ConnectionGUID: 55hloPUSTQClcBjDWfJ82g== X-CSE-MsgGUID: ZIT6NtYhScadPcP9j11jzA== X-IronPort-AV: E=Sophos;i="6.13,304,1732575600"; d="scan'208";a="31394139" X-MGA-submission: MDFW4O0aDTiL9gxNcaIHmRyeclc9yMOUbYDmiisDvvNH39AiPYSgU7yF9euH5lAJAa1FAAHKplK2coB626ygvSkQyArnyRuhSMb128ym2n3rOkrXLOKE70Wz2iIYrqZn0QfY/wCyh46P11r97Borc9Hd3UWKwjzDMVF1DZ2JSOn38Q== Received: from mail-vi1eur05lp2170.outbound.protection.outlook.com (HELO EUR05-VI1-obe.outbound.protection.outlook.com) ([104.47.17.170]) by smarthost1.eviden.com with ESMTP/TLS/TLS_AES_256_GCM_SHA384; 21 Feb 2025 09:07:44 +0100 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=SJhbVb8//KwnVB6Ktp6IAthOetYTNW1aYIZWnaWogqVZ6g9cyx9QW0gQF0LvO6LCtSpcSG4qOtbku6lbqKsX4hieZNL6tD1Phd1N9KGn8tTcgYjQH++qbbKtWujC/Y48P3VoHp7s4M7QKRhZHGzZ9Hs9mLCBlrs/BrMdAIpIIUv1ILwnOQ6zZRkGfUqTA/zSaAP69imLuqZRPGCi+vA8nlRFfPtE9YJWySKimMhwoklbH42Ux6i8fwDzvU3PBUyc8KmJC3L2vso1Tx8r5hHMGxCMF+W+gaESmH5FPNcyHnRTn0Ky27npyDPynaodiuR4S1ulroxTfRdR1s37S1Jq9A== 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=/agh/ytGu+2jrSulAJnbz0Adp579Lwlc/FfbJbH4Qww=; b=fGCUwNoyJthXpD4y1MafWKcz1vCA/9Ru/tjsk8Uz2X6ltGLHEuAUAOv7e4Fa8qS0K6lbWA55YfXRFLfqszpVurnFBG2mIN5XZ/egmyu6JhqMQXcoOFP0RLdDq4+MEyKTtS92B1xc5/It3PiTkJeYevym6YZ6NMfXSH54kRQ9BLRdu68M/CNqkHk2oxGlmphzlLVoEvrbeFHEnY3Rn8NzgjyTSbATaouQaZZcnnLCQWPK1OEn8BO52Pebw7F7j4U9RBWItUx+YF83p+FmAeOFtUvY/0hR4ZDlzZGyz12huDHCsYC8fsbK8tz0lt44H/3V/wGVhs9N8J8oRpm6gwPpbA== 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=/agh/ytGu+2jrSulAJnbz0Adp579Lwlc/FfbJbH4Qww=; b=mwPLx1R1q7etjoYeUgckrqSsI9Ow+9qYVkhl7aX39zSB+n/vz12Nye07mnwyo6n4QS+3N4bPGW0dhiBNCptYKCcfdc2jpVKlT/keBoEIViVH+TzvbRPagfZdNH85dN4/m+sS+Qc2Mait8asEHvHKkfESeCfcNXUTewOygfmgyxc1NltvcSy7dTPcP1gymkG0MRKX00JqkGg8YApCIUQK7/mUK8g4OcpZUpo6v+mDRzdDEJOvsn3DBxjLTdkXZrOtEv5x0sGqvm9p+rW5LGFwfzkey6bZx4t+wPs8beT6nC3COeryLqk8JG57H7oTPfjDRo10C3CTp09xZFy7j9rprA== Received: from AM8PR07MB7602.eurprd07.prod.outlook.com (2603:10a6:20b:24b::7) by DB8PR07MB6459.eurprd07.prod.outlook.com (2603:10a6:10:142::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.16; Fri, 21 Feb 2025 08:07:41 +0000 Received: from AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d]) by AM8PR07MB7602.eurprd07.prod.outlook.com ([fe80::fbd7:ca71:b636:6f9d%7]) with mapi id 15.20.8445.017; Fri, 21 Feb 2025 08:07:41 +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 v3 14/19] atc: Add unit tests Thread-Topic: [PATCH v3 14/19] atc: Add unit tests Thread-Index: AQHbhDet8wAXEtvxo0evRDLt6giiLg== Date: Fri, 21 Feb 2025 08:07:40 +0000 Message-ID: <20250221080331.186285-15-clement.mathieu--drif@eviden.com> References: <20250221080331.186285-1-clement.mathieu--drif@eviden.com> In-Reply-To: <20250221080331.186285-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_|DB8PR07MB6459:EE_ x-ms-office365-filtering-correlation-id: 59c5efbe-d102-40d6-1233-08dd524ed08b x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230040|1800799024|366016|376014|7416014|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?gfq5nWvLWhLOdEt9IIqpsUS7g/?= =?iso-8859-1?q?1MCybXdTDnZo9Rys05A571xRkWkCtaXqRfOO/fRX5er65P0qBFxxyg/yiJUk?= =?iso-8859-1?q?+qQmjzqLIkLtO/HXDFoB+7E1kGQVoKh2UO1bCEjb6sBuN+n37OBWm5tlSt+g?= =?iso-8859-1?q?+vWFeWRcZI1KeT3Rlk0qcym+ZoxQJphZX9RLpAQoYNdmzyhNskmFf+Bsg4QP?= =?iso-8859-1?q?051XNLWIzSyBdej/gFdGt2jMOvWi6vLHPd/uNFoBVR0Ms9pWS5wan3Anes6e?= =?iso-8859-1?q?aCFAcZwsjDzg7eLhV2+GFi8sS9Yd/L08g/69bX4uaWQVkPeAf1ee7QKPHCnn?= =?iso-8859-1?q?39Ep8OgpQ32W+MbzoBqL/rbH3G4GlVNmGv+f8qTEOFOhLesdTWPyTVrBQysN?= =?iso-8859-1?q?5cIBHeWBUu5Xj4zAlkh6khhthJimnR7XiQEfPNwKhoalH30rzZ866KIqeu/4?= =?iso-8859-1?q?NtrOsy4c9jRDFJxUFO2hZ2YLafmemZqXX1MHQOcHtBfilas5Of6bT1EW6X9q?= =?iso-8859-1?q?xO8dl2V+AxNMeEfNjwdbMBHXHPthe3qQieKffhhs0PY5tncaeAF9hbykWTfz?= =?iso-8859-1?q?qtknitNJbT/aeZM5roil2T3g3uB9DUb/lPeuK3f9Dy8GCagUZOUQ1viOn8fI?= =?iso-8859-1?q?W5Ur0v4+BSMVlTMCgW7Dxr3d4nPJ/av0S0ZWbjJ7xRYNJsDU7F18E8ZxeOx/?= =?iso-8859-1?q?MqGLxj/pdcpN4ejonSWk67+ImNTHIPKNwsXrM3de+1x0TKb3D35BtD/NzvsP?= =?iso-8859-1?q?+dQNRgw6FW3RbDfG4GTaoIdQakL66UxavpSsqI4W3UtPVbieNhoJy+zV0YC8?= =?iso-8859-1?q?mtfMtDM27IuGfaM8iOG49qfzP0pNusqSg506lNII+fdg0gTahCgsBeP5UnZc?= =?iso-8859-1?q?6qIjrdIXc/EVH/xX5IPYpAN2RSAX+0aHY8wF2TWn29ALaTdqZrxWO0wE0avt?= =?iso-8859-1?q?HqFkMNxS8AyJHlBjHheDo8f2VO4yYcQGb24abZibgJ1kuAvG4mZhJDAMhnLi?= =?iso-8859-1?q?SdrK5UgiI6icnmBOAgUDHa2PGNshd7drIo2emxHxGnjer0MuNSLyD8VKnj4p?= =?iso-8859-1?q?gMHGLCt2WlYob0PqOIkHlRUxR8jtny/IqTa/n7FhGkuvOxeuBhe6EaMIPVis?= =?iso-8859-1?q?b60Ndxq7E4XXA90in85yOloMVxIAvDkcZxfjg+QvcVSnw7mT6WBTzDT2AnhA?= =?iso-8859-1?q?ANG9VSm1W7pNs32QRx1EOu+mXnxvghcAbPLR/q6CeQ5TmSYgUpiV8xG+ERgr?= =?iso-8859-1?q?zjQGZkxcP7LGpNoq9ztvBEM0iW06j670wV1Gxrnz3JJovOXlzN9Lc7RhmD2Q?= =?iso-8859-1?q?wL/du7nwsnhx6q70diYKQSIzEoX3Lx4NFKhgEGUpQQGD7XtA8rnxsk15oL6j?= =?iso-8859-1?q?57pGrnAufZR0pHuVp8kO+4tQ0XNrNShEIFeJGxmWVkFy2OSTK+7rXzCi3O5t?= =?iso-8859-1?q?owq8Q/xuclkyK80MzLD/to+g=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)(376014)(7416014)(38070700018); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?6jy66aruHy4BjGl/5UzIX/h?= =?iso-8859-1?q?st9ZdNkTx+8bAcuzEDoVOJ8MAKPW1R6EJoj6mOHUa8e/zTPpER6awgHaS8oK?= =?iso-8859-1?q?xjZBuV4NNozx/HvkIZxJHSuo2zcjuvMfmIfJigZ1aTqajUabYHaouKq2qsSN?= =?iso-8859-1?q?UEd3sQuWrbsAQaHhxcOc5rnNB+G4FVLNaZX4Kfvc2Ei9lm/saFbiv8h9JC8i?= =?iso-8859-1?q?p7hMWfA704mpGPcvJTNVWWguX+E+P8YLYJ7nRjdgZ1ySErs0k+wm6RmCztiR?= =?iso-8859-1?q?sWbVC7TE/Qf9/1JQHTG8x4d/D2MkX+Q1lcmCliKXLTAZq2QSZxewXJ+rhe0d?= =?iso-8859-1?q?UhNBXgf/mg7KSIlolNRVRFmSf0mDctGCUyjH85vSxnScD/j5CU4Sq+pDYkl/?= =?iso-8859-1?q?2Uf37Gt0Yw5+ZNL0PKyYA194wyDJENQ0pQtMQQKFytpZ4GDgkCVzj9bIU7Cp?= =?iso-8859-1?q?ExTIcGwZq5UtaScFFGh6MVF/NFehXQtafz88kQ8czPtwMLyIk4xktuShGCfX?= =?iso-8859-1?q?U6QD0wV3Koib4Y/DFAq/sNSq51kKa6v/15hdKaOg2/RT5ryqcw7wkB3Ob7M5?= =?iso-8859-1?q?ItaT2T50wS5OyJiKjR7cai3/xODERpP+XyYPYRKTQdUl5OKJ3IGyo1KtJOlN?= =?iso-8859-1?q?6byUu6+8VHhSBmVWRCUjZfiHV2NqjVG0G3t6cmUnxNcVs5GEcS7Nfj9Je7zw?= =?iso-8859-1?q?uoEryLT1mf6s+uUgGXWKiQ2wFEgHRJpyyOllNs43an27uzcXkcj34jSGgRrl?= =?iso-8859-1?q?zO1m3Tge6lzUp8T/zqkPXYGRy5Uh9zD4AYJNJnrOga7+G+wHqtTLpYqDtiSX?= =?iso-8859-1?q?ftO2gFjYx/wfyNAAQbipbWdfpetNSt0nhIdK931lRIEG3Uf/eOYLghX1jhy1?= =?iso-8859-1?q?cdE68Enqw0Jsa2t09ogdqn112QID3eWGoVJs2qH8cuYRTLm0Mt0R3xt2N2UB?= =?iso-8859-1?q?7WRdy6Lmy/yQ5gVel9OAGQZWD+cIWRgCzgnQflBOMxoEVeimGkGpfFP54NbJ?= =?iso-8859-1?q?VxsmMSX6A33rJD7JfpLn9HK03KQnB2vl5K1ymVUaiUxi9SKuhQVcXs3E9uEZ?= =?iso-8859-1?q?S6DZur9RopnwlzlFbv0vWdTGOyCeQyNlIkVmmymx9ugiIXAT175YGdv/KQzA?= =?iso-8859-1?q?S5jibriDs+HcL8LavaFgYaQDSwu4R9qLJag1W0IT0aBECITy2wsOFRFAzH4y?= =?iso-8859-1?q?067uNnWWUbqKYrjd5oGf2sNBrA+6vUJu2+2gOewofOr6IHiBfJH8yTJxe+0J?= =?iso-8859-1?q?Y61xRRk6Vdm9TLrsv6XJsX6yBpiN+FZzPPlxJIaRLSreluBwV2UrV/b/L9Cu?= =?iso-8859-1?q?u2XAqFV+2F1irJBdKKR10jZdeCXbPfj62azqw8Dce+DgIBvXajr6iUlNnRXh?= =?iso-8859-1?q?0yLVomqDZ627sMX/GHCrHtrDUJNQfn8rb6hpE+0uoxZ82N36jg8fbX4LYYCL?= =?iso-8859-1?q?HQBkoMtUyQt3y+H8nrrjQVoQTMbDaOcEO430YFuTYgZ/DV2ys34QDg1naoNh?= =?iso-8859-1?q?7XWUsa6cGE0k7FrOUdtIlWoOt9itfbiJAjgUqpb1d0WHxuHrcmryyEwHZ81t?= =?iso-8859-1?q?dliQFSDzaMRi4dkm12Cq271WAYiGLRz4qaS0sQoXDayswc5WcjG+hqTQOEEU?= =?iso-8859-1?q?DTliSt+yH/L9PXJ2WT9u4bVWaCJmaMZJoTtnLSSd90DfOYP1rFScozyOheLY?= =?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: 59c5efbe-d102-40d6-1233-08dd524ed08b X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Feb 2025 08:07:40.7266 (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: 0gYI4TXqDTIR/BeKHssnE5Fm58s6BVJqxg0W5gkJ9ysO5UxkCRocXA7n6ANp23XHogY7WHS36fhPl6CamkOYFpojWPMLf5+XtpZUmJZQHuWl7+0uI63/tJeZbpPb8I3h X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR07MB6459 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_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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(); +}