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, 1);
17 
18 /*
19  * The sysfs interface to check additional details of last test
20  * cat /sys/devices/system/platform/ifs/details
21  */
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  */
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  */
63 static ssize_t run_test_store(struct device *dev,
64 			      struct device_attribute *attr,
65 			      const char *buf, size_t count)
66 {
67 	unsigned int cpu;
68 	int rc;
69 
70 	rc = kstrtouint(buf, 0, &cpu);
71 	if (rc < 0 || cpu >= nr_cpu_ids)
72 		return -EINVAL;
73 
74 	if (down_interruptible(&ifs_sem))
75 		return -EINTR;
76 
77 	rc = do_core_test(cpu, dev);
78 
79 	up(&ifs_sem);
80 
81 	return rc ? rc : count;
82 }
83 
84 static DEVICE_ATTR_WO(run_test);
85 
86 static ssize_t current_batch_store(struct device *dev,
87 				   struct device_attribute *attr,
88 				   const char *buf, size_t count)
89 {
90 	struct ifs_data *ifsd = ifs_get_data(dev);
91 	unsigned int cur_batch;
92 	int rc;
93 
94 	rc = kstrtouint(buf, 0, &cur_batch);
95 	if (rc < 0 || cur_batch > 0xff)
96 		return -EINVAL;
97 
98 	if (down_interruptible(&ifs_sem))
99 		return -EINTR;
100 
101 	ifsd->cur_batch = cur_batch;
102 
103 	rc = ifs_load_firmware(dev);
104 
105 	up(&ifs_sem);
106 
107 	return (rc == 0) ? count : rc;
108 }
109 
110 static ssize_t current_batch_show(struct device *dev,
111 				  struct device_attribute *attr, char *buf)
112 {
113 	struct ifs_data *ifsd = ifs_get_data(dev);
114 
115 	if (!ifsd->loaded)
116 		return sysfs_emit(buf, "none\n");
117 	else
118 		return sysfs_emit(buf, "0x%02x\n", ifsd->cur_batch);
119 }
120 
121 static DEVICE_ATTR_RW(current_batch);
122 
123 /*
124  * Display currently loaded IFS image version.
125  */
126 static ssize_t image_version_show(struct device *dev,
127 				  struct device_attribute *attr, char *buf)
128 {
129 	struct ifs_data *ifsd = ifs_get_data(dev);
130 
131 	if (!ifsd->loaded)
132 		return sysfs_emit(buf, "%s\n", "none");
133 	else
134 		return sysfs_emit(buf, "%#x\n", ifsd->loaded_version);
135 }
136 
137 static DEVICE_ATTR_RO(image_version);
138 
139 /* global scan sysfs attributes */
140 struct attribute *plat_ifs_attrs[] = {
141 	&dev_attr_details.attr,
142 	&dev_attr_status.attr,
143 	&dev_attr_run_test.attr,
144 	&dev_attr_current_batch.attr,
145 	&dev_attr_image_version.attr,
146 	NULL
147 };
148 
149 /* global array sysfs attributes */
150 struct attribute *plat_ifs_array_attrs[] = {
151 	&dev_attr_details.attr,
152 	&dev_attr_status.attr,
153 	&dev_attr_run_test.attr,
154 	NULL
155 };
156