11.5Sryo/* $NetBSD: cpu.h,v 1.5 2021/08/14 17:51:19 ryo Exp $ */
21.1Smatt
31.1Smatt/*-
41.1Smatt * Copyright (c) 2014 The NetBSD Foundation, Inc.
51.1Smatt * All rights reserved.
61.1Smatt *
71.1Smatt * This code is derived from software contributed to The NetBSD Foundation
81.1Smatt * by Matt Thomas of 3am Software Foundry.
91.1Smatt *
101.1Smatt * Redistribution and use in source and binary forms, with or without
111.1Smatt * modification, are permitted provided that the following conditions
121.1Smatt * are met:
131.1Smatt * 1. Redistributions of source code must retain the above copyright
141.1Smatt *    notice, this list of conditions and the following disclaimer.
151.1Smatt * 2. Redistributions in binary form must reproduce the above copyright
161.1Smatt *    notice, this list of conditions and the following disclaimer in the
171.1Smatt *    documentation and/or other materials provided with the distribution.
181.1Smatt *
191.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smatt * POSSIBILITY OF SUCH DAMAGE.
301.1Smatt */
311.1Smatt
321.1Smatt#ifndef _OR1K_CPU_H_
331.1Smatt#define _OR1K_CPU_H_
341.1Smatt
351.1Smatt#if defined(_KERNEL) || defined(_KMEMUSER)
361.1Smattstruct clockframe {
371.1Smatt	uintptr_t cf_pc;
381.1Smatt	uint32_t cf_sr;
391.1Smatt	int cf_intr_depth;
401.1Smatt};
411.1Smatt
421.1Smatt#define CLKF_USERMODE(cf)	(((cf)->cf_sr & 1) == 0)
431.1Smatt#define CLKF_PC(cf)		((cf)->cf_pc)
441.1Smatt#define CLKF_INTR(cf)		((cf)->cf_intr_depth > 0)
451.1Smatt
461.1Smatt#include <sys/cpu_data.h>
471.1Smatt#include <sys/device_if.h>
481.1Smatt#include <sys/intr.h>
491.1Smatt
501.1Smattstruct cpu_info {
511.1Smatt	struct cpu_data ci_data;
521.1Smatt	device_t ci_dev;
531.1Smatt	cpuid_t ci_cpuid;
541.1Smatt	struct lwp *ci_curlwp;
551.4Sad	struct lwp *ci_onproc;		/* current user LWP / kthread */
561.1Smatt	struct lwp *ci_softlwps[SOFTINT_COUNT];
571.1Smatt
581.1Smatt	uint64_t ci_lastintr;
591.1Smatt
601.1Smatt	int ci_mtx_oldspl;
611.1Smatt	int ci_mtx_count;
621.1Smatt
631.1Smatt	int ci_want_resched;
641.1Smatt	int ci_cpl;
651.1Smatt	u_int ci_softints;
661.1Smatt	volatile u_int ci_intr_depth;
671.5Sryo#if defined(GPROF) && defined(MULTIPROCESSOR)
681.5Sryo	struct gmonparam *ci_gmon;	/* MI per-cpu GPROF */
691.5Sryo#endif
701.1Smatt};
711.1Smatt
721.1Smattregister struct lwp *or1k_curlwp __asm("r10");
731.1Smatt#define	curlwp		or1k_curlwp
741.1Smatt
751.2Schristosstatic __inline struct cpu_info *
761.1Smattcurcpu(void)
771.1Smatt{
781.1Smatt	return curlwp->l_cpu;
791.1Smatt}
801.1Smatt
811.2Schristosstatic __inline cpuid_t
821.1Smattcpu_number(void)
831.1Smatt{
841.1Smatt#ifdef MULTIPROCESSOR
851.1Smatt	return curcpu()->ci_cpuid;
861.1Smatt#else
871.1Smatt	return 0;
881.1Smatt#endif
891.1Smatt}
901.1Smatt
911.1Smattvoid	cpu_proc_fork(struct proc *, struct proc *);
921.1Smattvoid	cpu_signotify(struct lwp *);
931.1Smattvoid	cpu_need_proftick(struct lwp *l);
941.1Smattvoid	cpu_boot_secondary_processors(void);
951.1Smatt
961.1Smatt#define CPU_INFO_ITERATOR	cpuid_t
971.1Smatt#ifdef MULTIPROCESSOR
981.1Smatt#define CPU_INFO_FOREACH(cii, ci) \
991.1Smatt	(cii) = 0; ((ci) = cpu_infos[cii]) != NULL; (cii)++
1001.1Smatt#else
1011.1Smatt#define CPU_INFO_FOREACH(cii, ci) \
1021.1Smatt	(cii) = 0; (cii) == 0 && (ci) = curcpu(); (cii)++
1031.1Smatt#endif
1041.1Smatt
1051.2Schristosstatic __inline void
1061.1Smattcpu_dosoftints(void)
1071.1Smatt{
1081.1Smatt	extern void dosoftints(void);
1091.1Smatt        struct cpu_info * const ci = curcpu();
1101.1Smatt        if (ci->ci_intr_depth == 0
1111.1Smatt	    && (ci->ci_data.cpu_softints >> ci->ci_cpl) > 0)
1121.1Smatt                dosoftints();
1131.1Smatt}
1141.1Smatt
1151.2Schristosstatic __inline bool
1161.1Smattcpu_intr_p(void)
1171.1Smatt{
1181.1Smatt	return curcpu()->ci_intr_depth > 0;
1191.1Smatt}
1201.1Smatt
1211.1Smatt#endif /* _KERNEL || _KMEMUSER */
1221.1Smatt
1231.1Smatt#endif /* _OR1K_CPU_H_ */
124