Linux 4.19.133
[linux/fpc-iii.git] / drivers / misc / ocxl / sysfs.c
blob0ab1fd1b2682668fee6b0a0c2ac9833380e0aaf8
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/sysfs.h>
4 #include "ocxl_internal.h"
6 static ssize_t global_mmio_size_show(struct device *device,
7 struct device_attribute *attr,
8 char *buf)
10 struct ocxl_afu *afu = to_ocxl_afu(device);
12 return scnprintf(buf, PAGE_SIZE, "%d\n",
13 afu->config.global_mmio_size);
16 static ssize_t pp_mmio_size_show(struct device *device,
17 struct device_attribute *attr,
18 char *buf)
20 struct ocxl_afu *afu = to_ocxl_afu(device);
22 return scnprintf(buf, PAGE_SIZE, "%d\n",
23 afu->config.pp_mmio_stride);
26 static ssize_t afu_version_show(struct device *device,
27 struct device_attribute *attr,
28 char *buf)
30 struct ocxl_afu *afu = to_ocxl_afu(device);
32 return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n",
33 afu->config.version_major,
34 afu->config.version_minor);
37 static ssize_t contexts_show(struct device *device,
38 struct device_attribute *attr,
39 char *buf)
41 struct ocxl_afu *afu = to_ocxl_afu(device);
43 return scnprintf(buf, PAGE_SIZE, "%d/%d\n",
44 afu->pasid_count, afu->pasid_max);
47 static struct device_attribute afu_attrs[] = {
48 __ATTR_RO(global_mmio_size),
49 __ATTR_RO(pp_mmio_size),
50 __ATTR_RO(afu_version),
51 __ATTR_RO(contexts),
54 static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj,
55 struct bin_attribute *bin_attr, char *buf,
56 loff_t off, size_t count)
58 struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
60 if (count == 0 || off < 0 ||
61 off >= afu->config.global_mmio_size)
62 return 0;
63 memcpy_fromio(buf, afu->global_mmio_ptr + off, count);
64 return count;
67 static vm_fault_t global_mmio_fault(struct vm_fault *vmf)
69 struct vm_area_struct *vma = vmf->vma;
70 struct ocxl_afu *afu = vma->vm_private_data;
71 unsigned long offset;
73 if (vmf->pgoff >= (afu->config.global_mmio_size >> PAGE_SHIFT))
74 return VM_FAULT_SIGBUS;
76 offset = vmf->pgoff;
77 offset += (afu->global_mmio_start >> PAGE_SHIFT);
78 return vmf_insert_pfn(vma, vmf->address, offset);
81 static const struct vm_operations_struct global_mmio_vmops = {
82 .fault = global_mmio_fault,
85 static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
86 struct bin_attribute *bin_attr,
87 struct vm_area_struct *vma)
89 struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
91 if ((vma_pages(vma) + vma->vm_pgoff) >
92 (afu->config.global_mmio_size >> PAGE_SHIFT))
93 return -EINVAL;
95 vma->vm_flags |= VM_IO | VM_PFNMAP;
96 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
97 vma->vm_ops = &global_mmio_vmops;
98 vma->vm_private_data = afu;
99 return 0;
102 int ocxl_sysfs_add_afu(struct ocxl_afu *afu)
104 int i, rc;
106 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
107 rc = device_create_file(&afu->dev, &afu_attrs[i]);
108 if (rc)
109 goto err;
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);
119 if (rc) {
120 dev_err(&afu->dev,
121 "Unable to create global mmio attr for afu: %d\n",
122 rc);
123 goto err;
126 return 0;
128 err:
129 for (i--; i >= 0; i--)
130 device_remove_file(&afu->dev, &afu_attrs[i]);
131 return rc;
134 void ocxl_sysfs_remove_afu(struct ocxl_afu *afu)
136 int i;
138 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);