grukservices.c 27 KB

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