linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Lin <tesheng@andestech.com>
To: <linux-riscv@lists.infradead.org>
Cc: <linux-kernel@vger.kernel.org>, <paul.walmsley@sifive.com>,
	<palmer@dabbelt.com>, <aou@eecs.berkeley.edu>,
	<green.hu@gmail.com>, <Anup.Patel@wdc.com>,
	<akpm@linux-foundation.org>, <logang@deltatee.com>,
	<david.abdurachmanov@gmail.com>, <atish.patra@wdc.com>,
	<tglx@linutronix.de>, <bp@suse.de>, <yash.shah@sifive.com>,
	<alex@ghiti.fr>, <zong.li@sifive.com>, <gary@garyguo.net>,
	<rppt@linux.ibm.com>, <steven.price@arm.com>,
	Eric Lin <tesheng@andestech.com>,
	Alan Kao <alankao@andestech.com>
Subject: [PATCH 2/3] riscv/mm: Implement kmap() and kmap_atomic()
Date: Tue, 31 Mar 2020 17:32:40 +0800	[thread overview]
Message-ID: <20200331093241.3728-3-tesheng@andestech.com> (raw)
In-Reply-To: <20200331093241.3728-1-tesheng@andestech.com>

Both kmap() and kmap_atomic() help kernel to create
temporary mappings from a highmem page.

Be aware that use kmap() might put calling function to sleep
and it cannot use in interrupt context. kmap_atomic() is an
atomic version of kmap() which can be used in interrupt context
and it is faster than kmap() because it doesn't hold a lock.

Here we preserve some memory slots from fixmap region for
kmap_atomic() and kmap() will use pkmap region.

Signed-off-by: Eric Lin <tesheng@andestech.com>
Cc: Alan Kao <alankao@andestech.com>
---
 arch/riscv/include/asm/fixmap.h  |  9 +++-
 arch/riscv/include/asm/highmem.h | 30 +++++++++++++
 arch/riscv/include/asm/pgtable.h |  5 +++
 arch/riscv/mm/Makefile           |  1 +
 arch/riscv/mm/highmem.c          | 74 ++++++++++++++++++++++++++++++++
 5 files changed, 118 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/mm/highmem.c

diff --git a/arch/riscv/include/asm/fixmap.h b/arch/riscv/include/asm/fixmap.h
index 42d2c42f3cc9..8dedc2bf2917 100644
--- a/arch/riscv/include/asm/fixmap.h
+++ b/arch/riscv/include/asm/fixmap.h
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
+ * Copyright (C) 2020 Andes Technology Corporation
  */
 
 #ifndef _ASM_RISCV_FIXMAP_H
@@ -10,6 +11,7 @@
 #include <linux/sizes.h>
 #include <asm/page.h>
 #include <asm/pgtable.h>
+#include <asm/kmap_types.h>
 
 #ifdef CONFIG_MMU
 /*
@@ -28,7 +30,12 @@ enum fixed_addresses {
 	FIX_PTE,
 	FIX_PMD,
 	FIX_EARLYCON_MEM_BASE,
-	__end_of_fixed_addresses
+#ifdef CONFIG_HIGHMEM
+	FIX_KMAP_RESERVED,
+	FIX_KMAP_BEGIN,
+	FIX_KMAP_END = FIX_KMAP_BEGIN + (KM_TYPE_NR * NR_CPUS),
+#endif
+	__end_of_fixed_addresses,
 };
 
 #define FIXMAP_PAGE_IO		PAGE_KERNEL
diff --git a/arch/riscv/include/asm/highmem.h b/arch/riscv/include/asm/highmem.h
index 7fc79e58f607..ec7c83d55830 100644
--- a/arch/riscv/include/asm/highmem.h
+++ b/arch/riscv/include/asm/highmem.h
@@ -17,3 +17,33 @@
 #define PKMAP_NR(virt)	(((virt) - (PKMAP_BASE)) >> PAGE_SHIFT)
 #define PKMAP_ADDR(nr)	(PKMAP_BASE + ((nr) << PAGE_SHIFT))
 #define kmap_prot		PAGE_KERNEL
+
+static inline void flush_cache_kmaps(void)
+{
+	flush_cache_all();
+}
+
+/* Declarations for highmem.c */
+extern unsigned long highstart_pfn, highend_pfn;
+
+extern pte_t *pkmap_page_table;
+
+extern void *kmap_high(struct page *page);
+extern void kunmap_high(struct page *page);
+
+extern void kmap_init(void);
+
+/*
+ * The following functions are already defined by <linux/highmem.h>
+ * when CONFIG_HIGHMEM is not set.
+ */
+#ifdef CONFIG_HIGHMEM
+extern void *kmap(struct page *page);
+extern void kunmap(struct page *page);
+extern void *kmap_atomic(struct page *page);
+extern void __kunmap_atomic(void *kvaddr);
+extern void *kmap_atomic_pfn(unsigned long pfn);
+extern struct page *kmap_atomic_to_page(void *ptr);
+#endif
+
+#endif
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index d9a3769f1f4e..1a774d5a8bbc 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -200,6 +200,11 @@ static inline pgd_t *pgd_offset(const struct mm_struct *mm, unsigned long addr)
 /* Locate an entry in the kernel page global directory */
 #define pgd_offset_k(addr)      pgd_offset(&init_mm, (addr))
 
