phram.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
  3. * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
  4. *
  5. * Usage:
  6. *
  7. * one commend line parameter per device, each in the form:
  8. * phram=<name>,<start>,<len>
  9. * <name> may be up to 63 characters.
  10. * <start> and <len> can be octal, decimal or hexadecimal. If followed
  11. * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
  12. * gigabytes.
  13. *
  14. * Example:
  15. * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/io.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/slab.h>
  25. #include <linux/mtd/mtd.h>
  26. struct phram_mtd_list {
  27. struct mtd_info mtd;
  28. struct list_head list;
  29. };
  30. static LIST_HEAD(phram_list);
  31. static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
  32. {
  33. u_char *start = mtd->priv;
  34. memset(start + instr->addr, 0xff, instr->len);
  35. return 0;
  36. }
  37. static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
  38. size_t *retlen, void **virt, resource_size_t *phys)
  39. {
  40. *virt = mtd->priv + from;
  41. *retlen = len;
  42. return 0;
  43. }
  44. static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  45. {
  46. return 0;
  47. }
  48. static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
  49. size_t *retlen, u_char *buf)
  50. {
  51. u_char *start = mtd->priv;
  52. memcpy(buf, start + from, len);
  53. *retlen = len;
  54. return 0;
  55. }
  56. static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
  57. size_t *retlen, const u_char *buf)
  58. {
  59. u_char *start = mtd->priv;
  60. memcpy(start + to, buf, len);
  61. *retlen = len;
  62. return 0;
  63. }
  64. static void unregister_devices(void)
  65. {
  66. struct phram_mtd_list *this, *safe;
  67. list_for_each_entry_safe(this, safe, &phram_list, list) {
  68. mtd_device_unregister(&this->mtd);
  69. iounmap(this->mtd.priv);
  70. kfree(this->mtd.name);
  71. kfree(this);
  72. }
  73. }
  74. static int register_device(char *name, phys_addr_t start, size_t len)
  75. {
  76. struct phram_mtd_list *new;
  77. int ret = -ENOMEM;
  78. new = kzalloc(sizeof(*new), GFP_KERNEL);
  79. if (!new)
  80. goto out0;
  81. ret = -EIO;
  82. new->mtd.priv = ioremap(start, len);
  83. if (!new->mtd.priv) {
  84. pr_err("ioremap failed\n");
  85. goto out1;
  86. }
  87. new->mtd.name = name;
  88. new->mtd.size = len;
  89. new->mtd.flags = MTD_CAP_RAM;
  90. new->mtd._erase = phram_erase;
  91. new->mtd._point = phram_point;
  92. new->mtd._unpoint = phram_unpoint;
  93. new->mtd._read = phram_read;
  94. new->mtd._write = phram_write;
  95. new->mtd.owner = THIS_MODULE;
  96. new->mtd.type = MTD_RAM;
  97. new->mtd.erasesize = PAGE_SIZE;
  98. new->mtd.writesize = 1;
  99. ret = -EAGAIN;
  100. if (mtd_device_register(&new->mtd, NULL, 0)) {
  101. pr_err("Failed to register new device\n");
  102. goto out2;
  103. }
  104. list_add_tail(&new->list, &phram_list);
  105. return 0;
  106. out2:
  107. iounmap(new->mtd.priv);
  108. out1:
  109. kfree(new);
  110. out0:
  111. return ret;
  112. }
  113. static int parse_num64(uint64_t *num64, char *token)
  114. {
  115. size_t len;
  116. int shift = 0;
  117. int ret;
  118. len = strlen(token);
  119. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  120. if (len > 2) {
  121. if (token[len - 1] == 'i') {
  122. switch (token[len - 2]) {
  123. case 'G':
  124. shift += 10;
  125. case 'M':
  126. shift += 10;
  127. case 'k':
  128. shift += 10;
  129. token[len - 2] = 0;
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. }
  135. }
  136. ret = kstrtou64(token, 0, num64);
  137. *num64 <<= shift;
  138. return ret;
  139. }
  140. static int parse_name(char **pname, const char *token)
  141. {
  142. size_t len;
  143. char *name;
  144. len = strlen(token) + 1;
  145. if (len > 64)
  146. return -ENOSPC;
  147. name = kstrdup(token, GFP_KERNEL);
  148. if (!name)
  149. return -ENOMEM;
  150. *pname = name;
  151. return 0;
  152. }
  153. static inline void kill_final_newline(char *str)
  154. {
  155. char *newline = strrchr(str, '\n');
  156. if (newline && !newline[1])
  157. *newline = 0;
  158. }
  159. #define parse_err(fmt, args...) do { \
  160. pr_err(fmt , ## args); \
  161. return 1; \
  162. } while (0)
  163. #ifndef MODULE
  164. static int phram_init_called;
  165. /*
  166. * This shall contain the module parameter if any. It is of the form:
  167. * - phram=<device>,<address>,<size> for module case
  168. * - phram.phram=<device>,<address>,<size> for built-in case
  169. * We leave 64 bytes for the device name, 20 for the address and 20 for the
  170. * size.
  171. * Example: phram.phram=rootfs,0xa0000000,512Mi
  172. */
  173. static char phram_paramline[64 + 20 + 20];
  174. #endif
  175. static int phram_setup(const char *val)
  176. {
  177. char buf[64 + 20 + 20], *str = buf;
  178. char *token[3];
  179. char *name;
  180. uint64_t start;
  181. uint64_t len;
  182. int i, ret;
  183. if (kernel_is_locked_down("Command line-specified device addresses"))
  184. return -EPERM;
  185. if (strnlen(val, sizeof(buf)) >= sizeof(buf))
  186. parse_err("parameter too long\n");
  187. strcpy(str, val);
  188. kill_final_newline(str);
  189. for (i = 0; i < 3; i++)
  190. token[i] = strsep(&str, ",");
  191. if (str)
  192. parse_err("too many arguments\n");
  193. if (!token[2])
  194. parse_err("not enough arguments\n");
  195. ret = parse_name(&name, token[0]);
  196. if (ret)
  197. return ret;
  198. ret = parse_num64(&start, token[1]);
  199. if (ret) {
  200. kfree(name);
  201. parse_err("illegal start address\n");
  202. }
  203. ret = parse_num64(&len, token[2]);
  204. if (ret) {
  205. kfree(name);
  206. parse_err("illegal device length\n");
  207. }
  208. ret = register_device(name, start, len);
  209. if (!ret)
  210. pr_info("%s device: %#llx at %#llx\n", name, len, start);
  211. else
  212. kfree(name);
  213. return ret;
  214. }
  215. static int phram_param_call(const char *val, const struct kernel_param *kp)
  216. {
  217. #ifdef MODULE
  218. return phram_setup(val);
  219. #else
  220. /*
  221. * If more parameters are later passed in via
  222. * /sys/module/phram/parameters/phram
  223. * and init_phram() has already been called,
  224. * we can parse the argument now.
  225. */
  226. if (phram_init_called)
  227. return phram_setup(val);
  228. /*
  229. * During early boot stage, we only save the parameters
  230. * here. We must parse them later: if the param passed
  231. * from kernel boot command line, phram_param_call() is
  232. * called so early that it is not possible to resolve
  233. * the device (even kmalloc() fails). Defer that work to
  234. * phram_setup().
  235. */
  236. if (strlen(val) >= sizeof(phram_paramline))
  237. return -ENOSPC;
  238. strcpy(phram_paramline, val);
  239. return 0;
  240. #endif
  241. }
  242. module_param_call(phram, phram_param_call, NULL, NULL, 000);
  243. MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
  244. static int __init init_phram(void)
  245. {
  246. int ret = 0;
  247. #ifndef MODULE
  248. if (phram_paramline[0])
  249. ret = phram_setup(phram_paramline);
  250. phram_init_called = 1;
  251. #endif
  252. return ret;
  253. }
  254. static void __exit cleanup_phram(void)
  255. {
  256. unregister_devices();
  257. }
  258. module_init(init_phram);
  259. module_exit(cleanup_phram);
  260. MODULE_LICENSE("GPL");
  261. MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
  262. MODULE_DESCRIPTION("MTD driver for physical RAM");