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/types.h>
23 #include <linux/list.h>
25 #define HASH_SIZE 1024ULL
26 #define HASH_FN_SHIFT 13
27 #define HASH_FN_MASK (HASH_SIZE - 1)
36 struct dma_debug_entry {
37 struct list_head list;
49 struct list_head list;
51 } __cacheline_aligned_in_smp;
53 /* Hash list to save the allocated dma addresses */
54 static struct hash_bucket dma_entry_hash[HASH_SIZE];
55 /* List of pre-allocated dma_debug_entry's */
56 static LIST_HEAD(free_entries);
57 /* Lock for the list above */
58 static DEFINE_SPINLOCK(free_entries_lock);
60 /* Global disable flag - will be set in case of an error */
61 static bool global_disable __read_mostly;
63 static u32 num_free_entries;
64 static u32 min_free_entries;
67 * Hash related functions
69 * Every DMA-API request is saved into a struct dma_debug_entry. To
70 * have quick access to these structs they are stored into a hash.
72 static int hash_fn(struct dma_debug_entry *entry)
75 * Hash function is based on the dma address.
76 * We use bits 20-27 here as the index into the hash
78 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
82 * Request exclusive access to a hash bucket for a given dma_debug_entry.
84 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
87 int idx = hash_fn(entry);
88 unsigned long __flags;
90 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
92 return &dma_entry_hash[idx];
96 * Give up exclusive access to the hash bucket
98 static void put_hash_bucket(struct hash_bucket *bucket,
101 unsigned long __flags = *flags;
103 spin_unlock_irqrestore(&bucket->lock, __flags);
107 * Search a given entry in the hash bucket list
109 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
110 struct dma_debug_entry *ref)
112 struct dma_debug_entry *entry;
114 list_for_each_entry(entry, &bucket->list, list) {
115 if ((entry->dev_addr == ref->dev_addr) &&
116 (entry->dev == ref->dev))
124 * Add an entry to a hash bucket
126 static void hash_bucket_add(struct hash_bucket *bucket,
127 struct dma_debug_entry *entry)
129 list_add_tail(&entry->list, &bucket->list);
133 * Remove entry from a hash bucket list
135 static void hash_bucket_del(struct dma_debug_entry *entry)
137 list_del(&entry->list);
141 * Wrapper function for adding an entry to the hash.
142 * This function takes care of locking itself.
144 static void add_dma_entry(struct dma_debug_entry *entry)
146 struct hash_bucket *bucket;
149 bucket = get_hash_bucket(entry, &flags);
150 hash_bucket_add(bucket, entry);
151 put_hash_bucket(bucket, &flags);
154 /* struct dma_entry allocator
156 * The next two functions implement the allocator for
157 * struct dma_debug_entries.
159 static struct dma_debug_entry *dma_entry_alloc(void)
161 struct dma_debug_entry *entry = NULL;
164 spin_lock_irqsave(&free_entries_lock, flags);
166 if (list_empty(&free_entries)) {
167 printk(KERN_ERR "DMA-API: debugging out of memory "
169 global_disable = true;
173 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
174 list_del(&entry->list);
175 memset(entry, 0, sizeof(*entry));
177 num_free_entries -= 1;
178 if (num_free_entries < min_free_entries)
179 min_free_entries = num_free_entries;
182 spin_unlock_irqrestore(&free_entries_lock, flags);
187 static void dma_entry_free(struct dma_debug_entry *entry)
192 * add to beginning of the list - this way the entries are
193 * more likely cache hot when they are reallocated.
195 spin_lock_irqsave(&free_entries_lock, flags);
196 list_add(&entry->list, &free_entries);
197 num_free_entries += 1;
198 spin_unlock_irqrestore(&free_entries_lock, flags);