grukservices.c 29 KB

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