grukservices.c 29 KB

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