buffer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/device.h>
  11. #include <linux/mm.h>
  12. #include <linux/io.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/hcd.h>
  17. /*
  18. * DMA-Coherent Buffers
  19. */
  20. /* FIXME tune these based on pool statistics ... */
  21. static size_t pool_max[HCD_BUFFER_POOLS] = {
  22. 32, 128, 512, 2048,
  23. };
  24. void __init usb_init_pool_max(void)
  25. {
  26. /*
  27. * The pool_max values must never be smaller than
  28. * ARCH_KMALLOC_MINALIGN.
  29. */
  30. if (ARCH_KMALLOC_MINALIGN <= 32)
  31. ; /* Original value is okay */
  32. else if (ARCH_KMALLOC_MINALIGN <= 64)
  33. pool_max[0] = 64;
  34. else if (ARCH_KMALLOC_MINALIGN <= 128)
  35. pool_max[0] = 0; /* Don't use this pool */
  36. else
  37. BUILD_BUG(); /* We don't allow this */
  38. }
  39. /* SETUP primitives */
  40. /**
  41. * hcd_buffer_create - initialize buffer pools
  42. * @hcd: the bus whose buffer pools are to be initialized
  43. * Context: !in_interrupt()
  44. *
  45. * Call this as part of initializing a host controller that uses the dma
  46. * memory allocators. It initializes some pools of dma-coherent memory that
  47. * will be shared by all drivers using that controller.
  48. *
  49. * Call hcd_buffer_destroy() to clean up after using those pools.
  50. *
  51. * Return: 0 if successful. A negative errno value otherwise.
  52. */
  53. int hcd_buffer_create(struct usb_hcd *hcd)
  54. {
  55. char name[16];
  56. int i, size;
  57. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  58. (!hcd->self.controller->dma_mask &&
  59. !(hcd->driver->flags & HCD_LOCAL_MEM)))
  60. return 0;
  61. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  62. size = pool_max[i];
  63. if (!size)
  64. continue;
  65. snprintf(name, sizeof(name), "buffer-%d", size);
  66. hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
  67. size, size, 0);
  68. if (!hcd->pool[i]) {
  69. hcd_buffer_destroy(hcd);
  70. return -ENOMEM;
  71. }
  72. }
  73. return 0;
  74. }
  75. /**
  76. * hcd_buffer_destroy - deallocate buffer pools
  77. * @hcd: the bus whose buffer pools are to be destroyed
  78. * Context: !in_interrupt()
  79. *
  80. * This frees the buffer pools created by hcd_buffer_create().
  81. */
  82. void hcd_buffer_destroy(struct usb_hcd *hcd)
  83. {
  84. int i;
  85. if (!IS_ENABLED(CONFIG_HAS_DMA))
  86. return;
  87. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  88. struct dma_pool *pool = hcd->pool[i];
  89. if (pool) {
  90. dma_pool_destroy(pool);
  91. hcd->pool[i] = NULL;
  92. }
  93. }
  94. }
  95. /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  96. * better sharing and to leverage mm/slab.c intelligence.
  97. */
  98. void *hcd_buffer_alloc(
  99. struct usb_bus *bus,
  100. size_t size,
  101. gfp_t mem_flags,
  102. dma_addr_t *dma
  103. )
  104. {
  105. struct usb_hcd *hcd = bus_to_hcd(bus);
  106. int i;
  107. if (size == 0)
  108. return NULL;
  109. /* some USB hosts just use PIO */
  110. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  111. (!bus->controller->dma_mask &&
  112. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  113. *dma = ~(dma_addr_t) 0;
  114. return kmalloc(size, mem_flags);
  115. }
  116. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  117. if (size <= pool_max[i])
  118. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  119. }
  120. return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
  121. }
  122. void hcd_buffer_free(
  123. struct usb_bus *bus,
  124. size_t size,
  125. void *addr,
  126. dma_addr_t dma
  127. )
  128. {
  129. struct usb_hcd *hcd = bus_to_hcd(bus);
  130. int i;
  131. if (!addr)
  132. return;
  133. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  134. (!bus->controller->dma_mask &&
  135. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  136. kfree(addr);
  137. return;
  138. }
  139. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  140. if (size <= pool_max[i]) {
  141. dma_pool_free(hcd->pool[i], addr, dma);
  142. return;
  143. }
  144. }
  145. dma_free_coherent(hcd->self.controller, size, addr, dma);
  146. }