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 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
);
261 bs
= gru_lock_kernel_context(-1);
262 lcpu
= uv_blade_processor_id();
263 *cb
= bs
->kernel_cb
+ lcpu
* GRU_HANDLE_STRIDE
;
264 *dsr
= bs
->kernel_dsr
+ lcpu
* GRU_NUM_KERNEL_DSR_BYTES
;
269 * Free the current cpus reserved DSR/CBR resources.
271 static void gru_free_cpu_resources(void *cb
, void *dsr
)
273 gru_unlock_kernel_context(uv_numa_blade_id());
277 * Reserve GRU resources to be used asynchronously.
278 * Note: currently supports only 1 reservation per blade.
281 * blade_id - blade on which resources should be reserved
282 * cbrs - number of CBRs
283 * dsr_bytes - number of DSR bytes needed
285 * handle to identify resource
286 * (0 = async resources already reserved)
288 unsigned long gru_reserve_async_resources(int blade_id
, int cbrs
, int dsr_bytes
,
289 struct completion
*cmp
)
291 struct gru_blade_state
*bs
;
292 struct gru_thread_state
*kgts
;
295 bs
= gru_base
[blade_id
];
297 down_write(&bs
->bs_kgts_sema
);
299 /* Verify no resources already reserved */
300 if (bs
->bs_async_dsr_bytes
+ bs
->bs_async_cbrs
)
302 bs
->bs_async_dsr_bytes
= dsr_bytes
;
303 bs
->bs_async_cbrs
= cbrs
;
304 bs
->bs_async_wq
= cmp
;
307 /* Resources changed. Unload context if already loaded */
308 if (kgts
&& kgts
->ts_gru
)
309 gru_unload_context(kgts
, 0);
310 ret
= ASYNC_BID_TO_HAN(blade_id
);
313 up_write(&bs
->bs_kgts_sema
);
318 * Release async resources previously reserved.
321 * han - handle to identify resources
323 void gru_release_async_resources(unsigned long han
)
325 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
327 down_write(&bs
->bs_kgts_sema
);
328 bs
->bs_async_dsr_bytes
= 0;
329 bs
->bs_async_cbrs
= 0;
330 bs
->bs_async_wq
= NULL
;
331 up_write(&bs
->bs_kgts_sema
);
335 * Wait for async GRU instructions to complete.
338 * han - handle to identify resources
340 void gru_wait_async_cbr(unsigned long han
)
342 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
344 wait_for_completion(bs
->bs_async_wq
);
349 * Lock previous reserved async GRU resources
352 * han - handle to identify resources
354 * cb - pointer to first CBR
355 * dsr - pointer to first DSR
357 void gru_lock_async_resource(unsigned long han
, void **cb
, void **dsr
)
359 struct gru_blade_state
*bs
= ASYNC_HAN_TO_BS(han
);
360 int blade_id
= ASYNC_HAN_TO_BID(han
);
363 gru_lock_kernel_context(blade_id
);
364 ncpus
= uv_blade_nr_possible_cpus(blade_id
);
366 *cb
= bs
->kernel_cb
+ ncpus
* GRU_HANDLE_STRIDE
;
368 *dsr
= bs
->kernel_dsr
+ ncpus
* GRU_NUM_KERNEL_DSR_BYTES
;
372 * Unlock previous reserved async GRU resources
375 * han - handle to identify resources
377 void gru_unlock_async_resource(unsigned long han
)
379 int blade_id
= ASYNC_HAN_TO_BID(han
);
381 gru_unlock_kernel_context(blade_id
);
384 /*----------------------------------------------------------------------*/
385 int gru_get_cb_exception_detail(void *cb
,
386 struct control_block_extended_exc_detail
*excdet
)
388 struct gru_control_block_extended
*cbe
;
389 struct gru_thread_state
*kgts
= NULL
;
394 * Locate kgts for cb. This algorithm is SLOW but
395 * this function is rarely called (ie., almost never).
396 * Performance does not matter.
398 for_each_possible_blade(bid
) {
401 kgts
= gru_base
[bid
]->bs_kgts
;
402 if (!kgts
|| !kgts
->ts_gru
)
404 off
= cb
- kgts
->ts_gru
->gs_gru_base_vaddr
;
410 cbrnum
= thread_cbr_number(kgts
, get_cb_number(cb
));
411 cbe
= get_cbe(GRUBASE(cb
), cbrnum
);
412 gru_flush_cache(cbe
); /* CBE not coherent */
414 excdet
->opc
= cbe
->opccpy
;
415 excdet
->exopc
= cbe
->exopccpy
;
416 excdet
->ecause
= cbe
->ecause
;
417 excdet
->exceptdet0
= cbe
->idef1upd
;
418 excdet
->exceptdet1
= cbe
->idef3upd
;
419 gru_flush_cache(cbe
);
423 static char *gru_get_cb_exception_detail_str(int ret
, void *cb
,
426 struct gru_control_block_status
*gen
= cb
;
427 struct control_block_extended_exc_detail excdet
;
429 if (ret
> 0 && gen
->istatus
== CBS_EXCEPTION
) {
430 gru_get_cb_exception_detail(cb
, &excdet
);
432 "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
433 "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
434 gen
, excdet
.opc
, excdet
.exopc
, excdet
.ecause
,
435 excdet
.exceptdet0
, excdet
.exceptdet1
);
437 snprintf(buf
, size
, "No exception");
442 static int gru_wait_idle_or_exception(struct gru_control_block_status
*gen
)
444 while (gen
->istatus
>= CBS_ACTIVE
) {
451 static int gru_retry_exception(void *cb
)
453 struct gru_control_block_status
*gen
= cb
;
454 struct control_block_extended_exc_detail excdet
;
455 int retry
= EXCEPTION_RETRY_LIMIT
;
458 if (gru_wait_idle_or_exception(gen
) == CBS_IDLE
)
460 if (gru_get_cb_message_queue_substatus(cb
))
461 return CBS_EXCEPTION
;
462 gru_get_cb_exception_detail(cb
, &excdet
);
463 if ((excdet
.ecause
& ~EXCEPTION_RETRY_BITS
) ||
464 (excdet
.cbrexecstatus
& CBR_EXS_ABORT_OCC
))
469 gru_flush_cache(gen
);
471 return CBS_EXCEPTION
;
474 int gru_check_status_proc(void *cb
)
476 struct gru_control_block_status
*gen
= cb
;
480 if (ret
== CBS_EXCEPTION
)
481 ret
= gru_retry_exception(cb
);
487 int gru_wait_proc(void *cb
)
489 struct gru_control_block_status
*gen
= cb
;
492 ret
= gru_wait_idle_or_exception(gen
);
493 if (ret
== CBS_EXCEPTION
)
494 ret
= gru_retry_exception(cb
);
499 static void gru_abort(int ret
, void *cb
, char *str
)
501 char buf
[GRU_EXC_STR_SIZE
];
503 panic("GRU FATAL ERROR: %s - %s\n", str
,
504 gru_get_cb_exception_detail_str(ret
, cb
, buf
, sizeof(buf
)));
507 void gru_wait_abort_proc(void *cb
)
511 ret
= gru_wait_proc(cb
);
513 gru_abort(ret
, cb
, "gru_wait_abort");
517 /*------------------------------ MESSAGE QUEUES -----------------------------*/
519 /* Internal status . These are NOT returned to the user. */
520 #define MQIE_AGAIN -1 /* try again */
524 * Save/restore the "present" flag that is in the second line of 2-line
527 static inline int get_present2(void *p
)
529 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
530 return mhdr
->present
;
533 static inline void restore_present2(void *p
, int val
)
535 struct message_header
*mhdr
= p
+ GRU_CACHE_LINE_BYTES
;
540 * Create a message queue.
541 * qlines - message queue size in cache lines. Includes 2-line header.
543 int gru_create_message_queue(struct gru_message_queue_desc
*mqd
,
544 void *p
, unsigned int bytes
, int nasid
, int vector
, int apicid
)
546 struct message_queue
*mq
= p
;
549 qlines
= bytes
/ GRU_CACHE_LINE_BYTES
- 2;
550 memset(mq
, 0, bytes
);
551 mq
->start
= &mq
->data
;
552 mq
->start2
= &mq
->data
+ (qlines
/ 2 - 1) * GRU_CACHE_LINE_BYTES
;
553 mq
->next
= &mq
->data
;
554 mq
->limit
= &mq
->data
+ (qlines
- 2) * GRU_CACHE_LINE_BYTES
;
558 mq
->head
= gru_mesq_head(2, qlines
/ 2 + 1);
560 mqd
->mq_gpa
= uv_gpa(mq
);
561 mqd
->qlines
= qlines
;
562 mqd
->interrupt_pnode
= nasid
>> 1;
563 mqd
->interrupt_vector
= vector
;
564 mqd
->interrupt_apicid
= apicid
;
567 EXPORT_SYMBOL_GPL(gru_create_message_queue
);
570 * Send a NOOP message to a message queue
572 * 0 - if queue is full after the send. This is the normal case
573 * but various races can change this.
574 * -1 - if mesq sent successfully but queue not full
575 * >0 - unexpected error. MQE_xxx returned
577 static int send_noop_message(void *cb
, struct gru_message_queue_desc
*mqd
,
580 const struct message_header noop_header
= {
581 .present
= MQS_NOOP
, .lines
= 1};
584 struct message_header save_mhdr
, *mhdr
= mesg
;
589 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), 1, IMA
);
593 substatus
= gru_get_cb_message_queue_substatus(cb
);
596 STAT(mesq_noop_unexpected_error
);
597 ret
= MQE_UNEXPECTED_CB_ERR
;
599 case CBSS_LB_OVERFLOWED
:
600 STAT(mesq_noop_lb_overflow
);
601 ret
= MQE_CONGESTION
;
603 case CBSS_QLIMIT_REACHED
:
604 STAT(mesq_noop_qlimit_reached
);
607 case CBSS_AMO_NACKED
:
608 STAT(mesq_noop_amo_nacked
);
609 ret
= MQE_CONGESTION
;
611 case CBSS_PUT_NACKED
:
612 STAT(mesq_noop_put_nacked
);
613 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
614 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, 1, 1,
616 if (gru_wait(cb
) == CBS_IDLE
)
619 ret
= MQE_UNEXPECTED_CB_ERR
;
621 case CBSS_PAGE_OVERFLOW
:
622 STAT(mesq_noop_page_overflow
);
633 * Handle a gru_mesq full.
635 static int send_message_queue_full(void *cb
, struct gru_message_queue_desc
*mqd
,
636 void *mesg
, int lines
)
638 union gru_mesqhead mqh
;
639 unsigned int limit
, head
;
640 unsigned long avalue
;
643 /* Determine if switching to first/second half of q */
644 avalue
= gru_get_amo_value(cb
);
645 head
= gru_get_amo_value_head(cb
);
646 limit
= gru_get_amo_value_limit(cb
);
648 qlines
= mqd
->qlines
;
649 half
= (limit
!= qlines
);
652 mqh
= gru_mesq_head(qlines
/ 2 + 1, qlines
);
654 mqh
= gru_mesq_head(2, qlines
/ 2 + 1);
656 /* Try to get lock for switching head pointer */
657 gru_gamir(cb
, EOP_IR_CLR
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
, IMA
);
658 if (gru_wait(cb
) != CBS_IDLE
)
660 if (!gru_get_amo_value(cb
)) {
661 STAT(mesq_qf_locked
);
662 return MQE_QUEUE_FULL
;
665 /* Got the lock. Send optional NOP if queue not full, */
667 if (send_noop_message(cb
, mqd
, mesg
)) {
668 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
),
670 if (gru_wait(cb
) != CBS_IDLE
)
672 STAT(mesq_qf_noop_not_full
);
678 /* Then flip queuehead to other half of queue. */
679 gru_gamer(cb
, EOP_ERR_CSWAP
, mqd
->mq_gpa
, XTYPE_DW
, mqh
.val
, avalue
,
681 if (gru_wait(cb
) != CBS_IDLE
)
684 /* If not successfully in swapping queue head, clear the hstatus lock */
685 if (gru_get_amo_value(cb
) != avalue
) {
686 STAT(mesq_qf_switch_head_failed
);
687 gru_gamir(cb
, EOP_IR_INC
, HSTATUS(mqd
->mq_gpa
, half
), XTYPE_DW
,
689 if (gru_wait(cb
) != CBS_IDLE
)
694 STAT(mesq_qf_unexpected_error
);
695 return MQE_UNEXPECTED_CB_ERR
;
699 * Handle a PUT failure. Note: if message was a 2-line message, one of the
700 * lines might have successfully have been written. Before sending the
701 * message, "present" must be cleared in BOTH lines to prevent the receiver
702 * from prematurely seeing the full message.
704 static int send_message_put_nacked(void *cb
, struct gru_message_queue_desc
*mqd
,
705 void *mesg
, int lines
)
708 int ret
, loops
= 200; /* experimentally determined */
710 m
= mqd
->mq_gpa
+ (gru_get_amo_value_head(cb
) << 6);
712 gru_vset(cb
, m
, 0, XTYPE_CL
, lines
, 1, IMA
);
713 if (gru_wait(cb
) != CBS_IDLE
)
714 return MQE_UNEXPECTED_CB_ERR
;
716 gru_vstore(cb
, m
, gru_get_tri(mesg
), XTYPE_CL
, lines
, 1, IMA
);
717 if (gru_wait(cb
) != CBS_IDLE
)
718 return MQE_UNEXPECTED_CB_ERR
;
720 if (!mqd
->interrupt_vector
)
724 * Send a noop message in order to deliver a cross-partition interrupt
725 * to the SSI that contains the target message queue. Normally, the
726 * interrupt is automatically delivered by hardware following mesq
727 * operations, but some error conditions require explicit delivery.
728 * The noop message will trigger delivery. Otherwise partition failures
729 * could cause unrecovered errors.
732 ret
= send_noop_message(cb
, mqd
, mesg
);
733 } while ((ret
== MQIE_AGAIN
|| ret
== MQE_CONGESTION
) && (loops
-- > 0));
735 if (ret
== MQIE_AGAIN
|| ret
== MQE_CONGESTION
) {
737 * Don't indicate to the app to resend the message, as it's
738 * already been successfully sent. We simply send an OK
739 * (rather than fail the send with MQE_UNEXPECTED_CB_ERR),
740 * assuming that the other side is receiving enough
741 * interrupts to get this message processed anyway.
749 * Handle a gru_mesq failure. Some of these failures are software recoverable
752 static int send_message_failure(void *cb
, struct gru_message_queue_desc
*mqd
,
753 void *mesg
, int lines
)
755 int substatus
, ret
= 0;
757 substatus
= gru_get_cb_message_queue_substatus(cb
);
760 STAT(mesq_send_unexpected_error
);
761 ret
= MQE_UNEXPECTED_CB_ERR
;
763 case CBSS_LB_OVERFLOWED
:
764 STAT(mesq_send_lb_overflow
);
765 ret
= MQE_CONGESTION
;
767 case CBSS_QLIMIT_REACHED
:
768 STAT(mesq_send_qlimit_reached
);
769 ret
= send_message_queue_full(cb
, mqd
, mesg
, lines
);
771 case CBSS_AMO_NACKED
:
772 STAT(mesq_send_amo_nacked
);
773 ret
= MQE_CONGESTION
;
775 case CBSS_PUT_NACKED
:
776 STAT(mesq_send_put_nacked
);
777 ret
= send_message_put_nacked(cb
, mqd
, mesg
, lines
);
779 case CBSS_PAGE_OVERFLOW
:
780 STAT(mesq_page_overflow
);
789 * Send a message to a message queue
790 * mqd message queue descriptor
791 * mesg message. ust be vaddr within a GSEG
792 * bytes message size (<= 2 CL)
794 int gru_send_message_gpa(struct gru_message_queue_desc
*mqd
, void *mesg
,
797 struct message_header
*mhdr
;
800 int istatus
, clines
, ret
;
803 BUG_ON(bytes
< sizeof(int) || bytes
> 2 * GRU_CACHE_LINE_BYTES
);
805 clines
= DIV_ROUND_UP(bytes
, GRU_CACHE_LINE_BYTES
);
806 if (gru_get_cpu_resources(bytes
, &cb
, &dsr
))
807 return MQE_BUG_NO_RESOURCES
;
808 memcpy(dsr
, mesg
, bytes
);
810 mhdr
->present
= MQS_FULL
;
811 mhdr
->lines
= clines
;
813 mhdr
->present2
= get_present2(mhdr
);
814 restore_present2(mhdr
, MQS_FULL
);
819 gru_mesq(cb
, mqd
->mq_gpa
, gru_get_tri(mhdr
), clines
, IMA
);
820 istatus
= gru_wait(cb
);
821 if (istatus
!= CBS_IDLE
)
822 ret
= send_message_failure(cb
, mqd
, dsr
, clines
);
823 } while (ret
== MQIE_AGAIN
);
824 gru_free_cpu_resources(cb
, dsr
);
827 STAT(mesq_send_failed
);
830 EXPORT_SYMBOL_GPL(gru_send_message_gpa
);
833 * Advance the receive pointer for the queue to the next message.
835 void gru_free_message(struct gru_message_queue_desc
*mqd
, void *mesg
)
837 struct message_queue
*mq
= mqd
->mq
;
838 struct message_header
*mhdr
= mq
->next
;
841 int lines
= mhdr
->lines
;
844 restore_present2(mhdr
, MQS_EMPTY
);
845 mhdr
->present
= MQS_EMPTY
;
848 next
= pnext
+ GRU_CACHE_LINE_BYTES
* lines
;
849 if (next
== mq
->limit
) {
852 } else if (pnext
< mq
->start2
&& next
>= mq
->start2
) {
857 mq
->hstatus
[half
] = 1;
860 EXPORT_SYMBOL_GPL(gru_free_message
);
863 * Get next message from message queue. Return NULL if no message
864 * present. User must call next_message() to move to next message.
867 void *gru_get_next_message(struct gru_message_queue_desc
*mqd
)
869 struct message_queue
*mq
= mqd
->mq
;
870 struct message_header
*mhdr
= mq
->next
;
871 int present
= mhdr
->present
;
873 /* skip NOOP messages */
874 while (present
== MQS_NOOP
) {
875 gru_free_message(mqd
, mhdr
);
877 present
= mhdr
->present
;
880 /* Wait for both halves of 2 line messages */
881 if (present
== MQS_FULL
&& mhdr
->lines
== 2 &&
882 get_present2(mhdr
) == MQS_EMPTY
)
886 STAT(mesq_receive_none
);
890 if (mhdr
->lines
== 2)
891 restore_present2(mhdr
, mhdr
->present2
);
896 EXPORT_SYMBOL_GPL(gru_get_next_message
);
898 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
901 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
903 int gru_read_gpa(unsigned long *value
, unsigned long gpa
)
910 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
911 return MQE_BUG_NO_RESOURCES
;
913 gru_vload_phys(cb
, gpa
, gru_get_tri(dsr
), iaa
, IMA
);
916 *value
= *(unsigned long *)dsr
;
917 gru_free_cpu_resources(cb
, dsr
);
920 EXPORT_SYMBOL_GPL(gru_read_gpa
);
924 * Copy a block of data using the GRU resources
926 int gru_copy_gpa(unsigned long dest_gpa
, unsigned long src_gpa
,
934 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES
, &cb
, &dsr
))
935 return MQE_BUG_NO_RESOURCES
;
936 gru_bcopy(cb
, src_gpa
, dest_gpa
, gru_get_tri(dsr
),
937 XTYPE_B
, bytes
, GRU_NUM_KERNEL_DSR_CL
, IMA
);
939 gru_free_cpu_resources(cb
, dsr
);
942 EXPORT_SYMBOL_GPL(gru_copy_gpa
);
944 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
945 /* Temp - will delete after we gain confidence in the GRU */
947 static int quicktest0(unsigned long arg
)
956 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES
, &cb
, &dsr
))
957 return MQE_BUG_NO_RESOURCES
;
962 gru_vload(cb
, uv_gpa(&word0
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
963 if (gru_wait(cb
) != CBS_IDLE
) {
964 printk(KERN_DEBUG
"GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
969 printk(KERN_DEBUG
"GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p
);
972 gru_vstore(cb
, uv_gpa(&word1
), gru_get_tri(dsr
), XTYPE_DW
, 1, 1, IMA
);
973 if (gru_wait(cb
) != CBS_IDLE
) {
974 printk(KERN_DEBUG
"GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
978 if (word0
!= word1
|| word1
!= MAGIC
) {
980 "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
981 smp_processor_id(), word1
, MAGIC
);
987 gru_free_cpu_resources(cb
, dsr
);
991 #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
993 static int quicktest1(unsigned long arg
)
995 struct gru_message_queue_desc mqd
;
998 char mes
[GRU_CACHE_LINE_BYTES
], *m
;
1000 /* Need 1K cacheline aligned that does not cross page boundary */
1001 p
= kmalloc(4096, 0);
1004 mq
= ALIGNUP(p
, 1024);
1005 memset(mes
, 0xee, sizeof(mes
));
1007 gru_create_message_queue(&mqd
, mq
, 8 * GRU_CACHE_LINE_BYTES
, 0, 0, 0);
1008 for (i
= 0; i
< 6; i
++) {
1011 ret
= gru_send_message_gpa(&mqd
, mes
, sizeof(mes
));
1012 } while (ret
== MQE_CONGESTION
);
1016 if (ret
!= MQE_QUEUE_FULL
|| i
!= 4) {
1017 printk(KERN_DEBUG
"GRU:%d quicktest1: unexpected status %d, i %d\n",
1018 smp_processor_id(), ret
, i
);
1022 for (i
= 0; i
< 6; i
++) {
1023 m
= gru_get_next_message(&mqd
);
1024 if (!m
|| m
[8] != i
)
1026 gru_free_message(&mqd
, m
);
1029 printk(KERN_DEBUG
"GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
1030 smp_processor_id(), i
, m
, m
? m
[8] : -1);
1040 static int quicktest2(unsigned long arg
)
1042 static DECLARE_COMPLETION(cmp
);
1049 struct gru_control_block_status
*gen
;
1050 int i
, k
, istatus
, bytes
;
1052 bytes
= numcb
* 4 * 8;
1053 buf
= kmalloc(bytes
, GFP_KERNEL
);
1058 han
= gru_reserve_async_resources(blade_id
, numcb
, 0, &cmp
);
1062 gru_lock_async_resource(han
, &cb0
, NULL
);
1063 memset(buf
, 0xee, bytes
);
1064 for (i
= 0; i
< numcb
; i
++)
1065 gru_vset(cb0
+ i
* GRU_HANDLE_STRIDE
, uv_gpa(&buf
[i
* 4]), 0,
1066 XTYPE_DW
, 4, 1, IMA_INTERRUPT
);
1071 gru_wait_async_cbr(han
);
1072 for (i
= 0; i
< numcb
; i
++) {
1073 cb
= cb0
+ i
* GRU_HANDLE_STRIDE
;
1074 istatus
= gru_check_status(cb
);
1075 if (istatus
!= CBS_ACTIVE
&& istatus
!= CBS_CALL_OS
)
1080 if (istatus
!= CBS_IDLE
) {
1081 printk(KERN_DEBUG
"GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i
);
1083 } else if (buf
[4 * i
] || buf
[4 * i
+ 1] || buf
[4 * i
+ 2] ||
1085 printk(KERN_DEBUG
"GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
1086 smp_processor_id(), i
, buf
[4 * i
], buf
[4 * i
+ 1], buf
[4 * i
+ 2], buf
[4 * i
+ 3]);
1091 gen
->istatus
= CBS_CALL_OS
; /* don't handle this CBR again */
1095 gru_unlock_async_resource(han
);
1096 gru_release_async_resources(han
);
1103 static int quicktest3(unsigned long arg
)
1105 char buf1
[BUFSIZE
], buf2
[BUFSIZE
];
1108 memset(buf2
, 0, sizeof(buf2
));
1109 memset(buf1
, get_cycles() & 255, sizeof(buf1
));
1110 gru_copy_gpa(uv_gpa(buf2
), uv_gpa(buf1
), BUFSIZE
);
1111 if (memcmp(buf1
, buf2
, BUFSIZE
)) {
1112 printk(KERN_DEBUG
"GRU:%d quicktest3 error\n", smp_processor_id());
1119 * Debugging only. User hook for various kernel tests
1122 int gru_ktest(unsigned long arg
)
1126 switch (arg
& 0xff) {
1128 ret
= quicktest0(arg
);
1131 ret
= quicktest1(arg
);
1134 ret
= quicktest2(arg
);
1137 ret
= quicktest3(arg
);
1140 ret
= gru_free_kernel_contexts();
1147 int gru_kservices_init(void)
1152 void gru_kservices_exit(void)
1154 if (gru_free_kernel_contexts())