dma-debug.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. #include <linux/scatterlist.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/stacktrace.h>
  22. #include <linux/dma-debug.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/device.h>
  26. #include <linux/types.h>
  27. #include <linux/sched.h>
  28. #include <linux/list.h>
  29. #include <linux/slab.h>
  30. #include <asm/sections.h>
  31. #define HASH_SIZE 1024ULL
  32. #define HASH_FN_SHIFT 13
  33. #define HASH_FN_MASK (HASH_SIZE - 1)
  34. enum {
  35. dma_debug_single,
  36. dma_debug_page,
  37. dma_debug_sg,
  38. dma_debug_coherent,
  39. };
  40. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  41. struct dma_debug_entry {
  42. struct list_head list;
  43. struct device *dev;
  44. int type;
  45. phys_addr_t paddr;
  46. u64 dev_addr;
  47. u64 size;
  48. int direction;
  49. int sg_call_ents;
  50. int sg_mapped_ents;
  51. #ifdef CONFIG_STACKTRACE
  52. struct stack_trace stacktrace;
  53. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  54. #endif
  55. };
  56. struct hash_bucket {
  57. struct list_head list;
  58. spinlock_t lock;
  59. } ____cacheline_aligned_in_smp;
  60. /* Hash list to save the allocated dma addresses */
  61. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  62. /* List of pre-allocated dma_debug_entry's */
  63. static LIST_HEAD(free_entries);
  64. /* Lock for the list above */
  65. static DEFINE_SPINLOCK(free_entries_lock);
  66. /* Global disable flag - will be set in case of an error */
  67. static bool global_disable __read_mostly;
  68. /* Global error count */
  69. static u32 error_count;
  70. /* Global error show enable*/
  71. static u32 show_all_errors __read_mostly;
  72. /* Number of errors to show */
  73. static u32 show_num_errors = 1;
  74. static u32 num_free_entries;
  75. static u32 min_free_entries;
  76. /* number of preallocated entries requested by kernel cmdline */
  77. static u32 req_entries;
  78. /* debugfs dentry's for the stuff above */
  79. static struct dentry *dma_debug_dent __read_mostly;
  80. static struct dentry *global_disable_dent __read_mostly;
  81. static struct dentry *error_count_dent __read_mostly;
  82. static struct dentry *show_all_errors_dent __read_mostly;
  83. static struct dentry *show_num_errors_dent __read_mostly;
  84. static struct dentry *num_free_entries_dent __read_mostly;
  85. static struct dentry *min_free_entries_dent __read_mostly;
  86. static const char *type2name[4] = { "single", "page",
  87. "scather-gather", "coherent" };
  88. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  89. "DMA_FROM_DEVICE", "DMA_NONE" };
  90. /*
  91. * The access to some variables in this macro is racy. We can't use atomic_t
  92. * here because all these variables are exported to debugfs. Some of them even
  93. * writeable. This is also the reason why a lock won't help much. But anyway,
  94. * the races are no big deal. Here is why:
  95. *
  96. * error_count: the addition is racy, but the worst thing that can happen is
  97. * that we don't count some errors
  98. * show_num_errors: the subtraction is racy. Also no big deal because in
  99. * worst case this will result in one warning more in the
  100. * system log than the user configured. This variable is
  101. * writeable via debugfs.
  102. */
  103. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  104. {
  105. #ifdef CONFIG_STACKTRACE
  106. if (entry) {
  107. printk(KERN_WARNING "Mapped at:\n");
  108. print_stack_trace(&entry->stacktrace, 0);
  109. }
  110. #endif
  111. }
  112. #define err_printk(dev, entry, format, arg...) do { \
  113. error_count += 1; \
  114. if (show_all_errors || show_num_errors > 0) { \
  115. WARN(1, "%s %s: " format, \
  116. dev_driver_string(dev), \
  117. dev_name(dev) , ## arg); \
  118. dump_entry_trace(entry); \
  119. } \
  120. if (!show_all_errors && show_num_errors > 0) \
  121. show_num_errors -= 1; \
  122. } while (0);
  123. /*
  124. * Hash related functions
  125. *
  126. * Every DMA-API request is saved into a struct dma_debug_entry. To
  127. * have quick access to these structs they are stored into a hash.
  128. */
  129. static int hash_fn(struct dma_debug_entry *entry)
  130. {
  131. /*
  132. * Hash function is based on the dma address.
  133. * We use bits 20-27 here as the index into the hash
  134. */
  135. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  136. }
  137. /*
  138. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  139. */
  140. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  141. unsigned long *flags)
  142. {
  143. int idx = hash_fn(entry);
  144. unsigned long __flags;
  145. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  146. *flags = __flags;
  147. return &dma_entry_hash[idx];
  148. }
  149. /*
  150. * Give up exclusive access to the hash bucket
  151. */
  152. static void put_hash_bucket(struct hash_bucket *bucket,
  153. unsigned long *flags)
  154. {
  155. unsigned long __flags = *flags;
  156. spin_unlock_irqrestore(&bucket->lock, __flags);
  157. }
  158. /*
  159. * Search a given entry in the hash bucket list
  160. */
  161. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  162. struct dma_debug_entry *ref)
  163. {
  164. struct dma_debug_entry *entry;
  165. list_for_each_entry(entry, &bucket->list, list) {
  166. if ((entry->dev_addr == ref->dev_addr) &&
  167. (entry->dev == ref->dev))
  168. return entry;
  169. }
  170. return NULL;
  171. }
  172. /*
  173. * Add an entry to a hash bucket
  174. */
  175. static void hash_bucket_add(struct hash_bucket *bucket,
  176. struct dma_debug_entry *entry)
  177. {
  178. list_add_tail(&entry->list, &bucket->list);
  179. }
  180. /*
  181. * Remove entry from a hash bucket list
  182. */
  183. static void hash_bucket_del(struct dma_debug_entry *entry)
  184. {
  185. list_del(&entry->list);
  186. }
  187. /*
  188. * Dump mapping entries for debugging purposes
  189. */
  190. void debug_dma_dump_mappings(struct device *dev)
  191. {
  192. int idx;
  193. for (idx = 0; idx < HASH_SIZE; idx++) {
  194. struct hash_bucket *bucket = &dma_entry_hash[idx];
  195. struct dma_debug_entry *entry;
  196. unsigned long flags;
  197. spin_lock_irqsave(&bucket->lock, flags);
  198. list_for_each_entry(entry, &bucket->list, list) {
  199. if (!dev || dev == entry->dev) {
  200. dev_info(entry->dev,
  201. "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
  202. type2name[entry->type], idx,
  203. (unsigned long long)entry->paddr,
  204. entry->dev_addr, entry->size,
  205. dir2name[entry->direction]);
  206. }
  207. }
  208. spin_unlock_irqrestore(&bucket->lock, flags);
  209. }
  210. }
  211. EXPORT_SYMBOL(debug_dma_dump_mappings);
  212. /*
  213. * Wrapper function for adding an entry to the hash.
  214. * This function takes care of locking itself.
  215. */
  216. static void add_dma_entry(struct dma_debug_entry *entry)
  217. {
  218. struct hash_bucket *bucket;
  219. unsigned long flags;
  220. bucket = get_hash_bucket(entry, &flags);
  221. hash_bucket_add(bucket, entry);
  222. put_hash_bucket(bucket, &flags);
  223. }
  224. /* struct dma_entry allocator
  225. *
  226. * The next two functions implement the allocator for
  227. * struct dma_debug_entries.
  228. */
  229. static struct dma_debug_entry *dma_entry_alloc(void)
  230. {
  231. struct dma_debug_entry *entry = NULL;
  232. unsigned long flags;
  233. spin_lock_irqsave(&free_entries_lock, flags);
  234. if (list_empty(&free_entries)) {
  235. printk(KERN_ERR "DMA-API: debugging out of memory "
  236. "- disabling\n");
  237. global_disable = true;
  238. goto out;
  239. }
  240. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  241. list_del(&entry->list);
  242. memset(entry, 0, sizeof(*entry));
  243. #ifdef CONFIG_STACKTRACE
  244. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  245. entry->stacktrace.entries = entry->st_entries;
  246. entry->stacktrace.skip = 2;
  247. save_stack_trace(&entry->stacktrace);
  248. #endif
  249. num_free_entries -= 1;
  250. if (num_free_entries < min_free_entries)
  251. min_free_entries = num_free_entries;
  252. out:
  253. spin_unlock_irqrestore(&free_entries_lock, flags);
  254. return entry;
  255. }
  256. static void dma_entry_free(struct dma_debug_entry *entry)
  257. {
  258. unsigned long flags;
  259. /*
  260. * add to beginning of the list - this way the entries are
  261. * more likely cache hot when they are reallocated.
  262. */
  263. spin_lock_irqsave(&free_entries_lock, flags);
  264. list_add(&entry->list, &free_entries);
  265. num_free_entries += 1;
  266. spin_unlock_irqrestore(&free_entries_lock, flags);
  267. }
  268. /*
  269. * DMA-API debugging init code
  270. *
  271. * The init code does two things:
  272. * 1. Initialize core data structures
  273. * 2. Preallocate a given number of dma_debug_entry structs
  274. */
  275. static int prealloc_memory(u32 num_entries)
  276. {
  277. struct dma_debug_entry *entry, *next_entry;
  278. int i;
  279. for (i = 0; i < num_entries; ++i) {
  280. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  281. if (!entry)
  282. goto out_err;
  283. list_add_tail(&entry->list, &free_entries);
  284. }
  285. num_free_entries = num_entries;
  286. min_free_entries = num_entries;
  287. printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
  288. num_entries);
  289. return 0;
  290. out_err:
  291. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  292. list_del(&entry->list);
  293. kfree(entry);
  294. }
  295. return -ENOMEM;
  296. }
  297. static int dma_debug_fs_init(void)
  298. {
  299. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  300. if (!dma_debug_dent) {
  301. printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
  302. return -ENOMEM;
  303. }
  304. global_disable_dent = debugfs_create_bool("disabled", 0444,
  305. dma_debug_dent,
  306. (u32 *)&global_disable);
  307. if (!global_disable_dent)
  308. goto out_err;
  309. error_count_dent = debugfs_create_u32("error_count", 0444,
  310. dma_debug_dent, &error_count);
  311. if (!error_count_dent)
  312. goto out_err;
  313. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  314. dma_debug_dent,
  315. &show_all_errors);
  316. if (!show_all_errors_dent)
  317. goto out_err;
  318. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  319. dma_debug_dent,
  320. &show_num_errors);
  321. if (!show_num_errors_dent)
  322. goto out_err;
  323. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  324. dma_debug_dent,
  325. &num_free_entries);
  326. if (!num_free_entries_dent)
  327. goto out_err;
  328. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  329. dma_debug_dent,
  330. &min_free_entries);
  331. if (!min_free_entries_dent)
  332. goto out_err;
  333. return 0;
  334. out_err:
  335. debugfs_remove_recursive(dma_debug_dent);
  336. return -ENOMEM;
  337. }
  338. /*
  339. * Let the architectures decide how many entries should be preallocated.
  340. */
  341. void dma_debug_init(u32 num_entries)
  342. {
  343. int i;
  344. if (global_disable)
  345. return;
  346. for (i = 0; i < HASH_SIZE; ++i) {
  347. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  348. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  349. }
  350. if (dma_debug_fs_init() != 0) {
  351. printk(KERN_ERR "DMA-API: error creating debugfs entries "
  352. "- disabling\n");
  353. global_disable = true;
  354. return;
  355. }
  356. if (req_entries)
  357. num_entries = req_entries;
  358. if (prealloc_memory(num_entries) != 0) {
  359. printk(KERN_ERR "DMA-API: debugging out of memory error "
  360. "- disabled\n");
  361. global_disable = true;
  362. return;
  363. }
  364. printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
  365. }
  366. static __init int dma_debug_cmdline(char *str)
  367. {
  368. if (!str)
  369. return -EINVAL;
  370. if (strncmp(str, "off", 3) == 0) {
  371. printk(KERN_INFO "DMA-API: debugging disabled on kernel "
  372. "command line\n");
  373. global_disable = true;
  374. }
  375. return 0;
  376. }
  377. static __init int dma_debug_entries_cmdline(char *str)
  378. {
  379. int res;
  380. if (!str)
  381. return -EINVAL;
  382. res = get_option(&str, &req_entries);
  383. if (!res)
  384. req_entries = 0;
  385. return 0;
  386. }
  387. __setup("dma_debug=", dma_debug_cmdline);
  388. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  389. static void check_unmap(struct dma_debug_entry *ref)
  390. {
  391. struct dma_debug_entry *entry;
  392. struct hash_bucket *bucket;
  393. unsigned long flags;
  394. if (dma_mapping_error(ref->dev, ref->dev_addr))
  395. return;
  396. bucket = get_hash_bucket(ref, &flags);
  397. entry = hash_bucket_find(bucket, ref);
  398. if (!entry) {
  399. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  400. "to free DMA memory it has not allocated "
  401. "[device address=0x%016llx] [size=%llu bytes]\n",
  402. ref->dev_addr, ref->size);
  403. goto out;
  404. }
  405. if (ref->size != entry->size) {
  406. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  407. "DMA memory with different size "
  408. "[device address=0x%016llx] [map size=%llu bytes] "
  409. "[unmap size=%llu bytes]\n",
  410. ref->dev_addr, entry->size, ref->size);
  411. }
  412. if (ref->type != entry->type) {
  413. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  414. "DMA memory with wrong function "
  415. "[device address=0x%016llx] [size=%llu bytes] "
  416. "[mapped as %s] [unmapped as %s]\n",
  417. ref->dev_addr, ref->size,
  418. type2name[entry->type], type2name[ref->type]);
  419. } else if ((entry->type == dma_debug_coherent) &&
  420. (ref->paddr != entry->paddr)) {
  421. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  422. "DMA memory with different CPU address "
  423. "[device address=0x%016llx] [size=%llu bytes] "
  424. "[cpu alloc address=%p] [cpu free address=%p]",
  425. ref->dev_addr, ref->size,
  426. (void *)entry->paddr, (void *)ref->paddr);
  427. }
  428. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  429. ref->sg_call_ents != entry->sg_call_ents) {
  430. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  431. "DMA sg list with different entry count "
  432. "[map count=%d] [unmap count=%d]\n",
  433. entry->sg_call_ents, ref->sg_call_ents);
  434. }
  435. /*
  436. * This may be no bug in reality - but most implementations of the
  437. * DMA API don't handle this properly, so check for it here
  438. */
  439. if (ref->direction != entry->direction) {
  440. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  441. "DMA memory with different direction "
  442. "[device address=0x%016llx] [size=%llu bytes] "
  443. "[mapped with %s] [unmapped with %s]\n",
  444. ref->dev_addr, ref->size,
  445. dir2name[entry->direction],
  446. dir2name[ref->direction]);
  447. }
  448. hash_bucket_del(entry);
  449. dma_entry_free(entry);
  450. out:
  451. put_hash_bucket(bucket, &flags);
  452. }
  453. static void check_for_stack(struct device *dev, void *addr)
  454. {
  455. if (object_is_on_stack(addr))
  456. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  457. "stack [addr=%p]\n", addr);
  458. }
  459. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  460. {
  461. void *addr2 = (char *)addr + size;
  462. return ((addr >= start && addr < end) ||
  463. (addr2 >= start && addr2 < end) ||
  464. ((addr < start) && (addr2 >= end)));
  465. }
  466. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  467. {
  468. if (overlap(addr, size, _text, _etext) ||
  469. overlap(addr, size, __start_rodata, __end_rodata))
  470. err_printk(dev, NULL, "DMA-API: device driver maps "
  471. "memory from kernel text or rodata "
  472. "[addr=%p] [size=%llu]\n", addr, size);
  473. }
  474. static void check_sync(struct device *dev, dma_addr_t addr,
  475. u64 size, u64 offset, int direction, bool to_cpu)
  476. {
  477. struct dma_debug_entry ref = {
  478. .dev = dev,
  479. .dev_addr = addr,
  480. .size = size,
  481. .direction = direction,
  482. };
  483. struct dma_debug_entry *entry;
  484. struct hash_bucket *bucket;
  485. unsigned long flags;
  486. bucket = get_hash_bucket(&ref, &flags);
  487. entry = hash_bucket_find(bucket, &ref);
  488. if (!entry) {
  489. err_printk(dev, NULL, "DMA-API: device driver tries "
  490. "to sync DMA memory it has not allocated "
  491. "[device address=0x%016llx] [size=%llu bytes]\n",
  492. addr, size);
  493. goto out;
  494. }
  495. if ((offset + size) > entry->size) {
  496. err_printk(dev, entry, "DMA-API: device driver syncs"
  497. " DMA memory outside allocated range "
  498. "[device address=0x%016llx] "
  499. "[allocation size=%llu bytes] [sync offset=%llu] "
  500. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  501. offset, size);
  502. }
  503. if (direction != entry->direction) {
  504. err_printk(dev, entry, "DMA-API: device driver syncs "
  505. "DMA memory with different direction "
  506. "[device address=0x%016llx] [size=%llu bytes] "
  507. "[mapped with %s] [synced with %s]\n",
  508. addr, entry->size,
  509. dir2name[entry->direction],
  510. dir2name[direction]);
  511. }
  512. if (entry->direction == DMA_BIDIRECTIONAL)
  513. goto out;
  514. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  515. !(direction == DMA_TO_DEVICE))
  516. err_printk(dev, entry, "DMA-API: device driver syncs "
  517. "device read-only DMA memory for cpu "
  518. "[device address=0x%016llx] [size=%llu bytes] "
  519. "[mapped with %s] [synced with %s]\n",
  520. addr, entry->size,
  521. dir2name[entry->direction],
  522. dir2name[direction]);
  523. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  524. !(direction == DMA_FROM_DEVICE))
  525. err_printk(dev, entry, "DMA-API: device driver syncs "
  526. "device write-only DMA memory to device "
  527. "[device address=0x%016llx] [size=%llu bytes] "
  528. "[mapped with %s] [synced with %s]\n",
  529. addr, entry->size,
  530. dir2name[entry->direction],
  531. dir2name[direction]);
  532. out:
  533. put_hash_bucket(bucket, &flags);
  534. }
  535. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  536. size_t size, int direction, dma_addr_t dma_addr,
  537. bool map_single)
  538. {
  539. struct dma_debug_entry *entry;
  540. if (unlikely(global_disable))
  541. return;
  542. if (unlikely(dma_mapping_error(dev, dma_addr)))
  543. return;
  544. entry = dma_entry_alloc();
  545. if (!entry)
  546. return;
  547. entry->dev = dev;
  548. entry->type = dma_debug_page;
  549. entry->paddr = page_to_phys(page) + offset;
  550. entry->dev_addr = dma_addr;
  551. entry->size = size;
  552. entry->direction = direction;
  553. if (map_single) {
  554. void *addr = ((char *)page_address(page)) + offset;
  555. entry->type = dma_debug_single;
  556. check_for_stack(dev, addr);
  557. check_for_illegal_area(dev, addr, size);
  558. }
  559. add_dma_entry(entry);
  560. }
  561. EXPORT_SYMBOL(debug_dma_map_page);
  562. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  563. size_t size, int direction, bool map_single)
  564. {
  565. struct dma_debug_entry ref = {
  566. .type = dma_debug_page,
  567. .dev = dev,
  568. .dev_addr = addr,
  569. .size = size,
  570. .direction = direction,
  571. };
  572. if (unlikely(global_disable))
  573. return;
  574. if (map_single)
  575. ref.type = dma_debug_single;
  576. check_unmap(&ref);
  577. }
  578. EXPORT_SYMBOL(debug_dma_unmap_page);
  579. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  580. int nents, int mapped_ents, int direction)
  581. {
  582. struct dma_debug_entry *entry;
  583. struct scatterlist *s;
  584. int i;
  585. if (unlikely(global_disable))
  586. return;
  587. for_each_sg(sg, s, mapped_ents, i) {
  588. entry = dma_entry_alloc();
  589. if (!entry)
  590. return;
  591. entry->type = dma_debug_sg;
  592. entry->dev = dev;
  593. entry->paddr = sg_phys(s);
  594. entry->size = s->length;
  595. entry->dev_addr = s->dma_address;
  596. entry->direction = direction;
  597. entry->sg_call_ents = nents;
  598. entry->sg_mapped_ents = mapped_ents;
  599. check_for_stack(dev, sg_virt(s));
  600. check_for_illegal_area(dev, sg_virt(s), s->length);
  601. add_dma_entry(entry);
  602. }
  603. }
  604. EXPORT_SYMBOL(debug_dma_map_sg);
  605. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  606. int nelems, int dir)
  607. {
  608. struct dma_debug_entry *entry;
  609. struct scatterlist *s;
  610. int mapped_ents = 0, i;
  611. unsigned long flags;
  612. if (unlikely(global_disable))
  613. return;
  614. for_each_sg(sglist, s, nelems, i) {
  615. struct dma_debug_entry ref = {
  616. .type = dma_debug_sg,
  617. .dev = dev,
  618. .paddr = sg_phys(s),
  619. .dev_addr = s->dma_address,
  620. .size = s->length,
  621. .direction = dir,
  622. .sg_call_ents = 0,
  623. };
  624. if (mapped_ents && i >= mapped_ents)
  625. break;
  626. if (mapped_ents == 0) {
  627. struct hash_bucket *bucket;
  628. ref.sg_call_ents = nelems;
  629. bucket = get_hash_bucket(&ref, &flags);
  630. entry = hash_bucket_find(bucket, &ref);
  631. if (entry)
  632. mapped_ents = entry->sg_mapped_ents;
  633. put_hash_bucket(bucket, &flags);
  634. }
  635. check_unmap(&ref);
  636. }
  637. }
  638. EXPORT_SYMBOL(debug_dma_unmap_sg);
  639. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  640. dma_addr_t dma_addr, void *virt)
  641. {
  642. struct dma_debug_entry *entry;
  643. if (unlikely(global_disable))
  644. return;
  645. if (unlikely(virt == NULL))
  646. return;
  647. entry = dma_entry_alloc();
  648. if (!entry)
  649. return;
  650. entry->type = dma_debug_coherent;
  651. entry->dev = dev;
  652. entry->paddr = virt_to_phys(virt);
  653. entry->size = size;
  654. entry->dev_addr = dma_addr;
  655. entry->direction = DMA_BIDIRECTIONAL;
  656. add_dma_entry(entry);
  657. }
  658. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  659. void debug_dma_free_coherent(struct device *dev, size_t size,
  660. void *virt, dma_addr_t addr)
  661. {
  662. struct dma_debug_entry ref = {
  663. .type = dma_debug_coherent,
  664. .dev = dev,
  665. .paddr = virt_to_phys(virt),
  666. .dev_addr = addr,
  667. .size = size,
  668. .direction = DMA_BIDIRECTIONAL,
  669. };
  670. if (unlikely(global_disable))
  671. return;
  672. check_unmap(&ref);
  673. }
  674. EXPORT_SYMBOL(debug_dma_free_coherent);
  675. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  676. size_t size, int direction)
  677. {
  678. if (unlikely(global_disable))
  679. return;
  680. check_sync(dev, dma_handle, size, 0, direction, true);
  681. }
  682. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  683. void debug_dma_sync_single_for_device(struct device *dev,
  684. dma_addr_t dma_handle, size_t size,
  685. int direction)
  686. {
  687. if (unlikely(global_disable))
  688. return;
  689. check_sync(dev, dma_handle, size, 0, direction, false);
  690. }
  691. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  692. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  693. dma_addr_t dma_handle,
  694. unsigned long offset, size_t size,
  695. int direction)
  696. {
  697. if (unlikely(global_disable))
  698. return;
  699. check_sync(dev, dma_handle, size, offset, direction, true);
  700. }
  701. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  702. void debug_dma_sync_single_range_for_device(struct device *dev,
  703. dma_addr_t dma_handle,
  704. unsigned long offset,
  705. size_t size, int direction)
  706. {
  707. if (unlikely(global_disable))
  708. return;
  709. check_sync(dev, dma_handle, size, offset, direction, false);
  710. }
  711. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  712. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  713. int nelems, int direction)
  714. {
  715. struct scatterlist *s;
  716. int i;
  717. if (unlikely(global_disable))
  718. return;
  719. for_each_sg(sg, s, nelems, i) {
  720. check_sync(dev, s->dma_address, s->dma_length, 0,
  721. direction, true);
  722. }
  723. }
  724. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  725. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  726. int nelems, int direction)
  727. {
  728. struct scatterlist *s;
  729. int i;
  730. if (unlikely(global_disable))
  731. return;
  732. for_each_sg(sg, s, nelems, i) {
  733. check_sync(dev, s->dma_address, s->dma_length, 0,
  734. direction, false);
  735. }
  736. }
  737. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);