buffer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /* some USB hosts just use PIO */
  108. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  109. (!bus->controller->dma_mask &&
  110. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  111. *dma = ~(dma_addr_t) 0;
  112. return kmalloc(size, mem_flags);
  113. }
  114. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  115. if (size <= pool_max[i])
  116. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  117. }
  118. return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
  119. }
  120. void hcd_buffer_free(
  121. struct usb_bus *bus,
  122. size_t size,
  123. void *addr,
  124. dma_addr_t dma
  125. )
  126. {
  127. struct usb_hcd *hcd = bus_to_hcd(bus);
  128. int i;
  129. if (!addr)
  130. return;
  131. if (!IS_ENABLED(CONFIG_HAS_DMA) ||
  132. (!bus->controller->dma_mask &&
  133. !(hcd->driver->flags & HCD_LOCAL_MEM))) {
  134. kfree(addr);
  135. return;
  136. }
  137. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  138. if (size <= pool_max[i]) {
  139. dma_pool_free(hcd->pool[i], addr, dma);
  140. return;
  141. }
  142. }
  143. dma_free_coherent(hcd->self.controller, size, addr, dma);
  144. }