pci_iommu.c 4.0 KB

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