grukservices.c 29 KB

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