From patchwork Mon Aug 24 13:52:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1350386 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=LUMcU4kB; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=S+MS38dr; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-sharedspace-onmicrosoft-com header.b=lCQ2D6lP; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BZtr263Rqz9sTF for ; Mon, 24 Aug 2020 23:53:22 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=CL2VgiwDnGalhHusQtbsG1b3Rv+M5zZSPvqRYu1xSxI=; b=LUMcU4kBQONJq7Wa4iAxijPBV CauFWN7kqv5CZORmEkLn25yKe2tG17MprYXj0b14Y1ToPNbx6yjdVpk51vLyIeK3zi73xJOlporSI 1xCFTjbJ5KRAyBTLc48hCK2F00xQOKFIsGZ686kj0B3OFkwxiGKsK/Q/WptYx12AuQOyLRavFhRoB BWssEpAuBZ0/cQR5o7btYPLhTEUgz+IHVm53liXM6+bRuOw6hjgiuejXKzBwYVqO4ujrX9thn4wv5 +H3Gw2XKrGR6uaL7K/PFBOJm9Kvx9WzY58DISb4PAewoDIuy24B01eGbZnCoye+FMmY8K3w2TqvGX ACDli38eg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kACuS-0006Fd-0h; Mon, 24 Aug 2020 13:53:16 +0000 Received: from esa5.hgst.iphmx.com ([216.71.153.144]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kACuP-0006Dz-3M for opensbi@lists.infradead.org; Mon, 24 Aug 2020 13:53:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1598277193; x=1629813193; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=R5s4jJcf8AO/sRDBG1y4S3DFmpspWl0x1EXE6nfySKo=; b=S+MS38drV3i0ttA7pkcg40uiziHbhGIsogk1jg/LbOnPP0rFwqRPlWYz 2ZRw/MNArP6LpW24XPrIaYbkSvqp1IiD/+wCPXvZ5BNQ1S1TxAV/pkiQo arQTVuawu3RJuvSOQI52DqCHj32e8/z3dtXsZh2YIeLDqgjNK8wI0Vroa ++tjJv8tl3N99/AZ2x9km9W8T1jqsR+sfq+JNUGFFlHL108OO4/sP2MR7 LVaH4PsSahxb8Iuk1zOfjxfsBShw9cQ4y+5vqIGZyRq1HPFLzDv3ZKbtZ KDC8mQTGZmxGeNDYrcvHXj33ikMXxH8yOwUE//w3mcCLaofeT85Ndl/0S g==; IronPort-SDR: KvcS5bo5WUFIU+8JN+X5eFipEcVTd1Us7I4lP7E9HWZW+uLTHxlsXq6Pno/M7sHmcPGJq44yQp In4kNLGK+iUpdb3ivQYHRNeGi0+l8Ez4MxmhGAVTkIWdq1SO8wkxdc5i3wxYXZ0ueR+cbe9bS8 vPdHIVvc8vBho5JlcwA1+RWmQ3ctZIzlQxuy5iEFtPOnQG/CVS44xYZ1fwXD5jzpA2MbMYv5W5 lwj57p8ADL1yyBb2GltwI+UJ5MXrwe8TazXqa2q5kkiEY5SJ6C+fPPM4/A40lC4fsouqEHNG9X fQs= X-IronPort-AV: E=Sophos;i="5.76,348,1592841600"; d="scan'208";a="145693962" Received: from mail-co1nam11lp2173.outbound.protection.outlook.com (HELO NAM11-CO1-obe.outbound.protection.outlook.com) ([104.47.56.173]) by ob1.hgst.iphmx.com with ESMTP; 24 Aug 2020 21:53:12 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Swje2Zxyl8j56LxUFKlyuNb8y90fKz14/RFYgV/ZBtJiWeC3VuJgnA+neGeuEvNYhYO5OURHGB1806HJGq8fHCZsUyz7M5cXYDFDdD2HL1jRRtthkJJlnMZUp6BLfjjildbsDPu6S5u3WSciscxRECDMXaodYyXobf29D7dUMQKMwQFF6E0849x/6+ZOg1kWgxzOQ+Ad3llDF6iT5x/RpE1/nUF3pWWDeqZqkQI6baIMFbi/kd2DJsZf5JmqiH2BRmyuYq4ZnlrWuTSFNyc+oxyarhw1H85pIs3vK2ADMoBgpTyhC3OBUfLwYajh6rqg8CX+GxCsuag9Ir0jahkq1Q== 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-SenderADCheck; bh=8z/wvGrDNQecL9nxaQ/CYA5+/mAV9xpkh/QKhW0ukOE=; b=nGTfyFKLkcT/odKIuwPrT3qnAuffH1DWlD3AZmkHA6IkM0V52CUTXAFVBXiY4AXLimMb3PaNL8dqgKyVQA2cQzx9LEjITRvDj1oZA/j+G/ZkZUw2AhMU7eZEi6AbbFIkp8889YBVU0rKY/r3YHcswU+RKEg7MJmVAfSUdy0jVmBw/L1symxmFmJTfrvbngZyBLWdPZ8lSsVK8I5KpVhBR599oSBNU2pFkR+iUiwzm9wGnk2x9S9q3rnbfF50kkbca4ekGkTbm9obYEPnyKdthtHPAkps1brV87K+8kinLf1icqLd8K/kILEBtY12d2faoRYh/2+FlLKWBm7JGtT+aQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8z/wvGrDNQecL9nxaQ/CYA5+/mAV9xpkh/QKhW0ukOE=; b=lCQ2D6lPpV/Stj3QbuhuuqqwUF3/qCKliB7CDsyUdN7oEgncpeTUABCI9Ce3LT3jqfMFtOw7Wm8HylGxeCc09uduHqHxpEVjllIFwpqFOSTX8aSGCbip56WMoSXhnTo+kcW765rZCaTtnteXP0J2+wazO5Od9jN4xwbjVJoNBYs= Authentication-Results: wdc.com; dkim=none (message not signed) header.d=none;wdc.com; dmarc=none action=none header.from=wdc.com; Received: from DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) by DM6PR04MB5962.namprd04.prod.outlook.com (2603:10b6:5:130::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24; Mon, 24 Aug 2020 13:53:11 +0000 Received: from DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::607a:44ed:1477:83e]) by DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::607a:44ed:1477:83e%7]) with mapi id 15.20.3305.026; Mon, 24 Aug 2020 13:53:11 +0000 From: Anup Patel To: Atish Patra , Alistair Francis Subject: [PATCH v2 2/5] lib: sbi: Improve PMP CSR detection and progamming Date: Mon, 24 Aug 2020 19:22:40 +0530 Message-Id: <20200824135243.136769-3-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200824135243.136769-1-anup.patel@wdc.com> References: <20200824135243.136769-1-anup.patel@wdc.com> X-ClientProxiedBy: BM1PR0101CA0010.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:18::20) To DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from 255.255.255.255 (255.255.255.255) by BM1PR0101CA0010.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:18::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Mon, 24 Aug 2020 13:53:09 +0000 X-Mailer: git-send-email 2.25.1 X-Originating-IP: [103.20.29.219] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f957411b-c708-40ba-5535-08d8483509f2 X-MS-TrafficTypeDiagnostic: DM6PR04MB5962: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:2089; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TmCVuTg0+3qoZHbsrNlBkFOTtG2JWZmcBALrOjlayC18BjOpnym/zFTLttJm4NWmQ+E6NJHHtqCx0YSHJ60dNGZrIfnK+FzLZ5wqTut2D9Pc9u5ARUIf/pZ2l3rs5RMxms+L8JTbnVnXD+QtWcn4gAlCv4Ev+wAZN/yKFmab6cKWI2EoT5DwN/6+5tyfEDbPsNnaOxSwGrtD4d3TCULIWCwOBz92S+9hpnWTNRxmcNVcHVhXS4gzxs0yuii8UxxKIzJaOkYb1cSCwt+p2QDx+Y4D0nI0Ol/mVtYFyTIyip94u/oMnzjKucxuy4RmhFQKfO5L39uh4n1TFgCygtjc6Q== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM6PR04MB6201.namprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(136003)(366004)(346002)(396003)(39860400002)(376002)(1076003)(8936002)(2906002)(86362001)(2616005)(66476007)(66556008)(478600001)(66946007)(186003)(956004)(54906003)(110136005)(6636002)(30864003)(26005)(316002)(36756003)(16576012)(5660300002)(83380400001)(4326008)(44832011)(6486002)(8676002)(6666004)(52116002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: stx8XNvtqhwDC06tW7K7MjXjLiUYZaPoZGp3a62uXNozaImEy0hEya6xsA0QI3gpaEErgxAliPkKhWqcX0j+xOKMyc3CjOSTIC6CtmZB3qK3VEUkKYb6x9Pfrsg+Tt/Wta9OpNaCrQEXT6CcWqfJzQ/A1ciopgq74HRlcsvpciNIkuKxKD4malI8DVr1TOSLBWrnCHiAxK5BIHbXnGxf75tAoodR4dDSrDHbM8HAyeNWOecL8TJZIHFKXwvnVlvCtGPPsP/BSOBTicR+kCCcWe7NQyD8poSd8nca4Ut4KGXx2A1ESjSGw1SrrLSbTUJ1ID/WqbMy4V30NRy1wGcT3hP4SJ8BiJAeVhAx6hPOhde9U7IRpjryMDqKeMOXmMdbmgBkvRLfjjEK5/IACOF8VJQfowy9wBsenCKLth5ffa8byUun5M6Viq/23GTfhft/XOuzswCcjDv8MI2TU1fI8f+myj8iEwf7M/4cFhJfNA7/lSgwNMwJz9UCE6voJ/bdOp+ujj4K338fcsJ/Z9oCVSZu7tp0WnEbaw3wqTXqnB1qDP83QtgKuT7pSUoZPZz5eRe/0NQOIJ4WcVy4mgB078x2XfML5e65VJodYAPPqg8KDKwwf7UL5jf50vhoV9IbYfQr+b+zwQ0r9J7i39Pg1w== X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: f957411b-c708-40ba-5535-08d8483509f2 X-MS-Exchange-CrossTenant-AuthSource: DM6PR04MB6201.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2020 13:53:11.4565 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9vAgO6BmT0feKhg/PA1o/l9YvXL6i+YG4dO2Xb3zJkwOasyL8KIH71cwxA4A0TrvmxejIFHc0vh76fn8l8iE3A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR04MB5962 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200824_095313_312528_86455FFA X-CRM114-Status: GOOD ( 14.86 ) X-Spam-Score: -1.2 (-) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-1.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [216.71.153.144 listed in list.dnswl.org] 1.3 RCVD_ILLEGAL_IP Received: contains illegal IP address -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.0 MSGID_FROM_MTA_HEADER Message-Id was added by a relay X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Anup Patel , Anup Patel , opensbi@lists.infradead.org Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The RISC-V spec allows 4, 16 or 64 PMP regions and some of the PMP regions may be hardwired to zero. This patch improves PMP CSR detection and progamming considering above facts. Signed-off-by: Anup Patel --- include/sbi/riscv_encoding.h | 67 ++++++++++++- lib/sbi/riscv_asm.c | 186 +++++++++++++---------------------- lib/sbi/sbi_hart.c | 72 +++++++++----- 3 files changed, 180 insertions(+), 145 deletions(-) diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h index 827c86c..073261f 100644 --- a/include/sbi/riscv_encoding.h +++ b/include/sbi/riscv_encoding.h @@ -144,7 +144,12 @@ #define PMP_L _UL(0x80) #define PMP_SHIFT 2 -#define PMP_COUNT 16 +#define PMP_COUNT 64 +#if __riscv_xlen == 64 +#define PMP_ADDR_MASK ((_ULL(0x1) << 54) - 1) +#else +#define PMP_ADDR_MASK _UL(0xFFFFFFFF) +#endif #if __riscv_xlen == 64 #define MSTATUS_SD MSTATUS64_SD @@ -263,6 +268,18 @@ #define CSR_PMPCFG1 0x3a1 #define CSR_PMPCFG2 0x3a2 #define CSR_PMPCFG3 0x3a3 +#define CSR_PMPCFG4 0x3a4 +#define CSR_PMPCFG5 0x3a5 +#define CSR_PMPCFG6 0x3a6 +#define CSR_PMPCFG7 0x3a7 +#define CSR_PMPCFG8 0x3a8 +#define CSR_PMPCFG9 0x3a9 +#define CSR_PMPCFG10 0x3aa +#define CSR_PMPCFG11 0x3ab +#define CSR_PMPCFG12 0x3ac +#define CSR_PMPCFG13 0x3ad +#define CSR_PMPCFG14 0x3ae +#define CSR_PMPCFG15 0x3af #define CSR_PMPADDR0 0x3b0 #define CSR_PMPADDR1 0x3b1 #define CSR_PMPADDR2 0x3b2 @@ -279,6 +296,54 @@ #define CSR_PMPADDR13 0x3bd #define CSR_PMPADDR14 0x3be #define CSR_PMPADDR15 0x3bf +#define CSR_PMPADDR16 0x3c0 +#define CSR_PMPADDR17 0x3c1 +#define CSR_PMPADDR18 0x3c2 +#define CSR_PMPADDR19 0x3c3 +#define CSR_PMPADDR20 0x3c4 +#define CSR_PMPADDR21 0x3c5 +#define CSR_PMPADDR22 0x3c6 +#define CSR_PMPADDR23 0x3c7 +#define CSR_PMPADDR24 0x3c8 +#define CSR_PMPADDR25 0x3c9 +#define CSR_PMPADDR26 0x3ca +#define CSR_PMPADDR27 0x3cb +#define CSR_PMPADDR28 0x3cc +#define CSR_PMPADDR29 0x3cd +#define CSR_PMPADDR30 0x3ce +#define CSR_PMPADDR31 0x3cf +#define CSR_PMPADDR32 0x3d0 +#define CSR_PMPADDR33 0x3d1 +#define CSR_PMPADDR34 0x3d2 +#define CSR_PMPADDR35 0x3d3 +#define CSR_PMPADDR36 0x3d4 +#define CSR_PMPADDR37 0x3d5 +#define CSR_PMPADDR38 0x3d6 +#define CSR_PMPADDR39 0x3d7 +#define CSR_PMPADDR40 0x3d8 +#define CSR_PMPADDR41 0x3d9 +#define CSR_PMPADDR42 0x3da +#define CSR_PMPADDR43 0x3db +#define CSR_PMPADDR44 0x3dc +#define CSR_PMPADDR45 0x3dd +#define CSR_PMPADDR46 0x3de +#define CSR_PMPADDR47 0x3df +#define CSR_PMPADDR48 0x3e0 +#define CSR_PMPADDR49 0x3e1 +#define CSR_PMPADDR50 0x3e2 +#define CSR_PMPADDR51 0x3e3 +#define CSR_PMPADDR52 0x3e4 +#define CSR_PMPADDR53 0x3e5 +#define CSR_PMPADDR54 0x3e6 +#define CSR_PMPADDR55 0x3e7 +#define CSR_PMPADDR56 0x3e8 +#define CSR_PMPADDR57 0x3e9 +#define CSR_PMPADDR58 0x3ea +#define CSR_PMPADDR59 0x3eb +#define CSR_PMPADDR60 0x3ec +#define CSR_PMPADDR61 0x3ed +#define CSR_PMPADDR62 0x3ee +#define CSR_PMPADDR63 0x3ef #define CSR_TSELECT 0x7a0 #define CSR_TDATA1 0x7a1 #define CSR_TDATA2 0x7a2 diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c index 6dfebd9..799123f 100644 --- a/lib/sbi/riscv_asm.c +++ b/lib/sbi/riscv_asm.c @@ -90,142 +90,88 @@ void misa_string(int xlen, char *out, unsigned int out_sz) unsigned long csr_read_num(int csr_num) { +#define switchcase_csr_read(__csr_num, __val) \ + case __csr_num: \ + __val = csr_read(__csr_num); \ + break; +#define switchcase_csr_read_2(__csr_num, __val) \ + switchcase_csr_read(__csr_num + 0, __val) \ + switchcase_csr_read(__csr_num + 1, __val) +#define switchcase_csr_read_4(__csr_num, __val) \ + switchcase_csr_read_2(__csr_num + 0, __val) \ + switchcase_csr_read_2(__csr_num + 2, __val) +#define switchcase_csr_read_8(__csr_num, __val) \ + switchcase_csr_read_4(__csr_num + 0, __val) \ + switchcase_csr_read_4(__csr_num + 4, __val) +#define switchcase_csr_read_16(__csr_num, __val) \ + switchcase_csr_read_8(__csr_num + 0, __val) \ + switchcase_csr_read_8(__csr_num + 8, __val) +#define switchcase_csr_read_32(__csr_num, __val) \ + switchcase_csr_read_16(__csr_num + 0, __val) \ + switchcase_csr_read_16(__csr_num + 16, __val) +#define switchcase_csr_read_64(__csr_num, __val) \ + switchcase_csr_read_32(__csr_num + 0, __val) \ + switchcase_csr_read_32(__csr_num + 32, __val) + unsigned long ret = 0; switch (csr_num) { - case CSR_PMPCFG0: - ret = csr_read(CSR_PMPCFG0); - break; - case CSR_PMPCFG1: - ret = csr_read(CSR_PMPCFG1); - break; - case CSR_PMPCFG2: - ret = csr_read(CSR_PMPCFG2); - break; - case CSR_PMPCFG3: - ret = csr_read(CSR_PMPCFG3); - break; - case CSR_PMPADDR0: - ret = csr_read(CSR_PMPADDR0); - break; - case CSR_PMPADDR1: - ret = csr_read(CSR_PMPADDR1); - break; - case CSR_PMPADDR2: - ret = csr_read(CSR_PMPADDR2); - break; - case CSR_PMPADDR3: - ret = csr_read(CSR_PMPADDR3); - break; - case CSR_PMPADDR4: - ret = csr_read(CSR_PMPADDR4); - break; - case CSR_PMPADDR5: - ret = csr_read(CSR_PMPADDR5); - break; - case CSR_PMPADDR6: - ret = csr_read(CSR_PMPADDR6); - break; - case CSR_PMPADDR7: - ret = csr_read(CSR_PMPADDR7); - break; - case CSR_PMPADDR8: - ret = csr_read(CSR_PMPADDR8); - break; - case CSR_PMPADDR9: - ret = csr_read(CSR_PMPADDR9); - break; - case CSR_PMPADDR10: - ret = csr_read(CSR_PMPADDR10); - break; - case CSR_PMPADDR11: - ret = csr_read(CSR_PMPADDR11); - break; - case CSR_PMPADDR12: - ret = csr_read(CSR_PMPADDR12); - break; - case CSR_PMPADDR13: - ret = csr_read(CSR_PMPADDR13); - break; - case CSR_PMPADDR14: - ret = csr_read(CSR_PMPADDR14); - break; - case CSR_PMPADDR15: - ret = csr_read(CSR_PMPADDR15); - break; + switchcase_csr_read_16(CSR_PMPCFG0, ret) + switchcase_csr_read_64(CSR_PMPADDR0, ret) default: break; }; return ret; + +#undef switchcase_csr_read_64 +#undef switchcase_csr_read_32 +#undef switchcase_csr_read_16 +#undef switchcase_csr_read_8 +#undef switchcase_csr_read_4 +#undef switchcase_csr_read_2 +#undef switchcase_csr_read } void csr_write_num(int csr_num, unsigned long val) { +#define switchcase_csr_write(__csr_num, __val) \ + case __csr_num: \ + csr_write(__csr_num, __val); \ + break; +#define switchcase_csr_write_2(__csr_num, __val) \ + switchcase_csr_write(__csr_num + 0, __val) \ + switchcase_csr_write(__csr_num + 1, __val) +#define switchcase_csr_write_4(__csr_num, __val) \ + switchcase_csr_write_2(__csr_num + 0, __val) \ + switchcase_csr_write_2(__csr_num + 2, __val) +#define switchcase_csr_write_8(__csr_num, __val) \ + switchcase_csr_write_4(__csr_num + 0, __val) \ + switchcase_csr_write_4(__csr_num + 4, __val) +#define switchcase_csr_write_16(__csr_num, __val) \ + switchcase_csr_write_8(__csr_num + 0, __val) \ + switchcase_csr_write_8(__csr_num + 8, __val) +#define switchcase_csr_write_32(__csr_num, __val) \ + switchcase_csr_write_16(__csr_num + 0, __val) \ + switchcase_csr_write_16(__csr_num + 16, __val) +#define switchcase_csr_write_64(__csr_num, __val) \ + switchcase_csr_write_32(__csr_num + 0, __val) \ + switchcase_csr_write_32(__csr_num + 32, __val) + switch (csr_num) { - case CSR_PMPCFG0: - csr_write(CSR_PMPCFG0, val); - break; - case CSR_PMPCFG1: - csr_write(CSR_PMPCFG1, val); - break; - case CSR_PMPCFG2: - csr_write(CSR_PMPCFG2, val); - break; - case CSR_PMPCFG3: - csr_write(CSR_PMPCFG3, val); - break; - case CSR_PMPADDR0: - csr_write(CSR_PMPADDR0, val); - break; - case CSR_PMPADDR1: - csr_write(CSR_PMPADDR1, val); - break; - case CSR_PMPADDR2: - csr_write(CSR_PMPADDR2, val); - break; - case CSR_PMPADDR3: - csr_write(CSR_PMPADDR3, val); - break; - case CSR_PMPADDR4: - csr_write(CSR_PMPADDR4, val); - break; - case CSR_PMPADDR5: - csr_write(CSR_PMPADDR5, val); - break; - case CSR_PMPADDR6: - csr_write(CSR_PMPADDR6, val); - break; - case CSR_PMPADDR7: - csr_write(CSR_PMPADDR7, val); - break; - case CSR_PMPADDR8: - csr_write(CSR_PMPADDR8, val); - break; - case CSR_PMPADDR9: - csr_write(CSR_PMPADDR9, val); - break; - case CSR_PMPADDR10: - csr_write(CSR_PMPADDR10, val); - break; - case CSR_PMPADDR11: - csr_write(CSR_PMPADDR11, val); - break; - case CSR_PMPADDR12: - csr_write(CSR_PMPADDR12, val); - break; - case CSR_PMPADDR13: - csr_write(CSR_PMPADDR13, val); - break; - case CSR_PMPADDR14: - csr_write(CSR_PMPADDR14, val); - break; - case CSR_PMPADDR15: - csr_write(CSR_PMPADDR15, val); - break; + switchcase_csr_write_16(CSR_PMPCFG0, val) + switchcase_csr_write_64(CSR_PMPADDR0, val) default: break; }; + +#undef switchcase_csr_write_64 +#undef switchcase_csr_write_32 +#undef switchcase_csr_write_16 +#undef switchcase_csr_write_8 +#undef switchcase_csr_write_4 +#undef switchcase_csr_write_2 +#undef switchcase_csr_write } static unsigned long ctz(unsigned long x) diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 80ed86a..8f31d58 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -340,31 +340,55 @@ static void hart_detect_features(struct sbi_scratch *scratch) hfeatures->features = 0; hfeatures->pmp_count = 0; - /* Detect if hart supports PMP feature */ -#define __detect_pmp(__pmp_csr) \ - val = csr_read_allowed(__pmp_csr, (ulong)&trap); \ - if (!trap.cause) { \ - csr_write_allowed(__pmp_csr, (ulong)&trap, val);\ - if (!trap.cause) \ - hfeatures->pmp_count++; \ +#define __check_csr(__csr, __rdonly, __wrval, __field, __skip) \ + val = csr_read_allowed(__csr, (ulong)&trap); \ + if (!trap.cause) { \ + if (__rdonly) { \ + (hfeatures->__field)++; \ + } else { \ + csr_write_allowed(__csr, (ulong)&trap, __wrval);\ + if (!trap.cause) { \ + if (csr_swap(__csr, val) == __wrval) \ + (hfeatures->__field)++; \ + else \ + goto __skip; \ + } else { \ + goto __skip; \ + } \ + } \ + } else { \ + goto __skip; \ } - __detect_pmp(CSR_PMPADDR0); - __detect_pmp(CSR_PMPADDR1); - __detect_pmp(CSR_PMPADDR2); - __detect_pmp(CSR_PMPADDR3); - __detect_pmp(CSR_PMPADDR4); - __detect_pmp(CSR_PMPADDR5); - __detect_pmp(CSR_PMPADDR6); - __detect_pmp(CSR_PMPADDR7); - __detect_pmp(CSR_PMPADDR8); - __detect_pmp(CSR_PMPADDR9); - __detect_pmp(CSR_PMPADDR10); - __detect_pmp(CSR_PMPADDR11); - __detect_pmp(CSR_PMPADDR12); - __detect_pmp(CSR_PMPADDR13); - __detect_pmp(CSR_PMPADDR14); - __detect_pmp(CSR_PMPADDR15); -#undef __detect_pmp +#define __check_csr_2(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr(__csr + 1, __rdonly, __wrval, __field, __skip) +#define __check_csr_4(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr_2(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr_2(__csr + 2, __rdonly, __wrval, __field, __skip) +#define __check_csr_8(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr_4(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr_4(__csr + 4, __rdonly, __wrval, __field, __skip) +#define __check_csr_16(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr_8(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr_8(__csr + 8, __rdonly, __wrval, __field, __skip) +#define __check_csr_32(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr_16(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr_16(__csr + 16, __rdonly, __wrval, __field, __skip) +#define __check_csr_64(__csr, __rdonly, __wrval, __field, __skip) \ + __check_csr_32(__csr + 0, __rdonly, __wrval, __field, __skip) \ + __check_csr_32(__csr + 32, __rdonly, __wrval, __field, __skip) + + /* Detect number of PMP regions */ + __check_csr_64(CSR_PMPADDR0, 0, PMP_ADDR_MASK, pmp_count, __pmp_skip); +__pmp_skip: + +#undef __check_csr_64 +#undef __check_csr_32 +#undef __check_csr_16 +#undef __check_csr_8 +#undef __check_csr_4 +#undef __check_csr_2 +#undef __check_csr /* Detect if hart supports SCOUNTEREN feature */ trap.cause = 0;