tlb.c 8.3 KB
Newer Older
H
Haavard Skinnemoen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * AVR32 TLB operations
 *
 * Copyright (C) 2004-2006 Atmel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/mm.h>

#include <asm/mmu_context.h>

14 15
/* TODO: Get the correct number from the CONFIG1 system register */
#define NR_TLB_ENTRIES 32
H
Haavard Skinnemoen 已提交
16

17
static void show_dtlb_entry(unsigned int index)
H
Haavard Skinnemoen 已提交
18
{
19
	u32 tlbehi, tlbehi_save, tlbelo, mmucr, mmucr_save;
20
	unsigned long flags;
H
Haavard Skinnemoen 已提交
21 22 23 24

	local_irq_save(flags);
	mmucr_save = sysreg_read(MMUCR);
	tlbehi_save = sysreg_read(TLBEHI);
25
	mmucr = SYSREG_BFINS(DRP, index, mmucr_save);
H
Haavard Skinnemoen 已提交
26 27
	sysreg_write(MMUCR, mmucr);

28
	__builtin_tlbr();
H
Haavard Skinnemoen 已提交
29 30 31 32 33 34 35
	cpu_sync_pipeline();

	tlbehi = sysreg_read(TLBEHI);
	tlbelo = sysreg_read(TLBELO);

	printk("%2u: %c %c %02x   %05x %05x %o  %o  %c %c %c %c\n",
	       index,
36 37 38 39 40 41 42 43 44 45 46
	       SYSREG_BFEXT(TLBEHI_V, tlbehi) ? '1' : '0',
	       SYSREG_BFEXT(G, tlbelo) ? '1' : '0',
	       SYSREG_BFEXT(ASID, tlbehi),
	       SYSREG_BFEXT(VPN, tlbehi) >> 2,
	       SYSREG_BFEXT(PFN, tlbelo) >> 2,
	       SYSREG_BFEXT(AP, tlbelo),
	       SYSREG_BFEXT(SZ, tlbelo),
	       SYSREG_BFEXT(TLBELO_C, tlbelo) ? 'C' : ' ',
	       SYSREG_BFEXT(B, tlbelo) ? 'B' : ' ',
	       SYSREG_BFEXT(W, tlbelo) ? 'W' : ' ',
	       SYSREG_BFEXT(TLBELO_D, tlbelo) ? 'D' : ' ');
H
Haavard Skinnemoen 已提交
47 48 49 50 51 52 53 54 55 56 57 58

	sysreg_write(MMUCR, mmucr_save);
	sysreg_write(TLBEHI, tlbehi_save);
	cpu_sync_pipeline();
	local_irq_restore(flags);
}

void dump_dtlb(void)
{
	unsigned int i;

	printk("ID  V G ASID VPN   PFN   AP SZ C B W D\n");
59
	for (i = 0; i < NR_TLB_ENTRIES; i++)
H
Haavard Skinnemoen 已提交
60 61 62
		show_dtlb_entry(i);
}

63
static void update_dtlb(unsigned long address, pte_t pte)
H
Haavard Skinnemoen 已提交
64
{
65 66
	u32 tlbehi;
	u32 mmucr;
H
Haavard Skinnemoen 已提交
67

68 69 70 71 72 73 74 75 76
	/*
	 * We're not changing the ASID here, so no need to flush the
	 * pipeline.
	 */
	tlbehi = sysreg_read(TLBEHI);
	tlbehi = SYSREG_BF(ASID, SYSREG_BFEXT(ASID, tlbehi));
	tlbehi |= address & MMU_VPN_MASK;
	tlbehi |= SYSREG_BIT(TLBEHI_V);
	sysreg_write(TLBEHI, tlbehi);
H
Haavard Skinnemoen 已提交
77 78

	/* Does this mapping already exist? */
79 80
	__builtin_tlbs();
	mmucr = sysreg_read(MMUCR);
H
Haavard Skinnemoen 已提交
81 82 83

	if (mmucr & SYSREG_BIT(MMUCR_N)) {
		/* Not found -- pick a not-recently-accessed entry */
84 85
		unsigned int rp;
		u32 tlbar = sysreg_read(TLBARLO);
H
Haavard Skinnemoen 已提交
86 87 88 89 90 91 92

		rp = 32 - fls(tlbar);
		if (rp == 32) {
			rp = 0;
			sysreg_write(TLBARLO, -1L);
		}

93
		mmucr = SYSREG_BFINS(DRP, rp, mmucr);
H
Haavard Skinnemoen 已提交
94 95 96 97 98 99
		sysreg_write(MMUCR, mmucr);
	}

	sysreg_write(TLBELO, pte_val(pte) & _PAGE_FLAGS_HARDWARE_MASK);

	/* Let's go */
100
	__builtin_tlbw();
H
Haavard Skinnemoen 已提交
101 102 103
}

