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>
27 #include <linux/spinlock.h>
28 #include <linux/device.h>
29 #include <linux/miscdevice.h>
30 #include <linux/proc_fs.h>
31 #include <linux/interrupt.h>
32 #include <linux/uaccess.h>
33 #include <linux/delay.h>
36 #include "grutables.h"
37 #include "grukservices.h"
38 #include "gru_instructions.h"
39 #include <asm/uv/uv_hub.h>
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.
48 * Blade percpu resources reserved for kernel use. These resources are
49 * reserved whenever the the kernel context for the blade is loaded. Note
50 * that the kernel context is not guaranteed to be always available. It is
51 * loaded on demand & can be stolen by a user if the user demand exceeds the
52 * kernel demand. The kernel can always reload the kernel context but
53 * a SLEEP may be required!!!.
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.
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
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
76 * (Now that we have dynamic loading of kernel contexts, I
77 * may rethink this & allow sharing between cpus....)
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.
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.
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)]
101 #define GRU_NUM_KERNEL_CBR 1
102 #define GRU_NUM_KERNEL_DSR_BYTES 256
103 #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
104 GRU_CACHE_LINE_BYTES)
106 /* GRU instruction attributes for all instructions */
107 #define IMA IMA_CB_DELAY
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)))
113 #define MAGIC 0x1234567887654321UL
115 /* Default retry count for GRU errors on kernel instructions */
116 #define EXCEPTION_RETRY_LIMIT 3
118 /* Status of message queue sections */
123 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
124 /* optimized for x86_64 */
125 struct message_queue {
126 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
127 int qlines; /* DW 1 */
129 void *next __gru_cacheline_aligned__;/* CL 1 */
133 char data ____cacheline_aligned; /* CL 2 */
136 /* First word in every message - used by mesq interface */
137 struct message_header {
144 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
147 * Reload the blade's kernel context into a GRU chiplet. Called holding
148 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
150 static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
152 struct gru_state *gru;
153 struct gru_thread_state *kgts;
157 up_read(&bs->bs_kgts_sema);
158 down_write(&bs->bs_kgts_sema);
161 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0, 0);
162 bs->bs_kgts->ts_user_blade_id = blade_id;
167 STAT(load_kernel_context);
168 ncpus = uv_blade_nr_possible_cpus(blade_id);
169 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
170 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
171 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
172 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
173 bs->bs_async_dsr_bytes);
174 while (!gru_assign_gru_context(kgts)) {
176 gru_steal_context(kgts);
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);
185 downgrade_write(&bs->bs_kgts_sema);
189 * Free all kernel contexts that are not currently in use.
190 * Returns 0 if all freed, else number of inuse context.
192 static int gru_free_kernel_contexts(void)
194 struct gru_blade_state *bs;
195 struct gru_thread_state *kgts;
198 for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
203 /* Ignore busy contexts. Don't want to block here. */
204 if (down_write_trylock(&bs->bs_kgts_sema)) {
206 if (kgts && kgts->ts_gru)
207 gru_unload_context(kgts, 0);
209 up_write(&bs->bs_kgts_sema);
219 * Lock & load the kernel context for the specified blade.
221 static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
223 struct gru_blade_state *bs;
225 STAT(lock_kernel_context);
226 bs = gru_base[blade_id];
228 down_read(&bs->bs_kgts_sema);
229 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
230 gru_load_kernel_context(bs, blade_id);
236 * Unlock the kernel context for the specified blade. Context is not
237 * unloaded but may be stolen before next use.
239 static void gru_unlock_kernel_context(int blade_id)
241 struct gru_blade_state *bs;
243 bs = gru_base[blade_id];
244 up_read(&bs->bs_kgts_sema);
245 STAT(unlock_kernel_context);
249 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
250 * - returns with preemption disabled
252 static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
254 struct gru_blade_state *bs;
257 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
259 bs = gru_lock_kernel_context(uv_numa_blade_id());
260 lcpu = uv_blade_processor_id();
261 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
262 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
267 * Free the current cpus reserved DSR/CBR resources.
269 static void gru_free_cpu_resources(void *cb, void *dsr)
271 gru_unlock_kernel_context(uv_numa_blade_id());
276 * Reserve GRU resources to be used asynchronously.
277 * Note: currently supports only 1 reservation per blade.
280 * blade_id - blade on which resources should be reserved
281 * cbrs - number of CBRs
282 * dsr_bytes - number of DSR bytes needed
284 * handle to identify resource
285 * (0 = async resources already reserved)
287 unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
288 struct completion *cmp)
290 struct gru_blade_state *bs;
291 struct gru_thread_state *kgts;
294 bs = gru_base[blade_id];
296 down_write(&bs->bs_kgts_sema);
298 /* Verify no resources already reserved */
299 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
301 bs->bs_async_dsr_bytes = dsr_bytes;
302 bs->bs_async_cbrs = cbrs;
303 bs->bs_async_wq = cmp;
306 /* Resources changed. Unload context if already loaded */
307 if (kgts && kgts->ts_gru)
308 gru_unload_context(kgts, 0);
309 ret = ASYNC_BID_TO_HAN(blade_id);
312 up_write(&bs->bs_kgts_sema);
317 * Release async resources previously reserved.
320 * han - handle to identify resources
322 void gru_release_async_resources(unsigned long han)
324 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
326 down_write(&bs->bs_kgts_sema);
327 bs->bs_async_dsr_bytes = 0;
328 bs->bs_async_cbrs = 0;
329 bs->bs_async_wq = NULL;
330 up_write(&bs->bs_kgts_sema);
334 * Wait for async GRU instructions to complete.
337 * han - handle to identify resources
339 void gru_wait_async_cbr(unsigned long han)
341 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
343 wait_for_completion(bs->bs_async_wq);
348 * Lock previous reserved async GRU resources
351 * han - handle to identify resources
353 * cb - pointer to first CBR
354 * dsr - pointer to first DSR
356 void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
358 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
359 int blade_id = ASYNC_HAN_TO_BID(han);
362 gru_lock_kernel_context(blade_id);
363 ncpus = uv_blade_nr_possible_cpus(blade_id);
365 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
367 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
371 * Unlock previous reserved async GRU resources
374 * han - handle to identify resources
376 void gru_unlock_async_resource(unsigned long han)
378 int blade_id = ASYNC_HAN_TO_BID(han);
380 gru_unlock_kernel_context(blade_id);
383 /*----------------------------------------------------------------------*/
384 int gru_get_cb_exception_detail(void *cb,
385 struct control_block_extended_exc_detail *excdet)
387 struct gru_control_block_extended *cbe;
388 struct gru_thread_state *kgts = NULL;
393 * Locate kgts for cb. This algorithm is SLOW but
394 * this function is rarely called (ie., almost never).
395 * Performance does not matter.
397 for_each_possible_blade(bid) {
400 kgts = gru_base[bid]->bs_kgts;
401 if (!kgts || !kgts->ts_gru)
403 off = cb - kgts->ts_gru->gs_gru_base_vaddr;
409 cbrnum = thread_cbr_number(kgts, get_cb_number(cb));
410 cbe = get_cbe(GRUBASE(cb), cbrnum);
411 gru_flush_cache(cbe); /* CBE not coherent */
413 excdet->opc = cbe->opccpy;
414 excdet->exopc = cbe->exopccpy;
415 excdet->ecause = cbe->ecause;
416 excdet->exceptdet0 = cbe->idef1upd;
417 excdet->exceptdet1 = cbe->idef3upd;
418 gru_flush_cache(cbe);
422 char *gru_get_cb_exception_detail_str(int ret, void *cb,
425 struct gru_control_block_status *gen = (void *)cb;
426 struct control_block_extended_exc_detail excdet;
428 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
429 gru_get_cb_exception_detail(cb, &excdet);
431 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
432 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
433 gen, excdet.opc, excdet.exopc, excdet.ecause,
434 excdet.exceptdet0, excdet.exceptdet1);
436 snprintf(buf, size, "No exception");
441 static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
443 while (gen->istatus >= CBS_ACTIVE) {
450 static int gru_retry_exception(void *cb)
452 struct gru_control_block_status *gen = (void *)cb;
453 struct control_block_extended_exc_detail excdet;
454 int retry = EXCEPTION_RETRY_LIMIT;
457 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
459 if (gru_get_cb_message_queue_substatus(cb))
460 return CBS_EXCEPTION;
461 gru_get_cb_exception_detail(cb, &excdet);
462 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
463 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
468 gru_flush_cache(gen);
470 return CBS_EXCEPTION;
473 int gru_check_status_proc(void *cb)
475 struct gru_control_block_status *gen = (void *)cb;
479 if (ret == CBS_EXCEPTION)
480 ret = gru_retry_exception(cb);
486 int gru_wait_proc(void *cb)
488 struct gru_control_block_status *gen = (void *)cb;
491 ret = gru_wait_idle_or_exception(gen);
492 if (ret == CBS_EXCEPTION)
493 ret = gru_retry_exception(cb);
498 void gru_abort(int ret, void *cb, char *str)
500 char buf[GRU_EXC_STR_SIZE];
502 panic("GRU FATAL ERROR: %s - %s\n", str,
503 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
506 void gru_wait_abort_proc(void *cb)
510 ret = gru_wait_proc(cb);
512 gru_abort(ret, cb, "gru_wait_abort");
516 /*------------------------------ MESSAGE QUEUES -----------------------------*/
518 /* Internal status . These are NOT returned to the user. */
519 #define MQIE_AGAIN -1 /* try again */
523 * Save/restore the "present" flag that is in the second line of 2-line
526 static inline int get_present2(void *p)
528 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
529 return mhdr->present;
532 static inline void restore_present2(void *p, int val)
534 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
539 * Create a message queue.
540 * qlines - message queue size in cache lines. Includes 2-line header.
542 int gru_create_message_queue(struct gru_message_queue_desc *mqd,
543 void *p, unsigned int bytes, int nasid, int vector, int apicid)
545 struct message_queue *mq = p;
548 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
549 memset(mq, 0, bytes);
550 mq->start = &mq->data;
551 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
552 mq->next = &mq->data;
553 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
557 mq->head = gru_mesq_head(2, qlines / 2 + 1);
559 mqd->mq_gpa = uv_gpa(mq);
560 mqd->qlines = qlines;
561 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
562 mqd->interrupt_vector = vector;
563 mqd->interrupt_apicid = apicid;
566 EXPORT_SYMBOL_GPL(gru_create_message_queue);
569 * Send a NOOP message to a message queue
571 * 0 - if queue is full after the send. This is the normal case
572 * but various races can change this.
573 * -1 - if mesq sent successfully but queue not full
574 * >0 - unexpected error. MQE_xxx returned
576 static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
579 const struct message_header noop_header = {
580 .present = MQS_NOOP, .lines = 1};
583 struct message_header save_mhdr, *mhdr = mesg;
588 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
592 substatus = gru_get_cb_message_queue_substatus(cb);
595 STAT(mesq_noop_unexpected_error);
596 ret = MQE_UNEXPECTED_CB_ERR;
598 case CBSS_LB_OVERFLOWED:
599 STAT(mesq_noop_lb_overflow);
600 ret = MQE_CONGESTION;
602 case CBSS_QLIMIT_REACHED:
603 STAT(mesq_noop_qlimit_reached);
606 case CBSS_AMO_NACKED:
607 STAT(mesq_noop_amo_nacked);
608 ret = MQE_CONGESTION;
610 case CBSS_PUT_NACKED:
611 STAT(mesq_noop_put_nacked);
612 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
613 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
615 if (gru_wait(cb) == CBS_IDLE)
618 ret = MQE_UNEXPECTED_CB_ERR;
620 case CBSS_PAGE_OVERFLOW:
621 STAT(mesq_noop_page_overflow);
632 * Handle a gru_mesq full.
634 static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
635 void *mesg, int lines)
637 union gru_mesqhead mqh;
638 unsigned int limit, head;
639 unsigned long avalue;
642 /* Determine if switching to first/second half of q */
643 avalue = gru_get_amo_value(cb);
644 head = gru_get_amo_value_head(cb);
645 limit = gru_get_amo_value_limit(cb);
647 qlines = mqd->qlines;
648 half = (limit != qlines);
651 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
653 mqh = gru_mesq_head(2, qlines / 2 + 1);
655 /* Try to get lock for switching head pointer */
656 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
657 if (gru_wait(cb) != CBS_IDLE)
659 if (!gru_get_amo_value(cb)) {
660 STAT(mesq_qf_locked);
661 return MQE_QUEUE_FULL;
664 /* Got the lock. Send optional NOP if queue not full, */
666 if (send_noop_message(cb, mqd, mesg)) {
667 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
669 if (gru_wait(cb) != CBS_IDLE)
671 STAT(mesq_qf_noop_not_full);
677 /* Then flip queuehead to other half of queue. */
678 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
680 if (gru_wait(cb) != CBS_IDLE)
683 /* If not successfully in swapping queue head, clear the hstatus lock */
684 if (gru_get_amo_value(cb) != avalue) {
685 STAT(mesq_qf_switch_head_failed);
686 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
688 if (gru_wait(cb) != CBS_IDLE)
693 STAT(mesq_qf_unexpected_error);
694 return MQE_UNEXPECTED_CB_ERR;
698 * Send a cross-partition interrupt to the SSI that contains the target
699 * message queue. Normally, the interrupt is automatically delivered by hardware
700 * but some error conditions require explicit delivery.
702 static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
704 if (mqd->interrupt_vector)
705 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
706 mqd->interrupt_vector);
710 * Handle a PUT failure. Note: if message was a 2-line message, one of the
711 * lines might have successfully have been written. Before sending the
712 * message, "present" must be cleared in BOTH lines to prevent the receiver
713 * from prematurely seeing the full message.
715 static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
716 void *mesg, int lines)
720 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
722 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
723 if (gru_wait(cb) != CBS_IDLE)
724 return MQE_UNEXPECTED_CB_ERR;
726 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
727 if (gru_wait(cb) != CBS_IDLE)
728 return MQE_UNEXPECTED_CB_ERR;
729 send_message_queue_interrupt(mqd);
734 * Handle a gru_mesq failure. Some of these failures are software recoverable
737 static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
738 void *mesg, int lines)
740 int substatus, ret = 0;
742 substatus = gru_get_cb_message_queue_substatus(cb);
745 STAT(mesq_send_unexpected_error);
746 ret = MQE_UNEXPECTED_CB_ERR;
748 case CBSS_LB_OVERFLOWED:
749 STAT(mesq_send_lb_overflow);
750 ret = MQE_CONGESTION;
752 case CBSS_QLIMIT_REACHED:
753 STAT(mesq_send_qlimit_reached);
754 ret = send_message_queue_full(cb, mqd, mesg, lines);
756 case CBSS_AMO_NACKED:
757 STAT(mesq_send_amo_nacked);
758 ret = MQE_CONGESTION;
760 case CBSS_PUT_NACKED:
761 STAT(mesq_send_put_nacked);
762 ret = send_message_put_nacked(cb, mqd, mesg, lines);
764 case CBSS_PAGE_OVERFLOW:
765 STAT(mesq_page_overflow);
774 * Send a message to a message queue
775 * mqd message queue descriptor
776 * mesg message. ust be vaddr within a GSEG
777 * bytes message size (<= 2 CL)
779 int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
782 struct message_header *mhdr;
785 int istatus, clines, ret;
788 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
790 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
791 if (gru_get_cpu_resources(bytes, &cb, &dsr))
792 return MQE_BUG_NO_RESOURCES;
793 memcpy(dsr, mesg, bytes);
795 mhdr->present = MQS_FULL;
796 mhdr->lines = clines;
798 mhdr->present2 = get_present2(mhdr);
799 restore_present2(mhdr, MQS_FULL);
804 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
805 istatus = gru_wait(cb);
806 if (istatus != CBS_IDLE)
807 ret = send_message_failure(cb, mqd, dsr, clines);
808 } while (ret == MQIE_AGAIN);
809 gru_free_cpu_resources(cb, dsr);
812 STAT(mesq_send_failed);
815 EXPORT_SYMBOL_GPL(gru_send_message_gpa);
818 * Advance the receive pointer for the queue to the next message.
820 void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
822 struct message_queue *mq = mqd->mq;
823 struct message_header *mhdr = mq->next;
826 int lines = mhdr->lines;
829 restore_present2(mhdr, MQS_EMPTY);
830 mhdr->present = MQS_EMPTY;
833 next = pnext + GRU_CACHE_LINE_BYTES * lines;
834 if (next == mq->limit) {
837 } else if (pnext < mq->start2 && next >= mq->start2) {
842 mq->hstatus[half] = 1;
845 EXPORT_SYMBOL_GPL(gru_free_message);
848 * Get next message from message queue. Return NULL if no message
849 * present. User must call next_message() to move to next message.
852 void *gru_get_next_message(struct gru_message_queue_desc *mqd)
854 struct message_queue *mq = mqd->mq;
855 struct message_header *mhdr = mq->next;
856 int present = mhdr->present;
858 /* skip NOOP messages */
859 while (present == MQS_NOOP) {
860 gru_free_message(mqd, mhdr);
862 present = mhdr->present;
865 /* Wait for both halves of 2 line messages */
866 if (present == MQS_FULL && mhdr->lines == 2 &&
867 get_present2(mhdr) == MQS_EMPTY)
871 STAT(mesq_receive_none);
875 if (mhdr->lines == 2)
876 restore_present2(mhdr, mhdr->present2);
881 EXPORT_SYMBOL_GPL(gru_get_next_message);
883 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
886 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
888 int gru_read_gpa(unsigned long *value, unsigned long gpa)
895 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
896 return MQE_BUG_NO_RESOURCES;
898 gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
901 *value = *(unsigned long *)dsr;
902 gru_free_cpu_resources(cb, dsr);
905 EXPORT_SYMBOL_GPL(gru_read_gpa);
909 * Copy a block of data using the GRU resources
911 int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
919 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
920 return MQE_BUG_NO_RESOURCES;
921 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
922 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
924 gru_free_cpu_resources(cb, dsr);
927 EXPORT_SYMBOL_GPL(gru_copy_gpa);
929 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
930 /* Temp - will delete after we gain confidence in the GRU */
932 static int quicktest0(unsigned long arg)
941 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
942 return MQE_BUG_NO_RESOURCES;
947 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
948 if (gru_wait(cb) != CBS_IDLE) {
949 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
954 printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
957 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
958 if (gru_wait(cb) != CBS_IDLE) {
959 printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
963 if (word0 != word1 || word1 != MAGIC) {
965 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
966 smp_processor_id(), word1, MAGIC);
972 gru_free_cpu_resources(cb, dsr);
976 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
978 static int quicktest1(unsigned long arg)
980 struct gru_message_queue_desc mqd;
984 char mes[GRU_CACHE_LINE_BYTES], *m;
986 /* Need 1K cacheline aligned that does not cross page boundary */
987 p = kmalloc(4096, 0);
990 mq = ALIGNUP(p, 1024);
991 memset(mes, 0xee, sizeof(mes));
994 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
995 for (i = 0; i < 6; i++) {
998 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
999 } while (ret == MQE_CONGESTION);
1003 if (ret != MQE_QUEUE_FULL || i != 4) {
1004 printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
1005 smp_processor_id(), ret, i);
1009 for (i = 0; i < 6; i++) {
1010 m = gru_get_next_message(&mqd);
1011 if (!m || m[8] != i)
1013 gru_free_message(&mqd, m);
1016 printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1017 smp_processor_id(), i, m, m ? m[8] : -1);
1027 static int quicktest2(unsigned long arg)
1029 static DECLARE_COMPLETION(cmp);
1036 struct gru_control_block_status *gen;
1037 int i, k, istatus, bytes;
1039 bytes = numcb * 4 * 8;
1040 buf = kmalloc(bytes, GFP_KERNEL);
1045 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
1049 gru_lock_async_resource(han, &cb0, NULL);
1050 memset(buf, 0xee, bytes);
1051 for (i = 0; i < numcb; i++)
1052 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
1053 XTYPE_DW, 4, 1, IMA_INTERRUPT);
1058 gru_wait_async_cbr(han);
1059 for (i = 0; i < numcb; i++) {
1060 cb = cb0 + i * GRU_HANDLE_STRIDE;
1061 istatus = gru_check_status(cb);
1062 if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
1067 if (istatus != CBS_IDLE) {
1068 printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
1070 } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
1072 printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1073 smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
1078 gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
1082 gru_unlock_async_resource(han);
1083 gru_release_async_resources(han);
1090 static int quicktest3(unsigned long arg)
1092 char buf1[BUFSIZE], buf2[BUFSIZE];
1095 memset(buf2, 0, sizeof(buf2));
1096 memset(buf1, get_cycles() & 255, sizeof(buf1));
1097 gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
1098 if (memcmp(buf1, buf2, BUFSIZE)) {
1099 printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
1106 * Debugging only. User hook for various kernel tests
1109 int gru_ktest(unsigned long arg)
1113 switch (arg & 0xff) {
1115 ret = quicktest0(arg);
1118 ret = quicktest1(arg);
1121 ret = quicktest2(arg);
1124 ret = quicktest3(arg);
1127 ret = gru_free_kernel_contexts();
1134 int gru_kservices_init(void)
1139 void gru_kservices_exit(void)
1141 if (gru_free_kernel_contexts())