Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2008 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Author: Joerg Roedel <joerg.roedel@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #define pr_fmt(fmt)	"DMA-API: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/dma-map-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define HASH_SIZE       16384ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define HASH_FN_SHIFT   13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define HASH_FN_MASK    (HASH_SIZE - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /* If the pool runs out, add this many new entries at once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define DMA_DEBUG_DYNAMIC_ENTRIES (PAGE_SIZE / sizeof(struct dma_debug_entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	dma_debug_single,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	dma_debug_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	dma_debug_coherent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	dma_debug_resource,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) enum map_err_types {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	MAP_ERR_CHECK_NOT_APPLICABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	MAP_ERR_NOT_CHECKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	MAP_ERR_CHECKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define DMA_DEBUG_STACKTRACE_ENTRIES 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * @list: node on pre-allocated free_entries list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * @size: length of the mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * @type: single, page, sg, coherent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * @direction: enum dma_data_direction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * @sg_call_ents: 'nents' from dma_map_sg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * @pfn: page frame of the start address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * @offset: offset of mapping relative to pfn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * @map_err_type: track whether dma_mapping_error() was checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * @stacktrace: support backtraces when a violation is detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) struct dma_debug_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct device    *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	u64              dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	u64              size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	int              type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	int              direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	int		 sg_call_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	int		 sg_mapped_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	unsigned long	 pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	size_t		 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	enum map_err_types  map_err_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #ifdef CONFIG_STACKTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	unsigned int	stack_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	unsigned long	stack_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) } ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) struct hash_bucket {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) /* Hash list to save the allocated dma addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static struct hash_bucket dma_entry_hash[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) /* List of pre-allocated dma_debug_entry's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) static LIST_HEAD(free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) /* Lock for the list above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static DEFINE_SPINLOCK(free_entries_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) /* Global disable flag - will be set in case of an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) static bool global_disable __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) /* Early initialization disable flag, set at the end of dma_debug_init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) static bool dma_debug_initialized __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static inline bool dma_debug_disabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	return global_disable || !dma_debug_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) /* Global error count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) static u32 error_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) /* Global error show enable*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static u32 show_all_errors __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) /* Number of errors to show */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static u32 show_num_errors = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static u32 num_free_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static u32 min_free_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) static u32 nr_total_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) /* number of preallocated entries requested by kernel cmdline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static u32 nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /* per-driver filter related state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define NAME_MAX_LEN	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static char                  current_driver_name[NAME_MAX_LEN] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) static struct device_driver *current_driver                    __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) static DEFINE_RWLOCK(driver_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static const char *const maperr2str[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	[MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	[MAP_ERR_NOT_CHECKED] = "dma map error not checked",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	[MAP_ERR_CHECKED] = "dma map error checked",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) static const char *type2name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	[dma_debug_single] = "single",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	[dma_debug_sg] = "scather-gather",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	[dma_debug_coherent] = "coherent",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	[dma_debug_resource] = "resource",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static const char *dir2name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	[DMA_BIDIRECTIONAL]	= "DMA_BIDIRECTIONAL",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	[DMA_TO_DEVICE]		= "DMA_TO_DEVICE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	[DMA_FROM_DEVICE]	= "DMA_FROM_DEVICE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	[DMA_NONE]		= "DMA_NONE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * The access to some variables in this macro is racy. We can't use atomic_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * here because all these variables are exported to debugfs. Some of them even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * writeable. This is also the reason why a lock won't help much. But anyway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * the races are no big deal. Here is why:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  *   error_count: the addition is racy, but the worst thing that can happen is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  *                that we don't count some errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  *   show_num_errors: the subtraction is racy. Also no big deal because in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  *                    worst case this will result in one warning more in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  *                    system log than the user configured. This variable is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  *                    writeable via debugfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static inline void dump_entry_trace(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) #ifdef CONFIG_STACKTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		pr_warn("Mapped at:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		stack_trace_print(entry->stack_entries, entry->stack_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static bool driver_filter(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct device_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	/* driver filter off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (likely(!current_driver_name[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	/* driver filter on and initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	if (current_driver && dev && dev->driver == current_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	/* driver filter on, but we can't filter on a NULL device... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	if (current_driver || !current_driver_name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	/* driver filter on but not yet initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	drv = dev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	/* lock to protect against change of current_driver_name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	read_lock_irqsave(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	if (drv->name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	    strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		current_driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	read_unlock_irqrestore(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) #define err_printk(dev, entry, format, arg...) do {			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		error_count += 1;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		if (driver_filter(dev) &&				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		    (show_all_errors || show_num_errors > 0)) {		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			WARN(1, pr_fmt("%s %s: ") format,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			     dev ? dev_driver_string(dev) : "NULL",	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			     dev ? dev_name(dev) : "NULL", ## arg);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			dump_entry_trace(entry);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		if (!show_all_errors && show_num_errors > 0)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			show_num_errors -= 1;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	} while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * Hash related functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * Every DMA-API request is saved into a struct dma_debug_entry. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  * have quick access to these structs they are stored into a hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) static int hash_fn(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * Hash function is based on the dma address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 * We use bits 20-27 here as the index into the hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * Request exclusive access to a hash bucket for a given dma_debug_entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 					   unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	__acquires(&dma_entry_hash[idx].lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	int idx = hash_fn(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	unsigned long __flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	*flags = __flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return &dma_entry_hash[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * Give up exclusive access to the hash bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) static void put_hash_bucket(struct hash_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			    unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	__releases(&bucket->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	spin_unlock_irqrestore(&bucket->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return ((a->dev_addr == b->dev_addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		(a->dev == b->dev)) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) static bool containing_match(struct dma_debug_entry *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			     struct dma_debug_entry *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (a->dev != b->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	if ((b->dev_addr <= a->dev_addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	    ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  * Search a given entry in the hash bucket list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 						  struct dma_debug_entry *ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 						  match_fn match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	struct dma_debug_entry *entry, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	int matches = 0, match_lvl, last_lvl = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	list_for_each_entry(entry, &bucket->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		if (!match(ref, entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		 * Some drivers map the same physical address multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		 * times. Without a hardware IOMMU this results in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		 * same device addresses being put into the dma-debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		 * hash multiple times too. This can result in false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		 * positives being reported. Therefore we implement a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		 * best-fit algorithm here which returns the entry from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		 * the hash which fits best to the reference value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		 * instead of the first-fit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		matches += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		match_lvl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		entry->size         == ref->size         ? ++match_lvl : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		entry->type         == ref->type         ? ++match_lvl : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		entry->direction    == ref->direction    ? ++match_lvl : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (match_lvl == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			/* perfect-fit - return the result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		} else if (match_lvl > last_lvl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			 * We found an entry that fits better then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			 * previous one or it is the 1st match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			last_lvl = match_lvl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 			ret      = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * If we have multiple matches but no perfect-fit, just return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 * NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ret = (matches == 1) ? ret : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 						 struct dma_debug_entry *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	return __hash_bucket_find(bucket, ref, exact_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 						   struct dma_debug_entry *ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 						   unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	unsigned int max_range = dma_get_max_seg_size(ref->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	struct dma_debug_entry *entry, index = *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	unsigned int range = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	while (range <= max_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		entry = __hash_bucket_find(*bucket, ref, containing_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		 * Nothing found, go back a hash bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		put_hash_bucket(*bucket, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		range          += (1 << HASH_FN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		index.dev_addr -= (1 << HASH_FN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		*bucket = get_hash_bucket(&index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * Add an entry to a hash bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static void hash_bucket_add(struct hash_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 			    struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	list_add_tail(&entry->list, &bucket->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385)  * Remove entry from a hash bucket list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) static void hash_bucket_del(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) static unsigned long long phys_addr(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (entry->type == dma_debug_resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return __pfn_to_phys(entry->pfn) + entry->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  * Dump mapping entries for debugging purposes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) void debug_dma_dump_mappings(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	for (idx = 0; idx < HASH_SIZE; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		struct hash_bucket *bucket = &dma_entry_hash[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		spin_lock_irqsave(&bucket->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		list_for_each_entry(entry, &bucket->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			if (!dev || dev == entry->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				dev_info(entry->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 					 "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 					 type2name[entry->type], idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 					 phys_addr(entry), entry->pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 					 entry->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 					 dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 					 maperr2str[entry->map_err_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		spin_unlock_irqrestore(&bucket->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * For each mapping (initial cacheline in the case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * scatterlist, or the cacheline specified in dma_map_single) insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * into this tree using the cacheline as the key. At
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  * the entry already exists at insertion time add a tag as a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * count for the overlapping mappings.  For now, the overlap tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * just ensures that 'unmaps' balance 'maps' before marking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  * cacheline idle, but we should also be flagging overlaps as an API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  * violation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  * Memory usage is mostly constrained by the maximum number of available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444)  * dma-debug entries in that we need a free dma_debug_entry before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  * inserting into the tree.  In the case of dma_map_page and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446)  * dma_alloc_coherent there is only one dma_debug_entry and one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447)  * dma_active_cacheline entry to track per event.  dma_map_sg(), on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * other hand, consumes a single dma_debug_entry, but inserts 'nents'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * entries into the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) static DEFINE_SPINLOCK(radix_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		(entry->offset >> L1_CACHE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static int active_cacheline_read_overlap(phys_addr_t cln)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	int overlap = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			overlap |= 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	return overlap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		return overlap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		if (overlap & 1 << i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			radix_tree_tag_set(&dma_active_cacheline, cln, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			radix_tree_tag_clear(&dma_active_cacheline, cln, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return overlap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static void active_cacheline_inc_overlap(phys_addr_t cln)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	int overlap = active_cacheline_read_overlap(cln);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	overlap = active_cacheline_set_overlap(cln, ++overlap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	/* If we overflowed the overlap counter then we're potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 * leaking dma-mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		  pr_fmt("exceeded %d overlapping mappings of cacheline %pa\n"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		  ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) static int active_cacheline_dec_overlap(phys_addr_t cln)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	int overlap = active_cacheline_read_overlap(cln);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	return active_cacheline_set_overlap(cln, --overlap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static int active_cacheline_insert(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	phys_addr_t cln = to_cacheline_number(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	/* If the device is not writing memory then we don't have any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	 * concerns about the cpu consuming stale data.  This mitigates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 * legitimate usages of overlapping mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	if (entry->direction == DMA_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	spin_lock_irqsave(&radix_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (rc == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		active_cacheline_inc_overlap(cln);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	spin_unlock_irqrestore(&radix_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static void active_cacheline_remove(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	phys_addr_t cln = to_cacheline_number(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	/* ...mirror the insert case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (entry->direction == DMA_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	spin_lock_irqsave(&radix_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	/* since we are counting overlaps the final put of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 * cacheline will occur when the overlap count is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * active_cacheline_dec_overlap() returns -1 in that case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (active_cacheline_dec_overlap(cln) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		radix_tree_delete(&dma_active_cacheline, cln);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	spin_unlock_irqrestore(&radix_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  * Wrapper function for adding an entry to the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553)  * This function takes care of locking itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) static void add_dma_entry(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	struct hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	bucket = get_hash_bucket(entry, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	hash_bucket_add(bucket, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	rc = active_cacheline_insert(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (rc == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		pr_err("cacheline tracking ENOMEM, dma-debug disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		global_disable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	/* TODO: report -EEXIST errors here as overlapping mappings are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	 * not supported by the DMA API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) static int dma_debug_create_entries(gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	entry = (void *)get_zeroed_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	for (i = 0; i < DMA_DEBUG_DYNAMIC_ENTRIES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		list_add_tail(&entry[i].list, &free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	num_free_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	nr_total_entries += DMA_DEBUG_DYNAMIC_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) static struct dma_debug_entry *__dma_entry_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	entry = list_entry(free_entries.next, struct dma_debug_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	memset(entry, 0, sizeof(*entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	num_free_entries -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	if (num_free_entries < min_free_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		min_free_entries = num_free_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) static void __dma_entry_alloc_check_leak(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	u32 tmp = nr_total_entries % nr_prealloc_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	/* Shout each time we tick over some multiple of the initial pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (tmp < DMA_DEBUG_DYNAMIC_ENTRIES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		pr_info("dma_debug_entry pool grown to %u (%u00%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			nr_total_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			(nr_total_entries / nr_prealloc_entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) /* struct dma_entry allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623)  * The next two functions implement the allocator for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624)  * struct dma_debug_entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) static struct dma_debug_entry *dma_entry_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	spin_lock_irqsave(&free_entries_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if (num_free_entries == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		if (dma_debug_create_entries(GFP_ATOMIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			global_disable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			spin_unlock_irqrestore(&free_entries_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			pr_err("debugging out of memory - disabling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		__dma_entry_alloc_check_leak();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	entry = __dma_entry_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	spin_unlock_irqrestore(&free_entries_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) #ifdef CONFIG_STACKTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	entry->stack_len = stack_trace_save(entry->stack_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 					    ARRAY_SIZE(entry->stack_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 					    1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static void dma_entry_free(struct dma_debug_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	active_cacheline_remove(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	 * add to beginning of the list - this way the entries are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	 * more likely cache hot when they are reallocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	spin_lock_irqsave(&free_entries_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	list_add(&entry->list, &free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	num_free_entries += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	spin_unlock_irqrestore(&free_entries_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  * DMA-API debugging init code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  * The init code does two things:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  *   1. Initialize core data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  *   2. Preallocate a given number of dma_debug_entry structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) static ssize_t filter_read(struct file *file, char __user *user_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			   size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	char buf[NAME_MAX_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (!current_driver_name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	 * We can't copy to userspace directly because current_driver_name can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 * only be read under the driver_name_lock with irqs disabled. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 * create a temporary copy first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	read_lock_irqsave(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	read_unlock_irqrestore(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) static ssize_t filter_write(struct file *file, const char __user *userbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			    size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	char buf[NAME_MAX_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	 * We can't copy from userspace directly. Access to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	 * current_driver_name is protected with a write_lock with irqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	 * disabled. Since copy_from_user can fault and may sleep we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	 * need to copy to temporary buffer first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	len = min(count, (size_t)(NAME_MAX_LEN - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	if (copy_from_user(buf, userbuf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	buf[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	write_lock_irqsave(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	 * Now handle the string we got from userspace very carefully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	 * The rules are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	 *         - only use the first token we got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	 *         - token delimiter is everything looking like a space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 *           character (' ', '\n', '\t' ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (!isalnum(buf[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		 * If the first character userspace gave us is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * alphanumerical then assume the filter should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * switched off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		if (current_driver_name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			pr_info("switching off dma-debug driver filter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		current_driver_name[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		current_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 * Now parse out the first token and use it as the name for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	 * driver to filter for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		current_driver_name[i] = buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	current_driver_name[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	current_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	pr_info("enable driver filter for driver [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		current_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	write_unlock_irqrestore(&driver_name_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static const struct file_operations filter_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	.read  = filter_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	.write = filter_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	.llseek = default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static int dump_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	for (idx = 0; idx < HASH_SIZE; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		struct hash_bucket *bucket = &dma_entry_hash[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		spin_lock_irqsave(&bucket->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		list_for_each_entry(entry, &bucket->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				   "%s %s %s idx %d P=%llx N=%lx D=%llx L=%llx %s %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 				   dev_name(entry->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 				   dev_driver_string(entry->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 				   type2name[entry->type], idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 				   phys_addr(entry), entry->pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 				   entry->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 				   dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 				   maperr2str[entry->map_err_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		spin_unlock_irqrestore(&bucket->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) DEFINE_SHOW_ATTRIBUTE(dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) static int __init dma_debug_fs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	debugfs_create_bool("disabled", 0444, dentry, &global_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	debugfs_create_u32("error_count", 0444, dentry, &error_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	debugfs_create_u32("all_errors", 0644, dentry, &show_all_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	debugfs_create_u32("num_errors", 0644, dentry, &show_num_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	debugfs_create_u32("num_free_entries", 0444, dentry, &num_free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	debugfs_create_u32("min_free_entries", 0444, dentry, &min_free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	debugfs_create_u32("nr_total_entries", 0444, dentry, &nr_total_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	debugfs_create_file("driver_filter", 0644, dentry, NULL, &filter_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	debugfs_create_file("dump", 0444, dentry, NULL, &dump_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) core_initcall_sync(dma_debug_fs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	int count = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	for (i = 0; i < HASH_SIZE; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			if (entry->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 				count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				*out_entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	struct device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (dma_debug_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	case BUS_NOTIFY_UNBOUND_DRIVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		count = device_dma_allocations(dev, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		err_printk(dev, entry, "device driver has pending "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				"DMA allocations while released from device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 				"[count=%d]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 				"One of leaked entries details: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 				"[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 				"[mapped with %s] [mapped as %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			count, entry->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			dir2name[entry->direction], type2name[entry->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) void dma_debug_add_bus(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct notifier_block *nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	if (dma_debug_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (nb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		pr_err("dma_debug_add_bus: out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	nb->notifier_call = dma_debug_device_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	bus_register_notifier(bus, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) static int dma_debug_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	int i, nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	/* Do not use dma_debug_initialized here, since we really want to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	 * called to set dma_debug_initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (global_disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	for (i = 0; i < HASH_SIZE; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		INIT_LIST_HEAD(&dma_entry_hash[i].list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		spin_lock_init(&dma_entry_hash[i].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	nr_pages = DIV_ROUND_UP(nr_prealloc_entries, DMA_DEBUG_DYNAMIC_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	for (i = 0; i < nr_pages; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		dma_debug_create_entries(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	if (num_free_entries >= nr_prealloc_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		pr_info("preallocated %d debug entries\n", nr_total_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	} else if (num_free_entries > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		pr_warn("%d debug entries requested but only %d allocated\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			nr_prealloc_entries, nr_total_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		pr_err("debugging out of memory error - disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		global_disable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	min_free_entries = num_free_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	dma_debug_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	pr_info("debugging enabled by kernel config\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) core_initcall(dma_debug_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) static __init int dma_debug_cmdline(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (strncmp(str, "off", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		pr_info("debugging disabled on kernel command line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		global_disable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) static __init int dma_debug_entries_cmdline(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (!get_option(&str, &nr_prealloc_entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) __setup("dma_debug=", dma_debug_cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) __setup("dma_debug_entries=", dma_debug_entries_cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) static void check_unmap(struct dma_debug_entry *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	bucket = get_hash_bucket(ref, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	entry = bucket_find_exact(bucket, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		/* must drop lock before calling dma_mapping_error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		if (dma_mapping_error(ref->dev, ref->dev_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			err_printk(ref->dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				   "device driver tries to free an "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				   "invalid DMA memory address\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			err_printk(ref->dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 				   "device driver tries to free DMA "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 				   "memory it has not allocated [device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 				   "address=0x%016llx] [size=%llu bytes]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 				   ref->dev_addr, ref->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	if (ref->size != entry->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		err_printk(ref->dev, entry, "device driver frees "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			   "DMA memory with different size "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			   "[device address=0x%016llx] [map size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			   "[unmap size=%llu bytes]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			   ref->dev_addr, entry->size, ref->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (ref->type != entry->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		err_printk(ref->dev, entry, "device driver frees "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			   "DMA memory with wrong function "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			   "[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			   "[mapped as %s] [unmapped as %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			   ref->dev_addr, ref->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			   type2name[entry->type], type2name[ref->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	} else if ((entry->type == dma_debug_coherent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		   (phys_addr(ref) != phys_addr(entry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		err_printk(ref->dev, entry, "device driver frees "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			   "DMA memory with different CPU address "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			   "[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			   "[cpu alloc address=0x%016llx] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			   "[cpu free address=0x%016llx]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			   ref->dev_addr, ref->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			   phys_addr(entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			   phys_addr(ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (ref->sg_call_ents && ref->type == dma_debug_sg &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	    ref->sg_call_ents != entry->sg_call_ents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		err_printk(ref->dev, entry, "device driver frees "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			   "DMA sg list with different entry count "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			   "[map count=%d] [unmap count=%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			   entry->sg_call_ents, ref->sg_call_ents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 * This may be no bug in reality - but most implementations of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	 * DMA API don't handle this properly, so check for it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (ref->direction != entry->direction) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		err_printk(ref->dev, entry, "device driver frees "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			   "DMA memory with different direction "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			   "[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			   "[mapped with %s] [unmapped with %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			   ref->dev_addr, ref->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			   dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			   dir2name[ref->direction]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	 * Drivers should use dma_mapping_error() to check the returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	 * addresses of dma_map_single() and dma_map_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * If not, print this warning message. See Documentation/core-api/dma-api.rst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		err_printk(ref->dev, entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			   "device driver failed to check map error"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			   "[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 			   "[mapped as %s]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			   ref->dev_addr, ref->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			   type2name[entry->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	hash_bucket_del(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	dma_entry_free(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) static void check_for_stack(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			    struct page *page, size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	struct vm_struct *stack_vm_area = task_stack_vm_area(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (!stack_vm_area) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		/* Stack is direct-mapped. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		if (PageHighMem(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		addr = page_address(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		if (object_is_on_stack(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			err_printk(dev, NULL, "device driver maps memory from stack [addr=%p]\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		/* Stack is vmalloced. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		for (i = 0; i < stack_vm_area->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			if (page != stack_vm_area->pages[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			err_printk(dev, NULL, "device driver maps memory from stack [probable addr=%p]\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	unsigned long a1 = (unsigned long)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	unsigned long b1 = a1 + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	unsigned long a2 = (unsigned long)start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	unsigned long b2 = (unsigned long)end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	return !(b1 <= a2 || a1 >= b2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (overlap(addr, len, _stext, _etext) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	    overlap(addr, len, __start_rodata, __end_rodata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		err_printk(dev, NULL, "device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) static void check_sync(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		       struct dma_debug_entry *ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		       bool to_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	struct hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	bucket = get_hash_bucket(ref, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	entry = bucket_find_contain(&bucket, ref, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		err_printk(dev, NULL, "device driver tries "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 				"to sync DMA memory it has not allocated "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 				"[device address=0x%016llx] [size=%llu bytes]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 				(unsigned long long)ref->dev_addr, ref->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	if (ref->size > entry->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		err_printk(dev, entry, "device driver syncs"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 				" DMA memory outside allocated range "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 				"[device address=0x%016llx] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 				"[allocation size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 				"[sync offset+size=%llu]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 				entry->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 				ref->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	if (entry->direction == DMA_BIDIRECTIONAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	if (ref->direction != entry->direction) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		err_printk(dev, entry, "device driver syncs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				"DMA memory with different direction "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				"[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 				"[mapped with %s] [synced with %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				(unsigned long long)ref->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 				dir2name[ref->direction]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		      !(ref->direction == DMA_TO_DEVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		err_printk(dev, entry, "device driver syncs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				"device read-only DMA memory for cpu "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 				"[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 				"[mapped with %s] [synced with %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 				(unsigned long long)ref->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 				dir2name[ref->direction]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		       !(ref->direction == DMA_FROM_DEVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		err_printk(dev, entry, "device driver syncs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 				"device write-only DMA memory to device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 				"[device address=0x%016llx] [size=%llu bytes] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				"[mapped with %s] [synced with %s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 				(unsigned long long)ref->dev_addr, entry->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 				dir2name[entry->direction],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 				dir2name[ref->direction]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	/* sg list count can be less than map count when partial cache sync */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (ref->sg_call_ents && ref->type == dma_debug_sg &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	    ref->sg_call_ents > entry->sg_call_ents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		err_printk(ref->dev, entry, "device driver syncs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			   "DMA sg list count larger than map count "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			   "[map count=%d] [sync count=%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			   entry->sg_call_ents, ref->sg_call_ents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static void check_sg_segment(struct device *dev, struct scatterlist *sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) #ifdef CONFIG_DMA_API_DEBUG_SG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	unsigned int max_seg = dma_get_max_seg_size(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	u64 start, end, boundary = dma_get_seg_boundary(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 * Either the driver forgot to set dma_parms appropriately, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 * whoever generated the list forgot to check them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if (sg->length > max_seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		err_printk(dev, NULL, "mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			   sg->length, max_seg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	 * In some cases this could potentially be the DMA API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	 * implementation's fault, but it would usually imply that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	 * the scatterlist was built inappropriately to begin with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	start = sg_dma_address(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	end = start + sg_dma_len(sg) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if ((start ^ end) & ~boundary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		err_printk(dev, NULL, "mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			   start, end, boundary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) void debug_dma_map_single(struct device *dev, const void *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			    unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	if (!virt_addr_valid(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		err_printk(dev, NULL, "device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			   addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	if (is_vmalloc_addr(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		err_printk(dev, NULL, "device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			   addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) EXPORT_SYMBOL(debug_dma_map_single);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			size_t size, int direction, dma_addr_t dma_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (dma_mapping_error(dev, dma_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	entry = dma_entry_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	entry->dev       = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	entry->type      = dma_debug_single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	entry->pfn	 = page_to_pfn(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	entry->offset	 = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	entry->dev_addr  = dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	entry->size      = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	entry->direction = direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	entry->map_err_type = MAP_ERR_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	check_for_stack(dev, page, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (!PageHighMem(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		void *addr = page_address(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		check_for_illegal_area(dev, addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	add_dma_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	struct dma_debug_entry ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	struct hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	ref.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	ref.dev_addr = dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	bucket = get_hash_bucket(&ref, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	list_for_each_entry(entry, &bucket->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		if (!exact_match(&ref, entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		 * The same physical address can be mapped multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		 * times. Without a hardware IOMMU this results in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		 * same device addresses being put into the dma-debug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		 * hash multiple times too. This can result in false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		 * positives being reported. Therefore we implement a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		 * best-fit algorithm here which updates the first entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		 * from the hash which fits the reference value and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		 * not currently listed as being checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			entry->map_err_type = MAP_ERR_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) EXPORT_SYMBOL(debug_dma_mapping_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			  size_t size, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		.type           = dma_debug_single,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		.dev_addr       = addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		.size           = size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		.direction      = direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	check_unmap(&ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		      int nents, int mapped_ents, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	struct scatterlist *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	for_each_sg(sg, s, nents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		check_for_stack(dev, sg_page(s), s->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		if (!PageHighMem(sg_page(s)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			check_for_illegal_area(dev, sg_virt(s), s->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	for_each_sg(sg, s, mapped_ents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		entry = dma_entry_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		entry->type           = dma_debug_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		entry->dev            = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		entry->pfn	      = page_to_pfn(sg_page(s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		entry->offset	      = s->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		entry->size           = sg_dma_len(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		entry->dev_addr       = sg_dma_address(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		entry->direction      = direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		entry->sg_call_ents   = nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		entry->sg_mapped_ents = mapped_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		check_sg_segment(dev, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		add_dma_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) static int get_nr_mapped_entries(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 				 struct dma_debug_entry *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	int mapped_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	bucket       = get_hash_bucket(ref, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	entry        = bucket_find_exact(bucket, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	mapped_ents  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		mapped_ents = entry->sg_mapped_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	put_hash_bucket(bucket, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	return mapped_ents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			int nelems, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	struct scatterlist *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	int mapped_ents = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	for_each_sg(sglist, s, nelems, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			.type           = dma_debug_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			.pfn		= page_to_pfn(sg_page(s)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			.offset		= s->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			.dev_addr       = sg_dma_address(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			.size           = sg_dma_len(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			.direction      = dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			.sg_call_ents   = nelems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		if (mapped_ents && i >= mapped_ents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		if (!i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			mapped_ents = get_nr_mapped_entries(dev, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		check_unmap(&ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) void debug_dma_alloc_coherent(struct device *dev, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			      dma_addr_t dma_addr, void *virt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (unlikely(virt == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	/* handle vmalloc and linear addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	entry = dma_entry_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	entry->type      = dma_debug_coherent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	entry->dev       = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	entry->offset	 = offset_in_page(virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	entry->size      = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	entry->dev_addr  = dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	entry->direction = DMA_BIDIRECTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	if (is_vmalloc_addr(virt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		entry->pfn = vmalloc_to_pfn(virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		entry->pfn = page_to_pfn(virt_to_page(virt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	add_dma_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) void debug_dma_free_coherent(struct device *dev, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 			 void *virt, dma_addr_t addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		.type           = dma_debug_coherent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		.offset		= offset_in_page(virt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		.dev_addr       = addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		.size           = size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		.direction      = DMA_BIDIRECTIONAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	/* handle vmalloc and linear addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	if (is_vmalloc_addr(virt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		ref.pfn = vmalloc_to_pfn(virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		ref.pfn = page_to_pfn(virt_to_page(virt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	check_unmap(&ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			    int direction, dma_addr_t dma_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	struct dma_debug_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	entry = dma_entry_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	entry->type		= dma_debug_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	entry->dev		= dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	entry->pfn		= PHYS_PFN(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	entry->offset		= offset_in_page(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	entry->size		= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	entry->dev_addr		= dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	entry->direction	= direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	entry->map_err_type	= MAP_ERR_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	add_dma_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			      size_t size, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		.type           = dma_debug_resource,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		.dev_addr       = dma_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		.size           = size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		.direction      = direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	check_unmap(&ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 				   size_t size, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	struct dma_debug_entry ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	ref.type         = dma_debug_single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	ref.dev          = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	ref.dev_addr     = dma_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	ref.size         = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	ref.direction    = direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	ref.sg_call_ents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	check_sync(dev, &ref, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) void debug_dma_sync_single_for_device(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 				      dma_addr_t dma_handle, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				      int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	struct dma_debug_entry ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	ref.type         = dma_debug_single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	ref.dev          = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	ref.dev_addr     = dma_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	ref.size         = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	ref.direction    = direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	ref.sg_call_ents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	check_sync(dev, &ref, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			       int nelems, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	struct scatterlist *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	int mapped_ents = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	for_each_sg(sg, s, nelems, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			.type           = dma_debug_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			.pfn		= page_to_pfn(sg_page(s)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			.offset		= s->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			.dev_addr       = sg_dma_address(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			.size           = sg_dma_len(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			.direction      = direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			.sg_call_ents   = nelems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		if (!i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			mapped_ents = get_nr_mapped_entries(dev, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		if (i >= mapped_ents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		check_sync(dev, &ref, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 				  int nelems, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	struct scatterlist *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	int mapped_ents = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (unlikely(dma_debug_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	for_each_sg(sg, s, nelems, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		struct dma_debug_entry ref = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			.type           = dma_debug_sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			.dev            = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			.pfn		= page_to_pfn(sg_page(s)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			.offset		= s->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			.dev_addr       = sg_dma_address(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 			.size           = sg_dma_len(s),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			.direction      = direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			.sg_call_ents   = nelems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		if (!i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			mapped_ents = get_nr_mapped_entries(dev, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		if (i >= mapped_ents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		check_sync(dev, &ref, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) static int __init dma_debug_driver_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		current_driver_name[i] = *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		if (*str == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	if (current_driver_name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		pr_info("enable driver filter for driver [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			current_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) __setup("dma_debug_driver=", dma_debug_driver_setup);