dma-debug.c 21 KB

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