blob: 8589a50febd0ff5166e63da243c78ffe54ed9c1f [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__
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/cpu/mmu_context.h>
12#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define MMU_CONTEXT_ASID_MASK 0x000000ff
23#define MMU_CONTEXT_VERSION_MASK 0xffffff00
24#define MMU_CONTEXT_FIRST_VERSION 0x00000100
25#define NO_CONTEXT 0
26
27/* ASID is 8-bit value, so it can't be 0x100 */
28#define MMU_NO_ASID 0x100
29
Paul Mundtaec5e0e2006-12-25 09:51:47 +090030#define asid_cache(cpu) (cpu_data[cpu].asid_cache)
Paul Mundt761656e2008-07-28 18:39:25 +090031
32#ifdef CONFIG_MMU
Paul Mundtcdcc9702007-11-09 16:37:18 +090033#define cpu_context(cpu, mm) ((mm)->context.id[cpu])
34
35#define cpu_asid(cpu, mm) \
36 (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK)
Paul Mundtaec5e0e2006-12-25 09:51:47 +090037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Virtual Page Number mask
40 */
41#define MMU_VPN_MASK 0xfffff000
42
Paul Mundtcdcc9702007-11-09 16:37:18 +090043#if defined(CONFIG_SUPERH32)
44#include "mmu_context_32.h"
45#else
46#include "mmu_context_64.h"
47#endif
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Get MMU context if needed.
51 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090052static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Paul Mundtaec5e0e2006-12-25 09:51:47 +090054 unsigned long asid = asid_cache(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /* Check if we have old version of context. */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090057 if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* It's up to date, do nothing */
59 return;
60
61 /* It's old, we need to get new context with new version. */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090062 if (!(++asid & MMU_CONTEXT_ASID_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /*
64 * We exhaust ASID of this version.
65 * Flush all TLB and start new cycle.
66 */
67 flush_tlb_all();
Stuart Menefy6e4662f2006-11-21 13:53:44 +090068
Paul Mundtcdcc9702007-11-09 16:37:18 +090069#ifdef CONFIG_SUPERH64
70 /*
71 * The SH-5 cache uses the ASIDs, requiring both the I and D
72 * cache to be flushed when the ASID is exhausted. Weak.
73 */
74 flush_cache_all();
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /*
78 * Fix version; Note that we avoid version #0
79 * to distingush NO_CONTEXT.
80 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +090081 if (!asid)
82 asid = MMU_CONTEXT_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Paul Mundtaec5e0e2006-12-25 09:51:47 +090084
85 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * Initialize the context related info for a new mm_struct
90 * instance.
91 */
Stuart Menefy6e4662f2006-11-21 13:53:44 +090092static inline int init_new_context(struct task_struct *tsk,
Paul Mundtaec5e0e2006-12-25 09:51:47 +090093 struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Paul Mundtaec5e0e2006-12-25 09:51:47 +090095 int i;
96
97 for (i = 0; i < num_online_cpus(); i++)
98 cpu_context(i, mm) = NO_CONTEXT;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * After we have set current->mm to a new value, this activates
105 * the context for the new mm so we see the new mappings.
106 */
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900107static inline void activate_context(struct mm_struct *mm, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900109 get_mmu_context(mm, cpu);
110 set_asid(cpu_asid(cpu, mm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900113static inline void switch_mm(struct mm_struct *prev,
114 struct mm_struct *next,
115 struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900117 unsigned int cpu = smp_processor_id();
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (likely(prev != next)) {
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900120 cpu_set(cpu, next->cpu_vm_mask);
Stuart Menefy6e4662f2006-11-21 13:53:44 +0900121 set_TTB(next->pgd);
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900122 activate_context(next, cpu);
123 } else
124 if (!cpu_test_and_set(cpu, next->cpu_vm_mask))
125 activate_context(next, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Paul Mundtcdcc9702007-11-09 16:37:18 +0900127#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define get_mmu_context(mm) do { } while (0)
129#define init_new_context(tsk,mm) (0)
130#define destroy_context(mm) do { } while (0)
131#define set_asid(asid) do { } while (0)
132#define get_asid() (0)
Paul Mundtccd80582008-04-25 12:58:40 +0900133#define cpu_asid(cpu, mm) ({ (void)cpu; 0; })
134#define switch_and_save_asid(asid) (0)
Paul Mundt01066622007-03-28 16:38:13 +0900135#define set_TTB(pgd) do { } while (0)
136#define get_TTB() (0)
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900137#define activate_context(mm,cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define switch_mm(prev,next,tsk) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif /* CONFIG_MMU */
140
Paul Mundtcdcc9702007-11-09 16:37:18 +0900141#define activate_mm(prev, next) switch_mm((prev),(next),NULL)
142#define deactivate_mm(tsk,mm) do { } while (0)
143#define enter_lazy_tlb(mm,tsk) do { } while (0)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
146/*
147 * If this processor has an MMU, we need methods to turn it off/on ..
148 * paging_init() will also have to be updated for the processor in
149 * question.
150 */
151static inline void enable_mmu(void)
152{
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900153 unsigned int cpu = smp_processor_id();
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* Enable MMU */
156 ctrl_outl(MMU_CONTROL_INIT, MMUCR);
Paul Mundt29847622006-09-27 14:57:44 +0900157 ctrl_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900159 if (asid_cache(cpu) == NO_CONTEXT)
160 asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900162 set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static inline void disable_mmu(void)
166{
167 unsigned long cr;
168
169 cr = ctrl_inl(MMUCR);
170 cr &= ~MMU_CONTROL_INIT;
171 ctrl_outl(cr, MMUCR);
Paul Mundt29847622006-09-27 14:57:44 +0900172
173 ctrl_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175#else
176/*
177 * MMU control handlers for processors lacking memory
178 * management hardware.
179 */
Paul Mundt01066622007-03-28 16:38:13 +0900180#define enable_mmu() do { } while (0)
181#define disable_mmu() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#endif
183
184#endif /* __KERNEL__ */
185#endif /* __ASM_SH_MMU_CONTEXT_H */