grukservices.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * KERNEL SERVICES THAT USE THE GRU
  5. *
  6. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/device.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/delay.h>
  34. #include "gru.h"
  35. #include "grulib.h"
  36. #include "grutables.h"
  37. #include "grukservices.h"
  38. #include "gru_instructions.h"
  39. #include <asm/uv/uv_hub.h>
  40. /*
  41. * Kernel GRU Usage
  42. *
  43. * The following is an interim algorithm for management of kernel GRU
  44. * resources. This will likely be replaced when we better understand the
  45. * kernel/user requirements.
  46. *
  47. * Blade percpu resources reserved for kernel use. These resources are
  48. * reserved whenever the the kernel context for the blade is loaded. Note
  49. * that the kernel context is not guaranteed to be always available. It is
  50. * loaded on demand & can be stolen by a user if the user demand exceeds the
  51. * kernel demand. The kernel can always reload the kernel context but
  52. * a SLEEP may be required!!!.
  53. *
  54. * Async Overview:
  55. *
  56. * Each blade has one "kernel context" that owns GRU kernel resources
  57. * located on the blade. Kernel drivers use GRU resources in this context
  58. * for sending messages, zeroing memory, etc.
  59. *
  60. * The kernel context is dynamically loaded on demand. If it is not in
  61. * use by the kernel, the kernel context can be unloaded & given to a user.
  62. * The kernel context will be reloaded when needed. This may require that
  63. * a context be stolen from a user.
  64. * NOTE: frequent unloading/reloading of the kernel context is
  65. * expensive. We are depending on batch schedulers, cpusets, sane
  66. * drivers or some other mechanism to prevent the need for frequent
  67. * stealing/reloading.
  68. *
  69. * The kernel context consists of two parts:
  70. * - 1 CB & a few DSRs that are reserved for each cpu on the blade.
  71. * Each cpu has it's own private resources & does not share them
  72. * with other cpus. These resources are used serially, ie,
  73. * locked, used & unlocked on each call to a function in
  74. * grukservices.
  75. * (Now that we have dynamic loading of kernel contexts, I
  76. * may rethink this & allow sharing between cpus....)
  77. *
  78. * - Additional resources can be reserved long term & used directly
  79. * by UV drivers located in the kernel. Drivers using these GRU
  80. * resources can use asynchronous GRU instructions that send
  81. * interrupts on completion.
  82. * - these resources must be explicitly locked/unlocked
  83. * - locked resources prevent (obviously) the kernel
  84. * context from being unloaded.
  85. * - drivers using these resource directly issue their own
  86. * GRU instruction and must wait/check completion.
  87. *
  88. * When these resources are reserved, the caller can optionally
  89. * associate a wait_queue with the resources and use asynchronous
  90. * GRU instructions. When an async GRU instruction completes, the
  91. * driver will do a wakeup on the event.
  92. *
  93. */
  94. #define ASYNC_HAN_TO_BID(h) ((h) - 1)
  95. #define ASYNC_BID_TO_HAN(b) ((b) + 1)
  96. #define ASYNC_HAN_TO_BS(h) gru_base[ASYNC_HAN_TO_BID(h)]
  97. #define KCB_TO_GID(cb) ((cb - gru_start_vaddr) / \
  98. (GRU_SIZE * GRU_CHIPLETS_PER_BLADE))
  99. #define KCB_TO_BS(cb) gru_base[KCB_TO_GID(cb)]
  100. #define GRU_NUM_KERNEL_CBR 1
  101. #define GRU_NUM_KERNEL_DSR_BYTES 256
  102. #define GRU_NUM_KERNEL_DSR_CL (GRU_NUM_KERNEL_DSR_BYTES / \
  103. GRU_CACHE_LINE_BYTES)
  104. /* GRU instruction attributes for all instructions */
  105. #define IMA IMA_CB_DELAY
  106. /* GRU cacheline size is always 64 bytes - even on arches with 128 byte lines */
  107. #define __gru_cacheline_aligned__ \
  108. __attribute__((__aligned__(GRU_CACHE_LINE_BYTES)))
  109. #define MAGIC 0x1234567887654321UL
  110. /* Default retry count for GRU errors on kernel instructions */
  111. #define EXCEPTION_RETRY_LIMIT 3
  112. /* Status of message queue sections */
  113. #define MQS_EMPTY 0
  114. #define MQS_FULL 1
  115. #define MQS_NOOP 2
  116. /*----------------- RESOURCE MANAGEMENT -------------------------------------*/
  117. /* optimized for x86_64 */
  118. struct message_queue {
  119. union gru_mesqhead head __gru_cacheline_aligned__; /* CL 0 */
  120. int qlines; /* DW 1 */
  121. long hstatus[2];
  122. void *next __gru_cacheline_aligned__;/* CL 1 */
  123. void *limit;
  124. void *start;
  125. void *start2;
  126. char data ____cacheline_aligned; /* CL 2 */
  127. };
  128. /* First word in every message - used by mesq interface */
  129. struct message_header {
  130. char present;
  131. char present2;
  132. char lines;
  133. char fill;
  134. };
  135. #define HSTATUS(mq, h) ((mq) + offsetof(struct message_queue, hstatus[h]))
  136. /*
  137. * Reload the blade's kernel context into a GRU chiplet. Called holding
  138. * the bs_kgts_sema for READ. Will steal user contexts if necessary.
  139. */
  140. static void gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
  141. {
  142. struct gru_state *gru;
  143. struct gru_thread_state *kgts;
  144. void *vaddr;
  145. int ctxnum, ncpus;
  146. up_read(&bs->bs_kgts_sema);
  147. down_write(&bs->bs_kgts_sema);
  148. if (!bs->bs_kgts)
  149. bs->bs_kgts = gru_alloc_gts(NULL, 0, 0, 0, 0);
  150. kgts = bs->bs_kgts;
  151. if (!kgts->ts_gru) {
  152. STAT(load_kernel_context);
  153. ncpus = uv_blade_nr_possible_cpus(blade_id);
  154. kgts->ts_cbr_au_count = GRU_CB_COUNT_TO_AU(
  155. GRU_NUM_KERNEL_CBR * ncpus + bs->bs_async_cbrs);
  156. kgts->ts_dsr_au_count = GRU_DS_BYTES_TO_AU(
  157. GRU_NUM_KERNEL_DSR_BYTES * ncpus +
  158. bs->bs_async_dsr_bytes);
  159. while (!gru_assign_gru_context(kgts, blade_id)) {
  160. msleep(1);
  161. gru_steal_context(kgts, blade_id);
  162. }
  163. gru_load_context(kgts);
  164. gru = bs->bs_kgts->ts_gru;
  165. vaddr = gru->gs_gru_base_vaddr;
  166. ctxnum = kgts->ts_ctxnum;
  167. bs->kernel_cb = get_gseg_base_address_cb(vaddr, ctxnum, 0);
  168. bs->kernel_dsr = get_gseg_base_address_ds(vaddr, ctxnum, 0);
  169. }
  170. downgrade_write(&bs->bs_kgts_sema);
  171. }
  172. /*
  173. * Free all kernel contexts that are not currently in use.
  174. * Returns 0 if all freed, else number of inuse context.
  175. */
  176. static int gru_free_kernel_contexts(void)
  177. {
  178. struct gru_blade_state *bs;
  179. struct gru_thread_state *kgts;
  180. int bid, ret = 0;
  181. for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
  182. bs = gru_base[bid];
  183. if (!bs)
  184. continue;
  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. kfree(kgts);
  190. bs->bs_kgts = NULL;
  191. up_write(&bs->bs_kgts_sema);
  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. * Copy a block of data using the GRU resources
  763. */
  764. int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
  765. unsigned int bytes)
  766. {
  767. void *cb;
  768. void *dsr;
  769. int ret;
  770. STAT(copy_gpa);
  771. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  772. return MQE_BUG_NO_RESOURCES;
  773. gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
  774. XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
  775. ret = gru_wait(cb);
  776. gru_free_cpu_resources(cb, dsr);
  777. return ret;
  778. }
  779. EXPORT_SYMBOL_GPL(gru_copy_gpa);
  780. /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
  781. /* Temp - will delete after we gain confidence in the GRU */
  782. static int quicktest0(unsigned long arg)
  783. {
  784. unsigned long word0;
  785. unsigned long word1;
  786. void *cb;
  787. void *dsr;
  788. unsigned long *p;
  789. int ret = -EIO;
  790. if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
  791. return MQE_BUG_NO_RESOURCES;
  792. p = dsr;
  793. word0 = MAGIC;
  794. word1 = 0;
  795. gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  796. if (gru_wait(cb) != CBS_IDLE) {
  797. printk(KERN_DEBUG "GRU quicktest0: CBR failure 1\n");
  798. goto done;
  799. }
  800. if (*p != MAGIC) {
  801. printk(KERN_DEBUG "GRU: quicktest0 bad magic 0x%lx\n", *p);
  802. goto done;
  803. }
  804. gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  805. if (gru_wait(cb) != CBS_IDLE) {
  806. printk(KERN_DEBUG "GRU quicktest0: CBR failure 2\n");
  807. goto done;
  808. }
  809. if (word0 != word1 || word1 != MAGIC) {
  810. printk(KERN_DEBUG
  811. "GRU quicktest0 err: found 0x%lx, expected 0x%lx\n",
  812. word1, MAGIC);
  813. goto done;
  814. }
  815. ret = 0;
  816. done:
  817. gru_free_cpu_resources(cb, dsr);
  818. return ret;
  819. }
  820. #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
  821. static int quicktest1(unsigned long arg)
  822. {
  823. struct gru_message_queue_desc mqd;
  824. void *p, *mq;
  825. unsigned long *dw;
  826. int i, ret = -EIO;
  827. char mes[GRU_CACHE_LINE_BYTES], *m;
  828. /* Need 1K cacheline aligned that does not cross page boundary */
  829. p = kmalloc(4096, 0);
  830. mq = ALIGNUP(p, 1024);
  831. memset(mes, 0xee, sizeof(mes));
  832. dw = mq;
  833. gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
  834. for (i = 0; i < 6; i++) {
  835. mes[8] = i;
  836. do {
  837. ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
  838. } while (ret == MQE_CONGESTION);
  839. if (ret)
  840. break;
  841. }
  842. if (ret != MQE_QUEUE_FULL || i != 4)
  843. goto done;
  844. for (i = 0; i < 6; i++) {
  845. m = gru_get_next_message(&mqd);
  846. if (!m || m[8] != i)
  847. break;
  848. gru_free_message(&mqd, m);
  849. }
  850. ret = (i == 4) ? 0 : -EIO;
  851. done:
  852. kfree(p);
  853. return ret;
  854. }
  855. static int quicktest2(unsigned long arg)
  856. {
  857. static DECLARE_COMPLETION(cmp);
  858. unsigned long han;
  859. int blade_id = 0;
  860. int numcb = 4;
  861. int ret = 0;
  862. unsigned long *buf;
  863. void *cb0, *cb;
  864. int i, k, istatus, bytes;
  865. bytes = numcb * 4 * 8;
  866. buf = kmalloc(bytes, GFP_KERNEL);
  867. if (!buf)
  868. return -ENOMEM;
  869. ret = -EBUSY;
  870. han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
  871. if (!han)
  872. goto done;
  873. gru_lock_async_resource(han, &cb0, NULL);
  874. memset(buf, 0xee, bytes);
  875. for (i = 0; i < numcb; i++)
  876. gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
  877. XTYPE_DW, 4, 1, IMA_INTERRUPT);
  878. ret = 0;
  879. for (k = 0; k < numcb; k++) {
  880. gru_wait_async_cbr(han);
  881. for (i = 0; i < numcb; i++) {
  882. cb = cb0 + i * GRU_HANDLE_STRIDE;
  883. istatus = gru_check_status(cb);
  884. if (istatus == CBS_ACTIVE)
  885. continue;
  886. if (istatus == CBS_EXCEPTION)
  887. ret = -EFAULT;
  888. else if (buf[i] || buf[i + 1] || buf[i + 2] ||
  889. buf[i + 3])
  890. ret = -EIO;
  891. }
  892. }
  893. BUG_ON(cmp.done);
  894. gru_unlock_async_resource(han);
  895. gru_release_async_resources(han);
  896. done:
  897. kfree(buf);
  898. return ret;
  899. }
  900. /*
  901. * Debugging only. User hook for various kernel tests
  902. * of driver & gru.
  903. */
  904. int gru_ktest(unsigned long arg)
  905. {
  906. int ret = -EINVAL;
  907. switch (arg & 0xff) {
  908. case 0:
  909. ret = quicktest0(arg);
  910. break;
  911. case 1:
  912. ret = quicktest1(arg);
  913. break;
  914. case 2:
  915. ret = quicktest2(arg);
  916. break;
  917. case 99:
  918. ret = gru_free_kernel_contexts();
  919. break;
  920. }
  921. return ret;
  922. }
  923. int gru_kservices_init(void)
  924. {
  925. return 0;
  926. }
  927. void gru_kservices_exit(void)
  928. {
  929. if (gru_free_kernel_contexts())
  930. BUG();
  931. }