34 #include <linux/types.h>
35 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/pci.h>
42 #include <linux/reboot.h>
47 #include <linux/export.h>
49 #include <asm/byteorder.h>
50 #include <asm/cache.h>
51 #include <asm/uaccess.h>
62 #define MODULE_NAME "ccio"
66 #undef DEBUG_CCIO_INIT
67 #undef DEBUG_CCIO_RUN_SG
71 #undef CCIO_COLLECT_STATS
76 #ifdef DEBUG_CCIO_INIT
77 #define DBG_INIT(x...) printk(x)
79 #define DBG_INIT(x...)
83 #define DBG_RUN(x...) printk(x)
89 #define DBG_RES(x...) printk(x)
94 #ifdef DEBUG_CCIO_RUN_SG
95 #define DBG_RUN_SG(x...) printk(x)
97 #define DBG_RUN_SG(x...)
100 #define CCIO_INLINE inline
101 #define WRITE_U32(value, addr) __raw_writel(value, addr)
102 #define READ_U32(addr) __raw_readl(addr)
104 #define U2_IOA_RUNWAY 0x580
105 #define U2_BC_GSC 0x501
106 #define UTURN_IOA_RUNWAY 0x581
107 #define UTURN_BC_GSC 0x502
109 #define IOA_NORMAL_MODE 0x00020080
110 #define CMD_TLB_DIRECT_WRITE 35
111 #define CMD_TLB_PURGE 33
235 #ifdef CCIO_COLLECT_STATS
236 #define CCIO_SEARCH_SAMPLE 0x100
237 unsigned long avg_search[CCIO_SEARCH_SAMPLE];
238 unsigned long avg_idx;
239 unsigned long used_pages;
240 unsigned long msingle_calls;
241 unsigned long msingle_pages;
242 unsigned long msg_calls;
243 unsigned long msg_pages;
244 unsigned long usingle_calls;
245 unsigned long usingle_pages;
246 unsigned long usg_calls;
247 unsigned long usg_pages;
260 static struct ioc *ioc_list;
261 static int ioc_count;
278 #define IOVP_SIZE PAGE_SIZE
279 #define IOVP_SHIFT PAGE_SHIFT
280 #define IOVP_MASK PAGE_MASK
283 #define CCIO_IOVA(iovp,offset) ((iovp) | (offset))
284 #define CCIO_IOVP(iova) ((iova) & IOVP_MASK)
286 #define PDIR_INDEX(iovp) ((iovp)>>IOVP_SHIFT)
287 #define MKIOVP(pdir_idx) ((long)(pdir_idx) << IOVP_SHIFT)
288 #define MKIOVA(iovp,offset) (dma_addr_t)((long)iovp | (long)offset)
295 #define CCIO_SEARCH_LOOP(ioc, res_idx, mask, size) \
296 for(; res_ptr < res_end; ++res_ptr) { \
299 idx = (unsigned int)((unsigned long)res_ptr - (unsigned long)ioc->res_map); \
300 ret = iommu_is_span_boundary(idx << 3, pages_needed, 0, boundary_size);\
301 if ((0 == (*res_ptr & mask)) && !ret) { \
304 ioc->res_hint = res_idx + (size >> 3); \
305 goto resource_found; \
309 #define CCIO_FIND_FREE_MAPPING(ioa, res_idx, mask, size) \
310 u##size *res_ptr = (u##size *)&((ioc)->res_map[ioa->res_hint & ~((size >> 3) - 1)]); \
311 u##size *res_end = (u##size *)&(ioc)->res_map[ioa->res_size]; \
312 CCIO_SEARCH_LOOP(ioc, res_idx, mask, size); \
313 res_ptr = (u##size *)&(ioc)->res_map[0]; \
314 CCIO_SEARCH_LOOP(ioa, res_idx, mask, size);
342 unsigned int pages_needed = size >>
IOVP_SHIFT;
343 unsigned int res_idx;
344 unsigned long boundary_size;
345 #ifdef CCIO_COLLECT_STATS
346 unsigned long cr_start =
mfctl(16);
349 BUG_ON(pages_needed == 0);
352 DBG_RES(
"%s() size: %d pages_needed %d\n",
353 __func__, size, pages_needed);
360 boundary_size =
ALIGN((
unsigned long long)dma_get_seg_boundary(dev) + 1,
363 if (pages_needed <= 8) {
374 unsigned long mask = ~(~0
UL >> pages_needed);
379 }
else if (pages_needed <= 16) {
381 }
else if (pages_needed <= 32) {
384 }
else if (pages_needed <= 64) {
388 panic(
"%s: %s() Too many pages to map. pages_needed: %u\n",
389 __FILE__, __func__, pages_needed);
392 panic(
"%s: %s() I/O MMU is out of mapping resources.\n", __FILE__,
397 DBG_RES(
"%s() res_idx %d res_hint: %d\n",
400 #ifdef CCIO_COLLECT_STATS
402 unsigned long cr_end =
mfctl(16);
403 unsigned long tmp = cr_end - cr_start;
405 cr_start = (cr_end < cr_start) ? -(tmp) : (
tmp);
407 ioc->avg_search[ioc->avg_idx++] = cr_start;
408 ioc->avg_idx &= CCIO_SEARCH_SAMPLE - 1;
409 ioc->used_pages += pages_needed;
417 #define CCIO_FREE_MAPPINGS(ioc, res_idx, mask, size) \
418 u##size *res_ptr = (u##size *)&((ioc)->res_map[res_idx]); \
419 BUG_ON((*res_ptr & mask) != mask); \
437 BUG_ON(pages_mapped == 0);
441 DBG_RES(
"%s(): res_idx: %d pages_mapped %d\n",
442 __func__, res_idx, pages_mapped);
444 #ifdef CCIO_COLLECT_STATS
445 ioc->used_pages -= pages_mapped;
448 if(pages_mapped <= 8) {
451 unsigned long mask = ~(~0
UL >> pages_mapped);
456 }
else if(pages_mapped <= 16) {
458 }
else if(pages_mapped <= 32) {
461 }
else if(pages_mapped <= 64) {
465 panic(
"%s:%s() Too many pages to unmap.\n", __FILE__,
477 #define KERNEL_SPACE 0
505 #define IOPDIR_VALID 0x01UL
506 #define HINT_SAFE_DMA 0x02UL
508 #define HINT_STOP_MOST 0x04UL
510 #define HINT_STOP_MOST 0x00UL
512 #define HINT_UDPATE_ENB 0x08UL
513 #define HINT_PREFETCH 0x10UL
521 static u32 hint_lookup[] = {
557 ccio_io_pdir_entry(
u64 *pdir_ptr,
space_t sid,
unsigned long vba,
560 register unsigned long pa;
561 register unsigned long ci;
574 asm volatile(
"depw %1,31,12,%0" :
"+r" (pa) :
"r" (hints));
575 ((
u32 *)pdir_ptr)[1] = (
u32) pa;
587 asm volatile (
"extrd,u %1,15,4,%0" :
"=r" (ci) :
"r" (pa));
588 asm volatile (
"extrd,u %1,31,16,%0" :
"+r" (pa) :
"r" (pa));
589 asm volatile (
"depd %1,35,4,%0" :
"+r" (pa) :
"r" (ci));
598 asm volatile (
"lci %%r0(%%sr1, %1), %0" :
"=r" (ci) :
"r" (vba));
599 asm volatile (
"extru %1,19,12,%0" :
"+r" (ci) :
"r" (ci));
600 asm volatile (
"depw %1,15,12,%0" :
"+r" (pa) :
"r" (ci));
602 ((
u32 *)pdir_ptr)[0] = (
u32) pa;
616 asm volatile(
"fdc %%r0(%0)" : :
"r" (pdir_ptr));
617 asm volatile(
"sync");
636 byte_cnt += chain_size;
638 while(byte_cnt > chain_size) {
641 byte_cnt -= chain_size;
664 ccio_mark_invalid(
struct ioc *ioc,
dma_addr_t iova,
size_t byte_cnt)
667 size_t saved_byte_cnt;
672 while(byte_cnt > 0) {
675 char *pdir_ptr = (
char *) &(ioc->
pdir_base[idx]);
687 asm volatile(
"fdc %%r0(%0)" : :
"r" (pdir_ptr[7]));
693 asm volatile(
"sync");
694 ccio_clear_io_tlb(ioc,
CCIO_IOVP(iova), saved_byte_cnt);
720 return (
int)(mask == 0xffffffff
UL);
742 unsigned long hint = hint_lookup[(
int)direction];
756 #ifdef CCIO_COLLECT_STATS
757 ioc->msingle_calls++;
761 idx = ccio_alloc_range(ioc, dev, size);
766 DBG_RUN(
"%s() 0x%p -> 0x%lx size: %0x%x\n",
767 __func__, addr, (
long)iovp | offset, size);
770 if((size %
L1_CACHE_BYTES) || ((
unsigned long)addr % L1_CACHE_BYTES))
774 ccio_io_pdir_entry(pdir_start,
KERNEL_SPACE, (
unsigned long)addr, hint);
778 (
u32) (((
u32 *) pdir_start)[0]),
779 (
u32) (((
u32 *) pdir_start)[1]));
785 spin_unlock_irqrestore(&ioc->
res_lock, flags);
811 DBG_RUN(
"%s() iovp 0x%lx/%x\n",
812 __func__, (
long)iova, size);
820 #ifdef CCIO_COLLECT_STATS
821 ioc->usingle_calls++;
825 ccio_mark_invalid(ioc, iova, size);
826 ccio_free_range(ioc, iova, (size >> IOVP_SHIFT));
827 spin_unlock_irqrestore(&ioc->
res_lock, flags);
872 ccio_free_consistent(
struct device *dev,
size_t size,
void *
cpu_addr,
875 ccio_unmap_single(dev, dma_handle, size, 0);
884 #define PIDE_FLAG 0x80000000UL
886 #ifdef CCIO_COLLECT_STATS
887 #define IOMMU_MAP_STATS
905 int coalesced, filled = 0;
907 unsigned long hint = hint_lookup[(
int)direction];
908 unsigned long prev_len = 0, current_len = 0;
914 DBG_RUN_SG(
"%s() START %d entries\n", __func__, nents);
925 for(i = 0; i < nents; i++)
926 prev_len += sglist[i].
length;
930 #ifdef CCIO_COLLECT_STATS
942 coalesced = iommu_coalesce_chunks(ioc, dev, sglist, nents, ccio_alloc_range);
952 filled = iommu_fill_pdir(ioc, sglist, nents, hint, ccio_io_pdir_entry);
954 spin_unlock_irqrestore(&ioc->
res_lock, flags);
956 BUG_ON(coalesced != filled);
958 DBG_RUN_SG(
"%s() DONE %d mappings\n", __func__, filled);
960 for (i = 0; i < filled; i++)
963 BUG_ON(current_len != prev_len);
986 DBG_RUN_SG(
"%s() START %d entries, %08lx,%x\n",
989 #ifdef CCIO_COLLECT_STATS
995 #ifdef CCIO_COLLECT_STATS
1003 DBG_RUN_SG(
"%s() DONE (nents %d)\n", __func__, nents);
1007 .dma_supported = ccio_dma_supported,
1008 .alloc_consistent = ccio_alloc_consistent,
1009 .alloc_noncoherent = ccio_alloc_consistent,
1010 .free_consistent = ccio_free_consistent,
1011 .map_single = ccio_map_single,
1012 .unmap_single = ccio_unmap_single,
1013 .map_sg = ccio_map_sg,
1014 .unmap_sg = ccio_unmap_sg,
1015 .dma_sync_single_for_cpu =
NULL,
1016 .dma_sync_single_for_device =
NULL,
1017 .dma_sync_sg_for_cpu =
NULL,
1018 .dma_sync_sg_for_device =
NULL,
1021 #ifdef CONFIG_PROC_FS
1022 static int ccio_proc_info(
struct seq_file *
m,
void *
p)
1025 struct ioc *ioc = ioc_list;
1027 while (ioc !=
NULL) {
1028 unsigned int total_pages = ioc->
res_size << 3;
1029 #ifdef CCIO_COLLECT_STATS
1039 len +=
seq_printf(m,
"IO PDIR size : %d bytes (%d entries)\n",
1040 total_pages * 8, total_pages);
1042 #ifdef CCIO_COLLECT_STATS
1043 len +=
seq_printf(m,
"IO PDIR entries : %ld free %ld used (%d%%)\n",
1044 total_pages - ioc->used_pages, ioc->used_pages,
1045 (
int)(ioc->used_pages * 100 / total_pages));
1048 len +=
seq_printf(m,
"Resource bitmap : %d bytes (%d pages)\n",
1051 #ifdef CCIO_COLLECT_STATS
1052 min = max = ioc->avg_search[0];
1053 for(j = 0; j < CCIO_SEARCH_SAMPLE; ++
j) {
1054 avg += ioc->avg_search[
j];
1055 if(ioc->avg_search[j] > max)
1056 max = ioc->avg_search[
j];
1057 if(ioc->avg_search[j] <
min)
1058 min = ioc->avg_search[
j];
1060 avg /= CCIO_SEARCH_SAMPLE;
1061 len +=
seq_printf(m,
" Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)\n",
1064 len +=
seq_printf(m,
"pci_map_single(): %8ld calls %8ld pages (avg %d/1000)\n",
1065 ioc->msingle_calls, ioc->msingle_pages,
1066 (
int)((ioc->msingle_pages * 1000)/ioc->msingle_calls));
1069 min = ioc->usingle_calls - ioc->usg_calls;
1070 max = ioc->usingle_pages - ioc->usg_pages;
1071 len +=
seq_printf(m,
"pci_unmap_single: %8ld calls %8ld pages (avg %d/1000)\n",
1072 min, max, (
int)((max * 1000)/
min));
1074 len +=
seq_printf(m,
"pci_map_sg() : %8ld calls %8ld pages (avg %d/1000)\n",
1075 ioc->msg_calls, ioc->msg_pages,
1076 (
int)((ioc->msg_pages * 1000)/ioc->msg_calls));
1078 len +=
seq_printf(m,
"pci_unmap_sg() : %8ld calls %8ld pages (avg %d/1000)\n\n\n",
1079 ioc->usg_calls, ioc->usg_pages,
1080 (
int)((ioc->usg_pages * 1000)/ioc->usg_calls));
1096 .open = ccio_proc_info_open,
1102 static int ccio_proc_bitmap_info(
struct seq_file *m,
void *p)
1105 struct ioc *ioc = ioc_list;
1107 while (ioc !=
NULL) {
1132 .open = ccio_proc_bitmap_open,
1146 static struct ioc * ccio_find_ioc(
int hw_path)
1152 for (i = 0; i < ioc_count; i++) {
1175 return ccio_find_ioc(dev->
hw_path);
1178 #define CUJO_20_STEP 0x10000000
1196 res_ptr[
idx] |= 0xff;
1217 if (dev->spa_shift == 0) {
1218 panic(
"%s() : Can't determine I/O TLB size.\n", __func__);
1220 return (1 << dev->spa_shift);
1225 #define CCIO_CHAINID_SHIFT 8
1226 #define CCIO_CHAINID_MASK 0xff
1240 .id_table = ccio_tbl,
1241 .probe = ccio_probe,
1253 ccio_ioc_init(
struct ioc *ioc)
1256 unsigned int iov_order;
1257 u32 iova_space_size;
1274 if (iova_space_size < (1 << (20 -
PAGE_SHIFT))) {
1277 }
else if (iova_space_size > (1 << (30 -
PAGE_SHIFT))) {
1298 iova_space_size = 1 << (iov_order +
PAGE_SHIFT);
1307 DBG_INIT(
"%s() hpa 0x%p mem %luMB IOV %dMB (%d bits)\n",
1309 (
unsigned long) totalram_pages >> (20 -
PAGE_SHIFT),
1310 iova_space_size>>20,
1316 panic(
"%s() could not allocate I/O Page Table\n", __func__);
1330 panic(
"%s() could not allocate resource map\n", __func__);
1367 for(i = 1 << CCIO_CHAINID_SHIFT;
i ; i--) {
1386 res->
end = (
unsigned long)((
signed) (
READ_U32(ioaddr + 4) << 16) - 1);
1401 printk(
KERN_ERR "%s() failed to claim CCIO bus address space (%08lx,%08lx)\n",
1402 __func__, (
unsigned long)res->
start, (
unsigned long)res->
end);
1406 static void __init ccio_init_resources(
struct ioc *ioc)
1413 ccio_init_resource(res, name, &ioc->
ioc_regs->io_io_low);
1414 ccio_init_resource(res + 1, name, &ioc->
ioc_regs->io_io_low_hv);
1417 static int new_ioc_area(
struct resource *res,
unsigned long size,
1418 unsigned long min,
unsigned long max,
unsigned long align)
1423 res->
start = (max - size + 1) &~ (align - 1);
1433 return new_ioc_area(res, size, min, max - size, align);
1436 static int expand_ioc_area(
struct resource *res,
unsigned long size,
1437 unsigned long min,
unsigned long max,
unsigned long align)
1439 unsigned long start, len;
1442 return new_ioc_area(res, size, min, max, align);
1444 start = (res->
start -
size) &~ (align - 1);
1445 len = res->
end - start + 1;
1452 len = ((size + res->
end +
align) &~ (align - 1)) - start;
1453 if (start + len <= max) {
1469 struct resource *res,
unsigned long size,
1470 unsigned long min,
unsigned long max,
unsigned long align)
1482 if ((parent + 1)->parent &&
1487 if (!expand_ioc_area(parent, size, min, max, align)) {
1492 }
else if (!expand_ioc_area(parent + 1, size, min, max, align)) {
1543 struct ioc *ioc, **ioc_p = &ioc_list;
1545 ioc = kzalloc(
sizeof(
struct ioc),
GFP_KERNEL);
1554 (
unsigned long)dev->
hpa.start);
1556 for (i = 0; i < ioc_count; i++) {
1557 ioc_p = &(*ioc_p)->
next;
1564 ccio_init_resources(ioc);
1572 #ifdef CONFIG_PROC_FS
1573 if (ioc_count == 0) {
1575 &ccio_proc_info_fops);
1576 proc_create(
MODULE_NAME"-bitmap", 0, proc_runway_root,
1577 &ccio_proc_bitmap_fops);