xref: /openbmc/linux/arch/sh/mm/kmap.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c456cfc2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22739742cSPaul Mundt /*
32739742cSPaul Mundt  * arch/sh/mm/kmap.c
42739742cSPaul Mundt  *
52739742cSPaul Mundt  * Copyright (C) 1999, 2000, 2002  Niibe Yutaka
62739742cSPaul Mundt  * Copyright (C) 2002 - 2009  Paul Mundt
72739742cSPaul Mundt  */
82739742cSPaul Mundt #include <linux/mm.h>
92739742cSPaul Mundt #include <linux/init.h>
102739742cSPaul Mundt #include <linux/mutex.h>
112739742cSPaul Mundt #include <linux/fs.h>
122739742cSPaul Mundt #include <linux/highmem.h>
132739742cSPaul Mundt #include <linux/module.h>
142739742cSPaul Mundt #include <asm/mmu_context.h>
152739742cSPaul Mundt #include <asm/cacheflush.h>
162739742cSPaul Mundt 
172739742cSPaul Mundt static pte_t *kmap_coherent_pte;
182739742cSPaul Mundt 
kmap_coherent_init(void)192739742cSPaul Mundt void __init kmap_coherent_init(void)
202739742cSPaul Mundt {
212739742cSPaul Mundt 	unsigned long vaddr;
222739742cSPaul Mundt 
232739742cSPaul Mundt 	/* cache the first coherent kmap pte */
242739742cSPaul Mundt 	vaddr = __fix_to_virt(FIX_CMAP_BEGIN);
25e05c7b1fSMike Rapoport 	kmap_coherent_pte = virt_to_kpte(vaddr);
262739742cSPaul Mundt }
272739742cSPaul Mundt 
kmap_coherent(struct page * page,unsigned long addr)282739742cSPaul Mundt void *kmap_coherent(struct page *page, unsigned long addr)
292739742cSPaul Mundt {
30*157efa29SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
312739742cSPaul Mundt 	enum fixed_addresses idx;
320906a3adSPaul Mundt 	unsigned long vaddr;
332739742cSPaul Mundt 
34*157efa29SMatthew Wilcox (Oracle) 	BUG_ON(!test_bit(PG_dcache_clean, &folio->flags));
352739742cSPaul Mundt 
36b15d53d0SDavid Hildenbrand 	preempt_disable();
370906a3adSPaul Mundt 	pagefault_disable();
382739742cSPaul Mundt 
390906a3adSPaul Mundt 	idx = FIX_CMAP_END -
40f9e2bdfdSPaul Mundt 		(((addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1)) +
41f9e2bdfdSPaul Mundt 		 (FIX_N_COLOURS * smp_processor_id()));
42f9e2bdfdSPaul Mundt 
430906a3adSPaul Mundt 	vaddr = __fix_to_virt(idx);
442739742cSPaul Mundt 
450906a3adSPaul Mundt 	BUG_ON(!pte_none(*(kmap_coherent_pte - idx)));
460906a3adSPaul Mundt 	set_pte(kmap_coherent_pte - idx, mk_pte(page, PAGE_KERNEL));
472739742cSPaul Mundt 
482739742cSPaul Mundt 	return (void *)vaddr;
492739742cSPaul Mundt }
502739742cSPaul Mundt 
kunmap_coherent(void * kvaddr)510906a3adSPaul Mundt void kunmap_coherent(void *kvaddr)
522739742cSPaul Mundt {
530906a3adSPaul Mundt 	if (kvaddr >= (void *)FIXADDR_START) {
540906a3adSPaul Mundt 		unsigned long vaddr = (unsigned long)kvaddr & PAGE_MASK;
550906a3adSPaul Mundt 		enum fixed_addresses idx = __virt_to_fix(vaddr);
560906a3adSPaul Mundt 
576e4154d4SPaul Mundt 		/* XXX.. Kill this later, here for sanity at the moment.. */
586e4154d4SPaul Mundt 		__flush_purge_region((void *)vaddr, PAGE_SIZE);
596e4154d4SPaul Mundt 
600906a3adSPaul Mundt 		pte_clear(&init_mm, vaddr, kmap_coherent_pte - idx);
610906a3adSPaul Mundt 		local_flush_tlb_one(get_asid(), vaddr);
620906a3adSPaul Mundt 	}
630906a3adSPaul Mundt 
640906a3adSPaul Mundt 	pagefault_enable();
65b15d53d0SDavid Hildenbrand 	preempt_enable();
662739742cSPaul Mundt }
67