Skip to content

Commit

Permalink
riscv: add RISC-V Svpbmt extension supports
Browse files Browse the repository at this point in the history
This patch follows the standard pure RISC-V Svpbmt extension in
privilege spec to solve the non-coherent SOC dma synchronization
issues.

Here is the svpbmt PTE format:
| 63 | 62-61 | 60-8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  N     MT     RSW    D   A   G   U   X   W   R   V
        ^

Of the Reserved bits [63:54] in a leaf PTE, the high bit is already
allocated (as the N bit), so bits [62:61] are used as the MT (aka
MemType) field. This field specifies one of three memory types that
are close equivalents (or equivalent in effect) to the three main x86
and ARMv8 memory types - as shown in the following table.

RISC-V
Encoding &
MemType     RISC-V Description
----------  ------------------------------------------------
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

The standard protection_map[] needn't be modified because the "PMA"
type keeps the highest bits zero. And the whole modification is
limited in the arch/riscv/* and using a global variable
(__riscv_svpbmt) as _PAGE_DMA_MASK/IO/NC for pgprot_noncached
(&writecombine) in pgtable.h. We also add _PAGE_CHG_MASK to filter
PFN than before.

Enable it in devicetree - (Add "mmu-supports-svpbmt" in cpu node)
 - mmu-supports-svpbmt

Signed-off-by: Wei Fu <wefu@redhat.com>
Co-developed-by: Liu Shaohua <liush@allwinnertech.com>
Signed-off-by: Liu Shaohua <liush@allwinnertech.com>
Co-developed-by: Guo Ren <guoren@kernel.org>
Signed-off-by: Guo Ren <guoren@kernel.org>
Cc: Palmer Dabbelt <palmerdabbelt@google.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Anup Patel <anup.patel@wdc.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Atish Patra <atish.patra@wdc.com>
Cc: Drew Fustini <drew@beagleboard.org>
Cc: Wei Fu <wefu@redhat.com>
Cc: Wei Wu <lazyparser@gmail.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Maxime Ripard <maxime@cerno.tech>
Cc: Daniel Lustig <dlustig@nvidia.com>
Cc: Greg Favor <gfavor@ventanamicro.com>
Cc: Andrea Mondelli <andrea.mondelli@huawei.com>
Cc: Jonathan Behrens <behrensj@mit.edu>
Cc: Xinhaoqu (Freddie) <xinhaoqu@huawei.com>
Cc: Bill Huffman <huffman@cadence.com>
Cc: Nick Kossifidis <mick@ics.forth.gr>
Cc: Allen Baum <allen.baum@esperantotech.com>
Cc: Josh Scheid <jscheid@ventanamicro.com>
Cc: Richard Trauben <rtrauben@gmail.com>
  • Loading branch information
tekkamanninja authored and guoren83 committed Dec 29, 2021
1 parent 2894557 commit 86840aa
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion arch/riscv/include/asm/fixmap.h
Expand Up @@ -44,7 +44,7 @@ enum fixed_addresses {
__end_of_fixed_addresses
};

#define FIXMAP_PAGE_IO PAGE_KERNEL
#define FIXMAP_PAGE_IO PAGE_IOREMAP

#define __early_set_fixmap __set_fixmap

Expand Down
8 changes: 5 additions & 3 deletions arch/riscv/include/asm/pgtable-64.h
Expand Up @@ -91,12 +91,14 @@ static inline unsigned long _pud_pfn(pud_t pud)

static inline pmd_t *pud_pgtable(pud_t pud)
{
return (pmd_t *)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT);
return (pmd_t *)pfn_to_virt((pud_val(pud) & _PAGE_CHG_MASK)
>> _PAGE_PFN_SHIFT);
}

static inline struct page *pud_page(pud_t pud)
{
return pfn_to_page(pud_val(pud) >> _PAGE_PFN_SHIFT);
return pfn_to_page((pud_val(pud) & _PAGE_CHG_MASK)
>> _PAGE_PFN_SHIFT);
}

#define mm_pud_folded mm_pud_folded
Expand All @@ -117,7 +119,7 @@ static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot)

static inline unsigned long _pmd_pfn(pmd_t pmd)
{
return pmd_val(pmd) >> _PAGE_PFN_SHIFT;
return (pmd_val(pmd) & _PAGE_CHG_MASK) >> _PAGE_PFN_SHIFT;
}

#define mk_pmd(page, prot) pfn_pmd(page_to_pfn(page), prot)
Expand Down
40 changes: 38 additions & 2 deletions arch/riscv/include/asm/pgtable-bits.h
Expand Up @@ -7,7 +7,7 @@
#define _ASM_RISCV_PGTABLE_BITS_H

/*
* PTE format:
* 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
*/
Expand All @@ -24,6 +24,41 @@
#define _PAGE_DIRTY (1 << 7) /* Set by hardware on any write */
#define _PAGE_SOFT (1 << 8) /* Reserved for software */

