blob: 73fd7cc99430768bdabadc497eebed197259cbd3 [file] [log] [blame]
Thomas Gleixnerc456cfc2019-05-28 10:10:14 -07001// SPDX-License-Identifier: GPL-2.0-only
Paul Mundt27397422009-08-15 09:19:19 +09002/*
3 * arch/sh/mm/kmap.c
4 *
5 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
6 * Copyright (C) 2002 - 2009 Paul Mundt
Paul Mundt27397422009-08-15 09:19:19 +09007 */
8#include <linux/mm.h>
9#include <linux/init.h>
10#include <linux/mutex.h>
11#include <linux/fs.h>
12#include <linux/highmem.h>
13#include <linux/module.h>
14#include <asm/mmu_context.h>
15#include <asm/cacheflush.h>
16
Paul Mundt27397422009-08-15 09:19:19 +090017static pte_t *kmap_coherent_pte;
18
19void __init kmap_coherent_init(void)
20{
21 unsigned long vaddr;
22
Paul Mundt27397422009-08-15 09:19:19 +090023 /* cache the first coherent kmap pte */
24 vaddr = __fix_to_virt(FIX_CMAP_BEGIN);
Mike Rapoporte05c7b12020-06-08 21:33:05 -070025 kmap_coherent_pte = virt_to_kpte(vaddr);
Paul Mundt27397422009-08-15 09:19:19 +090026}
27
28void *kmap_coherent(struct page *page, unsigned long addr)
29{
30 enum fixed_addresses idx;
Paul Mundt0906a3a2009-09-03 17:21:10 +090031 unsigned long vaddr;
Paul Mundt27397422009-08-15 09:19:19 +090032
Paul Mundt55661fc2010-12-01 15:39:51 +090033 BUG_ON(!test_bit(PG_dcache_clean, &page->flags));
Paul Mundt27397422009-08-15 09:19:19 +090034
David Hildenbrandb15d53d2016-02-29 09:19:24 +010035 preempt_disable();
Paul Mundt0906a3a2009-09-03 17:21:10 +090036 pagefault_disable();
Paul Mundt27397422009-08-15 09:19:19 +090037
Paul Mundt0906a3a2009-09-03 17:21:10 +090038 idx = FIX_CMAP_END -
Paul Mundtf9e2bdf2009-09-09 17:14:19 +090039 (((addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1)) +
40 (FIX_N_COLOURS * smp_processor_id()));
41
Paul Mundt0906a3a2009-09-03 17:21:10 +090042 vaddr = __fix_to_virt(idx);
Paul Mundt27397422009-08-15 09:19:19 +090043
Paul Mundt0906a3a2009-09-03 17:21:10 +090044 BUG_ON(!pte_none(*(kmap_coherent_pte - idx)));
45 set_pte(kmap_coherent_pte - idx, mk_pte(page, PAGE_KERNEL));
Paul Mundt27397422009-08-15 09:19:19 +090046
47 return (void *)vaddr;
48}
49
Paul Mundt0906a3a2009-09-03 17:21:10 +090050void kunmap_coherent(void *kvaddr)
Paul Mundt27397422009-08-15 09:19:19 +090051{
Paul Mundt0906a3a2009-09-03 17:21:10 +090052 if (kvaddr >= (void *)FIXADDR_START) {
53 unsigned long vaddr = (unsigned long)kvaddr & PAGE_MASK;
54 enum fixed_addresses idx = __virt_to_fix(vaddr);
55
Paul Mundt6e4154d2009-09-08 16:21:00 +090056 /* XXX.. Kill this later, here for sanity at the moment.. */
57 __flush_purge_region((void *)vaddr, PAGE_SIZE);
58
Paul Mundt0906a3a2009-09-03 17:21:10 +090059 pte_clear(&init_mm, vaddr, kmap_coherent_pte - idx);
60 local_flush_tlb_one(get_asid(), vaddr);
61 }
62
63 pagefault_enable();
David Hildenbrandb15d53d2016-02-29 09:19:24 +010064 preempt_enable();
Paul Mundt27397422009-08-15 09:19:19 +090065}