void update_mmu_cache(struct vm_area_struct *vma,
104
		      unsigned long address, pte_t *ptep)
H
Haavard Skinnemoen 已提交
105 106 107 108 109 110 111 112
{
	unsigned long flags;

	/* ptrace may call this routine */
	if (vma && current->active_mm != vma->vm_mm)
		return;

	local_irq_save(flags);
113
	update_dtlb(address, *ptep);
H
Haavard Skinnemoen 已提交
114 115 116
	local_irq_restore(flags);
}

117
static void __flush_tlb_page(unsigned long asid, unsigned long page)
H
Haavard Skinnemoen 已提交
118
{
119
	u32 mmucr, tlbehi;
H
Haavard Skinnemoen 已提交
120

121 122 123 124 125 126 127 128 129
	/*
	 * Caller is responsible for masking out non-PFN bits in page
	 * and changing the current ASID if necessary. This means that
	 * we don't need to flush the pipeline after writing TLBEHI.
	 */
	tlbehi = page | asid;
	sysreg_write(TLBEHI, tlbehi);

	__builtin_tlbs();
H
Haavard Skinnemoen 已提交
130 131 132
	mmucr = sysreg_read(MMUCR);

	if (!(mmucr & SYSREG_BIT(MMUCR_N))) {
133 134
		unsigned int entry;
		u32 tlbarlo;
H
Haavard Skinnemoen 已提交
135 136 137 138 139

		/* Clear the "valid" bit */
		sysreg_write(TLBEHI, tlbehi);

		/* mark the entry as "not accessed" */
140
		entry = SYSREG_BFEXT(DRP, mmucr);
H
Haavard Skinnemoen 已提交
141
		tlbarlo = sysreg_read(TLBARLO);
142
		tlbarlo |= (0x80000000UL >> entry);
H
Haavard Skinnemoen 已提交
143 144 145
		sysreg_write(TLBARLO, tlbarlo);

		/* update the entry with valid bit clear */
146
		__builtin_tlbw();
H
Haavard Skinnemoen 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	}
}

void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
{
	if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
		unsigned long flags, asid;
		unsigned long saved_asid = MMU_NO_ASID;

		asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
		page &= PAGE_MASK;

		local_irq_save(flags);
		if (vma->vm_mm != current->mm) {
			saved_asid = get_asid();
			set_asid(asid);
		}

		__flush_tlb_page(asid, page);

		if (saved_asid != MMU_NO_ASID)
			set_asid(saved_asid);
		local_irq_restore(flags);
	}
}

void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
		     unsigned long end)
{
	struct mm_struct *mm = vma->vm_mm;

	if (mm->context != NO_CONTEXT) {
		unsigned long flags;
		int size;

		local_irq_save(flags);
		size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
184

H
Haavard Skinnemoen 已提交
185 186 187 188 189
		if (size > (MMU_DTLB_ENTRIES / 4)) { /* Too many entries to flush */
			mm->context = NO_CONTEXT;
			if (mm == current->mm)
				activate_context(mm);
		} else {
190 191 192 193 194
			unsigned long asid;
			unsigned long saved_asid;

			asid = mm->context & MMU_CONTEXT_ASID_MASK;
			saved_asid = MMU_NO_ASID;
H
Haavard Skinnemoen 已提交
195 196 197 198

			start &= PAGE_MASK;
			end += (PAGE_SIZE - 1);
			end &= PAGE_MASK;
199

H
Haavard Skinnemoen 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
			if (mm != current->mm) {
				saved_asid = get_asid();
				set_asid(asid);
			}

			while (start < end) {
				__flush_tlb_page(asid, start);
				start += PAGE_SIZE;
			}
			if (saved_asid != MMU_NO_ASID)
				set_asid(saved_asid);
		}
		local_irq_restore(flags);
	}
}

/*
217 218 219
 * This function depends on the pages to be flushed having the G
 * (global) bit set in their pte. This is true for all
 * PAGE_KERNEL(_RO) pages.
H
Haavard Skinnemoen 已提交
220 221 222 223 224 225 226 227 228 229
 */
void flush_tlb_kernel_range(unsigned long start, unsigned long end)
{
	unsigned long flags;
	int size;

	size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
	if (size > (MMU_DTLB_ENTRIES / 4)) { /* Too many entries to flush */
		flush_tlb_all();
	} else {
230 231 232 233
		unsigned long asid;

		local_irq_save(flags);
		asid = get_asid();
H
Haavard Skinnemoen 已提交
234 235 236 237

		start &= PAGE_MASK;
		end += (PAGE_SIZE - 1);
		end &= PAGE_MASK;
238

H
Haavard Skinnemoen 已提交
239 240 241 242
		while (start < end) {
			__flush_tlb_page(asid, start);
			start += PAGE_SIZE;
		}
243
		local_irq_restore(flags);
H
Haavard Skinnemoen 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	}
}

