2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <joerg.roedel@amd.com>
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.
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.
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
20 #include <linux/dma-debug.h>
21 #include <linux/spinlock.h>
22 #include <linux/debugfs.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/slab.h>
27 #define HASH_SIZE 1024ULL
28 #define HASH_FN_SHIFT 13
29 #define HASH_FN_MASK (HASH_SIZE - 1)
38 struct dma_debug_entry {
39 struct list_head list;
51 struct list_head list;
53 } __cacheline_aligned_in_smp;
55 /* Hash list to save the allocated dma addresses */
56 static struct hash_bucket dma_entry_hash[HASH_SIZE];
57 /* List of pre-allocated dma_debug_entry's */
58 static LIST_HEAD(free_entries);
59 /* Lock for the list above */
60 static DEFINE_SPINLOCK(free_entries_lock);
62 /* Global disable flag - will be set in case of an error */
63 static bool global_disable __read_mostly;
65 /* Global error count */
66 static u32 error_count;
68 /* Global error show enable*/
69 static u32 show_all_errors __read_mostly;
70 /* Number of errors to show */
71 static u32 show_num_errors = 1;
73 static u32 num_free_entries;
74 static u32 min_free_entries;
76 /* number of preallocated entries requested by kernel cmdline */
77 static u32 req_entries;
79 /* debugfs dentry's for the stuff above */
80 static struct dentry *dma_debug_dent __read_mostly;
81 static struct dentry *global_disable_dent __read_mostly;
82 static struct dentry *error_count_dent __read_mostly;
83 static struct dentry *show_all_errors_dent __read_mostly;
84 static struct dentry *show_num_errors_dent __read_mostly;
85 static struct dentry *num_free_entries_dent __read_mostly;
86 static struct dentry *min_free_entries_dent __read_mostly;
89 * Hash related functions
91 * Every DMA-API request is saved into a struct dma_debug_entry. To
92 * have quick access to these structs they are stored into a hash.
94 static int hash_fn(struct dma_debug_entry *entry)
97 * Hash function is based on the dma address.
98 * We use bits 20-27 here as the index into the hash
100 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
104 * Request exclusive access to a hash bucket for a given dma_debug_entry.
106 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
107 unsigned long *flags)
109 int idx = hash_fn(entry);
110 unsigned long __flags;
112 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
114 return &dma_entry_hash[idx];
118 * Give up exclusive access to the hash bucket
120 static void put_hash_bucket(struct hash_bucket *bucket,
121 unsigned long *flags)
123 unsigned long __flags = *flags;
125 spin_unlock_irqrestore(&bucket->lock, __flags);
129 * Search a given entry in the hash bucket list
131 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
132 struct dma_debug_entry *ref)
134 struct dma_debug_entry *entry;
136 list_for_each_entry(entry, &bucket->list, list) {
137 if ((entry->dev_addr == ref->dev_addr) &&
138 (entry->dev == ref->dev))
146 * Add an entry to a hash bucket
148 static void hash_bucket_add(struct hash_bucket *bucket,
149 struct dma_debug_entry *entry)
151 list_add_tail(&entry->list, &bucket->list);
155 * Remove entry from a hash bucket list
157 static void hash_bucket_del(struct dma_debug_entry *entry)
159 list_del(&entry->list);
163 * Wrapper function for adding an entry to the hash.
164 * This function takes care of locking itself.
166 static void add_dma_entry(struct dma_debug_entry *entry)
168 struct hash_bucket *bucket;
171 bucket = get_hash_bucket(entry, &flags);
172 hash_bucket_add(bucket, entry);
173 put_hash_bucket(bucket, &flags);
176 /* struct dma_entry allocator
178 * The next two functions implement the allocator for
179 * struct dma_debug_entries.
181 static struct dma_debug_entry *dma_entry_alloc(void)
183 struct dma_debug_entry *entry = NULL;
186 spin_lock_irqsave(&free_entries_lock, flags);
188 if (list_empty(&free_entries)) {
189 printk(KERN_ERR "DMA-API: debugging out of memory "
191 global_disable = true;
195 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
196 list_del(&entry->list);
197 memset(entry, 0, sizeof(*entry));
199 num_free_entries -= 1;
200 if (num_free_entries < min_free_entries)
201 min_free_entries = num_free_entries;
204 spin_unlock_irqrestore(&free_entries_lock, flags);
209 static void dma_entry_free(struct dma_debug_entry *entry)
214 * add to beginning of the list - this way the entries are
215 * more likely cache hot when they are reallocated.
217 spin_lock_irqsave(&free_entries_lock, flags);
218 list_add(&entry->list, &free_entries);
219 num_free_entries += 1;
220 spin_unlock_irqrestore(&free_entries_lock, flags);
224 * DMA-API debugging init code
226 * The init code does two things:
227 * 1. Initialize core data structures
228 * 2. Preallocate a given number of dma_debug_entry structs
231 static int prealloc_memory(u32 num_entries)
233 struct dma_debug_entry *entry, *next_entry;
236 for (i = 0; i < num_entries; ++i) {
237 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
241 list_add_tail(&entry->list, &free_entries);
244 num_free_entries = num_entries;
245 min_free_entries = num_entries;
247 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
254 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
255 list_del(&entry->list);
262 static int dma_debug_fs_init(void)
264 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
265 if (!dma_debug_dent) {
266 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
270 global_disable_dent = debugfs_create_bool("disabled", 0444,
272 (u32 *)&global_disable);
273 if (!global_disable_dent)
276 error_count_dent = debugfs_create_u32("error_count", 0444,
277 dma_debug_dent, &error_count);
278 if (!error_count_dent)
281 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
284 if (!show_all_errors_dent)
287 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
290 if (!show_num_errors_dent)
293 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
296 if (!num_free_entries_dent)
299 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
302 if (!min_free_entries_dent)
308 debugfs_remove_recursive(dma_debug_dent);
315 * Let the architectures decide how many entries should be preallocated.
317 void dma_debug_init(u32 num_entries)
324 for (i = 0; i < HASH_SIZE; ++i) {
325 INIT_LIST_HEAD(&dma_entry_hash[i].list);
326 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
329 if (dma_debug_fs_init() != 0) {
330 printk(KERN_ERR "DMA-API: error creating debugfs entries "
332 global_disable = true;
338 num_entries = req_entries;
340 if (prealloc_memory(num_entries) != 0) {
341 printk(KERN_ERR "DMA-API: debugging out of memory error "
343 global_disable = true;
348 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
351 static __init int dma_debug_cmdline(char *str)
356 if (strncmp(str, "off", 3) == 0) {
357 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
359 global_disable = true;
365 static __init int dma_debug_entries_cmdline(char *str)
372 res = get_option(&str, &req_entries);
380 __setup("dma_debug=", dma_debug_cmdline);
381 __setup("dma_debug_entries=", dma_debug_entries_cmdline);