dma-debug.c 24 KB

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