linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vineet Gupta <Vineet.Gupta1@synopsys.com>
To: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: arnd@arndb.de, Vineet Gupta <Vineet.Gupta1@synopsys.com>
Subject: [PATCH v3 25/71] ARC: MMU Context Management
Date: Thu, 24 Jan 2013 16:35:58 +0530	[thread overview]
Message-ID: <1359025589-22277-15-git-send-email-vgupta@synopsys.com> (raw)
In-Reply-To: <1359025589-22277-1-git-send-email-vgupta@synopsys.com>

ARC700 MMU provides for tagging TLB entries with a 8-bit ASID to avoid
having to flush the TLB every task switch.

It also allows for a quick way to invalidate all the TLB entries for
task useful for:
* COW sementics during fork()
* task exit()ing

Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
---
 arch/arc/include/asm/arcregs.h     |    7 ++
 arch/arc/include/asm/mmu.h         |   23 ++++
 arch/arc/include/asm/mmu_context.h |  209 ++++++++++++++++++++++++++++++++++++
 arch/arc/mm/tlb.c                  |   23 ++++
 4 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 arch/arc/include/asm/mmu.h
 create mode 100644 arch/arc/include/asm/mmu_context.h
 create mode 100644 arch/arc/mm/tlb.c

diff --git a/arch/arc/include/asm/arcregs.h b/arch/arc/include/asm/arcregs.h
index c6e2805..c12eb9b 100644
--- a/arch/arc/include/asm/arcregs.h
+++ b/arch/arc/include/asm/arcregs.h
@@ -85,6 +85,13 @@
 #define DC_CTRL_INV_MODE_FLUSH  0x40
 #define DC_CTRL_FLUSH_STATUS    0x100
 