#ifndef __ASSEMBLY__
#ifdef CONFIG_64BIT
/*
* 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
* [62:61] 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 _SVPBMT_NC (1UL << 61)
#define _SVPBMT_IO (2UL << 61)
#define _SVPBMT_MASK (_SVPBMT_NC | _SVPBMT_IO)

extern struct __riscv_svpbmt_struct {
unsigned long mask;
unsigned long mt_pma;
unsigned long mt_nc;
unsigned long mt_io;
} __riscv_svpbmt;

#define _PAGE_MT_MASK __riscv_svpbmt.mask
#define _PAGE_MT_PMA __riscv_svpbmt.mt_pma
#define _PAGE_MT_NC __riscv_svpbmt.mt_nc
#define _PAGE_MT_IO __riscv_svpbmt.mt_io
#else
#define _PAGE_MT_MASK 0
#define _PAGE_MT_PMA 0
#define _PAGE_MT_NC 0
#define _PAGE_MT_IO 0
#endif /* CONFIG_64BIT */
#endif /* __ASSEMBLY__ */

#define _PAGE_SPECIAL _PAGE_SOFT
#define _PAGE_TABLE _PAGE_PRESENT

Expand All @@ -38,7 +73,8 @@
/* 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_USER | _PAGE_GLOBAL | \
_PAGE_MT_MASK))
/*
* 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.
Expand Down
39 changes: 30 additions & 9 deletions arch/riscv/include/asm/pgtable.h
Expand Up @@ -156,7 +156,8 @@ struct pt_alloc_ops {
| _PAGE_PRESENT \
| _PAGE_ACCESSED \
| _PAGE_DIRTY \
| _PAGE_GLOBAL)
| _PAGE_GLOBAL \
| _PAGE_MT_PMA)

#define PAGE_KERNEL __pgprot(_PAGE_KERNEL)
#define PAGE_KERNEL_READ __pgprot(_PAGE_KERNEL & ~_PAGE_WRITE)
Expand All @@ -166,11 +167,9 @@ struct pt_alloc_ops {

#define PAGE_TABLE __pgprot(_PAGE_TABLE)

/*
* The RISC-V ISA doesn't yet specify how to query or modify PMAs, so we can't
* change the properties of memory regions.
*/
#define _PAGE_IOREMAP _PAGE_KERNEL
#define _PAGE_IOREMAP ((_PAGE_KERNEL & ~_PAGE_MT_MASK) | _PAGE_MT_IO)

#define PAGE_IOREMAP __pgprot(_PAGE_IOREMAP)

extern pgd_t swapper_pg_dir[];

Expand Down Expand Up @@ -250,12 +249,12 @@ static inline unsigned long _pgd_pfn(pgd_t pgd)

static inline struct page *pmd_page(pmd_t pmd)
{
return pfn_to_page(pmd_val(pmd) >> _PAGE_PFN_SHIFT);
return pfn_to_page((pmd_val(pmd) & _PAGE_CHG_MASK) >> _PAGE_PFN_SHIFT);
}

static inline unsigned long pmd_page_vaddr(pmd_t pmd)
{
return (unsigned long)pfn_to_virt(pmd_val(pmd) >> _PAGE_PFN_SHIFT);
return (unsigned long)pfn_to_virt((pmd_val(pmd) & _PAGE_CHG_MASK) >> _PAGE_PFN_SHIFT);
}

static inline pte_t pmd_pte(pmd_t pmd)
Expand All @@ -271,7 +270,7 @@ static inline pte_t pud_pte(pud_t pud)
/* Yields the page frame number (PFN) of a page table entry */
static inline unsigned long pte_pfn(pte_t pte)
{
return (pte_val(pte) >> _PAGE_PFN_SHIFT);
return ((pte_val(pte) & _PAGE_CHG_MASK) >> _PAGE_PFN_SHIFT);
}

#define pte_page(x) pfn_to_page(pte_pfn(x))
Expand Down Expand Up @@ -510,6 +509,28 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
return ptep_test_and_clear_young(vma, address, ptep);
}

#define pgprot_noncached pgprot_noncached
static inline pgprot_t pgprot_noncached(pgprot_t _prot)
{
unsigned long prot = pgprot_val(_prot);

prot &= ~_PAGE_MT_MASK;
prot |= _PAGE_MT_IO;

return __pgprot(prot);
}

#define pgprot_writecombine pgprot_writecombine
static inline pgprot_t pgprot_writecombine(pgprot_t _prot)
{
unsigned long prot = pgprot_val(_prot);

prot &= ~_PAGE_MT_MASK;
prot |= _PAGE_MT_NC;

return __pgprot(prot);
}

/*
* THP functions
*/
Expand Down
1 change: 1 addition & 0 deletions arch/riscv/kernel/cpufeature.c
Expand Up @@ -8,6 +8,7 @@

#include <linux/bitmap.h>
#include <linux/of.h>
#include <linux/pgtable.h>
#include <asm/processor.h>
#include <asm/hwcap.h>
#include <asm/smp.h>
Expand Down
5 changes: 5 additions & 0 deletions arch/riscv/mm/init.c
Expand Up @@ -1116,3 +1116,8 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
return vmemmap_populate_basepages(start, end, node, NULL);
}
#endif

#ifdef CONFIG_64BIT
struct __riscv_svpbmt_struct __riscv_svpbmt __ro_after_init = {0UL, 0UL, 0UL, 0UL};
EXPORT_SYMBOL(__riscv_svpbmt);
#endif

0 comments on commit 86840aa

Please sign in to comment.