1 // SPDX-License-Identifier: GPL-2.0 AND MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include <kunit/test.h> 7 #include <kunit/visibility.h> 8 9 #include "tests/xe_kunit_helpers.h" 10 #include "tests/xe_pci_test.h" 11 #include "tests/xe_test.h" 12 13 #include "xe_device.h" 14 #include "xe_gt.h" 15 #include "xe_mocs.h" 16 #include "xe_pci.h" 17 #include "xe_pm.h" 18 19 struct live_mocs { 20 struct xe_mocs_info table; 21 }; 22 23 static int live_mocs_init(struct live_mocs *arg, struct xe_gt *gt) 24 { 25 unsigned int flags; 26 struct kunit *test = kunit_get_current_test(); 27 28 memset(arg, 0, sizeof(*arg)); 29 30 flags = get_mocs_settings(gt_to_xe(gt), &arg->table); 31 32 kunit_info(test, "gt %d", gt->info.id); 33 kunit_info(test, "gt type %d", gt->info.type); 34 kunit_info(test, "table size %d", arg->table.table_size); 35 kunit_info(test, "table uc_index %d", arg->table.uc_index); 36 kunit_info(test, "table num_mocs_regs %d", arg->table.num_mocs_regs); 37 38 return flags; 39 } 40 41 static void read_l3cc_table(struct xe_gt *gt, 42 const struct xe_mocs_info *info) 43 { 44 struct kunit *test = kunit_get_current_test(); 45 u32 l3cc, l3cc_expected; 46 unsigned int i; 47 u32 reg_val; 48 49 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL); 50 if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) 51 KUNIT_FAIL_AND_ABORT(test, "Forcewake Failed.\n"); 52 53 for (i = 0; i < info->num_mocs_regs; i++) { 54 if (!(i & 1)) { 55 if (regs_are_mcr(gt)) 56 reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_LNCFCMOCS(i >> 1)); 57 else 58 reg_val = xe_mmio_read32(>->mmio, XELP_LNCFCMOCS(i >> 1)); 59 60 mocs_dbg(gt, "reg_val=0x%x\n", reg_val); 61 } else { 62 /* Just reuse value read on previous iteration */ 63 reg_val >>= 16; 64 } 65 66 l3cc_expected = get_entry_l3cc(info, i); 67 l3cc = reg_val & 0xffff; 68 69 mocs_dbg(gt, "[%u] expected=0x%x actual=0x%x\n", 70 i, l3cc_expected, l3cc); 71 72 KUNIT_EXPECT_EQ_MSG(test, l3cc_expected, l3cc, 73 "l3cc idx=%u has incorrect val.\n", i); 74 } 75 } 76 77 static void read_mocs_table(struct xe_gt *gt, 78 const struct xe_mocs_info *info) 79 { 80 struct kunit *test = kunit_get_current_test(); 81 u32 mocs, mocs_expected; 82 unsigned int i; 83 u32 reg_val; 84 85 KUNIT_EXPECT_TRUE_MSG(test, info->unused_entries_index, 86 "Unused entries index should have been defined\n"); 87 88 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 89 KUNIT_ASSERT_NE_MSG(test, fw_ref.domains, 0, "Forcewake Failed.\n"); 90 91 for (i = 0; i < info->num_mocs_regs; i++) { 92 if (regs_are_mcr(gt)) 93 reg_val = xe_gt_mcr_unicast_read_any(gt, XEHP_GLOBAL_MOCS(i)); 94 else 95 reg_val = xe_mmio_read32(>->mmio, XELP_GLOBAL_MOCS(i)); 96 97 mocs_expected = get_entry_control(info, i); 98 mocs = reg_val; 99 100 mocs_dbg(gt, "[%u] expected=0x%x actual=0x%x\n", 101 i, mocs_expected, mocs); 102 103 KUNIT_EXPECT_EQ_MSG(test, mocs_expected, mocs, 104 "mocs reg 0x%x has incorrect val.\n", i); 105 } 106 } 107 108 static int mocs_kernel_test_run_device(struct xe_device *xe) 109 { 110 /* Basic check the system is configured with the expected mocs table */ 111 112 struct live_mocs mocs; 113 struct xe_gt *gt; 114 115 unsigned int flags; 116 int id; 117 118 guard(xe_pm_runtime)(xe); 119 for_each_gt(gt, xe, id) { 120 flags = live_mocs_init(&mocs, gt); 121 if (flags & HAS_GLOBAL_MOCS) 122 read_mocs_table(gt, &mocs.table); 123 if (flags & HAS_LNCF_MOCS) 124 read_l3cc_table(gt, &mocs.table); 125 } 126 127 return 0; 128 } 129 130 static void xe_live_mocs_kernel_kunit(struct kunit *test) 131 { 132 struct xe_device *xe = test->priv; 133 134 if (IS_SRIOV_VF(xe)) 135 kunit_skip(test, "this test is N/A for VF"); 136 137 mocs_kernel_test_run_device(xe); 138 } 139 140 static int mocs_reset_test_run_device(struct xe_device *xe) 141 { 142 /* Check the mocs setup is retained over GT reset */ 143 144 struct live_mocs mocs; 145 struct xe_gt *gt; 146 unsigned int flags; 147 int id; 148 struct kunit *test = kunit_get_current_test(); 149 150 guard(xe_pm_runtime)(xe); 151 for_each_gt(gt, xe, id) { 152 flags = live_mocs_init(&mocs, gt); 153 kunit_info(test, "mocs_reset_test before reset\n"); 154 if (flags & HAS_GLOBAL_MOCS) 155 read_mocs_table(gt, &mocs.table); 156 if (flags & HAS_LNCF_MOCS) 157 read_l3cc_table(gt, &mocs.table); 158 159 xe_gt_reset(gt); 160 161 kunit_info(test, "mocs_reset_test after reset\n"); 162 if (flags & HAS_GLOBAL_MOCS) 163 read_mocs_table(gt, &mocs.table); 164 if (flags & HAS_LNCF_MOCS) 165 read_l3cc_table(gt, &mocs.table); 166 } 167 168 return 0; 169 } 170 171 static void xe_live_mocs_reset_kunit(struct kunit *test) 172 { 173 struct xe_device *xe = test->priv; 174 175 if (IS_SRIOV_VF(xe)) 176 kunit_skip(test, "this test is N/A for VF"); 177 178 mocs_reset_test_run_device(xe); 179 } 180 181 static struct kunit_case xe_mocs_tests[] = { 182 KUNIT_CASE_PARAM(xe_live_mocs_kernel_kunit, xe_pci_live_device_gen_param), 183 KUNIT_CASE_PARAM(xe_live_mocs_reset_kunit, xe_pci_live_device_gen_param), 184 {} 185 }; 186 187 VISIBLE_IF_KUNIT 188 struct kunit_suite xe_mocs_test_suite = { 189 .name = "xe_mocs", 190 .test_cases = xe_mocs_tests, 191 .init = xe_kunit_helper_xe_device_live_test_init, 192 }; 193 EXPORT_SYMBOL_IF_KUNIT(xe_mocs_test_suite); 194