dma-mapping.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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(struct device *dev, struct page *page,
  224. size_t offset, size_t size,
  225. enum dma_data_direction dir)
  226. {
  227. struct dma_map_ops *ops = get_dma_ops(dev);
  228. dma_addr_t addr;
  229. kmemcheck_mark_initialized(page_address(page) + offset, size);
  230. BUG_ON(!valid_dma_direction(dir));
  231. addr = ops->map_page(dev, page, offset, size, dir, 0);
  232. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  233. return addr;
  234. }
  235. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  236. size_t size, enum dma_data_direction dir)
  237. {
  238. struct dma_map_ops *ops = get_dma_ops(dev);
  239. BUG_ON(!valid_dma_direction(dir));
  240. if (ops->unmap_page)
  241. ops->unmap_page(dev, addr, size, dir, 0);
  242. debug_dma_unmap_page(dev, addr, size, dir, false);
  243. }
  244. static inline dma_addr_t dma_map_resource(struct device *dev,
  245. phys_addr_t phys_addr,
  246. size_t size,
  247. enum dma_data_direction dir,
  248. unsigned long attrs)
  249. {
  250. struct dma_map_ops *ops = get_dma_ops(dev);
  251. dma_addr_t addr;
  252. BUG_ON(!valid_dma_direction(dir));
  253. /* Don't allow RAM to be mapped */
  254. BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
  255. addr = phys_addr;
  256. if (ops->map_resource)
  257. addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  258. debug_dma_map_resource(dev, phys_addr, size, dir, addr);
  259. return addr;
  260. }
  261. static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
  262. size_t size, enum dma_data_direction dir,
  263. unsigned long attrs)
  264. {
  265. struct dma_map_ops *ops = get_dma_ops(dev);
  266. BUG_ON(!valid_dma_direction(dir));
  267. if (ops->unmap_resource)
  268. ops->unmap_resource(dev, addr, size, dir, attrs);
  269. debug_dma_unmap_resource(dev, addr, size, dir);
  270. }
  271. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  272. size_t size,
  273. enum dma_data_direction dir)
  274. {
  275. struct dma_map_ops *ops = get_dma_ops(dev);
  276. BUG_ON(!valid_dma_direction(dir));
  277. if (ops->sync_single_for_cpu)
  278. ops->sync_single_for_cpu(dev, addr, size, dir);
  279. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  280. }
  281. static inline void dma_sync_single_for_device(struct device *dev,
  282. dma_addr_t addr, size_t size,
  283. enum dma_data_direction dir)
  284. {
  285. struct dma_map_ops *ops = get_dma_ops(dev);
  286. BUG_ON(!valid_dma_direction(dir));
  287. if (ops->sync_single_for_device)
  288. ops->sync_single_for_device(dev, addr, size, dir);
  289. debug_dma_sync_single_for_device(dev, addr, size, dir);
  290. }
  291. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  292. dma_addr_t addr,
  293. unsigned long offset,
  294. size_t size,
  295. enum dma_data_direction dir)
  296. {
  297. const struct dma_map_ops *ops = get_dma_ops(dev);
  298. BUG_ON(!valid_dma_direction(dir));
  299. if (ops->sync_single_for_cpu)
  300. ops->sync_single_for_cpu(dev, addr + offset, size, dir);
  301. debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
  302. }
  303. static inline void dma_sync_single_range_for_device(struct device *dev,
  304. dma_addr_t addr,
  305. unsigned long offset,
  306. size_t size,
  307. enum dma_data_direction dir)
  308. {
  309. const struct dma_map_ops *ops = get_dma_ops(dev);
  310. BUG_ON(!valid_dma_direction(dir));
  311. if (ops->sync_single_for_device)
  312. ops->sync_single_for_device(dev, addr + offset, size, dir);
  313. debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
  314. }
  315. static inline void
  316. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  317. int nelems, enum dma_data_direction dir)
  318. {
  319. struct dma_map_ops *ops = get_dma_ops(dev);
  320. BUG_ON(!valid_dma_direction(dir));
  321. if (ops->sync_sg_for_cpu)
  322. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  323. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  324. }
  325. static inline void
  326. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  327. int nelems, enum dma_data_direction dir)
  328. {
  329. struct dma_map_ops *ops = get_dma_ops(dev);
  330. BUG_ON(!valid_dma_direction(dir));
  331. if (ops->sync_sg_for_device)
  332. ops->sync_sg_for_device(dev, sg, nelems, dir);
  333. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  334. }
  335. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
  336. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
  337. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
  338. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
  339. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  340. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  341. void *dma_common_contiguous_remap(struct page *page, size_t size,
  342. unsigned long vm_flags,
  343. pgprot_t prot, const void *caller);
  344. void *dma_common_pages_remap(struct page **pages, size_t size,
  345. unsigned long vm_flags, pgprot_t prot,
  346. const void *caller);
  347. void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
  348. /**
  349. * dma_mmap_attrs - map a coherent DMA allocation into user space
  350. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  351. * @vma: vm_area_struct describing requested user mapping
  352. * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
  353. * @handle: device-view address returned from dma_alloc_attrs
  354. * @size: size of memory originally requested in dma_alloc_attrs
  355. * @attrs: attributes of mapping properties requested in dma_alloc_attrs
  356. *
  357. * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
  358. * into user space. The coherent DMA buffer must not be freed by the
  359. * driver until the user space mapping has been released.
  360. */
  361. static inline int
  362. dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
  363. dma_addr_t dma_addr, size_t size, unsigned long attrs)
  364. {
  365. struct dma_map_ops *ops = get_dma_ops(dev);
  366. BUG_ON(!ops);
  367. if (ops->mmap)
  368. return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
  369. return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  370. }
  371. #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
  372. int
  373. dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  374. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  375. static inline int
  376. dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr,
  377. dma_addr_t dma_addr, size_t size,
  378. unsigned long attrs)
  379. {
  380. struct dma_map_ops *ops = get_dma_ops(dev);
  381. BUG_ON(!ops);
  382. if (ops->get_sgtable)
  383. return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
  384. attrs);
  385. return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  386. }
  387. #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
  388. #ifndef arch_dma_alloc_attrs
  389. #define arch_dma_alloc_attrs(dev, flag) (true)
  390. #endif
  391. static inline void *dma_alloc_attrs(struct device *dev, size_t size,
  392. dma_addr_t *dma_handle, gfp_t flag,
  393. unsigned long attrs)
  394. {
  395. struct dma_map_ops *ops = get_dma_ops(dev);
  396. void *cpu_addr;
  397. BUG_ON(!ops);
  398. if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr))
  399. return cpu_addr;
  400. if (!arch_dma_alloc_attrs(&dev, &flag))
  401. return NULL;
  402. if (!ops->alloc)
  403. return NULL;
  404. cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
  405. debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
  406. return cpu_addr;
  407. }
  408. static inline void dma_free_attrs(struct device *dev, size_t size,
  409. void *cpu_addr, dma_addr_t dma_handle,
  410. unsigned long attrs)
  411. {
  412. struct dma_map_ops *ops = get_dma_ops(dev);
  413. BUG_ON(!ops);
  414. WARN_ON(irqs_disabled());
  415. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  416. return;
  417. if (!ops->free || !cpu_addr)
  418. return;
  419. debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
  420. ops->free(dev, size, cpu_addr, dma_handle, attrs);
  421. }
  422. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  423. dma_addr_t *dma_handle, gfp_t flag)
  424. {
  425. return dma_alloc_attrs(dev, size, dma_handle, flag, 0);
  426. }
  427. static inline void dma_free_coherent(struct device *dev, size_t size,
  428. void *cpu_addr, dma_addr_t dma_handle)
  429. {
  430. return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0);
  431. }
  432. static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
  433. dma_addr_t *dma_handle, gfp_t gfp)
  434. {
  435. return dma_alloc_attrs(dev, size, dma_handle, gfp,
  436. DMA_ATTR_NON_CONSISTENT);
  437. }
  438. static inline void dma_free_noncoherent(struct device *dev, size_t size,
  439. void *cpu_addr, dma_addr_t dma_handle)
  440. {
  441. dma_free_attrs(dev, size, cpu_addr, dma_handle,
  442. DMA_ATTR_NON_CONSISTENT);
  443. }
  444. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  445. {
  446. debug_dma_mapping_error(dev, dma_addr);
  447. if (get_dma_ops(dev)->mapping_error)
  448. return get_dma_ops(dev)->mapping_error(dev, dma_addr);
  449. #ifdef DMA_ERROR_CODE
  450. return dma_addr == DMA_ERROR_CODE;
  451. #else
  452. return 0;
  453. #endif
  454. }
  455. #ifndef HAVE_ARCH_DMA_SUPPORTED
  456. static inline int dma_supported(struct device *dev, u64 mask)
  457. {
  458. struct dma_map_ops *ops = get_dma_ops(dev);
  459. if (!ops)
  460. return 0;
  461. if (!ops->dma_supported)
  462. return 1;
  463. return ops->dma_supported(dev, mask);
  464. }
  465. #endif
  466. #ifndef HAVE_ARCH_DMA_SET_MASK
  467. static inline int dma_set_mask(struct device *dev, u64 mask)
  468. {
  469. struct dma_map_ops *ops = get_dma_ops(dev);
  470. if (ops->set_dma_mask)
  471. return ops->set_dma_mask(dev, mask);
  472. if (!dev->dma_mask || !dma_supported(dev, mask))
  473. return -EIO;
  474. *dev->dma_mask = mask;
  475. return 0;
  476. }
  477. #endif
  478. static inline u64 dma_get_mask(struct device *dev)
  479. {
  480. if (dev && dev->dma_mask && *dev->dma_mask)
  481. return *dev->dma_mask;
  482. return DMA_BIT_MASK(32);
  483. }
  484. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  485. int dma_set_coherent_mask(struct device *dev, u64 mask);
  486. #else
  487. static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
  488. {
  489. if (!dma_supported(dev, mask))
  490. return -EIO;
  491. dev->coherent_dma_mask = mask;
  492. return 0;
  493. }
  494. #endif
  495. /*
  496. * Set both the DMA mask and the coherent DMA mask to the same thing.
  497. * Note that we don't check the return value from dma_set_coherent_mask()
  498. * as the DMA API guarantees that the coherent DMA mask can be set to
  499. * the same or smaller than the streaming DMA mask.
  500. */
  501. static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
  502. {
  503. int rc = dma_set_mask(dev, mask);
  504. if (rc == 0)
  505. dma_set_coherent_mask(dev, mask);
  506. return rc;
  507. }
  508. /*
  509. * Similar to the above, except it deals with the case where the device
  510. * does not have dev->dma_mask appropriately setup.
  511. */
  512. static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
  513. {
  514. dev->dma_mask = &dev->coherent_dma_mask;
  515. return dma_set_mask_and_coherent(dev, mask);
  516. }
  517. extern u64 dma_get_required_mask(struct device *dev);
  518. #ifndef arch_setup_dma_ops
  519. static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
  520. u64 size, const struct iommu_ops *iommu,
  521. bool coherent) { }
  522. #endif
  523. #ifndef arch_teardown_dma_ops
  524. static inline void arch_teardown_dma_ops(struct device *dev) { }
  525. #endif
  526. static inline unsigned int dma_get_max_seg_size(struct device *dev)
  527. {
  528. if (dev->dma_parms && dev->dma_parms->max_segment_size)
  529. return dev->dma_parms->max_segment_size;
  530. return SZ_64K;
  531. }
  532. static inline unsigned int dma_set_max_seg_size(struct device *dev,
  533. unsigned int size)
  534. {
  535. if (dev->dma_parms) {
  536. dev->dma_parms->max_segment_size = size;
  537. return 0;
  538. }
  539. return -EIO;
  540. }
  541. static inline unsigned long dma_get_seg_boundary(struct device *dev)
  542. {
  543. if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
  544. return dev->dma_parms->segment_boundary_mask;
  545. return DMA_BIT_MASK(32);
  546. }
  547. static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
  548. {
  549. if (dev->dma_parms) {
  550. dev->dma_parms->segment_boundary_mask = mask;
  551. return 0;
  552. }
  553. return -EIO;
  554. }
  555. #ifndef dma_max_pfn
  556. static inline unsigned long dma_max_pfn(struct device *dev)
  557. {
  558. return *dev->dma_mask >> PAGE_SHIFT;
  559. }
  560. #endif
  561. static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
  562. dma_addr_t *dma_handle, gfp_t flag)
  563. {
  564. void *ret = dma_alloc_coherent(dev, size, dma_handle,
  565. flag | __GFP_ZERO);
  566. return ret;
  567. }
  568. #ifdef CONFIG_HAS_DMA
  569. static inline int dma_get_cache_alignment(void)
  570. {
  571. #ifdef ARCH_DMA_MINALIGN
  572. return ARCH_DMA_MINALIGN;
  573. #endif
  574. return 1;
  575. }
  576. #endif
  577. /* flags for the coherent memory api */
  578. #define DMA_MEMORY_MAP 0x01
  579. #define DMA_MEMORY_IO 0x02
  580. #define DMA_MEMORY_INCLUDES_CHILDREN 0x04
  581. #define DMA_MEMORY_EXCLUSIVE 0x08
  582. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  583. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  584. dma_addr_t device_addr, size_t size, int flags);
  585. void dma_release_declared_memory(struct device *dev);
  586. void *dma_mark_declared_memory_occupied(struct device *dev,
  587. dma_addr_t device_addr, size_t size);
  588. #else
  589. static inline int
  590. dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  591. dma_addr_t device_addr, size_t size, int flags)
  592. {
  593. return 0;
  594. }
  595. static inline void
  596. dma_release_declared_memory(struct device *dev)
  597. {
  598. }
  599. static inline void *
  600. dma_mark_declared_memory_occupied(struct device *dev,
  601. dma_addr_t device_addr, size_t size)
  602. {
  603. return ERR_PTR(-EBUSY);
  604. }
  605. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  606. /*
  607. * Managed DMA API
  608. */
  609. extern void *dmam_alloc_coherent(struct device *dev, size_t size,
  610. dma_addr_t *dma_handle, gfp_t gfp);
  611. extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
  612. dma_addr_t dma_handle);
  613. extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
  614. dma_addr_t *dma_handle, gfp_t gfp);
  615. extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  616. dma_addr_t dma_handle);
  617. #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
  618. extern int dmam_declare_coherent_memory(struct device *dev,
  619. phys_addr_t phys_addr,
  620. dma_addr_t device_addr, size_t size,
  621. int flags);
  622. extern void dmam_release_declared_memory(struct device *dev);
  623. #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  624. static inline int dmam_declare_coherent_memory(struct device *dev,
  625. phys_addr_t phys_addr, dma_addr_t device_addr,
  626. size_t size, gfp_t gfp)
  627. {
  628. return 0;
  629. }
  630. static inline void dmam_release_declared_memory(struct device *dev)
  631. {
  632. }
  633. #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
  634. static inline void *dma_alloc_wc(struct device *dev, size_t size,
  635. dma_addr_t *dma_addr, gfp_t gfp)
  636. {
  637. return dma_alloc_attrs(dev, size, dma_addr, gfp,
  638. DMA_ATTR_WRITE_COMBINE);
  639. }
  640. #ifndef dma_alloc_writecombine
  641. #define dma_alloc_writecombine dma_alloc_wc
  642. #endif
  643. static inline void dma_free_wc(struct device *dev, size_t size,
  644. void *cpu_addr, dma_addr_t dma_addr)
  645. {
  646. return dma_free_attrs(dev, size, cpu_addr, dma_addr,
  647. DMA_ATTR_WRITE_COMBINE);
  648. }
  649. #ifndef dma_free_writecombine
  650. #define dma_free_writecombine dma_free_wc
  651. #endif
  652. static inline int dma_mmap_wc(struct device *dev,
  653. struct vm_area_struct *vma,
  654. void *cpu_addr, dma_addr_t dma_addr,
  655. size_t size)
  656. {
  657. return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size,
  658. DMA_ATTR_WRITE_COMBINE);
  659. }
  660. #ifndef dma_mmap_writecombine
  661. #define dma_mmap_writecombine dma_mmap_wc
  662. #endif
  663. #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG)
  664. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME
  665. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME
  666. #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME)
  667. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL))
  668. #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME)
  669. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL))
  670. #else
  671. #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
  672. #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
  673. #define dma_unmap_addr(PTR, ADDR_NAME) (0)
  674. #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0)
  675. #define dma_unmap_len(PTR, LEN_NAME) (0)
  676. #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0)
  677. #endif
  678. #endif