sysfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright 2014 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/sysfs.h>
  12. #include "cxl.h"
  13. #define to_afu_chardev_m(d) dev_get_drvdata(d)
  14. /********* Adapter attributes **********************************************/
  15. static ssize_t caia_version_show(struct device *device,
  16. struct device_attribute *attr,
  17. char *buf)
  18. {
  19. struct cxl *adapter = to_cxl_adapter(device);
  20. return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
  21. adapter->caia_minor);
  22. }
  23. static ssize_t psl_revision_show(struct device *device,
  24. struct device_attribute *attr,
  25. char *buf)
  26. {
  27. struct cxl *adapter = to_cxl_adapter(device);
  28. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
  29. }
  30. static ssize_t base_image_show(struct device *device,
  31. struct device_attribute *attr,
  32. char *buf)
  33. {
  34. struct cxl *adapter = to_cxl_adapter(device);
  35. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
  36. }
  37. static ssize_t image_loaded_show(struct device *device,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. struct cxl *adapter = to_cxl_adapter(device);
  42. if (adapter->user_image_loaded)
  43. return scnprintf(buf, PAGE_SIZE, "user\n");
  44. return scnprintf(buf, PAGE_SIZE, "factory\n");
  45. }
  46. static struct device_attribute adapter_attrs[] = {
  47. __ATTR_RO(caia_version),
  48. __ATTR_RO(psl_revision),
  49. __ATTR_RO(base_image),
  50. __ATTR_RO(image_loaded),
  51. };
  52. /********* AFU master specific attributes **********************************/
  53. static ssize_t mmio_size_show_master(struct device *device,
  54. struct device_attribute *attr,
  55. char *buf)
  56. {
  57. struct cxl_afu *afu = to_afu_chardev_m(device);
  58. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
  59. }
  60. static ssize_t pp_mmio_off_show(struct device *device,
  61. struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct cxl_afu *afu = to_afu_chardev_m(device);
  65. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_offset);
  66. }
  67. static ssize_t pp_mmio_len_show(struct device *device,
  68. struct device_attribute *attr,
  69. char *buf)
  70. {
  71. struct cxl_afu *afu = to_afu_chardev_m(device);
  72. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
  73. }
  74. static struct device_attribute afu_master_attrs[] = {
  75. __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
  76. __ATTR_RO(pp_mmio_off),
  77. __ATTR_RO(pp_mmio_len),
  78. };
  79. /********* AFU attributes **************************************************/
  80. static ssize_t mmio_size_show(struct device *device,
  81. struct device_attribute *attr,
  82. char *buf)
  83. {
  84. struct cxl_afu *afu = to_cxl_afu(device);
  85. if (afu->pp_size)
  86. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
  87. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
  88. }
  89. static ssize_t reset_store_afu(struct device *device,
  90. struct device_attribute *attr,
  91. const char *buf, size_t count)
  92. {
  93. struct cxl_afu *afu = to_cxl_afu(device);
  94. int rc;
  95. /* Not safe to reset if it is currently in use */
  96. mutex_lock(&afu->contexts_lock);
  97. if (!idr_is_empty(&afu->contexts_idr)) {
  98. rc = -EBUSY;
  99. goto err;
  100. }
  101. if ((rc = cxl_afu_reset(afu)))
  102. goto err;
  103. rc = count;
  104. err:
  105. mutex_unlock(&afu->contexts_lock);
  106. return rc;
  107. }
  108. static ssize_t irqs_min_show(struct device *device,
  109. struct device_attribute *attr,
  110. char *buf)
  111. {
  112. struct cxl_afu *afu = to_cxl_afu(device);
  113. return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
  114. }
  115. static ssize_t irqs_max_show(struct device *device,
  116. struct device_attribute *attr,
  117. char *buf)
  118. {
  119. struct cxl_afu *afu = to_cxl_afu(device);
  120. return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
  121. }
  122. static ssize_t irqs_max_store(struct device *device,
  123. struct device_attribute *attr,
  124. const char *buf, size_t count)
  125. {
  126. struct cxl_afu *afu = to_cxl_afu(device);
  127. ssize_t ret;
  128. int irqs_max;
  129. ret = sscanf(buf, "%i", &irqs_max);
  130. if (ret != 1)
  131. return -EINVAL;
  132. if (irqs_max < afu->pp_irqs)
  133. return -EINVAL;
  134. if (irqs_max > afu->adapter->user_irqs)
  135. return -EINVAL;
  136. afu->irqs_max = irqs_max;
  137. return count;
  138. }
  139. static ssize_t modes_supported_show(struct device *device,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. struct cxl_afu *afu = to_cxl_afu(device);
  143. char *p = buf, *end = buf + PAGE_SIZE;
  144. if (afu->modes_supported & CXL_MODE_DEDICATED)
  145. p += scnprintf(p, end - p, "dedicated_process\n");
  146. if (afu->modes_supported & CXL_MODE_DIRECTED)
  147. p += scnprintf(p, end - p, "afu_directed\n");
  148. return (p - buf);
  149. }
  150. static ssize_t prefault_mode_show(struct device *device,
  151. struct device_attribute *attr,
  152. char *buf)
  153. {
  154. struct cxl_afu *afu = to_cxl_afu(device);
  155. switch (afu->prefault_mode) {
  156. case CXL_PREFAULT_WED:
  157. return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
  158. case CXL_PREFAULT_ALL:
  159. return scnprintf(buf, PAGE_SIZE, "all\n");
  160. default:
  161. return scnprintf(buf, PAGE_SIZE, "none\n");
  162. }
  163. }
  164. static ssize_t prefault_mode_store(struct device *device,
  165. struct device_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. struct cxl_afu *afu = to_cxl_afu(device);
  169. enum prefault_modes mode = -1;
  170. if (!strncmp(buf, "work_element_descriptor", 23))
  171. mode = CXL_PREFAULT_WED;
  172. if (!strncmp(buf, "all", 3))
  173. mode = CXL_PREFAULT_ALL;
  174. if (!strncmp(buf, "none", 4))
  175. mode = CXL_PREFAULT_NONE;
  176. if (mode == -1)
  177. return -EINVAL;
  178. afu->prefault_mode = mode;
  179. return count;
  180. }
  181. static ssize_t mode_show(struct device *device,
  182. struct device_attribute *attr,
  183. char *buf)
  184. {
  185. struct cxl_afu *afu = to_cxl_afu(device);
  186. if (afu->current_mode == CXL_MODE_DEDICATED)
  187. return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
  188. if (afu->current_mode == CXL_MODE_DIRECTED)
  189. return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
  190. return scnprintf(buf, PAGE_SIZE, "none\n");
  191. }
  192. static ssize_t mode_store(struct device *device, struct device_attribute *attr,
  193. const char *buf, size_t count)
  194. {
  195. struct cxl_afu *afu = to_cxl_afu(device);
  196. int old_mode, mode = -1;
  197. int rc = -EBUSY;
  198. /* can't change this if we have a user */
  199. mutex_lock(&afu->contexts_lock);
  200. if (!idr_is_empty(&afu->contexts_idr))
  201. goto err;
  202. if (!strncmp(buf, "dedicated_process", 17))
  203. mode = CXL_MODE_DEDICATED;
  204. if (!strncmp(buf, "afu_directed", 12))
  205. mode = CXL_MODE_DIRECTED;
  206. if (!strncmp(buf, "none", 4))
  207. mode = 0;
  208. if (mode == -1) {
  209. rc = -EINVAL;
  210. goto err;
  211. }
  212. /*
  213. * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
  214. * other contexts coming in before we are ready:
  215. */
  216. old_mode = afu->current_mode;
  217. afu->current_mode = 0;
  218. afu->num_procs = 0;
  219. mutex_unlock(&afu->contexts_lock);
  220. if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
  221. return rc;
  222. if ((rc = cxl_afu_activate_mode(afu, mode)))
  223. return rc;
  224. return count;
  225. err:
  226. mutex_unlock(&afu->contexts_lock);
  227. return rc;
  228. }
  229. static ssize_t api_version_show(struct device *device,
  230. struct device_attribute *attr,
  231. char *buf)
  232. {
  233. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
  234. }
  235. static ssize_t api_version_compatible_show(struct device *device,
  236. struct device_attribute *attr,
  237. char *buf)
  238. {
  239. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
  240. }
  241. static struct device_attribute afu_attrs[] = {
  242. __ATTR_RO(mmio_size),
  243. __ATTR_RO(irqs_min),
  244. __ATTR_RW(irqs_max),
  245. __ATTR_RO(modes_supported),
  246. __ATTR_RW(mode),
  247. __ATTR_RW(prefault_mode),
  248. __ATTR_RO(api_version),
  249. __ATTR_RO(api_version_compatible),
  250. __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
  251. };
  252. int cxl_sysfs_adapter_add(struct cxl *adapter)
  253. {
  254. int i, rc;
  255. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  256. if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
  257. goto err;
  258. }
  259. return 0;
  260. err:
  261. for (i--; i >= 0; i--)
  262. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  263. return rc;
  264. }
  265. void cxl_sysfs_adapter_remove(struct cxl *adapter)
  266. {
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
  269. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  270. }
  271. int cxl_sysfs_afu_add(struct cxl_afu *afu)
  272. {
  273. int i, rc;
  274. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  275. if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
  276. goto err;
  277. }
  278. return 0;
  279. err:
  280. for (i--; i >= 0; i--)
  281. device_remove_file(&afu->dev, &afu_attrs[i]);
  282. return rc;
  283. }
  284. void cxl_sysfs_afu_remove(struct cxl_afu *afu)
  285. {
  286. int i;
  287. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
  288. device_remove_file(&afu->dev, &afu_attrs[i]);
  289. }
  290. int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
  291. {
  292. int i, rc;
  293. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  294. if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
  295. goto err;
  296. }
  297. return 0;
  298. err:
  299. for (i--; i >= 0; i--)
  300. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  301. return rc;
  302. }
  303. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
  304. {
  305. int i;
  306. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
  307. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  308. }