Home | History | Annotate | Line # | Download | only in include
cpu.h revision 1.82.2.2
      1 /*	$NetBSD: cpu.h,v 1.82.2.2 2007/08/07 18:05:27 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ralph Campbell and Rick Macklem.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)cpu.h	8.4 (Berkeley) 1/4/94
     35  */
     36 
     37 #ifndef _CPU_H_
     38 #define _CPU_H_
     39 
     40 #include <mips/cpuregs.h>
     41 
     42 /*
     43  * Exported definitions unique to NetBSD/mips cpu support.
     44  */
     45 
     46 #ifdef _KERNEL
     47 #ifndef _LOCORE
     48 #include <sys/cpu_data.h>
     49 
     50 #if defined(_KERNEL_OPT)
     51 #include "opt_lockdebug.h"
     52 #endif
     53 
     54 struct cpu_info {
     55 	struct cpu_data ci_data;	/* MI per-cpu data */
     56 	cpuid_t	ci_cpuid;
     57 	u_long ci_cpu_freq;		/* CPU frequency */
     58 	u_long ci_cycles_per_hz;	/* CPU freq / hz */
     59 	u_long ci_divisor_delay;	/* for delay/DELAY */
     60 	u_long ci_divisor_recip;	/* scaled reciprocal of previous;
     61 					   see below */
     62 	struct lwp *ci_curlwp;		/* currently running lwp */
     63 	struct lwp *ci_fpcurlwp;	/* the current FPU owner */
     64 	int ci_want_resched;		/* user preemption pending */
     65 	int ci_mtx_count;		/* negative count of held mutexes */
     66 	int ci_mtx_oldspl;		/* saved SPL value */
     67 };
     68 
     69 /*
     70  * To implement a more accurate microtime using the CP0 COUNT register
     71  * we need to divide that register by the number of cycles per MHz.
     72  * But...
     73  *
     74  * DIV and DIVU are expensive on MIPS (eg 75 clocks on the R4000).  MULT
     75  * and MULTU are only 12 clocks on the same CPU.
     76  *
     77  * The strategy we use is to calculate the reciprical of cycles per MHz,
     78  * scaled by 1<<32.  Then we can simply issue a MULTU and pluck of the
     79  * HI register and have the results of the division.
     80  */
     81 #define	MIPS_SET_CI_RECIPRICAL(cpu)					\
     82 do {									\
     83 	KASSERT((cpu)->ci_divisor_delay != 0);				\
     84 	(cpu)->ci_divisor_recip = 0x100000000ULL / (cpu)->ci_divisor_delay; \
     85 } while (0)
     86 
     87 #define	MIPS_COUNT_TO_MHZ(cpu, count, res)				\
     88 	__asm volatile("multu %1,%2 ; mfhi %0"				\
     89 	    : "=r"((res)) : "r"((count)), "r"((cpu)->ci_divisor_recip))
     90 
     91 #endif /* !_LOCORE */
     92 #endif /* _KERNEL */
     93 
     94 /*
     95  * CTL_MACHDEP definitions.
     96  */
     97 #define CPU_CONSDEV		1	/* dev_t: console terminal device */
     98 #define CPU_BOOTED_KERNEL	2	/* string: booted kernel name */
     99 #define CPU_ROOT_DEVICE		3	/* string: root device name */
    100 #define CPU_LLSC		4	/* OS/CPU supports LL/SC instruction */
    101 
    102 /*
    103  * Platform can override, but note this breaks userland compatibility
    104  * with other mips platforms.
    105  */
    106 #ifndef CPU_MAXID
    107 #define CPU_MAXID		5	/* number of valid machdep ids */
    108 
    109 #define CTL_MACHDEP_NAMES { \
    110 	{ 0, 0 }, \
    111 	{ "console_device", CTLTYPE_STRUCT }, \
    112 	{ "booted_kernel", CTLTYPE_STRING }, \
    113 	{ "root_device", CTLTYPE_STRING }, \
    114 	{ "llsc", CTLTYPE_INT }, \
    115 }
    116 #endif
    117 
    118 #ifdef _KERNEL
    119 #ifdef _LKM
    120 /* Assume all CPU architectures are valid for LKM's */
    121 #define	MIPS1	1
    122 #define	MIPS3	1
    123 #define	MIPS4	1
    124 #define	MIPS32	1
    125 #define	MIPS64	1
    126 #endif
    127 
    128 #if (MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 0
    129 #error at least one of MIPS1, MIPS3, MIPS4, MIPS32 or MIPS64 must be specified
    130 #endif
    131 
    132 /* Shortcut for MIPS3 or above defined */
    133 #if defined(MIPS3) || defined(MIPS4) || defined(MIPS32) || defined(MIPS64)
    134 #define	MIPS3_PLUS	1
    135 #else
    136 #undef MIPS3_PLUS
    137 #endif
    138 
    139 /*
    140  * Macros to find the CPU architecture we're on at run-time,
    141  * or if possible, at compile-time.
    142  */
    143 
    144 #define	CPU_ARCH_MIPSx	0		/* XXX unknown */
    145 #define	CPU_ARCH_MIPS1	(1 << 0)
    146 #define	CPU_ARCH_MIPS2	(1 << 1)
    147 #define	CPU_ARCH_MIPS3	(1 << 2)
    148 #define	CPU_ARCH_MIPS4	(1 << 3)
    149 #define	CPU_ARCH_MIPS5	(1 << 4)
    150 #define	CPU_ARCH_MIPS32	(1 << 5)
    151 #define	CPU_ARCH_MIPS64	(1 << 6)
    152 
    153 /* Note: must be kept in sync with -ffixed-?? Makefile.mips. */
    154 #define MIPS_CURLWP             $23
    155 #define MIPS_CURLWP_QUOTED      "$23"
    156 #define MIPS_CURLWP_CARD	23
    157 #define	MIPS_CURLWP_FRAME(x)	FRAME_S7(x)
    158 
    159 #ifndef _LOCORE
    160 
    161 extern struct cpu_info cpu_info_store;
    162 register struct lwp *mips_curlwp asm(MIPS_CURLWP_QUOTED);
    163 
    164 #define	curlwp			mips_curlwp
    165 #define	curcpu()		(curlwp->l_cpu)
    166 #define	curpcb			((struct pcb *)curlwp->l_addr)
    167 #define	fpcurlwp		(curcpu()->ci_fpcurlwp)
    168 #ifdef MULTIPROCESSOR
    169 #error cpu_number to be redefined when MULTIPROCESSOR is defined
    170 #else
    171 #define	cpu_number()		(0)
    172 #endif
    173 #define	cpu_proc_fork(p1, p2)	((p2)->p_md = (p1)->p_md)
    174 
    175 /* XXX simonb
    176  * Should the following be in a cpu_info type structure?
    177  * And how many of these are per-cpu vs. per-system?  (Ie,
    178  * we can assume that all cpus have the same mmu-type, but
    179  * maybe not that all cpus run at the same clock speed.
    180  * Some SGI's apparently support R12k and R14k in the same
    181  * box.)
    182  */
    183 extern int cpu_arch;
    184 extern int mips_cpu_flags;
    185 extern int mips_has_r4k_mmu;
    186 extern int mips_has_llsc;
    187 extern int mips3_pg_cached;
    188 extern u_int mips3_pg_shift;
    189 
    190 #define	CPU_MIPS_R4K_MMU		0x0001
    191 #define	CPU_MIPS_NO_LLSC		0x0002
    192 #define	CPU_MIPS_CAUSE_IV		0x0004
    193 #define	CPU_MIPS_HAVE_SPECIAL_CCA	0x0008	/* Defaults to '3' if not set. */
    194 #define	CPU_MIPS_CACHED_CCA_MASK	0x0070
    195 #define	CPU_MIPS_CACHED_CCA_SHIFT	 4
    196 #define	CPU_MIPS_DOUBLE_COUNT		0x0080	/* 1 cp0 count == 2 clock cycles */
    197 #define	CPU_MIPS_USE_WAIT		0x0100	/* Use "wait"-based cpu_idle() */
    198 #define	CPU_MIPS_NO_WAIT		0x0200	/* Inverse of previous, for mips32/64 */
    199 #define	CPU_MIPS_D_CACHE_COHERENT	0x0400	/* D-cache is fully coherent */
    200 #define	CPU_MIPS_I_D_CACHE_COHERENT	0x0800	/* I-cache funcs don't need to flush the D-cache */
    201 #define	MIPS_NOT_SUPP			0x8000
    202 
    203 #endif	/* !_LOCORE */
    204 
    205 #if ((MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 1) || defined(_LOCORE)
    206 
    207 #if defined(MIPS1)
    208 
    209 # define CPUISMIPS3		0
    210 # define CPUIS64BITS		0
    211 # define CPUISMIPS32		0
    212 # define CPUISMIPS64		0
    213 # define CPUISMIPSNN		0
    214 # define MIPS_HAS_R4K_MMU	0
    215 # define MIPS_HAS_CLOCK		0
    216 # define MIPS_HAS_LLSC		0
    217 
    218 #elif defined(MIPS3) || defined(MIPS4)
    219 
    220 # define CPUISMIPS3		1
    221 # define CPUIS64BITS		1
    222 # define CPUISMIPS32		0
    223 # define CPUISMIPS64		0
    224 # define CPUISMIPSNN		0
    225 # define MIPS_HAS_R4K_MMU	1
    226 # define MIPS_HAS_CLOCK		1
    227 # if defined(_LOCORE)
    228 #  if !defined(MIPS3_5900) && !defined(MIPS3_4100)
    229 #   define MIPS_HAS_LLSC	1
    230 #  else
    231 #   define MIPS_HAS_LLSC	0
    232 #  endif
    233 # else	/* _LOCORE */
    234 #  define MIPS_HAS_LLSC		(mips_has_llsc)
    235 # endif	/* _LOCORE */
    236 
    237 #elif defined(MIPS32)
    238 
    239 # define CPUISMIPS3		1
    240 # define CPUIS64BITS		0
    241 # define CPUISMIPS32		1
    242 # define CPUISMIPS64		0
    243 # define CPUISMIPSNN		1
    244 # define MIPS_HAS_R4K_MMU	1
    245 # define MIPS_HAS_CLOCK		1
    246 # define MIPS_HAS_LLSC		1
    247 
    248 #elif defined(MIPS64)
    249 
    250 # define CPUISMIPS3		1
    251 # define CPUIS64BITS		1
    252 # define CPUISMIPS32		0
    253 # define CPUISMIPS64		1
    254 # define CPUISMIPSNN		1
    255 # define MIPS_HAS_R4K_MMU	1
    256 # define MIPS_HAS_CLOCK		1
    257 # define MIPS_HAS_LLSC		1
    258 
    259 #endif
    260 
    261 #else /* run-time test */
    262 
    263 #ifndef	_LOCORE
    264 
    265 #define	MIPS_HAS_R4K_MMU	(mips_has_r4k_mmu)
    266 #define	MIPS_HAS_LLSC		(mips_has_llsc)
    267 
    268 /* This test is ... rather bogus */
    269 #define	CPUISMIPS3	((cpu_arch & \
    270 	(CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
    271 
    272 /* And these aren't much better while the previous test exists as is... */
    273 #define	CPUISMIPS32	((cpu_arch & CPU_ARCH_MIPS32) != 0)
    274 #define	CPUISMIPS64	((cpu_arch & CPU_ARCH_MIPS64) != 0)
    275 #define	CPUISMIPSNN	((cpu_arch & (CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
    276 #define	CPUIS64BITS	((cpu_arch & \
    277 	(CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS64)) != 0)
    278 
    279 #define	MIPS_HAS_CLOCK	(cpu_arch >= CPU_ARCH_MIPS3)
    280 
    281 #else	/* !_LOCORE */
    282 
    283 #define	MIPS_HAS_LLSC	0
    284 
    285 #endif	/* !_LOCORE */
    286 
    287 #endif /* run-time test */
    288 
    289 #ifndef	_LOCORE
    290 
    291 /*
    292  * definitions of cpu-dependent requirements
    293  * referenced in generic code
    294  */
    295 #define	cpu_swapout(p)			panic("cpu_swapout: can't get here");
    296 
    297 void cpu_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    298 
    299 /*
    300  * Arguments to hardclock and gatherstats encapsulate the previous
    301  * machine state in an opaque clockframe.
    302  */
    303 struct clockframe {
    304 	int	pc;	/* program counter at time of interrupt */
    305 	int	sr;	/* status register at time of interrupt */
    306 	int	ppl;	/* previous priority level at time of interrupt */
    307 };
    308 
    309 /*
    310  * A port must provde CLKF_USERMODE() for use in machine-independent code.
    311  * These differ on r4000 and r3000 systems; provide them in the
    312  * port-dependent file that includes this one, using the macros below.
    313  */
    314 
    315 /* mips1 versions */
    316 #define	MIPS1_CLKF_USERMODE(framep)	((framep)->sr & MIPS_SR_KU_PREV)
    317 
    318 /* mips3 versions */
    319 #define	MIPS3_CLKF_USERMODE(framep)	((framep)->sr & MIPS_SR_KSU_USER)
    320 
    321 #define	CLKF_PC(framep)		((framep)->pc)
    322 #define	CLKF_INTR(framep)	(0)
    323 
    324 #if defined(MIPS3_PLUS) && !defined(MIPS1)		/* XXX bogus! */
    325 #define	CLKF_USERMODE(framep)	MIPS3_CLKF_USERMODE(framep)
    326 #endif
    327 
    328 #if !defined(MIPS3_PLUS) && defined(MIPS1)		/* XXX bogus! */
    329 #define	CLKF_USERMODE(framep)	MIPS1_CLKF_USERMODE(framep)
    330 #endif
    331 
    332 #if defined(MIPS3_PLUS) && defined(MIPS1)		/* XXX bogus! */
    333 #define CLKF_USERMODE(framep) \
    334     ((CPUISMIPS3) ? MIPS3_CLKF_USERMODE(framep):  MIPS1_CLKF_USERMODE(framep))
    335 #endif
    336 
    337 /*
    338  * This is used during profiling to integrate system time.  It can safely
    339  * assume that the process is resident.
    340  */
    341 #define	PROC_PC(p)							\
    342 	(((struct frame *)(p)->p_md.md_regs)->f_regs[37])	/* XXX PC */
    343 
    344 /*
    345  * Preempt the current process if in interrupt from user mode,
    346  * or after the current trap/syscall if in system mode.
    347  */
    348 void	cpu_need_resched(struct cpu_info *, int);
    349 
    350 /*
    351  * Give a profiling tick to the current process when the user profiling
    352  * buffer pages are invalid.  On the MIPS, request an ast to send us
    353  * through trap, marking the proc as needing a profiling tick.
    354  */
    355 #define	cpu_need_proftick(l)						\
    356 do {									\
    357 	(l)->l_pflag |= LP_OWEUPC;					\
    358 	aston(l);							\
    359 } while (/*CONSTCOND*/0)
    360 
    361 /*
    362  * Notify the current lwp (l) that it has a signal pending,
    363  * process as soon as possible.
    364  */
    365 #define	cpu_signotify(l)	aston(l)
    366 
    367 #define aston(l)		((l)->l_md.md_astpending = 1)
    368 
    369 /*
    370  * Misc prototypes and variable declarations.
    371  */
    372 struct lwp;
    373 struct user;
    374 
    375 extern struct segtab *segbase;	/* current segtab base */
    376 
    377 /* trap.c */
    378 void	netintr(void);
    379 int	kdbpeek(vaddr_t);
    380 
    381 /* mips_machdep.c */
    382 void	dumpsys(void);
    383 int	savectx(struct user *);
    384 void	mips_init_msgbuf(void);
    385 void	savefpregs(struct lwp *);
    386 void	loadfpregs(struct lwp *);
    387 
    388 /* locore*.S */
    389 int	badaddr(void *, size_t);
    390 int	badaddr64(uint64_t, size_t);
    391 
    392 /* mips_machdep.c */
    393 void	cpu_identify(void);
    394 void	mips_vector_init(void);
    395 
    396 #endif /* ! _LOCORE */
    397 #endif /* _KERNEL */
    398 #endif /* _CPU_H_ */
    399