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