Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0+
  2// Copyright 2017 IBM Corp.
  3#include <linux/sysfs.h>
  4#include "ocxl_internal.h"
  5
  6static 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
 13static ssize_t global_mmio_size_show(struct device *device,
 14				struct device_attribute *attr,
 15				char *buf)
 16{
 17	struct ocxl_afu *afu = to_afu(device);
 18
 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{
 27	struct ocxl_afu *afu = to_afu(device);
 28
 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{
 37	struct ocxl_afu *afu = to_afu(device);
 38
 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{
 48	struct ocxl_afu *afu = to_afu(device);
 49
 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{
 65	struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
 66
 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{
 96	struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
 97
 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
109int ocxl_sysfs_register_afu(struct ocxl_file_info *info)
110{
111	int i, rc;
112
113	for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
114		rc = device_create_file(&info->dev, &afu_attrs[i]);
115		if (rc)
116			goto err;
117	}
118
119	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);
126	if (rc) {
127		dev_err(&info->dev, "Unable to create global mmio attr for afu: %d\n", rc);
128		goto err;
129	}
130
131	return 0;
132
133err:
134	for (i--; i >= 0; i--)
135		device_remove_file(&info->dev, &afu_attrs[i]);
136
137	return rc;
138}
139
140void ocxl_sysfs_unregister_afu(struct ocxl_file_info *info)
141{
142	int i;
143
144	/*
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	 */
148	for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
149		device_remove_file(&info->dev, &afu_attrs[i]);
150	device_remove_bin_file(&info->dev, &info->attr_global_mmio);
151}