dma-mapping.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Implements the generic device dma API for microblaze and the pci
  3. *
  4. * Copyright (C) 2009-2010 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2009-2010 PetaLogix
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * This file is base on powerpc and x86 dma-mapping.h versions
  12. * Copyright (C) 2004 IBM
  13. */
  14. #ifndef _ASM_MICROBLAZE_DMA_MAPPING_H
  15. #define _ASM_MICROBLAZE_DMA_MAPPING_H
  16. /*
  17. * See Documentation/DMA-API-HOWTO.txt and
  18. * Documentation/DMA-API.txt for documentation.
  19. */
  20. #include <linux/types.h>
  21. #include <linux/cache.h>
  22. #include <linux/mm.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/dma-debug.h>
  25. #include <linux/dma-attrs.h>
  26. #include <asm/io.h>
  27. #include <asm-generic/dma-coherent.h>
  28. #include <asm/cacheflush.h>
  29. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  30. #define __dma_alloc_coherent(dev, gfp, size, handle) NULL
  31. #define __dma_free_coherent(size, addr) ((void)0)
  32. /*
  33. * Available generic sets of operations
  34. */
  35. extern struct dma_map_ops dma_direct_ops;
  36. static inline struct dma_map_ops *get_dma_ops(struct device *dev)
  37. {
  38. return &dma_direct_ops;
  39. }
  40. static inline int dma_supported(struct device *dev, u64 mask)
  41. {
  42. struct dma_map_ops *ops = get_dma_ops(dev);
  43. if (unlikely(!ops))
  44. return 0;
  45. if (!ops->dma_supported)
  46. return 1;
  47. return ops->dma_supported(dev, mask);
  48. }
  49. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  50. {
  51. struct dma_map_ops *ops = get_dma_ops(dev);
  52. if (unlikely(ops == NULL))
  53. return -EIO;
  54. if (ops->set_dma_mask)
  55. return ops->set_dma_mask(dev, dma_mask);
  56. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  57. return -EIO;
  58. *dev->dma_mask = dma_mask;
  59. return 0;
  60. }
  61. #include <asm-generic/dma-mapping-common.h>
  62. static inline void __dma_sync(unsigned long paddr,
  63. size_t size, enum dma_data_direction direction)
  64. {
  65. switch (direction) {
  66. case DMA_TO_DEVICE:
  67. case DMA_BIDIRECTIONAL:
  68. flush_dcache_range(paddr, paddr + size);
  69. break;
  70. case DMA_FROM_DEVICE:
  71. invalidate_dcache_range(paddr, paddr + size);
  72. break;
  73. default:
  74. BUG();
  75. }
  76. }
  77. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  78. {
  79. struct dma_map_ops *ops = get_dma_ops(dev);
  80. debug_dma_mapping_error(dev, dma_addr);
  81. if (ops->mapping_error)
  82. return ops->mapping_error(dev, dma_addr);
  83. return (dma_addr == DMA_ERROR_CODE);
  84. }
  85. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  86. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  87. #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
  88. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  89. dma_addr_t *dma_handle, gfp_t flag,
  90. struct dma_attrs *attrs)
  91. {
  92. struct dma_map_ops *ops = get_dma_ops(dev);
  93. void *memory;
  94. BUG_ON(!ops);
  95. memory = ops->alloc(dev, size, dma_handle, flag, attrs);
  96. debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
  97. return memory;
  98. }
  99. #define dma_free_coherent(d,s,c,h) dma_free_attrs(d, s, c, h, NULL)
  100. static inline void dma_free_attrs(struct device *dev, size_t size,
  101. void *cpu_addr, dma_addr_t dma_handle,
  102. struct dma_attrs *attrs)
  103. {
  104. struct dma_map_ops *ops = get_dma_ops(dev);
  105. BUG_ON(!ops);
  106. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  107. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  108. }
  109. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  110. enum dma_data_direction direction)
  111. {
  112. BUG_ON(direction == DMA_NONE);
  113. __dma_sync(virt_to_phys(vaddr), size, (int)direction);
  114. }
  115. #endif /* _ASM_MICROBLAZE_DMA_MAPPING_H */