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