blob: 913de07e577c752fc338ce8b30b39a58290763d0 [file] [log] [blame]
Jack Steiner28bffaf2008-07-29 22:33:57 -07001/*
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 Steiner28bffaf2008-07-29 22:33:57 -070027#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 Steiner836ce672009-06-17 16:28:22 -070033#include <linux/delay.h>
Paul Gortmaker7a321292011-07-10 12:41:41 -040034#include <linux/export.h>
Jack Steiner76148df2009-12-15 16:48:21 -080035#include <asm/io_apic.h>
Jack Steiner28bffaf2008-07-29 22:33:57 -070036#include "gru.h"
37#include "grulib.h"
38#include "grutables.h"
39#include "grukservices.h"
40#include "gru_instructions.h"
41#include <asm/uv/uv_hub.h>
42
43/*
44 * Kernel GRU Usage
45 *
46 * The following is an interim algorithm for management of kernel GRU
47 * resources. This will likely be replaced when we better understand the
48 * kernel/user requirements.
49 *
Jack Steiner836ce672009-06-17 16:28:22 -070050 * Blade percpu resources reserved for kernel use. These resources are
51 * reserved whenever the the kernel context for the blade is loaded. Note
52 * that the kernel context is not guaranteed to be always available. It is
53 * loaded on demand & can be stolen by a user if the user demand exceeds the
54 * kernel demand. The kernel can always reload the kernel context but
55 * a SLEEP may be required!!!.
Jack Steiner9120dec2009-06-17 16:28:25 -070056 *
57 * Async Overview:
58 *
59 * Each blade has one "kernel context" that owns GRU kernel resources
60 * located on the blade. Kernel drivers use GRU resources in this context
61 * for sending messages, zeroing memory, etc.
62 *
63 * The kernel context is dynamically loaded on demand. If it is not in
64 * use by the kernel, the kernel context can be unloaded & given to a user.
65 * The kernel context will be reloaded when needed. This may require that
66 * a context be stolen from a user.
67 * NOTE: frequent unloading/reloading of the kernel context is
68 * expensive. We are depending on batch schedulers, cpusets, sane
69 * drivers or some other mechanism to prevent the need for frequent
70 * stealing/reloading.
71 *
72 * The kernel context consists of two parts:
73 * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
74 * Each cpu has it's own private resources & does not share them
75 * with other cpus. These resources are used serially, ie,
76 * locked, used & unlocked on each call to a function in
77 * grukservices.
78 * (Now that we have dynamic loading of kernel contexts, I
79 * may rethink this & allow sharing between cpus....)
80 *
81 * - Additional resources can be reserved long term & used directly
82 * by UV drivers located in the kernel. Drivers using these GRU
83 * resources can use asynchronous GRU instructions that send
84 * interrupts on completion.
85 * - these resources must be explicitly locked/unlocked
86 * - locked resources prevent (obviously) the kernel
87 * context from being unloaded.
88 * - drivers using these resource directly issue their own
89 * GRU instruction and must wait/check completion.
90 *
91 * When these resources are reserved, the caller can optionally
92 * associate a wait_queue with the resources and use asynchronous
93 * GRU instructions. When an async GRU instruction completes, the
94 * driver will do a wakeup on the event.
95 *
Jack Steiner28bffaf2008-07-29 22:33:57 -070096 */
Jack Steiner9120dec2009-06-17 16:28:25 -070097
98
99#define ASYNC_HAN_TO_BID(h) ((h) - 1)
100#define ASYNC_BID_TO_HAN(b) ((b) + 1)
101#define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
102
Jack Steiner6f2584f2009-04-02 16:59:10 -0700103#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -0700104#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -0700105#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
106 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700107
108/* GRU instruction attributes for all instructions */
109#define IMA IMA_CB_DELAY
110
111/* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
112#define __gru_cacheline_aligned__ \
113 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
114
115#define MAGIC 0x1234567887654321UL
116
117/* Default retry count for GRU errors on kernel instructions */
118#define EXCEPTION_RETRY_LIMIT 3
119
120/* Status of message queue sections */
121#define MQS_EMPTY 0
122#define MQS_FULL 1
123#define MQS_NOOP 2
124
125/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
126/* optimized for x86_64 */
127struct message_queue {
128 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
129 int qlines; /* DW 1 */
130 long hstatus[2];
131 void *next __gru_cacheline_aligned__;/* CL 1 */
132 void *limit;
133 void *start;
134 void *start2;
135 char data ____cacheline_aligned; /* CL 2 */
136};
137
138/* First word in every message - used by mesq interface */
139struct message_header {
140 char present;
141 char present2;
142 char lines;
143 char fill;
144};
145
Jack Steiner28bffaf2008-07-29 22:33:57 -0700146#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
147
Jack Steiner836ce672009-06-17 16:28:22 -0700148/*
Jack Steiner836ce672009-06-17 16:28:22 -0700149 * Reload the blade's kernel context into a GRU chiplet. Called holding
150 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
151 */
152static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
153{
154 struct gru_state *gru;
155 struct gru_thread_state *kgts;
156 void *vaddr;
Jack Steiner9120dec2009-06-17 16:28:25 -0700157 int ctxnum, ncpus;
Jack Steiner836ce672009-06-17 16:28:22 -0700158
159 up_read(&bs->bs_kgts_sema);
160 down_write(&bs->bs_kgts_sema);
161
Jack Steiner55484c42009-12-15 16:48:05 -0800162 if (!bs->bs_kgts) {
Jack Steinerc5502222009-12-15 16:48:13 -0800163 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0, 0);
Jack Steiner55484c42009-12-15 16:48:05 -0800164 bs->bs_kgts->ts_user_blade_id = blade_id;
165 }
Jack Steiner836ce672009-06-17 16:28:22 -0700166 kgts = bs->bs_kgts;
167
168 if (!kgts->ts_gru) {
169 STAT(load_kernel_context);
Jack Steiner9120dec2009-06-17 16:28:25 -0700170 ncpus = uv_blade_nr_possible_cpus(blade_id);
171 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
172 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
173 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
174 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
175 bs->bs_async_dsr_bytes);
Jack Steiner55484c42009-12-15 16:48:05 -0800176 while (!gru_assign_gru_context(kgts)) {
Jack Steiner836ce672009-06-17 16:28:22 -0700177 msleep(1);
Jack Steiner55484c42009-12-15 16:48:05 -0800178 gru_steal_context(kgts);
Jack Steiner836ce672009-06-17 16:28:22 -0700179 }
180 gru_load_context(kgts);
181 gru = bs->bs_kgts->ts_gru;
182 vaddr = gru->gs_gru_base_vaddr;
183 ctxnum = kgts->ts_ctxnum;
184 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
185 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
186 }
187 downgrade_write(&bs->bs_kgts_sema);
188}
189
190/*
Jack Steinerd5826dd2009-06-17 16:28:28 -0700191 * Free all kernel contexts that are not currently in use.
192 * Returns 0 if all freed, else number of inuse context.
193 */
194static int gru_free_kernel_contexts(void)
195{
196 struct gru_blade_state *bs;
197 struct gru_thread_state *kgts;
198 int bid, ret = 0;
199
200 for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
201 bs = gru_base[bid];
202 if (!bs)
203 continue;
Jack Steiner091f1a12009-12-15 16:48:02 -0800204
205 /* Ignore busy contexts. Don't want to block here. */
Jack Steinerd5826dd2009-06-17 16:28:28 -0700206 if (down_write_trylock(&bs->bs_kgts_sema)) {
207 kgts = bs->bs_kgts;
208 if (kgts && kgts->ts_gru)
209 gru_unload_context(kgts, 0);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700210 bs->bs_kgts = NULL;
211 up_write(&bs->bs_kgts_sema);
Jack Steiner091f1a12009-12-15 16:48:02 -0800212 kfree(kgts);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700213 } else {
214 ret++;
215 }
216 }
217 return ret;
218}
219
220/*
Jack Steiner836ce672009-06-17 16:28:22 -0700221 * Lock & load the kernel context for the specified blade.
222 */
223static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
224{
225 struct gru_blade_state *bs;
Jack Steiner0cd2b082009-12-15 16:48:17 -0800226 int bid;
Jack Steiner836ce672009-06-17 16:28:22 -0700227
228 STAT(lock_kernel_context);
Jack Steiner0cd2b082009-12-15 16:48:17 -0800229again:
230 bid = blade_id < 0 ? uv_numa_blade_id() : blade_id;
231 bs = gru_base[bid];
Jack Steiner836ce672009-06-17 16:28:22 -0700232
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300233 /* Handle the case where migration occurred while waiting for the sema */
Jack Steiner836ce672009-06-17 16:28:22 -0700234 down_read(&bs->bs_kgts_sema);
Jack Steiner0cd2b082009-12-15 16:48:17 -0800235 if (blade_id < 0 && bid != uv_numa_blade_id()) {
236 up_read(&bs->bs_kgts_sema);
237 goto again;
238 }
Jack Steiner836ce672009-06-17 16:28:22 -0700239 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
Jack Steiner0cd2b082009-12-15 16:48:17 -0800240 gru_load_kernel_context(bs, bid);
Jack Steiner836ce672009-06-17 16:28:22 -0700241 return bs;
242
243}
244
245/*
246 * Unlock the kernel context for the specified blade. Context is not
247 * unloaded but may be stolen before next use.
248 */
249static void gru_unlock_kernel_context(int blade_id)
250{
251 struct gru_blade_state *bs;
252
253 bs = gru_base[blade_id];
254 up_read(&bs->bs_kgts_sema);
255 STAT(unlock_kernel_context);
256}
257
258/*
259 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
260 * - returns with preemption disabled
261 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700262static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
263{
264 struct gru_blade_state *bs;
265 int lcpu;
266
267 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
268 preempt_disable();
Jack Steiner0cd2b082009-12-15 16:48:17 -0800269 bs = gru_lock_kernel_context(-1);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700270 lcpu = uv_blade_processor_id();
271 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
272 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
273 return 0;
274}
275
Jack Steiner836ce672009-06-17 16:28:22 -0700276/*
277 * Free the current cpus reserved DSR/CBR resources.
278 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700279static void gru_free_cpu_resources(void *cb, void *dsr)
280{
Jack Steiner836ce672009-06-17 16:28:22 -0700281 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700282 preempt_enable();
283}
284
Jack Steiner9120dec2009-06-17 16:28:25 -0700285/*
286 * Reserve GRU resources to be used asynchronously.
287 * Note: currently supports only 1 reservation per blade.
288 *
289 * input:
290 * blade_id - blade on which resources should be reserved
291 * cbrs - number of CBRs
292 * dsr_bytes - number of DSR bytes needed
293 * output:
294 * handle to identify resource
295 * (0 = async resources already reserved)
296 */
297unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
298 struct completion *cmp)
299{
300 struct gru_blade_state *bs;
301 struct gru_thread_state *kgts;
302 int ret = 0;
303
304 bs = gru_base[blade_id];
305
306 down_write(&bs->bs_kgts_sema);
307
308 /* Verify no resources already reserved */
309 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
310 goto done;
311 bs->bs_async_dsr_bytes = dsr_bytes;
312 bs->bs_async_cbrs = cbrs;
313 bs->bs_async_wq = cmp;
314 kgts = bs->bs_kgts;
315
316 /* Resources changed. Unload context if already loaded */
317 if (kgts && kgts->ts_gru)
318 gru_unload_context(kgts, 0);
319 ret = ASYNC_BID_TO_HAN(blade_id);
320
321done:
322 up_write(&bs->bs_kgts_sema);
323 return ret;
324}
325
326/*
327 * Release async resources previously reserved.
328 *
329 * input:
330 * han - handle to identify resources
331 */
332void gru_release_async_resources(unsigned long han)
333{
334 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
335
336 down_write(&bs->bs_kgts_sema);
337 bs->bs_async_dsr_bytes = 0;
338 bs->bs_async_cbrs = 0;
339 bs->bs_async_wq = NULL;
340 up_write(&bs->bs_kgts_sema);
341}
342
343/*
344 * Wait for async GRU instructions to complete.
345 *
346 * input:
347 * han - handle to identify resources
348 */
349void gru_wait_async_cbr(unsigned long han)
350{
351 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
352
353 wait_for_completion(bs->bs_async_wq);
354 mb();
355}
356
357/*
358 * Lock previous reserved async GRU resources
359 *
360 * input:
361 * han - handle to identify resources
362 * output:
363 * cb - pointer to first CBR
364 * dsr - pointer to first DSR
365 */
366void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
367{
368 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
369 int blade_id = ASYNC_HAN_TO_BID(han);
370 int ncpus;
371
372 gru_lock_kernel_context(blade_id);
373 ncpus = uv_blade_nr_possible_cpus(blade_id);
374 if (cb)
375 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
376 if (dsr)
377 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
378}
379
380/*
381 * Unlock previous reserved async GRU resources
382 *
383 * input:
384 * han - handle to identify resources
385 */
386void gru_unlock_async_resource(unsigned long han)
387{
388 int blade_id = ASYNC_HAN_TO_BID(han);
389
390 gru_unlock_kernel_context(blade_id);
391}
392
Jack Steiner836ce672009-06-17 16:28:22 -0700393/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700394int gru_get_cb_exception_detail(void *cb,
395 struct control_block_extended_exc_detail *excdet)
396{
397 struct gru_control_block_extended *cbe;
Jack Steiner1848a712009-12-15 16:48:14 -0800398 struct gru_thread_state *kgts = NULL;
399 unsigned long off;
400 int cbrnum, bid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700401
Jack Steiner1848a712009-12-15 16:48:14 -0800402 /*
403 * Locate kgts for cb. This algorithm is SLOW but
404 * this function is rarely called (ie., almost never).
405 * Performance does not matter.
406 */
407 for_each_possible_blade(bid) {
408 if (!gru_base[bid])
409 break;
410 kgts = gru_base[bid]->bs_kgts;
411 if (!kgts || !kgts->ts_gru)
412 continue;
413 off = cb - kgts->ts_gru->gs_gru_base_vaddr;
414 if (off < GRU_SIZE)
415 break;
416 kgts = NULL;
417 }
418 BUG_ON(!kgts);
419 cbrnum = thread_cbr_number(kgts, get_cb_number(cb));
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700420 cbe = get_cbe(GRUBASE(cb), cbrnum);
421 gru_flush_cache(cbe); /* CBE not coherent */
Jack Steiner67bf04a2009-12-15 16:48:11 -0800422 sync_core();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700423 excdet->opc = cbe->opccpy;
424 excdet->exopc = cbe->exopccpy;
425 excdet->ecause = cbe->ecause;
426 excdet->exceptdet0 = cbe->idef1upd;
427 excdet->exceptdet1 = cbe->idef3upd;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700428 gru_flush_cache(cbe);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700429 return 0;
430}
431
432char *gru_get_cb_exception_detail_str(int ret, void *cb,
433 char *buf, int size)
434{
435 struct gru_control_block_status *gen = (void *)cb;
436 struct control_block_extended_exc_detail excdet;
437
438 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
439 gru_get_cb_exception_detail(cb, &excdet);
440 snprintf(buf, size,
Jack Steiner563447d2009-12-15 16:48:12 -0800441 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
442 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700443 gen, excdet.opc, excdet.exopc, excdet.ecause,
444 excdet.exceptdet0, excdet.exceptdet1);
445 } else {
446 snprintf(buf, size, "No exception");
447 }
448 return buf;
449}
450
451static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
452{
453 while (gen->istatus >= CBS_ACTIVE) {
454 cpu_relax();
455 barrier();
456 }
457 return gen->istatus;
458}
459
460static int gru_retry_exception(void *cb)
461{
462 struct gru_control_block_status *gen = (void *)cb;
463 struct control_block_extended_exc_detail excdet;
464 int retry = EXCEPTION_RETRY_LIMIT;
465
466 while (1) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700467 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
468 return CBS_IDLE;
Jack Steinerd6e2fbc2009-06-17 16:28:29 -0700469 if (gru_get_cb_message_queue_substatus(cb))
470 return CBS_EXCEPTION;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700471 gru_get_cb_exception_detail(cb, &excdet);
Jack Steiner270952a2009-06-17 16:28:27 -0700472 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
473 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
Jack Steiner28bffaf2008-07-29 22:33:57 -0700474 break;
475 if (retry-- == 0)
476 break;
477 gen->icmd = 1;
478 gru_flush_cache(gen);
479 }
480 return CBS_EXCEPTION;
481}
482
483int gru_check_status_proc(void *cb)
484{
485 struct gru_control_block_status *gen = (void *)cb;
486 int ret;
487
488 ret = gen->istatus;
Jack Steiner67bf04a2009-12-15 16:48:11 -0800489 if (ret == CBS_EXCEPTION)
490 ret = gru_retry_exception(cb);
491 rmb();
492 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700493
494}
495
496int gru_wait_proc(void *cb)
497{
498 struct gru_control_block_status *gen = (void *)cb;
499 int ret;
500
501 ret = gru_wait_idle_or_exception(gen);
502 if (ret == CBS_EXCEPTION)
503 ret = gru_retry_exception(cb);
Jack Steiner67bf04a2009-12-15 16:48:11 -0800504 rmb();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700505 return ret;
506}
507
508void gru_abort(int ret, void *cb, char *str)
509{
510 char buf[GRU_EXC_STR_SIZE];
511
512 panic("GRU FATAL ERROR: %s - %s\n", str,
513 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
514}
515
516void gru_wait_abort_proc(void *cb)
517{
518 int ret;
519
520 ret = gru_wait_proc(cb);
521 if (ret)
522 gru_abort(ret, cb, "gru_wait_abort");
523}
524
525
526/*------------------------------ MESSAGE QUEUES -----------------------------*/
527
528/* Internal status . These are NOT returned to the user. */
529#define MQIE_AGAIN -1 /* try again */
530
531
532/*
533 * Save/restore the "present" flag that is in the second line of 2-line
534 * messages
535 */
536static inline int get_present2(void *p)
537{
538 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
539 return mhdr->present;
540}
541
542static inline void restore_present2(void *p, int val)
543{
544 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
545 mhdr->present = val;
546}
547
548/*
549 * Create a message queue.
550 * qlines - message queue size in cache lines. Includes 2-line header.
551 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700552int gru_create_message_queue(struct gru_message_queue_desc *mqd,
553 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700554{
555 struct message_queue *mq = p;
556 unsigned int qlines;
557
558 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
559 memset(mq, 0, bytes);
560 mq->start = &mq->data;
561 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
562 mq->next = &mq->data;
563 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
564 mq->qlines = qlines;
565 mq->hstatus[0] = 0;
566 mq->hstatus[1] = 1;
567 mq->head = gru_mesq_head(2, qlines / 2 + 1);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700568 mqd->mq = mq;
569 mqd->mq_gpa = uv_gpa(mq);
570 mqd->qlines = qlines;
Jack Steiner76148df2009-12-15 16:48:21 -0800571 mqd->interrupt_pnode = nasid >> 1;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700572 mqd->interrupt_vector = vector;
573 mqd->interrupt_apicid = apicid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700574 return 0;
575}
576EXPORT_SYMBOL_GPL(gru_create_message_queue);
577
578/*
579 * Send a NOOP message to a message queue
580 * Returns:
581 * 0 - if queue is full after the send. This is the normal case
582 * but various races can change this.
583 * -1 - if mesq sent successfully but queue not full
584 * >0 - unexpected error. MQE_xxx returned
585 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700586static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
587 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700588{
589 const struct message_header noop_header = {
590 .present = MQS_NOOP, .lines = 1};
591 unsigned long m;
592 int substatus, ret;
593 struct message_header save_mhdr, *mhdr = mesg;
594
595 STAT(mesq_noop);
596 save_mhdr = *mhdr;
597 *mhdr = noop_header;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700598 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700599 ret = gru_wait(cb);
600
601 if (ret) {
602 substatus = gru_get_cb_message_queue_substatus(cb);
603 switch (substatus) {
604 case CBSS_NO_ERROR:
605 STAT(mesq_noop_unexpected_error);
606 ret = MQE_UNEXPECTED_CB_ERR;
607 break;
608 case CBSS_LB_OVERFLOWED:
609 STAT(mesq_noop_lb_overflow);
610 ret = MQE_CONGESTION;
611 break;
612 case CBSS_QLIMIT_REACHED:
613 STAT(mesq_noop_qlimit_reached);
614 ret = 0;
615 break;
616 case CBSS_AMO_NACKED:
617 STAT(mesq_noop_amo_nacked);
618 ret = MQE_CONGESTION;
619 break;
620 case CBSS_PUT_NACKED:
621 STAT(mesq_noop_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700622 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700623 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
624 IMA);
625 if (gru_wait(cb) == CBS_IDLE)
626 ret = MQIE_AGAIN;
627 else
628 ret = MQE_UNEXPECTED_CB_ERR;
629 break;
630 case CBSS_PAGE_OVERFLOW:
Jack Steiner563447d2009-12-15 16:48:12 -0800631 STAT(mesq_noop_page_overflow);
632 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700633 default:
634 BUG();
635 }
636 }
637 *mhdr = save_mhdr;
638 return ret;
639}
640
641/*
642 * Handle a gru_mesq full.
643 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700644static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
645 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700646{
647 union gru_mesqhead mqh;
648 unsigned int limit, head;
649 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700650 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700651
652 /* Determine if switching to first/second half of q */
653 avalue = gru_get_amo_value(cb);
654 head = gru_get_amo_value_head(cb);
655 limit = gru_get_amo_value_limit(cb);
656
Jack Steiner6f2584f2009-04-02 16:59:10 -0700657 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700658 half = (limit != qlines);
659
660 if (half)
661 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
662 else
663 mqh = gru_mesq_head(2, qlines / 2 + 1);
664
665 /* Try to get lock for switching head pointer */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700666 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700667 if (gru_wait(cb) != CBS_IDLE)
668 goto cberr;
669 if (!gru_get_amo_value(cb)) {
670 STAT(mesq_qf_locked);
671 return MQE_QUEUE_FULL;
672 }
673
674 /* Got the lock. Send optional NOP if queue not full, */
675 if (head != limit) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700676 if (send_noop_message(cb, mqd, mesg)) {
677 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700678 XTYPE_DW, IMA);
679 if (gru_wait(cb) != CBS_IDLE)
680 goto cberr;
681 STAT(mesq_qf_noop_not_full);
682 return MQIE_AGAIN;
683 }
684 avalue++;
685 }
686
687 /* Then flip queuehead to other half of queue. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700688 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
689 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700690 if (gru_wait(cb) != CBS_IDLE)
691 goto cberr;
692
693 /* If not successfully in swapping queue head, clear the hstatus lock */
694 if (gru_get_amo_value(cb) != avalue) {
695 STAT(mesq_qf_switch_head_failed);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700696 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
697 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700698 if (gru_wait(cb) != CBS_IDLE)
699 goto cberr;
700 }
701 return MQIE_AGAIN;
702cberr:
703 STAT(mesq_qf_unexpected_error);
704 return MQE_UNEXPECTED_CB_ERR;
705}
706
Jack Steiner6f2584f2009-04-02 16:59:10 -0700707/*
Jack Steiner17b49a62009-06-17 16:28:23 -0700708 * Handle a PUT failure. Note: if message was a 2-line message, one of the
709 * lines might have successfully have been written. Before sending the
710 * message, "present" must be cleared in BOTH lines to prevent the receiver
711 * from prematurely seeing the full message.
712 */
713static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
714 void *mesg, int lines)
715{
Jack Steiner76148df2009-12-15 16:48:21 -0800716 unsigned long m, *val = mesg, gpa, save;
717 int ret;
Jack Steiner17b49a62009-06-17 16:28:23 -0700718
719 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
720 if (lines == 2) {
721 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
722 if (gru_wait(cb) != CBS_IDLE)
723 return MQE_UNEXPECTED_CB_ERR;
724 }
725 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
726 if (gru_wait(cb) != CBS_IDLE)
727 return MQE_UNEXPECTED_CB_ERR;
Jack Steiner76148df2009-12-15 16:48:21 -0800728
729 if (!mqd->interrupt_vector)
730 return MQE_OK;
731
732 /*
733 * Send a cross-partition interrupt to the SSI that contains the target
734 * message queue. Normally, the interrupt is automatically delivered by
735 * hardware but some error conditions require explicit delivery.
736 * Use the GRU to deliver the interrupt. Otherwise partition failures
737 * could cause unrecovered errors.
738 */
739 gpa = uv_global_gru_mmr_address(mqd->interrupt_pnode, UVH_IPI_INT);
740 save = *val;
741 *val = uv_hub_ipi_value(mqd->interrupt_apicid, mqd->interrupt_vector,
742 dest_Fixed);
743 gru_vstore_phys(cb, gpa, gru_get_tri(mesg), IAA_REGISTER, IMA);
744 ret = gru_wait(cb);
745 *val = save;
746 if (ret != CBS_IDLE)
747 return MQE_UNEXPECTED_CB_ERR;
Jack Steiner17b49a62009-06-17 16:28:23 -0700748 return MQE_OK;
749}
Jack Steiner28bffaf2008-07-29 22:33:57 -0700750
751/*
752 * Handle a gru_mesq failure. Some of these failures are software recoverable
753 * or retryable.
754 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700755static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
756 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700757{
758 int substatus, ret = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700759
760 substatus = gru_get_cb_message_queue_substatus(cb);
761 switch (substatus) {
762 case CBSS_NO_ERROR:
763 STAT(mesq_send_unexpected_error);
764 ret = MQE_UNEXPECTED_CB_ERR;
765 break;
766 case CBSS_LB_OVERFLOWED:
767 STAT(mesq_send_lb_overflow);
768 ret = MQE_CONGESTION;
769 break;
770 case CBSS_QLIMIT_REACHED:
771 STAT(mesq_send_qlimit_reached);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700772 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700773 break;
774 case CBSS_AMO_NACKED:
775 STAT(mesq_send_amo_nacked);
776 ret = MQE_CONGESTION;
777 break;
778 case CBSS_PUT_NACKED:
779 STAT(mesq_send_put_nacked);
Jack Steiner17b49a62009-06-17 16:28:23 -0700780 ret = send_message_put_nacked(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700781 break;
Jack Steiner563447d2009-12-15 16:48:12 -0800782 case CBSS_PAGE_OVERFLOW:
783 STAT(mesq_page_overflow);
784 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700785 default:
786 BUG();
787 }
788 return ret;
789}
790
791/*
792 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700793 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700794 * mesg message. ust be vaddr within a GSEG
795 * bytes message size (<= 2 CL)
796 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700797int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
798 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700799{
800 struct message_header *mhdr;
801 void *cb;
802 void *dsr;
803 int istatus, clines, ret;
804
805 STAT(mesq_send);
806 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
807
Julia Lawallcbf330b2008-10-15 22:01:27 -0700808 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700809 if (gru_get_cpu_resources(bytes, &cb, &dsr))
810 return MQE_BUG_NO_RESOURCES;
811 memcpy(dsr, mesg, bytes);
812 mhdr = dsr;
813 mhdr->present = MQS_FULL;
814 mhdr->lines = clines;
815 if (clines == 2) {
816 mhdr->present2 = get_present2(mhdr);
817 restore_present2(mhdr, MQS_FULL);
818 }
819
820 do {
821 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700822 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700823 istatus = gru_wait(cb);
824 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700825 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700826 } while (ret == MQIE_AGAIN);
827 gru_free_cpu_resources(cb, dsr);
828
829 if (ret)
830 STAT(mesq_send_failed);
831 return ret;
832}
833EXPORT_SYMBOL_GPL(gru_send_message_gpa);
834
835/*
836 * Advance the receive pointer for the queue to the next message.
837 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700838void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700839{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700840 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700841 struct message_header *mhdr = mq->next;
842 void *next, *pnext;
843 int half = -1;
844 int lines = mhdr->lines;
845
846 if (lines == 2)
847 restore_present2(mhdr, MQS_EMPTY);
848 mhdr->present = MQS_EMPTY;
849
850 pnext = mq->next;
851 next = pnext + GRU_CACHE_LINE_BYTES * lines;
852 if (next == mq->limit) {
853 next = mq->start;
854 half = 1;
855 } else if (pnext < mq->start2 && next >= mq->start2) {
856 half = 0;
857 }
858
859 if (half >= 0)
860 mq->hstatus[half] = 1;
861 mq->next = next;
862}
863EXPORT_SYMBOL_GPL(gru_free_message);
864
865/*
866 * Get next message from message queue. Return NULL if no message
867 * present. User must call next_message() to move to next message.
868 * rmq message queue
869 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700870void *gru_get_next_message(struct gru_message_queue_desc *mqd)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700871{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700872 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700873 struct message_header *mhdr = mq->next;
874 int present = mhdr->present;
875
876 /* skip NOOP messages */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700877 while (present == MQS_NOOP) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700878 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700879 mhdr = mq->next;
880 present = mhdr->present;
881 }
882
883 /* Wait for both halves of 2 line messages */
884 if (present == MQS_FULL && mhdr->lines == 2 &&
885 get_present2(mhdr) == MQS_EMPTY)
886 present = MQS_EMPTY;
887
888 if (!present) {
889 STAT(mesq_receive_none);
890 return NULL;
891 }
892
893 if (mhdr->lines == 2)
894 restore_present2(mhdr, mhdr->present2);
895
Jack Steiner563447d2009-12-15 16:48:12 -0800896 STAT(mesq_receive);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700897 return mhdr;
898}
899EXPORT_SYMBOL_GPL(gru_get_next_message);
900
901/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
902
903/*
Robin Holt289750d2009-12-15 16:47:55 -0800904 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
905 */
906int gru_read_gpa(unsigned long *value, unsigned long gpa)
907{
908 void *cb;
909 void *dsr;
910 int ret, iaa;
911
912 STAT(read_gpa);
913 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
914 return MQE_BUG_NO_RESOURCES;
915 iaa = gpa >> 62;
916 gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
917 ret = gru_wait(cb);
918 if (ret == CBS_IDLE)
919 *value = *(unsigned long *)dsr;
920 gru_free_cpu_resources(cb, dsr);
921 return ret;
922}
923EXPORT_SYMBOL_GPL(gru_read_gpa);
924
925
926/*
Jack Steiner28bffaf2008-07-29 22:33:57 -0700927 * Copy a block of data using the GRU resources
928 */
929int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
930 unsigned int bytes)
931{
932 void *cb;
933 void *dsr;
934 int ret;
935
936 STAT(copy_gpa);
937 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
938 return MQE_BUG_NO_RESOURCES;
939 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700940 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700941 ret = gru_wait(cb);
942 gru_free_cpu_resources(cb, dsr);
943 return ret;
944}
945EXPORT_SYMBOL_GPL(gru_copy_gpa);
946
947/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
948/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700949
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700950static int quicktest0(unsigned long arg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700951{
Jack Steiner836ce672009-06-17 16:28:22 -0700952 unsigned long word0;
953 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700954 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700955 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700956 unsigned long *p;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700957 int ret = -EIO;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700958
Jack Steiner836ce672009-06-17 16:28:22 -0700959 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
960 return MQE_BUG_NO_RESOURCES;
961 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700962 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700963 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700964
Jack Steiner836ce672009-06-17 16:28:22 -0700965 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700966 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800967 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700968 goto done;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700969 }
970
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700971 if (*p != MAGIC) {
Jack Steiner563447d2009-12-15 16:48:12 -0800972 printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700973 goto done;
974 }
975 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
976 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800977 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700978 goto done;
979 }
980
981 if (word0 != word1 || word1 != MAGIC) {
982 printk(KERN_DEBUG
Jack Steiner563447d2009-12-15 16:48:12 -0800983 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
984 smp_processor_id(), word1, MAGIC);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700985 goto done;
986 }
987 ret = 0;
988
989done:
990 gru_free_cpu_resources(cb, dsr);
991 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700992}
993
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700994#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
995
996static int quicktest1(unsigned long arg)
997{
998 struct gru_message_queue_desc mqd;
999 void *p, *mq;
1000 unsigned long *dw;
1001 int i, ret = -EIO;
1002 char mes[GRU_CACHE_LINE_BYTES], *m;
1003
1004 /* Need 1K cacheline aligned that does not cross page boundary */
1005 p = kmalloc(4096, 0);
Roel Kluin9e5f1132009-09-23 15:57:33 -07001006 if (p == NULL)
1007 return -ENOMEM;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001008 mq = ALIGNUP(p, 1024);
1009 memset(mes, 0xee, sizeof(mes));
1010 dw = mq;
1011
1012 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
1013 for (i = 0; i < 6; i++) {
1014 mes[8] = i;
1015 do {
1016 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
1017 } while (ret == MQE_CONGESTION);
1018 if (ret)
1019 break;
1020 }
Jack Steiner563447d2009-12-15 16:48:12 -08001021 if (ret != MQE_QUEUE_FULL || i != 4) {
1022 printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
1023 smp_processor_id(), ret, i);
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001024 goto done;
Jack Steiner563447d2009-12-15 16:48:12 -08001025 }
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001026
1027 for (i = 0; i < 6; i++) {
1028 m = gru_get_next_message(&mqd);
1029 if (!m || m[8] != i)
1030 break;
1031 gru_free_message(&mqd, m);
1032 }
Jack Steiner563447d2009-12-15 16:48:12 -08001033 if (i != 4) {
1034 printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1035 smp_processor_id(), i, m, m ? m[8] : -1);
1036 goto done;
1037 }
1038 ret = 0;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001039
1040done:
1041 kfree(p);
1042 return ret;
1043}
1044
1045static int quicktest2(unsigned long arg)
1046{
1047 static DECLARE_COMPLETION(cmp);
1048 unsigned long han;
1049 int blade_id = 0;
1050 int numcb = 4;
1051 int ret = 0;
1052 unsigned long *buf;
1053 void *cb0, *cb;
Jack Steiner33f36482009-12-15 16:48:09 -08001054 struct gru_control_block_status *gen;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001055 int i, k, istatus, bytes;
1056
1057 bytes = numcb * 4 * 8;
1058 buf = kmalloc(bytes, GFP_KERNEL);
1059 if (!buf)
1060 return -ENOMEM;
1061
1062 ret = -EBUSY;
1063 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
1064 if (!han)
1065 goto done;
1066
1067 gru_lock_async_resource(han, &cb0, NULL);
1068 memset(buf, 0xee, bytes);
1069 for (i = 0; i < numcb; i++)
1070 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
1071 XTYPE_DW, 4, 1, IMA_INTERRUPT);
1072
1073 ret = 0;
Jack Steiner33f36482009-12-15 16:48:09 -08001074 k = numcb;
1075 do {
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001076 gru_wait_async_cbr(han);
1077 for (i = 0; i < numcb; i++) {
1078 cb = cb0 + i * GRU_HANDLE_STRIDE;
1079 istatus = gru_check_status(cb);
Jack Steiner33f36482009-12-15 16:48:09 -08001080 if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
1081 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001082 }
Jack Steiner33f36482009-12-15 16:48:09 -08001083 if (i == numcb)
1084 continue;
1085 if (istatus != CBS_IDLE) {
1086 printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
1087 ret = -EFAULT;
1088 } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
1089 buf[4 * i + 3]) {
1090 printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1091 smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
1092 ret = -EIO;
1093 }
1094 k--;
1095 gen = cb;
1096 gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
1097 } while (k);
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001098 BUG_ON(cmp.done);
1099
1100 gru_unlock_async_resource(han);
1101 gru_release_async_resources(han);
1102done:
1103 kfree(buf);
1104 return ret;
1105}
1106
Jack Steiner33f36482009-12-15 16:48:09 -08001107#define BUFSIZE 200
1108static int quicktest3(unsigned long arg)
1109{
1110 char buf1[BUFSIZE], buf2[BUFSIZE];
1111 int ret = 0;
1112
1113 memset(buf2, 0, sizeof(buf2));
1114 memset(buf1, get_cycles() & 255, sizeof(buf1));
1115 gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
1116 if (memcmp(buf1, buf2, BUFSIZE)) {
Jack Steiner563447d2009-12-15 16:48:12 -08001117 printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
Jack Steiner33f36482009-12-15 16:48:09 -08001118 ret = -EIO;
1119 }
1120 return ret;
1121}
1122
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001123/*
1124 * Debugging only. User hook for various kernel tests
1125 * of driver & gru.
1126 */
1127int gru_ktest(unsigned long arg)
1128{
1129 int ret = -EINVAL;
1130
1131 switch (arg & 0xff) {
1132 case 0:
1133 ret = quicktest0(arg);
1134 break;
1135 case 1:
1136 ret = quicktest1(arg);
1137 break;
1138 case 2:
1139 ret = quicktest2(arg);
1140 break;
Jack Steiner33f36482009-12-15 16:48:09 -08001141 case 3:
1142 ret = quicktest3(arg);
1143 break;
Jack Steinerd5826dd2009-06-17 16:28:28 -07001144 case 99:
1145 ret = gru_free_kernel_contexts();
1146 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001147 }
1148 return ret;
1149
1150}
Jack Steiner28bffaf2008-07-29 22:33:57 -07001151
Jack Steinerd5826dd2009-06-17 16:28:28 -07001152int gru_kservices_init(void)
Jack Steiner28bffaf2008-07-29 22:33:57 -07001153{
Jack Steiner28bffaf2008-07-29 22:33:57 -07001154 return 0;
1155}
Jack Steiner27ca8a72009-04-02 16:59:11 -07001156
Jack Steinerd5826dd2009-06-17 16:28:28 -07001157void gru_kservices_exit(void)
Jack Steiner27ca8a72009-04-02 16:59:11 -07001158{
Jack Steinerd5826dd2009-06-17 16:28:28 -07001159 if (gru_free_kernel_contexts())
1160 BUG();
Jack Steiner27ca8a72009-04-02 16:59:11 -07001161}
1162