diff options
Diffstat (limited to 'lib/dma-debug.c')
| -rw-r--r-- | lib/dma-debug.c | 432 |
1 files changed, 387 insertions, 45 deletions
diff --git a/lib/dma-debug.c b/lib/dma-debug.c index 69da09a085a1..ad65fc0317d9 100644 --- a/lib/dma-debug.c +++ b/lib/dma-debug.c | |||
| @@ -23,9 +23,11 @@ | |||
| 23 | #include <linux/dma-debug.h> | 23 | #include <linux/dma-debug.h> |
| 24 | #include <linux/spinlock.h> | 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/debugfs.h> | 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/uaccess.h> | ||
| 26 | #include <linux/device.h> | 27 | #include <linux/device.h> |
| 27 | #include <linux/types.h> | 28 | #include <linux/types.h> |
| 28 | #include <linux/sched.h> | 29 | #include <linux/sched.h> |
| 30 | #include <linux/ctype.h> | ||
| 29 | #include <linux/list.h> | 31 | #include <linux/list.h> |
| 30 | #include <linux/slab.h> | 32 | #include <linux/slab.h> |
| 31 | 33 | ||
| @@ -85,6 +87,7 @@ static u32 show_num_errors = 1; | |||
| 85 | 87 | ||
| 86 | static u32 num_free_entries; | 88 | static u32 num_free_entries; |
| 87 | static u32 min_free_entries; | 89 | static u32 min_free_entries; |
| 90 | static u32 nr_total_entries; | ||
| 88 | 91 | ||
| 89 | /* number of preallocated entries requested by kernel cmdline */ | 92 | /* number of preallocated entries requested by kernel cmdline */ |
| 90 | static u32 req_entries; | 93 | static u32 req_entries; |
| @@ -97,6 +100,16 @@ static struct dentry *show_all_errors_dent __read_mostly; | |||
| 97 | static struct dentry *show_num_errors_dent __read_mostly; | 100 | static struct dentry *show_num_errors_dent __read_mostly; |
| 98 | static struct dentry *num_free_entries_dent __read_mostly; | 101 | static struct dentry *num_free_entries_dent __read_mostly; |
| 99 | static struct dentry *min_free_entries_dent __read_mostly; | 102 | static struct dentry *min_free_entries_dent __read_mostly; |
| 103 | static struct dentry *filter_dent __read_mostly; | ||
| 104 | |||
| 105 | /* per-driver filter related state */ | ||
| 106 | |||
| 107 | #define NAME_MAX_LEN 64 | ||
| 108 | |||
| 109 | static char current_driver_name[NAME_MAX_LEN] __read_mostly; | ||
| 110 | static struct device_driver *current_driver __read_mostly; | ||
| 111 | |||
| 112 | static DEFINE_RWLOCK(driver_name_lock); | ||
| 100 | 113 | ||
| 101 | static const char *type2name[4] = { "single", "page", | 114 | static const char *type2name[4] = { "single", "page", |
| 102 | "scather-gather", "coherent" }; | 115 | "scather-gather", "coherent" }; |
| @@ -104,6 +117,11 @@ static const char *type2name[4] = { "single", "page", | |||
| 104 | static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", | 117 | static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE", |
| 105 | "DMA_FROM_DEVICE", "DMA_NONE" }; | 118 | "DMA_FROM_DEVICE", "DMA_NONE" }; |
| 106 | 119 | ||
| 120 | /* 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 | |||
| 107 | /* | 125 | /* |
| 108 | * The access to some variables in this macro is racy. We can't use atomic_t | 126 | * The access to some variables in this macro is racy. We can't use atomic_t |
| 109 | * here because all these variables are exported to debugfs. Some of them even | 127 | * here because all these variables are exported to debugfs. Some of them even |
| @@ -121,15 +139,54 @@ static inline void dump_entry_trace(struct dma_debug_entry *entry) | |||
| 121 | { | 139 | { |
| 122 | #ifdef CONFIG_STACKTRACE | 140 | #ifdef CONFIG_STACKTRACE |
| 123 | if (entry) { | 141 | if (entry) { |
| 124 | printk(KERN_WARNING "Mapped at:\n"); | 142 | pr_warning("Mapped at:\n"); |
| 125 | print_stack_trace(&entry->stacktrace, 0); | 143 | print_stack_trace(&entry->stacktrace, 0); |
| 126 | } | 144 | } |
| 127 | #endif | 145 | #endif |
| 128 | } | 146 | } |
| 129 | 147 | ||
| 148 | static bool driver_filter(struct device *dev) | ||
| 149 | { | ||
| 150 | struct device_driver *drv; | ||
| 151 | unsigned long flags; | ||
| 152 | bool ret; | ||
| 153 | |||
| 154 | /* driver filter off */ | ||
| 155 | if (likely(!current_driver_name[0])) | ||
| 156 | return true; | ||
| 157 | |||
| 158 | /* driver filter on and initialized */ | ||
| 159 | if (current_driver && dev->driver == current_driver) | ||
| 160 | return true; | ||
| 161 | |||
| 162 | if (current_driver || !current_driver_name[0]) | ||
| 163 | return false; | ||
| 164 | |||
| 165 | /* driver filter on but not yet initialized */ | ||
| 166 | drv = get_driver(dev->driver); | ||
| 167 | if (!drv) | ||
| 168 | return false; | ||
| 169 | |||
| 170 | /* lock to protect against change of current_driver_name */ | ||
| 171 | read_lock_irqsave(&driver_name_lock, flags); | ||
| 172 | |||
| 173 | ret = false; | ||
| 174 | if (drv->name && | ||
| 175 | strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) { | ||
| 176 | current_driver = drv; | ||
| 177 | ret = true; | ||
| 178 | } | ||
| 179 | |||
| 180 | read_unlock_irqrestore(&driver_name_lock, flags); | ||
| 181 | put_driver(drv); | ||
| 182 | |||
| 183 | return ret; | ||
| 184 | } | ||
| 185 | |||
| 130 | #define err_printk(dev, entry, format, arg...) do { \ | 186 | #define err_printk(dev, entry, format, arg...) do { \ |
| 131 | error_count += 1; \ | 187 | error_count += 1; \ |
| 132 | if (show_all_errors || show_num_errors > 0) { \ | 188 | if (driver_filter(dev) && \ |
| 189 | (show_all_errors || show_num_errors > 0)) { \ | ||
| 133 | WARN(1, "%s %s: " format, \ | 190 | WARN(1, "%s %s: " format, \ |
| 134 | dev_driver_string(dev), \ | 191 | dev_driver_string(dev), \ |
| 135 | dev_name(dev) , ## arg); \ | 192 | dev_name(dev) , ## arg); \ |
| @@ -185,15 +242,50 @@ static void put_hash_bucket(struct hash_bucket *bucket, | |||
| 185 | static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket, | 242 | static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket, |
| 186 | struct dma_debug_entry *ref) | 243 | struct dma_debug_entry *ref) |
| 187 | { | 244 | { |
| 188 | struct dma_debug_entry *entry; | 245 | struct dma_debug_entry *entry, *ret = NULL; |
| 246 | int matches = 0, match_lvl, last_lvl = 0; | ||
| 189 | 247 | ||
| 190 | list_for_each_entry(entry, &bucket->list, list) { | 248 | list_for_each_entry(entry, &bucket->list, list) { |
| 191 | if ((entry->dev_addr == ref->dev_addr) && | 249 | if ((entry->dev_addr != ref->dev_addr) || |
| 192 | (entry->dev == ref->dev)) | 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 */ | ||
| 193 | return entry; | 271 | return entry; |
| 272 | } 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 | } | ||
| 194 | } | 280 | } |
| 195 | 281 | ||
| 196 | return NULL; | 282 | /* |
| 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; | ||
| 197 | } | 289 | } |
| 198 | 290 | ||
| 199 | /* | 291 | /* |
| @@ -257,6 +349,21 @@ static void add_dma_entry(struct dma_debug_entry *entry) | |||
| 257 | put_hash_bucket(bucket, &flags); | 349 | put_hash_bucket(bucket, &flags); |
| 258 | } | 350 | } |
| 259 | 351 | ||
| 352 | static 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 | |||
| 260 | /* struct dma_entry allocator | 367 | /* struct dma_entry allocator |
| 261 | * | 368 | * |
| 262 | * The next two functions implement the allocator for | 369 | * The next two functions implement the allocator for |
| @@ -270,15 +377,12 @@ static struct dma_debug_entry *dma_entry_alloc(void) | |||
| 270 | spin_lock_irqsave(&free_entries_lock, flags); | 377 | spin_lock_irqsave(&free_entries_lock, flags); |
| 271 | 378 | ||
| 272 | if (list_empty(&free_entries)) { | 379 | if (list_empty(&free_entries)) { |
| 273 | printk(KERN_ERR "DMA-API: debugging out of memory " | 380 | pr_err("DMA-API: debugging out of memory - disabling\n"); |
| 274 | "- disabling\n"); | ||
| 275 | global_disable = true; | 381 | global_disable = true; |
| 276 | goto out; | 382 | goto out; |
| 277 | } | 383 | } |
| 278 | 384 | ||
| 279 | entry = list_entry(free_entries.next, struct dma_debug_entry, list); | 385 | entry = __dma_entry_alloc(); |
| 280 | list_del(&entry->list); | ||
| 281 | memset(entry, 0, sizeof(*entry)); | ||
| 282 | 386 | ||
| 283 | #ifdef CONFIG_STACKTRACE | 387 | #ifdef CONFIG_STACKTRACE |
| 284 | entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; | 388 | entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES; |
| @@ -286,9 +390,6 @@ static struct dma_debug_entry *dma_entry_alloc(void) | |||
| 286 | entry->stacktrace.skip = 2; | 390 | entry->stacktrace.skip = 2; |
| 287 | save_stack_trace(&entry->stacktrace); | 391 | save_stack_trace(&entry->stacktrace); |
| 288 | #endif | 392 | #endif |
| 289 | num_free_entries -= 1; | ||
| 290 | if (num_free_entries < min_free_entries) | ||
| 291 | min_free_entries = num_free_entries; | ||
| 292 | 393 | ||
| 293 | out: | 394 | out: |
| 294 | spin_unlock_irqrestore(&free_entries_lock, flags); | 395 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| @@ -310,6 +411,53 @@ static void dma_entry_free(struct dma_debug_entry *entry) | |||
| 310 | spin_unlock_irqrestore(&free_entries_lock, flags); | 411 | spin_unlock_irqrestore(&free_entries_lock, flags); |
| 311 | } | 412 | } |
| 312 | 413 | ||
| 414 | int dma_debug_resize_entries(u32 num_entries) | ||
| 415 | { | ||
| 416 | int i, delta, ret = 0; | ||
| 417 | unsigned long flags; | ||
| 418 | struct dma_debug_entry *entry; | ||
| 419 | LIST_HEAD(tmp); | ||
| 420 | |||
| 421 | spin_lock_irqsave(&free_entries_lock, flags); | ||
| 422 | |||
| 423 | if (nr_total_entries < num_entries) { | ||
| 424 | delta = num_entries - nr_total_entries; | ||
| 425 | |||
| 426 | spin_unlock_irqrestore(&free_entries_lock, flags); | ||
| 427 | |||
| 428 | for (i = 0; i < delta; i++) { | ||
| 429 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | ||
| 430 | if (!entry) | ||
| 431 | break; | ||
| 432 | |||
| 433 | list_add_tail(&entry->list, &tmp); | ||
| 434 | } | ||
| 435 | |||
| 436 | spin_lock_irqsave(&free_entries_lock, flags); | ||
| 437 | |||
| 438 | list_splice(&tmp, &free_entries); | ||
| 439 | nr_total_entries += i; | ||
| 440 | num_free_entries += i; | ||
| 441 | } else { | ||
| 442 | delta = nr_total_entries - num_entries; | ||
| 443 | |||
| 444 | for (i = 0; i < delta && !list_empty(&free_entries); i++) { | ||
| 445 | entry = __dma_entry_alloc(); | ||
| 446 | kfree(entry); | ||
| 447 | } | ||
| 448 | |||
| 449 | nr_total_entries -= i; | ||
| 450 | } | ||
| 451 | |||
| 452 | if (nr_total_entries != num_entries) | ||
| 453 | ret = 1; | ||
| 454 | |||
| 455 | spin_unlock_irqrestore(&free_entries_lock, flags); | ||
| 456 | |||
| 457 | return ret; | ||
| 458 | } | ||
| 459 | EXPORT_SYMBOL(dma_debug_resize_entries); | ||
| 460 | |||
| 313 | /* | 461 | /* |
| 314 | * DMA-API debugging init code | 462 | * DMA-API debugging init code |
| 315 | * | 463 | * |
| @@ -334,8 +482,7 @@ static int prealloc_memory(u32 num_entries) | |||
| 334 | num_free_entries = num_entries; | 482 | num_free_entries = num_entries; |
| 335 | min_free_entries = num_entries; | 483 | min_free_entries = num_entries; |
| 336 | 484 | ||
| 337 | printk(KERN_INFO "DMA-API: preallocated %d debug entries\n", | 485 | pr_info("DMA-API: preallocated %d debug entries\n", num_entries); |
| 338 | num_entries); | ||
| 339 | 486 | ||
| 340 | return 0; | 487 | return 0; |
| 341 | 488 | ||
| @@ -349,11 +496,102 @@ out_err: | |||
| 349 | return -ENOMEM; | 496 | return -ENOMEM; |
| 350 | } | 497 | } |
| 351 | 498 | ||
| 499 | static ssize_t filter_read(struct file *file, char __user *user_buf, | ||
| 500 | size_t count, loff_t *ppos) | ||
| 501 | { | ||
| 502 | char buf[NAME_MAX_LEN + 1]; | ||
| 503 | unsigned long flags; | ||
| 504 | int len; | ||
| 505 | |||
| 506 | if (!current_driver_name[0]) | ||
| 507 | return 0; | ||
| 508 | |||
| 509 | /* | ||
| 510 | * We can't copy to userspace directly because current_driver_name can | ||
| 511 | * only be read under the driver_name_lock with irqs disabled. So | ||
| 512 | * create a temporary copy first. | ||
| 513 | */ | ||
| 514 | read_lock_irqsave(&driver_name_lock, flags); | ||
| 515 | len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name); | ||
| 516 | read_unlock_irqrestore(&driver_name_lock, flags); | ||
| 517 | |||
| 518 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); | ||
| 519 | } | ||
| 520 | |||
| 521 | static ssize_t filter_write(struct file *file, const char __user *userbuf, | ||
| 522 | size_t count, loff_t *ppos) | ||
| 523 | { | ||
| 524 | char buf[NAME_MAX_LEN]; | ||
| 525 | unsigned long flags; | ||
| 526 | size_t len; | ||
| 527 | int i; | ||
| 528 | |||
| 529 | /* | ||
| 530 | * We can't copy from userspace directly. Access to | ||
| 531 | * current_driver_name is protected with a write_lock with irqs | ||
| 532 | * disabled. Since copy_from_user can fault and may sleep we | ||
| 533 | * need to copy to temporary buffer first | ||
| 534 | */ | ||
| 535 | len = min(count, (size_t)(NAME_MAX_LEN - 1)); | ||
| 536 | if (copy_from_user(buf, userbuf, len)) | ||
| 537 | return -EFAULT; | ||
| 538 | |||
| 539 | buf[len] = 0; | ||
| 540 | |||
| 541 | write_lock_irqsave(&driver_name_lock, flags); | ||
| 542 | |||
| 543 | /* | ||
| 544 | * Now handle the string we got from userspace very carefully. | ||
| 545 | * The rules are: | ||
| 546 | * - only use the first token we got | ||
| 547 | * - token delimiter is everything looking like a space | ||
| 548 | * character (' ', '\n', '\t' ...) | ||
| 549 | * | ||
| 550 | */ | ||
| 551 | if (!isalnum(buf[0])) { | ||
| 552 | /* | ||
| 553 | * If the first character userspace gave us is not | ||
| 554 | * alphanumerical then assume the filter should be | ||
| 555 | * switched off. | ||
| 556 | */ | ||
| 557 | if (current_driver_name[0]) | ||
| 558 | pr_info("DMA-API: switching off dma-debug driver filter\n"); | ||
| 559 | current_driver_name[0] = 0; | ||
| 560 | current_driver = NULL; | ||
| 561 | goto out_unlock; | ||
| 562 | } | ||
| 563 | |||
| 564 | /* | ||
| 565 | * Now parse out the first token and use it as the name for the | ||
| 566 | * driver to filter for. | ||
| 567 | */ | ||
| 568 | for (i = 0; i < NAME_MAX_LEN; ++i) { | ||
| 569 | current_driver_name[i] = buf[i]; | ||
| 570 | if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0) | ||
| 571 | break; | ||
| 572 | } | ||
| 573 | current_driver_name[i] = 0; | ||
| 574 | current_driver = NULL; | ||
| 575 | |||
| 576 | pr_info("DMA-API: enable driver filter for driver [%s]\n", | ||
| 577 | current_driver_name); | ||
| 578 | |||
| 579 | out_unlock: | ||
| 580 | write_unlock_irqrestore(&driver_name_lock, flags); | ||
| 581 | |||
| 582 | return count; | ||
| 583 | } | ||
| 584 | |||
| 585 | const struct file_operations filter_fops = { | ||
| 586 | .read = filter_read, | ||
| 587 | .write = filter_write, | ||
| 588 | }; | ||
| 589 | |||
| 352 | static int dma_debug_fs_init(void) | 590 | static int dma_debug_fs_init(void) |
| 353 | { | 591 | { |
| 354 | dma_debug_dent = debugfs_create_dir("dma-api", NULL); | 592 | dma_debug_dent = debugfs_create_dir("dma-api", NULL); |
| 355 | if (!dma_debug_dent) { | 593 | if (!dma_debug_dent) { |
| 356 | printk(KERN_ERR "DMA-API: can not create debugfs directory\n"); | 594 | pr_err("DMA-API: can not create debugfs directory\n"); |
| 357 | return -ENOMEM; | 595 | return -ENOMEM; |
| 358 | } | 596 | } |
| 359 | 597 | ||
| @@ -392,6 +630,11 @@ static int dma_debug_fs_init(void) | |||
| 392 | if (!min_free_entries_dent) | 630 | if (!min_free_entries_dent) |
| 393 | goto out_err; | 631 | goto out_err; |
| 394 | 632 | ||
| 633 | filter_dent = debugfs_create_file("driver_filter", 0644, | ||
| 634 | dma_debug_dent, NULL, &filter_fops); | ||
| 635 | if (!filter_dent) | ||
| 636 | goto out_err; | ||
| 637 | |||
| 395 | return 0; | 638 | return 0; |
| 396 | 639 | ||
| 397 | out_err: | 640 | out_err: |
| @@ -400,9 +643,64 @@ out_err: | |||
| 400 | return -ENOMEM; | 643 | return -ENOMEM; |
| 401 | } | 644 | } |
| 402 | 645 | ||
| 646 | static int device_dma_allocations(struct device *dev) | ||
| 647 | { | ||
| 648 | struct dma_debug_entry *entry; | ||
| 649 | unsigned long flags; | ||
| 650 | int count = 0, i; | ||
| 651 | |||
| 652 | local_irq_save(flags); | ||
| 653 | |||
| 654 | for (i = 0; i < HASH_SIZE; ++i) { | ||
| 655 | spin_lock(&dma_entry_hash[i].lock); | ||
| 656 | list_for_each_entry(entry, &dma_entry_hash[i].list, list) { | ||
| 657 | if (entry->dev == dev) | ||
| 658 | count += 1; | ||
| 659 | } | ||
| 660 | spin_unlock(&dma_entry_hash[i].lock); | ||
| 661 | } | ||
| 662 | |||
| 663 | local_irq_restore(flags); | ||
| 664 | |||
| 665 | return count; | ||
| 666 | } | ||
| 667 | |||
| 668 | static int dma_debug_device_change(struct notifier_block *nb, | ||
| 669 | unsigned long action, void *data) | ||
| 670 | { | ||
| 671 | struct device *dev = data; | ||
| 672 | int count; | ||
| 673 | |||
| 674 | |||
| 675 | switch (action) { | ||
| 676 | case BUS_NOTIFY_UNBOUND_DRIVER: | ||
| 677 | count = device_dma_allocations(dev); | ||
| 678 | if (count == 0) | ||
| 679 | break; | ||
| 680 | err_printk(dev, NULL, "DMA-API: device driver has pending " | ||
| 681 | "DMA allocations while released from device " | ||
| 682 | "[count=%d]\n", count); | ||
| 683 | break; | ||
| 684 | default: | ||
| 685 | break; | ||
| 686 | } | ||
| 687 | |||
| 688 | return 0; | ||
| 689 | } | ||
| 690 | |||
| 403 | void dma_debug_add_bus(struct bus_type *bus) | 691 | void dma_debug_add_bus(struct bus_type *bus) |
| 404 | { | 692 | { |
| 405 | /* FIXME: register notifier */ | 693 | struct notifier_block *nb; |
| 694 | |||
| 695 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); | ||
| 696 | if (nb == NULL) { | ||
| 697 | pr_err("dma_debug_add_bus: out of memory\n"); | ||
| 698 | return; | ||
| 699 | } | ||
| 700 | |||
| 701 | nb->notifier_call = dma_debug_device_change; | ||
| 702 | |||
| 703 | bus_register_notifier(bus, nb); | ||
| 406 | } | 704 | } |
| 407 | 705 | ||
| 408 | /* | 706 | /* |
| @@ -421,8 +719,7 @@ void dma_debug_init(u32 num_entries) | |||
| 421 | } | 719 | } |
| 422 | 720 | ||
| 423 | if (dma_debug_fs_init() != 0) { | 721 | if (dma_debug_fs_init() != 0) { |
| 424 | printk(KERN_ERR "DMA-API: error creating debugfs entries " | 722 | pr_err("DMA-API: error creating debugfs entries - disabling\n"); |
| 425 | "- disabling\n"); | ||
| 426 | global_disable = true; | 723 | global_disable = true; |
| 427 | 724 | ||
| 428 | return; | 725 | return; |
| @@ -432,14 +729,15 @@ void dma_debug_init(u32 num_entries) | |||
| 432 | num_entries = req_entries; | 729 | num_entries = req_entries; |
| 433 | 730 | ||
| 434 | if (prealloc_memory(num_entries) != 0) { | 731 | if (prealloc_memory(num_entries) != 0) { |
| 435 | printk(KERN_ERR "DMA-API: debugging out of memory error " | 732 | pr_err("DMA-API: debugging out of memory error - disabled\n"); |
| 436 | "- disabled\n"); | ||
| 437 | global_disable = true; | 733 | global_disable = true; |
| 438 | 734 | ||
| 439 | return; | 735 | return; |
| 440 | } | 736 | } |
| 441 | 737 | ||
| 442 | printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n"); | 738 | nr_total_entries = num_free_entries; |
| 739 | |||
| 740 | pr_info("DMA-API: debugging enabled by kernel config\n"); | ||
| 443 | } | 741 | } |
| 444 | 742 | ||
| 445 | static __init int dma_debug_cmdline(char *str) | 743 | static __init int dma_debug_cmdline(char *str) |
| @@ -448,8 +746,7 @@ static __init int dma_debug_cmdline(char *str) | |||
| 448 | return -EINVAL; | 746 | return -EINVAL; |
| 449 | 747 | ||
| 450 | if (strncmp(str, "off", 3) == 0) { | 748 | if (strncmp(str, "off", 3) == 0) { |
| 451 | printk(KERN_INFO "DMA-API: debugging disabled on kernel " | 749 | pr_info("DMA-API: debugging disabled on kernel command line\n"); |
| 452 | "command line\n"); | ||
| 453 | global_disable = true; | 750 | global_disable = true; |
| 454 | } | 751 | } |
| 455 | 752 | ||
| @@ -723,15 +1020,15 @@ void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, | |||
| 723 | entry->type = dma_debug_sg; | 1020 | entry->type = dma_debug_sg; |
| 724 | entry->dev = dev; | 1021 | entry->dev = dev; |
| 725 | entry->paddr = sg_phys(s); | 1022 | entry->paddr = sg_phys(s); |
| 726 | entry->size = s->length; | 1023 | entry->size = sg_dma_len(s); |
| 727 | entry->dev_addr = s->dma_address; | 1024 | entry->dev_addr = sg_dma_address(s); |
| 728 | entry->direction = direction; | 1025 | entry->direction = direction; |
| 729 | entry->sg_call_ents = nents; | 1026 | entry->sg_call_ents = nents; |
| 730 | entry->sg_mapped_ents = mapped_ents; | 1027 | entry->sg_mapped_ents = mapped_ents; |
| 731 | 1028 | ||
| 732 | if (!PageHighMem(sg_page(s))) { | 1029 | if (!PageHighMem(sg_page(s))) { |
| 733 | check_for_stack(dev, sg_virt(s)); | 1030 | check_for_stack(dev, sg_virt(s)); |
| 734 | check_for_illegal_area(dev, sg_virt(s), s->length); | 1031 | check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s)); |
| 735 | } | 1032 | } |
| 736 | 1033 | ||
| 737 | add_dma_entry(entry); | 1034 | add_dma_entry(entry); |
| @@ -739,13 +1036,33 @@ void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, | |||
| 739 | } | 1036 | } |
| 740 | EXPORT_SYMBOL(debug_dma_map_sg); | 1037 | EXPORT_SYMBOL(debug_dma_map_sg); |
| 741 | 1038 | ||
| 1039 | static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s) | ||
| 1040 | { | ||
| 1041 | struct dma_debug_entry *entry, ref; | ||
| 1042 | struct hash_bucket *bucket; | ||
| 1043 | unsigned long flags; | ||
| 1044 | int mapped_ents; | ||
| 1045 | |||
| 1046 | ref.dev = dev; | ||
| 1047 | ref.dev_addr = sg_dma_address(s); | ||
| 1048 | ref.size = sg_dma_len(s), | ||
| 1049 | |||
| 1050 | bucket = get_hash_bucket(&ref, &flags); | ||
| 1051 | entry = hash_bucket_find(bucket, &ref); | ||
| 1052 | mapped_ents = 0; | ||
| 1053 | |||
| 1054 | if (entry) | ||
| 1055 | mapped_ents = entry->sg_mapped_ents; | ||
| 1056 | put_hash_bucket(bucket, &flags); | ||
| 1057 | |||
| 1058 | return mapped_ents; | ||
| 1059 | } | ||
| 1060 | |||
| 742 | void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, | 1061 | void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 743 | int nelems, int dir) | 1062 | int nelems, int dir) |
| 744 | { | 1063 | { |
| 745 | struct dma_debug_entry *entry; | ||
| 746 | struct scatterlist *s; | 1064 | struct scatterlist *s; |
| 747 | int mapped_ents = 0, i; | 1065 | int mapped_ents = 0, i; |
| 748 | unsigned long flags; | ||
| 749 | 1066 | ||
| 750 | if (unlikely(global_disable)) | 1067 | if (unlikely(global_disable)) |
| 751 | return; | 1068 | return; |
| @@ -756,8 +1073,8 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, | |||
| 756 | .type = dma_debug_sg, | 1073 | .type = dma_debug_sg, |
| 757 | .dev = dev, | 1074 | .dev = dev, |
| 758 | .paddr = sg_phys(s), | 1075 | .paddr = sg_phys(s), |
| 759 | .dev_addr = s->dma_address, | 1076 | .dev_addr = sg_dma_address(s), |
| 760 | .size = s->length, | 1077 | .size = sg_dma_len(s), |
| 761 | .direction = dir, | 1078 | .direction = dir, |
| 762 | .sg_call_ents = 0, | 1079 | .sg_call_ents = 0, |
| 763 | }; | 1080 | }; |
| @@ -765,14 +1082,9 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, | |||
| 765 | if (mapped_ents && i >= mapped_ents) | 1082 | if (mapped_ents && i >= mapped_ents) |
| 766 | break; | 1083 | break; |
| 767 | 1084 | ||
| 768 | if (mapped_ents == 0) { | 1085 | if (!i) { |
| 769 | struct hash_bucket *bucket; | ||
| 770 | ref.sg_call_ents = nelems; | 1086 | ref.sg_call_ents = nelems; |
| 771 | bucket = get_hash_bucket(&ref, &flags); | 1087 | mapped_ents = get_nr_mapped_entries(dev, s); |
| 772 | entry = hash_bucket_find(bucket, &ref); | ||
| 773 | if (entry) | ||
| 774 | mapped_ents = entry->sg_mapped_ents; | ||
| 775 | put_hash_bucket(bucket, &flags); | ||
| 776 | } | 1088 | } |
| 777 | 1089 | ||
| 778 | check_unmap(&ref); | 1090 | check_unmap(&ref); |
| @@ -874,14 +1186,20 @@ void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, | |||
| 874 | int nelems, int direction) | 1186 | int nelems, int direction) |
| 875 | { | 1187 | { |
| 876 | struct scatterlist *s; | 1188 | struct scatterlist *s; |
| 877 | int i; | 1189 | int mapped_ents = 0, i; |
| 878 | 1190 | ||
| 879 | if (unlikely(global_disable)) | 1191 | if (unlikely(global_disable)) |
| 880 | return; | 1192 | return; |
| 881 | 1193 | ||
| 882 | for_each_sg(sg, s, nelems, i) { | 1194 | for_each_sg(sg, s, nelems, i) { |
| 883 | check_sync(dev, s->dma_address, s->dma_length, 0, | 1195 | if (!i) |
| 884 | direction, true); | 1196 | mapped_ents = get_nr_mapped_entries(dev, s); |
| 1197 | |||
| 1198 | if (i >= mapped_ents) | ||
| 1199 | break; | ||
| 1200 | |||
| 1201 | check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0, | ||
| 1202 | direction, true); | ||
| 885 | } | 1203 | } |
| 886 | } | 1204 | } |
| 887 | EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); | 1205 | EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu); |
| @@ -890,15 +1208,39 @@ void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, | |||
| 890 | int nelems, int direction) | 1208 | int nelems, int direction) |
| 891 | { | 1209 | { |
| 892 | struct scatterlist *s; | 1210 | struct scatterlist *s; |
| 893 | int i; | 1211 | int mapped_ents = 0, i; |
| 894 | 1212 | ||
| 895 | if (unlikely(global_disable)) | 1213 | if (unlikely(global_disable)) |
| 896 | return; | 1214 | return; |
| 897 | 1215 | ||
| 898 | for_each_sg(sg, s, nelems, i) { | 1216 | for_each_sg(sg, s, nelems, i) { |
| 899 | check_sync(dev, s->dma_address, s->dma_length, 0, | 1217 | if (!i) |
| 900 | direction, false); | 1218 | mapped_ents = get_nr_mapped_entries(dev, s); |
| 1219 | |||
| 1220 | if (i >= mapped_ents) | ||
| 1221 | break; | ||
| 1222 | |||
| 1223 | check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0, | ||
| 1224 | direction, false); | ||
| 901 | } | 1225 | } |
| 902 | } | 1226 | } |
| 903 | EXPORT_SYMBOL(debug_dma_sync_sg_for_device); | 1227 | EXPORT_SYMBOL(debug_dma_sync_sg_for_device); |
| 904 | 1228 | ||
| 1229 | static int __init dma_debug_driver_setup(char *str) | ||
| 1230 | { | ||
| 1231 | int i; | ||
| 1232 | |||
| 1233 | for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) { | ||
| 1234 | current_driver_name[i] = *str; | ||
| 1235 | if (*str == 0) | ||
| 1236 | break; | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | if (current_driver_name[0]) | ||
| 1240 | pr_info("DMA-API: enable driver filter for driver [%s]\n", | ||
| 1241 | current_driver_name); | ||
| 1242 | |||
| 1243 | |||
| 1244 | return 1; | ||
| 1245 | } | ||
| 1246 | __setup("dma_debug_driver=", dma_debug_driver_setup); | ||
