dma-debug.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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/uaccess.h>
  26. #include <linux/export.h>
  27. #include <linux/device.h>
  28. #include <linux/types.h>
  29. #include <linux/sched.h>
  30. #include <linux/ctype.h>
  31. #include <linux/list.h>
  32. #include <linux/slab.h>
  33. #include <asm/sections.h>
  34. #define HASH_SIZE 1024ULL
  35. #define HASH_FN_SHIFT 13
  36. #define HASH_FN_MASK (HASH_SIZE - 1)
  37. enum {
  38. dma_debug_single,
  39. dma_debug_page,
  40. dma_debug_sg,
  41. dma_debug_coherent,
  42. };
  43. enum map_err_types {
  44. MAP_ERR_CHECK_NOT_APPLICABLE,
  45. MAP_ERR_NOT_CHECKED,
  46. MAP_ERR_CHECKED,
  47. };
  48. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  49. struct dma_debug_entry {
  50. struct list_head list;
  51. struct device *dev;
  52. int type;
  53. phys_addr_t paddr;
  54. u64 dev_addr;
  55. u64 size;
  56. int direction;
  57. int sg_call_ents;
  58. int sg_mapped_ents;
  59. enum map_err_types map_err_type;
  60. #ifdef CONFIG_STACKTRACE
  61. struct stack_trace stacktrace;
  62. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  63. #endif
  64. };
  65. typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
  66. struct hash_bucket {
  67. struct list_head list;
  68. spinlock_t lock;
  69. } ____cacheline_aligned_in_smp;
  70. /* Hash list to save the allocated dma addresses */
  71. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  72. /* List of pre-allocated dma_debug_entry's */
  73. static LIST_HEAD(free_entries);
  74. /* Lock for the list above */
  75. static DEFINE_SPINLOCK(free_entries_lock);
  76. /* Global disable flag - will be set in case of an error */
  77. static u32 global_disable __read_mostly;
  78. /* Global error count */
  79. static u32 error_count;
  80. /* Global error show enable*/
  81. static u32 show_all_errors __read_mostly;
  82. /* Number of errors to show */
  83. static u32 show_num_errors = 1;
  84. static u32 num_free_entries;
  85. static u32 min_free_entries;
  86. static u32 nr_total_entries;
  87. /* number of preallocated entries requested by kernel cmdline */
  88. static u32 req_entries;
  89. /* debugfs dentry's for the stuff above */
  90. static struct dentry *dma_debug_dent __read_mostly;
  91. static struct dentry *global_disable_dent __read_mostly;
  92. static struct dentry *error_count_dent __read_mostly;
  93. static struct dentry *show_all_errors_dent __read_mostly;
  94. static struct dentry *show_num_errors_dent __read_mostly;
  95. static struct dentry *num_free_entries_dent __read_mostly;
  96. static struct dentry *min_free_entries_dent __read_mostly;
  97. static struct dentry *filter_dent __read_mostly;
  98. /* per-driver filter related state */
  99. #define NAME_MAX_LEN 64
  100. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  101. static struct device_driver *current_driver __read_mostly;
  102. static DEFINE_RWLOCK(driver_name_lock);
  103. static const char *const maperr2str[] = {
  104. [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
  105. [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
  106. [MAP_ERR_CHECKED] = "dma map error checked",
  107. };
  108. static const char *type2name[4] = { "single", "page",
  109. "scather-gather", "coherent" };
  110. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  111. "DMA_FROM_DEVICE", "DMA_NONE" };
  112. /*
  113. * The access to some variables in this macro is racy. We can't use atomic_t
  114. * here because all these variables are exported to debugfs. Some of them even
  115. * writeable. This is also the reason why a lock won't help much. But anyway,
  116. * the races are no big deal. Here is why:
  117. *
  118. * error_count: the addition is racy, but the worst thing that can happen is
  119. * that we don't count some errors
  120. * show_num_errors: the subtraction is racy. Also no big deal because in
  121. * worst case this will result in one warning more in the
  122. * system log than the user configured. This variable is
  123. * writeable via debugfs.
  124. */
  125. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  126. {
  127. #ifdef CONFIG_STACKTRACE
  128. if (entry) {
  129. pr_warning("Mapped at:\n");
  130. print_stack_trace(&entry->stacktrace, 0);
  131. }
  132. #endif
  133. }
  134. static bool driver_filter(struct device *dev)
  135. {
  136. struct device_driver *drv;
  137. unsigned long flags;
  138. bool ret;
  139. /* driver filter off */
  140. if (likely(!current_driver_name[0]))
  141. return true;
  142. /* driver filter on and initialized */
  143. if (current_driver && dev && dev->driver == current_driver)
  144. return true;
  145. /* driver filter on, but we can't filter on a NULL device... */
  146. if (!dev)
  147. return false;
  148. if (current_driver || !current_driver_name[0])
  149. return false;
  150. /* driver filter on but not yet initialized */
  151. drv = dev->driver;
  152. if (!drv)
  153. return false;
  154. /* lock to protect against change of current_driver_name */
  155. read_lock_irqsave(&driver_name_lock, flags);
  156. ret = false;
  157. if (drv->name &&
  158. strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
  159. current_driver = drv;
  160. ret = true;
  161. }
  162. read_unlock_irqrestore(&driver_name_lock, flags);
  163. return ret;
  164. }
  165. #define err_printk(dev, entry, format, arg...) do { \
  166. error_count += 1; \
  167. if (driver_filter(dev) && \
  168. (show_all_errors || show_num_errors > 0)) { \
  169. WARN(1, "%s %s: " format, \
  170. dev ? dev_driver_string(dev) : "NULL", \
  171. dev ? dev_name(dev) : "NULL", ## arg); \
  172. dump_entry_trace(entry); \
  173. } \
  174. if (!show_all_errors && show_num_errors > 0) \
  175. show_num_errors -= 1; \
  176. } while (0);
  177. /*
  178. * Hash related functions
  179. *
  180. * Every DMA-API request is saved into a struct dma_debug_entry. To
  181. * have quick access to these structs they are stored into a hash.
  182. */
  183. static int hash_fn(struct dma_debug_entry *entry)
  184. {
  185. /*
  186. * Hash function is based on the dma address.
  187. * We use bits 20-27 here as the index into the hash
  188. */
  189. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  190. }
  191. /*
  192. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  193. */
  194. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  195. unsigned long *flags)
  196. {
  197. int idx = hash_fn(entry);
  198. unsigned long __flags;
  199. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  200. *flags = __flags;
  201. return &dma_entry_hash[idx];
  202. }
  203. /*
  204. * Give up exclusive access to the hash bucket
  205. */
  206. static void put_hash_bucket(struct hash_bucket *bucket,
  207. unsigned long *flags)
  208. {
  209. unsigned long __flags = *flags;
  210. spin_unlock_irqrestore(&bucket->lock, __flags);
  211. }
  212. static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
  213. {
  214. return ((a->dev_addr == b->dev_addr) &&
  215. (a->dev == b->dev)) ? true : false;
  216. }
  217. static bool containing_match(struct dma_debug_entry *a,
  218. struct dma_debug_entry *b)
  219. {
  220. if (a->dev != b->dev)
  221. return false;
  222. if ((b->dev_addr <= a->dev_addr) &&
  223. ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
  224. return true;
  225. return false;
  226. }
  227. /*
  228. * Search a given entry in the hash bucket list
  229. */
  230. static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
  231. struct dma_debug_entry *ref,
  232. match_fn match)
  233. {
  234. struct dma_debug_entry *entry, *ret = NULL;
  235. int matches = 0, match_lvl, last_lvl = -1;
  236. list_for_each_entry(entry, &bucket->list, list) {
  237. if (!match(ref, entry))
  238. continue;
  239. /*
  240. * Some drivers map the same physical address multiple
  241. * times. Without a hardware IOMMU this results in the
  242. * same device addresses being put into the dma-debug
  243. * hash multiple times too. This can result in false
  244. * positives being reported. Therefore we implement a
  245. * best-fit algorithm here which returns the entry from
  246. * the hash which fits best to the reference value
  247. * instead of the first-fit.
  248. */
  249. matches += 1;
  250. match_lvl = 0;
  251. entry->size == ref->size ? ++match_lvl : 0;
  252. entry->type == ref->type ? ++match_lvl : 0;
  253. entry->direction == ref->direction ? ++match_lvl : 0;
  254. entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
  255. if (match_lvl == 4) {
  256. /* perfect-fit - return the result */
  257. return entry;
  258. } else if (match_lvl > last_lvl) {
  259. /*
  260. * We found an entry that fits better then the
  261. * previous one or it is the 1st match.
  262. */
  263. last_lvl = match_lvl;
  264. ret = entry;
  265. }
  266. }
  267. /*
  268. * If we have multiple matches but no perfect-fit, just return
  269. * NULL.
  270. */
  271. ret = (matches == 1) ? ret : NULL;
  272. return ret;
  273. }
  274. static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
  275. struct dma_debug_entry *ref)
  276. {
  277. return __hash_bucket_find(bucket, ref, exact_match);
  278. }
  279. static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
  280. struct dma_debug_entry *ref,
  281. unsigned long *flags)
  282. {
  283. unsigned int max_range = dma_get_max_seg_size(ref->dev);
  284. struct dma_debug_entry *entry, index = *ref;
  285. unsigned int range = 0;
  286. while (range <= max_range) {
  287. entry = __hash_bucket_find(*bucket, &index, containing_match);
  288. if (entry)
  289. return entry;
  290. /*
  291. * Nothing found, go back a hash bucket
  292. */
  293. put_hash_bucket(*bucket, flags);
  294. range += (1 << HASH_FN_SHIFT);
  295. index.dev_addr -= (1 << HASH_FN_SHIFT);
  296. *bucket = get_hash_bucket(&index, flags);
  297. }
  298. return NULL;
  299. }
  300. /*
  301. * Add an entry to a hash bucket
  302. */
  303. static void hash_bucket_add(struct hash_bucket *bucket,
  304. struct dma_debug_entry *entry)
  305. {
  306. list_add_tail(&entry->list, &bucket->list);
  307. }
  308. /*
  309. * Remove entry from a hash bucket list
  310. */
  311. static void hash_bucket_del(struct dma_debug_entry *entry)
  312. {
  313. list_del(&entry->list);
  314. }
  315. /*
  316. * Dump mapping entries for debugging purposes
  317. */
  318. void debug_dma_dump_mappings(struct device *dev)
  319. {
  320. int idx;
  321. for (idx = 0; idx < HASH_SIZE; idx++) {
  322. struct hash_bucket *bucket = &dma_entry_hash[idx];
  323. struct dma_debug_entry *entry;
  324. unsigned long flags;
  325. spin_lock_irqsave(&bucket->lock, flags);
  326. list_for_each_entry(entry, &bucket->list, list) {
  327. if (!dev || dev == entry->dev) {
  328. dev_info(entry->dev,
  329. "%s idx %d P=%Lx D=%Lx L=%Lx %s %s\n",
  330. type2name[entry->type], idx,
  331. (unsigned long long)entry->paddr,
  332. entry->dev_addr, entry->size,
  333. dir2name[entry->direction],
  334. maperr2str[entry->map_err_type]);
  335. }
  336. }
  337. spin_unlock_irqrestore(&bucket->lock, flags);
  338. }
  339. }
  340. EXPORT_SYMBOL(debug_dma_dump_mappings);
  341. /*
  342. * Wrapper function for adding an entry to the hash.
  343. * This function takes care of locking itself.
  344. */
  345. static void add_dma_entry(struct dma_debug_entry *entry)
  346. {
  347. struct hash_bucket *bucket;
  348. unsigned long flags;
  349. bucket = get_hash_bucket(entry, &flags);
  350. hash_bucket_add(bucket, entry);
  351. put_hash_bucket(bucket, &flags);
  352. }
  353. static struct dma_debug_entry *__dma_entry_alloc(void)
  354. {
  355. struct dma_debug_entry *entry;
  356. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  357. list_del(&entry->list);
  358. memset(entry, 0, sizeof(*entry));
  359. num_free_entries -= 1;
  360. if (num_free_entries < min_free_entries)
  361. min_free_entries = num_free_entries;
  362. return entry;
  363. }
  364. /* struct dma_entry allocator
  365. *
  366. * The next two functions implement the allocator for
  367. * struct dma_debug_entries.
  368. */
  369. static struct dma_debug_entry *dma_entry_alloc(void)
  370. {
  371. struct dma_debug_entry *entry;
  372. unsigned long flags;
  373. spin_lock_irqsave(&free_entries_lock, flags);
  374. if (list_empty(&free_entries)) {
  375. pr_err("DMA-API: debugging out of memory - disabling\n");
  376. global_disable = true;
  377. spin_unlock_irqrestore(&free_entries_lock, flags);
  378. return NULL;
  379. }
  380. entry = __dma_entry_alloc();
  381. spin_unlock_irqrestore(&free_entries_lock, flags);
  382. #ifdef CONFIG_STACKTRACE
  383. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  384. entry->stacktrace.entries = entry->st_entries;
  385. entry->stacktrace.skip = 2;
  386. save_stack_trace(&entry->stacktrace);
  387. #endif
  388. return entry;
  389. }
  390. static void dma_entry_free(struct dma_debug_entry *entry)
  391. {
  392. unsigned long flags;
  393. /*
  394. * add to beginning of the list - this way the entries are
  395. * more likely cache hot when they are reallocated.
  396. */
  397. spin_lock_irqsave(&free_entries_lock, flags);
  398. list_add(&entry->list, &free_entries);
  399. num_free_entries += 1;
  400. spin_unlock_irqrestore(&free_entries_lock, flags);
  401. }
  402. int dma_debug_resize_entries(u32 num_entries)
  403. {
  404. int i, delta, ret = 0;
  405. unsigned long flags;
  406. struct dma_debug_entry *entry;
  407. LIST_HEAD(tmp);
  408. spin_lock_irqsave(&free_entries_lock, flags);
  409. if (nr_total_entries < num_entries) {
  410. delta = num_entries - nr_total_entries;
  411. spin_unlock_irqrestore(&free_entries_lock, flags);
  412. for (i = 0; i < delta; i++) {
  413. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  414. if (!entry)
  415. break;
  416. list_add_tail(&entry->list, &tmp);
  417. }
  418. spin_lock_irqsave(&free_entries_lock, flags);
  419. list_splice(&tmp, &free_entries);
  420. nr_total_entries += i;
  421. num_free_entries += i;
  422. } else {
  423. delta = nr_total_entries - num_entries;
  424. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  425. entry = __dma_entry_alloc();
  426. kfree(entry);
  427. }
  428. nr_total_entries -= i;
  429. }
  430. if (nr_total_entries != num_entries)
  431. ret = 1;
  432. spin_unlock_irqrestore(&free_entries_lock, flags);
  433. return ret;
  434. }
  435. EXPORT_SYMBOL(dma_debug_resize_entries);
  436. /*
  437. * DMA-API debugging init code
  438. *
  439. * The init code does two things:
  440. * 1. Initialize core data structures
  441. * 2. Preallocate a given number of dma_debug_entry structs
  442. */
  443. static int prealloc_memory(u32 num_entries)
  444. {
  445. struct dma_debug_entry *entry, *next_entry;
  446. int i;
  447. for (i = 0; i < num_entries; ++i) {
  448. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  449. if (!entry)
  450. goto out_err;
  451. list_add_tail(&entry->list, &free_entries);
  452. }
  453. num_free_entries = num_entries;
  454. min_free_entries = num_entries;
  455. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  456. return 0;
  457. out_err:
  458. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  459. list_del(&entry->list);
  460. kfree(entry);
  461. }
  462. return -ENOMEM;
  463. }
  464. static ssize_t filter_read(struct file *file, char __user *user_buf,
  465. size_t count, loff_t *ppos)
  466. {
  467. char buf[NAME_MAX_LEN + 1];
  468. unsigned long flags;
  469. int len;
  470. if (!current_driver_name[0])
  471. return 0;
  472. /*
  473. * We can't copy to userspace directly because current_driver_name can
  474. * only be read under the driver_name_lock with irqs disabled. So
  475. * create a temporary copy first.
  476. */
  477. read_lock_irqsave(&driver_name_lock, flags);
  478. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  479. read_unlock_irqrestore(&driver_name_lock, flags);
  480. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  481. }
  482. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  483. size_t count, loff_t *ppos)
  484. {
  485. char buf[NAME_MAX_LEN];
  486. unsigned long flags;
  487. size_t len;
  488. int i;
  489. /*
  490. * We can't copy from userspace directly. Access to
  491. * current_driver_name is protected with a write_lock with irqs
  492. * disabled. Since copy_from_user can fault and may sleep we
  493. * need to copy to temporary buffer first
  494. */
  495. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  496. if (copy_from_user(buf, userbuf, len))
  497. return -EFAULT;
  498. buf[len] = 0;
  499. write_lock_irqsave(&driver_name_lock, flags);
  500. /*
  501. * Now handle the string we got from userspace very carefully.
  502. * The rules are:
  503. * - only use the first token we got
  504. * - token delimiter is everything looking like a space
  505. * character (' ', '\n', '\t' ...)
  506. *
  507. */
  508. if (!isalnum(buf[0])) {
  509. /*
  510. * If the first character userspace gave us is not
  511. * alphanumerical then assume the filter should be
  512. * switched off.
  513. */
  514. if (current_driver_name[0])
  515. pr_info("DMA-API: switching off dma-debug driver filter\n");
  516. current_driver_name[0] = 0;
  517. current_driver = NULL;
  518. goto out_unlock;
  519. }
  520. /*
  521. * Now parse out the first token and use it as the name for the
  522. * driver to filter for.
  523. */
  524. for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
  525. current_driver_name[i] = buf[i];
  526. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  527. break;
  528. }
  529. current_driver_name[i] = 0;
  530. current_driver = NULL;
  531. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  532. current_driver_name);
  533. out_unlock:
  534. write_unlock_irqrestore(&driver_name_lock, flags);
  535. return count;
  536. }
  537. static const struct file_operations filter_fops = {
  538. .read = filter_read,
  539. .write = filter_write,
  540. .llseek = default_llseek,
  541. };
  542. static int dma_debug_fs_init(void)
  543. {
  544. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  545. if (!dma_debug_dent) {
  546. pr_err("DMA-API: can not create debugfs directory\n");
  547. return -ENOMEM;
  548. }
  549. global_disable_dent = debugfs_create_bool("disabled", 0444,
  550. dma_debug_dent,
  551. &global_disable);
  552. if (!global_disable_dent)
  553. goto out_err;
  554. error_count_dent = debugfs_create_u32("error_count", 0444,
  555. dma_debug_dent, &error_count);
  556. if (!error_count_dent)
  557. goto out_err;
  558. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  559. dma_debug_dent,
  560. &show_all_errors);
  561. if (!show_all_errors_dent)
  562. goto out_err;
  563. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  564. dma_debug_dent,
  565. &show_num_errors);
  566. if (!show_num_errors_dent)
  567. goto out_err;
  568. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  569. dma_debug_dent,
  570. &num_free_entries);
  571. if (!num_free_entries_dent)
  572. goto out_err;
  573. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  574. dma_debug_dent,
  575. &min_free_entries);
  576. if (!min_free_entries_dent)
  577. goto out_err;
  578. filter_dent = debugfs_create_file("driver_filter", 0644,
  579. dma_debug_dent, NULL, &filter_fops);
  580. if (!filter_dent)
  581. goto out_err;
  582. return 0;
  583. out_err:
  584. debugfs_remove_recursive(dma_debug_dent);
  585. return -ENOMEM;
  586. }
  587. static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
  588. {
  589. struct dma_debug_entry *entry;
  590. unsigned long flags;
  591. int count = 0, i;
  592. local_irq_save(flags);
  593. for (i = 0; i < HASH_SIZE; ++i) {
  594. spin_lock(&dma_entry_hash[i].lock);
  595. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  596. if (entry->dev == dev) {
  597. count += 1;
  598. *out_entry = entry;
  599. }
  600. }
  601. spin_unlock(&dma_entry_hash[i].lock);
  602. }
  603. local_irq_restore(flags);
  604. return count;
  605. }
  606. static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
  607. {
  608. struct device *dev = data;
  609. struct dma_debug_entry *uninitialized_var(entry);
  610. int count;
  611. if (global_disable)
  612. return 0;
  613. switch (action) {
  614. case BUS_NOTIFY_UNBOUND_DRIVER:
  615. count = device_dma_allocations(dev, &entry);
  616. if (count == 0)
  617. break;
  618. err_printk(dev, entry, "DMA-API: device driver has pending "
  619. "DMA allocations while released from device "
  620. "[count=%d]\n"
  621. "One of leaked entries details: "
  622. "[device address=0x%016llx] [size=%llu bytes] "
  623. "[mapped with %s] [mapped as %s]\n",
  624. count, entry->dev_addr, entry->size,
  625. dir2name[entry->direction], type2name[entry->type]);
  626. break;
  627. default:
  628. break;
  629. }
  630. return 0;
  631. }
  632. void dma_debug_add_bus(struct bus_type *bus)
  633. {
  634. struct notifier_block *nb;
  635. if (global_disable)
  636. return;
  637. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  638. if (nb == NULL) {
  639. pr_err("dma_debug_add_bus: out of memory\n");
  640. return;
  641. }
  642. nb->notifier_call = dma_debug_device_change;
  643. bus_register_notifier(bus, nb);
  644. }
  645. /*
  646. * Let the architectures decide how many entries should be preallocated.
  647. */
  648. void dma_debug_init(u32 num_entries)
  649. {
  650. int i;
  651. if (global_disable)
  652. return;
  653. for (i = 0; i < HASH_SIZE; ++i) {
  654. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  655. spin_lock_init(&dma_entry_hash[i].lock);
  656. }
  657. if (dma_debug_fs_init() != 0) {
  658. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  659. global_disable = true;
  660. return;
  661. }
  662. if (req_entries)
  663. num_entries = req_entries;
  664. if (prealloc_memory(num_entries) != 0) {
  665. pr_err("DMA-API: debugging out of memory error - disabled\n");
  666. global_disable = true;
  667. return;
  668. }
  669. nr_total_entries = num_free_entries;
  670. pr_info("DMA-API: debugging enabled by kernel config\n");
  671. }
  672. static __init int dma_debug_cmdline(char *str)
  673. {
  674. if (!str)
  675. return -EINVAL;
  676. if (strncmp(str, "off", 3) == 0) {
  677. pr_info("DMA-API: debugging disabled on kernel command line\n");
  678. global_disable = true;
  679. }
  680. return 0;
  681. }
  682. static __init int dma_debug_entries_cmdline(char *str)
  683. {
  684. int res;
  685. if (!str)
  686. return -EINVAL;
  687. res = get_option(&str, &req_entries);
  688. if (!res)
  689. req_entries = 0;
  690. return 0;
  691. }
  692. __setup("dma_debug=", dma_debug_cmdline);
  693. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  694. /* Calling dma_mapping_error() from dma-debug api will result in calling
  695. debug_dma_mapping_error() - need internal mapping error routine to
  696. avoid debug checks */
  697. #ifndef DMA_ERROR_CODE
  698. #define DMA_ERROR_CODE 0
  699. #endif
  700. static inline int has_mapping_error(struct device *dev, dma_addr_t dma_addr)
  701. {
  702. const struct dma_map_ops *ops = get_dma_ops(dev);
  703. if (ops->mapping_error)
  704. return ops->mapping_error(dev, dma_addr);
  705. return (dma_addr == DMA_ERROR_CODE);
  706. }
  707. static void check_unmap(struct dma_debug_entry *ref)
  708. {
  709. struct dma_debug_entry *entry;
  710. struct hash_bucket *bucket;
  711. unsigned long flags;
  712. if (unlikely(has_mapping_error(ref->dev, ref->dev_addr))) {
  713. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  714. "to free an invalid DMA memory address\n");
  715. return;
  716. }
  717. bucket = get_hash_bucket(ref, &flags);
  718. entry = bucket_find_exact(bucket, ref);
  719. if (!entry) {
  720. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  721. "to free DMA memory it has not allocated "
  722. "[device address=0x%016llx] [size=%llu bytes]\n",
  723. ref->dev_addr, ref->size);
  724. goto out;
  725. }
  726. if (ref->size != entry->size) {
  727. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  728. "DMA memory with different size "
  729. "[device address=0x%016llx] [map size=%llu bytes] "
  730. "[unmap size=%llu bytes]\n",
  731. ref->dev_addr, entry->size, ref->size);
  732. }
  733. if (ref->type != entry->type) {
  734. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  735. "DMA memory with wrong function "
  736. "[device address=0x%016llx] [size=%llu bytes] "
  737. "[mapped as %s] [unmapped as %s]\n",
  738. ref->dev_addr, ref->size,
  739. type2name[entry->type], type2name[ref->type]);
  740. } else if ((entry->type == dma_debug_coherent) &&
  741. (ref->paddr != entry->paddr)) {
  742. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  743. "DMA memory with different CPU address "
  744. "[device address=0x%016llx] [size=%llu bytes] "
  745. "[cpu alloc address=0x%016llx] "
  746. "[cpu free address=0x%016llx]",
  747. ref->dev_addr, ref->size,
  748. (unsigned long long)entry->paddr,
  749. (unsigned long long)ref->paddr);
  750. }
  751. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  752. ref->sg_call_ents != entry->sg_call_ents) {
  753. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  754. "DMA sg list with different entry count "
  755. "[map count=%d] [unmap count=%d]\n",
  756. entry->sg_call_ents, ref->sg_call_ents);
  757. }
  758. /*
  759. * This may be no bug in reality - but most implementations of the
  760. * DMA API don't handle this properly, so check for it here
  761. */
  762. if (ref->direction != entry->direction) {
  763. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  764. "DMA memory with different direction "
  765. "[device address=0x%016llx] [size=%llu bytes] "
  766. "[mapped with %s] [unmapped with %s]\n",
  767. ref->dev_addr, ref->size,
  768. dir2name[entry->direction],
  769. dir2name[ref->direction]);
  770. }
  771. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  772. err_printk(ref->dev, entry,
  773. "DMA-API: device driver failed to check map error"
  774. "[device address=0x%016llx] [size=%llu bytes] "
  775. "[mapped as %s]",
  776. ref->dev_addr, ref->size,
  777. type2name[entry->type]);
  778. }
  779. hash_bucket_del(entry);
  780. dma_entry_free(entry);
  781. out:
  782. put_hash_bucket(bucket, &flags);
  783. }
  784. static void check_for_stack(struct device *dev, void *addr)
  785. {
  786. if (object_is_on_stack(addr))
  787. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  788. "stack [addr=%p]\n", addr);
  789. }
  790. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  791. {
  792. unsigned long a1 = (unsigned long)addr;
  793. unsigned long b1 = a1 + len;
  794. unsigned long a2 = (unsigned long)start;
  795. unsigned long b2 = (unsigned long)end;
  796. return !(b1 <= a2 || a1 >= b2);
  797. }
  798. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  799. {
  800. if (overlap(addr, len, _text, _etext) ||
  801. overlap(addr, len, __start_rodata, __end_rodata))
  802. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  803. }
  804. static void check_sync(struct device *dev,
  805. struct dma_debug_entry *ref,
  806. bool to_cpu)
  807. {
  808. struct dma_debug_entry *entry;
  809. struct hash_bucket *bucket;
  810. unsigned long flags;
  811. bucket = get_hash_bucket(ref, &flags);
  812. entry = bucket_find_contain(&bucket, ref, &flags);
  813. if (!entry) {
  814. err_printk(dev, NULL, "DMA-API: device driver tries "
  815. "to sync DMA memory it has not allocated "
  816. "[device address=0x%016llx] [size=%llu bytes]\n",
  817. (unsigned long long)ref->dev_addr, ref->size);
  818. goto out;
  819. }
  820. if (ref->size > entry->size) {
  821. err_printk(dev, entry, "DMA-API: device driver syncs"
  822. " DMA memory outside allocated range "
  823. "[device address=0x%016llx] "
  824. "[allocation size=%llu bytes] "
  825. "[sync offset+size=%llu]\n",
  826. entry->dev_addr, entry->size,
  827. ref->size);
  828. }
  829. if (entry->direction == DMA_BIDIRECTIONAL)
  830. goto out;
  831. if (ref->direction != entry->direction) {
  832. err_printk(dev, entry, "DMA-API: device driver syncs "
  833. "DMA memory with different direction "
  834. "[device address=0x%016llx] [size=%llu bytes] "
  835. "[mapped with %s] [synced with %s]\n",
  836. (unsigned long long)ref->dev_addr, entry->size,
  837. dir2name[entry->direction],
  838. dir2name[ref->direction]);
  839. }
  840. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  841. !(ref->direction == DMA_TO_DEVICE))
  842. err_printk(dev, entry, "DMA-API: device driver syncs "
  843. "device read-only DMA memory for cpu "
  844. "[device address=0x%016llx] [size=%llu bytes] "
  845. "[mapped with %s] [synced with %s]\n",
  846. (unsigned long long)ref->dev_addr, entry->size,
  847. dir2name[entry->direction],
  848. dir2name[ref->direction]);
  849. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  850. !(ref->direction == DMA_FROM_DEVICE))
  851. err_printk(dev, entry, "DMA-API: device driver syncs "
  852. "device write-only DMA memory to device "
  853. "[device address=0x%016llx] [size=%llu bytes] "
  854. "[mapped with %s] [synced with %s]\n",
  855. (unsigned long long)ref->dev_addr, entry->size,
  856. dir2name[entry->direction],
  857. dir2name[ref->direction]);
  858. out:
  859. put_hash_bucket(bucket, &flags);
  860. }
  861. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  862. size_t size, int direction, dma_addr_t dma_addr,
  863. bool map_single)
  864. {
  865. struct dma_debug_entry *entry;
  866. if (unlikely(global_disable))
  867. return;
  868. if (unlikely(has_mapping_error(dev, dma_addr)))
  869. return;
  870. entry = dma_entry_alloc();
  871. if (!entry)
  872. return;
  873. entry->dev = dev;
  874. entry->type = dma_debug_page;
  875. entry->paddr = page_to_phys(page) + offset;
  876. entry->dev_addr = dma_addr;
  877. entry->size = size;
  878. entry->direction = direction;
  879. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  880. if (map_single)
  881. entry->type = dma_debug_single;
  882. if (!PageHighMem(page)) {
  883. void *addr = page_address(page) + offset;
  884. check_for_stack(dev, addr);
  885. check_for_illegal_area(dev, addr, size);
  886. }
  887. add_dma_entry(entry);
  888. }
  889. EXPORT_SYMBOL(debug_dma_map_page);
  890. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  891. {
  892. struct dma_debug_entry ref;
  893. struct dma_debug_entry *entry;
  894. struct hash_bucket *bucket;
  895. unsigned long flags;
  896. if (unlikely(global_disable))
  897. return;
  898. ref.dev = dev;
  899. ref.dev_addr = dma_addr;
  900. bucket = get_hash_bucket(&ref, &flags);
  901. entry = bucket_find_exact(bucket, &ref);
  902. if (!entry)
  903. goto out;
  904. entry->map_err_type = MAP_ERR_CHECKED;
  905. out:
  906. put_hash_bucket(bucket, &flags);
  907. }
  908. EXPORT_SYMBOL(debug_dma_mapping_error);
  909. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  910. size_t size, int direction, bool map_single)
  911. {
  912. struct dma_debug_entry ref = {
  913. .type = dma_debug_page,
  914. .dev = dev,
  915. .dev_addr = addr,
  916. .size = size,
  917. .direction = direction,
  918. };
  919. if (unlikely(global_disable))
  920. return;
  921. if (map_single)
  922. ref.type = dma_debug_single;
  923. check_unmap(&ref);
  924. }
  925. EXPORT_SYMBOL(debug_dma_unmap_page);
  926. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  927. int nents, int mapped_ents, int direction)
  928. {
  929. struct dma_debug_entry *entry;
  930. struct scatterlist *s;
  931. int i;
  932. if (unlikely(global_disable))
  933. return;
  934. for_each_sg(sg, s, mapped_ents, i) {
  935. entry = dma_entry_alloc();
  936. if (!entry)
  937. return;
  938. entry->type = dma_debug_sg;
  939. entry->dev = dev;
  940. entry->paddr = sg_phys(s);
  941. entry->size = sg_dma_len(s);
  942. entry->dev_addr = sg_dma_address(s);
  943. entry->direction = direction;
  944. entry->sg_call_ents = nents;
  945. entry->sg_mapped_ents = mapped_ents;
  946. if (!PageHighMem(sg_page(s))) {
  947. check_for_stack(dev, sg_virt(s));
  948. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  949. }
  950. add_dma_entry(entry);
  951. }
  952. }
  953. EXPORT_SYMBOL(debug_dma_map_sg);
  954. static int get_nr_mapped_entries(struct device *dev,
  955. struct dma_debug_entry *ref)
  956. {
  957. struct dma_debug_entry *entry;
  958. struct hash_bucket *bucket;
  959. unsigned long flags;
  960. int mapped_ents;
  961. bucket = get_hash_bucket(ref, &flags);
  962. entry = bucket_find_exact(bucket, ref);
  963. mapped_ents = 0;
  964. if (entry)
  965. mapped_ents = entry->sg_mapped_ents;
  966. put_hash_bucket(bucket, &flags);
  967. return mapped_ents;
  968. }
  969. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  970. int nelems, int dir)
  971. {
  972. struct scatterlist *s;
  973. int mapped_ents = 0, i;
  974. if (unlikely(global_disable))
  975. return;
  976. for_each_sg(sglist, s, nelems, i) {
  977. struct dma_debug_entry ref = {
  978. .type = dma_debug_sg,
  979. .dev = dev,
  980. .paddr = sg_phys(s),
  981. .dev_addr = sg_dma_address(s),
  982. .size = sg_dma_len(s),
  983. .direction = dir,
  984. .sg_call_ents = nelems,
  985. };
  986. if (mapped_ents && i >= mapped_ents)
  987. break;
  988. if (!i)
  989. mapped_ents = get_nr_mapped_entries(dev, &ref);
  990. check_unmap(&ref);
  991. }
  992. }
  993. EXPORT_SYMBOL(debug_dma_unmap_sg);
  994. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  995. dma_addr_t dma_addr, void *virt)
  996. {
  997. struct dma_debug_entry *entry;
  998. if (unlikely(global_disable))
  999. return;
  1000. if (unlikely(virt == NULL))
  1001. return;
  1002. entry = dma_entry_alloc();
  1003. if (!entry)
  1004. return;
  1005. entry->type = dma_debug_coherent;
  1006. entry->dev = dev;
  1007. entry->paddr = virt_to_phys(virt);
  1008. entry->size = size;
  1009. entry->dev_addr = dma_addr;
  1010. entry->direction = DMA_BIDIRECTIONAL;
  1011. add_dma_entry(entry);
  1012. }
  1013. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1014. void debug_dma_free_coherent(struct device *dev, size_t size,
  1015. void *virt, dma_addr_t addr)
  1016. {
  1017. struct dma_debug_entry ref = {
  1018. .type = dma_debug_coherent,
  1019. .dev = dev,
  1020. .paddr = virt_to_phys(virt),
  1021. .dev_addr = addr,
  1022. .size = size,
  1023. .direction = DMA_BIDIRECTIONAL,
  1024. };
  1025. if (unlikely(global_disable))
  1026. return;
  1027. check_unmap(&ref);
  1028. }
  1029. EXPORT_SYMBOL(debug_dma_free_coherent);
  1030. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1031. size_t size, int direction)
  1032. {
  1033. struct dma_debug_entry ref;
  1034. if (unlikely(global_disable))
  1035. return;
  1036. ref.type = dma_debug_single;
  1037. ref.dev = dev;
  1038. ref.dev_addr = dma_handle;
  1039. ref.size = size;
  1040. ref.direction = direction;
  1041. ref.sg_call_ents = 0;
  1042. check_sync(dev, &ref, true);
  1043. }
  1044. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1045. void debug_dma_sync_single_for_device(struct device *dev,
  1046. dma_addr_t dma_handle, size_t size,
  1047. int direction)
  1048. {
  1049. struct dma_debug_entry ref;
  1050. if (unlikely(global_disable))
  1051. return;
  1052. ref.type = dma_debug_single;
  1053. ref.dev = dev;
  1054. ref.dev_addr = dma_handle;
  1055. ref.size = size;
  1056. ref.direction = direction;
  1057. ref.sg_call_ents = 0;
  1058. check_sync(dev, &ref, false);
  1059. }
  1060. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1061. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1062. dma_addr_t dma_handle,
  1063. unsigned long offset, size_t size,
  1064. int direction)
  1065. {
  1066. struct dma_debug_entry ref;
  1067. if (unlikely(global_disable))
  1068. return;
  1069. ref.type = dma_debug_single;
  1070. ref.dev = dev;
  1071. ref.dev_addr = dma_handle;
  1072. ref.size = offset + size;
  1073. ref.direction = direction;
  1074. ref.sg_call_ents = 0;
  1075. check_sync(dev, &ref, true);
  1076. }
  1077. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1078. void debug_dma_sync_single_range_for_device(struct device *dev,
  1079. dma_addr_t dma_handle,
  1080. unsigned long offset,
  1081. size_t size, int direction)
  1082. {
  1083. struct dma_debug_entry ref;
  1084. if (unlikely(global_disable))
  1085. return;
  1086. ref.type = dma_debug_single;
  1087. ref.dev = dev;
  1088. ref.dev_addr = dma_handle;
  1089. ref.size = offset + size;
  1090. ref.direction = direction;
  1091. ref.sg_call_ents = 0;
  1092. check_sync(dev, &ref, false);
  1093. }
  1094. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1095. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1096. int nelems, int direction)
  1097. {
  1098. struct scatterlist *s;
  1099. int mapped_ents = 0, i;
  1100. if (unlikely(global_disable))
  1101. return;
  1102. for_each_sg(sg, s, nelems, i) {
  1103. struct dma_debug_entry ref = {
  1104. .type = dma_debug_sg,
  1105. .dev = dev,
  1106. .paddr = sg_phys(s),
  1107. .dev_addr = sg_dma_address(s),
  1108. .size = sg_dma_len(s),
  1109. .direction = direction,
  1110. .sg_call_ents = nelems,
  1111. };
  1112. if (!i)
  1113. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1114. if (i >= mapped_ents)
  1115. break;
  1116. check_sync(dev, &ref, true);
  1117. }
  1118. }
  1119. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1120. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1121. int nelems, int direction)
  1122. {
  1123. struct scatterlist *s;
  1124. int mapped_ents = 0, i;
  1125. if (unlikely(global_disable))
  1126. return;
  1127. for_each_sg(sg, s, nelems, i) {
  1128. struct dma_debug_entry ref = {
  1129. .type = dma_debug_sg,
  1130. .dev = dev,
  1131. .paddr = sg_phys(s),
  1132. .dev_addr = sg_dma_address(s),
  1133. .size = sg_dma_len(s),
  1134. .direction = direction,
  1135. .sg_call_ents = nelems,
  1136. };
  1137. if (!i)
  1138. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1139. if (i >= mapped_ents)
  1140. break;
  1141. check_sync(dev, &ref, false);
  1142. }
  1143. }
  1144. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1145. static int __init dma_debug_driver_setup(char *str)
  1146. {
  1147. int i;
  1148. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1149. current_driver_name[i] = *str;
  1150. if (*str == 0)
  1151. break;
  1152. }
  1153. if (current_driver_name[0])
  1154. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1155. current_driver_name);
  1156. return 1;
  1157. }
  1158. __setup("dma_debug_driver=", dma_debug_driver_setup);