sysfs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/sysfs.h>
  4. #include "ocxl_internal.h"
  5. static ssize_t global_mmio_size_show(struct device *device,
  6. struct device_attribute *attr,
  7. char *buf)
  8. {
  9. struct ocxl_afu *afu = to_ocxl_afu(device);
  10. return scnprintf(buf, PAGE_SIZE, "%d\n",
  11. afu->config.global_mmio_size);
  12. }
  13. static ssize_t pp_mmio_size_show(struct device *device,
  14. struct device_attribute *attr,
  15. char *buf)
  16. {
  17. struct ocxl_afu *afu = to_ocxl_afu(device);
  18. return scnprintf(buf, PAGE_SIZE, "%d\n",
  19. afu->config.pp_mmio_stride);
  20. }
  21. static ssize_t afu_version_show(struct device *device,
  22. struct device_attribute *attr,
  23. char *buf)
  24. {
  25. struct ocxl_afu *afu = to_ocxl_afu(device);
  26. return scnprintf(buf, PAGE_SIZE, "%hhu:%hhu\n",
  27. afu->config.version_major,
  28. afu->config.version_minor);
  29. }
  30. static ssize_t contexts_show(struct device *device,
  31. struct device_attribute *attr,
  32. char *buf)
  33. {
  34. struct ocxl_afu *afu = to_ocxl_afu(device);
  35. return scnprintf(buf, PAGE_SIZE, "%d/%d\n",
  36. afu->pasid_count, afu->pasid_max);
  37. }
  38. static struct device_attribute afu_attrs[] = {
  39. __ATTR_RO(global_mmio_size),
  40. __ATTR_RO(pp_mmio_size),
  41. __ATTR_RO(afu_version),
  42. __ATTR_RO(contexts),
  43. };
  44. static ssize_t global_mmio_read(struct file *filp, struct kobject *kobj,
  45. struct bin_attribute *bin_attr, char *buf,
  46. loff_t off, size_t count)
  47. {
  48. struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
  49. if (count == 0 || off < 0 ||
  50. off >= afu->config.global_mmio_size)
  51. return 0;
  52. memcpy_fromio(buf, afu->global_mmio_ptr + off, count);
  53. return count;
  54. }
  55. static vm_fault_t global_mmio_fault(struct vm_fault *vmf)
  56. {
  57. struct vm_area_struct *vma = vmf->vma;
  58. struct ocxl_afu *afu = vma->vm_private_data;
  59. unsigned long offset;
  60. if (vmf->pgoff >= (afu->config.global_mmio_size >> PAGE_SHIFT))
  61. return VM_FAULT_SIGBUS;
  62. offset = vmf->pgoff;
  63. offset += (afu->global_mmio_start >> PAGE_SHIFT);
  64. return vmf_insert_pfn(vma, vmf->address, offset);
  65. }
  66. static const struct vm_operations_struct global_mmio_vmops = {
  67. .fault = global_mmio_fault,
  68. };
  69. static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
  70. struct bin_attribute *bin_attr,
  71. struct vm_area_struct *vma)
  72. {
  73. struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
  74. if ((vma_pages(vma) + vma->vm_pgoff) >
  75. (afu->config.global_mmio_size >> PAGE_SHIFT))
  76. return -EINVAL;
  77. vma->vm_flags |= VM_IO | VM_PFNMAP;
  78. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  79. vma->vm_ops = &global_mmio_vmops;
  80. vma->vm_private_data = afu;
  81. return 0;
  82. }
  83. int ocxl_sysfs_add_afu(struct ocxl_afu *afu)
  84. {
  85. int i, rc;
  86. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  87. rc = device_create_file(&afu->dev, &afu_attrs[i]);
  88. if (rc)
  89. goto err;
  90. }
  91. sysfs_attr_init(&afu->attr_global_mmio.attr);
  92. afu->attr_global_mmio.attr.name = "global_mmio_area";
  93. afu->attr_global_mmio.attr.mode = 0600;
  94. afu->attr_global_mmio.size = afu->config.global_mmio_size;
  95. afu->attr_global_mmio.read = global_mmio_read;
  96. afu->attr_global_mmio.mmap = global_mmio_mmap;
  97. rc = device_create_bin_file(&afu->dev, &afu->attr_global_mmio);
  98. if (rc) {
  99. dev_err(&afu->dev,
  100. "Unable to create global mmio attr for afu: %d\n",
  101. rc);
  102. goto err;
  103. }
  104. return 0;
  105. err:
  106. for (i--; i >= 0; i--)
  107. device_remove_file(&afu->dev, &afu_attrs[i]);
  108. return rc;
  109. }
  110. void ocxl_sysfs_remove_afu(struct ocxl_afu *afu)
  111. {
  112. int i;
  113. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
  114. device_remove_file(&afu->dev, &afu_attrs[i]);
  115. device_remove_bin_file(&afu->dev, &afu->attr_global_mmio);
  116. }