ohci-mem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ohci->td_cache = dma_pool_create ("ohci_td",
  34. ohci_to_hcd(ohci)->self.controller,
  35. sizeof (struct td),
  36. 32 /* byte alignment */,
  37. 0 /* no page-crossing issues */);
  38. if (!ohci->td_cache)
  39. return -ENOMEM;
  40. ohci->ed_cache = dma_pool_create ("ohci_ed",
  41. ohci_to_hcd(ohci)->self.controller,
  42. sizeof (struct ed),
  43. 16 /* byte alignment */,
  44. 0 /* no page-crossing issues */);
  45. if (!ohci->ed_cache) {
  46. dma_pool_destroy (ohci->td_cache);
  47. return -ENOMEM;
  48. }
  49. return 0;
  50. }
  51. static void ohci_mem_cleanup (struct ohci_hcd *ohci)
  52. {
  53. if (ohci->td_cache) {
  54. dma_pool_destroy (ohci->td_cache);
  55. ohci->td_cache = NULL;
  56. }
  57. if (ohci->ed_cache) {
  58. dma_pool_destroy (ohci->ed_cache);
  59. ohci->ed_cache = NULL;
  60. }
  61. }
  62. /*-------------------------------------------------------------------------*/
  63. /* ohci "done list" processing needs this mapping */
  64. static inline struct td *
  65. dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
  66. {
  67. struct td *td;
  68. td_dma &= TD_MASK;
  69. td = hc->td_hash [TD_HASH_FUNC(td_dma)];
  70. while (td && td->td_dma != td_dma)
  71. td = td->td_hash;
  72. return td;
  73. }
  74. /* TDs ... */
  75. static struct td *
  76. td_alloc (struct ohci_hcd *hc, gfp_t mem_flags)
  77. {
  78. dma_addr_t dma;
  79. struct td *td;
  80. td = dma_pool_zalloc (hc->td_cache, mem_flags, &dma);
  81. if (td) {
  82. /* in case hc fetches it, make it look dead */
  83. td->hwNextTD = cpu_to_hc32 (hc, dma);
  84. td->td_dma = dma;
  85. /* hashed in td_fill */
  86. }
  87. return td;
  88. }
  89. static void
  90. td_free (struct ohci_hcd *hc, struct td *td)
  91. {
  92. struct td **prev = &hc->td_hash [TD_HASH_FUNC (td->td_dma)];
  93. while (*prev && *prev != td)
  94. prev = &(*prev)->td_hash;
  95. if (*prev)
  96. *prev = td->td_hash;
  97. else if ((td->hwINFO & cpu_to_hc32(hc, TD_DONE)) != 0)
  98. ohci_dbg (hc, "no hash for td %p\n", td);
  99. dma_pool_free (hc->td_cache, td, td->td_dma);
  100. }
  101. /*-------------------------------------------------------------------------*/
  102. /* EDs ... */
  103. static struct ed *
  104. ed_alloc (struct ohci_hcd *hc, gfp_t mem_flags)
  105. {
  106. dma_addr_t dma;
  107. struct ed *ed;
  108. ed = dma_pool_zalloc (hc->ed_cache, mem_flags, &dma);
  109. if (ed) {
  110. INIT_LIST_HEAD (&ed->td_list);
  111. ed->dma = dma;
  112. }
  113. return ed;
  114. }
  115. static void
  116. ed_free (struct ohci_hcd *hc, struct ed *ed)
  117. {
  118. dma_pool_free (hc->ed_cache, ed, ed->dma);
  119. }