Commit 35d40952dba7b0689a16bd1463fb7698f8dbe639

Authored by FUJITA Tomonori
Committed by Ingo Molnar
1 parent 84be58d460

dma-debug: warn of unmapping an invalid dma address

Impact: extend DMA-debug checks

Calling dma_unmap families against an invalid dma address should be a
bug.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Cc: Joerg Roedel <joerg.roedel@amd.com>
LKML-Reference: <20090319103743N.fujita.tomonori@lab.ntt.co.jp>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

Showing 1 changed file with 4 additions and 1 deletions Inline Diff

1 /* 1 /*
2 * Copyright (C) 2008 Advanced Micro Devices, Inc. 2 * Copyright (C) 2008 Advanced Micro Devices, Inc.
3 * 3 *
4 * Author: Joerg Roedel <joerg.roedel@amd.com> 4 * Author: Joerg Roedel <joerg.roedel@amd.com>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify it 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 7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation. 8 * by the Free Software Foundation.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 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 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 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 18 */
19 19
20 #include <linux/scatterlist.h> 20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h> 21 #include <linux/dma-mapping.h>
22 #include <linux/stacktrace.h> 22 #include <linux/stacktrace.h>
23 #include <linux/dma-debug.h> 23 #include <linux/dma-debug.h>
24 #include <linux/spinlock.h> 24 #include <linux/spinlock.h>
25 #include <linux/debugfs.h> 25 #include <linux/debugfs.h>
26 #include <linux/device.h> 26 #include <linux/device.h>
27 #include <linux/types.h> 27 #include <linux/types.h>
28 #include <linux/sched.h> 28 #include <linux/sched.h>
29 #include <linux/list.h> 29 #include <linux/list.h>
30 #include <linux/slab.h> 30 #include <linux/slab.h>
31 31
32 #include <asm/sections.h> 32 #include <asm/sections.h>
33 33
34 #define HASH_SIZE 1024ULL 34 #define HASH_SIZE 1024ULL
35 #define HASH_FN_SHIFT 13 35 #define HASH_FN_SHIFT 13
36 #define HASH_FN_MASK (HASH_SIZE - 1) 36 #define HASH_FN_MASK (HASH_SIZE - 1)
37 37
38 enum { 38 enum {
39 dma_debug_single, 39 dma_debug_single,
40 dma_debug_page, 40 dma_debug_page,
41 dma_debug_sg, 41 dma_debug_sg,
42 dma_debug_coherent, 42 dma_debug_coherent,
43 }; 43 };
44 44
45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5 45 #define DMA_DEBUG_STACKTRACE_ENTRIES 5
46 46
47 struct dma_debug_entry { 47 struct dma_debug_entry {
48 struct list_head list; 48 struct list_head list;
49 struct device *dev; 49 struct device *dev;
50 int type; 50 int type;
51 phys_addr_t paddr; 51 phys_addr_t paddr;
52 u64 dev_addr; 52 u64 dev_addr;
53 u64 size; 53 u64 size;
54 int direction; 54 int direction;
55 int sg_call_ents; 55 int sg_call_ents;
56 int sg_mapped_ents; 56 int sg_mapped_ents;
57 #ifdef CONFIG_STACKTRACE 57 #ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace; 58 struct stack_trace stacktrace;
59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES]; 59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
60 #endif 60 #endif
61 }; 61 };
62 62
63 struct hash_bucket { 63 struct hash_bucket {
64 struct list_head list; 64 struct list_head list;
65 spinlock_t lock; 65 spinlock_t lock;
66 } ____cacheline_aligned_in_smp; 66 } ____cacheline_aligned_in_smp;
67 67
68 /* Hash list to save the allocated dma addresses */ 68 /* Hash list to save the allocated dma addresses */
69 static struct hash_bucket dma_entry_hash[HASH_SIZE]; 69 static struct hash_bucket dma_entry_hash[HASH_SIZE];
70 /* List of pre-allocated dma_debug_entry's */ 70 /* List of pre-allocated dma_debug_entry's */
71 static LIST_HEAD(free_entries); 71 static LIST_HEAD(free_entries);
72 /* Lock for the list above */ 72 /* Lock for the list above */
73 static DEFINE_SPINLOCK(free_entries_lock); 73 static DEFINE_SPINLOCK(free_entries_lock);
74 74
75 /* Global disable flag - will be set in case of an error */ 75 /* Global disable flag - will be set in case of an error */
76 static bool global_disable __read_mostly; 76 static bool global_disable __read_mostly;
77 77
78 /* Global error count */ 78 /* Global error count */
79 static u32 error_count; 79 static u32 error_count;
80 80
81 /* Global error show enable*/ 81 /* Global error show enable*/
82 static u32 show_all_errors __read_mostly; 82 static u32 show_all_errors __read_mostly;
83 /* Number of errors to show */ 83 /* Number of errors to show */
84 static u32 show_num_errors = 1; 84 static u32 show_num_errors = 1;
85 85
86 static u32 num_free_entries; 86 static u32 num_free_entries;
87 static u32 min_free_entries; 87 static u32 min_free_entries;
88 88
89 /* number of preallocated entries requested by kernel cmdline */ 89 /* number of preallocated entries requested by kernel cmdline */
90 static u32 req_entries; 90 static u32 req_entries;
91 91
92 /* debugfs dentry's for the stuff above */ 92 /* debugfs dentry's for the stuff above */
93 static struct dentry *dma_debug_dent __read_mostly; 93 static struct dentry *dma_debug_dent __read_mostly;
94 static struct dentry *global_disable_dent __read_mostly; 94 static struct dentry *global_disable_dent __read_mostly;
95 static struct dentry *error_count_dent __read_mostly; 95 static struct dentry *error_count_dent __read_mostly;
96 static struct dentry *show_all_errors_dent __read_mostly; 96 static struct dentry *show_all_errors_dent __read_mostly;
97 static struct dentry *show_num_errors_dent __read_mostly; 97 static struct dentry *show_num_errors_dent __read_mostly;
98 static struct dentry *num_free_entries_dent __read_mostly; 98 static struct dentry *num_free_entries_dent __read_mostly;
99 static struct dentry *min_free_entries_dent __read_mostly; 99 static struct dentry *min_free_entries_dent __read_mostly;
100 100
101 static const char *type2name[4] = { "single", "page", 101 static const char *type2name[4] = { "single", "page",
102 "scather-gather", "coherent" }; 102 "scather-gather", "coherent" };
103 103
104 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", 104 static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
105 "DMA_FROM_DEVICE", "DMA_NONE" }; 105 "DMA_FROM_DEVICE", "DMA_NONE" };
106 106
107 /* 107 /*
108 * The access to some variables in this macro is racy. We can't use atomic_t 108 * The access to some variables in this macro is racy. We can't use atomic_t
109 * here because all these variables are exported to debugfs. Some of them even 109 * here because all these variables are exported to debugfs. Some of them even
110 * writeable. This is also the reason why a lock won't help much. But anyway, 110 * writeable. This is also the reason why a lock won't help much. But anyway,
111 * the races are no big deal. Here is why: 111 * the races are no big deal. Here is why:
112 * 112 *
113 * error_count: the addition is racy, but the worst thing that can happen is 113 * error_count: the addition is racy, but the worst thing that can happen is
114 * that we don't count some errors 114 * that we don't count some errors
115 * show_num_errors: the subtraction is racy. Also no big deal because in 115 * show_num_errors: the subtraction is racy. Also no big deal because in
116 * worst case this will result in one warning more in the 116 * worst case this will result in one warning more in the
117 * system log than the user configured. This variable is 117 * system log than the user configured. This variable is
118 * writeable via debugfs. 118 * writeable via debugfs.
119 */ 119 */
120 static inline void dump_entry_trace(struct dma_debug_entry *entry) 120 static inline void dump_entry_trace(struct dma_debug_entry *entry)
121 { 121 {
122 #ifdef CONFIG_STACKTRACE 122 #ifdef CONFIG_STACKTRACE
123 if (entry) { 123 if (entry) {
124 printk(KERN_WARNING "Mapped at:\n"); 124 printk(KERN_WARNING "Mapped at:\n");
125 print_stack_trace(&entry->stacktrace, 0); 125 print_stack_trace(&entry->stacktrace, 0);
126 } 126 }
127 #endif 127 #endif
128 } 128 }
129 129
130 #define err_printk(dev, entry, format, arg...) do { \ 130 #define err_printk(dev, entry, format, arg...) do { \
131 error_count += 1; \ 131 error_count += 1; \
132 if (show_all_errors || show_num_errors > 0) { \ 132 if (show_all_errors || show_num_errors > 0) { \
133 WARN(1, "%s %s: " format, \ 133 WARN(1, "%s %s: " format, \
134 dev_driver_string(dev), \ 134 dev_driver_string(dev), \
135 dev_name(dev) , ## arg); \ 135 dev_name(dev) , ## arg); \
136 dump_entry_trace(entry); \ 136 dump_entry_trace(entry); \
137 } \ 137 } \
138 if (!show_all_errors && show_num_errors > 0) \ 138 if (!show_all_errors && show_num_errors > 0) \
139 show_num_errors -= 1; \ 139 show_num_errors -= 1; \
140 } while (0); 140 } while (0);
141 141
142 /* 142 /*
143 * Hash related functions 143 * Hash related functions
144 * 144 *
145 * Every DMA-API request is saved into a struct dma_debug_entry. To 145 * Every DMA-API request is saved into a struct dma_debug_entry. To
146 * have quick access to these structs they are stored into a hash. 146 * have quick access to these structs they are stored into a hash.
147 */ 147 */
148 static int hash_fn(struct dma_debug_entry *entry) 148 static int hash_fn(struct dma_debug_entry *entry)
149 { 149 {
150 /* 150 /*
151 * Hash function is based on the dma address. 151 * Hash function is based on the dma address.
152 * We use bits 20-27 here as the index into the hash 152 * We use bits 20-27 here as the index into the hash
153 */ 153 */
154 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; 154 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
155 } 155 }
156 156
157 /* 157 /*
158 * Request exclusive access to a hash bucket for a given dma_debug_entry. 158 * Request exclusive access to a hash bucket for a given dma_debug_entry.
159 */ 159 */
160 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry, 160 static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
161 unsigned long *flags) 161 unsigned long *flags)
162 { 162 {
163 int idx = hash_fn(entry); 163 int idx = hash_fn(entry);
164 unsigned long __flags; 164 unsigned long __flags;
165 165
166 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags); 166 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
167 *flags = __flags; 167 *flags = __flags;
168 return &dma_entry_hash[idx]; 168 return &dma_entry_hash[idx];
169 } 169 }
170 170
171 /* 171 /*
172 * Give up exclusive access to the hash bucket 172 * Give up exclusive access to the hash bucket
173 */ 173 */
174 static void put_hash_bucket(struct hash_bucket *bucket, 174 static void put_hash_bucket(struct hash_bucket *bucket,
175 unsigned long *flags) 175 unsigned long *flags)
176 { 176 {
177 unsigned long __flags = *flags; 177 unsigned long __flags = *flags;
178 178
179 spin_unlock_irqrestore(&bucket->lock, __flags); 179 spin_unlock_irqrestore(&bucket->lock, __flags);
180 } 180 }
181 181
182 /* 182 /*
183 * Search a given entry in the hash bucket list 183 * Search a given entry in the hash bucket list
184 */ 184 */
185 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket, 185 static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
186 struct dma_debug_entry *ref) 186 struct dma_debug_entry *ref)
187 { 187 {
188 struct dma_debug_entry *entry; 188 struct dma_debug_entry *entry;
189 189
190 list_for_each_entry(entry, &bucket->list, list) { 190 list_for_each_entry(entry, &bucket->list, list) {
191 if ((entry->dev_addr == ref->dev_addr) && 191 if ((entry->dev_addr == ref->dev_addr) &&
192 (entry->dev == ref->dev)) 192 (entry->dev == ref->dev))
193 return entry; 193 return entry;
194 } 194 }
195 195
196 return NULL; 196 return NULL;
197 } 197 }
198 198
199 /* 199 /*
200 * Add an entry to a hash bucket 200 * Add an entry to a hash bucket
201 */ 201 */
202 static void hash_bucket_add(struct hash_bucket *bucket, 202 static void hash_bucket_add(struct hash_bucket *bucket,
203 struct dma_debug_entry *entry) 203 struct dma_debug_entry *entry)
204 { 204 {
205 list_add_tail(&entry->list, &bucket->list); 205 list_add_tail(&entry->list, &bucket->list);
206 } 206 }
207 207
208 /* 208 /*
209 * Remove entry from a hash bucket list 209 * Remove entry from a hash bucket list
210 */ 210 */
211 static void hash_bucket_del(struct dma_debug_entry *entry) 211 static void hash_bucket_del(struct dma_debug_entry *entry)
212 { 212 {
213 list_del(&entry->list); 213 list_del(&entry->list);
214 } 214 }
215 215
216 /* 216 /*
217 * Dump mapping entries for debugging purposes 217 * Dump mapping entries for debugging purposes
218 */ 218 */
219 void debug_dma_dump_mappings(struct device *dev) 219 void debug_dma_dump_mappings(struct device *dev)
220 { 220 {
221 int idx; 221 int idx;
222 222
223 for (idx = 0; idx < HASH_SIZE; idx++) { 223 for (idx = 0; idx < HASH_SIZE; idx++) {
224 struct hash_bucket *bucket = &dma_entry_hash[idx]; 224 struct hash_bucket *bucket = &dma_entry_hash[idx];
225 struct dma_debug_entry *entry; 225 struct dma_debug_entry *entry;
226 unsigned long flags; 226 unsigned long flags;
227 227
228 spin_lock_irqsave(&bucket->lock, flags); 228 spin_lock_irqsave(&bucket->lock, flags);
229 229
230 list_for_each_entry(entry, &bucket->list, list) { 230 list_for_each_entry(entry, &bucket->list, list) {
231 if (!dev || dev == entry->dev) { 231 if (!dev || dev == entry->dev) {
232 dev_info(entry->dev, 232 dev_info(entry->dev,
233 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n", 233 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
234 type2name[entry->type], idx, 234 type2name[entry->type], idx,
235 (unsigned long long)entry->paddr, 235 (unsigned long long)entry->paddr,
236 entry->dev_addr, entry->size, 236 entry->dev_addr, entry->size,
237 dir2name[entry->direction]); 237 dir2name[entry->direction]);
238 } 238 }
239 } 239 }
240 240
241 spin_unlock_irqrestore(&bucket->lock, flags); 241 spin_unlock_irqrestore(&bucket->lock, flags);
242 } 242 }
243 } 243 }
244 EXPORT_SYMBOL(debug_dma_dump_mappings); 244 EXPORT_SYMBOL(debug_dma_dump_mappings);
245 245
246 /* 246 /*
247 * Wrapper function for adding an entry to the hash. 247 * Wrapper function for adding an entry to the hash.
248 * This function takes care of locking itself. 248 * This function takes care of locking itself.
249 */ 249 */
250 static void add_dma_entry(struct dma_debug_entry *entry) 250 static void add_dma_entry(struct dma_debug_entry *entry)
251 { 251 {
252 struct hash_bucket *bucket; 252 struct hash_bucket *bucket;
253 unsigned long flags; 253 unsigned long flags;
254 254
255 bucket = get_hash_bucket(entry, &flags); 255 bucket = get_hash_bucket(entry, &flags);
256 hash_bucket_add(bucket, entry); 256 hash_bucket_add(bucket, entry);
257 put_hash_bucket(bucket, &flags); 257 put_hash_bucket(bucket, &flags);
258 } 258 }
259 259
260 /* struct dma_entry allocator 260 /* struct dma_entry allocator
261 * 261 *
262 * The next two functions implement the allocator for 262 * The next two functions implement the allocator for
263 * struct dma_debug_entries. 263 * struct dma_debug_entries.
264 */ 264 */
265 static struct dma_debug_entry *dma_entry_alloc(void) 265 static struct dma_debug_entry *dma_entry_alloc(void)
266 { 266 {
267 struct dma_debug_entry *entry = NULL; 267 struct dma_debug_entry *entry = NULL;
268 unsigned long flags; 268 unsigned long flags;
269 269
270 spin_lock_irqsave(&free_entries_lock, flags); 270 spin_lock_irqsave(&free_entries_lock, flags);
271 271
272 if (list_empty(&free_entries)) { 272 if (list_empty(&free_entries)) {
273 printk(KERN_ERR "DMA-API: debugging out of memory " 273 printk(KERN_ERR "DMA-API: debugging out of memory "
274 "- disabling\n"); 274 "- disabling\n");
275 global_disable = true; 275 global_disable = true;
276 goto out; 276 goto out;
277 } 277 }
278 278
279 entry = list_entry(free_entries.next, struct dma_debug_entry, list); 279 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
280 list_del(&entry->list); 280 list_del(&entry->list);
281 memset(entry, 0, sizeof(*entry)); 281 memset(entry, 0, sizeof(*entry));
282 282
283 #ifdef CONFIG_STACKTRACE 283 #ifdef CONFIG_STACKTRACE
284 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; 284 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
285 entry->stacktrace.entries = entry->st_entries; 285 entry->stacktrace.entries = entry->st_entries;
286 entry->stacktrace.skip = 2; 286 entry->stacktrace.skip = 2;
287 save_stack_trace(&entry->stacktrace); 287 save_stack_trace(&entry->stacktrace);
288 #endif 288 #endif
289 num_free_entries -= 1; 289 num_free_entries -= 1;
290 if (num_free_entries < min_free_entries) 290 if (num_free_entries < min_free_entries)
291 min_free_entries = num_free_entries; 291 min_free_entries = num_free_entries;
292 292
293 out: 293 out:
294 spin_unlock_irqrestore(&free_entries_lock, flags); 294 spin_unlock_irqrestore(&free_entries_lock, flags);
295 295
296 return entry; 296 return entry;
297 } 297 }
298 298
299 static void dma_entry_free(struct dma_debug_entry *entry) 299 static void dma_entry_free(struct dma_debug_entry *entry)
300 { 300 {
301 unsigned long flags; 301 unsigned long flags;
302 302
303 /* 303 /*
304 * add to beginning of the list - this way the entries are 304 * add to beginning of the list - this way the entries are
305 * more likely cache hot when they are reallocated. 305 * more likely cache hot when they are reallocated.
306 */ 306 */
307 spin_lock_irqsave(&free_entries_lock, flags); 307 spin_lock_irqsave(&free_entries_lock, flags);
308 list_add(&entry->list, &free_entries); 308 list_add(&entry->list, &free_entries);
309 num_free_entries += 1; 309 num_free_entries += 1;
310 spin_unlock_irqrestore(&free_entries_lock, flags); 310 spin_unlock_irqrestore(&free_entries_lock, flags);
311 } 311 }
312 312
313 /* 313 /*
314 * DMA-API debugging init code 314 * DMA-API debugging init code
315 * 315 *
316 * The init code does two things: 316 * The init code does two things:
317 * 1. Initialize core data structures 317 * 1. Initialize core data structures
318 * 2. Preallocate a given number of dma_debug_entry structs 318 * 2. Preallocate a given number of dma_debug_entry structs
319 */ 319 */
320 320
321 static int prealloc_memory(u32 num_entries) 321 static int prealloc_memory(u32 num_entries)
322 { 322 {
323 struct dma_debug_entry *entry, *next_entry; 323 struct dma_debug_entry *entry, *next_entry;
324 int i; 324 int i;
325 325
326 for (i = 0; i < num_entries; ++i) { 326 for (i = 0; i < num_entries; ++i) {
327 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 327 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
328 if (!entry) 328 if (!entry)
329 goto out_err; 329 goto out_err;
330 330
331 list_add_tail(&entry->list, &free_entries); 331 list_add_tail(&entry->list, &free_entries);
332 } 332 }
333 333
334 num_free_entries = num_entries; 334 num_free_entries = num_entries;
335 min_free_entries = num_entries; 335 min_free_entries = num_entries;
336 336
337 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n", 337 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
338 num_entries); 338 num_entries);
339 339
340 return 0; 340 return 0;
341 341
342 out_err: 342 out_err:
343 343
344 list_for_each_entry_safe(entry, next_entry, &free_entries, list) { 344 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
345 list_del(&entry->list); 345 list_del(&entry->list);
346 kfree(entry); 346 kfree(entry);
347 } 347 }
348 348
349 return -ENOMEM; 349 return -ENOMEM;
350 } 350 }
351 351
352 static int dma_debug_fs_init(void) 352 static int dma_debug_fs_init(void)
353 { 353 {
354 dma_debug_dent = debugfs_create_dir("dma-api", NULL); 354 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
355 if (!dma_debug_dent) { 355 if (!dma_debug_dent) {
356 printk(KERN_ERR "DMA-API: can not create debugfs directory\n"); 356 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
357 return -ENOMEM; 357 return -ENOMEM;
358 } 358 }
359 359
360 global_disable_dent = debugfs_create_bool("disabled", 0444, 360 global_disable_dent = debugfs_create_bool("disabled", 0444,
361 dma_debug_dent, 361 dma_debug_dent,
362 (u32 *)&global_disable); 362 (u32 *)&global_disable);
363 if (!global_disable_dent) 363 if (!global_disable_dent)
364 goto out_err; 364 goto out_err;
365 365
366 error_count_dent = debugfs_create_u32("error_count", 0444, 366 error_count_dent = debugfs_create_u32("error_count", 0444,
367 dma_debug_dent, &error_count); 367 dma_debug_dent, &error_count);
368 if (!error_count_dent) 368 if (!error_count_dent)
369 goto out_err; 369 goto out_err;
370 370
371 show_all_errors_dent = debugfs_create_u32("all_errors", 0644, 371 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
372 dma_debug_dent, 372 dma_debug_dent,
373 &show_all_errors); 373 &show_all_errors);
374 if (!show_all_errors_dent) 374 if (!show_all_errors_dent)
375 goto out_err; 375 goto out_err;
376 376
377 show_num_errors_dent = debugfs_create_u32("num_errors", 0644, 377 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
378 dma_debug_dent, 378 dma_debug_dent,
379 &show_num_errors); 379 &show_num_errors);
380 if (!show_num_errors_dent) 380 if (!show_num_errors_dent)
381 goto out_err; 381 goto out_err;
382 382
383 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444, 383 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
384 dma_debug_dent, 384 dma_debug_dent,
385 &num_free_entries); 385 &num_free_entries);
386 if (!num_free_entries_dent) 386 if (!num_free_entries_dent)
387 goto out_err; 387 goto out_err;
388 388
389 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444, 389 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
390 dma_debug_dent, 390 dma_debug_dent,
391 &min_free_entries); 391 &min_free_entries);
392 if (!min_free_entries_dent) 392 if (!min_free_entries_dent)
393 goto out_err; 393 goto out_err;
394 394
395 return 0; 395 return 0;
396 396
397 out_err: 397 out_err:
398 debugfs_remove_recursive(dma_debug_dent); 398 debugfs_remove_recursive(dma_debug_dent);
399 399
400 return -ENOMEM; 400 return -ENOMEM;
401 } 401 }
402 402
403 static int device_dma_allocations(struct device *dev) 403 static int device_dma_allocations(struct device *dev)
404 { 404 {
405 struct dma_debug_entry *entry; 405 struct dma_debug_entry *entry;
406 unsigned long flags; 406 unsigned long flags;
407 int count = 0, i; 407 int count = 0, i;
408 408
409 for (i = 0; i < HASH_SIZE; ++i) { 409 for (i = 0; i < HASH_SIZE; ++i) {
410 spin_lock_irqsave(&dma_entry_hash[i].lock, flags); 410 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
411 list_for_each_entry(entry, &dma_entry_hash[i].list, list) { 411 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
412 if (entry->dev == dev) 412 if (entry->dev == dev)
413 count += 1; 413 count += 1;
414 } 414 }
415 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags); 415 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
416 } 416 }
417 417
418 return count; 418 return count;
419 } 419 }
420 420
421 static int dma_debug_device_change(struct notifier_block *nb, 421 static int dma_debug_device_change(struct notifier_block *nb,
422 unsigned long action, void *data) 422 unsigned long action, void *data)
423 { 423 {
424 struct device *dev = data; 424 struct device *dev = data;
425 int count; 425 int count;
426 426
427 427
428 switch (action) { 428 switch (action) {
429 case BUS_NOTIFY_UNBIND_DRIVER: 429 case BUS_NOTIFY_UNBIND_DRIVER:
430 count = device_dma_allocations(dev); 430 count = device_dma_allocations(dev);
431 if (count == 0) 431 if (count == 0)
432 break; 432 break;
433 err_printk(dev, NULL, "DMA-API: device driver has pending " 433 err_printk(dev, NULL, "DMA-API: device driver has pending "
434 "DMA allocations while released from device " 434 "DMA allocations while released from device "
435 "[count=%d]\n", count); 435 "[count=%d]\n", count);
436 break; 436 break;
437 default: 437 default:
438 break; 438 break;
439 } 439 }
440 440
441 return 0; 441 return 0;
442 } 442 }
443 443
444 void dma_debug_add_bus(struct bus_type *bus) 444 void dma_debug_add_bus(struct bus_type *bus)
445 { 445 {
446 struct notifier_block *nb; 446 struct notifier_block *nb;
447 447
448 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); 448 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
449 if (nb == NULL) { 449 if (nb == NULL) {
450 printk(KERN_ERR "dma_debug_add_bus: out of memory\n"); 450 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
451 return; 451 return;
452 } 452 }
453 453
454 nb->notifier_call = dma_debug_device_change; 454 nb->notifier_call = dma_debug_device_change;
455 455
456 bus_register_notifier(bus, nb); 456 bus_register_notifier(bus, nb);
457 } 457 }
458 458
459 /* 459 /*
460 * Let the architectures decide how many entries should be preallocated. 460 * Let the architectures decide how many entries should be preallocated.
461 */ 461 */
462 void dma_debug_init(u32 num_entries) 462 void dma_debug_init(u32 num_entries)
463 { 463 {
464 int i; 464 int i;
465 465
466 if (global_disable) 466 if (global_disable)
467 return; 467 return;
468 468
469 for (i = 0; i < HASH_SIZE; ++i) { 469 for (i = 0; i < HASH_SIZE; ++i) {
470 INIT_LIST_HEAD(&dma_entry_hash[i].list); 470 INIT_LIST_HEAD(&dma_entry_hash[i].list);
471 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED; 471 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
472 } 472 }
473 473
474 if (dma_debug_fs_init() != 0) { 474 if (dma_debug_fs_init() != 0) {
475 printk(KERN_ERR "DMA-API: error creating debugfs entries " 475 printk(KERN_ERR "DMA-API: error creating debugfs entries "
476 "- disabling\n"); 476 "- disabling\n");
477 global_disable = true; 477 global_disable = true;
478 478
479 return; 479 return;
480 } 480 }
481 481
482 if (req_entries) 482 if (req_entries)
483 num_entries = req_entries; 483 num_entries = req_entries;
484 484
485 if (prealloc_memory(num_entries) != 0) { 485 if (prealloc_memory(num_entries) != 0) {
486 printk(KERN_ERR "DMA-API: debugging out of memory error " 486 printk(KERN_ERR "DMA-API: debugging out of memory error "
487 "- disabled\n"); 487 "- disabled\n");
488 global_disable = true; 488 global_disable = true;
489 489
490 return; 490 return;
491 } 491 }
492 492
493 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n"); 493 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
494 } 494 }
495 495
496 static __init int dma_debug_cmdline(char *str) 496 static __init int dma_debug_cmdline(char *str)
497 { 497 {
498 if (!str) 498 if (!str)
499 return -EINVAL; 499 return -EINVAL;
500 500
501 if (strncmp(str, "off", 3) == 0) { 501 if (strncmp(str, "off", 3) == 0) {
502 printk(KERN_INFO "DMA-API: debugging disabled on kernel " 502 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
503 "command line\n"); 503 "command line\n");
504 global_disable = true; 504 global_disable = true;
505 } 505 }
506 506
507 return 0; 507 return 0;
508 } 508 }
509 509
510 static __init int dma_debug_entries_cmdline(char *str) 510 static __init int dma_debug_entries_cmdline(char *str)
511 { 511 {
512 int res; 512 int res;
513 513
514 if (!str) 514 if (!str)
515 return -EINVAL; 515 return -EINVAL;
516 516
517 res = get_option(&str, &req_entries); 517 res = get_option(&str, &req_entries);
518 518
519 if (!res) 519 if (!res)
520 req_entries = 0; 520 req_entries = 0;
521 521
522 return 0; 522 return 0;
523 } 523 }
524 524
525 __setup("dma_debug=", dma_debug_cmdline); 525 __setup("dma_debug=", dma_debug_cmdline);
526 __setup("dma_debug_entries=", dma_debug_entries_cmdline); 526 __setup("dma_debug_entries=", dma_debug_entries_cmdline);
527 527
528 static void check_unmap(struct dma_debug_entry *ref) 528 static void check_unmap(struct dma_debug_entry *ref)
529 { 529 {
530 struct dma_debug_entry *entry; 530 struct dma_debug_entry *entry;
531 struct hash_bucket *bucket; 531 struct hash_bucket *bucket;
532 unsigned long flags; 532 unsigned long flags;
533 533
534 if (dma_mapping_error(ref->dev, ref->dev_addr)) 534 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
535 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
536 "to free an invalid DMA memory address\n");
535 return; 537 return;
538 }
536 539
537 bucket = get_hash_bucket(ref, &flags); 540 bucket = get_hash_bucket(ref, &flags);
538 entry = hash_bucket_find(bucket, ref); 541 entry = hash_bucket_find(bucket, ref);
539 542
540 if (!entry) { 543 if (!entry) {
541 err_printk(ref->dev, NULL, "DMA-API: device driver tries " 544 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
542 "to free DMA memory it has not allocated " 545 "to free DMA memory it has not allocated "
543 "[device address=0x%016llx] [size=%llu bytes]\n", 546 "[device address=0x%016llx] [size=%llu bytes]\n",
544 ref->dev_addr, ref->size); 547 ref->dev_addr, ref->size);
545 goto out; 548 goto out;
546 } 549 }
547 550
548 if (ref->size != entry->size) { 551 if (ref->size != entry->size) {
549 err_printk(ref->dev, entry, "DMA-API: device driver frees " 552 err_printk(ref->dev, entry, "DMA-API: device driver frees "
550 "DMA memory with different size " 553 "DMA memory with different size "
551 "[device address=0x%016llx] [map size=%llu bytes] " 554 "[device address=0x%016llx] [map size=%llu bytes] "
552 "[unmap size=%llu bytes]\n", 555 "[unmap size=%llu bytes]\n",
553 ref->dev_addr, entry->size, ref->size); 556 ref->dev_addr, entry->size, ref->size);
554 } 557 }
555 558
556 if (ref->type != entry->type) { 559 if (ref->type != entry->type) {
557 err_printk(ref->dev, entry, "DMA-API: device driver frees " 560 err_printk(ref->dev, entry, "DMA-API: device driver frees "
558 "DMA memory with wrong function " 561 "DMA memory with wrong function "
559 "[device address=0x%016llx] [size=%llu bytes] " 562 "[device address=0x%016llx] [size=%llu bytes] "
560 "[mapped as %s] [unmapped as %s]\n", 563 "[mapped as %s] [unmapped as %s]\n",
561 ref->dev_addr, ref->size, 564 ref->dev_addr, ref->size,
562 type2name[entry->type], type2name[ref->type]); 565 type2name[entry->type], type2name[ref->type]);
563 } else if ((entry->type == dma_debug_coherent) && 566 } else if ((entry->type == dma_debug_coherent) &&
564 (ref->paddr != entry->paddr)) { 567 (ref->paddr != entry->paddr)) {
565 err_printk(ref->dev, entry, "DMA-API: device driver frees " 568 err_printk(ref->dev, entry, "DMA-API: device driver frees "
566 "DMA memory with different CPU address " 569 "DMA memory with different CPU address "
567 "[device address=0x%016llx] [size=%llu bytes] " 570 "[device address=0x%016llx] [size=%llu bytes] "
568 "[cpu alloc address=%p] [cpu free address=%p]", 571 "[cpu alloc address=%p] [cpu free address=%p]",
569 ref->dev_addr, ref->size, 572 ref->dev_addr, ref->size,
570 (void *)entry->paddr, (void *)ref->paddr); 573 (void *)entry->paddr, (void *)ref->paddr);
571 } 574 }
572 575
573 if (ref->sg_call_ents && ref->type == dma_debug_sg && 576 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
574 ref->sg_call_ents != entry->sg_call_ents) { 577 ref->sg_call_ents != entry->sg_call_ents) {
575 err_printk(ref->dev, entry, "DMA-API: device driver frees " 578 err_printk(ref->dev, entry, "DMA-API: device driver frees "
576 "DMA sg list with different entry count " 579 "DMA sg list with different entry count "
577 "[map count=%d] [unmap count=%d]\n", 580 "[map count=%d] [unmap count=%d]\n",
578 entry->sg_call_ents, ref->sg_call_ents); 581 entry->sg_call_ents, ref->sg_call_ents);
579 } 582 }
580 583
581 /* 584 /*
582 * This may be no bug in reality - but most implementations of the 585 * This may be no bug in reality - but most implementations of the
583 * DMA API don't handle this properly, so check for it here 586 * DMA API don't handle this properly, so check for it here
584 */ 587 */
585 if (ref->direction != entry->direction) { 588 if (ref->direction != entry->direction) {
586 err_printk(ref->dev, entry, "DMA-API: device driver frees " 589 err_printk(ref->dev, entry, "DMA-API: device driver frees "
587 "DMA memory with different direction " 590 "DMA memory with different direction "
588 "[device address=0x%016llx] [size=%llu bytes] " 591 "[device address=0x%016llx] [size=%llu bytes] "
589 "[mapped with %s] [unmapped with %s]\n", 592 "[mapped with %s] [unmapped with %s]\n",
590 ref->dev_addr, ref->size, 593 ref->dev_addr, ref->size,
591 dir2name[entry->direction], 594 dir2name[entry->direction],
592 dir2name[ref->direction]); 595 dir2name[ref->direction]);
593 } 596 }
594 597
595 hash_bucket_del(entry); 598 hash_bucket_del(entry);
596 dma_entry_free(entry); 599 dma_entry_free(entry);
597 600
598 out: 601 out:
599 put_hash_bucket(bucket, &flags); 602 put_hash_bucket(bucket, &flags);
600 } 603 }
601 604
602 static void check_for_stack(struct device *dev, void *addr) 605 static void check_for_stack(struct device *dev, void *addr)
603 { 606 {
604 if (object_is_on_stack(addr)) 607 if (object_is_on_stack(addr))
605 err_printk(dev, NULL, "DMA-API: device driver maps memory from" 608 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
606 "stack [addr=%p]\n", addr); 609 "stack [addr=%p]\n", addr);
607 } 610 }
608 611
609 static inline bool overlap(void *addr, u64 size, void *start, void *end) 612 static inline bool overlap(void *addr, u64 size, void *start, void *end)
610 { 613 {
611 void *addr2 = (char *)addr + size; 614 void *addr2 = (char *)addr + size;
612 615
613 return ((addr >= start && addr < end) || 616 return ((addr >= start && addr < end) ||
614 (addr2 >= start && addr2 < end) || 617 (addr2 >= start && addr2 < end) ||
615 ((addr < start) && (addr2 >= end))); 618 ((addr < start) && (addr2 >= end)));
616 } 619 }
617 620
618 static void check_for_illegal_area(struct device *dev, void *addr, u64 size) 621 static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
619 { 622 {
620 if (overlap(addr, size, _text, _etext) || 623 if (overlap(addr, size, _text, _etext) ||
621 overlap(addr, size, __start_rodata, __end_rodata)) 624 overlap(addr, size, __start_rodata, __end_rodata))
622 err_printk(dev, NULL, "DMA-API: device driver maps " 625 err_printk(dev, NULL, "DMA-API: device driver maps "
623 "memory from kernel text or rodata " 626 "memory from kernel text or rodata "
624 "[addr=%p] [size=%llu]\n", addr, size); 627 "[addr=%p] [size=%llu]\n", addr, size);
625 } 628 }
626 629
627 static void check_sync(struct device *dev, dma_addr_t addr, 630 static void check_sync(struct device *dev, dma_addr_t addr,
628 u64 size, u64 offset, int direction, bool to_cpu) 631 u64 size, u64 offset, int direction, bool to_cpu)
629 { 632 {
630 struct dma_debug_entry ref = { 633 struct dma_debug_entry ref = {
631 .dev = dev, 634 .dev = dev,
632 .dev_addr = addr, 635 .dev_addr = addr,
633 .size = size, 636 .size = size,
634 .direction = direction, 637 .direction = direction,
635 }; 638 };
636 struct dma_debug_entry *entry; 639 struct dma_debug_entry *entry;
637 struct hash_bucket *bucket; 640 struct hash_bucket *bucket;
638 unsigned long flags; 641 unsigned long flags;
639 642
640 bucket = get_hash_bucket(&ref, &flags); 643 bucket = get_hash_bucket(&ref, &flags);
641 644
642 entry = hash_bucket_find(bucket, &ref); 645 entry = hash_bucket_find(bucket, &ref);
643 646
644 if (!entry) { 647 if (!entry) {
645 err_printk(dev, NULL, "DMA-API: device driver tries " 648 err_printk(dev, NULL, "DMA-API: device driver tries "
646 "to sync DMA memory it has not allocated " 649 "to sync DMA memory it has not allocated "
647 "[device address=0x%016llx] [size=%llu bytes]\n", 650 "[device address=0x%016llx] [size=%llu bytes]\n",
648 addr, size); 651 addr, size);
649 goto out; 652 goto out;
650 } 653 }
651 654
652 if ((offset + size) > entry->size) { 655 if ((offset + size) > entry->size) {
653 err_printk(dev, entry, "DMA-API: device driver syncs" 656 err_printk(dev, entry, "DMA-API: device driver syncs"
654 " DMA memory outside allocated range " 657 " DMA memory outside allocated range "
655 "[device address=0x%016llx] " 658 "[device address=0x%016llx] "
656 "[allocation size=%llu bytes] [sync offset=%llu] " 659 "[allocation size=%llu bytes] [sync offset=%llu] "
657 "[sync size=%llu]\n", entry->dev_addr, entry->size, 660 "[sync size=%llu]\n", entry->dev_addr, entry->size,
658 offset, size); 661 offset, size);
659 } 662 }
660 663
661 if (direction != entry->direction) { 664 if (direction != entry->direction) {
662 err_printk(dev, entry, "DMA-API: device driver syncs " 665 err_printk(dev, entry, "DMA-API: device driver syncs "
663 "DMA memory with different direction " 666 "DMA memory with different direction "
664 "[device address=0x%016llx] [size=%llu bytes] " 667 "[device address=0x%016llx] [size=%llu bytes] "
665 "[mapped with %s] [synced with %s]\n", 668 "[mapped with %s] [synced with %s]\n",
666 addr, entry->size, 669 addr, entry->size,
667 dir2name[entry->direction], 670 dir2name[entry->direction],
668 dir2name[direction]); 671 dir2name[direction]);
669 } 672 }
670 673
671 if (entry->direction == DMA_BIDIRECTIONAL) 674 if (entry->direction == DMA_BIDIRECTIONAL)
672 goto out; 675 goto out;
673 676
674 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && 677 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
675 !(direction == DMA_TO_DEVICE)) 678 !(direction == DMA_TO_DEVICE))
676 err_printk(dev, entry, "DMA-API: device driver syncs " 679 err_printk(dev, entry, "DMA-API: device driver syncs "
677 "device read-only DMA memory for cpu " 680 "device read-only DMA memory for cpu "
678 "[device address=0x%016llx] [size=%llu bytes] " 681 "[device address=0x%016llx] [size=%llu bytes] "
679 "[mapped with %s] [synced with %s]\n", 682 "[mapped with %s] [synced with %s]\n",
680 addr, entry->size, 683 addr, entry->size,
681 dir2name[entry->direction], 684 dir2name[entry->direction],
682 dir2name[direction]); 685 dir2name[direction]);
683 686
684 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && 687 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
685 !(direction == DMA_FROM_DEVICE)) 688 !(direction == DMA_FROM_DEVICE))
686 err_printk(dev, entry, "DMA-API: device driver syncs " 689 err_printk(dev, entry, "DMA-API: device driver syncs "
687 "device write-only DMA memory to device " 690 "device write-only DMA memory to device "
688 "[device address=0x%016llx] [size=%llu bytes] " 691 "[device address=0x%016llx] [size=%llu bytes] "
689 "[mapped with %s] [synced with %s]\n", 692 "[mapped with %s] [synced with %s]\n",
690 addr, entry->size, 693 addr, entry->size,
691 dir2name[entry->direction], 694 dir2name[entry->direction],
692 dir2name[direction]); 695 dir2name[direction]);
693 696
694 out: 697 out:
695 put_hash_bucket(bucket, &flags); 698 put_hash_bucket(bucket, &flags);
696 699
697 } 700 }
698 701
699 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, 702 void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
700 size_t size, int direction, dma_addr_t dma_addr, 703 size_t size, int direction, dma_addr_t dma_addr,
701 bool map_single) 704 bool map_single)
702 { 705 {
703 struct dma_debug_entry *entry; 706 struct dma_debug_entry *entry;
704 707
705 if (unlikely(global_disable)) 708 if (unlikely(global_disable))
706 return; 709 return;
707 710
708 if (unlikely(dma_mapping_error(dev, dma_addr))) 711 if (unlikely(dma_mapping_error(dev, dma_addr)))
709 return; 712 return;
710 713
711 entry = dma_entry_alloc(); 714 entry = dma_entry_alloc();
712 if (!entry) 715 if (!entry)
713 return; 716 return;
714 717
715 entry->dev = dev; 718 entry->dev = dev;
716 entry->type = dma_debug_page; 719 entry->type = dma_debug_page;
717 entry->paddr = page_to_phys(page) + offset; 720 entry->paddr = page_to_phys(page) + offset;
718 entry->dev_addr = dma_addr; 721 entry->dev_addr = dma_addr;
719 entry->size = size; 722 entry->size = size;
720 entry->direction = direction; 723 entry->direction = direction;
721 724
722 if (map_single) { 725 if (map_single) {
723 void *addr = ((char *)page_address(page)) + offset; 726 void *addr = ((char *)page_address(page)) + offset;
724 727
725 entry->type = dma_debug_single; 728 entry->type = dma_debug_single;
726 check_for_stack(dev, addr); 729 check_for_stack(dev, addr);
727 check_for_illegal_area(dev, addr, size); 730 check_for_illegal_area(dev, addr, size);
728 } 731 }
729 732
730 add_dma_entry(entry); 733 add_dma_entry(entry);
731 } 734 }
732 EXPORT_SYMBOL(debug_dma_map_page); 735 EXPORT_SYMBOL(debug_dma_map_page);
733 736
734 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr, 737 void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
735 size_t size, int direction, bool map_single) 738 size_t size, int direction, bool map_single)
736 { 739 {
737 struct dma_debug_entry ref = { 740 struct dma_debug_entry ref = {
738 .type = dma_debug_page, 741 .type = dma_debug_page,
739 .dev = dev, 742 .dev = dev,
740 .dev_addr = addr, 743 .dev_addr = addr,
741 .size = size, 744 .size = size,
742 .direction = direction, 745 .direction = direction,
743 }; 746 };
744 747
745 if (unlikely(global_disable)) 748 if (unlikely(global_disable))
746 return; 749 return;
747 750
748 if (map_single) 751 if (map_single)
749 ref.type = dma_debug_single; 752 ref.type = dma_debug_single;
750 753
751 check_unmap(&ref); 754 check_unmap(&ref);
752 } 755 }
753 EXPORT_SYMBOL(debug_dma_unmap_page); 756 EXPORT_SYMBOL(debug_dma_unmap_page);
754 757
755 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, 758 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
756 int nents, int mapped_ents, int direction) 759 int nents, int mapped_ents, int direction)
757 { 760 {
758 struct dma_debug_entry *entry; 761 struct dma_debug_entry *entry;
759 struct scatterlist *s; 762 struct scatterlist *s;
760 int i; 763 int i;
761 764
762 if (unlikely(global_disable)) 765 if (unlikely(global_disable))
763 return; 766 return;
764 767
765 for_each_sg(sg, s, mapped_ents, i) { 768 for_each_sg(sg, s, mapped_ents, i) {
766 entry = dma_entry_alloc(); 769 entry = dma_entry_alloc();
767 if (!entry) 770 if (!entry)
768 return; 771 return;
769 772
770 entry->type = dma_debug_sg; 773 entry->type = dma_debug_sg;
771 entry->dev = dev; 774 entry->dev = dev;
772 entry->paddr = sg_phys(s); 775 entry->paddr = sg_phys(s);
773 entry->size = s->length; 776 entry->size = s->length;
774 entry->dev_addr = s->dma_address; 777 entry->dev_addr = s->dma_address;
775 entry->direction = direction; 778 entry->direction = direction;
776 entry->sg_call_ents = nents; 779 entry->sg_call_ents = nents;
777 entry->sg_mapped_ents = mapped_ents; 780 entry->sg_mapped_ents = mapped_ents;
778 781
779 check_for_stack(dev, sg_virt(s)); 782 check_for_stack(dev, sg_virt(s));
780 check_for_illegal_area(dev, sg_virt(s), s->length); 783 check_for_illegal_area(dev, sg_virt(s), s->length);
781 784
782 add_dma_entry(entry); 785 add_dma_entry(entry);
783 } 786 }
784 } 787 }
785 EXPORT_SYMBOL(debug_dma_map_sg); 788 EXPORT_SYMBOL(debug_dma_map_sg);
786 789
787 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, 790 void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
788 int nelems, int dir) 791 int nelems, int dir)
789 { 792 {
790 struct dma_debug_entry *entry; 793 struct dma_debug_entry *entry;
791 struct scatterlist *s; 794 struct scatterlist *s;
792 int mapped_ents = 0, i; 795 int mapped_ents = 0, i;
793 unsigned long flags; 796 unsigned long flags;
794 797
795 if (unlikely(global_disable)) 798 if (unlikely(global_disable))
796 return; 799 return;
797 800
798 for_each_sg(sglist, s, nelems, i) { 801 for_each_sg(sglist, s, nelems, i) {
799 802
800 struct dma_debug_entry ref = { 803 struct dma_debug_entry ref = {
801 .type = dma_debug_sg, 804 .type = dma_debug_sg,
802 .dev = dev, 805 .dev = dev,
803 .paddr = sg_phys(s), 806 .paddr = sg_phys(s),
804 .dev_addr = s->dma_address, 807 .dev_addr = s->dma_address,
805 .size = s->length, 808 .size = s->length,
806 .direction = dir, 809 .direction = dir,
807 .sg_call_ents = 0, 810 .sg_call_ents = 0,
808 }; 811 };
809 812
810 if (mapped_ents && i >= mapped_ents) 813 if (mapped_ents && i >= mapped_ents)
811 break; 814 break;
812 815
813 if (mapped_ents == 0) { 816 if (mapped_ents == 0) {
814 struct hash_bucket *bucket; 817 struct hash_bucket *bucket;
815 ref.sg_call_ents = nelems; 818 ref.sg_call_ents = nelems;
816 bucket = get_hash_bucket(&ref, &flags); 819 bucket = get_hash_bucket(&ref, &flags);
817 entry = hash_bucket_find(bucket, &ref); 820 entry = hash_bucket_find(bucket, &ref);
818 if (entry) 821 if (entry)
819 mapped_ents = entry->sg_mapped_ents; 822 mapped_ents = entry->sg_mapped_ents;
820 put_hash_bucket(bucket, &flags); 823 put_hash_bucket(bucket, &flags);
821 } 824 }
822 825
823 check_unmap(&ref); 826 check_unmap(&ref);
824 } 827 }
825 } 828 }
826 EXPORT_SYMBOL(debug_dma_unmap_sg); 829 EXPORT_SYMBOL(debug_dma_unmap_sg);
827 830
828 void debug_dma_alloc_coherent(struct device *dev, size_t size, 831 void debug_dma_alloc_coherent(struct device *dev, size_t size,
829 dma_addr_t dma_addr, void *virt) 832 dma_addr_t dma_addr, void *virt)
830 { 833 {
831 struct dma_debug_entry *entry; 834 struct dma_debug_entry *entry;
832 835
833 if (unlikely(global_disable)) 836 if (unlikely(global_disable))
834 return; 837 return;
835 838
836 if (unlikely(virt == NULL)) 839 if (unlikely(virt == NULL))
837 return; 840 return;
838 841
839 entry = dma_entry_alloc(); 842 entry = dma_entry_alloc();
840 if (!entry) 843 if (!entry)
841 return; 844 return;
842 845
843 entry->type = dma_debug_coherent; 846 entry->type = dma_debug_coherent;
844 entry->dev = dev; 847 entry->dev = dev;
845 entry->paddr = virt_to_phys(virt); 848 entry->paddr = virt_to_phys(virt);
846 entry->size = size; 849 entry->size = size;
847 entry->dev_addr = dma_addr; 850 entry->dev_addr = dma_addr;
848 entry->direction = DMA_BIDIRECTIONAL; 851 entry->direction = DMA_BIDIRECTIONAL;
849 852
850 add_dma_entry(entry); 853 add_dma_entry(entry);
851 } 854 }
852 EXPORT_SYMBOL(debug_dma_alloc_coherent); 855 EXPORT_SYMBOL(debug_dma_alloc_coherent);
853 856
854 void debug_dma_free_coherent(struct device *dev, size_t size, 857 void debug_dma_free_coherent(struct device *dev, size_t size,
855 void *virt, dma_addr_t addr) 858 void *virt, dma_addr_t addr)
856 { 859 {
857 struct dma_debug_entry ref = { 860 struct dma_debug_entry ref = {
858 .type = dma_debug_coherent, 861 .type = dma_debug_coherent,
859 .dev = dev, 862 .dev = dev,
860 .paddr = virt_to_phys(virt), 863 .paddr = virt_to_phys(virt),
861 .dev_addr = addr, 864 .dev_addr = addr,
862 .size = size, 865 .size = size,
863 .direction = DMA_BIDIRECTIONAL, 866 .direction = DMA_BIDIRECTIONAL,
864 }; 867 };
865 868
866 if (unlikely(global_disable)) 869 if (unlikely(global_disable))
867 return; 870 return;
868 871
869 check_unmap(&ref); 872 check_unmap(&ref);
870 } 873 }
871 EXPORT_SYMBOL(debug_dma_free_coherent); 874 EXPORT_SYMBOL(debug_dma_free_coherent);
872 875
873 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, 876 void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
874 size_t size, int direction) 877 size_t size, int direction)
875 { 878 {
876 if (unlikely(global_disable)) 879 if (unlikely(global_disable))
877 return; 880 return;
878 881
879 check_sync(dev, dma_handle, size, 0, direction, true); 882 check_sync(dev, dma_handle, size, 0, direction, true);
880 } 883 }
881 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu); 884 EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
882 885
883 void debug_dma_sync_single_for_device(struct device *dev, 886 void debug_dma_sync_single_for_device(struct device *dev,
884 dma_addr_t dma_handle, size_t size, 887 dma_addr_t dma_handle, size_t size,
885 int direction) 888 int direction)
886 { 889 {
887 if (unlikely(global_disable)) 890 if (unlikely(global_disable))
888 return; 891 return;
889 892
890 check_sync(dev, dma_handle, size, 0, direction, false); 893 check_sync(dev, dma_handle, size, 0, direction, false);
891 } 894 }
892 EXPORT_SYMBOL(debug_dma_sync_single_for_device); 895 EXPORT_SYMBOL(debug_dma_sync_single_for_device);
893 896
894 void debug_dma_sync_single_range_for_cpu(struct device *dev, 897 void debug_dma_sync_single_range_for_cpu(struct device *dev,
895 dma_addr_t dma_handle, 898 dma_addr_t dma_handle,
896 unsigned long offset, size_t size, 899 unsigned long offset, size_t size,
897 int direction) 900 int direction)
898 { 901 {
899 if (unlikely(global_disable)) 902 if (unlikely(global_disable))
900 return; 903 return;
901 904
902 check_sync(dev, dma_handle, size, offset, direction, true); 905 check_sync(dev, dma_handle, size, offset, direction, true);
903 } 906 }
904 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu); 907 EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
905 908
906 void debug_dma_sync_single_range_for_device(struct device *dev, 909 void debug_dma_sync_single_range_for_device(struct device *dev,
907 dma_addr_t dma_handle, 910 dma_addr_t dma_handle,
908 unsigned long offset, 911 unsigned long offset,
909 size_t size, int direction) 912 size_t size, int direction)
910 { 913 {
911 if (unlikely(global_disable)) 914 if (unlikely(global_disable))
912 return; 915 return;
913 916
914 check_sync(dev, dma_handle, size, offset, direction, false); 917 check_sync(dev, dma_handle, size, offset, direction, false);
915 } 918 }
916 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device); 919 EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
917 920
918 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 921 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
919 int nelems, int direction) 922 int nelems, int direction)
920 { 923 {
921 struct scatterlist *s; 924 struct scatterlist *s;
922 int i; 925 int i;
923 926
924 if (unlikely(global_disable)) 927 if (unlikely(global_disable))
925 return; 928 return;
926 929
927 for_each_sg(sg, s, nelems, i) { 930 for_each_sg(sg, s, nelems, i) {
928 check_sync(dev, s->dma_address, s->dma_length, 0, 931 check_sync(dev, s->dma_address, s->dma_length, 0,
929 direction, true); 932 direction, true);
930 } 933 }
931 } 934 }
932 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); 935 EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
933 936
934 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 937 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
935 int nelems, int direction) 938 int nelems, int direction)
936 { 939 {
937 struct scatterlist *s; 940 struct scatterlist *s;
938 int i; 941 int i;
939 942
940 if (unlikely(global_disable)) 943 if (unlikely(global_disable))
941 return; 944 return;
942 945
943 for_each_sg(sg, s, nelems, i) { 946 for_each_sg(sg, s, nelems, i) {
944 check_sync(dev, s->dma_address, s->dma_length, 0, 947 check_sync(dev, s->dma_address, s->dma_length, 0,
945 direction, false); 948 direction, false);
946 } 949 }
947 } 950 }
948 EXPORT_SYMBOL(debug_dma_sync_sg_for_device); 951 EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
949 952
950 953