blob: 50b4dd8b0c9f505cd3c48a16d76fff25a7c9af66 [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 Steiner28bffaf2008-07-29 22:33:57 -070055 */
Jack Steiner6f2584f2009-04-02 16:59:10 -070056#define GRU_NUM_KERNEL_CBR 1
Jack Steiner28bffaf2008-07-29 22:33:57 -070057#define GRU_NUM_KERNEL_DSR_BYTES 256
Jack Steiner6f2584f2009-04-02 16:59:10 -070058#define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
59 GRU_CACHE_LINE_BYTES)
Jack Steiner28bffaf2008-07-29 22:33:57 -070060
61/* GRU instruction attributes for all instructions */
62#define IMA IMA_CB_DELAY
63
64/* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
65#define __gru_cacheline_aligned__ \
66 __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
67
68#define MAGIC 0x1234567887654321UL
69
70/* Default retry count for GRU errors on kernel instructions */
71#define EXCEPTION_RETRY_LIMIT 3
72
73/* Status of message queue sections */
74#define MQS_EMPTY 0
75#define MQS_FULL 1
76#define MQS_NOOP 2
77
78/*----------------- RESOURCE MANAGEMENT -------------------------------------*/
79/* optimized for x86_64 */
80struct message_queue {
81 union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
82 int qlines; /* DW 1 */
83 long hstatus[2];
84 void *next __gru_cacheline_aligned__;/* CL 1 */
85 void *limit;
86 void *start;
87 void *start2;
88 char data ____cacheline_aligned; /* CL 2 */
89};
90
91/* First word in every message - used by mesq interface */
92struct message_header {
93 char present;
94 char present2;
95 char lines;
96 char fill;
97};
98
Jack Steiner28bffaf2008-07-29 22:33:57 -070099#define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
100
Jack Steiner836ce672009-06-17 16:28:22 -0700101/*
102 * Allocate a kernel context (GTS) for the specified blade.
103 * - protected by writelock on bs_kgts_sema.
104 */
105static void gru_alloc_kernel_context(struct gru_blade_state *bs, int blade_id)
106{
107 int cbr_au_count, dsr_au_count, ncpus;
108
109 ncpus = uv_blade_nr_possible_cpus(blade_id);
110 cbr_au_count = GRU_CB_COUNT_TO_AU(GRU_NUM_KERNEL_CBR * ncpus);
111 dsr_au_count = GRU_DS_BYTES_TO_AU(GRU_NUM_KERNEL_DSR_BYTES * ncpus);
112 bs->bs_kgts = gru_alloc_gts(NULL, cbr_au_count, dsr_au_count, 0, 0);
113}
114
115/*
116 * Reload the blade's kernel context into a GRU chiplet. Called holding
117 * the bs_kgts_sema for READ. Will steal user contexts if necessary.
118 */
119static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
120{
121 struct gru_state *gru;
122 struct gru_thread_state *kgts;
123 void *vaddr;
124 int ctxnum;
125
126 up_read(&bs->bs_kgts_sema);
127 down_write(&bs->bs_kgts_sema);
128
129 if (!bs->bs_kgts)
130 gru_alloc_kernel_context(bs, blade_id);
131 kgts = bs->bs_kgts;
132
133 if (!kgts->ts_gru) {
134 STAT(load_kernel_context);
135 while (!gru_assign_gru_context(kgts, blade_id)) {
136 msleep(1);
137 gru_steal_context(kgts, blade_id);
138 }
139 gru_load_context(kgts);
140 gru = bs->bs_kgts->ts_gru;
141 vaddr = gru->gs_gru_base_vaddr;
142 ctxnum = kgts->ts_ctxnum;
143 bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
144 bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
145 }
146 downgrade_write(&bs->bs_kgts_sema);
147}
148
149/*
150 * Lock & load the kernel context for the specified blade.
151 */
152static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
153{
154 struct gru_blade_state *bs;
155
156 STAT(lock_kernel_context);
157 bs = gru_base[blade_id];
158
159 down_read(&bs->bs_kgts_sema);
160 if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
161 gru_load_kernel_context(bs, blade_id);
162 return bs;
163
164}
165
166/*
167 * Unlock the kernel context for the specified blade. Context is not
168 * unloaded but may be stolen before next use.
169 */
170static void gru_unlock_kernel_context(int blade_id)
171{
172 struct gru_blade_state *bs;
173
174 bs = gru_base[blade_id];
175 up_read(&bs->bs_kgts_sema);
176 STAT(unlock_kernel_context);
177}
178
179/*
180 * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
181 * - returns with preemption disabled
182 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700183static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
184{
185 struct gru_blade_state *bs;
186 int lcpu;
187
188 BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
189 preempt_disable();
Jack Steiner836ce672009-06-17 16:28:22 -0700190 bs = gru_lock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700191 lcpu = uv_blade_processor_id();
192 *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
193 *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
194 return 0;
195}
196
Jack Steiner836ce672009-06-17 16:28:22 -0700197/*
198 * Free the current cpus reserved DSR/CBR resources.
199 */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700200static void gru_free_cpu_resources(void *cb, void *dsr)
201{
Jack Steiner836ce672009-06-17 16:28:22 -0700202 gru_unlock_kernel_context(uv_numa_blade_id());
Jack Steiner28bffaf2008-07-29 22:33:57 -0700203 preempt_enable();
204}
205
Jack Steiner836ce672009-06-17 16:28:22 -0700206/*----------------------------------------------------------------------*/
Jack Steiner28bffaf2008-07-29 22:33:57 -0700207int gru_get_cb_exception_detail(void *cb,
208 struct control_block_extended_exc_detail *excdet)
209{
210 struct gru_control_block_extended *cbe;
211
212 cbe = get_cbe(GRUBASE(cb), get_cb_number(cb));
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700213 prefetchw(cbe); /* Harmless on hardware, required for emulator */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700214 excdet->opc = cbe->opccpy;
215 excdet->exopc = cbe->exopccpy;
216 excdet->ecause = cbe->ecause;
217 excdet->exceptdet0 = cbe->idef1upd;
218 excdet->exceptdet1 = cbe->idef3upd;
219 return 0;
220}
221
222char *gru_get_cb_exception_detail_str(int ret, void *cb,
223 char *buf, int size)
224{
225 struct gru_control_block_status *gen = (void *)cb;
226 struct control_block_extended_exc_detail excdet;
227
228 if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
229 gru_get_cb_exception_detail(cb, &excdet);
230 snprintf(buf, size,
231 "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
232 "excdet0 0x%lx, excdet1 0x%x",
233 gen, excdet.opc, excdet.exopc, excdet.ecause,
234 excdet.exceptdet0, excdet.exceptdet1);
235 } else {
236 snprintf(buf, size, "No exception");
237 }
238 return buf;
239}
240
241static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
242{
243 while (gen->istatus >= CBS_ACTIVE) {
244 cpu_relax();
245 barrier();
246 }
247 return gen->istatus;
248}
249
250static int gru_retry_exception(void *cb)
251{
252 struct gru_control_block_status *gen = (void *)cb;
253 struct control_block_extended_exc_detail excdet;
254 int retry = EXCEPTION_RETRY_LIMIT;
255
256 while (1) {
257 if (gru_get_cb_message_queue_substatus(cb))
258 break;
259 if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
260 return CBS_IDLE;
261
262 gru_get_cb_exception_detail(cb, &excdet);
263 if (excdet.ecause & ~EXCEPTION_RETRY_BITS)
264 break;
265 if (retry-- == 0)
266 break;
267 gen->icmd = 1;
268 gru_flush_cache(gen);
269 }
270 return CBS_EXCEPTION;
271}
272
273int gru_check_status_proc(void *cb)
274{
275 struct gru_control_block_status *gen = (void *)cb;
276 int ret;
277
278 ret = gen->istatus;
279 if (ret != CBS_EXCEPTION)
280 return ret;
281 return gru_retry_exception(cb);
282
283}
284
285int gru_wait_proc(void *cb)
286{
287 struct gru_control_block_status *gen = (void *)cb;
288 int ret;
289
290 ret = gru_wait_idle_or_exception(gen);
291 if (ret == CBS_EXCEPTION)
292 ret = gru_retry_exception(cb);
293
294 return ret;
295}
296
297void gru_abort(int ret, void *cb, char *str)
298{
299 char buf[GRU_EXC_STR_SIZE];
300
301 panic("GRU FATAL ERROR: %s - %s\n", str,
302 gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
303}
304
305void gru_wait_abort_proc(void *cb)
306{
307 int ret;
308
309 ret = gru_wait_proc(cb);
310 if (ret)
311 gru_abort(ret, cb, "gru_wait_abort");
312}
313
314
315/*------------------------------ MESSAGE QUEUES -----------------------------*/
316
317/* Internal status . These are NOT returned to the user. */
318#define MQIE_AGAIN -1 /* try again */
319
320
321/*
322 * Save/restore the "present" flag that is in the second line of 2-line
323 * messages
324 */
325static inline int get_present2(void *p)
326{
327 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
328 return mhdr->present;
329}
330
331static inline void restore_present2(void *p, int val)
332{
333 struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
334 mhdr->present = val;
335}
336
337/*
338 * Create a message queue.
339 * qlines - message queue size in cache lines. Includes 2-line header.
340 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700341int gru_create_message_queue(struct gru_message_queue_desc *mqd,
342 void *p, unsigned int bytes, int nasid, int vector, int apicid)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700343{
344 struct message_queue *mq = p;
345 unsigned int qlines;
346
347 qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
348 memset(mq, 0, bytes);
349 mq->start = &mq->data;
350 mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
351 mq->next = &mq->data;
352 mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
353 mq->qlines = qlines;
354 mq->hstatus[0] = 0;
355 mq->hstatus[1] = 1;
356 mq->head = gru_mesq_head(2, qlines / 2 + 1);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700357 mqd->mq = mq;
358 mqd->mq_gpa = uv_gpa(mq);
359 mqd->qlines = qlines;
360 mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
361 mqd->interrupt_vector = vector;
362 mqd->interrupt_apicid = apicid;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700363 return 0;
364}
365EXPORT_SYMBOL_GPL(gru_create_message_queue);
366
367/*
368 * Send a NOOP message to a message queue
369 * Returns:
370 * 0 - if queue is full after the send. This is the normal case
371 * but various races can change this.
372 * -1 - if mesq sent successfully but queue not full
373 * >0 - unexpected error. MQE_xxx returned
374 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700375static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
376 void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700377{
378 const struct message_header noop_header = {
379 .present = MQS_NOOP, .lines = 1};
380 unsigned long m;
381 int substatus, ret;
382 struct message_header save_mhdr, *mhdr = mesg;
383
384 STAT(mesq_noop);
385 save_mhdr = *mhdr;
386 *mhdr = noop_header;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700387 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700388 ret = gru_wait(cb);
389
390 if (ret) {
391 substatus = gru_get_cb_message_queue_substatus(cb);
392 switch (substatus) {
393 case CBSS_NO_ERROR:
394 STAT(mesq_noop_unexpected_error);
395 ret = MQE_UNEXPECTED_CB_ERR;
396 break;
397 case CBSS_LB_OVERFLOWED:
398 STAT(mesq_noop_lb_overflow);
399 ret = MQE_CONGESTION;
400 break;
401 case CBSS_QLIMIT_REACHED:
402 STAT(mesq_noop_qlimit_reached);
403 ret = 0;
404 break;
405 case CBSS_AMO_NACKED:
406 STAT(mesq_noop_amo_nacked);
407 ret = MQE_CONGESTION;
408 break;
409 case CBSS_PUT_NACKED:
410 STAT(mesq_noop_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700411 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700412 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
413 IMA);
414 if (gru_wait(cb) == CBS_IDLE)
415 ret = MQIE_AGAIN;
416 else
417 ret = MQE_UNEXPECTED_CB_ERR;
418 break;
419 case CBSS_PAGE_OVERFLOW:
420 default:
421 BUG();
422 }
423 }
424 *mhdr = save_mhdr;
425 return ret;
426}
427
428/*
429 * Handle a gru_mesq full.
430 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700431static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
432 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700433{
434 union gru_mesqhead mqh;
435 unsigned int limit, head;
436 unsigned long avalue;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700437 int half, qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700438
439 /* Determine if switching to first/second half of q */
440 avalue = gru_get_amo_value(cb);
441 head = gru_get_amo_value_head(cb);
442 limit = gru_get_amo_value_limit(cb);
443
Jack Steiner6f2584f2009-04-02 16:59:10 -0700444 qlines = mqd->qlines;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700445 half = (limit != qlines);
446
447 if (half)
448 mqh = gru_mesq_head(qlines / 2 + 1, qlines);
449 else
450 mqh = gru_mesq_head(2, qlines / 2 + 1);
451
452 /* Try to get lock for switching head pointer */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700453 gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700454 if (gru_wait(cb) != CBS_IDLE)
455 goto cberr;
456 if (!gru_get_amo_value(cb)) {
457 STAT(mesq_qf_locked);
458 return MQE_QUEUE_FULL;
459 }
460
461 /* Got the lock. Send optional NOP if queue not full, */
462 if (head != limit) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700463 if (send_noop_message(cb, mqd, mesg)) {
464 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
Jack Steiner28bffaf2008-07-29 22:33:57 -0700465 XTYPE_DW, IMA);
466 if (gru_wait(cb) != CBS_IDLE)
467 goto cberr;
468 STAT(mesq_qf_noop_not_full);
469 return MQIE_AGAIN;
470 }
471 avalue++;
472 }
473
474 /* Then flip queuehead to other half of queue. */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700475 gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
476 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700477 if (gru_wait(cb) != CBS_IDLE)
478 goto cberr;
479
480 /* If not successfully in swapping queue head, clear the hstatus lock */
481 if (gru_get_amo_value(cb) != avalue) {
482 STAT(mesq_qf_switch_head_failed);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700483 gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
484 IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700485 if (gru_wait(cb) != CBS_IDLE)
486 goto cberr;
487 }
488 return MQIE_AGAIN;
489cberr:
490 STAT(mesq_qf_unexpected_error);
491 return MQE_UNEXPECTED_CB_ERR;
492}
493
Jack Steiner6f2584f2009-04-02 16:59:10 -0700494/*
495 * Send a cross-partition interrupt to the SSI that contains the target
496 * message queue. Normally, the interrupt is automatically delivered by hardware
497 * but some error conditions require explicit delivery.
498 */
499static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
500{
501 if (mqd->interrupt_vector)
502 uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
503 mqd->interrupt_vector);
504}
505
Jack Steiner28bffaf2008-07-29 22:33:57 -0700506
507/*
508 * Handle a gru_mesq failure. Some of these failures are software recoverable
509 * or retryable.
510 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700511static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
512 void *mesg, int lines)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700513{
514 int substatus, ret = 0;
515 unsigned long m;
516
517 substatus = gru_get_cb_message_queue_substatus(cb);
518 switch (substatus) {
519 case CBSS_NO_ERROR:
520 STAT(mesq_send_unexpected_error);
521 ret = MQE_UNEXPECTED_CB_ERR;
522 break;
523 case CBSS_LB_OVERFLOWED:
524 STAT(mesq_send_lb_overflow);
525 ret = MQE_CONGESTION;
526 break;
527 case CBSS_QLIMIT_REACHED:
528 STAT(mesq_send_qlimit_reached);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700529 ret = send_message_queue_full(cb, mqd, mesg, lines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700530 break;
531 case CBSS_AMO_NACKED:
532 STAT(mesq_send_amo_nacked);
533 ret = MQE_CONGESTION;
534 break;
535 case CBSS_PUT_NACKED:
536 STAT(mesq_send_put_nacked);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700537 m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700538 gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700539 if (gru_wait(cb) == CBS_IDLE) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700540 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700541 send_message_queue_interrupt(mqd);
542 } else {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700543 ret = MQE_UNEXPECTED_CB_ERR;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700544 }
Jack Steiner28bffaf2008-07-29 22:33:57 -0700545 break;
546 default:
547 BUG();
548 }
549 return ret;
550}
551
552/*
553 * Send a message to a message queue
Jack Steiner6f2584f2009-04-02 16:59:10 -0700554 * mqd message queue descriptor
Jack Steiner28bffaf2008-07-29 22:33:57 -0700555 * mesg message. ust be vaddr within a GSEG
556 * bytes message size (<= 2 CL)
557 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700558int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
559 unsigned int bytes)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700560{
561 struct message_header *mhdr;
562 void *cb;
563 void *dsr;
564 int istatus, clines, ret;
565
566 STAT(mesq_send);
567 BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
568
Julia Lawallcbf330b2008-10-15 22:01:27 -0700569 clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700570 if (gru_get_cpu_resources(bytes, &cb, &dsr))
571 return MQE_BUG_NO_RESOURCES;
572 memcpy(dsr, mesg, bytes);
573 mhdr = dsr;
574 mhdr->present = MQS_FULL;
575 mhdr->lines = clines;
576 if (clines == 2) {
577 mhdr->present2 = get_present2(mhdr);
578 restore_present2(mhdr, MQS_FULL);
579 }
580
581 do {
582 ret = MQE_OK;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700583 gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700584 istatus = gru_wait(cb);
585 if (istatus != CBS_IDLE)
Jack Steiner6f2584f2009-04-02 16:59:10 -0700586 ret = send_message_failure(cb, mqd, dsr, clines);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700587 } while (ret == MQIE_AGAIN);
588 gru_free_cpu_resources(cb, dsr);
589
590 if (ret)
591 STAT(mesq_send_failed);
592 return ret;
593}
594EXPORT_SYMBOL_GPL(gru_send_message_gpa);
595
596/*
597 * Advance the receive pointer for the queue to the next message.
598 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700599void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700600{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700601 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700602 struct message_header *mhdr = mq->next;
603 void *next, *pnext;
604 int half = -1;
605 int lines = mhdr->lines;
606
607 if (lines == 2)
608 restore_present2(mhdr, MQS_EMPTY);
609 mhdr->present = MQS_EMPTY;
610
611 pnext = mq->next;
612 next = pnext + GRU_CACHE_LINE_BYTES * lines;
613 if (next == mq->limit) {
614 next = mq->start;
615 half = 1;
616 } else if (pnext < mq->start2 && next >= mq->start2) {
617 half = 0;
618 }
619
620 if (half >= 0)
621 mq->hstatus[half] = 1;
622 mq->next = next;
623}
624EXPORT_SYMBOL_GPL(gru_free_message);
625
626/*
627 * Get next message from message queue. Return NULL if no message
628 * present. User must call next_message() to move to next message.
629 * rmq message queue
630 */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700631void *gru_get_next_message(struct gru_message_queue_desc *mqd)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700632{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700633 struct message_queue *mq = mqd->mq;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700634 struct message_header *mhdr = mq->next;
635 int present = mhdr->present;
636
637 /* skip NOOP messages */
638 STAT(mesq_receive);
639 while (present == MQS_NOOP) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700640 gru_free_message(mqd, mhdr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700641 mhdr = mq->next;
642 present = mhdr->present;
643 }
644
645 /* Wait for both halves of 2 line messages */
646 if (present == MQS_FULL && mhdr->lines == 2 &&
647 get_present2(mhdr) == MQS_EMPTY)
648 present = MQS_EMPTY;
649
650 if (!present) {
651 STAT(mesq_receive_none);
652 return NULL;
653 }
654
655 if (mhdr->lines == 2)
656 restore_present2(mhdr, mhdr->present2);
657
658 return mhdr;
659}
660EXPORT_SYMBOL_GPL(gru_get_next_message);
661
662/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
663
664/*
665 * Copy a block of data using the GRU resources
666 */
667int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
668 unsigned int bytes)
669{
670 void *cb;
671 void *dsr;
672 int ret;
673
674 STAT(copy_gpa);
675 if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
676 return MQE_BUG_NO_RESOURCES;
677 gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
Jack Steiner6f2584f2009-04-02 16:59:10 -0700678 XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700679 ret = gru_wait(cb);
680 gru_free_cpu_resources(cb, dsr);
681 return ret;
682}
683EXPORT_SYMBOL_GPL(gru_copy_gpa);
684
685/* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
686/* Temp - will delete after we gain confidence in the GRU */
Jack Steiner28bffaf2008-07-29 22:33:57 -0700687
Jack Steiner836ce672009-06-17 16:28:22 -0700688int quicktest(void)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700689{
Jack Steiner836ce672009-06-17 16:28:22 -0700690 unsigned long word0;
691 unsigned long word1;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700692 void *cb;
Jack Steiner836ce672009-06-17 16:28:22 -0700693 void *dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700694 unsigned long *p;
695
Jack Steiner836ce672009-06-17 16:28:22 -0700696 if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
697 return MQE_BUG_NO_RESOURCES;
698 p = dsr;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700699 word0 = MAGIC;
Jack Steiner836ce672009-06-17 16:28:22 -0700700 word1 = 0;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700701
Jack Steiner836ce672009-06-17 16:28:22 -0700702 gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700703 if (gru_wait(cb) != CBS_IDLE)
704 BUG();
705
Jack Steiner836ce672009-06-17 16:28:22 -0700706 if (*p != MAGIC)
Jack Steiner28bffaf2008-07-29 22:33:57 -0700707 BUG();
Jack Steiner836ce672009-06-17 16:28:22 -0700708 gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700709 if (gru_wait(cb) != CBS_IDLE)
710 BUG();
Jack Steiner836ce672009-06-17 16:28:22 -0700711 gru_free_cpu_resources(cb, dsr);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700712
Jack Steiner836ce672009-06-17 16:28:22 -0700713 if (word0 != word1 || word1 != MAGIC) {
Jack Steiner28bffaf2008-07-29 22:33:57 -0700714 printk
Jack Steiner836ce672009-06-17 16:28:22 -0700715 ("GRU quicktest err: found 0x%lx, expected 0x%lx\n",
716 word1, MAGIC);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700717 BUG(); /* ZZZ should not be fatal */
718 }
719
720 return 0;
721}
722
723
724int gru_kservices_init(struct gru_state *gru)
725{
726 struct gru_blade_state *bs;
Jack Steiner28bffaf2008-07-29 22:33:57 -0700727
Jack Steiner28bffaf2008-07-29 22:33:57 -0700728 bs = gru->gs_blade;
Jack Steiner836ce672009-06-17 16:28:22 -0700729 if (gru != &bs->bs_grus[0])
Jack Steiner28bffaf2008-07-29 22:33:57 -0700730 return 0;
731
Jack Steiner836ce672009-06-17 16:28:22 -0700732 init_rwsem(&bs->bs_kgts_sema);
Jack Steiner28bffaf2008-07-29 22:33:57 -0700733
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700734 if (gru_options & GRU_QUICKLOOK)
Jack Steiner836ce672009-06-17 16:28:22 -0700735 quicktest();
Jack Steiner28bffaf2008-07-29 22:33:57 -0700736 return 0;
737}
Jack Steiner27ca8a72009-04-02 16:59:11 -0700738
739void gru_kservices_exit(struct gru_state *gru)
740{
Jack Steiner27ca8a72009-04-02 16:59:11 -0700741 struct gru_blade_state *bs;
Jack Steiner836ce672009-06-17 16:28:22 -0700742 struct gru_thread_state *kgts;
Jack Steiner27ca8a72009-04-02 16:59:11 -0700743
744 bs = gru->gs_blade;
Jack Steiner836ce672009-06-17 16:28:22 -0700745 if (gru != &bs->bs_grus[0])
Jack Steiner27ca8a72009-04-02 16:59:11 -0700746 return;
747
Jack Steiner836ce672009-06-17 16:28:22 -0700748 kgts = bs->bs_kgts;
749 if (kgts && kgts->ts_gru)
750 gru_unload_context(kgts, 0);
751 kfree(kgts);
Jack Steiner27ca8a72009-04-02 16:59:11 -0700752}
753