Merge commit master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6 of HEAD
[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) 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/config.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <linux/mm.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/pci.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/bootmem.h>
33 #include <asm/tce.h>
34 #include <asm/calgary.h>
35 #include <asm/proto.h>
36
37 /* flush a tce at 'tceaddr' to main memory */
38 static inline void flush_tce(void* tceaddr)
39 {
40         /* a single tce can't cross a cache line */
41         if (cpu_has_clflush)
42                 asm volatile("clflush (%0)" :: "r" (tceaddr));
43         else
44                 asm volatile("wbinvd":::"memory");
45 }
46
47 void tce_build(struct iommu_table *tbl, unsigned long index,
48         unsigned int npages, unsigned long uaddr, int direction)
49 {
50         u64* tp;
51         u64 t;
52         u64 rpn;
53
54         t = (1 << TCE_READ_SHIFT);
55         if (direction != DMA_TO_DEVICE)
56                 t |= (1 << TCE_WRITE_SHIFT);
57
58         tp = ((u64*)tbl->it_base) + index;
59
60         while (npages--) {
61                 rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
62                 t &= ~TCE_RPN_MASK;
63                 t |= (rpn << TCE_RPN_SHIFT);
64
65                 *tp = cpu_to_be64(t);
66                 flush_tce(tp);
67
68                 uaddr += PAGE_SIZE;
69                 tp++;
70         }
71 }
72
73 void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
74 {
75         u64* tp;
76
77         tp  = ((u64*)tbl->it_base) + index;
78
79         while (npages--) {
80                 *tp = cpu_to_be64(0);
81                 flush_tce(tp);
82                 tp++;
83         }
84 }
85
86 static inline unsigned int table_size_to_number_of_entries(unsigned char size)
87 {
88         /*
89          * size is the order of the table, 0-7
90          * smallest table is 8K entries, so shift result by 13 to
91          * multiply by 8K
92          */
93         return (1 << size) << 13;
94 }
95
96 static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
97 {
98         unsigned int bitmapsz;
99         unsigned int tce_table_index;
100         unsigned long bmppages;
101         int ret;
102
103         tbl->it_busno = dev->bus->number;
104
105         /* set the tce table size - measured in entries */
106         tbl->it_size = table_size_to_number_of_entries(specified_table_size);
107
108         tce_table_index = bus_to_phb(tbl->it_busno);
109         tbl->it_base = (unsigned long)tce_table_kva[tce_table_index];
110         if (!tbl->it_base) {
111                 printk(KERN_ERR "Calgary: iommu_table_setparms: "
112                        "no table allocated?!\n");
113                 ret = -ENOMEM;
114                 goto done;
115         }
116
117         /*
118          * number of bytes needed for the bitmap size in number of
119          * entries; we need one bit per entry
120          */
121         bitmapsz = tbl->it_size / BITS_PER_BYTE;
122         bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
123         if (!bmppages) {
124                 printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
125                 ret = -ENOMEM;
126                 goto done;
127         }
128
129         tbl->it_map = (unsigned long*)bmppages;
130
131         memset(tbl->it_map, 0, bitmapsz);
132
133         tbl->it_hint = 0;
134
135         spin_lock_init(&tbl->it_lock);
136
137         return 0;
138
139 done:
140         return ret;
141 }
142
143 int build_tce_table(struct pci_dev *dev, void __iomem *bbar)
144 {
145         struct iommu_table *tbl;
146         int ret;
147
148         if (dev->sysdata) {
149                 printk(KERN_ERR "Calgary: dev %p has sysdata %p\n",
150                        dev, dev->sysdata);
151                 BUG();
152         }
153
154         tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
155         if (!tbl) {
156                 printk(KERN_ERR "Calgary: error allocating iommu_table\n");
157                 ret = -ENOMEM;
158                 goto done;
159         }
160
161         ret = tce_table_setparms(dev, tbl);
162         if (ret)
163                 goto free_tbl;
164
165         tce_free(tbl, 0, tbl->it_size);
166
167         tbl->bbar = bbar;
168
169         /*
170          * NUMA is already using the bus's sysdata pointer, so we use
171          * the bus's pci_dev's sysdata instead.
172          */
173         dev->sysdata = tbl;
174
175         return 0;
176
177 free_tbl:
178         kfree(tbl);
179 done:
180         return ret;
181 }
182
183 void* alloc_tce_table(void)
184 {
185         unsigned int size;
186
187         size = table_size_to_number_of_entries(specified_table_size);
188         size *= TCE_ENTRY_SIZE;
189
190         return __alloc_bootmem_low(size, size, 0);
191 }
192
193 void free_tce_table(void *tbl)
194 {
195         unsigned int size;
196
197         if (!tbl)
198                 return;
199
200         size = table_size_to_number_of_entries(specified_table_size);
201         size *= TCE_ENTRY_SIZE;
202
203         free_bootmem(__pa(tbl), size);
204 }