dma-debug.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Joerg Roedel <joerg.roedel@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/dma-debug.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/list.h>
  23. #define HASH_SIZE 1024ULL
  24. #define HASH_FN_SHIFT 13
  25. #define HASH_FN_MASK (HASH_SIZE - 1)
  26. enum {
  27. dma_debug_single,
  28. dma_debug_page,
  29. dma_debug_sg,
  30. dma_debug_coherent,
  31. };
  32. struct dma_debug_entry {
  33. struct list_head list;
  34. struct device *dev;
  35. int type;
  36. phys_addr_t paddr;
  37. u64 dev_addr;
  38. u64 size;
  39. int direction;
  40. int sg_call_ents;
  41. int sg_mapped_ents;
  42. };
  43. struct hash_bucket {
  44. struct list_head list;
  45. spinlock_t lock;
  46. } __cacheline_aligned_in_smp;
  47. /* Hash list to save the allocated dma addresses */
  48. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  49. /*
  50. * Hash related functions
  51. *
  52. * Every DMA-API request is saved into a struct dma_debug_entry. To
  53. * have quick access to these structs they are stored into a hash.
  54. */
  55. static int hash_fn(struct dma_debug_entry *entry)
  56. {
  57. /*
  58. * Hash function is based on the dma address.
  59. * We use bits 20-27 here as the index into the hash
  60. */
  61. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  62. }
  63. /*
  64. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  65. */
  66. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  67. unsigned long *flags)
  68. {
  69. int idx = hash_fn(entry);
  70. unsigned long __flags;
  71. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  72. *flags = __flags;
  73. return &dma_entry_hash[idx];
  74. }
  75. /*
  76. * Give up exclusive access to the hash bucket
  77. */
  78. static void put_hash_bucket(struct hash_bucket *bucket,
  79. unsigned long *flags)
  80. {
  81. unsigned long __flags = *flags;
  82. spin_unlock_irqrestore(&bucket->lock, __flags);
  83. }
  84. /*
  85. * Search a given entry in the hash bucket list
  86. */
  87. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  88. struct dma_debug_entry *ref)
  89. {
  90. struct dma_debug_entry *entry;
  91. list_for_each_entry(entry, &bucket->list, list) {
  92. if ((entry->dev_addr == ref->dev_addr) &&
  93. (entry->dev == ref->dev))
  94. return entry;
  95. }
  96. return NULL;
  97. }
  98. /*
  99. * Add an entry to a hash bucket
  100. */
  101. static void hash_bucket_add(struct hash_bucket *bucket,
  102. struct dma_debug_entry *entry)
  103. {
  104. list_add_tail(&entry->list, &bucket->list);
  105. }
  106. /*
  107. * Remove entry from a hash bucket list
  108. */
  109. static void hash_bucket_del(struct dma_debug_entry *entry)
  110. {
  111. list_del(&entry->list);
  112. }
  113. /*
  114. * Wrapper function for adding an entry to the hash.
  115. * This function takes care of locking itself.
  116. */
  117. static void add_dma_entry(struct dma_debug_entry *entry)
  118. {
  119. struct hash_bucket *bucket;
  120. unsigned long flags;
  121. bucket = get_hash_bucket(entry, &flags);
  122. hash_bucket_add(bucket, entry);
  123. put_hash_bucket(bucket, &flags);
  124. }