Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SN Platform GRU Driver |
| 3 | * |
| 4 | * KERNEL SERVICES THAT USE THE GRU |
| 5 | * |
| 6 | * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/mm.h> |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/uaccess.h> |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 34 | #include "gru.h" |
| 35 | #include "grulib.h" |
| 36 | #include "grutables.h" |
| 37 | #include "grukservices.h" |
| 38 | #include "gru_instructions.h" |
| 39 | #include <asm/uv/uv_hub.h> |
| 40 | |
| 41 | /* |
| 42 | * Kernel GRU Usage |
| 43 | * |
| 44 | * The following is an interim algorithm for management of kernel GRU |
| 45 | * resources. This will likely be replaced when we better understand the |
| 46 | * kernel/user requirements. |
| 47 | * |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 48 | * Blade percpu resources reserved for kernel use. These resources are |
| 49 | * reserved whenever the the kernel context for the blade is loaded. Note |
| 50 | * that the kernel context is not guaranteed to be always available. It is |
| 51 | * loaded on demand & can be stolen by a user if the user demand exceeds the |
| 52 | * kernel demand. The kernel can always reload the kernel context but |
| 53 | * a SLEEP may be required!!!. |
Jack Steiner | 9120dec | 2009-06-17 16:28:25 -0700 | [diff] [blame] | 54 | * |
| 55 | * Async Overview: |
| 56 | * |
| 57 | * Each blade has one "kernel context" that owns GRU kernel resources |
| 58 | * located on the blade. Kernel drivers use GRU resources in this context |
| 59 | * for sending messages, zeroing memory, etc. |
| 60 | * |
| 61 | * The kernel context is dynamically loaded on demand. If it is not in |
| 62 | * use by the kernel, the kernel context can be unloaded & given to a user. |
| 63 | * The kernel context will be reloaded when needed. This may require that |
| 64 | * a context be stolen from a user. |
| 65 | * NOTE: frequent unloading/reloading of the kernel context is |
| 66 | * expensive. We are depending on batch schedulers, cpusets, sane |
| 67 | * drivers or some other mechanism to prevent the need for frequent |
| 68 | * stealing/reloading. |
| 69 | * |
| 70 | * The kernel context consists of two parts: |
| 71 | * - 1 CB & a few DSRs that are reserved for each cpu on the blade. |
| 72 | * Each cpu has it's own private resources & does not share them |
| 73 | * with other cpus. These resources are used serially, ie, |
| 74 | * locked, used & unlocked on each call to a function in |
| 75 | * grukservices. |
| 76 | * (Now that we have dynamic loading of kernel contexts, I |
| 77 | * may rethink this & allow sharing between cpus....) |
| 78 | * |
| 79 | * - Additional resources can be reserved long term & used directly |
| 80 | * by UV drivers located in the kernel. Drivers using these GRU |
| 81 | * resources can use asynchronous GRU instructions that send |
| 82 | * interrupts on completion. |
| 83 | * - these resources must be explicitly locked/unlocked |
| 84 | * - locked resources prevent (obviously) the kernel |
| 85 | * context from being unloaded. |
| 86 | * - drivers using these resource directly issue their own |
| 87 | * GRU instruction and must wait/check completion. |
| 88 | * |
| 89 | * When these resources are reserved, the caller can optionally |
| 90 | * associate a wait_queue with the resources and use asynchronous |
| 91 | * GRU instructions. When an async GRU instruction completes, the |
| 92 | * driver will do a wakeup on the event. |
| 93 | * |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 94 | */ |
Jack Steiner | 9120dec | 2009-06-17 16:28:25 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | #define ASYNC_HAN_TO_BID(h) ((h) - 1) |
| 98 | #define ASYNC_BID_TO_HAN(b) ((b) + 1) |
| 99 | #define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)] |
| 100 | |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 101 | #define GRU_NUM_KERNEL_CBR 1 |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 102 | #define GRU_NUM_KERNEL_DSR_BYTES 256 |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 103 | #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \ |
| 104 | GRU_CACHE_LINE_BYTES) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 105 | |
| 106 | /* GRU instruction attributes for all instructions */ |
| 107 | #define IMA IMA_CB_DELAY |
| 108 | |
| 109 | /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */ |
| 110 | #define __gru_cacheline_aligned__ \ |
| 111 | __attribute__((__aligned__(GRU_CACHE_LINE_BYTES))) |
| 112 | |
| 113 | #define MAGIC 0x1234567887654321UL |
| 114 | |
| 115 | /* Default retry count for GRU errors on kernel instructions */ |
| 116 | #define EXCEPTION_RETRY_LIMIT 3 |
| 117 | |
| 118 | /* Status of message queue sections */ |
| 119 | #define MQS_EMPTY 0 |
| 120 | #define MQS_FULL 1 |
| 121 | #define MQS_NOOP 2 |
| 122 | |
| 123 | /*----------------- RESOURCE MANAGEMENT -------------------------------------*/ |
| 124 | /* optimized for x86_64 */ |
| 125 | struct message_queue { |
| 126 | union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */ |
| 127 | int qlines; /* DW 1 */ |
| 128 | long hstatus[2]; |
| 129 | void *next __gru_cacheline_aligned__;/* CL 1 */ |
| 130 | void *limit; |
| 131 | void *start; |
| 132 | void *start2; |
| 133 | char data ____cacheline_aligned; /* CL 2 */ |
| 134 | }; |
| 135 | |
| 136 | /* First word in every message - used by mesq interface */ |
| 137 | struct message_header { |
| 138 | char present; |
| 139 | char present2; |
| 140 | char lines; |
| 141 | char fill; |
| 142 | }; |
| 143 | |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 144 | #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h])) |
| 145 | |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 146 | /* |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 147 | * Reload the blade's kernel context into a GRU chiplet. Called holding |
| 148 | * the bs_kgts_sema for READ. Will steal user contexts if necessary. |
| 149 | */ |
| 150 | static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id) |
| 151 | { |
| 152 | struct gru_state *gru; |
| 153 | struct gru_thread_state *kgts; |
| 154 | void *vaddr; |
Jack Steiner | 9120dec | 2009-06-17 16:28:25 -0700 | [diff] [blame] | 155 | int ctxnum, ncpus; |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 156 | |
| 157 | up_read(&bs->bs_kgts_sema); |
| 158 | down_write(&bs->bs_kgts_sema); |
| 159 | |
Jack Steiner | 55484c4 | 2009-12-15 16:48:05 -0800 | [diff] [blame] | 160 | if (!bs->bs_kgts) { |
Jack Steiner | c550222 | 2009-12-15 16:48:13 -0800 | [diff] [blame] | 161 | bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0, 0); |
Jack Steiner | 55484c4 | 2009-12-15 16:48:05 -0800 | [diff] [blame] | 162 | bs->bs_kgts->ts_user_blade_id = blade_id; |
| 163 | } |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 164 | kgts = bs->bs_kgts; |
| 165 | |
| 166 | if (!kgts->ts_gru) { |
| 167 | STAT(load_kernel_context); |
Jack Steiner | 9120dec | 2009-06-17 16:28:25 -0700 | [diff] [blame] | 168 | ncpus = uv_blade_nr_possible_cpus(blade_id); |
| 169 | kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU( |
| 170 | GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs); |
| 171 | kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU( |
| 172 | GRU_NUM_KERNEL_DSR_BYTES * ncpus + |
| 173 | bs->bs_async_dsr_bytes); |
Jack Steiner | 55484c4 | 2009-12-15 16:48:05 -0800 | [diff] [blame] | 174 | while (!gru_assign_gru_context(kgts)) { |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 175 | msleep(1); |
Jack Steiner | 55484c4 | 2009-12-15 16:48:05 -0800 | [diff] [blame] | 176 | gru_steal_context(kgts); |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 177 | } |
| 178 | gru_load_context(kgts); |
| 179 | gru = bs->bs_kgts->ts_gru; |
| 180 | vaddr = gru->gs_gru_base_vaddr; |
| 181 | ctxnum = kgts->ts_ctxnum; |
| 182 | bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0); |
| 183 | bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0); |
| 184 | } |
| 185 | downgrade_write(&bs->bs_kgts_sema); |
| 186 | } |
| 187 | |
| 188 | /* |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 189 | * Free all kernel contexts that are not currently in use. |
| 190 | * Returns 0 if all freed, else number of inuse context. |
| 191 | */ |
| 192 | static int gru_free_kernel_contexts(void) |
| 193 | { |
| 194 | struct gru_blade_state *bs; |
| 195 | struct gru_thread_state *kgts; |
| 196 | int bid, ret = 0; |
| 197 | |
| 198 | for (bid = 0; bid < GRU_MAX_BLADES; bid++) { |
| 199 | bs = gru_base[bid]; |
| 200 | if (!bs) |
| 201 | continue; |
Jack Steiner | 091f1a1 | 2009-12-15 16:48:02 -0800 | [diff] [blame] | 202 | |
| 203 | /* Ignore busy contexts. Don't want to block here. */ |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 204 | if (down_write_trylock(&bs->bs_kgts_sema)) { |
| 205 | kgts = bs->bs_kgts; |
| 206 | if (kgts && kgts->ts_gru) |
| 207 | gru_unload_context(kgts, 0); |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 208 | bs->bs_kgts = NULL; |
| 209 | up_write(&bs->bs_kgts_sema); |
Jack Steiner | 091f1a1 | 2009-12-15 16:48:02 -0800 | [diff] [blame] | 210 | kfree(kgts); |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 211 | } else { |
| 212 | ret++; |
| 213 | } |
| 214 | } |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /* |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 219 | * Lock & load the kernel context for the specified blade. |
| 220 | */ |
| 221 | static struct gru_blade_state *gru_lock_kernel_context(int blade_id) |
| 222 | { |
| 223 | struct gru_blade_state *bs; |
| 224 | |
| 225 | STAT(lock_kernel_context); |
| 226 | bs = gru_base[blade_id]; |
| 227 | |
| 228 | down_read(&bs->bs_kgts_sema); |
| 229 | if (!bs->bs_kgts || !bs->bs_kgts->ts_gru) |
| 230 | gru_load_kernel_context(bs, blade_id); |
| 231 | return bs; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Unlock the kernel context for the specified blade. Context is not |
| 237 | * unloaded but may be stolen before next use. |
| 238 | */ |
| 239 | static void gru_unlock_kernel_context(int blade_id) |
| 240 | { |
| 241 | struct gru_blade_state *bs; |
| 242 | |
| 243 | bs = gru_base[blade_id]; |
| 244 | up_read(&bs->bs_kgts_sema); |
| 245 | STAT(unlock_kernel_context); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Reserve & get pointers to the DSR/CBRs reserved for the current cpu. |
| 250 | * - returns with preemption disabled |
| 251 | */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 252 | static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr) |
| 253 | { |
| 254 | struct gru_blade_state *bs; |
| 255 | int lcpu; |
| 256 | |
| 257 | BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES); |
| 258 | preempt_disable(); |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 259 | bs = gru_lock_kernel_context(uv_numa_blade_id()); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 260 | lcpu = uv_blade_processor_id(); |
| 261 | *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE; |
| 262 | *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES; |
| 263 | return 0; |
| 264 | } |
| 265 | |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 266 | /* |
| 267 | * Free the current cpus reserved DSR/CBR resources. |
| 268 | */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 269 | static void gru_free_cpu_resources(void *cb, void *dsr) |
| 270 | { |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 271 | gru_unlock_kernel_context(uv_numa_blade_id()); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 272 | preempt_enable(); |
| 273 | } |
| 274 | |
Jack Steiner | 9120dec | 2009-06-17 16:28:25 -0700 | [diff] [blame] | 275 | /* |
| 276 | * Reserve GRU resources to be used asynchronously. |
| 277 | * Note: currently supports only 1 reservation per blade. |
| 278 | * |
| 279 | * input: |
| 280 | * blade_id - blade on which resources should be reserved |
| 281 | * cbrs - number of CBRs |
| 282 | * dsr_bytes - number of DSR bytes needed |
| 283 | * output: |
| 284 | * handle to identify resource |
| 285 | * (0 = async resources already reserved) |
| 286 | */ |
| 287 | unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes, |
| 288 | struct completion *cmp) |
| 289 | { |
| 290 | struct gru_blade_state *bs; |
| 291 | struct gru_thread_state *kgts; |
| 292 | int ret = 0; |
| 293 | |
| 294 | bs = gru_base[blade_id]; |
| 295 | |
| 296 | down_write(&bs->bs_kgts_sema); |
| 297 | |
| 298 | /* Verify no resources already reserved */ |
| 299 | if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs) |
| 300 | goto done; |
| 301 | bs->bs_async_dsr_bytes = dsr_bytes; |
| 302 | bs->bs_async_cbrs = cbrs; |
| 303 | bs->bs_async_wq = cmp; |
| 304 | kgts = bs->bs_kgts; |
| 305 | |
| 306 | /* Resources changed. Unload context if already loaded */ |
| 307 | if (kgts && kgts->ts_gru) |
| 308 | gru_unload_context(kgts, 0); |
| 309 | ret = ASYNC_BID_TO_HAN(blade_id); |
| 310 | |
| 311 | done: |
| 312 | up_write(&bs->bs_kgts_sema); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Release async resources previously reserved. |
| 318 | * |
| 319 | * input: |
| 320 | * han - handle to identify resources |
| 321 | */ |
| 322 | void gru_release_async_resources(unsigned long han) |
| 323 | { |
| 324 | struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han); |
| 325 | |
| 326 | down_write(&bs->bs_kgts_sema); |
| 327 | bs->bs_async_dsr_bytes = 0; |
| 328 | bs->bs_async_cbrs = 0; |
| 329 | bs->bs_async_wq = NULL; |
| 330 | up_write(&bs->bs_kgts_sema); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Wait for async GRU instructions to complete. |
| 335 | * |
| 336 | * input: |
| 337 | * han - handle to identify resources |
| 338 | */ |
| 339 | void gru_wait_async_cbr(unsigned long han) |
| 340 | { |
| 341 | struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han); |
| 342 | |
| 343 | wait_for_completion(bs->bs_async_wq); |
| 344 | mb(); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Lock previous reserved async GRU resources |
| 349 | * |
| 350 | * input: |
| 351 | * han - handle to identify resources |
| 352 | * output: |
| 353 | * cb - pointer to first CBR |
| 354 | * dsr - pointer to first DSR |
| 355 | */ |
| 356 | void gru_lock_async_resource(unsigned long han, void **cb, void **dsr) |
| 357 | { |
| 358 | struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han); |
| 359 | int blade_id = ASYNC_HAN_TO_BID(han); |
| 360 | int ncpus; |
| 361 | |
| 362 | gru_lock_kernel_context(blade_id); |
| 363 | ncpus = uv_blade_nr_possible_cpus(blade_id); |
| 364 | if (cb) |
| 365 | *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE; |
| 366 | if (dsr) |
| 367 | *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Unlock previous reserved async GRU resources |
| 372 | * |
| 373 | * input: |
| 374 | * han - handle to identify resources |
| 375 | */ |
| 376 | void gru_unlock_async_resource(unsigned long han) |
| 377 | { |
| 378 | int blade_id = ASYNC_HAN_TO_BID(han); |
| 379 | |
| 380 | gru_unlock_kernel_context(blade_id); |
| 381 | } |
| 382 | |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 383 | /*----------------------------------------------------------------------*/ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 384 | int gru_get_cb_exception_detail(void *cb, |
| 385 | struct control_block_extended_exc_detail *excdet) |
| 386 | { |
| 387 | struct gru_control_block_extended *cbe; |
Jack Steiner | 1848a71 | 2009-12-15 16:48:14 -0800 | [diff] [blame^] | 388 | struct gru_thread_state *kgts = NULL; |
| 389 | unsigned long off; |
| 390 | int cbrnum, bid; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 391 | |
Jack Steiner | 1848a71 | 2009-12-15 16:48:14 -0800 | [diff] [blame^] | 392 | /* |
| 393 | * Locate kgts for cb. This algorithm is SLOW but |
| 394 | * this function is rarely called (ie., almost never). |
| 395 | * Performance does not matter. |
| 396 | */ |
| 397 | for_each_possible_blade(bid) { |
| 398 | if (!gru_base[bid]) |
| 399 | break; |
| 400 | kgts = gru_base[bid]->bs_kgts; |
| 401 | if (!kgts || !kgts->ts_gru) |
| 402 | continue; |
| 403 | off = cb - kgts->ts_gru->gs_gru_base_vaddr; |
| 404 | if (off < GRU_SIZE) |
| 405 | break; |
| 406 | kgts = NULL; |
| 407 | } |
| 408 | BUG_ON(!kgts); |
| 409 | cbrnum = thread_cbr_number(kgts, get_cb_number(cb)); |
Jack Steiner | 1a2c09e | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 410 | cbe = get_cbe(GRUBASE(cb), cbrnum); |
| 411 | gru_flush_cache(cbe); /* CBE not coherent */ |
Jack Steiner | 67bf04a | 2009-12-15 16:48:11 -0800 | [diff] [blame] | 412 | sync_core(); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 413 | excdet->opc = cbe->opccpy; |
| 414 | excdet->exopc = cbe->exopccpy; |
| 415 | excdet->ecause = cbe->ecause; |
| 416 | excdet->exceptdet0 = cbe->idef1upd; |
| 417 | excdet->exceptdet1 = cbe->idef3upd; |
Jack Steiner | 1a2c09e | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 418 | gru_flush_cache(cbe); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | char *gru_get_cb_exception_detail_str(int ret, void *cb, |
| 423 | char *buf, int size) |
| 424 | { |
| 425 | struct gru_control_block_status *gen = (void *)cb; |
| 426 | struct control_block_extended_exc_detail excdet; |
| 427 | |
| 428 | if (ret > 0 && gen->istatus == CBS_EXCEPTION) { |
| 429 | gru_get_cb_exception_detail(cb, &excdet); |
| 430 | snprintf(buf, size, |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 431 | "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x," |
| 432 | "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(), |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 433 | gen, excdet.opc, excdet.exopc, excdet.ecause, |
| 434 | excdet.exceptdet0, excdet.exceptdet1); |
| 435 | } else { |
| 436 | snprintf(buf, size, "No exception"); |
| 437 | } |
| 438 | return buf; |
| 439 | } |
| 440 | |
| 441 | static int gru_wait_idle_or_exception(struct gru_control_block_status *gen) |
| 442 | { |
| 443 | while (gen->istatus >= CBS_ACTIVE) { |
| 444 | cpu_relax(); |
| 445 | barrier(); |
| 446 | } |
| 447 | return gen->istatus; |
| 448 | } |
| 449 | |
| 450 | static int gru_retry_exception(void *cb) |
| 451 | { |
| 452 | struct gru_control_block_status *gen = (void *)cb; |
| 453 | struct control_block_extended_exc_detail excdet; |
| 454 | int retry = EXCEPTION_RETRY_LIMIT; |
| 455 | |
| 456 | while (1) { |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 457 | if (gru_wait_idle_or_exception(gen) == CBS_IDLE) |
| 458 | return CBS_IDLE; |
Jack Steiner | d6e2fbc | 2009-06-17 16:28:29 -0700 | [diff] [blame] | 459 | if (gru_get_cb_message_queue_substatus(cb)) |
| 460 | return CBS_EXCEPTION; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 461 | gru_get_cb_exception_detail(cb, &excdet); |
Jack Steiner | 270952a | 2009-06-17 16:28:27 -0700 | [diff] [blame] | 462 | if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) || |
| 463 | (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC)) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 464 | break; |
| 465 | if (retry-- == 0) |
| 466 | break; |
| 467 | gen->icmd = 1; |
| 468 | gru_flush_cache(gen); |
| 469 | } |
| 470 | return CBS_EXCEPTION; |
| 471 | } |
| 472 | |
| 473 | int gru_check_status_proc(void *cb) |
| 474 | { |
| 475 | struct gru_control_block_status *gen = (void *)cb; |
| 476 | int ret; |
| 477 | |
| 478 | ret = gen->istatus; |
Jack Steiner | 67bf04a | 2009-12-15 16:48:11 -0800 | [diff] [blame] | 479 | if (ret == CBS_EXCEPTION) |
| 480 | ret = gru_retry_exception(cb); |
| 481 | rmb(); |
| 482 | return ret; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 483 | |
| 484 | } |
| 485 | |
| 486 | int gru_wait_proc(void *cb) |
| 487 | { |
| 488 | struct gru_control_block_status *gen = (void *)cb; |
| 489 | int ret; |
| 490 | |
| 491 | ret = gru_wait_idle_or_exception(gen); |
| 492 | if (ret == CBS_EXCEPTION) |
| 493 | ret = gru_retry_exception(cb); |
Jack Steiner | 67bf04a | 2009-12-15 16:48:11 -0800 | [diff] [blame] | 494 | rmb(); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | void gru_abort(int ret, void *cb, char *str) |
| 499 | { |
| 500 | char buf[GRU_EXC_STR_SIZE]; |
| 501 | |
| 502 | panic("GRU FATAL ERROR: %s - %s\n", str, |
| 503 | gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf))); |
| 504 | } |
| 505 | |
| 506 | void gru_wait_abort_proc(void *cb) |
| 507 | { |
| 508 | int ret; |
| 509 | |
| 510 | ret = gru_wait_proc(cb); |
| 511 | if (ret) |
| 512 | gru_abort(ret, cb, "gru_wait_abort"); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /*------------------------------ MESSAGE QUEUES -----------------------------*/ |
| 517 | |
| 518 | /* Internal status . These are NOT returned to the user. */ |
| 519 | #define MQIE_AGAIN -1 /* try again */ |
| 520 | |
| 521 | |
| 522 | /* |
| 523 | * Save/restore the "present" flag that is in the second line of 2-line |
| 524 | * messages |
| 525 | */ |
| 526 | static inline int get_present2(void *p) |
| 527 | { |
| 528 | struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES; |
| 529 | return mhdr->present; |
| 530 | } |
| 531 | |
| 532 | static inline void restore_present2(void *p, int val) |
| 533 | { |
| 534 | struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES; |
| 535 | mhdr->present = val; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Create a message queue. |
| 540 | * qlines - message queue size in cache lines. Includes 2-line header. |
| 541 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 542 | int gru_create_message_queue(struct gru_message_queue_desc *mqd, |
| 543 | void *p, unsigned int bytes, int nasid, int vector, int apicid) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 544 | { |
| 545 | struct message_queue *mq = p; |
| 546 | unsigned int qlines; |
| 547 | |
| 548 | qlines = bytes / GRU_CACHE_LINE_BYTES - 2; |
| 549 | memset(mq, 0, bytes); |
| 550 | mq->start = &mq->data; |
| 551 | mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES; |
| 552 | mq->next = &mq->data; |
| 553 | mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES; |
| 554 | mq->qlines = qlines; |
| 555 | mq->hstatus[0] = 0; |
| 556 | mq->hstatus[1] = 1; |
| 557 | mq->head = gru_mesq_head(2, qlines / 2 + 1); |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 558 | mqd->mq = mq; |
| 559 | mqd->mq_gpa = uv_gpa(mq); |
| 560 | mqd->qlines = qlines; |
| 561 | mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid); |
| 562 | mqd->interrupt_vector = vector; |
| 563 | mqd->interrupt_apicid = apicid; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(gru_create_message_queue); |
| 567 | |
| 568 | /* |
| 569 | * Send a NOOP message to a message queue |
| 570 | * Returns: |
| 571 | * 0 - if queue is full after the send. This is the normal case |
| 572 | * but various races can change this. |
| 573 | * -1 - if mesq sent successfully but queue not full |
| 574 | * >0 - unexpected error. MQE_xxx returned |
| 575 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 576 | static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd, |
| 577 | void *mesg) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 578 | { |
| 579 | const struct message_header noop_header = { |
| 580 | .present = MQS_NOOP, .lines = 1}; |
| 581 | unsigned long m; |
| 582 | int substatus, ret; |
| 583 | struct message_header save_mhdr, *mhdr = mesg; |
| 584 | |
| 585 | STAT(mesq_noop); |
| 586 | save_mhdr = *mhdr; |
| 587 | *mhdr = noop_header; |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 588 | gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 589 | ret = gru_wait(cb); |
| 590 | |
| 591 | if (ret) { |
| 592 | substatus = gru_get_cb_message_queue_substatus(cb); |
| 593 | switch (substatus) { |
| 594 | case CBSS_NO_ERROR: |
| 595 | STAT(mesq_noop_unexpected_error); |
| 596 | ret = MQE_UNEXPECTED_CB_ERR; |
| 597 | break; |
| 598 | case CBSS_LB_OVERFLOWED: |
| 599 | STAT(mesq_noop_lb_overflow); |
| 600 | ret = MQE_CONGESTION; |
| 601 | break; |
| 602 | case CBSS_QLIMIT_REACHED: |
| 603 | STAT(mesq_noop_qlimit_reached); |
| 604 | ret = 0; |
| 605 | break; |
| 606 | case CBSS_AMO_NACKED: |
| 607 | STAT(mesq_noop_amo_nacked); |
| 608 | ret = MQE_CONGESTION; |
| 609 | break; |
| 610 | case CBSS_PUT_NACKED: |
| 611 | STAT(mesq_noop_put_nacked); |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 612 | m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 613 | gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1, |
| 614 | IMA); |
| 615 | if (gru_wait(cb) == CBS_IDLE) |
| 616 | ret = MQIE_AGAIN; |
| 617 | else |
| 618 | ret = MQE_UNEXPECTED_CB_ERR; |
| 619 | break; |
| 620 | case CBSS_PAGE_OVERFLOW: |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 621 | STAT(mesq_noop_page_overflow); |
| 622 | /* fallthru */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 623 | default: |
| 624 | BUG(); |
| 625 | } |
| 626 | } |
| 627 | *mhdr = save_mhdr; |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Handle a gru_mesq full. |
| 633 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 634 | static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd, |
| 635 | void *mesg, int lines) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 636 | { |
| 637 | union gru_mesqhead mqh; |
| 638 | unsigned int limit, head; |
| 639 | unsigned long avalue; |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 640 | int half, qlines; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 641 | |
| 642 | /* Determine if switching to first/second half of q */ |
| 643 | avalue = gru_get_amo_value(cb); |
| 644 | head = gru_get_amo_value_head(cb); |
| 645 | limit = gru_get_amo_value_limit(cb); |
| 646 | |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 647 | qlines = mqd->qlines; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 648 | half = (limit != qlines); |
| 649 | |
| 650 | if (half) |
| 651 | mqh = gru_mesq_head(qlines / 2 + 1, qlines); |
| 652 | else |
| 653 | mqh = gru_mesq_head(2, qlines / 2 + 1); |
| 654 | |
| 655 | /* Try to get lock for switching head pointer */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 656 | gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 657 | if (gru_wait(cb) != CBS_IDLE) |
| 658 | goto cberr; |
| 659 | if (!gru_get_amo_value(cb)) { |
| 660 | STAT(mesq_qf_locked); |
| 661 | return MQE_QUEUE_FULL; |
| 662 | } |
| 663 | |
| 664 | /* Got the lock. Send optional NOP if queue not full, */ |
| 665 | if (head != limit) { |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 666 | if (send_noop_message(cb, mqd, mesg)) { |
| 667 | gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 668 | XTYPE_DW, IMA); |
| 669 | if (gru_wait(cb) != CBS_IDLE) |
| 670 | goto cberr; |
| 671 | STAT(mesq_qf_noop_not_full); |
| 672 | return MQIE_AGAIN; |
| 673 | } |
| 674 | avalue++; |
| 675 | } |
| 676 | |
| 677 | /* Then flip queuehead to other half of queue. */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 678 | gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue, |
| 679 | IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 680 | if (gru_wait(cb) != CBS_IDLE) |
| 681 | goto cberr; |
| 682 | |
| 683 | /* If not successfully in swapping queue head, clear the hstatus lock */ |
| 684 | if (gru_get_amo_value(cb) != avalue) { |
| 685 | STAT(mesq_qf_switch_head_failed); |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 686 | gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, |
| 687 | IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 688 | if (gru_wait(cb) != CBS_IDLE) |
| 689 | goto cberr; |
| 690 | } |
| 691 | return MQIE_AGAIN; |
| 692 | cberr: |
| 693 | STAT(mesq_qf_unexpected_error); |
| 694 | return MQE_UNEXPECTED_CB_ERR; |
| 695 | } |
| 696 | |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 697 | /* |
| 698 | * Send a cross-partition interrupt to the SSI that contains the target |
| 699 | * message queue. Normally, the interrupt is automatically delivered by hardware |
| 700 | * but some error conditions require explicit delivery. |
| 701 | */ |
| 702 | static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd) |
| 703 | { |
| 704 | if (mqd->interrupt_vector) |
| 705 | uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid, |
| 706 | mqd->interrupt_vector); |
| 707 | } |
| 708 | |
Jack Steiner | 17b49a6 | 2009-06-17 16:28:23 -0700 | [diff] [blame] | 709 | /* |
| 710 | * Handle a PUT failure. Note: if message was a 2-line message, one of the |
| 711 | * lines might have successfully have been written. Before sending the |
| 712 | * message, "present" must be cleared in BOTH lines to prevent the receiver |
| 713 | * from prematurely seeing the full message. |
| 714 | */ |
| 715 | static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd, |
| 716 | void *mesg, int lines) |
| 717 | { |
| 718 | unsigned long m; |
| 719 | |
| 720 | m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6); |
| 721 | if (lines == 2) { |
| 722 | gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA); |
| 723 | if (gru_wait(cb) != CBS_IDLE) |
| 724 | return MQE_UNEXPECTED_CB_ERR; |
| 725 | } |
| 726 | gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA); |
| 727 | if (gru_wait(cb) != CBS_IDLE) |
| 728 | return MQE_UNEXPECTED_CB_ERR; |
| 729 | send_message_queue_interrupt(mqd); |
| 730 | return MQE_OK; |
| 731 | } |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * Handle a gru_mesq failure. Some of these failures are software recoverable |
| 735 | * or retryable. |
| 736 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 737 | static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd, |
| 738 | void *mesg, int lines) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 739 | { |
| 740 | int substatus, ret = 0; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 741 | |
| 742 | substatus = gru_get_cb_message_queue_substatus(cb); |
| 743 | switch (substatus) { |
| 744 | case CBSS_NO_ERROR: |
| 745 | STAT(mesq_send_unexpected_error); |
| 746 | ret = MQE_UNEXPECTED_CB_ERR; |
| 747 | break; |
| 748 | case CBSS_LB_OVERFLOWED: |
| 749 | STAT(mesq_send_lb_overflow); |
| 750 | ret = MQE_CONGESTION; |
| 751 | break; |
| 752 | case CBSS_QLIMIT_REACHED: |
| 753 | STAT(mesq_send_qlimit_reached); |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 754 | ret = send_message_queue_full(cb, mqd, mesg, lines); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 755 | break; |
| 756 | case CBSS_AMO_NACKED: |
| 757 | STAT(mesq_send_amo_nacked); |
| 758 | ret = MQE_CONGESTION; |
| 759 | break; |
| 760 | case CBSS_PUT_NACKED: |
| 761 | STAT(mesq_send_put_nacked); |
Jack Steiner | 17b49a6 | 2009-06-17 16:28:23 -0700 | [diff] [blame] | 762 | ret = send_message_put_nacked(cb, mqd, mesg, lines); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 763 | break; |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 764 | case CBSS_PAGE_OVERFLOW: |
| 765 | STAT(mesq_page_overflow); |
| 766 | /* fallthru */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 767 | default: |
| 768 | BUG(); |
| 769 | } |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Send a message to a message queue |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 775 | * mqd message queue descriptor |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 776 | * mesg message. ust be vaddr within a GSEG |
| 777 | * bytes message size (<= 2 CL) |
| 778 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 779 | int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg, |
| 780 | unsigned int bytes) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 781 | { |
| 782 | struct message_header *mhdr; |
| 783 | void *cb; |
| 784 | void *dsr; |
| 785 | int istatus, clines, ret; |
| 786 | |
| 787 | STAT(mesq_send); |
| 788 | BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES); |
| 789 | |
Julia Lawall | cbf330b | 2008-10-15 22:01:27 -0700 | [diff] [blame] | 790 | clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 791 | if (gru_get_cpu_resources(bytes, &cb, &dsr)) |
| 792 | return MQE_BUG_NO_RESOURCES; |
| 793 | memcpy(dsr, mesg, bytes); |
| 794 | mhdr = dsr; |
| 795 | mhdr->present = MQS_FULL; |
| 796 | mhdr->lines = clines; |
| 797 | if (clines == 2) { |
| 798 | mhdr->present2 = get_present2(mhdr); |
| 799 | restore_present2(mhdr, MQS_FULL); |
| 800 | } |
| 801 | |
| 802 | do { |
| 803 | ret = MQE_OK; |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 804 | gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 805 | istatus = gru_wait(cb); |
| 806 | if (istatus != CBS_IDLE) |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 807 | ret = send_message_failure(cb, mqd, dsr, clines); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 808 | } while (ret == MQIE_AGAIN); |
| 809 | gru_free_cpu_resources(cb, dsr); |
| 810 | |
| 811 | if (ret) |
| 812 | STAT(mesq_send_failed); |
| 813 | return ret; |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(gru_send_message_gpa); |
| 816 | |
| 817 | /* |
| 818 | * Advance the receive pointer for the queue to the next message. |
| 819 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 820 | void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 821 | { |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 822 | struct message_queue *mq = mqd->mq; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 823 | struct message_header *mhdr = mq->next; |
| 824 | void *next, *pnext; |
| 825 | int half = -1; |
| 826 | int lines = mhdr->lines; |
| 827 | |
| 828 | if (lines == 2) |
| 829 | restore_present2(mhdr, MQS_EMPTY); |
| 830 | mhdr->present = MQS_EMPTY; |
| 831 | |
| 832 | pnext = mq->next; |
| 833 | next = pnext + GRU_CACHE_LINE_BYTES * lines; |
| 834 | if (next == mq->limit) { |
| 835 | next = mq->start; |
| 836 | half = 1; |
| 837 | } else if (pnext < mq->start2 && next >= mq->start2) { |
| 838 | half = 0; |
| 839 | } |
| 840 | |
| 841 | if (half >= 0) |
| 842 | mq->hstatus[half] = 1; |
| 843 | mq->next = next; |
| 844 | } |
| 845 | EXPORT_SYMBOL_GPL(gru_free_message); |
| 846 | |
| 847 | /* |
| 848 | * Get next message from message queue. Return NULL if no message |
| 849 | * present. User must call next_message() to move to next message. |
| 850 | * rmq message queue |
| 851 | */ |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 852 | void *gru_get_next_message(struct gru_message_queue_desc *mqd) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 853 | { |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 854 | struct message_queue *mq = mqd->mq; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 855 | struct message_header *mhdr = mq->next; |
| 856 | int present = mhdr->present; |
| 857 | |
| 858 | /* skip NOOP messages */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 859 | while (present == MQS_NOOP) { |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 860 | gru_free_message(mqd, mhdr); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 861 | mhdr = mq->next; |
| 862 | present = mhdr->present; |
| 863 | } |
| 864 | |
| 865 | /* Wait for both halves of 2 line messages */ |
| 866 | if (present == MQS_FULL && mhdr->lines == 2 && |
| 867 | get_present2(mhdr) == MQS_EMPTY) |
| 868 | present = MQS_EMPTY; |
| 869 | |
| 870 | if (!present) { |
| 871 | STAT(mesq_receive_none); |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | if (mhdr->lines == 2) |
| 876 | restore_present2(mhdr, mhdr->present2); |
| 877 | |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 878 | STAT(mesq_receive); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 879 | return mhdr; |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(gru_get_next_message); |
| 882 | |
| 883 | /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/ |
| 884 | |
| 885 | /* |
Robin Holt | 289750d | 2009-12-15 16:47:55 -0800 | [diff] [blame] | 886 | * Load a DW from a global GPA. The GPA can be a memory or MMR address. |
| 887 | */ |
| 888 | int gru_read_gpa(unsigned long *value, unsigned long gpa) |
| 889 | { |
| 890 | void *cb; |
| 891 | void *dsr; |
| 892 | int ret, iaa; |
| 893 | |
| 894 | STAT(read_gpa); |
| 895 | if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr)) |
| 896 | return MQE_BUG_NO_RESOURCES; |
| 897 | iaa = gpa >> 62; |
| 898 | gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA); |
| 899 | ret = gru_wait(cb); |
| 900 | if (ret == CBS_IDLE) |
| 901 | *value = *(unsigned long *)dsr; |
| 902 | gru_free_cpu_resources(cb, dsr); |
| 903 | return ret; |
| 904 | } |
| 905 | EXPORT_SYMBOL_GPL(gru_read_gpa); |
| 906 | |
| 907 | |
| 908 | /* |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 909 | * Copy a block of data using the GRU resources |
| 910 | */ |
| 911 | int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa, |
| 912 | unsigned int bytes) |
| 913 | { |
| 914 | void *cb; |
| 915 | void *dsr; |
| 916 | int ret; |
| 917 | |
| 918 | STAT(copy_gpa); |
| 919 | if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr)) |
| 920 | return MQE_BUG_NO_RESOURCES; |
| 921 | gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr), |
Jack Steiner | 6f2584f | 2009-04-02 16:59:10 -0700 | [diff] [blame] | 922 | XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA); |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 923 | ret = gru_wait(cb); |
| 924 | gru_free_cpu_resources(cb, dsr); |
| 925 | return ret; |
| 926 | } |
| 927 | EXPORT_SYMBOL_GPL(gru_copy_gpa); |
| 928 | |
| 929 | /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/ |
| 930 | /* Temp - will delete after we gain confidence in the GRU */ |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 931 | |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 932 | static int quicktest0(unsigned long arg) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 933 | { |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 934 | unsigned long word0; |
| 935 | unsigned long word1; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 936 | void *cb; |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 937 | void *dsr; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 938 | unsigned long *p; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 939 | int ret = -EIO; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 940 | |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 941 | if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr)) |
| 942 | return MQE_BUG_NO_RESOURCES; |
| 943 | p = dsr; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 944 | word0 = MAGIC; |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 945 | word1 = 0; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 946 | |
Jack Steiner | 836ce67 | 2009-06-17 16:28:22 -0700 | [diff] [blame] | 947 | gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 948 | if (gru_wait(cb) != CBS_IDLE) { |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 949 | printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id()); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 950 | goto done; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 953 | if (*p != MAGIC) { |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 954 | printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 955 | goto done; |
| 956 | } |
| 957 | gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA); |
| 958 | if (gru_wait(cb) != CBS_IDLE) { |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 959 | printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id()); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 960 | goto done; |
| 961 | } |
| 962 | |
| 963 | if (word0 != word1 || word1 != MAGIC) { |
| 964 | printk(KERN_DEBUG |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 965 | "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n", |
| 966 | smp_processor_id(), word1, MAGIC); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 967 | goto done; |
| 968 | } |
| 969 | ret = 0; |
| 970 | |
| 971 | done: |
| 972 | gru_free_cpu_resources(cb, dsr); |
| 973 | return ret; |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 976 | #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1))) |
| 977 | |
| 978 | static int quicktest1(unsigned long arg) |
| 979 | { |
| 980 | struct gru_message_queue_desc mqd; |
| 981 | void *p, *mq; |
| 982 | unsigned long *dw; |
| 983 | int i, ret = -EIO; |
| 984 | char mes[GRU_CACHE_LINE_BYTES], *m; |
| 985 | |
| 986 | /* Need 1K cacheline aligned that does not cross page boundary */ |
| 987 | p = kmalloc(4096, 0); |
Roel Kluin | 9e5f113 | 2009-09-23 15:57:33 -0700 | [diff] [blame] | 988 | if (p == NULL) |
| 989 | return -ENOMEM; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 990 | mq = ALIGNUP(p, 1024); |
| 991 | memset(mes, 0xee, sizeof(mes)); |
| 992 | dw = mq; |
| 993 | |
| 994 | gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0); |
| 995 | for (i = 0; i < 6; i++) { |
| 996 | mes[8] = i; |
| 997 | do { |
| 998 | ret = gru_send_message_gpa(&mqd, mes, sizeof(mes)); |
| 999 | } while (ret == MQE_CONGESTION); |
| 1000 | if (ret) |
| 1001 | break; |
| 1002 | } |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 1003 | if (ret != MQE_QUEUE_FULL || i != 4) { |
| 1004 | printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n", |
| 1005 | smp_processor_id(), ret, i); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1006 | goto done; |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 1007 | } |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1008 | |
| 1009 | for (i = 0; i < 6; i++) { |
| 1010 | m = gru_get_next_message(&mqd); |
| 1011 | if (!m || m[8] != i) |
| 1012 | break; |
| 1013 | gru_free_message(&mqd, m); |
| 1014 | } |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 1015 | if (i != 4) { |
| 1016 | printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n", |
| 1017 | smp_processor_id(), i, m, m ? m[8] : -1); |
| 1018 | goto done; |
| 1019 | } |
| 1020 | ret = 0; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1021 | |
| 1022 | done: |
| 1023 | kfree(p); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static int quicktest2(unsigned long arg) |
| 1028 | { |
| 1029 | static DECLARE_COMPLETION(cmp); |
| 1030 | unsigned long han; |
| 1031 | int blade_id = 0; |
| 1032 | int numcb = 4; |
| 1033 | int ret = 0; |
| 1034 | unsigned long *buf; |
| 1035 | void *cb0, *cb; |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1036 | struct gru_control_block_status *gen; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1037 | int i, k, istatus, bytes; |
| 1038 | |
| 1039 | bytes = numcb * 4 * 8; |
| 1040 | buf = kmalloc(bytes, GFP_KERNEL); |
| 1041 | if (!buf) |
| 1042 | return -ENOMEM; |
| 1043 | |
| 1044 | ret = -EBUSY; |
| 1045 | han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp); |
| 1046 | if (!han) |
| 1047 | goto done; |
| 1048 | |
| 1049 | gru_lock_async_resource(han, &cb0, NULL); |
| 1050 | memset(buf, 0xee, bytes); |
| 1051 | for (i = 0; i < numcb; i++) |
| 1052 | gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0, |
| 1053 | XTYPE_DW, 4, 1, IMA_INTERRUPT); |
| 1054 | |
| 1055 | ret = 0; |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1056 | k = numcb; |
| 1057 | do { |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1058 | gru_wait_async_cbr(han); |
| 1059 | for (i = 0; i < numcb; i++) { |
| 1060 | cb = cb0 + i * GRU_HANDLE_STRIDE; |
| 1061 | istatus = gru_check_status(cb); |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1062 | if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS) |
| 1063 | break; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1064 | } |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1065 | if (i == numcb) |
| 1066 | continue; |
| 1067 | if (istatus != CBS_IDLE) { |
| 1068 | printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i); |
| 1069 | ret = -EFAULT; |
| 1070 | } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] || |
| 1071 | buf[4 * i + 3]) { |
| 1072 | printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n", |
| 1073 | smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]); |
| 1074 | ret = -EIO; |
| 1075 | } |
| 1076 | k--; |
| 1077 | gen = cb; |
| 1078 | gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */ |
| 1079 | } while (k); |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1080 | BUG_ON(cmp.done); |
| 1081 | |
| 1082 | gru_unlock_async_resource(han); |
| 1083 | gru_release_async_resources(han); |
| 1084 | done: |
| 1085 | kfree(buf); |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1089 | #define BUFSIZE 200 |
| 1090 | static int quicktest3(unsigned long arg) |
| 1091 | { |
| 1092 | char buf1[BUFSIZE], buf2[BUFSIZE]; |
| 1093 | int ret = 0; |
| 1094 | |
| 1095 | memset(buf2, 0, sizeof(buf2)); |
| 1096 | memset(buf1, get_cycles() & 255, sizeof(buf1)); |
| 1097 | gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE); |
| 1098 | if (memcmp(buf1, buf2, BUFSIZE)) { |
Jack Steiner | 563447d | 2009-12-15 16:48:12 -0800 | [diff] [blame] | 1099 | printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id()); |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1100 | ret = -EIO; |
| 1101 | } |
| 1102 | return ret; |
| 1103 | } |
| 1104 | |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1105 | /* |
| 1106 | * Debugging only. User hook for various kernel tests |
| 1107 | * of driver & gru. |
| 1108 | */ |
| 1109 | int gru_ktest(unsigned long arg) |
| 1110 | { |
| 1111 | int ret = -EINVAL; |
| 1112 | |
| 1113 | switch (arg & 0xff) { |
| 1114 | case 0: |
| 1115 | ret = quicktest0(arg); |
| 1116 | break; |
| 1117 | case 1: |
| 1118 | ret = quicktest1(arg); |
| 1119 | break; |
| 1120 | case 2: |
| 1121 | ret = quicktest2(arg); |
| 1122 | break; |
Jack Steiner | 33f3648 | 2009-12-15 16:48:09 -0800 | [diff] [blame] | 1123 | case 3: |
| 1124 | ret = quicktest3(arg); |
| 1125 | break; |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 1126 | case 99: |
| 1127 | ret = gru_free_kernel_contexts(); |
| 1128 | break; |
Jack Steiner | eb5bd5e | 2009-06-17 16:28:26 -0700 | [diff] [blame] | 1129 | } |
| 1130 | return ret; |
| 1131 | |
| 1132 | } |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 1133 | |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 1134 | int gru_kservices_init(void) |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 1135 | { |
Jack Steiner | 28bffaf | 2008-07-29 22:33:57 -0700 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
Jack Steiner | 27ca8a7 | 2009-04-02 16:59:11 -0700 | [diff] [blame] | 1138 | |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 1139 | void gru_kservices_exit(void) |
Jack Steiner | 27ca8a7 | 2009-04-02 16:59:11 -0700 | [diff] [blame] | 1140 | { |
Jack Steiner | d5826dd | 2009-06-17 16:28:28 -0700 | [diff] [blame] | 1141 | if (gru_free_kernel_contexts()) |
| 1142 | BUG(); |
Jack Steiner | 27ca8a7 | 2009-04-02 16:59:11 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |