Changes in 5.15.51 random: schedule mix_interrupt_randomness() less often random: quiet urandom warning ratelimit suppression message ALSA: hda/via: Fix missing beep setup ALSA: hda/conexant: Fix missing beep setup ALSA: hda/realtek: Add mute LED quirk for HP Omen laptop ALSA: hda/realtek - ALC897 headset MIC no sound ALSA: hda/realtek: Apply fixup for Lenovo Yoga Duet 7 properly ALSA: hda/realtek: Add quirk for Clevo PD70PNT ALSA: hda/realtek: Add quirk for Clevo NS50PU net: openvswitch: fix parsing of nw_proto for IPv6 fragments 9p: Fix refcounting during full path walks for fid lookups 9p: fix fid refcount leak in v9fs_vfs_atomic_open_dotl 9p: fix fid refcount leak in v9fs_vfs_get_link btrfs: fix hang during unmount when block group reclaim task is running btrfs: prevent remounting to v1 space cache for subpage mount btrfs: add error messages to all unrecognized mount options scsi: ibmvfc: Store vhost pointer during subcrq allocation scsi: ibmvfc: Allocate/free queue resource only during probe/remove mmc: sdhci-pci-o2micro: Fix card detect by dealing with debouncing mmc: mediatek: wait dma stop bit reset to 0 xen/gntdev: Avoid blocking in unmap_grant_pages() MAINTAINERS: Add new IOMMU development mailing list mtd: rawnand: gpmi: Fix setting busy timeout setting ata: libata: add qc->flags in ata_qc_complete_template tracepoint dm era: commit metadata in postsuspend after worker stops dm mirror log: clear log bits up to BITS_PER_LONG boundary tracing/kprobes: Check whether get_kretprobe() returns NULL in kretprobe_dispatcher() drm/i915: Implement w/a 22010492432 for adl-s USB: serial: pl2303: add support for more HXN (G) types USB: serial: option: add Telit LE910Cx 0x1250 composition USB: serial: option: add Quectel EM05-G modem USB: serial: option: add Quectel RM500K module support drm/msm: Ensure mmap offset is initialized drm/msm: Fix double pm_runtime_disable() call netfilter: use get_random_u32 instead of prandom scsi: scsi_debug: Fix zone transition to full condition drm/msm: Switch ordering of runpm put vs devfreq_idle scsi: iscsi: Exclude zero from the endpoint ID range xsk: Fix generic transmit when completion queue reservation fails drm/msm: use for_each_sgtable_sg to iterate over scatterlist bpf: Fix request_sock leak in sk lookup helpers drm/sun4i: Fix crash during suspend after component bind failure bpf, x86: Fix tail call count offset calculation on bpf2bpf call scsi: storvsc: Correct reporting of Hyper-V I/O size limits phy: aquantia: Fix AN when higher speeds than 1G are not advertised KVM: arm64: Prevent kmemleak from accessing pKVM memory net: Write lock dev_base_lock without disabling bottom halves. net: fix data-race in dev_isalive() tipc: fix use-after-free Read in tipc_named_reinit igb: fix a use-after-free issue in igb_clean_tx_ring bonding: ARP monitor spams NETDEV_NOTIFY_PEERS notifiers ethtool: Fix get module eeprom fallback net/sched: sch_netem: Fix arithmetic in netem_dump() for 32-bit platforms drm/msm/mdp4: Fix refcount leak in mdp4_modeset_init_intf drm/msm/dp: check core_initialized before disable interrupts at dp_display_unbind() drm/msm/dp: Drop now unused hpd_high member drm/msm/dp: dp_link_parse_sink_count() return immediately if aux read failed drm/msm/dp: do not initialize phy until plugin interrupt received drm/msm/dp: force link training for display resolution change perf arm-spe: Don't set data source if it's not a memory operation erspan: do not assume transport header is always set net/tls: fix tls_sk_proto_close executed repeatedly udmabuf: add back sanity check selftests: netfilter: correct PKTGEN_SCRIPT_PATHS in nft_concat_range.sh xen-blkfront: Handle NULL gendisk x86/xen: Remove undefined behavior in setup_features() MIPS: Remove repetitive increase irq_err_count afs: Fix dynamic root getattr ice: ethtool: advertise 1000M speeds properly regmap-irq: Fix a bug in regmap_irq_enable() for type_in_mask chips regmap-irq: Fix offset/index mismatch in read_sub_irq_data() igb: Make DMA faster when CPU is active on the PCIe link virtio_net: fix xdp_rxq_info bug after suspend/resume Revert "net/tls: fix tls_sk_proto_close executed repeatedly" sock: redo the psock vs ULP protection check nvme-pci: add NO APST quirk for Kioxia device nvme: move the Samsung X5 quirk entry to the core quirks gpio: winbond: Fix error code in winbond_gpio_get() s390/cpumf: Handle events cycles and instructions identical iio: mma8452: fix probe fail when device tree compatible is used. iio: magnetometer: yas530: Fix memchr_inv() misuse iio: adc: vf610: fix conversion mode sysfs node name usb: typec: wcove: Drop wrong dependency to INTEL_SOC_PMIC xhci: turn off port power in shutdown xhci-pci: Allow host runtime PM as default for Intel Raptor Lake xHCI xhci-pci: Allow host runtime PM as default for Intel Meteor Lake xHCI usb: gadget: Fix non-unique driver names in raw-gadget driver USB: gadget: Fix double-free bug in raw_gadget driver usb: chipidea: udc: check request status before setting device address dt-bindings: usb: ohci: Increase the number of PHYs dt-bindings: usb: ehci: Increase the number of PHYs btrfs: don't set lock_owner when locking extent buffer for reading btrfs: fix deadlock with fsync+fiemap+transaction commit f2fs: attach inline_data after setting compression iio:humidity:hts221: rearrange iio trigger get and register iio:chemical:ccs811: rearrange iio trigger get and register iio:accel:kxcjk-1013: rearrange iio trigger get and register iio:accel:bma180: rearrange iio trigger get and register iio:accel:mxc4005: rearrange iio trigger get and register iio: accel: mma8452: ignore the return value of reset operation iio: gyro: mpu3050: Fix the error handling in mpu3050_power_up() iio: trigger: sysfs: fix use-after-free on remove iio: adc: stm32: fix maximum clock rate for stm32mp15x iio: imu: inv_icm42600: Fix broken icm42600 (chip id 0 value) iio: afe: rescale: Fix boolean logic bug iio: adc: stm32: Fix ADCs iteration in irq handler iio: adc: stm32: Fix IRQs on STM32F4 by removing custom spurious IRQs message iio: adc: axp288: Override TS pin bias current for some models iio: adc: rzg2l_adc: add missing fwnode_handle_put() in rzg2l_adc_parse_properties() iio: adc: adi-axi-adc: Fix refcount leak in adi_axi_adc_attach_client iio: adc: ti-ads131e08: add missing fwnode_handle_put() in ads131e08_alloc_channels() xtensa: xtfpga: Fix refcount leak bug in setup xtensa: Fix refcount leak bug in time.c parisc/stifb: Fix fb_is_primary_device() only available with CONFIG_FB_STI parisc: Enable ARCH_HAS_STRICT_MODULE_RWX powerpc/microwatt: wire up rng during setup_arch() powerpc: Enable execve syscall exit tracepoint powerpc/rtas: Allow ibm,platform-dump RTAS call with null buffer address powerpc/powernv: wire up rng during setup_arch drm/msm/dp: Always clear mask bits to disable interrupts at dp_ctrl_reset_irq_ctrl() ARM: dts: imx7: Move hsic_phy power domain to HSIC PHY node ARM: dts: imx6qdl: correct PU regulator ramp delay arm64: dts: ti: k3-am64-main: Remove support for HS400 speed mode ARM: exynos: Fix refcount leak in exynos_map_pmu soc: bcm: brcmstb: pm: pm-arm: Fix refcount leak in brcmstb_pm_probe ARM: Fix refcount leak in axxia_boot_secondary memory: samsung: exynos5422-dmc: Fix refcount leak in of_get_dram_timings ARM: cns3xxx: Fix refcount leak in cns3xxx_init modpost: fix section mismatch check for exported init/exit sections ARM: dts: bcm2711-rpi-400: Fix GPIO line names random: update comment from copy_to_user() -> copy_to_iter() perf build-id: Fix caching files with a wrong build ID dma-direct: use the correct size for dma_set_encrypted() kbuild: link vmlinux only once for CONFIG_TRIM_UNUSED_KSYMS (2nd attempt) powerpc/pseries: wire up rng during setup_arch() Linux 5.15.51 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: Ic8819a78d2d84055e7a6d44bdfab6a6cd8296dac
594 lines
16 KiB
C
594 lines
16 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (C) 2018-2020 Christoph Hellwig.
|
|
*
|
|
* DMA operations that map physical memory directly without using an IOMMU.
|
|
*/
|
|
#include <linux/memblock.h> /* for max_pfn */
|
|
#include <linux/export.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/dma-map-ops.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/pfn.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <linux/set_memory.h>
|
|
#include <linux/slab.h>
|
|
#include "direct.h"
|
|
|
|
/*
|
|
* Most architectures use ZONE_DMA for the first 16 Megabytes, but some use
|
|
* it for entirely different regions. In that case the arch code needs to
|
|
* override the variable below for dma-direct to work properly.
|
|
*/
|
|
unsigned int zone_dma_bits __ro_after_init = 24;
|
|
|
|
static inline dma_addr_t phys_to_dma_direct(struct device *dev,
|
|
phys_addr_t phys)
|
|
{
|
|
if (force_dma_unencrypted(dev))
|
|
return phys_to_dma_unencrypted(dev, phys);
|
|
return phys_to_dma(dev, phys);
|
|
}
|
|
|
|
static inline struct page *dma_direct_to_page(struct device *dev,
|
|
dma_addr_t dma_addr)
|
|
{
|
|
return pfn_to_page(PHYS_PFN(dma_to_phys(dev, dma_addr)));
|
|
}
|
|
|
|
u64 dma_direct_get_required_mask(struct device *dev)
|
|
{
|
|
phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT;
|
|
u64 max_dma = phys_to_dma_direct(dev, phys);
|
|
|
|
return (1ULL << (fls64(max_dma) - 1)) * 2 - 1;
|
|
}
|
|
|
|
static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
|
|
u64 *phys_limit)
|
|
{
|
|
u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit);
|
|
|
|
/*
|
|
* Optimistically try the zone that the physical address mask falls
|
|
* into first. If that returns memory that isn't actually addressable
|
|
* we will fallback to the next lower zone and try again.
|
|
*
|
|
* Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding
|
|
* zones.
|
|
*/
|
|
*phys_limit = dma_to_phys(dev, dma_limit);
|
|
if (*phys_limit <= DMA_BIT_MASK(zone_dma_bits))
|
|
return GFP_DMA;
|
|
if (*phys_limit <= DMA_BIT_MASK(32) &&
|
|
!zone_dma32_are_empty())
|
|
return GFP_DMA32;
|
|
return 0;
|
|
}
|
|
|
|
static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
|
|
{
|
|
dma_addr_t dma_addr = phys_to_dma_direct(dev, phys);
|
|
|
|
if (dma_addr == DMA_MAPPING_ERROR)
|
|
return false;
|
|
return dma_addr + size - 1 <=
|
|
min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit);
|
|
}
|
|
|
|
static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size)
|
|
{
|
|
if (!force_dma_unencrypted(dev))
|
|
return 0;
|
|
return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size));
|
|
}
|
|
|
|
static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
|
|
{
|
|
int ret;
|
|
|
|
if (!force_dma_unencrypted(dev))
|
|
return 0;
|
|
ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size));
|
|
if (ret)
|
|
pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
|
|
return ret;
|
|
}
|
|
|
|
static void __dma_direct_free_pages(struct device *dev, struct page *page,
|
|
size_t size)
|
|
{
|
|
if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) &&
|
|
swiotlb_free(dev, page, size))
|
|
return;
|
|
dma_free_contiguous(dev, page, size);
|
|
}
|
|
|
|
static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
|
|
gfp_t gfp, bool allow_highmem)
|
|
{
|
|
int node = dev_to_node(dev);
|
|
struct page *page = NULL;
|
|
u64 phys_limit;
|
|
|
|
WARN_ON_ONCE(!PAGE_ALIGNED(size));
|
|
|
|
gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
|
|
&phys_limit);
|
|
if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) &&
|
|
is_swiotlb_for_alloc(dev)) {
|
|
page = swiotlb_alloc(dev, size);
|
|
if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
|
|
__dma_direct_free_pages(dev, page, size);
|
|
return NULL;
|
|
}
|
|
return page;
|
|
}
|
|
|
|
page = dma_alloc_contiguous(dev, size, gfp);
|
|
if (page) {
|
|
if (!dma_coherent_ok(dev, page_to_phys(page), size) ||
|
|
(!allow_highmem && PageHighMem(page))) {
|
|
dma_free_contiguous(dev, page, size);
|
|
page = NULL;
|
|
}
|
|
}
|
|
again:
|
|
if (!page)
|
|
page = alloc_pages_node(node, gfp, get_order(size));
|
|
if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
|
|
dma_free_contiguous(dev, page, size);
|
|
page = NULL;
|
|
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
|
|
phys_limit < DMA_BIT_MASK(64) &&
|
|
!(gfp & (GFP_DMA32 | GFP_DMA)) &&
|
|
!zone_dma32_are_empty()) {
|
|
gfp |= GFP_DMA32;
|
|
goto again;
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
|
|
gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
|
|
goto again;
|
|
}
|
|
}
|
|
|
|
return page;
|
|
}
|
|
|
|
static void *dma_direct_alloc_from_pool(struct device *dev, size_t size,
|
|
dma_addr_t *dma_handle, gfp_t gfp)
|
|
{
|
|
struct page *page;
|
|
u64 phys_mask;
|
|
void *ret;
|
|
|
|
gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
|
|
&phys_mask);
|
|
page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok);
|
|
if (!page)
|
|
return NULL;
|
|
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
|
|
return ret;
|
|
}
|
|
|
|
static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
|
|
dma_addr_t *dma_handle, gfp_t gfp)
|
|
{
|
|
struct page *page;
|
|
|
|
page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
|
|
if (!page)
|
|
return NULL;
|
|
|
|
/* remove any dirty cache lines on the kernel alias */
|
|
if (!PageHighMem(page))
|
|
arch_dma_prep_coherent(page, size);
|
|
|
|
/* return the page pointer as the opaque cookie */
|
|
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
|
|
return page;
|
|
}
|
|
|
|
void *dma_direct_alloc(struct device *dev, size_t size,
|
|
dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
|
|
{
|
|
struct page *page;
|
|
void *ret;
|
|
|
|
size = PAGE_ALIGN(size);
|
|
if (attrs & DMA_ATTR_NO_WARN)
|
|
gfp |= __GFP_NOWARN;
|
|
|
|
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
|
|
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev))
|
|
return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp);
|
|
|
|
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
|
|
!IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
|
|
!IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
|
|
!dev_is_dma_coherent(dev) &&
|
|
!is_swiotlb_for_alloc(dev))
|
|
return arch_dma_alloc(dev, size, dma_handle, gfp, attrs);
|
|
|
|
if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
|
|
!dev_is_dma_coherent(dev))
|
|
return dma_alloc_from_global_coherent(dev, size, dma_handle);
|
|
|
|
/*
|
|
* Remapping or decrypting memory may block. If either is required and
|
|
* we can't block, allocate the memory from the atomic pools.
|
|
* If restricted DMA (i.e., is_swiotlb_for_alloc) is required, one must
|
|
* set up another device coherent pool by shared-dma-pool and use
|
|
* dma_alloc_from_dev_coherent instead.
|
|
*/
|
|
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
|
|
!gfpflags_allow_blocking(gfp) &&
|
|
(force_dma_unencrypted(dev) ||
|
|
(IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
|
|
!dev_is_dma_coherent(dev))) &&
|
|
!is_swiotlb_for_alloc(dev))
|
|
return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
|
|
|
|
/* we always manually zero the memory once we are done */
|
|
page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
|
|
if (!page)
|
|
return NULL;
|
|
|
|
if ((IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
|
|
!dev_is_dma_coherent(dev)) ||
|
|
(IS_ENABLED(CONFIG_DMA_REMAP) && PageHighMem(page))) {
|
|
/* remove any dirty cache lines on the kernel alias */
|
|
arch_dma_prep_coherent(page, size);
|
|
|
|
/* create a coherent mapping */
|
|
ret = dma_common_contiguous_remap(page, size,
|
|
dma_pgprot(dev, PAGE_KERNEL, attrs),
|
|
__builtin_return_address(0));
|
|
if (!ret)
|
|
goto out_free_pages;
|
|
memset(ret, 0, size);
|
|
goto done;
|
|
}
|
|
|
|
if (PageHighMem(page)) {
|
|
/*
|
|
* Depending on the cma= arguments and per-arch setup
|
|
* dma_alloc_contiguous could return highmem pages.
|
|
* Without remapping there is no way to return them here,
|
|
* so log an error and fail.
|
|
*/
|
|
dev_info(dev, "Rejecting highmem page from CMA.\n");
|
|
goto out_free_pages;
|
|
}
|
|
|
|
ret = page_address(page);
|
|
if (dma_set_decrypted(dev, ret, size))
|
|
goto out_free_pages;
|
|
memset(ret, 0, size);
|
|
|
|
if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
|
|
!dev_is_dma_coherent(dev)) {
|
|
arch_dma_prep_coherent(page, size);
|
|
ret = arch_dma_set_uncached(ret, size);
|
|
if (IS_ERR(ret))
|
|
goto out_encrypt_pages;
|
|
}
|
|
done:
|
|
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
|
|
return ret;
|
|
|
|
out_encrypt_pages:
|
|
if (dma_set_encrypted(dev, page_address(page), size))
|
|
return NULL;
|
|
out_free_pages:
|
|
__dma_direct_free_pages(dev, page, size);
|
|
return NULL;
|
|
}
|
|
|
|
void dma_direct_free(struct device *dev, size_t size,
|
|
void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
|
|
{
|
|
unsigned int page_order = get_order(size);
|
|
|
|
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
|
|
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
|
|
/* cpu_addr is a struct page cookie, not a kernel address */
|
|
dma_free_contiguous(dev, cpu_addr, size);
|
|
return;
|
|
}
|
|
|
|
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
|
|
!IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
|
|
!IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
|
|
!dev_is_dma_coherent(dev) &&
|
|
!is_swiotlb_for_alloc(dev)) {
|
|
arch_dma_free(dev, size, cpu_addr, dma_addr, attrs);
|
|
return;
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
|
|
!dev_is_dma_coherent(dev)) {
|
|
if (!dma_release_from_global_coherent(page_order, cpu_addr))
|
|
WARN_ON_ONCE(1);
|
|
return;
|
|
}
|
|
|
|
/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
|
|
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
|
|
dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
|
|
return;
|
|
|
|
if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
|
|
vunmap(cpu_addr);
|
|
} else {
|
|
if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED))
|
|
arch_dma_clear_uncached(cpu_addr, size);
|
|
if (dma_set_encrypted(dev, cpu_addr, size))
|
|
return;
|
|
}
|
|
|
|
__dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);
|
|
}
|
|
|
|
struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
|
|
dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
|
|
{
|
|
struct page *page;
|
|
void *ret;
|
|
|
|
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
|
|
force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp) &&
|
|
!is_swiotlb_for_alloc(dev))
|
|
return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
|
|
|
|
page = __dma_direct_alloc_pages(dev, size, gfp, false);
|
|
if (!page)
|
|
return NULL;
|
|
|
|
ret = page_address(page);
|
|
if (dma_set_decrypted(dev, ret, size))
|
|
goto out_free_pages;
|
|
memset(ret, 0, size);
|
|
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
|
|
return page;
|
|
out_free_pages:
|
|
__dma_direct_free_pages(dev, page, size);
|
|
return NULL;
|
|
}
|
|
|
|
void dma_direct_free_pages(struct device *dev, size_t size,
|
|
struct page *page, dma_addr_t dma_addr,
|
|
enum dma_data_direction dir)
|
|
{
|
|
void *vaddr = page_address(page);
|
|
|
|
/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
|
|
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
|
|
dma_free_from_pool(dev, vaddr, size))
|
|
return;
|
|
|
|
if (dma_set_encrypted(dev, vaddr, size))
|
|
return;
|
|
__dma_direct_free_pages(dev, page, size);
|
|
}
|
|
|
|
#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
|
|
defined(CONFIG_SWIOTLB)
|
|
void dma_direct_sync_sg_for_device(struct device *dev,
|
|
struct scatterlist *sgl, int nents, enum dma_data_direction dir)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i) {
|
|
phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
|
|
|
|
if (unlikely(is_swiotlb_buffer(dev, paddr)))
|
|
swiotlb_sync_single_for_device(dev, paddr, sg->length,
|
|
dir);
|
|
|
|
if (!dev_is_dma_coherent(dev))
|
|
arch_sync_dma_for_device(paddr, sg->length,
|
|
dir);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
|
|
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
|
|
defined(CONFIG_SWIOTLB)
|
|
void dma_direct_sync_sg_for_cpu(struct device *dev,
|
|
struct scatterlist *sgl, int nents, enum dma_data_direction dir)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i) {
|
|
phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
|
|
|
|
if (!dev_is_dma_coherent(dev))
|
|
arch_sync_dma_for_cpu(paddr, sg->length, dir);
|
|
|
|
if (unlikely(is_swiotlb_buffer(dev, paddr)))
|
|
swiotlb_sync_single_for_cpu(dev, paddr, sg->length,
|
|
dir);
|
|
|
|
if (dir == DMA_FROM_DEVICE)
|
|
arch_dma_mark_clean(paddr, sg->length);
|
|
}
|
|
|
|
if (!dev_is_dma_coherent(dev))
|
|
arch_sync_dma_for_cpu_all();
|
|
}
|
|
|
|
void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
|
|
int nents, enum dma_data_direction dir, unsigned long attrs)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i)
|
|
dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir,
|
|
attrs);
|
|
}
|
|
#endif
|
|
|
|
int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
{
|
|
int i;
|
|
struct scatterlist *sg;
|
|
|
|
for_each_sg(sgl, sg, nents, i) {
|
|
sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
|
|
sg->offset, sg->length, dir, attrs);
|
|
if (sg->dma_address == DMA_MAPPING_ERROR)
|
|
goto out_unmap;
|
|
sg_dma_len(sg) = sg->length;
|
|
}
|
|
|
|
return nents;
|
|
|
|
out_unmap:
|
|
dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
|
|
return -EIO;
|
|
}
|
|
|
|
dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
|
|
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
|
{
|
|
dma_addr_t dma_addr = paddr;
|
|
|
|
if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
|
|
dev_err_once(dev,
|
|
"DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
|
|
&dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
|
|
WARN_ON_ONCE(1);
|
|
return DMA_MAPPING_ERROR;
|
|
}
|
|
|
|
return dma_addr;
|
|
}
|
|
|
|
int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
unsigned long attrs)
|
|
{
|
|
struct page *page = dma_direct_to_page(dev, dma_addr);
|
|
int ret;
|
|
|
|
ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
|
|
if (!ret)
|
|
sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
|
|
return ret;
|
|
}
|
|
|
|
bool dma_direct_can_mmap(struct device *dev)
|
|
{
|
|
return dev_is_dma_coherent(dev) ||
|
|
IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP);
|
|
}
|
|
|
|
int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
unsigned long attrs)
|
|
{
|
|
unsigned long user_count = vma_pages(vma);
|
|
unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
|
unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr));
|
|
int ret = -ENXIO;
|
|
|
|
vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
|
|
|
|
if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
|
|
return ret;
|
|
if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret))
|
|
return ret;
|
|
|
|
if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff)
|
|
return -ENXIO;
|
|
return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
|
|
user_count << PAGE_SHIFT, vma->vm_page_prot);
|
|
}
|
|
|
|
int dma_direct_supported(struct device *dev, u64 mask)
|
|
{
|
|
u64 min_mask = (max_pfn - 1) << PAGE_SHIFT;
|
|
|
|
/*
|
|
* Because 32-bit DMA masks are so common we expect every architecture
|
|
* to be able to satisfy them - either by not supporting more physical
|
|
* memory, or by providing a ZONE_DMA32. If neither is the case, the
|
|
* architecture needs to use an IOMMU instead of the direct mapping.
|
|
*/
|
|
if (mask >= DMA_BIT_MASK(32))
|
|
return 1;
|
|
|
|
/*
|
|
* This check needs to be against the actual bit mask value, so use
|
|
* phys_to_dma_unencrypted() here so that the SME encryption mask isn't
|
|
* part of the check.
|
|
*/
|
|
if (IS_ENABLED(CONFIG_ZONE_DMA))
|
|
min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits));
|
|
return mask >= phys_to_dma_unencrypted(dev, min_mask);
|
|
}
|
|
|
|
size_t dma_direct_max_mapping_size(struct device *dev)
|
|
{
|
|
/* If SWIOTLB is active, use its maximum mapping size */
|
|
if (is_swiotlb_active(dev) &&
|
|
(dma_addressing_limited(dev) || is_swiotlb_force_bounce(dev)))
|
|
return swiotlb_max_mapping_size(dev);
|
|
return SIZE_MAX;
|
|
}
|
|
|
|
bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr)
|
|
{
|
|
return !dev_is_dma_coherent(dev) ||
|
|
is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr));
|
|
}
|
|
|
|
/**
|
|
* dma_direct_set_offset - Assign scalar offset for a single DMA range.
|
|
* @dev: device pointer; needed to "own" the alloced memory.
|
|
* @cpu_start: beginning of memory region covered by this offset.
|
|
* @dma_start: beginning of DMA/PCI region covered by this offset.
|
|
* @size: size of the region.
|
|
*
|
|
* This is for the simple case of a uniform offset which cannot
|
|
* be discovered by "dma-ranges".
|
|
*
|
|
* It returns -ENOMEM if out of memory, -EINVAL if a map
|
|
* already exists, 0 otherwise.
|
|
*
|
|
* Note: any call to this from a driver is a bug. The mapping needs
|
|
* to be described by the device tree or other firmware interfaces.
|
|
*/
|
|
int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
|
|
dma_addr_t dma_start, u64 size)
|
|
{
|
|
struct bus_dma_region *map;
|
|
u64 offset = (u64)cpu_start - (u64)dma_start;
|
|
|
|
if (dev->dma_range_map) {
|
|
dev_err(dev, "attempt to add DMA range to existing map\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (!offset)
|
|
return 0;
|
|
|
|
map = kcalloc(2, sizeof(*map), GFP_KERNEL);
|
|
if (!map)
|
|
return -ENOMEM;
|
|
map[0].cpu_start = cpu_start;
|
|
map[0].dma_start = dma_start;
|
|
map[0].offset = offset;
|
|
map[0].size = size;
|
|
dev->dma_range_map = map;
|
|
return 0;
|
|
}
|