gru: update to rev 0.9 of gru spec
[linux-2.6/kvm.git] / drivers / misc / sgi-gru / grukservices.c
blobba6fcd963f3054cc917f92ebf45f87db54b465bd
1 /*
2 * SN Platform GRU Driver
4 * KERNEL SERVICES THAT USE THE GRU
6 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/mm.h>
27 #include <linux/smp_lock.h>
28 #include <linux/spinlock.h>
29 #include <linux/device.h>
30 #include <linux/miscdevice.h>
31 #include <linux/proc_fs.h>
32 #include <linux/interrupt.h>
33 #include <linux/uaccess.h>
34 #include <linux/delay.h>
35 #include "gru.h"
36 #include "grulib.h"
37 #include "grutables.h"
38 #include "grukservices.h"
39 #include "gru_instructions.h"
40 #include <asm/uv/uv_hub.h>
43 * Kernel GRU Usage
45 * The following is an interim algorithm for management of kernel GRU
46 * resources. This will likely be replaced when we better understand the
47 * kernel/user requirements.
49 * Blade percpu resources reserved for kernel use. These resources are
50 * reserved whenever the the kernel context for the blade is loaded. Note
51 * that the kernel context is not guaranteed to be always available. It is
52 * loaded on demand & can be stolen by a user if the user demand exceeds the
53 * kernel demand. The kernel can always reload the kernel context but
54 * a SLEEP may be required!!!.
56 * Async Overview:
58 * Each blade has one "kernel context" that owns GRU kernel resources
59 * located on the blade. Kernel drivers use GRU resources in this context
60 * for sending messages, zeroing memory, etc.
62 * The kernel context is dynamically loaded on demand. If it is not in
63 * use by the kernel, the kernel context can be unloaded & given to a user.
64 * The kernel context will be reloaded when needed. This may require that
65 * a context be stolen from a user.
66 * NOTE: frequent unloading/reloading of the kernel context is
67 * expensive. We are depending on batch schedulers, cpusets, sane
68 * drivers or some other mechanism to prevent the need for frequent
69 * stealing/reloading.
71 * The kernel context consists of two parts:
72 * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
73 * Each cpu has it's own private resources & does not share them
74 * with other cpus. These resources are used serially, ie,
75 * locked, used & unlocked on each call to a function in
76 * grukservices.
77 * (Now that we have dynamic loading of kernel contexts, I
78 * may rethink this & allow sharing between cpus....)
80 * - Additional resources can be reserved long term & used directly
81 * by UV drivers located in the kernel. Drivers using these GRU
82 * resources can use asynchronous GRU instructions that send
83 * interrupts on completion.
84 * - these resources must be explicitly locked/unlocked
85 * - locked resources prevent (obviously) the kernel
86 * context from being unloaded.
87 * - drivers using these resource directly issue their own
88 * GRU instruction and must wait/check completion.
90 * When these resources are reserved, the caller can optionally
91 * associate a wait_queue with the resources and use asynchronous
92 * GRU instructions. When an async GRU instruction completes, the
93 * driver will do a wakeup on the event.
98 #define ASYNC_HAN_TO_BID(h) ((h) - 1)
99 #define ASYNC_BID_TO_HAN(b) ((b) + 1)
100 #define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
102 #define GRU_NUM_KERNEL_CBR 1
103 #define GRU_NUM_KERNEL_DSR_BYTES 256
104 #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
105 GRU_CACHE_LINE_BYTES)
107 /* GRU instruction attributes for all instructions */
108 #define IMA IMA_CB_DELAY
110 /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
111 #define __gru_cacheline_aligned__ \
112 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
114 #define MAGIC 0x1234567887654321UL
116 /* Default retry count for GRU errors on kernel instructions */
117 #define EXCEPTION_RETRY_LIMIT 3
119 /* Status of message queue sections */
120 #define MQS_EMPTY 0
121 #define MQS_FULL 1
122 #define MQS_NOOP 2
124 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
125 /* optimized for x86_64 */
126 struct message_queue {
127 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
128 int qlines; /* DW 1 */
129 long hstatus[2];
130 void *next __gru_cacheline_aligned__;/* CL 1 */
131 void *limit;
132 void *start;
133 void *start2;
134 char data ____cacheline_aligned; /* CL 2 */
137 /* First word in every message - used by mesq interface */
138 struct message_header {
139 char present;
140 char present2;
141 char lines;
142 char fill;
145 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
148 * Reload the blade's kernel context into a GRU chiplet. Called holding
149 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
151 static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
153 struct gru_state *gru;
154 struct gru_thread_state *kgts;
155 void *vaddr;
156 int ctxnum, ncpus;
158 up_read(&bs->bs_kgts_sema);
159 down_write(&bs->bs_kgts_sema);
161 if (!bs->bs_kgts)
162 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
163 kgts = bs->bs_kgts;
165 if (!kgts->ts_gru) {
166 STAT(load_kernel_context);
167 ncpus = uv_blade_nr_possible_cpus(blade_id);
168 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
169 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
170 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
171 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
172 bs->bs_async_dsr_bytes);
173 while (!gru_assign_gru_context(kgts, blade_id)) {
174 msleep(1);
175 gru_steal_context(kgts, blade_id);
177 gru_load_context(kgts);
178 gru = bs->bs_kgts->ts_gru;
179 vaddr = gru->gs_gru_base_vaddr;
180 ctxnum = kgts->ts_ctxnum;
181 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
182 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
184 downgrade_write(&bs->bs_kgts_sema);
188 * Lock & load the kernel context for the specified blade.
190 static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
192 struct gru_blade_state *bs;
194 STAT(lock_kernel_context);
195 bs = gru_base[blade_id];
197 down_read(&bs->bs_kgts_sema);
198 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
199 gru_load_kernel_context(bs, blade_id);
200 return bs;
205 * Unlock the kernel context for the specified blade. Context is not
206 * unloaded but may be stolen before next use.
208 static void gru_unlock_kernel_context(int blade_id)
210 struct gru_blade_state *bs;
212 bs = gru_base[blade_id];
213 up_read(&bs->bs_kgts_sema);
214 STAT(unlock_kernel_context);
218 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
219 * - returns with preemption disabled
221 static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
223 struct gru_blade_state *bs;
224 int lcpu;
226 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
227 preempt_disable();
228 bs = gru_lock_kernel_context(uv_numa_blade_id());
229 lcpu = uv_blade_processor_id();
230 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
231 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
232 return 0;
236 * Free the current cpus reserved DSR/CBR resources.
238 static void gru_free_cpu_resources(void *cb, void *dsr)
240 gru_unlock_kernel_context(uv_numa_blade_id());
241 preempt_enable();
245 * Reserve GRU resources to be used asynchronously.
246 * Note: currently supports only 1 reservation per blade.
248 * input:
249 * blade_id - blade on which resources should be reserved
250 * cbrs - number of CBRs
251 * dsr_bytes - number of DSR bytes needed
252 * output:
253 * handle to identify resource
254 * (0 = async resources already reserved)
256 unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
257 struct completion *cmp)
259 struct gru_blade_state *bs;
260 struct gru_thread_state *kgts;
261 int ret = 0;
263 bs = gru_base[blade_id];
265 down_write(&bs->bs_kgts_sema);
267 /* Verify no resources already reserved */
268 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
269 goto done;
270 bs->bs_async_dsr_bytes = dsr_bytes;
271 bs->bs_async_cbrs = cbrs;
272 bs->bs_async_wq = cmp;
273 kgts = bs->bs_kgts;
275 /* Resources changed. Unload context if already loaded */
276 if (kgts && kgts->ts_gru)
277 gru_unload_context(kgts, 0);
278 ret = ASYNC_BID_TO_HAN(blade_id);
280 done:
281 up_write(&bs->bs_kgts_sema);
282 return ret;
286 * Release async resources previously reserved.
288 * input:
289 * han - handle to identify resources
291 void gru_release_async_resources(unsigned long han)
293 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
295 down_write(&bs->bs_kgts_sema);
296 bs->bs_async_dsr_bytes = 0;
297 bs->bs_async_cbrs = 0;
298 bs->bs_async_wq = NULL;
299 up_write(&bs->bs_kgts_sema);
303 * Wait for async GRU instructions to complete.
305 * input:
306 * han - handle to identify resources
308 void gru_wait_async_cbr(unsigned long han)
310 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
312 wait_for_completion(bs->bs_async_wq);
313 mb();
317 * Lock previous reserved async GRU resources
319 * input:
320 * han - handle to identify resources
321 * output:
322 * cb - pointer to first CBR
323 * dsr - pointer to first DSR
325 void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
327 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
328 int blade_id = ASYNC_HAN_TO_BID(han);
329 int ncpus;
331 gru_lock_kernel_context(blade_id);
332 ncpus = uv_blade_nr_possible_cpus(blade_id);
333 if (cb)
334 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
335 if (dsr)
336 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
340 * Unlock previous reserved async GRU resources
342 * input:
343 * han - handle to identify resources
345 void gru_unlock_async_resource(unsigned long han)
347 int blade_id = ASYNC_HAN_TO_BID(han);
349 gru_unlock_kernel_context(blade_id);
352 /*----------------------------------------------------------------------*/
353 int gru_get_cb_exception_detail(void *cb,
354 struct control_block_extended_exc_detail *excdet)
356 struct gru_control_block_extended *cbe;
358 cbe = get_cbe(GRUBASE(cb), get_cb_number(cb));
359 prefetchw(cbe); /* Harmless on hardware, required for emulator */
360 excdet->opc = cbe->opccpy;
361 excdet->exopc = cbe->exopccpy;
362 excdet->ecause = cbe->ecause;
363 excdet->exceptdet0 = cbe->idef1upd;
364 excdet->exceptdet1 = cbe->idef3upd;
365 return 0;
368 char *gru_get_cb_exception_detail_str(int ret, void *cb,
369 char *buf, int size)
371 struct gru_control_block_status *gen = (void *)cb;
372 struct control_block_extended_exc_detail excdet;
374 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
375 gru_get_cb_exception_detail(cb, &excdet);
376 snprintf(buf, size,
377 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
378 "excdet0 0x%lx, excdet1 0x%x",
379 gen, excdet.opc, excdet.exopc, excdet.ecause,
380 excdet.exceptdet0, excdet.exceptdet1);
381 } else {
382 snprintf(buf, size, "No exception");
384 return buf;
387 static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
389 while (gen->istatus >= CBS_ACTIVE) {
390 cpu_relax();
391 barrier();
393 return gen->istatus;
396 static int gru_retry_exception(void *cb)
398 struct gru_control_block_status *gen = (void *)cb;
399 struct control_block_extended_exc_detail excdet;
400 int retry = EXCEPTION_RETRY_LIMIT;
402 while (1) {
403 if (gru_get_cb_message_queue_substatus(cb))
404 break;
405 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
406 return CBS_IDLE;
408 gru_get_cb_exception_detail(cb, &excdet);
409 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
410 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
411 break;
412 if (retry-- == 0)
413 break;
414 gen->icmd = 1;
415 gru_flush_cache(gen);
417 return CBS_EXCEPTION;
420 int gru_check_status_proc(void *cb)
422 struct gru_control_block_status *gen = (void *)cb;
423 int ret;
425 ret = gen->istatus;
426 if (ret != CBS_EXCEPTION)
427 return ret;
428 return gru_retry_exception(cb);
432 int gru_wait_proc(void *cb)
434 struct gru_control_block_status *gen = (void *)cb;
435 int ret;
437 ret = gru_wait_idle_or_exception(gen);
438 if (ret == CBS_EXCEPTION)
439 ret = gru_retry_exception(cb);
441 return ret;
444 void gru_abort(int ret, void *cb, char *str)
446 char buf[GRU_EXC_STR_SIZE];
448 panic("GRU FATAL ERROR: %s - %s\n", str,
449 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
452 void gru_wait_abort_proc(void *cb)
454 int ret;
456 ret = gru_wait_proc(cb);
457 if (ret)
458 gru_abort(ret, cb, "gru_wait_abort");
462 /*------------------------------ MESSAGE QUEUES -----------------------------*/
464 /* Internal status . These are NOT returned to the user. */
465 #define MQIE_AGAIN -1 /* try again */
469 * Save/restore the "present" flag that is in the second line of 2-line
470 * messages
472 static inline int get_present2(void *p)
474 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
475 return mhdr->present;
478 static inline void restore_present2(void *p, int val)
480 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
481 mhdr->present = val;
485 * Create a message queue.
486 * qlines - message queue size in cache lines. Includes 2-line header.
488 int gru_create_message_queue(struct gru_message_queue_desc *mqd,
489 void *p, unsigned int bytes, int nasid, int vector, int apicid)
491 struct message_queue *mq = p;
492 unsigned int qlines;
494 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
495 memset(mq, 0, bytes);
496 mq->start = &mq->data;
497 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
498 mq->next = &mq->data;
499 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
500 mq->qlines = qlines;
501 mq->hstatus[0] = 0;
502 mq->hstatus[1] = 1;
503 mq->head = gru_mesq_head(2, qlines / 2 + 1);
504 mqd->mq = mq;
505 mqd->mq_gpa = uv_gpa(mq);
506 mqd->qlines = qlines;
507 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
508 mqd->interrupt_vector = vector;
509 mqd->interrupt_apicid = apicid;
510 return 0;
512 EXPORT_SYMBOL_GPL(gru_create_message_queue);
515 * Send a NOOP message to a message queue
516 * Returns:
517 * 0 - if queue is full after the send. This is the normal case
518 * but various races can change this.
519 * -1 - if mesq sent successfully but queue not full
520 * >0 - unexpected error. MQE_xxx returned
522 static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
523 void *mesg)
525 const struct message_header noop_header = {
526 .present = MQS_NOOP, .lines = 1};
527 unsigned long m;
528 int substatus, ret;
529 struct message_header save_mhdr, *mhdr = mesg;
531 STAT(mesq_noop);
532 save_mhdr = *mhdr;
533 *mhdr = noop_header;
534 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
535 ret = gru_wait(cb);
537 if (ret) {
538 substatus = gru_get_cb_message_queue_substatus(cb);
539 switch (substatus) {
540 case CBSS_NO_ERROR:
541 STAT(mesq_noop_unexpected_error);
542 ret = MQE_UNEXPECTED_CB_ERR;
543 break;
544 case CBSS_LB_OVERFLOWED:
545 STAT(mesq_noop_lb_overflow);
546 ret = MQE_CONGESTION;
547 break;
548 case CBSS_QLIMIT_REACHED:
549 STAT(mesq_noop_qlimit_reached);
550 ret = 0;
551 break;
552 case CBSS_AMO_NACKED:
553 STAT(mesq_noop_amo_nacked);
554 ret = MQE_CONGESTION;
555 break;
556 case CBSS_PUT_NACKED:
557 STAT(mesq_noop_put_nacked);
558 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
559 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
560 IMA);
561 if (gru_wait(cb) == CBS_IDLE)
562 ret = MQIE_AGAIN;
563 else
564 ret = MQE_UNEXPECTED_CB_ERR;
565 break;
566 case CBSS_PAGE_OVERFLOW:
567 default:
568 BUG();
571 *mhdr = save_mhdr;
572 return ret;
576 * Handle a gru_mesq full.
578 static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
579 void *mesg, int lines)
581 union gru_mesqhead mqh;
582 unsigned int limit, head;
583 unsigned long avalue;
584 int half, qlines;
586 /* Determine if switching to first/second half of q */
587 avalue = gru_get_amo_value(cb);
588 head = gru_get_amo_value_head(cb);
589 limit = gru_get_amo_value_limit(cb);
591 qlines = mqd->qlines;
592 half = (limit != qlines);
594 if (half)
595 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
596 else
597 mqh = gru_mesq_head(2, qlines / 2 + 1);
599 /* Try to get lock for switching head pointer */
600 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
601 if (gru_wait(cb) != CBS_IDLE)
602 goto cberr;
603 if (!gru_get_amo_value(cb)) {
604 STAT(mesq_qf_locked);
605 return MQE_QUEUE_FULL;
608 /* Got the lock. Send optional NOP if queue not full, */
609 if (head != limit) {
610 if (send_noop_message(cb, mqd, mesg)) {
611 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
612 XTYPE_DW, IMA);
613 if (gru_wait(cb) != CBS_IDLE)
614 goto cberr;
615 STAT(mesq_qf_noop_not_full);
616 return MQIE_AGAIN;
618 avalue++;
621 /* Then flip queuehead to other half of queue. */
622 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
623 IMA);
624 if (gru_wait(cb) != CBS_IDLE)
625 goto cberr;
627 /* If not successfully in swapping queue head, clear the hstatus lock */
628 if (gru_get_amo_value(cb) != avalue) {
629 STAT(mesq_qf_switch_head_failed);
630 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
631 IMA);
632 if (gru_wait(cb) != CBS_IDLE)
633 goto cberr;
635 return MQIE_AGAIN;
636 cberr:
637 STAT(mesq_qf_unexpected_error);
638 return MQE_UNEXPECTED_CB_ERR;
642 * Send a cross-partition interrupt to the SSI that contains the target
643 * message queue. Normally, the interrupt is automatically delivered by hardware
644 * but some error conditions require explicit delivery.
646 static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
648 if (mqd->interrupt_vector)
649 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
650 mqd->interrupt_vector);
654 * Handle a PUT failure. Note: if message was a 2-line message, one of the
655 * lines might have successfully have been written. Before sending the
656 * message, "present" must be cleared in BOTH lines to prevent the receiver
657 * from prematurely seeing the full message.
659 static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
660 void *mesg, int lines)
662 unsigned long m;
664 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
665 if (lines == 2) {
666 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
667 if (gru_wait(cb) != CBS_IDLE)
668 return MQE_UNEXPECTED_CB_ERR;
670 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
671 if (gru_wait(cb) != CBS_IDLE)
672 return MQE_UNEXPECTED_CB_ERR;
673 send_message_queue_interrupt(mqd);
674 return MQE_OK;
678 * Handle a gru_mesq failure. Some of these failures are software recoverable
679 * or retryable.
681 static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
682 void *mesg, int lines)
684 int substatus, ret = 0;
686 substatus = gru_get_cb_message_queue_substatus(cb);
687 switch (substatus) {
688 case CBSS_NO_ERROR:
689 STAT(mesq_send_unexpected_error);
690 ret = MQE_UNEXPECTED_CB_ERR;
691 break;
692 case CBSS_LB_OVERFLOWED:
693 STAT(mesq_send_lb_overflow);
694 ret = MQE_CONGESTION;
695 break;
696 case CBSS_QLIMIT_REACHED:
697 STAT(mesq_send_qlimit_reached);
698 ret = send_message_queue_full(cb, mqd, mesg, lines);
699 break;
700 case CBSS_AMO_NACKED:
701 STAT(mesq_send_amo_nacked);
702 ret = MQE_CONGESTION;
703 break;
704 case CBSS_PUT_NACKED:
705 STAT(mesq_send_put_nacked);
706 ret = send_message_put_nacked(cb, mqd, mesg, lines);
707 break;
708 default:
709 BUG();
711 return ret;
715 * Send a message to a message queue
716 * mqd message queue descriptor
717 * mesg message. ust be vaddr within a GSEG
718 * bytes message size (<= 2 CL)
720 int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
721 unsigned int bytes)
723 struct message_header *mhdr;
724 void *cb;
725 void *dsr;
726 int istatus, clines, ret;
728 STAT(mesq_send);
729 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
731 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
732 if (gru_get_cpu_resources(bytes, &cb, &dsr))
733 return MQE_BUG_NO_RESOURCES;
734 memcpy(dsr, mesg, bytes);
735 mhdr = dsr;
736 mhdr->present = MQS_FULL;
737 mhdr->lines = clines;
738 if (clines == 2) {
739 mhdr->present2 = get_present2(mhdr);
740 restore_present2(mhdr, MQS_FULL);
743 do {
744 ret = MQE_OK;
745 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
746 istatus = gru_wait(cb);
747 if (istatus != CBS_IDLE)
748 ret = send_message_failure(cb, mqd, dsr, clines);
749 } while (ret == MQIE_AGAIN);
750 gru_free_cpu_resources(cb, dsr);
752 if (ret)
753 STAT(mesq_send_failed);
754 return ret;
756 EXPORT_SYMBOL_GPL(gru_send_message_gpa);
759 * Advance the receive pointer for the queue to the next message.
761 void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
763 struct message_queue *mq = mqd->mq;
764 struct message_header *mhdr = mq->next;
765 void *next, *pnext;
766 int half = -1;
767 int lines = mhdr->lines;
769 if (lines == 2)
770 restore_present2(mhdr, MQS_EMPTY);
771 mhdr->present = MQS_EMPTY;
773 pnext = mq->next;
774 next = pnext + GRU_CACHE_LINE_BYTES * lines;
775 if (next == mq->limit) {
776 next = mq->start;
777 half = 1;
778 } else if (pnext < mq->start2 && next >= mq->start2) {
779 half = 0;
782 if (half >= 0)
783 mq->hstatus[half] = 1;
784 mq->next = next;
786 EXPORT_SYMBOL_GPL(gru_free_message);
789 * Get next message from message queue. Return NULL if no message
790 * present. User must call next_message() to move to next message.
791 * rmq message queue
793 void *gru_get_next_message(struct gru_message_queue_desc *mqd)
795 struct message_queue *mq = mqd->mq;
796 struct message_header *mhdr = mq->next;
797 int present = mhdr->present;
799 /* skip NOOP messages */
800 STAT(mesq_receive);
801 while (present == MQS_NOOP) {
802 gru_free_message(mqd, mhdr);
803 mhdr = mq->next;
804 present = mhdr->present;
807 /* Wait for both halves of 2 line messages */
808 if (present == MQS_FULL && mhdr->lines == 2 &&
809 get_present2(mhdr) == MQS_EMPTY)
810 present = MQS_EMPTY;
812 if (!present) {
813 STAT(mesq_receive_none);
814 return NULL;
817 if (mhdr->lines == 2)
818 restore_present2(mhdr, mhdr->present2);
820 return mhdr;
822 EXPORT_SYMBOL_GPL(gru_get_next_message);
824 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
827 * Copy a block of data using the GRU resources
829 int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
830 unsigned int bytes)
832 void *cb;
833 void *dsr;
834 int ret;
836 STAT(copy_gpa);
837 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
838 return MQE_BUG_NO_RESOURCES;
839 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
840 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
841 ret = gru_wait(cb);
842 gru_free_cpu_resources(cb, dsr);
843 return ret;
845 EXPORT_SYMBOL_GPL(gru_copy_gpa);
847 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
848 /* Temp - will delete after we gain confidence in the GRU */
850 static int quicktest0(unsigned long arg)
852 unsigned long word0;
853 unsigned long word1;
854 void *cb;
855 void *dsr;
856 unsigned long *p;
857 int ret = -EIO;
859 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
860 return MQE_BUG_NO_RESOURCES;
861 p = dsr;
862 word0 = MAGIC;
863 word1 = 0;
865 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
866 if (gru_wait(cb) != CBS_IDLE) {
867 printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
868 goto done;
871 if (*p != MAGIC) {
872 printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
873 goto done;
875 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
876 if (gru_wait(cb) != CBS_IDLE) {
877 printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
878 goto done;
881 if (word0 != word1 || word1 != MAGIC) {
882 printk(KERN_DEBUG
883 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
884 word1, MAGIC);
885 goto done;
887 ret = 0;
889 done:
890 gru_free_cpu_resources(cb, dsr);
891 return ret;
894 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
896 static int quicktest1(unsigned long arg)
898 struct gru_message_queue_desc mqd;
899 void *p, *mq;
900 unsigned long *dw;
901 int i, ret = -EIO;
902 char mes[GRU_CACHE_LINE_BYTES], *m;
904 /* Need 1K cacheline aligned that does not cross page boundary */
905 p = kmalloc(4096, 0);
906 mq = ALIGNUP(p, 1024);
907 memset(mes, 0xee, sizeof(mes));
908 dw = mq;
910 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
911 for (i = 0; i < 6; i++) {
912 mes[8] = i;
913 do {
914 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
915 } while (ret == MQE_CONGESTION);
916 if (ret)
917 break;
919 if (ret != MQE_QUEUE_FULL || i != 4)
920 goto done;
922 for (i = 0; i < 6; i++) {
923 m = gru_get_next_message(&mqd);
924 if (!m || m[8] != i)
925 break;
926 gru_free_message(&mqd, m);
928 ret = (i == 4) ? 0 : -EIO;
930 done:
931 kfree(p);
932 return ret;
935 static int quicktest2(unsigned long arg)
937 static DECLARE_COMPLETION(cmp);
938 unsigned long han;
939 int blade_id = 0;
940 int numcb = 4;
941 int ret = 0;
942 unsigned long *buf;
943 void *cb0, *cb;
944 int i, k, istatus, bytes;
946 bytes = numcb * 4 * 8;
947 buf = kmalloc(bytes, GFP_KERNEL);
948 if (!buf)
949 return -ENOMEM;
951 ret = -EBUSY;
952 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
953 if (!han)
954 goto done;
956 gru_lock_async_resource(han, &cb0, NULL);
957 memset(buf, 0xee, bytes);
958 for (i = 0; i < numcb; i++)
959 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
960 XTYPE_DW, 4, 1, IMA_INTERRUPT);
962 ret = 0;
963 for (k = 0; k < numcb; k++) {
964 gru_wait_async_cbr(han);
965 for (i = 0; i < numcb; i++) {
966 cb = cb0 + i * GRU_HANDLE_STRIDE;
967 istatus = gru_check_status(cb);
968 if (istatus == CBS_ACTIVE)
969 continue;
970 if (istatus == CBS_EXCEPTION)
971 ret = -EFAULT;
972 else if (buf[i] || buf[i + 1] || buf[i + 2] ||
973 buf[i + 3])
974 ret = -EIO;
977 BUG_ON(cmp.done);
979 gru_unlock_async_resource(han);
980 gru_release_async_resources(han);
981 done:
982 kfree(buf);
983 return ret;
987 * Debugging only. User hook for various kernel tests
988 * of driver & gru.
990 int gru_ktest(unsigned long arg)
992 int ret = -EINVAL;
994 switch (arg & 0xff) {
995 case 0:
996 ret = quicktest0(arg);
997 break;
998 case 1:
999 ret = quicktest1(arg);
1000 break;
1001 case 2:
1002 ret = quicktest2(arg);
1003 break;
1005 return ret;
1009 int gru_kservices_init(struct gru_state *gru)
1011 struct gru_blade_state *bs;
1013 bs = gru->gs_blade;
1014 if (gru != &bs->bs_grus[0])
1015 return 0;
1017 init_rwsem(&bs->bs_kgts_sema);
1018 return 0;
1021 void gru_kservices_exit(struct gru_state *gru)
1023 struct gru_blade_state *bs;
1024 struct gru_thread_state *kgts;
1026 bs = gru->gs_blade;
1027 if (gru != &bs->bs_grus[0])
1028 return;
1030 kgts = bs->bs_kgts;
1031 if (kgts && kgts->ts_gru)
1032 gru_unload_context(kgts, 0);
1033 kfree(kgts);