blob: e47e1a08c3370268a19ec8d70a877ff4332920b2 [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 Roedel2d62ece2009-01-09 14:10:26 +0100102static const char *type2name[4] = { "single", "page",
103 "scather-gather", "coherent" };
104
105static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
106 "DMA_FROM_DEVICE", "DMA_NONE" };
107
Joerg Roedeled888ae2009-05-22 17:16:04 +0200108/* little merge helper - remove it after the merge window */
109#ifndef BUS_NOTIFY_UNBOUND_DRIVER
110#define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
111#endif
112
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100113/*
114 * The access to some variables in this macro is racy. We can't use atomic_t
115 * here because all these variables are exported to debugfs. Some of them even
116 * writeable. This is also the reason why a lock won't help much. But anyway,
117 * the races are no big deal. Here is why:
118 *
119 * error_count: the addition is racy, but the worst thing that can happen is
120 * that we don't count some errors
121 * show_num_errors: the subtraction is racy. Also no big deal because in
122 * worst case this will result in one warning more in the
123 * system log than the user configured. This variable is
124 * writeable via debugfs.
125 */
David Woodhouse6c132d12009-01-19 16:52:39 +0100126static inline void dump_entry_trace(struct dma_debug_entry *entry)
127{
128#ifdef CONFIG_STACKTRACE
129 if (entry) {
130 printk(KERN_WARNING "Mapped at:\n");
131 print_stack_trace(&entry->stacktrace, 0);
132 }
133#endif
134}
135
136#define err_printk(dev, entry, format, arg...) do { \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100137 error_count += 1; \
138 if (show_all_errors || show_num_errors > 0) { \
139 WARN(1, "%s %s: " format, \
140 dev_driver_string(dev), \
141 dev_name(dev) , ## arg); \
David Woodhouse6c132d12009-01-19 16:52:39 +0100142 dump_entry_trace(entry); \
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100143 } \
144 if (!show_all_errors && show_num_errors > 0) \
145 show_num_errors -= 1; \
146 } while (0);
147
Joerg Roedel30dfa902009-01-09 12:34:49 +0100148/*
149 * Hash related functions
150 *
151 * Every DMA-API request is saved into a struct dma_debug_entry. To
152 * have quick access to these structs they are stored into a hash.
153 */
154static int hash_fn(struct dma_debug_entry *entry)
155{
156 /*
157 * Hash function is based on the dma address.
158 * We use bits 20-27 here as the index into the hash
159 */
160 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
161}
162
163/*
164 * Request exclusive access to a hash bucket for a given dma_debug_entry.
165 */
166static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
167 unsigned long *flags)
168{
169 int idx = hash_fn(entry);
170 unsigned long __flags;
171
172 spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
173 *flags = __flags;
174 return &dma_entry_hash[idx];
175}
176
177/*
178 * Give up exclusive access to the hash bucket
179 */
180static void put_hash_bucket(struct hash_bucket *bucket,
181 unsigned long *flags)
182{
183 unsigned long __flags = *flags;
184
185 spin_unlock_irqrestore(&bucket->lock, __flags);
186}
187
188/*
189 * Search a given entry in the hash bucket list
190 */
191static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
192 struct dma_debug_entry *ref)
193{
194 struct dma_debug_entry *entry;
195
196 list_for_each_entry(entry, &bucket->list, list) {
197 if ((entry->dev_addr == ref->dev_addr) &&
198 (entry->dev == ref->dev))
199 return entry;
200 }
201
202 return NULL;
203}
204
205/*
206 * Add an entry to a hash bucket
207 */
208static void hash_bucket_add(struct hash_bucket *bucket,
209 struct dma_debug_entry *entry)
210{
211 list_add_tail(&entry->list, &bucket->list);
212}
213
214/*
215 * Remove entry from a hash bucket list
216 */
217static void hash_bucket_del(struct dma_debug_entry *entry)
218{
219 list_del(&entry->list);
220}
221
222/*
David Woodhouseac26c182009-02-12 16:19:13 +0100223 * Dump mapping entries for debugging purposes
224 */
225void debug_dma_dump_mappings(struct device *dev)
226{
227 int idx;
228
229 for (idx = 0; idx < HASH_SIZE; idx++) {
230 struct hash_bucket *bucket = &dma_entry_hash[idx];
231 struct dma_debug_entry *entry;
232 unsigned long flags;
233
234 spin_lock_irqsave(&bucket->lock, flags);
235
236 list_for_each_entry(entry, &bucket->list, list) {
237 if (!dev || dev == entry->dev) {
238 dev_info(entry->dev,
239 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
240 type2name[entry->type], idx,
241 (unsigned long long)entry->paddr,
242 entry->dev_addr, entry->size,
243 dir2name[entry->direction]);
244 }
245 }
246
247 spin_unlock_irqrestore(&bucket->lock, flags);
248 }
249}
250EXPORT_SYMBOL(debug_dma_dump_mappings);
251
252/*
Joerg Roedel30dfa902009-01-09 12:34:49 +0100253 * Wrapper function for adding an entry to the hash.
254 * This function takes care of locking itself.
255 */
256static void add_dma_entry(struct dma_debug_entry *entry)
257{
258 struct hash_bucket *bucket;
259 unsigned long flags;
260
261 bucket = get_hash_bucket(entry, &flags);
262 hash_bucket_add(bucket, entry);
263 put_hash_bucket(bucket, &flags);
264}
265
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900266static struct dma_debug_entry *__dma_entry_alloc(void)
267{
268 struct dma_debug_entry *entry;
269
270 entry = list_entry(free_entries.next, struct dma_debug_entry, list);
271 list_del(&entry->list);
272 memset(entry, 0, sizeof(*entry));
273
274 num_free_entries -= 1;
275 if (num_free_entries < min_free_entries)
276 min_free_entries = num_free_entries;
277
278 return entry;
279}
280
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100281/* struct dma_entry allocator
282 *
283 * The next two functions implement the allocator for
284 * struct dma_debug_entries.
285 */
286static struct dma_debug_entry *dma_entry_alloc(void)
287{
288 struct dma_debug_entry *entry = NULL;
289 unsigned long flags;
290
291 spin_lock_irqsave(&free_entries_lock, flags);
292
293 if (list_empty(&free_entries)) {
294 printk(KERN_ERR "DMA-API: debugging out of memory "
295 "- disabling\n");
296 global_disable = true;
297 goto out;
298 }
299
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900300 entry = __dma_entry_alloc();
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100301
David Woodhouse6c132d12009-01-19 16:52:39 +0100302#ifdef CONFIG_STACKTRACE
303 entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
304 entry->stacktrace.entries = entry->st_entries;
305 entry->stacktrace.skip = 2;
306 save_stack_trace(&entry->stacktrace);
307#endif
Joerg Roedel3b1e79e2009-01-09 12:42:46 +0100308
309out:
310 spin_unlock_irqrestore(&free_entries_lock, flags);
311
312 return entry;
313}
314
315static void dma_entry_free(struct dma_debug_entry *entry)
316{
317 unsigned long flags;
318
319 /*
320 * add to beginning of the list - this way the entries are
321 * more likely cache hot when they are reallocated.
322 */
323 spin_lock_irqsave(&free_entries_lock, flags);
324 list_add(&entry->list, &free_entries);
325 num_free_entries += 1;
326 spin_unlock_irqrestore(&free_entries_lock, flags);
327}
328
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900329int dma_debug_resize_entries(u32 num_entries)
330{
331 int i, delta, ret = 0;
332 unsigned long flags;
333 struct dma_debug_entry *entry;
334 LIST_HEAD(tmp);
335
336 spin_lock_irqsave(&free_entries_lock, flags);
337
338 if (nr_total_entries < num_entries) {
339 delta = num_entries - nr_total_entries;
340
341 spin_unlock_irqrestore(&free_entries_lock, flags);
342
343 for (i = 0; i < delta; i++) {
344 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
345 if (!entry)
346 break;
347
348 list_add_tail(&entry->list, &tmp);
349 }
350
351 spin_lock_irqsave(&free_entries_lock, flags);
352
353 list_splice(&tmp, &free_entries);
354 nr_total_entries += i;
355 num_free_entries += i;
356 } else {
357 delta = nr_total_entries - num_entries;
358
359 for (i = 0; i < delta && !list_empty(&free_entries); i++) {
360 entry = __dma_entry_alloc();
361 kfree(entry);
362 }
363
364 nr_total_entries -= i;
365 }
366
367 if (nr_total_entries != num_entries)
368 ret = 1;
369
370 spin_unlock_irqrestore(&free_entries_lock, flags);
371
372 return ret;
373}
374EXPORT_SYMBOL(dma_debug_resize_entries);
375
Joerg Roedel6bf07872009-01-09 12:54:42 +0100376/*
377 * DMA-API debugging init code
378 *
379 * The init code does two things:
380 * 1. Initialize core data structures
381 * 2. Preallocate a given number of dma_debug_entry structs
382 */
383
384static int prealloc_memory(u32 num_entries)
385{
386 struct dma_debug_entry *entry, *next_entry;
387 int i;
388
389 for (i = 0; i < num_entries; ++i) {
390 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
391 if (!entry)
392 goto out_err;
393
394 list_add_tail(&entry->list, &free_entries);
395 }
396
397 num_free_entries = num_entries;
398 min_free_entries = num_entries;
399
400 printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
401 num_entries);
402
403 return 0;
404
405out_err:
406
407 list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
408 list_del(&entry->list);
409 kfree(entry);
410 }
411
412 return -ENOMEM;
413}
414
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100415static int dma_debug_fs_init(void)
416{
417 dma_debug_dent = debugfs_create_dir("dma-api", NULL);
418 if (!dma_debug_dent) {
419 printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
420 return -ENOMEM;
421 }
422
423 global_disable_dent = debugfs_create_bool("disabled", 0444,
424 dma_debug_dent,
425 (u32 *)&global_disable);
426 if (!global_disable_dent)
427 goto out_err;
428
429 error_count_dent = debugfs_create_u32("error_count", 0444,
430 dma_debug_dent, &error_count);
431 if (!error_count_dent)
432 goto out_err;
433
434 show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
435 dma_debug_dent,
436 &show_all_errors);
437 if (!show_all_errors_dent)
438 goto out_err;
439
440 show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
441 dma_debug_dent,
442 &show_num_errors);
443 if (!show_num_errors_dent)
444 goto out_err;
445
446 num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
447 dma_debug_dent,
448 &num_free_entries);
449 if (!num_free_entries_dent)
450 goto out_err;
451
452 min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
453 dma_debug_dent,
454 &min_free_entries);
455 if (!min_free_entries_dent)
456 goto out_err;
457
458 return 0;
459
460out_err:
461 debugfs_remove_recursive(dma_debug_dent);
462
463 return -ENOMEM;
464}
465
Joerg Roedeled888ae2009-05-22 17:16:04 +0200466static int device_dma_allocations(struct device *dev)
467{
468 struct dma_debug_entry *entry;
469 unsigned long flags;
470 int count = 0, i;
471
472 for (i = 0; i < HASH_SIZE; ++i) {
473 spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
474 list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
475 if (entry->dev == dev)
476 count += 1;
477 }
478 spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
479 }
480
481 return count;
482}
483
484static int dma_debug_device_change(struct notifier_block *nb,
485 unsigned long action, void *data)
486{
487 struct device *dev = data;
488 int count;
489
490
491 switch (action) {
492 case BUS_NOTIFY_UNBOUND_DRIVER:
493 count = device_dma_allocations(dev);
494 if (count == 0)
495 break;
496 err_printk(dev, NULL, "DMA-API: device driver has pending "
497 "DMA allocations while released from device "
498 "[count=%d]\n", count);
499 break;
500 default:
501 break;
502 }
503
504 return 0;
505}
506
Joerg Roedel41531c82009-03-16 17:32:14 +0100507void dma_debug_add_bus(struct bus_type *bus)
508{
Joerg Roedeled888ae2009-05-22 17:16:04 +0200509 struct notifier_block *nb;
510
511 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
512 if (nb == NULL) {
513 printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
514 return;
515 }
516
517 nb->notifier_call = dma_debug_device_change;
518
519 bus_register_notifier(bus, nb);
Joerg Roedel41531c82009-03-16 17:32:14 +0100520}
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100521
Joerg Roedel6bf07872009-01-09 12:54:42 +0100522/*
523 * Let the architectures decide how many entries should be preallocated.
524 */
525void dma_debug_init(u32 num_entries)
526{
527 int i;
528
529 if (global_disable)
530 return;
531
532 for (i = 0; i < HASH_SIZE; ++i) {
533 INIT_LIST_HEAD(&dma_entry_hash[i].list);
534 dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
535 }
536
Joerg Roedel788dcfa2009-01-09 13:13:27 +0100537 if (dma_debug_fs_init() != 0) {
538 printk(KERN_ERR "DMA-API: error creating debugfs entries "
539 "- disabling\n");
540 global_disable = true;
541
542 return;
543 }
544
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100545 if (req_entries)
546 num_entries = req_entries;
547
Joerg Roedel6bf07872009-01-09 12:54:42 +0100548 if (prealloc_memory(num_entries) != 0) {
549 printk(KERN_ERR "DMA-API: debugging out of memory error "
550 "- disabled\n");
551 global_disable = true;
552
553 return;
554 }
555
FUJITA Tomonorie6a1a892009-04-15 18:22:41 +0900556 nr_total_entries = num_free_entries;
557
Joerg Roedel6bf07872009-01-09 12:54:42 +0100558 printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
559}
560
Joerg Roedel59d3daa2009-01-09 13:01:56 +0100561static __init int dma_debug_cmdline(char *str)
562{
563 if (!str)
564 return -EINVAL;
565
566 if (strncmp(str, "off", 3) == 0) {
567 printk(KERN_INFO "DMA-API: debugging disabled on kernel "
568 "command line\n");
569 global_disable = true;
570 }
571
572 return 0;
573}
574
575static __init int dma_debug_entries_cmdline(char *str)
576{
577 int res;
578
579 if (!str)
580 return -EINVAL;
581
582 res = get_option(&str, &req_entries);
583
584 if (!res)
585 req_entries = 0;
586
587 return 0;
588}
589
590__setup("dma_debug=", dma_debug_cmdline);
591__setup("dma_debug_entries=", dma_debug_entries_cmdline);
592
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100593static void check_unmap(struct dma_debug_entry *ref)
594{
595 struct dma_debug_entry *entry;
596 struct hash_bucket *bucket;
597 unsigned long flags;
598
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900599 if (dma_mapping_error(ref->dev, ref->dev_addr)) {
600 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
601 "to free an invalid DMA memory address\n");
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100602 return;
FUJITA Tomonori35d40952009-03-19 10:39:31 +0900603 }
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100604
605 bucket = get_hash_bucket(ref, &flags);
606 entry = hash_bucket_find(bucket, ref);
607
608 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100609 err_printk(ref->dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100610 "to free DMA memory it has not allocated "
611 "[device address=0x%016llx] [size=%llu bytes]\n",
612 ref->dev_addr, ref->size);
613 goto out;
614 }
615
616 if (ref->size != entry->size) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100617 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100618 "DMA memory with different size "
619 "[device address=0x%016llx] [map size=%llu bytes] "
620 "[unmap size=%llu bytes]\n",
621 ref->dev_addr, entry->size, ref->size);
622 }
623
624 if (ref->type != entry->type) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100625 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100626 "DMA memory with wrong function "
627 "[device address=0x%016llx] [size=%llu bytes] "
628 "[mapped as %s] [unmapped as %s]\n",
629 ref->dev_addr, ref->size,
630 type2name[entry->type], type2name[ref->type]);
631 } else if ((entry->type == dma_debug_coherent) &&
632 (ref->paddr != entry->paddr)) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100633 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100634 "DMA memory with different CPU address "
635 "[device address=0x%016llx] [size=%llu bytes] "
636 "[cpu alloc address=%p] [cpu free address=%p]",
637 ref->dev_addr, ref->size,
638 (void *)entry->paddr, (void *)ref->paddr);
639 }
640
641 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
642 ref->sg_call_ents != entry->sg_call_ents) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100643 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100644 "DMA sg list with different entry count "
645 "[map count=%d] [unmap count=%d]\n",
646 entry->sg_call_ents, ref->sg_call_ents);
647 }
648
649 /*
650 * This may be no bug in reality - but most implementations of the
651 * DMA API don't handle this properly, so check for it here
652 */
653 if (ref->direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100654 err_printk(ref->dev, entry, "DMA-API: device driver frees "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100655 "DMA memory with different direction "
656 "[device address=0x%016llx] [size=%llu bytes] "
657 "[mapped with %s] [unmapped with %s]\n",
658 ref->dev_addr, ref->size,
659 dir2name[entry->direction],
660 dir2name[ref->direction]);
661 }
662
663 hash_bucket_del(entry);
664 dma_entry_free(entry);
665
666out:
667 put_hash_bucket(bucket, &flags);
668}
669
670static void check_for_stack(struct device *dev, void *addr)
671{
672 if (object_is_on_stack(addr))
David Woodhouse6c132d12009-01-19 16:52:39 +0100673 err_printk(dev, NULL, "DMA-API: device driver maps memory from"
674 "stack [addr=%p]\n", addr);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100675}
676
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100677static inline bool overlap(void *addr, u64 size, void *start, void *end)
678{
679 void *addr2 = (char *)addr + size;
680
681 return ((addr >= start && addr < end) ||
682 (addr2 >= start && addr2 < end) ||
683 ((addr < start) && (addr2 >= end)));
684}
685
686static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
687{
688 if (overlap(addr, size, _text, _etext) ||
689 overlap(addr, size, __start_rodata, __end_rodata))
690 err_printk(dev, NULL, "DMA-API: device driver maps "
691 "memory from kernel text or rodata "
692 "[addr=%p] [size=%llu]\n", addr, size);
693}
694
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100695static void check_sync(struct device *dev, dma_addr_t addr,
696 u64 size, u64 offset, int direction, bool to_cpu)
697{
698 struct dma_debug_entry ref = {
699 .dev = dev,
700 .dev_addr = addr,
701 .size = size,
702 .direction = direction,
703 };
704 struct dma_debug_entry *entry;
705 struct hash_bucket *bucket;
706 unsigned long flags;
707
708 bucket = get_hash_bucket(&ref, &flags);
709
710 entry = hash_bucket_find(bucket, &ref);
711
712 if (!entry) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100713 err_printk(dev, NULL, "DMA-API: device driver tries "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100714 "to sync DMA memory it has not allocated "
715 "[device address=0x%016llx] [size=%llu bytes]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700716 (unsigned long long)addr, size);
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100717 goto out;
718 }
719
720 if ((offset + size) > entry->size) {
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 outside allocated range "
723 "[device address=0x%016llx] "
724 "[allocation size=%llu bytes] [sync offset=%llu] "
725 "[sync size=%llu]\n", entry->dev_addr, entry->size,
726 offset, size);
727 }
728
729 if (direction != entry->direction) {
David Woodhouse6c132d12009-01-19 16:52:39 +0100730 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100731 "DMA memory with different direction "
732 "[device address=0x%016llx] [size=%llu bytes] "
733 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700734 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100735 dir2name[entry->direction],
736 dir2name[direction]);
737 }
738
739 if (entry->direction == DMA_BIDIRECTIONAL)
740 goto out;
741
742 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
743 !(direction == DMA_TO_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100744 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100745 "device read-only DMA memory for cpu "
746 "[device address=0x%016llx] [size=%llu bytes] "
747 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700748 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100749 dir2name[entry->direction],
750 dir2name[direction]);
751
752 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
753 !(direction == DMA_FROM_DEVICE))
David Woodhouse6c132d12009-01-19 16:52:39 +0100754 err_printk(dev, entry, "DMA-API: device driver syncs "
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100755 "device write-only DMA memory to device "
756 "[device address=0x%016llx] [size=%llu bytes] "
757 "[mapped with %s] [synced with %s]\n",
Randy Dunlap93c36ed2009-03-30 14:08:44 -0700758 (unsigned long long)addr, entry->size,
Joerg Roedel2d62ece2009-01-09 14:10:26 +0100759 dir2name[entry->direction],
760 dir2name[direction]);
761
762out:
763 put_hash_bucket(bucket, &flags);
764
765}
766
Joerg Roedelf62bc982009-01-09 14:14:49 +0100767void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
768 size_t size, int direction, dma_addr_t dma_addr,
769 bool map_single)
770{
771 struct dma_debug_entry *entry;
772
773 if (unlikely(global_disable))
774 return;
775
776 if (unlikely(dma_mapping_error(dev, dma_addr)))
777 return;
778
779 entry = dma_entry_alloc();
780 if (!entry)
781 return;
782
783 entry->dev = dev;
784 entry->type = dma_debug_page;
785 entry->paddr = page_to_phys(page) + offset;
786 entry->dev_addr = dma_addr;
787 entry->size = size;
788 entry->direction = direction;
789
Joerg Roedel9537a482009-03-23 15:35:08 +0100790 if (map_single)
Joerg Roedelf62bc982009-01-09 14:14:49 +0100791 entry->type = dma_debug_single;
Joerg Roedel9537a482009-03-23 15:35:08 +0100792
793 if (!PageHighMem(page)) {
794 void *addr = ((char *)page_address(page)) + offset;
Joerg Roedel2e34bde2009-03-16 16:51:55 +0100795 check_for_stack(dev, addr);
796 check_for_illegal_area(dev, addr, size);
Joerg Roedelf62bc982009-01-09 14:14:49 +0100797 }
798
799 add_dma_entry(entry);
800}
801EXPORT_SYMBOL(debug_dma_map_page);
802
803void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
804 size_t size, int direction, bool map_single)
805{
806 struct dma_debug_entry ref = {
807 .type = dma_debug_page,
808 .dev = dev,
809 .dev_addr = addr,
810 .size = size,
811 .direction = direction,
812 };
813
814 if (unlikely(global_disable))
815 return;
816
817 if (map_single)
818 ref.type = dma_debug_single;
819
820 check_unmap(&ref);
821}
822EXPORT_SYMBOL(debug_dma_unmap_page);
823
Joerg Roedel972aa452009-01-09 14:19:54 +0100824void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
825 int nents, int mapped_ents, int direction)
826{
827 struct dma_debug_entry *entry;
828 struct scatterlist *s;
829 int i;
830
831 if (unlikely(global_disable))
832 return;
833
834 for_each_sg(sg, s, mapped_ents, i) {
835 entry = dma_entry_alloc();
836 if (!entry)
837 return;
838
839 entry->type = dma_debug_sg;
840 entry->dev = dev;
841 entry->paddr = sg_phys(s);
842 entry->size = s->length;
843 entry->dev_addr = s->dma_address;
844 entry->direction = direction;
845 entry->sg_call_ents = nents;
846 entry->sg_mapped_ents = mapped_ents;
847
Joerg Roedel9537a482009-03-23 15:35:08 +0100848 if (!PageHighMem(sg_page(s))) {
849 check_for_stack(dev, sg_virt(s));
850 check_for_illegal_area(dev, sg_virt(s), s->length);
851 }
Joerg Roedel972aa452009-01-09 14:19:54 +0100852
853 add_dma_entry(entry);
854 }
855}
856EXPORT_SYMBOL(debug_dma_map_sg);
857
858void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
859 int nelems, int dir)
860{
861 struct dma_debug_entry *entry;
862 struct scatterlist *s;
863 int mapped_ents = 0, i;
864 unsigned long flags;
865
866 if (unlikely(global_disable))
867 return;
868
869 for_each_sg(sglist, s, nelems, i) {
870
871 struct dma_debug_entry ref = {
872 .type = dma_debug_sg,
873 .dev = dev,
874 .paddr = sg_phys(s),
875 .dev_addr = s->dma_address,
876 .size = s->length,
877 .direction = dir,
878 .sg_call_ents = 0,
879 };
880
881 if (mapped_ents && i >= mapped_ents)
882 break;
883
884 if (mapped_ents == 0) {
885 struct hash_bucket *bucket;
886 ref.sg_call_ents = nelems;
887 bucket = get_hash_bucket(&ref, &flags);
888 entry = hash_bucket_find(bucket, &ref);
889 if (entry)
890 mapped_ents = entry->sg_mapped_ents;
891 put_hash_bucket(bucket, &flags);
892 }
893
894 check_unmap(&ref);
895 }
896}
897EXPORT_SYMBOL(debug_dma_unmap_sg);
898
Joerg Roedel6bfd4492009-01-09 14:38:50 +0100899void debug_dma_alloc_coherent(struct device *dev, size_t size,
900 dma_addr_t dma_addr, void *virt)
901{
902 struct dma_debug_entry *entry;
903
904 if (unlikely(global_disable))
905 return;
906
907 if (unlikely(virt == NULL))
908 return;
909
910 entry = dma_entry_alloc();
911 if (!entry)
912 return;
913
914 entry->type = dma_debug_coherent;
915 entry->dev = dev;
916 entry->paddr = virt_to_phys(virt);
917 entry->size = size;
918 entry->dev_addr = dma_addr;
919 entry->direction = DMA_BIDIRECTIONAL;
920
921 add_dma_entry(entry);
922}
923EXPORT_SYMBOL(debug_dma_alloc_coherent);
924
925void debug_dma_free_coherent(struct device *dev, size_t size,
926 void *virt, dma_addr_t addr)
927{
928 struct dma_debug_entry ref = {
929 .type = dma_debug_coherent,
930 .dev = dev,
931 .paddr = virt_to_phys(virt),
932 .dev_addr = addr,
933 .size = size,
934 .direction = DMA_BIDIRECTIONAL,
935 };
936
937 if (unlikely(global_disable))
938 return;
939
940 check_unmap(&ref);
941}
942EXPORT_SYMBOL(debug_dma_free_coherent);
943
Joerg Roedelb9d23172009-01-09 14:43:04 +0100944void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
945 size_t size, int direction)
946{
947 if (unlikely(global_disable))
948 return;
949
950 check_sync(dev, dma_handle, size, 0, direction, true);
951}
952EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
953
954void debug_dma_sync_single_for_device(struct device *dev,
955 dma_addr_t dma_handle, size_t size,
956 int direction)
957{
958 if (unlikely(global_disable))
959 return;
960
961 check_sync(dev, dma_handle, size, 0, direction, false);
962}
963EXPORT_SYMBOL(debug_dma_sync_single_for_device);
964
Joerg Roedel948408b2009-01-09 14:55:38 +0100965void debug_dma_sync_single_range_for_cpu(struct device *dev,
966 dma_addr_t dma_handle,
967 unsigned long offset, size_t size,
968 int direction)
969{
970 if (unlikely(global_disable))
971 return;
972
973 check_sync(dev, dma_handle, size, offset, direction, true);
974}
975EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
976
977void debug_dma_sync_single_range_for_device(struct device *dev,
978 dma_addr_t dma_handle,
979 unsigned long offset,
980 size_t size, int direction)
981{
982 if (unlikely(global_disable))
983 return;
984
985 check_sync(dev, dma_handle, size, offset, direction, false);
986}
987EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
988
Joerg Roedela31fba52009-01-09 15:01:12 +0100989void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
990 int nelems, int direction)
991{
992 struct scatterlist *s;
993 int i;
994
995 if (unlikely(global_disable))
996 return;
997
998 for_each_sg(sg, s, nelems, i) {
999 check_sync(dev, s->dma_address, s->dma_length, 0,
1000 direction, true);
1001 }
1002}
1003EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
1004
1005void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1006 int nelems, int direction)
1007{
1008 struct scatterlist *s;
1009 int i;
1010
1011 if (unlikely(global_disable))
1012 return;
1013
1014 for_each_sg(sg, s, nelems, i) {
1015 check_sync(dev, s->dma_address, s->dma_length, 0,
1016 direction, false);
1017 }
1018}
1019EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
1020