+/* MMU Management regs */
+#define ARC_REG_PID		0x409
+#define ARC_REG_SCRATCH_DATA0	0x418
+
+/* Bits in MMU PID register */
+#define MMU_ENABLE		(1 << 31)	/* Enable MMU for process */
+
 /*
  * Floating Pt Registers
  * Status regs are read-only (build-time) so need not be saved/restored
diff --git a/arch/arc/include/asm/mmu.h b/arch/arc/include/asm/mmu.h
new file mode 100644
index 0000000..56b0232
--- /dev/null
+++ b/arch/arc/include/asm/mmu.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
+ *
+ * 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.
+ */
+
+#ifndef _ASM_ARC_MMU_H
+#define _ASM_ARC_MMU_H
+
+#ifndef __ASSEMBLY__
+
+typedef struct {
+	unsigned long asid;	/* Pvt Addr-Space ID for mm */
+#ifdef CONFIG_ARC_TLB_DBG
+	struct task_struct *tsk;
+#endif
+} mm_context_t;
+
+#endif
+
+#endif
diff --git a/arch/arc/include/asm/mmu_context.h b/arch/arc/include/asm/mmu_context.h
new file mode 100644
index 0000000..d12f3de
--- /dev/null
+++ b/arch/arc/include/asm/mmu_context.h
@@ -0,0 +1,209 @@
+/*
+ * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
+ *
+ * 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.
+ *
+ * vineetg: May 2011
+ *  -Refactored get_new_mmu_context( ) to only handle live-mm.
+ *   retiring-mm handled in other hooks
+ *
+ * Vineetg: March 25th, 2008: Bug #92690
+ *  -Major rewrite of Core ASID allocation routine get_new_mmu_context
+ *
+ * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
+ */
+
+#ifndef _ASM_ARC_MMU_CONTEXT_H
+#define _ASM_ARC_MMU_CONTEXT_H
+
+#include <asm/arcregs.h>
+#include <asm/tlb.h>
+
+#include <asm-generic/mm_hooks.h>
+
+/*		ARC700 ASID Management
+ *
+ * ARC MMU provides 8-bit ASID (0..255) to TAG TLB entries, allowing entries
+ * with same vaddr (different tasks) to co-exit. This provides for
+ * "Fast Context Switch" i.e. no TLB flush on ctxt-switch
+ *
+ * Linux assigns each task a unique ASID. A simple round-robin allocation
+ * of H/w ASID is done using software tracker @asid_cache.
+ * When it reaches max 255, the allocation cycle starts afresh by flushing
+ * the entire TLB and wrapping ASID back to zero.
+ *
+ * For book-keeping, Linux uses a couple of data-structures:
+ *  -mm_struct has an @asid field to keep a note of task's ASID (needed at the
+ *   time of say switch_mm( )
+ *  -An array of mm structs @asid_mm_map[] for asid->mm the reverse mapping,
+ *  given an ASID, finding the mm struct associated.
+ *
+ * The round-robin allocation algorithm allows for ASID stealing.
+ * If asid tracker is at "x-1", a new req will allocate "x", even if "x" was
+ * already assigned to another (switched-out) task. Obviously the prev owner
+ * is marked with an invalid ASID to make it request for a new ASID when it
+ * gets scheduled next time. However its TLB entries (with ASID "x") could
+ * exist, which must be cleared before the same ASID is used by the new owner.
+ * Flushing them would be plausible but costly solution. Instead we force a
+ * allocation policy quirk, which ensures that a stolen ASID won't have any
+ * TLB entries associates, alleviating the need to flush.
+ * The quirk essentially is not allowing ASID allocated in prev cycle
+ * to be used past a roll-over in the next cycle.
+ * When this happens (i.e. task ASID > asid tracker), task needs to refresh
+ * its ASID, aligning it to current value of tracker. If the task doesn't get
+ * scheduled past a roll-over, hence its ASID is not yet realigned with
+ * tracker, such ASID is anyways safely reusable because it is
+ * gauranteed that TLB entries with that ASID wont exist.
+ */
+
+#define FIRST_ASID  0
+#define MAX_ASID    255			/* 8 bit PID field in PID Aux reg */
+#define NO_ASID     (MAX_ASID + 1)	/* ASID Not alloc to mmu ctxt */
+#define NUM_ASID    ((MAX_ASID - FIRST_ASID) + 1)
+
+/* ASID to mm struct mapping */
+extern struct mm_struct *asid_mm_map[NUM_ASID + 1];
+
+extern int asid_cache;
+
+/*
+ * Assign a new ASID to task. If the task already has an ASID, it is
+ * relinquished.
+ */
+static inline void get_new_mmu_context(struct mm_struct *mm)
+{
+	struct mm_struct *prev_owner;
+	unsigned long flags;
+
+	local_irq_save(flags);
+
+	/*
+	 * Relinquish the currently owned ASID (if any).
+	 * Doing unconditionally saves a cmp-n-branch; for already unused
+	 * ASID slot, the value was/remains NULL
+	 */
+	asid_mm_map[mm->context.asid] = (struct mm_struct *)NULL;
+
+	/* move to new ASID */
+	if (++asid_cache > MAX_ASID) {	/* ASID roll-over */
+		asid_cache = FIRST_ASID;
+		flush_tlb_all();
+	}
+
+	/*
+	 * Is next ASID already owned by some-one else (we are stealing it).
+	 * If so, let the orig owner be aware of this, so when it runs, it
+	 * asks for a brand new ASID. This would only happen for a long-lived
+	 * task with ASID from prev allocation cycle (before ASID roll-over).
+	 *
+	 * This might look wrong - if we are re-using some other task's ASID,
+	 * won't we use it's stale TLB entries too. Actually switch_mm( ) takes
+	 * care of such a case: it ensures that task with ASID from prev alloc
+	 * cycle, when scheduled will refresh it's ASID: see switch_mm( ) below
+	 * The stealing scenario described here will only happen if that task
+	 * didn't get a chance to refresh it's ASID - implying stale entries
+	 * won't exist.
+	 */
+	prev_owner = asid_mm_map[asid_cache];
+	if (prev_owner)
+		prev_owner->context.asid = NO_ASID;
+
+	/* Assign new ASID to tsk */
+	asid_mm_map[asid_cache] = mm;
+	mm->context.asid = asid_cache;
+
+#ifdef CONFIG_ARC_TLB_DBG
+	pr_info("ARC_TLB_DBG: NewMM=0x%x OldMM=0x%x task_struct=0x%x Task: %s,"
+	       " pid:%u, assigned asid:%lu\n",
+	       (unsigned int)mm, (unsigned int)prev_owner,
+	       (unsigned int)(mm->context.tsk), (mm->context.tsk)->comm,
+	       (mm->context.tsk)->pid, mm->context.asid);
+#endif
+
+	write_aux_reg(ARC_REG_PID, asid_cache | MMU_ENABLE);
+
+	local_irq_restore(flags);
+}
+
+/*
+ * Initialize the context related info for a new mm_struct
+ * instance.
+ */
+static inline int
+init_new_context(struct task_struct *tsk, struct mm_struct *mm)
+{
+	mm->context.asid = NO_ASID;
+#ifdef CONFIG_ARC_TLB_DBG
+	mm->context.tsk = tsk;
+#endif
+	return 0;
+}
+
+/* Prepare the MMU for task: setup PID reg with allocated ASID
+    If task doesn't have an ASID (never alloc or stolen, get a new ASID)
+*/
+static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
+			     struct task_struct *tsk)
+{
+	/* PGD cached in MMU reg to avoid 3 mem lookups: task->mm->pgd */
+	write_aux_reg(ARC_REG_SCRATCH_DATA0, next->pgd);
+
+	/*
+	 * Get a new ASID if task doesn't have a valid one. Possible when
+	 *  -task never had an ASID (fresh after fork)
+	 *  -it's ASID was stolen - past an ASID roll-over.
+	 *  -There's a third obscure scenario (if this task is running for the
+	 *   first time afer an ASID rollover), where despite having a valid
+	 *   ASID, we force a get for new ASID - see comments at top.
+	 *
+	 * Both the non-alloc scenario and first-use-after-rollover can be
+	 * detected using the single condition below:  NO_ASID = 256
+	 * while asid_cache is always a valid ASID value (0-255).
+	 */
+	if (next->context.asid > asid_cache) {
+		get_new_mmu_context(next);
+	} else {
+		/*
+		 * XXX: This will never happen given the chks above
+		 * BUG_ON(next->context.asid > MAX_ASID);
+		 */
+		write_aux_reg(ARC_REG_PID, next->context.asid | MMU_ENABLE);
+	}
+
+}
+
+static inline void destroy_context(struct mm_struct *mm)
+{
+	unsigned long flags;
+
+	local_irq_save(flags);
+
+	asid_mm_map[mm->context.asid] = NULL;
+	mm->context.asid = NO_ASID;
+
+	local_irq_restore(flags);
+}
+
+/* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping
+ * for retiring-mm. However destroy_context( ) still needs to do that because
+ * between mm_release( ) = >deactive_mm( ) and
+ * mmput => .. => __mmdrop( ) => destroy_context( )
+ * there is a good chance that task gets sched-out/in, making it's ASID valid
+ * again (this teased me for a whole day).
+ */
+#define deactivate_mm(tsk, mm)   do { } while (0)
+
+static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
+{
+	write_aux_reg(ARC_REG_SCRATCH_DATA0, next->pgd);
+
+	/* Unconditionally get a new ASID */
+	get_new_mmu_context(next);
+
+}
+
+#define enter_lazy_tlb(mm, tsk)
+
+#endif /* __ASM_ARC_MMU_CONTEXT_H */
diff --git a/arch/arc/mm/tlb.c b/arch/arc/mm/tlb.c
new file mode 100644
index 0000000..f1edae2
--- /dev/null
+++ b/arch/arc/mm/tlb.c
@@ -0,0 +1,23 @@
+/*
+ * TLB Management (flush/create/diagnostics) for ARC700
+ *
+ * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
+ *
+ * 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/module.h>
+#include <asm/arcregs.h>
+#include <asm/mmu_context.h>
+#include <asm/tlb.h>
+
+/* A copy of the ASID from the PID reg is kept in asid_cache */
+int asid_cache = FIRST_ASID;
+
+/* ASID to mm struct mapping. We have one extra entry corresponding to
+ * NO_ASID to save us a compare when clearing the mm entry for old asid
+ * see get_new_mmu_context (asm-arc/mmu_context.h)
+ */
+struct mm_struct *asid_mm_map[NUM_ASID + 1];
-- 
1.7.4.1

  parent reply	other threads:[~2013-01-24 11:05 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-24 11:05 [PATCH v3 00/71] Synopsys ARC Linux kernel Port (Part #2) Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 03/71] ARC: irqflags - Interrupt enabling/disabling at in-core intc Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 04/71] ARC: Atomic/bitops/cmpxchg/barriers Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 05/71] asm-generic headers: uaccess.h to conditionally define segment_eq() Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 07/71] asm-generic: uaccess: Allow arches to over-ride __{get,put}_user_fn() Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 08/71] ARC: [optim] uaccess __{get,put}_user() optimised Vineet Gupta
2013-01-24 11:05   ` Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 09/71] asm-generic headers: Allow yet more arch overrides in checksum.h Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 10/71] ARC: Checksum/byteorder/swab routines Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 12/71] ARC: Spinlock/rwlock/mutex primitives Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 13/71] ARC: String library Vineet Gupta
2013-01-24 11:05   ` Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 14/71] ARC: Low level IRQ/Trap/Exception Handling Vineet Gupta
2013-01-28  7:44   ` Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 15/71] ARC: Interrupt Handling Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 16/71] ARC: Non-MMU Exception Handling Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 24/71] ARC: Page Table Management Vineet Gupta
2013-01-24 11:05 ` Vineet Gupta [this message]
2013-01-24 11:05   ` [PATCH v3 25/71] ARC: MMU Context Management Vineet Gupta
2013-01-24 11:05 ` [PATCH v3 26/71] ARC: MMU Exception Handling Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 27/71] ARC: TLB flush Handling Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 28/71] ARC: Page Fault handling Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 31/71] ARC: [plat-arcfpga] Static platform device for CONFIG_SERIAL_ARC Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 35/71] ARC: Last bits (stubs) to get to a running kernel with UART Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 36/71] ARC: [plat-arcfpga] defconfig Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 37/71] ARC: [optim] Cache "current" in Register r25 Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 38/71] ARC: ptrace support Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 39/71] ARC: Futex support Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 40/71] ARC: OProfile support Vineet Gupta
2013-01-29 17:05   ` James Hogan
2013-01-30  6:34     ` Vineet Gupta
2013-01-30 10:54       ` James Hogan
2013-01-30 11:46         ` Vineet Gupta
2013-01-30 11:46           ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 41/71] ARC: Support for high priority interrupts in the in-core intc Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 43/71] ARC: Diagnostics: show_regs() etc Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 46/71] ARC: stacktracing APIs based on dw2 unwinder Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 47/71] ARC: disassembly (needed by kprobes/kgdb/unaligned-access-emul) Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 49/71] sysctl: Enable PARISC "unaligned-trap" to be used cross-arch Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 50/71] ARC: Unaligned access emulation Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 51/71] ARC: kgdb support Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 52/71] ARC: Boot #2: Verbose Boot reporting / feature verification Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 53/71] ARC: [plat-arfpga] BVCI Latency Unit setup Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 54/71] perf, ARC: Enable building perf tools for ARC Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 55/71] ARC: perf support (software counters only) Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 56/71] ARC: Support for single cycle Close Coupled Mem (CCM) Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 60/71] ARC: [Review] Multi-platform image #1: Kconfig enablement Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 61/71] ARC: Fold boards sub-menu into platform/SoC menu Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 63/71] ARC: [Review] Multi-platform image #3: switch to board callback Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 64/71] ARC: [Review] Multi-platform image #4: Isolate platform headers Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 65/71] ARC: [Review] Multi-platform image #5: NR_IRQS defined by ARC core Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 66/71] ARC: [Review] Multi-platform image #6: cpu-to-dma-addr optional Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 67/71] ARC: [Review] Multi-platform image #7: SMP common code to use callbacks Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 68/71] ARC: [Review] Multi-platform image #8: platform registers SMP callbacks Vineet Gupta
2013-01-24 11:06   ` Vineet Gupta
2013-01-24 11:06 ` [PATCH v3 71/71] ARC: Add self to MAINTAINERS Vineet Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1359025589-22277-15-git-send-email-vgupta@synopsys.com \
    --to=vineet.gupta1@synopsys.com \
    --cc=arnd@arndb.de \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).