1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/types.h>
27#include <linux/slab.h>
28#include <linux/mm.h>
29#include <linux/spinlock.h>
30#include <linux/string.h>
31#include <linux/pci.h>
32#include <linux/dma-mapping.h>
33#include <linux/bootmem.h>
34#include <asm/tce.h>
35#include <asm/calgary.h>
36#include <asm/proto.h>
37
38
39static inline void flush_tce(void* tceaddr)
40{
41
42 if (cpu_has_clflush)
43 clflush(tceaddr);
44 else
45 wbinvd();
46}
47
48void tce_build(struct iommu_table *tbl, unsigned long index,
49 unsigned int npages, unsigned long uaddr, int direction)
50{
51 u64* tp;
52 u64 t;
53 u64 rpn;
54
55 t = (1 << TCE_READ_SHIFT);
56 if (direction != DMA_TO_DEVICE)
57 t |= (1 << TCE_WRITE_SHIFT);
58
59 tp = ((u64*)tbl->it_base) + index;
60
61 while (npages--) {
62 rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
63 t &= ~TCE_RPN_MASK;
64 t |= (rpn << TCE_RPN_SHIFT);
65
66 *tp = cpu_to_be64(t);
67 flush_tce(tp);
68
69 uaddr += PAGE_SIZE;
70 tp++;
71 }
72}
73
74void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
75{
76 u64* tp;
77
78 tp = ((u64*)tbl->it_base) + index;
79
80 while (npages--) {
81 *tp = cpu_to_be64(0);
82 flush_tce(tp);
83 tp++;
84 }
85}
86
87static inline unsigned int table_size_to_number_of_entries(unsigned char size)
88{
89
90
91
92
93
94 return (1 << size) << 13;
95}
96
97static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
98{
99 unsigned int bitmapsz;
100 unsigned long bmppages;
101 int ret;
102
103 tbl->it_busno = dev->bus->number;
104
105
106 tbl->it_size = table_size_to_number_of_entries(specified_table_size);
107
108
109
110
111
112 bitmapsz = tbl->it_size / BITS_PER_BYTE;
113 bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
114 if (!bmppages) {
115 printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
116 ret = -ENOMEM;
117 goto done;
118 }
119
120 tbl->it_map = (unsigned long*)bmppages;
121
122 memset(tbl->it_map, 0, bitmapsz);
123
124 tbl->it_hint = 0;
125
126 spin_lock_init(&tbl->it_lock);
127
128 return 0;
129
130done:
131 return ret;
132}
133
134int __init build_tce_table(struct pci_dev *dev, void __iomem *bbar)
135{
136 struct iommu_table *tbl;
137 int ret;
138
139 if (pci_iommu(dev->bus)) {
140 printk(KERN_ERR "Calgary: dev %p has sysdata->iommu %p\n",
141 dev, pci_iommu(dev->bus));
142 BUG();
143 }
144
145 tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
146 if (!tbl) {
147 printk(KERN_ERR "Calgary: error allocating iommu_table\n");
148 ret = -ENOMEM;
149 goto done;
150 }
151
152 ret = tce_table_setparms(dev, tbl);
153 if (ret)
154 goto free_tbl;
155
156 tbl->bbar = bbar;
157
158 set_pci_iommu(dev->bus, tbl);
159
160 return 0;
161
162free_tbl:
163 kfree(tbl);
164done:
165 return ret;
166}
167
168void * __init alloc_tce_table(void)
169{
170 unsigned int size;
171
172 size = table_size_to_number_of_entries(specified_table_size);
173 size *= TCE_ENTRY_SIZE;
174
175 return __alloc_bootmem_low(size, size, 0);
176}
177
178void __init free_tce_table(void *tbl)
179{
180 unsigned int size;
181
182 if (!tbl)
183 return;
184
185 size = table_size_to_number_of_entries(specified_table_size);
186 size *= TCE_ENTRY_SIZE;
187
188 free_bootmem(__pa(tbl), size);
189}
190