Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <linux/module.h>
  4#include <linux/init.h>
  5#include <linux/kernel.h>
  6#include <linux/mm.h>
  7#include <linux/sched.h>
  8#include <linux/kernel_stat.h>
  9#include <linux/notifier.h>
 10#include <linux/cpu.h>
 11#include <linux/percpu.h>
 12#include <linux/delay.h>
 13#include <linux/err.h>
 14#include <linux/irq.h>
 15#include <linux/irq_work.h>
 16#include <linux/irqdomain.h>
 17#include <linux/of.h>
 18#include <linux/seq_file.h>
 19#include <linux/sched/task_stack.h>
 20#include <linux/sched/mm.h>
 21#include <linux/sched/hotplug.h>
 22#include <asm/irq.h>
 23#include <asm/traps.h>
 24#include <asm/sections.h>
 25#include <asm/mmu_context.h>
 26#ifdef CONFIG_CPU_HAS_FPU
 27#include <abi/fpu.h>
 28#endif
 
 
 
 29
 30enum ipi_message_type {
 31	IPI_EMPTY,
 32	IPI_RESCHEDULE,
 33	IPI_CALL_FUNC,
 34	IPI_IRQ_WORK,
 35	IPI_MAX
 36};
 37
 38struct ipi_data_struct {
 39	unsigned long bits ____cacheline_aligned;
 40	unsigned long stats[IPI_MAX] ____cacheline_aligned;
 41};
 42static DEFINE_PER_CPU(struct ipi_data_struct, ipi_data);
 43
 44static irqreturn_t handle_ipi(int irq, void *dev)
 45{
 46	unsigned long *stats = this_cpu_ptr(&ipi_data)->stats;
 47
 48	while (true) {
 49		unsigned long ops;
 50
 51		ops = xchg(&this_cpu_ptr(&ipi_data)->bits, 0);
 52		if (ops == 0)
 53			return IRQ_HANDLED;
 54
 55		if (ops & (1 << IPI_RESCHEDULE)) {
 56			stats[IPI_RESCHEDULE]++;
 57			scheduler_ipi();
 58		}
 59
 60		if (ops & (1 << IPI_CALL_FUNC)) {
 61			stats[IPI_CALL_FUNC]++;
 62			generic_smp_call_function_interrupt();
 63		}
 64
 65		if (ops & (1 << IPI_IRQ_WORK)) {
 66			stats[IPI_IRQ_WORK]++;
 67			irq_work_run();
 68		}
 69
 70		BUG_ON((ops >> IPI_MAX) != 0);
 71	}
 72
 73	return IRQ_HANDLED;
 74}
 75
 76static void (*send_arch_ipi)(const struct cpumask *mask);
 77
 78static int ipi_irq;
 79void __init set_send_ipi(void (*func)(const struct cpumask *mask), int irq)
 80{
 81	if (send_arch_ipi)
 82		return;
 83
 84	send_arch_ipi = func;
 85	ipi_irq = irq;
 86}
 87
 88static void
 89send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
 90{
 91	int i;
 92
 93	for_each_cpu(i, to_whom)
 94		set_bit(operation, &per_cpu_ptr(&ipi_data, i)->bits);
 95
 96	smp_mb();
 97	send_arch_ipi(to_whom);
 98}
 99
