pci-dma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  194. sg->dma_length = sg->length;
  195. #endif
  196. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  197. continue;
  198. __dma_prep_pa_range(sg->dma_address, sg->length, direction);
  199. }
  200. return nents;
  201. }
  202. static void tile_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  203. int nents, enum dma_data_direction direction,
  204. unsigned long attrs)
  205. {
  206. struct scatterlist *sg;
  207. int i;
  208. BUG_ON(!valid_dma_direction(direction));
  209. for_each_sg(sglist, sg, nents, i) {
  210. sg->dma_address = sg_phys(sg);
  211. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  212. continue;
  213. __dma_complete_pa_range(sg->dma_address, sg->length,
  214. direction);
  215. }
  216. }
  217. static dma_addr_t tile_dma_map_page(struct device *dev, struct page *page,
  218. unsigned long offset, size_t size,
  219. enum dma_data_direction direction,
  220. unsigned long attrs)
  221. {
  222. BUG_ON(!valid_dma_direction(direction));
  223. BUG_ON(offset + size > PAGE_SIZE);
  224. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  225. __dma_prep_page(page, offset, size, direction);
  226. return page_to_pa(page) + offset;
  227. }
  228. static void tile_dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  229. size_t size, enum dma_data_direction direction,
  230. unsigned long attrs)
  231. {
  232. BUG_ON(!valid_dma_direction(direction));
  233. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  234. return;
  235. __dma_complete_page(pfn_to_page(PFN_DOWN(dma_address)),
  236. dma_address & (PAGE_SIZE - 1), size, direction);
  237. }
  238. static void tile_dma_sync_single_for_cpu(struct device *dev,
  239. dma_addr_t dma_handle,
  240. size_t size,
  241. enum dma_data_direction direction)
  242. {
  243. BUG_ON(!valid_dma_direction(direction));
  244. __dma_complete_pa_range(dma_handle, size, direction);
  245. }
  246. static void tile_dma_sync_single_for_device(struct device *dev,
  247. dma_addr_t dma_handle, size_t size,
  248. enum dma_data_direction direction)
  249. {
  250. __dma_prep_pa_range(dma_handle, size, direction);
  251. }
  252. static void tile_dma_sync_sg_for_cpu(struct device *dev,
  253. struct scatterlist *sglist, int nelems,
  254. enum dma_data_direction direction)
  255. {
  256. struct scatterlist *sg;
  257. int i;
  258. BUG_ON(!valid_dma_direction(direction));
  259. WARN_ON(nelems == 0 || sglist->length == 0);
  260. for_each_sg(sglist, sg, nelems, i) {
  261. dma_sync_single_for_cpu(dev, sg->dma_address,
  262. sg_dma_len(sg), direction);
  263. }
  264. }
  265. static void tile_dma_sync_sg_for_device(struct device *dev,
  266. struct scatterlist *sglist, int nelems,
  267. enum dma_data_direction direction)
  268. {
  269. struct scatterlist *sg;
  270. int i;
  271. BUG_ON(!valid_dma_direction(direction));
  272. WARN_ON(nelems == 0 || sglist->length == 0);
  273. for_each_sg(sglist, sg, nelems, i) {
  274. dma_sync_single_for_device(dev, sg->dma_address,
  275. sg_dma_len(sg), direction);
  276. }
  277. }
  278. static inline int
  279. tile_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  280. {
  281. return 0;
  282. }
  283. static inline int
  284. tile_dma_supported(struct device *dev, u64 mask)
  285. {
  286. return 1;
  287. }
  288. static struct dma_map_ops tile_default_dma_map_ops = {
  289. .alloc = tile_dma_alloc_coherent,
  290. .free = tile_dma_free_coherent,
  291. .map_page = tile_dma_map_page,
  292. .unmap_page = tile_dma_unmap_page,
  293. .map_sg = tile_dma_map_sg,
  294. .unmap_sg = tile_dma_unmap_sg,
  295. .sync_single_for_cpu = tile_dma_sync_single_for_cpu,
  296. .sync_single_for_device = tile_dma_sync_single_for_device,
  297. .sync_sg_for_cpu = tile_dma_sync_sg_for_cpu,
  298. .sync_sg_for_device = tile_dma_sync_sg_for_device,
  299. .mapping_error = tile_dma_mapping_error,
  300. .dma_supported = tile_dma_supported
  301. };
  302. struct dma_map_ops *tile_dma_map_ops = &tile_default_dma_map_ops;
  303. EXPORT_SYMBOL(tile_dma_map_ops);
  304. /* Generic PCI DMA mapping functions */
  305. static void *tile_pci_dma_alloc_coherent(struct device *dev, size_t size,
  306. dma_addr_t *dma_handle, gfp_t gfp,
  307. unsigned long attrs)
  308. {
  309. int node = dev_to_node(dev);
  310. int order = get_order(size);
  311. struct page *pg;
  312. dma_addr_t addr;
  313. gfp |= __GFP_ZERO;
  314. pg = homecache_alloc_pages_node(node, gfp, order, PAGE_HOME_DMA);
  315. if (pg == NULL)
  316. return NULL;
  317. addr = page_to_phys(pg);
  318. *dma_handle = addr + get_dma_offset(dev);
  319. return page_address(pg);
  320. }
  321. /*
  322. * Free memory that was allocated with tile_pci_dma_alloc_coherent.
  323. */
  324. static void tile_pci_dma_free_coherent(struct device *dev, size_t size,
  325. void *vaddr, dma_addr_t dma_handle,
  326. unsigned long attrs)
  327. {
  328. homecache_free_pages((unsigned long)vaddr, get_order(size));
  329. }
  330. static int tile_pci_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  331. int nents, enum dma_data_direction direction,
  332. unsigned long attrs)
  333. {
  334. struct scatterlist *sg;
  335. int i;
  336. BUG_ON(!valid_dma_direction(direction));
  337. WARN_ON(nents == 0 || sglist->length == 0);
  338. for_each_sg(sglist, sg, nents, i) {
  339. sg->dma_address = sg_phys(sg);
  340. __dma_prep_pa_range(sg->dma_address, sg->length, direction);
  341. sg->dma_address = sg->dma_address + get_dma_offset(dev);
  342. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  343. sg->dma_length = sg->length;
  344. #endif
  345. }
  346. return nents;
  347. }
  348. static void tile_pci_dma_unmap_sg(struct device *dev,
  349. struct scatterlist *sglist, int nents,
  350. enum dma_data_direction direction,
  351. unsigned long attrs)
  352. {
  353. struct scatterlist *sg;
  354. int i;
  355. BUG_ON(!valid_dma_direction(direction));
  356. for_each_sg(sglist, sg, nents, i) {
  357. sg->dma_address = sg_phys(sg);
  358. __dma_complete_pa_range(sg->dma_address, sg->length,
  359. direction);
  360. }
  361. }
  362. static dma_addr_t tile_pci_dma_map_page(struct device *dev, struct page *page,
  363. unsigned long offset, size_t size,
  364. enum dma_data_direction direction,
  365. unsigned long attrs)
  366. {
  367. BUG_ON(!valid_dma_direction(direction));
  368. BUG_ON(offset + size > PAGE_SIZE);
  369. __dma_prep_page(page, offset, size, direction);
  370. return page_to_pa(page) + offset + get_dma_offset(dev);
  371. }
  372. static void tile_pci_dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  373. size_t size,
  374. enum dma_data_direction direction,
  375. unsigned long attrs)
  376. {
  377. BUG_ON(!valid_dma_direction(direction));
  378. dma_address -= get_dma_offset(dev);
  379. __dma_complete_page(pfn_to_page(PFN_DOWN(dma_address)),
  380. dma_address & (PAGE_SIZE - 1), size, direction);
  381. }
  382. static void tile_pci_dma_sync_single_for_cpu(struct device *dev,
  383. dma_addr_t dma_handle,
  384. size_t size,
  385. enum dma_data_direction direction)
  386. {
  387. BUG_ON(!valid_dma_direction(direction));
  388. dma_handle -= get_dma_offset(dev);
  389. __dma_complete_pa_range(dma_handle, size, direction);
  390. }
  391. static void tile_pci_dma_sync_single_for_device(struct device *dev,
  392. dma_addr_t dma_handle,
  393. size_t size,
  394. enum dma_data_direction
  395. direction)
  396. {
  397. dma_handle -= get_dma_offset(dev);
  398. __dma_prep_pa_range(dma_handle, size, direction);
  399. }
  400. static void tile_pci_dma_sync_sg_for_cpu(struct device *dev,
  401. struct scatterlist *sglist,
  402. int nelems,
  403. enum dma_data_direction direction)
  404. {
  405. struct scatterlist *sg;
  406. int i;
  407. BUG_ON(!valid_dma_direction(direction));
  408. WARN_ON(nelems == 0 || sglist->length == 0);
  409. for_each_sg(sglist, sg, nelems, i) {
  410. dma_sync_single_for_cpu(dev, sg->dma_address,
  411. sg_dma_len(sg), direction);
  412. }
  413. }
  414. static void tile_pci_dma_sync_sg_for_device(struct device *dev,
  415. struct scatterlist *sglist,
  416. int nelems,
  417. enum dma_data_direction direction)
  418. {
  419. struct scatterlist *sg;
  420. int i;
  421. BUG_ON(!valid_dma_direction(direction));
  422. WARN_ON(nelems == 0 || sglist->length == 0);
  423. for_each_sg(sglist, sg, nelems, i) {
  424. dma_sync_single_for_device(dev, sg->dma_address,
  425. sg_dma_len(sg), direction);
  426. }
  427. }
  428. static inline int
  429. tile_pci_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  430. {
  431. return 0;
  432. }
  433. static inline int
  434. tile_pci_dma_supported(struct device *dev, u64 mask)
  435. {
  436. return 1;
  437. }
  438. static struct dma_map_ops tile_pci_default_dma_map_ops = {
  439. .alloc = tile_pci_dma_alloc_coherent,
  440. .free = tile_pci_dma_free_coherent,
  441. .map_page = tile_pci_dma_map_page,
  442. .unmap_page = tile_pci_dma_unmap_page,
  443. .map_sg = tile_pci_dma_map_sg,
  444. .unmap_sg = tile_pci_dma_unmap_sg,
  445. .sync_single_for_cpu = tile_pci_dma_sync_single_for_cpu,
  446. .sync_single_for_device = tile_pci_dma_sync_single_for_device,
  447. .sync_sg_for_cpu = tile_pci_dma_sync_sg_for_cpu,
  448. .sync_sg_for_device = tile_pci_dma_sync_sg_for_device,
  449. .mapping_error = tile_pci_dma_mapping_error,
  450. .dma_supported = tile_pci_dma_supported
  451. };
  452. struct dma_map_ops *gx_pci_dma_map_ops = &tile_pci_default_dma_map_ops;
  453. EXPORT_SYMBOL(gx_pci_dma_map_ops);
  454. /* PCI DMA mapping functions for legacy PCI devices */
  455. #ifdef CONFIG_SWIOTLB
  456. static void *tile_swiotlb_alloc_coherent(struct device *dev, size_t size,
  457. dma_addr_t *dma_handle, gfp_t gfp,
  458. unsigned long attrs)
  459. {
  460. gfp |= GFP_DMA;
  461. return swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
  462. }
  463. static void tile_swiotlb_free_coherent(struct device *dev, size_t size,
  464. void *vaddr, dma_addr_t dma_addr,
  465. unsigned long attrs)
  466. {
  467. swiotlb_free_coherent(dev, size, vaddr, dma_addr);
  468. }
  469. static struct dma_map_ops pci_swiotlb_dma_ops = {
  470. .alloc = tile_swiotlb_alloc_coherent,
  471. .free = tile_swiotlb_free_coherent,
  472. .map_page = swiotlb_map_page,
  473. .unmap_page = swiotlb_unmap_page,
  474. .map_sg = swiotlb_map_sg_attrs,
  475. .unmap_sg = swiotlb_unmap_sg_attrs,
  476. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  477. .sync_single_for_device = swiotlb_sync_single_for_device,
  478. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  479. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  480. .dma_supported = swiotlb_dma_supported,
  481. .mapping_error = swiotlb_dma_mapping_error,
  482. };
  483. static struct dma_map_ops pci_hybrid_dma_ops = {
  484. .alloc = tile_swiotlb_alloc_coherent,
  485. .free = tile_swiotlb_free_coherent,
  486. .map_page = tile_pci_dma_map_page,
  487. .unmap_page = tile_pci_dma_unmap_page,
  488. .map_sg = tile_pci_dma_map_sg,
  489. .unmap_sg = tile_pci_dma_unmap_sg,
  490. .sync_single_for_cpu = tile_pci_dma_sync_single_for_cpu,
  491. .sync_single_for_device = tile_pci_dma_sync_single_for_device,
  492. .sync_sg_for_cpu = tile_pci_dma_sync_sg_for_cpu,
  493. .sync_sg_for_device = tile_pci_dma_sync_sg_for_device,
  494. .mapping_error = tile_pci_dma_mapping_error,
  495. .dma_supported = tile_pci_dma_supported
  496. };
  497. struct dma_map_ops *gx_legacy_pci_dma_map_ops = &pci_swiotlb_dma_ops;
  498. struct dma_map_ops *gx_hybrid_pci_dma_map_ops = &pci_hybrid_dma_ops;
  499. #else
  500. struct dma_map_ops *gx_legacy_pci_dma_map_ops;
  501. struct dma_map_ops *gx_hybrid_pci_dma_map_ops;
  502. #endif
  503. EXPORT_SYMBOL(gx_legacy_pci_dma_map_ops);
  504. EXPORT_SYMBOL(gx_hybrid_pci_dma_map_ops);
  505. int dma_set_mask(struct device *dev, u64 mask)
  506. {
  507. struct dma_map_ops *dma_ops = get_dma_ops(dev);
  508. /*
  509. * For PCI devices with 64-bit DMA addressing capability, promote
  510. * the dma_ops to hybrid, with the consistent memory DMA space limited
  511. * to 32-bit. For 32-bit capable devices, limit the streaming DMA
  512. * address range to max_direct_dma_addr.
  513. */
  514. if (dma_ops == gx_pci_dma_map_ops ||
  515. dma_ops == gx_hybrid_pci_dma_map_ops ||
  516. dma_ops == gx_legacy_pci_dma_map_ops) {
  517. if (mask == DMA_BIT_MASK(64) &&
  518. dma_ops == gx_legacy_pci_dma_map_ops)
  519. set_dma_ops(dev, gx_hybrid_pci_dma_map_ops);
  520. else if (mask > dev->archdata.max_direct_dma_addr)
  521. mask = dev->archdata.max_direct_dma_addr;
  522. }
  523. if (!dev->dma_mask || !dma_supported(dev, mask))
  524. return -EIO;
  525. *dev->dma_mask = mask;
  526. return 0;
  527. }
  528. EXPORT_SYMBOL(dma_set_mask);
  529. #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  530. int dma_set_coherent_mask(struct device *dev, u64 mask)
  531. {
  532. struct dma_map_ops *dma_ops = get_dma_ops(dev);
  533. /*
  534. * For PCI devices with 64-bit DMA addressing capability, promote
  535. * the dma_ops to full capability for both streams and consistent
  536. * memory access. For 32-bit capable devices, limit the consistent
  537. * memory DMA range to max_direct_dma_addr.
  538. */
  539. if (dma_ops == gx_pci_dma_map_ops ||
  540. dma_ops == gx_hybrid_pci_dma_map_ops ||
  541. dma_ops == gx_legacy_pci_dma_map_ops) {
  542. if (mask == DMA_BIT_MASK(64))
  543. set_dma_ops(dev, gx_pci_dma_map_ops);
  544. else if (mask > dev->archdata.max_direct_dma_addr)
  545. mask = dev->archdata.max_direct_dma_addr;
  546. }
  547. if (!dma_supported(dev, mask))
  548. return -EIO;
  549. dev->coherent_dma_mask = mask;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(dma_set_coherent_mask);
  553. #endif
  554. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  555. /*
  556. * The generic dma_get_required_mask() uses the highest physical address
  557. * (max_pfn) to provide the hint to the PCI drivers regarding 32-bit or
  558. * 64-bit DMA configuration. Since TILEGx has I/O TLB/MMU, allowing the
  559. * DMAs to use the full 64-bit PCI address space and not limited by
  560. * the physical memory space, we always let the PCI devices use
  561. * 64-bit DMA if they have that capability, by returning the 64-bit
  562. * DMA mask here. The device driver has the option to use 32-bit DMA if
  563. * the device is not capable of 64-bit DMA.
  564. */
  565. u64 dma_get_required_mask(struct device *dev)
  566. {
  567. return DMA_BIT_MASK(64);
  568. }
  569. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  570. #endif