xref: /freebsd/sys/riscv/include/cpu.h (revision b37dc0903332c4e3b593f1c92df986e8d367d697)
18d7e7a98SRuslan Bukin /*-
2b51092c7SRuslan Bukin  * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
38d7e7a98SRuslan Bukin  * All rights reserved.
48d7e7a98SRuslan Bukin  *
58d7e7a98SRuslan Bukin  * Portions of this software were developed by SRI International and the
68d7e7a98SRuslan Bukin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
78d7e7a98SRuslan Bukin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
88d7e7a98SRuslan Bukin  *
98d7e7a98SRuslan Bukin  * Portions of this software were developed by the University of Cambridge
108d7e7a98SRuslan Bukin  * Computer Laboratory as part of the CTSRD Project, with support from the
118d7e7a98SRuslan Bukin  * UK Higher Education Innovation Fund (HEIF).
128d7e7a98SRuslan Bukin  *
138d7e7a98SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
148d7e7a98SRuslan Bukin  * modification, are permitted provided that the following conditions
158d7e7a98SRuslan Bukin  * are met:
168d7e7a98SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
178d7e7a98SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
188d7e7a98SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
198d7e7a98SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
208d7e7a98SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
218d7e7a98SRuslan Bukin  *
228d7e7a98SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
238d7e7a98SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248d7e7a98SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258d7e7a98SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
268d7e7a98SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278d7e7a98SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288d7e7a98SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298d7e7a98SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308d7e7a98SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318d7e7a98SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328d7e7a98SRuslan Bukin  * SUCH DAMAGE.
338d7e7a98SRuslan Bukin  *
348d7e7a98SRuslan Bukin  * $FreeBSD$
358d7e7a98SRuslan Bukin  */
368d7e7a98SRuslan Bukin 
378d7e7a98SRuslan Bukin #ifndef _MACHINE_CPU_H_
388d7e7a98SRuslan Bukin #define	_MACHINE_CPU_H_
398d7e7a98SRuslan Bukin 
408d7e7a98SRuslan Bukin #include <machine/atomic.h>
416f8ba916SMark Johnston #include <machine/cpufunc.h>
428d7e7a98SRuslan Bukin #include <machine/frame.h>
438d7e7a98SRuslan Bukin 
4401771021SJohn Baldwin #define	TRAPF_PC(tfp)		((tfp)->tf_sepc)
45b51092c7SRuslan Bukin #define	TRAPF_USERMODE(tfp)	(((tfp)->tf_sstatus & SSTATUS_SPP) == 0)
468d7e7a98SRuslan Bukin 
478d7e7a98SRuslan Bukin #define	cpu_getstack(td)	((td)->td_frame->tf_sp)
488d7e7a98SRuslan Bukin #define	cpu_setstack(td, sp)	((td)->td_frame->tf_sp = (sp))
498d7e7a98SRuslan Bukin #define	cpu_spinwait()		/* nothing */
504cbbb748SJohn Baldwin #define	cpu_lock_delay()	DELAY(1)
518d7e7a98SRuslan Bukin 
528d7e7a98SRuslan Bukin #ifdef _KERNEL
538d7e7a98SRuslan Bukin 
548d7e7a98SRuslan Bukin /*
5588b4d124SMitchell Horne  * Core manufacturer IDs, as reported by the mvendorid CSR.
568d7e7a98SRuslan Bukin  */
5788b4d124SMitchell Horne #define	MVENDORID_UNIMPL	0x0
5888b4d124SMitchell Horne #define	MVENDORID_SIFIVE	0x489
5988b4d124SMitchell Horne #define	MVENDORID_THEAD		0x5b7
608d7e7a98SRuslan Bukin 
6188b4d124SMitchell Horne /*
6288b4d124SMitchell Horne  * Micro-architecture ID register, marchid.
6388b4d124SMitchell Horne  *
6488b4d124SMitchell Horne  * IDs for open-source implementations are allocated globally. Commercial IDs
6588b4d124SMitchell Horne  * will have the most-significant bit set.
6688b4d124SMitchell Horne  */
6788b4d124SMitchell Horne #define	MARCHID_UNIMPL		0x0
6888b4d124SMitchell Horne #define	MARCHID_MSB		(1ul << (XLEN - 1))
6988b4d124SMitchell Horne #define	MARCHID_OPENSOURCE(v)	(v)
7088b4d124SMitchell Horne #define	MARCHID_COMMERCIAL(v)	(MARCHID_MSB | (v))
7188b4d124SMitchell Horne #define	MARCHID_IS_OPENSOURCE(m) (((m) & MARCHID_MSB) == 0)
728d7e7a98SRuslan Bukin 
7388b4d124SMitchell Horne /*
7488b4d124SMitchell Horne  * Open-source marchid values.
7588b4d124SMitchell Horne  *
7688b4d124SMitchell Horne  * https://github.com/riscv/riscv-isa-manual/blob/master/marchid.md
7788b4d124SMitchell Horne  */
7888b4d124SMitchell Horne #define	MARCHID_UCB_ROCKET	MARCHID_OPENSOURCE(1)
7988b4d124SMitchell Horne #define	MARCHID_UCB_BOOM	MARCHID_OPENSOURCE(2)
8088b4d124SMitchell Horne #define	MARCHID_UCB_SPIKE	MARCHID_OPENSOURCE(5)
8188b4d124SMitchell Horne #define	MARCHID_UCAM_RVBS	MARCHID_OPENSOURCE(10)
8288b4d124SMitchell Horne 
8388b4d124SMitchell Horne /* SiFive marchid values */
8488b4d124SMitchell Horne #define	MARCHID_SIFIVE_U7	MARCHID_COMMERCIAL(7)
858d7e7a98SRuslan Bukin 
868d7e7a98SRuslan Bukin extern char btext[];
878d7e7a98SRuslan Bukin extern char etext[];
888d7e7a98SRuslan Bukin 
898d7e7a98SRuslan Bukin void	cpu_halt(void) __dead2;
908d7e7a98SRuslan Bukin void	cpu_reset(void) __dead2;
918d7e7a98SRuslan Bukin void	fork_trampoline(void);
92*b37dc090SMitchell Horne void	identify_cpu(u_int cpu);
93*b37dc090SMitchell Horne void	printcpuinfo(u_int cpu);
948d7e7a98SRuslan Bukin 
958d7e7a98SRuslan Bukin static __inline uint64_t
968d7e7a98SRuslan Bukin get_cyclecount(void)
978d7e7a98SRuslan Bukin {
988d7e7a98SRuslan Bukin 
996f8ba916SMark Johnston 	return (rdcycle());
1008d7e7a98SRuslan Bukin }
1018d7e7a98SRuslan Bukin 
1028d7e7a98SRuslan Bukin #endif
1038d7e7a98SRuslan Bukin 
1048d7e7a98SRuslan Bukin #endif /* !_MACHINE_CPU_H_ */
105