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);
67 /* Recompute the status only in native mode */
68 if (cpu_has_feature(CPU_FTR_HVMODE)) {
69 psl_tb = adapter->native->sl_ops->timebase_read(adapter);
70 delta = abs(mftb() - psl_tb);
72 /* CORE TB and PSL TB difference <= 16usecs ? */
73 adapter->psl_timebase_synced = (tb_to_ns(delta) < 16000) ? true : false;
74 pr_devel("PSL timebase %s - delta: 0x%016llx\n",
75 (tb_to_ns(delta) < 16000) ? "synchronized" :
76 "not synchronized", tb_to_ns(delta));
78 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_timebase_synced);
81 static ssize_t reset_adapter_store(struct device *device,
82 struct device_attribute *attr,
83 const char *buf, size_t count)
85 struct cxl *adapter = to_cxl_adapter(device);
89 rc = sscanf(buf, "%i", &val);
90 if ((rc != 1) || (val != 1 && val != -1))
94 * See if we can lock the context mapping that's only allowed
95 * when there are no contexts attached to the adapter. Once
96 * taken this will also prevent any context from getting activated.
99 rc = cxl_adapter_context_lock(adapter);
103 rc = cxl_ops->adapter_reset(adapter);
104 /* In case reset failed release context lock */
106 cxl_adapter_context_unlock(adapter);
108 } else if (val == -1) {
109 /* Perform a forced adapter reset */
110 rc = cxl_ops->adapter_reset(adapter);
114 return rc ? rc : count;
117 static ssize_t load_image_on_perst_show(struct device *device,
118 struct device_attribute *attr,
121 struct cxl *adapter = to_cxl_adapter(device);
123 if (!adapter->perst_loads_image)
124 return scnprintf(buf, PAGE_SIZE, "none\n");
126 if (adapter->perst_select_user)
127 return scnprintf(buf, PAGE_SIZE, "user\n");
128 return scnprintf(buf, PAGE_SIZE, "factory\n");
131 static ssize_t load_image_on_perst_store(struct device *device,
132 struct device_attribute *attr,
133 const char *buf, size_t count)
135 struct cxl *adapter = to_cxl_adapter(device);
138 if (!strncmp(buf, "none", 4))
139 adapter->perst_loads_image = false;
140 else if (!strncmp(buf, "user", 4)) {
141 adapter->perst_select_user = true;
142 adapter->perst_loads_image = true;
143 } else if (!strncmp(buf, "factory", 7)) {
144 adapter->perst_select_user = false;
145 adapter->perst_loads_image = true;
149 if ((rc = cxl_update_image_control(adapter)))
155 static ssize_t perst_reloads_same_image_show(struct device *device,
156 struct device_attribute *attr,
159 struct cxl *adapter = to_cxl_adapter(device);
161 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->perst_same_image);
164 static ssize_t perst_reloads_same_image_store(struct device *device,
165 struct device_attribute *attr,
166 const char *buf, size_t count)
168 struct cxl *adapter = to_cxl_adapter(device);
172 rc = sscanf(buf, "%i", &val);
173 if ((rc != 1) || !(val == 1 || val == 0))
176 adapter->perst_same_image = (val == 1 ? true : false);
180 static struct device_attribute adapter_attrs[] = {
181 __ATTR_RO(caia_version),
182 __ATTR_RO(psl_revision),
183 __ATTR_RO(base_image),
184 __ATTR_RO(image_loaded),
185 __ATTR_RO(psl_timebase_synced),
186 __ATTR_RW(load_image_on_perst),
187 __ATTR_RW(perst_reloads_same_image),
188 __ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
192 /********* AFU master specific attributes **********************************/
194 static ssize_t mmio_size_show_master(struct device *device,
195 struct device_attribute *attr,
198 struct cxl_afu *afu = to_afu_chardev_m(device);
200 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
203 static ssize_t pp_mmio_off_show(struct device *device,
204 struct device_attribute *attr,
207 struct cxl_afu *afu = to_afu_chardev_m(device);
209 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->native->pp_offset);
212 static ssize_t pp_mmio_len_show(struct device *device,
213 struct device_attribute *attr,
216 struct cxl_afu *afu = to_afu_chardev_m(device);
218 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
221 static struct device_attribute afu_master_attrs[] = {
222 __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
223 __ATTR_RO(pp_mmio_off),
224 __ATTR_RO(pp_mmio_len),
228 /********* AFU attributes **************************************************/
230 static ssize_t mmio_size_show(struct device *device,
231 struct device_attribute *attr,
234 struct cxl_afu *afu = to_cxl_afu(device);
237 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
238 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
241 static ssize_t reset_store_afu(struct device *device,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
245 struct cxl_afu *afu = to_cxl_afu(device);
248 /* Not safe to reset if it is currently in use */
249 mutex_lock(&afu->contexts_lock);
250 if (!idr_is_empty(&afu->contexts_idr)) {
255 if ((rc = cxl_ops->afu_reset(afu)))
260 mutex_unlock(&afu->contexts_lock);
264 static ssize_t irqs_min_show(struct device *device,
265 struct device_attribute *attr,
268 struct cxl_afu *afu = to_cxl_afu(device);
270 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
273 static ssize_t irqs_max_show(struct device *device,
274 struct device_attribute *attr,
277 struct cxl_afu *afu = to_cxl_afu(device);
279 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
282 static ssize_t irqs_max_store(struct device *device,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct cxl_afu *afu = to_cxl_afu(device);
290 ret = sscanf(buf, "%i", &irqs_max);
294 if (irqs_max < afu->pp_irqs)
297 if (cpu_has_feature(CPU_FTR_HVMODE)) {
298 if (irqs_max > afu->adapter->user_irqs)
301 /* pHyp sets a per-AFU limit */
302 if (irqs_max > afu->guest->max_ints)
306 afu->irqs_max = irqs_max;
310 static ssize_t modes_supported_show(struct device *device,
311 struct device_attribute *attr, char *buf)
313 struct cxl_afu *afu = to_cxl_afu(device);
314 char *p = buf, *end = buf + PAGE_SIZE;
316 if (afu->modes_supported & CXL_MODE_DEDICATED)
317 p += scnprintf(p, end - p, "dedicated_process\n");
318 if (afu->modes_supported & CXL_MODE_DIRECTED)
319 p += scnprintf(p, end - p, "afu_directed\n");
323 static ssize_t prefault_mode_show(struct device *device,
324 struct device_attribute *attr,
327 struct cxl_afu *afu = to_cxl_afu(device);
329 switch (afu->prefault_mode) {
330 case CXL_PREFAULT_WED:
331 return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
332 case CXL_PREFAULT_ALL:
333 return scnprintf(buf, PAGE_SIZE, "all\n");
335 return scnprintf(buf, PAGE_SIZE, "none\n");
339 static ssize_t prefault_mode_store(struct device *device,
340 struct device_attribute *attr,
341 const char *buf, size_t count)
343 struct cxl_afu *afu = to_cxl_afu(device);
344 enum prefault_modes mode = -1;
346 if (!strncmp(buf, "work_element_descriptor", 23))
347 mode = CXL_PREFAULT_WED;
348 if (!strncmp(buf, "all", 3))
349 mode = CXL_PREFAULT_ALL;
350 if (!strncmp(buf, "none", 4))
351 mode = CXL_PREFAULT_NONE;
356 afu->prefault_mode = mode;
360 static ssize_t mode_show(struct device *device,
361 struct device_attribute *attr,
364 struct cxl_afu *afu = to_cxl_afu(device);
366 if (afu->current_mode == CXL_MODE_DEDICATED)
367 return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
368 if (afu->current_mode == CXL_MODE_DIRECTED)
369 return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
370 return scnprintf(buf, PAGE_SIZE, "none\n");
373 static ssize_t mode_store(struct device *device, struct device_attribute *attr,
374 const char *buf, size_t count)
376 struct cxl_afu *afu = to_cxl_afu(device);
377 int old_mode, mode = -1;
380 /* can't change this if we have a user */
381 mutex_lock(&afu->contexts_lock);
382 if (!idr_is_empty(&afu->contexts_idr))
385 if (!strncmp(buf, "dedicated_process", 17))
386 mode = CXL_MODE_DEDICATED;
387 if (!strncmp(buf, "afu_directed", 12))
388 mode = CXL_MODE_DIRECTED;
389 if (!strncmp(buf, "none", 4))
398 * afu_deactivate_mode needs to be done outside the lock, prevent
399 * other contexts coming in before we are ready:
401 old_mode = afu->current_mode;
402 afu->current_mode = 0;
405 mutex_unlock(&afu->contexts_lock);
407 if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
409 if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
414 mutex_unlock(&afu->contexts_lock);
418 static ssize_t api_version_show(struct device *device,
419 struct device_attribute *attr,
422 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
425 static ssize_t api_version_compatible_show(struct device *device,
426 struct device_attribute *attr,
429 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
432 static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
433 struct bin_attribute *bin_attr, char *buf,
434 loff_t off, size_t count)
436 struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
438 return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
441 static struct device_attribute afu_attrs[] = {
442 __ATTR_RO(mmio_size),
445 __ATTR_RO(modes_supported),
447 __ATTR_RW(prefault_mode),
448 __ATTR_RO(api_version),
449 __ATTR_RO(api_version_compatible),
450 __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
453 int cxl_sysfs_adapter_add(struct cxl *adapter)
455 struct device_attribute *dev_attr;
458 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
459 dev_attr = &adapter_attrs[i];
460 if (cxl_ops->support_attributes(dev_attr->attr.name,
461 CXL_ADAPTER_ATTRS)) {
462 if ((rc = device_create_file(&adapter->dev, dev_attr)))
468 for (i--; i >= 0; i--) {
469 dev_attr = &adapter_attrs[i];
470 if (cxl_ops->support_attributes(dev_attr->attr.name,
472 device_remove_file(&adapter->dev, dev_attr);
477 void cxl_sysfs_adapter_remove(struct cxl *adapter)
479 struct device_attribute *dev_attr;
482 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
483 dev_attr = &adapter_attrs[i];
484 if (cxl_ops->support_attributes(dev_attr->attr.name,
486 device_remove_file(&adapter->dev, dev_attr);
490 struct afu_config_record {
492 struct bin_attribute config_attr;
493 struct list_head list;
500 #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
502 static ssize_t vendor_show(struct kobject *kobj,
503 struct kobj_attribute *attr, char *buf)
505 struct afu_config_record *cr = to_cr(kobj);
507 return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
510 static ssize_t device_show(struct kobject *kobj,
511 struct kobj_attribute *attr, char *buf)
513 struct afu_config_record *cr = to_cr(kobj);
515 return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
518 static ssize_t class_show(struct kobject *kobj,
519 struct kobj_attribute *attr, char *buf)
521 struct afu_config_record *cr = to_cr(kobj);
523 return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
526 static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
527 struct bin_attribute *bin_attr, char *buf,
528 loff_t off, size_t count)
530 struct afu_config_record *cr = to_cr(kobj);
531 struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
535 for (i = 0; i < count;) {
536 rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
539 for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
540 buf[i] = (val >> (j * 8)) & 0xff;
546 static struct kobj_attribute vendor_attribute =
548 static struct kobj_attribute device_attribute =
550 static struct kobj_attribute class_attribute =
553 static struct attribute *afu_cr_attrs[] = {
554 &vendor_attribute.attr,
555 &device_attribute.attr,
556 &class_attribute.attr,
560 static void release_afu_config_record(struct kobject *kobj)
562 struct afu_config_record *cr = to_cr(kobj);
567 static struct kobj_type afu_config_record_type = {
568 .sysfs_ops = &kobj_sysfs_ops,
569 .release = release_afu_config_record,
570 .default_attrs = afu_cr_attrs,
573 static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
575 struct afu_config_record *cr;
578 cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
580 return ERR_PTR(-ENOMEM);
584 rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
587 rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
590 rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
596 * Export raw AFU PCIe like config record. For now this is read only by
597 * root - we can expand that later to be readable by non-root and maybe
598 * even writable provided we have a good use-case. Once we support
599 * exposing AFUs through a virtual PHB they will get that for free from
600 * Linux' PCI infrastructure, but until then it's not clear that we
601 * need it for anything since the main use case is just identifying
602 * AFUs, which can be done via the vendor, device and class attributes.
604 sysfs_bin_attr_init(&cr->config_attr);
605 cr->config_attr.attr.name = "config";
606 cr->config_attr.attr.mode = S_IRUSR;
607 cr->config_attr.size = afu->crs_len;
608 cr->config_attr.read = afu_read_config;
610 rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
611 &afu->dev.kobj, "cr%i", cr->cr);
615 rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
619 rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
625 sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
627 kobject_put(&cr->kobj);
634 void cxl_sysfs_afu_remove(struct cxl_afu *afu)
636 struct device_attribute *dev_attr;
637 struct afu_config_record *cr, *tmp;
640 /* remove the err buffer bin attribute */
642 device_remove_bin_file(&afu->dev, &afu->attr_eb);
644 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
645 dev_attr = &afu_attrs[i];
646 if (cxl_ops->support_attributes(dev_attr->attr.name,
648 device_remove_file(&afu->dev, &afu_attrs[i]);
651 list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
652 sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
653 kobject_put(&cr->kobj);
657 int cxl_sysfs_afu_add(struct cxl_afu *afu)
659 struct device_attribute *dev_attr;
660 struct afu_config_record *cr;
663 INIT_LIST_HEAD(&afu->crs);
665 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
666 dev_attr = &afu_attrs[i];
667 if (cxl_ops->support_attributes(dev_attr->attr.name,
669 if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
674 /* conditionally create the add the binary file for error info buffer */
676 sysfs_attr_init(&afu->attr_eb.attr);
678 afu->attr_eb.attr.name = "afu_err_buff";
679 afu->attr_eb.attr.mode = S_IRUGO;
680 afu->attr_eb.size = afu->eb_len;
681 afu->attr_eb.read = afu_eb_read;
683 rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
686 "Unable to create eb attr for the afu. Err(%d)\n",
692 for (i = 0; i < afu->crs_num; i++) {
693 cr = cxl_sysfs_afu_new_cr(afu, i);
698 list_add(&cr->list, &afu->crs);
704 cxl_sysfs_afu_remove(afu);
707 /* reset the eb_len as we havent created the bin attr */
710 for (i--; i >= 0; i--) {
711 dev_attr = &afu_attrs[i];
712 if (cxl_ops->support_attributes(dev_attr->attr.name,
714 device_remove_file(&afu->dev, &afu_attrs[i]);
719 int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
721 struct device_attribute *dev_attr;
724 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
725 dev_attr = &afu_master_attrs[i];
726 if (cxl_ops->support_attributes(dev_attr->attr.name,
727 CXL_AFU_MASTER_ATTRS)) {
728 if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
736 for (i--; i >= 0; i--) {
737 dev_attr = &afu_master_attrs[i];
738 if (cxl_ops->support_attributes(dev_attr->attr.name,
739 CXL_AFU_MASTER_ATTRS))
740 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
745 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
747 struct device_attribute *dev_attr;
750 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
751 dev_attr = &afu_master_attrs[i];
752 if (cxl_ops->support_attributes(dev_attr->attr.name,
753 CXL_AFU_MASTER_ATTRS))
754 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);