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