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