dma-debug.c 25 KB

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