1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SN Platform GRU Driver
5 * KERNEL SERVICES THAT USE THE GRU
7 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/device.h>
16 #include <linux/miscdevice.h>
17 #include <linux/proc_fs.h>
18 #include <linux/interrupt.h>
19 #include <linux/sync_core.h>
20 #include <linux/uaccess.h>
21 #include <linux/delay.h>
22 #include <linux/export.h>
23 #include <asm/io_apic.h>
26 #include "grutables.h"
27 #include "grukservices.h"
28 #include "gru_instructions.h"
29 #include <asm/uv/uv_hub.h>
34 * The following is an interim algorithm for management of kernel GRU
35 * resources. This will likely be replaced when we better understand the
36 * kernel/user requirements.
38 * Blade percpu resources reserved for kernel use. These resources are
39 * reserved whenever the the kernel context for the blade is loaded. Note
40 * that the kernel context is not guaranteed to be always available. It is
41 * loaded on demand & can be stolen by a user if the user demand exceeds the
42 * kernel demand. The kernel can always reload the kernel context but
43 * a SLEEP may be required!!!.
47 * Each blade has one "kernel context" that owns GRU kernel resources
48 * located on the blade. Kernel drivers use GRU resources in this context
49 * for sending messages, zeroing memory, etc.
51 * The kernel context is dynamically loaded on demand. If it is not in
52 * use by the kernel, the kernel context can be unloaded & given to a user.
53 * The kernel context will be reloaded when needed. This may require that
54 * a context be stolen from a user.
55 * NOTE: frequent unloading/reloading of the kernel context is
56 * expensive. We are depending on batch schedulers, cpusets, sane
57 * drivers or some other mechanism to prevent the need for frequent
60 * The kernel context consists of two parts:
61 * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
62 * Each cpu has it's own private resources & does not share them
63 * with other cpus. These resources are used serially, ie,
64 * locked, used & unlocked on each call to a function in
66 * (Now that we have dynamic loading of kernel contexts, I
67 * may rethink this & allow sharing between cpus....)
69 * - Additional resources can be reserved long term & used directly
70 * by UV drivers located in the kernel. Drivers using these GRU
71 * resources can use asynchronous GRU instructions that send
72 * interrupts on completion.
73 * - these resources must be explicitly locked/unlocked
74 * - locked resources prevent (obviously) the kernel
75 * context from being unloaded.
76 * - drivers using these resource directly issue their own
77 * GRU instruction and must wait/check completion.
79 * When these resources are reserved, the caller can optionally
80 * associate a wait_queue with the resources and use asynchronous
81 * GRU instructions. When an async GRU instruction completes, the
82 * driver will do a wakeup on the event.
87 #define ASYNC_HAN_TO_BID(h) ((h) - 1)
88 #define ASYNC_BID_TO_HAN(b) ((b) + 1)
89 #define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
91 #define GRU_NUM_KERNEL_CBR 1
92 #define GRU_NUM_KERNEL_DSR_BYTES 256
93 #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
96 /* GRU instruction attributes for all instructions */
97 #define IMA IMA_CB_DELAY
99 /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
100 #define __gru_cacheline_aligned__ \
101 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
103 #define MAGIC 0x1234567887654321UL
105 /* Default retry count for GRU errors on kernel instructions */
106 #define EXCEPTION_RETRY_LIMIT 3
108 /* Status of message queue sections */
113 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
114 /* optimized for x86_64 */
115 struct message_queue
{
116 union gru_mesqhead head __gru_cacheline_aligned__
; /* CL 0 */
117 int qlines
; /* DW 1 */
119 void *next __gru_cacheline_aligned__
;/* CL 1 */
123 char data ____cacheline_aligned
; /* CL 2 */
126 /* First word in every message - used by mesq interface */
127 struct message_header
{
134 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
137 * Reload the blade's kernel context into a GRU chiplet. Called holding
138 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
140 static void gru_load_kernel_context(struct gru_blade_state
*bs
, int blade_id
)
142 struct gru_state
*gru
;
143 struct gru_thread_state
*kgts
;
147 up_read(&bs
->bs_kgts_sema
);
148 down_write(&bs
->bs_kgts_sema
);
152 bs
->bs_kgts
= gru_alloc_gts(NULL
, 0, 0, 0, 0, 0);
153 if (!IS_ERR(bs
->bs_kgts
))
157 bs
->bs_kgts
->ts_user_blade_id
= blade_id
;
162 STAT(load_kernel_context
);
163 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
164 kgts
->ts_cbr_au_count
= GRU_CB_COUNT_TO_AU(
165 GRU_NUM_KERNEL_CBR
* ncpus
+ bs
->bs_async_cbrs
);
166 kgts
->ts_dsr_au_count
= GRU_DS_BYTES_TO_AU(
167 GRU_NUM_KERNEL_DSR_BYTES
* ncpus
+
168 bs
->bs_async_dsr_bytes
);
169 while (!gru_assign_gru_context(kgts
)) {
171 gru_steal_context(kgts
);
173 gru_load_context(kgts
);
174 gru
= bs
->bs_kgts
->ts_gru
;
175 vaddr
= gru
->gs_gru_base_vaddr
;
176 ctxnum
= kgts
->ts_ctxnum
;
177 bs
->kernel_cb
= get_gseg_base_address_cb(vaddr
, ctxnum
, 0);
178 bs
->kernel_dsr
= get_gseg_base_address_ds(vaddr
, ctxnum
, 0);
180 downgrade_write(&bs
->bs_kgts_sema
);
184 * Free all kernel contexts that are not currently in use.
185 * Returns 0 if all freed, else number of inuse context.
187 static int gru_free_kernel_contexts(void)
189 struct gru_blade_state
*bs
;
190 struct gru_thread_state
*kgts
;
193 for (bid
= 0; bid
< GRU_MAX_BLADES
; bid
++) {
198 /* Ignore busy contexts. Don't want to block here. */
199 if (down_write_trylock(&bs
->bs_kgts_sema
)) {
201 if (kgts
&& kgts
->ts_gru
)
202 gru_unload_context(kgts
, 0);
204 up_write(&bs
->bs_kgts_sema
);
214 * Lock & load the kernel context for the specified blade.
216 static struct gru_blade_state
*gru_lock_kernel_context(int blade_id
)
218 struct gru_blade_state
*bs
;
221 STAT(lock_kernel_context
);
223 bid
= blade_id
< 0 ? uv_numa_blade_id() : blade_id
;
226 /* Handle the case where migration occurred while waiting for the sema */
227 down_read(&bs
->bs_kgts_sema
);
228 if (blade_id
< 0 && bid
!= uv_numa_blade_id()) {
229 up_read(&bs
->bs_kgts_sema
);
232 if (!bs
->bs_kgts
|| !bs
->bs_kgts
->ts_gru
)
233 gru_load_kernel_context(bs
, bid
);
239 * Unlock the kernel context for the specified blade. Context is not
240 * unloaded but may be stolen before next use.
242 static void gru_unlock_kernel_context(int blade_id
)
244 struct gru_blade_state
*bs
;
246 bs
= gru_base
[blade_id
];
247 up_read(&bs
->bs_kgts_sema
);
248 STAT(unlock_kernel_context
);
252 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
253 * - returns with preemption disabled
255 static int gru_get_cpu_resources(int dsr_bytes
, void **cb
, void **dsr
)
257 struct gru_blade_state
*bs
;
260 BUG_ON(dsr_bytes
> GRU_NUM_KERNEL_DSR_BYTES
);
262 bs
= gru_lock_kernel_context(-1);
263 lcpu
= uv_blade_processor_id();
264 *cb
= bs
->kernel_cb
+ lcpu
* GRU_HANDLE_STRIDE
;
265 *dsr
= bs
->kernel_dsr
+ lcpu
* GRU_NUM_KERNEL_DSR_BYTES
;
270 * Free the current cpus reserved DSR/CBR resources.
272 static void gru_free_cpu_resources(void *cb
, void *dsr
)
274 gru_unlock_kernel_context(uv_numa_blade_id());
279 * Reserve GRU resources to be used asynchronously.
280 * Note: currently supports only 1 reservation per blade.
283 * blade_id - blade on which resources should be reserved
284 * cbrs - number of CBRs
285 * dsr_bytes - number of DSR bytes needed
287 * handle to identify resource
288 * (0 = async resources already reserved)
290 unsigned long gru_reserve_async_resources(int blade_id
, int cbrs
, int dsr_bytes
,
291 struct completion
*cmp
)
293 struct gru_blade_state
*bs
;
294 struct gru_thread_state
*kgts
;
297 bs
= gru_base
[blade_id
];
299 down_write(&bs
->bs_kgts_sema
);
301 /* Verify no resources already reserved */
302 if (bs
->bs_async_dsr_bytes
+ bs
->bs_async_cbrs
)
304 bs
->bs_async_dsr_bytes
= dsr_bytes
;
305 bs
->bs_async_cbrs
= cbrs
;
306 bs
->bs_async_wq
= cmp
;
309 /* Resources changed. Unload context if already loaded */
310 if (kgts
&& kgts
->ts_gru
)
311 gru_unload_context(kgts
, 0);
312 ret
= ASYNC_BID_TO_HAN(blade_id
);
315 up_write(&bs
->bs_kgts_sema
);
320 * Release async resources previously reserved.
323 * han - handle to identify resources
325 void gru_release_async_resources(unsigned long han
)
327 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
329 down_write(&bs
->bs_kgts_sema
);
330 bs
->bs_async_dsr_bytes
= 0;
331 bs
->bs_async_cbrs
= 0;
332 bs
->bs_async_wq
= NULL
;
333 up_write(&bs
->bs_kgts_sema
);
337 * Wait for async GRU instructions to complete.
340 * han - handle to identify resources
342 void gru_wait_async_cbr(unsigned long han
)
344 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
346 wait_for_completion(bs
->bs_async_wq
);
351 * Lock previous reserved async GRU resources
354 * han - handle to identify resources
356 * cb - pointer to first CBR
357 * dsr - pointer to first DSR
359 void gru_lock_async_resource(unsigned long han
, void **cb
, void **dsr
)
361 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
362 int blade_id
= ASYNC_HAN_TO_BID(han
);
365 gru_lock_kernel_context(blade_id
);
366 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
368 *cb
= bs
->kernel_cb
+ ncpus
* GRU_HANDLE_STRIDE
;
370 *dsr
= bs
->kernel_dsr
+ ncpus
* GRU_NUM_KERNEL_DSR_BYTES
;
374 * Unlock previous reserved async GRU resources
377 * han - handle to identify resources
379 void gru_unlock_async_resource(unsigned long han
)
381 int blade_id
= ASYNC_HAN_TO_BID(han
);
383 gru_unlock_kernel_context(blade_id
);
386 /*----------------------------------------------------------------------*/
387 int gru_get_cb_exception_detail(void *cb
,
388 struct control_block_extended_exc_detail
*excdet
)
390 struct gru_control_block_extended
*cbe
;
391 struct gru_thread_state
*kgts
= NULL
;
396 * Locate kgts for cb. This algorithm is SLOW but
397 * this function is rarely called (ie., almost never).
398 * Performance does not matter.
400 for_each_possible_blade(bid
) {
403 kgts
= gru_base
[bid
]->bs_kgts
;
404 if (!kgts
|| !kgts
->ts_gru
)
406 off
= cb
- kgts
->ts_gru
->gs_gru_base_vaddr
;
412 cbrnum
= thread_cbr_number(kgts
, get_cb_number(cb
));
413 cbe
= get_cbe(GRUBASE(cb
), cbrnum
);
414 gru_flush_cache(cbe
); /* CBE not coherent */
416 excdet
->opc
= cbe
->opccpy
;
417 excdet
->exopc
= cbe
->exopccpy
;
418 excdet
->ecause
= cbe
->ecause
;
419 excdet
->exceptdet0
= cbe
->idef1upd
;
420 excdet
->exceptdet1
= cbe
->idef3upd
;
421 gru_flush_cache(cbe
);
425 static char *gru_get_cb_exception_detail_str(int ret
, void *cb
,
428 struct gru_control_block_status
*gen
= (void *)cb
;
429 struct control_block_extended_exc_detail excdet
;
431 if (ret
> 0 && gen
->istatus
== CBS_EXCEPTION
) {
432 gru_get_cb_exception_detail(cb
, &excdet
);
434 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
435 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
436 gen
, excdet
.opc
, excdet
.exopc
, excdet
.ecause
,
437 excdet
.exceptdet0
, excdet
.exceptdet1
);
439 snprintf(buf
, size
, "No exception");
444 static int gru_wait_idle_or_exception(struct gru_control_block_status
*gen
)
446 while (gen
->istatus
>= CBS_ACTIVE
) {
453 static int gru_retry_exception(void *cb
)
455 struct gru_control_block_status
*gen
= (void *)cb
;
456 struct control_block_extended_exc_detail excdet
;
457 int retry
= EXCEPTION_RETRY_LIMIT
;
460 if (gru_wait_idle_or_exception(gen
) == CBS_IDLE
)
462 if (gru_get_cb_message_queue_substatus(cb
))
463 return CBS_EXCEPTION
;
464 gru_get_cb_exception_detail(cb
, &excdet
);
465 if ((excdet
.ecause
& ~EXCEPTION_RETRY_BITS
) ||
466 (excdet
.cbrexecstatus
& CBR_EXS_ABORT_OCC
))
471 gru_flush_cache(gen
);
473 return CBS_EXCEPTION
;
476 int gru_check_status_proc(void *cb
)
478 struct gru_control_block_status
*gen
= (void *)cb
;
482 if (ret
== CBS_EXCEPTION
)
483 ret
= gru_retry_exception(cb
);
489 int gru_wait_proc(void *cb
)
491 struct gru_control_block_status
*gen
= (void *)cb
;
494 ret
= gru_wait_idle_or_exception(gen
);
495 if (ret
== CBS_EXCEPTION
)
496 ret
= gru_retry_exception(cb
);
501 static void gru_abort(int ret
, void *cb
, char *str
)
503 char buf
[GRU_EXC_STR_SIZE
];
505 panic("GRU FATAL ERROR: %s - %s\n", str
,
506 gru_get_cb_exception_detail_str(ret
, cb
, buf
, sizeof(buf
)));
509 void gru_wait_abort_proc(void *cb
)
513 ret
= gru_wait_proc(cb
);
515 gru_abort(ret
, cb
, "gru_wait_abort");
519 /*------------------------------ MESSAGE QUEUES -----------------------------*/
521 /* Internal status . These are NOT returned to the user. */
522 #define MQIE_AGAIN -1 /* try again */
526 * Save/restore the "present" flag that is in the second line of 2-line
529 static inline int get_present2(void *p
)
531 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
532 return mhdr
->present
;
535 static inline void restore_present2(void *p
, int val
)
537 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
542 * Create a message queue.
543 * qlines - message queue size in cache lines. Includes 2-line header.
545 int gru_create_message_queue(struct gru_message_queue_desc
*mqd
,
546 void *p
, unsigned int bytes
, int nasid
, int vector
, int apicid
)
548 struct message_queue
*mq
= p
;
551 qlines
= bytes
/ GRU_CACHE_LINE_BYTES
- 2;
552 memset(mq
, 0, bytes
);
553 mq
->start
= &mq
->data
;
554 mq
->start2
= &mq
->data
+ (qlines
/ 2 - 1) * GRU_CACHE_LINE_BYTES
;
555 mq
->next
= &mq
->data
;
556 mq
->limit
= &mq
->data
+ (qlines
- 2) * GRU_CACHE_LINE_BYTES
;
560 mq
->head
= gru_mesq_head(2, qlines
/ 2 + 1);
562 mqd
->mq_gpa
= uv_gpa(mq
);
563 mqd
->qlines
= qlines
;
564 mqd
->interrupt_pnode
= nasid
>> 1;
565 mqd
->interrupt_vector
= vector
;
566 mqd
->interrupt_apicid
= apicid
;
569 EXPORT_SYMBOL_GPL(gru_create_message_queue
);
572 * Send a NOOP message to a message queue
574 * 0 - if queue is full after the send. This is the normal case
575 * but various races can change this.
576 * -1 - if mesq sent successfully but queue not full
577 * >0 - unexpected error. MQE_xxx returned
579 static int send_noop_message(void *cb
, struct gru_message_queue_desc
*mqd
,
582 const struct message_header noop_header
= {
583 .present
= MQS_NOOP
, .lines
= 1};
586 struct message_header save_mhdr
, *mhdr
= mesg
;
591 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), 1, IMA
);
595 substatus
= gru_get_cb_message_queue_substatus(cb
);
598 STAT(mesq_noop_unexpected_error
);
599 ret
= MQE_UNEXPECTED_CB_ERR
;
601 case CBSS_LB_OVERFLOWED
:
602 STAT(mesq_noop_lb_overflow
);
603 ret
= MQE_CONGESTION
;
605 case CBSS_QLIMIT_REACHED
:
606 STAT(mesq_noop_qlimit_reached
);
609 case CBSS_AMO_NACKED
:
610 STAT(mesq_noop_amo_nacked
);
611 ret
= MQE_CONGESTION
;
613 case CBSS_PUT_NACKED
:
614 STAT(mesq_noop_put_nacked
);
615 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
616 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, 1, 1,
618 if (gru_wait(cb
) == CBS_IDLE
)
621 ret
= MQE_UNEXPECTED_CB_ERR
;
623 case CBSS_PAGE_OVERFLOW
:
624 STAT(mesq_noop_page_overflow
);
635 * Handle a gru_mesq full.
637 static int send_message_queue_full(void *cb
, struct gru_message_queue_desc
*mqd
,
638 void *mesg
, int lines
)
640 union gru_mesqhead mqh
;
641 unsigned int limit
, head
;
642 unsigned long avalue
;
645 /* Determine if switching to first/second half of q */
646 avalue
= gru_get_amo_value(cb
);
647 head
= gru_get_amo_value_head(cb
);
648 limit
= gru_get_amo_value_limit(cb
);
650 qlines
= mqd
->qlines
;
651 half
= (limit
!= qlines
);
654 mqh
= gru_mesq_head(qlines
/ 2 + 1, qlines
);
656 mqh
= gru_mesq_head(2, qlines
/ 2 + 1);
658 /* Try to get lock for switching head pointer */
659 gru_gamir(cb
, EOP_IR_CLR
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
, IMA
);
660 if (gru_wait(cb
) != CBS_IDLE
)
662 if (!gru_get_amo_value(cb
)) {
663 STAT(mesq_qf_locked
);
664 return MQE_QUEUE_FULL
;
667 /* Got the lock. Send optional NOP if queue not full, */
669 if (send_noop_message(cb
, mqd
, mesg
)) {
670 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
),
672 if (gru_wait(cb
) != CBS_IDLE
)
674 STAT(mesq_qf_noop_not_full
);
680 /* Then flip queuehead to other half of queue. */
681 gru_gamer(cb
, EOP_ERR_CSWAP
, mqd
->mq_gpa
, XTYPE_DW
, mqh
.val
, avalue
,
683 if (gru_wait(cb
) != CBS_IDLE
)
686 /* If not successfully in swapping queue head, clear the hstatus lock */
687 if (gru_get_amo_value(cb
) != avalue
) {
688 STAT(mesq_qf_switch_head_failed
);
689 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
,
691 if (gru_wait(cb
) != CBS_IDLE
)
696 STAT(mesq_qf_unexpected_error
);
697 return MQE_UNEXPECTED_CB_ERR
;
701 * Handle a PUT failure. Note: if message was a 2-line message, one of the
702 * lines might have successfully have been written. Before sending the
703 * message, "present" must be cleared in BOTH lines to prevent the receiver
704 * from prematurely seeing the full message.
706 static int send_message_put_nacked(void *cb
, struct gru_message_queue_desc
*mqd
,
707 void *mesg
, int lines
)
710 int ret
, loops
= 200; /* experimentally determined */
712 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
714 gru_vset(cb
, m
, 0, XTYPE_CL
, lines
, 1, IMA
);
715 if (gru_wait(cb
) != CBS_IDLE
)
716 return MQE_UNEXPECTED_CB_ERR
;
718 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, lines
, 1, IMA
);
719 if (gru_wait(cb
) != CBS_IDLE
)
720 return MQE_UNEXPECTED_CB_ERR
;
722 if (!mqd
->interrupt_vector
)
726 * Send a noop message in order to deliver a cross-partition interrupt
727 * to the SSI that contains the target message queue. Normally, the
728 * interrupt is automatically delivered by hardware following mesq
729 * operations, but some error conditions require explicit delivery.
730 * The noop message will trigger delivery. Otherwise partition failures
731 * could cause unrecovered errors.
734 ret
= send_noop_message(cb
, mqd
, mesg
);
735 } while ((ret
== MQIE_AGAIN
|| ret
== MQE_CONGESTION
) && (loops
-- > 0));
737 if (ret
== MQIE_AGAIN
|| ret
== MQE_CONGESTION
) {
739 * Don't indicate to the app to resend the message, as it's
740 * already been successfully sent. We simply send an OK
741 * (rather than fail the send with MQE_UNEXPECTED_CB_ERR),
742 * assuming that the other side is receiving enough
743 * interrupts to get this message processed anyway.
751 * Handle a gru_mesq failure. Some of these failures are software recoverable
754 static int send_message_failure(void *cb
, struct gru_message_queue_desc
*mqd
,
755 void *mesg
, int lines
)
757 int substatus
, ret
= 0;
759 substatus
= gru_get_cb_message_queue_substatus(cb
);
762 STAT(mesq_send_unexpected_error
);
763 ret
= MQE_UNEXPECTED_CB_ERR
;
765 case CBSS_LB_OVERFLOWED
:
766 STAT(mesq_send_lb_overflow
);
767 ret
= MQE_CONGESTION
;
769 case CBSS_QLIMIT_REACHED
:
770 STAT(mesq_send_qlimit_reached
);
771 ret
= send_message_queue_full(cb
, mqd
, mesg
, lines
);
773 case CBSS_AMO_NACKED
:
774 STAT(mesq_send_amo_nacked
);
775 ret
= MQE_CONGESTION
;
777 case CBSS_PUT_NACKED
:
778 STAT(mesq_send_put_nacked
);
779 ret
= send_message_put_nacked(cb
, mqd
, mesg
, lines
);
781 case CBSS_PAGE_OVERFLOW
:
782 STAT(mesq_page_overflow
);
791 * Send a message to a message queue
792 * mqd message queue descriptor
793 * mesg message. ust be vaddr within a GSEG
794 * bytes message size (<= 2 CL)
796 int gru_send_message_gpa(struct gru_message_queue_desc
*mqd
, void *mesg
,
799 struct message_header
*mhdr
;
802 int istatus
, clines
, ret
;
805 BUG_ON(bytes
< sizeof(int) || bytes
> 2 * GRU_CACHE_LINE_BYTES
);
807 clines
= DIV_ROUND_UP(bytes
, GRU_CACHE_LINE_BYTES
);
808 if (gru_get_cpu_resources(bytes
, &cb
, &dsr
))
809 return MQE_BUG_NO_RESOURCES
;
810 memcpy(dsr
, mesg
, bytes
);
812 mhdr
->present
= MQS_FULL
;
813 mhdr
->lines
= clines
;
815 mhdr
->present2
= get_present2(mhdr
);
816 restore_present2(mhdr
, MQS_FULL
);
821 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), clines
, IMA
);
822 istatus
= gru_wait(cb
);
823 if (istatus
!= CBS_IDLE
)
824 ret
= send_message_failure(cb
, mqd
, dsr
, clines
);
825 } while (ret
== MQIE_AGAIN
);
826 gru_free_cpu_resources(cb
, dsr
);
829 STAT(mesq_send_failed
);
832 EXPORT_SYMBOL_GPL(gru_send_message_gpa
);
835 * Advance the receive pointer for the queue to the next message.
837 void gru_free_message(struct gru_message_queue_desc
*mqd
, void *mesg
)
839 struct message_queue
*mq
= mqd
->mq
;
840 struct message_header
*mhdr
= mq
->next
;
843 int lines
= mhdr
->lines
;
846 restore_present2(mhdr
, MQS_EMPTY
);
847 mhdr
->present
= MQS_EMPTY
;
850 next
= pnext
+ GRU_CACHE_LINE_BYTES
* lines
;
851 if (next
== mq
->limit
) {
854 } else if (pnext
< mq
->start2
&& next
>= mq
->start2
) {
859 mq
->hstatus
[half
] = 1;
862 EXPORT_SYMBOL_GPL(gru_free_message
);
865 * Get next message from message queue. Return NULL if no message
866 * present. User must call next_message() to move to next message.
869 void *gru_get_next_message(struct gru_message_queue_desc
*mqd
)
871 struct message_queue
*mq
= mqd
->mq
;
872 struct message_header
*mhdr
= mq
->next
;
873 int present
= mhdr
->present
;
875 /* skip NOOP messages */
876 while (present
== MQS_NOOP
) {
877 gru_free_message(mqd
, mhdr
);
879 present
= mhdr
->present
;
882 /* Wait for both halves of 2 line messages */
883 if (present
== MQS_FULL
&& mhdr
->lines
== 2 &&
884 get_present2(mhdr
) == MQS_EMPTY
)
888 STAT(mesq_receive_none
);
892 if (mhdr
->lines
== 2)
893 restore_present2(mhdr
, mhdr
->present2
);
898 EXPORT_SYMBOL_GPL(gru_get_next_message
);
900 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
903 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
905 int gru_read_gpa(unsigned long *value
, unsigned long gpa
)
912 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
913 return MQE_BUG_NO_RESOURCES
;
915 gru_vload_phys(cb
, gpa
, gru_get_tri(dsr
), iaa
, IMA
);
918 *value
= *(unsigned long *)dsr
;
919 gru_free_cpu_resources(cb
, dsr
);
922 EXPORT_SYMBOL_GPL(gru_read_gpa
);
926 * Copy a block of data using the GRU resources
928 int gru_copy_gpa(unsigned long dest_gpa
, unsigned long src_gpa
,
936 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
937 return MQE_BUG_NO_RESOURCES
;
938 gru_bcopy(cb
, src_gpa
, dest_gpa
, gru_get_tri(dsr
),
939 XTYPE_B
, bytes
, GRU_NUM_KERNEL_DSR_CL
, IMA
);
941 gru_free_cpu_resources(cb
, dsr
);
944 EXPORT_SYMBOL_GPL(gru_copy_gpa
);
946 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
947 /* Temp - will delete after we gain confidence in the GRU */
949 static int quicktest0(unsigned long arg
)
958 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES
, &cb
, &dsr
))
959 return MQE_BUG_NO_RESOURCES
;
964 gru_vload(cb
, uv_gpa(&word0
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
965 if (gru_wait(cb
) != CBS_IDLE
) {
966 printk(KERN_DEBUG
"GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
971 printk(KERN_DEBUG
"GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p
);
974 gru_vstore(cb
, uv_gpa(&word1
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
975 if (gru_wait(cb
) != CBS_IDLE
) {
976 printk(KERN_DEBUG
"GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
980 if (word0
!= word1
|| word1
!= MAGIC
) {
982 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
983 smp_processor_id(), word1
, MAGIC
);
989 gru_free_cpu_resources(cb
, dsr
);
993 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
995 static int quicktest1(unsigned long arg
)
997 struct gru_message_queue_desc mqd
;
1000 char mes
[GRU_CACHE_LINE_BYTES
], *m
;
1002 /* Need 1K cacheline aligned that does not cross page boundary */
1003 p
= kmalloc(4096, 0);
1006 mq
= ALIGNUP(p
, 1024);
1007 memset(mes
, 0xee, sizeof(mes
));
1009 gru_create_message_queue(&mqd
, mq
, 8 * GRU_CACHE_LINE_BYTES
, 0, 0, 0);
1010 for (i
= 0; i
< 6; i
++) {
1013 ret
= gru_send_message_gpa(&mqd
, mes
, sizeof(mes
));
1014 } while (ret
== MQE_CONGESTION
);
1018 if (ret
!= MQE_QUEUE_FULL
|| i
!= 4) {
1019 printk(KERN_DEBUG
"GRU:%d quicktest1: unexpect status %d, i %d\n",
1020 smp_processor_id(), ret
, i
);
1024 for (i
= 0; i
< 6; i
++) {
1025 m
= gru_get_next_message(&mqd
);
1026 if (!m
|| m
[8] != i
)
1028 gru_free_message(&mqd
, m
);
1031 printk(KERN_DEBUG
"GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1032 smp_processor_id(), i
, m
, m
? m
[8] : -1);
1042 static int quicktest2(unsigned long arg
)
1044 static DECLARE_COMPLETION(cmp
);
1051 struct gru_control_block_status
*gen
;
1052 int i
, k
, istatus
, bytes
;
1054 bytes
= numcb
* 4 * 8;
1055 buf
= kmalloc(bytes
, GFP_KERNEL
);
1060 han
= gru_reserve_async_resources(blade_id
, numcb
, 0, &cmp
);
1064 gru_lock_async_resource(han
, &cb0
, NULL
);
1065 memset(buf
, 0xee, bytes
);
1066 for (i
= 0; i
< numcb
; i
++)
1067 gru_vset(cb0
+ i
* GRU_HANDLE_STRIDE
, uv_gpa(&buf
[i
* 4]), 0,
1068 XTYPE_DW
, 4, 1, IMA_INTERRUPT
);
1073 gru_wait_async_cbr(han
);
1074 for (i
= 0; i
< numcb
; i
++) {
1075 cb
= cb0
+ i
* GRU_HANDLE_STRIDE
;
1076 istatus
= gru_check_status(cb
);
1077 if (istatus
!= CBS_ACTIVE
&& istatus
!= CBS_CALL_OS
)
1082 if (istatus
!= CBS_IDLE
) {
1083 printk(KERN_DEBUG
"GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i
);
1085 } else if (buf
[4 * i
] || buf
[4 * i
+ 1] || buf
[4 * i
+ 2] ||
1087 printk(KERN_DEBUG
"GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1088 smp_processor_id(), i
, buf
[4 * i
], buf
[4 * i
+ 1], buf
[4 * i
+ 2], buf
[4 * i
+ 3]);
1093 gen
->istatus
= CBS_CALL_OS
; /* don't handle this CBR again */
1097 gru_unlock_async_resource(han
);
1098 gru_release_async_resources(han
);
1105 static int quicktest3(unsigned long arg
)
1107 char buf1
[BUFSIZE
], buf2
[BUFSIZE
];
1110 memset(buf2
, 0, sizeof(buf2
));
1111 memset(buf1
, get_cycles() & 255, sizeof(buf1
));
1112 gru_copy_gpa(uv_gpa(buf2
), uv_gpa(buf1
), BUFSIZE
);
1113 if (memcmp(buf1
, buf2
, BUFSIZE
)) {
1114 printk(KERN_DEBUG
"GRU:%d quicktest3 error\n", smp_processor_id());
1121 * Debugging only. User hook for various kernel tests
1124 int gru_ktest(unsigned long arg
)
1128 switch (arg
& 0xff) {
1130 ret
= quicktest0(arg
);
1133 ret
= quicktest1(arg
);
1136 ret
= quicktest2(arg
);
1139 ret
= quicktest3(arg
);
1142 ret
= gru_free_kernel_contexts();
1149 int gru_kservices_init(void)
1154 void gru_kservices_exit(void)
1156 if (gru_free_kernel_contexts())