buffer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * DMA memory management for framework level HCD code (hc_driver)
  3. *
  4. * This implementation plugs in through generic "usb_bus" level methods,
  5. * and should work with all USB controllers, regardless of bus type.
  6. *
  7. * Released under the GPLv2 only.
  8. * SPDX-License-Identifier: GPL-2.0
  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/usb.h>
  19. #include <linux/usb/hcd.h>
  20. /*
  21. * DMA-Coherent Buffers
  22. */
  23. /* FIXME tune these based on pool statistics ... */
  24. static size_t pool_max[HCD_BUFFER_POOLS] = {
  25. 32, 128, 512, 2048,
  26. };
  27. void __init usb_init_pool_max(void)
  28. {
  29. /*
  30. * The pool_max values must never be smaller than
  31. * ARCH_KMALLOC_MINALIGN.
  32. */
  33. if (ARCH_KMALLOC_MINALIGN <= 32)
  34. ; /* Original value is okay */
  35. else if (ARCH_KMALLOC_MINALIGN <= 64)
  36. pool_max[0] = 64;
  37. else if (ARCH_KMALLOC_MINALIGN <= 128)
  38. pool_max[0] = 0; /* Don't use this pool */
  39. else
  40. BUILD_BUG(); /* We don't allow this */
  41. }
  42. /* SETUP primitives */
  43. /**
  44. * hcd_buffer_create - initialize buffer pools
  45. * @hcd: the bus whose buffer pools are to be initialized
  46. * Context: !in_interrupt()
  47. *
  48. * Call this as part of initializing a host controller that uses the dma
  49. * memory allocators. It initializes some pools of dma-coherent memory that
  50. * will be shared by all drivers using that controller.
  51. *
  52. * Call hcd_buffer_destroy() to clean up after using those pools.
  53. *
  54. * Return: 0 if successful. A negative errno value otherwise.
  55. */
  56. int hcd_buffer_create(struct usb_hcd *hcd)
  57. {
  58. char name[16];
  59. int i, size;
  60. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  61. (!hcd->self.controller->dma_mask &&
  62. !(hcd->driver->flags & HCD_LOCAL_MEM)))
  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.controller,
  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. * Context: !in_interrupt()
  82. *
  83. * This frees the buffer pools created by hcd_buffer_create().
  84. */
  85. void hcd_buffer_destroy(struct usb_hcd *hcd)
  86. {
  87. int i;
  88. if (!IS_ENABLED(CONFIG_HAS_DMA))
  89. return;
  90. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  91. struct dma_pool *pool = hcd->pool[i];
  92. if (pool) {
  93. dma_pool_destroy(pool);
  94. hcd->pool[i] = NULL;
  95. }
  96. }
  97. }
  98. /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  99. * better sharing and to leverage mm/slab.c intelligence.
  100. */
  101. void *hcd_buffer_alloc(
  102. struct usb_bus *bus,
  103. size_t size,
  104. gfp_t mem_flags,
  105. dma_addr_t *dma
  106. )
  107. {
  108. struct usb_hcd *hcd = bus_to_hcd(bus);
  109. int i;
  110. if (size == 0)
  111. return NULL;
  112. /* some USB hosts just use PIO */
  113. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  114. (!bus->controller->dma_mask &&
  115. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  116. *dma = ~(dma_addr_t) 0;
  117. return kmalloc(size, mem_flags);
  118. }
  119. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  120. if (size <= pool_max[i])
  121. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  122. }
  123. return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
  124. }
  125. void hcd_buffer_free(
  126. struct usb_bus *bus,
  127. size_t size,
  128. void *addr,
  129. dma_addr_t dma
  130. )
  131. {
  132. struct usb_hcd *hcd = bus_to_hcd(bus);
  133. int i;
  134. if (!addr)
  135. return;
  136. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  137. (!bus->controller->dma_mask &&
  138. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  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.controller, size, addr, dma);
  149. }