+#ifdef CONFIG_HIGHMEM
+/* Locate an entry in the second-level page table */
+#define pmd_off_k(addr)  pmd_offset((pud_t *)pgd_offset_k(addr), addr)
+#endif
+
 static inline struct page *pmd_page(pmd_t pmd)
 {
 	return pfn_to_page(pmd_val(pmd) >> _PAGE_PFN_SHIFT);
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index 50b7af58c566..6f9305afc632 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_SMP) += tlbflush.o
 endif
 obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
 obj-$(CONFIG_KASAN)   += kasan_init.o
+obj-$(CONFIG_HIGHMEM) += highmem.o
 
 ifdef CONFIG_KASAN
 KASAN_SANITIZE_kasan_init.o := n
diff --git a/arch/riscv/mm/highmem.c b/arch/riscv/mm/highmem.c
new file mode 100644
index 000000000000..b01ebe34619e
--- /dev/null
+++ b/arch/riscv/mm/highmem.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2005-2020 Andes Technology Corporation
+ */
+
+#include <linux/export.h>
+#include <linux/highmem.h>
+#include <linux/sched.h>
+#include <linux/smp.h>
+#include <linux/interrupt.h>
+#include <asm/fixmap.h>
+#include <asm/pgtable.h>
+#include <asm/tlbflush.h>
+
+void *kmap(struct page *page)
+{
+	unsigned long vaddr;
+
+	might_sleep();
+	if (!PageHighMem(page))
+		return page_address(page);
+	vaddr = (unsigned long)kmap_high(page);
+	return (void *)vaddr;
+}
+EXPORT_SYMBOL(kmap);
+
+void kunmap(struct page *page)
+{
+	BUG_ON(in_interrupt());
+	if (!PageHighMem(page))
+		return;
+	kunmap_high(page);
+}
+EXPORT_SYMBOL(kunmap);
+
+void *kmap_atomic(struct page *page)
+{
+	unsigned int idx;
+	unsigned long vaddr;
+	int type;
+	pte_t *ptep;
+
+	preempt_disable();
+	pagefault_disable();
+
+	if (!PageHighMem(page))
+		return page_address(page);
+
+	type = kmap_atomic_idx_push();
+
+	idx = type + KM_TYPE_NR * smp_processor_id();
+	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
+
+	ptep = pte_offset_kernel(pmd_off_k(vaddr), vaddr);
+	set_pte(ptep, mk_pte(page, kmap_prot));
+
+	return (void *)vaddr;
+}
+EXPORT_SYMBOL(kmap_atomic);
+
+void __kunmap_atomic(void *kvaddr)
+{
+	if (kvaddr >= (void *)FIXADDR_START && kvaddr < (void *)FIXADDR_TOP) {
+		unsigned long vaddr = (unsigned long)kvaddr;
+		pte_t *ptep;
+
+		kmap_atomic_idx_pop();
+		ptep = pte_offset_kernel(pmd_off_k(vaddr), vaddr);
+		set_pte(ptep, __pte(0));
+	}
+	pagefault_enable();
+	preempt_enable();
+}
+EXPORT_SYMBOL(__kunmap_atomic);
-- 
2.17.0


  parent reply	other threads:[~2020-03-31  9:50 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-31  9:32 [PATCH 0/3] Highmem support for 32-bit RISC-V Eric Lin
2020-03-31  9:32 ` [PATCH 1/3] riscv/mm: Add pkmap region and CONFIG_HIGHMEM Eric Lin
2020-03-31  9:32 ` Eric Lin [this message]
2020-03-31  9:32 ` [PATCH 3/3] riscv/mm: Add pkmap in print_vm_layout() Eric Lin
2020-04-02  9:31 ` [PATCH 0/3] Highmem support for 32-bit RISC-V Arnd Bergmann
2020-04-08  3:51   ` Alan Kao
2020-04-08 14:40     ` Arnd Bergmann
2020-04-14 15:17       ` afzal mohammed
2020-04-14 19:29         ` Arnd Bergmann
2020-04-15 13:54           ` afzal mohammed
2020-05-03 14:50             ` afzal mohammed
2020-05-03 20:20               ` Arnd Bergmann
2020-05-04  9:10                 ` afzal mohammed
2020-05-04 11:27                   ` Arnd Bergmann
2020-05-11 14:21                     ` ARM: static kernel in vmalloc space (was Re: [PATCH 0/3] Highmem support for 32-bit RISC-V) afzal mohammed
2020-05-11 15:29                       ` Arnd Bergmann
2020-05-12 10:47                         ` ARM: static kernel in vmalloc space afzal mohammed
2020-05-12 19:49                           ` Arnd Bergmann
2020-05-14 11:17                             ` afzal mohammed
2020-05-14 12:41                               ` Arnd Bergmann
2020-05-14 13:35                                 ` afzal mohammed
2020-05-14 14:44                                   ` afzal mohammed
2020-05-14 15:32                                   ` Arnd Bergmann
2020-05-16  6:06                                     ` afzal mohammed
2020-05-16  7:35                                       ` Arnd Bergmann
2020-06-07 12:59                                         ` ARM: vmsplit 4g/4g afzal mohammed
2020-06-07 16:11                                           ` Russell King - ARM Linux admin
2020-06-08 11:09                                             ` afzal mohammed
2020-06-10 10:10                                               ` Linus Walleij
2020-06-12 10:25                                                 ` afzal mohammed
2020-06-15  9:11                                                   ` Linus Walleij
2020-06-15 10:01                                                     ` afzal mohammed
2020-06-07 19:26                                           ` Arnd Bergmann
2020-06-08 11:18                                             ` afzal mohammed
2020-06-08 14:43                                               ` Arnd Bergmann
2020-06-08 15:17                                                 ` afzal mohammed
2020-06-09 12:15                                                   ` afzal mohammed
2020-06-09 14:22                                                     ` Arnd Bergmann
2020-05-14 16:25                                 ` ARM: static kernel in vmalloc space Russell King - ARM Linux admin
2020-05-14 21:12                                   ` Arnd Bergmann
2020-05-14 23:40                                     ` Russell King - ARM Linux admin
2020-05-15 15:41                                       ` Arnd Bergmann
2020-07-30  9:33                                         ` Linus Walleij
2020-07-30 10:17                                           ` Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200331093241.3728-3-tesheng@andestech.com \
    --to=tesheng@andestech.com \
    --cc=Anup.Patel@wdc.com \
    --cc=akpm@linux-foundation.org \
    --cc=alankao@andestech.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@wdc.com \
    --cc=bp@suse.de \
    --cc=david.abdurachmanov@gmail.com \
    --cc=gary@garyguo.net \
    --cc=green.hu@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=logang@deltatee.com \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=rppt@linux.ibm.com \
    --cc=steven.price@arm.com \
    --cc=tglx@linutronix.de \
    --cc=yash.shah@sifive.com \
    --cc=zong.li@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).