dma-debug.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include <linux/slab.h>
  24. #define HASH_SIZE 1024ULL
  25. #define HASH_FN_SHIFT 13
  26. #define HASH_FN_MASK (HASH_SIZE - 1)
  27. enum {
  28. dma_debug_single,
  29. dma_debug_page,
  30. dma_debug_sg,
  31. dma_debug_coherent,
  32. };
  33. struct dma_debug_entry {
  34. struct list_head list;
  35. struct device *dev;
  36. int type;
  37. phys_addr_t paddr;
  38. u64 dev_addr;
  39. u64 size;
  40. int direction;
  41. int sg_call_ents;
  42. int sg_mapped_ents;
  43. };
  44. struct hash_bucket {
  45. struct list_head list;
  46. spinlock_t lock;
  47. } __cacheline_aligned_in_smp;
  48. /* Hash list to save the allocated dma addresses */
  49. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  50. /* List of pre-allocated dma_debug_entry's */
  51. static LIST_HEAD(free_entries);
  52. /* Lock for the list above */
  53. static DEFINE_SPINLOCK(free_entries_lock);
  54. /* Global disable flag - will be set in case of an error */
  55. static bool global_disable __read_mostly;
  56. static u32 num_free_entries;
  57. static u32 min_free_entries;
  58. /* number of preallocated entries requested by kernel cmdline */
  59. static u32 req_entries;
  60. /*
  61. * Hash related functions
  62. *
  63. * Every DMA-API request is saved into a struct dma_debug_entry. To
  64. * have quick access to these structs they are stored into a hash.
  65. */
  66. static int hash_fn(struct dma_debug_entry *entry)
  67. {
  68. /*
  69. * Hash function is based on the dma address.
  70. * We use bits 20-27 here as the index into the hash
  71. */
  72. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  73. }
  74. /*
  75. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  76. */
  77. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  78. unsigned long *flags)
  79. {
  80. int idx = hash_fn(entry);
  81. unsigned long __flags;
  82. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  83. *flags = __flags;
  84. return &dma_entry_hash[idx];
  85. }
  86. /*
  87. * Give up exclusive access to the hash bucket
  88. */
  89. static void put_hash_bucket(struct hash_bucket *bucket,
  90. unsigned long *flags)
  91. {
  92. unsigned long __flags = *flags;
  93. spin_unlock_irqrestore(&bucket->lock, __flags);
  94. }
  95. /*
  96. * Search a given entry in the hash bucket list
  97. */
  98. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  99. struct dma_debug_entry *ref)
  100. {
  101. struct dma_debug_entry *entry;
  102. list_for_each_entry(entry, &bucket->list, list) {
  103. if ((entry->dev_addr == ref->dev_addr) &&
  104. (entry->dev == ref->dev))
  105. return entry;
  106. }
  107. return NULL;
  108. }
  109. /*
  110. * Add an entry to a hash bucket
  111. */
  112. static void hash_bucket_add(struct hash_bucket *bucket,
  113. struct dma_debug_entry *entry)
  114. {
  115. list_add_tail(&entry->list, &bucket->list);
  116. }
  117. /*
  118. * Remove entry from a hash bucket list
  119. */
  120. static void hash_bucket_del(struct dma_debug_entry *entry)
  121. {
  122. list_del(&entry->list);
  123. }
  124. /*
  125. * Wrapper function for adding an entry to the hash.
  126. * This function takes care of locking itself.
  127. */
  128. static void add_dma_entry(struct dma_debug_entry *entry)
  129. {
  130. struct hash_bucket *bucket;
  131. unsigned long flags;
  132. bucket = get_hash_bucket(entry, &flags);
  133. hash_bucket_add(bucket, entry);
  134. put_hash_bucket(bucket, &flags);
  135. }
  136. /* struct dma_entry allocator
  137. *
  138. * The next two functions implement the allocator for
  139. * struct dma_debug_entries.
  140. */
  141. static struct dma_debug_entry *dma_entry_alloc(void)
  142. {
  143. struct dma_debug_entry *entry = NULL;
  144. unsigned long flags;
  145. spin_lock_irqsave(&free_entries_lock, flags);
  146. if (list_empty(&free_entries)) {
  147. printk(KERN_ERR "DMA-API: debugging out of memory "
  148. "- disabling\n");
  149. global_disable = true;
  150. goto out;
  151. }
  152. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  153. list_del(&entry->list);
  154. memset(entry, 0, sizeof(*entry));
  155. num_free_entries -= 1;
  156. if (num_free_entries < min_free_entries)
  157. min_free_entries = num_free_entries;
  158. out:
  159. spin_unlock_irqrestore(&free_entries_lock, flags);
  160. return entry;
  161. }
  162. static void dma_entry_free(struct dma_debug_entry *entry)
  163. {
  164. unsigned long flags;
  165. /*
  166. * add to beginning of the list - this way the entries are
  167. * more likely cache hot when they are reallocated.
  168. */
  169. spin_lock_irqsave(&free_entries_lock, flags);
  170. list_add(&entry->list, &free_entries);
  171. num_free_entries += 1;
  172. spin_unlock_irqrestore(&free_entries_lock, flags);
  173. }
  174. /*
  175. * DMA-API debugging init code
  176. *
  177. * The init code does two things:
  178. * 1. Initialize core data structures
  179. * 2. Preallocate a given number of dma_debug_entry structs
  180. */
  181. static int prealloc_memory(u32 num_entries)
  182. {
  183. struct dma_debug_entry *entry, *next_entry;
  184. int i;
  185. for (i = 0; i < num_entries; ++i) {
  186. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  187. if (!entry)
  188. goto out_err;
  189. list_add_tail(&entry->list, &free_entries);
  190. }
  191. num_free_entries = num_entries;
  192. min_free_entries = num_entries;
  193. printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
  194. num_entries);
  195. return 0;
  196. out_err:
  197. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  198. list_del(&entry->list);
  199. kfree(entry);
  200. }
  201. return -ENOMEM;
  202. }
  203. /*
  204. * Let the architectures decide how many entries should be preallocated.
  205. */
  206. void dma_debug_init(u32 num_entries)
  207. {
  208. int i;
  209. if (global_disable)
  210. return;
  211. for (i = 0; i < HASH_SIZE; ++i) {
  212. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  213. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  214. }
  215. if (req_entries)
  216. num_entries = req_entries;
  217. if (prealloc_memory(num_entries) != 0) {
  218. printk(KERN_ERR "DMA-API: debugging out of memory error "
  219. "- disabled\n");
  220. global_disable = true;
  221. return;
  222. }
  223. printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
  224. }
  225. static __init int dma_debug_cmdline(char *str)
  226. {
  227. if (!str)
  228. return -EINVAL;
  229. if (strncmp(str, "off", 3) == 0) {
  230. printk(KERN_INFO "DMA-API: debugging disabled on kernel "
  231. "command line\n");
  232. global_disable = true;
  233. }
  234. return 0;
  235. }
  236. static __init int dma_debug_entries_cmdline(char *str)
  237. {
  238. int res;
  239. if (!str)
  240. return -EINVAL;
  241. res = get_option(&str, &req_entries);
  242. if (!res)
  243. req_entries = 0;
  244. return 0;
  245. }
  246. __setup("dma_debug=", dma_debug_cmdline);
  247. __setup("dma_debug_entries=", dma_debug_entries_cmdline);