2 * Copyright 2014 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sysfs.h>
16 #define to_afu_chardev_m(d) dev_get_drvdata(d)
18 /********* Adapter attributes **********************************************/
20 static ssize_t
caia_version_show(struct device
*device
,
21 struct device_attribute
*attr
,
24 struct cxl
*adapter
= to_cxl_adapter(device
);
26 return scnprintf(buf
, PAGE_SIZE
, "%i.%i\n", adapter
->caia_major
,
30 static ssize_t
psl_revision_show(struct device
*device
,
31 struct device_attribute
*attr
,
34 struct cxl
*adapter
= to_cxl_adapter(device
);
36 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->psl_rev
);
39 static ssize_t
base_image_show(struct device
*device
,
40 struct device_attribute
*attr
,
43 struct cxl
*adapter
= to_cxl_adapter(device
);
45 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->base_image
);
48 static ssize_t
image_loaded_show(struct device
*device
,
49 struct device_attribute
*attr
,
52 struct cxl
*adapter
= to_cxl_adapter(device
);
54 if (adapter
->user_image_loaded
)
55 return scnprintf(buf
, PAGE_SIZE
, "user\n");
56 return scnprintf(buf
, PAGE_SIZE
, "factory\n");
59 static struct device_attribute adapter_attrs
[] = {
60 __ATTR_RO(caia_version
),
61 __ATTR_RO(psl_revision
),
62 __ATTR_RO(base_image
),
63 __ATTR_RO(image_loaded
),
67 /********* AFU master specific attributes **********************************/
69 static ssize_t
mmio_size_show_master(struct device
*device
,
70 struct device_attribute
*attr
,
73 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
75 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
78 static ssize_t
pp_mmio_off_show(struct device
*device
,
79 struct device_attribute
*attr
,
82 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
84 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_offset
);
87 static ssize_t
pp_mmio_len_show(struct device
*device
,
88 struct device_attribute
*attr
,
91 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
93 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
96 static struct device_attribute afu_master_attrs
[] = {
97 __ATTR(mmio_size
, S_IRUGO
, mmio_size_show_master
, NULL
),
98 __ATTR_RO(pp_mmio_off
),
99 __ATTR_RO(pp_mmio_len
),
103 /********* AFU attributes **************************************************/
105 static ssize_t
mmio_size_show(struct device
*device
,
106 struct device_attribute
*attr
,
109 struct cxl_afu
*afu
= to_cxl_afu(device
);
112 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
113 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
116 static ssize_t
reset_store_afu(struct device
*device
,
117 struct device_attribute
*attr
,
118 const char *buf
, size_t count
)
120 struct cxl_afu
*afu
= to_cxl_afu(device
);
123 /* Not safe to reset if it is currently in use */
124 mutex_lock(&afu
->contexts_lock
);
125 if (!idr_is_empty(&afu
->contexts_idr
)) {
130 if ((rc
= cxl_afu_reset(afu
)))
135 mutex_unlock(&afu
->contexts_lock
);
139 static ssize_t
irqs_min_show(struct device
*device
,
140 struct device_attribute
*attr
,
143 struct cxl_afu
*afu
= to_cxl_afu(device
);
145 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->pp_irqs
);
148 static ssize_t
irqs_max_show(struct device
*device
,
149 struct device_attribute
*attr
,
152 struct cxl_afu
*afu
= to_cxl_afu(device
);
154 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->irqs_max
);
157 static ssize_t
irqs_max_store(struct device
*device
,
158 struct device_attribute
*attr
,
159 const char *buf
, size_t count
)
161 struct cxl_afu
*afu
= to_cxl_afu(device
);
165 ret
= sscanf(buf
, "%i", &irqs_max
);
169 if (irqs_max
< afu
->pp_irqs
)
172 if (irqs_max
> afu
->adapter
->user_irqs
)
175 afu
->irqs_max
= irqs_max
;
179 static ssize_t
modes_supported_show(struct device
*device
,
180 struct device_attribute
*attr
, char *buf
)
182 struct cxl_afu
*afu
= to_cxl_afu(device
);
183 char *p
= buf
, *end
= buf
+ PAGE_SIZE
;
185 if (afu
->modes_supported
& CXL_MODE_DEDICATED
)
186 p
+= scnprintf(p
, end
- p
, "dedicated_process\n");
187 if (afu
->modes_supported
& CXL_MODE_DIRECTED
)
188 p
+= scnprintf(p
, end
- p
, "afu_directed\n");
192 static ssize_t
prefault_mode_show(struct device
*device
,
193 struct device_attribute
*attr
,
196 struct cxl_afu
*afu
= to_cxl_afu(device
);
198 switch (afu
->prefault_mode
) {
199 case CXL_PREFAULT_WED
:
200 return scnprintf(buf
, PAGE_SIZE
, "work_element_descriptor\n");
201 case CXL_PREFAULT_ALL
:
202 return scnprintf(buf
, PAGE_SIZE
, "all\n");
204 return scnprintf(buf
, PAGE_SIZE
, "none\n");
208 static ssize_t
prefault_mode_store(struct device
*device
,
209 struct device_attribute
*attr
,
210 const char *buf
, size_t count
)
212 struct cxl_afu
*afu
= to_cxl_afu(device
);
213 enum prefault_modes mode
= -1;
215 if (!strncmp(buf
, "work_element_descriptor", 23))
216 mode
= CXL_PREFAULT_WED
;
217 if (!strncmp(buf
, "all", 3))
218 mode
= CXL_PREFAULT_ALL
;
219 if (!strncmp(buf
, "none", 4))
220 mode
= CXL_PREFAULT_NONE
;
225 afu
->prefault_mode
= mode
;
229 static ssize_t
mode_show(struct device
*device
,
230 struct device_attribute
*attr
,
233 struct cxl_afu
*afu
= to_cxl_afu(device
);
235 if (afu
->current_mode
== CXL_MODE_DEDICATED
)
236 return scnprintf(buf
, PAGE_SIZE
, "dedicated_process\n");
237 if (afu
->current_mode
== CXL_MODE_DIRECTED
)
238 return scnprintf(buf
, PAGE_SIZE
, "afu_directed\n");
239 return scnprintf(buf
, PAGE_SIZE
, "none\n");
242 static ssize_t
mode_store(struct device
*device
, struct device_attribute
*attr
,
243 const char *buf
, size_t count
)
245 struct cxl_afu
*afu
= to_cxl_afu(device
);
246 int old_mode
, mode
= -1;
249 /* can't change this if we have a user */
250 mutex_lock(&afu
->contexts_lock
);
251 if (!idr_is_empty(&afu
->contexts_idr
))
254 if (!strncmp(buf
, "dedicated_process", 17))
255 mode
= CXL_MODE_DEDICATED
;
256 if (!strncmp(buf
, "afu_directed", 12))
257 mode
= CXL_MODE_DIRECTED
;
258 if (!strncmp(buf
, "none", 4))
267 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
268 * other contexts coming in before we are ready:
270 old_mode
= afu
->current_mode
;
271 afu
->current_mode
= 0;
274 mutex_unlock(&afu
->contexts_lock
);
276 if ((rc
= _cxl_afu_deactivate_mode(afu
, old_mode
)))
278 if ((rc
= cxl_afu_activate_mode(afu
, mode
)))
283 mutex_unlock(&afu
->contexts_lock
);
287 static ssize_t
api_version_show(struct device
*device
,
288 struct device_attribute
*attr
,
291 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION
);
294 static ssize_t
api_version_compatible_show(struct device
*device
,
295 struct device_attribute
*attr
,
298 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION_COMPATIBLE
);
301 static struct device_attribute afu_attrs
[] = {
302 __ATTR_RO(mmio_size
),
305 __ATTR_RO(modes_supported
),
307 __ATTR_RW(prefault_mode
),
308 __ATTR_RO(api_version
),
309 __ATTR_RO(api_version_compatible
),
310 __ATTR(reset
, S_IWUSR
, NULL
, reset_store_afu
),
315 int cxl_sysfs_adapter_add(struct cxl
*adapter
)
319 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++) {
320 if ((rc
= device_create_file(&adapter
->dev
, &adapter_attrs
[i
])))
325 for (i
--; i
>= 0; i
--)
326 device_remove_file(&adapter
->dev
, &adapter_attrs
[i
]);
329 void cxl_sysfs_adapter_remove(struct cxl
*adapter
)
333 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++)
334 device_remove_file(&adapter
->dev
, &adapter_attrs
[i
]);
337 int cxl_sysfs_afu_add(struct cxl_afu
*afu
)
341 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++) {
342 if ((rc
= device_create_file(&afu
->dev
, &afu_attrs
[i
])))
349 for (i
--; i
>= 0; i
--)
350 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
354 void cxl_sysfs_afu_remove(struct cxl_afu
*afu
)
358 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++)
359 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
362 int cxl_sysfs_afu_m_add(struct cxl_afu
*afu
)
366 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++) {
367 if ((rc
= device_create_file(afu
->chardev_m
, &afu_master_attrs
[i
])))
374 for (i
--; i
>= 0; i
--)
375 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);
379 void cxl_sysfs_afu_m_remove(struct cxl_afu
*afu
)
383 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++)
384 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);