blob: 4da6f56833d125d209ff4637d17b3d6c43e8f9f5 [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>
Jack Steiner28bffaf2008-07-29 22:33:57 -070034#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 Steiner836ce672009-06-17 16:28:22 -070048 * 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 Steiner9120dec2009-06-17 16:28:25 -070054 *
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 Steiner28bffaf2008-07-29 22:33:57 -070094 */
Jack Steiner9120dec2009-06-17 16:28:25 -070095
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)]
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700100#define KCB_TO_GID(cb) ((cb - gru_start_vaddr) / \
101 (GRU_SIZE * GRU_CHIPLETS_PER_BLADE))
102#define KCB_TO_BS(cb) gru_base[KCB_TO_GID(cb)]
Jack Steiner9120dec2009-06-17 16:28:25 -0700103
Jack Steiner6f2584f2009-04-02 16:59:10 -0700104#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -0700105#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -0700106#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
107 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700108
109/* GRU instruction attributes for all instructions */
110#define IMA IMA_CB_DELAY
111
112/* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
113#define __gru_cacheline_aligned__ \
114 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
115
116#define MAGIC 0x1234567887654321UL
117
118/* Default retry count for GRU errors on kernel instructions */
119#define EXCEPTION_RETRY_LIMIT 3
120
121/* Status of message queue sections */
122#define MQS_EMPTY 0
123#define MQS_FULL 1
124#define MQS_NOOP 2
125
126/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
127/* optimized for x86_64 */
128struct message_queue {
129 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
130 int qlines; /* DW 1 */
131 long hstatus[2];
132 void *next __gru_cacheline_aligned__;/* CL 1 */
133 void *limit;
134 void *start;
135 void *start2;
136 char data ____cacheline_aligned; /* CL 2 */
137};
138
139/* First word in every message - used by mesq interface */
140struct message_header {
141 char present;
142 char present2;
143 char lines;
144 char fill;
145};
146
Jack Steiner28bffaf2008-07-29 22:33:57 -0700147#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
148
Jack Steiner836ce672009-06-17 16:28:22 -0700149/*
Jack Steiner836ce672009-06-17 16:28:22 -0700150 * Reload the blade's kernel context into a GRU chiplet. Called holding
151 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
152 */
153static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
154{
155 struct gru_state *gru;
156 struct gru_thread_state *kgts;
157 void *vaddr;
Jack Steiner9120dec2009-06-17 16:28:25 -0700158 int ctxnum, ncpus;
Jack Steiner836ce672009-06-17 16:28:22 -0700159
160 up_read(&bs->bs_kgts_sema);
161 down_write(&bs->bs_kgts_sema);
162
Jack Steiner55484c42009-12-15 16:48:05 -0800163 if (!bs->bs_kgts) {
Jack Steiner9120dec2009-06-17 16:28:25 -0700164 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
Jack Steiner55484c42009-12-15 16:48:05 -0800165 bs->bs_kgts->ts_user_blade_id = blade_id;
166 }
Jack Steiner836ce672009-06-17 16:28:22 -0700167 kgts = bs->bs_kgts;
168
169 if (!kgts->ts_gru) {
170 STAT(load_kernel_context);
Jack Steiner9120dec2009-06-17 16:28:25 -0700171 ncpus = uv_blade_nr_possible_cpus(blade_id);
172 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
173 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
174 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
175 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
176 bs->bs_async_dsr_bytes);
Jack Steiner55484c42009-12-15 16:48:05 -0800177 while (!gru_assign_gru_context(kgts)) {
Jack Steiner836ce672009-06-17 16:28:22 -0700178 msleep(1);
Jack Steiner55484c42009-12-15 16:48:05 -0800179 gru_steal_context(kgts);
Jack Steiner836ce672009-06-17 16:28:22 -0700180 }
181 gru_load_context(kgts);
182 gru = bs->bs_kgts->ts_gru;
183 vaddr = gru->gs_gru_base_vaddr;
184 ctxnum = kgts->ts_ctxnum;
185 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
186 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
187 }
188 downgrade_write(&bs->bs_kgts_sema);
189}
190
191/*
Jack Steinerd5826dd2009-06-17 16:28:28 -0700192 * Free all kernel contexts that are not currently in use.
193 * Returns 0 if all freed, else number of inuse context.
194 */
195static int gru_free_kernel_contexts(void)
196{
197 struct gru_blade_state *bs;
198 struct gru_thread_state *kgts;
199 int bid, ret = 0;
200
201 for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
202 bs = gru_base[bid];
203 if (!bs)
204 continue;
Jack Steiner091f1a12009-12-15 16:48:02 -0800205
206 /* Ignore busy contexts. Don't want to block here. */
Jack Steinerd5826dd2009-06-17 16:28:28 -0700207 if (down_write_trylock(&bs->bs_kgts_sema)) {
208 kgts = bs->bs_kgts;
209 if (kgts && kgts->ts_gru)
210 gru_unload_context(kgts, 0);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700211 bs->bs_kgts = NULL;
212 up_write(&bs->bs_kgts_sema);
Jack Steiner091f1a12009-12-15 16:48:02 -0800213 kfree(kgts);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700214 } else {
215 ret++;
216 }
217 }
218 return ret;
219}
220
221/*
Jack Steiner836ce672009-06-17 16:28:22 -0700222 * Lock & load the kernel context for the specified blade.
223 */
224static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
225{
226 struct gru_blade_state *bs;
227
228 STAT(lock_kernel_context);
229 bs = gru_base[blade_id];
230
231 down_read(&bs->bs_kgts_sema);
232 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
233 gru_load_kernel_context(bs, blade_id);
234 return bs;
235
236}
237
238/*
239 * Unlock the kernel context for the specified blade. Context is not
240 * unloaded but may be stolen before next use.
241 */
242static void gru_unlock_kernel_context(int blade_id)
243{
244 struct gru_blade_state *bs;
245
246 bs = gru_base[blade_id];
247 up_read(&bs->bs_kgts_sema);
248 STAT(unlock_kernel_context);
249}
250
251/*
252 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
253 * - returns with preemption disabled
254 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700255static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
256{
257 struct gru_blade_state *bs;
258 int lcpu;
259
260 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
261 preempt_disable();
Jack Steiner836ce672009-06-17 16:28:22 -0700262 bs = gru_lock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700263 lcpu = uv_blade_processor_id();
264 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
265 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
266 return 0;
267}
268
Jack Steiner836ce672009-06-17 16:28:22 -0700269/*
270 * Free the current cpus reserved DSR/CBR resources.
271 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700272static void gru_free_cpu_resources(void *cb, void *dsr)
273{
Jack Steiner836ce672009-06-17 16:28:22 -0700274 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700275 preempt_enable();
276}
277
Jack Steiner9120dec2009-06-17 16:28:25 -0700278/*
279 * Reserve GRU resources to be used asynchronously.
280 * Note: currently supports only 1 reservation per blade.
281 *
282 * input:
283 * blade_id - blade on which resources should be reserved
284 * cbrs - number of CBRs
285 * dsr_bytes - number of DSR bytes needed
286 * output:
287 * handle to identify resource
288 * (0 = async resources already reserved)
289 */
290unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
291 struct completion *cmp)
292{
293 struct gru_blade_state *bs;
294 struct gru_thread_state *kgts;
295 int ret = 0;
296
297 bs = gru_base[blade_id];
298
299 down_write(&bs->bs_kgts_sema);
300
301 /* Verify no resources already reserved */
302 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
303 goto done;
304 bs->bs_async_dsr_bytes = dsr_bytes;
305 bs->bs_async_cbrs = cbrs;
306 bs->bs_async_wq = cmp;
307 kgts = bs->bs_kgts;
308
309 /* Resources changed. Unload context if already loaded */
310 if (kgts && kgts->ts_gru)
311 gru_unload_context(kgts, 0);
312 ret = ASYNC_BID_TO_HAN(blade_id);
313
314done:
315 up_write(&bs->bs_kgts_sema);
316 return ret;
317}
318
319/*
320 * Release async resources previously reserved.
321 *
322 * input:
323 * han - handle to identify resources
324 */
325void gru_release_async_resources(unsigned long han)
326{
327 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
328
329 down_write(&bs->bs_kgts_sema);
330 bs->bs_async_dsr_bytes = 0;
331 bs->bs_async_cbrs = 0;
332 bs->bs_async_wq = NULL;
333 up_write(&bs->bs_kgts_sema);
334}
335
336/*
337 * Wait for async GRU instructions to complete.
338 *
339 * input:
340 * han - handle to identify resources
341 */
342void gru_wait_async_cbr(unsigned long han)
343{
344 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
345
346 wait_for_completion(bs->bs_async_wq);
347 mb();
348}
349
350/*
351 * Lock previous reserved async GRU resources
352 *
353 * input:
354 * han - handle to identify resources
355 * output:
356 * cb - pointer to first CBR
357 * dsr - pointer to first DSR
358 */
359void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
360{
361 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
362 int blade_id = ASYNC_HAN_TO_BID(han);
363 int ncpus;
364
365 gru_lock_kernel_context(blade_id);
366 ncpus = uv_blade_nr_possible_cpus(blade_id);
367 if (cb)
368 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
369 if (dsr)
370 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
371}
372
373/*
374 * Unlock previous reserved async GRU resources
375 *
376 * input:
377 * han - handle to identify resources
378 */
379void gru_unlock_async_resource(unsigned long han)
380{
381 int blade_id = ASYNC_HAN_TO_BID(han);
382
383 gru_unlock_kernel_context(blade_id);
384}
385
Jack Steiner836ce672009-06-17 16:28:22 -0700386/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700387int gru_get_cb_exception_detail(void *cb,
388 struct control_block_extended_exc_detail *excdet)
389{
390 struct gru_control_block_extended *cbe;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700391 struct gru_blade_state *bs;
392 int cbrnum;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700393
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700394 bs = KCB_TO_BS(cb);
395 cbrnum = thread_cbr_number(bs->bs_kgts, get_cb_number(cb));
396 cbe = get_cbe(GRUBASE(cb), cbrnum);
397 gru_flush_cache(cbe); /* CBE not coherent */
Jack Steiner67bf04a2009-12-15 16:48:11 -0800398 sync_core();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700399 excdet->opc = cbe->opccpy;
400 excdet->exopc = cbe->exopccpy;
401 excdet->ecause = cbe->ecause;
402 excdet->exceptdet0 = cbe->idef1upd;
403 excdet->exceptdet1 = cbe->idef3upd;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700404 gru_flush_cache(cbe);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700405 return 0;
406}
407
408char *gru_get_cb_exception_detail_str(int ret, void *cb,
409 char *buf, int size)
410{
411 struct gru_control_block_status *gen = (void *)cb;
412 struct control_block_extended_exc_detail excdet;
413
414 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
415 gru_get_cb_exception_detail(cb, &excdet);
416 snprintf(buf, size,
Jack Steiner563447d2009-12-15 16:48:12 -0800417 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
418 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700419 gen, excdet.opc, excdet.exopc, excdet.ecause,
420 excdet.exceptdet0, excdet.exceptdet1);
421 } else {
422 snprintf(buf, size, "No exception");
423 }
424 return buf;
425}
426
427static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
428{
429 while (gen->istatus >= CBS_ACTIVE) {
430 cpu_relax();
431 barrier();
432 }
433 return gen->istatus;
434}
435
436static int gru_retry_exception(void *cb)
437{
438 struct gru_control_block_status *gen = (void *)cb;
439 struct control_block_extended_exc_detail excdet;
440 int retry = EXCEPTION_RETRY_LIMIT;
441
442 while (1) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700443 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
444 return CBS_IDLE;
Jack Steinerd6e2fbc2009-06-17 16:28:29 -0700445 if (gru_get_cb_message_queue_substatus(cb))
446 return CBS_EXCEPTION;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700447 gru_get_cb_exception_detail(cb, &excdet);
Jack Steiner270952a2009-06-17 16:28:27 -0700448 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
449 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
Jack Steiner28bffaf2008-07-29 22:33:57 -0700450 break;
451 if (retry-- == 0)
452 break;
453 gen->icmd = 1;
454 gru_flush_cache(gen);
455 }
456 return CBS_EXCEPTION;
457}
458
459int gru_check_status_proc(void *cb)
460{
461 struct gru_control_block_status *gen = (void *)cb;
462 int ret;
463
464 ret = gen->istatus;
Jack Steiner67bf04a2009-12-15 16:48:11 -0800465 if (ret == CBS_EXCEPTION)
466 ret = gru_retry_exception(cb);
467 rmb();
468 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700469
470}
471
472int gru_wait_proc(void *cb)
473{
474 struct gru_control_block_status *gen = (void *)cb;
475 int ret;
476
477 ret = gru_wait_idle_or_exception(gen);
478 if (ret == CBS_EXCEPTION)
479 ret = gru_retry_exception(cb);
Jack Steiner67bf04a2009-12-15 16:48:11 -0800480 rmb();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700481 return ret;
482}
483
484void gru_abort(int ret, void *cb, char *str)
485{
486 char buf[GRU_EXC_STR_SIZE];
487
488 panic("GRU FATAL ERROR: %s - %s\n", str,
489 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
490}
491
492void gru_wait_abort_proc(void *cb)
493{
494 int ret;
495
496 ret = gru_wait_proc(cb);
497 if (ret)
498 gru_abort(ret, cb, "gru_wait_abort");
499}
500
501
502/*------------------------------ MESSAGE QUEUES -----------------------------*/
503
504/* Internal status . These are NOT returned to the user. */
505#define MQIE_AGAIN -1 /* try again */
506
507
508/*
509 * Save/restore the "present" flag that is in the second line of 2-line
510 * messages
511 */
512static inline int get_present2(void *p)
513{
514 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
515 return mhdr->present;
516}
517
518static inline void restore_present2(void *p, int val)
519{
520 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
521 mhdr->present = val;
522}
523
524/*
525 * Create a message queue.
526 * qlines - message queue size in cache lines. Includes 2-line header.
527 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700528int gru_create_message_queue(struct gru_message_queue_desc *mqd,
529 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700530{
531 struct message_queue *mq = p;
532 unsigned int qlines;
533
534 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
535 memset(mq, 0, bytes);
536 mq->start = &mq->data;
537 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
538 mq->next = &mq->data;
539 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
540 mq->qlines = qlines;
541 mq->hstatus[0] = 0;
542 mq->hstatus[1] = 1;
543 mq->head = gru_mesq_head(2, qlines / 2 + 1);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700544 mqd->mq = mq;
545 mqd->mq_gpa = uv_gpa(mq);
546 mqd->qlines = qlines;
547 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
548 mqd->interrupt_vector = vector;
549 mqd->interrupt_apicid = apicid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700550 return 0;
551}
552EXPORT_SYMBOL_GPL(gru_create_message_queue);
553
554/*
555 * Send a NOOP message to a message queue
556 * Returns:
557 * 0 - if queue is full after the send. This is the normal case
558 * but various races can change this.
559 * -1 - if mesq sent successfully but queue not full
560 * >0 - unexpected error. MQE_xxx returned
561 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700562static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
563 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700564{
565 const struct message_header noop_header = {
566 .present = MQS_NOOP, .lines = 1};
567 unsigned long m;
568 int substatus, ret;
569 struct message_header save_mhdr, *mhdr = mesg;
570
571 STAT(mesq_noop);
572 save_mhdr = *mhdr;
573 *mhdr = noop_header;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700574 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700575 ret = gru_wait(cb);
576
577 if (ret) {
578 substatus = gru_get_cb_message_queue_substatus(cb);
579 switch (substatus) {
580 case CBSS_NO_ERROR:
581 STAT(mesq_noop_unexpected_error);
582 ret = MQE_UNEXPECTED_CB_ERR;
583 break;
584 case CBSS_LB_OVERFLOWED:
585 STAT(mesq_noop_lb_overflow);
586 ret = MQE_CONGESTION;
587 break;
588 case CBSS_QLIMIT_REACHED:
589 STAT(mesq_noop_qlimit_reached);
590 ret = 0;
591 break;
592 case CBSS_AMO_NACKED:
593 STAT(mesq_noop_amo_nacked);
594 ret = MQE_CONGESTION;
595 break;
596 case CBSS_PUT_NACKED:
597 STAT(mesq_noop_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700598 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700599 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
600 IMA);
601 if (gru_wait(cb) == CBS_IDLE)
602 ret = MQIE_AGAIN;
603 else
604 ret = MQE_UNEXPECTED_CB_ERR;
605 break;
606 case CBSS_PAGE_OVERFLOW:
Jack Steiner563447d2009-12-15 16:48:12 -0800607 STAT(mesq_noop_page_overflow);
608 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700609 default:
610 BUG();
611 }
612 }
613 *mhdr = save_mhdr;
614 return ret;
615}
616
617/*
618 * Handle a gru_mesq full.
619 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700620static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
621 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700622{
623 union gru_mesqhead mqh;
624 unsigned int limit, head;
625 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700626 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700627
628 /* Determine if switching to first/second half of q */
629 avalue = gru_get_amo_value(cb);
630 head = gru_get_amo_value_head(cb);
631 limit = gru_get_amo_value_limit(cb);
632
Jack Steiner6f2584f2009-04-02 16:59:10 -0700633 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700634 half = (limit != qlines);
635
636 if (half)
637 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
638 else
639 mqh = gru_mesq_head(2, qlines / 2 + 1);
640
641 /* Try to get lock for switching head pointer */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700642 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700643 if (gru_wait(cb) != CBS_IDLE)
644 goto cberr;
645 if (!gru_get_amo_value(cb)) {
646 STAT(mesq_qf_locked);
647 return MQE_QUEUE_FULL;
648 }
649
650 /* Got the lock. Send optional NOP if queue not full, */
651 if (head != limit) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700652 if (send_noop_message(cb, mqd, mesg)) {
653 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700654 XTYPE_DW, IMA);
655 if (gru_wait(cb) != CBS_IDLE)
656 goto cberr;
657 STAT(mesq_qf_noop_not_full);
658 return MQIE_AGAIN;
659 }
660 avalue++;
661 }
662
663 /* Then flip queuehead to other half of queue. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700664 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
665 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700666 if (gru_wait(cb) != CBS_IDLE)
667 goto cberr;
668
669 /* If not successfully in swapping queue head, clear the hstatus lock */
670 if (gru_get_amo_value(cb) != avalue) {
671 STAT(mesq_qf_switch_head_failed);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700672 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
673 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700674 if (gru_wait(cb) != CBS_IDLE)
675 goto cberr;
676 }
677 return MQIE_AGAIN;
678cberr:
679 STAT(mesq_qf_unexpected_error);
680 return MQE_UNEXPECTED_CB_ERR;
681}
682
Jack Steiner6f2584f2009-04-02 16:59:10 -0700683/*
684 * Send a cross-partition interrupt to the SSI that contains the target
685 * message queue. Normally, the interrupt is automatically delivered by hardware
686 * but some error conditions require explicit delivery.
687 */
688static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
689{
690 if (mqd->interrupt_vector)
691 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
692 mqd->interrupt_vector);
693}
694
Jack Steiner17b49a62009-06-17 16:28:23 -0700695/*
696 * Handle a PUT failure. Note: if message was a 2-line message, one of the
697 * lines might have successfully have been written. Before sending the
698 * message, "present" must be cleared in BOTH lines to prevent the receiver
699 * from prematurely seeing the full message.
700 */
701static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
702 void *mesg, int lines)
703{
704 unsigned long m;
705
706 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
707 if (lines == 2) {
708 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
709 if (gru_wait(cb) != CBS_IDLE)
710 return MQE_UNEXPECTED_CB_ERR;
711 }
712 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
713 if (gru_wait(cb) != CBS_IDLE)
714 return MQE_UNEXPECTED_CB_ERR;
715 send_message_queue_interrupt(mqd);
716 return MQE_OK;
717}
Jack Steiner28bffaf2008-07-29 22:33:57 -0700718
719/*
720 * Handle a gru_mesq failure. Some of these failures are software recoverable
721 * or retryable.
722 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700723static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
724 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700725{
726 int substatus, ret = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700727
728 substatus = gru_get_cb_message_queue_substatus(cb);
729 switch (substatus) {
730 case CBSS_NO_ERROR:
731 STAT(mesq_send_unexpected_error);
732 ret = MQE_UNEXPECTED_CB_ERR;
733 break;
734 case CBSS_LB_OVERFLOWED:
735 STAT(mesq_send_lb_overflow);
736 ret = MQE_CONGESTION;
737 break;
738 case CBSS_QLIMIT_REACHED:
739 STAT(mesq_send_qlimit_reached);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700740 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700741 break;
742 case CBSS_AMO_NACKED:
743 STAT(mesq_send_amo_nacked);
744 ret = MQE_CONGESTION;
745 break;
746 case CBSS_PUT_NACKED:
747 STAT(mesq_send_put_nacked);
Jack Steiner17b49a62009-06-17 16:28:23 -0700748 ret = send_message_put_nacked(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700749 break;
Jack Steiner563447d2009-12-15 16:48:12 -0800750 case CBSS_PAGE_OVERFLOW:
751 STAT(mesq_page_overflow);
752 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700753 default:
754 BUG();
755 }
756 return ret;
757}
758
759/*
760 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700761 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700762 * mesg message. ust be vaddr within a GSEG
763 * bytes message size (<= 2 CL)
764 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700765int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
766 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700767{
768 struct message_header *mhdr;
769 void *cb;
770 void *dsr;
771 int istatus, clines, ret;
772
773 STAT(mesq_send);
774 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
775
Julia Lawallcbf330b2008-10-15 22:01:27 -0700776 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700777 if (gru_get_cpu_resources(bytes, &cb, &dsr))
778 return MQE_BUG_NO_RESOURCES;
779 memcpy(dsr, mesg, bytes);
780 mhdr = dsr;
781 mhdr->present = MQS_FULL;
782 mhdr->lines = clines;
783 if (clines == 2) {
784 mhdr->present2 = get_present2(mhdr);
785 restore_present2(mhdr, MQS_FULL);
786 }
787
788 do {
789 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700790 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700791 istatus = gru_wait(cb);
792 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700793 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700794 } while (ret == MQIE_AGAIN);
795 gru_free_cpu_resources(cb, dsr);
796
797 if (ret)
798 STAT(mesq_send_failed);
799 return ret;
800}
801EXPORT_SYMBOL_GPL(gru_send_message_gpa);
802
803/*
804 * Advance the receive pointer for the queue to the next message.
805 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700806void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700807{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700808 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700809 struct message_header *mhdr = mq->next;
810 void *next, *pnext;
811 int half = -1;
812 int lines = mhdr->lines;
813
814 if (lines == 2)
815 restore_present2(mhdr, MQS_EMPTY);
816 mhdr->present = MQS_EMPTY;
817
818 pnext = mq->next;
819 next = pnext + GRU_CACHE_LINE_BYTES * lines;
820 if (next == mq->limit) {
821 next = mq->start;
822 half = 1;
823 } else if (pnext < mq->start2 && next >= mq->start2) {
824 half = 0;
825 }
826
827 if (half >= 0)
828 mq->hstatus[half] = 1;
829 mq->next = next;
830}
831EXPORT_SYMBOL_GPL(gru_free_message);
832
833/*
834 * Get next message from message queue. Return NULL if no message
835 * present. User must call next_message() to move to next message.
836 * rmq message queue
837 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700838void *gru_get_next_message(struct gru_message_queue_desc *mqd)
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 int present = mhdr->present;
843
844 /* skip NOOP messages */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700845 while (present == MQS_NOOP) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700846 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700847 mhdr = mq->next;
848 present = mhdr->present;
849 }
850
851 /* Wait for both halves of 2 line messages */
852 if (present == MQS_FULL && mhdr->lines == 2 &&
853 get_present2(mhdr) == MQS_EMPTY)
854 present = MQS_EMPTY;
855
856 if (!present) {
857 STAT(mesq_receive_none);
858 return NULL;
859 }
860
861 if (mhdr->lines == 2)
862 restore_present2(mhdr, mhdr->present2);
863
Jack Steiner563447d2009-12-15 16:48:12 -0800864 STAT(mesq_receive);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700865 return mhdr;
866}
867EXPORT_SYMBOL_GPL(gru_get_next_message);
868
869/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
870
871/*
Robin Holt289750d2009-12-15 16:47:55 -0800872 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
873 */
874int gru_read_gpa(unsigned long *value, unsigned long gpa)
875{
876 void *cb;
877 void *dsr;
878 int ret, iaa;
879
880 STAT(read_gpa);
881 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
882 return MQE_BUG_NO_RESOURCES;
883 iaa = gpa >> 62;
884 gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
885 ret = gru_wait(cb);
886 if (ret == CBS_IDLE)
887 *value = *(unsigned long *)dsr;
888 gru_free_cpu_resources(cb, dsr);
889 return ret;
890}
891EXPORT_SYMBOL_GPL(gru_read_gpa);
892
893
894/*
Jack Steiner28bffaf2008-07-29 22:33:57 -0700895 * Copy a block of data using the GRU resources
896 */
897int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
898 unsigned int bytes)
899{
900 void *cb;
901 void *dsr;
902 int ret;
903
904 STAT(copy_gpa);
905 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
906 return MQE_BUG_NO_RESOURCES;
907 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700908 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700909 ret = gru_wait(cb);
910 gru_free_cpu_resources(cb, dsr);
911 return ret;
912}
913EXPORT_SYMBOL_GPL(gru_copy_gpa);
914
915/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
916/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700917
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700918static int quicktest0(unsigned long arg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700919{
Jack Steiner836ce672009-06-17 16:28:22 -0700920 unsigned long word0;
921 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700922 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700923 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700924 unsigned long *p;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700925 int ret = -EIO;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700926
Jack Steiner836ce672009-06-17 16:28:22 -0700927 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
928 return MQE_BUG_NO_RESOURCES;
929 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700930 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700931 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700932
Jack Steiner836ce672009-06-17 16:28:22 -0700933 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700934 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800935 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700936 goto done;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700937 }
938
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700939 if (*p != MAGIC) {
Jack Steiner563447d2009-12-15 16:48:12 -0800940 printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700941 goto done;
942 }
943 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
944 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800945 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700946 goto done;
947 }
948
949 if (word0 != word1 || word1 != MAGIC) {
950 printk(KERN_DEBUG
Jack Steiner563447d2009-12-15 16:48:12 -0800951 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
952 smp_processor_id(), word1, MAGIC);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700953 goto done;
954 }
955 ret = 0;
956
957done:
958 gru_free_cpu_resources(cb, dsr);
959 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700960}
961
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700962#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
963
964static int quicktest1(unsigned long arg)
965{
966 struct gru_message_queue_desc mqd;
967 void *p, *mq;
968 unsigned long *dw;
969 int i, ret = -EIO;
970 char mes[GRU_CACHE_LINE_BYTES], *m;
971
972 /* Need 1K cacheline aligned that does not cross page boundary */
973 p = kmalloc(4096, 0);
Roel Kluin9e5f1132009-09-23 15:57:33 -0700974 if (p == NULL)
975 return -ENOMEM;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700976 mq = ALIGNUP(p, 1024);
977 memset(mes, 0xee, sizeof(mes));
978 dw = mq;
979
980 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
981 for (i = 0; i < 6; i++) {
982 mes[8] = i;
983 do {
984 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
985 } while (ret == MQE_CONGESTION);
986 if (ret)
987 break;
988 }
Jack Steiner563447d2009-12-15 16:48:12 -0800989 if (ret != MQE_QUEUE_FULL || i != 4) {
990 printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
991 smp_processor_id(), ret, i);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700992 goto done;
Jack Steiner563447d2009-12-15 16:48:12 -0800993 }
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700994
995 for (i = 0; i < 6; i++) {
996 m = gru_get_next_message(&mqd);
997 if (!m || m[8] != i)
998 break;
999 gru_free_message(&mqd, m);
1000 }
Jack Steiner563447d2009-12-15 16:48:12 -08001001 if (i != 4) {
1002 printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1003 smp_processor_id(), i, m, m ? m[8] : -1);
1004 goto done;
1005 }
1006 ret = 0;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001007
1008done:
1009 kfree(p);
1010 return ret;
1011}
1012
1013static int quicktest2(unsigned long arg)
1014{
1015 static DECLARE_COMPLETION(cmp);
1016 unsigned long han;
1017 int blade_id = 0;
1018 int numcb = 4;
1019 int ret = 0;
1020 unsigned long *buf;
1021 void *cb0, *cb;
Jack Steiner33f36482009-12-15 16:48:09 -08001022 struct gru_control_block_status *gen;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001023 int i, k, istatus, bytes;
1024
1025 bytes = numcb * 4 * 8;
1026 buf = kmalloc(bytes, GFP_KERNEL);
1027 if (!buf)
1028 return -ENOMEM;
1029
1030 ret = -EBUSY;
1031 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
1032 if (!han)
1033 goto done;
1034
1035 gru_lock_async_resource(han, &cb0, NULL);
1036 memset(buf, 0xee, bytes);
1037 for (i = 0; i < numcb; i++)
1038 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
1039 XTYPE_DW, 4, 1, IMA_INTERRUPT);
1040
1041 ret = 0;
Jack Steiner33f36482009-12-15 16:48:09 -08001042 k = numcb;
1043 do {
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001044 gru_wait_async_cbr(han);
1045 for (i = 0; i < numcb; i++) {
1046 cb = cb0 + i * GRU_HANDLE_STRIDE;
1047 istatus = gru_check_status(cb);
Jack Steiner33f36482009-12-15 16:48:09 -08001048 if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
1049 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001050 }
Jack Steiner33f36482009-12-15 16:48:09 -08001051 if (i == numcb)
1052 continue;
1053 if (istatus != CBS_IDLE) {
1054 printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
1055 ret = -EFAULT;
1056 } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
1057 buf[4 * i + 3]) {
1058 printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1059 smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
1060 ret = -EIO;
1061 }
1062 k--;
1063 gen = cb;
1064 gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
1065 } while (k);
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001066 BUG_ON(cmp.done);
1067
1068 gru_unlock_async_resource(han);
1069 gru_release_async_resources(han);
1070done:
1071 kfree(buf);
1072 return ret;
1073}
1074
Jack Steiner33f36482009-12-15 16:48:09 -08001075#define BUFSIZE 200
1076static int quicktest3(unsigned long arg)
1077{
1078 char buf1[BUFSIZE], buf2[BUFSIZE];
1079 int ret = 0;
1080
1081 memset(buf2, 0, sizeof(buf2));
1082 memset(buf1, get_cycles() & 255, sizeof(buf1));
1083 gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
1084 if (memcmp(buf1, buf2, BUFSIZE)) {
Jack Steiner563447d2009-12-15 16:48:12 -08001085 printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
Jack Steiner33f36482009-12-15 16:48:09 -08001086 ret = -EIO;
1087 }
1088 return ret;
1089}
1090
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001091/*
1092 * Debugging only. User hook for various kernel tests
1093 * of driver & gru.
1094 */
1095int gru_ktest(unsigned long arg)
1096{
1097 int ret = -EINVAL;
1098
1099 switch (arg & 0xff) {
1100 case 0:
1101 ret = quicktest0(arg);
1102 break;
1103 case 1:
1104 ret = quicktest1(arg);
1105 break;
1106 case 2:
1107 ret = quicktest2(arg);
1108 break;
Jack Steiner33f36482009-12-15 16:48:09 -08001109 case 3:
1110 ret = quicktest3(arg);
1111 break;
Jack Steinerd5826dd2009-06-17 16:28:28 -07001112 case 99:
1113 ret = gru_free_kernel_contexts();
1114 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001115 }
1116 return ret;
1117
1118}
Jack Steiner28bffaf2008-07-29 22:33:57 -07001119
Jack Steinerd5826dd2009-06-17 16:28:28 -07001120int gru_kservices_init(void)
Jack Steiner28bffaf2008-07-29 22:33:57 -07001121{
Jack Steiner28bffaf2008-07-29 22:33:57 -07001122 return 0;
1123}
Jack Steiner27ca8a72009-04-02 16:59:11 -07001124
Jack Steinerd5826dd2009-06-17 16:28:28 -07001125void gru_kservices_exit(void)
Jack Steiner27ca8a72009-04-02 16:59:11 -07001126{
Jack Steinerd5826dd2009-06-17 16:28:28 -07001127 if (gru_free_kernel_contexts())
1128 BUG();
Jack Steiner27ca8a72009-04-02 16:59:11 -07001129}
1130