void flush_tlb_mm(struct mm_struct *mm)
{
	/* Invalidate all TLB entries of this process by getting a new ASID */
	if (mm->context != NO_CONTEXT) {
		unsigned long flags;

		local_irq_save(flags);
		mm->context = NO_CONTEXT;
		if (mm == current->mm)
			activate_context(mm);
		local_irq_restore(flags);
	}
}

void flush_tlb_all(void)
{
	unsigned long flags;

	local_irq_save(flags);
	sysreg_write(MMUCR, sysreg_read(MMUCR) | SYSREG_BIT(MMUCR_I));
	local_irq_restore(flags);
}

#ifdef CONFIG_PROC_FS

#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/init.h>

static void *tlb_start(struct seq_file *tlb, loff_t *pos)
{
	static unsigned long tlb_index;

280
	if (*pos >= NR_TLB_ENTRIES)
H
Haavard Skinnemoen 已提交
281 282 283 284 285 286 287 288 289 290
		return NULL;

	tlb_index = 0;
	return &tlb_index;
}

static void *tlb_next(struct seq_file *tlb, void *v, loff_t *pos)
{
	unsigned long *index = v;

291
	if (*index >= NR_TLB_ENTRIES - 1)
H
Haavard Skinnemoen 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305
		return NULL;

	++*pos;
	++*index;
	return index;
}

static void tlb_stop(struct seq_file *tlb, void *v)
{

}

static int tlb_show(struct seq_file *tlb, void *v)
{
306 307
	unsigned int tlbehi, tlbehi_save, tlbelo, mmucr, mmucr_save;
	unsigned long flags;
H
Haavard Skinnemoen 已提交
308 309 310 311 312
	unsigned long *index = v;

	if (*index == 0)
		seq_puts(tlb, "ID  V G ASID VPN   PFN   AP SZ C B W D\n");

313
	BUG_ON(*index >= NR_TLB_ENTRIES);
H
Haavard Skinnemoen 已提交
314 315 316 317

	local_irq_save(flags);
	mmucr_save = sysreg_read(MMUCR);
	tlbehi_save = sysreg_read(TLBEHI);
318
	mmucr = SYSREG_BFINS(DRP, *index, mmucr_save);
H
Haavard Skinnemoen 已提交
319 320
	sysreg_write(MMUCR, mmucr);

321 322
	/* TLBR might change the ASID */
	__builtin_tlbr();
H
Haavard Skinnemoen 已提交
323 324 325 326 327 328 329 330 331 332 333
	cpu_sync_pipeline();

	tlbehi = sysreg_read(TLBEHI);
	tlbelo = sysreg_read(TLBELO);

	sysreg_write(MMUCR, mmucr_save);
	sysreg_write(TLBEHI, tlbehi_save);
	cpu_sync_pipeline();
	local_irq_restore(flags);

	seq_printf(tlb, "%2lu: %c %c %02x   %05x %05x %o  %o  %c %c %c %c\n",
334 335 336 337 338 339 340 341 342 343 344 345
		   *index,
		   SYSREG_BFEXT(TLBEHI_V, tlbehi) ? '1' : '0',
		   SYSREG_BFEXT(G, tlbelo) ? '1' : '0',
		   SYSREG_BFEXT(ASID, tlbehi),
		   SYSREG_BFEXT(VPN, tlbehi) >> 2,
		   SYSREG_BFEXT(PFN, tlbelo) >> 2,
		   SYSREG_BFEXT(AP, tlbelo),
		   SYSREG_BFEXT(SZ, tlbelo),
		   SYSREG_BFEXT(TLBELO_C, tlbelo) ? '1' : '0',
		   SYSREG_BFEXT(B, tlbelo) ? '1' : '0',
		   SYSREG_BFEXT(W, tlbelo) ? '1' : '0',
		   SYSREG_BFEXT(TLBELO_D, tlbelo) ? '1' : '0');
H
Haavard Skinnemoen 已提交
346 347 348 349

	return 0;
}

350
static const struct seq_operations tlb_ops = {
H
Haavard Skinnemoen 已提交
351 352 353 354 355 356 357 358 359 360 361
	.start		= tlb_start,
	.next		= tlb_next,
	.stop		= tlb_stop,
	.show		= tlb_show,
};

static int tlb_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &tlb_ops);
}

362
static const struct file_operations proc_tlb_operations = {
H
Haavard Skinnemoen 已提交
363 364 365 366 367 368 369 370
	.open		= tlb_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};

static int __init proctlb_init(void)
{
371
	proc_create("tlb", 0, NULL, &proc_tlb_operations);
H
Haavard Skinnemoen 已提交
372 373 374 375
	return 0;
}
late_initcall(proctlb_init);
#endif /* CONFIG_PROC_FS */