grukservices.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. if (down_write_trylock(&bs->bs_kgts_sema)) {
  185. kgts = bs->bs_kgts;
  186. if (kgts && kgts->ts_gru)
  187. gru_unload_context(kgts, 0);
  188. kfree(kgts);
  189. bs->bs_kgts = NULL;
  190. up_write(&bs->bs_kgts_sema);
  191. } else {
  192. ret++;
  193. }
  194. }
  195. return ret;
  196. }
  197. /*
  198. * Lock & load the kernel context for the specified blade.
  199. */
  200. static struct gru_blade_state *gru_lock_kernel_context(int blade_id)
  201. {
  202. struct gru_blade_state *bs;
  203. STAT(lock_kernel_context);
  204. bs = gru_base[blade_id];
  205. down_read(&bs->bs_kgts_sema);
  206. if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
  207. gru_load_kernel_context(bs, blade_id);
  208. return bs;
  209. }
  210. /*
  211. * Unlock the kernel context for the specified blade. Context is not
  212. * unloaded but may be stolen before next use.
  213. */
  214. static void gru_unlock_kernel_context(int blade_id)
  215. {
  216. struct gru_blade_state *bs;
  217. bs = gru_base[blade_id];
  218. up_read(&bs->bs_kgts_sema);
  219. STAT(unlock_kernel_context);
  220. }
  221. /*
  222. * Reserve & get pointers to the DSR/CBRs reserved for the current cpu.
  223. * - returns with preemption disabled
  224. */
  225. static int gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
  226. {
  227. struct gru_blade_state *bs;
  228. int lcpu;
  229. BUG_ON(dsr_bytes > GRU_NUM_KERNEL_DSR_BYTES);
  230. preempt_disable();
  231. bs = gru_lock_kernel_context(uv_numa_blade_id());
  232. lcpu = uv_blade_processor_id();
  233. *cb = bs->kernel_cb + lcpu * GRU_HANDLE_STRIDE;
  234. *dsr = bs->kernel_dsr + lcpu * GRU_NUM_KERNEL_DSR_BYTES;
  235. return 0;
  236. }
  237. /*
  238. * Free the current cpus reserved DSR/CBR resources.
  239. */
  240. static void gru_free_cpu_resources(void *cb, void *dsr)
  241. {
  242. gru_unlock_kernel_context(uv_numa_blade_id());
  243. preempt_enable();
  244. }
  245. /*
  246. * Reserve GRU resources to be used asynchronously.
  247. * Note: currently supports only 1 reservation per blade.
  248. *
  249. * input:
  250. * blade_id - blade on which resources should be reserved
  251. * cbrs - number of CBRs
  252. * dsr_bytes - number of DSR bytes needed
  253. * output:
  254. * handle to identify resource
  255. * (0 = async resources already reserved)
  256. */
  257. unsigned long gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes,
  258. struct completion *cmp)
  259. {
  260. struct gru_blade_state *bs;
  261. struct gru_thread_state *kgts;
  262. int ret = 0;
  263. bs = gru_base[blade_id];
  264. down_write(&bs->bs_kgts_sema);
  265. /* Verify no resources already reserved */
  266. if (bs->bs_async_dsr_bytes + bs->bs_async_cbrs)
  267. goto done;
  268. bs->bs_async_dsr_bytes = dsr_bytes;
  269. bs->bs_async_cbrs = cbrs;
  270. bs->bs_async_wq = cmp;
  271. kgts = bs->bs_kgts;
  272. /* Resources changed. Unload context if already loaded */
  273. if (kgts && kgts->ts_gru)
  274. gru_unload_context(kgts, 0);
  275. ret = ASYNC_BID_TO_HAN(blade_id);
  276. done:
  277. up_write(&bs->bs_kgts_sema);
  278. return ret;
  279. }
  280. /*
  281. * Release async resources previously reserved.
  282. *
  283. * input:
  284. * han - handle to identify resources
  285. */
  286. void gru_release_async_resources(unsigned long han)
  287. {
  288. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  289. down_write(&bs->bs_kgts_sema);
  290. bs->bs_async_dsr_bytes = 0;
  291. bs->bs_async_cbrs = 0;
  292. bs->bs_async_wq = NULL;
  293. up_write(&bs->bs_kgts_sema);
  294. }
  295. /*
  296. * Wait for async GRU instructions to complete.
  297. *
  298. * input:
  299. * han - handle to identify resources
  300. */
  301. void gru_wait_async_cbr(unsigned long han)
  302. {
  303. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  304. wait_for_completion(bs->bs_async_wq);
  305. mb();
  306. }
  307. /*
  308. * Lock previous reserved async GRU resources
  309. *
  310. * input:
  311. * han - handle to identify resources
  312. * output:
  313. * cb - pointer to first CBR
  314. * dsr - pointer to first DSR
  315. */
  316. void gru_lock_async_resource(unsigned long han, void **cb, void **dsr)
  317. {
  318. struct gru_blade_state *bs = ASYNC_HAN_TO_BS(han);
  319. int blade_id = ASYNC_HAN_TO_BID(han);
  320. int ncpus;
  321. gru_lock_kernel_context(blade_id);
  322. ncpus = uv_blade_nr_possible_cpus(blade_id);
  323. if (cb)
  324. *cb = bs->kernel_cb + ncpus * GRU_HANDLE_STRIDE;
  325. if (dsr)
  326. *dsr = bs->kernel_dsr + ncpus * GRU_NUM_KERNEL_DSR_BYTES;
  327. }
  328. /*
  329. * Unlock previous reserved async GRU resources
  330. *
  331. * input:
  332. * han - handle to identify resources
  333. */
  334. void gru_unlock_async_resource(unsigned long han)
  335. {
  336. int blade_id = ASYNC_HAN_TO_BID(han);
  337. gru_unlock_kernel_context(blade_id);
  338. }
  339. /*----------------------------------------------------------------------*/
  340. int gru_get_cb_exception_detail(void *cb,
  341. struct control_block_extended_exc_detail *excdet)
  342. {
  343. struct gru_control_block_extended *cbe;
  344. struct gru_blade_state *bs;
  345. int cbrnum;
  346. bs = KCB_TO_BS(cb);
  347. cbrnum = thread_cbr_number(bs->bs_kgts, get_cb_number(cb));
  348. cbe = get_cbe(GRUBASE(cb), cbrnum);
  349. gru_flush_cache(cbe); /* CBE not coherent */
  350. excdet->opc = cbe->opccpy;
  351. excdet->exopc = cbe->exopccpy;
  352. excdet->ecause = cbe->ecause;
  353. excdet->exceptdet0 = cbe->idef1upd;
  354. excdet->exceptdet1 = cbe->idef3upd;
  355. gru_flush_cache(cbe);
  356. return 0;
  357. }
  358. char *gru_get_cb_exception_detail_str(int ret, void *cb,
  359. char *buf, int size)
  360. {
  361. struct gru_control_block_status *gen = (void *)cb;
  362. struct control_block_extended_exc_detail excdet;
  363. if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
  364. gru_get_cb_exception_detail(cb, &excdet);
  365. snprintf(buf, size,
  366. "GRU exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
  367. "excdet0 0x%lx, excdet1 0x%x",
  368. gen, excdet.opc, excdet.exopc, excdet.ecause,
  369. excdet.exceptdet0, excdet.exceptdet1);
  370. } else {
  371. snprintf(buf, size, "No exception");
  372. }
  373. return buf;
  374. }
  375. static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
  376. {
  377. while (gen->istatus >= CBS_ACTIVE) {
  378. cpu_relax();
  379. barrier();
  380. }
  381. return gen->istatus;
  382. }
  383. static int gru_retry_exception(void *cb)
  384. {
  385. struct gru_control_block_status *gen = (void *)cb;
  386. struct control_block_extended_exc_detail excdet;
  387. int retry = EXCEPTION_RETRY_LIMIT;
  388. while (1) {
  389. if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
  390. return CBS_IDLE;
  391. if (gru_get_cb_message_queue_substatus(cb))
  392. return CBS_EXCEPTION;
  393. gru_get_cb_exception_detail(cb, &excdet);
  394. if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
  395. (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
  396. break;
  397. if (retry-- == 0)
  398. break;
  399. gen->icmd = 1;
  400. gru_flush_cache(gen);
  401. }
  402. return CBS_EXCEPTION;
  403. }
  404. int gru_check_status_proc(void *cb)
  405. {
  406. struct gru_control_block_status *gen = (void *)cb;
  407. int ret;
  408. ret = gen->istatus;
  409. if (ret != CBS_EXCEPTION)
  410. return ret;
  411. return gru_retry_exception(cb);
  412. }
  413. int gru_wait_proc(void *cb)
  414. {
  415. struct gru_control_block_status *gen = (void *)cb;
  416. int ret;
  417. ret = gru_wait_idle_or_exception(gen);
  418. if (ret == CBS_EXCEPTION)
  419. ret = gru_retry_exception(cb);
  420. return ret;
  421. }
  422. void gru_abort(int ret, void *cb, char *str)
  423. {
  424. char buf[GRU_EXC_STR_SIZE];
  425. panic("GRU FATAL ERROR: %s - %s\n", str,
  426. gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
  427. }
  428. void gru_wait_abort_proc(void *cb)
  429. {
  430. int ret;
  431. ret = gru_wait_proc(cb);
  432. if (ret)
  433. gru_abort(ret, cb, "gru_wait_abort");
  434. }
  435. /*------------------------------ MESSAGE QUEUES -----------------------------*/
  436. /* Internal status . These are NOT returned to the user. */
  437. #define MQIE_AGAIN -1 /* try again */
  438. /*
  439. * Save/restore the "present" flag that is in the second line of 2-line
  440. * messages
  441. */
  442. static inline int get_present2(void *p)
  443. {
  444. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  445. return mhdr->present;
  446. }
  447. static inline void restore_present2(void *p, int val)
  448. {
  449. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  450. mhdr->present = val;
  451. }
  452. /*
  453. * Create a message queue.
  454. * qlines - message queue size in cache lines. Includes 2-line header.
  455. */
  456. int gru_create_message_queue(struct gru_message_queue_desc *mqd,
  457. void *p, unsigned int bytes, int nasid, int vector, int apicid)
  458. {
  459. struct message_queue *mq = p;
  460. unsigned int qlines;
  461. qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
  462. memset(mq, 0, bytes);
  463. mq->start = &mq->data;
  464. mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
  465. mq->next = &mq->data;
  466. mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
  467. mq->qlines = qlines;
  468. mq->hstatus[0] = 0;
  469. mq->hstatus[1] = 1;
  470. mq->head = gru_mesq_head(2, qlines / 2 + 1);
  471. mqd->mq = mq;
  472. mqd->mq_gpa = uv_gpa(mq);
  473. mqd->qlines = qlines;
  474. mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
  475. mqd->interrupt_vector = vector;
  476. mqd->interrupt_apicid = apicid;
  477. return 0;
  478. }
  479. EXPORT_SYMBOL_GPL(gru_create_message_queue);
  480. /*
  481. * Send a NOOP message to a message queue
  482. * Returns:
  483. * 0 - if queue is full after the send. This is the normal case
  484. * but various races can change this.
  485. * -1 - if mesq sent successfully but queue not full
  486. * >0 - unexpected error. MQE_xxx returned
  487. */
  488. static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
  489. void *mesg)
  490. {
  491. const struct message_header noop_header = {
  492. .present = MQS_NOOP, .lines = 1};
  493. unsigned long m;
  494. int substatus, ret;
  495. struct message_header save_mhdr, *mhdr = mesg;
  496. STAT(mesq_noop);
  497. save_mhdr = *mhdr;
  498. *mhdr = noop_header;
  499. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
  500. ret = gru_wait(cb);
  501. if (ret) {
  502. substatus = gru_get_cb_message_queue_substatus(cb);
  503. switch (substatus) {
  504. case CBSS_NO_ERROR:
  505. STAT(mesq_noop_unexpected_error);
  506. ret = MQE_UNEXPECTED_CB_ERR;
  507. break;
  508. case CBSS_LB_OVERFLOWED:
  509. STAT(mesq_noop_lb_overflow);
  510. ret = MQE_CONGESTION;
  511. break;
  512. case CBSS_QLIMIT_REACHED:
  513. STAT(mesq_noop_qlimit_reached);
  514. ret = 0;
  515. break;
  516. case CBSS_AMO_NACKED:
  517. STAT(mesq_noop_amo_nacked);
  518. ret = MQE_CONGESTION;
  519. break;
  520. case CBSS_PUT_NACKED:
  521. STAT(mesq_noop_put_nacked);
  522. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  523. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
  524. IMA);
  525. if (gru_wait(cb) == CBS_IDLE)
  526. ret = MQIE_AGAIN;
  527. else
  528. ret = MQE_UNEXPECTED_CB_ERR;
  529. break;
  530. case CBSS_PAGE_OVERFLOW:
  531. default:
  532. BUG();
  533. }
  534. }
  535. *mhdr = save_mhdr;
  536. return ret;
  537. }
  538. /*
  539. * Handle a gru_mesq full.
  540. */
  541. static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
  542. void *mesg, int lines)
  543. {
  544. union gru_mesqhead mqh;
  545. unsigned int limit, head;
  546. unsigned long avalue;
  547. int half, qlines;
  548. /* Determine if switching to first/second half of q */
  549. avalue = gru_get_amo_value(cb);
  550. head = gru_get_amo_value_head(cb);
  551. limit = gru_get_amo_value_limit(cb);
  552. qlines = mqd->qlines;
  553. half = (limit != qlines);
  554. if (half)
  555. mqh = gru_mesq_head(qlines / 2 + 1, qlines);
  556. else
  557. mqh = gru_mesq_head(2, qlines / 2 + 1);
  558. /* Try to get lock for switching head pointer */
  559. gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
  560. if (gru_wait(cb) != CBS_IDLE)
  561. goto cberr;
  562. if (!gru_get_amo_value(cb)) {
  563. STAT(mesq_qf_locked);
  564. return MQE_QUEUE_FULL;
  565. }
  566. /* Got the lock. Send optional NOP if queue not full, */
  567. if (head != limit) {
  568. if (send_noop_message(cb, mqd, mesg)) {
  569. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
  570. XTYPE_DW, IMA);
  571. if (gru_wait(cb) != CBS_IDLE)
  572. goto cberr;
  573. STAT(mesq_qf_noop_not_full);
  574. return MQIE_AGAIN;
  575. }
  576. avalue++;
  577. }
  578. /* Then flip queuehead to other half of queue. */
  579. gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
  580. IMA);
  581. if (gru_wait(cb) != CBS_IDLE)
  582. goto cberr;
  583. /* If not successfully in swapping queue head, clear the hstatus lock */
  584. if (gru_get_amo_value(cb) != avalue) {
  585. STAT(mesq_qf_switch_head_failed);
  586. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
  587. IMA);
  588. if (gru_wait(cb) != CBS_IDLE)
  589. goto cberr;
  590. }
  591. return MQIE_AGAIN;
  592. cberr:
  593. STAT(mesq_qf_unexpected_error);
  594. return MQE_UNEXPECTED_CB_ERR;
  595. }
  596. /*
  597. * Send a cross-partition interrupt to the SSI that contains the target
  598. * message queue. Normally, the interrupt is automatically delivered by hardware
  599. * but some error conditions require explicit delivery.
  600. */
  601. static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
  602. {
  603. if (mqd->interrupt_vector)
  604. uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
  605. mqd->interrupt_vector);
  606. }
  607. /*
  608. * Handle a PUT failure. Note: if message was a 2-line message, one of the
  609. * lines might have successfully have been written. Before sending the
  610. * message, "present" must be cleared in BOTH lines to prevent the receiver
  611. * from prematurely seeing the full message.
  612. */
  613. static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
  614. void *mesg, int lines)
  615. {
  616. unsigned long m;
  617. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  618. if (lines == 2) {
  619. gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
  620. if (gru_wait(cb) != CBS_IDLE)
  621. return MQE_UNEXPECTED_CB_ERR;
  622. }
  623. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
  624. if (gru_wait(cb) != CBS_IDLE)
  625. return MQE_UNEXPECTED_CB_ERR;
  626. send_message_queue_interrupt(mqd);
  627. return MQE_OK;
  628. }
  629. /*
  630. * Handle a gru_mesq failure. Some of these failures are software recoverable
  631. * or retryable.
  632. */
  633. static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
  634. void *mesg, int lines)
  635. {
  636. int substatus, ret = 0;
  637. substatus = gru_get_cb_message_queue_substatus(cb);
  638. switch (substatus) {
  639. case CBSS_NO_ERROR:
  640. STAT(mesq_send_unexpected_error);
  641. ret = MQE_UNEXPECTED_CB_ERR;
  642. break;
  643. case CBSS_LB_OVERFLOWED:
  644. STAT(mesq_send_lb_overflow);
  645. ret = MQE_CONGESTION;
  646. break;
  647. case CBSS_QLIMIT_REACHED:
  648. STAT(mesq_send_qlimit_reached);
  649. ret = send_message_queue_full(cb, mqd, mesg, lines);
  650. break;
  651. case CBSS_AMO_NACKED:
  652. STAT(mesq_send_amo_nacked);
  653. ret = MQE_CONGESTION;
  654. break;
  655. case CBSS_PUT_NACKED:
  656. STAT(mesq_send_put_nacked);
  657. ret = send_message_put_nacked(cb, mqd, mesg, lines);
  658. break;
  659. default:
  660. BUG();
  661. }
  662. return ret;
  663. }
  664. /*
  665. * Send a message to a message queue
  666. * mqd message queue descriptor
  667. * mesg message. ust be vaddr within a GSEG
  668. * bytes message size (<= 2 CL)
  669. */
  670. int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
  671. unsigned int bytes)
  672. {
  673. struct message_header *mhdr;
  674. void *cb;
  675. void *dsr;
  676. int istatus, clines, ret;
  677. STAT(mesq_send);
  678. BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
  679. clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
  680. if (gru_get_cpu_resources(bytes, &cb, &dsr))
  681. return MQE_BUG_NO_RESOURCES;
  682. memcpy(dsr, mesg, bytes);
  683. mhdr = dsr;
  684. mhdr->present = MQS_FULL;
  685. mhdr->lines = clines;
  686. if (clines == 2) {
  687. mhdr->present2 = get_present2(mhdr);
  688. restore_present2(mhdr, MQS_FULL);
  689. }
  690. do {
  691. ret = MQE_OK;
  692. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
  693. istatus = gru_wait(cb);
  694. if (istatus != CBS_IDLE)
  695. ret = send_message_failure(cb, mqd, dsr, clines);
  696. } while (ret == MQIE_AGAIN);
  697. gru_free_cpu_resources(cb, dsr);
  698. if (ret)
  699. STAT(mesq_send_failed);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL_GPL(gru_send_message_gpa);
  703. /*
  704. * Advance the receive pointer for the queue to the next message.
  705. */
  706. void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
  707. {
  708. struct message_queue *mq = mqd->mq;
  709. struct message_header *mhdr = mq->next;
  710. void *next, *pnext;
  711. int half = -1;
  712. int lines = mhdr->lines;
  713. if (lines == 2)
  714. restore_present2(mhdr, MQS_EMPTY);
  715. mhdr->present = MQS_EMPTY;
  716. pnext = mq->next;
  717. next = pnext + GRU_CACHE_LINE_BYTES * lines;
  718. if (next == mq->limit) {
  719. next = mq->start;
  720. half = 1;
  721. } else if (pnext < mq->start2 && next >= mq->start2) {
  722. half = 0;
  723. }
  724. if (half >= 0)
  725. mq->hstatus[half] = 1;
  726. mq->next = next;
  727. }
  728. EXPORT_SYMBOL_GPL(gru_free_message);
  729. /*
  730. * Get next message from message queue. Return NULL if no message
  731. * present. User must call next_message() to move to next message.
  732. * rmq message queue
  733. */
  734. void *gru_get_next_message(struct gru_message_queue_desc *mqd)
  735. {
  736. struct message_queue *mq = mqd->mq;
  737. struct message_header *mhdr = mq->next;
  738. int present = mhdr->present;
  739. /* skip NOOP messages */
  740. STAT(mesq_receive);
  741. while (present == MQS_NOOP) {
  742. gru_free_message(mqd, mhdr);
  743. mhdr = mq->next;
  744. present = mhdr->present;
  745. }
  746. /* Wait for both halves of 2 line messages */
  747. if (present == MQS_FULL && mhdr->lines == 2 &&
  748. get_present2(mhdr) == MQS_EMPTY)
  749. present = MQS_EMPTY;
  750. if (!present) {
  751. STAT(mesq_receive_none);
  752. return NULL;
  753. }
  754. if (mhdr->lines == 2)
  755. restore_present2(mhdr, mhdr->present2);
  756. return mhdr;
  757. }
  758. EXPORT_SYMBOL_GPL(gru_get_next_message);
  759. /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
  760. /*
  761. * Copy a block of data using the GRU resources
  762. */
  763. int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
  764. unsigned int bytes)
  765. {
  766. void *cb;
  767. void *dsr;
  768. int ret;
  769. STAT(copy_gpa);
  770. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  771. return MQE_BUG_NO_RESOURCES;
  772. gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
  773. XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
  774. ret = gru_wait(cb);
  775. gru_free_cpu_resources(cb, dsr);
  776. return ret;
  777. }
  778. EXPORT_SYMBOL_GPL(gru_copy_gpa);
  779. /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
  780. /* Temp - will delete after we gain confidence in the GRU */
  781. static int quicktest0(unsigned long arg)
  782. {
  783. unsigned long word0;
  784. unsigned long word1;
  785. void *cb;
  786. void *dsr;
  787. unsigned long *p;
  788. int ret = -EIO;
  789. if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
  790. return MQE_BUG_NO_RESOURCES;
  791. p = dsr;
  792. word0 = MAGIC;
  793. word1 = 0;
  794. gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  795. if (gru_wait(cb) != CBS_IDLE) {
  796. printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
  797. goto done;
  798. }
  799. if (*p != MAGIC) {
  800. printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
  801. goto done;
  802. }
  803. gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  804. if (gru_wait(cb) != CBS_IDLE) {
  805. printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
  806. goto done;
  807. }
  808. if (word0 != word1 || word1 != MAGIC) {
  809. printk(KERN_DEBUG
  810. "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
  811. word1, MAGIC);
  812. goto done;
  813. }
  814. ret = 0;
  815. done:
  816. gru_free_cpu_resources(cb, dsr);
  817. return ret;
  818. }
  819. #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
  820. static int quicktest1(unsigned long arg)
  821. {
  822. struct gru_message_queue_desc mqd;
  823. void *p, *mq;
  824. unsigned long *dw;
  825. int i, ret = -EIO;
  826. char mes[GRU_CACHE_LINE_BYTES], *m;
  827. /* Need 1K cacheline aligned that does not cross page boundary */
  828. p = kmalloc(4096, 0);
  829. mq = ALIGNUP(p, 1024);
  830. memset(mes, 0xee, sizeof(mes));
  831. dw = mq;
  832. gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
  833. for (i = 0; i < 6; i++) {
  834. mes[8] = i;
  835. do {
  836. ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
  837. } while (ret == MQE_CONGESTION);
  838. if (ret)
  839. break;
  840. }
  841. if (ret != MQE_QUEUE_FULL || i != 4)
  842. goto done;
  843. for (i = 0; i < 6; i++) {
  844. m = gru_get_next_message(&mqd);
  845. if (!m || m[8] != i)
  846. break;
  847. gru_free_message(&mqd, m);
  848. }
  849. ret = (i == 4) ? 0 : -EIO;
  850. done:
  851. kfree(p);
  852. return ret;
  853. }
  854. static int quicktest2(unsigned long arg)
  855. {
  856. static DECLARE_COMPLETION(cmp);
  857. unsigned long han;
  858. int blade_id = 0;
  859. int numcb = 4;
  860. int ret = 0;
  861. unsigned long *buf;
  862. void *cb0, *cb;
  863. int i, k, istatus, bytes;
  864. bytes = numcb * 4 * 8;
  865. buf = kmalloc(bytes, GFP_KERNEL);
  866. if (!buf)
  867. return -ENOMEM;
  868. ret = -EBUSY;
  869. han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
  870. if (!han)
  871. goto done;
  872. gru_lock_async_resource(han, &cb0, NULL);
  873. memset(buf, 0xee, bytes);
  874. for (i = 0; i < numcb; i++)
  875. gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
  876. XTYPE_DW, 4, 1, IMA_INTERRUPT);
  877. ret = 0;
  878. for (k = 0; k < numcb; k++) {
  879. gru_wait_async_cbr(han);
  880. for (i = 0; i < numcb; i++) {
  881. cb = cb0 + i * GRU_HANDLE_STRIDE;
  882. istatus = gru_check_status(cb);
  883. if (istatus == CBS_ACTIVE)
  884. continue;
  885. if (istatus == CBS_EXCEPTION)
  886. ret = -EFAULT;
  887. else if (buf[i] || buf[i + 1] || buf[i + 2] ||
  888. buf[i + 3])
  889. ret = -EIO;
  890. }
  891. }
  892. BUG_ON(cmp.done);
  893. gru_unlock_async_resource(han);
  894. gru_release_async_resources(han);
  895. done:
  896. kfree(buf);
  897. return ret;
  898. }
  899. /*
  900. * Debugging only. User hook for various kernel tests
  901. * of driver & gru.
  902. */
  903. int gru_ktest(unsigned long arg)
  904. {
  905. int ret = -EINVAL;
  906. switch (arg & 0xff) {
  907. case 0:
  908. ret = quicktest0(arg);
  909. break;
  910. case 1:
  911. ret = quicktest1(arg);
  912. break;
  913. case 2:
  914. ret = quicktest2(arg);
  915. break;
  916. case 99:
  917. ret = gru_free_kernel_contexts();
  918. break;
  919. }
  920. return ret;
  921. }
  922. int gru_kservices_init(void)
  923. {
  924. return 0;
  925. }
  926. void gru_kservices_exit(void)
  927. {
  928. if (gru_free_kernel_contexts())
  929. BUG();
  930. }