phram.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
  4. * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
  5. *
  6. * Usage:
  7. *
  8. * one commend line parameter per device, each in the form:
  9. * phram=<name>,<start>,<len>[,<erasesize>]
  10. * <name> may be up to 63 characters.
  11. * <start>, <len>, and <erasesize> can be octal, decimal or hexadecimal. If followed
  12. * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
  13. * gigabytes. <erasesize> is optional and defaults to PAGE_SIZE.
  14. *
  15. * Example:
  16. * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi,64Ki
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/io.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/slab.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <asm/div64.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of.h>
  31. #include <linux/security.h>
  32. struct phram_mtd_list {
  33. struct mtd_info mtd;
  34. struct list_head list;
  35. bool cached;
  36. };
  37. static LIST_HEAD(phram_list);
  38. static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
  39. {
  40. u_char *start = mtd->priv;
  41. memset(start + instr->addr, 0xff, instr->len);
  42. return 0;
  43. }
  44. static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
  45. size_t *retlen, void **virt, resource_size_t *phys)
  46. {
  47. *virt = mtd->priv + from;
  48. *retlen = len;
  49. return 0;
  50. }
  51. static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  52. {
  53. return 0;
  54. }
  55. static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
  56. size_t *retlen, u_char *buf)
  57. {
  58. u_char *start = mtd->priv;
  59. memcpy(buf, start + from, len);
  60. *retlen = len;
  61. return 0;
  62. }
  63. static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
  64. size_t *retlen, const u_char *buf)
  65. {
  66. u_char *start = mtd->priv;
  67. memcpy(start + to, buf, len);
  68. *retlen = len;
  69. return 0;
  70. }
  71. static int phram_map(struct phram_mtd_list *phram, phys_addr_t start, size_t len)
  72. {
  73. void *addr = NULL;
  74. if (phram->cached)
  75. addr = memremap(start, len, MEMREMAP_WB);
  76. else
  77. addr = (void __force *)ioremap(start, len);
  78. if (!addr)
  79. return -EIO;
  80. phram->mtd.priv = addr;
  81. return 0;
  82. }
  83. static void phram_unmap(struct phram_mtd_list *phram)
  84. {
  85. void *addr = phram->mtd.priv;
  86. if (phram->cached) {
  87. memunmap(addr);
  88. return;
  89. }
  90. iounmap((void __iomem *)addr);
  91. }
  92. static void unregister_devices(void)
  93. {
  94. struct phram_mtd_list *this, *safe;
  95. list_for_each_entry_safe(this, safe, &phram_list, list) {
  96. mtd_device_unregister(&this->mtd);
  97. phram_unmap(this);
  98. kfree(this->mtd.name);
  99. kfree(this);
  100. }
  101. }
  102. static int register_device(struct platform_device *pdev, const char *name,
  103. phys_addr_t start, size_t len, uint32_t erasesize)
  104. {
  105. struct device_node *np = pdev ? pdev->dev.of_node : NULL;
  106. bool cached = np ? !of_property_read_bool(np, "no-map") : false;
  107. struct phram_mtd_list *new;
  108. int ret = -ENOMEM;
  109. new = kzalloc_obj(*new);
  110. if (!new)
  111. goto out0;
  112. new->cached = cached;
  113. ret = phram_map(new, start, len);
  114. if (ret) {
  115. pr_err("ioremap failed\n");
  116. goto out1;
  117. }
  118. new->mtd.name = name;
  119. new->mtd.size = len;
  120. new->mtd.flags = MTD_CAP_RAM;
  121. new->mtd._erase = phram_erase;
  122. new->mtd._point = phram_point;
  123. new->mtd._unpoint = phram_unpoint;
  124. new->mtd._read = phram_read;
  125. new->mtd._write = phram_write;
  126. new->mtd.owner = THIS_MODULE;
  127. new->mtd.type = MTD_RAM;
  128. new->mtd.erasesize = erasesize;
  129. new->mtd.writesize = 1;
  130. mtd_set_of_node(&new->mtd, np);
  131. ret = -EAGAIN;
  132. if (mtd_device_register(&new->mtd, NULL, 0)) {
  133. pr_err("Failed to register new device\n");
  134. goto out2;
  135. }
  136. if (pdev)
  137. platform_set_drvdata(pdev, new);
  138. else
  139. list_add_tail(&new->list, &phram_list);
  140. return 0;
  141. out2:
  142. phram_unmap(new);
  143. out1:
  144. kfree(new);
  145. out0:
  146. return ret;
  147. }
  148. static int parse_num64(uint64_t *num64, char *token)
  149. {
  150. size_t len;
  151. int shift = 0;
  152. int ret;
  153. len = strlen(token);
  154. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  155. if (len > 2) {
  156. if (token[len - 1] == 'i') {
  157. switch (token[len - 2]) {
  158. case 'G':
  159. shift += 10;
  160. fallthrough;
  161. case 'M':
  162. shift += 10;
  163. fallthrough;
  164. case 'k':
  165. shift += 10;
  166. token[len - 2] = 0;
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. }
  172. }
  173. ret = kstrtou64(token, 0, num64);
  174. *num64 <<= shift;
  175. return ret;
  176. }
  177. static int parse_name(char **pname, const char *token)
  178. {
  179. size_t len;
  180. char *name;
  181. len = strlen(token) + 1;
  182. if (len > 64)
  183. return -ENOSPC;
  184. name = kstrdup(token, GFP_KERNEL);
  185. if (!name)
  186. return -ENOMEM;
  187. *pname = name;
  188. return 0;
  189. }
  190. static inline void kill_final_newline(char *str)
  191. {
  192. char *newline = strrchr(str, '\n');
  193. if (newline && !newline[1])
  194. *newline = 0;
  195. }
  196. #define parse_err(fmt, args...) do { \
  197. pr_err(fmt , ## args); \
  198. return 1; \
  199. } while (0)
  200. #ifndef MODULE
  201. static int phram_init_called;
  202. /*
  203. * This shall contain the module parameter if any. It is of the form:
  204. * - phram=<device>,<address>,<size>[,<erasesize>] for module case
  205. * - phram.phram=<device>,<address>,<size>[,<erasesize>] for built-in case
  206. * We leave 64 bytes for the device name, 20 for the address , 20 for the
  207. * size and 20 for the erasesize.
  208. * Example: phram.phram=rootfs,0xa0000000,512Mi,65536
  209. */
  210. static char phram_paramline[64 + 20 + 20 + 20];
  211. #endif
  212. static int phram_setup(const char *val)
  213. {
  214. char buf[64 + 20 + 20 + 20], *str = buf;
  215. char *token[4];
  216. char *name;
  217. uint64_t start;
  218. uint64_t len;
  219. uint64_t erasesize = PAGE_SIZE;
  220. uint32_t rem;
  221. int i, ret;
  222. if (strnlen(val, sizeof(buf)) >= sizeof(buf))
  223. parse_err("parameter too long\n");
  224. strcpy(str, val);
  225. kill_final_newline(str);
  226. for (i = 0; i < 4; i++)
  227. token[i] = strsep(&str, ",");
  228. if (str)
  229. parse_err("too many arguments\n");
  230. if (!token[2])
  231. parse_err("not enough arguments\n");
  232. ret = parse_name(&name, token[0]);
  233. if (ret)
  234. return ret;
  235. ret = parse_num64(&start, token[1]);
  236. if (ret) {
  237. parse_err("illegal start address\n");
  238. goto error;
  239. }
  240. ret = parse_num64(&len, token[2]);
  241. if (ret) {
  242. parse_err("illegal device length\n");
  243. goto error;
  244. }
  245. if (token[3]) {
  246. ret = parse_num64(&erasesize, token[3]);
  247. if (ret) {
  248. parse_err("illegal erasesize\n");
  249. goto error;
  250. }
  251. }
  252. if (len == 0 || erasesize == 0 || erasesize > len
  253. || erasesize > UINT_MAX) {
  254. parse_err("illegal erasesize or len\n");
  255. ret = -EINVAL;
  256. goto error;
  257. }
  258. div_u64_rem(len, (uint32_t)erasesize, &rem);
  259. if (rem) {
  260. parse_err("len is not multiple of erasesize\n");
  261. ret = -EINVAL;
  262. goto error;
  263. }
  264. ret = register_device(NULL, name, start, len, (uint32_t)erasesize);
  265. if (ret)
  266. goto error;
  267. pr_info("%s device: %#llx at %#llx for erasesize %#llx\n", name, len, start, erasesize);
  268. return 0;
  269. error:
  270. kfree(name);
  271. return ret;
  272. }
  273. static int phram_param_call(const char *val, const struct kernel_param *kp)
  274. {
  275. #ifdef MODULE
  276. return phram_setup(val);
  277. #else
  278. /*
  279. * If more parameters are later passed in via
  280. * /sys/module/phram/parameters/phram
  281. * and init_phram() has already been called,
  282. * we can parse the argument now.
  283. */
  284. if (phram_init_called)
  285. return phram_setup(val);
  286. /*
  287. * During early boot stage, we only save the parameters
  288. * here. We must parse them later: if the param passed
  289. * from kernel boot command line, phram_param_call() is
  290. * called so early that it is not possible to resolve
  291. * the device (even kmalloc() fails). Defer that work to
  292. * phram_setup().
  293. */
  294. if (strlen(val) >= sizeof(phram_paramline))
  295. return -ENOSPC;
  296. strcpy(phram_paramline, val);
  297. return 0;
  298. #endif
  299. }
  300. module_param_call(phram, phram_param_call, NULL, NULL, 0200);
  301. MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>[,<erasesize>]\"");
  302. #ifdef CONFIG_OF
  303. static const struct of_device_id phram_of_match[] = {
  304. { .compatible = "phram" },
  305. {}
  306. };
  307. MODULE_DEVICE_TABLE(of, phram_of_match);
  308. #endif
  309. static int phram_probe(struct platform_device *pdev)
  310. {
  311. struct resource *res;
  312. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. if (!res)
  314. return -ENOMEM;
  315. /* mtd_set_of_node() reads name from "label" */
  316. return register_device(pdev, NULL, res->start, resource_size(res),
  317. PAGE_SIZE);
  318. }
  319. static void phram_remove(struct platform_device *pdev)
  320. {
  321. struct phram_mtd_list *phram = platform_get_drvdata(pdev);
  322. mtd_device_unregister(&phram->mtd);
  323. phram_unmap(phram);
  324. kfree(phram);
  325. }
  326. static struct platform_driver phram_driver = {
  327. .probe = phram_probe,
  328. .remove = phram_remove,
  329. .driver = {
  330. .name = "phram",
  331. .of_match_table = of_match_ptr(phram_of_match),
  332. },
  333. };
  334. static int __init init_phram(void)
  335. {
  336. int ret;
  337. ret = security_locked_down(LOCKDOWN_DEV_MEM);
  338. if (ret)
  339. return ret;
  340. ret = platform_driver_register(&phram_driver);
  341. if (ret)
  342. return ret;
  343. #ifndef MODULE
  344. if (phram_paramline[0]) {
  345. ret = phram_setup(phram_paramline);
  346. if (ret)
  347. platform_driver_unregister(&phram_driver);
  348. }
  349. phram_init_called = 1;
  350. #endif
  351. return ret;
  352. }
  353. static void __exit cleanup_phram(void)
  354. {
  355. unregister_devices();
  356. platform_driver_unregister(&phram_driver);
  357. }
  358. module_init(init_phram);
  359. module_exit(cleanup_phram);
  360. MODULE_LICENSE("GPL");
  361. MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
  362. MODULE_DESCRIPTION("MTD driver for physical RAM");