100static const char * const ipi_names[] = {
101	[IPI_EMPTY]		= "Empty interrupts",
102	[IPI_RESCHEDULE]	= "Rescheduling interrupts",
103	[IPI_CALL_FUNC]		= "Function call interrupts",
104	[IPI_IRQ_WORK]		= "Irq work interrupts",
105};
106
107int arch_show_interrupts(struct seq_file *p, int prec)
108{
109	unsigned int cpu, i;
110
111	for (i = 0; i < IPI_MAX; i++) {
112		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
113			   prec >= 4 ? " " : "");
114		for_each_online_cpu(cpu)
115			seq_printf(p, "%10lu ",
116				per_cpu_ptr(&ipi_data, cpu)->stats[i]);
117		seq_printf(p, " %s\n", ipi_names[i]);
118	}
119
120	return 0;
121}
122
123void arch_send_call_function_ipi_mask(struct cpumask *mask)
124{
125	send_ipi_message(mask, IPI_CALL_FUNC);
126}
127
128void arch_send_call_function_single_ipi(int cpu)
129{
130	send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
131}
132
133static void ipi_stop(void *unused)
134{
135	while (1);
136}
137
138void smp_send_stop(void)
139{
140	on_each_cpu(ipi_stop, NULL, 1);
141}
142
143void arch_smp_send_reschedule(int cpu)
144{
145	send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
146}
147
148#ifdef CONFIG_IRQ_WORK
149void arch_irq_work_raise(void)
150{
151	send_ipi_message(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
152}
153#endif
154
155void __init smp_prepare_boot_cpu(void)
156{
157}
158
159void __init smp_prepare_cpus(unsigned int max_cpus)
160{
161}
162
163static int ipi_dummy_dev;
164
165void __init setup_smp_ipi(void)
166{
167	int rc;
168
169	if (ipi_irq == 0)
170		return;
171
172	rc = request_percpu_irq(ipi_irq, handle_ipi, "IPI Interrupt",
173				&ipi_dummy_dev);
174	if (rc)
175		panic("%s IRQ request failed\n", __func__);
176
177	enable_percpu_irq(ipi_irq, 0);
178}
179
180void __init setup_smp(void)
181{
182	struct device_node *node = NULL;
183	unsigned int cpu;
184
185	for_each_of_cpu_node(node) {
186		if (!of_device_is_available(node))
187			continue;
188
189		cpu = of_get_cpu_hwid(node, 0);
 
 
190		if (cpu >= NR_CPUS)
191			continue;
192
193		set_cpu_possible(cpu, true);
194		set_cpu_present(cpu, true);
195	}
196}
197
198extern void _start_smp_secondary(void);
199
200volatile unsigned int secondary_hint;
201volatile unsigned int secondary_hint2;
202volatile unsigned int secondary_ccr;
203volatile unsigned int secondary_stack;
204volatile unsigned int secondary_msa1;
205volatile unsigned int secondary_pgd;
206
207int __cpu_up(unsigned int cpu, struct task_struct *tidle)
208{
209	unsigned long mask = 1 << cpu;
210
211	secondary_stack =
212		(unsigned int) task_stack_page(tidle) + THREAD_SIZE - 8;
213	secondary_hint = mfcr("cr31");
214	secondary_hint2 = mfcr("cr<21, 1>");
215	secondary_ccr  = mfcr("cr18");
216	secondary_msa1 = read_mmu_msa1();
217	secondary_pgd = mfcr("cr<29, 15>");
218
219	/*
220	 * Because other CPUs are in reset status, we must flush data
221	 * from cache to out and secondary CPUs use them in
222	 * csky_start_secondary(void)
223	 */
224	mtcr("cr17", 0x22);
225
226	if (mask & mfcr("cr<29, 0>")) {
227		send_arch_ipi(cpumask_of(cpu));
228	} else {
229		/* Enable cpu in SMP reset ctrl reg */
230		mask |= mfcr("cr<29, 0>");
231		mtcr("cr<29, 0>", mask);
232	}
233
234	/* Wait for the cpu online */
235	while (!cpu_online(cpu));
236
237	secondary_stack = 0;
238
239	return 0;
240}
241
242void __init smp_cpus_done(unsigned int max_cpus)
243{
244}
245
 
 
 
 
 
246void csky_start_secondary(void)
247{
248	struct mm_struct *mm = &init_mm;
249	unsigned int cpu = smp_processor_id();
250
251	mtcr("cr31", secondary_hint);
252	mtcr("cr<21, 1>", secondary_hint2);
253	mtcr("cr18", secondary_ccr);
254
255	mtcr("vbr", vec_base);
256
257	flush_tlb_all();
258	write_mmu_pagemask(0);
 
 
259
260#ifdef CONFIG_CPU_HAS_FPU
261	init_fpu();
262#endif
263
264	enable_percpu_irq(ipi_irq, 0);
265
266	mmget(mm);
267	mmgrab(mm);
268	current->active_mm = mm;
269	cpumask_set_cpu(cpu, mm_cpumask(mm));
270
271	notify_cpu_starting(cpu);
272	set_cpu_online(cpu, true);
273
274	pr_info("CPU%u Online: %s...\n", cpu, __func__);
275
276	local_irq_enable();
 
277	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
278}
279
280#ifdef CONFIG_HOTPLUG_CPU
281int __cpu_disable(void)
282{
283	unsigned int cpu = smp_processor_id();
284
285	set_cpu_online(cpu, false);
286
287	irq_migrate_all_off_this_cpu();
288
289	clear_tasks_mm_cpumask(cpu);
290
291	return 0;
292}
293
294void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)
295{
 
 
 
 
296	pr_notice("CPU%u: shutdown\n", cpu);
297}
298
299void __noreturn arch_cpu_idle_dead(void)
300{
301	idle_task_exit();
302
303	cpuhp_ap_report_dead();
304
305	while (!secondary_stack)
306		arch_cpu_idle();
307
308	raw_local_irq_disable();
309
310	asm volatile(
311		"mov	sp, %0\n"
312		"mov	r8, %0\n"
313		"jmpi	csky_start_secondary"
314		:
315		: "r" (secondary_stack));
316
317	BUG();
318}
319#endif
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <linux/module.h>
  4#include <linux/init.h>
  5#include <linux/kernel.h>
  6#include <linux/mm.h>
  7#include <linux/sched.h>
  8#include <linux/kernel_stat.h>
  9#include <linux/notifier.h>
 10#include <linux/cpu.h>
 11#include <linux/percpu.h>
 12#include <linux/delay.h>
 13#include <linux/err.h>
 14#include <linux/irq.h>
 
 15#include <linux/irqdomain.h>
 16#include <linux/of.h>
 
 17#include <linux/sched/task_stack.h>
 18#include <linux/sched/mm.h>
 19#include <linux/sched/hotplug.h>
 20#include <asm/irq.h>
 21#include <asm/traps.h>
 22#include <asm/sections.h>
 23#include <asm/mmu_context.h>
 24#include <asm/pgalloc.h>
 25
 26struct ipi_data_struct {
 27	unsigned long bits ____cacheline_aligned;
 28};
 29static DEFINE_PER_CPU(struct ipi_data_struct, ipi_data);
 30
 31enum ipi_message_type {
 32	IPI_EMPTY,
 33	IPI_RESCHEDULE,
 34	IPI_CALL_FUNC,
 
 35	IPI_MAX
 36};
 37
 
 
 
 
 
 
 38static irqreturn_t handle_ipi(int irq, void *dev)
 39{
 
 
 40	while (true) {
 41		unsigned long ops;
 42
 43		ops = xchg(&this_cpu_ptr(&ipi_data)->bits, 0);
 44		if (ops == 0)
 45			return IRQ_HANDLED;
 46
 47		if (ops & (1 << IPI_RESCHEDULE))
 
 48			scheduler_ipi();
 
 49
 50		if (ops & (1 << IPI_CALL_FUNC))
 
 51			generic_smp_call_function_interrupt();
 
 
 
 
 
 
 52
 53		BUG_ON((ops >> IPI_MAX) != 0);
 54	}
 55
 56	return IRQ_HANDLED;
 57}
 58
 59static void (*send_arch_ipi)(const struct cpumask *mask);
 60
 61static int ipi_irq;
 62void __init set_send_ipi(void (*func)(const struct cpumask *mask), int irq)
 63{
 64	if (send_arch_ipi)
 65		return;
 66
 67	send_arch_ipi = func;
 68	ipi_irq = irq;
 69}
 70
 71static void
 72send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
 73{
 74	int i;
 75
 76	for_each_cpu(i, to_whom)
 77		set_bit(operation, &per_cpu_ptr(&ipi_data, i)->bits);
 78
 79	smp_mb();
 80	send_arch_ipi(to_whom);
 81}
 82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 83void arch_send_call_function_ipi_mask(struct cpumask *mask)
 84{
 85	send_ipi_message(mask, IPI_CALL_FUNC);
 86}
 87
 88void arch_send_call_function_single_ipi(int cpu)
 89{
 90	send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
 91}
 92
 93static void ipi_stop(void *unused)
 94{
 95	while (1);
 96}
 97
 98void smp_send_stop(void)
 99{
100	on_each_cpu(ipi_stop, NULL, 1);
101}
102
103void smp_send_reschedule(int cpu)
104{
105	send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
106}
107
 
 
 
 
 
 
 
