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>
31 #define HASH_FN_MASK (HASH_SIZE - 1)
53 * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
54 * @list: node on pre-allocated free_entries list
55 * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
58 * @type: single, page, sg, coherent
94 /* List of pre-allocated dma_debug_entry's */
99 /* Global disable flag - will be set in case of an error */
125 /* per-driver filter related state */
142 [dma_debug_sg] = "scather-gather",
172 stack_trace_print(entry->stack_entries, entry->stack_len, 0); in dump_entry_trace()
188 if (current_driver && dev && dev->driver == current_driver) in driver_filter()
199 drv = dev->driver; in driver_filter()
207 if (drv->name && in driver_filter()
208 strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) { in driver_filter()
228 show_num_errors -= 1; \
234 * Every DMA-API request is saved into a struct dma_debug_entry. To
241 * We use bits 20-27 here as the index into the hash in hash_fn()
243 return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK; in hash_fn()
266 __releases(&bucket->lock) in put_hash_bucket()
268 spin_unlock_irqrestore(&bucket->lock, flags); in put_hash_bucket()
273 return ((a->dev_addr == b->dev_addr) && in exact_match()
274 (a->dev == b->dev)) ? true : false; in exact_match()
280 if (a->dev != b->dev) in containing_match()
283 if ((b->dev_addr <= a->dev_addr) && in containing_match()
284 ((b->dev_addr + b->size) >= (a->dev_addr + a->size))) in containing_match()
298 int matches = 0, match_lvl, last_lvl = -1; in __hash_bucket_find()
300 list_for_each_entry(entry, &bucket->list, list) { in __hash_bucket_find()
307 * same device addresses being put into the dma-debug in __hash_bucket_find()
310 * best-fit algorithm here which returns the entry from in __hash_bucket_find()
312 * instead of the first-fit. in __hash_bucket_find()
316 entry->size == ref->size ? ++match_lvl : 0; in __hash_bucket_find()
317 entry->type == ref->type ? ++match_lvl : 0; in __hash_bucket_find()
318 entry->direction == ref->direction ? ++match_lvl : 0; in __hash_bucket_find()
319 entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0; in __hash_bucket_find()
322 /* perfect-fit - return the result */ in __hash_bucket_find()
335 * If we have multiple matches but no perfect-fit, just return in __hash_bucket_find()
367 index.dev_addr -= (1 << HASH_FN_SHIFT); in bucket_find_contain()
380 list_add_tail(&entry->list, &bucket->list); in hash_bucket_add()
388 list_del(&entry->list); in hash_bucket_del()
393 if (entry->type == dma_debug_resource) in phys_addr()
394 return __pfn_to_phys(entry->pfn) + entry->offset; in phys_addr()
396 return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset; in phys_addr()
404 * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
412 * dma-debug entries in that we need a free dma_debug_entry before
421 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
422 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
427 return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) + in to_cacheline_number()
428 (entry->offset >> L1_CACHE_SHIFT); in to_cacheline_number()
435 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_read_overlap()
448 for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--) in active_cacheline_set_overlap()
464 * leaking dma-mappings. in active_cacheline_inc_overlap()
475 return active_cacheline_set_overlap(cln, --overlap); in active_cacheline_dec_overlap()
488 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_insert()
493 if (rc == -EEXIST) in active_cacheline_insert()
506 if (entry->direction == DMA_TO_DEVICE) in active_cacheline_remove()
512 * active_cacheline_dec_overlap() returns -1 in that case in active_cacheline_remove()
532 spin_lock_irqsave(&bucket->lock, flags); in debug_dma_dump_mappings()
533 list_for_each_entry(entry, &bucket->list, list) { in debug_dma_dump_mappings()
534 if (!dev || dev == entry->dev) { in debug_dma_dump_mappings()
536 dev_info(entry->dev, in debug_dma_dump_mappings()
538 type2name[entry->type], idx, in debug_dma_dump_mappings()
539 phys_addr(entry), entry->pfn, in debug_dma_dump_mappings()
540 entry->dev_addr, entry->size, in debug_dma_dump_mappings()
541 &cln, dir2name[entry->direction], in debug_dma_dump_mappings()
542 maperr2str[entry->map_err_type]); in debug_dma_dump_mappings()
545 spin_unlock_irqrestore(&bucket->lock, flags); in debug_dma_dump_mappings()
564 spin_lock_irqsave(&bucket->lock, flags); in dump_show()
565 list_for_each_entry(entry, &bucket->list, list) { in dump_show()
569 dev_driver_string(entry->dev), in dump_show()
570 dev_name(entry->dev), in dump_show()
571 type2name[entry->type], idx, in dump_show()
572 phys_addr(entry), entry->pfn, in dump_show()
573 entry->dev_addr, entry->size, in dump_show()
574 &cln, dir2name[entry->direction], in dump_show()
575 maperr2str[entry->map_err_type]); in dump_show()
577 spin_unlock_irqrestore(&bucket->lock, flags); in dump_show()
598 if (rc == -ENOMEM) { in add_dma_entry()
599 pr_err_once("cacheline tracking ENOMEM, dma-debug disabled\n"); in add_dma_entry()
601 } else if (rc == -EEXIST && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) { in add_dma_entry()
602 err_printk(entry->dev, entry, in add_dma_entry()
614 return -ENOMEM; in dma_debug_create_entries()
630 list_del(&entry->list); in __dma_entry_alloc()
633 num_free_entries -= 1; in __dma_entry_alloc()
673 pr_err("debugging out of memory - disabling\n"); in dma_entry_alloc()
688 entry->stack_len = stack_trace_save(entry->stack_entries, in dma_entry_alloc()
689 ARRAY_SIZE(entry->stack_entries), in dma_entry_alloc()
702 * add to beginning of the list - this way the entries are in dma_entry_free()
706 list_add(&entry->list, &free_entries); in dma_entry_free()
712 * DMA-API debugging init code
755 len = min(count, (size_t)(NAME_MAX_LEN - 1)); in filter_write()
757 return -EFAULT; in filter_write()
766 * - only use the first token we got in filter_write()
767 * - token delimiter is everything looking like a space in filter_write()
778 pr_info("switching off dma-debug driver filter\n"); in filter_write()
788 for (i = 0; i < NAME_MAX_LEN - 1; ++i) { in filter_write()
813 struct dentry *dentry = debugfs_create_dir("dma-api", NULL); in dma_debug_fs_init()
838 if (entry->dev == dev) { in device_dma_allocations()
869 count, entry->dev_addr, entry->size, in dma_debug_device_change()
870 dir2name[entry->direction], type2name[entry->type]); in dma_debug_device_change()
892 nb->notifier_call = dma_debug_device_change; in dma_debug_add_bus()
921 pr_err("debugging out of memory error - disabled\n"); in dma_debug_init()
938 return -EINVAL; in dma_debug_cmdline()
951 return -EINVAL; in dma_debug_entries_cmdline()
973 if (dma_mapping_error(ref->dev, ref->dev_addr)) { in check_unmap()
974 err_printk(ref->dev, NULL, in check_unmap()
978 err_printk(ref->dev, NULL, in check_unmap()
982 ref->dev_addr, ref->size); in check_unmap()
987 if (ref->size != entry->size) { in check_unmap()
988 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
992 ref->dev_addr, entry->size, ref->size); in check_unmap()
995 if (ref->type != entry->type) { in check_unmap()
996 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1000 ref->dev_addr, ref->size, in check_unmap()
1001 type2name[entry->type], type2name[ref->type]); in check_unmap()
1002 } else if ((entry->type == dma_debug_coherent) && in check_unmap()
1004 err_printk(ref->dev, entry, "device driver frees " in check_unmap()
1009 ref->dev_addr, ref->size, 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()
1063 /* Stack is direct-mapped. */ in check_for_stack()
1073 for (i = 0; i < stack_vm_area->nr_pages; i++) { in check_for_stack()
1074 if (page != stack_vm_area->pages[i]) in check_for_stack()
1077 addr = (u8 *)current->stack + i * PAGE_SIZE + offset; in check_for_stack()
1107 (unsigned long long)ref->dev_addr, ref->size); in check_sync()
1111 if (ref->size > entry->size) { in check_sync()
1117 entry->dev_addr, entry->size, in check_sync()
1118 ref->size); in check_sync()
1121 if (entry->direction == DMA_BIDIRECTIONAL) in check_sync()
1124 if (ref->direction != entry->direction) { in check_sync()
1129 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1130 dir2name[entry->direction], in check_sync()
1131 dir2name[ref->direction]); in check_sync()
1134 if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) && in check_sync()
1135 !(ref->direction == DMA_TO_DEVICE)) in check_sync()
1137 "device read-only DMA memory for cpu " in check_sync()
1140 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1141 dir2name[entry->direction], in check_sync()
1142 dir2name[ref->direction]); in check_sync()
1144 if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) && in check_sync()
1145 !(ref->direction == DMA_FROM_DEVICE)) in check_sync()
1147 "device write-only DMA memory to device " in check_sync()
1150 (unsigned long long)ref->dev_addr, entry->size, in check_sync()
1151 dir2name[entry->direction], in check_sync()
1152 dir2name[ref->direction]); in check_sync()
1154 if (ref->sg_call_ents && ref->type == dma_debug_sg && in check_sync()
1155 ref->sg_call_ents != entry->sg_call_ents) { in check_sync()
1156 err_printk(ref->dev, entry, "device driver syncs " in check_sync()
1157 "DMA sg list with different entry count " in check_sync()
1159 entry->sg_call_ents, ref->sg_call_ents); in check_sync()
1166 static void check_sg_segment(struct device *dev, struct scatterlist *sg) in check_sg_segment() argument
1176 if (sg->length > max_seg) in check_sg_segment()
1177 …err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n… in check_sg_segment()
1178 sg->length, max_seg); in check_sg_segment()
1184 start = sg_dma_address(sg); in check_sg_segment()
1185 end = start + sg_dma_len(sg) - 1; in check_sg_segment()
1187 …err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [bound… in check_sg_segment()
1224 entry->dev = dev; in debug_dma_map_page()
1225 entry->type = dma_debug_single; in debug_dma_map_page()
1226 entry->pfn = page_to_pfn(page); in debug_dma_map_page()
1227 entry->offset = offset; in debug_dma_map_page()
1228 entry->dev_addr = dma_addr; in debug_dma_map_page()
1229 entry->size = size; in debug_dma_map_page()
1230 entry->direction = direction; in debug_dma_map_page()
1231 entry->map_err_type = MAP_ERR_NOT_CHECKED; in debug_dma_map_page()
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()
1310 check_for_stack(dev, sg_page(s), s->offset); 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->pfn = page_to_pfn(sg_page(s)); in debug_dma_map_sg()
1323 entry->offset = s->offset; in debug_dma_map_sg()
1324 entry->size = sg_dma_len(s); in debug_dma_map_sg()
1325 entry->dev_addr = sg_dma_address(s); in debug_dma_map_sg()
1326 entry->direction = direction; in debug_dma_map_sg()
1327 entry->sg_call_ents = nents; in debug_dma_map_sg()
1328 entry->sg_mapped_ents = mapped_ents; in debug_dma_map_sg()
1349 mapped_ents = entry->sg_mapped_ents; in get_nr_mapped_entries()
1370 .offset = s->offset, in debug_dma_unmap_sg()
1407 entry->type = dma_debug_coherent; in debug_dma_alloc_coherent()
1408 entry->dev = dev; in debug_dma_alloc_coherent()
1409 entry->offset = offset_in_page(virt); in debug_dma_alloc_coherent()
1410 entry->size = size; in debug_dma_alloc_coherent()
1411 entry->dev_addr = dma_addr; in debug_dma_alloc_coherent()
1412 entry->direction = DMA_BIDIRECTIONAL; in debug_dma_alloc_coherent()
1415 entry->pfn = vmalloc_to_pfn(virt); in debug_dma_alloc_coherent()
1417 entry->pfn = page_to_pfn(virt_to_page(virt)); in debug_dma_alloc_coherent()
1462 entry->type = dma_debug_resource; in debug_dma_map_resource()
1463 entry->dev = dev; in debug_dma_map_resource()
1464 entry->pfn = PHYS_PFN(addr); in debug_dma_map_resource()
1465 entry->offset = offset_in_page(addr); in debug_dma_map_resource()
1466 entry->size = size; in debug_dma_map_resource()
1467 entry->dev_addr = dma_addr; in debug_dma_map_resource()
1468 entry->direction = direction; in debug_dma_map_resource()
1469 entry->map_err_type = MAP_ERR_NOT_CHECKED; in debug_dma_map_resource()
1528 void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, in debug_dma_sync_sg_for_cpu() argument
1537 for_each_sg(sg, s, nelems, i) { in debug_dma_sync_sg_for_cpu()
1543 .offset = s->offset, in debug_dma_sync_sg_for_cpu()
1560 void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, in debug_dma_sync_sg_for_device() argument
1569 for_each_sg(sg, s, nelems, i) { in debug_dma_sync_sg_for_device()
1575 .offset = s->offset, in debug_dma_sync_sg_for_device()
1595 for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) { in dma_debug_driver_setup()