grukservices.c 29 KB

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