16f33a92bSJithu Joseph // SPDX-License-Identifier: GPL-2.0-only 26f33a92bSJithu Joseph /* Copyright(c) 2022 Intel Corporation. */ 36f33a92bSJithu Joseph 46f33a92bSJithu Joseph #include <linux/cpu.h> 56f33a92bSJithu Joseph #include <linux/delay.h> 66f33a92bSJithu Joseph #include <linux/fs.h> 76f33a92bSJithu Joseph #include <linux/semaphore.h> 86f33a92bSJithu Joseph #include <linux/slab.h> 96f33a92bSJithu Joseph 106f33a92bSJithu Joseph #include "ifs.h" 116f33a92bSJithu Joseph 126f33a92bSJithu Joseph /* 136f33a92bSJithu Joseph * Protects against simultaneous tests on multiple cores, or 146f33a92bSJithu Joseph * reloading can file while a test is in progress 156f33a92bSJithu Joseph */ 16a4c30fa4SJithu Joseph static DEFINE_SEMAPHORE(ifs_sem); 176f33a92bSJithu Joseph 186f33a92bSJithu Joseph /* 196f33a92bSJithu Joseph * The sysfs interface to check additional details of last test 206f33a92bSJithu Joseph * cat /sys/devices/system/platform/ifs/details 216f33a92bSJithu Joseph */ 226f33a92bSJithu Joseph static ssize_t details_show(struct device *dev, 236f33a92bSJithu Joseph struct device_attribute *attr, 246f33a92bSJithu Joseph char *buf) 256f33a92bSJithu Joseph { 266f33a92bSJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 276f33a92bSJithu Joseph 286f33a92bSJithu Joseph return sysfs_emit(buf, "%#llx\n", ifsd->scan_details); 296f33a92bSJithu Joseph } 306f33a92bSJithu Joseph 316f33a92bSJithu Joseph static DEVICE_ATTR_RO(details); 326f33a92bSJithu Joseph 336f33a92bSJithu Joseph static const char * const status_msg[] = { 346f33a92bSJithu Joseph [SCAN_NOT_TESTED] = "untested", 356f33a92bSJithu Joseph [SCAN_TEST_PASS] = "pass", 366f33a92bSJithu Joseph [SCAN_TEST_FAIL] = "fail" 376f33a92bSJithu Joseph }; 386f33a92bSJithu Joseph 396f33a92bSJithu Joseph /* 406f33a92bSJithu Joseph * The sysfs interface to check the test status: 416f33a92bSJithu Joseph * To check the status of last test 426f33a92bSJithu Joseph * cat /sys/devices/platform/ifs/status 436f33a92bSJithu Joseph */ 446f33a92bSJithu Joseph static ssize_t status_show(struct device *dev, 456f33a92bSJithu Joseph struct device_attribute *attr, 466f33a92bSJithu Joseph char *buf) 476f33a92bSJithu Joseph { 486f33a92bSJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 496f33a92bSJithu Joseph 506f33a92bSJithu Joseph return sysfs_emit(buf, "%s\n", status_msg[ifsd->status]); 516f33a92bSJithu Joseph } 526f33a92bSJithu Joseph 536f33a92bSJithu Joseph static DEVICE_ATTR_RO(status); 546f33a92bSJithu Joseph 556f33a92bSJithu Joseph /* 566f33a92bSJithu Joseph * The sysfs interface for single core testing 576f33a92bSJithu Joseph * To start test, for example, cpu5 586f33a92bSJithu Joseph * echo 5 > /sys/devices/platform/ifs/run_test 596f33a92bSJithu Joseph * To check the result: 606f33a92bSJithu Joseph * cat /sys/devices/platform/ifs/result 616f33a92bSJithu Joseph * The sibling core gets tested at the same time. 626f33a92bSJithu Joseph */ 636f33a92bSJithu Joseph static ssize_t run_test_store(struct device *dev, 646f33a92bSJithu Joseph struct device_attribute *attr, 656f33a92bSJithu Joseph const char *buf, size_t count) 666f33a92bSJithu Joseph { 676f33a92bSJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 686f33a92bSJithu Joseph unsigned int cpu; 696f33a92bSJithu Joseph int rc; 706f33a92bSJithu Joseph 716f33a92bSJithu Joseph rc = kstrtouint(buf, 0, &cpu); 726f33a92bSJithu Joseph if (rc < 0 || cpu >= nr_cpu_ids) 736f33a92bSJithu Joseph return -EINVAL; 746f33a92bSJithu Joseph 756f33a92bSJithu Joseph if (down_interruptible(&ifs_sem)) 766f33a92bSJithu Joseph return -EINTR; 776f33a92bSJithu Joseph 786f33a92bSJithu Joseph if (!ifsd->loaded) 796f33a92bSJithu Joseph rc = -EPERM; 806f33a92bSJithu Joseph else 816f33a92bSJithu Joseph rc = do_core_test(cpu, dev); 826f33a92bSJithu Joseph 836f33a92bSJithu Joseph up(&ifs_sem); 846f33a92bSJithu Joseph 856f33a92bSJithu Joseph return rc ? rc : count; 866f33a92bSJithu Joseph } 876f33a92bSJithu Joseph 886f33a92bSJithu Joseph static DEVICE_ATTR_WO(run_test); 896f33a92bSJithu Joseph 90*4fb858f3SJithu Joseph static ssize_t current_batch_store(struct device *dev, 91*4fb858f3SJithu Joseph struct device_attribute *attr, 92*4fb858f3SJithu Joseph const char *buf, size_t count) 93*4fb858f3SJithu Joseph { 94*4fb858f3SJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 95*4fb858f3SJithu Joseph unsigned int cur_batch; 96*4fb858f3SJithu Joseph int rc; 97*4fb858f3SJithu Joseph 98*4fb858f3SJithu Joseph rc = kstrtouint(buf, 0, &cur_batch); 99*4fb858f3SJithu Joseph if (rc < 0 || cur_batch > 0xff) 100*4fb858f3SJithu Joseph return -EINVAL; 101*4fb858f3SJithu Joseph 102*4fb858f3SJithu Joseph if (down_interruptible(&ifs_sem)) 103*4fb858f3SJithu Joseph return -EINTR; 104*4fb858f3SJithu Joseph 105*4fb858f3SJithu Joseph ifsd->cur_batch = cur_batch; 106*4fb858f3SJithu Joseph 107*4fb858f3SJithu Joseph rc = ifs_load_firmware(dev); 108*4fb858f3SJithu Joseph 109*4fb858f3SJithu Joseph up(&ifs_sem); 110*4fb858f3SJithu Joseph 111*4fb858f3SJithu Joseph return (rc == 0) ? count : rc; 112*4fb858f3SJithu Joseph } 113*4fb858f3SJithu Joseph 114*4fb858f3SJithu Joseph static ssize_t current_batch_show(struct device *dev, 115*4fb858f3SJithu Joseph struct device_attribute *attr, char *buf) 116*4fb858f3SJithu Joseph { 117*4fb858f3SJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 118*4fb858f3SJithu Joseph 119*4fb858f3SJithu Joseph if (!ifsd->loaded) 120*4fb858f3SJithu Joseph return sysfs_emit(buf, "none\n"); 121*4fb858f3SJithu Joseph else 122*4fb858f3SJithu Joseph return sysfs_emit(buf, "0x%02x\n", ifsd->cur_batch); 123*4fb858f3SJithu Joseph } 124*4fb858f3SJithu Joseph 125*4fb858f3SJithu Joseph static DEVICE_ATTR_RW(current_batch); 126*4fb858f3SJithu Joseph 1276f33a92bSJithu Joseph /* 1286f33a92bSJithu Joseph * Display currently loaded IFS image version. 1296f33a92bSJithu Joseph */ 1306f33a92bSJithu Joseph static ssize_t image_version_show(struct device *dev, 1316f33a92bSJithu Joseph struct device_attribute *attr, char *buf) 1326f33a92bSJithu Joseph { 1336f33a92bSJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev); 1346f33a92bSJithu Joseph 1356f33a92bSJithu Joseph if (!ifsd->loaded) 1366f33a92bSJithu Joseph return sysfs_emit(buf, "%s\n", "none"); 1376f33a92bSJithu Joseph else 1386f33a92bSJithu Joseph return sysfs_emit(buf, "%#x\n", ifsd->loaded_version); 1396f33a92bSJithu Joseph } 1406f33a92bSJithu Joseph 1416f33a92bSJithu Joseph static DEVICE_ATTR_RO(image_version); 1426f33a92bSJithu Joseph 1436f33a92bSJithu Joseph /* global scan sysfs attributes */ 1446f33a92bSJithu Joseph static struct attribute *plat_ifs_attrs[] = { 1456f33a92bSJithu Joseph &dev_attr_details.attr, 1466f33a92bSJithu Joseph &dev_attr_status.attr, 1476f33a92bSJithu Joseph &dev_attr_run_test.attr, 148*4fb858f3SJithu Joseph &dev_attr_current_batch.attr, 1496f33a92bSJithu Joseph &dev_attr_image_version.attr, 1506f33a92bSJithu Joseph NULL 1516f33a92bSJithu Joseph }; 1526f33a92bSJithu Joseph 1536f33a92bSJithu Joseph ATTRIBUTE_GROUPS(plat_ifs); 1546f33a92bSJithu Joseph 1556f33a92bSJithu Joseph const struct attribute_group **ifs_get_groups(void) 1566f33a92bSJithu Joseph { 1576f33a92bSJithu Joseph return plat_ifs_groups; 1586f33a92bSJithu Joseph } 159