* lsk-44/linux-linaro-lsk-v4.4: Linux 4.4.3 modules: fix modparam async_probe request module: wrapper for symbol name. itimers: Handle relative timers with CONFIG_TIME_LOW_RES proper posix-timers: Handle relative timers with CONFIG_TIME_LOW_RES proper timerfd: Handle relative timers with CONFIG_TIME_LOW_RES proper prctl: take mmap sem for writing to protect against others xfs: log mount failures don't wait for buffers to be released Revert "xfs: clear PF_NOFREEZE for xfsaild kthread" xfs: inode recovery readahead can race with inode buffer creation libxfs: pack the agfl header structure so XFS_AGFL_SIZE is correct ovl: setattr: check permissions before copy-up ovl: root: copy attr ovl: check dentry positiveness in ovl_cleanup_whiteouts() ovl: use a minimal buffer in ovl_copy_xattr ovl: allow zero size xattr futex: Drop refcount if requeue_pi() acquired the rtmutex devm_memremap_release(): fix memremap'd addr handling ipc/shm: handle removed segments gracefully in shm_mmap() intel_scu_ipcutil: underflow in scu_reg_access() mm,thp: khugepaged: call pte flush at the time of collapse dump_stack: avoid potential deadlocks radix-tree: fix oops after radix_tree_iter_retry drivers/hwspinlock: fix race between radix tree insertion and lookup radix-tree: fix race in gang lookup MAINTAINERS: return arch/sh to maintained state, with new maintainers memcg: only free spare array when readers are done numa: fix /proc/<pid>/numa_maps for hugetlbfs on s390 fs/hugetlbfs/inode.c: fix bugs in hugetlb_vmtruncate_list() scripts/bloat-o-meter: fix python3 syntax error dma-debug: switch check from _text to _stext m32r: fix m32104ut_defconfig build fail xhci: Fix list corruption in urb dequeue at host removal Revert "xhci: don't finish a TD if we get a short-transfer event mid TD" iommu/vt-d: Clear PPR bit to ensure we get more page request interrupts iommu/vt-d: Fix 64-bit accesses to 32-bit DMAR_GSTS_REG iommu/vt-d: Fix mm refcounting to hold mm_count not mm_users iommu/amd: Correct the wrong setting of alias DTE in do_attach iommu/vt-d: Don't skip PCI devices when disabling IOTLB Input: vmmouse - fix absolute device registration string_helpers: fix precision loss for some inputs Input: i8042 - add Fujitsu Lifebook U745 to the nomux list Input: elantech - mark protocols v2 and v3 as semi-mt mm: fix regression in remap_file_pages() emulation mm: replace vma_lock_anon_vma with anon_vma_lock_read/write mm: fix mlock accouting libnvdimm: fix namespace object confusion in is_uuid_busy() mm: soft-offline: check return value in second __get_any_page() call perf kvm record/report: 'unprocessable sample' error while recording/reporting guest data KVM: PPC: Fix ONE_REG AltiVec support KVM: PPC: Fix emulation of H_SET_DABR/X on POWER8 KVM: arm/arm64: Fix reference to uninitialised VGIC arm64: dma-mapping: fix handling of devices registered before arch_initcall ARM: OMAP2+: Fix ppa_zero_params and ppa_por_params for rodata ARM: OMAP2+: Fix save_secure_ram_context for rodata ARM: OMAP2+: Fix l2dis_3630 for rodata ARM: OMAP2+: Fix l2_inv_api_params for rodata ARM: OMAP2+: Fix wait_dll_lock_timed for rodata ARM: dts: at91: sama5d4ek: add phy address and IRQ for macb0 ARM: dts: at91: sama5d4 xplained: fix phy0 IRQ type ARM: dts: at91: sama5d4: fix instance id of DBGU ARM: dts: at91: sama5d4 xplained: properly mux phy interrupt ARM: dts: omap5-board-common: enable rtc and charging of backup battery ARM: dts: Fix omap5 PMIC control lines for RTC writes ARM: dts: Fix wl12xx missing clocks that cause hangs ARM: nomadik: fix up SD/MMC DT settings ARM: 8517/1: ICST: avoid arithmetic overflow in icst_hz() ARM: 8519/1: ICST: try other dividends than 1 arm64: mm: avoid calling apply_to_page_range on empty range ARM: mvebu: remove duplicated regulator definition in Armada 388 GP powerpc/ioda: Set "read" permission when "write" is set powerpc/powernv: Fix stale PE primary bus powerpc/eeh: Fix stale cached primary bus powerpc/eeh: Fix PE location code SUNRPC: Fixup socket wait for memory udf: Check output buffer length when converting name to CS0 udf: Prevent buffer overrun with multi-byte characters udf: limit the maximum number of indirect extents in a row pNFS/flexfiles: Fix an XDR encoding bug in layoutreturn nfs: Fix race in __update_open_stateid() pNFS/flexfiles: Fix an Oopsable typo in ff_mirror_match_fh() NFS: Fix attribute cache revalidation cifs: fix erroneous return value cifs_dbg() outputs an uninitialized buffer in cifs_readdir() cifs: fix race between call_async() and reconnect() cifs: Ratelimit kernel log messages iio: inkern: fix a NULL dereference on error iio: pressure: mpl115: fix temperature offset sign iio: light: acpi-als: Report data as processed iio: dac: mcp4725: set iio name property in sysfs iio: add IIO_TRIGGER dependency to STK8BA50 iio: add HAS_IOMEM dependency to VF610_ADC iio-light: Use a signed return type for ltr501_match_samp_freq() iio:adc:ti_am335x_adc Fix buffered mode by identifying as software buffer. iio: adis_buffer: Fix out-of-bounds memory access scsi: fix soft lockup in scsi_remove_target() on module removal SCSI: Add Marvell Console to VPD blacklist scsi_dh_rdac: always retry MODE SELECT on command lock violation drivers/scsi/sg.c: mark VMA as VM_IO to prevent migration SCSI: fix crashes in sd and sr runtime PM iscsi-target: Fix potential dead-lock during node acl delete scsi: add Synology to 1024 sector blacklist klist: fix starting point removed bug in klist iterators tracepoints: Do not trace when cpu is offline tracing: Fix freak link error caused by branch tracer perf tools: tracepoint_error() can receive e=NULL, robustify it tools lib traceevent: Fix output of %llu for 64 bit values read on 32 bit machines ptrace: use fsuid, fsgid, effective creds for fs access checks Btrfs: fix direct IO requests not reporting IO error to user space Btrfs: fix hang on extent buffer lock caused by the inode_paths ioctl Btrfs: fix page reading in extent_same ioctl leading to csum errors Btrfs: fix invalid page accesses in extent_same (dedup) ioctl btrfs: properly set the termination value of ctx->pos in readdir Revert "btrfs: clear PF_NOFREEZE in cleaner_kthread()" Btrfs: fix fitrim discarding device area reserved for boot loader's use btrfs: handle invalid num_stripes in sys_array ext4: don't read blocks from disk after extents being swapped ext4: fix potential integer overflow ext4: fix scheduling in atomic on group checksum failure serial: omap: Prevent DoS using unprivileged ioctl(TIOCSRS485) serial: 8250_pci: Add Intel Broadwell ports tty: Add support for PCIe WCH382 2S multi-IO card pty: make sure super_block is still valid in final /dev/tty close pty: fix possible use after free of tty->driver_data staging/speakup: Use tty_ldisc_ref() for paste kworker phy: twl4030-usb: Fix unbalanced pm_runtime_enable on module reload phy: twl4030-usb: Relase usb phy on unload ALSA: seq: Fix double port list deletion ALSA: seq: Fix leak of pool buffer at concurrent writes ALSA: pcm: Fix rwsem deadlock for non-atomic PCM stream ALSA: hda - Cancel probe work instead of flush at remove x86/mm: Fix vmalloc_fault() to handle large pages properly x86/uaccess/64: Handle the caching of 4-byte nocache copies properly in __copy_user_nocache() x86/uaccess/64: Make the __copy_user_nocache() assembly code more readable x86/mm/pat: Avoid truncation when converting cpa->numpages to address x86/mm: Fix types used in pgprot cacheability flags translations Linux 4.4.2 HID: multitouch: fix input mode switching on some Elan panels mm, vmstat: fix wrong WQ sleep when memory reclaim doesn't make any progress zsmalloc: fix migrate_zspage-zs_free race condition zram: don't call idr_remove() from zram_remove() zram: try vmalloc() after kmalloc() zram/zcomp: use GFP_NOIO to allocate streams rtlwifi: rtl8821ae: Fix 5G failure when EEPROM is incorrectly encoded rtlwifi: rtl8821ae: Fix errors in parameter initialization crypto: marvell/cesa - fix test in mv_cesa_dev_dma_init() crypto: atmel-sha - remove calls of clk_prepare() from atomic contexts crypto: atmel-sha - fix atmel_sha_remove() crypto: algif_skcipher - Do not set MAY_BACKLOG on the async path crypto: algif_skcipher - Do not dereference ctx without socket lock crypto: algif_skcipher - Do not assume that req is unchanged crypto: user - lock crypto_alg_list on alg dump EVM: Use crypto_memneq() for digest comparisons crypto: algif_hash - wait for crypto_ahash_init() to complete crypto: shash - Fix has_key setting crypto: chacha20-ssse3 - Align stack pointer to 64 bytes crypto: caam - make write transactions bufferable on PPC platforms crypto: algif_skcipher - sendmsg SG marking is off by one crypto: algif_skcipher - Load TX SG list after waiting crypto: crc32c - Fix crc32c soft dependency crypto: algif_skcipher - Fix race condition in skcipher_check_key crypto: algif_hash - Fix race condition in hash_check_key crypto: af_alg - Forbid bind(2) when nokey child sockets are present crypto: algif_skcipher - Remove custom release parent function crypto: algif_hash - Remove custom release parent function crypto: af_alg - Allow af_af_alg_release_parent to be called on nokey path ahci: Intel DNV device IDs SATA libata: disable forced PORTS_IMPL for >= AHCI 1.3 crypto: algif_skcipher - Add key check exception for cipher_null crypto: skcipher - Add crypto_skcipher_has_setkey crypto: algif_hash - Require setkey before accept(2) crypto: hash - Add crypto_ahash_has_setkey crypto: algif_skcipher - Add nokey compatibility path crypto: af_alg - Add nokey compatibility path crypto: af_alg - Fix socket double-free when accept fails crypto: af_alg - Disallow bind/setkey/... after accept(2) crypto: algif_skcipher - Require setkey before accept(2) sched: Fix crash in sched_init_numa() ext4 crypto: add missing locking for keyring_key access iommu/io-pgtable-arm: Ensure we free the final level on teardown tty: Fix unsafe ldisc reference via ioctl(TIOCGETD) tty: Retry failed reopen if tty teardown in-progress tty: Wait interruptibly for tty lock on reopen n_tty: Fix unsafe reference to "other" ldisc usb: xhci: apply XHCI_PME_STUCK_QUIRK to Intel Broxton-M platforms usb: xhci: handle both SSIC ports in PME stuck quirk usb: phy: msm: fix error handling in probe. usb: cdc-acm: send zero packet for intel 7260 modem usb: cdc-acm: handle unlinked urb in acm read callback USB: option: fix Cinterion AHxx enumeration USB: serial: option: Adding support for Telit LE922 USB: cp210x: add ID for IAI USB to RS485 adaptor USB: serial: ftdi_sio: add support for Yaesu SCU-18 cable usb: hub: do not clear BOS field during reset device USB: visor: fix null-deref at probe USB: serial: visor: fix crash on detecting device without write_urbs ASoC: rt5645: fix the shift bit of IN1 boost saa7134-alsa: Only frees registered sound cards ALSA: dummy: Implement timer backend switching more safely ALSA: hda - Fix bad dereference of jack object ALSA: hda - Fix speaker output from VAIO AiO machines Revert "ALSA: hda - Fix noise on Gigabyte Z170X mobo" ALSA: hda - Fix static checker warning in patch_hdmi.c ALSA: hda - Add fixup for Mac Mini 7,1 model ALSA: timer: Fix race between stop and interrupt ALSA: timer: Fix wrong instance passed to slave callbacks ALSA: timer: Fix race at concurrent reads ALSA: timer: Fix link corruption due to double start or stop ALSA: timer: Fix leftover link at closing ALSA: timer: Code cleanup ALSA: seq: Fix lockdep warnings due to double mutex locks ALSA: seq: Fix race at closing in virmidi driver ALSA: seq: Fix yet another races among ALSA timer accesses ASoC: dpcm: fix the BE state on hw_free ALSA: pcm: Fix potential deadlock in OSS emulation ALSA: hda/realtek - Support Dell headset mode for ALC225 ALSA: hda/realtek - Support headset mode for ALC225 ALSA: hda/realtek - New codec support of ALC225 ALSA: rawmidi: Fix race at copying & updating the position ALSA: rawmidi: Remove kernel WARNING for NULL user-space buffer check ALSA: rawmidi: Make snd_rawmidi_transmit() race-free ALSA: seq: Degrade the error message for too many opens ALSA: seq: Fix incorrect sanity check at snd_seq_oss_synth_cleanup() ALSA: dummy: Disable switching timer backend via sysfs ALSA: compress: Disable GET_CODEC_CAPS ioctl for some architectures ALSA: hda - disable dynamic clock gating on Broxton before reset ALSA: Add missing dependency on CONFIG_SND_TIMER ALSA: bebob: Use a signed return type for get_formation_index ALSA: usb-audio: avoid freeing umidi object twice ALSA: usb-audio: Add native DSD support for PS Audio NuWave DAC ALSA: usb-audio: Fix OPPO HA-1 vendor ID ALSA: usb-audio: Add quirk for Microsoft LifeCam HD-6000 ALSA: usb-audio: Fix TEAC UD-501/UD-503/NT-503 usb delay hrtimer: Handle remaining time proper for TIME_LOW_RES md/raid: only permit hot-add of compatible integrity profiles media: i2c: Don't export ir-kbd-i2c module alias parisc: Fix __ARCH_SI_PREAMBLE_SIZE parisc: Protect huge page pte changes with spinlocks printk: do cond_resched() between lines while outputting to consoles tracing/stacktrace: Show entire trace if passed in function not found tracing: Fix stacktrace skip depth in trace_buffer_unlock_commit_regs() PCI: Fix minimum allocation address overwrite PCI: host: Mark PCIe/PCI (MSI) IRQ cascade handlers as IRQF_NO_THREAD mtd: nand: assign reasonable default name for NAND drivers wlcore/wl12xx: spi: fix NULL pointer dereference (Oops) wlcore/wl12xx: spi: fix oops on firmware load ocfs2/dlm: clear refmap bit of recovery lock while doing local recovery cleanup ocfs2/dlm: ignore cleaning the migration mle that is inuse ALSA: hda - Implement loopback control switch for Realtek and other codecs block: fix bio splitting on max sectors base/platform: Fix platform drivers with no probe callback HID: usbhid: fix recursive deadlock ocfs2: NFS hangs in __ocfs2_cluster_lock due to race with ocfs2_unblock_lock block: split bios to max possible length NFSv4.1/pnfs: Fixup an lo->plh_block_lgets imbalance in layoutreturn crypto: sun4i-ss - add missing statesize Linux 4.4.1 arm64: kernel: fix architected PMU registers unconditional access arm64: kernel: enforce pmuserenr_el0 initialization and restore arm64: mm: ensure that the zero page is visible to the page table walker arm64: Clear out any singlestep state on a ptrace detach operation powerpc/module: Handle R_PPC64_ENTRY relocations scripts/recordmcount.pl: support data in text section on powerpc powerpc: Make {cmp}xchg* and their atomic_ versions fully ordered powerpc: Make value-returning atomics fully ordered powerpc/tm: Check for already reclaimed tasks batman-adv: Drop immediate orig_node free function batman-adv: Drop immediate batadv_hard_iface free function batman-adv: Drop immediate neigh_ifinfo free function batman-adv: Drop immediate batadv_neigh_node free function batman-adv: Drop immediate batadv_orig_ifinfo free function batman-adv: Avoid recursive call_rcu for batadv_nc_node batman-adv: Avoid recursive call_rcu for batadv_bla_claim team: Replace rcu_read_lock with a mutex in team_vlan_rx_kill_vid net/mlx5_core: Fix trimming down IRQ number bridge: fix lockdep addr_list_lock false positive splat ipv6: update skb->csum when CE mark is propagated net: bpf: reject invalid shifts phonet: properly unshare skbs in phonet_rcv() dwc_eth_qos: Fix dma address for multi-fragment skbs bonding: Prevent IPv6 link local address on enslaved devices net: preserve IP control block during GSO segmentation udp: disallow UFO for sockets with SO_NO_CHECK option net: pktgen: fix null ptr deref in skb allocation sched,cls_flower: set key address type when present tcp_yeah: don't set ssthresh below 2 ipv6: tcp: add rcu locking in tcp_v6_send_synack() net: sctp: prevent writes to cookie_hmac_alg from accessing invalid memory vxlan: fix test which detect duplicate vxlan iface unix: properly account for FDs passed over unix sockets xhci: refuse loading if nousb is used usb: core: lpm: fix usb3_hardware_lpm sysfs node USB: cp210x: add ID for ELV Marble Sound Board 1 rtlwifi: fix memory leak for USB device ASoC: compress: Fix compress device direction check ASoC: wm5110: Fix PGA clear when disabling DRE ALSA: timer: Handle disconnection more safely ALSA: hda - Flush the pending probe work at remove ALSA: hda - Fix missing module loading with model=generic option ALSA: hda - Fix bass pin fixup for ASUS N550JX ALSA: control: Avoid kernel warnings from tlv ioctl with numid 0 ALSA: hrtimer: Fix stall by hrtimer_cancel() ALSA: pcm: Fix snd_pcm_hw_params struct copy in compat mode ALSA: seq: Fix snd_seq_call_port_info_ioctl in compat mode ALSA: hda - Add fixup for Dell Latitidue E6540 ALSA: timer: Fix double unlink of active_list ALSA: timer: Fix race among timer ioctls ALSA: hda - fix the headset mic detection problem for a Dell laptop ALSA: timer: Harden slave timer list handling ALSA: usb-audio: Fix mixer ctl regression of Native Instrument devices ALSA: hda - Fix white noise on Dell Latitude E5550 ALSA: seq: Fix race at timer setup and close ALSA: usb-audio: Avoid calling usb_autopm_put_interface() at disconnect ALSA: seq: Fix missing NULL check at remove_events ioctl ALSA: hda - Fixup inverted internal mic for Lenovo E50-80 ALSA: usb: Add native DSD support for Oppo HA-1 x86/mm: Improve switch_mm() barrier comments x86/mm: Add barriers and document switch_mm()-vs-flush synchronization x86/boot: Double BOOT_HEAP_SIZE to 64KB x86/reboot/quirks: Add iMac10,1 to pci_reboot_dmi_table[] kvm: x86: Fix vmwrite to SECONDARY_VM_EXEC_CONTROL KVM: x86: correctly print #AC in traces KVM: x86: expose MSR_TSC_AUX to userspace x86/xen: don't reset vcpu_info on a cancelled suspend KEYS: Fix keyring ref leak in join_session_keyring() Conflicts: arch/arm64/kernel/perf_event.c drivers/scsi/sd.c sound/core/compress_offload.c Change-Id: I9f77fe42aaae249c24cd6e170202110ab1426878 Signed-off-by: Trilok Soni <tsoni@codeaurora.org>
1475 lines
38 KiB
C
1475 lines
38 KiB
C
/*
|
|
* CPU-agnostic ARM page table allocator.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* Copyright (C) 2014 ARM Limited
|
|
*
|
|
* Author: Will Deacon <will.deacon@arm.com>
|
|
*/
|
|
|
|
#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
|
|
|
|
#include <linux/iommu.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/sizes.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/types.h>
|
|
|
|
#include <asm/barrier.h>
|
|
|
|
#include "io-pgtable.h"
|
|
|
|
#define ARM_LPAE_MAX_ADDR_BITS 48
|
|
#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
|
|
#define ARM_LPAE_MAX_LEVELS 4
|
|
|
|
/* Struct accessors */
|
|
#define io_pgtable_to_data(x) \
|
|
container_of((x), struct arm_lpae_io_pgtable, iop)
|
|
|
|
#define io_pgtable_ops_to_pgtable(x) \
|
|
container_of((x), struct io_pgtable, ops)
|
|
|
|
#define io_pgtable_ops_to_data(x) \
|
|
io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
|
|
|
|
/*
|
|
* For consistency with the architecture, we always consider
|
|
* ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
|
|
*/
|
|
#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
|
|
|
|
/*
|
|
* Calculate the right shift amount to get to the portion describing level l
|
|
* in a virtual address mapped by the pagetable in d.
|
|
*/
|
|
#define ARM_LPAE_LVL_SHIFT(l,d) \
|
|
((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
|
|
* (d)->bits_per_level) + (d)->pg_shift)
|
|
|
|
#define ARM_LPAE_PAGES_PER_PGD(d) \
|
|
DIV_ROUND_UP((d)->pgd_size, 1UL << (d)->pg_shift)
|
|
|
|
/*
|
|
* Calculate the index at level l used to map virtual address a using the
|
|
* pagetable in d.
|
|
*/
|
|
#define ARM_LPAE_PGD_IDX(l,d) \
|
|
((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
|
|
|
|
#define ARM_LPAE_LVL_IDX(a,l,d) \
|
|
(((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
|
|
((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
|
|
|
|
/* Calculate the block/page mapping size at level l for pagetable in d. */
|
|
#define ARM_LPAE_BLOCK_SIZE(l,d) \
|
|
(1 << (ilog2(sizeof(arm_lpae_iopte)) + \
|
|
((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
|
|
|
|
/* Page table bits */
|
|
#define ARM_LPAE_PTE_TYPE_SHIFT 0
|
|
#define ARM_LPAE_PTE_TYPE_MASK 0x3
|
|
|
|
#define ARM_LPAE_PTE_TYPE_BLOCK 1
|
|
#define ARM_LPAE_PTE_TYPE_TABLE 3
|
|
#define ARM_LPAE_PTE_TYPE_PAGE 3
|
|
|
|
#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
|
|
#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
|
|
#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
|
|
#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
|
|
#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
|
|
#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
|
|
#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
|
|
#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
|
|
|
|
#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
|
|
/* Ignore the contiguous bit for block splitting */
|
|
#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
|
|
#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
|
|
ARM_LPAE_PTE_ATTR_HI_MASK)
|
|
|
|
/* Stage-1 PTE */
|
|
#define ARM_LPAE_PTE_AP_PRIV_RW (((arm_lpae_iopte)0) << 6)
|
|
#define ARM_LPAE_PTE_AP_RW (((arm_lpae_iopte)1) << 6)
|
|
#define ARM_LPAE_PTE_AP_PRIV_RO (((arm_lpae_iopte)2) << 6)
|
|
#define ARM_LPAE_PTE_AP_RO (((arm_lpae_iopte)3) << 6)
|
|
#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
|
|
#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
|
|
|
|
/* Stage-2 PTE */
|
|
#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
|
|
#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
|
|
#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
|
|
#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
|
|
#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
|
|
#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
|
|
|
|
/* Register bits */
|
|
#define ARM_32_LPAE_TCR_EAE (1 << 31)
|
|
#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
|
|
|
|
#define ARM_LPAE_TCR_EPD1 (1 << 23)
|
|
|
|
#define ARM_LPAE_TCR_TG0_4K (0 << 14)
|
|
#define ARM_LPAE_TCR_TG0_64K (1 << 14)
|
|
#define ARM_LPAE_TCR_TG0_16K (2 << 14)
|
|
|
|
#define ARM_LPAE_TCR_SH0_SHIFT 12
|
|
#define ARM_LPAE_TCR_SH0_MASK 0x3
|
|
#define ARM_LPAE_TCR_SH_NS 0
|
|
#define ARM_LPAE_TCR_SH_OS 2
|
|
#define ARM_LPAE_TCR_SH_IS 3
|
|
|
|
#define ARM_LPAE_TCR_ORGN0_SHIFT 10
|
|
#define ARM_LPAE_TCR_IRGN0_SHIFT 8
|
|
#define ARM_LPAE_TCR_RGN_MASK 0x3
|
|
#define ARM_LPAE_TCR_RGN_NC 0
|
|
#define ARM_LPAE_TCR_RGN_WBWA 1
|
|
#define ARM_LPAE_TCR_RGN_WT 2
|
|
#define ARM_LPAE_TCR_RGN_WB 3
|
|
|
|
#define ARM_LPAE_TCR_SL0_SHIFT 6
|
|
#define ARM_LPAE_TCR_SL0_MASK 0x3
|
|
|
|
#define ARM_LPAE_TCR_T0SZ_SHIFT 0
|
|
#define ARM_LPAE_TCR_SZ_MASK 0xf
|
|
|
|
#define ARM_LPAE_TCR_PS_SHIFT 16
|
|
#define ARM_LPAE_TCR_PS_MASK 0x7
|
|
|
|
#define ARM_LPAE_TCR_IPS_SHIFT 32
|
|
#define ARM_LPAE_TCR_IPS_MASK 0x7
|
|
|
|
#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
|
|
#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
|
|
#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
|
|
#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
|
|
#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
|
|
#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
|
|
|
|
#define ARM_LPAE_TCR_EPD1_SHIFT 23
|
|
#define ARM_LPAE_TCR_EPD1_FAULT 1
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
|
|
#define ARM_LPAE_MAIR_ATTR_MASK 0xff
|
|
#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
|
|
#define ARM_LPAE_MAIR_ATTR_NC 0x44
|
|
#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
|
|
|
|
/* IOPTE accessors */
|
|
#define iopte_deref(pte, d) \
|
|
(__va(iopte_val(pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
|
|
& ~((1ULL << (d)->pg_shift) - 1)))
|
|
|
|
#define iopte_type(pte,l) \
|
|
(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
|
|
|
|
#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
|
|
|
|
#define iopte_leaf(pte,l) \
|
|
(l == (ARM_LPAE_MAX_LEVELS - 1) ? \
|
|
(iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
|
|
(iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
|
|
|
|
#define iopte_to_pfn(pte,d) \
|
|
(((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
|
|
|
|
#define pfn_to_iopte(pfn,d) \
|
|
(((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
|
|
|
|
struct arm_lpae_io_pgtable {
|
|
struct io_pgtable iop;
|
|
|
|
int levels;
|
|
size_t pgd_size;
|
|
unsigned long pg_shift;
|
|
unsigned long bits_per_level;
|
|
|
|
void *pgd;
|
|
};
|
|
|
|
typedef u64 arm_lpae_iopte;
|
|
|
|
static bool selftest_running = false;
|
|
|
|
/*
|
|
* We'll use some ignored bits in table entries to keep track of the number
|
|
* of page mappings beneath the table. The maximum number of entries
|
|
* beneath any table mapping in armv8 is 8192 (which is possible at the
|
|
* 2nd- and 3rd-level when using a 64K granule size). The bits at our
|
|
* disposal are:
|
|
*
|
|
* 4k granule: [58..52], [11..2]
|
|
* 64k granule: [58..52], [15..2]
|
|
*
|
|
* [58..52], [11..2] is enough bits for tracking table mappings at any
|
|
* level for any granule, so we'll use those.
|
|
*/
|
|
#define BOTTOM_IGNORED_MASK 0x3ff
|
|
#define BOTTOM_IGNORED_SHIFT 2
|
|
#define BOTTOM_IGNORED_NUM_BITS 10
|
|
#define TOP_IGNORED_MASK 0x7fULL
|
|
#define TOP_IGNORED_SHIFT 52
|
|
#define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \
|
|
(TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
|
|
|
|
static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte)
|
|
{
|
|
return table_pte & ~IOPTE_RESERVED_MASK;
|
|
}
|
|
|
|
static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte)
|
|
{
|
|
return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT))
|
|
>> BOTTOM_IGNORED_SHIFT;
|
|
}
|
|
|
|
static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte)
|
|
{
|
|
return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
|
|
>> TOP_IGNORED_SHIFT;
|
|
}
|
|
|
|
static int iopte_tblcnt(arm_lpae_iopte table_pte)
|
|
{
|
|
return (_iopte_bottom_ignored_val(table_pte) |
|
|
(_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS));
|
|
}
|
|
|
|
static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val)
|
|
{
|
|
arm_lpae_iopte pte = iopte_val(*table_pte);
|
|
|
|
pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) |
|
|
(((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS))
|
|
>> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT);
|
|
*table_pte = pte;
|
|
}
|
|
|
|
static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt)
|
|
{
|
|
arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
|
|
|
|
current_cnt -= cnt;
|
|
iopte_tblcnt_set(table_ptep, current_cnt);
|
|
}
|
|
|
|
static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt)
|
|
{
|
|
arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
|
|
|
|
current_cnt += cnt;
|
|
iopte_tblcnt_set(table_ptep, current_cnt);
|
|
}
|
|
|
|
static bool suppress_map_failures;
|
|
|
|
static dma_addr_t __arm_lpae_dma_addr(void *pages)
|
|
{
|
|
return (dma_addr_t)virt_to_phys(pages);
|
|
}
|
|
|
|
static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
|
|
struct io_pgtable_cfg *cfg,
|
|
void *cookie)
|
|
{
|
|
struct device *dev = cfg->iommu_dev;
|
|
dma_addr_t dma;
|
|
void *pages = io_pgtable_alloc_pages_exact(cfg, cookie,
|
|
size, gfp | __GFP_ZERO);
|
|
|
|
if (!pages)
|
|
return NULL;
|
|
|
|
if (!selftest_running) {
|
|
dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
|
|
if (dma_mapping_error(dev, dma))
|
|
goto out_free;
|
|
/*
|
|
* We depend on the IOMMU being able to work with any physical
|
|
* address directly, so if the DMA layer suggests otherwise by
|
|
* translating or truncating them, that bodes very badly...
|
|
*/
|
|
if (dma != virt_to_phys(pages))
|
|
goto out_unmap;
|
|
}
|
|
|
|
return pages;
|
|
|
|
out_unmap:
|
|
dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
|
|
dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
|
|
out_free:
|
|
io_pgtable_free_pages_exact(cfg, cookie, pages, size);
|
|
return NULL;
|
|
}
|
|
|
|
static void __arm_lpae_free_pages(void *pages, size_t size,
|
|
struct io_pgtable_cfg *cfg, void *cookie)
|
|
{
|
|
if (!selftest_running)
|
|
dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
|
|
size, DMA_TO_DEVICE);
|
|
io_pgtable_free_pages_exact(cfg, cookie, pages, size);
|
|
}
|
|
|
|
static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
|
|
struct io_pgtable_cfg *cfg)
|
|
{
|
|
*ptep = pte;
|
|
|
|
if (!selftest_running)
|
|
dma_sync_single_for_device(cfg->iommu_dev,
|
|
__arm_lpae_dma_addr(ptep),
|
|
sizeof(pte), DMA_TO_DEVICE);
|
|
}
|
|
|
|
static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
|
|
unsigned long iova, phys_addr_t paddr,
|
|
arm_lpae_iopte prot, int lvl,
|
|
arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep,
|
|
bool flush)
|
|
{
|
|
arm_lpae_iopte pte = prot;
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
/* We require an unmap first */
|
|
if (*ptep & ARM_LPAE_PTE_VALID) {
|
|
BUG_ON(!suppress_map_failures);
|
|
return -EEXIST;
|
|
}
|
|
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
|
|
pte |= ARM_LPAE_PTE_NS;
|
|
|
|
if (lvl == ARM_LPAE_MAX_LEVELS - 1)
|
|
pte |= ARM_LPAE_PTE_TYPE_PAGE;
|
|
else
|
|
pte |= ARM_LPAE_PTE_TYPE_BLOCK;
|
|
|
|
pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
|
|
pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
|
|
|
|
*ptep = pte;
|
|
|
|
if (flush)
|
|
__arm_lpae_set_pte(ptep, pte, cfg);
|
|
|
|
if (prev_ptep)
|
|
iopte_tblcnt_add(prev_ptep, 1);
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct map_state {
|
|
unsigned long iova_end;
|
|
unsigned int pgsize;
|
|
arm_lpae_iopte *pgtable;
|
|
arm_lpae_iopte *prev_pgtable;
|
|
arm_lpae_iopte *pte_start;
|
|
unsigned int num_pte;
|
|
};
|
|
/* map state optimization works at level 3 (the 2nd-to-last level) */
|
|
#define MAP_STATE_LVL 3
|
|
|
|
static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
|
|
phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
|
|
int lvl, arm_lpae_iopte *ptep,
|
|
arm_lpae_iopte *prev_ptep, struct map_state *ms)
|
|
{
|
|
arm_lpae_iopte *cptep, pte;
|
|
void *cookie = data->iop.cookie;
|
|
size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
|
arm_lpae_iopte *pgtable = ptep;
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
/* Find our entry at the current level */
|
|
ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
|
|
/* If we can install a leaf entry at this level, then do so */
|
|
if (size == block_size && (size & cfg->pgsize_bitmap)) {
|
|
if (!ms)
|
|
return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
|
|
ptep, prev_ptep, true);
|
|
|
|
if (lvl == MAP_STATE_LVL) {
|
|
if (ms->pgtable)
|
|
dma_sync_single_for_device(cfg->iommu_dev,
|
|
__arm_lpae_dma_addr(ms->pte_start),
|
|
ms->num_pte * sizeof(*ptep),
|
|
DMA_TO_DEVICE);
|
|
|
|
ms->iova_end = round_down(iova, SZ_2M) + SZ_2M;
|
|
ms->pgtable = pgtable;
|
|
ms->prev_pgtable = prev_ptep;
|
|
ms->pgsize = size;
|
|
ms->pte_start = ptep;
|
|
ms->num_pte = 1;
|
|
} else {
|
|
/*
|
|
* We have some map state from previous page
|
|
* mappings, but we're about to set up a block
|
|
* mapping. Flush out the previous page mappings.
|
|
*/
|
|
if (ms->pgtable)
|
|
dma_sync_single_for_device(cfg->iommu_dev,
|
|
__arm_lpae_dma_addr(ms->pte_start),
|
|
ms->num_pte * sizeof(*ptep),
|
|
DMA_TO_DEVICE);
|
|
memset(ms, 0, sizeof(*ms));
|
|
ms = NULL;
|
|
}
|
|
|
|
return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep,
|
|
prev_ptep, ms == NULL);
|
|
}
|
|
|
|
/* We can't allocate tables at the final level */
|
|
if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
|
|
return -EINVAL;
|
|
|
|
/* Grab a pointer to the next level */
|
|
pte = *ptep;
|
|
if (!pte) {
|
|
cptep = __arm_lpae_alloc_pages(1UL << data->pg_shift,
|
|
GFP_ATOMIC, cfg, cookie);
|
|
if (!cptep)
|
|
return -ENOMEM;
|
|
|
|
pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
|
|
pte |= ARM_LPAE_PTE_NSTABLE;
|
|
__arm_lpae_set_pte(ptep, pte, cfg);
|
|
} else {
|
|
cptep = iopte_deref(pte, data);
|
|
}
|
|
|
|
/* Rinse, repeat */
|
|
return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep,
|
|
ptep, ms);
|
|
}
|
|
|
|
static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
|
|
int prot)
|
|
{
|
|
arm_lpae_iopte pte;
|
|
|
|
if (data->iop.fmt == ARM_64_LPAE_S1 ||
|
|
data->iop.fmt == ARM_32_LPAE_S1) {
|
|
pte = ARM_LPAE_PTE_nG;
|
|
|
|
if (prot & IOMMU_WRITE)
|
|
pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RW
|
|
: ARM_LPAE_PTE_AP_RW;
|
|
else
|
|
pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RO
|
|
: ARM_LPAE_PTE_AP_RO;
|
|
|
|
if (prot & IOMMU_CACHE)
|
|
pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
|
|
<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
|
|
|
|
if (prot & IOMMU_DEVICE)
|
|
pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV <<
|
|
ARM_LPAE_PTE_ATTRINDX_SHIFT);
|
|
} else {
|
|
pte = ARM_LPAE_PTE_HAP_FAULT;
|
|
if (prot & IOMMU_READ)
|
|
pte |= ARM_LPAE_PTE_HAP_READ;
|
|
if (prot & IOMMU_WRITE)
|
|
pte |= ARM_LPAE_PTE_HAP_WRITE;
|
|
if (prot & IOMMU_CACHE)
|
|
pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
|
|
else
|
|
pte |= ARM_LPAE_PTE_MEMATTR_NC;
|
|
|
|
if (prot & IOMMU_DEVICE)
|
|
pte |= ARM_LPAE_PTE_MEMATTR_DEV;
|
|
}
|
|
|
|
if (prot & IOMMU_NOEXEC)
|
|
pte |= ARM_LPAE_PTE_XN;
|
|
|
|
return pte;
|
|
}
|
|
|
|
static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
|
|
phys_addr_t paddr, size_t size, int iommu_prot)
|
|
{
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
arm_lpae_iopte *ptep = data->pgd;
|
|
int ret, lvl = ARM_LPAE_START_LVL(data);
|
|
arm_lpae_iopte prot;
|
|
|
|
/* If no access, then nothing to do */
|
|
if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
|
|
return 0;
|
|
|
|
prot = arm_lpae_prot_to_pte(data, iommu_prot);
|
|
ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL,
|
|
NULL);
|
|
/*
|
|
* Synchronise all PTE updates for the new mapping before there's
|
|
* a chance for anything to kick off a table walk for the new iova.
|
|
*/
|
|
wmb();
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
|
|
struct scatterlist *sg, unsigned int nents,
|
|
int iommu_prot, size_t *size)
|
|
{
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
arm_lpae_iopte *ptep = data->pgd;
|
|
int lvl = ARM_LPAE_START_LVL(data);
|
|
arm_lpae_iopte prot;
|
|
struct scatterlist *s;
|
|
size_t mapped = 0;
|
|
int i, ret;
|
|
unsigned int min_pagesz;
|
|
struct map_state ms;
|
|
|
|
/* If no access, then nothing to do */
|
|
if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
|
|
goto out_err;
|
|
|
|
prot = arm_lpae_prot_to_pte(data, iommu_prot);
|
|
|
|
min_pagesz = 1 << __ffs(data->iop.cfg.pgsize_bitmap);
|
|
|
|
memset(&ms, 0, sizeof(ms));
|
|
|
|
for_each_sg(sg, s, nents, i) {
|
|
phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
|
|
size_t size = s->length;
|
|
|
|
/*
|
|
* We are mapping on IOMMU page boundaries, so offset within
|
|
* the page must be 0. However, the IOMMU may support pages
|
|
* smaller than PAGE_SIZE, so s->offset may still represent
|
|
* an offset of that boundary within the CPU page.
|
|
*/
|
|
if (!IS_ALIGNED(s->offset, min_pagesz))
|
|
goto out_err;
|
|
|
|
while (size) {
|
|
size_t pgsize = iommu_pgsize(
|
|
data->iop.cfg.pgsize_bitmap, iova | phys, size);
|
|
|
|
if (ms.pgtable && (iova < ms.iova_end)) {
|
|
arm_lpae_iopte *ptep = ms.pgtable +
|
|
ARM_LPAE_LVL_IDX(iova, MAP_STATE_LVL,
|
|
data);
|
|
arm_lpae_init_pte(
|
|
data, iova, phys, prot, MAP_STATE_LVL,
|
|
ptep, ms.prev_pgtable, false);
|
|
ms.num_pte++;
|
|
} else {
|
|
ret = __arm_lpae_map(data, iova, phys, pgsize,
|
|
prot, lvl, ptep, NULL, &ms);
|
|
if (ret)
|
|
goto out_err;
|
|
}
|
|
|
|
iova += pgsize;
|
|
mapped += pgsize;
|
|
phys += pgsize;
|
|
size -= pgsize;
|
|
}
|
|
}
|
|
|
|
if (ms.pgtable)
|
|
dma_sync_single_for_device(cfg->iommu_dev,
|
|
__arm_lpae_dma_addr(ms.pte_start),
|
|
ms.num_pte * sizeof(*ms.pte_start),
|
|
DMA_TO_DEVICE);
|
|
/*
|
|
* Synchronise all PTE updates for the new mapping before there's
|
|
* a chance for anything to kick off a table walk for the new iova.
|
|
*/
|
|
wmb();
|
|
|
|
return mapped;
|
|
|
|
out_err:
|
|
/* Return the size of the partial mapping so that they can be undone */
|
|
*size = mapped;
|
|
return 0;
|
|
}
|
|
|
|
static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
|
|
arm_lpae_iopte *ptep)
|
|
{
|
|
arm_lpae_iopte *start, *end;
|
|
unsigned long table_size;
|
|
|
|
if (lvl == ARM_LPAE_START_LVL(data))
|
|
table_size = data->pgd_size;
|
|
else
|
|
table_size = 1UL << data->pg_shift;
|
|
|
|
start = ptep;
|
|
|
|
/* Only leaf entries at the last level */
|
|
if (lvl == ARM_LPAE_MAX_LEVELS - 1)
|
|
end = ptep;
|
|
else
|
|
end = (void *)ptep + table_size;
|
|
|
|
/* Only leaf entries at the last level */
|
|
if (lvl == ARM_LPAE_MAX_LEVELS - 1)
|
|
goto end;
|
|
|
|
while (ptep != end) {
|
|
arm_lpae_iopte pte = *ptep++;
|
|
|
|
if (!pte || iopte_leaf(pte, lvl))
|
|
continue;
|
|
|
|
__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
|
|
}
|
|
|
|
end:
|
|
__arm_lpae_free_pages(start, table_size, &data->iop.cfg,
|
|
data->iop.cookie);
|
|
}
|
|
|
|
static void arm_lpae_free_pgtable(struct io_pgtable *iop)
|
|
{
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
|
|
|
|
__arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
|
|
kfree(data);
|
|
}
|
|
|
|
static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
|
|
unsigned long iova, size_t size,
|
|
arm_lpae_iopte prot, int lvl,
|
|
arm_lpae_iopte *ptep,
|
|
arm_lpae_iopte *prev_ptep, size_t blk_size)
|
|
{
|
|
unsigned long blk_start, blk_end;
|
|
phys_addr_t blk_paddr;
|
|
arm_lpae_iopte table = 0;
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
blk_start = iova & ~(blk_size - 1);
|
|
blk_end = blk_start + blk_size;
|
|
blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
|
|
size = ARM_LPAE_BLOCK_SIZE(lvl + 1, data);
|
|
|
|
for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
|
|
arm_lpae_iopte *tablep;
|
|
|
|
/* Unmap! */
|
|
if (blk_start == iova)
|
|
continue;
|
|
|
|
/* __arm_lpae_map expects a pointer to the start of the table */
|
|
tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
|
|
if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
|
|
tablep, prev_ptep, NULL) < 0) {
|
|
if (table) {
|
|
/* Free the table we allocated */
|
|
tablep = iopte_deref(table, data);
|
|
__arm_lpae_free_pgtable(data, lvl + 1, tablep);
|
|
}
|
|
return 0; /* Bytes unmapped */
|
|
}
|
|
}
|
|
|
|
__arm_lpae_set_pte(ptep, table, cfg);
|
|
return size;
|
|
}
|
|
|
|
static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
|
|
unsigned long iova, size_t size, int lvl,
|
|
arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
|
|
{
|
|
arm_lpae_iopte pte;
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
void *cookie = data->iop.cookie;
|
|
size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
|
|
|
ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
pte = *ptep;
|
|
|
|
/* Something went horribly wrong and we ran out of page table */
|
|
if (WARN_ON(!pte || (lvl == ARM_LPAE_MAX_LEVELS)))
|
|
return 0;
|
|
|
|
/* If the size matches this level, we're in the right place */
|
|
if (size == blk_size) {
|
|
__arm_lpae_set_pte(ptep, 0, &data->iop.cfg);
|
|
|
|
if (!iopte_leaf(pte, lvl)) {
|
|
/* Also flush any partial walks */
|
|
ptep = iopte_deref(pte, data);
|
|
__arm_lpae_free_pgtable(data, lvl + 1, ptep);
|
|
}
|
|
|
|
return size;
|
|
} else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) {
|
|
arm_lpae_iopte *table = iopte_deref(pte, data);
|
|
arm_lpae_iopte *table_base = table;
|
|
int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data);
|
|
int entry_size = (1 << data->pg_shift);
|
|
int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) / entry_size;
|
|
int entries = min_t(int, size / entry_size,
|
|
max_entries - tl_offset);
|
|
int table_len = entries * sizeof(*table);
|
|
|
|
/*
|
|
* This isn't a block mapping so it must be a table mapping
|
|
* and since it's the 2nd-to-last level the next level has
|
|
* to be all page mappings. Zero them all out in one fell
|
|
* swoop.
|
|
*/
|
|
|
|
table += tl_offset;
|
|
|
|
memset(table, 0, table_len);
|
|
dma_sync_single_for_device(cfg->iommu_dev,
|
|
__arm_lpae_dma_addr(table),
|
|
table_len, DMA_TO_DEVICE);
|
|
|
|
iopte_tblcnt_sub(ptep, entries);
|
|
if (!iopte_tblcnt(*ptep)) {
|
|
/* no valid mappings left under this table. free it. */
|
|
__arm_lpae_set_pte(ptep, 0, cfg);
|
|
io_pgtable_free_pages_exact(
|
|
&data->iop.cfg, cookie, table_base,
|
|
max_entries * sizeof(*table_base));
|
|
}
|
|
|
|
return entries * entry_size;
|
|
} else if (iopte_leaf(pte, lvl)) {
|
|
/*
|
|
* Insert a table at the next level to map the old region,
|
|
* minus the part we want to unmap
|
|
*/
|
|
return arm_lpae_split_blk_unmap(data, iova, size,
|
|
iopte_prot(pte), lvl, ptep,
|
|
prev_ptep,
|
|
blk_size);
|
|
}
|
|
|
|
/* Keep on walkin' */
|
|
prev_ptep = ptep;
|
|
ptep = iopte_deref(pte, data);
|
|
return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep);
|
|
}
|
|
|
|
static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
|
|
size_t size)
|
|
{
|
|
size_t unmapped = 0;
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
struct io_pgtable *iop = &data->iop;
|
|
arm_lpae_iopte *ptep = data->pgd;
|
|
int lvl = ARM_LPAE_START_LVL(data);
|
|
|
|
while (unmapped < size) {
|
|
size_t ret, size_to_unmap, remaining;
|
|
|
|
remaining = (size - unmapped);
|
|
size_to_unmap = remaining < SZ_2M
|
|
? remaining
|
|
: iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova,
|
|
remaining);
|
|
ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep,
|
|
NULL);
|
|
if (ret == 0)
|
|
break;
|
|
unmapped += ret;
|
|
iova += ret;
|
|
}
|
|
if (unmapped)
|
|
iop->cfg.tlb->tlb_flush_all(iop->cookie);
|
|
|
|
return unmapped;
|
|
}
|
|
|
|
static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
|
|
unsigned long iova)
|
|
{
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
arm_lpae_iopte pte, *ptep = data->pgd;
|
|
int lvl = ARM_LPAE_START_LVL(data);
|
|
|
|
do {
|
|
/* Valid IOPTE pointer? */
|
|
if (!ptep)
|
|
return 0;
|
|
|
|
/* Grab the IOPTE we're interested in */
|
|
pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
|
|
|
|
/* Valid entry? */
|
|
if (!pte)
|
|
return 0;
|
|
|
|
/* Leaf entry? */
|
|
if (iopte_leaf(pte,lvl))
|
|
goto found_translation;
|
|
|
|
/* Take it to the next level */
|
|
ptep = iopte_deref(pte, data);
|
|
} while (++lvl < ARM_LPAE_MAX_LEVELS);
|
|
|
|
/* Ran out of page tables to walk */
|
|
return 0;
|
|
|
|
found_translation:
|
|
iova &= ((1 << ARM_LPAE_LVL_SHIFT(lvl, data)) - 1);
|
|
return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
|
|
}
|
|
|
|
static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
|
|
{
|
|
unsigned long granule;
|
|
|
|
/*
|
|
* We need to restrict the supported page sizes to match the
|
|
* translation regime for a particular granule. Aim to match
|
|
* the CPU page size if possible, otherwise prefer smaller sizes.
|
|
* While we're at it, restrict the block sizes to match the
|
|
* chosen granule.
|
|
*/
|
|
if (cfg->pgsize_bitmap & PAGE_SIZE)
|
|
granule = PAGE_SIZE;
|
|
else if (cfg->pgsize_bitmap & ~PAGE_MASK)
|
|
granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
|
|
else if (cfg->pgsize_bitmap & PAGE_MASK)
|
|
granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
|
|
else
|
|
granule = 0;
|
|
|
|
switch (granule) {
|
|
case SZ_4K:
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
|
break;
|
|
case SZ_16K:
|
|
cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
|
|
break;
|
|
case SZ_64K:
|
|
cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
|
|
break;
|
|
default:
|
|
cfg->pgsize_bitmap = 0;
|
|
}
|
|
}
|
|
|
|
static struct arm_lpae_io_pgtable *
|
|
arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
|
|
{
|
|
unsigned long va_bits, pgd_bits;
|
|
struct arm_lpae_io_pgtable *data;
|
|
|
|
arm_lpae_restrict_pgsizes(cfg);
|
|
|
|
if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
|
|
return NULL;
|
|
|
|
if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
|
|
return NULL;
|
|
|
|
if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
|
|
return NULL;
|
|
|
|
if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
|
|
dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
|
|
return NULL;
|
|
}
|
|
|
|
data = kmalloc(sizeof(*data), GFP_KERNEL);
|
|
if (!data)
|
|
return NULL;
|
|
|
|
data->pg_shift = __ffs(cfg->pgsize_bitmap);
|
|
data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
|
|
|
|
va_bits = cfg->ias - data->pg_shift;
|
|
data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
|
|
|
|
/* Calculate the actual size of our pgd (without concatenation) */
|
|
pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
|
|
data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
|
|
|
|
data->iop.ops = (struct io_pgtable_ops) {
|
|
.map = arm_lpae_map,
|
|
.map_sg = arm_lpae_map_sg,
|
|
.unmap = arm_lpae_unmap,
|
|
.iova_to_phys = arm_lpae_iova_to_phys,
|
|
};
|
|
|
|
return data;
|
|
}
|
|
|
|
static struct io_pgtable *
|
|
arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
|
|
{
|
|
u64 reg;
|
|
struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
|
|
|
|
if (!data)
|
|
return NULL;
|
|
|
|
/* TCR */
|
|
reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
|
|
(ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
|
|
(ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
|
|
|
|
switch (1 << data->pg_shift) {
|
|
case SZ_4K:
|
|
reg |= ARM_LPAE_TCR_TG0_4K;
|
|
break;
|
|
case SZ_16K:
|
|
reg |= ARM_LPAE_TCR_TG0_16K;
|
|
break;
|
|
case SZ_64K:
|
|
reg |= ARM_LPAE_TCR_TG0_64K;
|
|
break;
|
|
}
|
|
|
|
switch (cfg->oas) {
|
|
case 32:
|
|
reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
case 36:
|
|
reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
case 40:
|
|
reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
case 42:
|
|
reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
case 44:
|
|
reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
case 48:
|
|
reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
|
|
break;
|
|
default:
|
|
goto out_free_data;
|
|
}
|
|
|
|
reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
|
|
|
|
/* Disable speculative walks through TTBR1 */
|
|
reg |= ARM_LPAE_TCR_EPD1;
|
|
cfg->arm_lpae_s1_cfg.tcr = reg;
|
|
|
|
/* MAIRs */
|
|
reg = (ARM_LPAE_MAIR_ATTR_NC
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
|
|
(ARM_LPAE_MAIR_ATTR_WBRWA
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
|
|
(ARM_LPAE_MAIR_ATTR_DEVICE
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
|
|
|
|
cfg->arm_lpae_s1_cfg.mair[0] = reg;
|
|
cfg->arm_lpae_s1_cfg.mair[1] = 0;
|
|
|
|
/* Looking good; allocate a pgd */
|
|
data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg, cookie);
|
|
if (!data->pgd)
|
|
goto out_free_data;
|
|
|
|
/* Ensure the empty pgd is visible before any actual TTBR write */
|
|
wmb();
|
|
|
|
/* TTBRs */
|
|
cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
|
|
cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
|
|
return &data->iop;
|
|
|
|
out_free_data:
|
|
kfree(data);
|
|
return NULL;
|
|
}
|
|
|
|
static struct io_pgtable *
|
|
arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
|
|
{
|
|
u64 reg, sl;
|
|
struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
|
|
|
|
if (!data)
|
|
return NULL;
|
|
|
|
/*
|
|
* Concatenate PGDs at level 1 if possible in order to reduce
|
|
* the depth of the stage-2 walk.
|
|
*/
|
|
if (data->levels == ARM_LPAE_MAX_LEVELS) {
|
|
unsigned long pgd_pages;
|
|
|
|
pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
|
|
if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
|
|
data->pgd_size = pgd_pages << data->pg_shift;
|
|
data->levels--;
|
|
}
|
|
}
|
|
|
|
/* VTCR */
|
|
reg = ARM_64_LPAE_S2_TCR_RES1 |
|
|
(ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
|
|
(ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
|
|
(ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
|
|
|
|
sl = ARM_LPAE_START_LVL(data);
|
|
|
|
switch (1 << data->pg_shift) {
|
|
case SZ_4K:
|
|
reg |= ARM_LPAE_TCR_TG0_4K;
|
|
sl++; /* SL0 format is different for 4K granule size */
|
|
break;
|
|
case SZ_16K:
|
|
reg |= ARM_LPAE_TCR_TG0_16K;
|
|
break;
|
|
case SZ_64K:
|
|
reg |= ARM_LPAE_TCR_TG0_64K;
|
|
break;
|
|
}
|
|
|
|
switch (cfg->oas) {
|
|
case 32:
|
|
reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
case 36:
|
|
reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
case 40:
|
|
reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
case 42:
|
|
reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
case 44:
|
|
reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
case 48:
|
|
reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
|
|
break;
|
|
default:
|
|
goto out_free_data;
|
|
}
|
|
|
|
reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
|
|
reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
|
|
cfg->arm_lpae_s2_cfg.vtcr = reg;
|
|
|
|
/* Allocate pgd pages */
|
|
data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg, cookie);
|
|
if (!data->pgd)
|
|
goto out_free_data;
|
|
|
|
/* Ensure the empty pgd is visible before any actual TTBR write */
|
|
wmb();
|
|
|
|
/* VTTBR */
|
|
cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
|
|
return &data->iop;
|
|
|
|
out_free_data:
|
|
kfree(data);
|
|
return NULL;
|
|
}
|
|
|
|
static struct io_pgtable *
|
|
arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
|
|
{
|
|
struct io_pgtable *iop;
|
|
|
|
if (cfg->ias > 32 || cfg->oas > 40)
|
|
return NULL;
|
|
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
|
iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
|
|
if (iop) {
|
|
cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
|
|
cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
|
|
}
|
|
|
|
return iop;
|
|
}
|
|
|
|
static struct io_pgtable *
|
|
arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
|
|
{
|
|
struct io_pgtable *iop;
|
|
|
|
if (cfg->ias > 40 || cfg->oas > 40)
|
|
return NULL;
|
|
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
|
iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
|
|
if (iop)
|
|
cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
|
|
|
|
return iop;
|
|
}
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
|
|
.alloc = arm_64_lpae_alloc_pgtable_s1,
|
|
.free = arm_lpae_free_pgtable,
|
|
};
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
|
|
.alloc = arm_64_lpae_alloc_pgtable_s2,
|
|
.free = arm_lpae_free_pgtable,
|
|
};
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
|
|
.alloc = arm_32_lpae_alloc_pgtable_s1,
|
|
.free = arm_lpae_free_pgtable,
|
|
};
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
|
|
.alloc = arm_32_lpae_alloc_pgtable_s2,
|
|
.free = arm_lpae_free_pgtable,
|
|
};
|
|
|
|
#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
|
|
|
|
static struct io_pgtable_cfg *cfg_cookie;
|
|
|
|
static void dummy_tlb_flush_all(void *cookie)
|
|
{
|
|
WARN_ON(cookie != cfg_cookie);
|
|
}
|
|
|
|
static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf,
|
|
void *cookie)
|
|
{
|
|
WARN_ON(cookie != cfg_cookie);
|
|
WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
|
|
}
|
|
|
|
static void dummy_tlb_sync(void *cookie)
|
|
{
|
|
WARN_ON(cookie != cfg_cookie);
|
|
}
|
|
|
|
static struct iommu_gather_ops dummy_tlb_ops __initdata = {
|
|
.tlb_flush_all = dummy_tlb_flush_all,
|
|
.tlb_add_flush = dummy_tlb_add_flush,
|
|
.tlb_sync = dummy_tlb_sync,
|
|
};
|
|
|
|
static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
|
|
{
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
|
|
cfg->pgsize_bitmap, cfg->ias);
|
|
pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
|
|
data->levels, data->pgd_size, data->pg_shift,
|
|
data->bits_per_level, data->pgd);
|
|
}
|
|
|
|
#define __FAIL(ops, i) ({ \
|
|
WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
|
|
arm_lpae_dump_ops(ops); \
|
|
selftest_running = false; \
|
|
suppress_map_failures = false; \
|
|
-EFAULT; \
|
|
})
|
|
|
|
/*
|
|
* Returns true if there's any mapping in the given iova range in ops.
|
|
*/
|
|
static bool arm_lpae_range_has_mapping(struct io_pgtable_ops *ops,
|
|
unsigned long iova_start, size_t size)
|
|
{
|
|
unsigned long iova = iova_start;
|
|
|
|
while (iova < (iova_start + size)) {
|
|
if (ops->iova_to_phys(ops, iova + 42))
|
|
return true;
|
|
iova += SZ_4K;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Returns true if the iova range is successfully mapped to the contiguous
|
|
* phys range in ops.
|
|
*/
|
|
static bool arm_lpae_range_has_specific_mapping(struct io_pgtable_ops *ops,
|
|
const unsigned long iova_start,
|
|
const phys_addr_t phys_start,
|
|
const size_t size)
|
|
{
|
|
unsigned long iova = iova_start;
|
|
phys_addr_t phys = phys_start;
|
|
|
|
while (iova < (iova_start + size)) {
|
|
if (ops->iova_to_phys(ops, iova + 42) != (phys + 42))
|
|
return false;
|
|
iova += SZ_4K;
|
|
phys += SZ_4K;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
|
|
{
|
|
static const enum io_pgtable_fmt fmts[] = {
|
|
ARM_64_LPAE_S1,
|
|
ARM_64_LPAE_S2,
|
|
};
|
|
|
|
int i, j, k;
|
|
unsigned long iova;
|
|
size_t size;
|
|
struct io_pgtable_ops *ops;
|
|
|
|
selftest_running = true;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
|
|
unsigned long test_sg_sizes[] = { SZ_4K, SZ_64K, SZ_2M,
|
|
SZ_1M * 12, SZ_1M * 20 };
|
|
|
|
cfg_cookie = cfg;
|
|
ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
|
|
if (!ops) {
|
|
pr_err("selftest: failed to allocate io pgtable ops\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
/*
|
|
* Initial sanity checks. Empty page tables shouldn't
|
|
* provide any translations. TODO: check entire supported
|
|
* range for these ops rather than first 2G
|
|
*/
|
|
if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
|
|
return __FAIL(ops, i);
|
|
|
|
/*
|
|
* Distinct mappings of different granule sizes.
|
|
*/
|
|
iova = 0;
|
|
j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
|
|
while (j != BITS_PER_LONG) {
|
|
size = 1UL << j;
|
|
|
|
if (ops->map(ops, iova, iova, size, IOMMU_READ |
|
|
IOMMU_WRITE |
|
|
IOMMU_NOEXEC |
|
|
IOMMU_CACHE))
|
|
return __FAIL(ops, i);
|
|
|
|
suppress_map_failures = true;
|
|
/* Overlapping mappings */
|
|
if (!ops->map(ops, iova, iova + size, size,
|
|
IOMMU_READ | IOMMU_NOEXEC))
|
|
return __FAIL(ops, i);
|
|
suppress_map_failures = false;
|
|
|
|
if (!arm_lpae_range_has_specific_mapping(ops, iova,
|
|
iova, size))
|
|
return __FAIL(ops, i);
|
|
|
|
iova += SZ_1G;
|
|
j++;
|
|
j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
|
|
}
|
|
|
|
/* Partial unmap */
|
|
size = 1UL << __ffs(cfg->pgsize_bitmap);
|
|
if (ops->unmap(ops, SZ_1G + size, size) != size)
|
|
return __FAIL(ops, i);
|
|
|
|
if (arm_lpae_range_has_mapping(ops, SZ_1G + size, size))
|
|
return __FAIL(ops, i);
|
|
|
|
/* Remap of partial unmap */
|
|
if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
|
|
return __FAIL(ops, i);
|
|
|
|
if (!arm_lpae_range_has_specific_mapping(ops, SZ_1G + size,
|
|
size, size))
|
|
return __FAIL(ops, i);
|
|
|
|
/* Full unmap */
|
|
iova = 0;
|
|
j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
|
|
while (j != BITS_PER_LONG) {
|
|
size = 1UL << j;
|
|
|
|
if (ops->unmap(ops, iova, size) != size)
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42))
|
|
return __FAIL(ops, i);
|
|
|
|
/* Remap full block */
|
|
if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->unmap(ops, iova, size) != size)
|
|
return __FAIL(ops, i);
|
|
|
|
iova += SZ_1G;
|
|
j++;
|
|
j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
|
|
}
|
|
|
|
if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
|
|
return __FAIL(ops, i);
|
|
|
|
if ((cfg->pgsize_bitmap & SZ_2M) &&
|
|
(cfg->pgsize_bitmap & SZ_4K)) {
|
|
/* mixed block + page mappings */
|
|
iova = 0;
|
|
if (ops->map(ops, iova, iova, SZ_2M, IOMMU_READ))
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->map(ops, iova + SZ_2M, iova + SZ_2M, SZ_4K,
|
|
IOMMU_READ))
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
|
|
return __FAIL(ops, i);
|
|
|
|
if (ops->iova_to_phys(ops, iova + SZ_2M + 42) !=
|
|
(iova + SZ_2M + 42))
|
|
return __FAIL(ops, i);
|
|
|
|
/* unmap both mappings at once */
|
|
if (ops->unmap(ops, iova, SZ_2M + SZ_4K) !=
|
|
(SZ_2M + SZ_4K))
|
|
return __FAIL(ops, i);
|
|
|
|
if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
|
|
return __FAIL(ops, i);
|
|
}
|
|
|
|
/* map_sg */
|
|
for (j = 0; j < ARRAY_SIZE(test_sg_sizes); ++j) {
|
|
size_t mapped;
|
|
size_t unused;
|
|
struct page *page;
|
|
phys_addr_t page_phys;
|
|
struct sg_table table;
|
|
struct scatterlist *sg;
|
|
unsigned long total_size = test_sg_sizes[j];
|
|
int chunk_size = 1UL << find_first_bit(
|
|
&cfg->pgsize_bitmap, BITS_PER_LONG);
|
|
int nents = total_size / chunk_size;
|
|
|
|
if (total_size < chunk_size)
|
|
continue;
|
|
|
|
page = alloc_pages(GFP_KERNEL, get_order(chunk_size));
|
|
page_phys = page_to_phys(page);
|
|
|
|
iova = 0;
|
|
BUG_ON(sg_alloc_table(&table, nents, GFP_KERNEL));
|
|
BUG_ON(!page);
|
|
for_each_sg(table.sgl, sg, table.nents, k)
|
|
sg_set_page(sg, page, chunk_size, 0);
|
|
|
|
mapped = ops->map_sg(ops, iova, table.sgl, table.nents,
|
|
IOMMU_READ | IOMMU_WRITE, &unused);
|
|
|
|
if (mapped != total_size)
|
|
return __FAIL(ops, i);
|
|
|
|
if (!arm_lpae_range_has_mapping(ops, iova, total_size))
|
|
return __FAIL(ops, i);
|
|
|
|
if (arm_lpae_range_has_mapping(ops, iova + total_size,
|
|
SZ_2G - (iova + total_size)))
|
|
return __FAIL(ops, i);
|
|
|
|
for_each_sg(table.sgl, sg, table.nents, k) {
|
|
dma_addr_t newphys =
|
|
ops->iova_to_phys(ops, iova + 42);
|
|
if (newphys != (page_phys + 42))
|
|
return __FAIL(ops, i);
|
|
iova += chunk_size;
|
|
}
|
|
|
|
if (ops->unmap(ops, 0, total_size) != total_size)
|
|
return __FAIL(ops, i);
|
|
|
|
if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
|
|
return __FAIL(ops, i);
|
|
|
|
sg_free_table(&table);
|
|
__free_pages(page, get_order(chunk_size));
|
|
}
|
|
|
|
if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
|
|
return __FAIL(ops, i);
|
|
|
|
free_io_pgtable_ops(ops);
|
|
}
|
|
|
|
suppress_map_failures = false;
|
|
return 0;
|
|
}
|
|
|
|
static int __init arm_lpae_do_selftests(void)
|
|
{
|
|
static const unsigned long pgsize[] = {
|
|
SZ_4K | SZ_2M | SZ_1G,
|
|
SZ_16K | SZ_32M,
|
|
SZ_64K | SZ_512M,
|
|
};
|
|
|
|
static const unsigned int ias[] = {
|
|
32, 36, 40, 42, 44, 48,
|
|
};
|
|
|
|
int i, j, pass = 0, fail = 0;
|
|
struct io_pgtable_cfg cfg = {
|
|
.tlb = &dummy_tlb_ops,
|
|
.oas = 48,
|
|
};
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
|
|
for (j = 0; j < ARRAY_SIZE(ias); ++j) {
|
|
cfg.pgsize_bitmap = pgsize[i];
|
|
cfg.ias = ias[j];
|
|
pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
|
|
pgsize[i], ias[j]);
|
|
if (arm_lpae_run_tests(&cfg))
|
|
fail++;
|
|
else
|
|
pass++;
|
|
}
|
|
}
|
|
|
|
pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
|
|
return fail ? -EFAULT : 0;
|
|
}
|
|
subsys_initcall(arm_lpae_do_selftests);
|
|
#endif
|