0001
0002
0003 #include <linux/sysfs.h>
0004 #include "ocxl_internal.h"
0005
0006 static inline struct ocxl_afu *to_afu(struct device *device)
0007 {
0008 struct ocxl_file_info *info = container_of(device, struct ocxl_file_info, dev);
0009
0010 return info->afu;
0011 }
0012
0013 static ssize_t global_mmio_size_show(struct device *device,
0014 struct device_attribute *attr,
0015 char *buf)
0016 {
0017 struct ocxl_afu *afu = to_afu(device);
0018
0019 return scnprintf(buf, PAGE_SIZE, "%d\n",
0020 afu->config.global_mmio_size);
0021 }
0022
0023 static ssize_t pp_mmio_size_show(struct device *device,
0024 struct device_attribute *attr,
0025 char *buf)
0026 {
0027 struct ocxl_afu *afu = to_afu(device);
0028
0029 return scnprintf(buf, PAGE_SIZE, "%d\n",
0030 afu->config.pp_mmio_stride);
0031 }
0032
0033 static ssize_t afu_version_show(struct device *device,
0034 struct device_attribute *attr,
0035 char *buf)
0036 {
0037 struct ocxl_afu *afu = to_afu(device);
0038
0039 return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n",
0040 afu->config.version_major,
0041 afu->config.version_minor);
0042 }
0043
0044 static ssize_t contexts_show(struct device *device,
0045 struct device_attribute *attr,
0046 char *buf)
0047 {
0048 struct ocxl_afu *afu = to_afu(device);
0049
0050 return scnprintf(buf, PAGE_SIZE, "%d/%d\n",
0051 afu->pasid_count, afu->pasid_max);
0052 }
0053
0054 static ssize_t reload_on_reset_show(struct device *device,
0055 struct device_attribute *attr,
0056 char *buf)
0057 {
0058 struct ocxl_afu *afu = to_afu(device);
0059 struct ocxl_fn *fn = afu->fn;
0060 struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
0061 int val;
0062
0063 if (ocxl_config_get_reset_reload(pci_dev, &val))
0064 return scnprintf(buf, PAGE_SIZE, "unavailable\n");
0065
0066 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
0067 }
0068
0069 static ssize_t reload_on_reset_store(struct device *device,
0070 struct device_attribute *attr,
0071 const char *buf, size_t count)
0072 {
0073 struct ocxl_afu *afu = to_afu(device);
0074 struct ocxl_fn *fn = afu->fn;
0075 struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
0076 int rc, val;
0077
0078 rc = kstrtoint(buf, 0, &val);
0079 if (rc || (val != 0 && val != 1))
0080 return -EINVAL;
0081
0082 if (ocxl_config_set_reset_reload(pci_dev, val))
0083 return -ENODEV;
0084
0085 return count;
0086 }
0087
0088 static struct device_attribute afu_attrs[] = {
0089 __ATTR_RO(global_mmio_size),
0090 __ATTR_RO(pp_mmio_size),
0091 __ATTR_RO(afu_version),
0092 __ATTR_RO(contexts),
0093 __ATTR_RW(reload_on_reset),
0094 };
0095
0096 static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj,
0097 struct bin_attribute *bin_attr, char *buf,
0098 loff_t off, size_t count)
0099 {
0100 struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
0101
0102 if (count == 0 || off < 0 ||
0103 off >= afu->config.global_mmio_size)
0104 return 0;
0105 memcpy_fromio(buf, afu->global_mmio_ptr + off, count);
0106 return count;
0107 }
0108
0109 static vm_fault_t global_mmio_fault(struct vm_fault *vmf)
0110 {
0111 struct vm_area_struct *vma = vmf->vma;
0112 struct ocxl_afu *afu = vma->vm_private_data;
0113 unsigned long offset;
0114
0115 if (vmf->pgoff >= (afu->config.global_mmio_size >> PAGE_SHIFT))
0116 return VM_FAULT_SIGBUS;
0117
0118 offset = vmf->pgoff;
0119 offset += (afu->global_mmio_start >> PAGE_SHIFT);
0120 return vmf_insert_pfn(vma, vmf->address, offset);
0121 }
0122
0123 static const struct vm_operations_struct global_mmio_vmops = {
0124 .fault = global_mmio_fault,
0125 };
0126
0127 static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
0128 struct bin_attribute *bin_attr,
0129 struct vm_area_struct *vma)
0130 {
0131 struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
0132
0133 if ((vma_pages(vma) + vma->vm_pgoff) >
0134 (afu->config.global_mmio_size >> PAGE_SHIFT))
0135 return -EINVAL;
0136
0137 vma->vm_flags |= VM_IO | VM_PFNMAP;
0138 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0139 vma->vm_ops = &global_mmio_vmops;
0140 vma->vm_private_data = afu;
0141 return 0;
0142 }
0143
0144 int ocxl_sysfs_register_afu(struct ocxl_file_info *info)
0145 {
0146 int i, rc;
0147
0148 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
0149 rc = device_create_file(&info->dev, &afu_attrs[i]);
0150 if (rc)
0151 goto err;
0152 }
0153
0154 sysfs_attr_init(&info->attr_global_mmio.attr);
0155 info->attr_global_mmio.attr.name = "global_mmio_area";
0156 info->attr_global_mmio.attr.mode = 0600;
0157 info->attr_global_mmio.size = info->afu->config.global_mmio_size;
0158 info->attr_global_mmio.read = global_mmio_read;
0159 info->attr_global_mmio.mmap = global_mmio_mmap;
0160 rc = device_create_bin_file(&info->dev, &info->attr_global_mmio);
0161 if (rc) {
0162 dev_err(&info->dev, "Unable to create global mmio attr for afu: %d\n", rc);
0163 goto err;
0164 }
0165
0166 return 0;
0167
0168 err:
0169 for (i--; i >= 0; i--)
0170 device_remove_file(&info->dev, &afu_attrs[i]);
0171
0172 return rc;
0173 }
0174
0175 void ocxl_sysfs_unregister_afu(struct ocxl_file_info *info)
0176 {
0177 int i;
0178
0179
0180
0181
0182
0183 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
0184 device_remove_file(&info->dev, &afu_attrs[i]);
0185 device_remove_bin_file(&info->dev, &info->attr_global_mmio);
0186 }