vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc64 / kernel / pci_iommu.c
1 /*
2  * arch/ppc64/kernel/pci_iommu.c
3  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
4  * 
5  * Rewrite, cleanup, new allocation schemes: 
6  * Copyright (C) 2004 Olof Johansson, IBM Corporation
7  *
8  * Dynamic DMA mapping support, platform-independent parts.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25
26 #include <linux/config.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/spinlock.h>
32 #include <linux/string.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
35 #include <asm/io.h>
36 #include <asm/prom.h>
37 #include <asm/iommu.h>
38 #include <asm/pci-bridge.h>
39 #include <asm/machdep.h>
40 #include "pci.h"
41
42 #ifdef CONFIG_PPC_ISERIES
43 #include <asm/iSeries/iSeries_pci.h>
44 #endif /* CONFIG_PPC_ISERIES */
45
46 /*
47  * We can use ->sysdata directly and avoid the extra work in
48  * pci_device_to_OF_node since ->sysdata will have been initialised
49  * in the iommu init code for all devices.
50  */
51 #define PCI_GET_DN(dev) ((struct device_node *)((dev)->sysdata))
52
53 static inline struct iommu_table *devnode_table(struct pci_dev *dev)
54 {
55         if (!dev)
56                 dev = ppc64_isabridge_dev;
57         if (!dev)
58                 return NULL;
59
60 #ifdef CONFIG_PPC_ISERIES
61         return ISERIES_DEVNODE(dev)->iommu_table;
62 #endif /* CONFIG_PPC_ISERIES */
63
64 #ifdef CONFIG_PPC_MULTIPLATFORM
65         return PCI_GET_DN(dev)->iommu_table;
66 #endif /* CONFIG_PPC_MULTIPLATFORM */
67 }
68
69
70 /* Allocates a contiguous real buffer and creates mappings over it.
71  * Returns the virtual address of the buffer and sets dma_handle
72  * to the dma address (mapping) of the first page.
73  */
74 static void *pci_iommu_alloc_consistent(struct pci_dev *hwdev, size_t size,
75                            dma_addr_t *dma_handle)
76 {
77         return iommu_alloc_consistent(devnode_table(hwdev), size, dma_handle);
78 }
79
80 static void pci_iommu_free_consistent(struct pci_dev *hwdev, size_t size,
81                          void *vaddr, dma_addr_t dma_handle)
82 {
83         iommu_free_consistent(devnode_table(hwdev), size, vaddr, dma_handle);
84 }
85
86 /* Creates TCEs for a user provided buffer.  The user buffer must be 
87  * contiguous real kernel storage (not vmalloc).  The address of the buffer
88  * passed here is the kernel (virtual) address of the buffer.  The buffer
89  * need not be page aligned, the dma_addr_t returned will point to the same
90  * byte within the page as vaddr.
91  */
92 static dma_addr_t pci_iommu_map_single(struct pci_dev *hwdev, void *vaddr,
93                 size_t size, enum dma_data_direction direction)
94 {
95         return iommu_map_single(devnode_table(hwdev), vaddr, size, direction);
96 }
97
98
99 static void pci_iommu_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_handle,
100                 size_t size, enum dma_data_direction direction)
101 {
102         iommu_unmap_single(devnode_table(hwdev), dma_handle, size, direction);
103 }
104
105
106 static int pci_iommu_map_sg(struct pci_dev *pdev, struct scatterlist *sglist,
107                 int nelems, enum dma_data_direction direction)
108 {
109         return iommu_map_sg(&pdev->dev, devnode_table(pdev), sglist,
110                         nelems, direction);
111 }
112
113 static void pci_iommu_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist,
114                 int nelems, enum dma_data_direction direction)
115 {
116         iommu_unmap_sg(devnode_table(pdev), sglist, nelems, direction);
117 }
118
119 /* We support DMA to/from any memory page via the iommu */
120 static int pci_iommu_dma_supported(struct pci_dev *pdev, u64 mask)
121 {
122         return 1;
123 }
124
125 void pci_iommu_init(void)
126 {
127         pci_dma_ops.pci_alloc_consistent = pci_iommu_alloc_consistent;
128         pci_dma_ops.pci_free_consistent = pci_iommu_free_consistent;
129         pci_dma_ops.pci_map_single = pci_iommu_map_single;
130         pci_dma_ops.pci_unmap_single = pci_iommu_unmap_single;
131         pci_dma_ops.pci_map_sg = pci_iommu_map_sg;
132         pci_dma_ops.pci_unmap_sg = pci_iommu_unmap_sg;
133         pci_dma_ops.pci_dma_supported = pci_iommu_dma_supported;
134 }