grukservices.c 26 KB

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