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/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>
37 #include "grutables.h"
38 #include "grukservices.h"
39 #include "gru_instructions.h"
40 #include <asm/uv/uv_hub.h>
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!!!.
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
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
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)]
101 #define KCB_TO_GID(cb) ((cb - gru_start_vaddr) / \
102 (GRU_SIZE * GRU_CHIPLETS_PER_BLADE))
103 #define KCB_TO_BS(cb) gru_base[KCB_TO_GID(cb)]
105 #define GRU_NUM_KERNEL_CBR 1
106 #define GRU_NUM_KERNEL_DSR_BYTES 256
107 #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
108 GRU_CACHE_LINE_BYTES)
110 /* GRU instruction attributes for all instructions */
111 #define IMA IMA_CB_DELAY
113 /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
114 #define __gru_cacheline_aligned__ \
115 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
117 #define MAGIC 0x1234567887654321UL
119 /* Default retry count for GRU errors on kernel instructions */
120 #define EXCEPTION_RETRY_LIMIT 3
122 /* Status of message queue sections */
127 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
128 /* optimized for x86_64 */
129 struct message_queue
{
130 union gru_mesqhead head __gru_cacheline_aligned__
; /* CL 0 */
131 int qlines
; /* DW 1 */
133 void *next __gru_cacheline_aligned__
;/* CL 1 */
137 char data ____cacheline_aligned
; /* CL 2 */
140 /* First word in every message - used by mesq interface */
141 struct message_header
{
148 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
151 * Reload the blade's kernel context into a GRU chiplet. Called holding
152 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
154 static void gru_load_kernel_context(struct gru_blade_state
*bs
, int blade_id
)
156 struct gru_state
*gru
;
157 struct gru_thread_state
*kgts
;
161 up_read(&bs
->bs_kgts_sema
);
162 down_write(&bs
->bs_kgts_sema
);
165 bs
->bs_kgts
= gru_alloc_gts(NULL
, 0, 0, 0, 0);
169 STAT(load_kernel_context
);
170 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
171 kgts
->ts_cbr_au_count
= GRU_CB_COUNT_TO_AU(
172 GRU_NUM_KERNEL_CBR
* ncpus
+ bs
->bs_async_cbrs
);
173 kgts
->ts_dsr_au_count
= GRU_DS_BYTES_TO_AU(
174 GRU_NUM_KERNEL_DSR_BYTES
* ncpus
+
175 bs
->bs_async_dsr_bytes
);
176 while (!gru_assign_gru_context(kgts
, blade_id
)) {
178 gru_steal_context(kgts
, blade_id
);
180 gru_load_context(kgts
);
181 gru
= bs
->bs_kgts
->ts_gru
;
182 vaddr
= gru
->gs_gru_base_vaddr
;
183 ctxnum
= kgts
->ts_ctxnum
;
184 bs
->kernel_cb
= get_gseg_base_address_cb(vaddr
, ctxnum
, 0);
185 bs
->kernel_dsr
= get_gseg_base_address_ds(vaddr
, ctxnum
, 0);
187 downgrade_write(&bs
->bs_kgts_sema
);
191 * Lock & load the kernel context for the specified blade.
193 static struct gru_blade_state
*gru_lock_kernel_context(int blade_id
)
195 struct gru_blade_state
*bs
;
197 STAT(lock_kernel_context
);
198 bs
= gru_base
[blade_id
];
200 down_read(&bs
->bs_kgts_sema
);
201 if (!bs
->bs_kgts
|| !bs
->bs_kgts
->ts_gru
)
202 gru_load_kernel_context(bs
, blade_id
);
208 * Unlock the kernel context for the specified blade. Context is not
209 * unloaded but may be stolen before next use.
211 static void gru_unlock_kernel_context(int blade_id
)
213 struct gru_blade_state
*bs
;
215 bs
= gru_base
[blade_id
];
216 up_read(&bs
->bs_kgts_sema
);
217 STAT(unlock_kernel_context
);
221 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
222 * - returns with preemption disabled
224 static int gru_get_cpu_resources(int dsr_bytes
, void **cb
, void **dsr
)
226 struct gru_blade_state
*bs
;
229 BUG_ON(dsr_bytes
> GRU_NUM_KERNEL_DSR_BYTES
);
231 bs
= gru_lock_kernel_context(uv_numa_blade_id());
232 lcpu
= uv_blade_processor_id();
233 *cb
= bs
->kernel_cb
+ lcpu
* GRU_HANDLE_STRIDE
;
234 *dsr
= bs
->kernel_dsr
+ lcpu
* GRU_NUM_KERNEL_DSR_BYTES
;
239 * Free the current cpus reserved DSR/CBR resources.
241 static void gru_free_cpu_resources(void *cb
, void *dsr
)
243 gru_unlock_kernel_context(uv_numa_blade_id());
248 * Reserve GRU resources to be used asynchronously.
249 * Note: currently supports only 1 reservation per blade.
252 * blade_id - blade on which resources should be reserved
253 * cbrs - number of CBRs
254 * dsr_bytes - number of DSR bytes needed
256 * handle to identify resource
257 * (0 = async resources already reserved)
259 unsigned long gru_reserve_async_resources(int blade_id
, int cbrs
, int dsr_bytes
,
260 struct completion
*cmp
)
262 struct gru_blade_state
*bs
;
263 struct gru_thread_state
*kgts
;
266 bs
= gru_base
[blade_id
];
268 down_write(&bs
->bs_kgts_sema
);
270 /* Verify no resources already reserved */
271 if (bs
->bs_async_dsr_bytes
+ bs
->bs_async_cbrs
)
273 bs
->bs_async_dsr_bytes
= dsr_bytes
;
274 bs
->bs_async_cbrs
= cbrs
;
275 bs
->bs_async_wq
= cmp
;
278 /* Resources changed. Unload context if already loaded */
279 if (kgts
&& kgts
->ts_gru
)
280 gru_unload_context(kgts
, 0);
281 ret
= ASYNC_BID_TO_HAN(blade_id
);
284 up_write(&bs
->bs_kgts_sema
);
289 * Release async resources previously reserved.
292 * han - handle to identify resources
294 void gru_release_async_resources(unsigned long han
)
296 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
298 down_write(&bs
->bs_kgts_sema
);
299 bs
->bs_async_dsr_bytes
= 0;
300 bs
->bs_async_cbrs
= 0;
301 bs
->bs_async_wq
= NULL
;
302 up_write(&bs
->bs_kgts_sema
);
306 * Wait for async GRU instructions to complete.
309 * han - handle to identify resources
311 void gru_wait_async_cbr(unsigned long han
)
313 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
315 wait_for_completion(bs
->bs_async_wq
);
320 * Lock previous reserved async GRU resources
323 * han - handle to identify resources
325 * cb - pointer to first CBR
326 * dsr - pointer to first DSR
328 void gru_lock_async_resource(unsigned long han
, void **cb
, void **dsr
)
330 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
331 int blade_id
= ASYNC_HAN_TO_BID(han
);
334 gru_lock_kernel_context(blade_id
);
335 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
337 *cb
= bs
->kernel_cb
+ ncpus
* GRU_HANDLE_STRIDE
;
339 *dsr
= bs
->kernel_dsr
+ ncpus
* GRU_NUM_KERNEL_DSR_BYTES
;
343 * Unlock previous reserved async GRU resources
346 * han - handle to identify resources
348 void gru_unlock_async_resource(unsigned long han
)
350 int blade_id
= ASYNC_HAN_TO_BID(han
);
352 gru_unlock_kernel_context(blade_id
);
355 /*----------------------------------------------------------------------*/
356 int gru_get_cb_exception_detail(void *cb
,
357 struct control_block_extended_exc_detail
*excdet
)
359 struct gru_control_block_extended
*cbe
;
360 struct gru_blade_state
*bs
;
364 cbrnum
= thread_cbr_number(bs
->bs_kgts
, get_cb_number(cb
));
365 cbe
= get_cbe(GRUBASE(cb
), cbrnum
);
366 gru_flush_cache(cbe
); /* CBE not coherent */
367 excdet
->opc
= cbe
->opccpy
;
368 excdet
->exopc
= cbe
->exopccpy
;
369 excdet
->ecause
= cbe
->ecause
;
370 excdet
->exceptdet0
= cbe
->idef1upd
;
371 excdet
->exceptdet1
= cbe
->idef3upd
;
372 gru_flush_cache(cbe
);
376 char *gru_get_cb_exception_detail_str(int ret
, void *cb
,
379 struct gru_control_block_status
*gen
= (void *)cb
;
380 struct control_block_extended_exc_detail excdet
;
382 if (ret
> 0 && gen
->istatus
== CBS_EXCEPTION
) {
383 gru_get_cb_exception_detail(cb
, &excdet
);
385 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
386 "excdet0 0x%lx, excdet1 0x%x",
387 gen
, excdet
.opc
, excdet
.exopc
, excdet
.ecause
,
388 excdet
.exceptdet0
, excdet
.exceptdet1
);
390 snprintf(buf
, size
, "No exception");
395 static int gru_wait_idle_or_exception(struct gru_control_block_status
*gen
)
397 while (gen
->istatus
>= CBS_ACTIVE
) {
404 static int gru_retry_exception(void *cb
)
406 struct gru_control_block_status
*gen
= (void *)cb
;
407 struct control_block_extended_exc_detail excdet
;
408 int retry
= EXCEPTION_RETRY_LIMIT
;
411 if (gru_get_cb_message_queue_substatus(cb
))
413 if (gru_wait_idle_or_exception(gen
) == CBS_IDLE
)
416 gru_get_cb_exception_detail(cb
, &excdet
);
417 if ((excdet
.ecause
& ~EXCEPTION_RETRY_BITS
) ||
418 (excdet
.cbrexecstatus
& CBR_EXS_ABORT_OCC
))
423 gru_flush_cache(gen
);
425 return CBS_EXCEPTION
;
428 int gru_check_status_proc(void *cb
)
430 struct gru_control_block_status
*gen
= (void *)cb
;
434 if (ret
!= CBS_EXCEPTION
)
436 return gru_retry_exception(cb
);
440 int gru_wait_proc(void *cb
)
442 struct gru_control_block_status
*gen
= (void *)cb
;
445 ret
= gru_wait_idle_or_exception(gen
);
446 if (ret
== CBS_EXCEPTION
)
447 ret
= gru_retry_exception(cb
);
452 void gru_abort(int ret
, void *cb
, char *str
)
454 char buf
[GRU_EXC_STR_SIZE
];
456 panic("GRU FATAL ERROR: %s - %s\n", str
,
457 gru_get_cb_exception_detail_str(ret
, cb
, buf
, sizeof(buf
)));
460 void gru_wait_abort_proc(void *cb
)
464 ret
= gru_wait_proc(cb
);
466 gru_abort(ret
, cb
, "gru_wait_abort");
470 /*------------------------------ MESSAGE QUEUES -----------------------------*/
472 /* Internal status . These are NOT returned to the user. */
473 #define MQIE_AGAIN -1 /* try again */
477 * Save/restore the "present" flag that is in the second line of 2-line
480 static inline int get_present2(void *p
)
482 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
483 return mhdr
->present
;
486 static inline void restore_present2(void *p
, int val
)
488 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
493 * Create a message queue.
494 * qlines - message queue size in cache lines. Includes 2-line header.
496 int gru_create_message_queue(struct gru_message_queue_desc
*mqd
,
497 void *p
, unsigned int bytes
, int nasid
, int vector
, int apicid
)
499 struct message_queue
*mq
= p
;
502 qlines
= bytes
/ GRU_CACHE_LINE_BYTES
- 2;
503 memset(mq
, 0, bytes
);
504 mq
->start
= &mq
->data
;
505 mq
->start2
= &mq
->data
+ (qlines
/ 2 - 1) * GRU_CACHE_LINE_BYTES
;
506 mq
->next
= &mq
->data
;
507 mq
->limit
= &mq
->data
+ (qlines
- 2) * GRU_CACHE_LINE_BYTES
;
511 mq
->head
= gru_mesq_head(2, qlines
/ 2 + 1);
513 mqd
->mq_gpa
= uv_gpa(mq
);
514 mqd
->qlines
= qlines
;
515 mqd
->interrupt_pnode
= UV_NASID_TO_PNODE(nasid
);
516 mqd
->interrupt_vector
= vector
;
517 mqd
->interrupt_apicid
= apicid
;
520 EXPORT_SYMBOL_GPL(gru_create_message_queue
);
523 * Send a NOOP message to a message queue
525 * 0 - if queue is full after the send. This is the normal case
526 * but various races can change this.
527 * -1 - if mesq sent successfully but queue not full
528 * >0 - unexpected error. MQE_xxx returned
530 static int send_noop_message(void *cb
, struct gru_message_queue_desc
*mqd
,
533 const struct message_header noop_header
= {
534 .present
= MQS_NOOP
, .lines
= 1};
537 struct message_header save_mhdr
, *mhdr
= mesg
;
542 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), 1, IMA
);
546 substatus
= gru_get_cb_message_queue_substatus(cb
);
549 STAT(mesq_noop_unexpected_error
);
550 ret
= MQE_UNEXPECTED_CB_ERR
;
552 case CBSS_LB_OVERFLOWED
:
553 STAT(mesq_noop_lb_overflow
);
554 ret
= MQE_CONGESTION
;
556 case CBSS_QLIMIT_REACHED
:
557 STAT(mesq_noop_qlimit_reached
);
560 case CBSS_AMO_NACKED
:
561 STAT(mesq_noop_amo_nacked
);
562 ret
= MQE_CONGESTION
;
564 case CBSS_PUT_NACKED
:
565 STAT(mesq_noop_put_nacked
);
566 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
567 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, 1, 1,
569 if (gru_wait(cb
) == CBS_IDLE
)
572 ret
= MQE_UNEXPECTED_CB_ERR
;
574 case CBSS_PAGE_OVERFLOW
:
584 * Handle a gru_mesq full.
586 static int send_message_queue_full(void *cb
, struct gru_message_queue_desc
*mqd
,
587 void *mesg
, int lines
)
589 union gru_mesqhead mqh
;
590 unsigned int limit
, head
;
591 unsigned long avalue
;
594 /* Determine if switching to first/second half of q */
595 avalue
= gru_get_amo_value(cb
);
596 head
= gru_get_amo_value_head(cb
);
597 limit
= gru_get_amo_value_limit(cb
);
599 qlines
= mqd
->qlines
;
600 half
= (limit
!= qlines
);
603 mqh
= gru_mesq_head(qlines
/ 2 + 1, qlines
);
605 mqh
= gru_mesq_head(2, qlines
/ 2 + 1);
607 /* Try to get lock for switching head pointer */
608 gru_gamir(cb
, EOP_IR_CLR
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
, IMA
);
609 if (gru_wait(cb
) != CBS_IDLE
)
611 if (!gru_get_amo_value(cb
)) {
612 STAT(mesq_qf_locked
);
613 return MQE_QUEUE_FULL
;
616 /* Got the lock. Send optional NOP if queue not full, */
618 if (send_noop_message(cb
, mqd
, mesg
)) {
619 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
),
621 if (gru_wait(cb
) != CBS_IDLE
)
623 STAT(mesq_qf_noop_not_full
);
629 /* Then flip queuehead to other half of queue. */
630 gru_gamer(cb
, EOP_ERR_CSWAP
, mqd
->mq_gpa
, XTYPE_DW
, mqh
.val
, avalue
,
632 if (gru_wait(cb
) != CBS_IDLE
)
635 /* If not successfully in swapping queue head, clear the hstatus lock */
636 if (gru_get_amo_value(cb
) != avalue
) {
637 STAT(mesq_qf_switch_head_failed
);
638 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
,
640 if (gru_wait(cb
) != CBS_IDLE
)
645 STAT(mesq_qf_unexpected_error
);
646 return MQE_UNEXPECTED_CB_ERR
;
650 * Send a cross-partition interrupt to the SSI that contains the target
651 * message queue. Normally, the interrupt is automatically delivered by hardware
652 * but some error conditions require explicit delivery.
654 static void send_message_queue_interrupt(struct gru_message_queue_desc
*mqd
)
656 if (mqd
->interrupt_vector
)
657 uv_hub_send_ipi(mqd
->interrupt_pnode
, mqd
->interrupt_apicid
,
658 mqd
->interrupt_vector
);
662 * Handle a PUT failure. Note: if message was a 2-line message, one of the
663 * lines might have successfully have been written. Before sending the
664 * message, "present" must be cleared in BOTH lines to prevent the receiver
665 * from prematurely seeing the full message.
667 static int send_message_put_nacked(void *cb
, struct gru_message_queue_desc
*mqd
,
668 void *mesg
, int lines
)
672 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
674 gru_vset(cb
, m
, 0, XTYPE_CL
, lines
, 1, IMA
);
675 if (gru_wait(cb
) != CBS_IDLE
)
676 return MQE_UNEXPECTED_CB_ERR
;
678 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, lines
, 1, IMA
);
679 if (gru_wait(cb
) != CBS_IDLE
)
680 return MQE_UNEXPECTED_CB_ERR
;
681 send_message_queue_interrupt(mqd
);
686 * Handle a gru_mesq failure. Some of these failures are software recoverable
689 static int send_message_failure(void *cb
, struct gru_message_queue_desc
*mqd
,
690 void *mesg
, int lines
)
692 int substatus
, ret
= 0;
694 substatus
= gru_get_cb_message_queue_substatus(cb
);
697 STAT(mesq_send_unexpected_error
);
698 ret
= MQE_UNEXPECTED_CB_ERR
;
700 case CBSS_LB_OVERFLOWED
:
701 STAT(mesq_send_lb_overflow
);
702 ret
= MQE_CONGESTION
;
704 case CBSS_QLIMIT_REACHED
:
705 STAT(mesq_send_qlimit_reached
);
706 ret
= send_message_queue_full(cb
, mqd
, mesg
, lines
);
708 case CBSS_AMO_NACKED
:
709 STAT(mesq_send_amo_nacked
);
710 ret
= MQE_CONGESTION
;
712 case CBSS_PUT_NACKED
:
713 STAT(mesq_send_put_nacked
);
714 ret
= send_message_put_nacked(cb
, mqd
, mesg
, lines
);
723 * Send a message to a message queue
724 * mqd message queue descriptor
725 * mesg message. ust be vaddr within a GSEG
726 * bytes message size (<= 2 CL)
728 int gru_send_message_gpa(struct gru_message_queue_desc
*mqd
, void *mesg
,
731 struct message_header
*mhdr
;
734 int istatus
, clines
, ret
;
737 BUG_ON(bytes
< sizeof(int) || bytes
> 2 * GRU_CACHE_LINE_BYTES
);
739 clines
= DIV_ROUND_UP(bytes
, GRU_CACHE_LINE_BYTES
);
740 if (gru_get_cpu_resources(bytes
, &cb
, &dsr
))
741 return MQE_BUG_NO_RESOURCES
;
742 memcpy(dsr
, mesg
, bytes
);
744 mhdr
->present
= MQS_FULL
;
745 mhdr
->lines
= clines
;
747 mhdr
->present2
= get_present2(mhdr
);
748 restore_present2(mhdr
, MQS_FULL
);
753 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), clines
, IMA
);
754 istatus
= gru_wait(cb
);
755 if (istatus
!= CBS_IDLE
)
756 ret
= send_message_failure(cb
, mqd
, dsr
, clines
);
757 } while (ret
== MQIE_AGAIN
);
758 gru_free_cpu_resources(cb
, dsr
);
761 STAT(mesq_send_failed
);
764 EXPORT_SYMBOL_GPL(gru_send_message_gpa
);
767 * Advance the receive pointer for the queue to the next message.
769 void gru_free_message(struct gru_message_queue_desc
*mqd
, void *mesg
)
771 struct message_queue
*mq
= mqd
->mq
;
772 struct message_header
*mhdr
= mq
->next
;
775 int lines
= mhdr
->lines
;
778 restore_present2(mhdr
, MQS_EMPTY
);
779 mhdr
->present
= MQS_EMPTY
;
782 next
= pnext
+ GRU_CACHE_LINE_BYTES
* lines
;
783 if (next
== mq
->limit
) {
786 } else if (pnext
< mq
->start2
&& next
>= mq
->start2
) {
791 mq
->hstatus
[half
] = 1;
794 EXPORT_SYMBOL_GPL(gru_free_message
);
797 * Get next message from message queue. Return NULL if no message
798 * present. User must call next_message() to move to next message.
801 void *gru_get_next_message(struct gru_message_queue_desc
*mqd
)
803 struct message_queue
*mq
= mqd
->mq
;
804 struct message_header
*mhdr
= mq
->next
;
805 int present
= mhdr
->present
;
807 /* skip NOOP messages */
809 while (present
== MQS_NOOP
) {
810 gru_free_message(mqd
, mhdr
);
812 present
= mhdr
->present
;
815 /* Wait for both halves of 2 line messages */
816 if (present
== MQS_FULL
&& mhdr
->lines
== 2 &&
817 get_present2(mhdr
) == MQS_EMPTY
)
821 STAT(mesq_receive_none
);
825 if (mhdr
->lines
== 2)
826 restore_present2(mhdr
, mhdr
->present2
);
830 EXPORT_SYMBOL_GPL(gru_get_next_message
);
832 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
835 * Copy a block of data using the GRU resources
837 int gru_copy_gpa(unsigned long dest_gpa
, unsigned long src_gpa
,
845 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
846 return MQE_BUG_NO_RESOURCES
;
847 gru_bcopy(cb
, src_gpa
, dest_gpa
, gru_get_tri(dsr
),
848 XTYPE_B
, bytes
, GRU_NUM_KERNEL_DSR_CL
, IMA
);
850 gru_free_cpu_resources(cb
, dsr
);
853 EXPORT_SYMBOL_GPL(gru_copy_gpa
);
855 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
856 /* Temp - will delete after we gain confidence in the GRU */
858 static int quicktest0(unsigned long arg
)
867 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES
, &cb
, &dsr
))
868 return MQE_BUG_NO_RESOURCES
;
873 gru_vload(cb
, uv_gpa(&word0
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
874 if (gru_wait(cb
) != CBS_IDLE
) {
875 printk(KERN_DEBUG
"GRU quicktest0: CBR failure 1\n");
880 printk(KERN_DEBUG
"GRU: quicktest0 bad magic 0x%lx\n", *p
);
883 gru_vstore(cb
, uv_gpa(&word1
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
884 if (gru_wait(cb
) != CBS_IDLE
) {
885 printk(KERN_DEBUG
"GRU quicktest0: CBR failure 2\n");
889 if (word0
!= word1
|| word1
!= MAGIC
) {
891 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
898 gru_free_cpu_resources(cb
, dsr
);
902 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
904 static int quicktest1(unsigned long arg
)
906 struct gru_message_queue_desc mqd
;
910 char mes
[GRU_CACHE_LINE_BYTES
], *m
;
912 /* Need 1K cacheline aligned that does not cross page boundary */
913 p
= kmalloc(4096, 0);
914 mq
= ALIGNUP(p
, 1024);
915 memset(mes
, 0xee, sizeof(mes
));
918 gru_create_message_queue(&mqd
, mq
, 8 * GRU_CACHE_LINE_BYTES
, 0, 0, 0);
919 for (i
= 0; i
< 6; i
++) {
922 ret
= gru_send_message_gpa(&mqd
, mes
, sizeof(mes
));
923 } while (ret
== MQE_CONGESTION
);
927 if (ret
!= MQE_QUEUE_FULL
|| i
!= 4)
930 for (i
= 0; i
< 6; i
++) {
931 m
= gru_get_next_message(&mqd
);
934 gru_free_message(&mqd
, m
);
936 ret
= (i
== 4) ? 0 : -EIO
;
943 static int quicktest2(unsigned long arg
)
945 static DECLARE_COMPLETION(cmp
);
952 int i
, k
, istatus
, bytes
;
954 bytes
= numcb
* 4 * 8;
955 buf
= kmalloc(bytes
, GFP_KERNEL
);
960 han
= gru_reserve_async_resources(blade_id
, numcb
, 0, &cmp
);
964 gru_lock_async_resource(han
, &cb0
, NULL
);
965 memset(buf
, 0xee, bytes
);
966 for (i
= 0; i
< numcb
; i
++)
967 gru_vset(cb0
+ i
* GRU_HANDLE_STRIDE
, uv_gpa(&buf
[i
* 4]), 0,
968 XTYPE_DW
, 4, 1, IMA_INTERRUPT
);
971 for (k
= 0; k
< numcb
; k
++) {
972 gru_wait_async_cbr(han
);
973 for (i
= 0; i
< numcb
; i
++) {
974 cb
= cb0
+ i
* GRU_HANDLE_STRIDE
;
975 istatus
= gru_check_status(cb
);
976 if (istatus
== CBS_ACTIVE
)
978 if (istatus
== CBS_EXCEPTION
)
980 else if (buf
[i
] || buf
[i
+ 1] || buf
[i
+ 2] ||
987 gru_unlock_async_resource(han
);
988 gru_release_async_resources(han
);
995 * Debugging only. User hook for various kernel tests
998 int gru_ktest(unsigned long arg
)
1002 switch (arg
& 0xff) {
1004 ret
= quicktest0(arg
);
1007 ret
= quicktest1(arg
);
1010 ret
= quicktest2(arg
);
1017 int gru_kservices_init(struct gru_state
*gru
)
1019 struct gru_blade_state
*bs
;
1022 if (gru
!= &bs
->bs_grus
[0])
1025 init_rwsem(&bs
->bs_kgts_sema
);
1029 void gru_kservices_exit(struct gru_state
*gru
)
1031 struct gru_blade_state
*bs
;
1032 struct gru_thread_state
*kgts
;
1035 if (gru
!= &bs
->bs_grus
[0])
1039 if (kgts
&& kgts
->ts_gru
)
1040 gru_unload_context(kgts
, 0);