grukservices.c 26 KB

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