buffer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 (!hcd->self.controller->dma_mask &&
  58. !(hcd->driver->flags & HCD_LOCAL_MEM))
  59. return 0;
  60. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  61. size = pool_max[i];
  62. if (!size)
  63. continue;
  64. snprintf(name, sizeof(name), "buffer-%d", size);
  65. hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
  66. size, size, 0);
  67. if (!hcd->pool[i]) {
  68. hcd_buffer_destroy(hcd);
  69. return -ENOMEM;
  70. }
  71. }
  72. return 0;
  73. }
  74. /**
  75. * hcd_buffer_destroy - deallocate buffer pools
  76. * @hcd: the bus whose buffer pools are to be destroyed
  77. * Context: !in_interrupt()
  78. *
  79. * This frees the buffer pools created by hcd_buffer_create().
  80. */
  81. void hcd_buffer_destroy(struct usb_hcd *hcd)
  82. {
  83. int i;
  84. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  85. struct dma_pool *pool = hcd->pool[i];
  86. if (pool) {
  87. dma_pool_destroy(pool);
  88. hcd->pool[i] = NULL;
  89. }
  90. }
  91. }
  92. /* sometimes alloc/free could use kmalloc with GFP_DMA, for
  93. * better sharing and to leverage mm/slab.c intelligence.
  94. */
  95. void *hcd_buffer_alloc(
  96. struct usb_bus *bus,
  97. size_t size,
  98. gfp_t mem_flags,
  99. dma_addr_t *dma
  100. )
  101. {
  102. struct usb_hcd *hcd = bus_to_hcd(bus);
  103. int i;
  104. /* some USB hosts just use PIO */
  105. if (!bus->controller->dma_mask &&
  106. !(hcd->driver->flags & HCD_LOCAL_MEM)) {
  107. *dma = ~(dma_addr_t) 0;
  108. return kmalloc(size, mem_flags);
  109. }
  110. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  111. if (size <= pool_max[i])
  112. return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
  113. }
  114. return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
  115. }
  116. void hcd_buffer_free(
  117. struct usb_bus *bus,
  118. size_t size,
  119. void *addr,
  120. dma_addr_t dma
  121. )
  122. {
  123. struct usb_hcd *hcd = bus_to_hcd(bus);
  124. int i;
  125. if (!addr)
  126. return;
  127. if (!bus->controller->dma_mask &&
  128. !(hcd->driver->flags & HCD_LOCAL_MEM)) {
  129. kfree(addr);
  130. return;
  131. }
  132. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  133. if (size <= pool_max[i]) {
  134. dma_pool_free(hcd->pool[i], addr, dma);
  135. return;
  136. }
  137. }
  138. dma_free_coherent(hcd->self.controller, size, addr, dma);
  139. }