dma-debug.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. static void check_unmap(struct dma_debug_entry *ref)
  695. {
  696. struct dma_debug_entry *entry;
  697. struct hash_bucket *bucket;
  698. unsigned long flags;
  699. bucket = get_hash_bucket(ref, &flags);
  700. entry = bucket_find_exact(bucket, ref);
  701. if (!entry) {
  702. /* must drop lock before calling dma_mapping_error */
  703. put_hash_bucket(bucket, &flags);
  704. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  705. err_printk(ref->dev, NULL,
  706. "DMA-API: device driver tries to free an "
  707. "invalid DMA memory address\n");
  708. } else {
  709. err_printk(ref->dev, NULL,
  710. "DMA-API: device driver tries to free DMA "
  711. "memory it has not allocated [device "
  712. "address=0x%016llx] [size=%llu bytes]\n",
  713. ref->dev_addr, ref->size);
  714. }
  715. return;
  716. }
  717. if (ref->size != entry->size) {
  718. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  719. "DMA memory with different size "
  720. "[device address=0x%016llx] [map size=%llu bytes] "
  721. "[unmap size=%llu bytes]\n",
  722. ref->dev_addr, entry->size, ref->size);
  723. }
  724. if (ref->type != entry->type) {
  725. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  726. "DMA memory with wrong function "
  727. "[device address=0x%016llx] [size=%llu bytes] "
  728. "[mapped as %s] [unmapped as %s]\n",
  729. ref->dev_addr, ref->size,
  730. type2name[entry->type], type2name[ref->type]);
  731. } else if ((entry->type == dma_debug_coherent) &&
  732. (ref->paddr != entry->paddr)) {
  733. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  734. "DMA memory with different CPU address "
  735. "[device address=0x%016llx] [size=%llu bytes] "
  736. "[cpu alloc address=0x%016llx] "
  737. "[cpu free address=0x%016llx]",
  738. ref->dev_addr, ref->size,
  739. (unsigned long long)entry->paddr,
  740. (unsigned long long)ref->paddr);
  741. }
  742. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  743. ref->sg_call_ents != entry->sg_call_ents) {
  744. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  745. "DMA sg list with different entry count "
  746. "[map count=%d] [unmap count=%d]\n",
  747. entry->sg_call_ents, ref->sg_call_ents);
  748. }
  749. /*
  750. * This may be no bug in reality - but most implementations of the
  751. * DMA API don't handle this properly, so check for it here
  752. */
  753. if (ref->direction != entry->direction) {
  754. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  755. "DMA memory with different direction "
  756. "[device address=0x%016llx] [size=%llu bytes] "
  757. "[mapped with %s] [unmapped with %s]\n",
  758. ref->dev_addr, ref->size,
  759. dir2name[entry->direction],
  760. dir2name[ref->direction]);
  761. }
  762. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  763. err_printk(ref->dev, entry,
  764. "DMA-API: device driver failed to check map error"
  765. "[device address=0x%016llx] [size=%llu bytes] "
  766. "[mapped as %s]",
  767. ref->dev_addr, ref->size,
  768. type2name[entry->type]);
  769. }
  770. hash_bucket_del(entry);
  771. dma_entry_free(entry);
  772. put_hash_bucket(bucket, &flags);
  773. }
  774. static void check_for_stack(struct device *dev, void *addr)
  775. {
  776. if (object_is_on_stack(addr))
  777. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  778. "stack [addr=%p]\n", addr);
  779. }
  780. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  781. {
  782. unsigned long a1 = (unsigned long)addr;
  783. unsigned long b1 = a1 + len;
  784. unsigned long a2 = (unsigned long)start;
  785. unsigned long b2 = (unsigned long)end;
  786. return !(b1 <= a2 || a1 >= b2);
  787. }
  788. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  789. {
  790. if (overlap(addr, len, _text, _etext) ||
  791. overlap(addr, len, __start_rodata, __end_rodata))
  792. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  793. }
  794. static void check_sync(struct device *dev,
  795. struct dma_debug_entry *ref,
  796. bool to_cpu)
  797. {
  798. struct dma_debug_entry *entry;
  799. struct hash_bucket *bucket;
  800. unsigned long flags;
  801. bucket = get_hash_bucket(ref, &flags);
  802. entry = bucket_find_contain(&bucket, ref, &flags);
  803. if (!entry) {
  804. err_printk(dev, NULL, "DMA-API: device driver tries "
  805. "to sync DMA memory it has not allocated "
  806. "[device address=0x%016llx] [size=%llu bytes]\n",
  807. (unsigned long long)ref->dev_addr, ref->size);
  808. goto out;
  809. }
  810. if (ref->size > entry->size) {
  811. err_printk(dev, entry, "DMA-API: device driver syncs"
  812. " DMA memory outside allocated range "
  813. "[device address=0x%016llx] "
  814. "[allocation size=%llu bytes] "
  815. "[sync offset+size=%llu]\n",
  816. entry->dev_addr, entry->size,
  817. ref->size);
  818. }
  819. if (entry->direction == DMA_BIDIRECTIONAL)
  820. goto out;
  821. if (ref->direction != entry->direction) {
  822. err_printk(dev, entry, "DMA-API: device driver syncs "
  823. "DMA memory with different direction "
  824. "[device address=0x%016llx] [size=%llu bytes] "
  825. "[mapped with %s] [synced with %s]\n",
  826. (unsigned long long)ref->dev_addr, entry->size,
  827. dir2name[entry->direction],
  828. dir2name[ref->direction]);
  829. }
  830. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  831. !(ref->direction == DMA_TO_DEVICE))
  832. err_printk(dev, entry, "DMA-API: device driver syncs "
  833. "device read-only DMA memory for cpu "
  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. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  840. !(ref->direction == DMA_FROM_DEVICE))
  841. err_printk(dev, entry, "DMA-API: device driver syncs "
  842. "device write-only DMA memory to device "
  843. "[device address=0x%016llx] [size=%llu bytes] "
  844. "[mapped with %s] [synced with %s]\n",
  845. (unsigned long long)ref->dev_addr, entry->size,
  846. dir2name[entry->direction],
  847. dir2name[ref->direction]);
  848. out:
  849. put_hash_bucket(bucket, &flags);
  850. }
  851. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  852. size_t size, int direction, dma_addr_t dma_addr,
  853. bool map_single)
  854. {
  855. struct dma_debug_entry *entry;
  856. if (unlikely(global_disable))
  857. return;
  858. if (dma_mapping_error(dev, dma_addr))
  859. return;
  860. entry = dma_entry_alloc();
  861. if (!entry)
  862. return;
  863. entry->dev = dev;
  864. entry->type = dma_debug_page;
  865. entry->paddr = page_to_phys(page) + offset;
  866. entry->dev_addr = dma_addr;
  867. entry->size = size;
  868. entry->direction = direction;
  869. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  870. if (map_single)
  871. entry->type = dma_debug_single;
  872. if (!PageHighMem(page)) {
  873. void *addr = page_address(page) + offset;
  874. check_for_stack(dev, addr);
  875. check_for_illegal_area(dev, addr, size);
  876. }
  877. add_dma_entry(entry);
  878. }
  879. EXPORT_SYMBOL(debug_dma_map_page);
  880. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  881. {
  882. struct dma_debug_entry ref;
  883. struct dma_debug_entry *entry;
  884. struct hash_bucket *bucket;
  885. unsigned long flags;
  886. if (unlikely(global_disable))
  887. return;
  888. ref.dev = dev;
  889. ref.dev_addr = dma_addr;
  890. bucket = get_hash_bucket(&ref, &flags);
  891. list_for_each_entry(entry, &bucket->list, list) {
  892. if (!exact_match(&ref, entry))
  893. continue;
  894. /*
  895. * The same physical address can be mapped multiple
  896. * times. Without a hardware IOMMU this results in the
  897. * same device addresses being put into the dma-debug
  898. * hash multiple times too. This can result in false
  899. * positives being reported. Therefore we implement a
  900. * best-fit algorithm here which updates the first entry
  901. * from the hash which fits the reference value and is
  902. * not currently listed as being checked.
  903. */
  904. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  905. entry->map_err_type = MAP_ERR_CHECKED;
  906. break;
  907. }
  908. }
  909. put_hash_bucket(bucket, &flags);
  910. }
  911. EXPORT_SYMBOL(debug_dma_mapping_error);
  912. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  913. size_t size, int direction, bool map_single)
  914. {
  915. struct dma_debug_entry ref = {
  916. .type = dma_debug_page,
  917. .dev = dev,
  918. .dev_addr = addr,
  919. .size = size,
  920. .direction = direction,
  921. };
  922. if (unlikely(global_disable))
  923. return;
  924. if (map_single)
  925. ref.type = dma_debug_single;
  926. check_unmap(&ref);
  927. }
  928. EXPORT_SYMBOL(debug_dma_unmap_page);
  929. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  930. int nents, int mapped_ents, int direction)
  931. {
  932. struct dma_debug_entry *entry;
  933. struct scatterlist *s;
  934. int i;
  935. if (unlikely(global_disable))
  936. return;
  937. for_each_sg(sg, s, mapped_ents, i) {
  938. entry = dma_entry_alloc();
  939. if (!entry)
  940. return;
  941. entry->type = dma_debug_sg;
  942. entry->dev = dev;
  943. entry->paddr = sg_phys(s);
  944. entry->size = sg_dma_len(s);
  945. entry->dev_addr = sg_dma_address(s);
  946. entry->direction = direction;
  947. entry->sg_call_ents = nents;
  948. entry->sg_mapped_ents = mapped_ents;
  949. if (!PageHighMem(sg_page(s))) {
  950. check_for_stack(dev, sg_virt(s));
  951. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  952. }
  953. add_dma_entry(entry);
  954. }
  955. }
  956. EXPORT_SYMBOL(debug_dma_map_sg);
  957. static int get_nr_mapped_entries(struct device *dev,
  958. struct dma_debug_entry *ref)
  959. {
  960. struct dma_debug_entry *entry;
  961. struct hash_bucket *bucket;
  962. unsigned long flags;
  963. int mapped_ents;
  964. bucket = get_hash_bucket(ref, &flags);
  965. entry = bucket_find_exact(bucket, ref);
  966. mapped_ents = 0;
  967. if (entry)
  968. mapped_ents = entry->sg_mapped_ents;
  969. put_hash_bucket(bucket, &flags);
  970. return mapped_ents;
  971. }
  972. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  973. int nelems, int dir)
  974. {
  975. struct scatterlist *s;
  976. int mapped_ents = 0, i;
  977. if (unlikely(global_disable))
  978. return;
  979. for_each_sg(sglist, s, nelems, i) {
  980. struct dma_debug_entry ref = {
  981. .type = dma_debug_sg,
  982. .dev = dev,
  983. .paddr = sg_phys(s),
  984. .dev_addr = sg_dma_address(s),
  985. .size = sg_dma_len(s),
  986. .direction = dir,
  987. .sg_call_ents = nelems,
  988. };
  989. if (mapped_ents && i >= mapped_ents)
  990. break;
  991. if (!i)
  992. mapped_ents = get_nr_mapped_entries(dev, &ref);
  993. check_unmap(&ref);
  994. }
  995. }
  996. EXPORT_SYMBOL(debug_dma_unmap_sg);
  997. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  998. dma_addr_t dma_addr, void *virt)
  999. {
  1000. struct dma_debug_entry *entry;
  1001. if (unlikely(global_disable))
  1002. return;
  1003. if (unlikely(virt == NULL))
  1004. return;
  1005. entry = dma_entry_alloc();
  1006. if (!entry)
  1007. return;
  1008. entry->type = dma_debug_coherent;
  1009. entry->dev = dev;
  1010. entry->paddr = virt_to_phys(virt);
  1011. entry->size = size;
  1012. entry->dev_addr = dma_addr;
  1013. entry->direction = DMA_BIDIRECTIONAL;
  1014. add_dma_entry(entry);
  1015. }
  1016. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1017. void debug_dma_free_coherent(struct device *dev, size_t size,
  1018. void *virt, dma_addr_t addr)
  1019. {
  1020. struct dma_debug_entry ref = {
  1021. .type = dma_debug_coherent,
  1022. .dev = dev,
  1023. .paddr = virt_to_phys(virt),
  1024. .dev_addr = addr,
  1025. .size = size,
  1026. .direction = DMA_BIDIRECTIONAL,
  1027. };
  1028. if (unlikely(global_disable))
  1029. return;
  1030. check_unmap(&ref);
  1031. }
  1032. EXPORT_SYMBOL(debug_dma_free_coherent);
  1033. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1034. size_t size, int direction)
  1035. {
  1036. struct dma_debug_entry ref;
  1037. if (unlikely(global_disable))
  1038. return;
  1039. ref.type = dma_debug_single;
  1040. ref.dev = dev;
  1041. ref.dev_addr = dma_handle;
  1042. ref.size = size;
  1043. ref.direction = direction;
  1044. ref.sg_call_ents = 0;
  1045. check_sync(dev, &ref, true);
  1046. }
  1047. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1048. void debug_dma_sync_single_for_device(struct device *dev,
  1049. dma_addr_t dma_handle, size_t size,
  1050. int direction)
  1051. {
  1052. struct dma_debug_entry ref;
  1053. if (unlikely(global_disable))
  1054. return;
  1055. ref.type = dma_debug_single;
  1056. ref.dev = dev;
  1057. ref.dev_addr = dma_handle;
  1058. ref.size = size;
  1059. ref.direction = direction;
  1060. ref.sg_call_ents = 0;
  1061. check_sync(dev, &ref, false);
  1062. }
  1063. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1064. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1065. dma_addr_t dma_handle,
  1066. unsigned long offset, size_t size,
  1067. int direction)
  1068. {
  1069. struct dma_debug_entry ref;
  1070. if (unlikely(global_disable))
  1071. return;
  1072. ref.type = dma_debug_single;
  1073. ref.dev = dev;
  1074. ref.dev_addr = dma_handle;
  1075. ref.size = offset + size;
  1076. ref.direction = direction;
  1077. ref.sg_call_ents = 0;
  1078. check_sync(dev, &ref, true);
  1079. }
  1080. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1081. void debug_dma_sync_single_range_for_device(struct device *dev,
  1082. dma_addr_t dma_handle,
  1083. unsigned long offset,
  1084. size_t size, int direction)
  1085. {
  1086. struct dma_debug_entry ref;
  1087. if (unlikely(global_disable))
  1088. return;
  1089. ref.type = dma_debug_single;
  1090. ref.dev = dev;
  1091. ref.dev_addr = dma_handle;
  1092. ref.size = offset + size;
  1093. ref.direction = direction;
  1094. ref.sg_call_ents = 0;
  1095. check_sync(dev, &ref, false);
  1096. }
  1097. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1098. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1099. int nelems, int direction)
  1100. {
  1101. struct scatterlist *s;
  1102. int mapped_ents = 0, i;
  1103. if (unlikely(global_disable))
  1104. return;
  1105. for_each_sg(sg, s, nelems, i) {
  1106. struct dma_debug_entry ref = {
  1107. .type = dma_debug_sg,
  1108. .dev = dev,
  1109. .paddr = sg_phys(s),
  1110. .dev_addr = sg_dma_address(s),
  1111. .size = sg_dma_len(s),
  1112. .direction = direction,
  1113. .sg_call_ents = nelems,
  1114. };
  1115. if (!i)
  1116. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1117. if (i >= mapped_ents)
  1118. break;
  1119. check_sync(dev, &ref, true);
  1120. }
  1121. }
  1122. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1123. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1124. int nelems, int direction)
  1125. {
  1126. struct scatterlist *s;
  1127. int mapped_ents = 0, i;
  1128. if (unlikely(global_disable))
  1129. return;
  1130. for_each_sg(sg, s, nelems, i) {
  1131. struct dma_debug_entry ref = {
  1132. .type = dma_debug_sg,
  1133. .dev = dev,
  1134. .paddr = sg_phys(s),
  1135. .dev_addr = sg_dma_address(s),
  1136. .size = sg_dma_len(s),
  1137. .direction = direction,
  1138. .sg_call_ents = nelems,
  1139. };
  1140. if (!i)
  1141. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1142. if (i >= mapped_ents)
  1143. break;
  1144. check_sync(dev, &ref, false);
  1145. }
  1146. }
  1147. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1148. static int __init dma_debug_driver_setup(char *str)
  1149. {
  1150. int i;
  1151. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1152. current_driver_name[i] = *str;
  1153. if (*str == 0)
  1154. break;
  1155. }
  1156. if (current_driver_name[0])
  1157. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1158. current_driver_name);
  1159. return 1;
  1160. }
  1161. __setup("dma_debug_driver=", dma_debug_driver_setup);