[POWERPC] Remove iSeries_vio_dev
[safe/jmp/linux-2.6] / arch / powerpc / platforms / iseries / iommu.c
1 /*
2  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3  *
4  * Rewrite, cleanup:
5  *
6  * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
7  * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
8  *
9  * Dynamic DMA mapping support, iSeries-specific parts.
10  *
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <linux/types.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/list.h>
30 #include <linux/pci.h>
31 #include <linux/module.h>
32
33 #include <asm/iommu.h>
34 #include <asm/tce.h>
35 #include <asm/machdep.h>
36 #include <asm/abs_addr.h>
37 #include <asm/prom.h>
38 #include <asm/pci-bridge.h>
39 #include <asm/iseries/hv_call_xm.h>
40 #include <asm/iseries/hv_call_event.h>
41 #include <asm/iseries/iommu.h>
42
43 static void tce_build_iSeries(struct iommu_table *tbl, long index, long npages,
44                 unsigned long uaddr, enum dma_data_direction direction)
45 {
46         u64 rc;
47         u64 tce, rpn;
48
49         while (npages--) {
50                 rpn = virt_to_abs(uaddr) >> TCE_SHIFT;
51                 tce = (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
52
53                 if (tbl->it_type == TCE_VB) {
54                         /* Virtual Bus */
55                         tce |= TCE_VALID|TCE_ALLIO;
56                         if (direction != DMA_TO_DEVICE)
57                                 tce |= TCE_VB_WRITE;
58                 } else {
59                         /* PCI Bus */
60                         tce |= TCE_PCI_READ; /* Read allowed */
61                         if (direction != DMA_TO_DEVICE)
62                                 tce |= TCE_PCI_WRITE;
63                 }
64
65                 rc = HvCallXm_setTce((u64)tbl->it_index, (u64)index, tce);
66                 if (rc)
67                         panic("PCI_DMA: HvCallXm_setTce failed, Rc: 0x%lx\n",
68                                         rc);
69                 index++;
70                 uaddr += TCE_PAGE_SIZE;
71         }
72 }
73
74 static void tce_free_iSeries(struct iommu_table *tbl, long index, long npages)
75 {
76         u64 rc;
77
78         while (npages--) {
79                 rc = HvCallXm_setTce((u64)tbl->it_index, (u64)index, 0);
80                 if (rc)
81                         panic("PCI_DMA: HvCallXm_setTce failed, Rc: 0x%lx\n",
82                                         rc);
83                 index++;
84         }
85 }
86
87 /*
88  * Structure passed to HvCallXm_getTceTableParms
89  */
90 struct iommu_table_cb {
91         unsigned long   itc_busno;      /* Bus number for this tce table */
92         unsigned long   itc_start;      /* Will be NULL for secondary */
93         unsigned long   itc_totalsize;  /* Size (in pages) of whole table */
94         unsigned long   itc_offset;     /* Index into real tce table of the
95                                            start of our section */
96         unsigned long   itc_size;       /* Size (in pages) of our section */
97         unsigned long   itc_index;      /* Index of this tce table */
98         unsigned short  itc_maxtables;  /* Max num of tables for partition */
99         unsigned char   itc_virtbus;    /* Flag to indicate virtual bus */
100         unsigned char   itc_slotno;     /* IOA Tce Slot Index */
101         unsigned char   itc_rsvd[4];
102 };
103
104 /*
105  * Call Hv with the architected data structure to get TCE table info.
106  * info. Put the returned data into the Linux representation of the
107  * TCE table data.
108  * The Hardware Tce table comes in three flavors.
109  * 1. TCE table shared between Buses.
110  * 2. TCE table per Bus.
111  * 3. TCE Table per IOA.
112  */
113 void iommu_table_getparms_iSeries(unsigned long busno,
114                                   unsigned char slotno,
115                                   unsigned char virtbus,
116                                   struct iommu_table* tbl)
117 {
118         struct iommu_table_cb *parms;
119
120         parms = kzalloc(sizeof(*parms), GFP_KERNEL);
121         if (parms == NULL)
122                 panic("PCI_DMA: TCE Table Allocation failed.");
123
124         parms->itc_busno = busno;
125         parms->itc_slotno = slotno;
126         parms->itc_virtbus = virtbus;
127
128         HvCallXm_getTceTableParms(iseries_hv_addr(parms));
129
130         if (parms->itc_size == 0)
131                 panic("PCI_DMA: parms->size is zero, parms is 0x%p", parms);
132
133         /* itc_size is in pages worth of table, it_size is in # of entries */
134         tbl->it_size = (parms->itc_size * TCE_PAGE_SIZE) / TCE_ENTRY_SIZE;
135         tbl->it_busno = parms->itc_busno;
136         tbl->it_offset = parms->itc_offset;
137         tbl->it_index = parms->itc_index;
138         tbl->it_blocksize = 1;
139         tbl->it_type = virtbus ? TCE_VB : TCE_PCI;
140
141         kfree(parms);
142 }
143
144
145 #ifdef CONFIG_PCI
146 /*
147  * This function compares the known tables to find an iommu_table
148  * that has already been built for hardware TCEs.
149  */
150 static struct iommu_table *iommu_table_find(struct iommu_table * tbl)
151 {
152         struct device_node *node;
153
154         for (node = NULL; (node = of_find_all_nodes(node)); ) {
155                 struct pci_dn *pdn = PCI_DN(node);
156                 struct iommu_table *it;
157
158                 if (pdn == NULL)
159                         continue;
160                 it = pdn->iommu_table;
161                 if ((it != NULL) &&
162                     (it->it_type == TCE_PCI) &&
163                     (it->it_offset == tbl->it_offset) &&
164                     (it->it_index == tbl->it_index) &&
165                     (it->it_size == tbl->it_size))
166                         return it;
167         }
168         return NULL;
169 }
170
171
172 void iommu_devnode_init_iSeries(struct pci_dev *pdev, struct device_node *dn)
173 {
174         struct iommu_table *tbl;
175         struct pci_dn *pdn = PCI_DN(dn);
176         const u32 *lsn = of_get_property(dn, "linux,logical-slot-number", NULL);
177
178         BUG_ON(lsn == NULL);
179
180         tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
181
182         iommu_table_getparms_iSeries(pdn->busno, *lsn, 0, tbl);
183
184         /* Look for existing tce table */
185         pdn->iommu_table = iommu_table_find(tbl);
186         if (pdn->iommu_table == NULL)
187                 pdn->iommu_table = iommu_init_table(tbl, -1);
188         else
189                 kfree(tbl);
190         pdev->dev.archdata.dma_data = pdn->iommu_table;
191 }
192 #endif
193
194 extern struct iommu_table vio_iommu_table;
195
196 void *iseries_hv_alloc(size_t size, dma_addr_t *dma_handle, gfp_t flag)
197 {
198         return iommu_alloc_coherent(&vio_iommu_table, size, dma_handle,
199                                 DMA_32BIT_MASK, flag, -1);
200 }
201 EXPORT_SYMBOL_GPL(iseries_hv_alloc);
202
203 void iseries_hv_free(size_t size, void *vaddr, dma_addr_t dma_handle)
204 {
205         iommu_free_coherent(&vio_iommu_table, size, vaddr, dma_handle);
206 }
207 EXPORT_SYMBOL_GPL(iseries_hv_free);
208
209 dma_addr_t iseries_hv_map(void *vaddr, size_t size,
210                         enum dma_data_direction direction)
211 {
212         return iommu_map_single(&vio_iommu_table, vaddr, size,
213                                 DMA_32BIT_MASK, direction);
214 }
215
216 void iseries_hv_unmap(dma_addr_t dma_handle, size_t size,
217                         enum dma_data_direction direction)
218 {
219         iommu_unmap_single(&vio_iommu_table, dma_handle, size, direction);
220 }
221
222 void iommu_init_early_iSeries(void)
223 {
224         ppc_md.tce_build = tce_build_iSeries;
225         ppc_md.tce_free  = tce_free_iSeries;
226
227         set_pci_dma_ops(&dma_iommu_ops);
228 }