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>
13 #include <linux/pci_regs.h>
17 #define to_afu_chardev_m(d) dev_get_drvdata(d)
19 /********* Adapter attributes **********************************************/
21 static ssize_t
caia_version_show(struct device
*device
,
22 struct device_attribute
*attr
,
25 struct cxl
*adapter
= to_cxl_adapter(device
);
27 return scnprintf(buf
, PAGE_SIZE
, "%i.%i\n", adapter
->caia_major
,
31 static ssize_t
psl_revision_show(struct device
*device
,
32 struct device_attribute
*attr
,
35 struct cxl
*adapter
= to_cxl_adapter(device
);
37 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->psl_rev
);
40 static ssize_t
base_image_show(struct device
*device
,
41 struct device_attribute
*attr
,
44 struct cxl
*adapter
= to_cxl_adapter(device
);
46 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->base_image
);
49 static ssize_t
image_loaded_show(struct device
*device
,
50 struct device_attribute
*attr
,
53 struct cxl
*adapter
= to_cxl_adapter(device
);
55 if (adapter
->user_image_loaded
)
56 return scnprintf(buf
, PAGE_SIZE
, "user\n");
57 return scnprintf(buf
, PAGE_SIZE
, "factory\n");
60 static ssize_t
psl_timebase_synced_show(struct device
*device
,
61 struct device_attribute
*attr
,
64 struct cxl
*adapter
= to_cxl_adapter(device
);
66 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->psl_timebase_synced
);
69 static ssize_t
reset_adapter_store(struct device
*device
,
70 struct device_attribute
*attr
,
71 const char *buf
, size_t count
)
73 struct cxl
*adapter
= to_cxl_adapter(device
);
77 rc
= sscanf(buf
, "%i", &val
);
78 if ((rc
!= 1) || (val
!= 1))
81 if ((rc
= cxl_ops
->adapter_reset(adapter
)))
86 static ssize_t
load_image_on_perst_show(struct device
*device
,
87 struct device_attribute
*attr
,
90 struct cxl
*adapter
= to_cxl_adapter(device
);
92 if (!adapter
->perst_loads_image
)
93 return scnprintf(buf
, PAGE_SIZE
, "none\n");
95 if (adapter
->perst_select_user
)
96 return scnprintf(buf
, PAGE_SIZE
, "user\n");
97 return scnprintf(buf
, PAGE_SIZE
, "factory\n");
100 static ssize_t
load_image_on_perst_store(struct device
*device
,
101 struct device_attribute
*attr
,
102 const char *buf
, size_t count
)
104 struct cxl
*adapter
= to_cxl_adapter(device
);
107 if (!strncmp(buf
, "none", 4))
108 adapter
->perst_loads_image
= false;
109 else if (!strncmp(buf
, "user", 4)) {
110 adapter
->perst_select_user
= true;
111 adapter
->perst_loads_image
= true;
112 } else if (!strncmp(buf
, "factory", 7)) {
113 adapter
->perst_select_user
= false;
114 adapter
->perst_loads_image
= true;
118 if ((rc
= cxl_update_image_control(adapter
)))
124 static ssize_t
perst_reloads_same_image_show(struct device
*device
,
125 struct device_attribute
*attr
,
128 struct cxl
*adapter
= to_cxl_adapter(device
);
130 return scnprintf(buf
, PAGE_SIZE
, "%i\n", adapter
->perst_same_image
);
133 static ssize_t
perst_reloads_same_image_store(struct device
*device
,
134 struct device_attribute
*attr
,
135 const char *buf
, size_t count
)
137 struct cxl
*adapter
= to_cxl_adapter(device
);
141 rc
= sscanf(buf
, "%i", &val
);
142 if ((rc
!= 1) || !(val
== 1 || val
== 0))
145 adapter
->perst_same_image
= (val
== 1 ? true : false);
149 static struct device_attribute adapter_attrs
[] = {
150 __ATTR_RO(caia_version
),
151 __ATTR_RO(psl_revision
),
152 __ATTR_RO(base_image
),
153 __ATTR_RO(image_loaded
),
154 __ATTR_RO(psl_timebase_synced
),
155 __ATTR_RW(load_image_on_perst
),
156 __ATTR_RW(perst_reloads_same_image
),
157 __ATTR(reset
, S_IWUSR
, NULL
, reset_adapter_store
),
161 /********* AFU master specific attributes **********************************/
163 static ssize_t
mmio_size_show_master(struct device
*device
,
164 struct device_attribute
*attr
,
167 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
169 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
172 static ssize_t
pp_mmio_off_show(struct device
*device
,
173 struct device_attribute
*attr
,
176 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
178 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->native
->pp_offset
);
181 static ssize_t
pp_mmio_len_show(struct device
*device
,
182 struct device_attribute
*attr
,
185 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
187 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
190 static struct device_attribute afu_master_attrs
[] = {
191 __ATTR(mmio_size
, S_IRUGO
, mmio_size_show_master
, NULL
),
192 __ATTR_RO(pp_mmio_off
),
193 __ATTR_RO(pp_mmio_len
),
197 /********* AFU attributes **************************************************/
199 static ssize_t
mmio_size_show(struct device
*device
,
200 struct device_attribute
*attr
,
203 struct cxl_afu
*afu
= to_cxl_afu(device
);
206 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
207 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
210 static ssize_t
reset_store_afu(struct device
*device
,
211 struct device_attribute
*attr
,
212 const char *buf
, size_t count
)
214 struct cxl_afu
*afu
= to_cxl_afu(device
);
217 /* Not safe to reset if it is currently in use */
218 mutex_lock(&afu
->contexts_lock
);
219 if (!idr_is_empty(&afu
->contexts_idr
)) {
224 if ((rc
= cxl_ops
->afu_reset(afu
)))
229 mutex_unlock(&afu
->contexts_lock
);
233 static ssize_t
irqs_min_show(struct device
*device
,
234 struct device_attribute
*attr
,
237 struct cxl_afu
*afu
= to_cxl_afu(device
);
239 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->pp_irqs
);
242 static ssize_t
irqs_max_show(struct device
*device
,
243 struct device_attribute
*attr
,
246 struct cxl_afu
*afu
= to_cxl_afu(device
);
248 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->irqs_max
);
251 static ssize_t
irqs_max_store(struct device
*device
,
252 struct device_attribute
*attr
,
253 const char *buf
, size_t count
)
255 struct cxl_afu
*afu
= to_cxl_afu(device
);
259 ret
= sscanf(buf
, "%i", &irqs_max
);
263 if (irqs_max
< afu
->pp_irqs
)
266 if (cpu_has_feature(CPU_FTR_HVMODE
)) {
267 if (irqs_max
> afu
->adapter
->user_irqs
)
270 /* pHyp sets a per-AFU limit */
271 if (irqs_max
> afu
->guest
->max_ints
)
275 afu
->irqs_max
= irqs_max
;
279 static ssize_t
modes_supported_show(struct device
*device
,
280 struct device_attribute
*attr
, char *buf
)
282 struct cxl_afu
*afu
= to_cxl_afu(device
);
283 char *p
= buf
, *end
= buf
+ PAGE_SIZE
;
285 if (afu
->modes_supported
& CXL_MODE_DEDICATED
)
286 p
+= scnprintf(p
, end
- p
, "dedicated_process\n");
287 if (afu
->modes_supported
& CXL_MODE_DIRECTED
)
288 p
+= scnprintf(p
, end
- p
, "afu_directed\n");
292 static ssize_t
prefault_mode_show(struct device
*device
,
293 struct device_attribute
*attr
,
296 struct cxl_afu
*afu
= to_cxl_afu(device
);
298 switch (afu
->prefault_mode
) {
299 case CXL_PREFAULT_WED
:
300 return scnprintf(buf
, PAGE_SIZE
, "work_element_descriptor\n");
301 case CXL_PREFAULT_ALL
:
302 return scnprintf(buf
, PAGE_SIZE
, "all\n");
304 return scnprintf(buf
, PAGE_SIZE
, "none\n");
308 static ssize_t
prefault_mode_store(struct device
*device
,
309 struct device_attribute
*attr
,
310 const char *buf
, size_t count
)
312 struct cxl_afu
*afu
= to_cxl_afu(device
);
313 enum prefault_modes mode
= -1;
315 if (!strncmp(buf
, "work_element_descriptor", 23))
316 mode
= CXL_PREFAULT_WED
;
317 if (!strncmp(buf
, "all", 3))
318 mode
= CXL_PREFAULT_ALL
;
319 if (!strncmp(buf
, "none", 4))
320 mode
= CXL_PREFAULT_NONE
;
325 afu
->prefault_mode
= mode
;
329 static ssize_t
mode_show(struct device
*device
,
330 struct device_attribute
*attr
,
333 struct cxl_afu
*afu
= to_cxl_afu(device
);
335 if (afu
->current_mode
== CXL_MODE_DEDICATED
)
336 return scnprintf(buf
, PAGE_SIZE
, "dedicated_process\n");
337 if (afu
->current_mode
== CXL_MODE_DIRECTED
)
338 return scnprintf(buf
, PAGE_SIZE
, "afu_directed\n");
339 return scnprintf(buf
, PAGE_SIZE
, "none\n");
342 static ssize_t
mode_store(struct device
*device
, struct device_attribute
*attr
,
343 const char *buf
, size_t count
)
345 struct cxl_afu
*afu
= to_cxl_afu(device
);
346 int old_mode
, mode
= -1;
349 /* can't change this if we have a user */
350 mutex_lock(&afu
->contexts_lock
);
351 if (!idr_is_empty(&afu
->contexts_idr
))
354 if (!strncmp(buf
, "dedicated_process", 17))
355 mode
= CXL_MODE_DEDICATED
;
356 if (!strncmp(buf
, "afu_directed", 12))
357 mode
= CXL_MODE_DIRECTED
;
358 if (!strncmp(buf
, "none", 4))
367 * afu_deactivate_mode needs to be done outside the lock, prevent
368 * other contexts coming in before we are ready:
370 old_mode
= afu
->current_mode
;
371 afu
->current_mode
= 0;
374 mutex_unlock(&afu
->contexts_lock
);
376 if ((rc
= cxl_ops
->afu_deactivate_mode(afu
, old_mode
)))
378 if ((rc
= cxl_ops
->afu_activate_mode(afu
, mode
)))
383 mutex_unlock(&afu
->contexts_lock
);
387 static ssize_t
api_version_show(struct device
*device
,
388 struct device_attribute
*attr
,
391 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION
);
394 static ssize_t
api_version_compatible_show(struct device
*device
,
395 struct device_attribute
*attr
,
398 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION_COMPATIBLE
);
401 static ssize_t
afu_eb_read(struct file
*filp
, struct kobject
*kobj
,
402 struct bin_attribute
*bin_attr
, char *buf
,
403 loff_t off
, size_t count
)
405 struct cxl_afu
*afu
= to_cxl_afu(kobj_to_dev(kobj
));
407 return cxl_ops
->afu_read_err_buffer(afu
, buf
, off
, count
);
410 static struct device_attribute afu_attrs
[] = {
411 __ATTR_RO(mmio_size
),
414 __ATTR_RO(modes_supported
),
416 __ATTR_RW(prefault_mode
),
417 __ATTR_RO(api_version
),
418 __ATTR_RO(api_version_compatible
),
419 __ATTR(reset
, S_IWUSR
, NULL
, reset_store_afu
),
422 int cxl_sysfs_adapter_add(struct cxl
*adapter
)
424 struct device_attribute
*dev_attr
;
427 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++) {
428 dev_attr
= &adapter_attrs
[i
];
429 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
430 CXL_ADAPTER_ATTRS
)) {
431 if ((rc
= device_create_file(&adapter
->dev
, dev_attr
)))
437 for (i
--; i
>= 0; i
--) {
438 dev_attr
= &adapter_attrs
[i
];
439 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
441 device_remove_file(&adapter
->dev
, dev_attr
);
446 void cxl_sysfs_adapter_remove(struct cxl
*adapter
)
448 struct device_attribute
*dev_attr
;
451 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++) {
452 dev_attr
= &adapter_attrs
[i
];
453 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
455 device_remove_file(&adapter
->dev
, dev_attr
);
459 struct afu_config_record
{
461 struct bin_attribute config_attr
;
462 struct list_head list
;
469 #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
471 static ssize_t
vendor_show(struct kobject
*kobj
,
472 struct kobj_attribute
*attr
, char *buf
)
474 struct afu_config_record
*cr
= to_cr(kobj
);
476 return scnprintf(buf
, PAGE_SIZE
, "0x%.4x\n", cr
->vendor
);
479 static ssize_t
device_show(struct kobject
*kobj
,
480 struct kobj_attribute
*attr
, char *buf
)
482 struct afu_config_record
*cr
= to_cr(kobj
);
484 return scnprintf(buf
, PAGE_SIZE
, "0x%.4x\n", cr
->device
);
487 static ssize_t
class_show(struct kobject
*kobj
,
488 struct kobj_attribute
*attr
, char *buf
)
490 struct afu_config_record
*cr
= to_cr(kobj
);
492 return scnprintf(buf
, PAGE_SIZE
, "0x%.6x\n", cr
->class);
495 static ssize_t
afu_read_config(struct file
*filp
, struct kobject
*kobj
,
496 struct bin_attribute
*bin_attr
, char *buf
,
497 loff_t off
, size_t count
)
499 struct afu_config_record
*cr
= to_cr(kobj
);
500 struct cxl_afu
*afu
= to_cxl_afu(kobj_to_dev(kobj
->parent
));
504 for (i
= 0; i
< count
;) {
505 rc
= cxl_ops
->afu_cr_read64(afu
, cr
->cr
, off
& ~0x7, &val
);
508 for (j
= off
& 0x7; j
< 8 && i
< count
; i
++, j
++, off
++)
509 buf
[i
] = (val
>> (j
* 8)) & 0xff;
515 static struct kobj_attribute vendor_attribute
=
517 static struct kobj_attribute device_attribute
=
519 static struct kobj_attribute class_attribute
=
522 static struct attribute
*afu_cr_attrs
[] = {
523 &vendor_attribute
.attr
,
524 &device_attribute
.attr
,
525 &class_attribute
.attr
,
529 static void release_afu_config_record(struct kobject
*kobj
)
531 struct afu_config_record
*cr
= to_cr(kobj
);
536 static struct kobj_type afu_config_record_type
= {
537 .sysfs_ops
= &kobj_sysfs_ops
,
538 .release
= release_afu_config_record
,
539 .default_attrs
= afu_cr_attrs
,
542 static struct afu_config_record
*cxl_sysfs_afu_new_cr(struct cxl_afu
*afu
, int cr_idx
)
544 struct afu_config_record
*cr
;
547 cr
= kzalloc(sizeof(struct afu_config_record
), GFP_KERNEL
);
549 return ERR_PTR(-ENOMEM
);
553 rc
= cxl_ops
->afu_cr_read16(afu
, cr_idx
, PCI_DEVICE_ID
, &cr
->device
);
556 rc
= cxl_ops
->afu_cr_read16(afu
, cr_idx
, PCI_VENDOR_ID
, &cr
->vendor
);
559 rc
= cxl_ops
->afu_cr_read32(afu
, cr_idx
, PCI_CLASS_REVISION
, &cr
->class);
565 * Export raw AFU PCIe like config record. For now this is read only by
566 * root - we can expand that later to be readable by non-root and maybe
567 * even writable provided we have a good use-case. Once we support
568 * exposing AFUs through a virtual PHB they will get that for free from
569 * Linux' PCI infrastructure, but until then it's not clear that we
570 * need it for anything since the main use case is just identifying
571 * AFUs, which can be done via the vendor, device and class attributes.
573 sysfs_bin_attr_init(&cr
->config_attr
);
574 cr
->config_attr
.attr
.name
= "config";
575 cr
->config_attr
.attr
.mode
= S_IRUSR
;
576 cr
->config_attr
.size
= afu
->crs_len
;
577 cr
->config_attr
.read
= afu_read_config
;
579 rc
= kobject_init_and_add(&cr
->kobj
, &afu_config_record_type
,
580 &afu
->dev
.kobj
, "cr%i", cr
->cr
);
584 rc
= sysfs_create_bin_file(&cr
->kobj
, &cr
->config_attr
);
588 rc
= kobject_uevent(&cr
->kobj
, KOBJ_ADD
);
594 sysfs_remove_bin_file(&cr
->kobj
, &cr
->config_attr
);
596 kobject_put(&cr
->kobj
);
603 void cxl_sysfs_afu_remove(struct cxl_afu
*afu
)
605 struct device_attribute
*dev_attr
;
606 struct afu_config_record
*cr
, *tmp
;
609 /* remove the err buffer bin attribute */
611 device_remove_bin_file(&afu
->dev
, &afu
->attr_eb
);
613 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++) {
614 dev_attr
= &afu_attrs
[i
];
615 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
617 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
620 list_for_each_entry_safe(cr
, tmp
, &afu
->crs
, list
) {
621 sysfs_remove_bin_file(&cr
->kobj
, &cr
->config_attr
);
622 kobject_put(&cr
->kobj
);
626 int cxl_sysfs_afu_add(struct cxl_afu
*afu
)
628 struct device_attribute
*dev_attr
;
629 struct afu_config_record
*cr
;
632 INIT_LIST_HEAD(&afu
->crs
);
634 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++) {
635 dev_attr
= &afu_attrs
[i
];
636 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
638 if ((rc
= device_create_file(&afu
->dev
, &afu_attrs
[i
])))
643 /* conditionally create the add the binary file for error info buffer */
645 sysfs_attr_init(&afu
->attr_eb
.attr
);
647 afu
->attr_eb
.attr
.name
= "afu_err_buff";
648 afu
->attr_eb
.attr
.mode
= S_IRUGO
;
649 afu
->attr_eb
.size
= afu
->eb_len
;
650 afu
->attr_eb
.read
= afu_eb_read
;
652 rc
= device_create_bin_file(&afu
->dev
, &afu
->attr_eb
);
655 "Unable to create eb attr for the afu. Err(%d)\n",
661 for (i
= 0; i
< afu
->crs_num
; i
++) {
662 cr
= cxl_sysfs_afu_new_cr(afu
, i
);
667 list_add(&cr
->list
, &afu
->crs
);
673 cxl_sysfs_afu_remove(afu
);
676 /* reset the eb_len as we havent created the bin attr */
679 for (i
--; i
>= 0; i
--) {
680 dev_attr
= &afu_attrs
[i
];
681 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
683 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
688 int cxl_sysfs_afu_m_add(struct cxl_afu
*afu
)
690 struct device_attribute
*dev_attr
;
693 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++) {
694 dev_attr
= &afu_master_attrs
[i
];
695 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
696 CXL_AFU_MASTER_ATTRS
)) {
697 if ((rc
= device_create_file(afu
->chardev_m
, &afu_master_attrs
[i
])))
705 for (i
--; i
>= 0; i
--) {
706 dev_attr
= &afu_master_attrs
[i
];
707 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
708 CXL_AFU_MASTER_ATTRS
))
709 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);
714 void cxl_sysfs_afu_m_remove(struct cxl_afu
*afu
)
716 struct device_attribute
*dev_attr
;
719 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++) {
720 dev_attr
= &afu_master_attrs
[i
];
721 if (cxl_ops
->support_attributes(dev_attr
->attr
.name
,
722 CXL_AFU_MASTER_ATTRS
))
723 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);