xref: /linux/drivers/platform/x86/intel/ifs/sysfs.c (revision 48380368dec14859723b9e3fbd43e042638d9a76)
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  */
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 
904fb858f3SJithu Joseph static ssize_t current_batch_store(struct device *dev,
914fb858f3SJithu Joseph 				   struct device_attribute *attr,
924fb858f3SJithu Joseph 				   const char *buf, size_t count)
934fb858f3SJithu Joseph {
944fb858f3SJithu Joseph 	struct ifs_data *ifsd = ifs_get_data(dev);
954fb858f3SJithu Joseph 	unsigned int cur_batch;
964fb858f3SJithu Joseph 	int rc;
974fb858f3SJithu Joseph 
984fb858f3SJithu Joseph 	rc = kstrtouint(buf, 0, &cur_batch);
994fb858f3SJithu Joseph 	if (rc < 0 || cur_batch > 0xff)
1004fb858f3SJithu Joseph 		return -EINVAL;
1014fb858f3SJithu Joseph 
1024fb858f3SJithu Joseph 	if (down_interruptible(&ifs_sem))
1034fb858f3SJithu Joseph 		return -EINTR;
1044fb858f3SJithu Joseph 
1054fb858f3SJithu Joseph 	ifsd->cur_batch = cur_batch;
1064fb858f3SJithu Joseph 
1074fb858f3SJithu Joseph 	rc = ifs_load_firmware(dev);
1084fb858f3SJithu Joseph 
1094fb858f3SJithu Joseph 	up(&ifs_sem);
1104fb858f3SJithu Joseph 
1114fb858f3SJithu Joseph 	return (rc == 0) ? count : rc;
1124fb858f3SJithu Joseph }
1134fb858f3SJithu Joseph 
1144fb858f3SJithu Joseph static ssize_t current_batch_show(struct device *dev,
1154fb858f3SJithu Joseph 				  struct device_attribute *attr, char *buf)
1164fb858f3SJithu Joseph {
1174fb858f3SJithu Joseph 	struct ifs_data *ifsd = ifs_get_data(dev);
1184fb858f3SJithu Joseph 
1194fb858f3SJithu Joseph 	if (!ifsd->loaded)
1204fb858f3SJithu Joseph 		return sysfs_emit(buf, "none\n");
1214fb858f3SJithu Joseph 	else
1224fb858f3SJithu Joseph 		return sysfs_emit(buf, "0x%02x\n", ifsd->cur_batch);
1234fb858f3SJithu Joseph }
1244fb858f3SJithu Joseph 
1254fb858f3SJithu Joseph static DEVICE_ATTR_RW(current_batch);
1264fb858f3SJithu 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,
1484fb858f3SJithu 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