dma-iommu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  3. *
  4. * Provide default implementations of the DMA mapping callbacks for
  5. * busses using the iommu infrastructure
  6. */
  7. #include <asm/iommu.h>
  8. /*
  9. * Generic iommu implementation
  10. */
  11. /* Allocates a contiguous real buffer and creates mappings over it.
  12. * Returns the virtual address of the buffer and sets dma_handle
  13. * to the dma address (mapping) of the first page.
  14. */
  15. static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
  16. dma_addr_t *dma_handle, gfp_t flag)
  17. {
  18. return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
  19. dma_handle, device_to_mask(dev), flag,
  20. dev_to_node(dev));
  21. }
  22. static void dma_iommu_free_coherent(struct device *dev, size_t size,
  23. void *vaddr, dma_addr_t dma_handle)
  24. {
  25. iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
  26. }
  27. /* Creates TCEs for a user provided buffer. The user buffer must be
  28. * contiguous real kernel storage (not vmalloc). The address of the buffer
  29. * passed here is the kernel (virtual) address of the buffer. The buffer
  30. * need not be page aligned, the dma_addr_t returned will point to the same
  31. * byte within the page as vaddr.
  32. */
  33. static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
  34. size_t size,
  35. enum dma_data_direction direction,
  36. struct dma_attrs *attrs)
  37. {
  38. return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
  39. device_to_mask(dev), direction, attrs);
  40. }
  41. static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
  42. size_t size,
  43. enum dma_data_direction direction,
  44. struct dma_attrs *attrs)
  45. {
  46. iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
  47. attrs);
  48. }
  49. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  50. int nelems, enum dma_data_direction direction,
  51. struct dma_attrs *attrs)
  52. {
  53. return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
  54. device_to_mask(dev), direction, attrs);
  55. }
  56. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  57. int nelems, enum dma_data_direction direction,
  58. struct dma_attrs *attrs)
  59. {
  60. iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
  61. attrs);
  62. }
  63. /* We support DMA to/from any memory page via the iommu */
  64. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  65. {
  66. struct iommu_table *tbl = dev->archdata.dma_data;
  67. if (!tbl || tbl->it_offset > mask) {
  68. printk(KERN_INFO
  69. "Warning: IOMMU offset too big for device mask\n");
  70. if (tbl)
  71. printk(KERN_INFO
  72. "mask: 0x%08lx, table offset: 0x%08lx\n",
  73. mask, tbl->it_offset);
  74. else
  75. printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
  76. mask);
  77. return 0;
  78. } else
  79. return 1;
  80. }
  81. struct dma_mapping_ops dma_iommu_ops = {
  82. .alloc_coherent = dma_iommu_alloc_coherent,
  83. .free_coherent = dma_iommu_free_coherent,
  84. .map_single = dma_iommu_map_single,
  85. .unmap_single = dma_iommu_unmap_single,
  86. .map_sg = dma_iommu_map_sg,
  87. .unmap_sg = dma_iommu_unmap_sg,
  88. .dma_supported = dma_iommu_dma_supported,
  89. };
  90. EXPORT_SYMBOL(dma_iommu_ops);