grukservices.c 28 KB

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