Lines Matching +full:sg +full:- +full:micro
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
8 #define pr_fmt(fmt) "DMA-API: " fmt
12 #include <linux/dma-map-ops.h>
32 #define HASH_FN_MASK (HASH_SIZE - 1)
55 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
56 * @list: node on pre-allocated free_entries list
57 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
60 * @type: single, page, sg, coherent
95 /* List of pre-allocated dma_debug_entry's */
100 /* Global disable flag - will be set in case of an error */
126 /* per-driver filter related state */
143 [dma_debug_sg] = "scatter-gather",
174 stack_trace_print(entry->stack_entries, entry->stack_len, 0); in dump_entry_trace()
190 if (current_driver && dev && dev->driver == current_driver) in driver_filter()
201 drv = dev->driver; in driver_filter()
209 if (drv->name && in driver_filter()
210 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) { in driver_filter()
230 show_num_errors -= 1; \
236 * Every DMA-API request is saved into a struct dma_debug_entry. To
243 * We use bits 20-27 here as the index into the hash in hash_fn()
245 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; in hash_fn()
268 __releases(&bucket->lock) in put_hash_bucket()
270 spin_unlock_irqrestore(&bucket->lock, flags); in put_hash_bucket()
275 return ((a->dev_addr == b->dev_addr) && in exact_match()
276 (a->dev == b->dev)) ? true : false; in exact_match()
282 if (a->dev != b->dev) in containing_match()
285 if ((b->dev_addr <= a->dev_addr) && in containing_match()
286 ((b->dev_addr + b->size) >= (a->dev_addr + a->size))) in containing_match()
300 int matches = 0, match_lvl, last_lvl = -1; in __hash_bucket_find()
302 list_for_each_entry(entry, &bucket->list, list) { in __hash_bucket_find()
309 * same device addresses being put into the dma-debug in __hash_bucket_find()
312 * best-fit algorithm here which returns the entry from in __hash_bucket_find()
314 * instead of the first-fit. in __hash_bucket_find()
318 entry->size == ref->size ? ++match_lvl : 0; in __hash_bucket_find()
319 entry->type == ref->type ? ++match_lvl : 0; in __hash_bucket_find()
320 entry->direction == ref->direction ? ++match_lvl : 0; in __hash_bucket_find()
321 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0; in __hash_bucket_find()
324 /* perfect-fit - return the result */ in __hash_bucket_find()
337 * If we have multiple matches but no perfect-fit, just return in __hash_bucket_find()
369 index.dev_addr -= (1 << HASH_FN_SHIFT); in bucket_find_contain()
382 list_add_tail(&entry->list, &bucket->list); in hash_bucket_add()
390 list_del(&entry->list); in hash_bucket_del()
398 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
406 * dma-debug entries in that we need a free dma_debug_entry before
418 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
419 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
424 return ((entry->paddr >> PAGE_SHIFT) << CACHELINE_PER_PAGE_SHIFT) + in to_cacheline_number()
425 (offset_in_page(entry->paddr) >> L1_CACHE_SHIFT); in to_cacheline_number()
432 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_read_overlap()
445 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_set_overlap()
461 * leaking dma-mappings. in active_cacheline_inc_overlap()
472 return active_cacheline_set_overlap(cln, --overlap); in active_cacheline_dec_overlap()
485 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_insert()
490 if (rc == -EEXIST) in active_cacheline_insert()
503 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_remove()
509 * active_cacheline_dec_overlap() returns -1 in that case in active_cacheline_remove()
529 spin_lock_irqsave(&bucket->lock, flags); in debug_dma_dump_mappings()
530 list_for_each_entry(entry, &bucket->list, list) { in debug_dma_dump_mappings()
531 if (!dev || dev == entry->dev) { in debug_dma_dump_mappings()
533 dev_info(entry->dev, in debug_dma_dump_mappings()
535 type2name[entry->type], idx, in debug_dma_dump_mappings()
536 &entry->paddr, entry->dev_addr, in debug_dma_dump_mappings()
537 entry->size, &cln, in debug_dma_dump_mappings()
538 dir2name[entry->direction], in debug_dma_dump_mappings()
539 maperr2str[entry->map_err_type]); in debug_dma_dump_mappings()
542 spin_unlock_irqrestore(&bucket->lock, flags); in debug_dma_dump_mappings()
561 spin_lock_irqsave(&bucket->lock, flags); in dump_show()
562 list_for_each_entry(entry, &bucket->list, list) { in dump_show()
566 dev_driver_string(entry->dev), in dump_show()
567 dev_name(entry->dev), in dump_show()
568 type2name[entry->type], idx, in dump_show()
569 &entry->paddr, entry->dev_addr, in dump_show()
570 entry->size, &cln, in dump_show()
571 dir2name[entry->direction], in dump_show()
572 maperr2str[entry->map_err_type]); in dump_show()
574 spin_unlock_irqrestore(&bucket->lock, flags); in dump_show()
595 if (rc == -ENOMEM) { in add_dma_entry()
596 pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n"); in add_dma_entry()
598 } else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) && in add_dma_entry()
600 is_swiotlb_active(entry->dev))) { in add_dma_entry()
601 err_printk(entry->dev, entry, in add_dma_entry()
613 return -ENOMEM; in dma_debug_create_entries()
629 list_del(&entry->list); in __dma_entry_alloc()
632 num_free_entries -= 1; in __dma_entry_alloc()
672 pr_err("debugging out of memory - disabling\n"); in dma_entry_alloc()
687 entry->stack_len = stack_trace_save(entry->stack_entries, in dma_entry_alloc()
688 ARRAY_SIZE(entry->stack_entries), in dma_entry_alloc()
701 * add to beginning of the list - this way the entries are in dma_entry_free()
705 list_add(&entry->list, &free_entries); in dma_entry_free()
711 * DMA-API debugging init code
754 len = min(count, (size_t)(NAME_MAX_LEN - 1)); in filter_write()
756 return -EFAULT; in filter_write()
765 * - only use the first token we got in filter_write()
766 * - token delimiter is everything looking like a space in filter_write()
777 pr_info("switching off dma-debug driver filter\n"); in filter_write()
787 for (i = 0; i < NAME_MAX_LEN - 1; ++i) { in filter_write()
812 struct dentry *dentry = debugfs_create_dir("dma-api", NULL); in dma_debug_fs_init()
837 if (entry->dev == dev) { in device_dma_allocations()
868 count, entry->dev_addr, entry->size, in dma_debug_device_change()
869 dir2name[entry->direction], type2name[entry->type]); in dma_debug_device_change()
891 nb->notifier_call = dma_debug_device_change; in dma_debug_add_bus()
920 pr_err("debugging out of memory error - disabled\n"); in dma_debug_init()
937 return -EINVAL; in dma_debug_cmdline()
950 return -EINVAL; in dma_debug_entries_cmdline()
972 if (dma_mapping_error(ref->dev, ref->dev_addr)) { in check_unmap()
973 err_printk(ref->dev, NULL, in check_unmap()
977 err_printk(ref->dev, NULL, in check_unmap()
981 ref->dev_addr, ref->size); in check_unmap()
986 if (ref->size != entry->size) { in check_unmap()
987 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
991 ref->dev_addr, entry->size, ref->size); in check_unmap()
994 if (ref->type != entry->type) { in check_unmap()
995 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
999 ref->dev_addr, ref->size, in check_unmap()
1000 type2name[entry->type], type2name[ref->type]); in check_unmap()
1001 } else if ((entry->type == dma_debug_coherent || in check_unmap()
1002 entry->type == dma_debug_noncoherent) && in check_unmap()
1003 ref->paddr != entry->paddr) { in check_unmap()
1004 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1009 ref->dev_addr, ref->size, in check_unmap()
1010 &entry->paddr, in check_unmap()
1011 &ref->paddr); in check_unmap()
1014 if (ref->sg_call_ents && ref->type == dma_debug_sg && in check_unmap()
1015 ref->sg_call_ents != entry->sg_call_ents) { in check_unmap()
1016 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1017 "DMA sg list with different entry count " in check_unmap()
1019 entry->sg_call_ents, ref->sg_call_ents); in check_unmap()
1023 * This may be no bug in reality - but most implementations of the in check_unmap()
1026 if (ref->direction != entry->direction) { in check_unmap()
1027 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1031 ref->dev_addr, ref->size, in check_unmap()
1032 dir2name[entry->direction], in check_unmap()
1033 dir2name[ref->direction]); in check_unmap()
1039 * If not, print this warning message. See Documentation/core-api/dma-api.rst. in check_unmap()
1041 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { in check_unmap()
1042 err_printk(ref->dev, entry, in check_unmap()
1046 ref->dev_addr, ref->size, in check_unmap()
1047 type2name[entry->type]); in check_unmap()
1066 /* Stack is direct-mapped. */ in check_for_stack()
1076 for (i = 0; i < stack_vm_area->nr_pages; i++) { in check_for_stack()
1078 page_to_pfn(stack_vm_area->pages[i])) in check_for_stack()
1081 addr = (u8 *)current->stack + i * PAGE_SIZE + in check_for_stack()
1112 (unsigned long long)ref->dev_addr, ref->size); in check_sync()
1116 if (ref->size > entry->size) { in check_sync()
1122 entry->dev_addr, entry->size, in check_sync()
1123 ref->size); in check_sync()
1126 if (entry->direction == DMA_BIDIRECTIONAL) in check_sync()
1129 if (ref->direction != entry->direction) { in check_sync()
1134 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1135 dir2name[entry->direction], in check_sync()
1136 dir2name[ref->direction]); in check_sync()
1139 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && in check_sync()
1140 !(ref->direction == DMA_TO_DEVICE)) in check_sync()
1142 "device read-only DMA memory for cpu " in check_sync()
1145 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1146 dir2name[entry->direction], in check_sync()
1147 dir2name[ref->direction]); in check_sync()
1149 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && in check_sync()
1150 !(ref->direction == DMA_FROM_DEVICE)) in check_sync()
1152 "device write-only DMA memory to device " in check_sync()
1155 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1156 dir2name[entry->direction], in check_sync()
1157 dir2name[ref->direction]); in check_sync()
1159 if (ref->sg_call_ents && ref->type == dma_debug_sg && in check_sync()
1160 ref->sg_call_ents != entry->sg_call_ents) { in check_sync()
1161 err_printk(ref->dev, entry, "device driver syncs " in check_sync()
1162 "DMA sg list with different entry count " in check_sync()
1164 entry->sg_call_ents, ref->sg_call_ents); in check_sync()
1171 static void check_sg_segment(struct device *dev, struct scatterlist *sg) in check_sg_segment() argument
1180 if (sg->length > max_seg) in check_sg_segment()
1181 …err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n… in check_sg_segment()
1182 sg->length, max_seg); in check_sg_segment()
1188 start = sg_dma_address(sg); in check_sg_segment()
1189 end = start + sg_dma_len(sg) - 1; in check_sg_segment()
1191 …err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [bound… in check_sg_segment()
1226 entry->dev = dev; in debug_dma_map_phys()
1227 entry->type = dma_debug_phy; in debug_dma_map_phys()
1228 entry->paddr = phys; in debug_dma_map_phys()
1229 entry->dev_addr = dma_addr; in debug_dma_map_phys()
1230 entry->size = size; in debug_dma_map_phys()
1231 entry->direction = direction; in debug_dma_map_phys()
1232 entry->map_err_type = MAP_ERR_NOT_CHECKED; in debug_dma_map_phys()
1258 list_for_each_entry(entry, &bucket->list, list) { in debug_dma_mapping_error()
1265 * same device addresses being put into the dma-debug in debug_dma_mapping_error()
1268 * best-fit algorithm here which updates the first entry in debug_dma_mapping_error()
1272 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { in debug_dma_mapping_error()
1273 entry->map_err_type = MAP_ERR_CHECKED; in debug_dma_mapping_error()
1298 void debug_dma_map_sg(struct device *dev, struct scatterlist *sg, in debug_dma_map_sg() argument
1309 for_each_sg(sg, s, nents, i) { in debug_dma_map_sg()
1312 check_for_illegal_area(dev, sg_virt(s), s->length); in debug_dma_map_sg()
1315 for_each_sg(sg, s, mapped_ents, i) { in debug_dma_map_sg()
1320 entry->type = dma_debug_sg; in debug_dma_map_sg()
1321 entry->dev = dev; in debug_dma_map_sg()
1322 entry->paddr = sg_phys(s); in debug_dma_map_sg()
1323 entry->size = sg_dma_len(s); in debug_dma_map_sg()
1324 entry->dev_addr = sg_dma_address(s); in debug_dma_map_sg()
1325 entry->direction = direction; in debug_dma_map_sg()
1326 entry->sg_call_ents = nents; in debug_dma_map_sg()
1327 entry->sg_mapped_ents = mapped_ents; in debug_dma_map_sg()
1348 mapped_ents = entry->sg_mapped_ents; in get_nr_mapped_entries()
1417 entry->type = dma_debug_coherent; in debug_dma_alloc_coherent()
1418 entry->dev = dev; in debug_dma_alloc_coherent()
1419 entry->paddr = virt_to_paddr(virt); in debug_dma_alloc_coherent()
1420 entry->size = size; in debug_dma_alloc_coherent()
1421 entry->dev_addr = dma_addr; in debug_dma_alloc_coherent()
1422 entry->direction = DMA_BIDIRECTIONAL; in debug_dma_alloc_coherent()
1487 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, in debug_dma_sync_sg_for_cpu() argument
1496 for_each_sg(sg, s, nelems, i) { in debug_dma_sync_sg_for_cpu()
1518 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, in debug_dma_sync_sg_for_device() argument
1527 for_each_sg(sg, s, nelems, i) { in debug_dma_sync_sg_for_device()
1532 .paddr = sg_phys(sg), in debug_dma_sync_sg_for_device()
1562 entry->type = dma_debug_noncoherent; in debug_dma_alloc_pages()
1563 entry->dev = dev; in debug_dma_alloc_pages()
1564 entry->paddr = page_to_phys(page); in debug_dma_alloc_pages()
1565 entry->size = size; in debug_dma_alloc_pages()
1566 entry->dev_addr = dma_addr; in debug_dma_alloc_pages()
1567 entry->direction = direction; in debug_dma_alloc_pages()
1595 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) { in dma_debug_driver_setup()