buffer.c 3.2 KB

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