15448c155SJason Gunthorpe /* SPDX-License-Identifier: GPL-2.0-only */
25448c155SJason Gunthorpe /*
35448c155SJason Gunthorpe * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
45448c155SJason Gunthorpe *
55448c155SJason Gunthorpe * Intel VT-d Second Stange 5/4 level page table
65448c155SJason Gunthorpe *
75448c155SJason Gunthorpe * This is described in
85448c155SJason Gunthorpe * Section "3.7 Second-Stage Translation"
95448c155SJason Gunthorpe * Section "9.8 Second-Stage Paging Entries"
105448c155SJason Gunthorpe *
115448c155SJason Gunthorpe * Of the "Intel Virtualization Technology for Directed I/O Architecture
125448c155SJason Gunthorpe * Specification".
135448c155SJason Gunthorpe *
145448c155SJason Gunthorpe * The named levels in the spec map to the pts->level as:
155448c155SJason Gunthorpe * Table/SS-PTE - 0
165448c155SJason Gunthorpe * Directory/SS-PDE - 1
175448c155SJason Gunthorpe * Directory Ptr/SS-PDPTE - 2
185448c155SJason Gunthorpe * PML4/SS-PML4E - 3
195448c155SJason Gunthorpe * PML5/SS-PML5E - 4
205448c155SJason Gunthorpe */
215448c155SJason Gunthorpe #ifndef __GENERIC_PT_FMT_VTDSS_H
225448c155SJason Gunthorpe #define __GENERIC_PT_FMT_VTDSS_H
235448c155SJason Gunthorpe
245448c155SJason Gunthorpe #include "defs_vtdss.h"
255448c155SJason Gunthorpe #include "../pt_defs.h"
265448c155SJason Gunthorpe
275448c155SJason Gunthorpe #include <linux/bitfield.h>
285448c155SJason Gunthorpe #include <linux/container_of.h>
295448c155SJason Gunthorpe #include <linux/log2.h>
305448c155SJason Gunthorpe
315448c155SJason Gunthorpe enum {
325448c155SJason Gunthorpe PT_MAX_OUTPUT_ADDRESS_LG2 = 52,
335448c155SJason Gunthorpe PT_MAX_VA_ADDRESS_LG2 = 57,
345448c155SJason Gunthorpe PT_ITEM_WORD_SIZE = sizeof(u64),
355448c155SJason Gunthorpe PT_MAX_TOP_LEVEL = 4,
365448c155SJason Gunthorpe PT_GRANULE_LG2SZ = 12,
375448c155SJason Gunthorpe PT_TABLEMEM_LG2SZ = 12,
385448c155SJason Gunthorpe
395448c155SJason Gunthorpe /* SSPTPTR is 4k aligned and limited by HAW */
405448c155SJason Gunthorpe PT_TOP_PHYS_MASK = GENMASK_ULL(63, 12),
415448c155SJason Gunthorpe };
425448c155SJason Gunthorpe
435448c155SJason Gunthorpe /* Shared descriptor bits */
445448c155SJason Gunthorpe enum {
455448c155SJason Gunthorpe VTDSS_FMT_R = BIT(0),
465448c155SJason Gunthorpe VTDSS_FMT_W = BIT(1),
475448c155SJason Gunthorpe VTDSS_FMT_A = BIT(8),
485448c155SJason Gunthorpe VTDSS_FMT_D = BIT(9),
495448c155SJason Gunthorpe VTDSS_FMT_SNP = BIT(11),
505448c155SJason Gunthorpe VTDSS_FMT_OA = GENMASK_ULL(51, 12),
515448c155SJason Gunthorpe };
525448c155SJason Gunthorpe
535448c155SJason Gunthorpe /* PDPTE/PDE */
545448c155SJason Gunthorpe enum {
555448c155SJason Gunthorpe VTDSS_FMT_PS = BIT(7),
565448c155SJason Gunthorpe };
575448c155SJason Gunthorpe
585448c155SJason Gunthorpe #define common_to_vtdss_pt(common_ptr) \
595448c155SJason Gunthorpe container_of_const(common_ptr, struct pt_vtdss, common)
605448c155SJason Gunthorpe #define to_vtdss_pt(pts) common_to_vtdss_pt((pts)->range->common)
615448c155SJason Gunthorpe
vtdss_pt_table_pa(const struct pt_state * pts)625448c155SJason Gunthorpe static inline pt_oaddr_t vtdss_pt_table_pa(const struct pt_state *pts)
635448c155SJason Gunthorpe {
645448c155SJason Gunthorpe return oalog2_mul(FIELD_GET(VTDSS_FMT_OA, pts->entry),
655448c155SJason Gunthorpe PT_TABLEMEM_LG2SZ);
665448c155SJason Gunthorpe }
675448c155SJason Gunthorpe #define pt_table_pa vtdss_pt_table_pa
685448c155SJason Gunthorpe
vtdss_pt_entry_oa(const struct pt_state * pts)695448c155SJason Gunthorpe static inline pt_oaddr_t vtdss_pt_entry_oa(const struct pt_state *pts)
705448c155SJason Gunthorpe {
715448c155SJason Gunthorpe return oalog2_mul(FIELD_GET(VTDSS_FMT_OA, pts->entry),
725448c155SJason Gunthorpe PT_GRANULE_LG2SZ);
735448c155SJason Gunthorpe }
745448c155SJason Gunthorpe #define pt_entry_oa vtdss_pt_entry_oa
755448c155SJason Gunthorpe
vtdss_pt_can_have_leaf(const struct pt_state * pts)765448c155SJason Gunthorpe static inline bool vtdss_pt_can_have_leaf(const struct pt_state *pts)
775448c155SJason Gunthorpe {
785448c155SJason Gunthorpe return pts->level <= 2;
795448c155SJason Gunthorpe }
805448c155SJason Gunthorpe #define pt_can_have_leaf vtdss_pt_can_have_leaf
815448c155SJason Gunthorpe
vtdss_pt_num_items_lg2(const struct pt_state * pts)825448c155SJason Gunthorpe static inline unsigned int vtdss_pt_num_items_lg2(const struct pt_state *pts)
835448c155SJason Gunthorpe {
845448c155SJason Gunthorpe return PT_TABLEMEM_LG2SZ - ilog2(sizeof(u64));
855448c155SJason Gunthorpe }
865448c155SJason Gunthorpe #define pt_num_items_lg2 vtdss_pt_num_items_lg2
875448c155SJason Gunthorpe
vtdss_pt_load_entry_raw(struct pt_state * pts)885448c155SJason Gunthorpe static inline enum pt_entry_type vtdss_pt_load_entry_raw(struct pt_state *pts)
895448c155SJason Gunthorpe {
905448c155SJason Gunthorpe const u64 *tablep = pt_cur_table(pts, u64);
915448c155SJason Gunthorpe u64 entry;
925448c155SJason Gunthorpe
935448c155SJason Gunthorpe pts->entry = entry = READ_ONCE(tablep[pts->index]);
945448c155SJason Gunthorpe if (!entry)
955448c155SJason Gunthorpe return PT_ENTRY_EMPTY;
965448c155SJason Gunthorpe if (pts->level == 0 ||
975448c155SJason Gunthorpe (vtdss_pt_can_have_leaf(pts) && (pts->entry & VTDSS_FMT_PS)))
985448c155SJason Gunthorpe return PT_ENTRY_OA;
995448c155SJason Gunthorpe return PT_ENTRY_TABLE;
1005448c155SJason Gunthorpe }
1015448c155SJason Gunthorpe #define pt_load_entry_raw vtdss_pt_load_entry_raw
1025448c155SJason Gunthorpe
1035448c155SJason Gunthorpe static inline void
vtdss_pt_install_leaf_entry(struct pt_state * pts,pt_oaddr_t oa,unsigned int oasz_lg2,const struct pt_write_attrs * attrs)1045448c155SJason Gunthorpe vtdss_pt_install_leaf_entry(struct pt_state *pts, pt_oaddr_t oa,
1055448c155SJason Gunthorpe unsigned int oasz_lg2,
1065448c155SJason Gunthorpe const struct pt_write_attrs *attrs)
1075448c155SJason Gunthorpe {
1085448c155SJason Gunthorpe u64 *tablep = pt_cur_table(pts, u64);
1095448c155SJason Gunthorpe u64 entry;
1105448c155SJason Gunthorpe
1115448c155SJason Gunthorpe if (!pt_check_install_leaf_args(pts, oa, oasz_lg2))
1125448c155SJason Gunthorpe return;
1135448c155SJason Gunthorpe
1145448c155SJason Gunthorpe entry = FIELD_PREP(VTDSS_FMT_OA, log2_div(oa, PT_GRANULE_LG2SZ)) |
1155448c155SJason Gunthorpe attrs->descriptor_bits;
1165448c155SJason Gunthorpe if (pts->level != 0)
1175448c155SJason Gunthorpe entry |= VTDSS_FMT_PS;
1185448c155SJason Gunthorpe
1195448c155SJason Gunthorpe WRITE_ONCE(tablep[pts->index], entry);
1205448c155SJason Gunthorpe pts->entry = entry;
1215448c155SJason Gunthorpe }
1225448c155SJason Gunthorpe #define pt_install_leaf_entry vtdss_pt_install_leaf_entry
1235448c155SJason Gunthorpe
vtdss_pt_install_table(struct pt_state * pts,pt_oaddr_t table_pa,const struct pt_write_attrs * attrs)1245448c155SJason Gunthorpe static inline bool vtdss_pt_install_table(struct pt_state *pts,
1255448c155SJason Gunthorpe pt_oaddr_t table_pa,
1265448c155SJason Gunthorpe const struct pt_write_attrs *attrs)
1275448c155SJason Gunthorpe {
1285448c155SJason Gunthorpe u64 entry;
1295448c155SJason Gunthorpe
1305448c155SJason Gunthorpe entry = VTDSS_FMT_R | VTDSS_FMT_W |
1315448c155SJason Gunthorpe FIELD_PREP(VTDSS_FMT_OA, log2_div(table_pa, PT_GRANULE_LG2SZ));
1325448c155SJason Gunthorpe return pt_table_install64(pts, entry);
1335448c155SJason Gunthorpe }
1345448c155SJason Gunthorpe #define pt_install_table vtdss_pt_install_table
1355448c155SJason Gunthorpe
vtdss_pt_attr_from_entry(const struct pt_state * pts,struct pt_write_attrs * attrs)1365448c155SJason Gunthorpe static inline void vtdss_pt_attr_from_entry(const struct pt_state *pts,
1375448c155SJason Gunthorpe struct pt_write_attrs *attrs)
1385448c155SJason Gunthorpe {
1395448c155SJason Gunthorpe attrs->descriptor_bits = pts->entry &
1405448c155SJason Gunthorpe (VTDSS_FMT_R | VTDSS_FMT_W | VTDSS_FMT_SNP);
1415448c155SJason Gunthorpe }
1425448c155SJason Gunthorpe #define pt_attr_from_entry vtdss_pt_attr_from_entry
1435448c155SJason Gunthorpe
vtdss_pt_entry_is_write_dirty(const struct pt_state * pts)1445448c155SJason Gunthorpe static inline bool vtdss_pt_entry_is_write_dirty(const struct pt_state *pts)
1455448c155SJason Gunthorpe {
1465448c155SJason Gunthorpe u64 *tablep = pt_cur_table(pts, u64) + pts->index;
1475448c155SJason Gunthorpe
1485448c155SJason Gunthorpe return READ_ONCE(*tablep) & VTDSS_FMT_D;
1495448c155SJason Gunthorpe }
1505448c155SJason Gunthorpe #define pt_entry_is_write_dirty vtdss_pt_entry_is_write_dirty
1515448c155SJason Gunthorpe
vtdss_pt_entry_make_write_clean(struct pt_state * pts)1525448c155SJason Gunthorpe static inline void vtdss_pt_entry_make_write_clean(struct pt_state *pts)
1535448c155SJason Gunthorpe {
1545448c155SJason Gunthorpe u64 *tablep = pt_cur_table(pts, u64) + pts->index;
1555448c155SJason Gunthorpe
1565448c155SJason Gunthorpe WRITE_ONCE(*tablep, READ_ONCE(*tablep) & ~(u64)VTDSS_FMT_D);
1575448c155SJason Gunthorpe }
1585448c155SJason Gunthorpe #define pt_entry_make_write_clean vtdss_pt_entry_make_write_clean
1595448c155SJason Gunthorpe
vtdss_pt_entry_make_write_dirty(struct pt_state * pts)1605448c155SJason Gunthorpe static inline bool vtdss_pt_entry_make_write_dirty(struct pt_state *pts)
1615448c155SJason Gunthorpe {
1625448c155SJason Gunthorpe u64 *tablep = pt_cur_table(pts, u64) + pts->index;
1635448c155SJason Gunthorpe u64 new = pts->entry | VTDSS_FMT_D;
1645448c155SJason Gunthorpe
1655448c155SJason Gunthorpe return try_cmpxchg64(tablep, &pts->entry, new);
1665448c155SJason Gunthorpe }
1675448c155SJason Gunthorpe #define pt_entry_make_write_dirty vtdss_pt_entry_make_write_dirty
1685448c155SJason Gunthorpe
vtdss_pt_max_sw_bit(struct pt_common * common)1695448c155SJason Gunthorpe static inline unsigned int vtdss_pt_max_sw_bit(struct pt_common *common)
1705448c155SJason Gunthorpe {
1715448c155SJason Gunthorpe return 10;
1725448c155SJason Gunthorpe }
1735448c155SJason Gunthorpe #define pt_max_sw_bit vtdss_pt_max_sw_bit
1745448c155SJason Gunthorpe
vtdss_pt_sw_bit(unsigned int bitnr)1755448c155SJason Gunthorpe static inline u64 vtdss_pt_sw_bit(unsigned int bitnr)
1765448c155SJason Gunthorpe {
1775de863efSJason Gunthorpe if (__builtin_constant_p(bitnr) && bitnr > 10)
1785de863efSJason Gunthorpe BUILD_BUG();
1795de863efSJason Gunthorpe
1805448c155SJason Gunthorpe /* Bits marked Ignored in the specification */
1815448c155SJason Gunthorpe switch (bitnr) {
1825448c155SJason Gunthorpe case 0:
1835448c155SJason Gunthorpe return BIT(10);
1845448c155SJason Gunthorpe case 1 ... 9:
1855448c155SJason Gunthorpe return BIT_ULL((bitnr - 1) + 52);
1865448c155SJason Gunthorpe case 10:
1875448c155SJason Gunthorpe return BIT_ULL(63);
1885448c155SJason Gunthorpe /* Some bits in 9-3 are available in some entries */
1895448c155SJason Gunthorpe default:
1905448c155SJason Gunthorpe PT_WARN_ON(true);
1915448c155SJason Gunthorpe return 0;
1925448c155SJason Gunthorpe }
1935448c155SJason Gunthorpe }
1945448c155SJason Gunthorpe #define pt_sw_bit vtdss_pt_sw_bit
1955448c155SJason Gunthorpe
1965448c155SJason Gunthorpe /* --- iommu */
1975448c155SJason Gunthorpe #include <linux/generic_pt/iommu.h>
1985448c155SJason Gunthorpe #include <linux/iommu.h>
1995448c155SJason Gunthorpe
2005448c155SJason Gunthorpe #define pt_iommu_table pt_iommu_vtdss
2015448c155SJason Gunthorpe
2025448c155SJason Gunthorpe /* The common struct is in the per-format common struct */
common_from_iommu(struct pt_iommu * iommu_table)2035448c155SJason Gunthorpe static inline struct pt_common *common_from_iommu(struct pt_iommu *iommu_table)
2045448c155SJason Gunthorpe {
2055448c155SJason Gunthorpe return &container_of(iommu_table, struct pt_iommu_table, iommu)
2065448c155SJason Gunthorpe ->vtdss_pt.common;
2075448c155SJason Gunthorpe }
2085448c155SJason Gunthorpe
iommu_from_common(struct pt_common * common)2095448c155SJason Gunthorpe static inline struct pt_iommu *iommu_from_common(struct pt_common *common)
2105448c155SJason Gunthorpe {
2115448c155SJason Gunthorpe return &container_of(common, struct pt_iommu_table, vtdss_pt.common)
2125448c155SJason Gunthorpe ->iommu;
2135448c155SJason Gunthorpe }
2145448c155SJason Gunthorpe
vtdss_pt_iommu_set_prot(struct pt_common * common,struct pt_write_attrs * attrs,unsigned int iommu_prot)2155448c155SJason Gunthorpe static inline int vtdss_pt_iommu_set_prot(struct pt_common *common,
2165448c155SJason Gunthorpe struct pt_write_attrs *attrs,
2175448c155SJason Gunthorpe unsigned int iommu_prot)
2185448c155SJason Gunthorpe {
2195448c155SJason Gunthorpe u64 pte = 0;
2205448c155SJason Gunthorpe
2215448c155SJason Gunthorpe /*
2225448c155SJason Gunthorpe * VTDSS does not have a present bit, so we tell if any entry is present
2235448c155SJason Gunthorpe * by checking for R or W.
2245448c155SJason Gunthorpe */
2255448c155SJason Gunthorpe if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
2265448c155SJason Gunthorpe return -EINVAL;
2275448c155SJason Gunthorpe
2285448c155SJason Gunthorpe if (iommu_prot & IOMMU_READ)
2295448c155SJason Gunthorpe pte |= VTDSS_FMT_R;
2305448c155SJason Gunthorpe if (iommu_prot & IOMMU_WRITE)
2315448c155SJason Gunthorpe pte |= VTDSS_FMT_W;
2325448c155SJason Gunthorpe if (pt_feature(common, PT_FEAT_VTDSS_FORCE_COHERENCE))
2335448c155SJason Gunthorpe pte |= VTDSS_FMT_SNP;
2345448c155SJason Gunthorpe
2355448c155SJason Gunthorpe if (pt_feature(common, PT_FEAT_VTDSS_FORCE_WRITEABLE) &&
2365448c155SJason Gunthorpe !(iommu_prot & IOMMU_WRITE)) {
2375448c155SJason Gunthorpe pr_err_ratelimited(
2385448c155SJason Gunthorpe "Read-only mapping is disallowed on the domain which serves as the parent in a nested configuration, due to HW errata (ERRATA_772415_SPR17)\n");
2395448c155SJason Gunthorpe return -EINVAL;
2405448c155SJason Gunthorpe }
2415448c155SJason Gunthorpe
2425448c155SJason Gunthorpe attrs->descriptor_bits = pte;
2435448c155SJason Gunthorpe return 0;
2445448c155SJason Gunthorpe }
2455448c155SJason Gunthorpe #define pt_iommu_set_prot vtdss_pt_iommu_set_prot
2465448c155SJason Gunthorpe
vtdss_pt_iommu_fmt_init(struct pt_iommu_vtdss * iommu_table,const struct pt_iommu_vtdss_cfg * cfg)2475448c155SJason Gunthorpe static inline int vtdss_pt_iommu_fmt_init(struct pt_iommu_vtdss *iommu_table,
2485448c155SJason Gunthorpe const struct pt_iommu_vtdss_cfg *cfg)
2495448c155SJason Gunthorpe {
2505448c155SJason Gunthorpe struct pt_vtdss *table = &iommu_table->vtdss_pt;
2515448c155SJason Gunthorpe
252*d856f9d2SJason Gunthorpe if (cfg->top_level > 4 || cfg->top_level < 2)
2535448c155SJason Gunthorpe return -EOPNOTSUPP;
254*d856f9d2SJason Gunthorpe
255*d856f9d2SJason Gunthorpe pt_top_set_level(&table->common, cfg->top_level);
2565448c155SJason Gunthorpe return 0;
2575448c155SJason Gunthorpe }
2585448c155SJason Gunthorpe #define pt_iommu_fmt_init vtdss_pt_iommu_fmt_init
2595448c155SJason Gunthorpe
2605448c155SJason Gunthorpe static inline void
vtdss_pt_iommu_fmt_hw_info(struct pt_iommu_vtdss * table,const struct pt_range * top_range,struct pt_iommu_vtdss_hw_info * info)2615448c155SJason Gunthorpe vtdss_pt_iommu_fmt_hw_info(struct pt_iommu_vtdss *table,
2625448c155SJason Gunthorpe const struct pt_range *top_range,
2635448c155SJason Gunthorpe struct pt_iommu_vtdss_hw_info *info)
2645448c155SJason Gunthorpe {
2655448c155SJason Gunthorpe info->ssptptr = virt_to_phys(top_range->top_table);
2665448c155SJason Gunthorpe PT_WARN_ON(info->ssptptr & ~PT_TOP_PHYS_MASK);
2675448c155SJason Gunthorpe /*
2685448c155SJason Gunthorpe * top_level = 2 = 3 level table aw=1
2695448c155SJason Gunthorpe * top_level = 3 = 4 level table aw=2
2705448c155SJason Gunthorpe * top_level = 4 = 5 level table aw=3
2715448c155SJason Gunthorpe */
2725448c155SJason Gunthorpe info->aw = top_range->top_level - 1;
2735448c155SJason Gunthorpe }
2745448c155SJason Gunthorpe #define pt_iommu_fmt_hw_info vtdss_pt_iommu_fmt_hw_info
2755448c155SJason Gunthorpe
2765448c155SJason Gunthorpe #if defined(GENERIC_PT_KUNIT)
2775448c155SJason Gunthorpe static const struct pt_iommu_vtdss_cfg vtdss_kunit_fmt_cfgs[] = {
278*d856f9d2SJason Gunthorpe [0] = { .common.hw_max_vasz_lg2 = 39, .top_level = 2},
279*d856f9d2SJason Gunthorpe [1] = { .common.hw_max_vasz_lg2 = 48, .top_level = 3},
280*d856f9d2SJason Gunthorpe [2] = { .common.hw_max_vasz_lg2 = 57, .top_level = 4},
2815448c155SJason Gunthorpe };
2825448c155SJason Gunthorpe #define kunit_fmt_cfgs vtdss_kunit_fmt_cfgs
2835448c155SJason Gunthorpe enum { KUNIT_FMT_FEATURES = BIT(PT_FEAT_VTDSS_FORCE_WRITEABLE) };
2845448c155SJason Gunthorpe #endif
2855448c155SJason Gunthorpe #endif
286