grukservices.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. 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_thread_state *kgts = NULL;
  345. unsigned long off;
  346. int cbrnum, bid;
  347. /*
  348. * Locate kgts for cb. This algorithm is SLOW but
  349. * this function is rarely called (ie., almost never).
  350. * Performance does not matter.
  351. */
  352. for_each_possible_blade(bid) {
  353. if (!gru_base[bid])
  354. break;
  355. kgts = gru_base[bid]->bs_kgts;
  356. if (!kgts || !kgts->ts_gru)
  357. continue;
  358. off = cb - kgts->ts_gru->gs_gru_base_vaddr;
  359. if (off < GRU_SIZE)
  360. break;
  361. kgts = NULL;
  362. }
  363. BUG_ON(!kgts);
  364. cbrnum = thread_cbr_number(kgts, get_cb_number(cb));
  365. cbe = get_cbe(GRUBASE(cb), cbrnum);
  366. gru_flush_cache(cbe); /* CBE not coherent */
  367. sync_core();
  368. excdet->opc = cbe->opccpy;
  369. excdet->exopc = cbe->exopccpy;
  370. excdet->ecause = cbe->ecause;
  371. excdet->exceptdet0 = cbe->idef1upd;
  372. excdet->exceptdet1 = cbe->idef3upd;
  373. gru_flush_cache(cbe);
  374. return 0;
  375. }
  376. char *gru_get_cb_exception_detail_str(int ret, void *cb,
  377. char *buf, int size)
  378. {
  379. struct gru_control_block_status *gen = (void *)cb;
  380. struct control_block_extended_exc_detail excdet;
  381. if (ret > 0 && gen->istatus == CBS_EXCEPTION) {
  382. gru_get_cb_exception_detail(cb, &excdet);
  383. snprintf(buf, size,
  384. "GRU:%d exception: cb %p, opc %d, exopc %d, ecause 0x%x,"
  385. "excdet0 0x%lx, excdet1 0x%x", smp_processor_id(),
  386. gen, excdet.opc, excdet.exopc, excdet.ecause,
  387. excdet.exceptdet0, excdet.exceptdet1);
  388. } else {
  389. snprintf(buf, size, "No exception");
  390. }
  391. return buf;
  392. }
  393. static int gru_wait_idle_or_exception(struct gru_control_block_status *gen)
  394. {
  395. while (gen->istatus >= CBS_ACTIVE) {
  396. cpu_relax();
  397. barrier();
  398. }
  399. return gen->istatus;
  400. }
  401. static int gru_retry_exception(void *cb)
  402. {
  403. struct gru_control_block_status *gen = (void *)cb;
  404. struct control_block_extended_exc_detail excdet;
  405. int retry = EXCEPTION_RETRY_LIMIT;
  406. while (1) {
  407. if (gru_wait_idle_or_exception(gen) == CBS_IDLE)
  408. return CBS_IDLE;
  409. if (gru_get_cb_message_queue_substatus(cb))
  410. return CBS_EXCEPTION;
  411. gru_get_cb_exception_detail(cb, &excdet);
  412. if ((excdet.ecause & ~EXCEPTION_RETRY_BITS) ||
  413. (excdet.cbrexecstatus & CBR_EXS_ABORT_OCC))
  414. break;
  415. if (retry-- == 0)
  416. break;
  417. gen->icmd = 1;
  418. gru_flush_cache(gen);
  419. }
  420. return CBS_EXCEPTION;
  421. }
  422. int gru_check_status_proc(void *cb)
  423. {
  424. struct gru_control_block_status *gen = (void *)cb;
  425. int ret;
  426. ret = gen->istatus;
  427. if (ret == CBS_EXCEPTION)
  428. ret = gru_retry_exception(cb);
  429. rmb();
  430. return ret;
  431. }
  432. int gru_wait_proc(void *cb)
  433. {
  434. struct gru_control_block_status *gen = (void *)cb;
  435. int ret;
  436. ret = gru_wait_idle_or_exception(gen);
  437. if (ret == CBS_EXCEPTION)
  438. ret = gru_retry_exception(cb);
  439. rmb();
  440. return ret;
  441. }
  442. void gru_abort(int ret, void *cb, char *str)
  443. {
  444. char buf[GRU_EXC_STR_SIZE];
  445. panic("GRU FATAL ERROR: %s - %s\n", str,
  446. gru_get_cb_exception_detail_str(ret, cb, buf, sizeof(buf)));
  447. }
  448. void gru_wait_abort_proc(void *cb)
  449. {
  450. int ret;
  451. ret = gru_wait_proc(cb);
  452. if (ret)
  453. gru_abort(ret, cb, "gru_wait_abort");
  454. }
  455. /*------------------------------ MESSAGE QUEUES -----------------------------*/
  456. /* Internal status . These are NOT returned to the user. */
  457. #define MQIE_AGAIN -1 /* try again */
  458. /*
  459. * Save/restore the "present" flag that is in the second line of 2-line
  460. * messages
  461. */
  462. static inline int get_present2(void *p)
  463. {
  464. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  465. return mhdr->present;
  466. }
  467. static inline void restore_present2(void *p, int val)
  468. {
  469. struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
  470. mhdr->present = val;
  471. }
  472. /*
  473. * Create a message queue.
  474. * qlines - message queue size in cache lines. Includes 2-line header.
  475. */
  476. int gru_create_message_queue(struct gru_message_queue_desc *mqd,
  477. void *p, unsigned int bytes, int nasid, int vector, int apicid)
  478. {
  479. struct message_queue *mq = p;
  480. unsigned int qlines;
  481. qlines = bytes / GRU_CACHE_LINE_BYTES - 2;
  482. memset(mq, 0, bytes);
  483. mq->start = &mq->data;
  484. mq->start2 = &mq->data + (qlines / 2 - 1) * GRU_CACHE_LINE_BYTES;
  485. mq->next = &mq->data;
  486. mq->limit = &mq->data + (qlines - 2) * GRU_CACHE_LINE_BYTES;
  487. mq->qlines = qlines;
  488. mq->hstatus[0] = 0;
  489. mq->hstatus[1] = 1;
  490. mq->head = gru_mesq_head(2, qlines / 2 + 1);
  491. mqd->mq = mq;
  492. mqd->mq_gpa = uv_gpa(mq);
  493. mqd->qlines = qlines;
  494. mqd->interrupt_pnode = UV_NASID_TO_PNODE(nasid);
  495. mqd->interrupt_vector = vector;
  496. mqd->interrupt_apicid = apicid;
  497. return 0;
  498. }
  499. EXPORT_SYMBOL_GPL(gru_create_message_queue);
  500. /*
  501. * Send a NOOP message to a message queue
  502. * Returns:
  503. * 0 - if queue is full after the send. This is the normal case
  504. * but various races can change this.
  505. * -1 - if mesq sent successfully but queue not full
  506. * >0 - unexpected error. MQE_xxx returned
  507. */
  508. static int send_noop_message(void *cb, struct gru_message_queue_desc *mqd,
  509. void *mesg)
  510. {
  511. const struct message_header noop_header = {
  512. .present = MQS_NOOP, .lines = 1};
  513. unsigned long m;
  514. int substatus, ret;
  515. struct message_header save_mhdr, *mhdr = mesg;
  516. STAT(mesq_noop);
  517. save_mhdr = *mhdr;
  518. *mhdr = noop_header;
  519. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), 1, IMA);
  520. ret = gru_wait(cb);
  521. if (ret) {
  522. substatus = gru_get_cb_message_queue_substatus(cb);
  523. switch (substatus) {
  524. case CBSS_NO_ERROR:
  525. STAT(mesq_noop_unexpected_error);
  526. ret = MQE_UNEXPECTED_CB_ERR;
  527. break;
  528. case CBSS_LB_OVERFLOWED:
  529. STAT(mesq_noop_lb_overflow);
  530. ret = MQE_CONGESTION;
  531. break;
  532. case CBSS_QLIMIT_REACHED:
  533. STAT(mesq_noop_qlimit_reached);
  534. ret = 0;
  535. break;
  536. case CBSS_AMO_NACKED:
  537. STAT(mesq_noop_amo_nacked);
  538. ret = MQE_CONGESTION;
  539. break;
  540. case CBSS_PUT_NACKED:
  541. STAT(mesq_noop_put_nacked);
  542. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  543. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
  544. IMA);
  545. if (gru_wait(cb) == CBS_IDLE)
  546. ret = MQIE_AGAIN;
  547. else
  548. ret = MQE_UNEXPECTED_CB_ERR;
  549. break;
  550. case CBSS_PAGE_OVERFLOW:
  551. STAT(mesq_noop_page_overflow);
  552. /* fallthru */
  553. default:
  554. BUG();
  555. }
  556. }
  557. *mhdr = save_mhdr;
  558. return ret;
  559. }
  560. /*
  561. * Handle a gru_mesq full.
  562. */
  563. static int send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd,
  564. void *mesg, int lines)
  565. {
  566. union gru_mesqhead mqh;
  567. unsigned int limit, head;
  568. unsigned long avalue;
  569. int half, qlines;
  570. /* Determine if switching to first/second half of q */
  571. avalue = gru_get_amo_value(cb);
  572. head = gru_get_amo_value_head(cb);
  573. limit = gru_get_amo_value_limit(cb);
  574. qlines = mqd->qlines;
  575. half = (limit != qlines);
  576. if (half)
  577. mqh = gru_mesq_head(qlines / 2 + 1, qlines);
  578. else
  579. mqh = gru_mesq_head(2, qlines / 2 + 1);
  580. /* Try to get lock for switching head pointer */
  581. gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA);
  582. if (gru_wait(cb) != CBS_IDLE)
  583. goto cberr;
  584. if (!gru_get_amo_value(cb)) {
  585. STAT(mesq_qf_locked);
  586. return MQE_QUEUE_FULL;
  587. }
  588. /* Got the lock. Send optional NOP if queue not full, */
  589. if (head != limit) {
  590. if (send_noop_message(cb, mqd, mesg)) {
  591. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
  592. XTYPE_DW, IMA);
  593. if (gru_wait(cb) != CBS_IDLE)
  594. goto cberr;
  595. STAT(mesq_qf_noop_not_full);
  596. return MQIE_AGAIN;
  597. }
  598. avalue++;
  599. }
  600. /* Then flip queuehead to other half of queue. */
  601. gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
  602. IMA);
  603. if (gru_wait(cb) != CBS_IDLE)
  604. goto cberr;
  605. /* If not successfully in swapping queue head, clear the hstatus lock */
  606. if (gru_get_amo_value(cb) != avalue) {
  607. STAT(mesq_qf_switch_head_failed);
  608. gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
  609. IMA);
  610. if (gru_wait(cb) != CBS_IDLE)
  611. goto cberr;
  612. }
  613. return MQIE_AGAIN;
  614. cberr:
  615. STAT(mesq_qf_unexpected_error);
  616. return MQE_UNEXPECTED_CB_ERR;
  617. }
  618. /*
  619. * Send a cross-partition interrupt to the SSI that contains the target
  620. * message queue. Normally, the interrupt is automatically delivered by hardware
  621. * but some error conditions require explicit delivery.
  622. */
  623. static void send_message_queue_interrupt(struct gru_message_queue_desc *mqd)
  624. {
  625. if (mqd->interrupt_vector)
  626. uv_hub_send_ipi(mqd->interrupt_pnode, mqd->interrupt_apicid,
  627. mqd->interrupt_vector);
  628. }
  629. /*
  630. * Handle a PUT failure. Note: if message was a 2-line message, one of the
  631. * lines might have successfully have been written. Before sending the
  632. * message, "present" must be cleared in BOTH lines to prevent the receiver
  633. * from prematurely seeing the full message.
  634. */
  635. static int send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd,
  636. void *mesg, int lines)
  637. {
  638. unsigned long m;
  639. m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
  640. if (lines == 2) {
  641. gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA);
  642. if (gru_wait(cb) != CBS_IDLE)
  643. return MQE_UNEXPECTED_CB_ERR;
  644. }
  645. gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA);
  646. if (gru_wait(cb) != CBS_IDLE)
  647. return MQE_UNEXPECTED_CB_ERR;
  648. send_message_queue_interrupt(mqd);
  649. return MQE_OK;
  650. }
  651. /*
  652. * Handle a gru_mesq failure. Some of these failures are software recoverable
  653. * or retryable.
  654. */
  655. static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
  656. void *mesg, int lines)
  657. {
  658. int substatus, ret = 0;
  659. substatus = gru_get_cb_message_queue_substatus(cb);
  660. switch (substatus) {
  661. case CBSS_NO_ERROR:
  662. STAT(mesq_send_unexpected_error);
  663. ret = MQE_UNEXPECTED_CB_ERR;
  664. break;
  665. case CBSS_LB_OVERFLOWED:
  666. STAT(mesq_send_lb_overflow);
  667. ret = MQE_CONGESTION;
  668. break;
  669. case CBSS_QLIMIT_REACHED:
  670. STAT(mesq_send_qlimit_reached);
  671. ret = send_message_queue_full(cb, mqd, mesg, lines);
  672. break;
  673. case CBSS_AMO_NACKED:
  674. STAT(mesq_send_amo_nacked);
  675. ret = MQE_CONGESTION;
  676. break;
  677. case CBSS_PUT_NACKED:
  678. STAT(mesq_send_put_nacked);
  679. ret = send_message_put_nacked(cb, mqd, mesg, lines);
  680. break;
  681. case CBSS_PAGE_OVERFLOW:
  682. STAT(mesq_page_overflow);
  683. /* fallthru */
  684. default:
  685. BUG();
  686. }
  687. return ret;
  688. }
  689. /*
  690. * Send a message to a message queue
  691. * mqd message queue descriptor
  692. * mesg message. ust be vaddr within a GSEG
  693. * bytes message size (<= 2 CL)
  694. */
  695. int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
  696. unsigned int bytes)
  697. {
  698. struct message_header *mhdr;
  699. void *cb;
  700. void *dsr;
  701. int istatus, clines, ret;
  702. STAT(mesq_send);
  703. BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
  704. clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
  705. if (gru_get_cpu_resources(bytes, &cb, &dsr))
  706. return MQE_BUG_NO_RESOURCES;
  707. memcpy(dsr, mesg, bytes);
  708. mhdr = dsr;
  709. mhdr->present = MQS_FULL;
  710. mhdr->lines = clines;
  711. if (clines == 2) {
  712. mhdr->present2 = get_present2(mhdr);
  713. restore_present2(mhdr, MQS_FULL);
  714. }
  715. do {
  716. ret = MQE_OK;
  717. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
  718. istatus = gru_wait(cb);
  719. if (istatus != CBS_IDLE)
  720. ret = send_message_failure(cb, mqd, dsr, clines);
  721. } while (ret == MQIE_AGAIN);
  722. gru_free_cpu_resources(cb, dsr);
  723. if (ret)
  724. STAT(mesq_send_failed);
  725. return ret;
  726. }
  727. EXPORT_SYMBOL_GPL(gru_send_message_gpa);
  728. /*
  729. * Advance the receive pointer for the queue to the next message.
  730. */
  731. void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
  732. {
  733. struct message_queue *mq = mqd->mq;
  734. struct message_header *mhdr = mq->next;
  735. void *next, *pnext;
  736. int half = -1;
  737. int lines = mhdr->lines;
  738. if (lines == 2)
  739. restore_present2(mhdr, MQS_EMPTY);
  740. mhdr->present = MQS_EMPTY;
  741. pnext = mq->next;
  742. next = pnext + GRU_CACHE_LINE_BYTES * lines;
  743. if (next == mq->limit) {
  744. next = mq->start;
  745. half = 1;
  746. } else if (pnext < mq->start2 && next >= mq->start2) {
  747. half = 0;
  748. }
  749. if (half >= 0)
  750. mq->hstatus[half] = 1;
  751. mq->next = next;
  752. }
  753. EXPORT_SYMBOL_GPL(gru_free_message);
  754. /*
  755. * Get next message from message queue. Return NULL if no message
  756. * present. User must call next_message() to move to next message.
  757. * rmq message queue
  758. */
  759. void *gru_get_next_message(struct gru_message_queue_desc *mqd)
  760. {
  761. struct message_queue *mq = mqd->mq;
  762. struct message_header *mhdr = mq->next;
  763. int present = mhdr->present;
  764. /* skip NOOP messages */
  765. while (present == MQS_NOOP) {
  766. gru_free_message(mqd, mhdr);
  767. mhdr = mq->next;
  768. present = mhdr->present;
  769. }
  770. /* Wait for both halves of 2 line messages */
  771. if (present == MQS_FULL && mhdr->lines == 2 &&
  772. get_present2(mhdr) == MQS_EMPTY)
  773. present = MQS_EMPTY;
  774. if (!present) {
  775. STAT(mesq_receive_none);
  776. return NULL;
  777. }
  778. if (mhdr->lines == 2)
  779. restore_present2(mhdr, mhdr->present2);
  780. STAT(mesq_receive);
  781. return mhdr;
  782. }
  783. EXPORT_SYMBOL_GPL(gru_get_next_message);
  784. /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
  785. /*
  786. * Load a DW from a global GPA. The GPA can be a memory or MMR address.
  787. */
  788. int gru_read_gpa(unsigned long *value, unsigned long gpa)
  789. {
  790. void *cb;
  791. void *dsr;
  792. int ret, iaa;
  793. STAT(read_gpa);
  794. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  795. return MQE_BUG_NO_RESOURCES;
  796. iaa = gpa >> 62;
  797. gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
  798. ret = gru_wait(cb);
  799. if (ret == CBS_IDLE)
  800. *value = *(unsigned long *)dsr;
  801. gru_free_cpu_resources(cb, dsr);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL_GPL(gru_read_gpa);
  805. /*
  806. * Copy a block of data using the GRU resources
  807. */
  808. int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
  809. unsigned int bytes)
  810. {
  811. void *cb;
  812. void *dsr;
  813. int ret;
  814. STAT(copy_gpa);
  815. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  816. return MQE_BUG_NO_RESOURCES;
  817. gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
  818. XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
  819. ret = gru_wait(cb);
  820. gru_free_cpu_resources(cb, dsr);
  821. return ret;
  822. }
  823. EXPORT_SYMBOL_GPL(gru_copy_gpa);
  824. /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
  825. /* Temp - will delete after we gain confidence in the GRU */
  826. static int quicktest0(unsigned long arg)
  827. {
  828. unsigned long word0;
  829. unsigned long word1;
  830. void *cb;
  831. void *dsr;
  832. unsigned long *p;
  833. int ret = -EIO;
  834. if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
  835. return MQE_BUG_NO_RESOURCES;
  836. p = dsr;
  837. word0 = MAGIC;
  838. word1 = 0;
  839. gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  840. if (gru_wait(cb) != CBS_IDLE) {
  841. printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
  842. goto done;
  843. }
  844. if (*p != MAGIC) {
  845. printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
  846. goto done;
  847. }
  848. gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  849. if (gru_wait(cb) != CBS_IDLE) {
  850. printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
  851. goto done;
  852. }
  853. if (word0 != word1 || word1 != MAGIC) {
  854. printk(KERN_DEBUG
  855. "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
  856. smp_processor_id(), word1, MAGIC);
  857. goto done;
  858. }
  859. ret = 0;
  860. done:
  861. gru_free_cpu_resources(cb, dsr);
  862. return ret;
  863. }
  864. #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
  865. static int quicktest1(unsigned long arg)
  866. {
  867. struct gru_message_queue_desc mqd;
  868. void *p, *mq;
  869. unsigned long *dw;
  870. int i, ret = -EIO;
  871. char mes[GRU_CACHE_LINE_BYTES], *m;
  872. /* Need 1K cacheline aligned that does not cross page boundary */
  873. p = kmalloc(4096, 0);
  874. if (p == NULL)
  875. return -ENOMEM;
  876. mq = ALIGNUP(p, 1024);
  877. memset(mes, 0xee, sizeof(mes));
  878. dw = mq;
  879. gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
  880. for (i = 0; i < 6; i++) {
  881. mes[8] = i;
  882. do {
  883. ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
  884. } while (ret == MQE_CONGESTION);
  885. if (ret)
  886. break;
  887. }
  888. if (ret != MQE_QUEUE_FULL || i != 4) {
  889. printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
  890. smp_processor_id(), ret, i);
  891. goto done;
  892. }
  893. for (i = 0; i < 6; i++) {
  894. m = gru_get_next_message(&mqd);
  895. if (!m || m[8] != i)
  896. break;
  897. gru_free_message(&mqd, m);
  898. }
  899. if (i != 4) {
  900. printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
  901. smp_processor_id(), i, m, m ? m[8] : -1);
  902. goto done;
  903. }
  904. ret = 0;
  905. done:
  906. kfree(p);
  907. return ret;
  908. }
  909. static int quicktest2(unsigned long arg)
  910. {
  911. static DECLARE_COMPLETION(cmp);
  912. unsigned long han;
  913. int blade_id = 0;
  914. int numcb = 4;
  915. int ret = 0;
  916. unsigned long *buf;
  917. void *cb0, *cb;
  918. struct gru_control_block_status *gen;
  919. int i, k, istatus, bytes;
  920. bytes = numcb * 4 * 8;
  921. buf = kmalloc(bytes, GFP_KERNEL);
  922. if (!buf)
  923. return -ENOMEM;
  924. ret = -EBUSY;
  925. han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
  926. if (!han)
  927. goto done;
  928. gru_lock_async_resource(han, &cb0, NULL);
  929. memset(buf, 0xee, bytes);
  930. for (i = 0; i < numcb; i++)
  931. gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
  932. XTYPE_DW, 4, 1, IMA_INTERRUPT);
  933. ret = 0;
  934. k = numcb;
  935. do {
  936. gru_wait_async_cbr(han);
  937. for (i = 0; i < numcb; i++) {
  938. cb = cb0 + i * GRU_HANDLE_STRIDE;
  939. istatus = gru_check_status(cb);
  940. if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
  941. break;
  942. }
  943. if (i == numcb)
  944. continue;
  945. if (istatus != CBS_IDLE) {
  946. printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
  947. ret = -EFAULT;
  948. } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
  949. buf[4 * i + 3]) {
  950. printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
  951. smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
  952. ret = -EIO;
  953. }
  954. k--;
  955. gen = cb;
  956. gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
  957. } while (k);
  958. BUG_ON(cmp.done);
  959. gru_unlock_async_resource(han);
  960. gru_release_async_resources(han);
  961. done:
  962. kfree(buf);
  963. return ret;
  964. }
  965. #define BUFSIZE 200
  966. static int quicktest3(unsigned long arg)
  967. {
  968. char buf1[BUFSIZE], buf2[BUFSIZE];
  969. int ret = 0;
  970. memset(buf2, 0, sizeof(buf2));
  971. memset(buf1, get_cycles() & 255, sizeof(buf1));
  972. gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
  973. if (memcmp(buf1, buf2, BUFSIZE)) {
  974. printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
  975. ret = -EIO;
  976. }
  977. return ret;
  978. }
  979. /*
  980. * Debugging only. User hook for various kernel tests
  981. * of driver & gru.
  982. */
  983. int gru_ktest(unsigned long arg)
  984. {
  985. int ret = -EINVAL;
  986. switch (arg & 0xff) {
  987. case 0:
  988. ret = quicktest0(arg);
  989. break;
  990. case 1:
  991. ret = quicktest1(arg);
  992. break;
  993. case 2:
  994. ret = quicktest2(arg);
  995. break;
  996. case 3:
  997. ret = quicktest3(arg);
  998. break;
  999. case 99:
  1000. ret = gru_free_kernel_contexts();
  1001. break;
  1002. }
  1003. return ret;
  1004. }
  1005. int gru_kservices_init(void)
  1006. {
  1007. return 0;
  1008. }
  1009. void gru_kservices_exit(void)
  1010. {
  1011. if (gru_free_kernel_contexts())
  1012. BUG();
  1013. }