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 */
16*48380368SPeter Zijlstra static DEFINE_SEMAPHORE(ifs_sem, 1);
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 */
details_show(struct device * dev,struct device_attribute * attr,char * buf)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 */
status_show(struct device * dev,struct device_attribute * attr,char * buf)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 */
run_test_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 unsigned int cpu;
686f33a92bSJithu Joseph int rc;
696f33a92bSJithu Joseph
706f33a92bSJithu Joseph rc = kstrtouint(buf, 0, &cpu);
716f33a92bSJithu Joseph if (rc < 0 || cpu >= nr_cpu_ids)
726f33a92bSJithu Joseph return -EINVAL;
736f33a92bSJithu Joseph
746f33a92bSJithu Joseph if (down_interruptible(&ifs_sem))
756f33a92bSJithu Joseph return -EINTR;
766f33a92bSJithu Joseph
776f33a92bSJithu Joseph rc = do_core_test(cpu, dev);
786f33a92bSJithu Joseph
796f33a92bSJithu Joseph up(&ifs_sem);
806f33a92bSJithu Joseph
816f33a92bSJithu Joseph return rc ? rc : count;
826f33a92bSJithu Joseph }
836f33a92bSJithu Joseph
846f33a92bSJithu Joseph static DEVICE_ATTR_WO(run_test);
856f33a92bSJithu Joseph
current_batch_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)864fb858f3SJithu Joseph static ssize_t current_batch_store(struct device *dev,
874fb858f3SJithu Joseph struct device_attribute *attr,
884fb858f3SJithu Joseph const char *buf, size_t count)
894fb858f3SJithu Joseph {
904fb858f3SJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev);
914fb858f3SJithu Joseph unsigned int cur_batch;
924fb858f3SJithu Joseph int rc;
934fb858f3SJithu Joseph
944fb858f3SJithu Joseph rc = kstrtouint(buf, 0, &cur_batch);
954fb858f3SJithu Joseph if (rc < 0 || cur_batch > 0xff)
964fb858f3SJithu Joseph return -EINVAL;
974fb858f3SJithu Joseph
984fb858f3SJithu Joseph if (down_interruptible(&ifs_sem))
994fb858f3SJithu Joseph return -EINTR;
1004fb858f3SJithu Joseph
1014fb858f3SJithu Joseph ifsd->cur_batch = cur_batch;
1024fb858f3SJithu Joseph
1034fb858f3SJithu Joseph rc = ifs_load_firmware(dev);
1044fb858f3SJithu Joseph
1054fb858f3SJithu Joseph up(&ifs_sem);
1064fb858f3SJithu Joseph
1074fb858f3SJithu Joseph return (rc == 0) ? count : rc;
1084fb858f3SJithu Joseph }
1094fb858f3SJithu Joseph
current_batch_show(struct device * dev,struct device_attribute * attr,char * buf)1104fb858f3SJithu Joseph static ssize_t current_batch_show(struct device *dev,
1114fb858f3SJithu Joseph struct device_attribute *attr, char *buf)
1124fb858f3SJithu Joseph {
1134fb858f3SJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev);
1144fb858f3SJithu Joseph
1154fb858f3SJithu Joseph if (!ifsd->loaded)
1164fb858f3SJithu Joseph return sysfs_emit(buf, "none\n");
1174fb858f3SJithu Joseph else
1184fb858f3SJithu Joseph return sysfs_emit(buf, "0x%02x\n", ifsd->cur_batch);
1194fb858f3SJithu Joseph }
1204fb858f3SJithu Joseph
1214fb858f3SJithu Joseph static DEVICE_ATTR_RW(current_batch);
1224fb858f3SJithu Joseph
1236f33a92bSJithu Joseph /*
1246f33a92bSJithu Joseph * Display currently loaded IFS image version.
1256f33a92bSJithu Joseph */
image_version_show(struct device * dev,struct device_attribute * attr,char * buf)1266f33a92bSJithu Joseph static ssize_t image_version_show(struct device *dev,
1276f33a92bSJithu Joseph struct device_attribute *attr, char *buf)
1286f33a92bSJithu Joseph {
1296f33a92bSJithu Joseph struct ifs_data *ifsd = ifs_get_data(dev);
1306f33a92bSJithu Joseph
1316f33a92bSJithu Joseph if (!ifsd->loaded)
1326f33a92bSJithu Joseph return sysfs_emit(buf, "%s\n", "none");
1336f33a92bSJithu Joseph else
1346f33a92bSJithu Joseph return sysfs_emit(buf, "%#x\n", ifsd->loaded_version);
1356f33a92bSJithu Joseph }
1366f33a92bSJithu Joseph
1376f33a92bSJithu Joseph static DEVICE_ATTR_RO(image_version);
1386f33a92bSJithu Joseph
1396f33a92bSJithu Joseph /* global scan sysfs attributes */
140d847eddfSJithu Joseph struct attribute *plat_ifs_attrs[] = {
1416f33a92bSJithu Joseph &dev_attr_details.attr,
1426f33a92bSJithu Joseph &dev_attr_status.attr,
1436f33a92bSJithu Joseph &dev_attr_run_test.attr,
1444fb858f3SJithu Joseph &dev_attr_current_batch.attr,
1456f33a92bSJithu Joseph &dev_attr_image_version.attr,
1466f33a92bSJithu Joseph NULL
1476f33a92bSJithu Joseph };
1485210fb4eSJithu Joseph
1495210fb4eSJithu Joseph /* global array sysfs attributes */
1505210fb4eSJithu Joseph struct attribute *plat_ifs_array_attrs[] = {
1515210fb4eSJithu Joseph &dev_attr_details.attr,
1525210fb4eSJithu Joseph &dev_attr_status.attr,
1535210fb4eSJithu Joseph &dev_attr_run_test.attr,
1545210fb4eSJithu Joseph NULL
1555210fb4eSJithu Joseph };
156