Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 | 2 | * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation |
426c1a11 SR |
3 | * |
4 | * Rewrite, cleanup, new allocation schemes: | |
1da177e4 LT |
5 | * Copyright (C) 2004 Olof Johansson, IBM Corporation |
6 | * | |
7 | * Dynamic DMA mapping support, platform-independent parts. | |
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. | |
426c1a11 | 13 | * |
1da177e4 LT |
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. | |
426c1a11 | 18 | * |
1da177e4 LT |
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 | ||
1da177e4 LT |
25 | #include <linux/init.h> |
26 | #include <linux/types.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/mm.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/string.h> | |
31 | #include <linux/pci.h> | |
32 | #include <linux/dma-mapping.h> | |
33 | #include <asm/io.h> | |
34 | #include <asm/prom.h> | |
35 | #include <asm/iommu.h> | |
36 | #include <asm/pci-bridge.h> | |
37 | #include <asm/machdep.h> | |
d387899f | 38 | #include <asm/ppc-pci.h> |
1da177e4 | 39 | |
1da177e4 LT |
40 | /* |
41 | * We can use ->sysdata directly and avoid the extra work in | |
42 | * pci_device_to_OF_node since ->sysdata will have been initialised | |
43 | * in the iommu init code for all devices. | |
44 | */ | |
45 | #define PCI_GET_DN(dev) ((struct device_node *)((dev)->sysdata)) | |
46 | ||
47 | static inline struct iommu_table *devnode_table(struct device *dev) | |
48 | { | |
49 | struct pci_dev *pdev; | |
50 | ||
51 | if (!dev) { | |
52 | pdev = ppc64_isabridge_dev; | |
53 | if (!pdev) | |
54 | return NULL; | |
55 | } else | |
56 | pdev = to_pci_dev(dev); | |
57 | ||
1635317f | 58 | return PCI_DN(PCI_GET_DN(pdev))->iommu_table; |
1da177e4 LT |
59 | } |
60 | ||
61 | ||
62 | /* Allocates a contiguous real buffer and creates mappings over it. | |
63 | * Returns the virtual address of the buffer and sets dma_handle | |
64 | * to the dma address (mapping) of the first page. | |
65 | */ | |
66 | static void *pci_iommu_alloc_coherent(struct device *hwdev, size_t size, | |
dd0fc66f | 67 | dma_addr_t *dma_handle, gfp_t flag) |
1da177e4 LT |
68 | { |
69 | return iommu_alloc_coherent(devnode_table(hwdev), size, dma_handle, | |
70 | flag); | |
71 | } | |
72 | ||
73 | static void pci_iommu_free_coherent(struct device *hwdev, size_t size, | |
74 | void *vaddr, dma_addr_t dma_handle) | |
75 | { | |
76 | iommu_free_coherent(devnode_table(hwdev), size, vaddr, dma_handle); | |
77 | } | |
78 | ||
79 | /* Creates TCEs for a user provided buffer. The user buffer must be | |
80 | * contiguous real kernel storage (not vmalloc). The address of the buffer | |
81 | * passed here is the kernel (virtual) address of the buffer. The buffer | |
82 | * need not be page aligned, the dma_addr_t returned will point to the same | |
83 | * byte within the page as vaddr. | |
84 | */ | |
85 | static dma_addr_t pci_iommu_map_single(struct device *hwdev, void *vaddr, | |
86 | size_t size, enum dma_data_direction direction) | |
87 | { | |
88 | return iommu_map_single(devnode_table(hwdev), vaddr, size, direction); | |
89 | } | |
90 | ||
91 | ||
92 | static void pci_iommu_unmap_single(struct device *hwdev, dma_addr_t dma_handle, | |
93 | size_t size, enum dma_data_direction direction) | |
94 | { | |
95 | iommu_unmap_single(devnode_table(hwdev), dma_handle, size, direction); | |
96 | } | |
97 | ||
98 | ||
99 | static int pci_iommu_map_sg(struct device *pdev, struct scatterlist *sglist, | |
100 | int nelems, enum dma_data_direction direction) | |
101 | { | |
102 | return iommu_map_sg(pdev, devnode_table(pdev), sglist, | |
103 | nelems, direction); | |
104 | } | |
105 | ||
106 | static void pci_iommu_unmap_sg(struct device *pdev, struct scatterlist *sglist, | |
107 | int nelems, enum dma_data_direction direction) | |
108 | { | |
109 | iommu_unmap_sg(devnode_table(pdev), sglist, nelems, direction); | |
110 | } | |
111 | ||
112 | /* We support DMA to/from any memory page via the iommu */ | |
113 | static int pci_iommu_dma_supported(struct device *dev, u64 mask) | |
114 | { | |
115 | return 1; | |
116 | } | |
117 | ||
118 | void pci_iommu_init(void) | |
119 | { | |
120 | pci_dma_ops.alloc_coherent = pci_iommu_alloc_coherent; | |
121 | pci_dma_ops.free_coherent = pci_iommu_free_coherent; | |
122 | pci_dma_ops.map_single = pci_iommu_map_single; | |
123 | pci_dma_ops.unmap_single = pci_iommu_unmap_single; | |
124 | pci_dma_ops.map_sg = pci_iommu_map_sg; | |
125 | pci_dma_ops.unmap_sg = pci_iommu_unmap_sg; | |
126 | pci_dma_ops.dma_supported = pci_iommu_dma_supported; | |
127 | } |