dma-mapping.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/PCI/PCI-DMA-mapping.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. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  29. #define __dma_alloc_coherent(dev, gfp, size, handle) NULL
  30. #define __dma_free_coherent(size, addr) ((void)0)
  31. #define __dma_sync(addr, size, rw) ((void)0)
  32. static inline unsigned long device_to_mask(struct device *dev)
  33. {
  34. if (dev->dma_mask && *dev->dma_mask)
  35. return *dev->dma_mask;
  36. /* Assume devices without mask can take 32 bit addresses */
  37. return 0xfffffffful;
  38. }
  39. extern struct dma_map_ops *dma_ops;
  40. /*
  41. * Available generic sets of operations
  42. */
  43. extern struct dma_map_ops dma_direct_ops;
  44. static inline struct dma_map_ops *get_dma_ops(struct device *dev)
  45. {
  46. /* We don't handle the NULL dev case for ISA for now. We could
  47. * do it via an out of line call but it is not needed for now. The
  48. * only ISA DMA device we support is the floppy and we have a hack
  49. * in the floppy driver directly to get a device for us.
  50. */
  51. if (unlikely(!dev) || !dev->archdata.dma_ops)
  52. return NULL;
  53. return dev->archdata.dma_ops;
  54. }
  55. static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops)
  56. {
  57. dev->archdata.dma_ops = ops;
  58. }
  59. static inline int dma_supported(struct device *dev, u64 mask)
  60. {
  61. struct dma_map_ops *ops = get_dma_ops(dev);
  62. if (unlikely(!ops))
  63. return 0;
  64. if (!ops->dma_supported)
  65. return 1;
  66. return ops->dma_supported(dev, mask);
  67. }
  68. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  69. {
  70. struct dma_map_ops *ops = get_dma_ops(dev);
  71. if (unlikely(ops == NULL))
  72. return -EIO;
  73. if (ops->set_dma_mask)
  74. return ops->set_dma_mask(dev, dma_mask);
  75. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  76. return -EIO;
  77. *dev->dma_mask = dma_mask;
  78. return 0;
  79. }
  80. #include <asm-generic/dma-mapping-common.h>
  81. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  82. {
  83. struct dma_map_ops *ops = get_dma_ops(dev);
  84. if (ops->mapping_error)
  85. return ops->mapping_error(dev, dma_addr);
  86. return (dma_addr == DMA_ERROR_CODE);
  87. }
  88. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  89. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  90. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  91. dma_addr_t *dma_handle, gfp_t flag)
  92. {
  93. struct dma_map_ops *ops = get_dma_ops(dev);
  94. void *memory;
  95. BUG_ON(!ops);
  96. memory = ops->alloc_coherent(dev, size, dma_handle, flag);
  97. debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
  98. return memory;
  99. }
  100. static inline void dma_free_coherent(struct device *dev, size_t size,
  101. void *cpu_addr, dma_addr_t dma_handle)
  102. {
  103. struct dma_map_ops *ops = get_dma_ops(dev);
  104. BUG_ON(!ops);
  105. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  106. ops->free_coherent(dev, size, cpu_addr, dma_handle);
  107. }
  108. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  109. enum dma_data_direction direction)
  110. {
  111. BUG_ON(direction == DMA_NONE);
  112. __dma_sync(vaddr, size, (int)direction);
  113. }
  114. #endif /* _ASM_MICROBLAZE_DMA_MAPPING_H */