dma-iommu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 passed here
  29. * comprises a page address and offset into that page. The dma_addr_t
  30. * returned will point to the same byte within the page as was passed in.
  31. */
  32. static dma_addr_t dma_iommu_map_page(struct device *dev, struct page *page,
  33. unsigned long offset, size_t size,
  34. enum dma_data_direction direction,
  35. struct dma_attrs *attrs)
  36. {
  37. return iommu_map_page(dev, dev->archdata.dma_data, page, offset, size,
  38. device_to_mask(dev), direction, attrs);
  39. }
  40. static void dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
  41. size_t size, enum dma_data_direction direction,
  42. struct dma_attrs *attrs)
  43. {
  44. iommu_unmap_page(dev->archdata.dma_data, dma_handle, size, direction,
  45. attrs);
  46. }
  47. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  48. int nelems, enum dma_data_direction direction,
  49. struct dma_attrs *attrs)
  50. {
  51. return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
  52. device_to_mask(dev), direction, attrs);
  53. }
  54. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  55. int nelems, enum dma_data_direction direction,
  56. struct dma_attrs *attrs)
  57. {
  58. iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
  59. attrs);
  60. }
  61. /* We support DMA to/from any memory page via the iommu */
  62. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  63. {
  64. struct iommu_table *tbl = dev->archdata.dma_data;
  65. if (!tbl || tbl->it_offset > mask) {
  66. printk(KERN_INFO
  67. "Warning: IOMMU offset too big for device mask\n");
  68. if (tbl)
  69. printk(KERN_INFO
  70. "mask: 0x%08llx, table offset: 0x%08lx\n",
  71. mask, tbl->it_offset);
  72. else
  73. printk(KERN_INFO "mask: 0x%08llx, table unavailable\n",
  74. mask);
  75. return 0;
  76. } else
  77. return 1;
  78. }
  79. struct dma_mapping_ops dma_iommu_ops = {
  80. .alloc_coherent = dma_iommu_alloc_coherent,
  81. .free_coherent = dma_iommu_free_coherent,
  82. .map_sg = dma_iommu_map_sg,
  83. .unmap_sg = dma_iommu_unmap_sg,
  84. .dma_supported = dma_iommu_dma_supported,
  85. .map_page = dma_iommu_map_page,
  86. .unmap_page = dma_iommu_unmap_page,
  87. };
  88. EXPORT_SYMBOL(dma_iommu_ops);