grukservices.c 29 KB

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