blob: c01f64780caf74cbab3eebe6955a2d0bb8a28837 [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 Roedel2d62ece2009-01-09 14:10:26 +010026#include <linux/device.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010027#include <linux/types.h>
Joerg Roedel2d62ece2009-01-09 14:10:26 +010028#include <linux/sched.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010029#include <linux/list.h>
Joerg Roedel6bf07872009-01-09 12:54:42 +010030#include <linux/slab.h>
Joerg Roedelf2f45e52009-01-09 12:19:52 +010031
Joerg Roedel2e34bde2009-03-16 16:51:55 +010032#include <asm/sections.h>
33
Joerg Roedel30dfa902009-01-09 12:34:49 +010034#define HASH_SIZE 1024ULL
35#define HASH_FN_SHIFT 13
36#define HASH_FN_MASK (HASH_SIZE - 1)
37
Joerg Roedelf2f45e52009-01-09 12:19:52 +010038enum {
39 dma_debug_single,
40 dma_debug_page,
41 dma_debug_sg,
42 dma_debug_coherent,
43};
44
David Woodhouse6c132d12009-01-19 16:52:39 +010045#define DMA_DEBUG_STACKTRACE_ENTRIES 5
46
Joerg Roedelf2f45e52009-01-09 12:19:52 +010047struct dma_debug_entry {
48 struct list_head list;
49 struct device *dev;
50 int type;
51 phys_addr_t paddr;
52 u64 dev_addr;
53 u64 size;
54 int direction;
55 int sg_call_ents;
56 int sg_mapped_ents;
David Woodhouse6c132d12009-01-19 16:52:39 +010057#ifdef CONFIG_STACKTRACE
58 struct stack_trace stacktrace;
59 unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
60#endif
Joerg Roedelf2f45e52009-01-09 12:19:52 +010061};
62
Joerg Roedel30dfa902009-01-09 12:34:49 +010063struct hash_bucket {
64 struct list_head list;
65 spinlock_t lock;
Joerg Roedel2d62ece2009-01-09 14:10:26 +010066} ____cacheline_aligned_in_smp;
Joerg Roedel30dfa902009-01-09 12:34:49 +010067
68/* Hash list to save the allocated dma addresses */
69static struct hash_bucket dma_entry_hash[HASH_SIZE];
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010070/* List of pre-allocated dma_debug_entry's */
71static LIST_HEAD(free_entries);
72/* Lock for the list above */
73static DEFINE_SPINLOCK(free_entries_lock);
74
75/* Global disable flag - will be set in case of an error */
76static bool global_disable __read_mostly;
77
Joerg Roedel788dcfa2009-01-09 13:13:27 +010078/* Global error count */
79static u32 error_count;
80
81/* Global error show enable*/
82static u32 show_all_errors __read_mostly;
83/* Number of errors to show */
84static u32 show_num_errors = 1;
85
Joerg Roedel3b1e79e2009-01-09 12:42:46 +010086static u32 num_free_entries;
87static u32 min_free_entries;
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +090088static u32 nr_total_entries;
Joerg Roedel30dfa902009-01-09 12:34:49 +010089
Joerg Roedel59d3daa2009-01-09 13:01:56 +010090/* number of preallocated entries requested by kernel cmdline */
91static u32 req_entries;
92
Joerg Roedel788dcfa2009-01-09 13:13:27 +010093/* debugfs dentry's for the stuff above */
94static struct dentry *dma_debug_dent __read_mostly;
95static struct dentry *global_disable_dent __read_mostly;
96static struct dentry *error_count_dent __read_mostly;
97static struct dentry *show_all_errors_dent __read_mostly;
98static struct dentry *show_num_errors_dent __read_mostly;
99static struct dentry *num_free_entries_dent __read_mostly;
100static struct dentry *min_free_entries_dent __read_mostly;
101
Joerg Roedel2e507d82009-05-22 18:24:20 +0200102/* per-driver filter related state */
103
104#define NAME_MAX_LEN 64
105
106static char current_driver_name[NAME_MAX_LEN] __read_mostly;
107static struct device_driver *current_driver __read_mostly;
108
109static DEFINE_RWLOCK(driver_name_lock);
110
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100111static const char *type2name[4] = { "single", "page",
112 "scather-gather", "coherent" };
113
114static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
115 "DMA_FROM_DEVICE", "DMA_NONE" };
116
117/*
118 * The access to some variables in this macro is racy. We can't use atomic_t
119 * here because all these variables are exported to debugfs. Some of them even
120 * writeable. This is also the reason why a lock won't help much. But anyway,
121 * the races are no big deal. Here is why:
122 *
123 * error_count: the addition is racy, but the worst thing that can happen is
124 * that we don't count some errors
125 * show_num_errors: the subtraction is racy. Also no big deal because in
126 * worst case this will result in one warning more in the
127 * system log than the user configured. This variable is
128 * writeable via debugfs.
129 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100130static inline void dump_entry_trace(struct dma_debug_entry *entry)
131{
132#ifdef CONFIG_STACKTRACE
133 if (entry) {
134 printk(KERN_WARNING "Mapped at:\n");
135 print_stack_trace(&entry->stacktrace, 0);
136 }
137#endif
138}
139
Joerg Roedel2e507d82009-05-22 18:24:20 +0200140static bool driver_filter(struct device *dev)
141{
142 /* driver filter off */
143 if (likely(!current_driver_name[0]))
144 return true;
145
146 /* driver filter on and initialized */
147 if (current_driver && dev->driver == current_driver)
148 return true;
149
150 /* driver filter on but not yet initialized */
151 if (!current_driver && current_driver_name[0]) {
152 struct device_driver *drv = get_driver(dev->driver);
153 unsigned long flags;
154 bool ret = false;
155
156 if (!drv)
157 return false;
158
159 /* lock to protect against change of current_driver_name */
160 read_lock_irqsave(&driver_name_lock, flags);
161
162 if (drv->name &&
163 strncmp(current_driver_name, drv->name, 63) == 0) {
164 current_driver = drv;
165 ret = true;
166 }
167
168 read_unlock_irqrestore(&driver_name_lock, flags);
169 put_driver(drv);
170
171 return ret;
172 }
173
174 return false;
175}
176
David Woodhouse6c132d12009-01-19 16:52:39 +0100177#define err_printk(dev, entry, format, arg...) do { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100178 error_count += 1; \
Joerg Roedel2e507d82009-05-22 18:24:20 +0200179 if (driver_filter(dev) && \
180 (show_all_errors || show_num_errors > 0)) { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100181 WARN(1, "%s %s: " format, \
182 dev_driver_string(dev), \
183 dev_name(dev) , ## arg); \
David Woodhouse6c132d12009-01-19 16:52:39 +0100184 dump_entry_trace(entry); \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100185 } \
186 if (!show_all_errors && show_num_errors > 0) \
187 show_num_errors -= 1; \
188 } while (0);
189
Joerg Roedel30dfa902009-01-09 12:34:49 +0100190/*
191 * Hash related functions
192 *
193 * Every DMA-API request is saved into a struct dma_debug_entry. To
194 * have quick access to these structs they are stored into a hash.
195 */
196static int hash_fn(struct dma_debug_entry *entry)
197{
198 /*
199 * Hash function is based on the dma address.
200 * We use bits 20-27 here as the index into the hash
201 */
202 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
203}
204
205/*
206 * Request exclusive access to a hash bucket for a given dma_debug_entry.
207 */
208static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
209 unsigned long *flags)
210{
211 int idx = hash_fn(entry);
212 unsigned long __flags;
213
214 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
215 *flags = __flags;
216 return &dma_entry_hash[idx];
217}
218
219/*
220 * Give up exclusive access to the hash bucket
221 */
222static void put_hash_bucket(struct hash_bucket *bucket,
223 unsigned long *flags)
224{
225 unsigned long __flags = *flags;
226
227 spin_unlock_irqrestore(&bucket->lock, __flags);
228}
229
230/*
231 * Search a given entry in the hash bucket list
232 */
233static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
234 struct dma_debug_entry *ref)
235{
236 struct dma_debug_entry *entry;
237
238 list_for_each_entry(entry, &bucket->list, list) {
239 if ((entry->dev_addr == ref->dev_addr) &&
240 (entry->dev == ref->dev))
241 return entry;
242 }
243
244 return NULL;
245}
246
247/*
248 * Add an entry to a hash bucket
249 */
250static void hash_bucket_add(struct hash_bucket *bucket,
251 struct dma_debug_entry *entry)
252{
253 list_add_tail(&entry->list, &bucket->list);
254}
255
256/*
257 * Remove entry from a hash bucket list
258 */
259static void hash_bucket_del(struct dma_debug_entry *entry)
260{
261 list_del(&entry->list);
262}
263
264/*
David Woodhouseac26c182009-02-12 16:19:13 +0100265 * Dump mapping entries for debugging purposes
266 */
267void debug_dma_dump_mappings(struct device *dev)
268{
269 int idx;
270
271 for (idx = 0; idx < HASH_SIZE; idx++) {
272 struct hash_bucket *bucket = &dma_entry_hash[idx];
273 struct dma_debug_entry *entry;
274 unsigned long flags;
275
276 spin_lock_irqsave(&bucket->lock, flags);
277
278 list_for_each_entry(entry, &bucket->list, list) {
279 if (!dev || dev == entry->dev) {
280 dev_info(entry->dev,
281 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
282 type2name[entry->type], idx,
283 (unsigned long long)entry->paddr,
284 entry->dev_addr, entry->size,
285 dir2name[entry->direction]);
286 }
287 }
288
289 spin_unlock_irqrestore(&bucket->lock, flags);
290 }
291}
292EXPORT_SYMBOL(debug_dma_dump_mappings);
293
294/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100295 * Wrapper function for adding an entry to the hash.
296 * This function takes care of locking itself.
297 */
298static void add_dma_entry(struct dma_debug_entry *entry)
299{
300 struct hash_bucket *bucket;
301 unsigned long flags;
302
303 bucket = get_hash_bucket(entry, &flags);
304 hash_bucket_add(bucket, entry);
305 put_hash_bucket(bucket, &flags);
306}
307
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900308static struct dma_debug_entry *__dma_entry_alloc(void)
309{
310 struct dma_debug_entry *entry;
311
312 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
313 list_del(&entry->list);
314 memset(entry, 0, sizeof(*entry));
315
316 num_free_entries -= 1;
317 if (num_free_entries < min_free_entries)
318 min_free_entries = num_free_entries;
319
320 return entry;
321}
322
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100323/* struct dma_entry allocator
324 *
325 * The next two functions implement the allocator for
326 * struct dma_debug_entries.
327 */
328static struct dma_debug_entry *dma_entry_alloc(void)
329{
330 struct dma_debug_entry *entry = NULL;
331 unsigned long flags;
332
333 spin_lock_irqsave(&free_entries_lock, flags);
334
335 if (list_empty(&free_entries)) {
336 printk(KERN_ERR "DMA-API: debugging out of memory "
337 "- disabling\n");
338 global_disable = true;
339 goto out;
340 }
341
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900342 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100343
David Woodhouse6c132d12009-01-19 16:52:39 +0100344#ifdef CONFIG_STACKTRACE
345 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
346 entry->stacktrace.entries = entry->st_entries;
347 entry->stacktrace.skip = 2;
348 save_stack_trace(&entry->stacktrace);
349#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100350
351out:
352 spin_unlock_irqrestore(&free_entries_lock, flags);
353
354 return entry;
355}
356
357static void dma_entry_free(struct dma_debug_entry *entry)
358{
359 unsigned long flags;
360
361 /*
362 * add to beginning of the list - this way the entries are
363 * more likely cache hot when they are reallocated.
364 */
365 spin_lock_irqsave(&free_entries_lock, flags);
366 list_add(&entry->list, &free_entries);
367 num_free_entries += 1;
368 spin_unlock_irqrestore(&free_entries_lock, flags);
369}
370
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900371int dma_debug_resize_entries(u32 num_entries)
372{
373 int i, delta, ret = 0;
374 unsigned long flags;
375 struct dma_debug_entry *entry;
376 LIST_HEAD(tmp);
377
378 spin_lock_irqsave(&free_entries_lock, flags);
379
380 if (nr_total_entries < num_entries) {
381 delta = num_entries - nr_total_entries;
382
383 spin_unlock_irqrestore(&free_entries_lock, flags);
384
385 for (i = 0; i < delta; i++) {
386 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
387 if (!entry)
388 break;
389
390 list_add_tail(&entry->list, &tmp);
391 }
392
393 spin_lock_irqsave(&free_entries_lock, flags);
394
395 list_splice(&tmp, &free_entries);
396 nr_total_entries += i;
397 num_free_entries += i;
398 } else {
399 delta = nr_total_entries - num_entries;
400
401 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
402 entry = __dma_entry_alloc();
403 kfree(entry);
404 }
405
406 nr_total_entries -= i;
407 }
408
409 if (nr_total_entries != num_entries)
410 ret = 1;
411
412 spin_unlock_irqrestore(&free_entries_lock, flags);
413
414 return ret;
415}
416EXPORT_SYMBOL(dma_debug_resize_entries);
417
Joerg Roedel6bf07872009-01-09 12:54:42 +0100418/*
419 * DMA-API debugging init code
420 *
421 * The init code does two things:
422 * 1. Initialize core data structures
423 * 2. Preallocate a given number of dma_debug_entry structs
424 */
425
426static int prealloc_memory(u32 num_entries)
427{
428 struct dma_debug_entry *entry, *next_entry;
429 int i;
430
431 for (i = 0; i < num_entries; ++i) {
432 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
433 if (!entry)
434 goto out_err;
435
436 list_add_tail(&entry->list, &free_entries);
437 }
438
439 num_free_entries = num_entries;
440 min_free_entries = num_entries;
441
442 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
443 num_entries);
444
445 return 0;
446
447out_err:
448
449 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
450 list_del(&entry->list);
451 kfree(entry);
452 }
453
454 return -ENOMEM;
455}
456
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100457static int dma_debug_fs_init(void)
458{
459 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
460 if (!dma_debug_dent) {
461 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
462 return -ENOMEM;
463 }
464
465 global_disable_dent = debugfs_create_bool("disabled", 0444,
466 dma_debug_dent,
467 (u32 *)&global_disable);
468 if (!global_disable_dent)
469 goto out_err;
470
471 error_count_dent = debugfs_create_u32("error_count", 0444,
472 dma_debug_dent, &error_count);
473 if (!error_count_dent)
474 goto out_err;
475
476 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
477 dma_debug_dent,
478 &show_all_errors);
479 if (!show_all_errors_dent)
480 goto out_err;
481
482 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
483 dma_debug_dent,
484 &show_num_errors);
485 if (!show_num_errors_dent)
486 goto out_err;
487
488 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
489 dma_debug_dent,
490 &num_free_entries);
491 if (!num_free_entries_dent)
492 goto out_err;
493
494 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
495 dma_debug_dent,
496 &min_free_entries);
497 if (!min_free_entries_dent)
498 goto out_err;
499
500 return 0;
501
502out_err:
503 debugfs_remove_recursive(dma_debug_dent);
504
505 return -ENOMEM;
506}
507
Joerg Roedel41531c82009-03-16 17:32:14 +0100508void dma_debug_add_bus(struct bus_type *bus)
509{
Joerg Roedel314eeac2009-04-24 14:35:57 +0200510 /* FIXME: register notifier */
Joerg Roedel41531c82009-03-16 17:32:14 +0100511}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100512
Joerg Roedel6bf07872009-01-09 12:54:42 +0100513/*
514 * Let the architectures decide how many entries should be preallocated.
515 */
516void dma_debug_init(u32 num_entries)
517{
518 int i;
519
520 if (global_disable)
521 return;
522
523 for (i = 0; i < HASH_SIZE; ++i) {
524 INIT_LIST_HEAD(&dma_entry_hash[i].list);
525 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
526 }
527
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100528 if (dma_debug_fs_init() != 0) {
529 printk(KERN_ERR "DMA-API: error creating debugfs entries "
530 "- disabling\n");
531 global_disable = true;
532
533 return;
534 }
535
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100536 if (req_entries)
537 num_entries = req_entries;
538
Joerg Roedel6bf07872009-01-09 12:54:42 +0100539 if (prealloc_memory(num_entries) != 0) {
540 printk(KERN_ERR "DMA-API: debugging out of memory error "
541 "- disabled\n");
542 global_disable = true;
543
544 return;
545 }
546
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900547 nr_total_entries = num_free_entries;
548
Joerg Roedel6bf07872009-01-09 12:54:42 +0100549 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
550}
551
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100552static __init int dma_debug_cmdline(char *str)
553{
554 if (!str)
555 return -EINVAL;
556
557 if (strncmp(str, "off", 3) == 0) {
558 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
559 "command line\n");
560 global_disable = true;
561 }
562
563 return 0;
564}
565
566static __init int dma_debug_entries_cmdline(char *str)
567{
568 int res;
569
570 if (!str)
571 return -EINVAL;
572
573 res = get_option(&str, &req_entries);
574
575 if (!res)
576 req_entries = 0;
577
578 return 0;
579}
580
581__setup("dma_debug=", dma_debug_cmdline);
582__setup("dma_debug_entries=", dma_debug_entries_cmdline);
583
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100584static void check_unmap(struct dma_debug_entry *ref)
585{
586 struct dma_debug_entry *entry;
587 struct hash_bucket *bucket;
588 unsigned long flags;
589
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900590 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
591 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
592 "to free an invalid DMA memory address\n");
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100593 return;
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900594 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100595
596 bucket = get_hash_bucket(ref, &flags);
597 entry = hash_bucket_find(bucket, ref);
598
599 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100600 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100601 "to free DMA memory it has not allocated "
602 "[device address=0x%016llx] [size=%llu bytes]\n",
603 ref->dev_addr, ref->size);
604 goto out;
605 }
606
607 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100608 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100609 "DMA memory with different size "
610 "[device address=0x%016llx] [map size=%llu bytes] "
611 "[unmap size=%llu bytes]\n",
612 ref->dev_addr, entry->size, ref->size);
613 }
614
615 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100616 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100617 "DMA memory with wrong function "
618 "[device address=0x%016llx] [size=%llu bytes] "
619 "[mapped as %s] [unmapped as %s]\n",
620 ref->dev_addr, ref->size,
621 type2name[entry->type], type2name[ref->type]);
622 } else if ((entry->type == dma_debug_coherent) &&
623 (ref->paddr != entry->paddr)) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100624 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100625 "DMA memory with different CPU address "
626 "[device address=0x%016llx] [size=%llu bytes] "
627 "[cpu alloc address=%p] [cpu free address=%p]",
628 ref->dev_addr, ref->size,
629 (void *)entry->paddr, (void *)ref->paddr);
630 }
631
632 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
633 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100634 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100635 "DMA sg list with different entry count "
636 "[map count=%d] [unmap count=%d]\n",
637 entry->sg_call_ents, ref->sg_call_ents);
638 }
639
640 /*
641 * This may be no bug in reality - but most implementations of the
642 * DMA API don't handle this properly, so check for it here
643 */
644 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100645 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100646 "DMA memory with different direction "
647 "[device address=0x%016llx] [size=%llu bytes] "
648 "[mapped with %s] [unmapped with %s]\n",
649 ref->dev_addr, ref->size,
650 dir2name[entry->direction],
651 dir2name[ref->direction]);
652 }
653
654 hash_bucket_del(entry);
655 dma_entry_free(entry);
656
657out:
658 put_hash_bucket(bucket, &flags);
659}
660
661static void check_for_stack(struct device *dev, void *addr)
662{
663 if (object_is_on_stack(addr))
David Woodhouse6c132d12009-01-19 16:52:39 +0100664 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
665 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100666}
667
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100668static inline bool overlap(void *addr, u64 size, void *start, void *end)
669{
670 void *addr2 = (char *)addr + size;
671
672 return ((addr >= start && addr < end) ||
673 (addr2 >= start && addr2 < end) ||
674 ((addr < start) && (addr2 >= end)));
675}
676
677static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
678{
679 if (overlap(addr, size, _text, _etext) ||
680 overlap(addr, size, __start_rodata, __end_rodata))
681 err_printk(dev, NULL, "DMA-API: device driver maps "
682 "memory from kernel text or rodata "
683 "[addr=%p] [size=%llu]\n", addr, size);
684}
685
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100686static void check_sync(struct device *dev, dma_addr_t addr,
687 u64 size, u64 offset, int direction, bool to_cpu)
688{
689 struct dma_debug_entry ref = {
690 .dev = dev,
691 .dev_addr = addr,
692 .size = size,
693 .direction = direction,
694 };
695 struct dma_debug_entry *entry;
696 struct hash_bucket *bucket;
697 unsigned long flags;
698
699 bucket = get_hash_bucket(&ref, &flags);
700
701 entry = hash_bucket_find(bucket, &ref);
702
703 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100704 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100705 "to sync DMA memory it has not allocated "
706 "[device address=0x%016llx] [size=%llu bytes]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700707 (unsigned long long)addr, size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100708 goto out;
709 }
710
711 if ((offset + size) > entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100712 err_printk(dev, entry, "DMA-API: device driver syncs"
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100713 " DMA memory outside allocated range "
714 "[device address=0x%016llx] "
715 "[allocation size=%llu bytes] [sync offset=%llu] "
716 "[sync size=%llu]\n", entry->dev_addr, entry->size,
717 offset, size);
718 }
719
720 if (direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100721 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100722 "DMA memory with different direction "
723 "[device address=0x%016llx] [size=%llu bytes] "
724 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700725 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100726 dir2name[entry->direction],
727 dir2name[direction]);
728 }
729
730 if (entry->direction == DMA_BIDIRECTIONAL)
731 goto out;
732
733 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
734 !(direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100735 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100736 "device read-only DMA memory for cpu "
737 "[device address=0x%016llx] [size=%llu bytes] "
738 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700739 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100740 dir2name[entry->direction],
741 dir2name[direction]);
742
743 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
744 !(direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100745 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100746 "device write-only DMA memory to device "
747 "[device address=0x%016llx] [size=%llu bytes] "
748 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700749 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100750 dir2name[entry->direction],
751 dir2name[direction]);
752
753out:
754 put_hash_bucket(bucket, &flags);
755
756}
757
Joerg Roedelf62bc982009-01-09 14:14:49 +0100758void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
759 size_t size, int direction, dma_addr_t dma_addr,
760 bool map_single)
761{
762 struct dma_debug_entry *entry;
763
764 if (unlikely(global_disable))
765 return;
766
767 if (unlikely(dma_mapping_error(dev, dma_addr)))
768 return;
769
770 entry = dma_entry_alloc();
771 if (!entry)
772 return;
773
774 entry->dev = dev;
775 entry->type = dma_debug_page;
776 entry->paddr = page_to_phys(page) + offset;
777 entry->dev_addr = dma_addr;
778 entry->size = size;
779 entry->direction = direction;
780
Joerg Roedel9537a482009-03-23 15:35:08 +0100781 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +0100782 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +0100783
784 if (!PageHighMem(page)) {
785 void *addr = ((char *)page_address(page)) + offset;
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100786 check_for_stack(dev, addr);
787 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +0100788 }
789
790 add_dma_entry(entry);
791}
792EXPORT_SYMBOL(debug_dma_map_page);
793
794void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
795 size_t size, int direction, bool map_single)
796{
797 struct dma_debug_entry ref = {
798 .type = dma_debug_page,
799 .dev = dev,
800 .dev_addr = addr,
801 .size = size,
802 .direction = direction,
803 };
804
805 if (unlikely(global_disable))
806 return;
807
808 if (map_single)
809 ref.type = dma_debug_single;
810
811 check_unmap(&ref);
812}
813EXPORT_SYMBOL(debug_dma_unmap_page);
814
Joerg Roedel972aa452009-01-09 14:19:54 +0100815void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
816 int nents, int mapped_ents, int direction)
817{
818 struct dma_debug_entry *entry;
819 struct scatterlist *s;
820 int i;
821
822 if (unlikely(global_disable))
823 return;
824
825 for_each_sg(sg, s, mapped_ents, i) {
826 entry = dma_entry_alloc();
827 if (!entry)
828 return;
829
830 entry->type = dma_debug_sg;
831 entry->dev = dev;
832 entry->paddr = sg_phys(s);
833 entry->size = s->length;
834 entry->dev_addr = s->dma_address;
835 entry->direction = direction;
836 entry->sg_call_ents = nents;
837 entry->sg_mapped_ents = mapped_ents;
838
Joerg Roedel9537a482009-03-23 15:35:08 +0100839 if (!PageHighMem(sg_page(s))) {
840 check_for_stack(dev, sg_virt(s));
841 check_for_illegal_area(dev, sg_virt(s), s->length);
842 }
Joerg Roedel972aa452009-01-09 14:19:54 +0100843
844 add_dma_entry(entry);
845 }
846}
847EXPORT_SYMBOL(debug_dma_map_sg);
848
849void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
850 int nelems, int dir)
851{
852 struct dma_debug_entry *entry;
853 struct scatterlist *s;
854 int mapped_ents = 0, i;
855 unsigned long flags;
856
857 if (unlikely(global_disable))
858 return;
859
860 for_each_sg(sglist, s, nelems, i) {
861
862 struct dma_debug_entry ref = {
863 .type = dma_debug_sg,
864 .dev = dev,
865 .paddr = sg_phys(s),
866 .dev_addr = s->dma_address,
867 .size = s->length,
868 .direction = dir,
869 .sg_call_ents = 0,
870 };
871
872 if (mapped_ents && i >= mapped_ents)
873 break;
874
875 if (mapped_ents == 0) {
876 struct hash_bucket *bucket;
877 ref.sg_call_ents = nelems;
878 bucket = get_hash_bucket(&ref, &flags);
879 entry = hash_bucket_find(bucket, &ref);
880 if (entry)
881 mapped_ents = entry->sg_mapped_ents;
882 put_hash_bucket(bucket, &flags);
883 }
884
885 check_unmap(&ref);
886 }
887}
888EXPORT_SYMBOL(debug_dma_unmap_sg);
889
Joerg Roedel6bfd4492009-01-09 14:38:50 +0100890void debug_dma_alloc_coherent(struct device *dev, size_t size,
891 dma_addr_t dma_addr, void *virt)
892{
893 struct dma_debug_entry *entry;
894
895 if (unlikely(global_disable))
896 return;
897
898 if (unlikely(virt == NULL))
899 return;
900
901 entry = dma_entry_alloc();
902 if (!entry)
903 return;
904
905 entry->type = dma_debug_coherent;
906 entry->dev = dev;
907 entry->paddr = virt_to_phys(virt);
908 entry->size = size;
909 entry->dev_addr = dma_addr;
910 entry->direction = DMA_BIDIRECTIONAL;
911
912 add_dma_entry(entry);
913}
914EXPORT_SYMBOL(debug_dma_alloc_coherent);
915
916void debug_dma_free_coherent(struct device *dev, size_t size,
917 void *virt, dma_addr_t addr)
918{
919 struct dma_debug_entry ref = {
920 .type = dma_debug_coherent,
921 .dev = dev,
922 .paddr = virt_to_phys(virt),
923 .dev_addr = addr,
924 .size = size,
925 .direction = DMA_BIDIRECTIONAL,
926 };
927
928 if (unlikely(global_disable))
929 return;
930
931 check_unmap(&ref);
932}
933EXPORT_SYMBOL(debug_dma_free_coherent);
934
Joerg Roedelb9d23172009-01-09 14:43:04 +0100935void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
936 size_t size, int direction)
937{
938 if (unlikely(global_disable))
939 return;
940
941 check_sync(dev, dma_handle, size, 0, direction, true);
942}
943EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
944
945void debug_dma_sync_single_for_device(struct device *dev,
946 dma_addr_t dma_handle, size_t size,
947 int direction)
948{
949 if (unlikely(global_disable))
950 return;
951
952 check_sync(dev, dma_handle, size, 0, direction, false);
953}
954EXPORT_SYMBOL(debug_dma_sync_single_for_device);
955
Joerg Roedel948408b2009-01-09 14:55:38 +0100956void debug_dma_sync_single_range_for_cpu(struct device *dev,
957 dma_addr_t dma_handle,
958 unsigned long offset, size_t size,
959 int direction)
960{
961 if (unlikely(global_disable))
962 return;
963
964 check_sync(dev, dma_handle, size, offset, direction, true);
965}
966EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
967
968void debug_dma_sync_single_range_for_device(struct device *dev,
969 dma_addr_t dma_handle,
970 unsigned long offset,
971 size_t size, int direction)
972{
973 if (unlikely(global_disable))
974 return;
975
976 check_sync(dev, dma_handle, size, offset, direction, false);
977}
978EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
979
Joerg Roedela31fba52009-01-09 15:01:12 +0100980void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
981 int nelems, int direction)
982{
983 struct scatterlist *s;
984 int i;
985
986 if (unlikely(global_disable))
987 return;
988
989 for_each_sg(sg, s, nelems, i) {
990 check_sync(dev, s->dma_address, s->dma_length, 0,
991 direction, true);
992 }
993}
994EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
995
996void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
997 int nelems, int direction)
998{
999 struct scatterlist *s;
1000 int i;
1001
1002 if (unlikely(global_disable))
1003 return;
1004
1005 for_each_sg(sg, s, nelems, i) {
1006 check_sync(dev, s->dma_address, s->dma_length, 0,
1007 direction, false);
1008 }
1009}
1010EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1011