blob: 3281f10bbd16ef328c202c37e66f87b6f0f9b229 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3 *
4 * Rewrite, cleanup:
5 *
Olof Johansson91f14482005-11-21 02:12:32 -06006 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
Olof Johanssonbc97ce92006-04-28 22:51:59 -05007 * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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>
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110030#include <linux/pci.h>
Stephen Rothwell1670b2b2007-10-11 14:53:32 +100031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/iommu.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100034#include <asm/tce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/machdep.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100036#include <asm/abs_addr.h>
Stephen Rothwell96ff6af2006-05-19 16:48:47 +100037#include <asm/prom.h>
Stephen Rothwellbffa8fc2005-10-14 15:06:10 +100038#include <asm/pci-bridge.h>
Kelly Daly8021b8a2005-11-02 11:41:12 +110039#include <asm/iseries/hv_call_xm.h>
Stephen Rothwell1670b2b2007-10-11 14:53:32 +100040#include <asm/iseries/hv_call_event.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100041#include <asm/iseries/iommu.h>
Stephen Rothwell9bd7ea62006-01-12 13:58:19 +110042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static 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;
Olof Johanssonbc97ce92006-04-28 22:51:59 -050047 u64 tce, rpn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 while (npages--) {
Olof Johanssonbc97ce92006-04-28 22:51:59 -050050 rpn = virt_to_abs(uaddr) >> TCE_SHIFT;
51 tce = (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if (tbl->it_type == TCE_VB) {
54 /* Virtual Bus */
Olof Johanssonbc97ce92006-04-28 22:51:59 -050055 tce |= TCE_VALID|TCE_ALLIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050057 tce |= TCE_VB_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 } else {
59 /* PCI Bus */
Olof Johanssonbc97ce92006-04-28 22:51:59 -050060 tce |= TCE_PCI_READ; /* Read allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (direction != DMA_TO_DEVICE)
Olof Johanssonbc97ce92006-04-28 22:51:59 -050062 tce |= TCE_PCI_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
Olof Johanssonbc97ce92006-04-28 22:51:59 -050065 rc = HvCallXm_setTce((u64)tbl->it_index, (u64)index, tce);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (rc)
67 panic("PCI_DMA: HvCallXm_setTce failed, Rc: 0x%lx\n",
68 rc);
69 index++;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110070 uaddr += TCE_PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72}
73
74static 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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110087/*
Michael Ellermanca652c92006-07-14 14:25:33 +100088 * Structure passed to HvCallXm_getTceTableParms
89 */
90struct 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/*
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100105 * 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 */
113void 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
Yan Burmanf8485352006-12-02 13:26:57 +0200120 parms = kzalloc(sizeof(*parms), GFP_KERNEL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100121 if (parms == NULL)
122 panic("PCI_DMA: TCE Table Allocation failed.");
123
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100124 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 */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100134 tbl->it_size = (parms->itc_size * TCE_PAGE_SIZE) / TCE_ENTRY_SIZE;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100135 tbl->it_busno = parms->itc_busno;
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100136 tbl->it_offset = parms->itc_offset;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100137 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
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700145#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * This function compares the known tables to find an iommu_table
148 * that has already been built for hardware TCEs.
149 */
150static struct iommu_table *iommu_table_find(struct iommu_table * tbl)
151{
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000152 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000154 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;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000161 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 return NULL;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100172void iommu_devnode_init_iSeries(struct pci_dev *pdev, struct device_node *dn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct iommu_table *tbl;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000175 struct pci_dn *pdn = PCI_DN(dn);
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000176 const u32 *lsn = of_get_property(dn, "linux,logical-slot-number", NULL);
Stephen Rothwell403fac42006-05-19 16:51:57 +1000177
178 BUG_ON(lsn == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
181
Stephen Rothwell403fac42006-05-19 16:51:57 +1000182 iommu_table_getparms_iSeries(pdn->busno, *lsn, 0, tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* Look for existing tce table */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000185 pdn->iommu_table = iommu_table_find(tbl);
186 if (pdn->iommu_table == NULL)
Anton Blanchardca1588e2006-06-10 20:58:08 +1000187 pdn->iommu_table = iommu_init_table(tbl, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 else
189 kfree(tbl);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100190 pdev->dev.archdata.dma_data = pdn->iommu_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Stephen Rothwell145d01e2005-06-21 17:15:52 -0700192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Stephen Rothwell1670b2b2007-10-11 14:53:32 +1000194extern struct iommu_table vio_iommu_table;
195
196void *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}
201EXPORT_SYMBOL_GPL(iseries_hv_alloc);
202
203void 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}
207EXPORT_SYMBOL_GPL(iseries_hv_free);
208
209dma_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
216void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222void iommu_init_early_iSeries(void)
223{
224 ppc_md.tce_build = tce_build_iSeries;
225 ppc_md.tce_free = tce_free_iSeries;
226
Stephen Rothwell98747772007-03-04 16:58:39 +1100227 set_pci_dma_ops(&dma_iommu_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}