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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
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> |
Aaron Durbin | 168eda5 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 25 | #include <smp/node.h> |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 26 | #include "cpu-internal.h" |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 27 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 28 | struct cpu_info cpu_infos[CONFIG_MAX_CPUS]; |
Aaron Durbin | 74ff69f | 2014-09-17 11:51:41 -0500 | [diff] [blame] | 29 | struct cpu_info *bsp_cpu_info; |
Aaron Durbin | 1b315d0 | 2014-08-27 10:30:39 -0500 | [diff] [blame] | 30 | |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 31 | struct cpu_info *cpu_info(void) |
| 32 | { |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 33 | return cpu_info_for_cpu(smp_processor_id()); |
| 34 | } |
| 35 | |
Aaron Durbin | 931a218 | 2014-11-05 11:19:21 -0600 | [diff] [blame] | 36 | size_t cpus_online(void) |
| 37 | { |
| 38 | int i; |
| 39 | size_t num = 0; |
| 40 | |
| 41 | for (i = 0; i < ARRAY_SIZE(cpu_infos); i++) { |
| 42 | if (cpu_online(cpu_info_for_cpu(i))) |
| 43 | num++; |
| 44 | } |
| 45 | |
| 46 | return num; |
| 47 | } |
| 48 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 49 | static inline int action_queue_empty(struct cpu_action_queue *q) |
| 50 | { |
| 51 | return load_acquire_exclusive(&q->todo) == NULL; |
| 52 | } |
| 53 | |
| 54 | static inline int action_completed(struct cpu_action_queue *q, |
| 55 | struct cpu_action *action) |
| 56 | { |
| 57 | return load_acquire(&q->completed) == action; |
| 58 | } |
| 59 | |
| 60 | static inline void wait_for_action_queue_slot(struct cpu_action_queue *q) |
| 61 | { |
| 62 | while (!action_queue_empty(q)) |
| 63 | wfe(); |
| 64 | } |
| 65 | |
| 66 | static void wait_for_action_complete(struct cpu_action_queue *q, |
| 67 | struct cpu_action *a) |
| 68 | { |
| 69 | while (!action_completed(q, a)) |
| 70 | wfe(); |
| 71 | } |
| 72 | |
| 73 | static struct cpu_action *wait_for_action(struct cpu_action_queue *q, |
| 74 | struct cpu_action *local) |
| 75 | { |
| 76 | struct cpu_action *action; |
| 77 | |
| 78 | while (action_queue_empty(q)) |
| 79 | wfe(); |
| 80 | |
| 81 | /* |
| 82 | * Keep original address, but use a local copy for async processing. |
| 83 | */ |
| 84 | do { |
| 85 | action = load_acquire_exclusive(&q->todo); |
| 86 | *local = *action; |
| 87 | } while (!store_release_exclusive(&q->todo, NULL)); |
| 88 | |
| 89 | return action; |
| 90 | } |
| 91 | |
| 92 | static void queue_action(struct cpu_action_queue *q, struct cpu_action *action) |
| 93 | { |
| 94 | do { |
| 95 | wait_for_action_queue_slot(q); |
| 96 | if (load_acquire_exclusive(&q->todo) != NULL) |
| 97 | continue; |
| 98 | } while (!store_release_exclusive(&q->todo, action)); |
| 99 | } |
| 100 | |
| 101 | static void action_queue_complete(struct cpu_action_queue *q, |
| 102 | struct cpu_action *action) |
| 103 | { |
| 104 | /* Mark completion and send events to waiters. */ |
| 105 | store_release(&q->completed, action); |
| 106 | sev(); |
| 107 | } |
| 108 | |
| 109 | static void action_run(struct cpu_action *action) |
| 110 | { |
| 111 | action->run(action->arg); |
| 112 | } |
| 113 | |
| 114 | static void action_run_on_cpu(struct cpu_info *ci, struct cpu_action *action, |
| 115 | int sync) |
| 116 | { |
| 117 | struct cpu_action_queue *q = &ci->action_queue; |
| 118 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 119 | /* Don't run actions on non-online cpus. */ |
| 120 | if (!cpu_online(ci)) |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 121 | return; |
| 122 | |
| 123 | if (ci->id == smp_processor_id()) { |
| 124 | action->run(action->arg); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | queue_action(q, action); |
| 129 | /* Wait for CPU to pick it up. Empty slot means it was picked up. */ |
| 130 | wait_for_action_queue_slot(q); |
| 131 | /* Wait for completion if requested. */ |
| 132 | if (sync) |
| 133 | wait_for_action_complete(q, action); |
| 134 | } |
| 135 | |
| 136 | static int __arch_run_on_cpu(unsigned int cpu, struct cpu_action *action, |
| 137 | int sync) |
| 138 | { |
| 139 | struct cpu_info *ci; |
| 140 | |
| 141 | if (cpu >= CONFIG_MAX_CPUS) |
| 142 | return -1; |
| 143 | |
| 144 | ci = cpu_info_for_cpu(cpu); |
| 145 | |
| 146 | action_run_on_cpu(ci, action, sync); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action) |
| 152 | { |
| 153 | return __arch_run_on_cpu(cpu, action, 1); |
| 154 | } |
| 155 | |
| 156 | int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action) |
| 157 | { |
| 158 | return __arch_run_on_cpu(cpu, action, 0); |
| 159 | } |
| 160 | |
| 161 | static int __arch_run_on_all_cpus(struct cpu_action *action, int sync) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i < CONFIG_MAX_CPUS; i++) |
| 166 | action_run_on_cpu(cpu_info_for_cpu(i), action, sync); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Aaron Durbin | cf5b627 | 2014-09-17 12:00:57 -0500 | [diff] [blame] | 171 | static int __arch_run_on_all_cpus_but_self(struct cpu_action *action, int sync) |
| 172 | { |
| 173 | int i; |
| 174 | struct cpu_info *me = cpu_info(); |
| 175 | |
| 176 | for (i = 0; i < CONFIG_MAX_CPUS; i++) { |
| 177 | struct cpu_info *ci = cpu_info_for_cpu(i); |
| 178 | if (ci == me) |
| 179 | continue; |
| 180 | action_run_on_cpu(ci, action, sync); |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 186 | int arch_run_on_all_cpus(struct cpu_action *action) |
| 187 | { |
| 188 | return __arch_run_on_all_cpus(action, 1); |
| 189 | } |
| 190 | |
| 191 | int arch_run_on_all_cpus_async(struct cpu_action *action) |
| 192 | { |
| 193 | return __arch_run_on_all_cpus(action, 0); |
| 194 | } |
| 195 | |
Aaron Durbin | cf5b627 | 2014-09-17 12:00:57 -0500 | [diff] [blame] | 196 | int arch_run_on_all_cpus_but_self(struct cpu_action *action) |
| 197 | { |
| 198 | return __arch_run_on_all_cpus_but_self(action, 1); |
| 199 | } |
| 200 | |
| 201 | int arch_run_on_all_cpus_but_self_async(struct cpu_action *action) |
| 202 | { |
| 203 | return __arch_run_on_all_cpus_but_self(action, 0); |
| 204 | } |
| 205 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 206 | |
| 207 | void arch_cpu_wait_for_action(void) |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 208 | { |
| 209 | struct cpu_info *ci = cpu_info(); |
| 210 | struct cpu_action_queue *q = &ci->action_queue; |
| 211 | |
Aaron Durbin | 9fd4dc7 | 2014-09-06 02:31:30 -0500 | [diff] [blame] | 212 | while (1) { |
| 213 | struct cpu_action *orig; |
| 214 | struct cpu_action action; |
| 215 | |
| 216 | orig = wait_for_action(q, &action); |
| 217 | |
| 218 | action_run(&action); |
| 219 | action_queue_complete(q, orig); |
| 220 | } |
| 221 | } |
Aaron Durbin | 168eda5 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 222 | |
| 223 | int boot_cpu(void) |
| 224 | { |
| 225 | return cpu_is_bsp(); |
| 226 | } |