buffer.c 3.5 KB

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