xref: /kvm-unit-tests/lib/powerpc/smp.c (revision 4363f1d9a646a5c7ea673bee8fc33ca6f2cddbd8)
1 /*
2  * Secondary cpu support
3  *
4  * Copyright 2016 Suraj Jitindar Singh, IBM.
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 
9 #include <devicetree.h>
10 #include <asm/setup.h>
11 #include <asm/rtas.h>
12 #include <asm/smp.h>
13 
14 int nr_threads;
15 
16 struct secondary_entry_data {
17 	secondary_entry_fn entry;
18 	uint64_t r3;
19 	int nr_started;
20 };
21 
22 /*
23  * Start stopped thread cpu_id at entry
24  * Returns:	<0 on failure to start stopped cpu
25  *		0  on success
26  *		>0 on cpu not in stopped state
27  */
28 int start_thread(int cpu_id, secondary_entry_fn entry, uint32_t r3)
29 {
30 	uint32_t query_token, start_token;
31 	int outputs[1], ret;
32 
33 	ret = rtas_token("query-cpu-stopped-state", &query_token);
34 	assert(ret == 0);
35 	ret = rtas_token("start-cpu", &start_token);
36 	assert(ret == 0);
37 
38 	ret = rtas_call(query_token, 1, 2, outputs, cpu_id);
39 	if (ret) {
40 		printf("query-cpu-stopped-state failed for cpu %d\n", cpu_id);
41 	} else if (!outputs[0]) { /* cpu in stopped state */
42 		ret = rtas_call(start_token, 3, 1, NULL, cpu_id, entry, r3);
43 		if (ret)
44 			printf("failed to start cpu %d\n", cpu_id);
45 	} else { /* cpu not in stopped state */
46 		ret = outputs[0];
47 	}
48 
49 	return ret;
50 }
51 
52 /*
53  * Start all stopped threads (vcpus) on cpu_node
54  * Returns: Number of stopped cpus which were successfully started
55  */
56 struct start_threads start_cpu(int cpu_node, secondary_entry_fn entry,
57 			       uint32_t r3)
58 {
59 	int len, i, nr_threads, nr_started = 0;
60 	const struct fdt_property *prop;
61 	u32 *threads;
62 
63 	/* Get the id array of threads on this cpu_node */
64 	prop = fdt_get_property(dt_fdt(), cpu_node,
65 				"ibm,ppc-interrupt-server#s", &len);
66 	assert(prop);
67 
68 	nr_threads = len >> 2; /* Divide by 4 since 4 bytes per thread */
69 	threads = (u32 *)prop->data; /* Array of valid ids */
70 
71 	for (i = 0; i < nr_threads; i++) {
72 		if (!start_thread(fdt32_to_cpu(threads[i]), entry, r3))
73 			nr_started++;
74 	}
75 
76 	return (struct start_threads) { nr_threads, nr_started };
77 }
78 
79 static void start_each_secondary(int fdtnode, u64 regval __unused, void *info)
80 {
81 	struct secondary_entry_data *datap = info;
82 	struct start_threads ret = start_cpu(fdtnode, datap->entry, datap->r3);
83 
84 	nr_threads += ret.nr_threads;
85 	datap->nr_started += ret.nr_started;
86 }
87 
88 /*
89  * Start all stopped cpus on the guest at entry with register 3 set to r3
90  * We expect that we come in with only one thread currently started
91  * Returns:	TRUE on success
92  *		FALSE on failure
93  */
94 bool start_all_cpus(secondary_entry_fn entry, uint32_t r3)
95 {
96 	struct secondary_entry_data data = { entry, r3,	0 };
97 	int ret;
98 
99 	ret = dt_for_each_cpu_node(start_each_secondary, &data);
100 	assert(ret == 0);
101 
102 	/* We expect that we come in with one thread already started */
103 	return data.nr_started == nr_threads - 1;
104 }
105