forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 ee930fffee469d076998274a2ca55e13dc1efb67
kernel/drivers/misc/ocxl/sysfs.c
....@@ -3,11 +3,18 @@
33 #include <linux/sysfs.h>
44 #include "ocxl_internal.h"
55
6
+static inline struct ocxl_afu *to_afu(struct device *device)
7
+{
8
+ struct ocxl_file_info *info = container_of(device, struct ocxl_file_info, dev);
9
+
10
+ return info->afu;
11
+}
12
+
613 static ssize_t global_mmio_size_show(struct device *device,
714 struct device_attribute *attr,
815 char *buf)
916 {
10
- struct ocxl_afu *afu = to_ocxl_afu(device);
17
+ struct ocxl_afu *afu = to_afu(device);
1118
1219 return scnprintf(buf, PAGE_SIZE, "%d\n",
1320 afu->config.global_mmio_size);
....@@ -17,7 +24,7 @@
1724 struct device_attribute *attr,
1825 char *buf)
1926 {
20
- struct ocxl_afu *afu = to_ocxl_afu(device);
27
+ struct ocxl_afu *afu = to_afu(device);
2128
2229 return scnprintf(buf, PAGE_SIZE, "%d\n",
2330 afu->config.pp_mmio_stride);
....@@ -27,7 +34,7 @@
2734 struct device_attribute *attr,
2835 char *buf)
2936 {
30
- struct ocxl_afu *afu = to_ocxl_afu(device);
37
+ struct ocxl_afu *afu = to_afu(device);
3138
3239 return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n",
3340 afu->config.version_major,
....@@ -38,10 +45,44 @@
3845 struct device_attribute *attr,
3946 char *buf)
4047 {
41
- struct ocxl_afu *afu = to_ocxl_afu(device);
48
+ struct ocxl_afu *afu = to_afu(device);
4249
4350 return scnprintf(buf, PAGE_SIZE, "%d/%d\n",
4451 afu->pasid_count, afu->pasid_max);
52
+}
53
+
54
+static ssize_t reload_on_reset_show(struct device *device,
55
+ struct device_attribute *attr,
56
+ char *buf)
57
+{
58
+ struct ocxl_afu *afu = to_afu(device);
59
+ struct ocxl_fn *fn = afu->fn;
60
+ struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
61
+ int val;
62
+
63
+ if (ocxl_config_get_reset_reload(pci_dev, &val))
64
+ return scnprintf(buf, PAGE_SIZE, "unavailable\n");
65
+
66
+ return scnprintf(buf, PAGE_SIZE, "%d\n", val);
67
+}
68
+
69
+static ssize_t reload_on_reset_store(struct device *device,
70
+ struct device_attribute *attr,
71
+ const char *buf, size_t count)
72
+{
73
+ struct ocxl_afu *afu = to_afu(device);
74
+ struct ocxl_fn *fn = afu->fn;
75
+ struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
76
+ int rc, val;
77
+
78
+ rc = kstrtoint(buf, 0, &val);
79
+ if (rc || (val != 0 && val != 1))
80
+ return -EINVAL;
81
+
82
+ if (ocxl_config_set_reset_reload(pci_dev, val))
83
+ return -ENODEV;
84
+
85
+ return count;
4586 }
4687
4788 static struct device_attribute afu_attrs[] = {
....@@ -49,13 +90,14 @@
4990 __ATTR_RO(pp_mmio_size),
5091 __ATTR_RO(afu_version),
5192 __ATTR_RO(contexts),
93
+ __ATTR_RW(reload_on_reset),
5294 };
5395
5496 static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj,
5597 struct bin_attribute *bin_attr, char *buf,
5698 loff_t off, size_t count)
5799 {
58
- struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
100
+ struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
59101
60102 if (count == 0 || off < 0 ||
61103 off >= afu->config.global_mmio_size)
....@@ -86,7 +128,7 @@
86128 struct bin_attribute *bin_attr,
87129 struct vm_area_struct *vma)
88130 {
89
- struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
131
+ struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
90132
91133 if ((vma_pages(vma) + vma->vm_pgoff) >
92134 (afu->config.global_mmio_size >> PAGE_SHIFT))
....@@ -99,27 +141,25 @@
99141 return 0;
100142 }
101143
102
-int ocxl_sysfs_add_afu(struct ocxl_afu *afu)
144
+int ocxl_sysfs_register_afu(struct ocxl_file_info *info)
103145 {
104146 int i, rc;
105147
106148 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
107
- rc = device_create_file(&afu->dev, &afu_attrs[i]);
149
+ rc = device_create_file(&info->dev, &afu_attrs[i]);
108150 if (rc)
109151 goto err;
110152 }
111153
112
- sysfs_attr_init(&afu->attr_global_mmio.attr);
113
- afu->attr_global_mmio.attr.name = "global_mmio_area";
114
- afu->attr_global_mmio.attr.mode = 0600;
115
- afu->attr_global_mmio.size = afu->config.global_mmio_size;
116
- afu->attr_global_mmio.read = global_mmio_read;
117
- afu->attr_global_mmio.mmap = global_mmio_mmap;
118
- rc = device_create_bin_file(&afu->dev, &afu->attr_global_mmio);
154
+ sysfs_attr_init(&info->attr_global_mmio.attr);
155
+ info->attr_global_mmio.attr.name = "global_mmio_area";
156
+ info->attr_global_mmio.attr.mode = 0600;
157
+ info->attr_global_mmio.size = info->afu->config.global_mmio_size;
158
+ info->attr_global_mmio.read = global_mmio_read;
159
+ info->attr_global_mmio.mmap = global_mmio_mmap;
160
+ rc = device_create_bin_file(&info->dev, &info->attr_global_mmio);
119161 if (rc) {
120
- dev_err(&afu->dev,
121
- "Unable to create global mmio attr for afu: %d\n",
122
- rc);
162
+ dev_err(&info->dev, "Unable to create global mmio attr for afu: %d\n", rc);
123163 goto err;
124164 }
125165
....@@ -127,15 +167,20 @@
127167
128168 err:
129169 for (i--; i >= 0; i--)
130
- device_remove_file(&afu->dev, &afu_attrs[i]);
170
+ device_remove_file(&info->dev, &afu_attrs[i]);
171
+
131172 return rc;
132173 }
133174
134
-void ocxl_sysfs_remove_afu(struct ocxl_afu *afu)
175
+void ocxl_sysfs_unregister_afu(struct ocxl_file_info *info)
135176 {
136177 int i;
137178
179
+ /*
180
+ * device_remove_bin_file is safe to call if the file is not added as
181
+ * the files are removed by name, and early exit if not found
182
+ */
138183 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
139
- device_remove_file(&afu->dev, &afu_attrs[i]);
140
- device_remove_bin_file(&afu->dev, &afu->attr_global_mmio);
184
+ device_remove_file(&info->dev, &afu_attrs[i]);
185
+ device_remove_bin_file(&info->dev, &info->attr_global_mmio);
141186 }