sysfs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 int 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. vm_insert_pfn(vma, vmf->address, offset);
  65. return VM_FAULT_NOPAGE;
  66. }
  67. static const struct vm_operations_struct global_mmio_vmops = {
  68. .fault = global_mmio_fault,
  69. };
  70. static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
  71. struct bin_attribute *bin_attr,
  72. struct vm_area_struct *vma)
  73. {
  74. struct ocxl_afu *afu = to_ocxl_afu(kobj_to_dev(kobj));
  75. if ((vma_pages(vma) + vma->vm_pgoff) >
  76. (afu->config.global_mmio_size >> PAGE_SHIFT))
  77. return -EINVAL;
  78. vma->vm_flags |= VM_IO | VM_PFNMAP;
  79. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  80. vma->vm_ops = &global_mmio_vmops;
  81. vma->vm_private_data = afu;
  82. return 0;
  83. }
  84. int ocxl_sysfs_add_afu(struct ocxl_afu *afu)
  85. {
  86. int i, rc;
  87. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  88. rc = device_create_file(&afu->dev, &afu_attrs[i]);
  89. if (rc)
  90. goto err;
  91. }
  92. sysfs_attr_init(&afu->attr_global_mmio.attr);
  93. afu->attr_global_mmio.attr.name = "global_mmio_area";
  94. afu->attr_global_mmio.attr.mode = 0600;
  95. afu->attr_global_mmio.size = afu->config.global_mmio_size;
  96. afu->attr_global_mmio.read = global_mmio_read;
  97. afu->attr_global_mmio.mmap = global_mmio_mmap;
  98. rc = device_create_bin_file(&afu->dev, &afu->attr_global_mmio);
  99. if (rc) {
  100. dev_err(&afu->dev,
  101. "Unable to create global mmio attr for afu: %d\n",
  102. rc);
  103. goto err;
  104. }
  105. return 0;
  106. err:
  107. for (i--; i >= 0; i--)
  108. device_remove_file(&afu->dev, &afu_attrs[i]);
  109. return rc;
  110. }
  111. void ocxl_sysfs_remove_afu(struct ocxl_afu *afu)
  112. {
  113. int i;
  114. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
  115. device_remove_file(&afu->dev, &afu_attrs[i]);
  116. device_remove_bin_file(&afu->dev, &afu->attr_global_mmio);
  117. }