blob: 306855cc80fe4dece5f8a9179a9a1f28a25cc789 [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>
Jack Steiner28bffaf2008-07-29 22:33:57 -070027#include <linux/spinlock.h>
28#include <linux/device.h>
29#include <linux/miscdevice.h>
30#include <linux/proc_fs.h>
31#include <linux/interrupt.h>
32#include <linux/uaccess.h>
Jack Steiner836ce672009-06-17 16:28:22 -070033#include <linux/delay.h>
Jack Steiner28bffaf2008-07-29 22:33:57 -070034#include "gru.h"
35#include "grulib.h"
36#include "grutables.h"
37#include "grukservices.h"
38#include "gru_instructions.h"
39#include <asm/uv/uv_hub.h>
40
41/*
42 * Kernel GRU Usage
43 *
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.
47 *
Jack Steiner836ce672009-06-17 16:28:22 -070048 * Blade percpu resources reserved for kernel use. These resources are
49 * reserved whenever the the kernel context for the blade is loaded. Note
50 * that the kernel context is not guaranteed to be always available. It is
51 * loaded on demand & can be stolen by a user if the user demand exceeds the
52 * kernel demand. The kernel can always reload the kernel context but
53 * a SLEEP may be required!!!.
Jack Steiner9120dec2009-06-17 16:28:25 -070054 *
55 * Async Overview:
56 *
57 * Each blade has one "kernel context" that owns GRU kernel resources
58 * located on the blade. Kernel drivers use GRU resources in this context
59 * for sending messages, zeroing memory, etc.
60 *
61 * The kernel context is dynamically loaded on demand. If it is not in
62 * use by the kernel, the kernel context can be unloaded & given to a user.
63 * The kernel context will be reloaded when needed. This may require that
64 * a context be stolen from a user.
65 * NOTE: frequent unloading/reloading of the kernel context is
66 * expensive. We are depending on batch schedulers, cpusets, sane
67 * drivers or some other mechanism to prevent the need for frequent
68 * stealing/reloading.
69 *
70 * The kernel context consists of two parts:
71 * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
72 * Each cpu has it's own private resources & does not share them
73 * with other cpus. These resources are used serially, ie,
74 * locked, used & unlocked on each call to a function in
75 * grukservices.
76 * (Now that we have dynamic loading of kernel contexts, I
77 * may rethink this & allow sharing between cpus....)
78 *
79 * - Additional resources can be reserved long term & used directly
80 * by UV drivers located in the kernel. Drivers using these GRU
81 * resources can use asynchronous GRU instructions that send
82 * interrupts on completion.
83 * - these resources must be explicitly locked/unlocked
84 * - locked resources prevent (obviously) the kernel
85 * context from being unloaded.
86 * - drivers using these resource directly issue their own
87 * GRU instruction and must wait/check completion.
88 *
89 * When these resources are reserved, the caller can optionally
90 * associate a wait_queue with the resources and use asynchronous
91 * GRU instructions. When an async GRU instruction completes, the
92 * driver will do a wakeup on the event.
93 *
Jack Steiner28bffaf2008-07-29 22:33:57 -070094 */
Jack Steiner9120dec2009-06-17 16:28:25 -070095
96
97#define ASYNC_HAN_TO_BID(h) ((h) - 1)
98#define ASYNC_BID_TO_HAN(b) ((b) + 1)
99#define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700100#define KCB_TO_GID(cb) ((cb - gru_start_vaddr) / \
101 (GRU_SIZE * GRU_CHIPLETS_PER_BLADE))
102#define KCB_TO_BS(cb) gru_base[KCB_TO_GID(cb)]
Jack Steiner9120dec2009-06-17 16:28:25 -0700103
Jack Steiner6f2584f2009-04-02 16:59:10 -0700104#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -0700105#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -0700106#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
107 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700108
109/* GRU instruction attributes for all instructions */
110#define IMA IMA_CB_DELAY
111
112/* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
113#define __gru_cacheline_aligned__ \
114 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
115
116#define MAGIC 0x1234567887654321UL
117
118/* Default retry count for GRU errors on kernel instructions */
119#define EXCEPTION_RETRY_LIMIT 3
120
121/* Status of message queue sections */
122#define MQS_EMPTY 0
123#define MQS_FULL 1
124#define MQS_NOOP 2
125
126/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
127/* optimized for x86_64 */
128struct message_queue {
129 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
130 int qlines; /* DW 1 */
131 long hstatus[2];
132 void *next __gru_cacheline_aligned__;/* CL 1 */
133 void *limit;
134 void *start;
135 void *start2;
136 char data ____cacheline_aligned; /* CL 2 */
137};
138
139/* First word in every message - used by mesq interface */
140struct message_header {
141 char present;
142 char present2;
143 char lines;
144 char fill;
145};
146
Jack Steiner28bffaf2008-07-29 22:33:57 -0700147#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
148
Jack Steiner836ce672009-06-17 16:28:22 -0700149/*
Jack Steiner836ce672009-06-17 16:28:22 -0700150 * Reload the blade's kernel context into a GRU chiplet. Called holding
151 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
152 */
153static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
154{
155 struct gru_state *gru;
156 struct gru_thread_state *kgts;
157 void *vaddr;
Jack Steiner9120dec2009-06-17 16:28:25 -0700158 int ctxnum, ncpus;
Jack Steiner836ce672009-06-17 16:28:22 -0700159
160 up_read(&bs->bs_kgts_sema);
161 down_write(&bs->bs_kgts_sema);
162
163 if (!bs->bs_kgts)
Jack Steiner9120dec2009-06-17 16:28:25 -0700164 bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
Jack Steiner836ce672009-06-17 16:28:22 -0700165 kgts = bs->bs_kgts;
166
167 if (!kgts->ts_gru) {
168 STAT(load_kernel_context);
Jack Steiner9120dec2009-06-17 16:28:25 -0700169 ncpus = uv_blade_nr_possible_cpus(blade_id);
170 kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
171 GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
172 kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
173 GRU_NUM_KERNEL_DSR_BYTES * ncpus +
174 bs->bs_async_dsr_bytes);
Jack Steiner836ce672009-06-17 16:28:22 -0700175 while (!gru_assign_gru_context(kgts, blade_id)) {
176 msleep(1);
177 gru_steal_context(kgts, blade_id);
178 }
179 gru_load_context(kgts);
180 gru = bs->bs_kgts->ts_gru;
181 vaddr = gru->gs_gru_base_vaddr;
182 ctxnum = kgts->ts_ctxnum;
183 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
184 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
185 }
186 downgrade_write(&bs->bs_kgts_sema);
187}
188
189/*
Jack Steinerd5826dd2009-06-17 16:28:28 -0700190 * Free all kernel contexts that are not currently in use.
191 * Returns 0 if all freed, else number of inuse context.
192 */
193static int gru_free_kernel_contexts(void)
194{
195 struct gru_blade_state *bs;
196 struct gru_thread_state *kgts;
197 int bid, ret = 0;
198
199 for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
200 bs = gru_base[bid];
201 if (!bs)
202 continue;
Jack Steiner091f1a12009-12-15 16:48:02 -0800203
204 /* Ignore busy contexts. Don't want to block here. */
Jack Steinerd5826dd2009-06-17 16:28:28 -0700205 if (down_write_trylock(&bs->bs_kgts_sema)) {
206 kgts = bs->bs_kgts;
207 if (kgts && kgts->ts_gru)
208 gru_unload_context(kgts, 0);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700209 bs->bs_kgts = NULL;
210 up_write(&bs->bs_kgts_sema);
Jack Steiner091f1a12009-12-15 16:48:02 -0800211 kfree(kgts);
Jack Steinerd5826dd2009-06-17 16:28:28 -0700212 } else {
213 ret++;
214 }
215 }
216 return ret;
217}
218
219/*
Jack Steiner836ce672009-06-17 16:28:22 -0700220 * Lock & load the kernel context for the specified blade.
221 */
222static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
223{
224 struct gru_blade_state *bs;
225
226 STAT(lock_kernel_context);
227 bs = gru_base[blade_id];
228
229 down_read(&bs->bs_kgts_sema);
230 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
231 gru_load_kernel_context(bs, blade_id);
232 return bs;
233
234}
235
236/*
237 * Unlock the kernel context for the specified blade. Context is not
238 * unloaded but may be stolen before next use.
239 */
240static void gru_unlock_kernel_context(int blade_id)
241{
242 struct gru_blade_state *bs;
243
244 bs = gru_base[blade_id];
245 up_read(&bs->bs_kgts_sema);
246 STAT(unlock_kernel_context);
247}
248
249/*
250 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
251 * - returns with preemption disabled
252 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700253static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
254{
255 struct gru_blade_state *bs;
256 int lcpu;
257
258 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
259 preempt_disable();
Jack Steiner836ce672009-06-17 16:28:22 -0700260 bs = gru_lock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700261 lcpu = uv_blade_processor_id();
262 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
263 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
264 return 0;
265}
266
Jack Steiner836ce672009-06-17 16:28:22 -0700267/*
268 * Free the current cpus reserved DSR/CBR resources.
269 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700270static void gru_free_cpu_resources(void *cb, void *dsr)
271{
Jack Steiner836ce672009-06-17 16:28:22 -0700272 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700273 preempt_enable();
274}
275
Jack Steiner9120dec2009-06-17 16:28:25 -0700276/*
277 * Reserve GRU resources to be used asynchronously.
278 * Note: currently supports only 1 reservation per blade.
279 *
280 * input:
281 * blade_id - blade on which resources should be reserved
282 * cbrs - number of CBRs
283 * dsr_bytes - number of DSR bytes needed
284 * output:
285 * handle to identify resource
286 * (0 = async resources already reserved)
287 */
288unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
289 struct completion *cmp)
290{
291 struct gru_blade_state *bs;
292 struct gru_thread_state *kgts;
293 int ret = 0;
294
295 bs = gru_base[blade_id];
296
297 down_write(&bs->bs_kgts_sema);
298
299 /* Verify no resources already reserved */
300 if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
301 goto done;
302 bs->bs_async_dsr_bytes = dsr_bytes;
303 bs->bs_async_cbrs = cbrs;
304 bs->bs_async_wq = cmp;
305 kgts = bs->bs_kgts;
306
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);
311
312done:
313 up_write(&bs->bs_kgts_sema);
314 return ret;
315}
316
317/*
318 * Release async resources previously reserved.
319 *
320 * input:
321 * han - handle to identify resources
322 */
323void gru_release_async_resources(unsigned long han)
324{
325 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
326
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);
332}
333
334/*
335 * Wait for async GRU instructions to complete.
336 *
337 * input:
338 * han - handle to identify resources
339 */
340void gru_wait_async_cbr(unsigned long han)
341{
342 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
343
344 wait_for_completion(bs->bs_async_wq);
345 mb();
346}
347
348/*
349 * Lock previous reserved async GRU resources
350 *
351 * input:
352 * han - handle to identify resources
353 * output:
354 * cb - pointer to first CBR
355 * dsr - pointer to first DSR
356 */
357void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
358{
359 struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
360 int blade_id = ASYNC_HAN_TO_BID(han);
361 int ncpus;
362
363 gru_lock_kernel_context(blade_id);
364 ncpus = uv_blade_nr_possible_cpus(blade_id);
365 if (cb)
366 *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
367 if (dsr)
368 *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
369}
370
371/*
372 * Unlock previous reserved async GRU resources
373 *
374 * input:
375 * han - handle to identify resources
376 */
377void gru_unlock_async_resource(unsigned long han)
378{
379 int blade_id = ASYNC_HAN_TO_BID(han);
380
381 gru_unlock_kernel_context(blade_id);
382}
383
Jack Steiner836ce672009-06-17 16:28:22 -0700384/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700385int gru_get_cb_exception_detail(void *cb,
386 struct control_block_extended_exc_detail *excdet)
387{
388 struct gru_control_block_extended *cbe;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700389 struct gru_blade_state *bs;
390 int cbrnum;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700391
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700392 bs = KCB_TO_BS(cb);
393 cbrnum = thread_cbr_number(bs->bs_kgts, get_cb_number(cb));
394 cbe = get_cbe(GRUBASE(cb), cbrnum);
395 gru_flush_cache(cbe); /* CBE not coherent */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700396 excdet->opc = cbe->opccpy;
397 excdet->exopc = cbe->exopccpy;
398 excdet->ecause = cbe->ecause;
399 excdet->exceptdet0 = cbe->idef1upd;
400 excdet->exceptdet1 = cbe->idef3upd;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700401 gru_flush_cache(cbe);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700402 return 0;
403}
404
405char *gru_get_cb_exception_detail_str(int ret, void *cb,
406 char *buf, int size)
407{
408 struct gru_control_block_status *gen = (void *)cb;
409 struct control_block_extended_exc_detail excdet;
410
411 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
412 gru_get_cb_exception_detail(cb, &excdet);
413 snprintf(buf, size,
414 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
415 "excdet0 0x%lx, excdet1 0x%x",
416 gen, excdet.opc, excdet.exopc, excdet.ecause,
417 excdet.exceptdet0, excdet.exceptdet1);
418 } else {
419 snprintf(buf, size, "No exception");
420 }
421 return buf;
422}
423
424static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
425{
426 while (gen->istatus >= CBS_ACTIVE) {
427 cpu_relax();
428 barrier();
429 }
430 return gen->istatus;
431}
432
433static int gru_retry_exception(void *cb)
434{
435 struct gru_control_block_status *gen = (void *)cb;
436 struct control_block_extended_exc_detail excdet;
437 int retry = EXCEPTION_RETRY_LIMIT;
438
439 while (1) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700440 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
441 return CBS_IDLE;
Jack Steinerd6e2fbc2009-06-17 16:28:29 -0700442 if (gru_get_cb_message_queue_substatus(cb))
443 return CBS_EXCEPTION;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700444 gru_get_cb_exception_detail(cb, &excdet);
Jack Steiner270952a2009-06-17 16:28:27 -0700445 if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
446 (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
Jack Steiner28bffaf2008-07-29 22:33:57 -0700447 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700524int gru_create_message_queue(struct gru_message_queue_desc *mqd,
525 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700526{
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);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700540 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;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700546 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700558static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
559 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700560{
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;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700570 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700571 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);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700594 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700595 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700614static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
615 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700616{
617 union gru_mesqhead mqh;
618 unsigned int limit, head;
619 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700620 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700621
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
Jack Steiner6f2584f2009-04-02 16:59:10 -0700627 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700628 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700636 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700637 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) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700646 if (send_noop_message(cb, mqd, mesg)) {
647 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700648 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. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700658 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
659 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700660 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);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700666 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
667 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700668 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
Jack Steiner6f2584f2009-04-02 16:59:10 -0700677/*
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
Jack Steiner17b49a62009-06-17 16:28:23 -0700689/*
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}
Jack Steiner28bffaf2008-07-29 22:33:57 -0700712
713/*
714 * Handle a gru_mesq failure. Some of these failures are software recoverable
715 * or retryable.
716 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700717static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
718 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700719{
720 int substatus, ret = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700721
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);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700734 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700735 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);
Jack Steiner17b49a62009-06-17 16:28:23 -0700742 ret = send_message_put_nacked(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700743 break;
744 default:
745 BUG();
746 }
747 return ret;
748}
749
750/*
751 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700752 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700753 * mesg message. ust be vaddr within a GSEG
754 * bytes message size (<= 2 CL)
755 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700756int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
757 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700758{
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
Julia Lawallcbf330b2008-10-15 22:01:27 -0700767 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700768 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;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700781 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700782 istatus = gru_wait(cb);
783 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700784 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700785 } 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700797void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700798{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700799 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700800 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 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700829void *gru_get_next_message(struct gru_message_queue_desc *mqd)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700830{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700831 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700832 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) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700838 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700839 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/*
Robin Holt289750d2009-12-15 16:47:55 -0800863 * Load a DW from a global GPA. The GPA can be a memory or MMR address.
864 */
865int gru_read_gpa(unsigned long *value, unsigned long gpa)
866{
867 void *cb;
868 void *dsr;
869 int ret, iaa;
870
871 STAT(read_gpa);
872 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
873 return MQE_BUG_NO_RESOURCES;
874 iaa = gpa >> 62;
875 gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
876 ret = gru_wait(cb);
877 if (ret == CBS_IDLE)
878 *value = *(unsigned long *)dsr;
879 gru_free_cpu_resources(cb, dsr);
880 return ret;
881}
882EXPORT_SYMBOL_GPL(gru_read_gpa);
883
884
885/*
Jack Steiner28bffaf2008-07-29 22:33:57 -0700886 * Copy a block of data using the GRU resources
887 */
888int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
889 unsigned int bytes)
890{
891 void *cb;
892 void *dsr;
893 int ret;
894
895 STAT(copy_gpa);
896 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
897 return MQE_BUG_NO_RESOURCES;
898 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700899 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700900 ret = gru_wait(cb);
901 gru_free_cpu_resources(cb, dsr);
902 return ret;
903}
904EXPORT_SYMBOL_GPL(gru_copy_gpa);
905
906/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
907/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700908
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700909static int quicktest0(unsigned long arg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700910{
Jack Steiner836ce672009-06-17 16:28:22 -0700911 unsigned long word0;
912 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700913 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700914 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700915 unsigned long *p;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700916 int ret = -EIO;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700917
Jack Steiner836ce672009-06-17 16:28:22 -0700918 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
919 return MQE_BUG_NO_RESOURCES;
920 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700921 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700922 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700923
Jack Steiner836ce672009-06-17 16:28:22 -0700924 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700925 if (gru_wait(cb) != CBS_IDLE) {
926 printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
927 goto done;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700928 }
929
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700930 if (*p != MAGIC) {
931 printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
932 goto done;
933 }
934 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
935 if (gru_wait(cb) != CBS_IDLE) {
936 printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
937 goto done;
938 }
939
940 if (word0 != word1 || word1 != MAGIC) {
941 printk(KERN_DEBUG
942 "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
943 word1, MAGIC);
944 goto done;
945 }
946 ret = 0;
947
948done:
949 gru_free_cpu_resources(cb, dsr);
950 return ret;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700951}
952
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700953#define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
954
955static int quicktest1(unsigned long arg)
956{
957 struct gru_message_queue_desc mqd;
958 void *p, *mq;
959 unsigned long *dw;
960 int i, ret = -EIO;
961 char mes[GRU_CACHE_LINE_BYTES], *m;
962
963 /* Need 1K cacheline aligned that does not cross page boundary */
964 p = kmalloc(4096, 0);
Roel Kluin9e5f1132009-09-23 15:57:33 -0700965 if (p == NULL)
966 return -ENOMEM;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700967 mq = ALIGNUP(p, 1024);
968 memset(mes, 0xee, sizeof(mes));
969 dw = mq;
970
971 gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
972 for (i = 0; i < 6; i++) {
973 mes[8] = i;
974 do {
975 ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
976 } while (ret == MQE_CONGESTION);
977 if (ret)
978 break;
979 }
980 if (ret != MQE_QUEUE_FULL || i != 4)
981 goto done;
982
983 for (i = 0; i < 6; i++) {
984 m = gru_get_next_message(&mqd);
985 if (!m || m[8] != i)
986 break;
987 gru_free_message(&mqd, m);
988 }
989 ret = (i == 4) ? 0 : -EIO;
990
991done:
992 kfree(p);
993 return ret;
994}
995
996static int quicktest2(unsigned long arg)
997{
998 static DECLARE_COMPLETION(cmp);
999 unsigned long han;
1000 int blade_id = 0;
1001 int numcb = 4;
1002 int ret = 0;
1003 unsigned long *buf;
1004 void *cb0, *cb;
1005 int i, k, istatus, bytes;
1006
1007 bytes = numcb * 4 * 8;
1008 buf = kmalloc(bytes, GFP_KERNEL);
1009 if (!buf)
1010 return -ENOMEM;
1011
1012 ret = -EBUSY;
1013 han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
1014 if (!han)
1015 goto done;
1016
1017 gru_lock_async_resource(han, &cb0, NULL);
1018 memset(buf, 0xee, bytes);
1019 for (i = 0; i < numcb; i++)
1020 gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
1021 XTYPE_DW, 4, 1, IMA_INTERRUPT);
1022
1023 ret = 0;
1024 for (k = 0; k < numcb; k++) {
1025 gru_wait_async_cbr(han);
1026 for (i = 0; i < numcb; i++) {
1027 cb = cb0 + i * GRU_HANDLE_STRIDE;
1028 istatus = gru_check_status(cb);
1029 if (istatus == CBS_ACTIVE)
1030 continue;
1031 if (istatus == CBS_EXCEPTION)
1032 ret = -EFAULT;
1033 else if (buf[i] || buf[i + 1] || buf[i + 2] ||
1034 buf[i + 3])
1035 ret = -EIO;
1036 }
1037 }
1038 BUG_ON(cmp.done);
1039
1040 gru_unlock_async_resource(han);
1041 gru_release_async_resources(han);
1042done:
1043 kfree(buf);
1044 return ret;
1045}
1046
1047/*
1048 * Debugging only. User hook for various kernel tests
1049 * of driver & gru.
1050 */
1051int gru_ktest(unsigned long arg)
1052{
1053 int ret = -EINVAL;
1054
1055 switch (arg & 0xff) {
1056 case 0:
1057 ret = quicktest0(arg);
1058 break;
1059 case 1:
1060 ret = quicktest1(arg);
1061 break;
1062 case 2:
1063 ret = quicktest2(arg);
1064 break;
Jack Steinerd5826dd2009-06-17 16:28:28 -07001065 case 99:
1066 ret = gru_free_kernel_contexts();
1067 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -07001068 }
1069 return ret;
1070
1071}
Jack Steiner28bffaf2008-07-29 22:33:57 -07001072
Jack Steinerd5826dd2009-06-17 16:28:28 -07001073int gru_kservices_init(void)
Jack Steiner28bffaf2008-07-29 22:33:57 -07001074{
Jack Steiner28bffaf2008-07-29 22:33:57 -07001075 return 0;
1076}
Jack Steiner27ca8a72009-04-02 16:59:11 -07001077
Jack Steinerd5826dd2009-06-17 16:28:28 -07001078void gru_kservices_exit(void)
Jack Steiner27ca8a72009-04-02 16:59:11 -07001079{
Jack Steinerd5826dd2009-06-17 16:28:28 -07001080 if (gru_free_kernel_contexts())
1081 BUG();
Jack Steiner27ca8a72009-04-02 16:59:11 -07001082}
1083