buffer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/config.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/device.h>
  12. #include <linux/mm.h>
  13. #include <asm/io.h>
  14. #include <asm/scatterlist.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmapool.h>
  17. #ifdef CONFIG_USB_DEBUG
  18. #define DEBUG
  19. #else
  20. #undef DEBUG
  21. #endif
  22. #include <linux/usb.h>
  23. #include "hcd.h"
  24. /*
  25. * DMA-Coherent Buffers
  26. */
  27. /* FIXME tune these based on pool statistics ... */
  28. static const size_t pool_max [HCD_BUFFER_POOLS] = {
  29. /* platforms without dma-friendly caches might need to
  30. * prevent cacheline sharing...
  31. */
  32. 32,
  33. 128,
  34. 512,
  35. PAGE_SIZE / 2
  36. /* bigger --> allocate pages */
  37. };
  38. /* SETUP primitives */
  39. /**
  40. * hcd_buffer_create - initialize buffer pools
  41. * @hcd: the bus whose buffer pools are to be initialized
  42. * Context: !in_interrupt()
  43. *
  44. * Call this as part of initializing a host controller that uses the dma
  45. * memory allocators. It initializes some pools of dma-coherent memory that
  46. * will be shared by all drivers using that controller, or returns a negative
  47. * errno value on error.
  48. *
  49. * Call hcd_buffer_destroy() to clean up after using those pools.
  50. */
  51. int hcd_buffer_create (struct usb_hcd *hcd)
  52. {
  53. char name [16];
  54. int i, size;
  55. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  56. if (!(size = pool_max [i]))
  57. continue;
  58. snprintf (name, sizeof name, "buffer-%d", size);
  59. hcd->pool [i] = dma_pool_create (name, hcd->self.controller,
  60. size, size, 0);
  61. if (!hcd->pool [i]) {
  62. hcd_buffer_destroy (hcd);
  63. return -ENOMEM;
  64. }
  65. }
  66. return 0;
  67. }
  68. /**
  69. * hcd_buffer_destroy - deallocate buffer pools
  70. * @hcd: the bus whose buffer pools are to be destroyed
  71. * Context: !in_interrupt()
  72. *
  73. * This frees the buffer pools created by hcd_buffer_create().
  74. */
  75. void hcd_buffer_destroy (struct usb_hcd *hcd)
  76. {
  77. int i;
  78. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  79. struct dma_pool *pool = hcd->pool [i];
  80. if (pool) {
  81. dma_pool_destroy (pool);
  82. hcd->pool[i] = NULL;
  83. }
  84. }
  85. }
  86. /* sometimes alloc/free could use kmalloc with SLAB_DMA, for
  87. * better sharing and to leverage mm/slab.c intelligence.
  88. */
  89. void *hcd_buffer_alloc (
  90. struct usb_bus *bus,
  91. size_t size,
  92. unsigned mem_flags,
  93. dma_addr_t *dma
  94. )
  95. {
  96. struct usb_hcd *hcd = bus->hcpriv;
  97. int i;
  98. /* some USB hosts just use PIO */
  99. if (!bus->controller->dma_mask) {
  100. *dma = ~(dma_addr_t) 0;
  101. return kmalloc (size, mem_flags);
  102. }
  103. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  104. if (size <= pool_max [i])
  105. return dma_pool_alloc (hcd->pool [i], mem_flags, dma);
  106. }
  107. return dma_alloc_coherent (hcd->self.controller, size, dma, 0);
  108. }
  109. void hcd_buffer_free (
  110. struct usb_bus *bus,
  111. size_t size,
  112. void *addr,
  113. dma_addr_t dma
  114. )
  115. {
  116. struct usb_hcd *hcd = bus->hcpriv;
  117. int i;
  118. if (!addr)
  119. return;
  120. if (!bus->controller->dma_mask) {
  121. kfree (addr);
  122. return;
  123. }
  124. for (i = 0; i < HCD_BUFFER_POOLS; i++) {
  125. if (size <= pool_max [i]) {
  126. dma_pool_free (hcd->pool [i], addr, dma);
  127. return;
  128. }
  129. }
  130. dma_free_coherent (hcd->self.controller, size, addr, dma);
  131. }