gru: add user request to explicitly unload a gru context
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / sgi-gru / grukservices.c
CommitLineData
28bffaf0
JS
1/*
2 * SN Platform GRU Driver
3 *
4 * KERNEL SERVICES THAT USE THE GRU
5 *
6 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/slab.h>
26#include <linux/mm.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>
836ce679 34#include <linux/delay.h>
28bffaf0
JS
35#include "gru.h"
36#include "grulib.h"
37#include "grutables.h"
38#include "grukservices.h"
39#include "gru_instructions.h"
40#include <asm/uv/uv_hub.h>
41
42/*
43 * Kernel GRU Usage
44 *
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.
48 *
836ce679
JS
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!!!.
9120dec4
JS
55 *
56 * Async Overview:
57 *
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.
61 *
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
69 * stealing/reloading.
70 *
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
76 * grukservices.
77 * (Now that we have dynamic loading of kernel contexts, I
78 * may rethink this & allow sharing between cpus....)
79 *
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.
89 *
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.
94 *
28bffaf0 95 */
9120dec4
JS
96
97
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)]
1a2c09e3
JS
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)]
9120dec4 104
6f2584f4 105#define GRU_NUM_KERNEL_CBR 1
28bffaf0 106#define GRU_NUM_KERNEL_DSR_BYTES 256
6f2584f4
JS
107#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
108 GRU_CACHE_LINE_BYTES)
28bffaf0
JS
109
110/* GRU instruction attributes for all instructions */
111#define IMA IMA_CB_DELAY
112
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)))
116
117#define MAGIC 0x1234567887654321UL
118
119/* Default retry count for GRU errors on kernel instructions */
120#define EXCEPTION_RETRY_LIMIT 3
121
122/* Status of message queue sections */
123#define MQS_EMPTY 0
124#define MQS_FULL 1
125#define MQS_NOOP 2
126
127/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
128/* optimized for x86_64 */
129struct message_queue {
130 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
131 int qlines; /* DW 1 */
132 long hstatus[2];
133 void *next __gru_cacheline_aligned__;/* CL 1 */
134 void *limit;
135 void *start;
136 void *start2;
137 char data ____cacheline_aligned; /* CL 2 */
138};
139
140/* First word in every message - used by mesq interface */
141struct message_header {
142 char present;
143 char present2;
144 char lines;
145 char fill;
146};
147
28bffaf0
JS
148#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
149
836ce679
JS
150/*
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.
153 */
154static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
155{
156 struct gru_state *gru;
157 struct gru_thread_state *kgts;
158 void *vaddr;
9120dec4 159 int ctxnum, ncpus;
836ce679
JS
160
161 up_read(&bs->bs_kgts_sema);
162 down_write(&bs->bs_kgts_sema);
163
164 if (!bs->bs_kgts)
9120dec4 165 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
836ce679
JS
166 kgts = bs->bs_kgts;
167
168 if (!kgts->ts_gru) {
169 STAT(load_kernel_context);
9120dec4
JS
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);
836ce679
JS
176 while (!gru_assign_gru_context(kgts, blade_id)) {
177 msleep(1);
178 gru_steal_context(kgts, blade_id);
179 }
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);
186 }
187 downgrade_write(&bs->bs_kgts_sema);
188}
189
d5826dd6
JS
190/*
191 * Free all kernel contexts that are not currently in use.
192 * Returns 0 if all freed, else number of inuse context.
193 */
194static int gru_free_kernel_contexts(void)
195{
196 struct gru_blade_state *bs;
197 struct gru_thread_state *kgts;
198 int bid, ret = 0;
199
200 for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
201 bs = gru_base[bid];
202 if (!bs)
203 continue;
204 if (down_write_trylock(&bs->bs_kgts_sema)) {
205 kgts = bs->bs_kgts;
206 if (kgts && kgts->ts_gru)
207 gru_unload_context(kgts, 0);
208 kfree(kgts);
209 bs->bs_kgts = NULL;
210 up_write(&bs->bs_kgts_sema);
211 } else {
212 ret++;
213 }
214 }
215 return ret;
216}
217
836ce679
JS
218/*
219 * Lock & load the kernel context for the specified blade.
220 */
221static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
222{
223 struct gru_blade_state *bs;
224
225 STAT(lock_kernel_context);
226 bs = gru_base[blade_id];
227
228 down_read(&bs->bs_kgts_sema);
229 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
230 gru_load_kernel_context(bs, blade_id);
231 return bs;
232
233}
234
235/*
236 * Unlock the kernel context for the specified blade. Context is not
237 * unloaded but may be stolen before next use.
238 */
239static void gru_unlock_kernel_context(int blade_id)
240{
241 struct gru_blade_state *bs;
242
243 bs = gru_base[blade_id];
244 up_read(&bs->bs_kgts_sema);
245 STAT(unlock_kernel_context);
246}
247
248/*
249 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
250 * - returns with preemption disabled
251 */
28bffaf0
JS
252static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
253{
254 struct gru_blade_state *bs;
255 int lcpu;
256
257 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
258 preempt_disable();
836ce679 259 bs = gru_lock_kernel_context(uv_numa_blade_id());
28bffaf0
JS
260 lcpu = uv_blade_processor_id();
261 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
262 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
263 return 0;
264}
265
836ce679
JS
266/*
267 * Free the current cpus reserved DSR/CBR resources.
268 */
28bffaf0
JS
269static void gru_free_cpu_resources(void *cb, void *dsr)
270{
836ce679 271 gru_unlock_kernel_context(uv_numa_blade_id());
28bffaf0
JS
272 preempt_enable();
273}
274
9120dec4
JS
275/*
276 * Reserve GRU resources to be used asynchronously.
277 * Note: currently supports only 1 reservation per blade.
278 *
279 * input:
280 * blade_id - blade on which resources should be reserved
281 * cbrs - number of CBRs
282 * dsr_bytes - number of DSR bytes needed
283 * output:
284 * handle to identify resource
285 * (0 = async resources already reserved)
286 */
287unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
288 struct completion *cmp)
289{
290 struct gru_blade_state *bs;
291 struct gru_thread_state *kgts;
292 int ret = 0;
293
294 bs = gru_base[blade_id];
295
296 down_write(&bs->bs_kgts_sema);
297
298 /* Verify no resources already reserved */
299 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
300 goto done;
301 bs->bs_async_dsr_bytes = dsr_bytes;
302 bs->bs_async_cbrs = cbrs;
303 bs->bs_async_wq = cmp;
304 kgts = bs->bs_kgts;
305
306 /* Resources changed. Unload context if already loaded */
307 if (kgts && kgts->ts_gru)
308 gru_unload_context(kgts, 0);
309 ret = ASYNC_BID_TO_HAN(blade_id);
310
311done:
312 up_write(&bs->bs_kgts_sema);
313 return ret;
314}
315
316/*
317 * Release async resources previously reserved.
318 *
319 * input:
320 * han - handle to identify resources
321 */
322void gru_release_async_resources(unsigned long han)
323{
324 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
325
326 down_write(&bs->bs_kgts_sema);
327 bs->bs_async_dsr_bytes = 0;
328 bs->bs_async_cbrs = 0;
329 bs->bs_async_wq = NULL;
330 up_write(&bs->bs_kgts_sema);
331}
332
333/*
334 * Wait for async GRU instructions to complete.
335 *
336 * input:
337 * han - handle to identify resources
338 */
339void gru_wait_async_cbr(unsigned long han)
340{
341 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
342
343 wait_for_completion(bs->bs_async_wq);
344 mb();
345}
346
347/*
348 * Lock previous reserved async GRU resources
349 *
350 * input:
351 * han - handle to identify resources
352 * output:
353 * cb - pointer to first CBR
354 * dsr - pointer to first DSR
355 */
356void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
357{
358 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
359 int blade_id = ASYNC_HAN_TO_BID(han);
360 int ncpus;
361
362 gru_lock_kernel_context(blade_id);
363 ncpus = uv_blade_nr_possible_cpus(blade_id);
364 if (cb)
365 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
366 if (dsr)
367 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
368}
369
370/*
371 * Unlock previous reserved async GRU resources
372 *
373 * input:
374 * han - handle to identify resources
375 */
376void gru_unlock_async_resource(unsigned long han)
377{
378 int blade_id = ASYNC_HAN_TO_BID(han);
379
380 gru_unlock_kernel_context(blade_id);
381}
382
836ce679 383/*----------------------------------------------------------------------*/
28bffaf0
JS
384int gru_get_cb_exception_detail(void *cb,
385 struct control_block_extended_exc_detail *excdet)
386{
387 struct gru_control_block_extended *cbe;
1a2c09e3
JS
388 struct gru_blade_state *bs;
389 int cbrnum;
28bffaf0 390
1a2c09e3
JS
391 bs = KCB_TO_BS(cb);
392 cbrnum = thread_cbr_number(bs->bs_kgts, get_cb_number(cb));
393 cbe = get_cbe(GRUBASE(cb), cbrnum);
394 gru_flush_cache(cbe); /* CBE not coherent */
28bffaf0
JS
395 excdet->opc = cbe->opccpy;
396 excdet->exopc = cbe->exopccpy;
397 excdet->ecause = cbe->ecause;
398 excdet->exceptdet0 = cbe->idef1upd;
399 excdet->exceptdet1 = cbe->idef3upd;
1a2c09e3 400 gru_flush_cache(cbe);
28bffaf0
JS
401 return 0;
402}
403
404char *gru_get_cb_exception_detail_str(int ret, void *cb,
405 char *buf, int size)
406{
407 struct gru_control_block_status *gen = (void *)cb;
408 struct control_block_extended_exc_detail excdet;
409
410 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
411 gru_get_cb_exception_detail(cb, &excdet);
412 snprintf(buf, size,
413 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
414 "excdet0 0x%lx, excdet1 0x%x",
415 gen, excdet.opc, excdet.exopc, excdet.ecause,
416 excdet.exceptdet0, excdet.exceptdet1);
417 } else {
418 snprintf(buf, size, "No exception");
419 }
420 return buf;
421}
422
423static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
424{
425 while (gen->istatus >= CBS_ACTIVE) {
426 cpu_relax();
427 barrier();
428 }
429 return gen->istatus;
430}
431
432static int gru_retry_exception(void *cb)
433{
434 struct gru_control_block_status *gen = (void *)cb;
435 struct control_block_extended_exc_detail excdet;
436 int retry = EXCEPTION_RETRY_LIMIT;
437
438 while (1) {
439 if (gru_get_cb_message_queue_substatus(cb))
440 break;
441 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
442 return CBS_IDLE;
443
444 gru_get_cb_exception_detail(cb, &excdet);
270952a9
JS
445 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
446 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
28bffaf0
JS
447 break;
448 if (retry-- == 0)
449 break;
450 gen->icmd = 1;
451 gru_flush_cache(gen);
452 }
453 return CBS_EXCEPTION;
454}
455
456int gru_check_status_proc(void *cb)
457{
458 struct gru_control_block_status *gen = (void *)cb;
459 int ret;
460
461 ret = gen->istatus;
462 if (ret != CBS_EXCEPTION)
463 return ret;
464 return gru_retry_exception(cb);
465
466}
467
468int gru_wait_proc(void *cb)
469{
470 struct gru_control_block_status *gen = (void *)cb;
471 int ret;
472
473 ret = gru_wait_idle_or_exception(gen);
474 if (ret == CBS_EXCEPTION)
475 ret = gru_retry_exception(cb);
476
477 return ret;
478}
479
480void gru_abort(int ret, void *cb, char *str)
481{
482 char buf[GRU_EXC_STR_SIZE];
483
484 panic("GRU FATAL ERROR: %s - %s\n", str,
485 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
486}
487
488void gru_wait_abort_proc(void *cb)
489{
490 int ret;
491
492 ret = gru_wait_proc(cb);
493 if (ret)
494 gru_abort(ret, cb, "gru_wait_abort");
495}
496
497
498/*------------------------------ MESSAGE QUEUES -----------------------------*/
499
500/* Internal status . These are NOT returned to the user. */
501#define MQIE_AGAIN -1 /* try again */
502
503
504/*
505 * Save/restore the "present" flag that is in the second line of 2-line
506 * messages
507 */
508static inline int get_present2(void *p)
509{
510 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
511 return mhdr->present;
512}
513
514static inline void restore_present2(void *p, int val)
515{
516 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
517 mhdr->present = val;
518}
519
520/*
521 * Create a message queue.
522 * qlines - message queue size in cache lines. Includes 2-line header.
523 */
6f2584f4
JS
524int gru_create_message_queue(struct gru_message_queue_desc *mqd,
525 void *p, unsigned int bytes, int nasid, int vector, int apicid)
28bffaf0
JS
526{
527 struct message_queue *mq = p;
528 unsigned int qlines;
529
530 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
531 memset(mq, 0, bytes);
532 mq->start = &mq->data;
533 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
534 mq->next = &mq->data;
535 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
536 mq->qlines = qlines;
537 mq->hstatus[0] = 0;
538 mq->hstatus[1] = 1;
539 mq->head = gru_mesq_head(2, qlines / 2 + 1);
6f2584f4
JS
540 mqd->mq = mq;
541 mqd->mq_gpa = uv_gpa(mq);
542 mqd->qlines = qlines;
543 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
544 mqd->interrupt_vector = vector;
545 mqd->interrupt_apicid = apicid;
28bffaf0
JS
546 return 0;
547}
548EXPORT_SYMBOL_GPL(gru_create_message_queue);
549
550/*
551 * Send a NOOP message to a message queue
552 * Returns:
553 * 0 - if queue is full after the send. This is the normal case
554 * but various races can change this.
555 * -1 - if mesq sent successfully but queue not full
556 * >0 - unexpected error. MQE_xxx returned
557 */
6f2584f4
JS
558static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
559 void *mesg)
28bffaf0
JS
560{
561 const struct message_header noop_header = {
562 .present = MQS_NOOP, .lines = 1};
563 unsigned long m;
564 int substatus, ret;
565 struct message_header save_mhdr, *mhdr = mesg;
566
567 STAT(mesq_noop);
568 save_mhdr = *mhdr;
569 *mhdr = noop_header;
6f2584f4 570 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
28bffaf0
JS
571 ret = gru_wait(cb);
572
573 if (ret) {
574 substatus = gru_get_cb_message_queue_substatus(cb);
575 switch (substatus) {
576 case CBSS_NO_ERROR:
577 STAT(mesq_noop_unexpected_error);
578 ret = MQE_UNEXPECTED_CB_ERR;
579 break;
580 case CBSS_LB_OVERFLOWED:
581 STAT(mesq_noop_lb_overflow);
582 ret = MQE_CONGESTION;
583 break;
584 case CBSS_QLIMIT_REACHED:
585 STAT(mesq_noop_qlimit_reached);
586 ret = 0;
587 break;
588 case CBSS_AMO_NACKED:
589 STAT(mesq_noop_amo_nacked);
590 ret = MQE_CONGESTION;
591 break;
592 case CBSS_PUT_NACKED:
593 STAT(mesq_noop_put_nacked);
6f2584f4 594 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
28bffaf0
JS
595 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
596 IMA);
597 if (gru_wait(cb) == CBS_IDLE)
598 ret = MQIE_AGAIN;
599 else
600 ret = MQE_UNEXPECTED_CB_ERR;
601 break;
602 case CBSS_PAGE_OVERFLOW:
603 default:
604 BUG();
605 }
606 }
607 *mhdr = save_mhdr;
608 return ret;
609}
610
611/*
612 * Handle a gru_mesq full.
613 */
6f2584f4
JS
614static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
615 void *mesg, int lines)
28bffaf0
JS
616{
617 union gru_mesqhead mqh;
618 unsigned int limit, head;
619 unsigned long avalue;
6f2584f4 620 int half, qlines;
28bffaf0
JS
621
622 /* Determine if switching to first/second half of q */
623 avalue = gru_get_amo_value(cb);
624 head = gru_get_amo_value_head(cb);
625 limit = gru_get_amo_value_limit(cb);
626
6f2584f4 627 qlines = mqd->qlines;
28bffaf0
JS
628 half = (limit != qlines);
629
630 if (half)
631 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
632 else
633 mqh = gru_mesq_head(2, qlines / 2 + 1);
634
635 /* Try to get lock for switching head pointer */
6f2584f4 636 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
28bffaf0
JS
637 if (gru_wait(cb) != CBS_IDLE)
638 goto cberr;
639 if (!gru_get_amo_value(cb)) {
640 STAT(mesq_qf_locked);
641 return MQE_QUEUE_FULL;
642 }
643
644 /* Got the lock. Send optional NOP if queue not full, */
645 if (head != limit) {
6f2584f4
JS
646 if (send_noop_message(cb, mqd, mesg)) {
647 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
28bffaf0
JS
648 XTYPE_DW, IMA);
649 if (gru_wait(cb) != CBS_IDLE)
650 goto cberr;
651 STAT(mesq_qf_noop_not_full);
652 return MQIE_AGAIN;
653 }
654 avalue++;
655 }
656
657 /* Then flip queuehead to other half of queue. */
6f2584f4
JS
658 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
659 IMA);
28bffaf0
JS
660 if (gru_wait(cb) != CBS_IDLE)
661 goto cberr;
662
663 /* If not successfully in swapping queue head, clear the hstatus lock */
664 if (gru_get_amo_value(cb) != avalue) {
665 STAT(mesq_qf_switch_head_failed);
6f2584f4
JS
666 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
667 IMA);
28bffaf0
JS
668 if (gru_wait(cb) != CBS_IDLE)
669 goto cberr;
670 }
671 return MQIE_AGAIN;
672cberr:
673 STAT(mesq_qf_unexpected_error);
674 return MQE_UNEXPECTED_CB_ERR;
675}
676
6f2584f4
JS
677/*
678 * Send a cross-partition interrupt to the SSI that contains the target
679 * message queue. Normally, the interrupt is automatically delivered by hardware
680 * but some error conditions require explicit delivery.
681 */
682static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
683{
684 if (mqd->interrupt_vector)
685 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
686 mqd->interrupt_vector);
687}
688
17b49a67
JS
689/*
690 * Handle a PUT failure. Note: if message was a 2-line message, one of the
691 * lines might have successfully have been written. Before sending the
692 * message, "present" must be cleared in BOTH lines to prevent the receiver
693 * from prematurely seeing the full message.
694 */
695static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
696 void *mesg, int lines)
697{
698 unsigned long m;
699
700 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
701 if (lines == 2) {
702 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
703 if (gru_wait(cb) != CBS_IDLE)
704 return MQE_UNEXPECTED_CB_ERR;
705 }
706 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
707 if (gru_wait(cb) != CBS_IDLE)
708 return MQE_UNEXPECTED_CB_ERR;
709 send_message_queue_interrupt(mqd);
710 return MQE_OK;
711}
28bffaf0
JS
712
713/*
714 * Handle a gru_mesq failure. Some of these failures are software recoverable
715 * or retryable.
716 */
6f2584f4
JS
717static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
718 void *mesg, int lines)
28bffaf0
JS
719{
720 int substatus, ret = 0;
28bffaf0
JS
721
722 substatus = gru_get_cb_message_queue_substatus(cb);
723 switch (substatus) {
724 case CBSS_NO_ERROR:
725 STAT(mesq_send_unexpected_error);
726 ret = MQE_UNEXPECTED_CB_ERR;
727 break;
728 case CBSS_LB_OVERFLOWED:
729 STAT(mesq_send_lb_overflow);
730 ret = MQE_CONGESTION;
731 break;
732 case CBSS_QLIMIT_REACHED:
733 STAT(mesq_send_qlimit_reached);
6f2584f4 734 ret = send_message_queue_full(cb, mqd, mesg, lines);
28bffaf0
JS
735 break;
736 case CBSS_AMO_NACKED:
737 STAT(mesq_send_amo_nacked);
738 ret = MQE_CONGESTION;
739 break;
740 case CBSS_PUT_NACKED:
741 STAT(mesq_send_put_nacked);
17b49a67 742 ret = send_message_put_nacked(cb, mqd, mesg, lines);
28bffaf0
JS
743 break;
744 default:
745 BUG();
746 }
747 return ret;
748}
749
750/*
751 * Send a message to a message queue
6f2584f4 752 * mqd message queue descriptor
28bffaf0
JS
753 * mesg message. ust be vaddr within a GSEG
754 * bytes message size (<= 2 CL)
755 */
6f2584f4
JS
756int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
757 unsigned int bytes)
28bffaf0
JS
758{
759 struct message_header *mhdr;
760 void *cb;
761 void *dsr;
762 int istatus, clines, ret;
763
764 STAT(mesq_send);
765 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
766
cbf330b9 767 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
28bffaf0
JS
768 if (gru_get_cpu_resources(bytes, &cb, &dsr))
769 return MQE_BUG_NO_RESOURCES;
770 memcpy(dsr, mesg, bytes);
771 mhdr = dsr;
772 mhdr->present = MQS_FULL;
773 mhdr->lines = clines;
774 if (clines == 2) {
775 mhdr->present2 = get_present2(mhdr);
776 restore_present2(mhdr, MQS_FULL);
777 }
778
779 do {
780 ret = MQE_OK;
6f2584f4 781 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
28bffaf0
JS
782 istatus = gru_wait(cb);
783 if (istatus != CBS_IDLE)
6f2584f4 784 ret = send_message_failure(cb, mqd, dsr, clines);
28bffaf0
JS
785 } while (ret == MQIE_AGAIN);
786 gru_free_cpu_resources(cb, dsr);
787
788 if (ret)
789 STAT(mesq_send_failed);
790 return ret;
791}
792EXPORT_SYMBOL_GPL(gru_send_message_gpa);
793
794/*
795 * Advance the receive pointer for the queue to the next message.
796 */
6f2584f4 797void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
28bffaf0 798{
6f2584f4 799 struct message_queue *mq = mqd->mq;
28bffaf0
JS
800 struct message_header *mhdr = mq->next;
801 void *next, *pnext;
802 int half = -1;
803 int lines = mhdr->lines;
804
805 if (lines == 2)
806 restore_present2(mhdr, MQS_EMPTY);
807 mhdr->present = MQS_EMPTY;
808
809 pnext = mq->next;
810 next = pnext + GRU_CACHE_LINE_BYTES * lines;
811 if (next == mq->limit) {
812 next = mq->start;
813 half = 1;
814 } else if (pnext < mq->start2 && next >= mq->start2) {
815 half = 0;
816 }
817
818 if (half >= 0)
819 mq->hstatus[half] = 1;
820 mq->next = next;
821}
822EXPORT_SYMBOL_GPL(gru_free_message);
823
824/*
825 * Get next message from message queue. Return NULL if no message
826 * present. User must call next_message() to move to next message.
827 * rmq message queue
828 */
6f2584f4 829void *gru_get_next_message(struct gru_message_queue_desc *mqd)
28bffaf0 830{
6f2584f4 831 struct message_queue *mq = mqd->mq;
28bffaf0
JS
832 struct message_header *mhdr = mq->next;
833 int present = mhdr->present;
834
835 /* skip NOOP messages */
836 STAT(mesq_receive);
837 while (present == MQS_NOOP) {
6f2584f4 838 gru_free_message(mqd, mhdr);
28bffaf0
JS
839 mhdr = mq->next;
840 present = mhdr->present;
841 }
842
843 /* Wait for both halves of 2 line messages */
844 if (present == MQS_FULL && mhdr->lines == 2 &&
845 get_present2(mhdr) == MQS_EMPTY)
846 present = MQS_EMPTY;
847
848 if (!present) {
849 STAT(mesq_receive_none);
850 return NULL;
851 }
852
853 if (mhdr->lines == 2)
854 restore_present2(mhdr, mhdr->present2);
855
856 return mhdr;
857}
858EXPORT_SYMBOL_GPL(gru_get_next_message);
859
860/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
861
862/*
863 * Copy a block of data using the GRU resources
864 */
865int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
866 unsigned int bytes)
867{
868 void *cb;
869 void *dsr;
870 int ret;
871
872 STAT(copy_gpa);
873 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
874 return MQE_BUG_NO_RESOURCES;
875 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
6f2584f4 876 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
28bffaf0
JS
877 ret = gru_wait(cb);
878 gru_free_cpu_resources(cb, dsr);
879 return ret;
880}
881EXPORT_SYMBOL_GPL(gru_copy_gpa);
882
883/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
884/* Temp - will delete after we gain confidence in the GRU */
28bffaf0 885
eb5bd5e5 886static int quicktest0(unsigned long arg)
28bffaf0 887{
836ce679
JS
888 unsigned long word0;
889 unsigned long word1;
28bffaf0 890 void *cb;
836ce679 891 void *dsr;
28bffaf0 892 unsigned long *p;
eb5bd5e5 893 int ret = -EIO;
28bffaf0 894
836ce679
JS
895 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
896 return MQE_BUG_NO_RESOURCES;
897 p = dsr;
28bffaf0 898 word0 = MAGIC;
836ce679 899 word1 = 0;
28bffaf0 900
836ce679 901 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
eb5bd5e5
JS
902 if (gru_wait(cb) != CBS_IDLE) {
903 printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
904 goto done;
905 }
28bffaf0 906
eb5bd5e5
JS
907 if (*p != MAGIC) {
908 printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
909 goto done;
910 }
836ce679 911 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
eb5bd5e5
JS
912 if (gru_wait(cb) != CBS_IDLE) {
913 printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
914 goto done;
915 }
28bffaf0 916
836ce679 917 if (word0 != word1 || word1 != MAGIC) {
eb5bd5e5
JS
918 printk(KERN_DEBUG
919 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
836ce679 920 word1, MAGIC);
eb5bd5e5 921 goto done;
28bffaf0 922 }
eb5bd5e5 923 ret = 0;
28bffaf0 924
eb5bd5e5
JS
925done:
926 gru_free_cpu_resources(cb, dsr);
927 return ret;
28bffaf0
JS
928}
929
eb5bd5e5
JS
930#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
931
932static int quicktest1(unsigned long arg)
933{
934 struct gru_message_queue_desc mqd;
935 void *p, *mq;
936 unsigned long *dw;
937 int i, ret = -EIO;
938 char mes[GRU_CACHE_LINE_BYTES], *m;
939
940 /* Need 1K cacheline aligned that does not cross page boundary */
941 p = kmalloc(4096, 0);
942 mq = ALIGNUP(p, 1024);
943 memset(mes, 0xee, sizeof(mes));
944 dw = mq;
945
946 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
947 for (i = 0; i < 6; i++) {
948 mes[8] = i;
949 do {
950 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
951 } while (ret == MQE_CONGESTION);
952 if (ret)
953 break;
954 }
955 if (ret != MQE_QUEUE_FULL || i != 4)
956 goto done;
957
958 for (i = 0; i < 6; i++) {
959 m = gru_get_next_message(&mqd);
960 if (!m || m[8] != i)
961 break;
962 gru_free_message(&mqd, m);
963 }
964 ret = (i == 4) ? 0 : -EIO;
965
966done:
967 kfree(p);
968 return ret;
969}
970
971static int quicktest2(unsigned long arg)
972{
973 static DECLARE_COMPLETION(cmp);
974 unsigned long han;
975 int blade_id = 0;
976 int numcb = 4;
977 int ret = 0;
978 unsigned long *buf;
979 void *cb0, *cb;
980 int i, k, istatus, bytes;
981
982 bytes = numcb * 4 * 8;
983 buf = kmalloc(bytes, GFP_KERNEL);
984 if (!buf)
985 return -ENOMEM;
986
987 ret = -EBUSY;
988 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
989 if (!han)
990 goto done;
991
992 gru_lock_async_resource(han, &cb0, NULL);
993 memset(buf, 0xee, bytes);
994 for (i = 0; i < numcb; i++)
995 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
996 XTYPE_DW, 4, 1, IMA_INTERRUPT);
997
998 ret = 0;
999 for (k = 0; k < numcb; k++) {
1000 gru_wait_async_cbr(han);
1001 for (i = 0; i < numcb; i++) {
1002 cb = cb0 + i * GRU_HANDLE_STRIDE;
1003 istatus = gru_check_status(cb);
1004 if (istatus == CBS_ACTIVE)
1005 continue;
1006 if (istatus == CBS_EXCEPTION)
1007 ret = -EFAULT;
1008 else if (buf[i] || buf[i + 1] || buf[i + 2] ||
1009 buf[i + 3])
1010 ret = -EIO;
1011 }
1012 }
1013 BUG_ON(cmp.done);
1014
1015 gru_unlock_async_resource(han);
1016 gru_release_async_resources(han);
1017done:
1018 kfree(buf);
1019 return ret;
1020}
1021
1022/*
1023 * Debugging only. User hook for various kernel tests
1024 * of driver & gru.
1025 */
1026int gru_ktest(unsigned long arg)
1027{
1028 int ret = -EINVAL;
1029
1030 switch (arg & 0xff) {
1031 case 0:
1032 ret = quicktest0(arg);
1033 break;
1034 case 1:
1035 ret = quicktest1(arg);
1036 break;
1037 case 2:
1038 ret = quicktest2(arg);
1039 break;
d5826dd6
JS
1040 case 99:
1041 ret = gru_free_kernel_contexts();
1042 break;
eb5bd5e5
JS
1043 }
1044 return ret;
1045
1046}
28bffaf0 1047
d5826dd6 1048int gru_kservices_init(void)
28bffaf0 1049{
28bffaf0
JS
1050 return 0;
1051}
27ca8a7b 1052
d5826dd6 1053void gru_kservices_exit(void)
27ca8a7b 1054{
d5826dd6
JS
1055 if (gru_free_kernel_contexts())
1056 BUG();
27ca8a7b
JS
1057}
1058