blob: ba6fcd963f3054cc917f92ebf45f87db54b465bd [file] [log] [blame]
Jack Steiner28bffaf2008-07-29 22:33:57 -07001/*
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>
Jack Steiner836ce672009-06-17 16:28:22 -070034#include <linux/delay.h>
Jack Steiner28bffaf2008-07-29 22:33:57 -070035#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 *
Jack Steiner836ce672009-06-17 16:28:22 -070049 * 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!!!.
Jack Steiner9120dec2009-06-17 16:28:25 -070055 *
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 *
Jack Steiner28bffaf2008-07-29 22:33:57 -070095 */
Jack Steiner9120dec2009-06-17 16:28:25 -070096
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)]
101
Jack Steiner6f2584f2009-04-02 16:59:10 -0700102#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -0700103#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -0700104#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
105 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700106
107/* GRU instruction attributes for all instructions */
108#define IMA IMA_CB_DELAY
109
110/* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
111#define __gru_cacheline_aligned__ \
112 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
113
114#define MAGIC 0x1234567887654321UL
115
116/* Default retry count for GRU errors on kernel instructions */
117#define EXCEPTION_RETRY_LIMIT 3
118
119/* Status of message queue sections */
120#define MQS_EMPTY 0
121#define MQS_FULL 1
122#define MQS_NOOP 2
123
124/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
125/* optimized for x86_64 */
126struct message_queue {
127 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
128 int qlines; /* DW 1 */
129 long hstatus[2];
130 void *next __gru_cacheline_aligned__;/* CL 1 */
131 void *limit;
132 void *start;
133 void *start2;
134 char data ____cacheline_aligned; /* CL 2 */
135};
136
137/* First word in every message - used by mesq interface */
138struct message_header {
139 char present;
140 char present2;
141 char lines;
142 char fill;
143};
144
Jack Steiner28bffaf2008-07-29 22:33:57 -0700145#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
146
Jack Steiner836ce672009-06-17 16:28:22 -0700147/*
Jack Steiner836ce672009-06-17 16:28:22 -0700148 * Reload the blade's kernel context into a GRU chiplet. Called holding
149 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
150 */
151static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
152{
153 struct gru_state *gru;
154 struct gru_thread_state *kgts;
155 void *vaddr;
Jack Steiner9120dec2009-06-17 16:28:25 -0700156 int ctxnum, ncpus;
Jack Steiner836ce672009-06-17 16:28:22 -0700157
158 up_read(&bs->bs_kgts_sema);
159 down_write(&bs->bs_kgts_sema);
160
161 if (!bs->bs_kgts)
Jack Steiner9120dec2009-06-17 16:28:25 -0700162 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
Jack Steiner836ce672009-06-17 16:28:22 -0700163 kgts = bs->bs_kgts;
164
165 if (!kgts->ts_gru) {
166 STAT(load_kernel_context);
Jack Steiner9120dec2009-06-17 16:28:25 -0700167 ncpus = uv_blade_nr_possible_cpus(blade_id);
168 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
169 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
170 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
171 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
172 bs->bs_async_dsr_bytes);
Jack Steiner836ce672009-06-17 16:28:22 -0700173 while (!gru_assign_gru_context(kgts, blade_id)) {
174 msleep(1);
175 gru_steal_context(kgts, blade_id);
176 }
177 gru_load_context(kgts);
178 gru = bs->bs_kgts->ts_gru;
179 vaddr = gru->gs_gru_base_vaddr;
180 ctxnum = kgts->ts_ctxnum;
181 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
182 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
183 }
184 downgrade_write(&bs->bs_kgts_sema);
185}
186
187/*
188 * Lock & load the kernel context for the specified blade.
189 */
190static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
191{
192 struct gru_blade_state *bs;
193
194 STAT(lock_kernel_context);
195 bs = gru_base[blade_id];
196
197 down_read(&bs->bs_kgts_sema);
198 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
199 gru_load_kernel_context(bs, blade_id);
200 return bs;
201
202}
203
204/*
205 * Unlock the kernel context for the specified blade. Context is not
206 * unloaded but may be stolen before next use.
207 */
208static void gru_unlock_kernel_context(int blade_id)
209{
210 struct gru_blade_state *bs;
211
212 bs = gru_base[blade_id];
213 up_read(&bs->bs_kgts_sema);
214 STAT(unlock_kernel_context);
215}
216
217/*
218 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
219 * - returns with preemption disabled
220 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700221static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
222{
223 struct gru_blade_state *bs;
224 int lcpu;
225
226 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
227 preempt_disable();
Jack Steiner836ce672009-06-17 16:28:22 -0700228 bs = gru_lock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700229 lcpu = uv_blade_processor_id();
230 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
231 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
232 return 0;
233}
234
Jack Steiner836ce672009-06-17 16:28:22 -0700235/*
236 * Free the current cpus reserved DSR/CBR resources.
237 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700238static void gru_free_cpu_resources(void *cb, void *dsr)
239{
Jack Steiner836ce672009-06-17 16:28:22 -0700240 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700241 preempt_enable();
242}
243
Jack Steiner9120dec2009-06-17 16:28:25 -0700244/*
245 * Reserve GRU resources to be used asynchronously.
246 * Note: currently supports only 1 reservation per blade.
247 *
248 * input:
249 * blade_id - blade on which resources should be reserved
250 * cbrs - number of CBRs
251 * dsr_bytes - number of DSR bytes needed
252 * output:
253 * handle to identify resource
254 * (0 = async resources already reserved)
255 */
256unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
257 struct completion *cmp)
258{
259 struct gru_blade_state *bs;
260 struct gru_thread_state *kgts;
261 int ret = 0;
262
263 bs = gru_base[blade_id];
264
265 down_write(&bs->bs_kgts_sema);
266
267 /* Verify no resources already reserved */
268 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
269 goto done;
270 bs->bs_async_dsr_bytes = dsr_bytes;
271 bs->bs_async_cbrs = cbrs;
272 bs->bs_async_wq = cmp;
273 kgts = bs->bs_kgts;
274
275 /* Resources changed. Unload context if already loaded */
276 if (kgts && kgts->ts_gru)
277 gru_unload_context(kgts, 0);
278 ret = ASYNC_BID_TO_HAN(blade_id);
279
280done:
281 up_write(&bs->bs_kgts_sema);
282 return ret;
283}
284
285/*
286 * Release async resources previously reserved.
287 *
288 * input:
289 * han - handle to identify resources
290 */
291void gru_release_async_resources(unsigned long han)
292{
293 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
294
295 down_write(&bs->bs_kgts_sema);
296 bs->bs_async_dsr_bytes = 0;
297 bs->bs_async_cbrs = 0;
298 bs->bs_async_wq = NULL;
299 up_write(&bs->bs_kgts_sema);
300}
301
302/*
303 * Wait for async GRU instructions to complete.
304 *
305 * input:
306 * han - handle to identify resources
307 */
308void gru_wait_async_cbr(unsigned long han)
309{
310 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
311
312 wait_for_completion(bs->bs_async_wq);
313 mb();
314}
315
316/*
317 * Lock previous reserved async GRU resources
318 *
319 * input:
320 * han - handle to identify resources
321 * output:
322 * cb - pointer to first CBR
323 * dsr - pointer to first DSR
324 */
325void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
326{
327 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
328 int blade_id = ASYNC_HAN_TO_BID(han);
329 int ncpus;
330
331 gru_lock_kernel_context(blade_id);
332 ncpus = uv_blade_nr_possible_cpus(blade_id);
333 if (cb)
334 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
335 if (dsr)
336 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
337}
338
339/*
340 * Unlock previous reserved async GRU resources
341 *
342 * input:
343 * han - handle to identify resources
344 */
345void gru_unlock_async_resource(unsigned long han)
346{
347 int blade_id = ASYNC_HAN_TO_BID(han);
348
349 gru_unlock_kernel_context(blade_id);
350}
351
Jack Steiner836ce672009-06-17 16:28:22 -0700352/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700353int gru_get_cb_exception_detail(void *cb,
354 struct control_block_extended_exc_detail *excdet)
355{
356 struct gru_control_block_extended *cbe;
357
358 cbe = get_cbe(GRUBASE(cb), get_cb_number(cb));
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700359 prefetchw(cbe); /* Harmless on hardware, required for emulator */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700360 excdet->opc = cbe->opccpy;
361 excdet->exopc = cbe->exopccpy;
362 excdet->ecause = cbe->ecause;
363 excdet->exceptdet0 = cbe->idef1upd;
364 excdet->exceptdet1 = cbe->idef3upd;
365 return 0;
366}
367
368char *gru_get_cb_exception_detail_str(int ret, void *cb,
369 char *buf, int size)
370{
371 struct gru_control_block_status *gen = (void *)cb;
372 struct control_block_extended_exc_detail excdet;
373
374 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
375 gru_get_cb_exception_detail(cb, &excdet);
376 snprintf(buf, size,
377 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
378 "excdet0 0x%lx, excdet1 0x%x",
379 gen, excdet.opc, excdet.exopc, excdet.ecause,
380 excdet.exceptdet0, excdet.exceptdet1);
381 } else {
382 snprintf(buf, size, "No exception");
383 }
384 return buf;
385}
386
387static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
388{
389 while (gen->istatus >= CBS_ACTIVE) {
390 cpu_relax();
391 barrier();
392 }
393 return gen->istatus;
394}
395
396static int gru_retry_exception(void *cb)
397{
398 struct gru_control_block_status *gen = (void *)cb;
399 struct control_block_extended_exc_detail excdet;
400 int retry = EXCEPTION_RETRY_LIMIT;
401
402 while (1) {
403 if (gru_get_cb_message_queue_substatus(cb))
404 break;
405 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
406 return CBS_IDLE;
407
408 gru_get_cb_exception_detail(cb, &excdet);
Jack Steiner270952a2009-06-17 16:28:27 -0700409 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
410 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
Jack Steiner28bffaf2008-07-29 22:33:57 -0700411 break;
412 if (retry-- == 0)
413 break;
414 gen->icmd = 1;
415 gru_flush_cache(gen);
416 }
417 return CBS_EXCEPTION;
418}
419
420int gru_check_status_proc(void *cb)
421{
422 struct gru_control_block_status *gen = (void *)cb;
423 int ret;
424
425 ret = gen->istatus;
426 if (ret != CBS_EXCEPTION)
427 return ret;
428 return gru_retry_exception(cb);
429
430}
431
432int gru_wait_proc(void *cb)
433{
434 struct gru_control_block_status *gen = (void *)cb;
435 int ret;
436
437 ret = gru_wait_idle_or_exception(gen);
438 if (ret == CBS_EXCEPTION)
439 ret = gru_retry_exception(cb);
440
441 return ret;
442}
443
444void gru_abort(int ret, void *cb, char *str)
445{
446 char buf[GRU_EXC_STR_SIZE];
447
448 panic("GRU FATAL ERROR: %s - %s\n", str,
449 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
450}
451
452void gru_wait_abort_proc(void *cb)
453{
454 int ret;
455
456 ret = gru_wait_proc(cb);
457 if (ret)
458 gru_abort(ret, cb, "gru_wait_abort");
459}
460
461
462/*------------------------------ MESSAGE QUEUES -----------------------------*/
463
464/* Internal status . These are NOT returned to the user. */
465#define MQIE_AGAIN -1 /* try again */
466
467
468/*
469 * Save/restore the "present" flag that is in the second line of 2-line
470 * messages
471 */
472static inline int get_present2(void *p)
473{
474 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
475 return mhdr->present;
476}
477
478static inline void restore_present2(void *p, int val)
479{
480 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
481 mhdr->present = val;
482}
483
484/*
485 * Create a message queue.
486 * qlines - message queue size in cache lines. Includes 2-line header.
487 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700488int gru_create_message_queue(struct gru_message_queue_desc *mqd,
489 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700490{
491 struct message_queue *mq = p;
492 unsigned int qlines;
493
494 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
495 memset(mq, 0, bytes);
496 mq->start = &mq->data;
497 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
498 mq->next = &mq->data;
499 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
500 mq->qlines = qlines;
501 mq->hstatus[0] = 0;
502 mq->hstatus[1] = 1;
503 mq->head = gru_mesq_head(2, qlines / 2 + 1);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700504 mqd->mq = mq;
505 mqd->mq_gpa = uv_gpa(mq);
506 mqd->qlines = qlines;
507 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
508 mqd->interrupt_vector = vector;
509 mqd->interrupt_apicid = apicid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700510 return 0;
511}
512EXPORT_SYMBOL_GPL(gru_create_message_queue);
513
514/*
515 * Send a NOOP message to a message queue
516 * Returns:
517 * 0 - if queue is full after the send. This is the normal case
518 * but various races can change this.
519 * -1 - if mesq sent successfully but queue not full
520 * >0 - unexpected error. MQE_xxx returned
521 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700522static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
523 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700524{
525 const struct message_header noop_header = {
526 .present = MQS_NOOP, .lines = 1};
527 unsigned long m;
528 int substatus, ret;
529 struct message_header save_mhdr, *mhdr = mesg;
530
531 STAT(mesq_noop);
532 save_mhdr = *mhdr;
533 *mhdr = noop_header;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700534 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700535 ret = gru_wait(cb);
536
537 if (ret) {
538 substatus = gru_get_cb_message_queue_substatus(cb);
539 switch (substatus) {
540 case CBSS_NO_ERROR:
541 STAT(mesq_noop_unexpected_error);
542 ret = MQE_UNEXPECTED_CB_ERR;
543 break;
544 case CBSS_LB_OVERFLOWED:
545 STAT(mesq_noop_lb_overflow);
546 ret = MQE_CONGESTION;
547 break;
548 case CBSS_QLIMIT_REACHED:
549 STAT(mesq_noop_qlimit_reached);
550 ret = 0;
551 break;
552 case CBSS_AMO_NACKED:
553 STAT(mesq_noop_amo_nacked);
554 ret = MQE_CONGESTION;
555 break;
556 case CBSS_PUT_NACKED:
557 STAT(mesq_noop_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700558 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700559 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
560 IMA);
561 if (gru_wait(cb) == CBS_IDLE)
562 ret = MQIE_AGAIN;
563 else
564 ret = MQE_UNEXPECTED_CB_ERR;
565 break;
566 case CBSS_PAGE_OVERFLOW:
567 default:
568 BUG();
569 }
570 }
571 *mhdr = save_mhdr;
572 return ret;
573}
574
575/*
576 * Handle a gru_mesq full.
577 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700578static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
579 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700580{
581 union gru_mesqhead mqh;
582 unsigned int limit, head;
583 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700584 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700585
586 /* Determine if switching to first/second half of q */
587 avalue = gru_get_amo_value(cb);
588 head = gru_get_amo_value_head(cb);
589 limit = gru_get_amo_value_limit(cb);
590
Jack Steiner6f2584f2009-04-02 16:59:10 -0700591 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700592 half = (limit != qlines);
593
594 if (half)
595 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
596 else
597 mqh = gru_mesq_head(2, qlines / 2 + 1);
598
599 /* Try to get lock for switching head pointer */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700600 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700601 if (gru_wait(cb) != CBS_IDLE)
602 goto cberr;
603 if (!gru_get_amo_value(cb)) {
604 STAT(mesq_qf_locked);
605 return MQE_QUEUE_FULL;
606 }
607
608 /* Got the lock. Send optional NOP if queue not full, */
609 if (head != limit) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700610 if (send_noop_message(cb, mqd, mesg)) {
611 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700612 XTYPE_DW, IMA);
613 if (gru_wait(cb) != CBS_IDLE)
614 goto cberr;
615 STAT(mesq_qf_noop_not_full);
616 return MQIE_AGAIN;
617 }
618 avalue++;
619 }
620
621 /* Then flip queuehead to other half of queue. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700622 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
623 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700624 if (gru_wait(cb) != CBS_IDLE)
625 goto cberr;
626
627 /* If not successfully in swapping queue head, clear the hstatus lock */
628 if (gru_get_amo_value(cb) != avalue) {
629 STAT(mesq_qf_switch_head_failed);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700630 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
631 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700632 if (gru_wait(cb) != CBS_IDLE)
633 goto cberr;
634 }
635 return MQIE_AGAIN;
636cberr:
637 STAT(mesq_qf_unexpected_error);
638 return MQE_UNEXPECTED_CB_ERR;
639}
640
Jack Steiner6f2584f2009-04-02 16:59:10 -0700641/*
642 * Send a cross-partition interrupt to the SSI that contains the target
643 * message queue. Normally, the interrupt is automatically delivered by hardware
644 * but some error conditions require explicit delivery.
645 */
646static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
647{
648 if (mqd->interrupt_vector)
649 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
650 mqd->interrupt_vector);
651}
652
Jack Steiner17b49a62009-06-17 16:28:23 -0700653/*
654 * Handle a PUT failure. Note: if message was a 2-line message, one of the
655 * lines might have successfully have been written. Before sending the
656 * message, "present" must be cleared in BOTH lines to prevent the receiver
657 * from prematurely seeing the full message.
658 */
659static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
660 void *mesg, int lines)
661{
662 unsigned long m;
663
664 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
665 if (lines == 2) {
666 gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
667 if (gru_wait(cb) != CBS_IDLE)
668 return MQE_UNEXPECTED_CB_ERR;
669 }
670 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
671 if (gru_wait(cb) != CBS_IDLE)
672 return MQE_UNEXPECTED_CB_ERR;
673 send_message_queue_interrupt(mqd);
674 return MQE_OK;
675}
Jack Steiner28bffaf2008-07-29 22:33:57 -0700676
677/*
678 * Handle a gru_mesq failure. Some of these failures are software recoverable
679 * or retryable.
680 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700681static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
682 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700683{
684 int substatus, ret = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700685
686 substatus = gru_get_cb_message_queue_substatus(cb);
687 switch (substatus) {
688 case CBSS_NO_ERROR:
689 STAT(mesq_send_unexpected_error);
690 ret = MQE_UNEXPECTED_CB_ERR;
691 break;
692 case CBSS_LB_OVERFLOWED:
693 STAT(mesq_send_lb_overflow);
694 ret = MQE_CONGESTION;
695 break;
696 case CBSS_QLIMIT_REACHED:
697 STAT(mesq_send_qlimit_reached);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700698 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700699 break;
700 case CBSS_AMO_NACKED:
701 STAT(mesq_send_amo_nacked);
702 ret = MQE_CONGESTION;
703 break;
704 case CBSS_PUT_NACKED:
705 STAT(mesq_send_put_nacked);
Jack Steiner17b49a62009-06-17 16:28:23 -0700706 ret = send_message_put_nacked(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700707 break;
708 default:
709 BUG();
710 }
711 return ret;
712}
713
714/*
715 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700716 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700717 * mesg message. ust be vaddr within a GSEG
718 * bytes message size (<= 2 CL)
719 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700720int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
721 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700722{
723 struct message_header *mhdr;
724 void *cb;
725 void *dsr;
726 int istatus, clines, ret;
727
728 STAT(mesq_send);
729 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
730
Julia Lawallcbf330b2008-10-15 22:01:27 -0700731 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700732 if (gru_get_cpu_resources(bytes, &cb, &dsr))
733 return MQE_BUG_NO_RESOURCES;
734 memcpy(dsr, mesg, bytes);
735 mhdr = dsr;
736 mhdr->present = MQS_FULL;
737 mhdr->lines = clines;
738 if (clines == 2) {
739 mhdr->present2 = get_present2(mhdr);
740 restore_present2(mhdr, MQS_FULL);
741 }
742
743 do {
744 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700745 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700746 istatus = gru_wait(cb);
747 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700748 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700749 } while (ret == MQIE_AGAIN);
750 gru_free_cpu_resources(cb, dsr);
751
752 if (ret)
753 STAT(mesq_send_failed);
754 return ret;
755}
756EXPORT_SYMBOL_GPL(gru_send_message_gpa);
757
758/*
759 * Advance the receive pointer for the queue to the next message.
760 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700761void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700762{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700763 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700764 struct message_header *mhdr = mq->next;
765 void *next, *pnext;
766 int half = -1;
767 int lines = mhdr->lines;
768
769 if (lines == 2)
770 restore_present2(mhdr, MQS_EMPTY);
771 mhdr->present = MQS_EMPTY;
772
773 pnext = mq->next;
774 next = pnext + GRU_CACHE_LINE_BYTES * lines;
775 if (next == mq->limit) {
776 next = mq->start;
777 half = 1;
778 } else if (pnext < mq->start2 && next >= mq->start2) {
779 half = 0;
780 }
781
782 if (half >= 0)
783 mq->hstatus[half] = 1;
784 mq->next = next;
785}
786EXPORT_SYMBOL_GPL(gru_free_message);
787
788/*
789 * Get next message from message queue. Return NULL if no message
790 * present. User must call next_message() to move to next message.
791 * rmq message queue
792 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700793void *gru_get_next_message(struct gru_message_queue_desc *mqd)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700794{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700795 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700796 struct message_header *mhdr = mq->next;
797 int present = mhdr->present;
798
799 /* skip NOOP messages */
800 STAT(mesq_receive);
801 while (present == MQS_NOOP) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700802 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700803 mhdr = mq->next;
804 present = mhdr->present;
805 }
806
807 /* Wait for both halves of 2 line messages */
808 if (present == MQS_FULL && mhdr->lines == 2 &&
809 get_present2(mhdr) == MQS_EMPTY)
810 present = MQS_EMPTY;
811
812 if (!present) {
813 STAT(mesq_receive_none);
814 return NULL;
815 }
816
817 if (mhdr->lines == 2)
818 restore_present2(mhdr, mhdr->present2);
819
820 return mhdr;
821}
822EXPORT_SYMBOL_GPL(gru_get_next_message);
823
824/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
825
826/*
827 * Copy a block of data using the GRU resources
828 */
829int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
830 unsigned int bytes)
831{
832 void *cb;
833 void *dsr;
834 int ret;
835
836 STAT(copy_gpa);
837 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
838 return MQE_BUG_NO_RESOURCES;
839 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700840 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700841 ret = gru_wait(cb);
842 gru_free_cpu_resources(cb, dsr);
843 return ret;
844}
845EXPORT_SYMBOL_GPL(gru_copy_gpa);
846
847/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
848/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700849
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700850static int quicktest0(unsigned long arg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700851{
Jack Steiner836ce672009-06-17 16:28:22 -0700852 unsigned long word0;
853 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700854 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700855 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700856 unsigned long *p;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700857 int ret = -EIO;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700858
Jack Steiner836ce672009-06-17 16:28:22 -0700859 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
860 return MQE_BUG_NO_RESOURCES;
861 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700862 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700863 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700864
Jack Steiner836ce672009-06-17 16:28:22 -0700865 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700866 if (gru_wait(cb) != CBS_IDLE) {
867 printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
868 goto done;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700869 }
870
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700871 if (*p != MAGIC) {
872 printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
873 goto done;
874 }
875 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
876 if (gru_wait(cb) != CBS_IDLE) {
877 printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
878 goto done;
879 }
880
881 if (word0 != word1 || word1 != MAGIC) {
882 printk(KERN_DEBUG
883 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
884 word1, MAGIC);
885 goto done;
886 }
887 ret = 0;
888
889done:
890 gru_free_cpu_resources(cb, dsr);
891 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700892}
893
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700894#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
895
896static int quicktest1(unsigned long arg)
897{
898 struct gru_message_queue_desc mqd;
899 void *p, *mq;
900 unsigned long *dw;
901 int i, ret = -EIO;
902 char mes[GRU_CACHE_LINE_BYTES], *m;
903
904 /* Need 1K cacheline aligned that does not cross page boundary */
905 p = kmalloc(4096, 0);
906 mq = ALIGNUP(p, 1024);
907 memset(mes, 0xee, sizeof(mes));
908 dw = mq;
909
910 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
911 for (i = 0; i < 6; i++) {
912 mes[8] = i;
913 do {
914 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
915 } while (ret == MQE_CONGESTION);
916 if (ret)
917 break;
918 }
919 if (ret != MQE_QUEUE_FULL || i != 4)
920 goto done;
921
922 for (i = 0; i < 6; i++) {
923 m = gru_get_next_message(&mqd);
924 if (!m || m[8] != i)
925 break;
926 gru_free_message(&mqd, m);
927 }
928 ret = (i == 4) ? 0 : -EIO;
929
930done:
931 kfree(p);
932 return ret;
933}
934
935static int quicktest2(unsigned long arg)
936{
937 static DECLARE_COMPLETION(cmp);
938 unsigned long han;
939 int blade_id = 0;
940 int numcb = 4;
941 int ret = 0;
942 unsigned long *buf;
943 void *cb0, *cb;
944 int i, k, istatus, bytes;
945
946 bytes = numcb * 4 * 8;
947 buf = kmalloc(bytes, GFP_KERNEL);
948 if (!buf)
949 return -ENOMEM;
950
951 ret = -EBUSY;
952 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
953 if (!han)
954 goto done;
955
956 gru_lock_async_resource(han, &cb0, NULL);
957 memset(buf, 0xee, bytes);
958 for (i = 0; i < numcb; i++)
959 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
960 XTYPE_DW, 4, 1, IMA_INTERRUPT);
961
962 ret = 0;
963 for (k = 0; k < numcb; k++) {
964 gru_wait_async_cbr(han);
965 for (i = 0; i < numcb; i++) {
966 cb = cb0 + i * GRU_HANDLE_STRIDE;
967 istatus = gru_check_status(cb);
968 if (istatus == CBS_ACTIVE)
969 continue;
970 if (istatus == CBS_EXCEPTION)
971 ret = -EFAULT;
972 else if (buf[i] || buf[i + 1] || buf[i + 2] ||
973 buf[i + 3])
974 ret = -EIO;
975 }
976 }
977 BUG_ON(cmp.done);
978
979 gru_unlock_async_resource(han);
980 gru_release_async_resources(han);
981done:
982 kfree(buf);
983 return ret;
984}
985
986/*
987 * Debugging only. User hook for various kernel tests
988 * of driver & gru.
989 */
990int gru_ktest(unsigned long arg)
991{
992 int ret = -EINVAL;
993
994 switch (arg & 0xff) {
995 case 0:
996 ret = quicktest0(arg);
997 break;
998 case 1:
999 ret = quicktest1(arg);
1000 break;
1001 case 2:
1002 ret = quicktest2(arg);
1003 break;
1004 }
1005 return ret;
1006
1007}
Jack Steiner28bffaf2008-07-29 22:33:57 -07001008
1009int gru_kservices_init(struct gru_state *gru)
1010{
1011 struct gru_blade_state *bs;
Jack Steiner28bffaf2008-07-29 22:33:57 -07001012
Jack Steiner28bffaf2008-07-29 22:33:57 -07001013 bs = gru->gs_blade;
Jack Steiner836ce672009-06-17 16:28:22 -07001014 if (gru != &bs->bs_grus[0])
Jack Steiner28bffaf2008-07-29 22:33:57 -07001015 return 0;
1016
Jack Steiner836ce672009-06-17 16:28:22 -07001017 init_rwsem(&bs->bs_kgts_sema);
Jack Steiner28bffaf2008-07-29 22:33:57 -07001018 return 0;
1019}
Jack Steiner27ca8a72009-04-02 16:59:11 -07001020
1021void gru_kservices_exit(struct gru_state *gru)
1022{
Jack Steiner27ca8a72009-04-02 16:59:11 -07001023 struct gru_blade_state *bs;
Jack Steiner836ce672009-06-17 16:28:22 -07001024 struct gru_thread_state *kgts;
Jack Steiner27ca8a72009-04-02 16:59:11 -07001025
1026 bs = gru->gs_blade;
Jack Steiner836ce672009-06-17 16:28:22 -07001027 if (gru != &bs->bs_grus[0])
Jack Steiner27ca8a72009-04-02 16:59:11 -07001028 return;
1029
Jack Steiner836ce672009-06-17 16:28:22 -07001030 kgts = bs->bs_kgts;
1031 if (kgts && kgts->ts_gru)
1032 gru_unload_context(kgts, 0);
1033 kfree(kgts);
Jack Steiner27ca8a72009-04-02 16:59:11 -07001034}
1035