blob: 384c7471a374b05ea5bf2fe96ec5306b3723266a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1999 Niibe Yutaka
Paul Mundtcdcc9702007-11-09 16:37:18 +09003 * Copyright (C) 2003 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * ASID handling idea taken from MIPS implementation.
6 */
7#ifndef __ASM_SH_MMU_CONTEXT_H
8#define __ASM_SH_MMU_CONTEXT_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Paul Mundtcdcc9702007-11-09 16:37:18 +090010#ifdef __KERNEL__
Paul Mundtf15cbe62008-07-29 08:09:44 +090011#include <cpu/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/uaccess.h>
14#include <asm/io.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020015#include <asm-generic/mm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * The MMU "context" consists of two things:
19 * (a) TLB cache version (or round, cycle whatever expression you like)
20 * (b) ASID (Address Space IDentifier)
21 */
Paul Mundt8263a672009-03-17 17:49:49 +090022#ifdef CONFIG_CPU_HAS_PTEAEX
23#define MMU_CONTEXT_ASID_MASK 0x0000ffff
24#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define MMU_CONTEXT_ASID_MASK 0x000000ff
Paul Mundt8263a672009-03-17 17:49:49 +090026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Paul Mundt8263a672009-03-17 17:49:49 +090028#define MMU_CONTEXT_VERSION_MASK (~0UL & ~MMU_CONTEXT_ASID_MASK)
29#define MMU_CONTEXT_FIRST_VERSION (MMU_CONTEXT_ASID_MASK + 1)
30
31/* Impossible ASID value, to differentiate from NO_CONTEXT. */
32#define MMU_NO_ASID MMU_CONTEXT_FIRST_VERSION
33#define NO_CONTEXT 0UL
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mundtaec5e0e2006-12-25 09:51:47 +090035#define asid_cache(cpu) (cpu_data[cpu].asid_cache)
Paul Mundt761656e2008-07-28 18:39:25 +090036
37#ifdef CONFIG_MMU
Paul Mundtcdcc9702007-11-09 16:37:18 +090038#define cpu_context(cpu, mm) ((mm)->context.id[cpu])
39
40#define cpu_asid(cpu, mm) \
41 (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
Paul Mundtaec5e0e2006-12-25 09:51:47 +090042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Virtual Page Number mask
45 */
46#define MMU_VPN_MASK 0xfffff000
47
Paul Mundtcdcc9702007-11-09 16:37:18 +090048#if defined(CONFIG_SUPERH32)
49#include "mmu_context_32.h"
50#else
51#include "mmu_context_64.h"
52#endif
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Get MMU context if needed.
56 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090057static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Paul Mundtaec5e0e2006-12-25 09:51:47 +090059 unsigned long asid = asid_cache(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 /* Check if we have old version of context. */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090062 if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /* It's up to date, do nothing */
64 return;
65
66 /* It's old, we need to get new context with new version. */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090067 if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /*
69 * We exhaust ASID of this version.
70 * Flush all TLB and start new cycle.
71 */
Paul Mundt711e5222009-08-20 17:24:40 +090072 local_flush_tlb_all();
Stuart Menefy6e4662f2006-11-21 13:53:44 +090073
Paul Mundtcdcc9702007-11-09 16:37:18 +090074#ifdef CONFIG_SUPERH64
75 /*
76 * The SH-5 cache uses the ASIDs, requiring both the I and D
77 * cache to be flushed when the ASID is exhausted. Weak.
78 */
79 flush_cache_all();
80#endif
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /*
83 * Fix version; Note that we avoid version #0
84 * to distingush NO_CONTEXT.
85 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090086 if (!asid)
87 asid = MMU_CONTEXT_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Paul Mundtaec5e0e2006-12-25 09:51:47 +090089
90 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * Initialize the context related info for a new mm_struct
95 * instance.
96 */
Stuart Menefy6e4662f2006-11-21 13:53:44 +090097static inline int init_new_context(struct task_struct *tsk,
Paul Mundtaec5e0e2006-12-25 09:51:47 +090098 struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900100 int i;
101
102 for (i = 0; i < num_online_cpus(); i++)
103 cpu_context(i, mm) = NO_CONTEXT;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * After we have set current->mm to a new value, this activates
110 * the context for the new mm so we see the new mappings.
111 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900112static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900114 get_mmu_context(mm, cpu);
115 set_asid(cpu_asid(cpu, mm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900118static inline void switch_mm(struct mm_struct *prev,
119 struct mm_struct *next,
120 struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900122 unsigned int cpu = smp_processor_id();
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (likely(prev != next)) {
Rusty Russell74c86d62009-06-12 22:33:14 +0930125 cpumask_set_cpu(cpu, mm_cpumask(next));
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900126 set_TTB(next->pgd);
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900127 activate_context(next, cpu);
128 } else
Rusty Russell74c86d62009-06-12 22:33:14 +0930129 if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)))
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900130 activate_context(next, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Paul Mundt7b275822009-06-14 23:23:41 +0900132
133#define activate_mm(prev, next) switch_mm((prev),(next),NULL)
134#define deactivate_mm(tsk,mm) do { } while (0)
135#define enter_lazy_tlb(mm,tsk) do { } while (0)
136
Paul Mundtcdcc9702007-11-09 16:37:18 +0900137#else
Paul Mundt7b275822009-06-14 23:23:41 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define set_asid(asid) do { } while (0)
140#define get_asid() (0)
Paul Mundt35724a02008-12-10 18:17:19 +0900141#define cpu_asid(cpu, mm) ({ (void)cpu; NO_CONTEXT; })
Paul Mundtccd80582008-04-25 12:58:40 +0900142#define switch_and_save_asid(asid) (0)
Paul Mundt01066622007-03-28 16:38:13 +0900143#define set_TTB(pgd) do { } while (0)
144#define get_TTB() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Paul Mundt7b275822009-06-14 23:23:41 +0900146#include <asm-generic/mmu_context.h>
147
148#endif /* CONFIG_MMU */
Paul Mundtcdcc9702007-11-09 16:37:18 +0900149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
151/*
152 * If this processor has an MMU, we need methods to turn it off/on ..
153 * paging_init() will also have to be updated for the processor in
154 * question.
155 */
156static inline void enable_mmu(void)
157{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900158 unsigned int cpu = smp_processor_id();
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Enable MMU */
Paul Mundt9d56dd32010-01-26 12:58:40 +0900161 __raw_writel(MMU_CONTROL_INIT, MMUCR);
Paul Mundt29847622006-09-27 14:57:44 +0900162 ctrl_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900164 if (asid_cache(cpu) == NO_CONTEXT)
165 asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900167 set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static inline void disable_mmu(void)
171{
172 unsigned long cr;
173
Paul Mundt9d56dd32010-01-26 12:58:40 +0900174 cr = __raw_readl(MMUCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 cr &= ~MMU_CONTROL_INIT;
Paul Mundt9d56dd32010-01-26 12:58:40 +0900176 __raw_writel(cr, MMUCR);
Paul Mundt29847622006-09-27 14:57:44 +0900177
178 ctrl_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180#else
181/*
182 * MMU control handlers for processors lacking memory
183 * management hardware.
184 */
Paul Mundt01066622007-03-28 16:38:13 +0900185#define enable_mmu() do { } while (0)
186#define disable_mmu() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
188
189#endif /* __KERNEL__ */
190#endif /* __ASM_SH_MMU_CONTEXT_H */