ohci-mem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * OHCI HCD (Host Controller Driver) for USB.
  4. *
  5. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  6. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  7. *
  8. * This file is licenced under the GPL.
  9. */
  10. /*-------------------------------------------------------------------------*/
  11. /*
  12. * OHCI deals with three types of memory:
  13. * - data used only by the HCD ... kmalloc is fine
  14. * - async and periodic schedules, shared by HC and HCD ... these
  15. * need to use dma_pool or dma_alloc_coherent
  16. * - driver buffers, read/written by HC ... the hcd glue or the
  17. * device driver provides us with dma addresses
  18. *
  19. * There's also "register" data, which is memory mapped.
  20. * No memory seen by this driver (or any HCD) may be paged out.
  21. */
  22. /*-------------------------------------------------------------------------*/
  23. static void ohci_hcd_init (struct ohci_hcd *ohci)
  24. {
  25. ohci->next_statechange = jiffies;
  26. spin_lock_init (&ohci->lock);
  27. INIT_LIST_HEAD (&ohci->pending);
  28. INIT_LIST_HEAD(&ohci->eds_in_use);
  29. }
  30. /*-------------------------------------------------------------------------*/
  31. static int ohci_mem_init (struct ohci_hcd *ohci)
  32. {
  33. /*
  34. * HCs with local memory allocate from localmem_pool so there's
  35. * no need to create the below dma pools.
  36. */
  37. if (ohci_to_hcd(ohci)->localmem_pool)
  38. return 0;
  39. ohci->td_cache = dma_pool_create ("ohci_td",
  40. ohci_to_hcd(ohci)->self.controller,
  41. sizeof (struct td),
  42. 32 /* byte alignment */,
  43. 0 /* no page-crossing issues */);
  44. if (!ohci->td_cache)
  45. return -ENOMEM;
  46. ohci->ed_cache = dma_pool_create ("ohci_ed",
  47. ohci_to_hcd(ohci)->self.controller,
  48. sizeof (struct ed),
  49. 16 /* byte alignment */,
  50. 0 /* no page-crossing issues */);
  51. if (!ohci->ed_cache) {
  52. dma_pool_destroy (ohci->td_cache);
  53. return -ENOMEM;
  54. }
  55. return 0;
  56. }
  57. static void ohci_mem_cleanup (struct ohci_hcd *ohci)
  58. {
  59. dma_pool_destroy(ohci->td_cache);
  60. ohci->td_cache = NULL;
  61. dma_pool_destroy(ohci->ed_cache);
  62. ohci->ed_cache = NULL;
  63. }
  64. /*-------------------------------------------------------------------------*/
  65. /* ohci "done list" processing needs this mapping */
  66. static inline struct td *
  67. dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
  68. {
  69. struct td *td;
  70. td_dma &= TD_MASK;
  71. td = hc->td_hash [TD_HASH_FUNC(td_dma)];
  72. while (td && td->td_dma != td_dma)
  73. td = td->td_hash;
  74. return td;
  75. }
  76. /* TDs ... */
  77. static struct td *
  78. td_alloc (struct ohci_hcd *hc, gfp_t mem_flags)
  79. {
  80. dma_addr_t dma;
  81. struct td *td;
  82. struct usb_hcd *hcd = ohci_to_hcd(hc);
  83. if (hcd->localmem_pool)
  84. td = gen_pool_dma_zalloc_align(hcd->localmem_pool,
  85. sizeof(*td), &dma, 32);
  86. else
  87. td = dma_pool_zalloc(hc->td_cache, mem_flags, &dma);
  88. if (td) {
  89. /* in case hc fetches it, make it look dead */
  90. td->hwNextTD = cpu_to_hc32 (hc, dma);
  91. td->td_dma = dma;
  92. /* hashed in td_fill */
  93. }
  94. return td;
  95. }
  96. static void
  97. td_free (struct ohci_hcd *hc, struct td *td)
  98. {
  99. struct td **prev = &hc->td_hash [TD_HASH_FUNC (td->td_dma)];
  100. struct usb_hcd *hcd = ohci_to_hcd(hc);
  101. while (*prev && *prev != td)
  102. prev = &(*prev)->td_hash;
  103. if (*prev)
  104. *prev = td->td_hash;
  105. else if ((td->hwINFO & cpu_to_hc32(hc, TD_DONE)) != 0)
  106. ohci_dbg (hc, "no hash for td %p\n", td);
  107. if (hcd->localmem_pool)
  108. gen_pool_free(hcd->localmem_pool, (unsigned long)td,
  109. sizeof(*td));
  110. else
  111. dma_pool_free(hc->td_cache, td, td->td_dma);
  112. }
  113. /*-------------------------------------------------------------------------*/
  114. /* EDs ... */
  115. static struct ed *
  116. ed_alloc (struct ohci_hcd *hc, gfp_t mem_flags)
  117. {
  118. dma_addr_t dma;
  119. struct ed *ed;
  120. struct usb_hcd *hcd = ohci_to_hcd(hc);
  121. if (hcd->localmem_pool)
  122. ed = gen_pool_dma_zalloc_align(hcd->localmem_pool,
  123. sizeof(*ed), &dma, 16);
  124. else
  125. ed = dma_pool_zalloc(hc->ed_cache, mem_flags, &dma);
  126. if (ed) {
  127. INIT_LIST_HEAD (&ed->td_list);
  128. ed->dma = dma;
  129. }
  130. return ed;
  131. }
  132. static void
  133. ed_free (struct ohci_hcd *hc, struct ed *ed)
  134. {
  135. struct usb_hcd *hcd = ohci_to_hcd(hc);
  136. if (hcd->localmem_pool)
  137. gen_pool_free(hcd->localmem_pool, (unsigned long)ed,
  138. sizeof(*ed));
  139. else
  140. dma_pool_free(hc->ed_cache, ed, ed->dma);
  141. }