grukservices.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * KERNEL SERVICES THAT USE THE GRU
  5. *
  6. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include <linux/smp_lock.h>
  27. #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>
  33. #include <linux/delay.h>
  34. #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. * Kernel GRU Usage
  42. *
  43. * The following is an interim algorithm for management of kernel GRU
  44. * resources. This will likely be replaced when we better understand the
  45. * kernel/user requirements.
  46. *
  47. * Blade percpu resources reserved for kernel use. These resources are
  48. * reserved whenever the the kernel context for the blade is loaded. Note
  49. * that the kernel context is not guaranteed to be always available. It is
  50. * loaded on demand & can be stolen by a user if the user demand exceeds the
  51. * kernel demand. The kernel can always reload the kernel context but
  52. * a SLEEP may be required!!!.
  53. *
  54. * Async Overview:
  55. *
  56. * Each blade has one "kernel context" that owns GRU kernel resources
  57. * located on the blade. Kernel drivers use GRU resources in this context
  58. * for sending messages, zeroing memory, etc.
  59. *
  60. * The kernel context is dynamically loaded on demand. If it is not in
  61. * use by the kernel, the kernel context can be unloaded & given to a user.
  62. * The kernel context will be reloaded when needed. This may require that
  63. * a context be stolen from a user.
  64. * NOTE: frequent unloading/reloading of the kernel context is
  65. * expensive. We are depending on batch schedulers, cpusets, sane
  66. * drivers or some other mechanism to prevent the need for frequent
  67. * stealing/reloading.
  68. *
  69. * The kernel context consists of two parts:
  70. * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
  71. * Each cpu has it's own private resources & does not share them
  72. * with other cpus. These resources are used serially, ie,
  73. * locked, used & unlocked on each call to a function in
  74. * grukservices.
  75. * (Now that we have dynamic loading of kernel contexts, I
  76. * may rethink this & allow sharing between cpus....)
  77. *
  78. * - Additional resources can be reserved long term & used directly
  79. * by UV drivers located in the kernel. Drivers using these GRU
  80. * resources can use asynchronous GRU instructions that send
  81. * interrupts on completion.
  82. * - these resources must be explicitly locked/unlocked
  83. * - locked resources prevent (obviously) the kernel
  84. * context from being unloaded.
  85. * - drivers using these resource directly issue their own
  86. * GRU instruction and must wait/check completion.
  87. *
  88. * When these resources are reserved, the caller can optionally
  89. * associate a wait_queue with the resources and use asynchronous
  90. * GRU instructions. When an async GRU instruction completes, the
  91. * driver will do a wakeup on the event.
  92. *
  93. */
  94. #define ASYNC_HAN_TO_BID(h) ((h) - 1)
  95. #define ASYNC_BID_TO_HAN(b) ((b) + 1)
  96. #define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
  97. #define GRU_NUM_KERNEL_CBR 1
  98. #define GRU_NUM_KERNEL_DSR_BYTES 256
  99. #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
  100. GRU_CACHE_LINE_BYTES)
  101. /* GRU instruction attributes for all instructions */
  102. #define IMA IMA_CB_DELAY
  103. /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
  104. #define __gru_cacheline_aligned__ \
  105. __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
  106. #define MAGIC 0x1234567887654321UL
  107. /* Default retry count for GRU errors on kernel instructions */
  108. #define EXCEPTION_RETRY_LIMIT 3
  109. /* Status of message queue sections */
  110. #define MQS_EMPTY 0
  111. #define MQS_FULL 1
  112. #define MQS_NOOP 2
  113. /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
  114. /* optimized for x86_64 */
  115. struct message_queue {
  116. union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
  117. int qlines; /* DW 1 */
  118. long hstatus[2];
  119. void *next __gru_cacheline_aligned__;/* CL 1 */
  120. void *limit;
  121. void *start;
  122. void *start2;
  123. char data ____cacheline_aligned; /* CL 2 */
  124. };
  125. /* First word in every message - used by mesq interface */
  126. struct message_header {
  127. char present;
  128. char present2;
  129. char lines;
  130. char fill;
  131. };
  132. #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
  133. /*
  134. * Reload the blade's kernel context into a GRU chiplet. Called holding
  135. * the bs_kgts_sema for READ. Will steal user contexts if necessary.
  136. */
  137. static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
  138. {
  139. struct gru_state *gru;
  140. struct gru_thread_state *kgts;
  141. void *vaddr;
  142. int ctxnum, ncpus;
  143. up_read(&bs->bs_kgts_sema);
  144. down_write(&bs->bs_kgts_sema);
  145. if (!bs->bs_kgts)
  146. bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
  147. kgts = bs->bs_kgts;
  148. if (!kgts->ts_gru) {
  149. STAT(load_kernel_context);
  150. ncpus = uv_blade_nr_possible_cpus(blade_id);
  151. kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
  152. GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
  153. kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
  154. GRU_NUM_KERNEL_DSR_BYTES * ncpus +
  155. bs->bs_async_dsr_bytes);
  156. while (!gru_assign_gru_context(kgts, blade_id)) {
  157. msleep(1);
  158. gru_steal_context(kgts, blade_id);
  159. }
  160. gru_load_context(kgts);
  161. gru = bs->bs_kgts->ts_gru;
  162. vaddr = gru->gs_gru_base_vaddr;
  163. ctxnum = kgts->ts_ctxnum;
  164. bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
  165. bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
  166. }
  167. downgrade_write(&bs->bs_kgts_sema);
  168. }
  169. /*
  170. * Lock & load the kernel context for the specified blade.
  171. */
  172. static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
  173. {
  174. struct gru_blade_state *bs;
  175. STAT(lock_kernel_context);
  176. bs = gru_base[blade_id];
  177. down_read(&bs->bs_kgts_sema);
  178. if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
  179. gru_load_kernel_context(bs, blade_id);
  180. return bs;
  181. }
  182. /*
  183. * Unlock the kernel context for the specified blade. Context is not
  184. * unloaded but may be stolen before next use.
  185. */
  186. static void gru_unlock_kernel_context(int blade_id)
  187. {
  188. struct gru_blade_state *bs;
  189. bs = gru_base[blade_id];
  190. up_read(&bs->bs_kgts_sema);
  191. STAT(unlock_kernel_context);
  192. }
  193. /*
  194. * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
  195. * - returns with preemption disabled
  196. */
  197. static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
  198. {
  199. struct gru_blade_state *bs;
  200. int lcpu;
  201. BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
  202. preempt_disable();
  203. bs = gru_lock_kernel_context(uv_numa_blade_id());
  204. lcpu = uv_blade_processor_id();
  205. *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
  206. *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
  207. return 0;
  208. }
  209. /*
  210. * Free the current cpus reserved DSR/CBR resources.
  211. */
  212. static void gru_free_cpu_resources(void *cb, void *dsr)
  213. {
  214. gru_unlock_kernel_context(uv_numa_blade_id());
  215. preempt_enable();
  216. }
  217. /*
  218. * Reserve GRU resources to be used asynchronously.
  219. * Note: currently supports only 1 reservation per blade.
  220. *
  221. * input:
  222. * blade_id - blade on which resources should be reserved
  223. * cbrs - number of CBRs
  224. * dsr_bytes - number of DSR bytes needed
  225. * output:
  226. * handle to identify resource
  227. * (0 = async resources already reserved)
  228. */
  229. unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
  230. struct completion *cmp)
  231. {
  232. struct gru_blade_state *bs;
  233. struct gru_thread_state *kgts;
  234. int ret = 0;
  235. bs = gru_base[blade_id];
  236. down_write(&bs->bs_kgts_sema);
  237. /* Verify no resources already reserved */
  238. if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
  239. goto done;
  240. bs->bs_async_dsr_bytes = dsr_bytes;
  241. bs->bs_async_cbrs = cbrs;
  242. bs->bs_async_wq = cmp;
  243. kgts = bs->bs_kgts;
  244. /* Resources changed. Unload context if already loaded */
  245. if (kgts && kgts->ts_gru)
  246. gru_unload_context(kgts, 0);
  247. ret = ASYNC_BID_TO_HAN(blade_id);
  248. done:
  249. up_write(&bs->bs_kgts_sema);
  250. return ret;
  251. }
  252. /*
  253. * Release async resources previously reserved.
  254. *
  255. * input:
  256. * han - handle to identify resources
  257. */
  258. void gru_release_async_resources(unsigned long han)
  259. {
  260. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  261. down_write(&bs->bs_kgts_sema);
  262. bs->bs_async_dsr_bytes = 0;
  263. bs->bs_async_cbrs = 0;
  264. bs->bs_async_wq = NULL;
  265. up_write(&bs->bs_kgts_sema);
  266. }
  267. /*
  268. * Wait for async GRU instructions to complete.
  269. *
  270. * input:
  271. * han - handle to identify resources
  272. */
  273. void gru_wait_async_cbr(unsigned long han)
  274. {
  275. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  276. wait_for_completion(bs->bs_async_wq);
  277. mb();
  278. }
  279. /*
  280. * Lock previous reserved async GRU resources
  281. *
  282. * input:
  283. * han - handle to identify resources
  284. * output:
  285. * cb - pointer to first CBR
  286. * dsr - pointer to first DSR
  287. */
  288. void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
  289. {
  290. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  291. int blade_id = ASYNC_HAN_TO_BID(han);
  292. int ncpus;
  293. gru_lock_kernel_context(blade_id);
  294. ncpus = uv_blade_nr_possible_cpus(blade_id);
  295. if (cb)
  296. *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
  297. if (dsr)
  298. *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
  299. }
  300. /*
  301. * Unlock previous reserved async GRU resources
  302. *
  303. * input:
  304. * han - handle to identify resources
  305. */
  306. void gru_unlock_async_resource(unsigned long han)
  307. {
  308. int blade_id = ASYNC_HAN_TO_BID(han);
  309. gru_unlock_kernel_context(blade_id);
  310. }
  311. /*----------------------------------------------------------------------*/
  312. int gru_get_cb_exception_detail(void *cb,
  313. struct control_block_extended_exc_detail *excdet)
  314. {
  315. struct gru_control_block_extended *cbe;
  316. cbe = get_cbe(GRUBASE(cb), get_cb_number(cb));
  317. prefetchw(cbe); /* Harmless on hardware, required for emulator */
  318. excdet->opc = cbe->opccpy;
  319. excdet->exopc = cbe->exopccpy;
  320. excdet->ecause = cbe->ecause;
  321. excdet->exceptdet0 = cbe->idef1upd;
  322. excdet->exceptdet1 = cbe->idef3upd;
  323. return 0;
  324. }
  325. char *gru_get_cb_exception_detail_str(int ret, void *cb,
  326. char *buf, int size)
  327. {
  328. struct gru_control_block_status *gen = (void *)cb;
  329. struct control_block_extended_exc_detail excdet;
  330. if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
  331. gru_get_cb_exception_detail(cb, &excdet);
  332. snprintf(buf, size,
  333. "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
  334. "excdet0 0x%lx, excdet1 0x%x",
  335. gen, excdet.opc, excdet.exopc, excdet.ecause,
  336. excdet.exceptdet0, excdet.exceptdet1);
  337. } else {
  338. snprintf(buf, size, "No exception");
  339. }
  340. return buf;
  341. }
  342. static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
  343. {
  344. while (gen->istatus >= CBS_ACTIVE) {
  345. cpu_relax();
  346. barrier();
  347. }
  348. return gen->istatus;
  349. }
  350. static int gru_retry_exception(void *cb)
  351. {
  352. struct gru_control_block_status *gen = (void *)cb;
  353. struct control_block_extended_exc_detail excdet;
  354. int retry = EXCEPTION_RETRY_LIMIT;
  355. while (1) {
  356. if (gru_get_cb_message_queue_substatus(cb))
  357. break;
  358. if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
  359. return CBS_IDLE;
  360. gru_get_cb_exception_detail(cb, &excdet);
  361. if (excdet.ecause & ~EXCEPTION_RETRY_BITS)
  362. break;
  363. if (retry-- == 0)
  364. break;
  365. gen->icmd = 1;
  366. gru_flush_cache(gen);
  367. }
  368. return CBS_EXCEPTION;
  369. }
  370. int gru_check_status_proc(void *cb)
  371. {
  372. struct gru_control_block_status *gen = (void *)cb;
  373. int ret;
  374. ret = gen->istatus;
  375. if (ret != CBS_EXCEPTION)
  376. return ret;
  377. return gru_retry_exception(cb);
  378. }
  379. int gru_wait_proc(void *cb)
  380. {
  381. struct gru_control_block_status *gen = (void *)cb;
  382. int ret;
  383. ret = gru_wait_idle_or_exception(gen);
  384. if (ret == CBS_EXCEPTION)
  385. ret = gru_retry_exception(cb);
  386. return ret;
  387. }
  388. void gru_abort(int ret, void *cb, char *str)
  389. {
  390. char buf[GRU_EXC_STR_SIZE];
  391. panic("GRU FATAL ERROR: %s - %s\n", str,
  392. gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
  393. }
  394. void gru_wait_abort_proc(void *cb)
  395. {
  396. int ret;
  397. ret = gru_wait_proc(cb);
  398. if (ret)
  399. gru_abort(ret, cb, "gru_wait_abort");
  400. }
  401. /*------------------------------ MESSAGE QUEUES -----------------------------*/
  402. /* Internal status . These are NOT returned to the user. */
  403. #define MQIE_AGAIN -1 /* try again */
  404. /*
  405. * Save/restore the "present" flag that is in the second line of 2-line
  406. * messages
  407. */
  408. static inline int get_present2(void *p)
  409. {
  410. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  411. return mhdr->present;
  412. }
  413. static inline void restore_present2(void *p, int val)
  414. {
  415. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  416. mhdr->present = val;
  417. }
  418. /*
  419. * Create a message queue.
  420. * qlines - message queue size in cache lines. Includes 2-line header.
  421. */
  422. int gru_create_message_queue(struct gru_message_queue_desc *mqd,
  423. void *p, unsigned int bytes, int nasid, int vector, int apicid)
  424. {
  425. struct message_queue *mq = p;
  426. unsigned int qlines;
  427. qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
  428. memset(mq, 0, bytes);
  429. mq->start = &mq->data;
  430. mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
  431. mq->next = &mq->data;
  432. mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
  433. mq->qlines = qlines;
  434. mq->hstatus[0] = 0;
  435. mq->hstatus[1] = 1;
  436. mq->head = gru_mesq_head(2, qlines / 2 + 1);
  437. mqd->mq = mq;
  438. mqd->mq_gpa = uv_gpa(mq);
  439. mqd->qlines = qlines;
  440. mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
  441. mqd->interrupt_vector = vector;
  442. mqd->interrupt_apicid = apicid;
  443. return 0;
  444. }
  445. EXPORT_SYMBOL_GPL(gru_create_message_queue);
  446. /*
  447. * Send a NOOP message to a message queue
  448. * Returns:
  449. * 0 - if queue is full after the send. This is the normal case
  450. * but various races can change this.
  451. * -1 - if mesq sent successfully but queue not full
  452. * >0 - unexpected error. MQE_xxx returned
  453. */
  454. static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
  455. void *mesg)
  456. {
  457. const struct message_header noop_header = {
  458. .present = MQS_NOOP, .lines = 1};
  459. unsigned long m;
  460. int substatus, ret;
  461. struct message_header save_mhdr, *mhdr = mesg;
  462. STAT(mesq_noop);
  463. save_mhdr = *mhdr;
  464. *mhdr = noop_header;
  465. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
  466. ret = gru_wait(cb);
  467. if (ret) {
  468. substatus = gru_get_cb_message_queue_substatus(cb);
  469. switch (substatus) {
  470. case CBSS_NO_ERROR:
  471. STAT(mesq_noop_unexpected_error);
  472. ret = MQE_UNEXPECTED_CB_ERR;
  473. break;
  474. case CBSS_LB_OVERFLOWED:
  475. STAT(mesq_noop_lb_overflow);
  476. ret = MQE_CONGESTION;
  477. break;
  478. case CBSS_QLIMIT_REACHED:
  479. STAT(mesq_noop_qlimit_reached);
  480. ret = 0;
  481. break;
  482. case CBSS_AMO_NACKED:
  483. STAT(mesq_noop_amo_nacked);
  484. ret = MQE_CONGESTION;
  485. break;
  486. case CBSS_PUT_NACKED:
  487. STAT(mesq_noop_put_nacked);
  488. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  489. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
  490. IMA);
  491. if (gru_wait(cb) == CBS_IDLE)
  492. ret = MQIE_AGAIN;
  493. else
  494. ret = MQE_UNEXPECTED_CB_ERR;
  495. break;
  496. case CBSS_PAGE_OVERFLOW:
  497. default:
  498. BUG();
  499. }
  500. }
  501. *mhdr = save_mhdr;
  502. return ret;
  503. }
  504. /*
  505. * Handle a gru_mesq full.
  506. */
  507. static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
  508. void *mesg, int lines)
  509. {
  510. union gru_mesqhead mqh;
  511. unsigned int limit, head;
  512. unsigned long avalue;
  513. int half, qlines;
  514. /* Determine if switching to first/second half of q */
  515. avalue = gru_get_amo_value(cb);
  516. head = gru_get_amo_value_head(cb);
  517. limit = gru_get_amo_value_limit(cb);
  518. qlines = mqd->qlines;
  519. half = (limit != qlines);
  520. if (half)
  521. mqh = gru_mesq_head(qlines / 2 + 1, qlines);
  522. else
  523. mqh = gru_mesq_head(2, qlines / 2 + 1);
  524. /* Try to get lock for switching head pointer */
  525. gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
  526. if (gru_wait(cb) != CBS_IDLE)
  527. goto cberr;
  528. if (!gru_get_amo_value(cb)) {
  529. STAT(mesq_qf_locked);
  530. return MQE_QUEUE_FULL;
  531. }
  532. /* Got the lock. Send optional NOP if queue not full, */
  533. if (head != limit) {
  534. if (send_noop_message(cb, mqd, mesg)) {
  535. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
  536. XTYPE_DW, IMA);
  537. if (gru_wait(cb) != CBS_IDLE)
  538. goto cberr;
  539. STAT(mesq_qf_noop_not_full);
  540. return MQIE_AGAIN;
  541. }
  542. avalue++;
  543. }
  544. /* Then flip queuehead to other half of queue. */
  545. gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
  546. IMA);
  547. if (gru_wait(cb) != CBS_IDLE)
  548. goto cberr;
  549. /* If not successfully in swapping queue head, clear the hstatus lock */
  550. if (gru_get_amo_value(cb) != avalue) {
  551. STAT(mesq_qf_switch_head_failed);
  552. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
  553. IMA);
  554. if (gru_wait(cb) != CBS_IDLE)
  555. goto cberr;
  556. }
  557. return MQIE_AGAIN;
  558. cberr:
  559. STAT(mesq_qf_unexpected_error);
  560. return MQE_UNEXPECTED_CB_ERR;
  561. }
  562. /*
  563. * Send a cross-partition interrupt to the SSI that contains the target
  564. * message queue. Normally, the interrupt is automatically delivered by hardware
  565. * but some error conditions require explicit delivery.
  566. */
  567. static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
  568. {
  569. if (mqd->interrupt_vector)
  570. uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
  571. mqd->interrupt_vector);
  572. }
  573. /*
  574. * Handle a PUT failure. Note: if message was a 2-line message, one of the
  575. * lines might have successfully have been written. Before sending the
  576. * message, "present" must be cleared in BOTH lines to prevent the receiver
  577. * from prematurely seeing the full message.
  578. */
  579. static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
  580. void *mesg, int lines)
  581. {
  582. unsigned long m;
  583. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  584. if (lines == 2) {
  585. gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
  586. if (gru_wait(cb) != CBS_IDLE)
  587. return MQE_UNEXPECTED_CB_ERR;
  588. }
  589. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
  590. if (gru_wait(cb) != CBS_IDLE)
  591. return MQE_UNEXPECTED_CB_ERR;
  592. send_message_queue_interrupt(mqd);
  593. return MQE_OK;
  594. }
  595. /*
  596. * Handle a gru_mesq failure. Some of these failures are software recoverable
  597. * or retryable.
  598. */
  599. static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
  600. void *mesg, int lines)
  601. {
  602. int substatus, ret = 0;
  603. substatus = gru_get_cb_message_queue_substatus(cb);
  604. switch (substatus) {
  605. case CBSS_NO_ERROR:
  606. STAT(mesq_send_unexpected_error);
  607. ret = MQE_UNEXPECTED_CB_ERR;
  608. break;
  609. case CBSS_LB_OVERFLOWED:
  610. STAT(mesq_send_lb_overflow);
  611. ret = MQE_CONGESTION;
  612. break;
  613. case CBSS_QLIMIT_REACHED:
  614. STAT(mesq_send_qlimit_reached);
  615. ret = send_message_queue_full(cb, mqd, mesg, lines);
  616. break;
  617. case CBSS_AMO_NACKED:
  618. STAT(mesq_send_amo_nacked);
  619. ret = MQE_CONGESTION;
  620. break;
  621. case CBSS_PUT_NACKED:
  622. STAT(mesq_send_put_nacked);
  623. ret = send_message_put_nacked(cb, mqd, mesg, lines);
  624. break;
  625. default:
  626. BUG();
  627. }
  628. return ret;
  629. }
  630. /*
  631. * Send a message to a message queue
  632. * mqd message queue descriptor
  633. * mesg message. ust be vaddr within a GSEG
  634. * bytes message size (<= 2 CL)
  635. */
  636. int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
  637. unsigned int bytes)
  638. {
  639. struct message_header *mhdr;
  640. void *cb;
  641. void *dsr;
  642. int istatus, clines, ret;
  643. STAT(mesq_send);
  644. BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
  645. clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
  646. if (gru_get_cpu_resources(bytes, &cb, &dsr))
  647. return MQE_BUG_NO_RESOURCES;
  648. memcpy(dsr, mesg, bytes);
  649. mhdr = dsr;
  650. mhdr->present = MQS_FULL;
  651. mhdr->lines = clines;
  652. if (clines == 2) {
  653. mhdr->present2 = get_present2(mhdr);
  654. restore_present2(mhdr, MQS_FULL);
  655. }
  656. do {
  657. ret = MQE_OK;
  658. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
  659. istatus = gru_wait(cb);
  660. if (istatus != CBS_IDLE)
  661. ret = send_message_failure(cb, mqd, dsr, clines);
  662. } while (ret == MQIE_AGAIN);
  663. gru_free_cpu_resources(cb, dsr);
  664. if (ret)
  665. STAT(mesq_send_failed);
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(gru_send_message_gpa);
  669. /*
  670. * Advance the receive pointer for the queue to the next message.
  671. */
  672. void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
  673. {
  674. struct message_queue *mq = mqd->mq;
  675. struct message_header *mhdr = mq->next;
  676. void *next, *pnext;
  677. int half = -1;
  678. int lines = mhdr->lines;
  679. if (lines == 2)
  680. restore_present2(mhdr, MQS_EMPTY);
  681. mhdr->present = MQS_EMPTY;
  682. pnext = mq->next;
  683. next = pnext + GRU_CACHE_LINE_BYTES * lines;
  684. if (next == mq->limit) {
  685. next = mq->start;
  686. half = 1;
  687. } else if (pnext < mq->start2 && next >= mq->start2) {
  688. half = 0;
  689. }
  690. if (half >= 0)
  691. mq->hstatus[half] = 1;
  692. mq->next = next;
  693. }
  694. EXPORT_SYMBOL_GPL(gru_free_message);
  695. /*
  696. * Get next message from message queue. Return NULL if no message
  697. * present. User must call next_message() to move to next message.
  698. * rmq message queue
  699. */
  700. void *gru_get_next_message(struct gru_message_queue_desc *mqd)
  701. {
  702. struct message_queue *mq = mqd->mq;
  703. struct message_header *mhdr = mq->next;
  704. int present = mhdr->present;
  705. /* skip NOOP messages */
  706. STAT(mesq_receive);
  707. while (present == MQS_NOOP) {
  708. gru_free_message(mqd, mhdr);
  709. mhdr = mq->next;
  710. present = mhdr->present;
  711. }
  712. /* Wait for both halves of 2 line messages */
  713. if (present == MQS_FULL && mhdr->lines == 2 &&
  714. get_present2(mhdr) == MQS_EMPTY)
  715. present = MQS_EMPTY;
  716. if (!present) {
  717. STAT(mesq_receive_none);
  718. return NULL;
  719. }
  720. if (mhdr->lines == 2)
  721. restore_present2(mhdr, mhdr->present2);
  722. return mhdr;
  723. }
  724. EXPORT_SYMBOL_GPL(gru_get_next_message);
  725. /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
  726. /*
  727. * Copy a block of data using the GRU resources
  728. */
  729. int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
  730. unsigned int bytes)
  731. {
  732. void *cb;
  733. void *dsr;
  734. int ret;
  735. STAT(copy_gpa);
  736. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  737. return MQE_BUG_NO_RESOURCES;
  738. gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
  739. XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
  740. ret = gru_wait(cb);
  741. gru_free_cpu_resources(cb, dsr);
  742. return ret;
  743. }
  744. EXPORT_SYMBOL_GPL(gru_copy_gpa);
  745. /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
  746. /* Temp - will delete after we gain confidence in the GRU */
  747. int quicktest(void)
  748. {
  749. unsigned long word0;
  750. unsigned long word1;
  751. void *cb;
  752. void *dsr;
  753. unsigned long *p;
  754. if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
  755. return MQE_BUG_NO_RESOURCES;
  756. p = dsr;
  757. word0 = MAGIC;
  758. word1 = 0;
  759. gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  760. if (gru_wait(cb) != CBS_IDLE)
  761. BUG();
  762. if (*p != MAGIC)
  763. BUG();
  764. gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  765. if (gru_wait(cb) != CBS_IDLE)
  766. BUG();
  767. gru_free_cpu_resources(cb, dsr);
  768. if (word0 != word1 || word1 != MAGIC) {
  769. printk
  770. ("GRU quicktest err: found 0x%lx, expected 0x%lx\n",
  771. word1, MAGIC);
  772. BUG(); /* ZZZ should not be fatal */
  773. }
  774. return 0;
  775. }
  776. int gru_kservices_init(struct gru_state *gru)
  777. {
  778. struct gru_blade_state *bs;
  779. bs = gru->gs_blade;
  780. if (gru != &bs->bs_grus[0])
  781. return 0;
  782. init_rwsem(&bs->bs_kgts_sema);
  783. if (gru_options & GRU_QUICKLOOK)
  784. quicktest();
  785. return 0;
  786. }
  787. void gru_kservices_exit(struct gru_state *gru)
  788. {
  789. struct gru_blade_state *bs;
  790. struct gru_thread_state *kgts;
  791. bs = gru->gs_blade;
  792. if (gru != &bs->bs_grus[0])
  793. return;
  794. kgts = bs->bs_kgts;
  795. if (kgts && kgts->ts_gru)
  796. gru_unload_context(kgts, 0);
  797. kfree(kgts);
  798. }