buffer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_KMALLOC_MINALIGN.
  33. */
  34. if (ARCH_KMALLOC_MINALIGN <= 32)
  35. ; /* Original value is okay */
  36. else if (ARCH_KMALLOC_MINALIGN <= 64)
  37. pool_max[0] = 64;
  38. else if (ARCH_KMALLOC_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. * Context: !in_interrupt()
  48. *
  49. * Call this as part of initializing a host controller that uses the dma
  50. * memory allocators. It initializes some pools of dma-coherent memory that
  51. * will be shared by all drivers using that controller.
  52. *
  53. * Call hcd_buffer_destroy() to clean up after using those pools.
  54. *
  55. * Return: 0 if successful. A negative errno value otherwise.
  56. */
  57. int hcd_buffer_create(struct usb_hcd *hcd)
  58. {
  59. char name[16];
  60. int i, size;
  61. if (hcd->localmem_pool || !hcd_uses_dma(hcd))
  62. return 0;
  63. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  64. size = pool_max[i];
  65. if (!size)
  66. continue;
  67. snprintf(name, sizeof(name), "buffer-%d", size);
  68. hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
  69. size, size, 0);
  70. if (!hcd->pool[i]) {
  71. hcd_buffer_destroy(hcd);
  72. return -ENOMEM;
  73. }
  74. }
  75. return 0;
  76. }
  77. /**
  78. * hcd_buffer_destroy - deallocate buffer pools
  79. * @hcd: the bus whose buffer pools are to be destroyed
  80. * Context: !in_interrupt()
  81. *
  82. * This frees the buffer pools created by hcd_buffer_create().
  83. */
  84. void hcd_buffer_destroy(struct usb_hcd *hcd)
  85. {
  86. int i;
  87. if (!IS_ENABLED(CONFIG_HAS_DMA))
  88. return;
  89. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  90. dma_pool_destroy(hcd->pool[i]);
  91. hcd->pool[i] = NULL;
  92. }
  93. }
  94. /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  95. * better sharing and to leverage mm/slab.c intelligence.
  96. */
  97. void *hcd_buffer_alloc(
  98. struct usb_bus *bus,
  99. size_t size,
  100. gfp_t mem_flags,
  101. dma_addr_t *dma
  102. )
  103. {
  104. struct usb_hcd *hcd = bus_to_hcd(bus);
  105. int i;
  106. if (size == 0)
  107. return NULL;
  108. if (hcd->localmem_pool)
  109. return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
  110. /* some USB hosts just use PIO */
  111. if (!hcd_uses_dma(hcd)) {
  112. *dma = ~(dma_addr_t) 0;
  113. return kmalloc(size, mem_flags);
  114. }
  115. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  116. if (size <= pool_max[i])
  117. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  118. }
  119. return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
  120. }
  121. void hcd_buffer_free(
  122. struct usb_bus *bus,
  123. size_t size,
  124. void *addr,
  125. dma_addr_t dma
  126. )
  127. {
  128. struct usb_hcd *hcd = bus_to_hcd(bus);
  129. int i;
  130. if (!addr)
  131. return;
  132. if (hcd->localmem_pool) {
  133. gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
  134. return;
  135. }
  136. if (!hcd_uses_dma(hcd)) {
  137. kfree(addr);
  138. return;
  139. }
  140. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  141. if (size <= pool_max[i]) {
  142. dma_pool_free(hcd->pool[i], addr, dma);
  143. return;
  144. }
  145. }
  146. dma_free_coherent(hcd->self.sysdev, size, addr, dma);
  147. }