dma-mapping.h 23 KB

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