memalloc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. * Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Generic memory allocators
  7. */
  8. #ifndef __SOUND_MEMALLOC_H
  9. #define __SOUND_MEMALLOC_H
  10. #include <linux/dma-direction.h>
  11. #include <asm/page.h>
  12. struct device;
  13. struct vm_area_struct;
  14. struct sg_table;
  15. /*
  16. * buffer device info
  17. */
  18. struct snd_dma_device {
  19. int type; /* SNDRV_DMA_TYPE_XXX */
  20. enum dma_data_direction dir; /* DMA direction */
  21. bool need_sync; /* explicit sync needed? */
  22. struct device *dev; /* generic device */
  23. };
  24. /*
  25. * buffer types
  26. */
  27. #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */
  28. #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */
  29. #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */
  30. #define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */
  31. #ifdef CONFIG_GENERIC_ALLOCATOR
  32. #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */
  33. #else
  34. #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV
  35. #endif
  36. #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */
  37. #define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */
  38. #define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */
  39. #ifdef CONFIG_SND_DMA_SGBUF
  40. #define SNDRV_DMA_TYPE_DEV_SG 3 /* S/G pages */
  41. #define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */
  42. #else
  43. #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */
  44. #define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC
  45. #endif
  46. /*
  47. * info for buffer allocation
  48. */
  49. struct snd_dma_buffer {
  50. struct snd_dma_device dev; /* device type */
  51. unsigned char *area; /* virtual pointer */
  52. dma_addr_t addr; /* physical address */
  53. size_t bytes; /* buffer size in bytes */
  54. void *private_data; /* private for allocator; don't touch */
  55. };
  56. /*
  57. * return the pages matching with the given byte size
  58. */
  59. static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
  60. {
  61. return (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  62. }
  63. /* allocate/release a buffer */
  64. int snd_dma_alloc_dir_pages(int type, struct device *dev,
  65. enum dma_data_direction dir, size_t size,
  66. struct snd_dma_buffer *dmab);
  67. static inline int snd_dma_alloc_pages(int type, struct device *dev,
  68. size_t size, struct snd_dma_buffer *dmab)
  69. {
  70. return snd_dma_alloc_dir_pages(type, dev, DMA_BIDIRECTIONAL, size, dmab);
  71. }
  72. int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,
  73. struct snd_dma_buffer *dmab);
  74. void snd_dma_free_pages(struct snd_dma_buffer *dmab);
  75. int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
  76. struct vm_area_struct *area);
  77. enum snd_dma_sync_mode { SNDRV_DMA_SYNC_CPU, SNDRV_DMA_SYNC_DEVICE };
  78. #ifdef CONFIG_HAS_DMA
  79. void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
  80. enum snd_dma_sync_mode mode);
  81. #else
  82. static inline void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
  83. enum snd_dma_sync_mode mode) {}
  84. #endif
  85. dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset);
  86. struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset);
  87. unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
  88. unsigned int ofs, unsigned int size);
  89. /* device-managed memory allocator */
  90. struct snd_dma_buffer *snd_devm_alloc_dir_pages(struct device *dev, int type,
  91. enum dma_data_direction dir,
  92. size_t size);
  93. static inline struct snd_dma_buffer *
  94. snd_devm_alloc_pages(struct device *dev, int type, size_t size)
  95. {
  96. return snd_devm_alloc_dir_pages(dev, type, DMA_BIDIRECTIONAL, size);
  97. }
  98. static inline struct sg_table *
  99. snd_dma_noncontig_sg_table(struct snd_dma_buffer *dmab)
  100. {
  101. return dmab->private_data;
  102. }
  103. #endif /* __SOUND_MEMALLOC_H */