dma-mapping.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. #ifndef _LINUX_DMA_MAPPING_H
  2. #define _LINUX_DMA_MAPPING_H
  3. #include <linux/sizes.h>
  4. #include <linux/string.h>
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/dma-debug.h>
  8. #include <linux/dma-direction.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/kmemcheck.h>
  11. #include <linux/bug.h>
  12. /**
  13. * List of possible attributes associated with a DMA mapping. The semantics
  14. * of each attribute should be defined in Documentation/DMA-attributes.txt.
  15. *
  16. * DMA_ATTR_WRITE_BARRIER: DMA to a memory region with this attribute
  17. * forces all pending DMA writes to complete.
  18. */
  19. #define DMA_ATTR_WRITE_BARRIER (1UL << 0)
  20. /*
  21. * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping
  22. * may be weakly ordered, that is that reads and writes may pass each other.
  23. */
  24. #define DMA_ATTR_WEAK_ORDERING (1UL << 1)
  25. /*
  26. * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be
  27. * buffered to improve performance.
  28. */
  29. #define DMA_ATTR_WRITE_COMBINE (1UL << 2)
  30. /*
  31. * DMA_ATTR_NON_CONSISTENT: Lets the platform to choose to return either
  32. * consistent or non-consistent memory as it sees fit.
  33. */
  34. #define DMA_ATTR_NON_CONSISTENT (1UL << 3)
  35. /*
  36. * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel
  37. * virtual mapping for the allocated buffer.
  38. */
  39. #define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
  40. /*
  41. * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of
  42. * the CPU cache for the given buffer assuming that it has been already
  43. * transferred to 'device' domain.
  44. */
  45. #define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
  46. /*
  47. * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer
  48. * in physical memory.
  49. */
  50. #define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
  51. /*
  52. * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem
  53. * that it's probably not worth the time to try to allocate memory to in a way
  54. * that gives better TLB efficiency.
  55. */
  56. #define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
  57. /*
  58. * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress
  59. * allocation failure reports (similarly to __GFP_NOWARN).
  60. */
  61. #define DMA_ATTR_NO_WARN (1UL << 8)
  62. /*
  63. * A dma_addr_t can hold any valid DMA or bus address for the platform.
  64. * It can be given to a device to use as a DMA source or target. A CPU cannot
  65. * reference a dma_addr_t directly because there may be translation between
  66. * its physical address space and the bus address space.
  67. */
  68. struct dma_map_ops {
  69. void* (*alloc)(struct device *dev, size_t size,
  70. dma_addr_t *dma_handle, gfp_t gfp,
  71. unsigned long attrs);
  72. void (*free)(struct device *dev, size_t size,
  73. void *vaddr, dma_addr_t dma_handle,
  74. unsigned long attrs);
  75. int (*mmap)(struct device *, struct vm_area_struct *,
  76. void *, dma_addr_t, size_t,
  77. unsigned long attrs);
  78. int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
  79. dma_addr_t, size_t, unsigned long attrs);
  80. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  81. unsigned long offset, size_t size,
  82. enum dma_data_direction dir,
  83. unsigned long attrs);
  84. void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
  85. size_t size, enum dma_data_direction dir,
  86. unsigned long attrs);
  87. /*
  88. * map_sg returns 0 on error and a value > 0 on success.
  89. * It should never return a value < 0.
  90. */
  91. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  92. int nents, enum dma_data_direction dir,
  93. unsigned long attrs);
  94. void (*unmap_sg)(struct device *dev,
  95. struct scatterlist *sg, int nents,
  96. enum dma_data_direction dir,
  97. unsigned long attrs);
  98. dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
  99. size_t size, enum dma_data_direction dir,
  100. unsigned long attrs);
  101. void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
  102. size_t size, enum dma_data_direction dir,
  103. unsigned long attrs);
  104. void (*sync_single_for_cpu)(struct device *dev,
  105. dma_addr_t dma_handle, size_t size,
  106. enum dma_data_direction dir);
  107. void (*sync_single_for_device)(struct device *dev,
  108. dma_addr_t dma_handle, size_t size,
  109. enum dma_data_direction dir);
  110. void (*sync_sg_for_cpu)(struct device *dev,
  111. struct scatterlist *sg, int nents,
  112. enum dma_data_direction dir);
  113. void (*sync_sg_for_device)(struct device *dev,
  114. struct scatterlist *sg, int nents,
  115. enum dma_data_direction dir);
  116. int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
  117. int (*dma_supported)(struct device *dev, u64 mask);
  118. int (*set_dma_mask)(struct device *dev, u64 mask);
  119. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  120. u64 (*get_required_mask)(struct device *dev);
  121. #endif
  122. int is_phys;
  123. };
  124. extern struct dma_map_ops dma_noop_ops;
  125. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
  126. #define DMA_MASK_NONE 0x0ULL
  127. static inline int valid_dma_direction(int dma_direction)
  128. {
  129. return ((dma_direction == DMA_BIDIRECTIONAL) ||
  130. (dma_direction == DMA_TO_DEVICE) ||
  131. (dma_direction == DMA_FROM_DEVICE));
  132. }
  133. static inline int is_device_dma_capable(struct device *dev)
  134. {
  135. return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
  136. }
  137. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  138. /*
  139. * These three functions are only for dma allocator.
  140. * Don't use them in device drivers.
  141. */
  142. int dma_alloc_from_coherent(struct device *dev, ssize_t size,
  143. dma_addr_t *dma_handle, void **ret);
  144. int dma_release_from_coherent(struct device *dev, int order, void *vaddr);
  145. int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
  146. void *cpu_addr, size_t size, int *ret);
  147. #else
  148. #define dma_alloc_from_coherent(dev, size, handle, ret) (0)
  149. #define dma_release_from_coherent(dev, order, vaddr) (0)
  150. #define dma_mmap_from_coherent(dev, vma, vaddr, order, ret) (0)
  151. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  152. #ifdef CONFIG_HAS_DMA
  153. #include <asm/dma-mapping.h>
  154. #else
  155. /*
  156. * Define the dma api to allow compilation but not linking of
  157. * dma dependent code. Code that depends on the dma-mapping
  158. * API needs to set 'depends on HAS_DMA' in its Kconfig
  159. */
  160. extern struct dma_map_ops bad_dma_ops;
  161. static inline struct dma_map_ops *get_dma_ops(struct device *dev)
  162. {
  163. return &bad_dma_ops;
  164. }
  165. #endif
  166. static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  167. size_t size,
  168. enum dma_data_direction dir,
  169. unsigned long attrs)
  170. {
  171. struct dma_map_ops *ops = get_dma_ops(dev);
  172. dma_addr_t addr;
  173. kmemcheck_mark_initialized(ptr, size);
  174. BUG_ON(!valid_dma_direction(dir));
  175. addr = ops->map_page(dev, virt_to_page(ptr),
  176. offset_in_page(ptr), size,
  177. dir, attrs);
  178. debug_dma_map_page(dev, virt_to_page(ptr),
  179. offset_in_page(ptr), size,
  180. dir, addr, true);
  181. return addr;
  182. }
  183. static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
  184. size_t size,
  185. enum dma_data_direction dir,
  186. unsigned long attrs)
  187. {
  188. struct dma_map_ops *ops = get_dma_ops(dev);
  189. BUG_ON(!valid_dma_direction(dir));
  190. if (ops->unmap_page)
  191. ops->unmap_page(dev, addr, size, dir, attrs);
  192. debug_dma_unmap_page(dev, addr, size, dir, true);
  193. }
  194. /*
  195. * dma_maps_sg_attrs returns 0 on error and > 0 on success.
  196. * It should never return a value < 0.
  197. */
  198. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  199. int nents, enum dma_data_direction dir,
  200. unsigned long attrs)
  201. {
  202. struct dma_map_ops *ops = get_dma_ops(dev);
  203. int i, ents;
  204. struct scatterlist *s;
  205. for_each_sg(sg, s, nents, i)
  206. kmemcheck_mark_initialized(sg_virt(s), s->length);
  207. BUG_ON(!valid_dma_direction(dir));
  208. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  209. BUG_ON(ents < 0);
  210. debug_dma_map_sg(dev, sg, nents, ents, dir);
  211. return ents;
  212. }
  213. static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  214. int nents, enum dma_data_direction dir,
  215. unsigned long attrs)
  216. {
  217. struct dma_map_ops *ops = get_dma_ops(dev);
  218. BUG_ON(!valid_dma_direction(dir));
  219. debug_dma_unmap_sg(dev, sg, nents, dir);
  220. if (ops->unmap_sg)
  221. ops->unmap_sg(dev, sg, nents, dir, attrs);
  222. }
  223. static inline dma_addr_t dma_map_page_attrs(struct device *dev,
  224. struct page *page,
  225. size_t offset, size_t size,
  226. enum dma_data_direction dir,
  227. unsigned long attrs)
  228. {
  229. struct dma_map_ops *ops = get_dma_ops(dev);
  230. dma_addr_t addr;
  231. kmemcheck_mark_initialized(page_address(page) + offset, size);
  232. BUG_ON(!valid_dma_direction(dir));
  233. addr = ops->map_page(dev, page, offset, size, dir, attrs);
  234. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  235. return addr;
  236. }
  237. static inline void dma_unmap_page_attrs(struct device *dev,
  238. dma_addr_t addr, size_t size,
  239. enum dma_data_direction dir,
  240. unsigned long attrs)
  241. {
  242. struct dma_map_ops *ops = get_dma_ops(dev);
  243. BUG_ON(!valid_dma_direction(dir));
  244. if (ops->unmap_page)
  245. ops->unmap_page(dev, addr, size, dir, attrs);
  246. debug_dma_unmap_page(dev, addr, size, dir, false);
  247. }
  248. static inline dma_addr_t dma_map_resource(struct device *dev,
  249. phys_addr_t phys_addr,
  250. size_t size,
  251. enum dma_data_direction dir,
  252. unsigned long attrs)
  253. {
  254. struct dma_map_ops *ops = get_dma_ops(dev);
  255. dma_addr_t addr;
  256. BUG_ON(!valid_dma_direction(dir));
  257. /* Don't allow RAM to be mapped */
  258. BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
  259. addr = phys_addr;
  260. if (ops->map_resource)
  261. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  262. debug_dma_map_resource(dev, phys_addr, size, dir, addr);
  263. return addr;
  264. }
  265. static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
  266. size_t size, enum dma_data_direction dir,
  267. unsigned long attrs)
  268. {
  269. struct dma_map_ops *ops = get_dma_ops(dev);
  270. BUG_ON(!valid_dma_direction(dir));
  271. if (ops->unmap_resource)
  272. ops->unmap_resource(dev, addr, size, dir, attrs);
  273. debug_dma_unmap_resource(dev, addr, size, dir);
  274. }
  275. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  276. size_t size,
  277. enum dma_data_direction dir)
  278. {
  279. struct dma_map_ops *ops = get_dma_ops(dev);
  280. BUG_ON(!valid_dma_direction(dir));
  281. if (ops->sync_single_for_cpu)
  282. ops->sync_single_for_cpu(dev, addr, size, dir);
  283. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  284. }
  285. static inline void dma_sync_single_for_device(struct device *dev,
  286. dma_addr_t addr, size_t size,
  287. enum dma_data_direction dir)
  288. {
  289. struct dma_map_ops *ops = get_dma_ops(dev);
  290. BUG_ON(!valid_dma_direction(dir));
  291. if (ops->sync_single_for_device)
  292. ops->sync_single_for_device(dev, addr, size, dir);
  293. debug_dma_sync_single_for_device(dev, addr, size, dir);
  294. }
  295. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  296. dma_addr_t addr,
  297. unsigned long offset,
  298. size_t size,
  299. enum dma_data_direction dir)
  300. {
  301. const struct dma_map_ops *ops = get_dma_ops(dev);
  302. BUG_ON(!valid_dma_direction(dir));
  303. if (ops->sync_single_for_cpu)
  304. ops->sync_single_for_cpu(dev, addr + offset, size, dir);
  305. debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  306. }
  307. static inline void dma_sync_single_range_for_device(struct device *dev,
  308. dma_addr_t addr,
  309. unsigned long offset,
  310. size_t size,
  311. enum dma_data_direction dir)
  312. {
  313. const struct dma_map_ops *ops = get_dma_ops(dev);
  314. BUG_ON(!valid_dma_direction(dir));
  315. if (ops->sync_single_for_device)
  316. ops->sync_single_for_device(dev, addr + offset, size, dir);
  317. debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  318. }
  319. static inline void
  320. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  321. int nelems, enum dma_data_direction dir)
  322. {
  323. struct dma_map_ops *ops = get_dma_ops(dev);
  324. BUG_ON(!valid_dma_direction(dir));
  325. if (ops->sync_sg_for_cpu)
  326. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  327. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  328. }
  329. static inline void
  330. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  331. int nelems, enum dma_data_direction dir)
  332. {
  333. struct dma_map_ops *ops = get_dma_ops(dev);
  334. BUG_ON(!valid_dma_direction(dir));
  335. if (ops->sync_sg_for_device)
  336. ops->sync_sg_for_device(dev, sg, nelems, dir);
  337. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  338. }
  339. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
  340. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
  341. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
  342. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
  343. #define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
  344. #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
  345. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  346. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  347. void *dma_common_contiguous_remap(struct page *page, size_t size,
  348. unsigned long vm_flags,
  349. pgprot_t prot, const void *caller);
  350. void *dma_common_pages_remap(struct page **pages, size_t size,
  351. unsigned long vm_flags, pgprot_t prot,
  352. const void *caller);
  353. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
  354. /**
  355. * dma_mmap_attrs - map a coherent DMA allocation into user space
  356. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  357. * @vma: vm_area_struct describing requested user mapping
  358. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  359. * @handle: device-view address returned from dma_alloc_attrs
  360. * @size: size of memory originally requested in dma_alloc_attrs
  361. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  362. *
  363. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
  364. * into user space. The coherent DMA buffer must not be freed by the
  365. * driver until the user space mapping has been released.
  366. */
  367. static inline int
  368. dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
  369. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  370. {
  371. struct dma_map_ops *ops = get_dma_ops(dev);
  372. BUG_ON(!ops);
  373. if (ops->mmap)
  374. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  375. return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  376. }
  377. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
  378. int
  379. dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  380. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  381. static inline int
  382. dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
  383. dma_addr_t dma_addr, size_t size,
  384. unsigned long attrs)
  385. {
  386. struct dma_map_ops *ops = get_dma_ops(dev);
  387. BUG_ON(!ops);
  388. if (ops->get_sgtable)
  389. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
  390. attrs);
  391. return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  392. }
  393. #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
  394. #ifndef arch_dma_alloc_attrs
  395. #define arch_dma_alloc_attrs(dev, flag) (true)
  396. #endif
  397. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  398. dma_addr_t *dma_handle, gfp_t flag,
  399. unsigned long attrs)
  400. {
  401. struct dma_map_ops *ops = get_dma_ops(dev);
  402. void *cpu_addr;
  403. BUG_ON(!ops);
  404. if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr))
  405. return cpu_addr;
  406. if (!arch_dma_alloc_attrs(&dev, &flag))
  407. return NULL;
  408. if (!ops->alloc)
  409. return NULL;
  410. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  411. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  412. return cpu_addr;
  413. }
  414. static inline void dma_free_attrs(struct device *dev, size_t size,
  415. void *cpu_addr, dma_addr_t dma_handle,
  416. unsigned long attrs)
  417. {
  418. struct dma_map_ops *ops = get_dma_ops(dev);
  419. BUG_ON(!ops);
  420. WARN_ON(irqs_disabled());
  421. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  422. return;
  423. if (!ops->free || !cpu_addr)
  424. return;
  425. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  426. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  427. }
  428. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  429. dma_addr_t *dma_handle, gfp_t flag)
  430. {
  431. return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
  432. }
  433. static inline void dma_free_coherent(struct device *dev, size_t size,
  434. void *cpu_addr, dma_addr_t dma_handle)
  435. {
  436. return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
  437. }
  438. static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
  439. dma_addr_t *dma_handle, gfp_t gfp)
  440. {
  441. return dma_alloc_attrs(dev, size, dma_handle, gfp,
  442. DMA_ATTR_NON_CONSISTENT);
  443. }
  444. static inline void dma_free_noncoherent(struct device *dev, size_t size,
  445. void *cpu_addr, dma_addr_t dma_handle)
  446. {
  447. dma_free_attrs(dev, size, cpu_addr, dma_handle,
  448. DMA_ATTR_NON_CONSISTENT);
  449. }
  450. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  451. {
  452. debug_dma_mapping_error(dev, dma_addr);
  453. if (get_dma_ops(dev)->mapping_error)
  454. return get_dma_ops(dev)->mapping_error(dev, dma_addr);
  455. #ifdef DMA_ERROR_CODE
  456. return dma_addr == DMA_ERROR_CODE;
  457. #else
  458. return 0;
  459. #endif
  460. }
  461. #ifndef HAVE_ARCH_DMA_SUPPORTED
  462. static inline int dma_supported(struct device *dev, u64 mask)
  463. {
  464. struct dma_map_ops *ops = get_dma_ops(dev);
  465. if (!ops)
  466. return 0;
  467. if (!ops->dma_supported)
  468. return 1;
  469. return ops->dma_supported(dev, mask);
  470. }
  471. #endif
  472. #ifndef HAVE_ARCH_DMA_SET_MASK
  473. static inline int dma_set_mask(struct device *dev, u64 mask)
  474. {
  475. struct dma_map_ops *ops = get_dma_ops(dev);
  476. if (ops->set_dma_mask)
  477. return ops->set_dma_mask(dev, mask);
  478. if (!dev->dma_mask || !dma_supported(dev, mask))
  479. return -EIO;
  480. *dev->dma_mask = mask;
  481. return 0;
  482. }
  483. #endif
  484. static inline u64 dma_get_mask(struct device *dev)
  485. {
  486. if (dev && dev->dma_mask && *dev->dma_mask)
  487. return *dev->dma_mask;
  488. return DMA_BIT_MASK(32);
  489. }
  490. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  491. int dma_set_coherent_mask(struct device *dev, u64 mask);
  492. #else
  493. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  494. {
  495. if (!dma_supported(dev, mask))
  496. return -EIO;
  497. dev->coherent_dma_mask = mask;
  498. return 0;
  499. }
  500. #endif
  501. /*
  502. * Set both the DMA mask and the coherent DMA mask to the same thing.
  503. * Note that we don't check the return value from dma_set_coherent_mask()
  504. * as the DMA API guarantees that the coherent DMA mask can be set to
  505. * the same or smaller than the streaming DMA mask.
  506. */
  507. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  508. {
  509. int rc = dma_set_mask(dev, mask);
  510. if (rc == 0)
  511. dma_set_coherent_mask(dev, mask);
  512. return rc;
  513. }
  514. /*
  515. * Similar to the above, except it deals with the case where the device
  516. * does not have dev->dma_mask appropriately setup.
  517. */
  518. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  519. {
  520. dev->dma_mask = &dev->coherent_dma_mask;
  521. return dma_set_mask_and_coherent(dev, mask);
  522. }
  523. extern u64 dma_get_required_mask(struct device *dev);
  524. #ifndef arch_setup_dma_ops
  525. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  526. u64 size, const struct iommu_ops *iommu,
  527. bool coherent) { }
  528. #endif
  529. #ifndef arch_teardown_dma_ops
  530. static inline void arch_teardown_dma_ops(struct device *dev) { }
  531. #endif
  532. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  533. {
  534. if (dev->dma_parms && dev->dma_parms->max_segment_size)
  535. return dev->dma_parms->max_segment_size;
  536. return SZ_64K;
  537. }
  538. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  539. unsigned int size)
  540. {
  541. if (dev->dma_parms) {
  542. dev->dma_parms->max_segment_size = size;
  543. return 0;
  544. }
  545. return -EIO;
  546. }
  547. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  548. {
  549. if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
  550. return dev->dma_parms->segment_boundary_mask;
  551. return DMA_BIT_MASK(32);
  552. }
  553. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  554. {
  555. if (dev->dma_parms) {
  556. dev->dma_parms->segment_boundary_mask = mask;
  557. return 0;
  558. }
  559. return -EIO;
  560. }
  561. #ifndef dma_max_pfn
  562. static inline unsigned long dma_max_pfn(struct device *dev)
  563. {
  564. return *dev->dma_mask >> PAGE_SHIFT;
  565. }
  566. #endif
  567. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  568. dma_addr_t *dma_handle, gfp_t flag)
  569. {
  570. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  571. flag | __GFP_ZERO);
  572. return ret;
  573. }
  574. #ifdef CONFIG_HAS_DMA
  575. static inline int dma_get_cache_alignment(void)
  576. {
  577. #ifdef ARCH_DMA_MINALIGN
  578. return ARCH_DMA_MINALIGN;
  579. #endif
  580. return 1;
  581. }
  582. #endif
  583. /* flags for the coherent memory api */
  584. #define DMA_MEMORY_MAP 0x01
  585. #define DMA_MEMORY_IO 0x02
  586. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  587. #define DMA_MEMORY_EXCLUSIVE 0x08
  588. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  589. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  590. dma_addr_t device_addr, size_t size, int flags);
  591. void dma_release_declared_memory(struct device *dev);
  592. void *dma_mark_declared_memory_occupied(struct device *dev,
  593. dma_addr_t device_addr, size_t size);
  594. #else
  595. static inline int
  596. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  597. dma_addr_t device_addr, size_t size, int flags)
  598. {
  599. return 0;
  600. }
  601. static inline void
  602. dma_release_declared_memory(struct device *dev)
  603. {
  604. }
  605. static inline void *
  606. dma_mark_declared_memory_occupied(struct device *dev,
  607. dma_addr_t device_addr, size_t size)
  608. {
  609. return ERR_PTR(-EBUSY);
  610. }
  611. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  612. /*
  613. * Managed DMA API
  614. */
  615. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  616. dma_addr_t *dma_handle, gfp_t gfp);
  617. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  618. dma_addr_t dma_handle);
  619. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  620. dma_addr_t *dma_handle, gfp_t gfp);
  621. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  622. dma_addr_t dma_handle);
  623. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  624. extern int dmam_declare_coherent_memory(struct device *dev,
  625. phys_addr_t phys_addr,
  626. dma_addr_t device_addr, size_t size,
  627. int flags);
  628. extern void dmam_release_declared_memory(struct device *dev);
  629. #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  630. static inline int dmam_declare_coherent_memory(struct device *dev,
  631. phys_addr_t phys_addr, dma_addr_t device_addr,
  632. size_t size, gfp_t gfp)
  633. {
  634. return 0;
  635. }
  636. static inline void dmam_release_declared_memory(struct device *dev)
  637. {
  638. }
  639. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  640. static inline void *dma_alloc_wc(struct device *dev, size_t size,
  641. dma_addr_t *dma_addr, gfp_t gfp)
  642. {
  643. return dma_alloc_attrs(dev, size, dma_addr, gfp,
  644. DMA_ATTR_WRITE_COMBINE);
  645. }
  646. #ifndef dma_alloc_writecombine
  647. #define dma_alloc_writecombine dma_alloc_wc
  648. #endif
  649. static inline void dma_free_wc(struct device *dev, size_t size,
  650. void *cpu_addr, dma_addr_t dma_addr)
  651. {
  652. return dma_free_attrs(dev, size, cpu_addr, dma_addr,
  653. DMA_ATTR_WRITE_COMBINE);
  654. }
  655. #ifndef dma_free_writecombine
  656. #define dma_free_writecombine dma_free_wc
  657. #endif
  658. static inline int dma_mmap_wc(struct device *dev,
  659. struct vm_area_struct *vma,
  660. void *cpu_addr, dma_addr_t dma_addr,
  661. size_t size)
  662. {
  663. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
  664. DMA_ATTR_WRITE_COMBINE);
  665. }
  666. #ifndef dma_mmap_writecombine
  667. #define dma_mmap_writecombine dma_mmap_wc
  668. #endif
  669. #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
  670. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  671. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  672. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  673. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  674. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  675. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  676. #else
  677. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  678. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  679. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  680. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  681. #define dma_unmap_len(PTR, LEN_NAME) (0)
  682. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  683. #endif
  684. #endif