Skip to content

Commit 57a9a11

Browse files
eaugerChristoffer Dall
authored andcommitted
KVM: arm64: vgic-its: Device table save/restore
This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used. For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines. Those functions will be implemented in a subsequent patch. On restore, devices are re-allocated and their itt are re-built. Signed-off-by: Eric Auger <eric.auger@redhat.com> Reviewed-by: Christoffer Dall <cdall@linaro.org>
1 parent dceff70 commit 57a9a11

File tree

2 files changed

+199
-5
lines changed

2 files changed

+199
-5
lines changed

virt/kvm/arm/vgic/vgic-its.c

Lines changed: 189 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/interrupt.h>
2424
#include <linux/list.h>
2525
#include <linux/uaccess.h>
26+
#include <linux/list_sort.h>
2627

2728
#include <linux/irqchip/arm-gic-v3.h>
2829

@@ -1735,7 +1736,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
17351736
return ret;
17361737
}
17371738

1738-
u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
1739+
static u32 compute_next_devid_offset(struct list_head *h,
1740+
struct its_device *dev)
17391741
{
17401742
struct its_device *next;
17411743
u32 next_offset;
@@ -1789,8 +1791,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
17891791
* Return: < 0 on error, 0 if last element was identified, 1 otherwise
17901792
* (the last element may not be found on second level tables)
17911793
*/
1792-
int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
1793-
int start_id, entry_fn_t fn, void *opaque)
1794+
static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
1795+
int start_id, entry_fn_t fn, void *opaque)
17941796
{
17951797
void *entry = kzalloc(esz, GFP_KERNEL);
17961798
struct kvm *kvm = its->dev->kvm;
@@ -1825,13 +1827,171 @@ int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz,
18251827
return ret;
18261828
}
18271829

1830+
static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
1831+
{
1832+
return -ENXIO;
1833+
}
1834+
1835+
static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
1836+
{
1837+
return -ENXIO;
1838+
}
1839+
1840+
/**
1841+
* vgic_its_save_dte - Save a device table entry at a given GPA
1842+
*
1843+
* @its: ITS handle
1844+
* @dev: ITS device
1845+
* @ptr: GPA
1846+
*/
1847+
static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
1848+
gpa_t ptr, int dte_esz)
1849+
{
1850+
struct kvm *kvm = its->dev->kvm;
1851+
u64 val, itt_addr_field;
1852+
u32 next_offset;
1853+
1854+
itt_addr_field = dev->itt_addr >> 8;
1855+
next_offset = compute_next_devid_offset(&its->device_list, dev);
1856+
val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
1857+
((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
1858+
(itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
1859+
(dev->num_eventid_bits - 1));
1860+
val = cpu_to_le64(val);
1861+
return kvm_write_guest(kvm, ptr, &val, dte_esz);
1862+
}
1863+
1864+
/**
1865+
* vgic_its_restore_dte - restore a device table entry
1866+
*
1867+
* @its: its handle
1868+
* @id: device id the DTE corresponds to
1869+
* @ptr: kernel VA where the 8 byte DTE is located
1870+
* @opaque: unused
1871+
*
1872+
* Return: < 0 on error, 0 if the dte is the last one, id offset to the
1873+
* next dte otherwise
1874+
*/
1875+
static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
1876+
void *ptr, void *opaque)
1877+
{
1878+
struct its_device *dev;
1879+
gpa_t itt_addr;
1880+
u8 num_eventid_bits;
1881+
u64 entry = *(u64 *)ptr;
1882+
bool valid;
1883+
u32 offset;
1884+
int ret;
1885+
1886+
entry = le64_to_cpu(entry);
1887+
1888+
valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
1889+
num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
1890+
itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
1891+
>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
1892+
1893+
if (!valid)
1894+
return 1;
1895+
1896+
/* dte entry is valid */
1897+
offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
1898+
1899+
dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
1900+
if (IS_ERR(dev))
1901+
return PTR_ERR(dev);
1902+
1903+
ret = vgic_its_restore_itt(its, dev);
1904+
if (ret)
1905+
return ret;
1906+
1907+
return offset;
1908+
}
1909+
1910+
static int vgic_its_device_cmp(void *priv, struct list_head *a,
1911+
struct list_head *b)
1912+
{
1913+
struct its_device *deva = container_of(a, struct its_device, dev_list);
1914+
struct its_device *devb = container_of(b, struct its_device, dev_list);
1915+
1916+
if (deva->device_id < devb->device_id)
1917+
return -1;
1918+
else
1919+
return 1;
1920+
}
1921+
18281922
/**
18291923
* vgic_its_save_device_tables - Save the device table and all ITT
18301924
* into guest RAM
1925+
*
1926+
* L1/L2 handling is hidden by vgic_its_check_id() helper which directly
1927+
* returns the GPA of the device entry
18311928
*/
18321929
static int vgic_its_save_device_tables(struct vgic_its *its)
18331930
{
1834-
return -ENXIO;
1931+
const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1932+
struct its_device *dev;
1933+
int dte_esz = abi->dte_esz;
1934+
u64 baser;
1935+
1936+
baser = its->baser_device_table;
1937+
1938+
list_sort(NULL, &its->device_list, vgic_its_device_cmp);
1939+
1940+
list_for_each_entry(dev, &its->device_list, dev_list) {
1941+
int ret;
1942+
gpa_t eaddr;
1943+
1944+
if (!vgic_its_check_id(its, baser,
1945+
dev->device_id, &eaddr))
1946+
return -EINVAL;
1947+
1948+
ret = vgic_its_save_itt(its, dev);
1949+
if (ret)
1950+
return ret;
1951+
1952+
ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
1953+
if (ret)
1954+
return ret;
1955+
}
1956+
return 0;
1957+
}
1958+
1959+
/**
1960+
* handle_l1_dte - callback used for L1 device table entries (2 stage case)
1961+
*
1962+
* @its: its handle
1963+
* @id: index of the entry in the L1 table
1964+
* @addr: kernel VA
1965+
* @opaque: unused
1966+
*
1967+
* L1 table entries are scanned by steps of 1 entry
1968+
* Return < 0 if error, 0 if last dte was found when scanning the L2
1969+
* table, +1 otherwise (meaning next L1 entry must be scanned)
1970+
*/
1971+
static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
1972+
void *opaque)
1973+
{
1974+
const struct vgic_its_abi *abi = vgic_its_get_abi(its);
1975+
int l2_start_id = id * (SZ_64K / abi->dte_esz);
1976+
u64 entry = *(u64 *)addr;
1977+
int dte_esz = abi->dte_esz;
1978+
gpa_t gpa;
1979+
int ret;
1980+
1981+
entry = le64_to_cpu(entry);
1982+
1983+
if (!(entry & KVM_ITS_L1E_VALID_MASK))
1984+
return 1;
1985+
1986+
gpa = entry & KVM_ITS_L1E_ADDR_MASK;
1987+
1988+
ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
1989+
l2_start_id, vgic_its_restore_dte, NULL);
1990+
1991+
if (ret <= 0)
1992+
return ret;
1993+
1994+
return 1;
18351995
}
18361996

