sysfs.c 4.6 KB

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