bte.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2000-2007 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. #include <linux/module.h>
  9. #include <asm/sn/nodepda.h>
  10. #include <asm/sn/addrs.h>
  11. #include <asm/sn/arch.h>
  12. #include <asm/sn/sn_cpuid.h>
  13. #include <asm/sn/pda.h>
  14. #include <asm/sn/shubio.h>
  15. #include <asm/nodedata.h>
  16. #include <asm/delay.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/string.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <asm/sn/bte.h>
  22. #ifndef L1_CACHE_MASK
  23. #define L1_CACHE_MASK (L1_CACHE_BYTES - 1)
  24. #endif
  25. /* two interfaces on two btes */
  26. #define MAX_INTERFACES_TO_TRY 4
  27. #define MAX_NODES_TO_TRY 2
  28. static struct bteinfo_s *bte_if_on_node(nasid_t nasid, int interface)
  29. {
  30. nodepda_t *tmp_nodepda;
  31. if (nasid_to_cnodeid(nasid) == -1)
  32. return (struct bteinfo_s *)NULL;
  33. tmp_nodepda = NODEPDA(nasid_to_cnodeid(nasid));
  34. return &tmp_nodepda->bte_if[interface];
  35. }
  36. static inline void bte_start_transfer(struct bteinfo_s *bte, u64 len, u64 mode)
  37. {
  38. if (is_shub2()) {
  39. BTE_CTRL_STORE(bte, (IBLS_BUSY | ((len) | (mode) << 24)));
  40. } else {
  41. BTE_LNSTAT_STORE(bte, len);
  42. BTE_CTRL_STORE(bte, mode);
  43. }
  44. }
  45. /************************************************************************
  46. * Block Transfer Engine copy related functions.
  47. *
  48. ***********************************************************************/
  49. /*
  50. * bte_copy(src, dest, len, mode, notification)
  51. *
  52. * Use the block transfer engine to move kernel memory from src to dest
  53. * using the assigned mode.
  54. *
  55. * Parameters:
  56. * src - physical address of the transfer source.
  57. * dest - physical address of the transfer destination.
  58. * len - number of bytes to transfer from source to dest.
  59. * mode - hardware defined. See reference information
  60. * for IBCT0/1 in the SHUB Programmers Reference
  61. * notification - kernel virtual address of the notification cache
  62. * line. If NULL, the default is used and
  63. * the bte_copy is synchronous.
  64. *
  65. * NOTE: This function requires src, dest, and len to
  66. * be cacheline aligned.
  67. */
  68. bte_result_t bte_copy(u64 src, u64 dest, u64 len, u64 mode, void *notification)
  69. {
  70. u64 transfer_size;
  71. u64 transfer_stat;
  72. u64 notif_phys_addr;
  73. struct bteinfo_s *bte;
  74. bte_result_t bte_status;
  75. unsigned long irq_flags;
  76. unsigned long itc_end = 0;
  77. int nasid_to_try[MAX_NODES_TO_TRY];
  78. int my_nasid = cpuid_to_nasid(raw_smp_processor_id());
  79. int bte_if_index, nasid_index;
  80. int bte_first, btes_per_node = BTES_PER_NODE;
  81. BTE_PRINTK(("bte_copy(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%p)\n",
  82. src, dest, len, mode, notification));
  83. if (len == 0) {
  84. return BTE_SUCCESS;
  85. }
  86. BUG_ON(len & L1_CACHE_MASK);
  87. BUG_ON(src & L1_CACHE_MASK);
  88. BUG_ON(dest & L1_CACHE_MASK);
  89. BUG_ON(len > BTE_MAX_XFER);
  90. /*
  91. * Start with interface corresponding to cpu number
  92. */
  93. bte_first = raw_smp_processor_id() % btes_per_node;
  94. if (mode & BTE_USE_DEST) {
  95. /* try remote then local */
  96. nasid_to_try[0] = NASID_GET(dest);
  97. if (mode & BTE_USE_ANY) {
  98. nasid_to_try[1] = my_nasid;
  99. } else {
  100. nasid_to_try[1] = (int)NULL;
  101. }
  102. } else {
  103. /* try local then remote */
  104. nasid_to_try[0] = my_nasid;
  105. if (mode & BTE_USE_ANY) {
  106. nasid_to_try[1] = NASID_GET(dest);
  107. } else {
  108. nasid_to_try[1] = (int)NULL;
  109. }
  110. }
  111. retry_bteop:
  112. do {
  113. local_irq_save(irq_flags);
  114. bte_if_index = bte_first;
  115. nasid_index = 0;
  116. /* Attempt to lock one of the BTE interfaces. */
  117. while (nasid_index < MAX_NODES_TO_TRY) {
  118. bte = bte_if_on_node(nasid_to_try[nasid_index],bte_if_index);
  119. if (bte == NULL) {
  120. nasid_index++;
  121. continue;
  122. }
  123. if (spin_trylock(&bte->spinlock)) {
  124. if (!(*bte->most_rcnt_na & BTE_WORD_AVAILABLE) ||
  125. (BTE_LNSTAT_LOAD(bte) & BTE_ACTIVE)) {
  126. /* Got the lock but BTE still busy */
  127. spin_unlock(&bte->spinlock);
  128. } else {
  129. /* we got the lock and it's not busy */
  130. break;
  131. }
  132. }
  133. bte_if_index = (bte_if_index + 1) % btes_per_node; /* Next interface */
  134. if (bte_if_index == bte_first) {
  135. /*
  136. * We've tried all interfaces on this node
  137. */
  138. nasid_index++;
  139. }
  140. bte = NULL;
  141. }
  142. if (bte != NULL) {
  143. break;
  144. }
  145. local_irq_restore(irq_flags);
  146. if (!(mode & BTE_WACQUIRE)) {
  147. return BTEFAIL_NOTAVAIL;
  148. }
  149. } while (1);
  150. if (notification == NULL) {
  151. /* User does not want to be notified. */
  152. bte->most_rcnt_na = &bte->notify;
  153. } else {
  154. bte->most_rcnt_na = notification;
  155. }
  156. /* Calculate the number of cache lines to transfer. */
  157. transfer_size = ((len >> L1_CACHE_SHIFT) & BTE_LEN_MASK);
  158. /* Initialize the notification to a known value. */
  159. *bte->most_rcnt_na = BTE_WORD_BUSY;
  160. notif_phys_addr = (u64)bte->most_rcnt_na;
  161. /* Set the source and destination registers */
  162. BTE_PRINTKV(("IBSA = 0x%lx)\n", src));
  163. BTE_SRC_STORE(bte, src);
  164. BTE_PRINTKV(("IBDA = 0x%lx)\n", dest));
  165. BTE_DEST_STORE(bte, dest);
  166. /* Set the notification register */
  167. BTE_PRINTKV(("IBNA = 0x%lx)\n", notif_phys_addr));
  168. BTE_NOTIF_STORE(bte, notif_phys_addr);
  169. /* Initiate the transfer */
  170. BTE_PRINTK(("IBCT = 0x%lx)\n", BTE_VALID_MODE(mode)));
  171. bte_start_transfer(bte, transfer_size, BTE_VALID_MODE(mode));
  172. itc_end = ia64_get_itc() + (40000000 * local_cpu_data->cyc_per_usec);
  173. spin_unlock_irqrestore(&bte->spinlock, irq_flags);
  174. if (notification != NULL) {
  175. return BTE_SUCCESS;
  176. }
  177. while ((transfer_stat = *bte->most_rcnt_na) == BTE_WORD_BUSY) {
  178. cpu_relax();
  179. if (ia64_get_itc() > itc_end) {
  180. BTE_PRINTK(("BTE timeout nasid 0x%x bte%d IBLS = 0x%lx na 0x%lx\n",
  181. NASID_GET(bte->bte_base_addr), bte->bte_num,
  182. BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na) );
  183. bte->bte_error_count++;
  184. bte->bh_error = IBLS_ERROR;
  185. bte_error_handler((unsigned long)NODEPDA(bte->bte_cnode));
  186. *bte->most_rcnt_na = BTE_WORD_AVAILABLE;
  187. goto retry_bteop;
  188. }
  189. }
  190. BTE_PRINTKV((" Delay Done. IBLS = 0x%lx, most_rcnt_na = 0x%lx\n",
  191. BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na));
  192. if (transfer_stat & IBLS_ERROR) {
  193. bte_status = BTE_GET_ERROR_STATUS(transfer_stat);
  194. } else {
  195. bte_status = BTE_SUCCESS;
  196. }
  197. *bte->most_rcnt_na = BTE_WORD_AVAILABLE;
  198. BTE_PRINTK(("Returning status is 0x%lx and most_rcnt_na is 0x%lx\n",
  199. BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na));
  200. return bte_status;
  201. }
  202. EXPORT_SYMBOL(bte_copy);
  203. /*
  204. * bte_unaligned_copy(src, dest, len, mode)
  205. *
  206. * use the block transfer engine to move kernel
  207. * memory from src to dest using the assigned mode.
  208. *
  209. * Parameters:
  210. * src - physical address of the transfer source.
  211. * dest - physical address of the transfer destination.
  212. * len - number of bytes to transfer from source to dest.
  213. * mode - hardware defined. See reference information
  214. * for IBCT0/1 in the SGI documentation.
  215. *
  216. * NOTE: If the source, dest, and len are all cache line aligned,
  217. * then it would be _FAR_ preferable to use bte_copy instead.
  218. */
  219. bte_result_t bte_unaligned_copy(u64 src, u64 dest, u64 len, u64 mode)
  220. {
  221. int destFirstCacheOffset;
  222. u64 headBteSource;
  223. u64 headBteLen;
  224. u64 headBcopySrcOffset;
  225. u64 headBcopyDest;
  226. u64 headBcopyLen;
  227. u64 footBteSource;
  228. u64 footBteLen;
  229. u64 footBcopyDest;
  230. u64 footBcopyLen;
  231. bte_result_t rv;
  232. char *bteBlock, *bteBlock_unaligned;
  233. if (len == 0) {
  234. return BTE_SUCCESS;
  235. }
  236. /* temporary buffer used during unaligned transfers */
  237. bteBlock_unaligned = kmalloc(len + 3 * L1_CACHE_BYTES, GFP_KERNEL);
  238. if (bteBlock_unaligned == NULL) {
  239. return BTEFAIL_NOTAVAIL;
  240. }
  241. bteBlock = (char *)L1_CACHE_ALIGN((u64) bteBlock_unaligned);
  242. headBcopySrcOffset = src & L1_CACHE_MASK;
  243. destFirstCacheOffset = dest & L1_CACHE_MASK;
  244. /*
  245. * At this point, the transfer is broken into
  246. * (up to) three sections. The first section is
  247. * from the start address to the first physical
  248. * cache line, the second is from the first physical
  249. * cache line to the last complete cache line,
  250. * and the third is from the last cache line to the
  251. * end of the buffer. The first and third sections
  252. * are handled by bte copying into a temporary buffer
  253. * and then bcopy'ing the necessary section into the
  254. * final location. The middle section is handled with
  255. * a standard bte copy.
  256. *
  257. * One nasty exception to the above rule is when the
  258. * source and destination are not symmetrically
  259. * mis-aligned. If the source offset from the first
  260. * cache line is different from the destination offset,
  261. * we make the first section be the entire transfer
  262. * and the bcopy the entire block into place.
  263. */
  264. if (headBcopySrcOffset == destFirstCacheOffset) {
  265. /*
  266. * Both the source and destination are the same
  267. * distance from a cache line boundary so we can
  268. * use the bte to transfer the bulk of the
  269. * data.
  270. */
  271. headBteSource = src & ~L1_CACHE_MASK;
  272. headBcopyDest = dest;
  273. if (headBcopySrcOffset) {
  274. headBcopyLen =
  275. (len >
  276. (L1_CACHE_BYTES -
  277. headBcopySrcOffset) ? L1_CACHE_BYTES
  278. - headBcopySrcOffset : len);
  279. headBteLen = L1_CACHE_BYTES;
  280. } else {
  281. headBcopyLen = 0;
  282. headBteLen = 0;
  283. }
  284. if (len > headBcopyLen) {
  285. footBcopyLen = (len - headBcopyLen) & L1_CACHE_MASK;
  286. footBteLen = L1_CACHE_BYTES;
  287. footBteSource = src + len - footBcopyLen;
  288. footBcopyDest = dest + len - footBcopyLen;
  289. if (footBcopyDest == (headBcopyDest + headBcopyLen)) {
  290. /*
  291. * We have two contiguous bcopy
  292. * blocks. Merge them.
  293. */
  294. headBcopyLen += footBcopyLen;
  295. headBteLen += footBteLen;
  296. } else if (footBcopyLen > 0) {
  297. rv = bte_copy(footBteSource,
  298. ia64_tpa((unsigned long)bteBlock),
  299. footBteLen, mode, NULL);
  300. if (rv != BTE_SUCCESS) {
  301. kfree(bteBlock_unaligned);
  302. return rv;
  303. }
  304. memcpy(__va(footBcopyDest),
  305. (char *)bteBlock, footBcopyLen);
  306. }
  307. } else {
  308. footBcopyLen = 0;
  309. footBteLen = 0;
  310. }
  311. if (len > (headBcopyLen + footBcopyLen)) {
  312. /* now transfer the middle. */
  313. rv = bte_copy((src + headBcopyLen),
  314. (dest +
  315. headBcopyLen),
  316. (len - headBcopyLen -
  317. footBcopyLen), mode, NULL);
  318. if (rv != BTE_SUCCESS) {
  319. kfree(bteBlock_unaligned);
  320. return rv;
  321. }
  322. }
  323. } else {
  324. /*
  325. * The transfer is not symmetric, we will
  326. * allocate a buffer large enough for all the
  327. * data, bte_copy into that buffer and then
  328. * bcopy to the destination.
  329. */
  330. headBcopySrcOffset = src & L1_CACHE_MASK;
  331. headBcopyDest = dest;
  332. headBcopyLen = len;
  333. headBteSource = src - headBcopySrcOffset;
  334. /* Add the leading and trailing bytes from source */
  335. headBteLen = L1_CACHE_ALIGN(len + headBcopySrcOffset);
  336. }
  337. if (headBcopyLen > 0) {
  338. rv = bte_copy(headBteSource,
  339. ia64_tpa((unsigned long)bteBlock), headBteLen,
  340. mode, NULL);
  341. if (rv != BTE_SUCCESS) {
  342. kfree(bteBlock_unaligned);
  343. return rv;
  344. }
  345. memcpy(__va(headBcopyDest), ((char *)bteBlock +
  346. headBcopySrcOffset), headBcopyLen);
  347. }
  348. kfree(bteBlock_unaligned);
  349. return BTE_SUCCESS;
  350. }
  351. EXPORT_SYMBOL(bte_unaligned_copy);
  352. /************************************************************************
  353. * Block Transfer Engine initialization functions.
  354. *
  355. ***********************************************************************/
  356. /*
  357. * bte_init_node(nodepda, cnode)
  358. *
  359. * Initialize the nodepda structure with BTE base addresses and
  360. * spinlocks.
  361. */
  362. void bte_init_node(nodepda_t * mynodepda, cnodeid_t cnode)
  363. {
  364. int i;
  365. /*
  366. * Indicate that all the block transfer engines on this node
  367. * are available.
  368. */
  369. /*
  370. * Allocate one bte_recover_t structure per node. It holds
  371. * the recovery lock for node. All the bte interface structures
  372. * will point at this one bte_recover structure to get the lock.
  373. */
  374. spin_lock_init(&mynodepda->bte_recovery_lock);
  375. init_timer(&mynodepda->bte_recovery_timer);
  376. mynodepda->bte_recovery_timer.function = bte_error_handler;
  377. mynodepda->bte_recovery_timer.data = (unsigned long)mynodepda;
  378. for (i = 0; i < BTES_PER_NODE; i++) {
  379. u64 *base_addr;
  380. /* Which link status register should we use? */
  381. base_addr = (u64 *)
  382. REMOTE_HUB_ADDR(cnodeid_to_nasid(cnode), BTE_BASE_ADDR(i));
  383. mynodepda->bte_if[i].bte_base_addr = base_addr;
  384. mynodepda->bte_if[i].bte_source_addr = BTE_SOURCE_ADDR(base_addr);
  385. mynodepda->bte_if[i].bte_destination_addr = BTE_DEST_ADDR(base_addr);
  386. mynodepda->bte_if[i].bte_control_addr = BTE_CTRL_ADDR(base_addr);
  387. mynodepda->bte_if[i].bte_notify_addr = BTE_NOTIF_ADDR(base_addr);
  388. /*
  389. * Initialize the notification and spinlock
  390. * so the first transfer can occur.
  391. */
  392. mynodepda->bte_if[i].most_rcnt_na =
  393. &(mynodepda->bte_if[i].notify);
  394. mynodepda->bte_if[i].notify = BTE_WORD_AVAILABLE;
  395. spin_lock_init(&mynodepda->bte_if[i].spinlock);
  396. mynodepda->bte_if[i].bte_cnode = cnode;
  397. mynodepda->bte_if[i].bte_error_count = 0;
  398. mynodepda->bte_if[i].bte_num = i;
  399. mynodepda->bte_if[i].cleanup_active = 0;
  400. mynodepda->bte_if[i].bh_error = 0;
  401. }
  402. }