blob: 9561825c14a4297ffc1570d07530b36443a710a9 [file] [log] [blame]
Joerg Roedelf2f45e52009-01-09 12:19:52 +01001/*
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
Joerg Roedel972aa452009-01-09 14:19:54 +010020#include <linux/scatterlist.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010021#include <linux/dma-mapping.h>
David Woodhouse6c132d12009-01-19 16:52:39 +010022#include <linux/stacktrace.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010023#include <linux/dma-debug.h>
Joerg Roedel30dfa902009-01-09 12:34:49 +010024#include <linux/spinlock.h>
Joerg Roedel788dcfa2009-01-09 13:13:27 +010025#include <linux/debugfs.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020026#include <linux/uaccess.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010027#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010028#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010029#include <linux/sched.h>
Joerg Roedel8a6fc702009-05-22 21:23:13 +020030#include <linux/ctype.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010031#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010032#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010033
Joerg Roedel2e34bde2009-03-16 16:51:55 +010034#include <asm/sections.h>
35
Joerg Roedel30dfa902009-01-09 12:34:49 +010036#define HASH_SIZE 1024ULL
37#define HASH_FN_SHIFT 13
38#define HASH_FN_MASK (HASH_SIZE - 1)
39
Joerg Roedelf2f45e52009-01-09 12:19:52 +010040enum {
41 dma_debug_single,
42 dma_debug_page,
43 dma_debug_sg,
44 dma_debug_coherent,
45};
46
David Woodhouse6c132d12009-01-19 16:52:39 +010047#define DMA_DEBUG_STACKTRACE_ENTRIES 5
48
Joerg Roedelf2f45e52009-01-09 12:19:52 +010049struct dma_debug_entry {
50 struct list_head list;
51 struct device *dev;
52 int type;
53 phys_addr_t paddr;
54 u64 dev_addr;
55 u64 size;
56 int direction;
57 int sg_call_ents;
58 int sg_mapped_ents;
David Woodhouse6c132d12009-01-19 16:52:39 +010059#ifdef CONFIG_STACKTRACE
60 struct stack_trace stacktrace;
61 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
62#endif
Joerg Roedelf2f45e52009-01-09 12:19:52 +010063};
64
Joerg Roedel30dfa902009-01-09 12:34:49 +010065struct hash_bucket {
66 struct list_head list;
67 spinlock_t lock;
Joerg Roedel2d62ece2009-01-09 14:10:26 +010068} ____cacheline_aligned_in_smp;
Joerg Roedel30dfa902009-01-09 12:34:49 +010069
70/* Hash list to save the allocated dma addresses */
71static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010072/* List of pre-allocated dma_debug_entry's */
73static LIST_HEAD(free_entries);
74/* Lock for the list above */
75static DEFINE_SPINLOCK(free_entries_lock);
76
77/* Global disable flag - will be set in case of an error */
78static bool global_disable __read_mostly;
79
Joerg Roedel788dcfa2009-01-09 13:13:27 +010080/* Global error count */
81static u32 error_count;
82
83/* Global error show enable*/
84static u32 show_all_errors __read_mostly;
85/* Number of errors to show */
86static u32 show_num_errors = 1;
87
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010088static u32 num_free_entries;
89static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +090090static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +010091
Joerg Roedel59d3daa2009-01-09 13:01:56 +010092/* number of preallocated entries requested by kernel cmdline */
93static u32 req_entries;
94
Joerg Roedel788dcfa2009-01-09 13:13:27 +010095/* debugfs dentry's for the stuff above */
96static struct dentry *dma_debug_dent __read_mostly;
97static struct dentry *global_disable_dent __read_mostly;
98static struct dentry *error_count_dent __read_mostly;
99static struct dentry *show_all_errors_dent __read_mostly;
100static struct dentry *show_num_errors_dent __read_mostly;
101static struct dentry *num_free_entries_dent __read_mostly;
102static struct dentry *min_free_entries_dent __read_mostly;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200103static struct dentry *filter_dent __read_mostly;
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100104
Joerg Roedel2e507d82009-05-22 18:24:20 +0200105/* per-driver filter related state */
106
107#define NAME_MAX_LEN 64
108
109static char current_driver_name[NAME_MAX_LEN] __read_mostly;
110static struct device_driver *current_driver __read_mostly;
111
112static DEFINE_RWLOCK(driver_name_lock);
Joerg Roedel30dfa902009-01-09 12:34:49 +0100113
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100114static const char *type2name[4] = { "single", "page",
115 "scather-gather", "coherent" };
116
117static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
118 "DMA_FROM_DEVICE", "DMA_NONE" };
119
Joerg Roedeled888ae2009-05-22 17:16:04 +0200120/* little merge helper - remove it after the merge window */
121#ifndef BUS_NOTIFY_UNBOUND_DRIVER
122#define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
123#endif
124
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100125/*
126 * The access to some variables in this macro is racy. We can't use atomic_t
127 * here because all these variables are exported to debugfs. Some of them even
128 * writeable. This is also the reason why a lock won't help much. But anyway,
129 * the races are no big deal. Here is why:
130 *
131 * error_count: the addition is racy, but the worst thing that can happen is
132 * that we don't count some errors
133 * show_num_errors: the subtraction is racy. Also no big deal because in
134 * worst case this will result in one warning more in the
135 * system log than the user configured. This variable is
136 * writeable via debugfs.
137 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100138static inline void dump_entry_trace(struct dma_debug_entry *entry)
139{
140#ifdef CONFIG_STACKTRACE
141 if (entry) {
142 printk(KERN_WARNING "Mapped at:\n");
143 print_stack_trace(&entry->stacktrace, 0);
144 }
145#endif
146}
147
Joerg Roedel2e507d82009-05-22 18:24:20 +0200148static bool driver_filter(struct device *dev)
149{
150 /* driver filter off */
151 if (likely(!current_driver_name[0]))
152 return true;
153
154 /* driver filter on and initialized */
155 if (current_driver && dev->driver == current_driver)
156 return true;
157
158 /* driver filter on but not yet initialized */
159 if (!current_driver && current_driver_name[0]) {
160 struct device_driver *drv = get_driver(dev->driver);
161 unsigned long flags;
162 bool ret = false;
163
164 if (!drv)
165 return false;
166
167 /* lock to protect against change of current_driver_name */
168 read_lock_irqsave(&driver_name_lock, flags);
169
170 if (drv->name &&
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200171 strncmp(current_driver_name, drv->name,
172 NAME_MAX_LEN-1) == 0) {
Joerg Roedel2e507d82009-05-22 18:24:20 +0200173 current_driver = drv;
174 ret = true;
175 }
176
177 read_unlock_irqrestore(&driver_name_lock, flags);
178 put_driver(drv);
179
180 return ret;
181 }
182
183 return false;
184}
185
David Woodhouse6c132d12009-01-19 16:52:39 +0100186#define err_printk(dev, entry, format, arg...) do { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100187 error_count += 1; \
Joerg Roedel2e507d82009-05-22 18:24:20 +0200188 if (driver_filter(dev) && \
189 (show_all_errors || show_num_errors > 0)) { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100190 WARN(1, "%s %s: " format, \
191 dev_driver_string(dev), \
192 dev_name(dev) , ## arg); \
David Woodhouse6c132d12009-01-19 16:52:39 +0100193 dump_entry_trace(entry); \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100194 } \
195 if (!show_all_errors && show_num_errors > 0) \
196 show_num_errors -= 1; \
197 } while (0);
198
Joerg Roedel30dfa902009-01-09 12:34:49 +0100199/*
200 * Hash related functions
201 *
202 * Every DMA-API request is saved into a struct dma_debug_entry. To
203 * have quick access to these structs they are stored into a hash.
204 */
205static int hash_fn(struct dma_debug_entry *entry)
206{
207 /*
208 * Hash function is based on the dma address.
209 * We use bits 20-27 here as the index into the hash
210 */
211 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
212}
213
214/*
215 * Request exclusive access to a hash bucket for a given dma_debug_entry.
216 */
217static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
218 unsigned long *flags)
219{
220 int idx = hash_fn(entry);
221 unsigned long __flags;
222
223 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
224 *flags = __flags;
225 return &dma_entry_hash[idx];
226}
227
228/*
229 * Give up exclusive access to the hash bucket
230 */
231static void put_hash_bucket(struct hash_bucket *bucket,
232 unsigned long *flags)
233{
234 unsigned long __flags = *flags;
235
236 spin_unlock_irqrestore(&bucket->lock, __flags);
237}
238
239/*
240 * Search a given entry in the hash bucket list
241 */
242static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
243 struct dma_debug_entry *ref)
244{
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200245 struct dma_debug_entry *entry, *ret = NULL;
246 int matches = 0, match_lvl, last_lvl = 0;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100247
248 list_for_each_entry(entry, &bucket->list, list) {
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200249 if ((entry->dev_addr != ref->dev_addr) ||
250 (entry->dev != ref->dev))
251 continue;
252
253 /*
254 * Some drivers map the same physical address multiple
255 * times. Without a hardware IOMMU this results in the
256 * same device addresses being put into the dma-debug
257 * hash multiple times too. This can result in false
258 * positives being reported. Therfore we implement a
259 * best-fit algorithm here which returns the entry from
260 * the hash which fits best to the reference value
261 * instead of the first-fit.
262 */
263 matches += 1;
264 match_lvl = 0;
265 entry->size == ref->size ? ++match_lvl : match_lvl;
266 entry->type == ref->type ? ++match_lvl : match_lvl;
267 entry->direction == ref->direction ? ++match_lvl : match_lvl;
268
269 if (match_lvl == 3) {
270 /* perfect-fit - return the result */
Joerg Roedel30dfa902009-01-09 12:34:49 +0100271 return entry;
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200272 } else if (match_lvl > last_lvl) {
273 /*
274 * We found an entry that fits better then the
275 * previous one
276 */
277 last_lvl = match_lvl;
278 ret = entry;
279 }
Joerg Roedel30dfa902009-01-09 12:34:49 +0100280 }
281
Joerg Roedel7caf6a42009-06-05 12:01:35 +0200282 /*
283 * If we have multiple matches but no perfect-fit, just return
284 * NULL.
285 */
286 ret = (matches == 1) ? ret : NULL;
287
288 return ret;
Joerg Roedel30dfa902009-01-09 12:34:49 +0100289}
290
291/*
292 * Add an entry to a hash bucket
293 */
294static void hash_bucket_add(struct hash_bucket *bucket,
295 struct dma_debug_entry *entry)
296{
297 list_add_tail(&entry->list, &bucket->list);
298}
299
300/*
301 * Remove entry from a hash bucket list
302 */
303static void hash_bucket_del(struct dma_debug_entry *entry)
304{
305 list_del(&entry->list);
306}
307
308/*
David Woodhouseac26c182009-02-12 16:19:13 +0100309 * Dump mapping entries for debugging purposes
310 */
311void debug_dma_dump_mappings(struct device *dev)
312{
313 int idx;
314
315 for (idx = 0; idx < HASH_SIZE; idx++) {
316 struct hash_bucket *bucket = &dma_entry_hash[idx];
317 struct dma_debug_entry *entry;
318 unsigned long flags;
319
320 spin_lock_irqsave(&bucket->lock, flags);
321
322 list_for_each_entry(entry, &bucket->list, list) {
323 if (!dev || dev == entry->dev) {
324 dev_info(entry->dev,
325 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
326 type2name[entry->type], idx,
327 (unsigned long long)entry->paddr,
328 entry->dev_addr, entry->size,
329 dir2name[entry->direction]);
330 }
331 }
332
333 spin_unlock_irqrestore(&bucket->lock, flags);
334 }
335}
336EXPORT_SYMBOL(debug_dma_dump_mappings);
337
338/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100339 * Wrapper function for adding an entry to the hash.
340 * This function takes care of locking itself.
341 */
342static void add_dma_entry(struct dma_debug_entry *entry)
343{
344 struct hash_bucket *bucket;
345 unsigned long flags;
346
347 bucket = get_hash_bucket(entry, &flags);
348 hash_bucket_add(bucket, entry);
349 put_hash_bucket(bucket, &flags);
350}
351
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900352static struct dma_debug_entry *__dma_entry_alloc(void)
353{
354 struct dma_debug_entry *entry;
355
356 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
357 list_del(&entry->list);
358 memset(entry, 0, sizeof(*entry));
359
360 num_free_entries -= 1;
361 if (num_free_entries < min_free_entries)
362 min_free_entries = num_free_entries;
363
364 return entry;
365}
366
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100367/* struct dma_entry allocator
368 *
369 * The next two functions implement the allocator for
370 * struct dma_debug_entries.
371 */
372static struct dma_debug_entry *dma_entry_alloc(void)
373{
374 struct dma_debug_entry *entry = NULL;
375 unsigned long flags;
376
377 spin_lock_irqsave(&free_entries_lock, flags);
378
379 if (list_empty(&free_entries)) {
380 printk(KERN_ERR "DMA-API: debugging out of memory "
381 "- disabling\n");
382 global_disable = true;
383 goto out;
384 }
385
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900386 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100387
David Woodhouse6c132d12009-01-19 16:52:39 +0100388#ifdef CONFIG_STACKTRACE
389 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
390 entry->stacktrace.entries = entry->st_entries;
391 entry->stacktrace.skip = 2;
392 save_stack_trace(&entry->stacktrace);
393#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100394
395out:
396 spin_unlock_irqrestore(&free_entries_lock, flags);
397
398 return entry;
399}
400
401static void dma_entry_free(struct dma_debug_entry *entry)
402{
403 unsigned long flags;
404
405 /*
406 * add to beginning of the list - this way the entries are
407 * more likely cache hot when they are reallocated.
408 */
409 spin_lock_irqsave(&free_entries_lock, flags);
410 list_add(&entry->list, &free_entries);
411 num_free_entries += 1;
412 spin_unlock_irqrestore(&free_entries_lock, flags);
413}
414
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900415int dma_debug_resize_entries(u32 num_entries)
416{
417 int i, delta, ret = 0;
418 unsigned long flags;
419 struct dma_debug_entry *entry;
420 LIST_HEAD(tmp);
421
422 spin_lock_irqsave(&free_entries_lock, flags);
423
424 if (nr_total_entries < num_entries) {
425 delta = num_entries - nr_total_entries;
426
427 spin_unlock_irqrestore(&free_entries_lock, flags);
428
429 for (i = 0; i < delta; i++) {
430 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
431 if (!entry)
432 break;
433
434 list_add_tail(&entry->list, &tmp);
435 }
436
437 spin_lock_irqsave(&free_entries_lock, flags);
438
439 list_splice(&tmp, &free_entries);
440 nr_total_entries += i;
441 num_free_entries += i;
442 } else {
443 delta = nr_total_entries - num_entries;
444
445 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
446 entry = __dma_entry_alloc();
447 kfree(entry);
448 }
449
450 nr_total_entries -= i;
451 }
452
453 if (nr_total_entries != num_entries)
454 ret = 1;
455
456 spin_unlock_irqrestore(&free_entries_lock, flags);
457
458 return ret;
459}
460EXPORT_SYMBOL(dma_debug_resize_entries);
461
Joerg Roedel6bf07872009-01-09 12:54:42 +0100462/*
463 * DMA-API debugging init code
464 *
465 * The init code does two things:
466 * 1. Initialize core data structures
467 * 2. Preallocate a given number of dma_debug_entry structs
468 */
469
470static int prealloc_memory(u32 num_entries)
471{
472 struct dma_debug_entry *entry, *next_entry;
473 int i;
474
475 for (i = 0; i < num_entries; ++i) {
476 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
477 if (!entry)
478 goto out_err;
479
480 list_add_tail(&entry->list, &free_entries);
481 }
482
483 num_free_entries = num_entries;
484 min_free_entries = num_entries;
485
486 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
487 num_entries);
488
489 return 0;
490
491out_err:
492
493 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
494 list_del(&entry->list);
495 kfree(entry);
496 }
497
498 return -ENOMEM;
499}
500
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200501static ssize_t filter_read(struct file *file, char __user *user_buf,
502 size_t count, loff_t *ppos)
503{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200504 char buf[NAME_MAX_LEN + 1];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200505 unsigned long flags;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200506 int len;
507
508 if (!current_driver_name[0])
509 return 0;
510
511 /*
512 * We can't copy to userspace directly because current_driver_name can
513 * only be read under the driver_name_lock with irqs disabled. So
514 * create a temporary copy first.
515 */
516 read_lock_irqsave(&driver_name_lock, flags);
517 len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
518 read_unlock_irqrestore(&driver_name_lock, flags);
519
520 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
521}
522
523static ssize_t filter_write(struct file *file, const char __user *userbuf,
524 size_t count, loff_t *ppos)
525{
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200526 char buf[NAME_MAX_LEN];
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200527 unsigned long flags;
528 size_t len;
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200529 int i;
530
531 /*
532 * We can't copy from userspace directly. Access to
533 * current_driver_name is protected with a write_lock with irqs
534 * disabled. Since copy_from_user can fault and may sleep we
535 * need to copy to temporary buffer first
536 */
Joerg Roedelc17e2cf2009-06-08 15:19:29 +0200537 len = min(count, NAME_MAX_LEN - 1);
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200538 if (copy_from_user(buf, userbuf, len))
539 return -EFAULT;
540
541 buf[len] = 0;
542
543 write_lock_irqsave(&driver_name_lock, flags);
544
Joerg Roedel312325092009-06-08 15:07:08 +0200545 /*
546 * Now handle the string we got from userspace very carefully.
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200547 * The rules are:
548 * - only use the first token we got
549 * - token delimiter is everything looking like a space
550 * character (' ', '\n', '\t' ...)
551 *
552 */
553 if (!isalnum(buf[0])) {
554 /*
Joerg Roedel312325092009-06-08 15:07:08 +0200555 * If the first character userspace gave us is not
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200556 * alphanumerical then assume the filter should be
557 * switched off.
558 */
559 if (current_driver_name[0])
560 printk(KERN_INFO "DMA-API: switching off dma-debug "
561 "driver filter\n");
562 current_driver_name[0] = 0;
563 current_driver = NULL;
564 goto out_unlock;
565 }
566
567 /*
568 * Now parse out the first token and use it as the name for the
569 * driver to filter for.
570 */
571 for (i = 0; i < NAME_MAX_LEN; ++i) {
572 current_driver_name[i] = buf[i];
573 if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
574 break;
575 }
576 current_driver_name[i] = 0;
577 current_driver = NULL;
578
579 printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n",
580 current_driver_name);
581
582out_unlock:
583 write_unlock_irqrestore(&driver_name_lock, flags);
584
585 return count;
586}
587
588const struct file_operations filter_fops = {
589 .read = filter_read,
590 .write = filter_write,
591};
592
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100593static int dma_debug_fs_init(void)
594{
595 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
596 if (!dma_debug_dent) {
597 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
598 return -ENOMEM;
599 }
600
601 global_disable_dent = debugfs_create_bool("disabled", 0444,
602 dma_debug_dent,
603 (u32 *)&global_disable);
604 if (!global_disable_dent)
605 goto out_err;
606
607 error_count_dent = debugfs_create_u32("error_count", 0444,
608 dma_debug_dent, &error_count);
609 if (!error_count_dent)
610 goto out_err;
611
612 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
613 dma_debug_dent,
614 &show_all_errors);
615 if (!show_all_errors_dent)
616 goto out_err;
617
618 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
619 dma_debug_dent,
620 &show_num_errors);
621 if (!show_num_errors_dent)
622 goto out_err;
623
624 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
625 dma_debug_dent,
626 &num_free_entries);
627 if (!num_free_entries_dent)
628 goto out_err;
629
630 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
631 dma_debug_dent,
632 &min_free_entries);
633 if (!min_free_entries_dent)
634 goto out_err;
635
Joerg Roedel8a6fc702009-05-22 21:23:13 +0200636 filter_dent = debugfs_create_file("driver_filter", 0644,
637 dma_debug_dent, NULL, &filter_fops);
638 if (!filter_dent)
639 goto out_err;
640
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100641 return 0;
642
643out_err:
644 debugfs_remove_recursive(dma_debug_dent);
645
646 return -ENOMEM;
647}
648
Joerg Roedeled888ae2009-05-22 17:16:04 +0200649static int device_dma_allocations(struct device *dev)
650{
651 struct dma_debug_entry *entry;
652 unsigned long flags;
653 int count = 0, i;
654
655 for (i = 0; i < HASH_SIZE; ++i) {
656 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
657 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
658 if (entry->dev == dev)
659 count += 1;
660 }
661 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
662 }
663
664 return count;
665}
666
667static int dma_debug_device_change(struct notifier_block *nb,
668 unsigned long action, void *data)
669{
670 struct device *dev = data;
671 int count;
672
673
674 switch (action) {
675 case BUS_NOTIFY_UNBOUND_DRIVER:
676 count = device_dma_allocations(dev);
677 if (count == 0)
678 break;
679 err_printk(dev, NULL, "DMA-API: device driver has pending "
680 "DMA allocations while released from device "
681 "[count=%d]\n", count);
682 break;
683 default:
684 break;
685 }
686
687 return 0;
688}
689
Joerg Roedel41531c82009-03-16 17:32:14 +0100690void dma_debug_add_bus(struct bus_type *bus)
691{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200692 struct notifier_block *nb;
693
694 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
695 if (nb == NULL) {
696 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
697 return;
698 }
699
700 nb->notifier_call = dma_debug_device_change;
701
702 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100703}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100704
Joerg Roedel6bf07872009-01-09 12:54:42 +0100705/*
706 * Let the architectures decide how many entries should be preallocated.
707 */
708void dma_debug_init(u32 num_entries)
709{
710 int i;
711
712 if (global_disable)
713 return;
714
715 for (i = 0; i < HASH_SIZE; ++i) {
716 INIT_LIST_HEAD(&dma_entry_hash[i].list);
717 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
718 }
719
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100720 if (dma_debug_fs_init() != 0) {
721 printk(KERN_ERR "DMA-API: error creating debugfs entries "
722 "- disabling\n");
723 global_disable = true;
724
725 return;
726 }
727
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100728 if (req_entries)
729 num_entries = req_entries;
730
Joerg Roedel6bf07872009-01-09 12:54:42 +0100731 if (prealloc_memory(num_entries) != 0) {
732 printk(KERN_ERR "DMA-API: debugging out of memory error "
733 "- disabled\n");
734 global_disable = true;
735
736 return;
737 }
738
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900739 nr_total_entries = num_free_entries;
740
Joerg Roedel6bf07872009-01-09 12:54:42 +0100741 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
742}
743
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100744static __init int dma_debug_cmdline(char *str)
745{
746 if (!str)
747 return -EINVAL;
748
749 if (strncmp(str, "off", 3) == 0) {
750 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
751 "command line\n");
752 global_disable = true;
753 }
754
755 return 0;
756}
757
758static __init int dma_debug_entries_cmdline(char *str)
759{
760 int res;
761
762 if (!str)
763 return -EINVAL;
764
765 res = get_option(&str, &req_entries);
766
767 if (!res)
768 req_entries = 0;
769
770 return 0;
771}
772
773__setup("dma_debug=", dma_debug_cmdline);
774__setup("dma_debug_entries=", dma_debug_entries_cmdline);
775
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100776static void check_unmap(struct dma_debug_entry *ref)
777{
778 struct dma_debug_entry *entry;
779 struct hash_bucket *bucket;
780 unsigned long flags;
781
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900782 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
783 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
784 "to free an invalid DMA memory address\n");
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100785 return;
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900786 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100787
788 bucket = get_hash_bucket(ref, &flags);
789 entry = hash_bucket_find(bucket, ref);
790
791 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100792 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100793 "to free DMA memory it has not allocated "
794 "[device address=0x%016llx] [size=%llu bytes]\n",
795 ref->dev_addr, ref->size);
796 goto out;
797 }
798
799 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100800 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100801 "DMA memory with different size "
802 "[device address=0x%016llx] [map size=%llu bytes] "
803 "[unmap size=%llu bytes]\n",
804 ref->dev_addr, entry->size, ref->size);
805 }
806
807 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100808 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100809 "DMA memory with wrong function "
810 "[device address=0x%016llx] [size=%llu bytes] "
811 "[mapped as %s] [unmapped as %s]\n",
812 ref->dev_addr, ref->size,
813 type2name[entry->type], type2name[ref->type]);
814 } else if ((entry->type == dma_debug_coherent) &&
815 (ref->paddr != entry->paddr)) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100816 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100817 "DMA memory with different CPU address "
818 "[device address=0x%016llx] [size=%llu bytes] "
819 "[cpu alloc address=%p] [cpu free address=%p]",
820 ref->dev_addr, ref->size,
821 (void *)entry->paddr, (void *)ref->paddr);
822 }
823
824 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
825 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100826 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100827 "DMA sg list with different entry count "
828 "[map count=%d] [unmap count=%d]\n",
829 entry->sg_call_ents, ref->sg_call_ents);
830 }
831
832 /*
833 * This may be no bug in reality - but most implementations of the
834 * DMA API don't handle this properly, so check for it here
835 */
836 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100837 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100838 "DMA memory with different direction "
839 "[device address=0x%016llx] [size=%llu bytes] "
840 "[mapped with %s] [unmapped with %s]\n",
841 ref->dev_addr, ref->size,
842 dir2name[entry->direction],
843 dir2name[ref->direction]);
844 }
845
846 hash_bucket_del(entry);
847 dma_entry_free(entry);
848
849out:
850 put_hash_bucket(bucket, &flags);
851}
852
853static void check_for_stack(struct device *dev, void *addr)
854{
855 if (object_is_on_stack(addr))
David Woodhouse6c132d12009-01-19 16:52:39 +0100856 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
857 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100858}
859
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100860static inline bool overlap(void *addr, u64 size, void *start, void *end)
861{
862 void *addr2 = (char *)addr + size;
863
864 return ((addr >= start && addr < end) ||
865 (addr2 >= start && addr2 < end) ||
866 ((addr < start) && (addr2 >= end)));
867}
868
869static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
870{
871 if (overlap(addr, size, _text, _etext) ||
872 overlap(addr, size, __start_rodata, __end_rodata))
873 err_printk(dev, NULL, "DMA-API: device driver maps "
874 "memory from kernel text or rodata "
875 "[addr=%p] [size=%llu]\n", addr, size);
876}
877
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100878static void check_sync(struct device *dev, dma_addr_t addr,
879 u64 size, u64 offset, int direction, bool to_cpu)
880{
881 struct dma_debug_entry ref = {
882 .dev = dev,
883 .dev_addr = addr,
884 .size = size,
885 .direction = direction,
886 };
887 struct dma_debug_entry *entry;
888 struct hash_bucket *bucket;
889 unsigned long flags;
890
891 bucket = get_hash_bucket(&ref, &flags);
892
893 entry = hash_bucket_find(bucket, &ref);
894
895 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100896 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100897 "to sync DMA memory it has not allocated "
898 "[device address=0x%016llx] [size=%llu bytes]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700899 (unsigned long long)addr, size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100900 goto out;
901 }
902
903 if ((offset + size) > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100904 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100905 " DMA memory outside allocated range "
906 "[device address=0x%016llx] "
907 "[allocation size=%llu bytes] [sync offset=%llu] "
908 "[sync size=%llu]\n", entry->dev_addr, entry->size,
909 offset, size);
910 }
911
912 if (direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100913 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100914 "DMA memory with different direction "
915 "[device address=0x%016llx] [size=%llu bytes] "
916 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700917 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100918 dir2name[entry->direction],
919 dir2name[direction]);
920 }
921
922 if (entry->direction == DMA_BIDIRECTIONAL)
923 goto out;
924
925 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
926 !(direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100927 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100928 "device read-only DMA memory for cpu "
929 "[device address=0x%016llx] [size=%llu bytes] "
930 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700931 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100932 dir2name[entry->direction],
933 dir2name[direction]);
934
935 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
936 !(direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100937 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100938 "device write-only DMA memory to device "
939 "[device address=0x%016llx] [size=%llu bytes] "
940 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700941 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100942 dir2name[entry->direction],
943 dir2name[direction]);
944
945out:
946 put_hash_bucket(bucket, &flags);
947
948}
949
Joerg Roedelf62bc982009-01-09 14:14:49 +0100950void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
951 size_t size, int direction, dma_addr_t dma_addr,
952 bool map_single)
953{
954 struct dma_debug_entry *entry;
955
956 if (unlikely(global_disable))
957 return;
958
959 if (unlikely(dma_mapping_error(dev, dma_addr)))
960 return;
961
962 entry = dma_entry_alloc();
963 if (!entry)
964 return;
965
966 entry->dev = dev;
967 entry->type = dma_debug_page;
968 entry->paddr = page_to_phys(page) + offset;
969 entry->dev_addr = dma_addr;
970 entry->size = size;
971 entry->direction = direction;
972
Joerg Roedel9537a482009-03-23 15:35:08 +0100973 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +0100974 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +0100975
976 if (!PageHighMem(page)) {
977 void *addr = ((char *)page_address(page)) + offset;
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100978 check_for_stack(dev, addr);
979 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +0100980 }
981
982 add_dma_entry(entry);
983}
984EXPORT_SYMBOL(debug_dma_map_page);
985
986void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
987 size_t size, int direction, bool map_single)
988{
989 struct dma_debug_entry ref = {
990 .type = dma_debug_page,
991 .dev = dev,
992 .dev_addr = addr,
993 .size = size,
994 .direction = direction,
995 };
996
997 if (unlikely(global_disable))
998 return;
999
1000 if (map_single)
1001 ref.type = dma_debug_single;
1002
1003 check_unmap(&ref);
1004}
1005EXPORT_SYMBOL(debug_dma_unmap_page);
1006
Joerg Roedel972aa452009-01-09 14:19:54 +01001007void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
1008 int nents, int mapped_ents, int direction)
1009{
1010 struct dma_debug_entry *entry;
1011 struct scatterlist *s;
1012 int i;
1013
1014 if (unlikely(global_disable))
1015 return;
1016
1017 for_each_sg(sg, s, mapped_ents, i) {
1018 entry = dma_entry_alloc();
1019 if (!entry)
1020 return;
1021
1022 entry->type = dma_debug_sg;
1023 entry->dev = dev;
1024 entry->paddr = sg_phys(s);
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001025 entry->size = sg_dma_len(s);
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001026 entry->dev_addr = sg_dma_address(s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001027 entry->direction = direction;
1028 entry->sg_call_ents = nents;
1029 entry->sg_mapped_ents = mapped_ents;
1030
Joerg Roedel9537a482009-03-23 15:35:08 +01001031 if (!PageHighMem(sg_page(s))) {
1032 check_for_stack(dev, sg_virt(s));
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001033 check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
Joerg Roedel9537a482009-03-23 15:35:08 +01001034 }
Joerg Roedel972aa452009-01-09 14:19:54 +01001035
1036 add_dma_entry(entry);
1037 }
1038}
1039EXPORT_SYMBOL(debug_dma_map_sg);
1040
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001041static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s)
1042{
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001043 struct dma_debug_entry *entry, ref;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001044 struct hash_bucket *bucket;
1045 unsigned long flags;
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001046 int mapped_ents;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001047
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001048 ref.dev = dev;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001049 ref.dev_addr = sg_dma_address(s);
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001050 ref.size = sg_dma_len(s),
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001051
Joerg Roedelc17e2cf2009-06-08 15:19:29 +02001052 bucket = get_hash_bucket(&ref, &flags);
1053 entry = hash_bucket_find(bucket, &ref);
1054 mapped_ents = 0;
1055
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001056 if (entry)
1057 mapped_ents = entry->sg_mapped_ents;
1058 put_hash_bucket(bucket, &flags);
1059
1060 return mapped_ents;
1061}
1062
Joerg Roedel972aa452009-01-09 14:19:54 +01001063void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
1064 int nelems, int dir)
1065{
Joerg Roedel972aa452009-01-09 14:19:54 +01001066 struct scatterlist *s;
1067 int mapped_ents = 0, i;
Joerg Roedel972aa452009-01-09 14:19:54 +01001068
1069 if (unlikely(global_disable))
1070 return;
1071
1072 for_each_sg(sglist, s, nelems, i) {
1073
1074 struct dma_debug_entry ref = {
1075 .type = dma_debug_sg,
1076 .dev = dev,
1077 .paddr = sg_phys(s),
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001078 .dev_addr = sg_dma_address(s),
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001079 .size = sg_dma_len(s),
Joerg Roedel972aa452009-01-09 14:19:54 +01001080 .direction = dir,
1081 .sg_call_ents = 0,
1082 };
1083
1084 if (mapped_ents && i >= mapped_ents)
1085 break;
1086
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001087 if (!i) {
Joerg Roedel972aa452009-01-09 14:19:54 +01001088 ref.sg_call_ents = nelems;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001089 mapped_ents = get_nr_mapped_entries(dev, s);
Joerg Roedel972aa452009-01-09 14:19:54 +01001090 }
1091
1092 check_unmap(&ref);
1093 }
1094}
1095EXPORT_SYMBOL(debug_dma_unmap_sg);
1096
Joerg Roedel6bfd4492009-01-09 14:38:50 +01001097void debug_dma_alloc_coherent(struct device *dev, size_t size,
1098 dma_addr_t dma_addr, void *virt)
1099{
1100 struct dma_debug_entry *entry;
1101
1102 if (unlikely(global_disable))
1103 return;
1104
1105 if (unlikely(virt == NULL))
1106 return;
1107
1108 entry = dma_entry_alloc();
1109 if (!entry)
1110 return;
1111
1112 entry->type = dma_debug_coherent;
1113 entry->dev = dev;
1114 entry->paddr = virt_to_phys(virt);
1115 entry->size = size;
1116 entry->dev_addr = dma_addr;
1117 entry->direction = DMA_BIDIRECTIONAL;
1118
1119 add_dma_entry(entry);
1120}
1121EXPORT_SYMBOL(debug_dma_alloc_coherent);
1122
1123void debug_dma_free_coherent(struct device *dev, size_t size,
1124 void *virt, dma_addr_t addr)
1125{
1126 struct dma_debug_entry ref = {
1127 .type = dma_debug_coherent,
1128 .dev = dev,
1129 .paddr = virt_to_phys(virt),
1130 .dev_addr = addr,
1131 .size = size,
1132 .direction = DMA_BIDIRECTIONAL,
1133 };
1134
1135 if (unlikely(global_disable))
1136 return;
1137
1138 check_unmap(&ref);
1139}
1140EXPORT_SYMBOL(debug_dma_free_coherent);
1141
Joerg Roedelb9d23172009-01-09 14:43:04 +01001142void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
1143 size_t size, int direction)
1144{
1145 if (unlikely(global_disable))
1146 return;
1147
1148 check_sync(dev, dma_handle, size, 0, direction, true);
1149}
1150EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
1151
1152void debug_dma_sync_single_for_device(struct device *dev,
1153 dma_addr_t dma_handle, size_t size,
1154 int direction)
1155{
1156 if (unlikely(global_disable))
1157 return;
1158
1159 check_sync(dev, dma_handle, size, 0, direction, false);
1160}
1161EXPORT_SYMBOL(debug_dma_sync_single_for_device);
1162
Joerg Roedel948408b2009-01-09 14:55:38 +01001163void debug_dma_sync_single_range_for_cpu(struct device *dev,
1164 dma_addr_t dma_handle,
1165 unsigned long offset, size_t size,
1166 int direction)
1167{
1168 if (unlikely(global_disable))
1169 return;
1170
1171 check_sync(dev, dma_handle, size, offset, direction, true);
1172}
1173EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
1174
1175void debug_dma_sync_single_range_for_device(struct device *dev,
1176 dma_addr_t dma_handle,
1177 unsigned long offset,
1178 size_t size, int direction)
1179{
1180 if (unlikely(global_disable))
1181 return;
1182
1183 check_sync(dev, dma_handle, size, offset, direction, false);
1184}
1185EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
1186
Joerg Roedela31fba52009-01-09 15:01:12 +01001187void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1188 int nelems, int direction)
1189{
1190 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001191 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001192
1193 if (unlikely(global_disable))
1194 return;
1195
1196 for_each_sg(sg, s, nelems, i) {
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001197 if (!i)
1198 mapped_ents = get_nr_mapped_entries(dev, s);
1199
1200 if (i >= mapped_ents)
1201 break;
1202
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001203 check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001204 direction, true);
Joerg Roedela31fba52009-01-09 15:01:12 +01001205 }
1206}
1207EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1208
1209void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1210 int nelems, int direction)
1211{
1212 struct scatterlist *s;
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001213 int mapped_ents = 0, i;
Joerg Roedela31fba52009-01-09 15:01:12 +01001214
1215 if (unlikely(global_disable))
1216 return;
1217
1218 for_each_sg(sg, s, nelems, i) {
FUJITA Tomonori88f39072009-05-27 09:43:03 +09001219 if (!i)
1220 mapped_ents = get_nr_mapped_entries(dev, s);
1221
1222 if (i >= mapped_ents)
1223 break;
1224
FUJITA Tomonori884d0592009-05-27 09:43:02 +09001225 check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
FUJITA Tomonori15aedea42009-05-27 09:43:01 +09001226 direction, false);
Joerg Roedela31fba52009-01-09 15:01:12 +01001227 }
1228}
1229EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1230
Joerg Roedel1745de52009-05-22 21:49:51 +02001231static int __init dma_debug_driver_setup(char *str)
1232{
1233 int i;
1234
1235 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
1236 current_driver_name[i] = *str;
1237 if (*str == 0)
1238 break;
1239 }
1240
1241 if (current_driver_name[0])
1242 printk(KERN_INFO "DMA-API: enable driver filter for "
1243 "driver [%s]\n", current_driver_name);
1244
1245
1246 return 1;
1247}
1248__setup("dma_debug_driver=", dma_debug_driver_setup);