buffer.c 3.1 KB

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