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