/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ifndef	lint
static	char	rcsid[] = "$Header: tlb.c 1.3 90/04/05 $";
#endif

/*
 * tlb.c
 * 	TLB (translation lookaside buffer) manipulation routines.
 *
 * Architecture specific, since mechanism to broadcast interrupts (or whatever)
 * is architecture dependent.
 *
 * TODO:
 *
 *	Move atomic_dec_zero() definition to more appropriate place.  Move
 *	lint version to lintasm.c
 *
 *	consider making tmo_onoff a lock_t instead of a sema_t.  See sys_tmp.c.
 */

/* $Log:	tlb.c,v $
 */

#include "sys/types.h"
#include "sys/timer.h"
#include "sys/signal.h"
#include "sys/param.h"
#include "sys/mutex.h"
#include "sys/user.h"
#include "sys/proc.h"
#include "sys/vm.h"

#include "sys/engine.h"
#include "sys/slic.h"
#include "sys/SGSproc.h"

#include "sys/pte.h"
#include "sys/intctl.h"
#include "sys/plocal.h"
#include "sys/mftpr.h"
#include "sys/gate.h"

#include "sys/debug.h"

#ifdef	i386
#ifndef	lint
/*
 * atomic_dec_zero()
 *	Atomic decrement an integer variable, returns "true" if this
 *	decrement caused it to hit zero.
 */
asm	atomic_dec_zero(var)
{
%mem var; lab done;
	movl	var, %eax
	lock
	decl	(%eax)
	jz	done
	xorl	%eax, %eax
done:
}
#else	/* lint */
atomic_dec_zero(intp) int *intp; { return --*intp == 0; }
#endif	/* lint */
#endif	/* i386 */

extern	sema_t	tmp_onoff;	/* coordination of online(s) and offline(s) */

/*
 * Struct TLBflush contains necessary information for processors to flush
 * their TLB's.
 */

struct	TLBflush {
	lock_t	mutex;			/* mutex nengine */
	int	nengine;		/* # engines to go */
	caddr_t	start;			/* starting kernel virtual address */
	int	len;			/* length (HW pages) */
	int	initiator;		/* who started it? */
	sema_t	wait;			/* initiator waits here */
};

static	struct	TLBflush tlb_flush;	/* mutex'd via tmp_onoff */

#ifdef	DEBUG
/*
 * Statistics on time to flush TLB's.
 */
static	int	num_tlb_flush;
static	ulong	tlb_flush_time;
#endif	/* DEBUG */

/*
 * InitFlushTLB()
 *	Called at boot time to initialize Coherent TLB functions.
 */

InitFlushTLB()
{
	init_lock(&tlb_flush.mutex);
	init_sema(&tlb_flush.wait, 0);
}

/*
 * FlushTLB()
 *	Insure all processor TLB's are coherent for given address range.
 *
 * Called after some kernel mapping has changed to insure all processors have
 * up to date TLB -- no stale mappings.
 *
 * Must be called in process context, holding no locks -- uses tmp_onoff sema
 * to mutex against concurrent online/offline.
 *
 * Assumes calling processor did own TLB.
 *
 * Each processor "checks in" to the TLBflush "barrier".  Last in wakes waiting
 * process.  Note: could keep waiting process spinning, if this is faster: need
 * to measure time this takes.  Also, not truly a barrier, since each process
 * checks in and leaves immediately.
 */

FlushTLB(start, len)
	caddr_t	start;			/* start kernel virtual address */
	int	len;			/* length (bytes) */
{
	spl_t  s;
#ifdef	DEBUG
	ulong	start_etc, end_etc;
#endif	/* DEBUG */

	/*
	 * Coordinate with concurrent online or offline.  This also mutex's
	 * the TLBflush structure.
	 *
	 * If only one processor online, nothing to do (since caller is
	 * running on it ;-)
	 */

	(void)p_sema(&tmp_onoff, PZERO);

	if (nonline == 1) {
		v_sema(&tmp_onoff);
		return;
	}
#ifdef	DEBUG
	start_etc = *(int*)(VA_ETC);
#endif	/* DEBUG */

	/*
	 * Set up TLBflush structure.
	 */

	tlb_flush.nengine = nonline - 1;	/* don't include self */
	tlb_flush.start = start;		/* starting kernel address */
	tlb_flush.len = clrnd(btoc(len));	/* length (HW pages) */
	tlb_flush.initiator = l.me;		/* processor who started this */

	/*
	 * Get all processors interested in updating their TLB's.
	 * Done by sending SW interrupt to all processors (using SLIC
	 * group interrupt).  This also sends to calling processor.
	 */

	s = splhi();
	sendsoft(SL_GROUP|TMPOS_GROUP, SWINT_TLB_FLUSH);
	splx(s);

	/*
	 * Wait for all processors to flush their TLB's.
	 */

	(void)p_sema(&tlb_flush.wait, PSWP);
#ifdef	DEBUG
	end_etc = *(int*)(VA_ETC);
	num_tlb_flush++;
	tlb_flush_time += (end_etc - start_etc);
#endif	/* DEBUG */

	/*
	 * Done!
	 */

	v_sema(&tmp_onoff);
}

/*
 * FlushTLBIntr()
 *	SW interrupt handler called as result of SWINT_TLB_FLUSH.
 *
 * Flush relevant TLB, then check into TLBflush barrier.
 * Last one in signals initiator.
 *
 * Initiator of the flush operation is assumed to have taken care of self,
 * thus does a NOP here.  Takes interrupt in interest of using SLIC to
 * broadcast a SW interrupt (much lower overhead to send the interrupts -- one
 * SLIC message).
 *
 * On i386, could use "atomic-decrement" barrier and not use the lock.
 */

FlushTLBIntr()
{
#ifdef	portable
	spl_t	s;
#endif	/* portable */

	/*
	 * If this is initiating processor, no need to sync TLB nor check
	 * into barrier.
	 */

	if (tlb_flush.initiator == l.me)
		return;

	/*
	 * Flush relevant parts of TLB.  Machine dependent.
	 */

#ifdef	i386
	/*
	 * i386 can't flush individually; must flush whole TLB.
	 */
	FLUSH_TLB();

	if (atomic_dec_zero(&tlb_flush.nengine))
		v_sema(&tlb_flush.wait);
#endif	/* i386 */
#ifdef	portable
	/*
	 * Check into the barrier.  Last one in gooses initiator.
	 */

	s = p_lock(&tlb_flush.mutex, SPLHI);

	if (--tlb_flush.nengine == 0)
		v_sema(&tlb_flush.wait);

	v_lock(&tlb_flush.mutex, s);
#endif	/* portable */
}
