dma-iommu.c 3.5 KB

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