pci-dma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/swiotlb.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/export.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/homecache.h>
  21. /* Generic DMA mapping functions: */
  22. /*
  23. * Allocate what Linux calls "coherent" memory. On TILEPro this is
  24. * uncached memory; on TILE-Gx it is hash-for-home memory.
  25. */
  26. #ifdef __tilepro__
  27. #define PAGE_HOME_DMA PAGE_HOME_UNCACHED
  28. #else
  29. #define PAGE_HOME_DMA PAGE_HOME_HASH
  30. #endif
  31. static void *tile_dma_alloc_coherent(struct device *dev, size_t size,
  32. dma_addr_t *dma_handle, gfp_t gfp,
  33. unsigned long attrs)
  34. {
  35. u64 dma_mask = (dev && dev->coherent_dma_mask) ?
  36. dev->coherent_dma_mask : DMA_BIT_MASK(32);
  37. int node = dev ? dev_to_node(dev) : 0;
  38. int order = get_order(size);
  39. struct page *pg;
  40. dma_addr_t addr;
  41. gfp |= __GFP_ZERO;
  42. /*
  43. * If the mask specifies that the memory be in the first 4 GB, then
  44. * we force the allocation to come from the DMA zone. We also
  45. * force the node to 0 since that's the only node where the DMA
  46. * zone isn't empty. If the mask size is smaller than 32 bits, we
  47. * may still not be able to guarantee a suitable memory address, in
  48. * which case we will return NULL. But such devices are uncommon.
  49. */
  50. if (dma_mask <= DMA_BIT_MASK(32)) {
  51. gfp |= GFP_DMA;
  52. node = 0;
  53. }
  54. pg = homecache_alloc_pages_node(node, gfp, order, PAGE_HOME_DMA);
  55. if (pg == NULL)
  56. return NULL;
  57. addr = page_to_phys(pg);
  58. if (addr + size > dma_mask) {
  59. __homecache_free_pages(pg, order);
  60. return NULL;
  61. }
  62. *dma_handle = addr;
  63. return page_address(pg);
  64. }
  65. /*
  66. * Free memory that was allocated with tile_dma_alloc_coherent.
  67. */
  68. static void tile_dma_free_coherent(struct device *dev, size_t size,
  69. void *vaddr, dma_addr_t dma_handle,
  70. unsigned long attrs)
  71. {
  72. homecache_free_pages((unsigned long)vaddr, get_order(size));
  73. }
  74. /*
  75. * The map routines "map" the specified address range for DMA
  76. * accesses. The memory belongs to the device after this call is
  77. * issued, until it is unmapped with dma_unmap_single.
  78. *
  79. * We don't need to do any mapping, we just flush the address range
  80. * out of the cache and return a DMA address.
  81. *
  82. * The unmap routines do whatever is necessary before the processor
  83. * accesses the memory again, and must be called before the driver
  84. * touches the memory. We can get away with a cache invalidate if we
  85. * can count on nothing having been touched.
  86. */
  87. /* Set up a single page for DMA access. */
  88. static void __dma_prep_page(struct page *page, unsigned long offset,
  89. size_t size, enum dma_data_direction direction)
  90. {
  91. /*
  92. * Flush the page from cache if necessary.
  93. * On tilegx, data is delivered to hash-for-home L3; on tilepro,
  94. * data is delivered direct to memory.
  95. *
  96. * NOTE: If we were just doing DMA_TO_DEVICE we could optimize
  97. * this to be a "flush" not a "finv" and keep some of the
  98. * state in cache across the DMA operation, but it doesn't seem
  99. * worth creating the necessary flush_buffer_xxx() infrastructure.
  100. */
  101. int home = page_home(page);
  102. switch (home) {
  103. case PAGE_HOME_HASH:
  104. #ifdef __tilegx__
  105. return;
  106. #endif
  107. break;
  108. case PAGE_HOME_UNCACHED:
  109. #ifdef __tilepro__
  110. return;
  111. #endif
  112. break;
  113. case PAGE_HOME_IMMUTABLE:
  114. /* Should be going to the device only. */
  115. BUG_ON(direction == DMA_FROM_DEVICE ||
  116. direction == DMA_BIDIRECTIONAL);
  117. return;
  118. case PAGE_HOME_INCOHERENT:
  119. /* Incoherent anyway, so no need to work hard here. */
  120. return;
  121. default:
  122. BUG_ON(home < 0 || home >= NR_CPUS);
  123. break;
  124. }
  125. homecache_finv_page(page);
  126. #ifdef DEBUG_ALIGNMENT
  127. /* Warn if the region isn't cacheline aligned. */
  128. if (offset & (L2_CACHE_BYTES - 1) || (size & (L2_CACHE_BYTES - 1)))
  129. pr_warn("Unaligned DMA to non-hfh memory: PA %#llx/%#lx\n",
  130. PFN_PHYS(page_to_pfn(page)) + offset, size);
  131. #endif
  132. }
  133. /* Make the page ready to be read by the core. */
  134. static void __dma_complete_page(struct page *page, unsigned long offset,
  135. size_t size, enum dma_data_direction direction)
  136. {
  137. #ifdef __tilegx__
  138. switch (page_home(page)) {
  139. case PAGE_HOME_HASH:
  140. /* I/O device delivered data the way the cpu wanted it. */
  141. break;
  142. case PAGE_HOME_INCOHERENT:
  143. /* Incoherent anyway, so no need to work hard here. */
  144. break;
  145. case PAGE_HOME_IMMUTABLE:
  146. /* Extra read-only copies are not a problem. */
  147. break;
  148. default:
  149. /* Flush the bogus hash-for-home I/O entries to memory. */
  150. homecache_finv_map_page(page, PAGE_HOME_HASH);
  151. break;
  152. }
  153. #endif
  154. }
  155. static void __dma_prep_pa_range(dma_addr_t dma_addr, size_t size,
  156. enum dma_data_direction direction)
  157. {
  158. struct page *page = pfn_to_page(PFN_DOWN(dma_addr));
  159. unsigned long offset = dma_addr & (PAGE_SIZE - 1);
  160. size_t bytes = min(size, (size_t)(PAGE_SIZE - offset));
  161. while (size != 0) {
  162. __dma_prep_page(page, offset, bytes, direction);
  163. size -= bytes;
  164. ++page;
  165. offset = 0;
  166. bytes = min((size_t)PAGE_SIZE, size);
  167. }
  168. }
  169. static void __dma_complete_pa_range(dma_addr_t dma_addr, size_t size,
  170. enum dma_data_direction direction)
  171. {
  172. struct page *page = pfn_to_page(PFN_DOWN(dma_addr));
  173. unsigned long offset = dma_addr & (PAGE_SIZE - 1);
  174. size_t bytes = min(size, (size_t)(PAGE_SIZE - offset));
  175. while (size != 0) {
  176. __dma_complete_page(page, offset, bytes, direction);
  177. size -= bytes;
  178. ++page;
  179. offset = 0;
  180. bytes = min((size_t)PAGE_SIZE, size);
  181. }
  182. }
  183. static int tile_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  184. int nents, enum dma_data_direction direction,
  185. unsigned long attrs)
  186. {
  187. struct scatterlist *sg;
  188. int i;
  189. BUG_ON(!valid_dma_direction(direction));
  190. WARN_ON(nents == 0 || sglist->length == 0);
  191. for_each_sg(sglist, sg, nents, i) {
  192. sg->dma_address = sg_phys(sg);
  193. __dma_prep_pa_range(sg->dma_address, sg->length, direction);
  194. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  195. sg->dma_length = sg->length;
  196. #endif
  197. }
  198. return nents;
  199. }
  200. static void tile_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  201. int nents, enum dma_data_direction direction,
  202. unsigned long attrs)
  203. {
  204. struct scatterlist *sg;
  205. int i;
  206. BUG_ON(!valid_dma_direction(direction));
  207. for_each_sg(sglist, sg, nents, i) {
  208. sg->dma_address = sg_phys(sg);
  209. __dma_complete_pa_range(sg->dma_address, sg->length,
  210. direction);
  211. }
  212. }
  213. static dma_addr_t tile_dma_map_page(struct device *dev, struct page *page,
  214. unsigned long offset, size_t size,
  215. enum dma_data_direction direction,
  216. unsigned long attrs)
  217. {
  218. BUG_ON(!valid_dma_direction(direction));
  219. BUG_ON(offset + size > PAGE_SIZE);
  220. __dma_prep_page(page, offset, size, direction);
  221. return page_to_pa(page) + offset;
  222. }
  223. static void tile_dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  224. size_t size, enum dma_data_direction direction,
  225. unsigned long attrs)
  226. {
  227. BUG_ON(!valid_dma_direction(direction));
  228. __dma_complete_page(pfn_to_page(PFN_DOWN(dma_address)),
  229. dma_address & (PAGE_SIZE - 1), size, direction);
  230. }
  231. static void tile_dma_sync_single_for_cpu(struct device *dev,
  232. dma_addr_t dma_handle,
  233. size_t size,
  234. enum dma_data_direction direction)
  235. {
  236. BUG_ON(!valid_dma_direction(direction));
  237. __dma_complete_pa_range(dma_handle, size, direction);
  238. }
  239. static void tile_dma_sync_single_for_device(struct device *dev,
  240. dma_addr_t dma_handle, size_t size,
  241. enum dma_data_direction direction)
  242. {
  243. __dma_prep_pa_range(dma_handle, size, direction);
  244. }
  245. static void tile_dma_sync_sg_for_cpu(struct device *dev,
  246. struct scatterlist *sglist, int nelems,
  247. enum dma_data_direction direction)
  248. {
  249. struct scatterlist *sg;
  250. int i;
  251. BUG_ON(!valid_dma_direction(direction));
  252. WARN_ON(nelems == 0 || sglist->length == 0);
  253. for_each_sg(sglist, sg, nelems, i) {
  254. dma_sync_single_for_cpu(dev, sg->dma_address,
  255. sg_dma_len(sg), direction);
  256. }
  257. }
  258. static void tile_dma_sync_sg_for_device(struct device *dev,
  259. struct scatterlist *sglist, int nelems,
  260. enum dma_data_direction direction)
  261. {
  262. struct scatterlist *sg;
  263. int i;
  264. BUG_ON(!valid_dma_direction(direction));
  265. WARN_ON(nelems == 0 || sglist->length == 0);
  266. for_each_sg(sglist, sg, nelems, i) {
  267. dma_sync_single_for_device(dev, sg->dma_address,
  268. sg_dma_len(sg), direction);
  269. }
  270. }
  271. static inline int
  272. tile_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  273. {
  274. return 0;
  275. }
  276. static inline int
  277. tile_dma_supported(struct device *dev, u64 mask)
  278. {
  279. return 1;
  280. }
  281. static struct dma_map_ops tile_default_dma_map_ops = {
  282. .alloc = tile_dma_alloc_coherent,
  283. .free = tile_dma_free_coherent,
  284. .map_page = tile_dma_map_page,
  285. .unmap_page = tile_dma_unmap_page,
  286. .map_sg = tile_dma_map_sg,
  287. .unmap_sg = tile_dma_unmap_sg,
  288. .sync_single_for_cpu = tile_dma_sync_single_for_cpu,
  289. .sync_single_for_device = tile_dma_sync_single_for_device,
  290. .sync_sg_for_cpu = tile_dma_sync_sg_for_cpu,
  291. .sync_sg_for_device = tile_dma_sync_sg_for_device,
  292. .mapping_error = tile_dma_mapping_error,
  293. .dma_supported = tile_dma_supported
  294. };
  295. struct dma_map_ops *tile_dma_map_ops = &tile_default_dma_map_ops;
  296. EXPORT_SYMBOL(tile_dma_map_ops);
  297. /* Generic PCI DMA mapping functions */
  298. static void *tile_pci_dma_alloc_coherent(struct device *dev, size_t size,
  299. dma_addr_t *dma_handle, gfp_t gfp,
  300. unsigned long attrs)
  301. {
  302. int node = dev_to_node(dev);
  303. int order = get_order(size);
  304. struct page *pg;
  305. dma_addr_t addr;
  306. gfp |= __GFP_ZERO;
  307. pg = homecache_alloc_pages_node(node, gfp, order, PAGE_HOME_DMA);
  308. if (pg == NULL)
  309. return NULL;
  310. addr = page_to_phys(pg);
  311. *dma_handle = addr + get_dma_offset(dev);
  312. return page_address(pg);
  313. }
  314. /*
  315. * Free memory that was allocated with tile_pci_dma_alloc_coherent.
  316. */
  317. static void tile_pci_dma_free_coherent(struct device *dev, size_t size,
  318. void *vaddr, dma_addr_t dma_handle,
  319. unsigned long attrs)
  320. {
  321. homecache_free_pages((unsigned long)vaddr, get_order(size));
  322. }
  323. static int tile_pci_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  324. int nents, enum dma_data_direction direction,
  325. unsigned long attrs)
  326. {
  327. struct scatterlist *sg;
  328. int i;
  329. BUG_ON(!valid_dma_direction(direction));
  330. WARN_ON(nents == 0 || sglist->length == 0);
  331. for_each_sg(sglist, sg, nents, i) {
  332. sg->dma_address = sg_phys(sg);
  333. __dma_prep_pa_range(sg->dma_address, sg->length, direction);
  334. sg->dma_address = sg->dma_address + get_dma_offset(dev);
  335. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  336. sg->dma_length = sg->length;
  337. #endif
  338. }
  339. return nents;
  340. }
  341. static void tile_pci_dma_unmap_sg(struct device *dev,
  342. struct scatterlist *sglist, int nents,
  343. enum dma_data_direction direction,
  344. unsigned long attrs)
  345. {
  346. struct scatterlist *sg;
  347. int i;
  348. BUG_ON(!valid_dma_direction(direction));
  349. for_each_sg(sglist, sg, nents, i) {
  350. sg->dma_address = sg_phys(sg);
  351. __dma_complete_pa_range(sg->dma_address, sg->length,
  352. direction);
  353. }
  354. }
  355. static dma_addr_t tile_pci_dma_map_page(struct device *dev, struct page *page,
  356. unsigned long offset, size_t size,
  357. enum dma_data_direction direction,
  358. unsigned long attrs)
  359. {
  360. BUG_ON(!valid_dma_direction(direction));
  361. BUG_ON(offset + size > PAGE_SIZE);
  362. __dma_prep_page(page, offset, size, direction);
  363. return page_to_pa(page) + offset + get_dma_offset(dev);
  364. }
  365. static void tile_pci_dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  366. size_t size,
  367. enum dma_data_direction direction,
  368. unsigned long attrs)
  369. {
  370. BUG_ON(!valid_dma_direction(direction));
  371. dma_address -= get_dma_offset(dev);
  372. __dma_complete_page(pfn_to_page(PFN_DOWN(dma_address)),
  373. dma_address & (PAGE_SIZE - 1), size, direction);
  374. }
  375. static void tile_pci_dma_sync_single_for_cpu(struct device *dev,
  376. dma_addr_t dma_handle,
  377. size_t size,
  378. enum dma_data_direction direction)
  379. {
  380. BUG_ON(!valid_dma_direction(direction));
  381. dma_handle -= get_dma_offset(dev);
  382. __dma_complete_pa_range(dma_handle, size, direction);
  383. }
  384. static void tile_pci_dma_sync_single_for_device(struct device *dev,
  385. dma_addr_t dma_handle,
  386. size_t size,
  387. enum dma_data_direction
  388. direction)
  389. {
  390. dma_handle -= get_dma_offset(dev);
  391. __dma_prep_pa_range(dma_handle, size, direction);
  392. }
  393. static void tile_pci_dma_sync_sg_for_cpu(struct device *dev,
  394. struct scatterlist *sglist,
  395. int nelems,
  396. enum dma_data_direction direction)
  397. {
  398. struct scatterlist *sg;
  399. int i;
  400. BUG_ON(!valid_dma_direction(direction));
  401. WARN_ON(nelems == 0 || sglist->length == 0);
  402. for_each_sg(sglist, sg, nelems, i) {
  403. dma_sync_single_for_cpu(dev, sg->dma_address,
  404. sg_dma_len(sg), direction);
  405. }
  406. }
  407. static void tile_pci_dma_sync_sg_for_device(struct device *dev,
  408. struct scatterlist *sglist,
  409. int nelems,
  410. enum dma_data_direction direction)
  411. {
  412. struct scatterlist *sg;
  413. int i;
  414. BUG_ON(!valid_dma_direction(direction));
  415. WARN_ON(nelems == 0 || sglist->length == 0);
  416. for_each_sg(sglist, sg, nelems, i) {
  417. dma_sync_single_for_device(dev, sg->dma_address,
  418. sg_dma_len(sg), direction);
  419. }
  420. }
  421. static inline int
  422. tile_pci_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  423. {
  424. return 0;
  425. }
  426. static inline int
  427. tile_pci_dma_supported(struct device *dev, u64 mask)
  428. {
  429. return 1;
  430. }
  431. static struct dma_map_ops tile_pci_default_dma_map_ops = {
  432. .alloc = tile_pci_dma_alloc_coherent,
  433. .free = tile_pci_dma_free_coherent,
  434. .map_page = tile_pci_dma_map_page,
  435. .unmap_page = tile_pci_dma_unmap_page,
  436. .map_sg = tile_pci_dma_map_sg,
  437. .unmap_sg = tile_pci_dma_unmap_sg,
  438. .sync_single_for_cpu = tile_pci_dma_sync_single_for_cpu,
  439. .sync_single_for_device = tile_pci_dma_sync_single_for_device,
  440. .sync_sg_for_cpu = tile_pci_dma_sync_sg_for_cpu,
  441. .sync_sg_for_device = tile_pci_dma_sync_sg_for_device,
  442. .mapping_error = tile_pci_dma_mapping_error,
  443. .dma_supported = tile_pci_dma_supported
  444. };
  445. struct dma_map_ops *gx_pci_dma_map_ops = &tile_pci_default_dma_map_ops;
  446. EXPORT_SYMBOL(gx_pci_dma_map_ops);
  447. /* PCI DMA mapping functions for legacy PCI devices */
  448. #ifdef CONFIG_SWIOTLB
  449. static void *tile_swiotlb_alloc_coherent(struct device *dev, size_t size,
  450. dma_addr_t *dma_handle, gfp_t gfp,
  451. unsigned long attrs)
  452. {
  453. gfp |= GFP_DMA;
  454. return swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  455. }
  456. static void tile_swiotlb_free_coherent(struct device *dev, size_t size,
  457. void *vaddr, dma_addr_t dma_addr,
  458. unsigned long attrs)
  459. {
  460. swiotlb_free_coherent(dev, size, vaddr, dma_addr);
  461. }
  462. static struct dma_map_ops pci_swiotlb_dma_ops = {
  463. .alloc = tile_swiotlb_alloc_coherent,
  464. .free = tile_swiotlb_free_coherent,
  465. .map_page = swiotlb_map_page,
  466. .unmap_page = swiotlb_unmap_page,
  467. .map_sg = swiotlb_map_sg_attrs,
  468. .unmap_sg = swiotlb_unmap_sg_attrs,
  469. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  470. .sync_single_for_device = swiotlb_sync_single_for_device,
  471. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  472. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  473. .dma_supported = swiotlb_dma_supported,
  474. .mapping_error = swiotlb_dma_mapping_error,
  475. };
  476. static struct dma_map_ops pci_hybrid_dma_ops = {
  477. .alloc = tile_swiotlb_alloc_coherent,
  478. .free = tile_swiotlb_free_coherent,
  479. .map_page = tile_pci_dma_map_page,
  480. .unmap_page = tile_pci_dma_unmap_page,
  481. .map_sg = tile_pci_dma_map_sg,
  482. .unmap_sg = tile_pci_dma_unmap_sg,
  483. .sync_single_for_cpu = tile_pci_dma_sync_single_for_cpu,
  484. .sync_single_for_device = tile_pci_dma_sync_single_for_device,
  485. .sync_sg_for_cpu = tile_pci_dma_sync_sg_for_cpu,
  486. .sync_sg_for_device = tile_pci_dma_sync_sg_for_device,
  487. .mapping_error = tile_pci_dma_mapping_error,
  488. .dma_supported = tile_pci_dma_supported
  489. };
  490. struct dma_map_ops *gx_legacy_pci_dma_map_ops = &pci_swiotlb_dma_ops;
  491. struct dma_map_ops *gx_hybrid_pci_dma_map_ops = &pci_hybrid_dma_ops;
  492. #else
  493. struct dma_map_ops *gx_legacy_pci_dma_map_ops;
  494. struct dma_map_ops *gx_hybrid_pci_dma_map_ops;
  495. #endif
  496. EXPORT_SYMBOL(gx_legacy_pci_dma_map_ops);
  497. EXPORT_SYMBOL(gx_hybrid_pci_dma_map_ops);
  498. int dma_set_mask(struct device *dev, u64 mask)
  499. {
  500. struct dma_map_ops *dma_ops = get_dma_ops(dev);
  501. /*
  502. * For PCI devices with 64-bit DMA addressing capability, promote
  503. * the dma_ops to hybrid, with the consistent memory DMA space limited
  504. * to 32-bit. For 32-bit capable devices, limit the streaming DMA
  505. * address range to max_direct_dma_addr.
  506. */
  507. if (dma_ops == gx_pci_dma_map_ops ||
  508. dma_ops == gx_hybrid_pci_dma_map_ops ||
  509. dma_ops == gx_legacy_pci_dma_map_ops) {
  510. if (mask == DMA_BIT_MASK(64) &&
  511. dma_ops == gx_legacy_pci_dma_map_ops)
  512. set_dma_ops(dev, gx_hybrid_pci_dma_map_ops);
  513. else if (mask > dev->archdata.max_direct_dma_addr)
  514. mask = dev->archdata.max_direct_dma_addr;
  515. }
  516. if (!dev->dma_mask || !dma_supported(dev, mask))
  517. return -EIO;
  518. *dev->dma_mask = mask;
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(dma_set_mask);
  522. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  523. int dma_set_coherent_mask(struct device *dev, u64 mask)
  524. {
  525. struct dma_map_ops *dma_ops = get_dma_ops(dev);
  526. /*
  527. * For PCI devices with 64-bit DMA addressing capability, promote
  528. * the dma_ops to full capability for both streams and consistent
  529. * memory access. For 32-bit capable devices, limit the consistent
  530. * memory DMA range to max_direct_dma_addr.
  531. */
  532. if (dma_ops == gx_pci_dma_map_ops ||
  533. dma_ops == gx_hybrid_pci_dma_map_ops ||
  534. dma_ops == gx_legacy_pci_dma_map_ops) {
  535. if (mask == DMA_BIT_MASK(64))
  536. set_dma_ops(dev, gx_pci_dma_map_ops);
  537. else if (mask > dev->archdata.max_direct_dma_addr)
  538. mask = dev->archdata.max_direct_dma_addr;
  539. }
  540. if (!dma_supported(dev, mask))
  541. return -EIO;
  542. dev->coherent_dma_mask = mask;
  543. return 0;
  544. }
  545. EXPORT_SYMBOL(dma_set_coherent_mask);
  546. #endif
  547. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  548. /*
  549. * The generic dma_get_required_mask() uses the highest physical address
  550. * (max_pfn) to provide the hint to the PCI drivers regarding 32-bit or
  551. * 64-bit DMA configuration. Since TILEGx has I/O TLB/MMU, allowing the
  552. * DMAs to use the full 64-bit PCI address space and not limited by
  553. * the physical memory space, we always let the PCI devices use
  554. * 64-bit DMA if they have that capability, by returning the 64-bit
  555. * DMA mask here. The device driver has the option to use 32-bit DMA if
  556. * the device is not capable of 64-bit DMA.
  557. */
  558. u64 dma_get_required_mask(struct device *dev)
  559. {
  560. return DMA_BIT_MASK(64);
  561. }
  562. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  563. #endif