dma-debug.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. void dma_debug_add_bus(struct bus_type *bus)
  339. {
  340. /* FIXME: register notifier */
  341. }
  342. /*
  343. * Let the architectures decide how many entries should be preallocated.
  344. */
  345. void dma_debug_init(u32 num_entries)
  346. {
  347. int i;
  348. if (global_disable)
  349. return;
  350. for (i = 0; i < HASH_SIZE; ++i) {
  351. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  352. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  353. }
  354. if (dma_debug_fs_init() != 0) {
  355. printk(KERN_ERR "DMA-API: error creating debugfs entries "
  356. "- disabling\n");
  357. global_disable = true;
  358. return;
  359. }
  360. if (req_entries)
  361. num_entries = req_entries;
  362. if (prealloc_memory(num_entries) != 0) {
  363. printk(KERN_ERR "DMA-API: debugging out of memory error "
  364. "- disabled\n");
  365. global_disable = true;
  366. return;
  367. }
  368. printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
  369. }
  370. static __init int dma_debug_cmdline(char *str)
  371. {
  372. if (!str)
  373. return -EINVAL;
  374. if (strncmp(str, "off", 3) == 0) {
  375. printk(KERN_INFO "DMA-API: debugging disabled on kernel "
  376. "command line\n");
  377. global_disable = true;
  378. }
  379. return 0;
  380. }
  381. static __init int dma_debug_entries_cmdline(char *str)
  382. {
  383. int res;
  384. if (!str)
  385. return -EINVAL;
  386. res = get_option(&str, &req_entries);
  387. if (!res)
  388. req_entries = 0;
  389. return 0;
  390. }
  391. __setup("dma_debug=", dma_debug_cmdline);
  392. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  393. static void check_unmap(struct dma_debug_entry *ref)
  394. {
  395. struct dma_debug_entry *entry;
  396. struct hash_bucket *bucket;
  397. unsigned long flags;
  398. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  399. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  400. "to free an invalid DMA memory address\n");
  401. return;
  402. }
  403. bucket = get_hash_bucket(ref, &flags);
  404. entry = hash_bucket_find(bucket, ref);
  405. if (!entry) {
  406. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  407. "to free DMA memory it has not allocated "
  408. "[device address=0x%016llx] [size=%llu bytes]\n",
  409. ref->dev_addr, ref->size);
  410. goto out;
  411. }
  412. if (ref->size != entry->size) {
  413. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  414. "DMA memory with different size "
  415. "[device address=0x%016llx] [map size=%llu bytes] "
  416. "[unmap size=%llu bytes]\n",
  417. ref->dev_addr, entry->size, ref->size);
  418. }
  419. if (ref->type != entry->type) {
  420. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  421. "DMA memory with wrong function "
  422. "[device address=0x%016llx] [size=%llu bytes] "
  423. "[mapped as %s] [unmapped as %s]\n",
  424. ref->dev_addr, ref->size,
  425. type2name[entry->type], type2name[ref->type]);
  426. } else if ((entry->type == dma_debug_coherent) &&
  427. (ref->paddr != entry->paddr)) {
  428. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  429. "DMA memory with different CPU address "
  430. "[device address=0x%016llx] [size=%llu bytes] "
  431. "[cpu alloc address=%p] [cpu free address=%p]",
  432. ref->dev_addr, ref->size,
  433. (void *)entry->paddr, (void *)ref->paddr);
  434. }
  435. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  436. ref->sg_call_ents != entry->sg_call_ents) {
  437. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  438. "DMA sg list with different entry count "
  439. "[map count=%d] [unmap count=%d]\n",
  440. entry->sg_call_ents, ref->sg_call_ents);
  441. }
  442. /*
  443. * This may be no bug in reality - but most implementations of the
  444. * DMA API don't handle this properly, so check for it here
  445. */
  446. if (ref->direction != entry->direction) {
  447. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  448. "DMA memory with different direction "
  449. "[device address=0x%016llx] [size=%llu bytes] "
  450. "[mapped with %s] [unmapped with %s]\n",
  451. ref->dev_addr, ref->size,
  452. dir2name[entry->direction],
  453. dir2name[ref->direction]);
  454. }
  455. hash_bucket_del(entry);
  456. dma_entry_free(entry);
  457. out:
  458. put_hash_bucket(bucket, &flags);
  459. }
  460. static void check_for_stack(struct device *dev, void *addr)
  461. {
  462. if (object_is_on_stack(addr))
  463. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  464. "stack [addr=%p]\n", addr);
  465. }
  466. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  467. {
  468. void *addr2 = (char *)addr + size;
  469. return ((addr >= start && addr < end) ||
  470. (addr2 >= start && addr2 < end) ||
  471. ((addr < start) && (addr2 >= end)));
  472. }
  473. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  474. {
  475. if (overlap(addr, size, _text, _etext) ||
  476. overlap(addr, size, __start_rodata, __end_rodata))
  477. err_printk(dev, NULL, "DMA-API: device driver maps "
  478. "memory from kernel text or rodata "
  479. "[addr=%p] [size=%llu]\n", addr, size);
  480. }
  481. static void check_sync(struct device *dev, dma_addr_t addr,
  482. u64 size, u64 offset, int direction, bool to_cpu)
  483. {
  484. struct dma_debug_entry ref = {
  485. .dev = dev,
  486. .dev_addr = addr,
  487. .size = size,
  488. .direction = direction,
  489. };
  490. struct dma_debug_entry *entry;
  491. struct hash_bucket *bucket;
  492. unsigned long flags;
  493. bucket = get_hash_bucket(&ref, &flags);
  494. entry = hash_bucket_find(bucket, &ref);
  495. if (!entry) {
  496. err_printk(dev, NULL, "DMA-API: device driver tries "
  497. "to sync DMA memory it has not allocated "
  498. "[device address=0x%016llx] [size=%llu bytes]\n",
  499. (unsigned long long)addr, size);
  500. goto out;
  501. }
  502. if ((offset + size) > entry->size) {
  503. err_printk(dev, entry, "DMA-API: device driver syncs"
  504. " DMA memory outside allocated range "
  505. "[device address=0x%016llx] "
  506. "[allocation size=%llu bytes] [sync offset=%llu] "
  507. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  508. offset, size);
  509. }
  510. if (direction != entry->direction) {
  511. err_printk(dev, entry, "DMA-API: device driver syncs "
  512. "DMA memory with different direction "
  513. "[device address=0x%016llx] [size=%llu bytes] "
  514. "[mapped with %s] [synced with %s]\n",
  515. (unsigned long long)addr, entry->size,
  516. dir2name[entry->direction],
  517. dir2name[direction]);
  518. }
  519. if (entry->direction == DMA_BIDIRECTIONAL)
  520. goto out;
  521. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  522. !(direction == DMA_TO_DEVICE))
  523. err_printk(dev, entry, "DMA-API: device driver syncs "
  524. "device read-only DMA memory for cpu "
  525. "[device address=0x%016llx] [size=%llu bytes] "
  526. "[mapped with %s] [synced with %s]\n",
  527. (unsigned long long)addr, entry->size,
  528. dir2name[entry->direction],
  529. dir2name[direction]);
  530. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  531. !(direction == DMA_FROM_DEVICE))
  532. err_printk(dev, entry, "DMA-API: device driver syncs "
  533. "device write-only DMA memory to device "
  534. "[device address=0x%016llx] [size=%llu bytes] "
  535. "[mapped with %s] [synced with %s]\n",
  536. (unsigned long long)addr, entry->size,
  537. dir2name[entry->direction],
  538. dir2name[direction]);
  539. out:
  540. put_hash_bucket(bucket, &flags);
  541. }
  542. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  543. size_t size, int direction, dma_addr_t dma_addr,
  544. bool map_single)
  545. {
  546. struct dma_debug_entry *entry;
  547. if (unlikely(global_disable))
  548. return;
  549. if (unlikely(dma_mapping_error(dev, dma_addr)))
  550. return;
  551. entry = dma_entry_alloc();
  552. if (!entry)
  553. return;
  554. entry->dev = dev;
  555. entry->type = dma_debug_page;
  556. entry->paddr = page_to_phys(page) + offset;
  557. entry->dev_addr = dma_addr;
  558. entry->size = size;
  559. entry->direction = direction;
  560. if (map_single)
  561. entry->type = dma_debug_single;
  562. if (!PageHighMem(page)) {
  563. void *addr = ((char *)page_address(page)) + offset;
  564. check_for_stack(dev, addr);
  565. check_for_illegal_area(dev, addr, size);
  566. }
  567. add_dma_entry(entry);
  568. }
  569. EXPORT_SYMBOL(debug_dma_map_page);
  570. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  571. size_t size, int direction, bool map_single)
  572. {
  573. struct dma_debug_entry ref = {
  574. .type = dma_debug_page,
  575. .dev = dev,
  576. .dev_addr = addr,
  577. .size = size,
  578. .direction = direction,
  579. };
  580. if (unlikely(global_disable))
  581. return;
  582. if (map_single)
  583. ref.type = dma_debug_single;
  584. check_unmap(&ref);
  585. }
  586. EXPORT_SYMBOL(debug_dma_unmap_page);
  587. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  588. int nents, int mapped_ents, int direction)
  589. {
  590. struct dma_debug_entry *entry;
  591. struct scatterlist *s;
  592. int i;
  593. if (unlikely(global_disable))
  594. return;
  595. for_each_sg(sg, s, mapped_ents, i) {
  596. entry = dma_entry_alloc();
  597. if (!entry)
  598. return;
  599. entry->type = dma_debug_sg;
  600. entry->dev = dev;
  601. entry->paddr = sg_phys(s);
  602. entry->size = s->length;
  603. entry->dev_addr = s->dma_address;
  604. entry->direction = direction;
  605. entry->sg_call_ents = nents;
  606. entry->sg_mapped_ents = mapped_ents;
  607. if (!PageHighMem(sg_page(s))) {
  608. check_for_stack(dev, sg_virt(s));
  609. check_for_illegal_area(dev, sg_virt(s), s->length);
  610. }
  611. add_dma_entry(entry);
  612. }
  613. }
  614. EXPORT_SYMBOL(debug_dma_map_sg);
  615. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  616. int nelems, int dir)
  617. {
  618. struct dma_debug_entry *entry;
  619. struct scatterlist *s;
  620. int mapped_ents = 0, i;
  621. unsigned long flags;
  622. if (unlikely(global_disable))
  623. return;
  624. for_each_sg(sglist, s, nelems, i) {
  625. struct dma_debug_entry ref = {
  626. .type = dma_debug_sg,
  627. .dev = dev,
  628. .paddr = sg_phys(s),
  629. .dev_addr = s->dma_address,
  630. .size = s->length,
  631. .direction = dir,
  632. .sg_call_ents = 0,
  633. };
  634. if (mapped_ents && i >= mapped_ents)
  635. break;
  636. if (mapped_ents == 0) {
  637. struct hash_bucket *bucket;
  638. ref.sg_call_ents = nelems;
  639. bucket = get_hash_bucket(&ref, &flags);
  640. entry = hash_bucket_find(bucket, &ref);
  641. if (entry)
  642. mapped_ents = entry->sg_mapped_ents;
  643. put_hash_bucket(bucket, &flags);
  644. }
  645. check_unmap(&ref);
  646. }
  647. }
  648. EXPORT_SYMBOL(debug_dma_unmap_sg);
  649. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  650. dma_addr_t dma_addr, void *virt)
  651. {
  652. struct dma_debug_entry *entry;
  653. if (unlikely(global_disable))
  654. return;
  655. if (unlikely(virt == NULL))
  656. return;
  657. entry = dma_entry_alloc();
  658. if (!entry)
  659. return;
  660. entry->type = dma_debug_coherent;
  661. entry->dev = dev;
  662. entry->paddr = virt_to_phys(virt);
  663. entry->size = size;
  664. entry->dev_addr = dma_addr;
  665. entry->direction = DMA_BIDIRECTIONAL;
  666. add_dma_entry(entry);
  667. }
  668. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  669. void debug_dma_free_coherent(struct device *dev, size_t size,
  670. void *virt, dma_addr_t addr)
  671. {
  672. struct dma_debug_entry ref = {
  673. .type = dma_debug_coherent,
  674. .dev = dev,
  675. .paddr = virt_to_phys(virt),
  676. .dev_addr = addr,
  677. .size = size,
  678. .direction = DMA_BIDIRECTIONAL,
  679. };
  680. if (unlikely(global_disable))
  681. return;
  682. check_unmap(&ref);
  683. }
  684. EXPORT_SYMBOL(debug_dma_free_coherent);
  685. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  686. size_t size, int direction)
  687. {
  688. if (unlikely(global_disable))
  689. return;
  690. check_sync(dev, dma_handle, size, 0, direction, true);
  691. }
  692. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  693. void debug_dma_sync_single_for_device(struct device *dev,
  694. dma_addr_t dma_handle, size_t size,
  695. int direction)
  696. {
  697. if (unlikely(global_disable))
  698. return;
  699. check_sync(dev, dma_handle, size, 0, direction, false);
  700. }
  701. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  702. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  703. dma_addr_t dma_handle,
  704. unsigned long offset, size_t size,
  705. int direction)
  706. {
  707. if (unlikely(global_disable))
  708. return;
  709. check_sync(dev, dma_handle, size, offset, direction, true);
  710. }
  711. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  712. void debug_dma_sync_single_range_for_device(struct device *dev,
  713. dma_addr_t dma_handle,
  714. unsigned long offset,
  715. size_t size, int direction)
  716. {
  717. if (unlikely(global_disable))
  718. return;
  719. check_sync(dev, dma_handle, size, offset, direction, false);
  720. }
  721. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  722. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  723. int nelems, int direction)
  724. {
  725. struct scatterlist *s;
  726. int i;
  727. if (unlikely(global_disable))
  728. return;
  729. for_each_sg(sg, s, nelems, i) {
  730. check_sync(dev, s->dma_address, s->dma_length, 0,
  731. direction, true);
  732. }
  733. }
  734. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  735. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  736. int nelems, int direction)
  737. {
  738. struct scatterlist *s;
  739. int i;
  740. if (unlikely(global_disable))
  741. return;
  742. for_each_sg(sg, s, nelems, i) {
  743. check_sync(dev, s->dma_address, s->dma_length, 0,
  744. direction, false);
  745. }
  746. }
  747. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);