1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. */
3 
4 #include <linux/cpu.h>
5 #include <linux/delay.h>
6 #include <linux/fs.h>
7 #include <linux/semaphore.h>
8 #include <linux/slab.h>
9 
10 #include "ifs.h"
11 
12 /*
13  * Protects against simultaneous tests on multiple cores, or
14  * reloading can file while a test is in progress
15  */
16 static DEFINE_SEMAPHORE(ifs_sem);
17 
18 /*
19  * The sysfs interface to check additional details of last test
20  * cat /sys/devices/system/platform/ifs/details
21  */
details_show(struct device * dev,struct device_attribute * attr,char * buf)22 static ssize_t details_show(struct device *dev,
23 			    struct device_attribute *attr,
24 			    char *buf)
25 {
26 	struct ifs_data *ifsd = ifs_get_data(dev);
27 
28 	return sysfs_emit(buf, "%#llx\n", ifsd->scan_details);
29 }
30 
31 static DEVICE_ATTR_RO(details);
32 
33 static const char * const status_msg[] = {
34 	[SCAN_NOT_TESTED] = "untested",
35 	[SCAN_TEST_PASS] = "pass",
36 	[SCAN_TEST_FAIL] = "fail"
37 };
38 
39 /*
40  * The sysfs interface to check the test status:
41  * To check the status of last test
42  * cat /sys/devices/platform/ifs/status
43  */
status_show(struct device * dev,struct device_attribute * attr,char * buf)44 static ssize_t status_show(struct device *dev,
45 			   struct device_attribute *attr,
46 			   char *buf)
47 {
48 	struct ifs_data *ifsd = ifs_get_data(dev);
49 
50 	return sysfs_emit(buf, "%s\n", status_msg[ifsd->status]);
51 }
52 
53 static DEVICE_ATTR_RO(status);
54 
55 /*
56  * The sysfs interface for single core testing
57  * To start test, for example, cpu5
58  * echo 5 > /sys/devices/platform/ifs/run_test
59  * To check the result:
60  * cat /sys/devices/platform/ifs/result
61  * The sibling core gets tested at the same time.
62  */
run_test_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)63 static ssize_t run_test_store(struct device *dev,
64 			      struct device_attribute *attr,
65 			      const char *buf, size_t count)
66 {
67 	struct ifs_data *ifsd = ifs_get_data(dev);
68 	unsigned int cpu;
69 	int rc;
70 
71 	rc = kstrtouint(buf, 0, &cpu);
72 	if (rc < 0 || cpu >= nr_cpu_ids)
73 		return -EINVAL;
74 
75 	if (down_interruptible(&ifs_sem))
76 		return -EINTR;
77 
78 	if (!ifsd->loaded)
79 		rc = -EPERM;
80 	else
81 		rc = do_core_test(cpu, dev);
82 
83 	up(&ifs_sem);
84 
85 	return rc ? rc : count;
86 }
87 
88 static DEVICE_ATTR_WO(run_test);
89 
current_batch_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)90 static ssize_t current_batch_store(struct device *dev,
91 				   struct device_attribute *attr,
92 				   const char *buf, size_t count)
93 {
94 	struct ifs_data *ifsd = ifs_get_data(dev);
95 	unsigned int cur_batch;
96 	int rc;
97 
98 	rc = kstrtouint(buf, 0, &cur_batch);
99 	if (rc < 0 || cur_batch > 0xff)
100 		return -EINVAL;
101 
102 	if (down_interruptible(&ifs_sem))
103 		return -EINTR;
104 
105 	ifsd->cur_batch = cur_batch;
106 
107 	rc = ifs_load_firmware(dev);
108 
109 	up(&ifs_sem);
110 
111 	return (rc == 0) ? count : rc;
112 }
113 
current_batch_show(struct device * dev,struct device_attribute * attr,char * buf)114 static ssize_t current_batch_show(struct device *dev,
115 				  struct device_attribute *attr, char *buf)
116 {
117 	struct ifs_data *ifsd = ifs_get_data(dev);
118 
119 	if (!ifsd->loaded)
120 		return sysfs_emit(buf, "none\n");
121 	else
122 		return sysfs_emit(buf, "0x%02x\n", ifsd->cur_batch);
123 }
124 
125 static DEVICE_ATTR_RW(current_batch);
126 
127 /*
128  * Display currently loaded IFS image version.
129  */
image_version_show(struct device * dev,struct device_attribute * attr,char * buf)130 static ssize_t image_version_show(struct device *dev,
131 				  struct device_attribute *attr, char *buf)
132 {
133 	struct ifs_data *ifsd = ifs_get_data(dev);
134 
135 	if (!ifsd->loaded)
136 		return sysfs_emit(buf, "%s\n", "none");
137 	else
138 		return sysfs_emit(buf, "%#x\n", ifsd->loaded_version);
139 }
140 
141 static DEVICE_ATTR_RO(image_version);
142 
143 /* global scan sysfs attributes */
144 static struct attribute *plat_ifs_attrs[] = {
145 	&dev_attr_details.attr,
146 	&dev_attr_status.attr,
147 	&dev_attr_run_test.attr,
148 	&dev_attr_current_batch.attr,
149 	&dev_attr_image_version.attr,
150 	NULL
151 };
152 
153 ATTRIBUTE_GROUPS(plat_ifs);
154 
ifs_get_groups(void)155 const struct attribute_group **ifs_get_groups(void)
156 {
157 	return plat_ifs_groups;
158 }
159