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)]
100 #define KCB_TO_GID(cb) ((cb - gru_start_vaddr) / \
101 (GRU_SIZE * GRU_CHIPLETS_PER_BLADE))
102 #define KCB_TO_BS(cb) gru_base[KCB_TO_GID(cb)]
104 #define GRU_NUM_KERNEL_CBR 1
105 #define GRU_NUM_KERNEL_DSR_BYTES 256
106 #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
107 GRU_CACHE_LINE_BYTES)
109 /* GRU instruction attributes for all instructions */
110 #define IMA IMA_CB_DELAY
112 /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
113 #define __gru_cacheline_aligned__ \
114 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
116 #define MAGIC 0x1234567887654321UL
118 /* Default retry count for GRU errors on kernel instructions */
119 #define EXCEPTION_RETRY_LIMIT 3
121 /* Status of message queue sections */
126 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
127 /* optimized for x86_64 */
128 struct message_queue
{
129 union gru_mesqhead head __gru_cacheline_aligned__
; /* CL 0 */
130 int qlines
; /* DW 1 */
132 void *next __gru_cacheline_aligned__
;/* CL 1 */
136 char data ____cacheline_aligned
; /* CL 2 */
139 /* First word in every message - used by mesq interface */
140 struct message_header
{
147 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
150 * Reload the blade's kernel context into a GRU chiplet. Called holding
151 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
153 static void gru_load_kernel_context(struct gru_blade_state
*bs
, int blade_id
)
155 struct gru_state
*gru
;
156 struct gru_thread_state
*kgts
;
160 up_read(&bs
->bs_kgts_sema
);
161 down_write(&bs
->bs_kgts_sema
);
164 bs
->bs_kgts
= gru_alloc_gts(NULL
, 0, 0, 0, 0);
168 STAT(load_kernel_context
);
169 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
170 kgts
->ts_cbr_au_count
= GRU_CB_COUNT_TO_AU(
171 GRU_NUM_KERNEL_CBR
* ncpus
+ bs
->bs_async_cbrs
);
172 kgts
->ts_dsr_au_count
= GRU_DS_BYTES_TO_AU(
173 GRU_NUM_KERNEL_DSR_BYTES
* ncpus
+
174 bs
->bs_async_dsr_bytes
);
175 while (!gru_assign_gru_context(kgts
, blade_id
)) {
177 gru_steal_context(kgts
, blade_id
);
179 gru_load_context(kgts
);
180 gru
= bs
->bs_kgts
->ts_gru
;
181 vaddr
= gru
->gs_gru_base_vaddr
;
182 ctxnum
= kgts
->ts_ctxnum
;
183 bs
->kernel_cb
= get_gseg_base_address_cb(vaddr
, ctxnum
, 0);
184 bs
->kernel_dsr
= get_gseg_base_address_ds(vaddr
, ctxnum
, 0);
186 downgrade_write(&bs
->bs_kgts_sema
);
190 * Free all kernel contexts that are not currently in use.
191 * Returns 0 if all freed, else number of inuse context.
193 static int gru_free_kernel_contexts(void)
195 struct gru_blade_state
*bs
;
196 struct gru_thread_state
*kgts
;
199 for (bid
= 0; bid
< GRU_MAX_BLADES
; bid
++) {
203 if (down_write_trylock(&bs
->bs_kgts_sema
)) {
205 if (kgts
&& kgts
->ts_gru
)
206 gru_unload_context(kgts
, 0);
209 up_write(&bs
->bs_kgts_sema
);
218 * Lock & load the kernel context for the specified blade.
220 static struct gru_blade_state
*gru_lock_kernel_context(int blade_id
)
222 struct gru_blade_state
*bs
;
224 STAT(lock_kernel_context
);
225 bs
= gru_base
[blade_id
];
227 down_read(&bs
->bs_kgts_sema
);
228 if (!bs
->bs_kgts
|| !bs
->bs_kgts
->ts_gru
)
229 gru_load_kernel_context(bs
, blade_id
);
235 * Unlock the kernel context for the specified blade. Context is not
236 * unloaded but may be stolen before next use.
238 static void gru_unlock_kernel_context(int blade_id
)
240 struct gru_blade_state
*bs
;
242 bs
= gru_base
[blade_id
];
243 up_read(&bs
->bs_kgts_sema
);
244 STAT(unlock_kernel_context
);
248 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
249 * - returns with preemption disabled
251 static int gru_get_cpu_resources(int dsr_bytes
, void **cb
, void **dsr
)
253 struct gru_blade_state
*bs
;
256 BUG_ON(dsr_bytes
> GRU_NUM_KERNEL_DSR_BYTES
);
258 bs
= gru_lock_kernel_context(uv_numa_blade_id());
259 lcpu
= uv_blade_processor_id();
260 *cb
= bs
->kernel_cb
+ lcpu
* GRU_HANDLE_STRIDE
;
261 *dsr
= bs
->kernel_dsr
+ lcpu
* GRU_NUM_KERNEL_DSR_BYTES
;
266 * Free the current cpus reserved DSR/CBR resources.
268 static void gru_free_cpu_resources(void *cb
, void *dsr
)
270 gru_unlock_kernel_context(uv_numa_blade_id());
275 * Reserve GRU resources to be used asynchronously.
276 * Note: currently supports only 1 reservation per blade.
279 * blade_id - blade on which resources should be reserved
280 * cbrs - number of CBRs
281 * dsr_bytes - number of DSR bytes needed
283 * handle to identify resource
284 * (0 = async resources already reserved)
286 unsigned long gru_reserve_async_resources(int blade_id
, int cbrs
, int dsr_bytes
,
287 struct completion
*cmp
)
289 struct gru_blade_state
*bs
;
290 struct gru_thread_state
*kgts
;
293 bs
= gru_base
[blade_id
];
295 down_write(&bs
->bs_kgts_sema
);
297 /* Verify no resources already reserved */
298 if (bs
->bs_async_dsr_bytes
+ bs
->bs_async_cbrs
)
300 bs
->bs_async_dsr_bytes
= dsr_bytes
;
301 bs
->bs_async_cbrs
= cbrs
;
302 bs
->bs_async_wq
= cmp
;
305 /* Resources changed. Unload context if already loaded */
306 if (kgts
&& kgts
->ts_gru
)
307 gru_unload_context(kgts
, 0);
308 ret
= ASYNC_BID_TO_HAN(blade_id
);
311 up_write(&bs
->bs_kgts_sema
);
316 * Release async resources previously reserved.
319 * han - handle to identify resources
321 void gru_release_async_resources(unsigned long han
)
323 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
325 down_write(&bs
->bs_kgts_sema
);
326 bs
->bs_async_dsr_bytes
= 0;
327 bs
->bs_async_cbrs
= 0;
328 bs
->bs_async_wq
= NULL
;
329 up_write(&bs
->bs_kgts_sema
);
333 * Wait for async GRU instructions to complete.
336 * han - handle to identify resources
338 void gru_wait_async_cbr(unsigned long han
)
340 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
342 wait_for_completion(bs
->bs_async_wq
);
347 * Lock previous reserved async GRU resources
350 * han - handle to identify resources
352 * cb - pointer to first CBR
353 * dsr - pointer to first DSR
355 void gru_lock_async_resource(unsigned long han
, void **cb
, void **dsr
)
357 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
358 int blade_id
= ASYNC_HAN_TO_BID(han
);
361 gru_lock_kernel_context(blade_id
);
362 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
364 *cb
= bs
->kernel_cb
+ ncpus
* GRU_HANDLE_STRIDE
;
366 *dsr
= bs
->kernel_dsr
+ ncpus
* GRU_NUM_KERNEL_DSR_BYTES
;
370 * Unlock previous reserved async GRU resources
373 * han - handle to identify resources
375 void gru_unlock_async_resource(unsigned long han
)
377 int blade_id
= ASYNC_HAN_TO_BID(han
);
379 gru_unlock_kernel_context(blade_id
);
382 /*----------------------------------------------------------------------*/
383 int gru_get_cb_exception_detail(void *cb
,
384 struct control_block_extended_exc_detail
*excdet
)
386 struct gru_control_block_extended
*cbe
;
387 struct gru_blade_state
*bs
;
391 cbrnum
= thread_cbr_number(bs
->bs_kgts
, get_cb_number(cb
));
392 cbe
= get_cbe(GRUBASE(cb
), cbrnum
);
393 gru_flush_cache(cbe
); /* CBE not coherent */
394 excdet
->opc
= cbe
->opccpy
;
395 excdet
->exopc
= cbe
->exopccpy
;
396 excdet
->ecause
= cbe
->ecause
;
397 excdet
->exceptdet0
= cbe
->idef1upd
;
398 excdet
->exceptdet1
= cbe
->idef3upd
;
399 gru_flush_cache(cbe
);
403 char *gru_get_cb_exception_detail_str(int ret
, void *cb
,
406 struct gru_control_block_status
*gen
= (void *)cb
;
407 struct control_block_extended_exc_detail excdet
;
409 if (ret
> 0 && gen
->istatus
== CBS_EXCEPTION
) {
410 gru_get_cb_exception_detail(cb
, &excdet
);
412 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
413 "excdet0 0x%lx, excdet1 0x%x",
414 gen
, excdet
.opc
, excdet
.exopc
, excdet
.ecause
,
415 excdet
.exceptdet0
, excdet
.exceptdet1
);
417 snprintf(buf
, size
, "No exception");
422 static int gru_wait_idle_or_exception(struct gru_control_block_status
*gen
)
424 while (gen
->istatus
>= CBS_ACTIVE
) {
431 static int gru_retry_exception(void *cb
)
433 struct gru_control_block_status
*gen
= (void *)cb
;
434 struct control_block_extended_exc_detail excdet
;
435 int retry
= EXCEPTION_RETRY_LIMIT
;
438 if (gru_wait_idle_or_exception(gen
) == CBS_IDLE
)
440 if (gru_get_cb_message_queue_substatus(cb
))
441 return CBS_EXCEPTION
;
442 gru_get_cb_exception_detail(cb
, &excdet
);
443 if ((excdet
.ecause
& ~EXCEPTION_RETRY_BITS
) ||
444 (excdet
.cbrexecstatus
& CBR_EXS_ABORT_OCC
))
449 gru_flush_cache(gen
);
451 return CBS_EXCEPTION
;
454 int gru_check_status_proc(void *cb
)
456 struct gru_control_block_status
*gen
= (void *)cb
;
460 if (ret
!= CBS_EXCEPTION
)
462 return gru_retry_exception(cb
);
466 int gru_wait_proc(void *cb
)
468 struct gru_control_block_status
*gen
= (void *)cb
;
471 ret
= gru_wait_idle_or_exception(gen
);
472 if (ret
== CBS_EXCEPTION
)
473 ret
= gru_retry_exception(cb
);
478 void gru_abort(int ret
, void *cb
, char *str
)
480 char buf
[GRU_EXC_STR_SIZE
];
482 panic("GRU FATAL ERROR: %s - %s\n", str
,
483 gru_get_cb_exception_detail_str(ret
, cb
, buf
, sizeof(buf
)));
486 void gru_wait_abort_proc(void *cb
)
490 ret
= gru_wait_proc(cb
);
492 gru_abort(ret
, cb
, "gru_wait_abort");
496 /*------------------------------ MESSAGE QUEUES -----------------------------*/
498 /* Internal status . These are NOT returned to the user. */
499 #define MQIE_AGAIN -1 /* try again */
503 * Save/restore the "present" flag that is in the second line of 2-line
506 static inline int get_present2(void *p
)
508 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
509 return mhdr
->present
;
512 static inline void restore_present2(void *p
, int val
)
514 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
519 * Create a message queue.
520 * qlines - message queue size in cache lines. Includes 2-line header.
522 int gru_create_message_queue(struct gru_message_queue_desc
*mqd
,
523 void *p
, unsigned int bytes
, int nasid
, int vector
, int apicid
)
525 struct message_queue
*mq
= p
;
528 qlines
= bytes
/ GRU_CACHE_LINE_BYTES
- 2;
529 memset(mq
, 0, bytes
);
530 mq
->start
= &mq
->data
;
531 mq
->start2
= &mq
->data
+ (qlines
/ 2 - 1) * GRU_CACHE_LINE_BYTES
;
532 mq
->next
= &mq
->data
;
533 mq
->limit
= &mq
->data
+ (qlines
- 2) * GRU_CACHE_LINE_BYTES
;
537 mq
->head
= gru_mesq_head(2, qlines
/ 2 + 1);
539 mqd
->mq_gpa
= uv_gpa(mq
);
540 mqd
->qlines
= qlines
;
541 mqd
->interrupt_pnode
= UV_NASID_TO_PNODE(nasid
);
542 mqd
->interrupt_vector
= vector
;
543 mqd
->interrupt_apicid
= apicid
;
546 EXPORT_SYMBOL_GPL(gru_create_message_queue
);
549 * Send a NOOP message to a message queue
551 * 0 - if queue is full after the send. This is the normal case
552 * but various races can change this.
553 * -1 - if mesq sent successfully but queue not full
554 * >0 - unexpected error. MQE_xxx returned
556 static int send_noop_message(void *cb
, struct gru_message_queue_desc
*mqd
,
559 const struct message_header noop_header
= {
560 .present
= MQS_NOOP
, .lines
= 1};
563 struct message_header save_mhdr
, *mhdr
= mesg
;
568 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), 1, IMA
);
572 substatus
= gru_get_cb_message_queue_substatus(cb
);
575 STAT(mesq_noop_unexpected_error
);
576 ret
= MQE_UNEXPECTED_CB_ERR
;
578 case CBSS_LB_OVERFLOWED
:
579 STAT(mesq_noop_lb_overflow
);
580 ret
= MQE_CONGESTION
;
582 case CBSS_QLIMIT_REACHED
:
583 STAT(mesq_noop_qlimit_reached
);
586 case CBSS_AMO_NACKED
:
587 STAT(mesq_noop_amo_nacked
);
588 ret
= MQE_CONGESTION
;
590 case CBSS_PUT_NACKED
:
591 STAT(mesq_noop_put_nacked
);
592 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
593 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, 1, 1,
595 if (gru_wait(cb
) == CBS_IDLE
)
598 ret
= MQE_UNEXPECTED_CB_ERR
;
600 case CBSS_PAGE_OVERFLOW
:
610 * Handle a gru_mesq full.
612 static int send_message_queue_full(void *cb
, struct gru_message_queue_desc
*mqd
,
613 void *mesg
, int lines
)
615 union gru_mesqhead mqh
;
616 unsigned int limit
, head
;
617 unsigned long avalue
;
620 /* Determine if switching to first/second half of q */
621 avalue
= gru_get_amo_value(cb
);
622 head
= gru_get_amo_value_head(cb
);
623 limit
= gru_get_amo_value_limit(cb
);
625 qlines
= mqd
->qlines
;
626 half
= (limit
!= qlines
);
629 mqh
= gru_mesq_head(qlines
/ 2 + 1, qlines
);
631 mqh
= gru_mesq_head(2, qlines
/ 2 + 1);
633 /* Try to get lock for switching head pointer */
634 gru_gamir(cb
, EOP_IR_CLR
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
, IMA
);
635 if (gru_wait(cb
) != CBS_IDLE
)
637 if (!gru_get_amo_value(cb
)) {
638 STAT(mesq_qf_locked
);
639 return MQE_QUEUE_FULL
;
642 /* Got the lock. Send optional NOP if queue not full, */
644 if (send_noop_message(cb
, mqd
, mesg
)) {
645 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
),
647 if (gru_wait(cb
) != CBS_IDLE
)
649 STAT(mesq_qf_noop_not_full
);
655 /* Then flip queuehead to other half of queue. */
656 gru_gamer(cb
, EOP_ERR_CSWAP
, mqd
->mq_gpa
, XTYPE_DW
, mqh
.val
, avalue
,
658 if (gru_wait(cb
) != CBS_IDLE
)
661 /* If not successfully in swapping queue head, clear the hstatus lock */
662 if (gru_get_amo_value(cb
) != avalue
) {
663 STAT(mesq_qf_switch_head_failed
);
664 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
,
666 if (gru_wait(cb
) != CBS_IDLE
)
671 STAT(mesq_qf_unexpected_error
);
672 return MQE_UNEXPECTED_CB_ERR
;
676 * Send a cross-partition interrupt to the SSI that contains the target
677 * message queue. Normally, the interrupt is automatically delivered by hardware
678 * but some error conditions require explicit delivery.
680 static void send_message_queue_interrupt(struct gru_message_queue_desc
*mqd
)
682 if (mqd
->interrupt_vector
)
683 uv_hub_send_ipi(mqd
->interrupt_pnode
, mqd
->interrupt_apicid
,
684 mqd
->interrupt_vector
);
688 * Handle a PUT failure. Note: if message was a 2-line message, one of the
689 * lines might have successfully have been written. Before sending the
690 * message, "present" must be cleared in BOTH lines to prevent the receiver
691 * from prematurely seeing the full message.
693 static int send_message_put_nacked(void *cb
, struct gru_message_queue_desc
*mqd
,
694 void *mesg
, int lines
)
698 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
700 gru_vset(cb
, m
, 0, XTYPE_CL
, lines
, 1, IMA
);
701 if (gru_wait(cb
) != CBS_IDLE
)
702 return MQE_UNEXPECTED_CB_ERR
;
704 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, lines
, 1, IMA
);
705 if (gru_wait(cb
) != CBS_IDLE
)
706 return MQE_UNEXPECTED_CB_ERR
;
707 send_message_queue_interrupt(mqd
);
712 * Handle a gru_mesq failure. Some of these failures are software recoverable
715 static int send_message_failure(void *cb
, struct gru_message_queue_desc
*mqd
,
716 void *mesg
, int lines
)
718 int substatus
, ret
= 0;
720 substatus
= gru_get_cb_message_queue_substatus(cb
);
723 STAT(mesq_send_unexpected_error
);
724 ret
= MQE_UNEXPECTED_CB_ERR
;
726 case CBSS_LB_OVERFLOWED
:
727 STAT(mesq_send_lb_overflow
);
728 ret
= MQE_CONGESTION
;
730 case CBSS_QLIMIT_REACHED
:
731 STAT(mesq_send_qlimit_reached
);
732 ret
= send_message_queue_full(cb
, mqd
, mesg
, lines
);
734 case CBSS_AMO_NACKED
:
735 STAT(mesq_send_amo_nacked
);
736 ret
= MQE_CONGESTION
;
738 case CBSS_PUT_NACKED
:
739 STAT(mesq_send_put_nacked
);
740 ret
= send_message_put_nacked(cb
, mqd
, mesg
, lines
);
749 * Send a message to a message queue
750 * mqd message queue descriptor
751 * mesg message. ust be vaddr within a GSEG
752 * bytes message size (<= 2 CL)
754 int gru_send_message_gpa(struct gru_message_queue_desc
*mqd
, void *mesg
,
757 struct message_header
*mhdr
;
760 int istatus
, clines
, ret
;
763 BUG_ON(bytes
< sizeof(int) || bytes
> 2 * GRU_CACHE_LINE_BYTES
);
765 clines
= DIV_ROUND_UP(bytes
, GRU_CACHE_LINE_BYTES
);
766 if (gru_get_cpu_resources(bytes
, &cb
, &dsr
))
767 return MQE_BUG_NO_RESOURCES
;
768 memcpy(dsr
, mesg
, bytes
);
770 mhdr
->present
= MQS_FULL
;
771 mhdr
->lines
= clines
;
773 mhdr
->present2
= get_present2(mhdr
);
774 restore_present2(mhdr
, MQS_FULL
);
779 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), clines
, IMA
);
780 istatus
= gru_wait(cb
);
781 if (istatus
!= CBS_IDLE
)
782 ret
= send_message_failure(cb
, mqd
, dsr
, clines
);
783 } while (ret
== MQIE_AGAIN
);
784 gru_free_cpu_resources(cb
, dsr
);
787 STAT(mesq_send_failed
);
790 EXPORT_SYMBOL_GPL(gru_send_message_gpa
);
793 * Advance the receive pointer for the queue to the next message.
795 void gru_free_message(struct gru_message_queue_desc
*mqd
, void *mesg
)
797 struct message_queue
*mq
= mqd
->mq
;
798 struct message_header
*mhdr
= mq
->next
;
801 int lines
= mhdr
->lines
;
804 restore_present2(mhdr
, MQS_EMPTY
);
805 mhdr
->present
= MQS_EMPTY
;
808 next
= pnext
+ GRU_CACHE_LINE_BYTES
* lines
;
809 if (next
== mq
->limit
) {
812 } else if (pnext
< mq
->start2
&& next
>= mq
->start2
) {
817 mq
->hstatus
[half
] = 1;
820 EXPORT_SYMBOL_GPL(gru_free_message
);
823 * Get next message from message queue. Return NULL if no message
824 * present. User must call next_message() to move to next message.
827 void *gru_get_next_message(struct gru_message_queue_desc
*mqd
)
829 struct message_queue
*mq
= mqd
->mq
;
830 struct message_header
*mhdr
= mq
->next
;
831 int present
= mhdr
->present
;
833 /* skip NOOP messages */
835 while (present
== MQS_NOOP
) {
836 gru_free_message(mqd
, mhdr
);
838 present
= mhdr
->present
;
841 /* Wait for both halves of 2 line messages */
842 if (present
== MQS_FULL
&& mhdr
->lines
== 2 &&
843 get_present2(mhdr
) == MQS_EMPTY
)
847 STAT(mesq_receive_none
);
851 if (mhdr
->lines
== 2)
852 restore_present2(mhdr
, mhdr
->present2
);
856 EXPORT_SYMBOL_GPL(gru_get_next_message
);
858 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
861 * Copy a block of data using the GRU resources
863 int gru_copy_gpa(unsigned long dest_gpa
, unsigned long src_gpa
,
871 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
872 return MQE_BUG_NO_RESOURCES
;
873 gru_bcopy(cb
, src_gpa
, dest_gpa
, gru_get_tri(dsr
),
874 XTYPE_B
, bytes
, GRU_NUM_KERNEL_DSR_CL
, IMA
);
876 gru_free_cpu_resources(cb
, dsr
);
879 EXPORT_SYMBOL_GPL(gru_copy_gpa
);
881 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
882 /* Temp - will delete after we gain confidence in the GRU */
884 static int quicktest0(unsigned long arg
)
893 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES
, &cb
, &dsr
))
894 return MQE_BUG_NO_RESOURCES
;
899 gru_vload(cb
, uv_gpa(&word0
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
900 if (gru_wait(cb
) != CBS_IDLE
) {
901 printk(KERN_DEBUG
"GRU quicktest0: CBR failure 1\n");
906 printk(KERN_DEBUG
"GRU: quicktest0 bad magic 0x%lx\n", *p
);
909 gru_vstore(cb
, uv_gpa(&word1
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
910 if (gru_wait(cb
) != CBS_IDLE
) {
911 printk(KERN_DEBUG
"GRU quicktest0: CBR failure 2\n");
915 if (word0
!= word1
|| word1
!= MAGIC
) {
917 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
924 gru_free_cpu_resources(cb
, dsr
);
928 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
930 static int quicktest1(unsigned long arg
)
932 struct gru_message_queue_desc mqd
;
936 char mes
[GRU_CACHE_LINE_BYTES
], *m
;
938 /* Need 1K cacheline aligned that does not cross page boundary */
939 p
= kmalloc(4096, 0);
940 mq
= ALIGNUP(p
, 1024);
941 memset(mes
, 0xee, sizeof(mes
));
944 gru_create_message_queue(&mqd
, mq
, 8 * GRU_CACHE_LINE_BYTES
, 0, 0, 0);
945 for (i
= 0; i
< 6; i
++) {
948 ret
= gru_send_message_gpa(&mqd
, mes
, sizeof(mes
));
949 } while (ret
== MQE_CONGESTION
);
953 if (ret
!= MQE_QUEUE_FULL
|| i
!= 4)
956 for (i
= 0; i
< 6; i
++) {
957 m
= gru_get_next_message(&mqd
);
960 gru_free_message(&mqd
, m
);
962 ret
= (i
== 4) ? 0 : -EIO
;
969 static int quicktest2(unsigned long arg
)
971 static DECLARE_COMPLETION(cmp
);
978 int i
, k
, istatus
, bytes
;
980 bytes
= numcb
* 4 * 8;
981 buf
= kmalloc(bytes
, GFP_KERNEL
);
986 han
= gru_reserve_async_resources(blade_id
, numcb
, 0, &cmp
);
990 gru_lock_async_resource(han
, &cb0
, NULL
);
991 memset(buf
, 0xee, bytes
);
992 for (i
= 0; i
< numcb
; i
++)
993 gru_vset(cb0
+ i
* GRU_HANDLE_STRIDE
, uv_gpa(&buf
[i
* 4]), 0,
994 XTYPE_DW
, 4, 1, IMA_INTERRUPT
);
997 for (k
= 0; k
< numcb
; k
++) {
998 gru_wait_async_cbr(han
);
999 for (i
= 0; i
< numcb
; i
++) {
1000 cb
= cb0
+ i
* GRU_HANDLE_STRIDE
;
1001 istatus
= gru_check_status(cb
);
1002 if (istatus
== CBS_ACTIVE
)
1004 if (istatus
== CBS_EXCEPTION
)
1006 else if (buf
[i
] || buf
[i
+ 1] || buf
[i
+ 2] ||
1013 gru_unlock_async_resource(han
);
1014 gru_release_async_resources(han
);
1021 * Debugging only. User hook for various kernel tests
1024 int gru_ktest(unsigned long arg
)
1028 switch (arg
& 0xff) {
1030 ret
= quicktest0(arg
);
1033 ret
= quicktest1(arg
);
1036 ret
= quicktest2(arg
);
1039 ret
= gru_free_kernel_contexts();
1046 int gru_kservices_init(void)
1051 void gru_kservices_exit(void)
1053 if (gru_free_kernel_contexts())