18371997
/**
@@ -1840,7 +2000,31 @@ static int vgic_its_save_device_tables(struct vgic_its *its)
18402000
*/
18412001
static int vgic_its_restore_device_tables(struct vgic_its *its)
18422002
{
1843-
return -ENXIO;
2003+
const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2004+
u64 baser = its->baser_device_table;
2005+
int l1_esz, ret;
2006+
int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2007+
gpa_t l1_gpa;
2008+
2009+
if (!(baser & GITS_BASER_VALID))
2010+
return 0;
2011+
2012+
l1_gpa = BASER_ADDRESS(baser);
2013+
2014+
if (baser & GITS_BASER_INDIRECT) {
2015+
l1_esz = GITS_LVL1_ENTRY_SIZE;
2016+
ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2017+
handle_l1_dte, NULL);
2018+
} else {
2019+
l1_esz = abi->dte_esz;
2020+
ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2021+
vgic_its_restore_dte, NULL);
2022+
}
2023+
2024+
if (ret > 0)
2025+
ret = -EINVAL;
2026+
2027+
return ret;
18442028
}
18452029

18462030
static int vgic_its_save_cte(struct vgic_its *its,

virt/kvm/arm/vgic/vgic.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@
8181
#define KVM_ITS_CTE_VALID_MASK BIT_ULL(63)
8282
#define KVM_ITS_CTE_RDBASE_SHIFT 16
8383
#define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0)
84+
#define KVM_ITS_DTE_VALID_SHIFT 63
85+
#define KVM_ITS_DTE_VALID_MASK BIT_ULL(63)
86+
#define KVM_ITS_DTE_NEXT_SHIFT 49
87+
#define KVM_ITS_DTE_NEXT_MASK GENMASK_ULL(62, 49)
88+
#define KVM_ITS_DTE_ITTADDR_SHIFT 5
89+
#define KVM_ITS_DTE_ITTADDR_MASK GENMASK_ULL(48, 5)
90+
#define KVM_ITS_DTE_SIZE_MASK GENMASK_ULL(4, 0)
91+
#define KVM_ITS_L1E_VALID_MASK BIT_ULL(63)
92+
/* we only support 64 kB translation table page size */
93+
#define KVM_ITS_L1E_ADDR_MASK GENMASK_ULL(51, 16)
8494

8595
static inline bool irq_is_pending(struct vgic_irq *irq)
8696
{

0 commit comments

Comments
 (0)