From patchwork Sat Nov 2 00:07:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859847 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 33386E6F098 for ; Sat, 2 Nov 2024 00:09:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eVoFqj1aRkv/k273pjio0nd3KkyV6RQujKvygsjC9lU=; b=HgnCmqKB7A3evF HI+0qXaZKy8SXuUpwDg9rWiQ92Squ44BO3WnvR4d0uKqNnRsowNkxV3G0tprBLUdOgvN5BQ3qkqHC HkgEQDW1KteG7DQjStfYFw1eR1kgFgv83SQdb2aQEP52t1vNfDb+lO/rk9rqOqXF7uWGOxnH1uyXv r1PafZ89p7F4RuypjDhtJ/n+X0IP2RWnekYGoje3C9ZYeSTncxK2mTKRID8sbjOFigIPi8EfBbnfA c7zR11N0FPHDenUtU6OY/jJ1HpVcAYK/UtPRcsvlMW+qJtsjZVW3r4iJ5jzyHA2ipOCY6YhnqNsNE WK4jaOBm4BcjhVMhnQkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hD-00000008ZyA-1wbS; Sat, 02 Nov 2024 00:08:51 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hA-00000008ZwK-3qhw for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:50 +0000 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2e34a089cd3so1972123a91.3 for ; Fri, 01 Nov 2024 17:08:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506127; x=1731110927; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kOEMNZ+33viJM90wJhw0Ad72Zh5qPAKNdSNu0KRw6Xo=; b=Z2dY0tFf6e26h/uPMfQrCLXscPjfgRzFeO38sxrOzRhNXhBM3uQjye+CK6FG1peRFY mLIvlJdudU7Rc8KP6d5B+s4NhEkQHdravTQeOACazRdpiLqd3/30xwogBiGjLuTZ0r+1 9O5yQnaBgS9UwzJ/b243V9FgyIme00k/rGpxKmk4TtkpLwZMbUU361jrD3rLJ414ReBr AWqoEuAovOYzrQ1opSeGok7epT6DxS2B+NApC+43/xe8PYJRBf9caSS1TeRa2ig8Rg7H aqpiPt06muwucgxrf1Px9Uv05H22HxsWBcyHXmT6Lj7npWrWnSfWODEWWP3IpleR8RPD 6hOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506127; x=1731110927; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kOEMNZ+33viJM90wJhw0Ad72Zh5qPAKNdSNu0KRw6Xo=; b=sxlamh9G/MTyXWz+y1VfwWtoZ0BvcP3U1cwFhyCTtvRLaFdFn14QEEPDONLt7E6nWH UBteGtF1Xo/FVw1roY2n0zvndv4EtgRS2zEKOOVJK35l8RNtLHKM3mFrFiko9PawzkbD YumJIPvZfWrQlMpvw1pL8RRPtrHxmo01LJZxeeT14HWquucMoRx2h6UzlaQgDTFAWrNd iRrYgxvU+uBHD/RQm/Jyohl+JKZtpXhSo4OOjFlUvPzdJvdJzCieiRWoFtalkuDtAc1C mZFXsZ7toELw80yAucpLyn4SN1wCNuufjKgnmGgsHaV/V2DwIXmPH4PGV0I9RY51Yadm 6QwQ== X-Forwarded-Encrypted: i=1; AJvYcCW4YrDFhYW00ii4rpufMoA6WB4NOtJrR8xBndp+4phpLYgldN2pJkRyDsXAKGXbuuGGJp6C1CIlaDlAfg==@lists.infradead.org X-Gm-Message-State: AOJu0YxoiRB4+hdY4nHHHBfejyh8AsqthKD0s0DDrV98yMbXnhpIRAWo dcYeWeDjrOdQN/2PP5JdLxXp/XCjvMD9spzTekjpT4B56ZQA0hz3/vbnX4MjTEA= X-Google-Smtp-Source: AGHT+IE6EkC5NIMo5m4ZPw7HiXuYchFGSfyQgkFEdoKuXEzVgx9eOAQYjDGPGRWM5i/c7czbrOnwSQ== X-Received: by 2002:a17:90a:f406:b0:2e0:d693:7884 with SMTP id 98e67ed59e1d1-2e8f0f55a2bmr27105930a91.5.1730506127517; Fri, 01 Nov 2024 17:08:47 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:47 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 01/11] dt-bindings: riscv: Describe physical memory regions Date: Fri, 1 Nov 2024 17:07:55 -0700 Message-ID: <20241102000843.1301099-2-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170848_992099_2B1EBEEF X-CRM114-Status: GOOD ( 23.12 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Information about physical memory regions is needed by both the kernel and M-mode firmware. For example, the kernel needs to know about noncacheable aliases of cacheable memory in order to allocate coherent memory pages for DMA. M-mode firmware needs to know about aliases so it can protect itself from lower-privileged software. Firmware also needs to know the platform's Physical Address Width in order to efficiently implement Smmpt. The RISC-V Privileged Architecture delegates the description of Physical Memory Attributes to the platform. On DT-based platforms, it makes sense to put this information in the devicetree. Signed-off-by: Samuel Holland --- .../bindings/riscv/physical-memory.yaml | 101 ++++++++++++++++++ include/dt-bindings/riscv/physical-memory.h | 44 ++++++++ 2 files changed, 145 insertions(+) create mode 100644 Documentation/devicetree/bindings/riscv/physical-memory.yaml create mode 100644 include/dt-bindings/riscv/physical-memory.h diff --git a/Documentation/devicetree/bindings/riscv/physical-memory.yaml b/Documentation/devicetree/bindings/riscv/physical-memory.yaml new file mode 100644 index 000000000000..deb49b34672f --- /dev/null +++ b/Documentation/devicetree/bindings/riscv/physical-memory.yaml @@ -0,0 +1,101 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/riscv/physical-memory.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: RISC-V Physical Memory Regions + +maintainers: + - Samuel Holland + +description: + The RISC-V Privileged Architecture defines a number of Physical Memory + Attributes (PMAs) which apply to a given region of memory. These include the + types of accesses (read, write, execute, LR/SC, and/or AMO) allowed within + a region, the supported access widths and alignments, the cacheability and + coherence of the region, and whether or not accesses to the region may have + side effects. + + Some RISC-V platforms provide multiple physical address mappings for main + memory or certain peripherals. Each alias of a region generally has different + PMAs (e.g. cacheable vs non-cacheable), which allows software to dynamically + select the PMAs for an access by referencing the corresponding alias. + + The RISC-V Supervisor Domains specification defines a platform-specific + Physical Address Width (PAW), which describes the largest physical address + supported by a platform. Any access to an address >= 2^PAW is guaranteed to + raise an access fault, and therefore metadata (e.g. Memory Protection Tables) + need not be maintained for those addresses. + + On DT-based RISC-V platforms, all of this information is provided by the + riscv,physical-memory-regions property of the root node. + +properties: + $nodename: + const: '/' + + riscv,physical-memory-regions: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: + A table of physical memory regions. The first entry in the table must + cover the entire range of physical addresses supported by the platform + (i.e. 0 to 2^PAW-1) and provides the default PMAs for all addresses not + covered by another table entry. Remaining table entries provide PMAs for + more specific physical memory regions, which must be contained within the + range of entry 0, but which must not overlap with each other. + minItems: 1 + maxItems: 256 + items: + minItems: 4 + maxItems: 6 + additionalItems: true + items: + - description: CPU physical address (#address-cells) + - description: > + Size (#size-cells). For entry 0, if the size is zero, the size is + assumed to be 2^(32 * #size-cells). + - description: > + Flags describing the most restrictive PMAs for any address within + the region. + + The least significant byte indicates the types of accesses allowed + for this region. Note that a memory region may support a type of + access (e.g. AMOs) even if the CPU does not. + + The next byte describes the cacheability, coherence, idempotency, + and ordering PMAs for this region. It also includes a flag to + indicate that accesses to a region are unsafe and must be + prohibited by software (for example using PMPs or Smmpt). + + The third byte is reserved for future PMAs. + + The most significant byte is the index of the lowest-numbered entry + which this entry is an alias of, if any. Aliases need not be the + same size, for example if a smaller memory region repeats within a + larger alias. + - description: Reserved for describing future PMAs + +additionalProperties: true + +examples: + - | + #include + + / { + compatible = "starfive,jh7100"; + #address-cells = <2>; + #size-cells = <2>; + riscv,physical-memory-regions = + <0x00 0x00000000 0x40 0x00000000 (PMA_RW | PMA_IO) 0x0>, + <0x00 0x18000000 0x00 0x00020000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY) 0x0>, + <0x00 0x18080000 0x00 0x00020000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY) 0x0>, + <0x00 0x41000000 0x00 0x1f000000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY) 0x0>, + <0x00 0x61000000 0x00 0x1f000000 (PMA_RWXA | PMA_NONCOHERENT_MEMORY | PMR_ALIAS(3)) 0x0>, + <0x00 0x80000000 0x08 0x00000000 (PMA_RWXA | PMA_NONCOHERENT_MEMORY) 0x0>, + <0x10 0x00000000 0x08 0x00000000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY | PMR_ALIAS(5)) 0x0>, + <0x20 0x00000000 0x10 0x00000000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY) 0x0>, + <0x30 0x00000000 0x10 0x00000000 (PMA_RWXA | PMA_NONCOHERENT_MEMORY | PMR_ALIAS(7)) 0x0>; + }; + +... diff --git a/include/dt-bindings/riscv/physical-memory.h b/include/dt-bindings/riscv/physical-memory.h new file mode 100644 index 000000000000..7cb2e58fa8c1 --- /dev/null +++ b/include/dt-bindings/riscv/physical-memory.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ + +#ifndef _DT_BINDINGS_RISCV_PHYSICAL_MEMORY_H +#define _DT_BINDINGS_RISCV_PHYSICAL_MEMORY_H + +#define PMA_READ (1 << 0) +#define PMA_WRITE (1 << 1) +#define PMA_EXECUTE (1 << 2) +#define PMA_AMO_MASK (3 << 4) +#define PMA_AMO_NONE (0 << 4) +#define PMA_AMO_SWAP (1 << 4) +#define PMA_AMO_LOGICAL (2 << 4) +#define PMA_AMO_ARITHMETIC (3 << 4) +#define PMA_RSRV_MASK (3 << 6) +#define PMA_RSRV_NONE (0 << 6) +#define PMA_RSRV_NON_EVENTUAL (1 << 6) +#define PMA_RSRV_EVENTUAL (2 << 6) + +#define PMA_RW (PMA_READ | PMA_WRITE) +#define PMA_RWA (PMA_RW | PMA_AMO_ARITHMETIC | PMA_RSRV_EVENTUAL) +#define PMA_RWX (PMA_RW | PMA_EXECUTE) +#define PMA_RWXA (PMA_RWA | PMA_EXECUTE) + +#define PMA_ORDER_MASK (3 << 8) +#define PMA_ORDER_IO_RELAXED (0 << 8) +#define PMA_ORDER_IO_STRONG (1 << 8) +#define PMA_ORDER_MEMORY (2 << 8) +#define PMA_READ_IDEMPOTENT (1 << 10) +#define PMA_WRITE_IDEMPOTENT (1 << 11) +#define PMA_CACHEABLE (1 << 12) +#define PMA_COHERENT (1 << 13) + +#define PMA_UNSAFE (1 << 15) + +#define PMA_IO (PMA_ORDER_IO_RELAXED) +#define PMA_NONCACHEABLE_MEMORY (PMA_ORDER_MEMORY | PMA_READ_IDEMPOTENT | \ + PMA_WRITE_IDEMPOTENT) +#define PMA_NONCOHERENT_MEMORY (PMA_NONCACHEABLE_MEMORY | PMA_CACHEABLE) +#define PMA_NORMAL_MEMORY (PMA_NONCOHERENT_MEMORY | PMA_COHERENT) + +#define PMR_ALIAS_MASK (0xff << 24) +#define PMR_ALIAS(n) ((n) << 24) + +#endif /* _DT_BINDINGS_RISCV_PHYSICAL_MEMORY_H */ From patchwork Sat Nov 2 00:07:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859842 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 EB1B9E6F095 for ; Sat, 2 Nov 2024 00:09:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9RnnErcCvFfaL7Il2q0YaNvO3NK/3jjPgIk70WQUfFE=; b=oW+dGP7QApYsyg MEW4+dcJTOCqZOoamuhDXCInIaAacrfEJqd+yTVGIkNyK8HBex5AzppJWGZNEQmhqJRlfIWRN0m14 kQPIw62WLjUNuNHwE7AMa4b8epC8Zv5T5e/JFCpsq/19RrE8AoiatMbBpLgQFogDAoL+Z9ouRuIY3 lgNZD2Tv57GMUjk/Y7KKZdFgKnmfvi8BfIFPy35fZ4f9YMVSDtrvxGSd+4RjsjBsVMwRlRRMx2bFe AGM5+1l9o+hzQIsSRyVIugc2ffXdLIlmZnUpDcr7hWVd/PfHJceeU4T72jYrJP4lKYRq8qXBXQGz4 SPD5LaUU9ywN+3vko4dg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hH-00000008a0E-1BH4; Sat, 02 Nov 2024 00:08:55 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hB-00000008Zx9-40Q0 for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:51 +0000 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-2e2b9480617so1934929a91.1 for ; Fri, 01 Nov 2024 17:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506129; x=1731110929; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lekxDnwARtrYdlJ1Ne7TEw2fH4iE05/E7poMDP757sA=; b=NmQ0Az5SDWx8xtrodxvud8+HKjbDIf1KbXZoaIAqanZushIZKvAOHdm9XV2aBWTgNq Jlaep2xM7vi5VOL7GJLng43Sb7b1EfvuFwXgsHpvi1o9KcsnPH8J+CKxbkK7YJBP1Zgq uCRGnt5+Cb2nh3zZp4TlAkDJDsCP5D93v+HEnNTv0Ty8FhwuWBuUt7yqaJEXNoOj8cT7 2OFox/k4FTmyIwezTgzdsOJl1FTV1WJriuBH/iWc9BylnCVdsi2eVjL39YX9CGbEe80/ KdrOF3AE7yrCrU2ra0zWNqzwGH7Tbzonlr72dG+R8vaoqDRtNF7yhhcqsUcdOAJrky0c C4fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506129; x=1731110929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lekxDnwARtrYdlJ1Ne7TEw2fH4iE05/E7poMDP757sA=; b=bXKWC9eNhFlTelOO6nUJJALL8PoXX13JK1UYzKjwHaEVnMA13KGKkGy/ZI6euWMia4 MT/vRvk5DNh882BrQWg/Z1Tb4pBUyRbPpWKhGy6nh0xUXanWGd2atYC+Hj6gbLbZrSZv 0lK7AzO4oul3HAlOQ9HDKQ83RYWPticGhwEa5OVWBovjy1GKP5dNxTEmFFHc2Yj9YyPR /qhnUqBXTpNssIg8FzM25Y95wMrp8VLWhnCibgy98GHSZcHeVEkOCXkoGACJ9X9OFk5D yXbvk6RjyNqGa1sj2QNiYaiE3rEXKizLF1Drj2KoPcgA3E28e6WQ0plNGWVz1zBHUPmP sQow== X-Forwarded-Encrypted: i=1; AJvYcCWnWzFKp+Y5u22CoyKdQSX8uSsFoYgQz513oMbcj4sKOuMetFSexKQiWozgnl6mZT74wP0U2Na4o9z2qA==@lists.infradead.org X-Gm-Message-State: AOJu0YzJx05ubFmVmAUrVcx7juju5t1wrBxEzKyWOAlMcr0Bv1wj2iAA 66MGlPmPmccO4Z58OW1bq38lSawk8w6RdGWHxgzWV6ggkwGQNviloheff1t3vK4= X-Google-Smtp-Source: AGHT+IFIFmJ6HbS9Ca94WbQx+pPA5itCw4zhwiKe4XcgzfhIEgH9OEfG0R25JvEsk6agnHfgYMkYmA== X-Received: by 2002:a17:90b:53c3:b0:2e2:d859:1603 with SMTP id 98e67ed59e1d1-2e93c1d39b0mr11018955a91.25.1730506128809; Fri, 01 Nov 2024 17:08:48 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:48 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 02/11] riscv: mm: Increment PFN in place when splitting mappings Date: Fri, 1 Nov 2024 17:07:56 -0700 Message-ID: <20241102000843.1301099-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170850_018858_1A225098 X-CRM114-Status: GOOD ( 12.22 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The current code separates page table entry values into a PFN and a pgprot_t before incrementing the PFN and combining the two parts using pfn_pXX(). On some hardware with custom page table formats or memory aliases, the pfn_pXX() functions need to transform the PTE value, so these functions would need to apply the opposite transformation when breaking apart the PTE value. However, both transformations can be avoided by incrementing the PFN in place, as done by pte_advance_pfn() and set_ptes(). Signed-off-by: Samuel Holland --- arch/riscv/mm/pageattr.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c index 271d01a5ba4d..335060adc1a6 100644 --- a/arch/riscv/mm/pageattr.c +++ b/arch/riscv/mm/pageattr.c @@ -109,9 +109,8 @@ static int __split_linear_mapping_pmd(pud_t *pudp, continue; if (pmd_leaf(pmdp_get(pmdp))) { + pte_t pte = pmd_pte(pmdp_get(pmdp)); struct page *pte_page; - unsigned long pfn = _pmd_pfn(pmdp_get(pmdp)); - pgprot_t prot = __pgprot(pmd_val(pmdp_get(pmdp)) & ~_PAGE_PFN_MASK); pte_t *ptep_new; int i; @@ -121,7 +120,7 @@ static int __split_linear_mapping_pmd(pud_t *pudp, ptep_new = (pte_t *)page_address(pte_page); for (i = 0; i < PTRS_PER_PTE; ++i, ++ptep_new) - set_pte(ptep_new, pfn_pte(pfn + i, prot)); + set_pte(ptep_new, pte_advance_pfn(pte, i)); smp_wmb(); @@ -149,9 +148,8 @@ static int __split_linear_mapping_pud(p4d_t *p4dp, continue; if (pud_leaf(pudp_get(pudp))) { + pmd_t pmd = __pmd(pud_val(pudp_get(pudp))); struct page *pmd_page; - unsigned long pfn = _pud_pfn(pudp_get(pudp)); - pgprot_t prot = __pgprot(pud_val(pudp_get(pudp)) & ~_PAGE_PFN_MASK); pmd_t *pmdp_new; int i; @@ -162,7 +160,8 @@ static int __split_linear_mapping_pud(p4d_t *p4dp, pmdp_new = (pmd_t *)page_address(pmd_page); for (i = 0; i < PTRS_PER_PMD; ++i, ++pmdp_new) set_pmd(pmdp_new, - pfn_pmd(pfn + ((i * PMD_SIZE) >> PAGE_SHIFT), prot)); + __pmd(pmd_val(pmd) + + (i << (PMD_SHIFT - PAGE_SHIFT + PFN_PTE_SHIFT)))); smp_wmb(); @@ -198,9 +197,8 @@ static int __split_linear_mapping_p4d(pgd_t *pgdp, continue; if (p4d_leaf(p4dp_get(p4dp))) { + pud_t pud = __pud(p4d_val(p4dp_get(p4dp))); struct page *pud_page; - unsigned long pfn = _p4d_pfn(p4dp_get(p4dp)); - pgprot_t prot = __pgprot(p4d_val(p4dp_get(p4dp)) & ~_PAGE_PFN_MASK); pud_t *pudp_new; int i; @@ -215,7 +213,8 @@ static int __split_linear_mapping_p4d(pgd_t *pgdp, pudp_new = (pud_t *)page_address(pud_page); for (i = 0; i < PTRS_PER_PUD; ++i, ++pudp_new) set_pud(pudp_new, - pfn_pud(pfn + ((i * PUD_SIZE) >> PAGE_SHIFT), prot)); + __pud(pud_val(pud) + + (i << (PUD_SHIFT - PAGE_SHIFT + PFN_PTE_SHIFT)))); /* * Make sure the pud filling is not reordered with the From patchwork Sat Nov 2 00:07:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859840 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 6E15DE6F097 for ; Sat, 2 Nov 2024 00:09:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=LnybKwQ2gRumjoj9H+OhVHzM9mXaSAYiFyxj4a7yv+4=; b=urREfNOptmKd9H 49+2ckaKS/lSo306Oi/a2TtgjoELD4Z09U20Dea46R4h7Cl9XUAiWnM2XcMPogNPHdbI+W5tbDUbj qxllsKeUnpLZLYiBAL6C5FaLQgxPTzfSFlo9qv/GFjFygSJdpJbCKaB37ypsF8tYU7d7XtUc9CENO Nn6pLAVZY6sp8janwpTlAra4VcHcs2lk0oiB7FtOxK8QpK15RHg329TzeI0ugIm/dFsVagvDPQhsI YLMvrvTwZ4puDoGSvnBENQ2HPXpQpHOhk+Mx/zGOIm1rcn6Lkg8br6Hx0iUWYrb3a59/jiGsxZNMd ycxM8/wcrRFWc1XnYerg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hI-00000008a18-0JiD; Sat, 02 Nov 2024 00:08:56 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hD-00000008Zxy-1Rg0 for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:52 +0000 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2e59746062fso2075863a91.2 for ; Fri, 01 Nov 2024 17:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506130; x=1731110930; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mL0WzPRS7hGxrV+VhJxke/rBFtDo9qOrbNgPZOvxHPQ=; b=gykS+aCqG5dT0CqB/A+TuvPWBgYlcZgCV2Mqr7L7xceE5jG7dir3x86oS9H0qaUVsi z5ndGdmum211YTPKW8xHOBHaEIjYINQXi10iep4zTFxxdSxaEN0SWBtdnljW3pU4oFX7 cFSOolC0urpjBM8FOMKqi1giP7lQ6J7HFIimsGK5GapfYEHptTQHNnvzQA+xIrtzL0N4 qm9MeHagb7EdyIWAcNmJWJouvjGsEOh+JW6dmaIm+LLLG26LeeqZd2GBTElIfsV4BPl+ LD2uoKWjUcDCDzcyHgPAIpfA+onltWsBBXfQwyr68kk4jWVOL/Rz/XXW6fjEtC5fPoY5 IyYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506130; x=1731110930; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mL0WzPRS7hGxrV+VhJxke/rBFtDo9qOrbNgPZOvxHPQ=; b=j+TdZx5HqTxJMtxjI/XwpOJpaq5yyNLXU3ZSr+0+yDWwSGWr/1kojYEXIDS/43w84J fzpU0MeCBEvfsN2kBQNJxla8CtCN8JBTAsTMVGsYiDTet/66CKkeRruBGq1q4CP5Nk3b JcjtxQ+8EQjO+yIetwBreaeo71zljSEwX3jWQcFRXcqQuzBSNFMf2wnhLhxggtp34Sf7 yt0YcKA7HtbW9AXr+rA8hWK971ZmU+FPQe4SmH8jktze1/vijfbWUF1ROAkYfAeAXSKS vn2HrIL0vDXGfraTKHhgn80Zmi7FcFxp7Nuxb8BImSdQsfk6rOXtZJTX6s3Uf1Ggbs9R gDEw== X-Forwarded-Encrypted: i=1; AJvYcCVXNYqpQ9EU8HZb5g08u6AW/568dIHVYReEFPuy3lDOs9CVoGe4W+ucKp/KFLUI27btP3d7N3tpakJAWQ==@lists.infradead.org X-Gm-Message-State: AOJu0Ywnk6N410JJK+xGpLLsARX91WOMAqIK7uBC21g+3sJQOwRyoA43 wcZFPhuK5mvIpc15X8gZfUAqA2M5amHR6YjFinDyCYeYfcHSvlHjBKgY2LX/Djo= X-Google-Smtp-Source: AGHT+IEAzP0/lUzcVjGHo7zMNTFPjFuS/q2EkpeAXen6krZ/qDoO/6VUQL430cJI3dz42uV1XEAj6g== X-Received: by 2002:a17:90b:2d8d:b0:2e2:cd79:ec06 with SMTP id 98e67ed59e1d1-2e93c18815cmr12230152a91.10.1730506130083; Fri, 01 Nov 2024 17:08:50 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:49 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 03/11] riscv: mm: Deduplicate pgtable address conversion functions Date: Fri, 1 Nov 2024 17:07:57 -0700 Message-ID: <20241102000843.1301099-4-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170851_433419_2CA8BA00 X-CRM114-Status: GOOD ( 13.39 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some functions were defined equivalently in both pgtable.h and pgtable-64.h. Keep only one definition, and move it to pgtable-64.h unless it is also used for Sv32. Note that while Sv32 uses only two levels of page tables, the kernel is not consistent with how they are folded. THP requires pfn_pmd()/pmd_pfn() and mm/init.c requires pfn_pgd()/pgd_pfn(), so for now both are provided. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/pgtable-32.h | 4 ++++ arch/riscv/include/asm/pgtable-64.h | 28 ++++++++++++++-------------- arch/riscv/include/asm/pgtable.h | 23 ++++------------------- arch/riscv/mm/init.c | 8 ++++---- arch/riscv/mm/kasan_init.c | 8 ++++---- 5 files changed, 30 insertions(+), 41 deletions(-) diff --git a/arch/riscv/include/asm/pgtable-32.h b/arch/riscv/include/asm/pgtable-32.h index 00f3369570a8..23137347dc15 100644 --- a/arch/riscv/include/asm/pgtable-32.h +++ b/arch/riscv/include/asm/pgtable-32.h @@ -33,6 +33,10 @@ _PAGE_WRITE | _PAGE_EXEC | \ _PAGE_USER | _PAGE_GLOBAL)) +#define pud_pfn(pud) (pmd_pfn((pmd_t){ pud })) +#define p4d_pfn(p4d) (pud_pfn((pud_t){ p4d })) +#define pgd_pfn(pgd) (p4d_pfn((p4d_t){ pgd })) + static const __maybe_unused int pgtable_l4_enabled; static const __maybe_unused int pgtable_l5_enabled; diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h index 0897dd99ab8d..33e7ff049c4a 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -213,19 +213,20 @@ static inline pud_t pfn_pud(unsigned long pfn, pgprot_t prot) return __pud((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); } -static inline unsigned long _pud_pfn(pud_t pud) +#define pud_pfn pud_pfn +static inline unsigned long pud_pfn(pud_t pud) { return __page_val_to_pfn(pud_val(pud)); } static inline pmd_t *pud_pgtable(pud_t pud) { - return (pmd_t *)pfn_to_virt(__page_val_to_pfn(pud_val(pud))); + return (pmd_t *)pfn_to_virt(pud_pfn(pud)); } static inline struct page *pud_page(pud_t pud) { - return pfn_to_page(__page_val_to_pfn(pud_val(pud))); + return pfn_to_page(pud_pfn(pud)); } #define mm_p4d_folded mm_p4d_folded @@ -257,11 +258,6 @@ static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot) return __pmd((pfn << _PAGE_PFN_SHIFT) | prot_val); } -static inline unsigned long _pmd_pfn(pmd_t pmd) -{ - return __page_val_to_pfn(pmd_val(pmd)); -} - #define mk_pmd(page, prot) pfn_pmd(page_to_pfn(page), prot) #define pmd_ERROR(e) \ @@ -316,7 +312,7 @@ static inline p4d_t pfn_p4d(unsigned long pfn, pgprot_t prot) return __p4d((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); } -static inline unsigned long _p4d_pfn(p4d_t p4d) +static inline unsigned long p4d_pfn(p4d_t p4d) { return __page_val_to_pfn(p4d_val(p4d)); } @@ -324,7 +320,7 @@ static inline unsigned long _p4d_pfn(p4d_t p4d) static inline pud_t *p4d_pgtable(p4d_t p4d) { if (pgtable_l4_enabled) - return (pud_t *)pfn_to_virt(__page_val_to_pfn(p4d_val(p4d))); + return (pud_t *)pfn_to_virt(p4d_pfn(p4d)); return (pud_t *)pud_pgtable((pud_t) { p4d_val(p4d) }); } @@ -332,7 +328,7 @@ static inline pud_t *p4d_pgtable(p4d_t p4d) static inline struct page *p4d_page(p4d_t p4d) { - return pfn_to_page(__page_val_to_pfn(p4d_val(p4d))); + return pfn_to_page(p4d_pfn(p4d)); } #define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1)) @@ -378,10 +374,15 @@ static inline void pgd_clear(pgd_t *pgd) set_pgd(pgd, __pgd(0)); } +static inline unsigned long pgd_pfn(pgd_t pgd) +{ + return __page_val_to_pfn(pgd_val(pgd)); +} + static inline p4d_t *pgd_pgtable(pgd_t pgd) { if (pgtable_l5_enabled) - return (p4d_t *)pfn_to_virt(__page_val_to_pfn(pgd_val(pgd))); + return (p4d_t *)pfn_to_virt(pgd_pfn(pgd)); return (p4d_t *)p4d_pgtable((p4d_t) { pgd_val(pgd) }); } @@ -389,9 +390,8 @@ static inline p4d_t *pgd_pgtable(pgd_t pgd) static inline struct page *pgd_page(pgd_t pgd) { - return pfn_to_page(__page_val_to_pfn(pgd_val(pgd))); + return pfn_to_page(pgd_pfn(pgd)); } -#define pgd_page(pgd) pgd_page(pgd) #define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1)) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index e79f15293492..3e0e1177107d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -258,19 +258,19 @@ static inline pgd_t pfn_pgd(unsigned long pfn, pgprot_t prot) return __pgd((pfn << _PAGE_PFN_SHIFT) | prot_val); } -static inline unsigned long _pgd_pfn(pgd_t pgd) +static inline unsigned long pmd_pfn(pmd_t pmd) { - return __page_val_to_pfn(pgd_val(pgd)); + return __page_val_to_pfn(pmd_val(pmd)); } static inline struct page *pmd_page(pmd_t pmd) { - return pfn_to_page(__page_val_to_pfn(pmd_val(pmd))); + return pfn_to_page(pmd_pfn(pmd)); } static inline unsigned long pmd_page_vaddr(pmd_t pmd) { - return (unsigned long)pfn_to_virt(__page_val_to_pfn(pmd_val(pmd))); + return (unsigned long)pfn_to_virt(pmd_pfn(pmd)); } static inline pte_t pmd_pte(pmd_t pmd) @@ -673,21 +673,6 @@ static inline pmd_t pmd_mkinvalid(pmd_t pmd) return __pmd(pmd_val(pmd) & ~(_PAGE_PRESENT|_PAGE_PROT_NONE)); } -#define __pmd_to_phys(pmd) (__page_val_to_pfn(pmd_val(pmd)) << PAGE_SHIFT) - -static inline unsigned long pmd_pfn(pmd_t pmd) -{ - return ((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT); -} - -#define __pud_to_phys(pud) (__page_val_to_pfn(pud_val(pud)) << PAGE_SHIFT) - -#define pud_pfn pud_pfn -static inline unsigned long pud_pfn(pud_t pud) -{ - return ((__pud_to_phys(pud) & PUD_MASK) >> PAGE_SHIFT); -} - static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) { return pte_pmd(pte_modify(pmd_pte(pmd), newprot)); diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 0e8c20adcd98..7282b62b7e8d 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -497,7 +497,7 @@ static void __meminit create_pmd_mapping(pmd_t *pmdp, ptep = pt_ops.get_pte_virt(pte_phys); memset(ptep, 0, PAGE_SIZE); } else { - pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); + pte_phys = PFN_PHYS(pmd_pfn(pmdp[pmd_idx])); ptep = pt_ops.get_pte_virt(pte_phys); } @@ -599,7 +599,7 @@ static void __meminit create_pud_mapping(pud_t *pudp, uintptr_t va, phys_addr_t nextp = pt_ops.get_pmd_virt(next_phys); memset(nextp, 0, PAGE_SIZE); } else { - next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index])); + next_phys = PFN_PHYS(pud_pfn(pudp[pud_index])); nextp = pt_ops.get_pmd_virt(next_phys); } @@ -625,7 +625,7 @@ static void __meminit create_p4d_mapping(p4d_t *p4dp, uintptr_t va, phys_addr_t nextp = pt_ops.get_pud_virt(next_phys); memset(nextp, 0, PAGE_SIZE); } else { - next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index])); + next_phys = PFN_PHYS(p4d_pfn(p4dp[p4d_index])); nextp = pt_ops.get_pud_virt(next_phys); } @@ -682,7 +682,7 @@ void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phy nextp = get_pgd_next_virt(next_phys); memset(nextp, 0, PAGE_SIZE); } else { - next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx])); + next_phys = PFN_PHYS(pgd_pfn(pgdp[pgd_idx])); nextp = get_pgd_next_virt(next_phys); } diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index c301c8d291d2..bac65e3268a4 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -171,7 +171,7 @@ static void __init kasan_early_clear_pud(p4d_t *p4dp, if (!pgtable_l4_enabled) { pudp = (pud_t *)p4dp; } else { - base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp)))); + base_pud = pt_ops.get_pud_virt(pfn_to_phys(p4d_pfn(p4dp_get(p4dp)))); pudp = base_pud + pud_index(vaddr); } @@ -196,7 +196,7 @@ static void __init kasan_early_clear_p4d(pgd_t *pgdp, if (!pgtable_l5_enabled) { p4dp = (p4d_t *)pgdp; } else { - base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp)))); + base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(pgd_pfn(pgdp_get(pgdp)))); p4dp = base_p4d + p4d_index(vaddr); } @@ -242,7 +242,7 @@ static void __init kasan_early_populate_pud(p4d_t *p4dp, if (!pgtable_l4_enabled) { pudp = (pud_t *)p4dp; } else { - base_pud = pt_ops.get_pud_virt(pfn_to_phys(_p4d_pfn(p4dp_get(p4dp)))); + base_pud = pt_ops.get_pud_virt(pfn_to_phys(p4d_pfn(p4dp_get(p4dp)))); pudp = base_pud + pud_index(vaddr); } @@ -280,7 +280,7 @@ static void __init kasan_early_populate_p4d(pgd_t *pgdp, if (!pgtable_l5_enabled) { p4dp = (p4d_t *)pgdp; } else { - base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(_pgd_pfn(pgdp_get(pgdp)))); + base_p4d = pt_ops.get_p4d_virt(pfn_to_phys(pgd_pfn(pgdp_get(pgdp)))); p4dp = base_p4d + p4d_index(vaddr); } From patchwork Sat Nov 2 00:07:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859843 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 0FCBFE6F099 for ; Sat, 2 Nov 2024 00:09:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9tNl0pR1MUTPqXdlMyzzww2d+pNnpHc4qSsXH8ACp4k=; b=vapixWwlXiX+ph bXgMihtQ1sQtE0J4ZcTHPfXEq8PrdB3UKaEHDvDsAoEN2RptUoEiXzTlyOKUD+pyPpL+iJF7sNCH2 uKr3wL7niH36NJRmvWlR8WMJazfdd0I+Hd2+j3z+J8E1+MntMUVPCvQRtNYqf3zkNOn3nGRAUGbFG rKZsQdiq7KGk+95AlRwcHAOUF++L+2lmTqEYBfy8EhsTCPFHYwl1t6i4Bp3OtT5XoVhUglIkWenHm +8vMOAUuC1b3aI9GHfBOE2r2VN6JZFbV66smiG32glI4LAndEx97xmJk6FvueOqdFhXcwzqg4GMSd 6sCefghnx5skwNPlXmYQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hI-00000008a1q-3haP; Sat, 02 Nov 2024 00:08:56 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hE-00000008Zyf-28cz for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:53 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2e30db524c2so1932728a91.1 for ; Fri, 01 Nov 2024 17:08:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506131; x=1731110931; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LGKCUDOPoqSxjqjZbl0XFkIURJ67OmxtD1LdZJwnrMc=; b=BB2I+veFyQIsX96rlorI5iXVdm9HDNf+qUxnbT85WbDrOGKOOsR8+n7gJkc3xiOQe8 jExl5AJoqCL+lF3KIKICIjg9XMrL5jtgFQaaE234l98UE/XAj3LHlFxYIQB0sAqC9u32 hrw8FRjwsw9nX42cPwGiBEXLeOonPMkCO19fSmWsqw9PN4f4f01zuUR67jDPRPizS/vR Fif1dBnmNNvYvO3BFVQ8L5LzxGRNz+EF2hkTAWAvTEWX4QbjIlqEVx8Ff/j6crSK492L y4enYeb3cv/HFgslosqSGXyRU9idWQwSqW2g1jwjv9CLiCY4PdzXBWRF8oj9GZnlxnR6 9PMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506131; x=1731110931; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LGKCUDOPoqSxjqjZbl0XFkIURJ67OmxtD1LdZJwnrMc=; b=SEU1/Tr41T4alQMjUIHEGp4LWVzcSTR5K7Yfgl9fPdOMo56zpEXacfejiO1JcXPEjg 5jk7LsJQsbuMbadfXzgG58IiHFmfeIlP22ZOZkfAz008AF6TMuQQqIiCh8IzAZxw273A 91FeVL5FEN2x+UvNJQw832q1l4I/KtHlGvB3Pm6896XizDM8U2P9RtbRtBTyQE9Uno/o +PdA8S5XkI1gmrLzGFz8O2UyJiRPAPecVN9hjrDsdANeYT7VvO841/7MwwpVEmnl/PK0 znRLJ4ApIyCjPOQA5fg4pacg83mGGMDvlprtanxEqR3MnWE5JoOfeCxLYdZ6HI5fURMu yXaw== X-Forwarded-Encrypted: i=1; AJvYcCX2vuEoLsWPOdDrobeOirp6jeDu0vgXwYM8260fhkUbyV4jOgxifIJE9YzBcCu8leiidmM20wU+Q0Ic+w==@lists.infradead.org X-Gm-Message-State: AOJu0YwE3OtuIpCzgKxvjDWRBlZV0bOmpHpUbp+w/xS0sGowjsThOvDt xOpGEkjfvnxSKZE0c1OzOoabLd7fpHFtQwPbaGg4UUQS9AKBzbpxYmTMOJsyTN8= X-Google-Smtp-Source: AGHT+IGCxiVZb4g11Y9M7gntQVTVm+MFF1ZkzqRsWftmVRLra8MKii1/bFnU2KEsW1jE0QSKLkS3ow== X-Received: by 2002:a17:90b:3c49:b0:2d8:8430:8a91 with SMTP id 98e67ed59e1d1-2e94c2b3825mr8142120a91.10.1730506131400; Fri, 01 Nov 2024 17:08:51 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:51 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 04/11] riscv: mm: Deduplicate _PAGE_CHG_MASK definition Date: Fri, 1 Nov 2024 17:07:58 -0700 Message-ID: <20241102000843.1301099-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170852_571794_94D4BD34 X-CRM114-Status: GOOD ( 10.21 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The two existing definitions are equivalent because _PAGE_MTMASK is defined as 0 on riscv32. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/pgtable-32.h | 5 ----- arch/riscv/include/asm/pgtable-64.h | 7 ------- arch/riscv/include/asm/pgtable.h | 6 ++++++ 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/arch/riscv/include/asm/pgtable-32.h b/arch/riscv/include/asm/pgtable-32.h index 23137347dc15..7dc0751d67dc 100644 --- a/arch/riscv/include/asm/pgtable-32.h +++ b/arch/riscv/include/asm/pgtable-32.h @@ -28,11 +28,6 @@ #define _PAGE_IO 0 #define _PAGE_MTMASK 0 -/* Set of bits to preserve across pte_modify() */ -#define _PAGE_CHG_MASK (~(unsigned long)(_PAGE_PRESENT | _PAGE_READ | \ - _PAGE_WRITE | _PAGE_EXEC | \ - _PAGE_USER | _PAGE_GLOBAL)) - #define pud_pfn(pud) (pmd_pfn((pmd_t){ pud })) #define p4d_pfn(p4d) (pud_pfn((pud_t){ p4d })) #define pgd_pfn(pgd) (p4d_pfn((p4d_t){ pgd })) diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h index 33e7ff049c4a..4ba88592b8d1 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -66,7 +66,6 @@ typedef struct { #define pmd_val(x) ((x).pmd) #define __pmd(x) ((pmd_t) { (x) }) - #define PTRS_PER_PMD (PAGE_SIZE / sizeof(pmd_t)) /* @@ -166,12 +165,6 @@ static inline u64 riscv_page_io(void) #define _PAGE_IO riscv_page_io() #define _PAGE_MTMASK riscv_page_mtmask() -/* Set of bits to preserve across pte_modify() */ -#define _PAGE_CHG_MASK (~(unsigned long)(_PAGE_PRESENT | _PAGE_READ | \ - _PAGE_WRITE | _PAGE_EXEC | \ - _PAGE_USER | _PAGE_GLOBAL | \ - _PAGE_MTMASK)) - static inline int pud_present(pud_t pud) { return (pud_val(pud) & _PAGE_PRESENT); diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 3e0e1177107d..afa0b455eaa4 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -201,6 +201,12 @@ extern struct pt_alloc_ops pt_ops __meminitdata; #define _PAGE_IOREMAP ((_PAGE_KERNEL & ~_PAGE_MTMASK) | _PAGE_IO) #define PAGE_KERNEL_IO __pgprot(_PAGE_IOREMAP) +/* Set of bits to preserve across pte_modify() */ +#define _PAGE_CHG_MASK (~(unsigned long)(_PAGE_PRESENT | _PAGE_READ | \ + _PAGE_WRITE | _PAGE_EXEC | \ + _PAGE_USER | _PAGE_GLOBAL | \ + _PAGE_MTMASK)) + extern pgd_t swapper_pg_dir[]; extern pgd_t trampoline_pg_dir[]; extern pgd_t early_pg_dir[]; From patchwork Sat Nov 2 00:07:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859841 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 F3279E6F098 for ; Sat, 2 Nov 2024 00:09:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=oveFcgakkXAYnyGV/wex3Qfc2ZSVlC6X1oCXOUYMMAM=; b=sL8QsOf+X4ItjK lexJmL0GETm2HXPt9gO8sgcxcwVdjEclAUiaPsK1Ykg0Nk9/LOqUrKEqSmGSEQVzYAcz1+wU1/hym nOeBaBf1NgLSN1VeiYIIYmgRDa1cP8emtV51Z2Qkz+gEI96IDSZCM1ybTc+6FiQ/CXO0Rs1tzO7s1 Asmgq1QBji5kpyC3wlESS9kRM0E3qCcUKkO0/QRuC2ImgbxwgmwAP811bQymv9SP/ZhZF97n04mnF rMZyVCV0CcWVJr/XO51w01WytmpHu4eN6x1CsGa7dZIjA8R6rrzV1EXO0WPs/O8GddWE6mnAe6sLh 11Nl8ctAqNvunZpsy+RA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hK-00000008a3A-12BN; Sat, 02 Nov 2024 00:08:58 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hG-00000008ZzU-0sF4 for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:55 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2e3d523a24dso1998610a91.0 for ; Fri, 01 Nov 2024 17:08:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506133; x=1731110933; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6f0bfv/UG/JqamBlr6Ml5vEqFTtFdcwNeUu8mlrjgUs=; b=TIIkhkA8gQKOCKOzdF3vs5gAtcBzlkzSeyXaNXISyco0ZsKAbll+4Ik1bPG8oP98Ns kBqziNrlxuMiv0W0OWcZXMU4p9s7B+PSJitbW7D0p8Lh4JLPTLKm3nSrx2/zYCXOI+KL QP//wYoa2gyPcGLoyCnpt6Uf7ou8rZ9RktD8p1xHmFZM3PixUOGA/Vu2l1tcLa0Bfzv/ MlB80o9uAUxq9ltl6GrHCEc8VmYylvTS8jehYp9fPW5HBODN+P17C/vRaRpA9s3W/ZkB vqHYM0zR7itGkzCN/MnlUe4DC3wzdszaC3u9WIqmV4OjQnGYs5hBggAyDmYXzhZfPnaV cXJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506133; x=1731110933; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6f0bfv/UG/JqamBlr6Ml5vEqFTtFdcwNeUu8mlrjgUs=; b=f0HF8gWIefppD+oB1+VvT1h1agsue8g03SeK2y/EzXSIuEBaltfgbPrCMCQ183k71U qkmItU7/PPyyHIcc71S1zNvFtdu2MsDvP4kDb/WL5rMu1/wD3p01iqNwGQ+JR1Y7+vJb 8rBZ5nDAXu0Ky28G0AqWs75xWM4/0SPL4T3i6M2TY6TJkaEyd2ia/L8KPRDdT8mSoLvV fLHF4V7FbdMbHBiPOgJ/CHdcYo6gCIiCPGxKTDvXMD5jzDJpR+T0kRodhFcvnOndvrjX TlzCfLn+Irt1ZQNpNlhyUnGKRrqfkV0ZnVBOfHuF99aOFKl62/lEUNKerihsXjoPIxrX XAhQ== X-Forwarded-Encrypted: i=1; AJvYcCVO0AI4Zyb4OMqRJTT9odhzk+xecNW5zFB/sUAk+vHKxC84HK00CyYczYGFVmOhqnMtPt6E5Qv3eE2/OA==@lists.infradead.org X-Gm-Message-State: AOJu0YwKblWb47Tz/twdWxBu1ZUFbK22BJtDPNzqyGFLUFimmylHz0so IjKLgcsbKae5e03HzsDBzSrFVNpjrmFV1DRFq+X2+RKLD2RCyxqkvjHukl4p0y0= X-Google-Smtp-Source: AGHT+IFzEa5gdEJWrmZ3qEErWiVz4Nclj5KffdLeaW0o88JxBl0OqASLc7B/YtrAYBvQuvsBiZVowQ== X-Received: by 2002:a17:90a:7c03:b0:2e2:e6fa:cfef with SMTP id 98e67ed59e1d1-2e8f11bad3emr24772724a91.25.1730506133062; Fri, 01 Nov 2024 17:08:53 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:52 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 05/11] riscv: ptdump: Only show N and MT bits when enabled in the kernel Date: Fri, 1 Nov 2024 17:07:59 -0700 Message-ID: <20241102000843.1301099-6-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170854_269064_137A0A76 X-CRM114-Status: GOOD ( 13.65 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org When the Svnapot or Svpbmt extension is not implemented, the corresponding page table bits are reserved, and must be zero. There is no need to show them in the ptdump output. When the Kconfig option for an extension is disabled, we assume it is not implemented. In that case, the kernel may provide a fallback definition for the fields, like how _PAGE_MTMASK is defined on riscv32. Using those fallback definitions in ptdump would produce incorrect results. To avoid this, hide the fields from the ptdump output. Signed-off-by: Samuel Holland --- arch/riscv/mm/ptdump.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/riscv/mm/ptdump.c b/arch/riscv/mm/ptdump.c index 9d5f657a251b..58a7322e9a82 100644 --- a/arch/riscv/mm/ptdump.c +++ b/arch/riscv/mm/ptdump.c @@ -135,11 +135,13 @@ struct prot_bits { static const struct prot_bits pte_bits[] = { { -#ifdef CONFIG_64BIT +#ifdef CONFIG_RISCV_ISA_SVNAPOT .mask = _PAGE_NAPOT, .set = "N", .clear = ".", }, { +#endif +#ifdef CONFIG_RISCV_ISA_SVPBMT .mask = _PAGE_MTMASK_SVPBMT, .set = "MT(%s)", .clear = " .. ", @@ -215,7 +217,7 @@ static void dump_prot(struct pg_state *st) if (val) { if (pte_bits[i].mask == _PAGE_SOFT) sprintf(s, pte_bits[i].set, val >> 8); -#ifdef CONFIG_64BIT +#ifdef CONFIG_RISCV_ISA_SVPBMT else if (pte_bits[i].mask == _PAGE_MTMASK_SVPBMT) { if (val == _PAGE_NOCACHE_SVPBMT) sprintf(s, pte_bits[i].set, "NC"); From patchwork Sat Nov 2 00:08:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859844 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 6DE0EE6F09A for ; Sat, 2 Nov 2024 00:09:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=daxlHV73XeapuQMgKEw4GH99zzCUoMVd0F5LoCm0nf0=; b=W4auPoCGBVIM5r TeyO3vKAuviQkoKnUdgdkVi4HhQq+mmBkx2uPXEJNCXexny4D/QqC+p+Q9L1oTgFe7fpCtS9vp/fT 2M5jV0FtnU3MxDYBiVpbEbtkwCD8WV0y8hZ1BTb9J0lQehY+0MXpg4et/jL5qGI+/1WOfF2sqL4Lf OySip8r5bW02z3MXA33zbS32PoCgIB5IzcREJJtNonee8GfvLfdpX2Y0b6sfDGw8Smj3w0m+XWd+n MtltR6ys4pcK4viK4xqUwbcFa0QlqASEoLuZt2VMGazUzQ13XSzuhkdEylr9NWI+zwgY3xVcz+rtw wzmXCd4MH1Q+Wqu37ikg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hL-00000008a4G-2dBf; Sat, 02 Nov 2024 00:08:59 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hH-00000008a0C-13fB for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:56 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2e2ad9825a7so1884094a91.0 for ; Fri, 01 Nov 2024 17:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506135; x=1731110935; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DMt0e0PXpWjMqjxD9TZrMjT1DgeDdbgWuHWjw4p2qQ4=; b=cIUEZ7WTpTS2xzmqxQ75/JOibHyQ81IwSSTLIK02NUdQFqnlXqC9OSWuBuSMigZGly Za0EkwcT/+lwzDWXCSjagbOj8ccL8uau6gx2lxtKG8kZmVlp1y3ZHD4v3mn3qH6T8rEc WWTC44s5m6RcqVMuYYmov5hiiINm8zRfsqdz/CVvGikm/D7q6sPYBHvmubXcFP2nOoDa 5G+hXkL2goywqGp0tK6AE6na0oTqmXukzlqNeA65ByzkPrtKKMM89Z7FKnLqYHVAncIv pdEfGGvMqDZYy7NBMZhpCWTLTvejDq7Ig/Iu3yntzG/j7fYWrfc4oW3RR5tdFFw4QAzx EdsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506135; x=1731110935; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DMt0e0PXpWjMqjxD9TZrMjT1DgeDdbgWuHWjw4p2qQ4=; b=rWDaTVYyI6UCFdmK1dfm4a4h+xwL+AGFtNou7GR/i042mrIN+kDG2Qq3mjiEuNFpgc 0ww3hGQHAP5vMf/ZBi+1NJ5bdHQNthJqhHOJJZeghHbSbvQPzpulV1foHjX2JkokJ0sZ cyDeyCdBHcnSTa3CpV+byLPDtMNR2J7/GLSwv36aIFVvc9gRqU997NLCflPIC6gwWJ1y 32Xl3cRj/BjriDSM273Qqm7wFeTH5UUZvxTNu9Rfv53H1rc9/1U/H+hicu62bifbgrIL dFA5/SNnvVXe07vAdJkmv7chjDE18xvPdt+wKTKfs/9iJq2vxevIz7TF1mRbRJ6v5YKk nXiA== X-Forwarded-Encrypted: i=1; AJvYcCU+imPEoCABNAW/tj8+C5th4X/dKzPmMwH7E3/i6Gb44ykzJZxYFtajXO4QVur5vp0S12wHce3WKdHVdA==@lists.infradead.org X-Gm-Message-State: AOJu0YxXJnf0Dty3j89s2+ABA1tt5jjro1YtAbNVd4kTV7OcwCBi06tb IDoIG46DfPDuowpyltCl4l8JDu+fhTNft2PeoCcbZ2NVTThUoWaMzVK2fu2C7ME= X-Google-Smtp-Source: AGHT+IGMDMDnYQja/VMsL8qyOr773n770HJzgkUAslo6h99AvoGePUEFx6d5XngignCVr6mFiZ4Fsw== X-Received: by 2002:a17:90b:4b0d:b0:2e2:b219:40a2 with SMTP id 98e67ed59e1d1-2e8f11b8bdfmr27282773a91.28.1730506134638; Fri, 01 Nov 2024 17:08:54 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:54 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 06/11] riscv: mm: Fix up memory types when writing page tables Date: Fri, 1 Nov 2024 17:08:00 -0700 Message-ID: <20241102000843.1301099-7-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170855_358348_900B4FC3 X-CRM114-Status: GOOD ( 25.64 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Currently, Linux on RISC-V has three ways to specify the cacheability and ordering PMAs of a page: 1) Do nothing; assume the system is entirely cache-coherent and rely on the hardware for any ordering requirements 2) Use the page table bits specified by Svpbmt 3) Use the page table bits specified by XTheadMae To support all three methods, the kernel dynamically determines the definitions of the _PAGE_NOCACHE and _PAGE_IO fields. However, this alone is not sufficient, as XTheadMae uses a nonzero memory type value for normal memory pages. So the kernel has an additional alternative sequence (ALT_THEAD_PMA) to insert the correct memory type when writing page table entries. Some RISC-V platforms use a fourth method to specify the cacheability of a page of RAM: RAM is mapped to multiple physical address ranges, with each alias having a different set of statically-determined PMAs. Software selects the PMAs for a page by choosing a PFN from the corresponding physical address range. This strategy also requires applying a transformation when writing page table entries. Since these physical memory aliases should be invisible to the rest of the kernel, the opposite transformation must be applied when reading page table entries. However, with this last method of specifying PMAs, there is no inherent way to indicate the cacheability of a page in the pgprot_t value, since the PFN itself determines cacheability. One possible way is to reuse the PTE bits from Svpbmt, as Svpbmt is the standard extension. This requires the Svpbmt version of _PAGE_NOCACHE and _PAGE_IO to be available even when the CPU does not support the extension. It turns out that with some clever bit manipulation, it is just as efficient to transform all three Svpbmt memory type values to the corresponding XTheadMae values, as it is to check for and insert the one XTheadMae memory type value for normal memory. This allows the _PAGE_NOCACHE and _PAGE_IO definitions to be compile-time constants, and it centralizes all memory type handling to one set of ALTERNATIVE macros. For a kernel with both Svpbmt and XTheadMae enabled, this change reduces both kernel text size and the number of alternatives applied at boot. However, there are a couple of small costs. For platforms using the first method ("do nothing"), we must mask off the memory type bits when writing page table entries, whereas previously no action was needed. Second, when reading page table entries, the XTheadMae values must be transformed back to the Svpbmt values. This "unfix" operation is also needed for alias-based PMA selection, so both methods can use the same ALTERNATIVE. As a side effect, this change fixes the reporting of the NAPOT and memory type bits from ptdump on platforms with XTheadMae. Signed-off-by: Samuel Holland --- arch/riscv/Kconfig.errata | 1 + arch/riscv/include/asm/errata_list.h | 45 ----------- arch/riscv/include/asm/pgtable-32.h | 3 + arch/riscv/include/asm/pgtable-64.h | 108 ++++++++++++++++++--------- arch/riscv/include/asm/pgtable.h | 18 +++-- arch/riscv/mm/ptdump.c | 13 ++-- 6 files changed, 93 insertions(+), 95 deletions(-) diff --git a/arch/riscv/Kconfig.errata b/arch/riscv/Kconfig.errata index 2acc7d876e1f..2806ed7916c7 100644 --- a/arch/riscv/Kconfig.errata +++ b/arch/riscv/Kconfig.errata @@ -86,6 +86,7 @@ config ERRATA_THEAD_MAE bool "Apply T-Head's memory attribute extension (XTheadMae) errata" depends on ERRATA_THEAD && 64BIT && MMU select RISCV_ALTERNATIVE_EARLY + select RISCV_ISA_SVPBMT default y help This will apply the memory attribute extension errata to handle the diff --git a/arch/riscv/include/asm/errata_list.h b/arch/riscv/include/asm/errata_list.h index 7c8a71a526a3..b127f4891083 100644 --- a/arch/riscv/include/asm/errata_list.h +++ b/arch/riscv/include/asm/errata_list.h @@ -58,51 +58,6 @@ asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID, \ ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200) \ : : "r" (addr), "r" (asid) : "memory") -/* - * _val is marked as "will be overwritten", so need to set it to 0 - * in the default case. - */ -#define ALT_SVPBMT_SHIFT 61 -#define ALT_THEAD_MAE_SHIFT 59 -#define ALT_SVPBMT(_val, prot) \ -asm(ALTERNATIVE_2("li %0, 0\t\nnop", \ - "li %0, %1\t\nslli %0,%0,%3", 0, \ - RISCV_ISA_EXT_SVPBMT, CONFIG_RISCV_ISA_SVPBMT, \ - "li %0, %2\t\nslli %0,%0,%4", THEAD_VENDOR_ID, \ - ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE) \ - : "=r"(_val) \ - : "I"(prot##_SVPBMT >> ALT_SVPBMT_SHIFT), \ - "I"(prot##_THEAD >> ALT_THEAD_MAE_SHIFT), \ - "I"(ALT_SVPBMT_SHIFT), \ - "I"(ALT_THEAD_MAE_SHIFT)) - -#ifdef CONFIG_ERRATA_THEAD_MAE -/* - * IO/NOCACHE memory types are handled together with svpbmt, - * so on T-Head chips, check if no other memory type is set, - * and set the non-0 PMA type if applicable. - */ -#define ALT_THEAD_PMA(_val) \ -asm volatile(ALTERNATIVE( \ - __nops(7), \ - "li t3, %1\n\t" \ - "slli t3, t3, %3\n\t" \ - "and t3, %0, t3\n\t" \ - "bne t3, zero, 2f\n\t" \ - "li t3, %2\n\t" \ - "slli t3, t3, %3\n\t" \ - "or %0, %0, t3\n\t" \ - "2:", THEAD_VENDOR_ID, \ - ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE) \ - : "+r"(_val) \ - : "I"(_PAGE_MTMASK_THEAD >> ALT_THEAD_MAE_SHIFT), \ - "I"(_PAGE_PMA_THEAD >> ALT_THEAD_MAE_SHIFT), \ - "I"(ALT_THEAD_MAE_SHIFT) \ - : "t3") -#else -#define ALT_THEAD_PMA(_val) -#endif - #define ALT_CMO_OP(_op, _start, _size, _cachesize) \ asm volatile(ALTERNATIVE( \ __nops(5), \ diff --git a/arch/riscv/include/asm/pgtable-32.h b/arch/riscv/include/asm/pgtable-32.h index 7dc0751d67dc..b422a15fb464 100644 --- a/arch/riscv/include/asm/pgtable-32.h +++ b/arch/riscv/include/asm/pgtable-32.h @@ -28,6 +28,9 @@ #define _PAGE_IO 0 #define _PAGE_MTMASK 0 +#define ALT_FIXUP_MT(_val) +#define ALT_UNFIX_MT(_val) + #define pud_pfn(pud) (pmd_pfn((pmd_t){ pud })) #define p4d_pfn(p4d) (pud_pfn((pud_t){ p4d })) #define pgd_pfn(pgd) (p4d_pfn((p4d_t){ pgd })) diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h index 4ba88592b8d1..4e8a32f035d7 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -8,7 +8,7 @@ #include #include -#include +#include extern bool pgtable_l4_enabled; extern bool pgtable_l5_enabled; @@ -109,6 +109,8 @@ enum napot_cont_order { #define HUGE_MAX_HSTATE 2 #endif +#ifdef CONFIG_RISCV_ISA_SVPBMT + /* * [62:61] Svpbmt Memory Type definitions: * @@ -117,9 +119,9 @@ enum napot_cont_order { * 10 - IO Non-cacheable, non-idempotent, strongly-ordered I/O memory * 11 - Rsvd Reserved for future standard use */ -#define _PAGE_NOCACHE_SVPBMT (1UL << 61) -#define _PAGE_IO_SVPBMT (1UL << 62) -#define _PAGE_MTMASK_SVPBMT (_PAGE_NOCACHE_SVPBMT | _PAGE_IO_SVPBMT) +#define _PAGE_NOCACHE (1UL << 61) +#define _PAGE_IO (2UL << 61) +#define _PAGE_MTMASK (3UL << 61) /* * [63:59] T-Head Memory Type definitions: @@ -128,42 +130,66 @@ enum napot_cont_order { * bit[61] B - Bufferable * bit[60] SH - Shareable * bit[59] Sec - Trustable - * 00110 - NC Weakly-ordered, Non-cacheable, Bufferable, Shareable, Non-trustable * 01110 - PMA Weakly-ordered, Cacheable, Bufferable, Shareable, Non-trustable + * 00110 - NC Weakly-ordered, Non-cacheable, Bufferable, Shareable, Non-trustable * 10010 - IO Strongly-ordered, Non-cacheable, Non-bufferable, Shareable, Non-trustable + * + * ALT_FIXUP_MT translates Svpbmt memory types to XTheadMae memory types. + * Pseudocode operating on bits [63:60]: + * t0 = mt << 1 + * if (t0 == 0) + * t0 |= 2 + * t0 ^= 0x5 + * mt ^= t0 + * + * ALT_UNFIX_MT translates XTheadMae memory types to Svpbmt memory types. + * Pseudocode operating on bits [63:60]: + * t0 = mt & 0xd + * t0 ^= t0 >> 1 + * mt ^= t0 */ -#define _PAGE_PMA_THEAD ((1UL << 62) | (1UL << 61) | (1UL << 60)) -#define _PAGE_NOCACHE_THEAD ((1UL << 61) | (1UL << 60)) -#define _PAGE_IO_THEAD ((1UL << 63) | (1UL << 60)) -#define _PAGE_MTMASK_THEAD (_PAGE_PMA_THEAD | _PAGE_IO_THEAD | (1UL << 59)) - -static inline u64 riscv_page_mtmask(void) -{ - u64 val; - - ALT_SVPBMT(val, _PAGE_MTMASK); - return val; -} -static inline u64 riscv_page_nocache(void) -{ - u64 val; +#define ALT_FIXUP_MT(_val) \ + asm(ALTERNATIVE_2("addi t0, zero, 0x3\n\t" \ + "slli t0, t0, 61\n\t" \ + "not t0, t0\n\t" \ + "and %0, %0, t0\n\t" \ + "nop\n\t" \ + "nop\n\t" \ + "nop", \ + __nops(7), \ + 0, RISCV_ISA_EXT_SVPBMT, CONFIG_RISCV_ISA_SVPBMT, \ + "srli t0, %0, 59\n\t" \ + "seqz t1, t0\n\t" \ + "slli t1, t1, 1\n\t" \ + "or t0, t0, t1\n\t" \ + "xori t0, t0, 0x5\n\t" \ + "slli t0, t0, 60\n\t" \ + "xor %0, %0, t0", \ + THEAD_VENDOR_ID, ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE) \ + : "+r" (_val) :: "t0", "t1") + +#define ALT_UNFIX_MT(_val) \ + asm(ALTERNATIVE(__nops(6), \ + "srli t0, %0, 60\n\t" \ + "andi t0, t0, 0xd\n\t" \ + "srli t1, t0, 1\n\t" \ + "xor t0, t0, t1\n\t" \ + "slli t0, t0, 60\n\t" \ + "xor %0, %0, t0", \ + THEAD_VENDOR_ID, ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE) \ + : "+r" (_val) :: "t0", "t1") - ALT_SVPBMT(val, _PAGE_NOCACHE); - return val; -} +#else -static inline u64 riscv_page_io(void) -{ - u64 val; +#define _PAGE_NOCACHE 0 +#define _PAGE_IO 0 +#define _PAGE_MTMASK 0 - ALT_SVPBMT(val, _PAGE_IO); - return val; -} +#define ALT_FIXUP_MT(_val) +#define ALT_UNFIX_MT(_val) -#define _PAGE_NOCACHE riscv_page_nocache() -#define _PAGE_IO riscv_page_io() -#define _PAGE_MTMASK riscv_page_mtmask() +#endif /* CONFIG_RISCV_ISA_SVPBMT */ static inline int pud_present(pud_t pud) { @@ -203,7 +229,11 @@ static inline void pud_clear(pud_t *pudp) static inline pud_t pfn_pud(unsigned long pfn, pgprot_t prot) { - return __pud((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); + pud_t pud = __pud((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); + + ALT_FIXUP_MT(pud); + + return pud; } #define pud_pfn pud_pfn @@ -244,11 +274,11 @@ static inline bool mm_pud_folded(struct mm_struct *mm) static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot) { - unsigned long prot_val = pgprot_val(prot); + pmd_t pmd = __pmd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); - ALT_THEAD_PMA(prot_val); + ALT_FIXUP_MT(pmd); - return __pmd((pfn << _PAGE_PFN_SHIFT) | prot_val); + return pmd; } #define mk_pmd(page, prot) pfn_pmd(page_to_pfn(page), prot) @@ -302,7 +332,11 @@ static inline void p4d_clear(p4d_t *p4d) static inline p4d_t pfn_p4d(unsigned long pfn, pgprot_t prot) { - return __p4d((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); + p4d_t p4d = __p4d((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); + + ALT_FIXUP_MT(p4d); + + return p4d; } static inline unsigned long p4d_pfn(p4d_t p4d) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index afa0b455eaa4..3ffcff76ac0d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -257,11 +257,11 @@ static inline void pmd_clear(pmd_t *pmdp) static inline pgd_t pfn_pgd(unsigned long pfn, pgprot_t prot) { - unsigned long prot_val = pgprot_val(prot); + pgd_t pgd = __pgd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); - ALT_THEAD_PMA(prot_val); + ALT_FIXUP_MT(pgd); - return __pgd((pfn << _PAGE_PFN_SHIFT) | prot_val); + return pgd; } static inline unsigned long pmd_pfn(pmd_t pmd) @@ -338,11 +338,11 @@ static inline unsigned long pte_pfn(pte_t pte) /* Constructs a page table entry */ static inline pte_t pfn_pte(unsigned long pfn, pgprot_t prot) { - unsigned long prot_val = pgprot_val(prot); + pte_t pte = __pte((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot)); - ALT_THEAD_PMA(prot_val); + ALT_FIXUP_MT(pte); - return __pte((pfn << _PAGE_PFN_SHIFT) | prot_val); + return pte; } #define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot) @@ -489,9 +489,11 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) { unsigned long newprot_val = pgprot_val(newprot); - ALT_THEAD_PMA(newprot_val); + ALT_UNFIX_MT(pte); + pte = __pte((pte_val(pte) & _PAGE_CHG_MASK) | newprot_val); + ALT_FIXUP_MT(pte); - return __pte((pte_val(pte) & _PAGE_CHG_MASK) | newprot_val); + return pte; } #define pgd_ERROR(e) \ diff --git a/arch/riscv/mm/ptdump.c b/arch/riscv/mm/ptdump.c index 58a7322e9a82..6528c2561437 100644 --- a/arch/riscv/mm/ptdump.c +++ b/arch/riscv/mm/ptdump.c @@ -142,7 +142,7 @@ static const struct prot_bits pte_bits[] = { }, { #endif #ifdef CONFIG_RISCV_ISA_SVPBMT - .mask = _PAGE_MTMASK_SVPBMT, + .mask = _PAGE_MTMASK, .set = "MT(%s)", .clear = " .. ", }, { @@ -218,10 +218,10 @@ static void dump_prot(struct pg_state *st) if (pte_bits[i].mask == _PAGE_SOFT) sprintf(s, pte_bits[i].set, val >> 8); #ifdef CONFIG_RISCV_ISA_SVPBMT - else if (pte_bits[i].mask == _PAGE_MTMASK_SVPBMT) { - if (val == _PAGE_NOCACHE_SVPBMT) + else if (pte_bits[i].mask == _PAGE_MTMASK) { + if (val == _PAGE_NOCACHE) sprintf(s, pte_bits[i].set, "NC"); - else if (val == _PAGE_IO_SVPBMT) + else if (val == _PAGE_IO) sprintf(s, pte_bits[i].set, "IO"); else sprintf(s, pte_bits[i].set, "??"); @@ -282,9 +282,12 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val) { struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); - u64 pa = PFN_PHYS(pte_pfn(__pte(val))); u64 prot = 0; + u64 pa; + ALT_UNFIX_MT(val); + + pa = PFN_PHYS(pte_pfn(__pte(val))); if (level >= 0) prot = val & pg_level[level].mask; From patchwork Sat Nov 2 00:08:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859845 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 95854E6F095 for ; Sat, 2 Nov 2024 00:09:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=UtoLdrJDvbFDeFO+MAOIWXiuNlvgL8I+OYMvMgVNHbs=; b=oSvx3IQFHx+SiA LPoxom4m6W8yV/mpeBvecR84SssY4jVMPcYV5GR9fGaQd/Jg6k01qZJRfbNfRvscmP3/jiZtsfxj+ DOZieIou1VYrN7UehAOOMUUtlj29GHmd9/kqTlnezC14zzkQVzO5/IivGODjU6bwu5pW9D8hgOE8s +wow5sPa5wTCBpXl0GbCUsMz9cQiBWpk1FtnHqthMboA8q3AzTkktaO/Ts2Q/OWBxnJzoz3FM+jet Gfd4L+4iRxif+kL8XnfFYUT6/zUv9hv6NqVZq9eRZ/sC/Oevj15XtTDiP5MhxbzJ9PIkeztUY2iyq 4CIxyRYp9GWTygV2JfdA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hN-00000008a5p-2CWN; Sat, 02 Nov 2024 00:09:01 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hJ-00000008a1P-0Lu9 for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:59 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-207115e3056so24316985ad.2 for ; Fri, 01 Nov 2024 17:08:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506136; x=1731110936; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VLWTqwFTklP5kOAS4tWrZE7o6PRbxvUdE0W7VG/oyEc=; b=C3EOTHbFKEmHXkCKGvXix/0usMVUC7SoJDEREJ0EM2gRr5ZcWGR37I4tzNN/KCf6GZ URbKFNAMqPROfcE/2vYSwLiYn5Dt0Hd+VR7te7INKYw9nWsyLfv5ak/RflnNOIjvYmTD GlmzLT2fqd2a8C14i+OL4jmgKeJDv+5kRBwCQhJ8KymKX8FKQXyBaYqhqu68tO1Ia4kD D7ST5eRW2wVZCLWsgf6mjxGuj5vtRvSg7/9r/kYQ5TjVFB+tTxF2mrAWjCSRwbAqdVuS vud4OHzQRsPTU6P6xU91BP4+m/Fn4QoUBCeNS+uTJokFPTH/+a5vPbEh8ZYQeKzFyHbQ UAyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506136; x=1731110936; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VLWTqwFTklP5kOAS4tWrZE7o6PRbxvUdE0W7VG/oyEc=; b=SIRs+yAj6OzGzSpv+8Cl8yMxKoF7YnXieKlnCmSIF8laooNEDkNHUxQuqBc2Vj2G5o KoCWQifpmChdPTnu7LxC5kN42WHGmVGhe1ZqAT/kM2rqCWs/+tuwb1Wo1gsTHm9UDuni Hzp2mKWFSSgQAY5/E+uUIFO6pnacB9nnIoLHs3hP4vBKYMZ9IYVrIoIbS/pLD9a50lal PSKOr+srIe4W5B0fLHr8tVBv6/GIkQ3D6kMzteTYJwZegG3CZRPpN6OQBSVooKVuAekv ciwcHFT3ppzW9hQpb0N6+2q1JS0ZA1ApZOwlni6PZuS2mAiwVCmDj/Ug6lOnQOsuiU2Z FeVQ== X-Forwarded-Encrypted: i=1; AJvYcCVSsA0f9FbigqZYbi6wmwHRM/m6xiFok4Wl7EuFIVHbmi8PeiAoHg3oUHaX3vePvS286tIa3A9IJS9TAw==@lists.infradead.org X-Gm-Message-State: AOJu0YwgsFGW8Qe61fwmZFSSi6Bph5/6vipKegJ/QglkWqpurmLEHtCs smU9zwKg2xC0EwK/aJIXzfDmfouV50QMpayEzuFxyB/8klqSFx59mS4ww1vobk0= X-Google-Smtp-Source: AGHT+IFrBzJx8mZGS+kpSXYQaBncFZYtUSdQlG/06GLokAQ9rvGT8eZfFNaYR53VSzsL3JuFc3E7IQ== X-Received: by 2002:a17:902:ecc3:b0:20c:f292:3a21 with SMTP id d9443c01a7336-210c6892a36mr346018705ad.15.1730506135958; Fri, 01 Nov 2024 17:08:55 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:55 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 07/11] riscv: mm: Expose all page table bits to assembly code Date: Fri, 1 Nov 2024 17:08:01 -0700 Message-ID: <20241102000843.1301099-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170857_296382_43653261 X-CRM114-Status: GOOD ( 13.97 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org pgtable-32.h and pgtable-64.h are not usable by assembly code, so move all page table field definitions to pgtable-bits.h. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/pgtable-32.h | 11 ------- arch/riscv/include/asm/pgtable-64.h | 30 ------------------- arch/riscv/include/asm/pgtable-bits.h | 42 +++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/arch/riscv/include/asm/pgtable-32.h b/arch/riscv/include/asm/pgtable-32.h index b422a15fb464..ba50b65b434b 100644 --- a/arch/riscv/include/asm/pgtable-32.h +++ b/arch/riscv/include/asm/pgtable-32.h @@ -17,17 +17,6 @@ #define MAX_POSSIBLE_PHYSMEM_BITS 34 -/* - * rv32 PTE format: - * | XLEN-1 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 - * PFN reserved for SW D A G U X W R V - */ -#define _PAGE_PFN_MASK GENMASK(31, 10) - -#define _PAGE_NOCACHE 0 -#define _PAGE_IO 0 -#define _PAGE_MTMASK 0 - #define ALT_FIXUP_MT(_val) #define ALT_UNFIX_MT(_val) diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h index 4e8a32f035d7..174b6a5837c2 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -68,20 +68,6 @@ typedef struct { #define __pmd(x) ((pmd_t) { (x) }) #define PTRS_PER_PMD (PAGE_SIZE / sizeof(pmd_t)) -/* - * rv64 PTE format: - * | 63 | 62 61 | 60 54 | 53 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 - * N MT RSV PFN reserved for SW D A G U X W R V - */ -#define _PAGE_PFN_MASK GENMASK(53, 10) - -/* - * [63] Svnapot definitions: - * 0 Svnapot disabled - * 1 Svnapot enabled - */ -#define _PAGE_NAPOT_SHIFT 63 -#define _PAGE_NAPOT BIT(_PAGE_NAPOT_SHIFT) /* * Only 64KB (order 4) napot ptes supported. */ @@ -111,18 +97,6 @@ enum napot_cont_order { #ifdef CONFIG_RISCV_ISA_SVPBMT -/* - * [62:61] Svpbmt Memory Type definitions: - * - * 00 - PMA Normal Cacheable, No change to implied PMA memory type - * 01 - NC Non-cacheable, idempotent, weakly-ordered Main Memory - * 10 - IO Non-cacheable, non-idempotent, strongly-ordered I/O memory - * 11 - Rsvd Reserved for future standard use - */ -#define _PAGE_NOCACHE (1UL << 61) -#define _PAGE_IO (2UL << 61) -#define _PAGE_MTMASK (3UL << 61) - /* * [63:59] T-Head Memory Type definitions: * bit[63] SO - Strong Order @@ -182,10 +156,6 @@ enum napot_cont_order { #else -#define _PAGE_NOCACHE 0 -#define _PAGE_IO 0 -#define _PAGE_MTMASK 0 - #define ALT_FIXUP_MT(_val) #define ALT_UNFIX_MT(_val) diff --git a/arch/riscv/include/asm/pgtable-bits.h b/arch/riscv/include/asm/pgtable-bits.h index a8f5205cea54..96710d4c1817 100644 --- a/arch/riscv/include/asm/pgtable-bits.h +++ b/arch/riscv/include/asm/pgtable-bits.h @@ -6,6 +6,16 @@ #ifndef _ASM_RISCV_PGTABLE_BITS_H #define _ASM_RISCV_PGTABLE_BITS_H +/* + * rv32 PTE format: + * | XLEN-1 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 + * PFN reserved for SW D A G U X W R V + * + * rv64 PTE format: + * | 63 | 62 61 | 60 54 | 53 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 + * N MT RSV PFN reserved for SW D A G U X W R V + */ + #define _PAGE_ACCESSED_OFFSET 6 #define _PAGE_PRESENT (1 << 0) @@ -22,6 +32,36 @@ #define _PAGE_DEVMAP (1 << 9) /* RSW, devmap */ #define _PAGE_TABLE _PAGE_PRESENT +#define _PAGE_PFN_SHIFT 10 +#ifdef CONFIG_64BIT +#define _PAGE_PFN_MASK GENMASK(53, 10) +#else +#define _PAGE_PFN_MASK GENMASK(31, 10) +#endif /* CONFIG_64BIT */ + +#ifdef CONFIG_RISCV_ISA_SVPBMT +/* + * [62:61] Svpbmt Memory Type definitions: + * + * 00 - PMA Normal Cacheable, No change to implied PMA memory type + * 01 - NC Non-cacheable, idempotent, weakly-ordered Main Memory + * 10 - IO Non-cacheable, non-idempotent, strongly-ordered I/O memory + * 11 - Rsvd Reserved for future standard use + */ +#define _PAGE_NOCACHE (UL(1) << 61) +#define _PAGE_IO (UL(2) << 61) +#define _PAGE_MTMASK (UL(3) << 61) +#else +#define _PAGE_NOCACHE 0 +#define _PAGE_IO 0 +#define _PAGE_MTMASK 0 +#endif /* CONFIG_RISCV_ISA_SVPBMT */ + +#ifdef CONFIG_RISCV_ISA_SVNAPOT +#define _PAGE_NAPOT_SHIFT 63 +#define _PAGE_NAPOT BIT(_PAGE_NAPOT_SHIFT) +#endif /* CONFIG_RISCV_ISA_SVNAPOT */ + /* * _PAGE_PROT_NONE is set on not-present pages (and ignored by the hardware) to * distinguish them from swapped out pages @@ -31,8 +71,6 @@ /* Used for swap PTEs only. */ #define _PAGE_SWP_EXCLUSIVE _PAGE_ACCESSED -#define _PAGE_PFN_SHIFT 10 - /* * when all of R/W/X are zero, the PTE is a pointer to the next level * of the page table; otherwise, it is a leaf PTE. From patchwork Sat Nov 2 00:08:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859846 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 F2896E6F096 for ; Sat, 2 Nov 2024 00:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ZhuAZUdQl7hHgxOKSrvjGXreoeFBUSkV1xBC9AAOvfw=; b=rtKT2Gn3NE3luW aplrNCSCyCkpqCP++b8g+oLA8R7STtItTDywKPx3Q8gB746qDzELH68thAr+HxyF1D05doml1nyb0 8gf1ReE7Rt0WBG3XZebjZPEKyA2xSAUH56Dx7q21GfiwHczMaSEAnCFqhUn0D+wTnrTCIOEh73BZY 1QFAtyka57fEHxvEO0f3STZ010Y8K44i8fZVjMrbs5NQn6GgVurJdKPzJWV3OQoHpMnjUa4EZ/VjS eBo28yFcVAbHoeB+qbVUk8yD3t2z3JyGAtv9I8xFlsL5C9gnETOAtAsJnvxu7zKCi8hdcd/R9aY/o akPR1fXHb2bJ6T1GIXlA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hO-00000008a6a-36jQ; Sat, 02 Nov 2024 00:09:02 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hJ-00000008a2q-3pP7 for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:08:59 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2e2e8c8915eso1870202a91.3 for ; Fri, 01 Nov 2024 17:08:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506137; x=1731110937; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JRtASMxkZ2ldGOvfeerHbEGvM2z0DU3xjXzdLsuPJUE=; b=ZtwPQS+ReCuHug4myEESvla0Rd/gGFG8U8qSopqKZs8BEYs8LyrmE9zQPtgLIMlt4u TCYNNaxLdiCBx59pKE6GpyF+N+/w5UEQwbkrnMpZbzTdchay5oriljvH/jt66akAnQtL exHKtD7hcUAHN1cBGtRCv/s9AOwDO2uUMcN/+A3ZQCaqH/rmWYouo/Mk4Tcnkx+vTxYY tcD6gdlwvew8b+zdUNmvdUOD6zKBhxa0t3jyzBzztyOGfi9GmNDHP/QZ9A8chUp5402w 0Bu/zv1wkjPM/HVCg9ikdtJtpv2I4L/MS+KhABI0PfjJyYOBF23+VhDj80XcE2xVJGS/ CQxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506137; x=1731110937; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JRtASMxkZ2ldGOvfeerHbEGvM2z0DU3xjXzdLsuPJUE=; b=mfuLPQp4g1t7r0gCxyxlyUiycmhkiWNNlreqqkV2ugsVJesiYfzsqmC1bOfGt5Og4/ wBUOdkLvPk5I5+e8UclAc+dWx/YbXBVONsWNLNQZPjxrN8Ac7gA19Z8D28tbCIXtpjFL n9r1mjwewPdpkF36EDwoXqAp3LDD8lK7+KxU3dFy5sFc8L2rV0cM081UBcNYzEmTXpHC QraSk1og1u8nWYrU5pN6P8NDUW654hGOoN/OeAKe2XEINDVByLEkzQWC0bnAS0f07yAb KGQC4Ktm6OtEmVQQjUYl/4dIhkzsRjYEuQr0vBXb2YcTrO8c4HSDCeYoQtp7MeQa0Q1g DimA== X-Forwarded-Encrypted: i=1; AJvYcCXeY71+YrsGtdu8UY4L87Svs16NsTvn6wwD2n9tJDQkskiqrRATxUhVg3AzQE+EvGB4E5ZG65wvpRSnOQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxQABWiwviHiNuGp5J8Ed08twSI2/wvhPsqQW0U/4z9tbYzzxAC k3+6S5+BWB0utHJO+nMd8zB1toGHpDk3sLuIF6DLItH/QUbrGzDDStiTs7rzOVo= X-Google-Smtp-Source: AGHT+IH5KcByeEBw2OOY5V7H9n30x3o1ATzmOMlVsm210lwm8Cl0yUhEsGHB9DB3VS3H/jnYnQ0xAQ== X-Received: by 2002:a17:90b:4c11:b0:2e2:e6bf:cd64 with SMTP id 98e67ed59e1d1-2e94c2a15e2mr7677527a91.5.1730506137321; Fri, 01 Nov 2024 17:08:57 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:56 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 08/11] riscv: alternative: Add an ALTERNATIVE_3 macro Date: Fri, 1 Nov 2024 17:08:02 -0700 Message-ID: <20241102000843.1301099-9-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170858_081473_FED29B34 X-CRM114-Status: GOOD ( 12.64 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org ALT_FIXUP_PMA() is already using ALTERNATIVE_2(), but needs to be extended to handle a fourth case. Add ALTERNATIVE_3(), which extends ALTERNATIVE_2() with another block of new content. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/alternative-macros.h | 45 ++++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/alternative-macros.h b/arch/riscv/include/asm/alternative-macros.h index 721ec275ce57..b6027a8b6b50 100644 --- a/arch/riscv/include/asm/alternative-macros.h +++ b/arch/riscv/include/asm/alternative-macros.h @@ -50,8 +50,17 @@ ALT_NEW_CONTENT \vendor_id_2, \patch_id_2, \enable_2, "\new_c_2" .endm +.macro ALTERNATIVE_CFG_3 old_c, new_c_1, vendor_id_1, patch_id_1, enable_1, \ + new_c_2, vendor_id_2, patch_id_2, enable_2, \ + new_c_3, vendor_id_3, patch_id_3, enable_3 + ALTERNATIVE_CFG_2 "\old_c", "\new_c_1", \vendor_id_1, \patch_id_1, \enable_1 \ + "\new_c_2", \vendor_id_2, \patch_id_2, \enable_2 \ + ALT_NEW_CONTENT \vendor_id_3, \patch_id_3, \enable_3, "\new_c_3" +.endm + #define __ALTERNATIVE_CFG(...) ALTERNATIVE_CFG __VA_ARGS__ #define __ALTERNATIVE_CFG_2(...) ALTERNATIVE_CFG_2 __VA_ARGS__ +#define __ALTERNATIVE_CFG_3(...) ALTERNATIVE_CFG_3 __VA_ARGS__ #else /* !__ASSEMBLY__ */ @@ -98,6 +107,13 @@ __ALTERNATIVE_CFG(old_c, new_c_1, vendor_id_1, patch_id_1, enable_1) \ ALT_NEW_CONTENT(vendor_id_2, patch_id_2, enable_2, new_c_2) +#define __ALTERNATIVE_CFG_3(old_c, new_c_1, vendor_id_1, patch_id_1, enable_1, \ + new_c_2, vendor_id_2, patch_id_2, enable_2, \ + new_c_3, vendor_id_3, patch_id_3, enable_3) \ + __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, patch_id_1, enable_1, \ + new_c_2, vendor_id_2, patch_id_2, enable_2) \ + ALT_NEW_CONTENT(vendor_id_3, patch_id_3, enable_3, new_c_3) + #endif /* __ASSEMBLY__ */ #define _ALTERNATIVE_CFG(old_c, new_c, vendor_id, patch_id, CONFIG_k) \ @@ -108,6 +124,13 @@ __ALTERNATIVE_CFG_2(old_c, new_c_1, vendor_id_1, patch_id_1, IS_ENABLED(CONFIG_k_1), \ new_c_2, vendor_id_2, patch_id_2, IS_ENABLED(CONFIG_k_2)) +#define _ALTERNATIVE_CFG_3(old_c, new_c_1, vendor_id_1, patch_id_1, CONFIG_k_1, \ + new_c_2, vendor_id_2, patch_id_2, CONFIG_k_2, \ + new_c_3, vendor_id_3, patch_id_3, CONFIG_k_3) \ + __ALTERNATIVE_CFG_3(old_c, new_c_1, vendor_id_1, patch_id_1, IS_ENABLED(CONFIG_k_1), \ + new_c_2, vendor_id_2, patch_id_2, IS_ENABLED(CONFIG_k_2), \ + new_c_3, vendor_id_3, patch_id_3, IS_ENABLED(CONFIG_k_3)) + #else /* CONFIG_RISCV_ALTERNATIVE */ #ifdef __ASSEMBLY__ @@ -121,6 +144,9 @@ #define _ALTERNATIVE_CFG_2(old_c, ...) \ ALTERNATIVE_CFG old_c +#define _ALTERNATIVE_CFG_3(old_c, ...) \ + ALTERNATIVE_CFG old_c + #else /* !__ASSEMBLY__ */ #define __ALTERNATIVE_CFG(old_c) \ @@ -132,6 +158,9 @@ #define _ALTERNATIVE_CFG_2(old_c, ...) \ __ALTERNATIVE_CFG(old_c) +#define _ALTERNATIVE_CFG_3(old_c, ...) \ + __ALTERNATIVE_CFG(old_c) + #endif /* __ASSEMBLY__ */ #endif /* CONFIG_RISCV_ALTERNATIVE */ @@ -152,15 +181,21 @@ _ALTERNATIVE_CFG(old_content, new_content, vendor_id, patch_id, CONFIG_k) /* - * A vendor wants to replace an old_content, but another vendor has used - * ALTERNATIVE() to patch its customized content at the same location. In - * this case, this vendor can create a new macro ALTERNATIVE_2() based - * on the following sample code and then replace ALTERNATIVE() with - * ALTERNATIVE_2() to append its customized content. + * Variant of ALTERNATIVE() that supports two sets of replacement content. */ #define ALTERNATIVE_2(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \ new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2) \ _ALTERNATIVE_CFG_2(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \ new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2) +/* + * Variant of ALTERNATIVE() that supports three sets of replacement content. + */ +#define ALTERNATIVE_3(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \ + new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2, \ + new_content_3, vendor_id_3, patch_id_3, CONFIG_k_3) \ + _ALTERNATIVE_CFG_3(old_content, new_content_1, vendor_id_1, patch_id_1, CONFIG_k_1, \ + new_content_2, vendor_id_2, patch_id_2, CONFIG_k_2, \ + new_content_3, vendor_id_3, patch_id_3, CONFIG_k_3) + #endif From patchwork Sat Nov 2 00:08:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859848 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 7C091E6F099 for ; Sat, 2 Nov 2024 00:09:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4QBzUFA4N9PxCfzT0yBRcAIEZEiCQUJJzH0mYBU2Kjg=; b=WssvsGy45xmzV9 QChoG0oaNV6d0WUBp1KJ1a/VQG/rZA/zsFUMvz3QDAEzhRS/Cn4vbc38MoQs1BPcHew4RYoCycKEE ip4313wqIizHiLc2Ihl8IivDtLO2o2UGz9FKs8tnJSlbfjLGUdooj3RFd/0iv8B0jNBnvbnVnzqXp mmFBR6kVQEKQvUAYtdVrNGAHDVS75MNhqbmPAFnmzAcdqzgiyrYzbr4J5GPbeaprHyu+DN0zOUKRI HYmY45fFNgKsatEIcD24Agpe7o9i7vraK8mPLaUaGUBYQPGAybG6krqliChaeZUbeEidGawrWk8L3 rUihrX6ueB0U6fW+925Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hP-00000008a7d-3kgE; Sat, 02 Nov 2024 00:09:03 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hL-00000008a3t-2ngw for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:09:01 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2e2eba31d3aso1867945a91.2 for ; Fri, 01 Nov 2024 17:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506139; x=1731110939; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qj12kTh6O/rQ/Xl4Qt+B24adktPEP3DMoxSVhcVbOAI=; b=lFgu6dCfzcCRKmcJgikWpUHNKbb464K9yTLwEaztQDvq5pkHtZVOQ7SHJjm9D7Dwea h7/s9Sl1ldkJWwlTnI9nOxzywQPIpOTZLO5GcLz/OemA/8aZjbyTROnVnsYMaJgJ2gac vdvo3aHn38uWqjD2iG5a+NxP4saEH34fRxIhgm9Ikj/Fr9DSaxV9hs/NkJX2i1ec9bKx 9T4xsg11PNZMIqJPDSAP2wYB2PFKQcLCBJ+eG94Zz2tJXB3cfajIu4owhiLkkYJrC4dM 9kkUpSTmR7g+Ig4ryZkZmEvib6/nf4ixEQBdl6clFpIo8TsrerYEgsGKbkzVQ6XiKwKg 8jyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506139; x=1731110939; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qj12kTh6O/rQ/Xl4Qt+B24adktPEP3DMoxSVhcVbOAI=; b=dY67DGRNlt9Vex4azZXpEk+0RumGqzQOX2C8eeg5y3HIlvaEPkifdns533ANbmcm6V Yoqhe6xGy1IyU4ni5lNHgxIJt5QGhXBbmd5lCJC3H2v0sZUpe6gqZ827zTUKD/+dRJCZ b2AWLBWDLHlkbWsLiGinVfFxGFALoYAtE4+zHZ7HZ1RjhEepqBiaS5ZfVLYV/nyOLB9j aBkelQb8Eq3SSacOo1i8nNZH11XL3cbYfC7DAPVsFDsMBxkyELmpvRNSmilH+WzOBgBi 3RG39G6gKfVjbOZqKLaMexA+1BoHV/SQmK7E+IAEAUN6LipnYBaKdriHxRnnvSSe9VYT Kzaw== X-Forwarded-Encrypted: i=1; AJvYcCUXi4ZEKB7Kxb0MYRF8tHvM91Hb/8OMQ8kwalh9VI5mSzxRYYv3PxuaoVYFiEDuRk3vvm95aToDbtraJg==@lists.infradead.org X-Gm-Message-State: AOJu0YwMHKjgpGu1QsBKL0mKbFUTDteprpe3MwTSDtKaMIBky5Wm5Qcr MhS3ALqMt9M3yTcsFCQ8cftt7drTPOLGNWRQ/led3P1hE6WtAVHr/v4Ij4gy6ko= X-Google-Smtp-Source: AGHT+IGji3cval3a5wb8acdD/53zei/e3sJ2bqSexeB5xbUIDHdLdQW6YclDCN4wNPBQMFu1+TtgTQ== X-Received: by 2002:a17:90b:315:b0:2e2:ba35:356c with SMTP id 98e67ed59e1d1-2e8f11dceb8mr25968856a91.39.1730506138674; Fri, 01 Nov 2024 17:08:58 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:58 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 09/11] riscv: alternative: Allow calls with alternate link registers Date: Fri, 1 Nov 2024 17:08:03 -0700 Message-ID: <20241102000843.1301099-10-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170859_835673_A6CD8A05 X-CRM114-Status: GOOD ( 11.68 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Alternative assembly code may wish to use an alternate link register to minimize the number of clobbered registers. Apply the offset fix to all jalr (not jr) instructions, i.e. where rd is not x0. Signed-off-by: Samuel Holland --- arch/riscv/kernel/alternative.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c index 0128b161bfda..54d79e6f4afa 100644 --- a/arch/riscv/kernel/alternative.c +++ b/arch/riscv/kernel/alternative.c @@ -121,8 +121,8 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len, if (!riscv_insn_is_jalr(insn2)) continue; - /* if instruction pair is a call, it will use the ra register */ - if (RV_EXTRACT_RD_REG(insn) != 1) + /* if instruction pair is a call, it will save a link register */ + if (RV_EXTRACT_RD_REG(insn) == 0) continue; riscv_alternative_fix_auipc_jalr(alt_ptr + i * sizeof(u32), From patchwork Sat Nov 2 00:08:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859849 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 A602BE6F097 for ; Sat, 2 Nov 2024 00:09:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=S+llj3H3A7IJcjYa1ZoEK/UQYmbZdnS8jH91AZf5w9g=; b=tUboBC/9HYGCLT d0yKbtMUId7VomcCGgJUesoMJ53C/flKrmLhE5EDQXgTBLLgFAf6Vq3/NgKmuHG/gh6cXj7Bka41L B1VMH12zBPciABIAn/rnjH3V1OTwFJqM6TYYenvHf250D2/MOQQhM5Crr7yEWJljGIGtDXhc3XcR5 aqTljuCN98CA5+njSvCAjtvb+VDCs+1HTXrPzRexu2r2v9LIc5qjnbZO636xIuOq91HZ4OjWbxTSf tilMXuuaQxga5j2gkGOIhlAnLRRICOlF2l8WrqVYDHD7kxo/H8GnVhy5pQPVM+JfbVyvz5Pn86FAG QbQYIbRxkQZjo77YKmtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hQ-00000008a8L-43Za; Sat, 02 Nov 2024 00:09:04 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hM-00000008a57-2rdz for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:09:02 +0000 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-2e2eba31d3aso1867962a91.2 for ; Fri, 01 Nov 2024 17:09:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506140; x=1731110940; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bMdTiw85E/ndb+s2ImQQp45t4KJbvNlvAuA5mb4sn38=; b=TBmMpVayzillyTzQrVzB6RtyW6AwbTIZu5n7DusFahHS+WUtpe40OFJNgtqDBOtI/f ak/+pKsXQemyDvHb+0yIpHRTxpF7A/MDauOyRROKvaM6DsF6G+q5wmcR45tH+tuM9AxU /s9lmsNIFTCMuJEMbA+icQTSpDZ8BXomWJVddTVq/8g6TEMfeGvgscF2qT7Lyu+tuJm7 pFUPdo4tgzo4bq/QiEW1Bi6SEW+d7fCUYDdN88GIaZT3ZRT9HeVaz03Bk+Zt/OA4YtjT c5VfV6E/sLV7WySAnbAj/74w8+KZFBrM1/Bc27nABGeOKwbEeWogpaaquotRQ8Y7+J8G EcnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506140; x=1731110940; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bMdTiw85E/ndb+s2ImQQp45t4KJbvNlvAuA5mb4sn38=; b=cara75FYr10D78kIzthf3GnUJ6BRcJVI1BKf/v6+oQCcfCvvCsbTxziZlht24SBTRO AXm6taJhb3NMBbILUpmIvzEvpOnV0vpRcKaASQhWuTR0rVuI8ARQOuwiObWYYu5abUEQ WVg2CEbUOAEBFPps5YnbcoF/ri4cMa9ohqhpkEzsxRSdhUCqOidXPYDfTXPmmClquit3 yM9TME96fJvvK6rDDAJKWZ4WYJbOfVyVhILbsD0i8nbdVfmaCRQ0DqwU7AY0hq01bW+j YXWK2n1d6CQe9X7DawkV2GIzDWC32OUo9ud7i8BNFNbGFnELM4Z4tfU8ibeosIa+59M8 ljnA== X-Forwarded-Encrypted: i=1; AJvYcCUzeamULCvkDTZ0JPLmK8u3Hn9gjBjC0DBfuQu5ZklVcS/ABUpuRe9y2OhrjsbabRsQcBTilroteNZEwg==@lists.infradead.org X-Gm-Message-State: AOJu0YyhU/HC0g7q939wTtftcpG83Q9LycbhBfwCCBJcoFcjtCDR4Kz2 UAHOzROqRa5DnFCYm5W5qD2L7STx7GGhhF7jnoXUCvph4nC7g7moH3ja/RW2tDI= X-Google-Smtp-Source: AGHT+IHB07oy//WNtEG4aQ2tSWxlcw5NVWtvl+A9a2GkhEm/z9LgVjJWHeMoDLv894FIO26fgWOlJg== X-Received: by 2002:a17:90b:390e:b0:2e2:b64e:f4f7 with SMTP id 98e67ed59e1d1-2e8f11b8c2amr26674437a91.29.1730506140045; Fri, 01 Nov 2024 17:09:00 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.08.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:08:59 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 10/11] riscv: mm: Use physical memory aliases to apply PMAs Date: Fri, 1 Nov 2024 17:08:04 -0700 Message-ID: <20241102000843.1301099-11-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170900_756004_EE6422DD X-CRM114-Status: GOOD ( 30.13 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On some RISC-V platforms, RAM is mapped to multiple physical address ranges, with each alias having a different set of statically-determined Physical Memory Attributes (PMAs). Software selects the PMAs for a page by choosing a PFN from the corresponding physical address range. Implement this by transforming the PFN when writing page tables. If the memory type field is nonzero, replace the PFN with the corresponding PFN from the noncached alias. Similarly, when reading from the page tables, if the PFN is found in a noncached alias, replace it with the corresponding PFN from the cached alias, and insert _PAGE_NOCACHE. The rest of the kernel sees only the cached PFNs and _PAGE_MTMASK values as if Svpbmt was implemented. Memory alias pairs are determined from the devicetree. A new cpufeature bit is required because that is the only way to trigger alternative patching. Signed-off-by: Samuel Holland --- arch/riscv/Kconfig | 3 + arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/pgtable-64.h | 27 ++++++-- arch/riscv/include/asm/pgtable.h | 8 +++ arch/riscv/kernel/cpufeature.c | 6 ++ arch/riscv/kernel/setup.c | 1 + arch/riscv/mm/Makefile | 1 + arch/riscv/mm/memory-alias.S | 101 ++++++++++++++++++++++++++++ arch/riscv/mm/pgtable.c | 91 +++++++++++++++++++++++++ 9 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 arch/riscv/mm/memory-alias.S diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 62545946ecf4..d28d1dab5f26 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -566,6 +566,9 @@ config RISCV_ISA_SVPBMT The Svpbmt extension is only available on 64-bit cpus. + This option also controls selection of memory type based on + physical memory aliases. + If you don't know what to do here, say Y. config TOOLCHAIN_HAS_V diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 46d9de54179e..8a37e22f4223 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -94,6 +94,7 @@ #define RISCV_ISA_EXT_ZAWRS 85 #define RISCV_ISA_EXT_SVVPTC 86 +#define RISCV_ISA_EXT_XLINUXMEMALIAS 126 #define RISCV_ISA_EXT_XLINUXENVCFG 127 #define RISCV_ISA_EXT_MAX 128 diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h index 174b6a5837c2..6b4af408a37a 100644 --- a/arch/riscv/include/asm/pgtable-64.h +++ b/arch/riscv/include/asm/pgtable-64.h @@ -124,27 +124,46 @@ enum napot_cont_order { */ #define ALT_FIXUP_MT(_val) \ - asm(ALTERNATIVE_2("addi t0, zero, 0x3\n\t" \ + asm(ALTERNATIVE_3("addi t0, zero, 0x3\n\t" \ "slli t0, t0, 61\n\t" \ "not t0, t0\n\t" \ "and %0, %0, t0\n\t" \ "nop\n\t" \ "nop\n\t" \ + "nop\n\t" \ "nop", \ - __nops(7), \ + __nops(8), \ 0, RISCV_ISA_EXT_SVPBMT, CONFIG_RISCV_ISA_SVPBMT, \ + "addi t0, zero, 0x3\n\t" \ + "slli t0, t0, 61\n\t" \ + "and t0, %0, t0\n\t" \ + "beqz t0, 2f\n\t" \ + "xor t1, %0, t0\n\t" \ + "1: auipc t0, %%pcrel_hi(riscv_fixup_memory_alias)\n\t" \ + "jalr t0, t0, %%pcrel_lo(1b)\n\t" \ + "mv %0, t1\n" \ + "2:", \ + 0, RISCV_ISA_EXT_XLINUXMEMALIAS, CONFIG_RISCV_ISA_SVPBMT, \ "srli t0, %0, 59\n\t" \ "seqz t1, t0\n\t" \ "slli t1, t1, 1\n\t" \ "or t0, t0, t1\n\t" \ "xori t0, t0, 0x5\n\t" \ "slli t0, t0, 60\n\t" \ - "xor %0, %0, t0", \ + "xor %0, %0, t0\n\t" \ + "nop", \ THEAD_VENDOR_ID, ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE) \ : "+r" (_val) :: "t0", "t1") #define ALT_UNFIX_MT(_val) \ - asm(ALTERNATIVE(__nops(6), \ + asm(ALTERNATIVE_2(__nops(6), \ + "mv t1, %0\n\t" \ + "1: auipc t0, %%pcrel_hi(riscv_unfix_memory_alias)\n\t" \ + "jalr t0, t0, %%pcrel_lo(1b)\n\t" \ + "mv %0, t1\n\t" \ + "nop\n\t" \ + "nop", \ + 0, RISCV_ISA_EXT_XLINUXMEMALIAS, CONFIG_RISCV_ISA_SVPBMT, \ "srli t0, %0, 60\n\t" \ "andi t0, t0, 0xd\n\t" \ "srli t1, t0, 1\n\t" \ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 3ffcff76ac0d..0e52dfaaff63 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -949,6 +949,14 @@ extern u64 satp_mode; void paging_init(void); void misc_mem_init(void); +#ifdef CONFIG_RISCV_ISA_SVPBMT +bool __init riscv_have_memory_alias(void); +void __init riscv_init_memory_alias(void); +#else +static inline bool riscv_have_memory_alias(void) { return false; } +static inline void riscv_init_memory_alias(void) {} +#endif /* CONFIG_RISCV_ISA_SVPBMT */ + /* * ZERO_PAGE is a global shared page that is always zero, * used for zero-mapped memory areas, etc. diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 3a8eeaa9310c..ca36f8240a86 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -892,6 +892,12 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_V; } + /* Vendor-independent alternatives require a bit in the ISA bitmap. */ + if (riscv_have_memory_alias()) { + set_bit(RISCV_ISA_EXT_XLINUXMEMALIAS, riscv_isa); + pr_info("Using physical memory alias for noncached mappings\n"); + } + memset(print_str, 0, sizeof(print_str)); for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++) if (riscv_isa[0] & BIT_MASK(i)) diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index a2cde65b69e9..ab718fc4538f 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -287,6 +287,7 @@ void __init setup_arch(char **cmdline_p) } riscv_init_cbo_blocksizes(); + riscv_init_memory_alias(); riscv_fill_hwcap(); init_rt_signal_env(); apply_boot_alternatives(); diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index cbe4d775ef56..50d843b298cd 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -33,3 +33,4 @@ endif obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_RISCV_DMA_NONCOHERENT) += dma-noncoherent.o obj-$(CONFIG_RISCV_NONSTANDARD_CACHE_OPS) += cache-ops.o +obj-$(CONFIG_RISCV_ISA_SVPBMT) += memory-alias.o diff --git a/arch/riscv/mm/memory-alias.S b/arch/riscv/mm/memory-alias.S new file mode 100644 index 000000000000..df2e8cc3f69c --- /dev/null +++ b/arch/riscv/mm/memory-alias.S @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2024 SiFive + */ + +#include +#include +#include +#include + +#define CACHED_BASE_OFFSET (0 * RISCV_SZPTR) +#define NONCACHED_BASE_OFFSET (1 * RISCV_SZPTR) +#define SIZE_OFFSET (2 * RISCV_SZPTR) + +#define SIZEOF_PAIR (4 * RISCV_SZPTR) + +SYM_CODE_START(riscv_fixup_memory_alias) + addi sp, sp, -4 * SZREG + REG_S t2, (0 * SZREG)(sp) + REG_S t3, (1 * SZREG)(sp) + REG_S t4, (2 * SZREG)(sp) +#ifdef CONFIG_RISCV_ISA_SVNAPOT + REG_S t5, (3 * SZREG)(sp) + + /* Save and mask off _PAGE_NAPOT if present. */ + li t5, _PAGE_NAPOT + and t5, t1, t5 + xor t1, t1, t5 +#endif + + lla t2, memory_alias_pairs +.Lfixup_loop: + REG_L t3, SIZE_OFFSET(t2) + beqz t3, .Lfixup_end + REG_L t4, CACHED_BASE_OFFSET(t2) + sub t4, t1, t4 + bltu t4, t3, .Lfixup_found + addi t2, t2, SIZEOF_PAIR + j .Lfixup_loop + +.Lfixup_found: + REG_L t3, NONCACHED_BASE_OFFSET(t2) + add t1, t3, t4 + +.Lfixup_end: +#ifdef CONFIG_RISCV_ISA_SVNAPOT + xor t1, t1, t5 + + REG_L t5, (3 * SZREG)(sp) +#endif + REG_L t4, (2 * SZREG)(sp) + REG_L t3, (1 * SZREG)(sp) + REG_L t2, (0 * SZREG)(sp) + addi sp, sp, 4 * SZREG + jr t0 +SYM_CODE_END(riscv_fixup_memory_alias) + +SYM_CODE_START(riscv_unfix_memory_alias) + addi sp, sp, -4 * SZREG + REG_S t2, (0 * SZREG)(sp) + REG_S t3, (1 * SZREG)(sp) + REG_S t4, (2 * SZREG)(sp) +#ifdef CONFIG_RISCV_ISA_SVNAPOT + REG_S t5, (3 * SZREG)(sp) + + /* Save and mask off _PAGE_NAPOT if present. */ + li t5, _PAGE_NAPOT + and t5, t1, t5 + xor t1, t1, t5 +#endif + + lla t2, memory_alias_pairs +.Lunfix_loop: + REG_L t3, SIZE_OFFSET(t2) + beqz t3, .Lunfix_end + REG_L t4, NONCACHED_BASE_OFFSET(t2) + sub t4, t1, t4 + bltu t4, t3, .Lunfix_found + addi t2, t2, SIZEOF_PAIR + j .Lunfix_loop + +.Lunfix_found: + REG_L t3, CACHED_BASE_OFFSET(t2) + add t1, t3, t4 + + /* PFN was in the noncached alias, so mark it as such. */ + li t2, _PAGE_NOCACHE + or t1, t1, t2 + +.Lunfix_end: +#ifdef CONFIG_RISCV_ISA_SVNAPOT + xor t1, t1, t5 + + REG_L t5, (3 * SZREG)(sp) +#endif + REG_L t4, (2 * SZREG)(sp) + REG_L t3, (1 * SZREG)(sp) + REG_L t2, (0 * SZREG)(sp) + addi sp, sp, 4 * SZREG + jr t0 +SYM_CODE_END(riscv_unfix_memory_alias) diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..8dd43001cd10 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -1,8 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include #include #include +#include +#include #include int ptep_set_access_flags(struct vm_area_struct *vma, @@ -155,3 +159,90 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +#ifdef CONFIG_RISCV_ISA_SVPBMT +struct memory_alias_pair { + unsigned long cached_base; + unsigned long noncached_base; + unsigned long size; + int index; +} memory_alias_pairs[5]; + +bool __init riscv_have_memory_alias(void) +{ + return memory_alias_pairs[0].size; +} + +void __init riscv_init_memory_alias(void) +{ + int na = of_n_addr_cells(of_root); + int ns = of_n_size_cells(of_root); + int nc = na + ns + 2; + const __be32 *prop; + int pairs = 0; + int len; + + prop = of_get_property(of_root, "riscv,physical-memory-regions", &len); + if (!prop) + return; + + len /= sizeof(__be32); + for (int i = 0; len >= nc; i++, prop += nc, len -= nc) { + unsigned long base = of_read_ulong(prop, na); + unsigned long size = of_read_ulong(prop + na, ns); + unsigned long flags = be32_to_cpup(prop + na + ns); + struct memory_alias_pair *pair; + int alias; + + /* We only care about non-coherent memory. */ + if ((flags & PMA_ORDER_MASK) != PMA_ORDER_MEMORY || (flags & PMA_COHERENT)) + continue; + + /* The cacheable alias must be usable memory. */ + if ((flags & PMA_CACHEABLE) && + !memblock_overlaps_region(&memblock.memory, base, size)) + continue; + + alias = FIELD_GET(PMR_ALIAS_MASK, flags); + if (alias) { + pair = NULL; + for (int j = 0; j < pairs; j++) { + if (alias == memory_alias_pairs[j].index) { + pair = &memory_alias_pairs[j]; + break; + } + } + if (!pair) + continue; + } else { + /* Leave room for the null sentinel. */ + if (pairs == ARRAY_SIZE(memory_alias_pairs) - 1) + continue; + pair = &memory_alias_pairs[pairs++]; + pair->index = i; + } + + /* Align the address and size with the page table PFN field. */ + base >>= PAGE_SHIFT - _PAGE_PFN_SHIFT; + size >>= PAGE_SHIFT - _PAGE_PFN_SHIFT; + + if (flags & PMA_CACHEABLE) + pair->cached_base = base; + else + pair->noncached_base = base; + pair->size = min_not_zero(pair->size, size); + } + + /* Remove any unmatched pairs. */ + for (int i = 0; i < pairs; i++) { + struct memory_alias_pair *pair = &memory_alias_pairs[i]; + + if (pair->cached_base && pair->noncached_base && pair->size) + continue; + + for (int j = i + 1; j < pairs; j++) + memory_alias_pairs[j - 1] = memory_alias_pairs[j]; + memory_alias_pairs[--pairs].size = 0; + } +} +#endif /* CONFIG_RISCV_ISA_SVPBMT */ From patchwork Sat Nov 2 00:08:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13859850 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 A9BD6E6F095 for ; Sat, 2 Nov 2024 00:09:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=wBtHZbZgn2SzZy9EmO3mW+SrTWcpcdYvRaLP5wzyJfQ=; b=SEWPJGVo+CAape f8+0KxviM4tQjSfO/rZfKEhFkvbcJqtesrBodeGvlVjG5RzxKMymeqkCwLaCD9suOnQvBxpyYA4SG TVR589yzMPwj+V8nLNyDB24wqF1mhu+62aBYYr8cj2QMhVokb4BkRx7t15l17abz5UZT/pCFFfTKP z4gPwjUTKy6D9y2mzwNKFwpW1i8oAR+X/HPNgHO0w+7rZdNA5oFWmoKLwUepTf3Rnc+DlmgTnj5pw Z0JAmIUaxnPlS+ehTJqnZc58f4/FEJ+YCP+WMCFM2wCvhbrv9sSP4F1yUKTgqpRoPvvwBFlFFTpVV A44em5LkzAMioxVMK8+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t71hR-00000008a9H-3zF2; Sat, 02 Nov 2024 00:09:05 +0000 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t71hO-00000008a69-2TIr for linux-riscv@lists.infradead.org; Sat, 02 Nov 2024 00:09:04 +0000 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2e2eba31d3aso1867971a91.2 for ; Fri, 01 Nov 2024 17:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1730506141; x=1731110941; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z+rAnka3y7HDTfLG1azR9CpKSqpVN1TkP7KNPAjIJyU=; b=iXrkZTa7tRXth5DcxyVHCHCvaH8mWS5AOV7AgjPfKFN3JAoE//RPsgbVHJWFmullLB 5jx2hlkg3RvECAwy1PIKyk7Yqx0NenoRFx7Mf5wWUGqvwfDytWfOUApNh3Z2eU1iOMAZ jwnwx4E93dfjhdnQXsCFrBosZIVwgndwete5FU3dHQ/wfzxdwgOdq3Gvpu+OyrvZhw5p IDz/hGDepMmO27mNlK7c0kqqkRwmMlb5PV3TGRqW1ZKgxalN/9uAxNF+iOx/aLe1JTHZ unzioZ/T0x1cyIkPgg9Yz+VFp5U8wtAVjq1bkMSINRKD3bQLGva9jHHXkQf+xmJEYcMo bSxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506141; x=1731110941; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z+rAnka3y7HDTfLG1azR9CpKSqpVN1TkP7KNPAjIJyU=; b=wNU+K1iubFZzoR4IE1KEiFBdkN6vJJQedFuOY0N90ONs9feZDOgmUWTCvtHq/jUv5d XTvgw9nA2C8fzQSMG3UNryOJr+ebufN57+cXSaiEbE4XFY92cPtm45YetnLk/yyVa31W RNMNbn/lZwp/+B5TKBOPQzKOS3BtecmKvzH0pIVWD0drB3QmleYwNHagYlrzEgm+Rfo+ Cs/paa/StLMwts0MwQJnY5owcV01hu1/5yteLBvTdAqt1xjG9L7Hc4A0y2xM6/cejMzN wBIeayQjv7uUONC391fMhd5Fy4XDOUC+KOnP5cEvyDgK97wxWpTcBF0Ztjg60KPnW0// Ks2g== X-Forwarded-Encrypted: i=1; AJvYcCUKqQDldn3IPU1YkYOR0upEupeAk+Ek6kDo99zzO8eTqPYYGMst3DByH11/outlcBHXFpUCcUhYock5kQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxIVlSyeyAIHemmLTixD/LodA+yINV5i5hWaFQWWQrizOvT896U tifbSaCS15R4OZlWEHAxYd6xKrSw2qP22kXZGpC6ftXr00B9rXY/PKPzPz/F2VI= X-Google-Smtp-Source: AGHT+IE9dycbI2epbg/rXlLb7+LyF9i1Z79AYkysLeojOoNqjBBp2pOKME/1fRPoHW8Jt+G33gBPpg== X-Received: by 2002:a17:90b:4d01:b0:2e2:c98e:c33f with SMTP id 98e67ed59e1d1-2e8f1058039mr30664103a91.1.1730506141388; Fri, 01 Nov 2024 17:09:01 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e92fc00856sm5505749a91.54.2024.11.01.17.09.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 17:09:00 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Conor Dooley Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Ghiti , Lad Prabhakar , Emil Renner Berthing , Rob Herring , Krzysztof Kozlowski , Samuel Holland Subject: [PATCH 11/11] riscv: dts: starfive: jh7100: Use physical memory ranges for DMA Date: Fri, 1 Nov 2024 17:08:05 -0700 Message-ID: <20241102000843.1301099-12-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20241102000843.1301099-1-samuel.holland@sifive.com> References: <20241102000843.1301099-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241101_170902_872835_DBBCB9B0 X-CRM114-Status: GOOD ( 13.97 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org JH7100 provides a physical memory region which is a noncached alias of normal cacheable DRAM. Now that Linux can apply PMAs by selecting between aliases of a physical memory region, any page of DRAM can be marked as noncached for use with DMA, and the preallocated DMA pool is no longer needed. This allows portable kernels to boot on JH7100 boards. Signed-off-by: Samuel Holland --- arch/riscv/Kconfig.errata | 19 ------------ .../boot/dts/starfive/jh7100-common.dtsi | 30 ++++--------------- 2 files changed, 6 insertions(+), 43 deletions(-) diff --git a/arch/riscv/Kconfig.errata b/arch/riscv/Kconfig.errata index 2806ed7916c7..fc2c7fb2caff 100644 --- a/arch/riscv/Kconfig.errata +++ b/arch/riscv/Kconfig.errata @@ -53,25 +53,6 @@ config ERRATA_SIFIVE_CIP_1200 If you don't know what to do here, say "Y". -config ERRATA_STARFIVE_JH7100 - bool "StarFive JH7100 support" - depends on ARCH_STARFIVE - depends on !DMA_DIRECT_REMAP - depends on NONPORTABLE - select DMA_GLOBAL_POOL - select RISCV_DMA_NONCOHERENT - select RISCV_NONSTANDARD_CACHE_OPS - select SIFIVE_CCACHE - default n - help - The StarFive JH7100 was a test chip for the JH7110 and has - caches that are non-coherent with respect to peripheral DMAs. - It was designed before the Zicbom extension so needs non-standard - cache operations through the SiFive cache controller. - - Say "Y" if you want to support the BeagleV Starlight and/or - StarFive VisionFive V1 boards. - config ERRATA_THEAD bool "T-HEAD errata" depends on RISCV_ALTERNATIVE diff --git a/arch/riscv/boot/dts/starfive/jh7100-common.dtsi b/arch/riscv/boot/dts/starfive/jh7100-common.dtsi index ae1a6aeb0aea..34885fe40e2d 100644 --- a/arch/riscv/boot/dts/starfive/jh7100-common.dtsi +++ b/arch/riscv/boot/dts/starfive/jh7100-common.dtsi @@ -9,8 +9,14 @@ #include #include #include +#include / { + riscv,physical-memory-regions = + <0x00 0x00000000 0x40 0x00000000 (PMA_RW | PMA_IO) 0x0>, + <0x00 0x80000000 0x08 0x00000000 (PMA_RWXA | PMA_NONCOHERENT_MEMORY) 0x0>, + <0x10 0x00000000 0x08 0x00000000 (PMA_RWX | PMA_NONCACHEABLE_MEMORY | PMR_ALIAS(1)) 0x0>; + aliases { mmc0 = &sdio0; mmc1 = &sdio1; @@ -42,30 +48,6 @@ led-ack { }; }; - reserved-memory { - #address-cells = <2>; - #size-cells = <2>; - ranges; - - dma-reserved@fa000000 { - reg = <0x0 0xfa000000 0x0 0x1000000>; - no-map; - }; - - linux,dma@107a000000 { - compatible = "shared-dma-pool"; - reg = <0x10 0x7a000000 0x0 0x1000000>; - no-map; - linux,dma-default; - }; - }; - - soc { - dma-ranges = <0x00 0x80000000 0x00 0x80000000 0x00 0x7a000000>, - <0x00 0xfa000000 0x10 0x7a000000 0x00 0x01000000>, - <0x00 0xfb000000 0x00 0xfb000000 0x07 0x85000000>; - }; - wifi_pwrseq: wifi-pwrseq { compatible = "mmc-pwrseq-simple"; reset-gpios = <&gpio 37 GPIO_ACTIVE_LOW>;