pci_iommu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  3. *
  4. * Rewrite, cleanup, new allocation schemes:
  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.
  13. *
  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.
  18. *
  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. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/string.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-mapping.h>
  31. #include <asm/io.h>
  32. #include <asm/prom.h>
  33. #include <asm/iommu.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/machdep.h>
  36. #include <asm/ppc-pci.h>
  37. /*
  38. * We can use ->sysdata directly and avoid the extra work in
  39. * pci_device_to_OF_node since ->sysdata will have been initialised
  40. * in the iommu init code for all devices.
  41. */
  42. #define PCI_GET_DN(dev) ((struct device_node *)((dev)->sysdata))
  43. static inline struct iommu_table *device_to_table(struct device *hwdev)
  44. {
  45. struct pci_dev *pdev;
  46. if (!hwdev) {
  47. pdev = ppc64_isabridge_dev;
  48. if (!pdev)
  49. return NULL;
  50. } else
  51. pdev = to_pci_dev(hwdev);
  52. return PCI_DN(PCI_GET_DN(pdev))->iommu_table;
  53. }
  54. static inline unsigned long device_to_mask(struct device *hwdev)
  55. {
  56. struct pci_dev *pdev;
  57. if (!hwdev) {
  58. pdev = ppc64_isabridge_dev;
  59. if (!pdev) /* This is the best guess we can do */
  60. return 0xfffffffful;
  61. } else
  62. pdev = to_pci_dev(hwdev);
  63. if (pdev->dma_mask)
  64. return pdev->dma_mask;
  65. /* Assume devices without mask can take 32 bit addresses */
  66. return 0xfffffffful;
  67. }
  68. /* Allocates a contiguous real buffer and creates mappings over it.
  69. * Returns the virtual address of the buffer and sets dma_handle
  70. * to the dma address (mapping) of the first page.
  71. */
  72. static void *pci_iommu_alloc_coherent(struct device *hwdev, size_t size,
  73. dma_addr_t *dma_handle, gfp_t flag)
  74. {
  75. return iommu_alloc_coherent(device_to_table(hwdev), size, dma_handle,
  76. device_to_mask(hwdev), flag,
  77. pcibus_to_node(to_pci_dev(hwdev)->bus));
  78. }
  79. static void pci_iommu_free_coherent(struct device *hwdev, size_t size,
  80. void *vaddr, dma_addr_t dma_handle)
  81. {
  82. iommu_free_coherent(device_to_table(hwdev), size, vaddr, dma_handle);
  83. }
  84. /* Creates TCEs for a user provided buffer. The user buffer must be
  85. * contiguous real kernel storage (not vmalloc). The address of the buffer
  86. * passed here is the kernel (virtual) address of the buffer. The buffer
  87. * need not be page aligned, the dma_addr_t returned will point to the same
  88. * byte within the page as vaddr.
  89. */
  90. static dma_addr_t pci_iommu_map_single(struct device *hwdev, void *vaddr,
  91. size_t size, enum dma_data_direction direction)
  92. {
  93. return iommu_map_single(device_to_table(hwdev), vaddr, size,
  94. device_to_mask(hwdev), direction);
  95. }
  96. static void pci_iommu_unmap_single(struct device *hwdev, dma_addr_t dma_handle,
  97. size_t size, enum dma_data_direction direction)
  98. {
  99. iommu_unmap_single(device_to_table(hwdev), dma_handle, size, direction);
  100. }
  101. static int pci_iommu_map_sg(struct device *pdev, struct scatterlist *sglist,
  102. int nelems, enum dma_data_direction direction)
  103. {
  104. return iommu_map_sg(pdev, device_to_table(pdev), sglist,
  105. nelems, device_to_mask(pdev), direction);
  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(device_to_table(pdev), sglist, nelems, direction);
  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. struct iommu_table *tbl = device_to_table(dev);
  116. if (!tbl || tbl->it_offset > mask) {
  117. printk(KERN_INFO "Warning: IOMMU table offset too big for device mask\n");
  118. if (tbl)
  119. printk(KERN_INFO "mask: 0x%08lx, table offset: 0x%08lx\n",
  120. mask, tbl->it_offset);
  121. else
  122. printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
  123. mask);
  124. return 0;
  125. } else
  126. return 1;
  127. }
  128. struct dma_mapping_ops pci_iommu_ops = {
  129. .alloc_coherent = pci_iommu_alloc_coherent,
  130. .free_coherent = pci_iommu_free_coherent,
  131. .map_single = pci_iommu_map_single,
  132. .unmap_single = pci_iommu_unmap_single,
  133. .map_sg = pci_iommu_map_sg,
  134. .unmap_sg = pci_iommu_unmap_sg,
  135. .dma_supported = pci_iommu_dma_supported,
  136. };
  137. void pci_iommu_init(void)
  138. {
  139. pci_dma_ops = pci_iommu_ops;
  140. }