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>
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 * At boot time, the kernel permanently reserves a fixed number of
49 * CBRs/DSRs for each cpu to use. The resources are all taken from
50 * the GRU chiplet 1 on the blade. This leaves the full set of resources
51 * of chiplet 0 available to be allocated to a single user.
54 /* Blade percpu resources PERMANENTLY reserved for kernel use */
55 #define GRU_NUM_KERNEL_CBR 1
56 #define GRU_NUM_KERNEL_DSR_BYTES 256
57 #define KERNEL_CTXNUM 15
59 /* GRU instruction attributes for all instructions */
60 #define IMA IMA_CB_DELAY
62 /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
63 #define __gru_cacheline_aligned__ \
64 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
66 #define MAGIC 0x1234567887654321UL
68 /* Default retry count for GRU errors on kernel instructions */
69 #define EXCEPTION_RETRY_LIMIT 3
71 /* Status of message queue sections */
76 /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
77 /* optimized for x86_64 */
78 struct message_queue {
79 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
80 int qlines; /* DW 1 */
82 void *next __gru_cacheline_aligned__;/* CL 1 */
86 char data ____cacheline_aligned; /* CL 2 */
89 /* First word in every message - used by mesq interface */
90 struct message_header {
97 #define QLINES(mq) ((mq) + offsetof(struct message_queue, qlines))
98 #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
100 static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
102 struct gru_blade_state *bs;
105 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
107 bs = gru_base[uv_numa_blade_id()];
108 lcpu = uv_blade_processor_id();
109 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
110 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
114 static void gru_free_cpu_resources(void *cb, void *dsr)
119 int gru_get_cb_exception_detail(void *cb,
120 struct control_block_extended_exc_detail *excdet)
122 struct gru_control_block_extended *cbe;
124 cbe = get_cbe(GRUBASE(cb), get_cb_number(cb));
125 excdet->opc = cbe->opccpy;
126 excdet->exopc = cbe->exopccpy;
127 excdet->ecause = cbe->ecause;
128 excdet->exceptdet0 = cbe->idef1upd;
129 excdet->exceptdet1 = cbe->idef3upd;
133 char *gru_get_cb_exception_detail_str(int ret, void *cb,
136 struct gru_control_block_status *gen = (void *)cb;
137 struct control_block_extended_exc_detail excdet;
139 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
140 gru_get_cb_exception_detail(cb, &excdet);
142 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
143 "excdet0 0x%lx, excdet1 0x%x",
144 gen, excdet.opc, excdet.exopc, excdet.ecause,
145 excdet.exceptdet0, excdet.exceptdet1);
147 snprintf(buf, size, "No exception");
152 static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
154 while (gen->istatus >= CBS_ACTIVE) {
161 static int gru_retry_exception(void *cb)
163 struct gru_control_block_status *gen = (void *)cb;
164 struct control_block_extended_exc_detail excdet;
165 int retry = EXCEPTION_RETRY_LIMIT;
168 if (gru_get_cb_message_queue_substatus(cb))
170 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
173 gru_get_cb_exception_detail(cb, &excdet);
174 if (excdet.ecause & ~EXCEPTION_RETRY_BITS)
179 gru_flush_cache(gen);
181 return CBS_EXCEPTION;
184 int gru_check_status_proc(void *cb)
186 struct gru_control_block_status *gen = (void *)cb;
190 if (ret != CBS_EXCEPTION)
192 return gru_retry_exception(cb);
196 int gru_wait_proc(void *cb)
198 struct gru_control_block_status *gen = (void *)cb;
201 ret = gru_wait_idle_or_exception(gen);
202 if (ret == CBS_EXCEPTION)
203 ret = gru_retry_exception(cb);
208 void gru_abort(int ret, void *cb, char *str)
210 char buf[GRU_EXC_STR_SIZE];
212 panic("GRU FATAL ERROR: %s - %s\n", str,
213 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
216 void gru_wait_abort_proc(void *cb)
220 ret = gru_wait_proc(cb);
222 gru_abort(ret, cb, "gru_wait_abort");
226 /*------------------------------ MESSAGE QUEUES -----------------------------*/
228 /* Internal status . These are NOT returned to the user. */
229 #define MQIE_AGAIN -1 /* try again */
233 * Save/restore the "present" flag that is in the second line of 2-line
236 static inline int get_present2(void *p)
238 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
239 return mhdr->present;
242 static inline void restore_present2(void *p, int val)
244 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
249 * Create a message queue.
250 * qlines - message queue size in cache lines. Includes 2-line header.
252 int gru_create_message_queue(void *p, unsigned int bytes)
254 struct message_queue *mq = p;
257 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
258 memset(mq, 0, bytes);
259 mq->start = &mq->data;
260 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
261 mq->next = &mq->data;
262 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
266 mq->head = gru_mesq_head(2, qlines / 2 + 1);
269 EXPORT_SYMBOL_GPL(gru_create_message_queue);
272 * Send a NOOP message to a message queue
274 * 0 - if queue is full after the send. This is the normal case
275 * but various races can change this.
276 * -1 - if mesq sent successfully but queue not full
277 * >0 - unexpected error. MQE_xxx returned
279 static int send_noop_message(void *cb,
280 unsigned long mq, void *mesg)
282 const struct message_header noop_header = {
283 .present = MQS_NOOP, .lines = 1};
286 struct message_header save_mhdr, *mhdr = mesg;
291 gru_mesq(cb, mq, gru_get_tri(mhdr), 1, IMA);
295 substatus = gru_get_cb_message_queue_substatus(cb);
298 STAT(mesq_noop_unexpected_error);
299 ret = MQE_UNEXPECTED_CB_ERR;
301 case CBSS_LB_OVERFLOWED:
302 STAT(mesq_noop_lb_overflow);
303 ret = MQE_CONGESTION;
305 case CBSS_QLIMIT_REACHED:
306 STAT(mesq_noop_qlimit_reached);
309 case CBSS_AMO_NACKED:
310 STAT(mesq_noop_amo_nacked);
311 ret = MQE_CONGESTION;
313 case CBSS_PUT_NACKED:
314 STAT(mesq_noop_put_nacked);
315 m = mq + (gru_get_amo_value_head(cb) << 6);
316 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
318 if (gru_wait(cb) == CBS_IDLE)
321 ret = MQE_UNEXPECTED_CB_ERR;
323 case CBSS_PAGE_OVERFLOW:
333 * Handle a gru_mesq full.
335 static int send_message_queue_full(void *cb,
336 unsigned long mq, void *mesg, int lines)
338 union gru_mesqhead mqh;
339 unsigned int limit, head;
340 unsigned long avalue;
341 int half, qlines, save;
343 /* Determine if switching to first/second half of q */
344 avalue = gru_get_amo_value(cb);
345 head = gru_get_amo_value_head(cb);
346 limit = gru_get_amo_value_limit(cb);
349 * Fetch "qlines" from the queue header. Since the queue may be
350 * in memory that can't be accessed using socket addresses, use
351 * the GRU to access the data. Use DSR space from the message.
354 gru_vload(cb, QLINES(mq), gru_get_tri(mesg), XTYPE_W, 1, 1, IMA);
355 if (gru_wait(cb) != CBS_IDLE)
357 qlines = *(int *)mesg;
359 half = (limit != qlines);
362 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
364 mqh = gru_mesq_head(2, qlines / 2 + 1);
366 /* Try to get lock for switching head pointer */
367 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mq, half), XTYPE_DW, IMA);
368 if (gru_wait(cb) != CBS_IDLE)
370 if (!gru_get_amo_value(cb)) {
371 STAT(mesq_qf_locked);
372 return MQE_QUEUE_FULL;
375 /* Got the lock. Send optional NOP if queue not full, */
377 if (send_noop_message(cb, mq, mesg)) {
378 gru_gamir(cb, EOP_IR_INC, HSTATUS(mq, half),
380 if (gru_wait(cb) != CBS_IDLE)
382 STAT(mesq_qf_noop_not_full);
388 /* Then flip queuehead to other half of queue. */
389 gru_gamer(cb, EOP_ERR_CSWAP, mq, XTYPE_DW, mqh.val, avalue, IMA);
390 if (gru_wait(cb) != CBS_IDLE)
393 /* If not successfully in swapping queue head, clear the hstatus lock */
394 if (gru_get_amo_value(cb) != avalue) {
395 STAT(mesq_qf_switch_head_failed);
396 gru_gamir(cb, EOP_IR_INC, HSTATUS(mq, half), XTYPE_DW, IMA);
397 if (gru_wait(cb) != CBS_IDLE)
402 STAT(mesq_qf_unexpected_error);
403 return MQE_UNEXPECTED_CB_ERR;
408 * Handle a gru_mesq failure. Some of these failures are software recoverable
411 static int send_message_failure(void *cb,
416 int substatus, ret = 0;
419 substatus = gru_get_cb_message_queue_substatus(cb);
422 STAT(mesq_send_unexpected_error);
423 ret = MQE_UNEXPECTED_CB_ERR;
425 case CBSS_LB_OVERFLOWED:
426 STAT(mesq_send_lb_overflow);
427 ret = MQE_CONGESTION;
429 case CBSS_QLIMIT_REACHED:
430 STAT(mesq_send_qlimit_reached);
431 ret = send_message_queue_full(cb, mq, mesg, lines);
433 case CBSS_AMO_NACKED:
434 STAT(mesq_send_amo_nacked);
435 ret = MQE_CONGESTION;
437 case CBSS_PUT_NACKED:
438 STAT(mesq_send_put_nacked);
439 m =mq + (gru_get_amo_value_head(cb) << 6);
440 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
441 if (gru_wait(cb) == CBS_IDLE)
444 ret = MQE_UNEXPECTED_CB_ERR;
453 * Send a message to a message queue
454 * cb GRU control block to use to send message
456 * mesg message. ust be vaddr within a GSEG
457 * bytes message size (<= 2 CL)
459 int gru_send_message_gpa(unsigned long mq, void *mesg, unsigned int bytes)
461 struct message_header *mhdr;
464 int istatus, clines, ret;
467 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
469 clines = (bytes + GRU_CACHE_LINE_BYTES - 1) / GRU_CACHE_LINE_BYTES;
470 if (gru_get_cpu_resources(bytes, &cb, &dsr))
471 return MQE_BUG_NO_RESOURCES;
472 memcpy(dsr, mesg, bytes);
474 mhdr->present = MQS_FULL;
475 mhdr->lines = clines;
477 mhdr->present2 = get_present2(mhdr);
478 restore_present2(mhdr, MQS_FULL);
483 gru_mesq(cb, mq, gru_get_tri(mhdr), clines, IMA);
484 istatus = gru_wait(cb);
485 if (istatus != CBS_IDLE)
486 ret = send_message_failure(cb, mq, dsr, clines);
487 } while (ret == MQIE_AGAIN);
488 gru_free_cpu_resources(cb, dsr);
491 STAT(mesq_send_failed);
494 EXPORT_SYMBOL_GPL(gru_send_message_gpa);
497 * Advance the receive pointer for the queue to the next message.
499 void gru_free_message(void *rmq, void *mesg)
501 struct message_queue *mq = rmq;
502 struct message_header *mhdr = mq->next;
505 int lines = mhdr->lines;
508 restore_present2(mhdr, MQS_EMPTY);
509 mhdr->present = MQS_EMPTY;
512 next = pnext + GRU_CACHE_LINE_BYTES * lines;
513 if (next == mq->limit) {
516 } else if (pnext < mq->start2 && next >= mq->start2) {
521 mq->hstatus[half] = 1;
524 EXPORT_SYMBOL_GPL(gru_free_message);
527 * Get next message from message queue. Return NULL if no message
528 * present. User must call next_message() to move to next message.
531 void *gru_get_next_message(void *rmq)
533 struct message_queue *mq = rmq;
534 struct message_header *mhdr = mq->next;
535 int present = mhdr->present;
537 /* skip NOOP messages */
539 while (present == MQS_NOOP) {
540 gru_free_message(rmq, mhdr);
542 present = mhdr->present;
545 /* Wait for both halves of 2 line messages */
546 if (present == MQS_FULL && mhdr->lines == 2 &&
547 get_present2(mhdr) == MQS_EMPTY)
551 STAT(mesq_receive_none);
555 if (mhdr->lines == 2)
556 restore_present2(mhdr, mhdr->present2);
560 EXPORT_SYMBOL_GPL(gru_get_next_message);
562 /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
565 * Copy a block of data using the GRU resources
567 int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
575 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
576 return MQE_BUG_NO_RESOURCES;
577 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
578 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_BYTES, IMA);
580 gru_free_cpu_resources(cb, dsr);
583 EXPORT_SYMBOL_GPL(gru_copy_gpa);
585 /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
586 /* Temp - will delete after we gain confidence in the GRU */
587 static __cacheline_aligned unsigned long word0;
588 static __cacheline_aligned unsigned long word1;
590 static int quicktest(struct gru_state *gru)
596 cb = get_gseg_base_address_cb(gru->gs_gru_base_vaddr, KERNEL_CTXNUM, 0);
597 ds = get_gseg_base_address_ds(gru->gs_gru_base_vaddr, KERNEL_CTXNUM, 0);
601 gru_vload(cb, uv_gpa(&word0), 0, XTYPE_DW, 1, 1, IMA);
602 if (gru_wait(cb) != CBS_IDLE)
605 if (*(unsigned long *)ds != MAGIC)
607 gru_vstore(cb, uv_gpa(&word1), 0, XTYPE_DW, 1, 1, IMA);
608 if (gru_wait(cb) != CBS_IDLE)
611 if (word0 != word1 || word0 != MAGIC) {
613 ("GRU quicktest err: gru %d, found 0x%lx, expected 0x%lx\n",
614 gru->gs_gid, word1, MAGIC);
615 BUG(); /* ZZZ should not be fatal */
622 int gru_kservices_init(struct gru_state *gru)
624 struct gru_blade_state *bs;
625 struct gru_context_configuration_handle *cch;
626 unsigned long cbr_map, dsr_map;
627 int err, num, cpus_possible;
630 * Currently, resources are reserved ONLY on the second chiplet
631 * on each blade. This leaves ALL resources on chiplet 0 available
635 if (gru != &bs->bs_grus[1])
638 cpus_possible = uv_blade_nr_possible_cpus(gru->gs_blade_id);
640 num = GRU_NUM_KERNEL_CBR * cpus_possible;
641 cbr_map = gru_reserve_cb_resources(gru, GRU_CB_COUNT_TO_AU(num), NULL);
642 gru->gs_reserved_cbrs += num;
644 num = GRU_NUM_KERNEL_DSR_BYTES * cpus_possible;
645 dsr_map = gru_reserve_ds_resources(gru, GRU_DS_BYTES_TO_AU(num), NULL);
646 gru->gs_reserved_dsr_bytes += num;
648 gru->gs_active_contexts++;
649 __set_bit(KERNEL_CTXNUM, &gru->gs_context_map);
650 cch = get_cch(gru->gs_gru_base_vaddr, KERNEL_CTXNUM);
652 bs->kernel_cb = get_gseg_base_address_cb(gru->gs_gru_base_vaddr,
654 bs->kernel_dsr = get_gseg_base_address_ds(gru->gs_gru_base_vaddr,
657 lock_cch_handle(cch);
658 cch->tfm_fault_bit_enable = 0;
659 cch->tlb_int_enable = 0;
660 cch->tfm_done_bit_enable = 0;
661 cch->unmap_enable = 1;
662 err = cch_allocate(cch, 0, cbr_map, dsr_map);
665 "Unable to allocate kernel CCH: gru %d, err %d\n",
669 if (cch_start(cch)) {
670 gru_dbg(grudev, "Unable to start kernel CCH: gru %d, err %d\n",
674 unlock_cch_handle(cch);
676 if (gru_options & GRU_QUICKLOOK)