dma-iommu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <linux/export.h>
  8. #include <asm/iommu.h>
  9. /*
  10. * Generic iommu implementation
  11. */
  12. /* Allocates a contiguous real buffer and creates mappings over it.
  13. * Returns the virtual address of the buffer and sets dma_handle
  14. * to the dma address (mapping) of the first page.
  15. */
  16. static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
  17. dma_addr_t *dma_handle, gfp_t flag,
  18. struct dma_attrs *attrs)
  19. {
  20. return iommu_alloc_coherent(dev, get_iommu_table_base(dev), size,
  21. dma_handle, dev->coherent_dma_mask, flag,
  22. dev_to_node(dev));
  23. }
  24. static void dma_iommu_free_coherent(struct device *dev, size_t size,
  25. void *vaddr, dma_addr_t dma_handle,
  26. struct dma_attrs *attrs)
  27. {
  28. iommu_free_coherent(get_iommu_table_base(dev), size, vaddr, dma_handle);
  29. }
  30. /* Creates TCEs for a user provided buffer. The user buffer must be
  31. * contiguous real kernel storage (not vmalloc). The address passed here
  32. * comprises a page address and offset into that page. The dma_addr_t
  33. * returned will point to the same byte within the page as was passed in.
  34. */
  35. static dma_addr_t dma_iommu_map_page(struct device *dev, struct page *page,
  36. unsigned long offset, size_t size,
  37. enum dma_data_direction direction,
  38. struct dma_attrs *attrs)
  39. {
  40. return iommu_map_page(dev, get_iommu_table_base(dev), page, offset,
  41. size, device_to_mask(dev), direction, attrs);
  42. }
  43. static void dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
  44. size_t size, enum dma_data_direction direction,
  45. struct dma_attrs *attrs)
  46. {
  47. iommu_unmap_page(get_iommu_table_base(dev), dma_handle, size, direction,
  48. attrs);
  49. }
  50. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  51. int nelems, enum dma_data_direction direction,
  52. struct dma_attrs *attrs)
  53. {
  54. return ppc_iommu_map_sg(dev, get_iommu_table_base(dev), sglist, nelems,
  55. device_to_mask(dev), direction, attrs);
  56. }
  57. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  58. int nelems, enum dma_data_direction direction,
  59. struct dma_attrs *attrs)
  60. {
  61. ppc_iommu_unmap_sg(get_iommu_table_base(dev), sglist, nelems,
  62. direction, attrs);
  63. }
  64. /* We support DMA to/from any memory page via the iommu */
  65. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  66. {
  67. struct iommu_table *tbl = get_iommu_table_base(dev);
  68. if (!tbl) {
  69. dev_info(dev, "Warning: IOMMU dma not supported: mask 0x%08llx"
  70. ", table unavailable\n", mask);
  71. return 0;
  72. }
  73. if (tbl->it_offset > (mask >> tbl->it_page_shift)) {
  74. dev_info(dev, "Warning: IOMMU offset too big for device mask\n");
  75. dev_info(dev, "mask: 0x%08llx, table offset: 0x%08lx\n",
  76. mask, tbl->it_offset << tbl->it_page_shift);
  77. return 0;
  78. } else
  79. return 1;
  80. }
  81. static u64 dma_iommu_get_required_mask(struct device *dev)
  82. {
  83. struct iommu_table *tbl = get_iommu_table_base(dev);
  84. u64 mask;
  85. if (!tbl)
  86. return 0;
  87. mask = 1ULL < (fls_long(tbl->it_offset + tbl->it_size) - 1);
  88. mask += mask - 1;
  89. return mask;
  90. }
  91. struct dma_map_ops dma_iommu_ops = {
  92. .alloc = dma_iommu_alloc_coherent,
  93. .free = dma_iommu_free_coherent,
  94. .mmap = dma_direct_mmap_coherent,
  95. .map_sg = dma_iommu_map_sg,
  96. .unmap_sg = dma_iommu_unmap_sg,
  97. .dma_supported = dma_iommu_dma_supported,
  98. .map_page = dma_iommu_map_page,
  99. .unmap_page = dma_iommu_unmap_page,
  100. .get_required_mask = dma_iommu_get_required_mask,
  101. };
  102. EXPORT_SYMBOL(dma_iommu_ops);