dma_64.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  3. *
  4. * Provide default implementations of the DMA mapping callbacks for
  5. * directly mapped busses and busses using the iommu infrastructure
  6. */
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <asm/bug.h>
  10. #include <asm/iommu.h>
  11. #include <asm/abs_addr.h>
  12. /*
  13. * Generic iommu implementation
  14. */
  15. static inline unsigned long device_to_mask(struct device *dev)
  16. {
  17. if (dev->dma_mask && *dev->dma_mask)
  18. return *dev->dma_mask;
  19. /* Assume devices without mask can take 32 bit addresses */
  20. return 0xfffffffful;
  21. }
  22. /* Allocates a contiguous real buffer and creates mappings over it.
  23. * Returns the virtual address of the buffer and sets dma_handle
  24. * to the dma address (mapping) of the first page.
  25. */
  26. static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
  27. dma_addr_t *dma_handle, gfp_t flag)
  28. {
  29. return iommu_alloc_coherent(dev->archdata.dma_data, size, dma_handle,
  30. device_to_mask(dev), flag,
  31. dev->archdata.numa_node);
  32. }
  33. static void dma_iommu_free_coherent(struct device *dev, size_t size,
  34. void *vaddr, dma_addr_t dma_handle)
  35. {
  36. iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
  37. }
  38. /* Creates TCEs for a user provided buffer. The user buffer must be
  39. * contiguous real kernel storage (not vmalloc). The address of the buffer
  40. * passed here is the kernel (virtual) address of the buffer. The buffer
  41. * need not be page aligned, the dma_addr_t returned will point to the same
  42. * byte within the page as vaddr.
  43. */
  44. static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
  45. size_t size,
  46. enum dma_data_direction direction)
  47. {
  48. return iommu_map_single(dev->archdata.dma_data, vaddr, size,
  49. device_to_mask(dev), direction);
  50. }
  51. static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
  52. size_t size,
  53. enum dma_data_direction direction)
  54. {
  55. iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction);
  56. }
  57. static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  58. int nelems, enum dma_data_direction direction)
  59. {
  60. return iommu_map_sg(dev->archdata.dma_data, sglist, nelems,
  61. device_to_mask(dev), direction);
  62. }
  63. static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
  64. int nelems, enum dma_data_direction direction)
  65. {
  66. iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction);
  67. }
  68. /* We support DMA to/from any memory page via the iommu */
  69. static int dma_iommu_dma_supported(struct device *dev, u64 mask)
  70. {
  71. struct iommu_table *tbl = dev->archdata.dma_data;
  72. if (!tbl || tbl->it_offset > mask) {
  73. printk(KERN_INFO
  74. "Warning: IOMMU offset too big for device mask\n");
  75. if (tbl)
  76. printk(KERN_INFO
  77. "mask: 0x%08lx, table offset: 0x%08lx\n",
  78. mask, tbl->it_offset);
  79. else
  80. printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
  81. mask);
  82. return 0;
  83. } else
  84. return 1;
  85. }
  86. struct dma_mapping_ops dma_iommu_ops = {
  87. .alloc_coherent = dma_iommu_alloc_coherent,
  88. .free_coherent = dma_iommu_free_coherent,
  89. .map_single = dma_iommu_map_single,
  90. .unmap_single = dma_iommu_unmap_single,
  91. .map_sg = dma_iommu_map_sg,
  92. .unmap_sg = dma_iommu_unmap_sg,
  93. .dma_supported = dma_iommu_dma_supported,
  94. };
  95. EXPORT_SYMBOL(dma_iommu_ops);
  96. /*
  97. * Generic direct DMA implementation
  98. *
  99. * This implementation supports a global offset that can be applied if
  100. * the address at which memory is visible to devices is not 0.
  101. */
  102. unsigned long dma_direct_offset;
  103. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  104. dma_addr_t *dma_handle, gfp_t flag)
  105. {
  106. struct page *page;
  107. void *ret;
  108. int node = dev->archdata.numa_node;
  109. /* TODO: Maybe use the numa node here too ? */
  110. page = alloc_pages_node(node, flag, get_order(size));
  111. if (page == NULL)
  112. return NULL;
  113. ret = page_address(page);
  114. memset(ret, 0, size);
  115. *dma_handle = virt_to_abs(ret) | dma_direct_offset;
  116. return ret;
  117. }
  118. static void dma_direct_free_coherent(struct device *dev, size_t size,
  119. void *vaddr, dma_addr_t dma_handle)
  120. {
  121. free_pages((unsigned long)vaddr, get_order(size));
  122. }
  123. static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
  124. size_t size,
  125. enum dma_data_direction direction)
  126. {
  127. return virt_to_abs(ptr) | dma_direct_offset;
  128. }
  129. static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
  130. size_t size,
  131. enum dma_data_direction direction)
  132. {
  133. }
  134. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sg,
  135. int nents, enum dma_data_direction direction)
  136. {
  137. int i;
  138. for (i = 0; i < nents; i++, sg++) {
  139. sg->dma_address = (page_to_phys(sg->page) + sg->offset) |
  140. dma_direct_offset;
  141. sg->dma_length = sg->length;
  142. }
  143. return nents;
  144. }
  145. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  146. int nents, enum dma_data_direction direction)
  147. {
  148. }
  149. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  150. {
  151. /* Could be improved to check for memory though it better be
  152. * done via some global so platforms can set the limit in case
  153. * they have limited DMA windows
  154. */
  155. return mask >= DMA_32BIT_MASK;
  156. }
  157. struct dma_mapping_ops dma_direct_ops = {
  158. .alloc_coherent = dma_direct_alloc_coherent,
  159. .free_coherent = dma_direct_free_coherent,
  160. .map_single = dma_direct_map_single,
  161. .unmap_single = dma_direct_unmap_single,
  162. .map_sg = dma_direct_map_sg,
  163. .unmap_sg = dma_direct_unmap_sg,
  164. .dma_supported = dma_direct_dma_supported,
  165. };
  166. EXPORT_SYMBOL(dma_direct_ops);