dma-iommu.c 3.7 KB

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