2018-07-14 15:46:54 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2021-06-10 10:00:54 +08:00
|
|
|
/*
|
2018-07-14 15:46:54 +08:00
|
|
|
* intel-pasid.c - PASID idr, table and entry manipulation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* Author: Lu Baolu <baolu.lu@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "DMAR: " fmt
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
#include <linux/bitops.h>
|
2018-12-10 09:59:04 +08:00
|
|
|
#include <linux/cpufeature.h>
|
2018-07-14 15:46:54 +08:00
|
|
|
#include <linux/dmar.h>
|
|
|
|
#include <linux/iommu.h>
|
|
|
|
#include <linux/memory.h>
|
2018-07-14 15:46:59 +08:00
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/pci-ats.h>
|
2018-07-14 15:46:54 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
2022-07-12 08:08:50 +08:00
|
|
|
#include "iommu.h"
|
2020-07-24 09:49:25 +08:00
|
|
|
#include "pasid.h"
|
2024-04-13 00:25:12 +00:00
|
|
|
#include "../iommu-pages.h"
|
2018-07-14 15:46:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Intel IOMMU system wide PASID name space:
|
|
|
|
*/
|
|
|
|
u32 intel_pasid_max_id = PASID_MAX;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Per device pasid table management:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a pasid table for @dev. It should be called in a
|
|
|
|
* single-thread context.
|
|
|
|
*/
|
|
|
|
int intel_pasid_alloc_table(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
struct pasid_table *pasid_table;
|
2024-04-13 00:25:12 +00:00
|
|
|
struct pasid_dir_entry *dir;
|
2020-09-15 09:30:05 -07:00
|
|
|
u32 max_pasid = 0;
|
2022-06-25 21:34:30 +08:00
|
|
|
int order, size;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
might_sleep();
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2022-10-17 16:02:13 -07:00
|
|
|
if (WARN_ON(!info || !dev_is_pci(dev)))
|
|
|
|
return -ENODEV;
|
|
|
|
if (WARN_ON(info->pasid_table))
|
|
|
|
return -EEXIST;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
|
2018-07-14 15:46:59 +08:00
|
|
|
if (!pasid_table)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
if (info->pasid_supported)
|
2020-09-15 09:30:05 -07:00
|
|
|
max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
|
2018-12-10 09:58:56 +08:00
|
|
|
intel_pasid_max_id);
|
|
|
|
|
|
|
|
size = max_pasid >> (PASID_PDE_SHIFT - 3);
|
|
|
|
order = size ? get_order(size) : 0;
|
2025-04-08 13:54:11 -03:00
|
|
|
dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL,
|
|
|
|
1 << (order + PAGE_SHIFT));
|
2024-04-13 00:25:12 +00:00
|
|
|
if (!dir) {
|
2019-04-30 09:30:04 +02:00
|
|
|
kfree(pasid_table);
|
2018-07-14 15:46:59 +08:00
|
|
|
return -ENOMEM;
|
2019-04-30 09:30:04 +02:00
|
|
|
}
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2024-04-13 00:25:12 +00:00
|
|
|
pasid_table->table = dir;
|
2018-12-10 09:58:56 +08:00
|
|
|
pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
|
2022-06-25 21:34:30 +08:00
|
|
|
info->pasid_table = pasid_table;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2023-02-16 21:08:15 +08:00
|
|
|
if (!ecap_coherent(info->iommu->ecap))
|
2023-08-09 20:48:04 +08:00
|
|
|
clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
|
2023-02-16 21:08:15 +08:00
|
|
|
|
2018-07-14 15:46:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intel_pasid_free_table(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
struct pasid_table *pasid_table;
|
2018-12-10 09:58:56 +08:00
|
|
|
struct pasid_dir_entry *dir;
|
|
|
|
struct pasid_entry *table;
|
|
|
|
int i, max_pde;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2018-12-10 09:58:56 +08:00
|
|
|
if (!info || !dev_is_pci(dev) || !info->pasid_table)
|
2018-07-14 15:46:59 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
pasid_table = info->pasid_table;
|
2022-06-25 21:34:30 +08:00
|
|
|
info->pasid_table = NULL;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
/* Free scalable mode PASID directory tables: */
|
|
|
|
dir = pasid_table->table;
|
|
|
|
max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
|
|
|
|
for (i = 0; i < max_pde; i++) {
|
|
|
|
table = get_pasid_table_from_pde(&dir[i]);
|
2025-04-08 13:53:54 -03:00
|
|
|
iommu_free_pages(table);
|
2018-12-10 09:58:56 +08:00
|
|
|
}
|
|
|
|
|
2025-04-08 13:53:53 -03:00
|
|
|
iommu_free_pages(pasid_table->table);
|
2018-07-14 15:46:59 +08:00
|
|
|
kfree(pasid_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pasid_table *intel_pasid_get_table(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2018-07-14 15:46:59 +08:00
|
|
|
if (!info)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return info->pasid_table;
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:06:00 +08:00
|
|
|
static int intel_pasid_get_dev_max_id(struct device *dev)
|
2018-07-14 15:46:59 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2018-07-14 15:46:59 +08:00
|
|
|
if (!info || !info->pasid_table)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return info->pasid_table->max_pasid;
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:06:00 +08:00
|
|
|
static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
|
2018-07-14 15:46:59 +08:00
|
|
|
{
|
2018-12-10 09:58:56 +08:00
|
|
|
struct device_domain_info *info;
|
2018-07-14 15:46:59 +08:00
|
|
|
struct pasid_table *pasid_table;
|
2018-12-10 09:58:56 +08:00
|
|
|
struct pasid_dir_entry *dir;
|
2018-07-14 15:46:59 +08:00
|
|
|
struct pasid_entry *entries;
|
2018-12-10 09:58:56 +08:00
|
|
|
int dir_index, index;
|
2018-07-14 15:46:59 +08:00
|
|
|
|
|
|
|
pasid_table = intel_pasid_get_table(dev);
|
2020-09-15 09:30:05 -07:00
|
|
|
if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
|
2018-07-14 15:46:59 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
dir = pasid_table->table;
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2018-12-10 09:58:56 +08:00
|
|
|
dir_index = pasid >> PASID_PDE_SHIFT;
|
|
|
|
index = pasid & PASID_PTE_MASK;
|
|
|
|
|
2021-03-20 10:09:16 +08:00
|
|
|
retry:
|
2018-12-10 09:58:56 +08:00
|
|
|
entries = get_pasid_table_from_pde(&dir[dir_index]);
|
|
|
|
if (!entries) {
|
2024-05-22 10:26:48 +02:00
|
|
|
u64 tmp;
|
|
|
|
|
2025-04-08 13:54:09 -03:00
|
|
|
entries = iommu_alloc_pages_node_sz(info->iommu->node,
|
|
|
|
GFP_ATOMIC, SZ_4K);
|
2021-03-20 10:09:16 +08:00
|
|
|
if (!entries)
|
2018-12-10 09:58:56 +08:00
|
|
|
return NULL;
|
|
|
|
|
2021-03-20 10:09:16 +08:00
|
|
|
/*
|
|
|
|
* The pasid directory table entry won't be freed after
|
|
|
|
* allocation. No worry about the race with free and
|
|
|
|
* clear. However, this entry might be populated by others
|
|
|
|
* while we are preparing it. Use theirs with a retry.
|
|
|
|
*/
|
2024-05-22 10:26:48 +02:00
|
|
|
tmp = 0ULL;
|
|
|
|
if (!try_cmpxchg64(&dir[dir_index].val, &tmp,
|
|
|
|
(u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
|
2025-04-08 13:53:54 -03:00
|
|
|
iommu_free_pages(entries);
|
2021-03-20 10:09:16 +08:00
|
|
|
goto retry;
|
|
|
|
}
|
2023-02-16 21:08:15 +08:00
|
|
|
if (!ecap_coherent(info->iommu->ecap)) {
|
|
|
|
clflush_cache_range(entries, VTD_PAGE_SIZE);
|
|
|
|
clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
|
|
|
|
}
|
2018-12-10 09:58:56 +08:00
|
|
|
}
|
2018-07-14 15:46:59 +08:00
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
return &entries[index];
|
2018-07-14 15:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interfaces for PASID table entry manipulation:
|
|
|
|
*/
|
2020-05-16 14:20:57 +08:00
|
|
|
static void
|
2020-09-15 09:30:05 -07:00
|
|
|
intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
|
2018-07-14 15:46:59 +08:00
|
|
|
{
|
|
|
|
struct pasid_entry *pe;
|
|
|
|
|
|
|
|
pe = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (WARN_ON(!pe))
|
|
|
|
return;
|
|
|
|
|
2020-05-16 14:20:57 +08:00
|
|
|
if (fault_ignore && pasid_pte_is_present(pe))
|
|
|
|
pasid_clear_entry_with_fpd(pe);
|
|
|
|
else
|
|
|
|
pasid_clear_entry(pe);
|
2018-07-14 15:46:59 +08:00
|
|
|
}
|
2018-12-10 09:59:00 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
|
2020-09-15 09:30:05 -07:00
|
|
|
u16 did, u32 pasid)
|
2018-12-10 09:59:00 +08:00
|
|
|
{
|
|
|
|
struct qi_desc desc;
|
|
|
|
|
2020-05-16 14:20:48 +08:00
|
|
|
desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
|
|
|
|
QI_PC_PASID(pasid) | QI_PC_TYPE;
|
2018-12-10 09:59:00 +08:00
|
|
|
desc.qw1 = 0;
|
|
|
|
desc.qw2 = 0;
|
|
|
|
desc.qw3 = 0;
|
|
|
|
|
2020-05-16 14:20:55 +08:00
|
|
|
qi_submit_sync(iommu, &desc, 1, 0);
|
2018-12-10 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
|
2020-09-15 09:30:05 -07:00
|
|
|
struct device *dev, u32 pasid)
|
2018-12-10 09:59:00 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
u16 sid, qdep, pfsid;
|
|
|
|
|
2022-03-01 10:01:52 +08:00
|
|
|
info = dev_iommu_priv_get(dev);
|
2018-12-10 09:59:00 +08:00
|
|
|
if (!info || !info->ats_enabled)
|
|
|
|
return;
|
|
|
|
|
iommu/vt-d: Don't issue ATS Invalidation request when device is disconnected
For those endpoint devices connect to system via hotplug capable ports,
users could request a hot reset to the device by flapping device's link
through setting the slot's link control register, as pciehp_ist() DLLSC
interrupt sequence response, pciehp will unload the device driver and
then power it off. thus cause an IOMMU device-TLB invalidation (Intel
VT-d spec, or ATS Invalidation in PCIe spec r6.1) request for non-existence
target device to be sent and deadly loop to retry that request after ITE
fault triggered in interrupt context.
That would cause following continuous hard lockup warning and system hang
[ 4211.433662] pcieport 0000:17:01.0: pciehp: Slot(108): Link Down
[ 4211.433664] pcieport 0000:17:01.0: pciehp: Slot(108): Card not present
[ 4223.822591] NMI watchdog: Watchdog detected hard LOCKUP on cpu 144
[ 4223.822622] CPU: 144 PID: 1422 Comm: irq/57-pciehp Kdump: loaded Tainted: G S
OE kernel version xxxx
[ 4223.822623] Hardware name: vendorname xxxx 666-106,
BIOS 01.01.02.03.01 05/15/2023
[ 4223.822623] RIP: 0010:qi_submit_sync+0x2c0/0x490
[ 4223.822624] Code: 48 be 00 00 00 00 00 08 00 00 49 85 74 24 20 0f 95 c1 48 8b
57 10 83 c1 04 83 3c 1a 03 0f 84 a2 01 00 00 49 8b 04 24 8b 70 34 <40> f6 c6 1
0 74 17 49 8b 04 24 8b 80 80 00 00 00 89 c2 d3 fa 41 39
[ 4223.822624] RSP: 0018:ffffc4f074f0bbb8 EFLAGS: 00000093
[ 4223.822625] RAX: ffffc4f040059000 RBX: 0000000000000014 RCX: 0000000000000005
[ 4223.822625] RDX: ffff9f3841315800 RSI: 0000000000000000 RDI: ffff9f38401a8340
[ 4223.822625] RBP: ffff9f38401a8340 R08: ffffc4f074f0bc00 R09: 0000000000000000
[ 4223.822626] R10: 0000000000000010 R11: 0000000000000018 R12: ffff9f384005e200
[ 4223.822626] R13: 0000000000000004 R14: 0000000000000046 R15: 0000000000000004
[ 4223.822626] FS: 0000000000000000(0000) GS:ffffa237ae400000(0000)
knlGS:0000000000000000
[ 4223.822627] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 4223.822627] CR2: 00007ffe86515d80 CR3: 000002fd3000a001 CR4: 0000000000770ee0
[ 4223.822627] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 4223.822628] DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400
[ 4223.822628] PKRU: 55555554
[ 4223.822628] Call Trace:
[ 4223.822628] qi_flush_dev_iotlb+0xb1/0xd0
[ 4223.822628] __dmar_remove_one_dev_info+0x224/0x250
[ 4223.822629] dmar_remove_one_dev_info+0x3e/0x50
[ 4223.822629] intel_iommu_release_device+0x1f/0x30
[ 4223.822629] iommu_release_device+0x33/0x60
[ 4223.822629] iommu_bus_notifier+0x7f/0x90
[ 4223.822630] blocking_notifier_call_chain+0x60/0x90
[ 4223.822630] device_del+0x2e5/0x420
[ 4223.822630] pci_remove_bus_device+0x70/0x110
[ 4223.822630] pciehp_unconfigure_device+0x7c/0x130
[ 4223.822631] pciehp_disable_slot+0x6b/0x100
[ 4223.822631] pciehp_handle_presence_or_link_change+0xd8/0x320
[ 4223.822631] pciehp_ist+0x176/0x180
[ 4223.822631] ? irq_finalize_oneshot.part.50+0x110/0x110
[ 4223.822632] irq_thread_fn+0x19/0x50
[ 4223.822632] irq_thread+0x104/0x190
[ 4223.822632] ? irq_forced_thread_fn+0x90/0x90
[ 4223.822632] ? irq_thread_check_affinity+0xe0/0xe0
[ 4223.822633] kthread+0x114/0x130
[ 4223.822633] ? __kthread_cancel_work+0x40/0x40
[ 4223.822633] ret_from_fork+0x1f/0x30
[ 4223.822633] Kernel panic - not syncing: Hard LOCKUP
[ 4223.822634] CPU: 144 PID: 1422 Comm: irq/57-pciehp Kdump: loaded Tainted: G S
OE kernel version xxxx
[ 4223.822634] Hardware name: vendorname xxxx 666-106,
BIOS 01.01.02.03.01 05/15/2023
[ 4223.822634] Call Trace:
[ 4223.822634] <NMI>
[ 4223.822635] dump_stack+0x6d/0x88
[ 4223.822635] panic+0x101/0x2d0
[ 4223.822635] ? ret_from_fork+0x11/0x30
[ 4223.822635] nmi_panic.cold.14+0xc/0xc
[ 4223.822636] watchdog_overflow_callback.cold.8+0x6d/0x81
[ 4223.822636] __perf_event_overflow+0x4f/0xf0
[ 4223.822636] handle_pmi_common+0x1ef/0x290
[ 4223.822636] ? __set_pte_vaddr+0x28/0x40
[ 4223.822637] ? flush_tlb_one_kernel+0xa/0x20
[ 4223.822637] ? __native_set_fixmap+0x24/0x30
[ 4223.822637] ? ghes_copy_tofrom_phys+0x70/0x100
[ 4223.822637] ? __ghes_peek_estatus.isra.16+0x49/0xa0
[ 4223.822637] intel_pmu_handle_irq+0xba/0x2b0
[ 4223.822638] perf_event_nmi_handler+0x24/0x40
[ 4223.822638] nmi_handle+0x4d/0xf0
[ 4223.822638] default_do_nmi+0x49/0x100
[ 4223.822638] exc_nmi+0x134/0x180
[ 4223.822639] end_repeat_nmi+0x16/0x67
[ 4223.822639] RIP: 0010:qi_submit_sync+0x2c0/0x490
[ 4223.822639] Code: 48 be 00 00 00 00 00 08 00 00 49 85 74 24 20 0f 95 c1 48 8b
57 10 83 c1 04 83 3c 1a 03 0f 84 a2 01 00 00 49 8b 04 24 8b 70 34 <40> f6 c6 10
74 17 49 8b 04 24 8b 80 80 00 00 00 89 c2 d3 fa 41 39
[ 4223.822640] RSP: 0018:ffffc4f074f0bbb8 EFLAGS: 00000093
[ 4223.822640] RAX: ffffc4f040059000 RBX: 0000000000000014 RCX: 0000000000000005
[ 4223.822640] RDX: ffff9f3841315800 RSI: 0000000000000000 RDI: ffff9f38401a8340
[ 4223.822641] RBP: ffff9f38401a8340 R08: ffffc4f074f0bc00 R09: 0000000000000000
[ 4223.822641] R10: 0000000000000010 R11: 0000000000000018 R12: ffff9f384005e200
[ 4223.822641] R13: 0000000000000004 R14: 0000000000000046 R15: 0000000000000004
[ 4223.822641] ? qi_submit_sync+0x2c0/0x490
[ 4223.822642] ? qi_submit_sync+0x2c0/0x490
[ 4223.822642] </NMI>
[ 4223.822642] qi_flush_dev_iotlb+0xb1/0xd0
[ 4223.822642] __dmar_remove_one_dev_info+0x224/0x250
[ 4223.822643] dmar_remove_one_dev_info+0x3e/0x50
[ 4223.822643] intel_iommu_release_device+0x1f/0x30
[ 4223.822643] iommu_release_device+0x33/0x60
[ 4223.822643] iommu_bus_notifier+0x7f/0x90
[ 4223.822644] blocking_notifier_call_chain+0x60/0x90
[ 4223.822644] device_del+0x2e5/0x420
[ 4223.822644] pci_remove_bus_device+0x70/0x110
[ 4223.822644] pciehp_unconfigure_device+0x7c/0x130
[ 4223.822644] pciehp_disable_slot+0x6b/0x100
[ 4223.822645] pciehp_handle_presence_or_link_change+0xd8/0x320
[ 4223.822645] pciehp_ist+0x176/0x180
[ 4223.822645] ? irq_finalize_oneshot.part.50+0x110/0x110
[ 4223.822645] irq_thread_fn+0x19/0x50
[ 4223.822646] irq_thread+0x104/0x190
[ 4223.822646] ? irq_forced_thread_fn+0x90/0x90
[ 4223.822646] ? irq_thread_check_affinity+0xe0/0xe0
[ 4223.822646] kthread+0x114/0x130
[ 4223.822647] ? __kthread_cancel_work+0x40/0x40
[ 4223.822647] ret_from_fork+0x1f/0x30
[ 4223.822647] Kernel Offset: 0x6400000 from 0xffffffff81000000 (relocation
range: 0xffffffff80000000-0xffffffffbfffffff)
Such issue could be triggered by all kinds of regular surprise removal
hotplug operation. like:
1. pull EP(endpoint device) out directly.
2. turn off EP's power.
3. bring the link down.
etc.
this patch aims to work for regular safe removal and surprise removal
unplug. these hot unplug handling process could be optimized for fix the
ATS Invalidation hang issue by calling pci_dev_is_disconnected() in
function devtlb_invalidation_with_pasid() to check target device state to
avoid sending meaningless ATS Invalidation request to iommu when device is
gone. (see IMPLEMENTATION NOTE in PCIe spec r6.1 section 10.3.1)
For safe removal, device wouldn't be removed until the whole software
handling process is done, it wouldn't trigger the hard lock up issue
caused by too long ATS Invalidation timeout wait. In safe removal path,
device state isn't set to pci_channel_io_perm_failure in
pciehp_unconfigure_device() by checking 'presence' parameter, calling
pci_dev_is_disconnected() in devtlb_invalidation_with_pasid() will return
false there, wouldn't break the function.
For surprise removal, device state is set to pci_channel_io_perm_failure in
pciehp_unconfigure_device(), means device is already gone (disconnected)
call pci_dev_is_disconnected() in devtlb_invalidation_with_pasid() will
return true to break the function not to send ATS Invalidation request to
the disconnected device blindly, thus avoid to trigger further ITE fault,
and ITE fault will block all invalidation request to be handled.
furthermore retry the timeout request could trigger hard lockup.
safe removal (present) & surprise removal (not present)
pciehp_ist()
pciehp_handle_presence_or_link_change()
pciehp_disable_slot()
remove_board()
pciehp_unconfigure_device(presence) {
if (!presence)
pci_walk_bus(parent, pci_dev_set_disconnected, NULL);
}
this patch works for regular safe removal and surprise removal of ATS
capable endpoint on PCIe switch downstream ports.
Fixes: 6f7db75e1c46 ("iommu/vt-d: Add second level page table interface")
Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>
Tested-by: Haorong Ye <yehaorong@bytedance.com>
Signed-off-by: Ethan Zhao <haifeng.zhao@linux.intel.com>
Link: https://lore.kernel.org/r/20240301080727.3529832-3-haifeng.zhao@linux.intel.com
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2024-03-05 20:21:15 +08:00
|
|
|
if (pci_dev_is_disconnected(to_pci_dev(dev)))
|
|
|
|
return;
|
|
|
|
|
2024-11-04 09:40:28 +08:00
|
|
|
sid = PCI_DEVID(info->bus, info->devfn);
|
2018-12-10 09:59:00 +08:00
|
|
|
qdep = info->ats_qdep;
|
|
|
|
pfsid = info->pfsid;
|
|
|
|
|
2020-07-24 09:49:16 +08:00
|
|
|
/*
|
|
|
|
* When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
|
|
|
|
* devTLB flush w/o PASID should be used. For non-zero PASID under
|
|
|
|
* SVA usage, device could do DMA with multiple PASIDs. It is more
|
|
|
|
* efficient to flush devTLB specific to the PASID.
|
|
|
|
*/
|
2023-08-09 20:47:54 +08:00
|
|
|
if (pasid == IOMMU_NO_PASID)
|
2020-07-24 09:49:16 +08:00
|
|
|
qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
|
|
|
|
else
|
|
|
|
qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
|
2018-12-10 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 14:20:57 +08:00
|
|
|
void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
|
2020-09-15 09:30:05 -07:00
|
|
|
u32 pasid, bool fault_ignore)
|
2018-12-10 09:59:00 +08:00
|
|
|
{
|
|
|
|
struct pasid_entry *pte;
|
2021-08-17 20:43:21 +08:00
|
|
|
u16 did, pgtt;
|
2018-12-10 09:59:00 +08:00
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_lock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
2025-01-07 10:17:45 +08:00
|
|
|
if (WARN_ON(!pte)) {
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
2021-03-20 10:54:15 +08:00
|
|
|
return;
|
2022-07-12 08:08:58 +08:00
|
|
|
}
|
2021-03-20 10:54:15 +08:00
|
|
|
|
2025-01-07 10:17:45 +08:00
|
|
|
if (!pasid_pte_is_present(pte)) {
|
|
|
|
if (!pasid_pte_is_fault_disabled(pte)) {
|
|
|
|
WARN_ON(READ_ONCE(pte->val[0]) != 0);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When a PASID is used for SVA by a device, it's possible
|
|
|
|
* that the pasid entry is non-present with the Fault
|
|
|
|
* Processing Disabled bit set. Clear the pasid entry and
|
|
|
|
* drain the PRQ for the PASID before return.
|
|
|
|
*/
|
|
|
|
pasid_clear_entry(pte);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
did = pasid_get_domain_id(pte);
|
2021-08-17 20:43:21 +08:00
|
|
|
pgtt = pasid_pte_get_pgtt(pte);
|
2020-05-16 14:20:57 +08:00
|
|
|
intel_pasid_clear_entry(dev, pasid, fault_ignore);
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
|
|
|
|
if (!ecap_coherent(iommu->ecap))
|
|
|
|
clflush_cache_range(pte, sizeof(*pte));
|
|
|
|
|
|
|
|
pasid_cache_invalidation_with_pasid(iommu, did, pasid);
|
2021-08-17 20:43:21 +08:00
|
|
|
|
|
|
|
if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
|
|
|
|
qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
|
|
|
|
else
|
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
|
2018-12-10 09:59:00 +08:00
|
|
|
|
2024-09-02 10:27:20 +08:00
|
|
|
devtlb_invalidation_with_pasid(iommu, dev, pasid);
|
2024-12-13 09:17:52 +08:00
|
|
|
if (!fault_ignore)
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
2018-12-10 09:59:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 21:48:52 +08:00
|
|
|
/*
|
|
|
|
* This function flushes cache for a newly setup pasid table entry.
|
|
|
|
* Caller of it should not modify the in-use pasid table entries.
|
|
|
|
*/
|
2020-01-02 08:18:06 +08:00
|
|
|
static void pasid_flush_caches(struct intel_iommu *iommu,
|
|
|
|
struct pasid_entry *pte,
|
2020-09-15 09:30:05 -07:00
|
|
|
u32 pasid, u16 did)
|
2020-01-02 08:18:06 +08:00
|
|
|
{
|
|
|
|
if (!ecap_coherent(iommu->ecap))
|
|
|
|
clflush_cache_range(pte, sizeof(*pte));
|
|
|
|
|
|
|
|
if (cap_caching_mode(iommu->cap)) {
|
|
|
|
pasid_cache_invalidation_with_pasid(iommu, did, pasid);
|
2021-01-14 16:50:21 +08:00
|
|
|
qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
|
2020-01-02 08:18:06 +08:00
|
|
|
} else {
|
|
|
|
iommu_flush_write_buffer(iommu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 10:13:53 +08:00
|
|
|
/*
|
|
|
|
* This function is supposed to be used after caller updates the fields
|
|
|
|
* except for the SSADE and P bit of a pasid table entry. It does the
|
|
|
|
* below:
|
|
|
|
* - Flush cacheline if needed
|
|
|
|
* - Flush the caches per Table 28 ”Guidance to Software for Invalidations“
|
|
|
|
* of VT-d spec 5.0.
|
|
|
|
*/
|
|
|
|
static void intel_pasid_flush_present(struct intel_iommu *iommu,
|
|
|
|
struct device *dev,
|
|
|
|
u32 pasid, u16 did,
|
|
|
|
struct pasid_entry *pte)
|
|
|
|
{
|
|
|
|
if (!ecap_coherent(iommu->ecap))
|
|
|
|
clflush_cache_range(pte, sizeof(*pte));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VT-d spec 5.0 table28 states guides for cache invalidation:
|
|
|
|
*
|
|
|
|
* - PASID-selective-within-Domain PASID-cache invalidation
|
|
|
|
* - PASID-selective PASID-based IOTLB invalidation
|
|
|
|
* - If (pasid is RID_PASID)
|
|
|
|
* - Global Device-TLB invalidation to affected functions
|
|
|
|
* Else
|
|
|
|
* - PASID-based Device-TLB invalidation (with S=1 and
|
|
|
|
* Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
|
|
|
|
*/
|
|
|
|
pasid_cache_invalidation_with_pasid(iommu, did, pasid);
|
|
|
|
qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
|
|
|
|
|
|
|
|
devtlb_invalidation_with_pasid(iommu, dev, pasid);
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:04 +08:00
|
|
|
/*
|
|
|
|
* Set up the scalable mode pasid table entry for first only
|
|
|
|
* translation type.
|
|
|
|
*/
|
2024-11-08 10:13:54 +08:00
|
|
|
static void pasid_pte_config_first_level(struct intel_iommu *iommu,
|
|
|
|
struct pasid_entry *pte,
|
2025-07-14 12:50:20 +08:00
|
|
|
phys_addr_t fsptptr, u16 did,
|
|
|
|
int flags)
|
2024-11-08 10:13:54 +08:00
|
|
|
{
|
|
|
|
lockdep_assert_held(&iommu->lock);
|
|
|
|
|
|
|
|
pasid_clear_entry(pte);
|
|
|
|
|
|
|
|
/* Setup the first level page table pointer: */
|
2025-07-14 12:50:20 +08:00
|
|
|
pasid_set_flptr(pte, fsptptr);
|
2024-11-08 10:13:54 +08:00
|
|
|
|
|
|
|
if (flags & PASID_FLAG_FL5LP)
|
|
|
|
pasid_set_flpm(pte, 1);
|
|
|
|
|
|
|
|
if (flags & PASID_FLAG_PAGE_SNOOP)
|
|
|
|
pasid_set_pgsnp(pte);
|
|
|
|
|
|
|
|
pasid_set_domain_id(pte, did);
|
|
|
|
pasid_set_address_width(pte, iommu->agaw);
|
|
|
|
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
|
|
|
|
|
|
|
|
/* Setup Present and PASID Granular Transfer Type: */
|
|
|
|
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
|
|
|
|
pasid_set_present(pte);
|
|
|
|
}
|
|
|
|
|
2025-07-14 12:50:20 +08:00
|
|
|
int intel_pasid_setup_first_level(struct intel_iommu *iommu, struct device *dev,
|
|
|
|
phys_addr_t fsptptr, u32 pasid, u16 did,
|
|
|
|
int flags)
|
2018-12-10 09:59:04 +08:00
|
|
|
{
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
|
|
|
|
if (!ecap_flts(iommu->ecap)) {
|
|
|
|
pr_err("No first level translation support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-26 21:15:27 +08:00
|
|
|
if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
|
2022-07-12 08:08:58 +08:00
|
|
|
pr_err("No 5-level paging support for first-level on %s\n",
|
|
|
|
iommu->name);
|
2018-12-10 09:59:04 +08:00
|
|
|
return -EINVAL;
|
2022-07-12 08:08:58 +08:00
|
|
|
}
|
2018-12-10 09:59:04 +08:00
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
2021-08-18 21:48:52 +08:00
|
|
|
return -EBUSY;
|
2022-07-12 08:08:58 +08:00
|
|
|
}
|
2021-08-18 21:48:52 +08:00
|
|
|
|
2025-07-14 12:50:20 +08:00
|
|
|
pasid_pte_config_first_level(iommu, pte, fsptptr, did, flags);
|
2018-12-10 09:59:04 +08:00
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
2020-01-02 08:18:06 +08:00
|
|
|
pasid_flush_caches(iommu, pte, pasid, did);
|
2018-12-10 09:59:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-08 10:13:55 +08:00
|
|
|
int intel_pasid_replace_first_level(struct intel_iommu *iommu,
|
2025-07-14 12:50:20 +08:00
|
|
|
struct device *dev, phys_addr_t fsptptr,
|
2024-11-08 10:13:55 +08:00
|
|
|
u32 pasid, u16 did, u16 old_did,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
struct pasid_entry *pte, new_pte;
|
|
|
|
|
|
|
|
if (!ecap_flts(iommu->ecap)) {
|
|
|
|
pr_err("No first level translation support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
|
|
|
|
pr_err("No 5-level paging support for first-level on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2025-07-14 12:50:20 +08:00
|
|
|
pasid_pte_config_first_level(iommu, &new_pte, fsptptr, did, flags);
|
2024-11-08 10:13:55 +08:00
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(old_did != pasid_get_domain_id(pte));
|
|
|
|
|
|
|
|
*pte = new_pte;
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
/*
|
|
|
|
* Set up the scalable mode pasid entry for second only translation type.
|
|
|
|
*/
|
2024-11-08 10:13:54 +08:00
|
|
|
static void pasid_pte_config_second_level(struct intel_iommu *iommu,
|
|
|
|
struct pasid_entry *pte,
|
|
|
|
u64 pgd_val, int agaw, u16 did,
|
|
|
|
bool dirty_tracking)
|
|
|
|
{
|
|
|
|
lockdep_assert_held(&iommu->lock);
|
|
|
|
|
|
|
|
pasid_clear_entry(pte);
|
|
|
|
pasid_set_domain_id(pte, did);
|
|
|
|
pasid_set_slptr(pte, pgd_val);
|
|
|
|
pasid_set_address_width(pte, agaw);
|
|
|
|
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
|
|
|
|
pasid_set_fault_enable(pte);
|
|
|
|
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
|
|
|
|
if (dirty_tracking)
|
|
|
|
pasid_set_ssade(pte);
|
|
|
|
|
|
|
|
pasid_set_present(pte);
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
int intel_pasid_setup_second_level(struct intel_iommu *iommu,
|
|
|
|
struct dmar_domain *domain,
|
2020-09-15 09:30:05 -07:00
|
|
|
struct device *dev, u32 pasid)
|
2018-12-10 09:59:00 +08:00
|
|
|
{
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
struct dma_pte *pgd;
|
|
|
|
u64 pgd_val;
|
|
|
|
u16 did;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If hardware advertises no support for second level
|
|
|
|
* translation, return directly.
|
|
|
|
*/
|
|
|
|
if (!ecap_slts(iommu->ecap)) {
|
|
|
|
pr_err("No second level translation support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pgd = domain->pgd;
|
|
|
|
pgd_val = virt_to_phys(pgd);
|
2022-07-12 08:09:05 +08:00
|
|
|
did = domain_id_iommu(domain, iommu);
|
2018-12-10 09:59:00 +08:00
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_lock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
if (pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
2021-08-18 21:48:52 +08:00
|
|
|
return -EBUSY;
|
2022-07-12 08:08:58 +08:00
|
|
|
}
|
2021-08-18 21:48:52 +08:00
|
|
|
|
2024-11-08 10:13:54 +08:00
|
|
|
pasid_pte_config_second_level(iommu, pte, pgd_val, domain->agaw,
|
|
|
|
did, domain->dirty_tracking);
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
2020-01-02 08:18:06 +08:00
|
|
|
pasid_flush_caches(iommu, pte, pasid, did);
|
2018-12-10 09:59:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-08 10:13:55 +08:00
|
|
|
int intel_pasid_replace_second_level(struct intel_iommu *iommu,
|
|
|
|
struct dmar_domain *domain,
|
|
|
|
struct device *dev, u16 old_did,
|
|
|
|
u32 pasid)
|
|
|
|
{
|
|
|
|
struct pasid_entry *pte, new_pte;
|
|
|
|
struct dma_pte *pgd;
|
|
|
|
u64 pgd_val;
|
|
|
|
u16 did;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If hardware advertises no support for second level
|
|
|
|
* translation, return directly.
|
|
|
|
*/
|
|
|
|
if (!ecap_slts(iommu->ecap)) {
|
|
|
|
pr_err("No second level translation support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pgd = domain->pgd;
|
|
|
|
pgd_val = virt_to_phys(pgd);
|
|
|
|
did = domain_id_iommu(domain, iommu);
|
|
|
|
|
|
|
|
pasid_pte_config_second_level(iommu, &new_pte, pgd_val,
|
|
|
|
domain->agaw, did,
|
|
|
|
domain->dirty_tracking);
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(old_did != pasid_get_domain_id(pte));
|
|
|
|
|
|
|
|
*pte = new_pte;
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:51:03 +01:00
|
|
|
/*
|
|
|
|
* Set up dirty tracking on a second only or nested translation type.
|
|
|
|
*/
|
|
|
|
int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
|
|
|
|
struct device *dev, u32 pasid,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
u16 did, pgtt;
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
dev_err_ratelimited(
|
|
|
|
dev, "Failed to get pasid entry of PASID %d\n", pasid);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2024-02-19 19:15:57 +08:00
|
|
|
did = pasid_get_domain_id(pte);
|
2023-10-24 14:51:03 +01:00
|
|
|
pgtt = pasid_pte_get_pgtt(pte);
|
|
|
|
if (pgtt != PASID_ENTRY_PGTT_SL_ONLY &&
|
|
|
|
pgtt != PASID_ENTRY_PGTT_NESTED) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
dev_err_ratelimited(
|
|
|
|
dev,
|
|
|
|
"Dirty tracking not supported on translation type %d\n",
|
|
|
|
pgtt);
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pasid_get_ssade(pte) == enabled) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
pasid_set_ssade(pte);
|
|
|
|
else
|
|
|
|
pasid_clear_ssade(pte);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
if (!ecap_coherent(iommu->ecap))
|
|
|
|
clflush_cache_range(pte, sizeof(*pte));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From VT-d spec table 25 "Guidance to Software for Invalidations":
|
|
|
|
*
|
|
|
|
* - PASID-selective-within-Domain PASID-cache invalidation
|
|
|
|
* If (PGTT=SS or Nested)
|
|
|
|
* - Domain-selective IOTLB invalidation
|
|
|
|
* Else
|
|
|
|
* - PASID-selective PASID-based IOTLB invalidation
|
|
|
|
* - If (pasid is RID_PASID)
|
|
|
|
* - Global Device-TLB invalidation to affected functions
|
|
|
|
* Else
|
|
|
|
* - PASID-based Device-TLB invalidation (with S=1 and
|
|
|
|
* Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
|
|
|
|
*/
|
|
|
|
pasid_cache_invalidation_with_pasid(iommu, did, pasid);
|
|
|
|
|
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
|
|
|
|
|
2024-09-02 10:27:20 +08:00
|
|
|
devtlb_invalidation_with_pasid(iommu, dev, pasid);
|
2023-10-24 14:51:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
/*
|
|
|
|
* Set up the scalable mode pasid entry for passthrough translation type.
|
|
|
|
*/
|
2024-11-08 10:13:54 +08:00
|
|
|
static void pasid_pte_config_pass_through(struct intel_iommu *iommu,
|
|
|
|
struct pasid_entry *pte, u16 did)
|
|
|
|
{
|
|
|
|
lockdep_assert_held(&iommu->lock);
|
|
|
|
|
|
|
|
pasid_clear_entry(pte);
|
|
|
|
pasid_set_domain_id(pte, did);
|
|
|
|
pasid_set_address_width(pte, iommu->agaw);
|
|
|
|
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
|
|
|
|
pasid_set_fault_enable(pte);
|
|
|
|
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
|
|
|
|
pasid_set_present(pte);
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
|
2020-09-15 09:30:05 -07:00
|
|
|
struct device *dev, u32 pasid)
|
2018-12-10 09:59:00 +08:00
|
|
|
{
|
|
|
|
u16 did = FLPT_DEFAULT_DID;
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_lock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
2018-12-10 09:59:00 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:08:58 +08:00
|
|
|
if (pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
2021-08-18 21:48:52 +08:00
|
|
|
return -EBUSY;
|
2022-07-12 08:08:58 +08:00
|
|
|
}
|
2021-08-18 21:48:52 +08:00
|
|
|
|
2024-11-08 10:13:54 +08:00
|
|
|
pasid_pte_config_pass_through(iommu, pte, did);
|
2022-07-12 08:08:58 +08:00
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
2020-01-02 08:18:06 +08:00
|
|
|
pasid_flush_caches(iommu, pte, pasid, did);
|
2018-12-10 09:59:00 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-10 10:34:05 +08:00
|
|
|
|
2024-11-08 10:13:55 +08:00
|
|
|
int intel_pasid_replace_pass_through(struct intel_iommu *iommu,
|
|
|
|
struct device *dev, u16 old_did,
|
|
|
|
u32 pasid)
|
|
|
|
{
|
|
|
|
struct pasid_entry *pte, new_pte;
|
|
|
|
u16 did = FLPT_DEFAULT_DID;
|
|
|
|
|
|
|
|
pasid_pte_config_pass_through(iommu, &new_pte, did);
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(old_did != pasid_get_domain_id(pte));
|
|
|
|
|
|
|
|
*pte = new_pte;
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-10 10:34:05 +08:00
|
|
|
/*
|
|
|
|
* Set the page snoop control for a pasid entry which has been set up.
|
|
|
|
*/
|
|
|
|
void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
|
|
|
|
struct device *dev, u32 pasid)
|
|
|
|
{
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
u16 did;
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pasid_set_pgsnp(pte);
|
|
|
|
did = pasid_get_domain_id(pte);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
2024-11-08 10:13:53 +08:00
|
|
|
intel_pasid_flush_present(iommu, dev, pasid, did, pte);
|
2022-05-10 10:34:05 +08:00
|
|
|
}
|
2023-10-25 21:42:12 -07:00
|
|
|
|
2024-11-08 10:13:54 +08:00
|
|
|
static void pasid_pte_config_nestd(struct intel_iommu *iommu,
|
|
|
|
struct pasid_entry *pte,
|
|
|
|
struct iommu_hwpt_vtd_s1 *s1_cfg,
|
|
|
|
struct dmar_domain *s2_domain,
|
|
|
|
u16 did)
|
|
|
|
{
|
|
|
|
struct dma_pte *pgd = s2_domain->pgd;
|
|
|
|
|
|
|
|
lockdep_assert_held(&iommu->lock);
|
|
|
|
|
|
|
|
pasid_clear_entry(pte);
|
|
|
|
|
|
|
|
if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL)
|
|
|
|
pasid_set_flpm(pte, 1);
|
|
|
|
|
|
|
|
pasid_set_flptr(pte, s1_cfg->pgtbl_addr);
|
|
|
|
|
|
|
|
if (s1_cfg->flags & IOMMU_VTD_S1_SRE) {
|
|
|
|
pasid_set_sre(pte);
|
|
|
|
if (s1_cfg->flags & IOMMU_VTD_S1_WPE)
|
|
|
|
pasid_set_wpe(pte);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s1_cfg->flags & IOMMU_VTD_S1_EAFE)
|
|
|
|
pasid_set_eafe(pte);
|
|
|
|
|
|
|
|
if (s2_domain->force_snooping)
|
|
|
|
pasid_set_pgsnp(pte);
|
|
|
|
|
|
|
|
pasid_set_slptr(pte, virt_to_phys(pgd));
|
|
|
|
pasid_set_fault_enable(pte);
|
|
|
|
pasid_set_domain_id(pte, did);
|
|
|
|
pasid_set_address_width(pte, s2_domain->agaw);
|
|
|
|
pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
|
|
|
|
if (s2_domain->dirty_tracking)
|
|
|
|
pasid_set_ssade(pte);
|
|
|
|
pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
|
|
|
|
pasid_set_present(pte);
|
|
|
|
}
|
|
|
|
|
2023-10-25 21:42:12 -07:00
|
|
|
/**
|
|
|
|
* intel_pasid_setup_nested() - Set up PASID entry for nested translation.
|
|
|
|
* @iommu: IOMMU which the device belong to
|
|
|
|
* @dev: Device to be set up for translation
|
|
|
|
* @pasid: PASID to be programmed in the device PASID table
|
|
|
|
* @domain: User stage-1 domain nested on a stage-2 domain
|
|
|
|
*
|
|
|
|
* This is used for nested translation. The input domain should be
|
|
|
|
* nested type and nested on a parent with 'is_nested_parent' flag
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
|
|
|
|
u32 pasid, struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
|
|
|
|
struct dmar_domain *s2_domain = domain->s2_domain;
|
|
|
|
u16 did = domain_id_iommu(domain, iommu);
|
|
|
|
struct pasid_entry *pte;
|
|
|
|
|
|
|
|
/* Address width should match the address width supported by hardware */
|
|
|
|
switch (s1_cfg->addr_width) {
|
|
|
|
case ADDR_WIDTH_4LEVEL:
|
|
|
|
break;
|
|
|
|
case ADDR_WIDTH_5LEVEL:
|
|
|
|
if (!cap_fl5lp_support(iommu->cap)) {
|
|
|
|
dev_err_ratelimited(dev,
|
|
|
|
"5-level paging not supported\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
|
|
|
|
s1_cfg->addr_width);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
|
|
|
|
pr_err_ratelimited("No supervisor request support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
|
|
|
|
pr_err_ratelimited("No extended access flag support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
if (pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2024-11-08 10:13:54 +08:00
|
|
|
pasid_pte_config_nestd(iommu, pte, s1_cfg, s2_domain, did);
|
2023-10-25 21:42:12 -07:00
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
pasid_flush_caches(iommu, pte, pasid, did);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-03-05 20:21:18 +08:00
|
|
|
|
2024-11-08 10:13:55 +08:00
|
|
|
int intel_pasid_replace_nested(struct intel_iommu *iommu,
|
|
|
|
struct device *dev, u32 pasid,
|
|
|
|
u16 old_did, struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
|
|
|
|
struct dmar_domain *s2_domain = domain->s2_domain;
|
|
|
|
u16 did = domain_id_iommu(domain, iommu);
|
|
|
|
struct pasid_entry *pte, new_pte;
|
|
|
|
|
|
|
|
/* Address width should match the address width supported by hardware */
|
|
|
|
switch (s1_cfg->addr_width) {
|
|
|
|
case ADDR_WIDTH_4LEVEL:
|
|
|
|
break;
|
|
|
|
case ADDR_WIDTH_5LEVEL:
|
|
|
|
if (!cap_fl5lp_support(iommu->cap)) {
|
|
|
|
dev_err_ratelimited(dev,
|
|
|
|
"5-level paging not supported\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
|
|
|
|
s1_cfg->addr_width);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
|
|
|
|
pr_err_ratelimited("No supervisor request support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
|
|
|
|
pr_err_ratelimited("No extended access flag support on %s\n",
|
|
|
|
iommu->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pasid_pte_config_nestd(iommu, &new_pte, s1_cfg, s2_domain, did);
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
pte = intel_pasid_get_entry(dev, pasid);
|
|
|
|
if (!pte) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pasid_pte_is_present(pte)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(old_did != pasid_get_domain_id(pte));
|
|
|
|
|
|
|
|
*pte = new_pte;
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
|
|
|
|
intel_iommu_drain_pasid_prq(dev, pasid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-05 20:21:18 +08:00
|
|
|
/*
|
|
|
|
* Interfaces to setup or teardown a pasid table to the scalable-mode
|
|
|
|
* context table entry:
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev_iommu_priv_get(dev);
|
|
|
|
struct intel_iommu *iommu = info->iommu;
|
|
|
|
struct context_entry *context;
|
2024-08-15 20:48:57 +08:00
|
|
|
u16 did;
|
2024-03-05 20:21:18 +08:00
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
context = iommu_context_addr(iommu, bus, devfn, false);
|
|
|
|
if (!context) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-15 20:48:57 +08:00
|
|
|
did = context_domain_id(context);
|
2024-03-05 20:21:18 +08:00
|
|
|
context_clear_entry(context);
|
|
|
|
__iommu_flush_cache(iommu, context, sizeof(*context));
|
|
|
|
spin_unlock(&iommu->lock);
|
2025-03-10 10:47:49 +08:00
|
|
|
intel_context_flush_no_pasid(info, context, did);
|
2024-03-05 20:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data)
|
|
|
|
{
|
|
|
|
struct device *dev = data;
|
|
|
|
|
|
|
|
if (dev == &pdev->dev)
|
|
|
|
device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intel_pasid_teardown_sm_context(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev_iommu_priv_get(dev);
|
|
|
|
|
|
|
|
if (!dev_is_pci(dev)) {
|
|
|
|
device_pasid_table_teardown(dev, info->bus, info->devfn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev);
|
|
|
|
}
|
2024-03-05 20:21:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the PASID directory size for scalable mode context entry.
|
|
|
|
* Value of X in the PDTS field of a scalable mode context entry
|
|
|
|
* indicates PASID directory with 2^(X + 7) entries.
|
|
|
|
*/
|
|
|
|
static unsigned long context_get_sm_pds(struct pasid_table *table)
|
|
|
|
{
|
|
|
|
unsigned long pds, max_pde;
|
|
|
|
|
|
|
|
max_pde = table->max_pasid >> PASID_PDE_SHIFT;
|
|
|
|
pds = find_first_bit(&max_pde, MAX_NR_PASID_BITS);
|
|
|
|
if (pds < 7)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pds - 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int context_entry_set_pasid_table(struct context_entry *context,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev_iommu_priv_get(dev);
|
|
|
|
struct pasid_table *table = info->pasid_table;
|
|
|
|
struct intel_iommu *iommu = info->iommu;
|
|
|
|
unsigned long pds;
|
|
|
|
|
|
|
|
context_clear_entry(context);
|
|
|
|
|
|
|
|
pds = context_get_sm_pds(table);
|
|
|
|
context->lo = (u64)virt_to_phys(table->table) | context_pdts(pds);
|
|
|
|
context_set_sm_rid2pasid(context, IOMMU_NO_PASID);
|
|
|
|
|
|
|
|
if (info->ats_supported)
|
|
|
|
context_set_sm_dte(context);
|
|
|
|
if (info->pasid_supported)
|
|
|
|
context_set_pasid(context);
|
2025-03-10 10:47:48 +08:00
|
|
|
if (info->pri_supported)
|
|
|
|
context_set_sm_pre(context);
|
2024-03-05 20:21:19 +08:00
|
|
|
|
|
|
|
context_set_fault_enable(context);
|
|
|
|
context_set_present(context);
|
|
|
|
__iommu_flush_cache(iommu, context, sizeof(*context));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int device_pasid_table_setup(struct device *dev, u8 bus, u8 devfn)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev_iommu_priv_get(dev);
|
|
|
|
struct intel_iommu *iommu = info->iommu;
|
|
|
|
struct context_entry *context;
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
context = iommu_context_addr(iommu, bus, devfn, true);
|
|
|
|
if (!context) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context_present(context) && !context_copied(iommu, bus, devfn)) {
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context_copied(iommu, bus, devfn)) {
|
|
|
|
context_clear_entry(context);
|
|
|
|
__iommu_flush_cache(iommu, context, sizeof(*context));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For kdump cases, old valid entries may be cached due to
|
|
|
|
* the in-flight DMA and copied pgtable, but there is no
|
|
|
|
* unmapping behaviour for them, thus we need explicit cache
|
|
|
|
* flushes for all affected domain IDs and PASIDs used in
|
|
|
|
* the copied PASID table. Given that we have no idea about
|
|
|
|
* which domain IDs and PASIDs were used in the copied tables,
|
|
|
|
* upgrade them to global PASID and IOTLB cache invalidation.
|
|
|
|
*/
|
|
|
|
iommu->flush.flush_context(iommu, 0,
|
|
|
|
PCI_DEVID(bus, devfn),
|
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
|
|
|
qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
|
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
|
|
|
|
devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, the device is supposed to finish reset at
|
|
|
|
* its driver probe stage, so no in-flight DMA will exist,
|
|
|
|
* and we don't need to worry anymore hereafter.
|
|
|
|
*/
|
|
|
|
clear_context_copied(iommu, bus, devfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
context_entry_set_pasid_table(context, dev);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's a non-present to present mapping. If hardware doesn't cache
|
|
|
|
* non-present entry we don't need to flush the caches. If it does
|
|
|
|
* cache non-present entries, then it does so in the special
|
|
|
|
* domain #0, which we have to flush:
|
|
|
|
*/
|
|
|
|
if (cap_caching_mode(iommu->cap)) {
|
|
|
|
iommu->flush.flush_context(iommu, 0,
|
|
|
|
PCI_DEVID(bus, devfn),
|
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pci_pasid_table_setup(struct pci_dev *pdev, u16 alias, void *data)
|
|
|
|
{
|
|
|
|
struct device *dev = data;
|
|
|
|
|
|
|
|
if (dev != &pdev->dev)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return device_pasid_table_setup(dev, PCI_BUS_NUM(alias), alias & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the device's PASID table to its context table entry.
|
|
|
|
*
|
|
|
|
* The PASID table is set to the context entries of both device itself
|
|
|
|
* and its alias requester ID for DMA.
|
|
|
|
*/
|
|
|
|
int intel_pasid_setup_sm_context(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev_iommu_priv_get(dev);
|
|
|
|
|
|
|
|
if (!dev_is_pci(dev))
|
|
|
|
return device_pasid_table_setup(dev, info->bus, info->devfn);
|
|
|
|
|
|
|
|
return pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_setup, dev);
|
|
|
|
}
|
2024-07-02 21:08:38 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Global Device-TLB invalidation following changes in a context entry which
|
|
|
|
* was present.
|
|
|
|
*/
|
|
|
|
static void __context_flush_dev_iotlb(struct device_domain_info *info)
|
|
|
|
{
|
|
|
|
if (!info->ats_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qi_flush_dev_iotlb(info->iommu, PCI_DEVID(info->bus, info->devfn),
|
|
|
|
info->pfsid, info->ats_qdep, 0, MAX_AGAW_PFN_WIDTH);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no guarantee that the device DMA is stopped when it reaches
|
|
|
|
* here. Therefore, always attempt the extra device TLB invalidation
|
|
|
|
* quirk. The impact on performance is acceptable since this is not a
|
|
|
|
* performance-critical path.
|
|
|
|
*/
|
|
|
|
quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, IOMMU_NO_PASID,
|
|
|
|
info->ats_qdep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache invalidations after change in a context table entry that was present
|
2025-03-10 10:47:49 +08:00
|
|
|
* according to the Spec 6.5.3.3 (Guidance to Software for Invalidations).
|
|
|
|
* This helper can only be used when IOMMU is working in the legacy mode or
|
|
|
|
* IOMMU is in scalable mode but all PASID table entries of the device are
|
|
|
|
* non-present.
|
2024-07-02 21:08:38 +08:00
|
|
|
*/
|
2025-03-10 10:47:49 +08:00
|
|
|
void intel_context_flush_no_pasid(struct device_domain_info *info,
|
|
|
|
struct context_entry *context, u16 did)
|
2024-07-02 21:08:38 +08:00
|
|
|
{
|
|
|
|
struct intel_iommu *iommu = info->iommu;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Device-selective context-cache invalidation. The Domain-ID field
|
|
|
|
* of the Context-cache Invalidate Descriptor is ignored by hardware
|
|
|
|
* when operating in scalable mode. Therefore the @did value doesn't
|
|
|
|
* matter in scalable mode.
|
|
|
|
*/
|
|
|
|
iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn),
|
|
|
|
DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For legacy mode:
|
|
|
|
* - Domain-selective IOTLB invalidation
|
|
|
|
* - Global Device-TLB invalidation to all affected functions
|
|
|
|
*/
|
|
|
|
if (!sm_supported(iommu)) {
|
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
|
|
|
|
__context_flush_dev_iotlb(info);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
__context_flush_dev_iotlb(info);
|
|
|
|
}
|