xref: /netbsd-src/sys/arch/riscv/riscv/ipifuncs.c (revision 6345bad4cf90683aa020ee19fd41aef146c00135)
1 /*	$NetBSD: ipifuncs.c,v 1.2 2023/09/03 08:48:20 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas of 3am Software Foundry.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ipifuncs.c,v 1.2 2023/09/03 08:48:20 skrll Exp $");
34 
35 #include <sys/param.h>
36 
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/intr.h>
40 #include <sys/ipi.h>
41 #include <sys/xcall.h>
42 
43 #include <uvm/uvm_extern.h>
44 #include <uvm/pmap/pmap_synci.h>
45 #include <uvm/pmap/pmap_tlb.h>
46 
47 static void ipi_halt(void) __dead;
48 
49 static const char * const ipi_names[] = {
50 	[IPI_NOP]	= "ipi nop",
51 	[IPI_AST]	= "ipi ast",
52 	[IPI_KPREEMPT]	= "ipi kpreempt",
53 	[IPI_SUSPEND]	= "ipi suspend",
54 	[IPI_HALT]	= "ipi halt",
55 	[IPI_XCALL]	= "ipi xcall",
56 	[IPI_GENERIC]	= "ipi generic",
57 };
58 
59 static void
ipi_nop(struct cpu_info * ci)60 ipi_nop(struct cpu_info *ci)
61 {
62 	/*
63 	 * This is just a reason to get an interrupt so we get
64 	 * kicked out of cpu_idle().
65 	 */
66 }
67 
68 static void
ipi_ast(struct cpu_info * ci)69 ipi_ast(struct cpu_info *ci)
70 {
71 	ci->ci_onproc->l_md.md_astpending = 1;
72 }
73 
74 #ifdef __HAVE_PREEMPTION
75 static inline void
ipi_kpreempt(struct cpu_info * ci)76 ipi_kpreempt(struct cpu_info *ci)
77 {
78 	softint_trigger(SOFTINT_KPREEMPT);
79 }
80 #endif
81 
82 /*
83  * Process cpu stop-self event.
84  * XXX could maybe add/use locoresw halt function?
85  */
86 static void
ipi_halt(void)87 ipi_halt(void)
88 {
89 	const u_int my_cpu = cpu_index(curcpu());
90 	printf("cpu%u: shutting down\n", my_cpu);
91 	kcpuset_set(cpus_halted, my_cpu);
92 	splhigh();
93 	for (;;)
94 		;
95 	/* NOTREACHED */
96 }
97 
98 void
ipi_process(struct cpu_info * ci,unsigned long ipi_mask)99 ipi_process(struct cpu_info *ci, unsigned long ipi_mask)
100 {
101 	KASSERT(cpu_intr_p());
102 
103 	if (ipi_mask & __BIT(IPI_NOP)) {
104 		ci->ci_evcnt_per_ipi[IPI_NOP].ev_count++;
105 		ipi_nop(ci);
106 	}
107 	if (ipi_mask & __BIT(IPI_AST)) {
108 		ci->ci_evcnt_per_ipi[IPI_AST].ev_count++;
109 		ipi_ast(ci);
110 	}
111 	if (ipi_mask & __BIT(IPI_SUSPEND)) {
112 		ci->ci_evcnt_per_ipi[IPI_SUSPEND].ev_count++;
113 		cpu_pause();
114 	}
115 	if (ipi_mask & __BIT(IPI_HALT)) {
116 		ci->ci_evcnt_per_ipi[IPI_HALT].ev_count++;
117 		ipi_halt();
118 	}
119 	if (ipi_mask & __BIT(IPI_XCALL)) {
120 		ci->ci_evcnt_per_ipi[IPI_XCALL].ev_count++;
121 		xc_ipi_handler();
122 	}
123 	if (ipi_mask & __BIT(IPI_GENERIC)) {
124 		ci->ci_evcnt_per_ipi[IPI_GENERIC].ev_count++;
125 		ipi_cpu_handler();
126 	}
127 #ifdef __HAVE_PREEMPTION
128 	if (ipi_mask & __BIT(IPI_KPREEMPT)) {
129 		ci->ci_evcnt_per_ipi[IPI_KPREEMPT].ev_count++;
130 		ipi_kpreempt(ci);
131 	}
132 #endif
133 }
134 
135 void
ipi_init(struct cpu_info * ci)136 ipi_init(struct cpu_info *ci)
137 {
138 	evcnt_attach_dynamic(&ci->ci_evcnt_all_ipis, EVCNT_TYPE_INTR,
139 	    NULL, device_xname(ci->ci_dev), "ipi");
140 
141 	for (size_t i = 0; i < NIPIS; i++) {
142 		KASSERTMSG(ipi_names[i] != NULL, "%zu", i);
143 		evcnt_attach_dynamic(&ci->ci_evcnt_per_ipi[i], EVCNT_TYPE_INTR,
144 		    NULL, device_xname(ci->ci_dev), ipi_names[i]);
145 	}
146 }
147