blob: dd73c0cb754b4051214f113bf0bad237b684148a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Russ Anderson95ff4392005-04-25 13:19:11 -07006 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9#include <linux/config.h>
10#include <linux/module.h>
11#include <asm/sn/nodepda.h>
12#include <asm/sn/addrs.h>
13#include <asm/sn/arch.h>
14#include <asm/sn/sn_cpuid.h>
15#include <asm/sn/pda.h>
16#include <asm/sn/shubio.h>
17#include <asm/nodedata.h>
18#include <asm/delay.h>
19
20#include <linux/bootmem.h>
21#include <linux/string.h>
22#include <linux/sched.h>
23
24#include <asm/sn/bte.h>
25
26#ifndef L1_CACHE_MASK
27#define L1_CACHE_MASK (L1_CACHE_BYTES - 1)
28#endif
29
30/* two interfaces on two btes */
31#define MAX_INTERFACES_TO_TRY 4
Jack Steiner7e95b9d2005-08-11 10:27:00 -070032#define MAX_NODES_TO_TRY 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static struct bteinfo_s *bte_if_on_node(nasid_t nasid, int interface)
35{
36 nodepda_t *tmp_nodepda;
37
Jack Steiner7e95b9d2005-08-11 10:27:00 -070038 if (nasid_to_cnodeid(nasid) == -1)
39 return (struct bteinfo_s *)NULL;;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 tmp_nodepda = NODEPDA(nasid_to_cnodeid(nasid));
42 return &tmp_nodepda->bte_if[interface];
43
44}
45
Jack Steiner7e95b9d2005-08-11 10:27:00 -070046static inline void bte_start_transfer(struct bteinfo_s *bte, u64 len, u64 mode)
47{
48 if (is_shub2()) {
49 BTE_CTRL_STORE(bte, (IBLS_BUSY | ((len) | (mode) << 24)));
50 } else {
51 BTE_LNSTAT_STORE(bte, len);
52 BTE_CTRL_STORE(bte, mode);
53 }
54}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/************************************************************************
57 * Block Transfer Engine copy related functions.
58 *
59 ***********************************************************************/
60
61/*
62 * bte_copy(src, dest, len, mode, notification)
63 *
64 * Use the block transfer engine to move kernel memory from src to dest
65 * using the assigned mode.
66 *
67 * Paramaters:
68 * src - physical address of the transfer source.
69 * dest - physical address of the transfer destination.
70 * len - number of bytes to transfer from source to dest.
71 * mode - hardware defined. See reference information
72 * for IBCT0/1 in the SHUB Programmers Reference
73 * notification - kernel virtual address of the notification cache
74 * line. If NULL, the default is used and
75 * the bte_copy is synchronous.
76 *
77 * NOTE: This function requires src, dest, and len to
78 * be cacheline aligned.
79 */
80bte_result_t bte_copy(u64 src, u64 dest, u64 len, u64 mode, void *notification)
81{
82 u64 transfer_size;
83 u64 transfer_stat;
Jack Steiner7e95b9d2005-08-11 10:27:00 -070084 u64 notif_phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct bteinfo_s *bte;
86 bte_result_t bte_status;
87 unsigned long irq_flags;
88 unsigned long itc_end = 0;
Jack Steiner7e95b9d2005-08-11 10:27:00 -070089 int nasid_to_try[MAX_NODES_TO_TRY];
Russ Andersone7f98db2005-10-25 17:34:19 -050090 int my_nasid = cpuid_to_nasid(raw_smp_processor_id());
Jack Steiner7e95b9d2005-08-11 10:27:00 -070091 int bte_if_index, nasid_index;
92 int bte_first, btes_per_node = BTES_PER_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 BTE_PRINTK(("bte_copy(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%p)\n",
95 src, dest, len, mode, notification));
96
97 if (len == 0) {
98 return BTE_SUCCESS;
99 }
100
101 BUG_ON((len & L1_CACHE_MASK) ||
102 (src & L1_CACHE_MASK) || (dest & L1_CACHE_MASK));
103 BUG_ON(!(len < ((BTE_LEN_MASK + 1) << L1_CACHE_SHIFT)));
104
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700105 /*
106 * Start with interface corresponding to cpu number
107 */
Russ Andersond1e079b2005-08-15 14:46:00 -0700108 bte_first = raw_smp_processor_id() % btes_per_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (mode & BTE_USE_DEST) {
111 /* try remote then local */
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700112 nasid_to_try[0] = NASID_GET(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (mode & BTE_USE_ANY) {
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700114 nasid_to_try[1] = my_nasid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 } else {
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700116 nasid_to_try[1] = (int)NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 } else {
119 /* try local then remote */
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700120 nasid_to_try[0] = my_nasid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (mode & BTE_USE_ANY) {
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700122 nasid_to_try[1] = NASID_GET(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else {
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700124 nasid_to_try[1] = (int)NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
127
128retry_bteop:
129 do {
130 local_irq_save(irq_flags);
131
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700132 bte_if_index = bte_first;
133 nasid_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* Attempt to lock one of the BTE interfaces. */
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700136 while (nasid_index < MAX_NODES_TO_TRY) {
137 bte = bte_if_on_node(nasid_to_try[nasid_index],bte_if_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (bte == NULL) {
Russ Andersonab2ff462005-11-11 16:52:02 -0600140 nasid_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 continue;
142 }
143
144 if (spin_trylock(&bte->spinlock)) {
145 if (!(*bte->most_rcnt_na & BTE_WORD_AVAILABLE) ||
146 (BTE_LNSTAT_LOAD(bte) & BTE_ACTIVE)) {
147 /* Got the lock but BTE still busy */
148 spin_unlock(&bte->spinlock);
149 } else {
150 /* we got the lock and it's not busy */
151 break;
152 }
153 }
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700154
155 bte_if_index = (bte_if_index + 1) % btes_per_node; /* Next interface */
156 if (bte_if_index == bte_first) {
157 /*
158 * We've tried all interfaces on this node
159 */
160 nasid_index++;
161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 bte = NULL;
164 }
165
166 if (bte != NULL) {
167 break;
168 }
169
170 local_irq_restore(irq_flags);
171
172 if (!(mode & BTE_WACQUIRE)) {
173 return BTEFAIL_NOTAVAIL;
174 }
175 } while (1);
176
177 if (notification == NULL) {
178 /* User does not want to be notified. */
179 bte->most_rcnt_na = &bte->notify;
180 } else {
181 bte->most_rcnt_na = notification;
182 }
183
184 /* Calculate the number of cache lines to transfer. */
185 transfer_size = ((len >> L1_CACHE_SHIFT) & BTE_LEN_MASK);
186
187 /* Initialize the notification to a known value. */
188 *bte->most_rcnt_na = BTE_WORD_BUSY;
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700189 notif_phys_addr = TO_PHYS(ia64_tpa((unsigned long)bte->most_rcnt_na));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700191 if (is_shub2()) {
192 src = SH2_TIO_PHYS_TO_DMA(src);
193 dest = SH2_TIO_PHYS_TO_DMA(dest);
194 notif_phys_addr = SH2_TIO_PHYS_TO_DMA(notif_phys_addr);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Set the source and destination registers */
197 BTE_PRINTKV(("IBSA = 0x%lx)\n", (TO_PHYS(src))));
198 BTE_SRC_STORE(bte, TO_PHYS(src));
199 BTE_PRINTKV(("IBDA = 0x%lx)\n", (TO_PHYS(dest))));
200 BTE_DEST_STORE(bte, TO_PHYS(dest));
201
202 /* Set the notification register */
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700203 BTE_PRINTKV(("IBNA = 0x%lx)\n", notif_phys_addr));
204 BTE_NOTIF_STORE(bte, notif_phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Initiate the transfer */
207 BTE_PRINTK(("IBCT = 0x%lx)\n", BTE_VALID_MODE(mode)));
Jack Steiner7e95b9d2005-08-11 10:27:00 -0700208 bte_start_transfer(bte, transfer_size, BTE_VALID_MODE(mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 itc_end = ia64_get_itc() + (40000000 * local_cpu_data->cyc_per_usec);
211
212 spin_unlock_irqrestore(&bte->spinlock, irq_flags);
213
214 if (notification != NULL) {
215 return BTE_SUCCESS;
216 }
217
218 while ((transfer_stat = *bte->most_rcnt_na) == BTE_WORD_BUSY) {
Jack Steiner68b97532005-08-11 10:28:00 -0700219 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (ia64_get_itc() > itc_end) {
221 BTE_PRINTK(("BTE timeout nasid 0x%x bte%d IBLS = 0x%lx na 0x%lx\n",
222 NASID_GET(bte->bte_base_addr), bte->bte_num,
223 BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na) );
224 bte->bte_error_count++;
225 bte->bh_error = IBLS_ERROR;
226 bte_error_handler((unsigned long)NODEPDA(bte->bte_cnode));
227 *bte->most_rcnt_na = BTE_WORD_AVAILABLE;
228 goto retry_bteop;
229 }
230 }
231
232 BTE_PRINTKV((" Delay Done. IBLS = 0x%lx, most_rcnt_na = 0x%lx\n",
233 BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na));
234
235 if (transfer_stat & IBLS_ERROR) {
236 bte_status = transfer_stat & ~IBLS_ERROR;
237 } else {
238 bte_status = BTE_SUCCESS;
239 }
240 *bte->most_rcnt_na = BTE_WORD_AVAILABLE;
241
242 BTE_PRINTK(("Returning status is 0x%lx and most_rcnt_na is 0x%lx\n",
243 BTE_LNSTAT_LOAD(bte), *bte->most_rcnt_na));
244
245 return bte_status;
246}
247
248EXPORT_SYMBOL(bte_copy);
249
250/*
251 * bte_unaligned_copy(src, dest, len, mode)
252 *
253 * use the block transfer engine to move kernel
254 * memory from src to dest using the assigned mode.
255 *
256 * Paramaters:
257 * src - physical address of the transfer source.
258 * dest - physical address of the transfer destination.
259 * len - number of bytes to transfer from source to dest.
260 * mode - hardware defined. See reference information
261 * for IBCT0/1 in the SGI documentation.
262 *
263 * NOTE: If the source, dest, and len are all cache line aligned,
264 * then it would be _FAR_ preferrable to use bte_copy instead.
265 */
266bte_result_t bte_unaligned_copy(u64 src, u64 dest, u64 len, u64 mode)
267{
268 int destFirstCacheOffset;
269 u64 headBteSource;
270 u64 headBteLen;
271 u64 headBcopySrcOffset;
272 u64 headBcopyDest;
273 u64 headBcopyLen;
274 u64 footBteSource;
275 u64 footBteLen;
276 u64 footBcopyDest;
277 u64 footBcopyLen;
278 bte_result_t rv;
279 char *bteBlock, *bteBlock_unaligned;
280
281 if (len == 0) {
282 return BTE_SUCCESS;
283 }
284
285 /* temporary buffer used during unaligned transfers */
286 bteBlock_unaligned = kmalloc(len + 3 * L1_CACHE_BYTES,
287 GFP_KERNEL | GFP_DMA);
288 if (bteBlock_unaligned == NULL) {
289 return BTEFAIL_NOTAVAIL;
290 }
291 bteBlock = (char *)L1_CACHE_ALIGN((u64) bteBlock_unaligned);
292
293 headBcopySrcOffset = src & L1_CACHE_MASK;
294 destFirstCacheOffset = dest & L1_CACHE_MASK;
295
296 /*
297 * At this point, the transfer is broken into
298 * (up to) three sections. The first section is
299 * from the start address to the first physical
300 * cache line, the second is from the first physical
301 * cache line to the last complete cache line,
302 * and the third is from the last cache line to the
303 * end of the buffer. The first and third sections
304 * are handled by bte copying into a temporary buffer
305 * and then bcopy'ing the necessary section into the
306 * final location. The middle section is handled with
307 * a standard bte copy.
308 *
309 * One nasty exception to the above rule is when the
310 * source and destination are not symetrically
311 * mis-aligned. If the source offset from the first
312 * cache line is different from the destination offset,
313 * we make the first section be the entire transfer
314 * and the bcopy the entire block into place.
315 */
316 if (headBcopySrcOffset == destFirstCacheOffset) {
317
318 /*
319 * Both the source and destination are the same
320 * distance from a cache line boundary so we can
321 * use the bte to transfer the bulk of the
322 * data.
323 */
324 headBteSource = src & ~L1_CACHE_MASK;
325 headBcopyDest = dest;
326 if (headBcopySrcOffset) {
327 headBcopyLen =
328 (len >
329 (L1_CACHE_BYTES -
330 headBcopySrcOffset) ? L1_CACHE_BYTES
331 - headBcopySrcOffset : len);
332 headBteLen = L1_CACHE_BYTES;
333 } else {
334 headBcopyLen = 0;
335 headBteLen = 0;
336 }
337
338 if (len > headBcopyLen) {
339 footBcopyLen = (len - headBcopyLen) & L1_CACHE_MASK;
340 footBteLen = L1_CACHE_BYTES;
341
342 footBteSource = src + len - footBcopyLen;
343 footBcopyDest = dest + len - footBcopyLen;
344
345 if (footBcopyDest == (headBcopyDest + headBcopyLen)) {
346 /*
347 * We have two contigous bcopy
348 * blocks. Merge them.
349 */
350 headBcopyLen += footBcopyLen;
351 headBteLen += footBteLen;
352 } else if (footBcopyLen > 0) {
353 rv = bte_copy(footBteSource,
354 ia64_tpa((unsigned long)bteBlock),
355 footBteLen, mode, NULL);
356 if (rv != BTE_SUCCESS) {
357 kfree(bteBlock_unaligned);
358 return rv;
359 }
360
361 memcpy(__va(footBcopyDest),
362 (char *)bteBlock, footBcopyLen);
363 }
364 } else {
365 footBcopyLen = 0;
366 footBteLen = 0;
367 }
368
369 if (len > (headBcopyLen + footBcopyLen)) {
370 /* now transfer the middle. */
371 rv = bte_copy((src + headBcopyLen),
372 (dest +
373 headBcopyLen),
374 (len - headBcopyLen -
375 footBcopyLen), mode, NULL);
376 if (rv != BTE_SUCCESS) {
377 kfree(bteBlock_unaligned);
378 return rv;
379 }
380
381 }
382 } else {
383
384 /*
385 * The transfer is not symetric, we will
386 * allocate a buffer large enough for all the
387 * data, bte_copy into that buffer and then
388 * bcopy to the destination.
389 */
390
391 /* Add the leader from source */
392 headBteLen = len + (src & L1_CACHE_MASK);
393 /* Add the trailing bytes from footer. */
394 headBteLen += L1_CACHE_BYTES - (headBteLen & L1_CACHE_MASK);
395 headBteSource = src & ~L1_CACHE_MASK;
396 headBcopySrcOffset = src & L1_CACHE_MASK;
397 headBcopyDest = dest;
398 headBcopyLen = len;
399 }
400
401 if (headBcopyLen > 0) {
402 rv = bte_copy(headBteSource,
403 ia64_tpa((unsigned long)bteBlock), headBteLen,
404 mode, NULL);
405 if (rv != BTE_SUCCESS) {
406 kfree(bteBlock_unaligned);
407 return rv;
408 }
409
410 memcpy(__va(headBcopyDest), ((char *)bteBlock +
411 headBcopySrcOffset), headBcopyLen);
412 }
413 kfree(bteBlock_unaligned);
414 return BTE_SUCCESS;
415}
416
417EXPORT_SYMBOL(bte_unaligned_copy);
418
419/************************************************************************
420 * Block Transfer Engine initialization functions.
421 *
422 ***********************************************************************/
423
424/*
425 * bte_init_node(nodepda, cnode)
426 *
427 * Initialize the nodepda structure with BTE base addresses and
428 * spinlocks.
429 */
430void bte_init_node(nodepda_t * mynodepda, cnodeid_t cnode)
431{
432 int i;
433
434 /*
435 * Indicate that all the block transfer engines on this node
436 * are available.
437 */
438
439 /*
440 * Allocate one bte_recover_t structure per node. It holds
441 * the recovery lock for node. All the bte interface structures
442 * will point at this one bte_recover structure to get the lock.
443 */
444 spin_lock_init(&mynodepda->bte_recovery_lock);
445 init_timer(&mynodepda->bte_recovery_timer);
446 mynodepda->bte_recovery_timer.function = bte_error_handler;
447 mynodepda->bte_recovery_timer.data = (unsigned long)mynodepda;
448
449 for (i = 0; i < BTES_PER_NODE; i++) {
Russ Anderson95ff4392005-04-25 13:19:11 -0700450 u64 *base_addr;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Which link status register should we use? */
Russ Anderson95ff4392005-04-25 13:19:11 -0700453 base_addr = (u64 *)
454 REMOTE_HUB_ADDR(cnodeid_to_nasid(cnode), BTE_BASE_ADDR(i));
455 mynodepda->bte_if[i].bte_base_addr = base_addr;
456 mynodepda->bte_if[i].bte_source_addr = BTE_SOURCE_ADDR(base_addr);
457 mynodepda->bte_if[i].bte_destination_addr = BTE_DEST_ADDR(base_addr);
458 mynodepda->bte_if[i].bte_control_addr = BTE_CTRL_ADDR(base_addr);
459 mynodepda->bte_if[i].bte_notify_addr = BTE_NOTIF_ADDR(base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * Initialize the notification and spinlock
463 * so the first transfer can occur.
464 */
465 mynodepda->bte_if[i].most_rcnt_na =
466 &(mynodepda->bte_if[i].notify);
467 mynodepda->bte_if[i].notify = BTE_WORD_AVAILABLE;
468 spin_lock_init(&mynodepda->bte_if[i].spinlock);
469
470 mynodepda->bte_if[i].bte_cnode = cnode;
471 mynodepda->bte_if[i].bte_error_count = 0;
472 mynodepda->bte_if[i].bte_num = i;
473 mynodepda->bte_if[i].cleanup_active = 0;
474 mynodepda->bte_if[i].bh_error = 0;
475 }
476
477}