2014-11-06 15:19:50 +08:00
|
|
|
/*
|
|
|
|
* Nios2 TLB handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Wind River Systems Inc
|
|
|
|
* Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file "COPYING" in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
|
|
|
|
#include <asm/tlb.h>
|
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/cpuinfo.h>
|
|
|
|
|
|
|
|
#define TLB_INDEX_MASK \
|
|
|
|
((((1UL << (cpuinfo.tlb_ptr_sz - cpuinfo.tlb_num_ways_log2))) - 1) \
|
|
|
|
<< PAGE_SHIFT)
|
|
|
|
|
|
|
|
static void get_misc_and_pid(unsigned long *misc, unsigned long *pid)
|
|
|
|
{
|
|
|
|
*misc = RDCTL(CTL_TLBMISC);
|
|
|
|
*misc &= (TLBMISC_PID | TLBMISC_WAY);
|
|
|
|
*pid = *misc & TLBMISC_PID;
|
|
|
|
}
|
|
|
|
|
2018-11-05 10:35:12 +08:00
|
|
|
/*
|
|
|
|
* This provides a PTEADDR value for addr that will cause a TLB miss
|
|
|
|
* (fast TLB miss). TLB invalidation replaces entries with this value.
|
|
|
|
*/
|
|
|
|
static unsigned long pteaddr_invalid(unsigned long addr)
|
|
|
|
{
|
|
|
|
return ((addr | 0xC0000000UL) >> PAGE_SHIFT) << 2;
|
|
|
|
}
|
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
/*
|
|
|
|
* This one is only used for pages with the global bit set so we don't care
|
|
|
|
* much about the ASID.
|
|
|
|
*/
|
|
|
|
void flush_tlb_one_pid(unsigned long addr, unsigned long mmu_pid)
|
|
|
|
{
|
|
|
|
unsigned int way;
|
|
|
|
unsigned long org_misc, pid_misc;
|
|
|
|
|
|
|
|
pr_debug("Flush tlb-entry for vaddr=%#lx\n", addr);
|
|
|
|
|
|
|
|
/* remember pid/way until we return. */
|
|
|
|
get_misc_and_pid(&org_misc, &pid_misc);
|
|
|
|
|
|
|
|
WRCTL(CTL_PTEADDR, (addr >> PAGE_SHIFT) << 2);
|
|
|
|
|
|
|
|
for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
|
|
|
|
unsigned long pteaddr;
|
|
|
|
unsigned long tlbmisc;
|
|
|
|
unsigned long pid;
|
|
|
|
|
2018-11-12 23:03:35 +08:00
|
|
|
tlbmisc = TLBMISC_RD | (way << TLBMISC_WAY_SHIFT);
|
2014-11-06 15:19:50 +08:00
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
2018-11-16 07:04:05 +08:00
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
pteaddr = RDCTL(CTL_PTEADDR);
|
2018-11-16 07:04:05 +08:00
|
|
|
if (((pteaddr >> 2) & 0xfffff) != (addr >> PAGE_SHIFT))
|
|
|
|
continue;
|
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
tlbmisc = RDCTL(CTL_TLBMISC);
|
|
|
|
pid = (tlbmisc >> TLBMISC_PID_SHIFT) & TLBMISC_PID_MASK;
|
2018-11-16 07:04:05 +08:00
|
|
|
if (pid != mmu_pid)
|
|
|
|
continue;
|
2014-11-06 15:19:50 +08:00
|
|
|
|
2018-11-16 07:04:05 +08:00
|
|
|
tlbmisc = TLBMISC_WE | (way << TLBMISC_WAY_SHIFT);
|
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
|
|
|
WRCTL(CTL_PTEADDR, pteaddr_invalid(addr));
|
|
|
|
WRCTL(CTL_TLBACC, 0);
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WRCTL(CTL_TLBMISC, org_misc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long mmu_pid = get_pid_from_context(&vma->vm_mm->context);
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
flush_tlb_one_pid(start, mmu_pid);
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This one is only used for pages with the global bit set so we don't care
|
|
|
|
* much about the ASID.
|
|
|
|
*/
|
2018-11-05 10:00:15 +08:00
|
|
|
static void flush_tlb_one(unsigned long addr)
|
2014-11-06 15:19:50 +08:00
|
|
|
{
|
|
|
|
unsigned int way;
|
|
|
|
unsigned long org_misc, pid_misc;
|
|
|
|
|
|
|
|
pr_debug("Flush tlb-entry for vaddr=%#lx\n", addr);
|
|
|
|
|
|
|
|
/* remember pid/way until we return. */
|
|
|
|
get_misc_and_pid(&org_misc, &pid_misc);
|
|
|
|
|
|
|
|
WRCTL(CTL_PTEADDR, (addr >> PAGE_SHIFT) << 2);
|
|
|
|
|
|
|
|
for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
|
|
|
|
unsigned long pteaddr;
|
|
|
|
unsigned long tlbmisc;
|
|
|
|
|
2018-11-12 23:03:35 +08:00
|
|
|
tlbmisc = TLBMISC_RD | (way << TLBMISC_WAY_SHIFT);
|
2014-11-06 15:19:50 +08:00
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
2018-11-16 07:04:05 +08:00
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
pteaddr = RDCTL(CTL_PTEADDR);
|
2018-11-16 07:04:05 +08:00
|
|
|
if (((pteaddr >> 2) & 0xfffff) != (addr >> PAGE_SHIFT))
|
|
|
|
continue;
|
2014-11-06 15:19:50 +08:00
|
|
|
|
2018-11-16 07:04:05 +08:00
|
|
|
pr_debug("Flush entry by writing way=%dl pid=%ld\n",
|
|
|
|
way, (pid_misc >> TLBMISC_PID_SHIFT));
|
2014-11-06 15:19:50 +08:00
|
|
|
|
2018-11-16 07:04:05 +08:00
|
|
|
tlbmisc = TLBMISC_WE | (way << TLBMISC_WAY_SHIFT);
|
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
|
|
|
WRCTL(CTL_PTEADDR, pteaddr_invalid(addr));
|
|
|
|
WRCTL(CTL_TLBACC, 0);
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WRCTL(CTL_TLBMISC, org_misc);
|
|
|
|
}
|
|
|
|
|
2018-11-05 10:00:15 +08:00
|
|
|
void flush_tlb_kernel_range(unsigned long start, unsigned long end)
|
|
|
|
{
|
|
|
|
while (start < end) {
|
|
|
|
flush_tlb_one(start);
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
void dump_tlb_line(unsigned long line)
|
|
|
|
{
|
|
|
|
unsigned int way;
|
|
|
|
unsigned long org_misc;
|
|
|
|
|
|
|
|
pr_debug("dump tlb-entries for line=%#lx (addr %08lx)\n", line,
|
|
|
|
line << (PAGE_SHIFT + cpuinfo.tlb_num_ways_log2));
|
|
|
|
|
|
|
|
/* remember pid/way until we return */
|
|
|
|
org_misc = (RDCTL(CTL_TLBMISC) & (TLBMISC_PID | TLBMISC_WAY));
|
|
|
|
|
|
|
|
WRCTL(CTL_PTEADDR, line << 2);
|
|
|
|
|
|
|
|
for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
|
|
|
|
unsigned long pteaddr;
|
|
|
|
unsigned long tlbmisc;
|
|
|
|
unsigned long tlbacc;
|
|
|
|
|
|
|
|
WRCTL(CTL_TLBMISC, TLBMISC_RD | (way << TLBMISC_WAY_SHIFT));
|
|
|
|
pteaddr = RDCTL(CTL_PTEADDR);
|
|
|
|
tlbmisc = RDCTL(CTL_TLBMISC);
|
|
|
|
tlbacc = RDCTL(CTL_TLBACC);
|
|
|
|
|
2018-11-05 10:35:12 +08:00
|
|
|
if ((tlbacc << PAGE_SHIFT) != 0) {
|
2014-11-06 15:19:50 +08:00
|
|
|
pr_debug("-- way:%02x vpn:0x%08lx phys:0x%08lx pid:0x%02lx flags:%c%c%c%c%c\n",
|
|
|
|
way,
|
|
|
|
(pteaddr << (PAGE_SHIFT-2)),
|
|
|
|
(tlbacc << PAGE_SHIFT),
|
|
|
|
((tlbmisc >> TLBMISC_PID_SHIFT) &
|
|
|
|
TLBMISC_PID_MASK),
|
|
|
|
(tlbacc & _PAGE_READ ? 'r' : '-'),
|
|
|
|
(tlbacc & _PAGE_WRITE ? 'w' : '-'),
|
|
|
|
(tlbacc & _PAGE_EXEC ? 'x' : '-'),
|
|
|
|
(tlbacc & _PAGE_GLOBAL ? 'g' : '-'),
|
|
|
|
(tlbacc & _PAGE_CACHED ? 'c' : '-'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WRCTL(CTL_TLBMISC, org_misc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_tlb(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < cpuinfo.tlb_num_lines; i++)
|
|
|
|
dump_tlb_line(i);
|
|
|
|
}
|
|
|
|
|
2018-11-16 07:04:05 +08:00
|
|
|
void flush_tlb_pid(unsigned long mmu_pid)
|
2014-11-06 15:19:50 +08:00
|
|
|
{
|
2018-11-05 10:35:12 +08:00
|
|
|
unsigned long addr = 0;
|
2014-11-06 15:19:50 +08:00
|
|
|
unsigned int line;
|
|
|
|
unsigned int way;
|
|
|
|
unsigned long org_misc, pid_misc;
|
|
|
|
|
|
|
|
/* remember pid/way until we return */
|
|
|
|
get_misc_and_pid(&org_misc, &pid_misc);
|
|
|
|
|
|
|
|
for (line = 0; line < cpuinfo.tlb_num_lines; line++) {
|
2018-11-05 10:35:12 +08:00
|
|
|
WRCTL(CTL_PTEADDR, pteaddr_invalid(addr));
|
2014-11-06 15:19:50 +08:00
|
|
|
|
|
|
|
for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
|
|
|
|
unsigned long tlbmisc;
|
2018-11-16 07:04:05 +08:00
|
|
|
unsigned long pid;
|
2014-11-06 15:19:50 +08:00
|
|
|
|
2018-11-12 23:03:35 +08:00
|
|
|
tlbmisc = TLBMISC_RD | (way << TLBMISC_WAY_SHIFT);
|
2014-11-06 15:19:50 +08:00
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
|
|
|
tlbmisc = RDCTL(CTL_TLBMISC);
|
2018-11-16 07:04:05 +08:00
|
|
|
pid = (tlbmisc >> TLBMISC_PID_SHIFT) & TLBMISC_PID_MASK;
|
|
|
|
if (pid != mmu_pid)
|
|
|
|
continue;
|
2014-11-06 15:19:50 +08:00
|
|
|
|
2018-11-16 07:04:05 +08:00
|
|
|
tlbmisc = TLBMISC_WE | (way << TLBMISC_WAY_SHIFT);
|
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
|
|
|
WRCTL(CTL_TLBACC, 0);
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 10:35:12 +08:00
|
|
|
addr += PAGE_SIZE;
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
2018-11-05 10:47:32 +08:00
|
|
|
|
|
|
|
WRCTL(CTL_TLBMISC, org_misc);
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 10:51:44 +08:00
|
|
|
/*
|
|
|
|
* All entries common to a mm share an asid. To effectively flush these
|
|
|
|
* entries, we just bump the asid.
|
|
|
|
*/
|
|
|
|
void flush_tlb_mm(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
if (current->mm == mm) {
|
|
|
|
unsigned long mmu_pid = get_pid_from_context(&mm->context);
|
|
|
|
flush_tlb_pid(mmu_pid);
|
|
|
|
} else {
|
|
|
|
memset(&mm->context, 0, sizeof(mm_context_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
void flush_tlb_all(void)
|
|
|
|
{
|
2018-11-05 10:35:12 +08:00
|
|
|
unsigned long addr = 0;
|
|
|
|
unsigned int line;
|
2014-11-06 15:19:50 +08:00
|
|
|
unsigned int way;
|
2018-11-07 10:21:02 +08:00
|
|
|
unsigned long org_misc, pid_misc;
|
2014-11-06 15:19:50 +08:00
|
|
|
|
|
|
|
/* remember pid/way until we return */
|
|
|
|
get_misc_and_pid(&org_misc, &pid_misc);
|
|
|
|
|
2018-11-07 10:21:02 +08:00
|
|
|
/* Start at way 0, way is auto-incremented after each TLBACC write */
|
|
|
|
WRCTL(CTL_TLBMISC, TLBMISC_WE);
|
|
|
|
|
2014-11-06 15:19:50 +08:00
|
|
|
/* Map each TLB entry to physcal address 0 with no-access and a
|
|
|
|
bad ptbase */
|
2018-11-05 10:35:12 +08:00
|
|
|
for (line = 0; line < cpuinfo.tlb_num_lines; line++) {
|
|
|
|
WRCTL(CTL_PTEADDR, pteaddr_invalid(addr));
|
2018-11-07 10:21:02 +08:00
|
|
|
for (way = 0; way < cpuinfo.tlb_num_ways; way++)
|
2018-11-05 10:35:12 +08:00
|
|
|
WRCTL(CTL_TLBACC, 0);
|
|
|
|
|
|
|
|
addr += PAGE_SIZE;
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* restore pid/way */
|
|
|
|
WRCTL(CTL_TLBMISC, org_misc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_mmu_pid(unsigned long pid)
|
|
|
|
{
|
2018-11-16 07:04:05 +08:00
|
|
|
unsigned long tlbmisc;
|
|
|
|
|
|
|
|
tlbmisc = RDCTL(CTL_TLBMISC);
|
|
|
|
tlbmisc = (tlbmisc & TLBMISC_WAY);
|
|
|
|
tlbmisc |= (pid & TLBMISC_PID_MASK) << TLBMISC_PID_SHIFT;
|
|
|
|
WRCTL(CTL_TLBMISC, tlbmisc);
|
2014-11-06 15:19:50 +08:00
|
|
|
}
|