grukservices.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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;
  643. int ret, loops = 200; /* experimentally determined */
  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 noop message in order to deliver a cross-partition interrupt
  657. * to the SSI that contains the target message queue. Normally, the
  658. * interrupt is automatically delivered by hardware following mesq
  659. * operations, but some error conditions require explicit delivery.
  660. * The noop message will trigger delivery. Otherwise partition failures
  661. * could cause unrecovered errors.
  662. */
  663. do {
  664. ret = send_noop_message(cb, mqd, mesg);
  665. } while ((ret == MQIE_AGAIN || ret == MQE_CONGESTION) && (loops-- > 0));
  666. if (ret == MQIE_AGAIN || ret == MQE_CONGESTION) {
  667. /*
  668. * Don't indicate to the app to resend the message, as it's
  669. * already been successfully sent. We simply send an OK
  670. * (rather than fail the send with MQE_UNEXPECTED_CB_ERR),
  671. * assuming that the other side is receiving enough
  672. * interrupts to get this message processed anyway.
  673. */
  674. ret = MQE_OK;
  675. }
  676. return ret;
  677. }
  678. /*
  679. * Handle a gru_mesq failure. Some of these failures are software recoverable
  680. * or retryable.
  681. */
  682. static int send_message_failure(void *cb, struct gru_message_queue_desc *mqd,
  683. void *mesg, int lines)
  684. {
  685. int substatus, ret = 0;
  686. substatus = gru_get_cb_message_queue_substatus(cb);
  687. switch (substatus) {
  688. case CBSS_NO_ERROR:
  689. STAT(mesq_send_unexpected_error);
  690. ret = MQE_UNEXPECTED_CB_ERR;
  691. break;
  692. case CBSS_LB_OVERFLOWED:
  693. STAT(mesq_send_lb_overflow);
  694. ret = MQE_CONGESTION;
  695. break;
  696. case CBSS_QLIMIT_REACHED:
  697. STAT(mesq_send_qlimit_reached);
  698. ret = send_message_queue_full(cb, mqd, mesg, lines);
  699. break;
  700. case CBSS_AMO_NACKED:
  701. STAT(mesq_send_amo_nacked);
  702. ret = MQE_CONGESTION;
  703. break;
  704. case CBSS_PUT_NACKED:
  705. STAT(mesq_send_put_nacked);
  706. ret = send_message_put_nacked(cb, mqd, mesg, lines);
  707. break;
  708. case CBSS_PAGE_OVERFLOW:
  709. STAT(mesq_page_overflow);
  710. /* fallthru */
  711. default:
  712. BUG();
  713. }
  714. return ret;
  715. }
  716. /*
  717. * Send a message to a message queue
  718. * mqd message queue descriptor
  719. * mesg message. ust be vaddr within a GSEG
  720. * bytes message size (<= 2 CL)
  721. */
  722. int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg,
  723. unsigned int bytes)
  724. {
  725. struct message_header *mhdr;
  726. void *cb;
  727. void *dsr;
  728. int istatus, clines, ret;
  729. STAT(mesq_send);
  730. BUG_ON(bytes < sizeof(int) || bytes > 2 * GRU_CACHE_LINE_BYTES);
  731. clines = DIV_ROUND_UP(bytes, GRU_CACHE_LINE_BYTES);
  732. if (gru_get_cpu_resources(bytes, &cb, &dsr))
  733. return MQE_BUG_NO_RESOURCES;
  734. memcpy(dsr, mesg, bytes);
  735. mhdr = dsr;
  736. mhdr->present = MQS_FULL;
  737. mhdr->lines = clines;
  738. if (clines == 2) {
  739. mhdr->present2 = get_present2(mhdr);
  740. restore_present2(mhdr, MQS_FULL);
  741. }
  742. do {
  743. ret = MQE_OK;
  744. gru_mesq(cb, mqd->mq_gpa, gru_get_tri(mhdr), clines, IMA);
  745. istatus = gru_wait(cb);
  746. if (istatus != CBS_IDLE)
  747. ret = send_message_failure(cb, mqd, dsr, clines);
  748. } while (ret == MQIE_AGAIN);
  749. gru_free_cpu_resources(cb, dsr);
  750. if (ret)
  751. STAT(mesq_send_failed);
  752. return ret;
  753. }
  754. EXPORT_SYMBOL_GPL(gru_send_message_gpa);
  755. /*
  756. * Advance the receive pointer for the queue to the next message.
  757. */
  758. void gru_free_message(struct gru_message_queue_desc *mqd, void *mesg)
  759. {
  760. struct message_queue *mq = mqd->mq;
  761. struct message_header *mhdr = mq->next;
  762. void *next, *pnext;
  763. int half = -1;
  764. int lines = mhdr->lines;
  765. if (lines == 2)
  766. restore_present2(mhdr, MQS_EMPTY);
  767. mhdr->present = MQS_EMPTY;
  768. pnext = mq->next;
  769. next = pnext + GRU_CACHE_LINE_BYTES * lines;
  770. if (next == mq->limit) {
  771. next = mq->start;
  772. half = 1;
  773. } else if (pnext < mq->start2 && next >= mq->start2) {
  774. half = 0;
  775. }
  776. if (half >= 0)
  777. mq->hstatus[half] = 1;
  778. mq->next = next;
  779. }
  780. EXPORT_SYMBOL_GPL(gru_free_message);
  781. /*
  782. * Get next message from message queue. Return NULL if no message
  783. * present. User must call next_message() to move to next message.
  784. * rmq message queue
  785. */
  786. void *gru_get_next_message(struct gru_message_queue_desc *mqd)
  787. {
  788. struct message_queue *mq = mqd->mq;
  789. struct message_header *mhdr = mq->next;
  790. int present = mhdr->present;
  791. /* skip NOOP messages */
  792. while (present == MQS_NOOP) {
  793. gru_free_message(mqd, mhdr);
  794. mhdr = mq->next;
  795. present = mhdr->present;
  796. }
  797. /* Wait for both halves of 2 line messages */
  798. if (present == MQS_FULL && mhdr->lines == 2 &&
  799. get_present2(mhdr) == MQS_EMPTY)
  800. present = MQS_EMPTY;
  801. if (!present) {
  802. STAT(mesq_receive_none);
  803. return NULL;
  804. }
  805. if (mhdr->lines == 2)
  806. restore_present2(mhdr, mhdr->present2);
  807. STAT(mesq_receive);
  808. return mhdr;
  809. }
  810. EXPORT_SYMBOL_GPL(gru_get_next_message);
  811. /* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
  812. /*
  813. * Load a DW from a global GPA. The GPA can be a memory or MMR address.
  814. */
  815. int gru_read_gpa(unsigned long *value, unsigned long gpa)
  816. {
  817. void *cb;
  818. void *dsr;
  819. int ret, iaa;
  820. STAT(read_gpa);
  821. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  822. return MQE_BUG_NO_RESOURCES;
  823. iaa = gpa >> 62;
  824. gru_vload_phys(cb, gpa, gru_get_tri(dsr), iaa, IMA);
  825. ret = gru_wait(cb);
  826. if (ret == CBS_IDLE)
  827. *value = *(unsigned long *)dsr;
  828. gru_free_cpu_resources(cb, dsr);
  829. return ret;
  830. }
  831. EXPORT_SYMBOL_GPL(gru_read_gpa);
  832. /*
  833. * Copy a block of data using the GRU resources
  834. */
  835. int gru_copy_gpa(unsigned long dest_gpa, unsigned long src_gpa,
  836. unsigned int bytes)
  837. {
  838. void *cb;
  839. void *dsr;
  840. int ret;
  841. STAT(copy_gpa);
  842. if (gru_get_cpu_resources(GRU_NUM_KERNEL_DSR_BYTES, &cb, &dsr))
  843. return MQE_BUG_NO_RESOURCES;
  844. gru_bcopy(cb, src_gpa, dest_gpa, gru_get_tri(dsr),
  845. XTYPE_B, bytes, GRU_NUM_KERNEL_DSR_CL, IMA);
  846. ret = gru_wait(cb);
  847. gru_free_cpu_resources(cb, dsr);
  848. return ret;
  849. }
  850. EXPORT_SYMBOL_GPL(gru_copy_gpa);
  851. /* ------------------- KERNEL QUICKTESTS RUN AT STARTUP ----------------*/
  852. /* Temp - will delete after we gain confidence in the GRU */
  853. static int quicktest0(unsigned long arg)
  854. {
  855. unsigned long word0;
  856. unsigned long word1;
  857. void *cb;
  858. void *dsr;
  859. unsigned long *p;
  860. int ret = -EIO;
  861. if (gru_get_cpu_resources(GRU_CACHE_LINE_BYTES, &cb, &dsr))
  862. return MQE_BUG_NO_RESOURCES;
  863. p = dsr;
  864. word0 = MAGIC;
  865. word1 = 0;
  866. gru_vload(cb, uv_gpa(&word0), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  867. if (gru_wait(cb) != CBS_IDLE) {
  868. printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 1\n", smp_processor_id());
  869. goto done;
  870. }
  871. if (*p != MAGIC) {
  872. printk(KERN_DEBUG "GRU:%d quicktest0 bad magic 0x%lx\n", smp_processor_id(), *p);
  873. goto done;
  874. }
  875. gru_vstore(cb, uv_gpa(&word1), gru_get_tri(dsr), XTYPE_DW, 1, 1, IMA);
  876. if (gru_wait(cb) != CBS_IDLE) {
  877. printk(KERN_DEBUG "GRU:%d quicktest0: CBR failure 2\n", smp_processor_id());
  878. goto done;
  879. }
  880. if (word0 != word1 || word1 != MAGIC) {
  881. printk(KERN_DEBUG
  882. "GRU:%d quicktest0 err: found 0x%lx, expected 0x%lx\n",
  883. smp_processor_id(), word1, MAGIC);
  884. goto done;
  885. }
  886. ret = 0;
  887. done:
  888. gru_free_cpu_resources(cb, dsr);
  889. return ret;
  890. }
  891. #define ALIGNUP(p, q) ((void *)(((unsigned long)(p) + (q) - 1) & ~(q - 1)))
  892. static int quicktest1(unsigned long arg)
  893. {
  894. struct gru_message_queue_desc mqd;
  895. void *p, *mq;
  896. int i, ret = -EIO;
  897. char mes[GRU_CACHE_LINE_BYTES], *m;
  898. /* Need 1K cacheline aligned that does not cross page boundary */
  899. p = kmalloc(4096, 0);
  900. if (p == NULL)
  901. return -ENOMEM;
  902. mq = ALIGNUP(p, 1024);
  903. memset(mes, 0xee, sizeof(mes));
  904. gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0);
  905. for (i = 0; i < 6; i++) {
  906. mes[8] = i;
  907. do {
  908. ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
  909. } while (ret == MQE_CONGESTION);
  910. if (ret)
  911. break;
  912. }
  913. if (ret != MQE_QUEUE_FULL || i != 4) {
  914. printk(KERN_DEBUG "GRU:%d quicktest1: unexpect status %d, i %d\n",
  915. smp_processor_id(), ret, i);
  916. goto done;
  917. }
  918. for (i = 0; i < 6; i++) {
  919. m = gru_get_next_message(&mqd);
  920. if (!m || m[8] != i)
  921. break;
  922. gru_free_message(&mqd, m);
  923. }
  924. if (i != 4) {
  925. printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
  926. smp_processor_id(), i, m, m ? m[8] : -1);
  927. goto done;
  928. }
  929. ret = 0;
  930. done:
  931. kfree(p);
  932. return ret;
  933. }
  934. static int quicktest2(unsigned long arg)
  935. {
  936. static DECLARE_COMPLETION(cmp);
  937. unsigned long han;
  938. int blade_id = 0;
  939. int numcb = 4;
  940. int ret = 0;
  941. unsigned long *buf;
  942. void *cb0, *cb;
  943. struct gru_control_block_status *gen;
  944. int i, k, istatus, bytes;
  945. bytes = numcb * 4 * 8;
  946. buf = kmalloc(bytes, GFP_KERNEL);
  947. if (!buf)
  948. return -ENOMEM;
  949. ret = -EBUSY;
  950. han = gru_reserve_async_resources(blade_id, numcb, 0, &cmp);
  951. if (!han)
  952. goto done;
  953. gru_lock_async_resource(han, &cb0, NULL);
  954. memset(buf, 0xee, bytes);
  955. for (i = 0; i < numcb; i++)
  956. gru_vset(cb0 + i * GRU_HANDLE_STRIDE, uv_gpa(&buf[i * 4]), 0,
  957. XTYPE_DW, 4, 1, IMA_INTERRUPT);
  958. ret = 0;
  959. k = numcb;
  960. do {
  961. gru_wait_async_cbr(han);
  962. for (i = 0; i < numcb; i++) {
  963. cb = cb0 + i * GRU_HANDLE_STRIDE;
  964. istatus = gru_check_status(cb);
  965. if (istatus != CBS_ACTIVE && istatus != CBS_CALL_OS)
  966. break;
  967. }
  968. if (i == numcb)
  969. continue;
  970. if (istatus != CBS_IDLE) {
  971. printk(KERN_DEBUG "GRU:%d quicktest2: cb %d, exception\n", smp_processor_id(), i);
  972. ret = -EFAULT;
  973. } else if (buf[4 * i] || buf[4 * i + 1] || buf[4 * i + 2] ||
  974. buf[4 * i + 3]) {
  975. printk(KERN_DEBUG "GRU:%d quicktest2:cb %d, buf 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
  976. smp_processor_id(), i, buf[4 * i], buf[4 * i + 1], buf[4 * i + 2], buf[4 * i + 3]);
  977. ret = -EIO;
  978. }
  979. k--;
  980. gen = cb;
  981. gen->istatus = CBS_CALL_OS; /* don't handle this CBR again */
  982. } while (k);
  983. BUG_ON(cmp.done);
  984. gru_unlock_async_resource(han);
  985. gru_release_async_resources(han);
  986. done:
  987. kfree(buf);
  988. return ret;
  989. }
  990. #define BUFSIZE 200
  991. static int quicktest3(unsigned long arg)
  992. {
  993. char buf1[BUFSIZE], buf2[BUFSIZE];
  994. int ret = 0;
  995. memset(buf2, 0, sizeof(buf2));
  996. memset(buf1, get_cycles() & 255, sizeof(buf1));
  997. gru_copy_gpa(uv_gpa(buf2), uv_gpa(buf1), BUFSIZE);
  998. if (memcmp(buf1, buf2, BUFSIZE)) {
  999. printk(KERN_DEBUG "GRU:%d quicktest3 error\n", smp_processor_id());
  1000. ret = -EIO;
  1001. }
  1002. return ret;
  1003. }
  1004. /*
  1005. * Debugging only. User hook for various kernel tests
  1006. * of driver & gru.
  1007. */
  1008. int gru_ktest(unsigned long arg)
  1009. {
  1010. int ret = -EINVAL;
  1011. switch (arg & 0xff) {
  1012. case 0:
  1013. ret = quicktest0(arg);
  1014. break;
  1015. case 1:
  1016. ret = quicktest1(arg);
  1017. break;
  1018. case 2:
  1019. ret = quicktest2(arg);
  1020. break;
  1021. case 3:
  1022. ret = quicktest3(arg);
  1023. break;
  1024. case 99:
  1025. ret = gru_free_kernel_contexts();
  1026. break;
  1027. }
  1028. return ret;
  1029. }
  1030. int gru_kservices_init(void)
  1031. {
  1032. return 0;
  1033. }
  1034. void gru_kservices_exit(void)
  1035. {
  1036. if (gru_free_kernel_contexts())
  1037. BUG();
  1038. }