dma-mapping.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. * Author: Catalin Marinas <[email protected]>
  5. */
  6. #include <linux/gfp.h>
  7. #include <linux/cache.h>
  8. #include <linux/dma-map-ops.h>
  9. #include <linux/iommu.h>
  10. #include <xen/xen.h>
  11. #include <trace/hooks/iommu.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/xen/xen-ops.h>
  14. void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  15. enum dma_data_direction dir)
  16. {
  17. unsigned long start = (unsigned long)phys_to_virt(paddr);
  18. dcache_clean_poc(start, start + size);
  19. }
  20. void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  21. enum dma_data_direction dir)
  22. {
  23. unsigned long start = (unsigned long)phys_to_virt(paddr);
  24. if (dir == DMA_TO_DEVICE)
  25. return;
  26. dcache_inval_poc(start, start + size);
  27. }
  28. void arch_dma_prep_coherent(struct page *page, size_t size)
  29. {
  30. unsigned long start = (unsigned long)page_address(page);
  31. /*
  32. * The architecture only requires a clean to the PoC here in order to
  33. * meet the requirements of the DMA API. However, some vendors (i.e.
  34. * Qualcomm) abuse the DMA API for transferring buffers from the
  35. * non-secure to the secure world, resetting the system if a non-secure
  36. * access shows up after the buffer has been transferred:
  37. *
  38. * https://lore.kernel.org/r/[email protected]
  39. *
  40. * Using clean+invalidate appears to make this issue less likely, but
  41. * the drivers themselves still need fixing as the CPU could issue a
  42. * speculative read from the buffer via the linear mapping irrespective
  43. * of the cache maintenance we use. Once the drivers are fixed, we can
  44. * relax this to a clean operation.
  45. */
  46. dcache_clean_inval_poc(start, start + size);
  47. }
  48. #ifdef CONFIG_IOMMU_DMA
  49. void arch_teardown_dma_ops(struct device *dev)
  50. {
  51. dev->dma_ops = NULL;
  52. }
  53. #endif
  54. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  55. const struct iommu_ops *iommu, bool coherent)
  56. {
  57. int cls = cache_line_size_of_cpu();
  58. if (!coherent && cls > ARCH_DMA_MINALIGN)
  59. panic("%s %s: ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
  60. dev_driver_string(dev), dev_name(dev), ARCH_DMA_MINALIGN,
  61. cls);
  62. dev->dma_coherent = coherent;
  63. if (iommu) {
  64. iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1);
  65. trace_android_rvh_iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1);
  66. }
  67. xen_setup_dma_ops(dev);
  68. }