dma-debug.c 8.7 KB

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