blob: bfbf8fdbcd37bad3889e3a35b0de9a733aca7fee [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)]
100
Jack Steiner6f2584f2009-04-02 16:59:10 -0700101#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -0700102#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -0700103#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
104 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700105
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 */
125struct 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 */
137struct message_header {
138 char present;
139 char present2;
140 char lines;
141 char fill;
142};
143
Jack Steiner28bffaf2008-07-29 22:33:57 -0700144#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
145
Jack Steiner836ce672009-06-17 16:28:22 -0700146/*
Jack Steiner836ce672009-06-17 16:28:22 -0700147 * 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 */
150static 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 Steiner9120dec2009-06-17 16:28:25 -0700155 int ctxnum, ncpus;
Jack Steiner836ce672009-06-17 16:28:22 -0700156
157 up_read(&bs->bs_kgts_sema);
158 down_write(&bs->bs_kgts_sema);
159
Jack Steiner55484c42009-12-15 16:48:05 -0800160 if (!bs->bs_kgts) {
Jack Steinerc5502222009-12-15 16:48:13 -0800161 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0, 0);
Jack Steiner55484c42009-12-15 16:48:05 -0800162 bs->bs_kgts->ts_user_blade_id = blade_id;
163 }
Jack Steiner836ce672009-06-17 16:28:22 -0700164 kgts = bs->bs_kgts;
165
166 if (!kgts->ts_gru) {
167 STAT(load_kernel_context);
Jack Steiner9120dec2009-06-17 16:28:25 -0700168 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 Steiner55484c42009-12-15 16:48:05 -0800174 while (!gru_assign_gru_context(kgts)) {
Jack Steiner836ce672009-06-17 16:28:22 -0700175 msleep(1);
Jack Steiner55484c42009-12-15 16:48:05 -0800176 gru_steal_context(kgts);
Jack Steiner836ce672009-06-17 16:28:22 -0700177 }
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 Steinerd5826dd2009-06-17 16:28:28 -0700189 * Free all kernel contexts that are not currently in use.
190 * Returns 0 if all freed, else number of inuse context.
191 */
192static 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 Steiner091f1a12009-12-15 16:48:02 -0800202
203 /* Ignore busy contexts. Don't want to block here. */
Jack Steinerd5826dd2009-06-17 16:28:28 -0700204 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 Steinerd5826dd2009-06-17 16:28:28 -0700208 bs->bs_kgts = NULL;
209 up_write(&bs->bs_kgts_sema);
Jack Steiner091f1a12009-12-15 16:48:02 -0800210 kfree(kgts);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700211 } else {
212 ret++;
213 }
214 }
215 return ret;
216}
217
218/*
Jack Steiner836ce672009-06-17 16:28:22 -0700219 * Lock & load the kernel context for the specified blade.
220 */
221static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
222{
223 struct gru_blade_state *bs;
Jack Steiner0cd2b082009-12-15 16:48:17 -0800224 int bid;
Jack Steiner836ce672009-06-17 16:28:22 -0700225
226 STAT(lock_kernel_context);
Jack Steiner0cd2b082009-12-15 16:48:17 -0800227again:
228 bid = blade_id < 0 ? uv_numa_blade_id() : blade_id;
229 bs = gru_base[bid];
Jack Steiner836ce672009-06-17 16:28:22 -0700230
Jack Steiner0cd2b082009-12-15 16:48:17 -0800231 /* Handle the case where migration occured while waiting for the sema */
Jack Steiner836ce672009-06-17 16:28:22 -0700232 down_read(&bs->bs_kgts_sema);
Jack Steiner0cd2b082009-12-15 16:48:17 -0800233 if (blade_id < 0 && bid != uv_numa_blade_id()) {
234 up_read(&bs->bs_kgts_sema);
235 goto again;
236 }
Jack Steiner836ce672009-06-17 16:28:22 -0700237 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
Jack Steiner0cd2b082009-12-15 16:48:17 -0800238 gru_load_kernel_context(bs, bid);
Jack Steiner836ce672009-06-17 16:28:22 -0700239 return bs;
240
241}
242
243/*
244 * Unlock the kernel context for the specified blade. Context is not
245 * unloaded but may be stolen before next use.
246 */
247static void gru_unlock_kernel_context(int blade_id)
248{
249 struct gru_blade_state *bs;
250
251 bs = gru_base[blade_id];
252 up_read(&bs->bs_kgts_sema);
253 STAT(unlock_kernel_context);
254}
255
256/*
257 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
258 * - returns with preemption disabled
259 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700260static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
261{
262 struct gru_blade_state *bs;
263 int lcpu;
264
265 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
266 preempt_disable();
Jack Steiner0cd2b082009-12-15 16:48:17 -0800267 bs = gru_lock_kernel_context(-1);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700268 lcpu = uv_blade_processor_id();
269 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
270 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
271 return 0;
272}
273
Jack Steiner836ce672009-06-17 16:28:22 -0700274/*
275 * Free the current cpus reserved DSR/CBR resources.
276 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700277static void gru_free_cpu_resources(void *cb, void *dsr)
278{
Jack Steiner836ce672009-06-17 16:28:22 -0700279 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700280 preempt_enable();
281}
282
Jack Steiner9120dec2009-06-17 16:28:25 -0700283/*
284 * Reserve GRU resources to be used asynchronously.
285 * Note: currently supports only 1 reservation per blade.
286 *
287 * input:
288 * blade_id - blade on which resources should be reserved
289 * cbrs - number of CBRs
290 * dsr_bytes - number of DSR bytes needed
291 * output:
292 * handle to identify resource
293 * (0 = async resources already reserved)
294 */
295unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
296 struct completion *cmp)
297{
298 struct gru_blade_state *bs;
299 struct gru_thread_state *kgts;
300 int ret = 0;
301
302 bs = gru_base[blade_id];
303
304 down_write(&bs->bs_kgts_sema);
305
306 /* Verify no resources already reserved */
307 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
308 goto done;
309 bs->bs_async_dsr_bytes = dsr_bytes;
310 bs->bs_async_cbrs = cbrs;
311 bs->bs_async_wq = cmp;
312 kgts = bs->bs_kgts;
313
314 /* Resources changed. Unload context if already loaded */
315 if (kgts && kgts->ts_gru)
316 gru_unload_context(kgts, 0);
317 ret = ASYNC_BID_TO_HAN(blade_id);
318
319done:
320 up_write(&bs->bs_kgts_sema);
321 return ret;
322}
323
324/*
325 * Release async resources previously reserved.
326 *
327 * input:
328 * han - handle to identify resources
329 */
330void gru_release_async_resources(unsigned long han)
331{
332 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
333
334 down_write(&bs->bs_kgts_sema);
335 bs->bs_async_dsr_bytes = 0;
336 bs->bs_async_cbrs = 0;
337 bs->bs_async_wq = NULL;
338 up_write(&bs->bs_kgts_sema);
339}
340
341/*
342 * Wait for async GRU instructions to complete.
343 *
344 * input:
345 * han - handle to identify resources
346 */
347void gru_wait_async_cbr(unsigned long han)
348{
349 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
350
351 wait_for_completion(bs->bs_async_wq);
352 mb();
353}
354
355/*
356 * Lock previous reserved async GRU resources
357 *
358 * input:
359 * han - handle to identify resources
360 * output:
361 * cb - pointer to first CBR
362 * dsr - pointer to first DSR
363 */
364void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
365{
366 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
367 int blade_id = ASYNC_HAN_TO_BID(han);
368 int ncpus;
369
370 gru_lock_kernel_context(blade_id);
371 ncpus = uv_blade_nr_possible_cpus(blade_id);
372 if (cb)
373 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
374 if (dsr)
375 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
376}
377
378/*
379 * Unlock previous reserved async GRU resources
380 *
381 * input:
382 * han - handle to identify resources
383 */
384void gru_unlock_async_resource(unsigned long han)
385{
386 int blade_id = ASYNC_HAN_TO_BID(han);
387
388 gru_unlock_kernel_context(blade_id);
389}
390
Jack Steiner836ce672009-06-17 16:28:22 -0700391/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700392int gru_get_cb_exception_detail(void *cb,
393 struct control_block_extended_exc_detail *excdet)
394{
395 struct gru_control_block_extended *cbe;
Jack Steiner1848a712009-12-15 16:48:14 -0800396 struct gru_thread_state *kgts = NULL;
397 unsigned long off;
398 int cbrnum, bid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700399
Jack Steiner1848a712009-12-15 16:48:14 -0800400 /*
401 * Locate kgts for cb. This algorithm is SLOW but
402 * this function is rarely called (ie., almost never).
403 * Performance does not matter.
404 */
405 for_each_possible_blade(bid) {
406 if (!gru_base[bid])
407 break;
408 kgts = gru_base[bid]->bs_kgts;
409 if (!kgts || !kgts->ts_gru)
410 continue;
411 off = cb - kgts->ts_gru->gs_gru_base_vaddr;
412 if (off < GRU_SIZE)
413 break;
414 kgts = NULL;
415 }
416 BUG_ON(!kgts);
417 cbrnum = thread_cbr_number(kgts, get_cb_number(cb));
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700418 cbe = get_cbe(GRUBASE(cb), cbrnum);
419 gru_flush_cache(cbe); /* CBE not coherent */
Jack Steiner67bf04a2009-12-15 16:48:11 -0800420 sync_core();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700421 excdet->opc = cbe->opccpy;
422 excdet->exopc = cbe->exopccpy;
423 excdet->ecause = cbe->ecause;
424 excdet->exceptdet0 = cbe->idef1upd;
425 excdet->exceptdet1 = cbe->idef3upd;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700426 gru_flush_cache(cbe);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700427 return 0;
428}
429
430char *gru_get_cb_exception_detail_str(int ret, void *cb,
431 char *buf, int size)
432{
433 struct gru_control_block_status *gen = (void *)cb;
434 struct control_block_extended_exc_detail excdet;
435
436 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
437 gru_get_cb_exception_detail(cb, &excdet);
438 snprintf(buf, size,
Jack Steiner563447d2009-12-15 16:48:12 -0800439 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
440 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700441 gen, excdet.opc, excdet.exopc, excdet.ecause,
442 excdet.exceptdet0, excdet.exceptdet1);
443 } else {
444 snprintf(buf, size, "No exception");
445 }
446 return buf;
447}
448
449static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
450{
451 while (gen->istatus >= CBS_ACTIVE) {
452 cpu_relax();
453 barrier();
454 }
455 return gen->istatus;
456}
457
458static int gru_retry_exception(void *cb)
459{
460 struct gru_control_block_status *gen = (void *)cb;
461 struct control_block_extended_exc_detail excdet;
462 int retry = EXCEPTION_RETRY_LIMIT;
463
464 while (1) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700465 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
466 return CBS_IDLE;
Jack Steinerd6e2fbc2009-06-17 16:28:29 -0700467 if (gru_get_cb_message_queue_substatus(cb))
468 return CBS_EXCEPTION;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700469 gru_get_cb_exception_detail(cb, &excdet);
Jack Steiner270952a2009-06-17 16:28:27 -0700470 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
471 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
Jack Steiner28bffaf2008-07-29 22:33:57 -0700472 break;
473 if (retry-- == 0)
474 break;
475 gen->icmd = 1;
476 gru_flush_cache(gen);
477 }
478 return CBS_EXCEPTION;
479}
480
481int gru_check_status_proc(void *cb)
482{
483 struct gru_control_block_status *gen = (void *)cb;
484 int ret;
485
486 ret = gen->istatus;
Jack Steiner67bf04a2009-12-15 16:48:11 -0800487 if (ret == CBS_EXCEPTION)
488 ret = gru_retry_exception(cb);
489 rmb();
490 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700491
492}
493
494int gru_wait_proc(void *cb)
495{
496 struct gru_control_block_status *gen = (void *)cb;
497 int ret;
498
499 ret = gru_wait_idle_or_exception(gen);
500 if (ret == CBS_EXCEPTION)
501 ret = gru_retry_exception(cb);
Jack Steiner67bf04a2009-12-15 16:48:11 -0800502 rmb();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700503 return ret;
504}
505
506void gru_abort(int ret, void *cb, char *str)
507{
508 char buf[GRU_EXC_STR_SIZE];
509
510 panic("GRU FATAL ERROR: %s - %s\n", str,
511 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
512}
513
514void gru_wait_abort_proc(void *cb)
515{
516 int ret;
517
518 ret = gru_wait_proc(cb);
519 if (ret)
520 gru_abort(ret, cb, "gru_wait_abort");
521}
522
523
524/*------------------------------ MESSAGE QUEUES -----------------------------*/
525
526/* Internal status . These are NOT returned to the user. */
527#define MQIE_AGAIN -1 /* try again */
528
529
530/*
531 * Save/restore the "present" flag that is in the second line of 2-line
532 * messages
533 */
534static inline int get_present2(void *p)
535{
536 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
537 return mhdr->present;
538}
539
540static inline void restore_present2(void *p, int val)
541{
542 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
543 mhdr->present = val;
544}
545
546/*
547 * Create a message queue.
548 * qlines - message queue size in cache lines. Includes 2-line header.
549 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700550int gru_create_message_queue(struct gru_message_queue_desc *mqd,
551 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700552{
553 struct message_queue *mq = p;
554 unsigned int qlines;
555
556 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
557 memset(mq, 0, bytes);
558 mq->start = &mq->data;
559 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
560 mq->next = &mq->data;
561 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
562 mq->qlines = qlines;
563 mq->hstatus[0] = 0;
564 mq->hstatus[1] = 1;
565 mq->head = gru_mesq_head(2, qlines / 2 + 1);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700566 mqd->mq = mq;
567 mqd->mq_gpa = uv_gpa(mq);
568 mqd->qlines = qlines;
569 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
570 mqd->interrupt_vector = vector;
571 mqd->interrupt_apicid = apicid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700572 return 0;
573}
574EXPORT_SYMBOL_GPL(gru_create_message_queue);
575
576/*
577 * Send a NOOP message to a message queue
578 * Returns:
579 * 0 - if queue is full after the send. This is the normal case
580 * but various races can change this.
581 * -1 - if mesq sent successfully but queue not full
582 * >0 - unexpected error. MQE_xxx returned
583 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700584static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
585 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700586{
587 const struct message_header noop_header = {
588 .present = MQS_NOOP, .lines = 1};
589 unsigned long m;
590 int substatus, ret;
591 struct message_header save_mhdr, *mhdr = mesg;
592
593 STAT(mesq_noop);
594 save_mhdr = *mhdr;
595 *mhdr = noop_header;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700596 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700597 ret = gru_wait(cb);
598
599 if (ret) {
600 substatus = gru_get_cb_message_queue_substatus(cb);
601 switch (substatus) {
602 case CBSS_NO_ERROR:
603 STAT(mesq_noop_unexpected_error);
604 ret = MQE_UNEXPECTED_CB_ERR;
605 break;
606 case CBSS_LB_OVERFLOWED:
607 STAT(mesq_noop_lb_overflow);
608 ret = MQE_CONGESTION;
609 break;
610 case CBSS_QLIMIT_REACHED:
611 STAT(mesq_noop_qlimit_reached);
612 ret = 0;
613 break;
614 case CBSS_AMO_NACKED:
615 STAT(mesq_noop_amo_nacked);
616 ret = MQE_CONGESTION;
617 break;
618 case CBSS_PUT_NACKED:
619 STAT(mesq_noop_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700620 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700621 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
622 IMA);
623 if (gru_wait(cb) == CBS_IDLE)
624 ret = MQIE_AGAIN;
625 else
626 ret = MQE_UNEXPECTED_CB_ERR;
627 break;
628 case CBSS_PAGE_OVERFLOW:
Jack Steiner563447d2009-12-15 16:48:12 -0800629 STAT(mesq_noop_page_overflow);
630 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700631 default:
632 BUG();
633 }
634 }
635 *mhdr = save_mhdr;
636 return ret;
637}
638
639/*
640 * Handle a gru_mesq full.
641 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700642static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
643 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700644{
645 union gru_mesqhead mqh;
646 unsigned int limit, head;
647 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700648 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700649
650 /* Determine if switching to first/second half of q */
651 avalue = gru_get_amo_value(cb);
652 head = gru_get_amo_value_head(cb);
653 limit = gru_get_amo_value_limit(cb);
654
Jack Steiner6f2584f2009-04-02 16:59:10 -0700655 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700656 half = (limit != qlines);
657
658 if (half)
659 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
660 else
661 mqh = gru_mesq_head(2, qlines / 2 + 1);
662
663 /* Try to get lock for switching head pointer */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700664 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700665 if (gru_wait(cb) != CBS_IDLE)
666 goto cberr;
667 if (!gru_get_amo_value(cb)) {
668 STAT(mesq_qf_locked);
669 return MQE_QUEUE_FULL;
670 }
671
672 /* Got the lock. Send optional NOP if queue not full, */
673 if (head != limit) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700674 if (send_noop_message(cb, mqd, mesg)) {
675 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700676 XTYPE_DW, IMA);
677 if (gru_wait(cb) != CBS_IDLE)
678 goto cberr;
679 STAT(mesq_qf_noop_not_full);
680 return MQIE_AGAIN;
681 }
682 avalue++;
683 }
684
685 /* Then flip queuehead to other half of queue. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700686 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
687 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700688 if (gru_wait(cb) != CBS_IDLE)
689 goto cberr;
690
691 /* If not successfully in swapping queue head, clear the hstatus lock */
692 if (gru_get_amo_value(cb) != avalue) {
693 STAT(mesq_qf_switch_head_failed);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700694 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
695 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700696 if (gru_wait(cb) != CBS_IDLE)
697 goto cberr;
698 }
699 return MQIE_AGAIN;
700cberr:
701 STAT(mesq_qf_unexpected_error);
702 return MQE_UNEXPECTED_CB_ERR;
703}
704
Jack Steiner6f2584f2009-04-02 16:59:10 -0700705/*
706 * Send a cross-partition interrupt to the SSI that contains the target
707 * message queue. Normally, the interrupt is automatically delivered by hardware
708 * but some error conditions require explicit delivery.
709 */
710static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
711{
712 if (mqd->interrupt_vector)
713 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
714 mqd->interrupt_vector);
715}
716
Jack Steiner17b49a62009-06-17 16:28:23 -0700717/*
718 * Handle a PUT failure. Note: if message was a 2-line message, one of the
719 * lines might have successfully have been written. Before sending the
720 * message, "present" must be cleared in BOTH lines to prevent the receiver
721 * from prematurely seeing the full message.
722 */
723static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
724 void *mesg, int lines)
725{
726 unsigned long m;
727
728 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
729 if (lines == 2) {
730 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
731 if (gru_wait(cb) != CBS_IDLE)
732 return MQE_UNEXPECTED_CB_ERR;
733 }
734 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
735 if (gru_wait(cb) != CBS_IDLE)
736 return MQE_UNEXPECTED_CB_ERR;
737 send_message_queue_interrupt(mqd);
738 return MQE_OK;
739}
Jack Steiner28bffaf2008-07-29 22:33:57 -0700740
741/*
742 * Handle a gru_mesq failure. Some of these failures are software recoverable
743 * or retryable.
744 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700745static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
746 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700747{
748 int substatus, ret = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700749
750 substatus = gru_get_cb_message_queue_substatus(cb);
751 switch (substatus) {
752 case CBSS_NO_ERROR:
753 STAT(mesq_send_unexpected_error);
754 ret = MQE_UNEXPECTED_CB_ERR;
755 break;
756 case CBSS_LB_OVERFLOWED:
757 STAT(mesq_send_lb_overflow);
758 ret = MQE_CONGESTION;
759 break;
760 case CBSS_QLIMIT_REACHED:
761 STAT(mesq_send_qlimit_reached);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700762 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700763 break;
764 case CBSS_AMO_NACKED:
765 STAT(mesq_send_amo_nacked);
766 ret = MQE_CONGESTION;
767 break;
768 case CBSS_PUT_NACKED:
769 STAT(mesq_send_put_nacked);
Jack Steiner17b49a62009-06-17 16:28:23 -0700770 ret = send_message_put_nacked(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700771 break;
Jack Steiner563447d2009-12-15 16:48:12 -0800772 case CBSS_PAGE_OVERFLOW:
773 STAT(mesq_page_overflow);
774 /* fallthru */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700775 default:
776 BUG();
777 }
778 return ret;
779}
780
781/*
782 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700783 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700784 * mesg message. ust be vaddr within a GSEG
785 * bytes message size (<= 2 CL)
786 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700787int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
788 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700789{
790 struct message_header *mhdr;
791 void *cb;
792 void *dsr;
793 int istatus, clines, ret;
794
795 STAT(mesq_send);
796 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
797
Julia Lawallcbf330b2008-10-15 22:01:27 -0700798 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700799 if (gru_get_cpu_resources(bytes, &cb, &dsr))
800 return MQE_BUG_NO_RESOURCES;
801 memcpy(dsr, mesg, bytes);
802 mhdr = dsr;
803 mhdr->present = MQS_FULL;
804 mhdr->lines = clines;
805 if (clines == 2) {
806 mhdr->present2 = get_present2(mhdr);
807 restore_present2(mhdr, MQS_FULL);
808 }
809
810 do {
811 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700812 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700813 istatus = gru_wait(cb);
814 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700815 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700816 } while (ret == MQIE_AGAIN);
817 gru_free_cpu_resources(cb, dsr);
818
819 if (ret)
820 STAT(mesq_send_failed);
821 return ret;
822}
823EXPORT_SYMBOL_GPL(gru_send_message_gpa);
824
825/*
826 * Advance the receive pointer for the queue to the next message.
827 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700828void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700829{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700830 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700831 struct message_header *mhdr = mq->next;
832 void *next, *pnext;
833 int half = -1;
834 int lines = mhdr->lines;
835
836 if (lines == 2)
837 restore_present2(mhdr, MQS_EMPTY);
838 mhdr->present = MQS_EMPTY;
839
840 pnext = mq->next;
841 next = pnext + GRU_CACHE_LINE_BYTES * lines;
842 if (next == mq->limit) {
843 next = mq->start;
844 half = 1;
845 } else if (pnext < mq->start2 && next >= mq->start2) {
846 half = 0;
847 }
848
849 if (half >= 0)
850 mq->hstatus[half] = 1;
851 mq->next = next;
852}
853EXPORT_SYMBOL_GPL(gru_free_message);
854
855/*
856 * Get next message from message queue. Return NULL if no message
857 * present. User must call next_message() to move to next message.
858 * rmq message queue
859 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700860void *gru_get_next_message(struct gru_message_queue_desc *mqd)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700861{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700862 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700863 struct message_header *mhdr = mq->next;
864 int present = mhdr->present;
865
866 /* skip NOOP messages */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700867 while (present == MQS_NOOP) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700868 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700869 mhdr = mq->next;
870 present = mhdr->present;
871 }
872
873 /* Wait for both halves of 2 line messages */
874 if (present == MQS_FULL && mhdr->lines == 2 &&
875 get_present2(mhdr) == MQS_EMPTY)
876 present = MQS_EMPTY;
877
878 if (!present) {
879 STAT(mesq_receive_none);
880 return NULL;
881 }
882
883 if (mhdr->lines == 2)
884 restore_present2(mhdr, mhdr->present2);
885
Jack Steiner563447d2009-12-15 16:48:12 -0800886 STAT(mesq_receive);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700887 return mhdr;
888}
889EXPORT_SYMBOL_GPL(gru_get_next_message);
890
891/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
892
893/*
Robin Holt289750d2009-12-15 16:47:55 -0800894 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
895 */
896int gru_read_gpa(unsigned long *value, unsigned long gpa)
897{
898 void *cb;
899 void *dsr;
900 int ret, iaa;
901
902 STAT(read_gpa);
903 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
904 return MQE_BUG_NO_RESOURCES;
905 iaa = gpa >> 62;
906 gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
907 ret = gru_wait(cb);
908 if (ret == CBS_IDLE)
909 *value = *(unsigned long *)dsr;
910 gru_free_cpu_resources(cb, dsr);
911 return ret;
912}
913EXPORT_SYMBOL_GPL(gru_read_gpa);
914
915
916/*
Jack Steiner28bffaf2008-07-29 22:33:57 -0700917 * Copy a block of data using the GRU resources
918 */
919int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
920 unsigned int bytes)
921{
922 void *cb;
923 void *dsr;
924 int ret;
925
926 STAT(copy_gpa);
927 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
928 return MQE_BUG_NO_RESOURCES;
929 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700930 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700931 ret = gru_wait(cb);
932 gru_free_cpu_resources(cb, dsr);
933 return ret;
934}
935EXPORT_SYMBOL_GPL(gru_copy_gpa);
936
937/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
938/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700939
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700940static int quicktest0(unsigned long arg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700941{
Jack Steiner836ce672009-06-17 16:28:22 -0700942 unsigned long word0;
943 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700944 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700945 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700946 unsigned long *p;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700947 int ret = -EIO;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700948
Jack Steiner836ce672009-06-17 16:28:22 -0700949 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
950 return MQE_BUG_NO_RESOURCES;
951 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700952 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700953 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700954
Jack Steiner836ce672009-06-17 16:28:22 -0700955 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700956 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800957 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700958 goto done;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700959 }
960
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700961 if (*p != MAGIC) {
Jack Steiner563447d2009-12-15 16:48:12 -0800962 printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700963 goto done;
964 }
965 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
966 if (gru_wait(cb) != CBS_IDLE) {
Jack Steiner563447d2009-12-15 16:48:12 -0800967 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700968 goto done;
969 }
970
971 if (word0 != word1 || word1 != MAGIC) {
972 printk(KERN_DEBUG
Jack Steiner563447d2009-12-15 16:48:12 -0800973 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
974 smp_processor_id(), word1, MAGIC);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700975 goto done;
976 }
977 ret = 0;
978
979done:
980 gru_free_cpu_resources(cb, dsr);
981 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700982}
983
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700984#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
985
986static int quicktest1(unsigned long arg)
987{
988 struct gru_message_queue_desc mqd;
989 void *p, *mq;
990 unsigned long *dw;
991 int i, ret = -EIO;
992 char mes[GRU_CACHE_LINE_BYTES], *m;
993
994 /* Need 1K cacheline aligned that does not cross page boundary */
995 p = kmalloc(4096, 0);
Roel Kluin9e5f1132009-09-23 15:57:33 -0700996 if (p == NULL)
997 return -ENOMEM;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700998 mq = ALIGNUP(p, 1024);
999 memset(mes, 0xee, sizeof(mes));
1000 dw = mq;
1001
1002 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
1003 for (i = 0; i < 6; i++) {
1004 mes[8] = i;
1005 do {
1006 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
1007 } while (ret == MQE_CONGESTION);
1008 if (ret)
1009 break;
1010 }
Jack Steiner563447d2009-12-15 16:48:12 -08001011 if (ret != MQE_QUEUE_FULL || i != 4) {
1012 printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
1013 smp_processor_id(), ret, i);
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001014 goto done;
Jack Steiner563447d2009-12-15 16:48:12 -08001015 }
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001016
1017 for (i = 0; i < 6; i++) {
1018 m = gru_get_next_message(&mqd);
1019 if (!m || m[8] != i)
1020 break;
1021 gru_free_message(&mqd, m);
1022 }
Jack Steiner563447d2009-12-15 16:48:12 -08001023 if (i != 4) {
1024 printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1025 smp_processor_id(), i, m, m ? m[8] : -1);
1026 goto done;
1027 }
1028 ret = 0;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001029
1030done:
1031 kfree(p);
1032 return ret;
1033}
1034
1035static int quicktest2(unsigned long arg)
1036{
1037 static DECLARE_COMPLETION(cmp);
1038 unsigned long han;
1039 int blade_id = 0;
1040 int numcb = 4;
1041 int ret = 0;
1042 unsigned long *buf;
1043 void *cb0, *cb;
Jack Steiner33f36482009-12-15 16:48:09 -08001044 struct gru_control_block_status *gen;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001045 int i, k, istatus, bytes;
1046
1047 bytes = numcb * 4 * 8;
1048 buf = kmalloc(bytes, GFP_KERNEL);
1049 if (!buf)
1050 return -ENOMEM;
1051
1052 ret = -EBUSY;
1053 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
1054 if (!han)
1055 goto done;
1056
1057 gru_lock_async_resource(han, &cb0, NULL);
1058 memset(buf, 0xee, bytes);
1059 for (i = 0; i < numcb; i++)
1060 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
1061 XTYPE_DW, 4, 1, IMA_INTERRUPT);
1062
1063 ret = 0;
Jack Steiner33f36482009-12-15 16:48:09 -08001064 k = numcb;
1065 do {
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001066 gru_wait_async_cbr(han);
1067 for (i = 0; i < numcb; i++) {
1068 cb = cb0 + i * GRU_HANDLE_STRIDE;
1069 istatus = gru_check_status(cb);
Jack Steiner33f36482009-12-15 16:48:09 -08001070 if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
1071 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001072 }
Jack Steiner33f36482009-12-15 16:48:09 -08001073 if (i == numcb)
1074 continue;
1075 if (istatus != CBS_IDLE) {
1076 printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
1077 ret = -EFAULT;
1078 } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
1079 buf[4 * i + 3]) {
1080 printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1081 smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
1082 ret = -EIO;
1083 }
1084 k--;
1085 gen = cb;
1086 gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
1087 } while (k);
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001088 BUG_ON(cmp.done);
1089
1090 gru_unlock_async_resource(han);
1091 gru_release_async_resources(han);
1092done:
1093 kfree(buf);
1094 return ret;
1095}
1096
Jack Steiner33f36482009-12-15 16:48:09 -08001097#define BUFSIZE 200
1098static int quicktest3(unsigned long arg)
1099{
1100 char buf1[BUFSIZE], buf2[BUFSIZE];
1101 int ret = 0;
1102
1103 memset(buf2, 0, sizeof(buf2));
1104 memset(buf1, get_cycles() & 255, sizeof(buf1));
1105 gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
1106 if (memcmp(buf1, buf2, BUFSIZE)) {
Jack Steiner563447d2009-12-15 16:48:12 -08001107 printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
Jack Steiner33f36482009-12-15 16:48:09 -08001108 ret = -EIO;
1109 }
1110 return ret;
1111}
1112
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001113/*
1114 * Debugging only. User hook for various kernel tests
1115 * of driver & gru.
1116 */
1117int gru_ktest(unsigned long arg)
1118{
1119 int ret = -EINVAL;
1120
1121 switch (arg & 0xff) {
1122 case 0:
1123 ret = quicktest0(arg);
1124 break;
1125 case 1:
1126 ret = quicktest1(arg);
1127 break;
1128 case 2:
1129 ret = quicktest2(arg);
1130 break;
Jack Steiner33f36482009-12-15 16:48:09 -08001131 case 3:
1132 ret = quicktest3(arg);
1133 break;
Jack Steinerd5826dd2009-06-17 16:28:28 -07001134 case 99:
1135 ret = gru_free_kernel_contexts();
1136 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001137 }
1138 return ret;
1139
1140}
Jack Steiner28bffaf2008-07-29 22:33:57 -07001141
Jack Steinerd5826dd2009-06-17 16:28:28 -07001142int gru_kservices_init(void)
Jack Steiner28bffaf2008-07-29 22:33:57 -07001143{
Jack Steiner28bffaf2008-07-29 22:33:57 -07001144 return 0;
1145}
Jack Steiner27ca8a72009-04-02 16:59:11 -07001146
Jack Steinerd5826dd2009-06-17 16:28:28 -07001147void gru_kservices_exit(void)
Jack Steiner27ca8a72009-04-02 16:59:11 -07001148{
Jack Steinerd5826dd2009-06-17 16:28:28 -07001149 if (gru_free_kernel_contexts())
1150 BUG();
Jack Steiner27ca8a72009-04-02 16:59:11 -07001151}
1152