dma-debug.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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. dma_debug_resource,
  43. };
  44. enum map_err_types {
  45. MAP_ERR_CHECK_NOT_APPLICABLE,
  46. MAP_ERR_NOT_CHECKED,
  47. MAP_ERR_CHECKED,
  48. };
  49. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  50. /**
  51. * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
  52. * @list: node on pre-allocated free_entries list
  53. * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
  54. * @type: single, page, sg, coherent
  55. * @pfn: page frame of the start address
  56. * @offset: offset of mapping relative to pfn
  57. * @size: length of the mapping
  58. * @direction: enum dma_data_direction
  59. * @sg_call_ents: 'nents' from dma_map_sg
  60. * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
  61. * @map_err_type: track whether dma_mapping_error() was checked
  62. * @stacktrace: support backtraces when a violation is detected
  63. */
  64. struct dma_debug_entry {
  65. struct list_head list;
  66. struct device *dev;
  67. int type;
  68. unsigned long pfn;
  69. size_t offset;
  70. u64 dev_addr;
  71. u64 size;
  72. int direction;
  73. int sg_call_ents;
  74. int sg_mapped_ents;
  75. enum map_err_types map_err_type;
  76. #ifdef CONFIG_STACKTRACE
  77. struct stack_trace stacktrace;
  78. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  79. #endif
  80. };
  81. typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
  82. struct hash_bucket {
  83. struct list_head list;
  84. spinlock_t lock;
  85. } ____cacheline_aligned_in_smp;
  86. /* Hash list to save the allocated dma addresses */
  87. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  88. /* List of pre-allocated dma_debug_entry's */
  89. static LIST_HEAD(free_entries);
  90. /* Lock for the list above */
  91. static DEFINE_SPINLOCK(free_entries_lock);
  92. /* Global disable flag - will be set in case of an error */
  93. static bool global_disable __read_mostly;
  94. /* Early initialization disable flag, set at the end of dma_debug_init */
  95. static bool dma_debug_initialized __read_mostly;
  96. static inline bool dma_debug_disabled(void)
  97. {
  98. return global_disable || !dma_debug_initialized;
  99. }
  100. /* Global error count */
  101. static u32 error_count;
  102. /* Global error show enable*/
  103. static u32 show_all_errors __read_mostly;
  104. /* Number of errors to show */
  105. static u32 show_num_errors = 1;
  106. static u32 num_free_entries;
  107. static u32 min_free_entries;
  108. static u32 nr_total_entries;
  109. /* number of preallocated entries requested by kernel cmdline */
  110. static u32 req_entries;
  111. /* debugfs dentry's for the stuff above */
  112. static struct dentry *dma_debug_dent __read_mostly;
  113. static struct dentry *global_disable_dent __read_mostly;
  114. static struct dentry *error_count_dent __read_mostly;
  115. static struct dentry *show_all_errors_dent __read_mostly;
  116. static struct dentry *show_num_errors_dent __read_mostly;
  117. static struct dentry *num_free_entries_dent __read_mostly;
  118. static struct dentry *min_free_entries_dent __read_mostly;
  119. static struct dentry *filter_dent __read_mostly;
  120. /* per-driver filter related state */
  121. #define NAME_MAX_LEN 64
  122. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  123. static struct device_driver *current_driver __read_mostly;
  124. static DEFINE_RWLOCK(driver_name_lock);
  125. static const char *const maperr2str[] = {
  126. [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
  127. [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
  128. [MAP_ERR_CHECKED] = "dma map error checked",
  129. };
  130. static const char *type2name[5] = { "single", "page",
  131. "scather-gather", "coherent",
  132. "resource" };
  133. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  134. "DMA_FROM_DEVICE", "DMA_NONE" };
  135. /*
  136. * The access to some variables in this macro is racy. We can't use atomic_t
  137. * here because all these variables are exported to debugfs. Some of them even
  138. * writeable. This is also the reason why a lock won't help much. But anyway,
  139. * the races are no big deal. Here is why:
  140. *
  141. * error_count: the addition is racy, but the worst thing that can happen is
  142. * that we don't count some errors
  143. * show_num_errors: the subtraction is racy. Also no big deal because in
  144. * worst case this will result in one warning more in the
  145. * system log than the user configured. This variable is
  146. * writeable via debugfs.
  147. */
  148. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  149. {
  150. #ifdef CONFIG_STACKTRACE
  151. if (entry) {
  152. pr_warning("Mapped at:\n");
  153. print_stack_trace(&entry->stacktrace, 0);
  154. }
  155. #endif
  156. }
  157. static bool driver_filter(struct device *dev)
  158. {
  159. struct device_driver *drv;
  160. unsigned long flags;
  161. bool ret;
  162. /* driver filter off */
  163. if (likely(!current_driver_name[0]))
  164. return true;
  165. /* driver filter on and initialized */
  166. if (current_driver && dev && dev->driver == current_driver)
  167. return true;
  168. /* driver filter on, but we can't filter on a NULL device... */
  169. if (!dev)
  170. return false;
  171. if (current_driver || !current_driver_name[0])
  172. return false;
  173. /* driver filter on but not yet initialized */
  174. drv = dev->driver;
  175. if (!drv)
  176. return false;
  177. /* lock to protect against change of current_driver_name */
  178. read_lock_irqsave(&driver_name_lock, flags);
  179. ret = false;
  180. if (drv->name &&
  181. strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
  182. current_driver = drv;
  183. ret = true;
  184. }
  185. read_unlock_irqrestore(&driver_name_lock, flags);
  186. return ret;
  187. }
  188. #define err_printk(dev, entry, format, arg...) do { \
  189. error_count += 1; \
  190. if (driver_filter(dev) && \
  191. (show_all_errors || show_num_errors > 0)) { \
  192. WARN(1, "%s %s: " format, \
  193. dev ? dev_driver_string(dev) : "NULL", \
  194. dev ? dev_name(dev) : "NULL", ## arg); \
  195. dump_entry_trace(entry); \
  196. } \
  197. if (!show_all_errors && show_num_errors > 0) \
  198. show_num_errors -= 1; \
  199. } while (0);
  200. /*
  201. * Hash related functions
  202. *
  203. * Every DMA-API request is saved into a struct dma_debug_entry. To
  204. * have quick access to these structs they are stored into a hash.
  205. */
  206. static int hash_fn(struct dma_debug_entry *entry)
  207. {
  208. /*
  209. * Hash function is based on the dma address.
  210. * We use bits 20-27 here as the index into the hash
  211. */
  212. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  213. }
  214. /*
  215. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  216. */
  217. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  218. unsigned long *flags)
  219. __acquires(&dma_entry_hash[idx].lock)
  220. {
  221. int idx = hash_fn(entry);
  222. unsigned long __flags;
  223. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  224. *flags = __flags;
  225. return &dma_entry_hash[idx];
  226. }
  227. /*
  228. * Give up exclusive access to the hash bucket
  229. */
  230. static void put_hash_bucket(struct hash_bucket *bucket,
  231. unsigned long *flags)
  232. __releases(&bucket->lock)
  233. {
  234. unsigned long __flags = *flags;
  235. spin_unlock_irqrestore(&bucket->lock, __flags);
  236. }
  237. static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
  238. {
  239. return ((a->dev_addr == b->dev_addr) &&
  240. (a->dev == b->dev)) ? true : false;
  241. }
  242. static bool containing_match(struct dma_debug_entry *a,
  243. struct dma_debug_entry *b)
  244. {
  245. if (a->dev != b->dev)
  246. return false;
  247. if ((b->dev_addr <= a->dev_addr) &&
  248. ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
  249. return true;
  250. return false;
  251. }
  252. /*
  253. * Search a given entry in the hash bucket list
  254. */
  255. static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
  256. struct dma_debug_entry *ref,
  257. match_fn match)
  258. {
  259. struct dma_debug_entry *entry, *ret = NULL;
  260. int matches = 0, match_lvl, last_lvl = -1;
  261. list_for_each_entry(entry, &bucket->list, list) {
  262. if (!match(ref, entry))
  263. continue;
  264. /*
  265. * Some drivers map the same physical address multiple
  266. * times. Without a hardware IOMMU this results in the
  267. * same device addresses being put into the dma-debug
  268. * hash multiple times too. This can result in false
  269. * positives being reported. Therefore we implement a
  270. * best-fit algorithm here which returns the entry from
  271. * the hash which fits best to the reference value
  272. * instead of the first-fit.
  273. */
  274. matches += 1;
  275. match_lvl = 0;
  276. entry->size == ref->size ? ++match_lvl : 0;
  277. entry->type == ref->type ? ++match_lvl : 0;
  278. entry->direction == ref->direction ? ++match_lvl : 0;
  279. entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
  280. if (match_lvl == 4) {
  281. /* perfect-fit - return the result */
  282. return entry;
  283. } else if (match_lvl > last_lvl) {
  284. /*
  285. * We found an entry that fits better then the
  286. * previous one or it is the 1st match.
  287. */
  288. last_lvl = match_lvl;
  289. ret = entry;
  290. }
  291. }
  292. /*
  293. * If we have multiple matches but no perfect-fit, just return
  294. * NULL.
  295. */
  296. ret = (matches == 1) ? ret : NULL;
  297. return ret;
  298. }
  299. static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
  300. struct dma_debug_entry *ref)
  301. {
  302. return __hash_bucket_find(bucket, ref, exact_match);
  303. }
  304. static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
  305. struct dma_debug_entry *ref,
  306. unsigned long *flags)
  307. {
  308. unsigned int max_range = dma_get_max_seg_size(ref->dev);
  309. struct dma_debug_entry *entry, index = *ref;
  310. unsigned int range = 0;
  311. while (range <= max_range) {
  312. entry = __hash_bucket_find(*bucket, ref, containing_match);
  313. if (entry)
  314. return entry;
  315. /*
  316. * Nothing found, go back a hash bucket
  317. */
  318. put_hash_bucket(*bucket, flags);
  319. range += (1 << HASH_FN_SHIFT);
  320. index.dev_addr -= (1 << HASH_FN_SHIFT);
  321. *bucket = get_hash_bucket(&index, flags);
  322. }
  323. return NULL;
  324. }
  325. /*
  326. * Add an entry to a hash bucket
  327. */
  328. static void hash_bucket_add(struct hash_bucket *bucket,
  329. struct dma_debug_entry *entry)
  330. {
  331. list_add_tail(&entry->list, &bucket->list);
  332. }
  333. /*
  334. * Remove entry from a hash bucket list
  335. */
  336. static void hash_bucket_del(struct dma_debug_entry *entry)
  337. {
  338. list_del(&entry->list);
  339. }
  340. static unsigned long long phys_addr(struct dma_debug_entry *entry)
  341. {
  342. if (entry->type == dma_debug_resource)
  343. return __pfn_to_phys(entry->pfn) + entry->offset;
  344. return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
  345. }
  346. /*
  347. * Dump mapping entries for debugging purposes
  348. */
  349. void debug_dma_dump_mappings(struct device *dev)
  350. {
  351. int idx;
  352. for (idx = 0; idx < HASH_SIZE; idx++) {
  353. struct hash_bucket *bucket = &dma_entry_hash[idx];
  354. struct dma_debug_entry *entry;
  355. unsigned long flags;
  356. spin_lock_irqsave(&bucket->lock, flags);
  357. list_for_each_entry(entry, &bucket->list, list) {
  358. if (!dev || dev == entry->dev) {
  359. dev_info(entry->dev,
  360. "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
  361. type2name[entry->type], idx,
  362. phys_addr(entry), entry->pfn,
  363. entry->dev_addr, entry->size,
  364. dir2name[entry->direction],
  365. maperr2str[entry->map_err_type]);
  366. }
  367. }
  368. spin_unlock_irqrestore(&bucket->lock, flags);
  369. }
  370. }
  371. EXPORT_SYMBOL(debug_dma_dump_mappings);
  372. /*
  373. * For each mapping (initial cacheline in the case of
  374. * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
  375. * scatterlist, or the cacheline specified in dma_map_single) insert
  376. * into this tree using the cacheline as the key. At
  377. * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
  378. * the entry already exists at insertion time add a tag as a reference
  379. * count for the overlapping mappings. For now, the overlap tracking
  380. * just ensures that 'unmaps' balance 'maps' before marking the
  381. * cacheline idle, but we should also be flagging overlaps as an API
  382. * violation.
  383. *
  384. * Memory usage is mostly constrained by the maximum number of available
  385. * dma-debug entries in that we need a free dma_debug_entry before
  386. * inserting into the tree. In the case of dma_map_page and
  387. * dma_alloc_coherent there is only one dma_debug_entry and one
  388. * dma_active_cacheline entry to track per event. dma_map_sg(), on the
  389. * other hand, consumes a single dma_debug_entry, but inserts 'nents'
  390. * entries into the tree.
  391. *
  392. * At any time debug_dma_assert_idle() can be called to trigger a
  393. * warning if any cachelines in the given page are in the active set.
  394. */
  395. static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
  396. static DEFINE_SPINLOCK(radix_lock);
  397. #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
  398. #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
  399. #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
  400. static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
  401. {
  402. return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
  403. (entry->offset >> L1_CACHE_SHIFT);
  404. }
  405. static int active_cacheline_read_overlap(phys_addr_t cln)
  406. {
  407. int overlap = 0, i;
  408. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  409. if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
  410. overlap |= 1 << i;
  411. return overlap;
  412. }
  413. static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
  414. {
  415. int i;
  416. if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
  417. return overlap;
  418. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  419. if (overlap & 1 << i)
  420. radix_tree_tag_set(&dma_active_cacheline, cln, i);
  421. else
  422. radix_tree_tag_clear(&dma_active_cacheline, cln, i);
  423. return overlap;
  424. }
  425. static void active_cacheline_inc_overlap(phys_addr_t cln)
  426. {
  427. int overlap = active_cacheline_read_overlap(cln);
  428. overlap = active_cacheline_set_overlap(cln, ++overlap);
  429. /* If we overflowed the overlap counter then we're potentially
  430. * leaking dma-mappings. Otherwise, if maps and unmaps are
  431. * balanced then this overflow may cause false negatives in
  432. * debug_dma_assert_idle() as the cacheline may be marked idle
  433. * prematurely.
  434. */
  435. WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
  436. "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
  437. ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
  438. }
  439. static int active_cacheline_dec_overlap(phys_addr_t cln)
  440. {
  441. int overlap = active_cacheline_read_overlap(cln);
  442. return active_cacheline_set_overlap(cln, --overlap);
  443. }
  444. static int active_cacheline_insert(struct dma_debug_entry *entry)
  445. {
  446. phys_addr_t cln = to_cacheline_number(entry);
  447. unsigned long flags;
  448. int rc;
  449. /* If the device is not writing memory then we don't have any
  450. * concerns about the cpu consuming stale data. This mitigates
  451. * legitimate usages of overlapping mappings.
  452. */
  453. if (entry->direction == DMA_TO_DEVICE)
  454. return 0;
  455. spin_lock_irqsave(&radix_lock, flags);
  456. rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
  457. if (rc == -EEXIST)
  458. active_cacheline_inc_overlap(cln);
  459. spin_unlock_irqrestore(&radix_lock, flags);
  460. return rc;
  461. }
  462. static void active_cacheline_remove(struct dma_debug_entry *entry)
  463. {
  464. phys_addr_t cln = to_cacheline_number(entry);
  465. unsigned long flags;
  466. /* ...mirror the insert case */
  467. if (entry->direction == DMA_TO_DEVICE)
  468. return;
  469. spin_lock_irqsave(&radix_lock, flags);
  470. /* since we are counting overlaps the final put of the
  471. * cacheline will occur when the overlap count is 0.
  472. * active_cacheline_dec_overlap() returns -1 in that case
  473. */
  474. if (active_cacheline_dec_overlap(cln) < 0)
  475. radix_tree_delete(&dma_active_cacheline, cln);
  476. spin_unlock_irqrestore(&radix_lock, flags);
  477. }
  478. /**
  479. * debug_dma_assert_idle() - assert that a page is not undergoing dma
  480. * @page: page to lookup in the dma_active_cacheline tree
  481. *
  482. * Place a call to this routine in cases where the cpu touching the page
  483. * before the dma completes (page is dma_unmapped) will lead to data
  484. * corruption.
  485. */
  486. void debug_dma_assert_idle(struct page *page)
  487. {
  488. static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
  489. struct dma_debug_entry *entry = NULL;
  490. void **results = (void **) &ents;
  491. unsigned int nents, i;
  492. unsigned long flags;
  493. phys_addr_t cln;
  494. if (dma_debug_disabled())
  495. return;
  496. if (!page)
  497. return;
  498. cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
  499. spin_lock_irqsave(&radix_lock, flags);
  500. nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
  501. CACHELINES_PER_PAGE);
  502. for (i = 0; i < nents; i++) {
  503. phys_addr_t ent_cln = to_cacheline_number(ents[i]);
  504. if (ent_cln == cln) {
  505. entry = ents[i];
  506. break;
  507. } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
  508. break;
  509. }
  510. spin_unlock_irqrestore(&radix_lock, flags);
  511. if (!entry)
  512. return;
  513. cln = to_cacheline_number(entry);
  514. err_printk(entry->dev, entry,
  515. "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
  516. &cln);
  517. }
  518. /*
  519. * Wrapper function for adding an entry to the hash.
  520. * This function takes care of locking itself.
  521. */
  522. static void add_dma_entry(struct dma_debug_entry *entry)
  523. {
  524. struct hash_bucket *bucket;
  525. unsigned long flags;
  526. int rc;
  527. bucket = get_hash_bucket(entry, &flags);
  528. hash_bucket_add(bucket, entry);
  529. put_hash_bucket(bucket, &flags);
  530. rc = active_cacheline_insert(entry);
  531. if (rc == -ENOMEM) {
  532. pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
  533. global_disable = true;
  534. }
  535. /* TODO: report -EEXIST errors here as overlapping mappings are
  536. * not supported by the DMA API
  537. */
  538. }
  539. static struct dma_debug_entry *__dma_entry_alloc(void)
  540. {
  541. struct dma_debug_entry *entry;
  542. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  543. list_del(&entry->list);
  544. memset(entry, 0, sizeof(*entry));
  545. num_free_entries -= 1;
  546. if (num_free_entries < min_free_entries)
  547. min_free_entries = num_free_entries;
  548. return entry;
  549. }
  550. /* struct dma_entry allocator
  551. *
  552. * The next two functions implement the allocator for
  553. * struct dma_debug_entries.
  554. */
  555. static struct dma_debug_entry *dma_entry_alloc(void)
  556. {
  557. struct dma_debug_entry *entry;
  558. unsigned long flags;
  559. spin_lock_irqsave(&free_entries_lock, flags);
  560. if (list_empty(&free_entries)) {
  561. global_disable = true;
  562. spin_unlock_irqrestore(&free_entries_lock, flags);
  563. pr_err("DMA-API: debugging out of memory - disabling\n");
  564. return NULL;
  565. }
  566. entry = __dma_entry_alloc();
  567. spin_unlock_irqrestore(&free_entries_lock, flags);
  568. #ifdef CONFIG_STACKTRACE
  569. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  570. entry->stacktrace.entries = entry->st_entries;
  571. entry->stacktrace.skip = 2;
  572. save_stack_trace(&entry->stacktrace);
  573. #endif
  574. return entry;
  575. }
  576. static void dma_entry_free(struct dma_debug_entry *entry)
  577. {
  578. unsigned long flags;
  579. active_cacheline_remove(entry);
  580. /*
  581. * add to beginning of the list - this way the entries are
  582. * more likely cache hot when they are reallocated.
  583. */
  584. spin_lock_irqsave(&free_entries_lock, flags);
  585. list_add(&entry->list, &free_entries);
  586. num_free_entries += 1;
  587. spin_unlock_irqrestore(&free_entries_lock, flags);
  588. }
  589. int dma_debug_resize_entries(u32 num_entries)
  590. {
  591. int i, delta, ret = 0;
  592. unsigned long flags;
  593. struct dma_debug_entry *entry;
  594. LIST_HEAD(tmp);
  595. spin_lock_irqsave(&free_entries_lock, flags);
  596. if (nr_total_entries < num_entries) {
  597. delta = num_entries - nr_total_entries;
  598. spin_unlock_irqrestore(&free_entries_lock, flags);
  599. for (i = 0; i < delta; i++) {
  600. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  601. if (!entry)
  602. break;
  603. list_add_tail(&entry->list, &tmp);
  604. }
  605. spin_lock_irqsave(&free_entries_lock, flags);
  606. list_splice(&tmp, &free_entries);
  607. nr_total_entries += i;
  608. num_free_entries += i;
  609. } else {
  610. delta = nr_total_entries - num_entries;
  611. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  612. entry = __dma_entry_alloc();
  613. kfree(entry);
  614. }
  615. nr_total_entries -= i;
  616. }
  617. if (nr_total_entries != num_entries)
  618. ret = 1;
  619. spin_unlock_irqrestore(&free_entries_lock, flags);
  620. return ret;
  621. }
  622. EXPORT_SYMBOL(dma_debug_resize_entries);
  623. /*
  624. * DMA-API debugging init code
  625. *
  626. * The init code does two things:
  627. * 1. Initialize core data structures
  628. * 2. Preallocate a given number of dma_debug_entry structs
  629. */
  630. static int prealloc_memory(u32 num_entries)
  631. {
  632. struct dma_debug_entry *entry, *next_entry;
  633. int i;
  634. for (i = 0; i < num_entries; ++i) {
  635. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  636. if (!entry)
  637. goto out_err;
  638. list_add_tail(&entry->list, &free_entries);
  639. }
  640. num_free_entries = num_entries;
  641. min_free_entries = num_entries;
  642. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  643. return 0;
  644. out_err:
  645. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  646. list_del(&entry->list);
  647. kfree(entry);
  648. }
  649. return -ENOMEM;
  650. }
  651. static ssize_t filter_read(struct file *file, char __user *user_buf,
  652. size_t count, loff_t *ppos)
  653. {
  654. char buf[NAME_MAX_LEN + 1];
  655. unsigned long flags;
  656. int len;
  657. if (!current_driver_name[0])
  658. return 0;
  659. /*
  660. * We can't copy to userspace directly because current_driver_name can
  661. * only be read under the driver_name_lock with irqs disabled. So
  662. * create a temporary copy first.
  663. */
  664. read_lock_irqsave(&driver_name_lock, flags);
  665. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  666. read_unlock_irqrestore(&driver_name_lock, flags);
  667. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  668. }
  669. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  670. size_t count, loff_t *ppos)
  671. {
  672. char buf[NAME_MAX_LEN];
  673. unsigned long flags;
  674. size_t len;
  675. int i;
  676. /*
  677. * We can't copy from userspace directly. Access to
  678. * current_driver_name is protected with a write_lock with irqs
  679. * disabled. Since copy_from_user can fault and may sleep we
  680. * need to copy to temporary buffer first
  681. */
  682. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  683. if (copy_from_user(buf, userbuf, len))
  684. return -EFAULT;
  685. buf[len] = 0;
  686. write_lock_irqsave(&driver_name_lock, flags);
  687. /*
  688. * Now handle the string we got from userspace very carefully.
  689. * The rules are:
  690. * - only use the first token we got
  691. * - token delimiter is everything looking like a space
  692. * character (' ', '\n', '\t' ...)
  693. *
  694. */
  695. if (!isalnum(buf[0])) {
  696. /*
  697. * If the first character userspace gave us is not
  698. * alphanumerical then assume the filter should be
  699. * switched off.
  700. */
  701. if (current_driver_name[0])
  702. pr_info("DMA-API: switching off dma-debug driver filter\n");
  703. current_driver_name[0] = 0;
  704. current_driver = NULL;
  705. goto out_unlock;
  706. }
  707. /*
  708. * Now parse out the first token and use it as the name for the
  709. * driver to filter for.
  710. */
  711. for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
  712. current_driver_name[i] = buf[i];
  713. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  714. break;
  715. }
  716. current_driver_name[i] = 0;
  717. current_driver = NULL;
  718. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  719. current_driver_name);
  720. out_unlock:
  721. write_unlock_irqrestore(&driver_name_lock, flags);
  722. return count;
  723. }
  724. static const struct file_operations filter_fops = {
  725. .read = filter_read,
  726. .write = filter_write,
  727. .llseek = default_llseek,
  728. };
  729. static int dma_debug_fs_init(void)
  730. {
  731. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  732. if (!dma_debug_dent) {
  733. pr_err("DMA-API: can not create debugfs directory\n");
  734. return -ENOMEM;
  735. }
  736. global_disable_dent = debugfs_create_bool("disabled", 0444,
  737. dma_debug_dent,
  738. &global_disable);
  739. if (!global_disable_dent)
  740. goto out_err;
  741. error_count_dent = debugfs_create_u32("error_count", 0444,
  742. dma_debug_dent, &error_count);
  743. if (!error_count_dent)
  744. goto out_err;
  745. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  746. dma_debug_dent,
  747. &show_all_errors);
  748. if (!show_all_errors_dent)
  749. goto out_err;
  750. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  751. dma_debug_dent,
  752. &show_num_errors);
  753. if (!show_num_errors_dent)
  754. goto out_err;
  755. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  756. dma_debug_dent,
  757. &num_free_entries);
  758. if (!num_free_entries_dent)
  759. goto out_err;
  760. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  761. dma_debug_dent,
  762. &min_free_entries);
  763. if (!min_free_entries_dent)
  764. goto out_err;
  765. filter_dent = debugfs_create_file("driver_filter", 0644,
  766. dma_debug_dent, NULL, &filter_fops);
  767. if (!filter_dent)
  768. goto out_err;
  769. return 0;
  770. out_err:
  771. debugfs_remove_recursive(dma_debug_dent);
  772. return -ENOMEM;
  773. }
  774. static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
  775. {
  776. struct dma_debug_entry *entry;
  777. unsigned long flags;
  778. int count = 0, i;
  779. local_irq_save(flags);
  780. for (i = 0; i < HASH_SIZE; ++i) {
  781. spin_lock(&dma_entry_hash[i].lock);
  782. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  783. if (entry->dev == dev) {
  784. count += 1;
  785. *out_entry = entry;
  786. }
  787. }
  788. spin_unlock(&dma_entry_hash[i].lock);
  789. }
  790. local_irq_restore(flags);
  791. return count;
  792. }
  793. static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
  794. {
  795. struct device *dev = data;
  796. struct dma_debug_entry *uninitialized_var(entry);
  797. int count;
  798. if (dma_debug_disabled())
  799. return 0;
  800. switch (action) {
  801. case BUS_NOTIFY_UNBOUND_DRIVER:
  802. count = device_dma_allocations(dev, &entry);
  803. if (count == 0)
  804. break;
  805. err_printk(dev, entry, "DMA-API: device driver has pending "
  806. "DMA allocations while released from device "
  807. "[count=%d]\n"
  808. "One of leaked entries details: "
  809. "[device address=0x%016llx] [size=%llu bytes] "
  810. "[mapped with %s] [mapped as %s]\n",
  811. count, entry->dev_addr, entry->size,
  812. dir2name[entry->direction], type2name[entry->type]);
  813. break;
  814. default:
  815. break;
  816. }
  817. return 0;
  818. }
  819. void dma_debug_add_bus(struct bus_type *bus)
  820. {
  821. struct notifier_block *nb;
  822. if (dma_debug_disabled())
  823. return;
  824. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  825. if (nb == NULL) {
  826. pr_err("dma_debug_add_bus: out of memory\n");
  827. return;
  828. }
  829. nb->notifier_call = dma_debug_device_change;
  830. bus_register_notifier(bus, nb);
  831. }
  832. /*
  833. * Let the architectures decide how many entries should be preallocated.
  834. */
  835. void dma_debug_init(u32 num_entries)
  836. {
  837. int i;
  838. /* Do not use dma_debug_initialized here, since we really want to be
  839. * called to set dma_debug_initialized
  840. */
  841. if (global_disable)
  842. return;
  843. for (i = 0; i < HASH_SIZE; ++i) {
  844. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  845. spin_lock_init(&dma_entry_hash[i].lock);
  846. }
  847. if (dma_debug_fs_init() != 0) {
  848. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  849. global_disable = true;
  850. return;
  851. }
  852. if (req_entries)
  853. num_entries = req_entries;
  854. if (prealloc_memory(num_entries) != 0) {
  855. pr_err("DMA-API: debugging out of memory error - disabled\n");
  856. global_disable = true;
  857. return;
  858. }
  859. nr_total_entries = num_free_entries;
  860. dma_debug_initialized = true;
  861. pr_info("DMA-API: debugging enabled by kernel config\n");
  862. }
  863. static __init int dma_debug_cmdline(char *str)
  864. {
  865. if (!str)
  866. return -EINVAL;
  867. if (strncmp(str, "off", 3) == 0) {
  868. pr_info("DMA-API: debugging disabled on kernel command line\n");
  869. global_disable = true;
  870. }
  871. return 0;
  872. }
  873. static __init int dma_debug_entries_cmdline(char *str)
  874. {
  875. int res;
  876. if (!str)
  877. return -EINVAL;
  878. res = get_option(&str, &req_entries);
  879. if (!res)
  880. req_entries = 0;
  881. return 0;
  882. }
  883. __setup("dma_debug=", dma_debug_cmdline);
  884. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  885. static void check_unmap(struct dma_debug_entry *ref)
  886. {
  887. struct dma_debug_entry *entry;
  888. struct hash_bucket *bucket;
  889. unsigned long flags;
  890. bucket = get_hash_bucket(ref, &flags);
  891. entry = bucket_find_exact(bucket, ref);
  892. if (!entry) {
  893. /* must drop lock before calling dma_mapping_error */
  894. put_hash_bucket(bucket, &flags);
  895. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  896. err_printk(ref->dev, NULL,
  897. "DMA-API: device driver tries to free an "
  898. "invalid DMA memory address\n");
  899. } else {
  900. err_printk(ref->dev, NULL,
  901. "DMA-API: device driver tries to free DMA "
  902. "memory it has not allocated [device "
  903. "address=0x%016llx] [size=%llu bytes]\n",
  904. ref->dev_addr, ref->size);
  905. }
  906. return;
  907. }
  908. if (ref->size != entry->size) {
  909. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  910. "DMA memory with different size "
  911. "[device address=0x%016llx] [map size=%llu bytes] "
  912. "[unmap size=%llu bytes]\n",
  913. ref->dev_addr, entry->size, ref->size);
  914. }
  915. if (ref->type != entry->type) {
  916. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  917. "DMA memory with wrong function "
  918. "[device address=0x%016llx] [size=%llu bytes] "
  919. "[mapped as %s] [unmapped as %s]\n",
  920. ref->dev_addr, ref->size,
  921. type2name[entry->type], type2name[ref->type]);
  922. } else if ((entry->type == dma_debug_coherent) &&
  923. (phys_addr(ref) != phys_addr(entry))) {
  924. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  925. "DMA memory with different CPU address "
  926. "[device address=0x%016llx] [size=%llu bytes] "
  927. "[cpu alloc address=0x%016llx] "
  928. "[cpu free address=0x%016llx]",
  929. ref->dev_addr, ref->size,
  930. phys_addr(entry),
  931. phys_addr(ref));
  932. }
  933. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  934. ref->sg_call_ents != entry->sg_call_ents) {
  935. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  936. "DMA sg list with different entry count "
  937. "[map count=%d] [unmap count=%d]\n",
  938. entry->sg_call_ents, ref->sg_call_ents);
  939. }
  940. /*
  941. * This may be no bug in reality - but most implementations of the
  942. * DMA API don't handle this properly, so check for it here
  943. */
  944. if (ref->direction != entry->direction) {
  945. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  946. "DMA memory with different direction "
  947. "[device address=0x%016llx] [size=%llu bytes] "
  948. "[mapped with %s] [unmapped with %s]\n",
  949. ref->dev_addr, ref->size,
  950. dir2name[entry->direction],
  951. dir2name[ref->direction]);
  952. }
  953. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  954. err_printk(ref->dev, entry,
  955. "DMA-API: device driver failed to check map error"
  956. "[device address=0x%016llx] [size=%llu bytes] "
  957. "[mapped as %s]",
  958. ref->dev_addr, ref->size,
  959. type2name[entry->type]);
  960. }
  961. hash_bucket_del(entry);
  962. dma_entry_free(entry);
  963. put_hash_bucket(bucket, &flags);
  964. }
  965. static void check_for_stack(struct device *dev, void *addr)
  966. {
  967. if (object_is_on_stack(addr))
  968. err_printk(dev, NULL, "DMA-API: device driver maps memory from "
  969. "stack [addr=%p]\n", addr);
  970. }
  971. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  972. {
  973. unsigned long a1 = (unsigned long)addr;
  974. unsigned long b1 = a1 + len;
  975. unsigned long a2 = (unsigned long)start;
  976. unsigned long b2 = (unsigned long)end;
  977. return !(b1 <= a2 || a1 >= b2);
  978. }
  979. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  980. {
  981. if (overlap(addr, len, _stext, _etext) ||
  982. overlap(addr, len, __start_rodata, __end_rodata))
  983. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  984. }
  985. static void check_sync(struct device *dev,
  986. struct dma_debug_entry *ref,
  987. bool to_cpu)
  988. {
  989. struct dma_debug_entry *entry;
  990. struct hash_bucket *bucket;
  991. unsigned long flags;
  992. bucket = get_hash_bucket(ref, &flags);
  993. entry = bucket_find_contain(&bucket, ref, &flags);
  994. if (!entry) {
  995. err_printk(dev, NULL, "DMA-API: device driver tries "
  996. "to sync DMA memory it has not allocated "
  997. "[device address=0x%016llx] [size=%llu bytes]\n",
  998. (unsigned long long)ref->dev_addr, ref->size);
  999. goto out;
  1000. }
  1001. if (ref->size > entry->size) {
  1002. err_printk(dev, entry, "DMA-API: device driver syncs"
  1003. " DMA memory outside allocated range "
  1004. "[device address=0x%016llx] "
  1005. "[allocation size=%llu bytes] "
  1006. "[sync offset+size=%llu]\n",
  1007. entry->dev_addr, entry->size,
  1008. ref->size);
  1009. }
  1010. if (entry->direction == DMA_BIDIRECTIONAL)
  1011. goto out;
  1012. if (ref->direction != entry->direction) {
  1013. err_printk(dev, entry, "DMA-API: device driver syncs "
  1014. "DMA memory with different direction "
  1015. "[device address=0x%016llx] [size=%llu bytes] "
  1016. "[mapped with %s] [synced with %s]\n",
  1017. (unsigned long long)ref->dev_addr, entry->size,
  1018. dir2name[entry->direction],
  1019. dir2name[ref->direction]);
  1020. }
  1021. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  1022. !(ref->direction == DMA_TO_DEVICE))
  1023. err_printk(dev, entry, "DMA-API: device driver syncs "
  1024. "device read-only DMA memory for cpu "
  1025. "[device address=0x%016llx] [size=%llu bytes] "
  1026. "[mapped with %s] [synced with %s]\n",
  1027. (unsigned long long)ref->dev_addr, entry->size,
  1028. dir2name[entry->direction],
  1029. dir2name[ref->direction]);
  1030. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  1031. !(ref->direction == DMA_FROM_DEVICE))
  1032. err_printk(dev, entry, "DMA-API: device driver syncs "
  1033. "device write-only DMA memory to device "
  1034. "[device address=0x%016llx] [size=%llu bytes] "
  1035. "[mapped with %s] [synced with %s]\n",
  1036. (unsigned long long)ref->dev_addr, entry->size,
  1037. dir2name[entry->direction],
  1038. dir2name[ref->direction]);
  1039. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  1040. ref->sg_call_ents != entry->sg_call_ents) {
  1041. err_printk(ref->dev, entry, "DMA-API: device driver syncs "
  1042. "DMA sg list with different entry count "
  1043. "[map count=%d] [sync count=%d]\n",
  1044. entry->sg_call_ents, ref->sg_call_ents);
  1045. }
  1046. out:
  1047. put_hash_bucket(bucket, &flags);
  1048. }
  1049. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  1050. size_t size, int direction, dma_addr_t dma_addr,
  1051. bool map_single)
  1052. {
  1053. struct dma_debug_entry *entry;
  1054. if (unlikely(dma_debug_disabled()))
  1055. return;
  1056. if (dma_mapping_error(dev, dma_addr))
  1057. return;
  1058. entry = dma_entry_alloc();
  1059. if (!entry)
  1060. return;
  1061. entry->dev = dev;
  1062. entry->type = dma_debug_page;
  1063. entry->pfn = page_to_pfn(page);
  1064. entry->offset = offset,
  1065. entry->dev_addr = dma_addr;
  1066. entry->size = size;
  1067. entry->direction = direction;
  1068. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1069. if (map_single)
  1070. entry->type = dma_debug_single;
  1071. if (!PageHighMem(page)) {
  1072. void *addr = page_address(page) + offset;
  1073. check_for_stack(dev, addr);
  1074. check_for_illegal_area(dev, addr, size);
  1075. }
  1076. add_dma_entry(entry);
  1077. }
  1078. EXPORT_SYMBOL(debug_dma_map_page);
  1079. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  1080. {
  1081. struct dma_debug_entry ref;
  1082. struct dma_debug_entry *entry;
  1083. struct hash_bucket *bucket;
  1084. unsigned long flags;
  1085. if (unlikely(dma_debug_disabled()))
  1086. return;
  1087. ref.dev = dev;
  1088. ref.dev_addr = dma_addr;
  1089. bucket = get_hash_bucket(&ref, &flags);
  1090. list_for_each_entry(entry, &bucket->list, list) {
  1091. if (!exact_match(&ref, entry))
  1092. continue;
  1093. /*
  1094. * The same physical address can be mapped multiple
  1095. * times. Without a hardware IOMMU this results in the
  1096. * same device addresses being put into the dma-debug
  1097. * hash multiple times too. This can result in false
  1098. * positives being reported. Therefore we implement a
  1099. * best-fit algorithm here which updates the first entry
  1100. * from the hash which fits the reference value and is
  1101. * not currently listed as being checked.
  1102. */
  1103. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  1104. entry->map_err_type = MAP_ERR_CHECKED;
  1105. break;
  1106. }
  1107. }
  1108. put_hash_bucket(bucket, &flags);
  1109. }
  1110. EXPORT_SYMBOL(debug_dma_mapping_error);
  1111. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  1112. size_t size, int direction, bool map_single)
  1113. {
  1114. struct dma_debug_entry ref = {
  1115. .type = dma_debug_page,
  1116. .dev = dev,
  1117. .dev_addr = addr,
  1118. .size = size,
  1119. .direction = direction,
  1120. };
  1121. if (unlikely(dma_debug_disabled()))
  1122. return;
  1123. if (map_single)
  1124. ref.type = dma_debug_single;
  1125. check_unmap(&ref);
  1126. }
  1127. EXPORT_SYMBOL(debug_dma_unmap_page);
  1128. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  1129. int nents, int mapped_ents, int direction)
  1130. {
  1131. struct dma_debug_entry *entry;
  1132. struct scatterlist *s;
  1133. int i;
  1134. if (unlikely(dma_debug_disabled()))
  1135. return;
  1136. for_each_sg(sg, s, mapped_ents, i) {
  1137. entry = dma_entry_alloc();
  1138. if (!entry)
  1139. return;
  1140. entry->type = dma_debug_sg;
  1141. entry->dev = dev;
  1142. entry->pfn = page_to_pfn(sg_page(s));
  1143. entry->offset = s->offset,
  1144. entry->size = sg_dma_len(s);
  1145. entry->dev_addr = sg_dma_address(s);
  1146. entry->direction = direction;
  1147. entry->sg_call_ents = nents;
  1148. entry->sg_mapped_ents = mapped_ents;
  1149. if (!PageHighMem(sg_page(s))) {
  1150. check_for_stack(dev, sg_virt(s));
  1151. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  1152. }
  1153. add_dma_entry(entry);
  1154. }
  1155. }
  1156. EXPORT_SYMBOL(debug_dma_map_sg);
  1157. static int get_nr_mapped_entries(struct device *dev,
  1158. struct dma_debug_entry *ref)
  1159. {
  1160. struct dma_debug_entry *entry;
  1161. struct hash_bucket *bucket;
  1162. unsigned long flags;
  1163. int mapped_ents;
  1164. bucket = get_hash_bucket(ref, &flags);
  1165. entry = bucket_find_exact(bucket, ref);
  1166. mapped_ents = 0;
  1167. if (entry)
  1168. mapped_ents = entry->sg_mapped_ents;
  1169. put_hash_bucket(bucket, &flags);
  1170. return mapped_ents;
  1171. }
  1172. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  1173. int nelems, int dir)
  1174. {
  1175. struct scatterlist *s;
  1176. int mapped_ents = 0, i;
  1177. if (unlikely(dma_debug_disabled()))
  1178. return;
  1179. for_each_sg(sglist, s, nelems, i) {
  1180. struct dma_debug_entry ref = {
  1181. .type = dma_debug_sg,
  1182. .dev = dev,
  1183. .pfn = page_to_pfn(sg_page(s)),
  1184. .offset = s->offset,
  1185. .dev_addr = sg_dma_address(s),
  1186. .size = sg_dma_len(s),
  1187. .direction = dir,
  1188. .sg_call_ents = nelems,
  1189. };
  1190. if (mapped_ents && i >= mapped_ents)
  1191. break;
  1192. if (!i)
  1193. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1194. check_unmap(&ref);
  1195. }
  1196. }
  1197. EXPORT_SYMBOL(debug_dma_unmap_sg);
  1198. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  1199. dma_addr_t dma_addr, void *virt)
  1200. {
  1201. struct dma_debug_entry *entry;
  1202. if (unlikely(dma_debug_disabled()))
  1203. return;
  1204. if (unlikely(virt == NULL))
  1205. return;
  1206. entry = dma_entry_alloc();
  1207. if (!entry)
  1208. return;
  1209. entry->type = dma_debug_coherent;
  1210. entry->dev = dev;
  1211. entry->pfn = page_to_pfn(virt_to_page(virt));
  1212. entry->offset = (size_t) virt & ~PAGE_MASK;
  1213. entry->size = size;
  1214. entry->dev_addr = dma_addr;
  1215. entry->direction = DMA_BIDIRECTIONAL;
  1216. add_dma_entry(entry);
  1217. }
  1218. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1219. void debug_dma_free_coherent(struct device *dev, size_t size,
  1220. void *virt, dma_addr_t addr)
  1221. {
  1222. struct dma_debug_entry ref = {
  1223. .type = dma_debug_coherent,
  1224. .dev = dev,
  1225. .pfn = page_to_pfn(virt_to_page(virt)),
  1226. .offset = (size_t) virt & ~PAGE_MASK,
  1227. .dev_addr = addr,
  1228. .size = size,
  1229. .direction = DMA_BIDIRECTIONAL,
  1230. };
  1231. if (unlikely(dma_debug_disabled()))
  1232. return;
  1233. check_unmap(&ref);
  1234. }
  1235. EXPORT_SYMBOL(debug_dma_free_coherent);
  1236. void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
  1237. int direction, dma_addr_t dma_addr)
  1238. {
  1239. struct dma_debug_entry *entry;
  1240. if (unlikely(dma_debug_disabled()))
  1241. return;
  1242. entry = dma_entry_alloc();
  1243. if (!entry)
  1244. return;
  1245. entry->type = dma_debug_resource;
  1246. entry->dev = dev;
  1247. entry->pfn = __phys_to_pfn(addr);
  1248. entry->offset = offset_in_page(addr);
  1249. entry->size = size;
  1250. entry->dev_addr = dma_addr;
  1251. entry->direction = direction;
  1252. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1253. add_dma_entry(entry);
  1254. }
  1255. EXPORT_SYMBOL(debug_dma_map_resource);
  1256. void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
  1257. size_t size, int direction)
  1258. {
  1259. struct dma_debug_entry ref = {
  1260. .type = dma_debug_resource,
  1261. .dev = dev,
  1262. .dev_addr = dma_addr,
  1263. .size = size,
  1264. .direction = direction,
  1265. };
  1266. if (unlikely(dma_debug_disabled()))
  1267. return;
  1268. check_unmap(&ref);
  1269. }
  1270. EXPORT_SYMBOL(debug_dma_unmap_resource);
  1271. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1272. size_t size, int direction)
  1273. {
  1274. struct dma_debug_entry ref;
  1275. if (unlikely(dma_debug_disabled()))
  1276. return;
  1277. ref.type = dma_debug_single;
  1278. ref.dev = dev;
  1279. ref.dev_addr = dma_handle;
  1280. ref.size = size;
  1281. ref.direction = direction;
  1282. ref.sg_call_ents = 0;
  1283. check_sync(dev, &ref, true);
  1284. }
  1285. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1286. void debug_dma_sync_single_for_device(struct device *dev,
  1287. dma_addr_t dma_handle, size_t size,
  1288. int direction)
  1289. {
  1290. struct dma_debug_entry ref;
  1291. if (unlikely(dma_debug_disabled()))
  1292. return;
  1293. ref.type = dma_debug_single;
  1294. ref.dev = dev;
  1295. ref.dev_addr = dma_handle;
  1296. ref.size = size;
  1297. ref.direction = direction;
  1298. ref.sg_call_ents = 0;
  1299. check_sync(dev, &ref, false);
  1300. }
  1301. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1302. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1303. dma_addr_t dma_handle,
  1304. unsigned long offset, size_t size,
  1305. int direction)
  1306. {
  1307. struct dma_debug_entry ref;
  1308. if (unlikely(dma_debug_disabled()))
  1309. return;
  1310. ref.type = dma_debug_single;
  1311. ref.dev = dev;
  1312. ref.dev_addr = dma_handle;
  1313. ref.size = offset + size;
  1314. ref.direction = direction;
  1315. ref.sg_call_ents = 0;
  1316. check_sync(dev, &ref, true);
  1317. }
  1318. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1319. void debug_dma_sync_single_range_for_device(struct device *dev,
  1320. dma_addr_t dma_handle,
  1321. unsigned long offset,
  1322. size_t size, int direction)
  1323. {
  1324. struct dma_debug_entry ref;
  1325. if (unlikely(dma_debug_disabled()))
  1326. return;
  1327. ref.type = dma_debug_single;
  1328. ref.dev = dev;
  1329. ref.dev_addr = dma_handle;
  1330. ref.size = offset + size;
  1331. ref.direction = direction;
  1332. ref.sg_call_ents = 0;
  1333. check_sync(dev, &ref, false);
  1334. }
  1335. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1336. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1337. int nelems, int direction)
  1338. {
  1339. struct scatterlist *s;
  1340. int mapped_ents = 0, i;
  1341. if (unlikely(dma_debug_disabled()))
  1342. return;
  1343. for_each_sg(sg, s, nelems, i) {
  1344. struct dma_debug_entry ref = {
  1345. .type = dma_debug_sg,
  1346. .dev = dev,
  1347. .pfn = page_to_pfn(sg_page(s)),
  1348. .offset = s->offset,
  1349. .dev_addr = sg_dma_address(s),
  1350. .size = sg_dma_len(s),
  1351. .direction = direction,
  1352. .sg_call_ents = nelems,
  1353. };
  1354. if (!i)
  1355. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1356. if (i >= mapped_ents)
  1357. break;
  1358. check_sync(dev, &ref, true);
  1359. }
  1360. }
  1361. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1362. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1363. int nelems, int direction)
  1364. {
  1365. struct scatterlist *s;
  1366. int mapped_ents = 0, i;
  1367. if (unlikely(dma_debug_disabled()))
  1368. return;
  1369. for_each_sg(sg, s, nelems, i) {
  1370. struct dma_debug_entry ref = {
  1371. .type = dma_debug_sg,
  1372. .dev = dev,
  1373. .pfn = page_to_pfn(sg_page(s)),
  1374. .offset = s->offset,
  1375. .dev_addr = sg_dma_address(s),
  1376. .size = sg_dma_len(s),
  1377. .direction = direction,
  1378. .sg_call_ents = nelems,
  1379. };
  1380. if (!i)
  1381. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1382. if (i >= mapped_ents)
  1383. break;
  1384. check_sync(dev, &ref, false);
  1385. }
  1386. }
  1387. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1388. static int __init dma_debug_driver_setup(char *str)
  1389. {
  1390. int i;
  1391. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1392. current_driver_name[i] = *str;
  1393. if (*str == 0)
  1394. break;
  1395. }
  1396. if (current_driver_name[0])
  1397. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1398. current_driver_name);
  1399. return 1;
  1400. }
  1401. __setup("dma_debug_driver=", dma_debug_driver_setup);