dma-debug.c 16 KB

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