blob: 58f1ba2642068e8ece7bbd8d3bccdf2e10feaba9 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2// Copyright 2017 IBM Corp.
3#include <linux/sysfs.h>
4#include "ocxl_internal.h"
5
David Brazdil0f672f62019-12-10 10:32:29 +00006static inline struct ocxl_afu *to_afu(struct device *device)
7{
8 struct ocxl_file_info *info = container_of(device, struct ocxl_file_info, dev);
9
10 return info->afu;
11}
12
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013static ssize_t global_mmio_size_show(struct device *device,
14 struct device_attribute *attr,
15 char *buf)
16{
David Brazdil0f672f62019-12-10 10:32:29 +000017 struct ocxl_afu *afu = to_afu(device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
19 return scnprintf(buf, PAGE_SIZE, "%d\n",
20 afu->config.global_mmio_size);
21}
22
23static ssize_t pp_mmio_size_show(struct device *device,
24 struct device_attribute *attr,
25 char *buf)
26{
David Brazdil0f672f62019-12-10 10:32:29 +000027 struct ocxl_afu *afu = to_afu(device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29 return scnprintf(buf, PAGE_SIZE, "%d\n",
30 afu->config.pp_mmio_stride);
31}
32
33static ssize_t afu_version_show(struct device *device,
34 struct device_attribute *attr,
35 char *buf)
36{
David Brazdil0f672f62019-12-10 10:32:29 +000037 struct ocxl_afu *afu = to_afu(device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038
39 return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n",
40 afu->config.version_major,
41 afu->config.version_minor);
42}
43
44static ssize_t contexts_show(struct device *device,
45 struct device_attribute *attr,
46 char *buf)
47{
David Brazdil0f672f62019-12-10 10:32:29 +000048 struct ocxl_afu *afu = to_afu(device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049
50 return scnprintf(buf, PAGE_SIZE, "%d/%d\n",
51 afu->pasid_count, afu->pasid_max);
52}
53
54static struct device_attribute afu_attrs[] = {
55 __ATTR_RO(global_mmio_size),
56 __ATTR_RO(pp_mmio_size),
57 __ATTR_RO(afu_version),
58 __ATTR_RO(contexts),
59};
60
61static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj,
62 struct bin_attribute *bin_attr, char *buf,
63 loff_t off, size_t count)
64{
David Brazdil0f672f62019-12-10 10:32:29 +000065 struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
67 if (count == 0 || off < 0 ||
68 off >= afu->config.global_mmio_size)
69 return 0;
70 memcpy_fromio(buf, afu->global_mmio_ptr + off, count);
71 return count;
72}
73
74static vm_fault_t global_mmio_fault(struct vm_fault *vmf)
75{
76 struct vm_area_struct *vma = vmf->vma;
77 struct ocxl_afu *afu = vma->vm_private_data;
78 unsigned long offset;
79
80 if (vmf->pgoff >= (afu->config.global_mmio_size >> PAGE_SHIFT))
81 return VM_FAULT_SIGBUS;
82
83 offset = vmf->pgoff;
84 offset += (afu->global_mmio_start >> PAGE_SHIFT);
85 return vmf_insert_pfn(vma, vmf->address, offset);
86}
87
88static const struct vm_operations_struct global_mmio_vmops = {
89 .fault = global_mmio_fault,
90};
91
92static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
93 struct bin_attribute *bin_attr,
94 struct vm_area_struct *vma)
95{
David Brazdil0f672f62019-12-10 10:32:29 +000096 struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
98 if ((vma_pages(vma) + vma->vm_pgoff) >
99 (afu->config.global_mmio_size >> PAGE_SHIFT))
100 return -EINVAL;
101
102 vma->vm_flags |= VM_IO | VM_PFNMAP;
103 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
104 vma->vm_ops = &global_mmio_vmops;
105 vma->vm_private_data = afu;
106 return 0;
107}
108
David Brazdil0f672f62019-12-10 10:32:29 +0000109int ocxl_sysfs_register_afu(struct ocxl_file_info *info)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110{
111 int i, rc;
112
113 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000114 rc = device_create_file(&info->dev, &afu_attrs[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 if (rc)
116 goto err;
117 }
118
David Brazdil0f672f62019-12-10 10:32:29 +0000119 sysfs_attr_init(&info->attr_global_mmio.attr);
120 info->attr_global_mmio.attr.name = "global_mmio_area";
121 info->attr_global_mmio.attr.mode = 0600;
122 info->attr_global_mmio.size = info->afu->config.global_mmio_size;
123 info->attr_global_mmio.read = global_mmio_read;
124 info->attr_global_mmio.mmap = global_mmio_mmap;
125 rc = device_create_bin_file(&info->dev, &info->attr_global_mmio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +0000127 dev_err(&info->dev, "Unable to create global mmio attr for afu: %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 goto err;
129 }
130
131 return 0;
132
133err:
134 for (i--; i >= 0; i--)
David Brazdil0f672f62019-12-10 10:32:29 +0000135 device_remove_file(&info->dev, &afu_attrs[i]);
136
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137 return rc;
138}
139
David Brazdil0f672f62019-12-10 10:32:29 +0000140void ocxl_sysfs_unregister_afu(struct ocxl_file_info *info)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141{
142 int i;
143
David Brazdil0f672f62019-12-10 10:32:29 +0000144 /*
145 * device_remove_bin_file is safe to call if the file is not added as
146 * the files are removed by name, and early exit if not found
147 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
David Brazdil0f672f62019-12-10 10:32:29 +0000149 device_remove_file(&info->dev, &afu_attrs[i]);
150 device_remove_bin_file(&info->dev, &info->attr_global_mmio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151}