bte.c 13 KB

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