buffer.c 3.1 KB

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