access_ramoops.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Persistent memory accessor
  3. *
  4. * Copyright (C) 2019 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/fs.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #define DEVICE_NAME "access_ramoops"
  24. #define MAX_OPEN 4
  25. struct access_ramoops_info {
  26. char *name;
  27. phys_addr_t phys;
  28. void *addr;
  29. size_t size;
  30. struct mutex lock;
  31. int is_mapped;
  32. int is_open;
  33. const char *label;
  34. struct miscdevice miscdev;
  35. };
  36. static ssize_t label_show(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct miscdevice *miscdev = dev_get_drvdata(dev);
  40. struct access_ramoops_info *info =
  41. container_of(miscdev, struct access_ramoops_info, miscdev);
  42. return snprintf(buf, PAGE_SIZE, "%s\n", info->label);
  43. }
  44. static ssize_t size_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct miscdevice *miscdev = dev_get_drvdata(dev);
  48. struct access_ramoops_info *info =
  49. container_of(miscdev, struct access_ramoops_info, miscdev);
  50. return snprintf(buf, PAGE_SIZE, "%zd\n", info->size);
  51. }
  52. static DEVICE_ATTR_RO(label);
  53. static DEVICE_ATTR_RO(size);
  54. static struct attribute *access_ramoops_attrs[] = {
  55. &dev_attr_label.attr,
  56. &dev_attr_size.attr,
  57. NULL,
  58. };
  59. ATTRIBUTE_GROUPS(access_ramoops);
  60. static ssize_t access_ramoops_read(struct file *filp, char __user *buf,
  61. size_t count, loff_t *ppos)
  62. {
  63. struct access_ramoops_info *info = filp->private_data;
  64. return simple_read_from_buffer(buf, count, ppos,
  65. info->addr, info->size);
  66. }
  67. static ssize_t access_ramoops_write(struct file *filp, const char __user *buf,
  68. size_t count, loff_t *ppos)
  69. {
  70. struct access_ramoops_info *info = filp->private_data;
  71. return simple_write_to_buffer(info->addr, info->size,
  72. ppos, buf, count);
  73. }
  74. static int access_ramoops_open(struct inode *inode, struct file *filp)
  75. {
  76. int res = 0;
  77. struct access_ramoops_info *info =
  78. container_of(filp->private_data, struct access_ramoops_info,
  79. miscdev);
  80. filp->private_data = info;
  81. mutex_lock(&info->lock);
  82. if (info->is_open == MAX_OPEN) {
  83. res = -EBUSY;
  84. goto out;
  85. }
  86. if (!info->is_open) {
  87. if (!request_mem_region(info->phys, info->size,
  88. "access_ramoops")) {
  89. dev_err(info->miscdev.this_device,
  90. "request mem region (%zx@%pa) failed\n",
  91. info->size, &info->phys);
  92. res = -ENOMEM;
  93. goto out;
  94. }
  95. info->addr = ioremap(info->phys, info->size);
  96. if (IS_ERR(info->addr)) {
  97. dev_err(info->miscdev.this_device,
  98. "unable to map region (%zx@%pa)\n",
  99. info->size, &info->phys);
  100. info->addr = NULL;
  101. res = -ENOMEM;
  102. goto out;
  103. }
  104. info->is_mapped = 1;
  105. }
  106. info->is_open++;
  107. out:
  108. mutex_unlock(&info->lock);
  109. return res;
  110. }
  111. static int access_ramoops_release(struct inode *inode, struct file *filp)
  112. {
  113. struct access_ramoops_info *info = filp->private_data;
  114. mutex_lock(&info->lock);
  115. info->is_open--;
  116. if (!info->is_open) {
  117. info->is_mapped = 0;
  118. iounmap(info->addr);
  119. info->addr = NULL;
  120. release_mem_region(info->phys, info->size);
  121. }
  122. filp->private_data = NULL;
  123. mutex_unlock(&info->lock);
  124. return 0;
  125. }
  126. static const struct file_operations access_ramoops_fops = {
  127. .owner = THIS_MODULE,
  128. .open = access_ramoops_open,
  129. .read = access_ramoops_read,
  130. .write = access_ramoops_write,
  131. .release = access_ramoops_release,
  132. };
  133. static int __init access_ramoops_probe(struct platform_device *pdev)
  134. {
  135. int ret = 0;
  136. struct access_ramoops_info *info;
  137. struct device_node *of_node = pdev->dev.of_node;
  138. struct device_node *mem_region;
  139. struct resource res;
  140. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  141. if (!info)
  142. return -ENOMEM;
  143. platform_set_drvdata(pdev, info);
  144. mem_region = of_parse_phandle(of_node, "memory-region", 0);
  145. if (!mem_region) {
  146. dev_err(&pdev->dev, "no memory-region phandle\n");
  147. return -ENODEV;
  148. }
  149. ret = of_property_read_string(of_node, "label", &info->label);
  150. if (ret) {
  151. dev_err(&pdev->dev, "failed to get region label: %d\n", ret);
  152. return -EINVAL;
  153. }
  154. info->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "access-%s",
  155. info->label);
  156. if (!info->name) {
  157. dev_err(&pdev->dev, "failed to alloc name\n");
  158. return -ENOMEM;
  159. }
  160. ret = of_address_to_resource(mem_region, 0, &res);
  161. of_node_put(mem_region);
  162. if (ret) {
  163. dev_err(&pdev->dev,
  164. "failed to get memory-region resource: %d\n", ret);
  165. return -ENOMEM;
  166. }
  167. info->phys = res.start;
  168. info->size = resource_size(&res);
  169. mutex_init(&info->lock);
  170. info->miscdev.minor = MISC_DYNAMIC_MINOR;
  171. info->miscdev.name = info->name;
  172. info->miscdev.fops = &access_ramoops_fops;
  173. info->miscdev.groups = access_ramoops_groups;
  174. info->miscdev.parent = &pdev->dev;
  175. ret = misc_register(&info->miscdev);
  176. if (ret) {
  177. dev_err(info->miscdev.this_device,
  178. "failed to register misc device %d\n", ret);
  179. return ret;
  180. }
  181. dev_info(info->miscdev.this_device,
  182. "registered '%s' %d:%d, (%zx@%pa)\n", info->label,
  183. MISC_MAJOR, info->miscdev.minor, info->size, &info->phys);
  184. return ret;
  185. }
  186. static int __exit access_ramoops_remove(struct platform_device *pdev)
  187. {
  188. struct access_ramoops_info *info = platform_get_drvdata(pdev);
  189. dev_info(info->miscdev.this_device, "removing");
  190. misc_deregister(&info->miscdev);
  191. if (!info->is_mapped)
  192. return 0;
  193. mutex_lock(&info->lock);
  194. info->is_mapped = 0;
  195. iounmap(info->addr);
  196. release_mem_region(info->phys, info->size);
  197. mutex_unlock(&info->lock);
  198. return 0;
  199. }
  200. static const struct of_device_id dt_match[] = {
  201. { .compatible = "access_ramoops" },
  202. {}
  203. };
  204. static struct platform_driver access_ramoops_driver = {
  205. .driver = {
  206. .name = "access_ramoops",
  207. .of_match_table = dt_match,
  208. },
  209. .remove = __exit_p(access_ramoops_remove),
  210. };
  211. module_platform_driver_probe(access_ramoops_driver, access_ramoops_probe);
  212. MODULE_LICENSE("GPL v2");
  213. MODULE_AUTHOR("Patrick Tjin <pattjin@google.com>");
  214. MODULE_DESCRIPTION("Persistent memory access driver");