dma-debug.c 21 KB

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