Merge master.kernel.org:/home/rmk/linux-2.6-arm
[sfrench/cifs-2.6.git] / arch / x86_64 / kernel / tce.c
1 /*
2  * Derived from arch/powerpc/platforms/pseries/iommu.c
3  *
4  * Copyright (C) 2006 Jon Mason <jdmason@us.ibm.com>, IBM Corporation
5  * Copyright (C) 2006 Muli Ben-Yehuda <muli@il.ibm.com>, IBM Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <linux/config.h>
23 #include <linux/types.h>
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/spinlock.h>
27 #include <linux/string.h>
28 #include <linux/pci.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/bootmem.h>
31 #include <asm/tce.h>
32 #include <asm/calgary.h>
33 #include <asm/proto.h>
34
35 /* flush a tce at 'tceaddr' to main memory */
36 static inline void flush_tce(void* tceaddr)
37 {
38         /* a single tce can't cross a cache line */
39         if (cpu_has_clflush)
40                 asm volatile("clflush (%0)" :: "r" (tceaddr));
41         else
42                 asm volatile("wbinvd":::"memory");
43 }
44
45 void tce_build(struct iommu_table *tbl, unsigned long index,
46         unsigned int npages, unsigned long uaddr, int direction)
47 {
48         u64* tp;
49         u64 t;
50         u64 rpn;
51
52         t = (1 << TCE_READ_SHIFT);
53         if (direction != DMA_TO_DEVICE)
54                 t |= (1 << TCE_WRITE_SHIFT);
55
56         tp = ((u64*)tbl->it_base) + index;
57
58         while (npages--) {
59                 rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
60                 t &= ~TCE_RPN_MASK;
61                 t |= (rpn << TCE_RPN_SHIFT);
62
63                 *tp = cpu_to_be64(t);
64                 flush_tce(tp);
65
66                 uaddr += PAGE_SIZE;
67                 tp++;
68         }
69 }
70
71 void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
72 {
73         u64* tp;
74
75         tp  = ((u64*)tbl->it_base) + index;
76
77         while (npages--) {
78                 *tp = cpu_to_be64(0);
79                 flush_tce(tp);
80                 tp++;
81         }
82 }
83
84 static inline unsigned int table_size_to_number_of_entries(unsigned char size)
85 {
86         /*
87          * size is the order of the table, 0-7
88          * smallest table is 8K entries, so shift result by 13 to
89          * multiply by 8K
90          */
91         return (1 << size) << 13;
92 }
93
94 static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
95 {
96         unsigned int bitmapsz;
97         unsigned int tce_table_index;
98         unsigned long bmppages;
99         int ret;
100
101         tbl->it_busno = dev->bus->number;
102
103         /* set the tce table size - measured in entries */
104         tbl->it_size = table_size_to_number_of_entries(specified_table_size);
105
106         tce_table_index = bus_to_phb(tbl->it_busno);
107         tbl->it_base = (unsigned long)tce_table_kva[tce_table_index];
108         if (!tbl->it_base) {
109                 printk(KERN_ERR "Calgary: iommu_table_setparms: "
110                        "no table allocated?!\n");
111                 ret = -ENOMEM;
112                 goto done;
113         }
114
115         /*
116          * number of bytes needed for the bitmap size in number of
117          * entries; we need one bit per entry
118          */
119         bitmapsz = tbl->it_size / BITS_PER_BYTE;
120         bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
121         if (!bmppages) {
122                 printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
123                 ret = -ENOMEM;
124                 goto done;
125         }
126
127         tbl->it_map = (unsigned long*)bmppages;
128
129         memset(tbl->it_map, 0, bitmapsz);
130
131         tbl->it_hint = 0;
132
133         spin_lock_init(&tbl->it_lock);
134
135         return 0;
136
137 done:
138         return ret;
139 }
140
141 int build_tce_table(struct pci_dev *dev, void __iomem *bbar)
142 {
143         struct iommu_table *tbl;
144         int ret;
145
146         if (dev->sysdata) {
147                 printk(KERN_ERR "Calgary: dev %p has sysdata %p\n",
148                        dev, dev->sysdata);
149                 BUG();
150         }
151
152         tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
153         if (!tbl) {
154                 printk(KERN_ERR "Calgary: error allocating iommu_table\n");
155                 ret = -ENOMEM;
156                 goto done;
157         }
158
159         ret = tce_table_setparms(dev, tbl);
160         if (ret)
161                 goto free_tbl;
162
163         tce_free(tbl, 0, tbl->it_size);
164
165         tbl->bbar = bbar;
166
167         /*
168          * NUMA is already using the bus's sysdata pointer, so we use
169          * the bus's pci_dev's sysdata instead.
170          */
171         dev->sysdata = tbl;
172
173         return 0;
174
175 free_tbl:
176         kfree(tbl);
177 done:
178         return ret;
179 }
180
181 void* alloc_tce_table(void)
182 {
183         unsigned int size;
184
185         size = table_size_to_number_of_entries(specified_table_size);
186         size *= TCE_ENTRY_SIZE;
187
188         return __alloc_bootmem_low(size, size, 0);
189 }
190
191 void free_tce_table(void *tbl)
192 {
193         unsigned int size;
194
195         if (!tbl)
196                 return;
197
198         size = table_size_to_number_of_entries(specified_table_size);
199         size *= TCE_ENTRY_SIZE;
200
201         free_bootmem(__pa(tbl), size);
202 }