ohci-mem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  6. *
  7. * This file is licenced under the GPL.
  8. */
  9. /*-------------------------------------------------------------------------*/
  10. /*
  11. * OHCI deals with three types of memory:
  12. * - data used only by the HCD ... kmalloc is fine
  13. * - async and periodic schedules, shared by HC and HCD ... these
  14. * need to use dma_pool or dma_alloc_coherent
  15. * - driver buffers, read/written by HC ... the hcd glue or the
  16. * device driver provides us with dma addresses
  17. *
  18. * There's also "register" data, which is memory mapped.
  19. * No memory seen by this driver (or any HCD) may be paged out.
  20. */
  21. /*-------------------------------------------------------------------------*/
  22. static void ohci_hcd_init (struct ohci_hcd *ohci)
  23. {
  24. ohci->next_statechange = jiffies;
  25. spin_lock_init (&ohci->lock);
  26. INIT_LIST_HEAD (&ohci->pending);
  27. INIT_LIST_HEAD(&ohci->eds_in_use);
  28. }
  29. /*-------------------------------------------------------------------------*/
  30. static int ohci_mem_init (struct ohci_hcd *ohci)
  31. {
  32. ohci->td_cache = dma_pool_create ("ohci_td",
  33. ohci_to_hcd(ohci)->self.controller,
  34. sizeof (struct td),
  35. 32 /* byte alignment */,
  36. 0 /* no page-crossing issues */);
  37. if (!ohci->td_cache)
  38. return -ENOMEM;
  39. ohci->ed_cache = dma_pool_create ("ohci_ed",
  40. ohci_to_hcd(ohci)->self.controller,
  41. sizeof (struct ed),
  42. 16 /* byte alignment */,
  43. 0 /* no page-crossing issues */);
  44. if (!ohci->ed_cache) {
  45. dma_pool_destroy (ohci->td_cache);
  46. return -ENOMEM;
  47. }
  48. return 0;
  49. }
  50. static void ohci_mem_cleanup (struct ohci_hcd *ohci)
  51. {
  52. if (ohci->td_cache) {
  53. dma_pool_destroy (ohci->td_cache);
  54. ohci->td_cache = NULL;
  55. }
  56. if (ohci->ed_cache) {
  57. dma_pool_destroy (ohci->ed_cache);
  58. ohci->ed_cache = NULL;
  59. }
  60. }
  61. /*-------------------------------------------------------------------------*/
  62. /* ohci "done list" processing needs this mapping */
  63. static inline struct td *
  64. dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
  65. {
  66. struct td *td;
  67. td_dma &= TD_MASK;
  68. td = hc->td_hash [TD_HASH_FUNC(td_dma)];
  69. while (td && td->td_dma != td_dma)
  70. td = td->td_hash;
  71. return td;
  72. }
  73. /* TDs ... */
  74. static struct td *
  75. td_alloc (struct ohci_hcd *hc, gfp_t mem_flags)
  76. {
  77. dma_addr_t dma;
  78. struct td *td;
  79. td = dma_pool_alloc (hc->td_cache, mem_flags, &dma);
  80. if (td) {
  81. /* in case hc fetches it, make it look dead */
  82. memset (td, 0, sizeof *td);
  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_alloc (hc->ed_cache, mem_flags, &dma);
  109. if (ed) {
  110. memset (ed, 0, sizeof (*ed));
  111. INIT_LIST_HEAD (&ed->td_list);
  112. ed->dma = dma;
  113. }
  114. return ed;
  115. }
  116. static void
  117. ed_free (struct ohci_hcd *hc, struct ed *ed)
  118. {
  119. dma_pool_free (hc->ed_cache, ed, ed->dma);
  120. }