Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2013 Google Inc. |
| 5 | * |
Aaron Durbin | 0b0a1e3 | 2014-09-06 01:28:54 -0500 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 9 | * |
Aaron Durbin | 0b0a1e3 | 2014-09-06 01:28:54 -0500 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 14 | * |
Aaron Durbin | 0b0a1e3 | 2014-09-06 01:28:54 -0500 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 18 | */ |
Aaron Durbin | 0b0a1e3 | 2014-09-06 01:28:54 -0500 | [diff] [blame] | 19 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 20 | #include <stdint.h> |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 22 | #include <arch/lib_helpers.h> |
| 23 | #include <cpu/cpu.h> |
| 24 | #include <console/console.h> |
| 25 | #include "cpu-internal.h" |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 26 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 27 | struct cpu_info cpu_infos[CONFIG_MAX_CPUS]; |
Aaron Durbin | 74ff69f | 2014-09-17 11:51:41 -0500 | [diff] [blame] | 28 | struct cpu_info *bsp_cpu_info; |
Aaron Durbin | 1b315d0 | 2014-08-27 10:30:39 -0500 | [diff] [blame] | 29 | |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 30 | struct cpu_info *cpu_info(void) |
| 31 | { |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 32 | return cpu_info_for_cpu(smp_processor_id()); |
| 33 | } |
| 34 | |
Aaron Durbin | 931a218 | 2014-11-05 11:19:21 -0600 | [diff] [blame] | 35 | size_t cpus_online(void) |
| 36 | { |
| 37 | int i; |
| 38 | size_t num = 0; |
| 39 | |
| 40 | for (i = 0; i < ARRAY_SIZE(cpu_infos); i++) { |
| 41 | if (cpu_online(cpu_info_for_cpu(i))) |
| 42 | num++; |
| 43 | } |
| 44 | |
| 45 | return num; |
| 46 | } |
| 47 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 48 | static inline int action_queue_empty(struct cpu_action_queue *q) |
| 49 | { |
| 50 | return load_acquire_exclusive(&q->todo) == NULL; |
| 51 | } |
| 52 | |
| 53 | static inline int action_completed(struct cpu_action_queue *q, |
| 54 | struct cpu_action *action) |
| 55 | { |
| 56 | return load_acquire(&q->completed) == action; |
| 57 | } |
| 58 | |
| 59 | static inline void wait_for_action_queue_slot(struct cpu_action_queue *q) |
| 60 | { |
| 61 | while (!action_queue_empty(q)) |
| 62 | wfe(); |
| 63 | } |
| 64 | |
| 65 | static void wait_for_action_complete(struct cpu_action_queue *q, |
| 66 | struct cpu_action *a) |
| 67 | { |
| 68 | while (!action_completed(q, a)) |
| 69 | wfe(); |
| 70 | } |
| 71 | |
| 72 | static struct cpu_action *wait_for_action(struct cpu_action_queue *q, |
| 73 | struct cpu_action *local) |
| 74 | { |
| 75 | struct cpu_action *action; |
| 76 | |
| 77 | while (action_queue_empty(q)) |
| 78 | wfe(); |
| 79 | |
| 80 | /* |
| 81 | * Keep original address, but use a local copy for async processing. |
| 82 | */ |
| 83 | do { |
| 84 | action = load_acquire_exclusive(&q->todo); |
| 85 | *local = *action; |
| 86 | } while (!store_release_exclusive(&q->todo, NULL)); |
| 87 | |
| 88 | return action; |
| 89 | } |
| 90 | |
| 91 | static void queue_action(struct cpu_action_queue *q, struct cpu_action *action) |
| 92 | { |
| 93 | do { |
| 94 | wait_for_action_queue_slot(q); |
| 95 | if (load_acquire_exclusive(&q->todo) != NULL) |
| 96 | continue; |
| 97 | } while (!store_release_exclusive(&q->todo, action)); |
| 98 | } |
| 99 | |
| 100 | static void action_queue_complete(struct cpu_action_queue *q, |
| 101 | struct cpu_action *action) |
| 102 | { |
| 103 | /* Mark completion and send events to waiters. */ |
| 104 | store_release(&q->completed, action); |
| 105 | sev(); |
| 106 | } |
| 107 | |
| 108 | static void action_run(struct cpu_action *action) |
| 109 | { |
| 110 | action->run(action->arg); |
| 111 | } |
| 112 | |
| 113 | static void action_run_on_cpu(struct cpu_info *ci, struct cpu_action *action, |
| 114 | int sync) |
| 115 | { |
| 116 | struct cpu_action_queue *q = &ci->action_queue; |
| 117 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 118 | /* Don't run actions on non-online cpus. */ |
| 119 | if (!cpu_online(ci)) |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 120 | return; |
| 121 | |
| 122 | if (ci->id == smp_processor_id()) { |
| 123 | action->run(action->arg); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | queue_action(q, action); |
| 128 | /* Wait for CPU to pick it up. Empty slot means it was picked up. */ |
| 129 | wait_for_action_queue_slot(q); |
| 130 | /* Wait for completion if requested. */ |
| 131 | if (sync) |
| 132 | wait_for_action_complete(q, action); |
| 133 | } |
| 134 | |
| 135 | static int __arch_run_on_cpu(unsigned int cpu, struct cpu_action *action, |
| 136 | int sync) |
| 137 | { |
| 138 | struct cpu_info *ci; |
| 139 | |
| 140 | if (cpu >= CONFIG_MAX_CPUS) |
| 141 | return -1; |
| 142 | |
| 143 | ci = cpu_info_for_cpu(cpu); |
| 144 | |
| 145 | action_run_on_cpu(ci, action, sync); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action) |
| 151 | { |
| 152 | return __arch_run_on_cpu(cpu, action, 1); |
| 153 | } |
| 154 | |
| 155 | int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action) |
| 156 | { |
| 157 | return __arch_run_on_cpu(cpu, action, 0); |
| 158 | } |
| 159 | |
| 160 | static int __arch_run_on_all_cpus(struct cpu_action *action, int sync) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < CONFIG_MAX_CPUS; i++) |
| 165 | action_run_on_cpu(cpu_info_for_cpu(i), action, sync); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Aaron Durbin | cf5b627 | 2014-09-17 12:00:57 -0500 | [diff] [blame] | 170 | static int __arch_run_on_all_cpus_but_self(struct cpu_action *action, int sync) |
| 171 | { |
| 172 | int i; |
| 173 | struct cpu_info *me = cpu_info(); |
| 174 | |
| 175 | for (i = 0; i < CONFIG_MAX_CPUS; i++) { |
| 176 | struct cpu_info *ci = cpu_info_for_cpu(i); |
| 177 | if (ci == me) |
| 178 | continue; |
| 179 | action_run_on_cpu(ci, action, sync); |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 185 | int arch_run_on_all_cpus(struct cpu_action *action) |
| 186 | { |
| 187 | return __arch_run_on_all_cpus(action, 1); |
| 188 | } |
| 189 | |
| 190 | int arch_run_on_all_cpus_async(struct cpu_action *action) |
| 191 | { |
| 192 | return __arch_run_on_all_cpus(action, 0); |
| 193 | } |
| 194 | |
Aaron Durbin | cf5b627 | 2014-09-17 12:00:57 -0500 | [diff] [blame] | 195 | int arch_run_on_all_cpus_but_self(struct cpu_action *action) |
| 196 | { |
| 197 | return __arch_run_on_all_cpus_but_self(action, 1); |
| 198 | } |
| 199 | |
| 200 | int arch_run_on_all_cpus_but_self_async(struct cpu_action *action) |
| 201 | { |
| 202 | return __arch_run_on_all_cpus_but_self(action, 0); |
| 203 | } |
| 204 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 205 | |
| 206 | void arch_cpu_wait_for_action(void) |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 207 | { |
| 208 | struct cpu_info *ci = cpu_info(); |
| 209 | struct cpu_action_queue *q = &ci->action_queue; |
| 210 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 211 | while (1) { |
| 212 | struct cpu_action *orig; |
| 213 | struct cpu_action action; |
| 214 | |
| 215 | orig = wait_for_action(q, &action); |
| 216 | |
| 217 | action_run(&action); |
| 218 | action_queue_complete(q, orig); |
| 219 | } |
| 220 | } |