mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-26 06:04:14 +08:00
c37c792dec
We allocate only the first level of multilevel TCE tables for KVM already (alloc_userspace_copy==true), and the rest is allocated on demand. This is not enabled though for bare metal. This removes the KVM limitation (implicit, via the alloc_userspace_copy parameter) and always allocates just the first level. The on-demand allocation of missing levels is already implemented. As from now on DMA map might happen with disabled interrupts, this allocates TCEs with GFP_ATOMIC; otherwise lockdep reports errors 1]. In practice just a single page is allocated there so chances for failure are quite low. To save time when creating a new clean table, this skips non-allocated indirect TCE entries in pnv_tce_free just like we already do in the VFIO IOMMU TCE driver. This changes the default level number from 1 to 2 to reduce the amount of memory required for the default 32bit DMA window at the boot time. The default window size is up to 2GB which requires 4MB of TCEs which is unlikely to be used entirely or at all as most devices these days are 64bit capable so by switching to 2 levels by default we save 4032KB of RAM per a device. While at this, add __GFP_NOWARN to alloc_pages_node() as the userspace can trigger this path via VFIO, see the failure and try creating a table again with different parameters which might succeed. [1]: === BUG: sleeping function called from invalid context at mm/page_alloc.c:4596 in_atomic(): 1, irqs_disabled(): 1, pid: 1038, name: scsi_eh_1 2 locks held by scsi_eh_1/1038: #0: 000000005efd659a (&host->eh_mutex){+.+.}, at: ata_eh_acquire+0x34/0x80 #1: 0000000006cf56a6 (&(&host->lock)->rlock){....}, at: ata_exec_internal_sg+0xb0/0x5c0 irq event stamp: 500 hardirqs last enabled at (499): [<c000000000cb8a74>] _raw_spin_unlock_irqrestore+0x94/0xd0 hardirqs last disabled at (500): [<c000000000cb85c4>] _raw_spin_lock_irqsave+0x44/0x120 softirqs last enabled at (0): [<c000000000101120>] copy_process.isra.4.part.5+0x640/0x1a80 softirqs last disabled at (0): [<0000000000000000>] 0x0 CPU: 73 PID: 1038 Comm: scsi_eh_1 Not tainted 5.2.0-rc6-le_nv2_aikATfstn1-p1 #634 Call Trace: [c000003d064cef50] [c000000000c8e6c4] dump_stack+0xe8/0x164 (unreliable) [c000003d064cefa0] [c00000000014ed78] ___might_sleep+0x2f8/0x310 [c000003d064cf020] [c0000000003ca084] __alloc_pages_nodemask+0x2a4/0x1560 [c000003d064cf220] [c0000000000c2530] pnv_alloc_tce_level.isra.0+0x90/0x130 [c000003d064cf290] [c0000000000c2888] pnv_tce+0x128/0x3b0 [c000003d064cf360] [c0000000000c2c00] pnv_tce_build+0xb0/0xf0 [c000003d064cf3c0] [c0000000000bbd9c] pnv_ioda2_tce_build+0x3c/0xb0 [c000003d064cf400] [c00000000004cfe0] ppc_iommu_map_sg+0x210/0x550 [c000003d064cf510] [c00000000004b7a4] dma_iommu_map_sg+0x74/0xb0 [c000003d064cf530] [c000000000863944] ata_qc_issue+0x134/0x470 [c000003d064cf5b0] [c000000000863ec4] ata_exec_internal_sg+0x244/0x5c0 [c000003d064cf700] [c0000000008642d0] ata_exec_internal+0x90/0xe0 [c000003d064cf780] [c0000000008650ac] ata_dev_read_id+0x2ec/0x640 [c000003d064cf8d0] [c000000000878e28] ata_eh_recover+0x948/0x16d0 [c000003d064cfa10] [c00000000087d760] sata_pmp_error_handler+0x480/0xbf0 [c000003d064cfbc0] [c000000000884624] ahci_error_handler+0x74/0xe0 [c000003d064cfbf0] [c000000000879fa8] ata_scsi_port_error_handler+0x2d8/0x7c0 [c000003d064cfca0] [c00000000087a544] ata_scsi_error+0xb4/0x100 [c000003d064cfd00] [c000000000802450] scsi_error_handler+0x120/0x510 [c000003d064cfdb0] [c000000000140c48] kthread+0x1b8/0x1c0 [c000003d064cfe20] [c00000000000bd8c] ret_from_kernel_thread+0x5c/0x70 ata1: SATA link up 6.0 Gbps (SStatus 133 SControl 300) irq event stamp: 2305 ======================================================== hardirqs last enabled at (2305): [<c00000000000e4c8>] fast_exc_return_irq+0x28/0x34 hardirqs last disabled at (2303): [<c000000000cb9fd0>] __do_softirq+0x4a0/0x654 WARNING: possible irq lock inversion dependency detected 5.2.0-rc6-le_nv2_aikATfstn1-p1 #634 Tainted: G W softirqs last enabled at (2304): [<c000000000cba054>] __do_softirq+0x524/0x654 softirqs last disabled at (2297): [<c00000000010f278>] irq_exit+0x128/0x180 -------------------------------------------------------- swapper/0/0 just changed the state of lock: 0000000006cf56a6 (&(&host->lock)->rlock){-...}, at: ahci_single_level_irq_intr+0xac/0x120 but this lock took another, HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.} and interrupts could create inverse lock ordering between them. other info that might help us debug this: Possible interrupt unsafe locking scenario: CPU0 CPU1 ---- ---- lock(fs_reclaim); local_irq_disable(); lock(&(&host->lock)->rlock); lock(fs_reclaim); <Interrupt> lock(&(&host->lock)->rlock); *** DEADLOCK *** no locks held by swapper/0/0. the shortest dependencies between 2nd lock and 1st lock: -> (fs_reclaim){+.+.} ops: 167579 { HARDIRQ-ON-W at: lock_acquire+0xf8/0x2a0 fs_reclaim_acquire.part.23+0x44/0x60 kmem_cache_alloc_node_trace+0x80/0x590 alloc_desc+0x64/0x270 __irq_alloc_descs+0x2e4/0x3a0 irq_domain_alloc_descs+0xb0/0x150 irq_create_mapping+0x168/0x2c0 xics_smp_probe+0x2c/0x98 pnv_smp_probe+0x40/0x9c smp_prepare_cpus+0x524/0x6c4 kernel_init_freeable+0x1b4/0x650 kernel_init+0x2c/0x148 ret_from_kernel_thread+0x5c/0x70 SOFTIRQ-ON-W at: lock_acquire+0xf8/0x2a0 fs_reclaim_acquire.part.23+0x44/0x60 kmem_cache_alloc_node_trace+0x80/0x590 alloc_desc+0x64/0x270 __irq_alloc_descs+0x2e4/0x3a0 irq_domain_alloc_descs+0xb0/0x150 irq_create_mapping+0x168/0x2c0 xics_smp_probe+0x2c/0x98 pnv_smp_probe+0x40/0x9c smp_prepare_cpus+0x524/0x6c4 kernel_init_freeable+0x1b4/0x650 kernel_init+0x2c/0x148 ret_from_kernel_thread+0x5c/0x70 INITIAL USE at: lock_acquire+0xf8/0x2a0 fs_reclaim_acquire.part.23+0x44/0x60 kmem_cache_alloc_node_trace+0x80/0x590 alloc_desc+0x64/0x270 __irq_alloc_descs+0x2e4/0x3a0 irq_domain_alloc_descs+0xb0/0x150 irq_create_mapping+0x168/0x2c0 xics_smp_probe+0x2c/0x98 pnv_smp_probe+0x40/0x9c smp_prepare_cpus+0x524/0x6c4 kernel_init_freeable+0x1b4/0x650 kernel_init+0x2c/0x148 ret_from_kernel_thread+0x5c/0x70 } === Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru> Reviewed-by: Alistair Popple <alistair@popple.id.au> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20190718051139.74787-4-aik@ozlabs.ru
408 lines
9.9 KiB
C
408 lines
9.9 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* TCE helpers for IODA PCI/PCIe on PowerNV platforms
|
|
*
|
|
* Copyright 2018 IBM Corp.
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/iommu.h>
|
|
|
|
#include <asm/iommu.h>
|
|
#include <asm/tce.h>
|
|
#include "pci.h"
|
|
|
|
void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
|
|
void *tce_mem, u64 tce_size,
|
|
u64 dma_offset, unsigned int page_shift)
|
|
{
|
|
tbl->it_blocksize = 16;
|
|
tbl->it_base = (unsigned long)tce_mem;
|
|
tbl->it_page_shift = page_shift;
|
|
tbl->it_offset = dma_offset >> tbl->it_page_shift;
|
|
tbl->it_index = 0;
|
|
tbl->it_size = tce_size >> 3;
|
|
tbl->it_busno = 0;
|
|
tbl->it_type = TCE_PCI;
|
|
}
|
|
|
|
static __be64 *pnv_alloc_tce_level(int nid, unsigned int shift)
|
|
{
|
|
struct page *tce_mem = NULL;
|
|
__be64 *addr;
|
|
|
|
tce_mem = alloc_pages_node(nid, GFP_ATOMIC | __GFP_NOWARN,
|
|
shift - PAGE_SHIFT);
|
|
if (!tce_mem) {
|
|
pr_err("Failed to allocate a TCE memory, level shift=%d\n",
|
|
shift);
|
|
return NULL;
|
|
}
|
|
addr = page_address(tce_mem);
|
|
memset(addr, 0, 1UL << shift);
|
|
|
|
return addr;
|
|
}
|
|
|
|
static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
|
|
unsigned long size, unsigned int levels);
|
|
|
|
static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx, bool alloc)
|
|
{
|
|
__be64 *tmp = user ? tbl->it_userspace : (__be64 *) tbl->it_base;
|
|
int level = tbl->it_indirect_levels;
|
|
const long shift = ilog2(tbl->it_level_size);
|
|
unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
|
|
|
|
while (level) {
|
|
int n = (idx & mask) >> (level * shift);
|
|
unsigned long oldtce, tce = be64_to_cpu(READ_ONCE(tmp[n]));
|
|
|
|
if (!tce) {
|
|
__be64 *tmp2;
|
|
|
|
if (!alloc)
|
|
return NULL;
|
|
|
|
tmp2 = pnv_alloc_tce_level(tbl->it_nid,
|
|
ilog2(tbl->it_level_size) + 3);
|
|
if (!tmp2)
|
|
return NULL;
|
|
|
|
tce = __pa(tmp2) | TCE_PCI_READ | TCE_PCI_WRITE;
|
|
oldtce = be64_to_cpu(cmpxchg(&tmp[n], 0,
|
|
cpu_to_be64(tce)));
|
|
if (oldtce) {
|
|
pnv_pci_ioda2_table_do_free_pages(tmp2,
|
|
ilog2(tbl->it_level_size) + 3, 1);
|
|
tce = oldtce;
|
|
}
|
|
}
|
|
|
|
tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
|
|
idx &= ~mask;
|
|
mask >>= shift;
|
|
--level;
|
|
}
|
|
|
|
return tmp + idx;
|
|
}
|
|
|
|
int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
|
|
unsigned long uaddr, enum dma_data_direction direction,
|
|
unsigned long attrs)
|
|
{
|
|
u64 proto_tce = iommu_direction_to_tce_perm(direction);
|
|
u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
|
|
long i;
|
|
|
|
if (proto_tce & TCE_PCI_WRITE)
|
|
proto_tce |= TCE_PCI_READ;
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
unsigned long newtce = proto_tce |
|
|
((rpn + i) << tbl->it_page_shift);
|
|
unsigned long idx = index - tbl->it_offset + i;
|
|
|
|
*(pnv_tce(tbl, false, idx, true)) = cpu_to_be64(newtce);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_IOMMU_API
|
|
int pnv_tce_xchg(struct iommu_table *tbl, long index,
|
|
unsigned long *hpa, enum dma_data_direction *direction,
|
|
bool alloc)
|
|
{
|
|
u64 proto_tce = iommu_direction_to_tce_perm(*direction);
|
|
unsigned long newtce = *hpa | proto_tce, oldtce;
|
|
unsigned long idx = index - tbl->it_offset;
|
|
__be64 *ptce = NULL;
|
|
|
|
BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
|
|
|
|
if (*direction == DMA_NONE) {
|
|
ptce = pnv_tce(tbl, false, idx, false);
|
|
if (!ptce) {
|
|
*hpa = 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (!ptce) {
|
|
ptce = pnv_tce(tbl, false, idx, alloc);
|
|
if (!ptce)
|
|
return alloc ? H_HARDWARE : H_TOO_HARD;
|
|
}
|
|
|
|
if (newtce & TCE_PCI_WRITE)
|
|
newtce |= TCE_PCI_READ;
|
|
|
|
oldtce = be64_to_cpu(xchg(ptce, cpu_to_be64(newtce)));
|
|
*hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
|
|
*direction = iommu_tce_direction(oldtce);
|
|
|
|
return 0;
|
|
}
|
|
|
|
__be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index, bool alloc)
|
|
{
|
|
if (WARN_ON_ONCE(!tbl->it_userspace))
|
|
return NULL;
|
|
|
|
return pnv_tce(tbl, true, index - tbl->it_offset, alloc);
|
|
}
|
|
#endif
|
|
|
|
void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
|
|
{
|
|
long i;
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
unsigned long idx = index - tbl->it_offset + i;
|
|
__be64 *ptce = pnv_tce(tbl, false, idx, false);
|
|
|
|
if (ptce)
|
|
*ptce = cpu_to_be64(0);
|
|
else
|
|
/* Skip the rest of the level */
|
|
i |= tbl->it_level_size - 1;
|
|
}
|
|
}
|
|
|
|
unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
|
|
{
|
|
__be64 *ptce = pnv_tce(tbl, false, index - tbl->it_offset, false);
|
|
|
|
if (!ptce)
|
|
return 0;
|
|
|
|
return be64_to_cpu(*ptce);
|
|
}
|
|
|
|
static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
|
|
unsigned long size, unsigned int levels)
|
|
{
|
|
const unsigned long addr_ul = (unsigned long) addr &
|
|
~(TCE_PCI_READ | TCE_PCI_WRITE);
|
|
|
|
if (levels) {
|
|
long i;
|
|
u64 *tmp = (u64 *) addr_ul;
|
|
|
|
for (i = 0; i < size; ++i) {
|
|
unsigned long hpa = be64_to_cpu(tmp[i]);
|
|
|
|
if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
|
|
continue;
|
|
|
|
pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
|
|
levels - 1);
|
|
}
|
|
}
|
|
|
|
free_pages(addr_ul, get_order(size << 3));
|
|
}
|
|
|
|
void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
|
|
{
|
|
const unsigned long size = tbl->it_indirect_levels ?
|
|
tbl->it_level_size : tbl->it_size;
|
|
|
|
if (!tbl->it_size)
|
|
return;
|
|
|
|
pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
|
|
tbl->it_indirect_levels);
|
|
if (tbl->it_userspace) {
|
|
pnv_pci_ioda2_table_do_free_pages(tbl->it_userspace, size,
|
|
tbl->it_indirect_levels);
|
|
}
|
|
}
|
|
|
|
static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned int shift,
|
|
unsigned int levels, unsigned long limit,
|
|
unsigned long *current_offset, unsigned long *total_allocated)
|
|
{
|
|
__be64 *addr, *tmp;
|
|
unsigned long allocated = 1UL << shift;
|
|
unsigned int entries = 1UL << (shift - 3);
|
|
long i;
|
|
|
|
addr = pnv_alloc_tce_level(nid, shift);
|
|
*total_allocated += allocated;
|
|
|
|
--levels;
|
|
if (!levels) {
|
|
*current_offset += allocated;
|
|
return addr;
|
|
}
|
|
|
|
for (i = 0; i < entries; ++i) {
|
|
tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
|
|
levels, limit, current_offset, total_allocated);
|
|
if (!tmp)
|
|
break;
|
|
|
|
addr[i] = cpu_to_be64(__pa(tmp) |
|
|
TCE_PCI_READ | TCE_PCI_WRITE);
|
|
|
|
if (*current_offset >= limit)
|
|
break;
|
|
}
|
|
|
|
return addr;
|
|
}
|
|
|
|
long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
|
|
__u32 page_shift, __u64 window_size, __u32 levels,
|
|
bool alloc_userspace_copy, struct iommu_table *tbl)
|
|
{
|
|
void *addr, *uas = NULL;
|
|
unsigned long offset = 0, level_shift, total_allocated = 0;
|
|
unsigned long total_allocated_uas = 0;
|
|
const unsigned int window_shift = ilog2(window_size);
|
|
unsigned int entries_shift = window_shift - page_shift;
|
|
unsigned int table_shift = max_t(unsigned int, entries_shift + 3,
|
|
PAGE_SHIFT);
|
|
const unsigned long tce_table_size = 1UL << table_shift;
|
|
|
|
if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
|
|
return -EINVAL;
|
|
|
|
if (!is_power_of_2(window_size))
|
|
return -EINVAL;
|
|
|
|
/* Adjust direct table size from window_size and levels */
|
|
entries_shift = (entries_shift + levels - 1) / levels;
|
|
level_shift = entries_shift + 3;
|
|
level_shift = max_t(unsigned int, level_shift, PAGE_SHIFT);
|
|
|
|
if ((level_shift - 3) * levels + page_shift >= 55)
|
|
return -EINVAL;
|
|
|
|
/* Allocate TCE table */
|
|
addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
|
|
1, tce_table_size, &offset, &total_allocated);
|
|
|
|
/* addr==NULL means that the first level allocation failed */
|
|
if (!addr)
|
|
return -ENOMEM;
|
|
|
|
/*
|
|
* First level was allocated but some lower level failed as
|
|
* we did not allocate as much as we wanted,
|
|
* release partially allocated table.
|
|
*/
|
|
if (levels == 1 && offset < tce_table_size)
|
|
goto free_tces_exit;
|
|
|
|
/* Allocate userspace view of the TCE table */
|
|
if (alloc_userspace_copy) {
|
|
offset = 0;
|
|
uas = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
|
|
1, tce_table_size, &offset,
|
|
&total_allocated_uas);
|
|
if (!uas)
|
|
goto free_tces_exit;
|
|
if (levels == 1 && (offset < tce_table_size ||
|
|
total_allocated_uas != total_allocated))
|
|
goto free_uas_exit;
|
|
}
|
|
|
|
/* Setup linux iommu table */
|
|
pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
|
|
page_shift);
|
|
tbl->it_level_size = 1ULL << (level_shift - 3);
|
|
tbl->it_indirect_levels = levels - 1;
|
|
tbl->it_userspace = uas;
|
|
tbl->it_nid = nid;
|
|
|
|
pr_debug("Created TCE table: ws=%08llx ts=%lx @%08llx base=%lx uas=%p levels=%d/%d\n",
|
|
window_size, tce_table_size, bus_offset, tbl->it_base,
|
|
tbl->it_userspace, 1, levels);
|
|
|
|
return 0;
|
|
|
|
free_uas_exit:
|
|
pnv_pci_ioda2_table_do_free_pages(uas,
|
|
1ULL << (level_shift - 3), levels - 1);
|
|
free_tces_exit:
|
|
pnv_pci_ioda2_table_do_free_pages(addr,
|
|
1ULL << (level_shift - 3), levels - 1);
|
|
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static void pnv_iommu_table_group_link_free(struct rcu_head *head)
|
|
{
|
|
struct iommu_table_group_link *tgl = container_of(head,
|
|
struct iommu_table_group_link, rcu);
|
|
|
|
kfree(tgl);
|
|
}
|
|
|
|
void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
|
|
struct iommu_table_group *table_group)
|
|
{
|
|
long i;
|
|
bool found;
|
|
struct iommu_table_group_link *tgl;
|
|
|
|
if (!tbl || !table_group)
|
|
return;
|
|
|
|
/* Remove link to a group from table's list of attached groups */
|
|
found = false;
|
|
list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
|
|
if (tgl->table_group == table_group) {
|
|
list_del_rcu(&tgl->next);
|
|
call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (WARN_ON(!found))
|
|
return;
|
|
|
|
/* Clean a pointer to iommu_table in iommu_table_group::tables[] */
|
|
found = false;
|
|
for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
|
|
if (table_group->tables[i] == tbl) {
|
|
iommu_tce_table_put(tbl);
|
|
table_group->tables[i] = NULL;
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
WARN_ON(!found);
|
|
}
|
|
|
|
long pnv_pci_link_table_and_group(int node, int num,
|
|
struct iommu_table *tbl,
|
|
struct iommu_table_group *table_group)
|
|
{
|
|
struct iommu_table_group_link *tgl = NULL;
|
|
|
|
if (WARN_ON(!tbl || !table_group))
|
|
return -EINVAL;
|
|
|
|
tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
|
|
node);
|
|
if (!tgl)
|
|
return -ENOMEM;
|
|
|
|
tgl->table_group = table_group;
|
|
list_add_rcu(&tgl->next, &tbl->it_group_list);
|
|
|
|
table_group->tables[num] = iommu_tce_table_get(tbl);
|
|
|
|
return 0;
|
|
}
|