108void __init smp_prepare_boot_cpu(void)
109{
110}
111
112void __init smp_prepare_cpus(unsigned int max_cpus)
113{
114}
115
116static int ipi_dummy_dev;
117
118void __init setup_smp_ipi(void)
119{
120	int rc;
121
122	if (ipi_irq == 0)
123		panic("%s IRQ mapping failed\n", __func__);
124
125	rc = request_percpu_irq(ipi_irq, handle_ipi, "IPI Interrupt",
126				&ipi_dummy_dev);
127	if (rc)
128		panic("%s IRQ request failed\n", __func__);
129
130	enable_percpu_irq(ipi_irq, 0);
131}
132
133void __init setup_smp(void)
134{
135	struct device_node *node = NULL;
136	int cpu;
137
138	for_each_of_cpu_node(node) {
139		if (!of_device_is_available(node))
140			continue;
141
142		if (of_property_read_u32(node, "reg", &cpu))
143			continue;
144
145		if (cpu >= NR_CPUS)
146			continue;
147
148		set_cpu_possible(cpu, true);
149		set_cpu_present(cpu, true);
150	}
151}
152
153extern void _start_smp_secondary(void);
154
155volatile unsigned int secondary_hint;
 
156volatile unsigned int secondary_ccr;
157volatile unsigned int secondary_stack;
 
 
158
159int __cpu_up(unsigned int cpu, struct task_struct *tidle)
160{
161	unsigned long mask = 1 << cpu;
162
163	secondary_stack =
164		(unsigned int) task_stack_page(tidle) + THREAD_SIZE - 8;
165	secondary_hint = mfcr("cr31");
 
166	secondary_ccr  = mfcr("cr18");
 
 
167
168	/*
169	 * Because other CPUs are in reset status, we must flush data
170	 * from cache to out and secondary CPUs use them in
171	 * csky_start_secondary(void)
172	 */
173	mtcr("cr17", 0x22);
174
175	if (mask & mfcr("cr<29, 0>")) {
176		send_arch_ipi(cpumask_of(cpu));
177	} else {
178		/* Enable cpu in SMP reset ctrl reg */
179		mask |= mfcr("cr<29, 0>");
180		mtcr("cr<29, 0>", mask);
181	}
182
183	/* Wait for the cpu online */
184	while (!cpu_online(cpu));
185
186	secondary_stack = 0;
187
188	return 0;
189}
190
191void __init smp_cpus_done(unsigned int max_cpus)
192{
193}
194
195int setup_profiling_timer(unsigned int multiplier)
196{
197	return -EINVAL;
198}
199
200void csky_start_secondary(void)
201{
202	struct mm_struct *mm = &init_mm;
203	unsigned int cpu = smp_processor_id();
204
205	mtcr("cr31", secondary_hint);
 
206	mtcr("cr18", secondary_ccr);
207
208	mtcr("vbr", vec_base);
209
210	flush_tlb_all();
211	write_mmu_pagemask(0);
212	TLBMISS_HANDLER_SETUP_PGD(swapper_pg_dir);
213	TLBMISS_HANDLER_SETUP_PGD_KERNEL(swapper_pg_dir);
214
215#ifdef CONFIG_CPU_HAS_FPU
216	init_fpu();
217#endif
218
219	enable_percpu_irq(ipi_irq, 0);
220
221	mmget(mm);
222	mmgrab(mm);
223	current->active_mm = mm;
224	cpumask_set_cpu(cpu, mm_cpumask(mm));
225
226	notify_cpu_starting(cpu);
227	set_cpu_online(cpu, true);
228
229	pr_info("CPU%u Online: %s...\n", cpu, __func__);
230
231	local_irq_enable();
232	preempt_disable();
233	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
234}
235
236#ifdef CONFIG_HOTPLUG_CPU
237int __cpu_disable(void)
238{
239	unsigned int cpu = smp_processor_id();
240
241	set_cpu_online(cpu, false);
242
243	irq_migrate_all_off_this_cpu();
244
245	clear_tasks_mm_cpumask(cpu);
246
247	return 0;
248}
249
250void __cpu_die(unsigned int cpu)
251{
252	if (!cpu_wait_death(cpu, 5)) {
253		pr_crit("CPU%u: shutdown failed\n", cpu);
254		return;
255	}
256	pr_notice("CPU%u: shutdown\n", cpu);
257}
258
259void arch_cpu_idle_dead(void)
260{
261	idle_task_exit();
262
263	cpu_report_death();
264
265	while (!secondary_stack)
266		arch_cpu_idle();
267
268	local_irq_disable();
269
270	asm volatile(
271		"mov	sp, %0\n"
272		"mov	r8, %0\n"
273		"jmpi	csky_start_secondary"
274		:
275		: "r" (secondary_stack));
 
 
276}
277#endif