buffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMA memory management for framework level HCD code (hc_driver)
  4. *
  5. * This implementation plugs in through generic "usb_bus" level methods,
  6. * and should work with all USB controllers, regardless of bus type.
  7. *
  8. * Released under the GPLv2 only.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/device.h>
  14. #include <linux/mm.h>
  15. #include <linux/io.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/dmapool.h>
  18. #include <linux/genalloc.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/hcd.h>
  21. /*
  22. * DMA-Coherent Buffers
  23. */
  24. /* FIXME tune these based on pool statistics ... */
  25. static size_t pool_max[HCD_BUFFER_POOLS] = {
  26. 32, 128, 512, 2048,
  27. };
  28. void __init usb_init_pool_max(void)
  29. {
  30. /*
  31. * The pool_max values must never be smaller than
  32. * ARCH_DMA_MINALIGN.
  33. */
  34. if (ARCH_DMA_MINALIGN <= 32)
  35. ; /* Original value is okay */
  36. else if (ARCH_DMA_MINALIGN <= 64)
  37. pool_max[0] = 64;
  38. else if (ARCH_DMA_MINALIGN <= 128)
  39. pool_max[0] = 0; /* Don't use this pool */
  40. else
  41. BUILD_BUG(); /* We don't allow this */
  42. }
  43. /* SETUP primitives */
  44. /**
  45. * hcd_buffer_create - initialize buffer pools
  46. * @hcd: the bus whose buffer pools are to be initialized
  47. *
  48. * Context: task context, might sleep
  49. *
  50. * Call this as part of initializing a host controller that uses the dma
  51. * memory allocators. It initializes some pools of dma-coherent memory that
  52. * will be shared by all drivers using that controller.
  53. *
  54. * Call hcd_buffer_destroy() to clean up after using those pools.
  55. *
  56. * Return: 0 if successful. A negative errno value otherwise.
  57. */
  58. int hcd_buffer_create(struct usb_hcd *hcd)
  59. {
  60. char name[16];
  61. int i, size;
  62. if (hcd->localmem_pool || !hcd_uses_dma(hcd))
  63. return 0;
  64. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  65. size = pool_max[i];
  66. if (!size)
  67. continue;
  68. snprintf(name, sizeof(name), "buffer-%d", size);
  69. hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
  70. size, size, 0);
  71. if (!hcd->pool[i]) {
  72. hcd_buffer_destroy(hcd);
  73. return -ENOMEM;
  74. }
  75. }
  76. return 0;
  77. }
  78. /**
  79. * hcd_buffer_destroy - deallocate buffer pools
  80. * @hcd: the bus whose buffer pools are to be destroyed
  81. *
  82. * Context: task context, might sleep
  83. *
  84. * This frees the buffer pools created by hcd_buffer_create().
  85. */
  86. void hcd_buffer_destroy(struct usb_hcd *hcd)
  87. {
  88. int i;
  89. if (!IS_ENABLED(CONFIG_HAS_DMA))
  90. return;
  91. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  92. dma_pool_destroy(hcd->pool[i]);
  93. hcd->pool[i] = NULL;
  94. }
  95. }
  96. /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  97. * better sharing and to leverage mm/slab.c intelligence.
  98. */
  99. void *hcd_buffer_alloc(
  100. struct usb_bus *bus,
  101. size_t size,
  102. gfp_t mem_flags,
  103. dma_addr_t *dma
  104. )
  105. {
  106. struct usb_hcd *hcd = bus_to_hcd(bus);
  107. int i;
  108. if (size == 0)
  109. return NULL;
  110. if (hcd->localmem_pool)
  111. return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
  112. /* some USB hosts just use PIO */
  113. if (!hcd_uses_dma(hcd)) {
  114. *dma = ~(dma_addr_t) 0;
  115. return kmalloc(size, mem_flags);
  116. }
  117. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  118. if (size <= pool_max[i])
  119. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  120. }
  121. return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
  122. }
  123. void hcd_buffer_free(
  124. struct usb_bus *bus,
  125. size_t size,
  126. void *addr,
  127. dma_addr_t dma
  128. )
  129. {
  130. struct usb_hcd *hcd = bus_to_hcd(bus);
  131. int i;
  132. if (!addr)
  133. return;
  134. if (hcd->localmem_pool) {
  135. gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
  136. return;
  137. }
  138. if (!hcd_uses_dma(hcd)) {
  139. kfree(addr);
  140. return;
  141. }
  142. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  143. if (size <= pool_max[i]) {
  144. dma_pool_free(hcd->pool[i], addr, dma);
  145. return;
  146. }
  147. }
  148. dma_free_coherent(hcd->self.sysdev, size, addr, dma);
  149. }
  150. void *hcd_buffer_alloc_pages(struct usb_hcd *hcd,
  151. size_t size, gfp_t mem_flags, dma_addr_t *dma)
  152. {
  153. if (size == 0)
  154. return NULL;
  155. if (hcd->localmem_pool)
  156. return gen_pool_dma_alloc_align(hcd->localmem_pool,
  157. size, dma, PAGE_SIZE);
  158. /* some USB hosts just use PIO */
  159. if (!hcd_uses_dma(hcd)) {
  160. *dma = DMA_MAPPING_ERROR;
  161. return (void *)__get_free_pages(mem_flags,
  162. get_order(size));
  163. }
  164. return dma_alloc_coherent(hcd->self.sysdev,
  165. size, dma, mem_flags);
  166. }
  167. void hcd_buffer_free_pages(struct usb_hcd *hcd,
  168. size_t size, void *addr, dma_addr_t dma)
  169. {
  170. if (!addr)
  171. return;
  172. if (hcd->localmem_pool) {
  173. gen_pool_free(hcd->localmem_pool,
  174. (unsigned long)addr, size);
  175. return;
  176. }
  177. if (!hcd_uses_dma(hcd)) {
  178. free_pages((unsigned long)addr, get_order(size));
  179. return;
  180. }
  181. dma_free_coherent(hcd->self.sysdev, size, addr, dma);
  182. }