slram.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*======================================================================
  2. This driver provides a method to access memory not used by the kernel
  3. itself (i.e. if the kernel commandline mem=xxx is used). To actually
  4. use slram at least mtdblock or mtdchar is required (for block or
  5. character device access).
  6. Usage:
  7. if compiled as loadable module:
  8. modprobe slram map=<name>,<start>,<end/offset>
  9. if statically linked into the kernel use the following kernel cmd.line
  10. slram=<name>,<start>,<end/offset>
  11. <name>: name of the device that will be listed in /proc/mtd
  12. <start>: start of the memory region, decimal or hex (0xabcdef)
  13. <end/offset>: end of the memory region. It's possible to use +0x1234
  14. to specify the offset instead of the absolute address
  15. NOTE:
  16. With slram it's only possible to map a contiguous memory region. Therefore
  17. if there's a device mapped somewhere in the region specified slram will
  18. fail to load (see kernel log if modprobe fails).
  19. -
  20. Jochen Schaeuble <psionic@psionic.de>
  21. ======================================================================*/
  22. #include <linux/module.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/init.h>
  34. #include <linux/io.h>
  35. #include <linux/mtd/mtd.h>
  36. #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
  37. #define SLRAM_BLK_SZ 0x4000
  38. #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
  39. #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
  40. typedef struct slram_priv {
  41. u_char *start;
  42. u_char *end;
  43. } slram_priv_t;
  44. typedef struct slram_mtd_list {
  45. struct mtd_info *mtdinfo;
  46. struct slram_mtd_list *next;
  47. } slram_mtd_list_t;
  48. #ifdef MODULE
  49. static char *map[SLRAM_MAX_DEVICES_PARAMS];
  50. module_param_array(map, charp, NULL, 0);
  51. MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
  52. #else
  53. static char *map;
  54. #endif
  55. static slram_mtd_list_t *slram_mtdlist = NULL;
  56. static int slram_erase(struct mtd_info *, struct erase_info *);
  57. static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, void **,
  58. resource_size_t *);
  59. static int slram_unpoint(struct mtd_info *, loff_t, size_t);
  60. static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  61. static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  62. static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
  63. {
  64. slram_priv_t *priv = mtd->priv;
  65. memset(priv->start + instr->addr, 0xff, instr->len);
  66. return(0);
  67. }
  68. static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
  69. size_t *retlen, void **virt, resource_size_t *phys)
  70. {
  71. slram_priv_t *priv = mtd->priv;
  72. *virt = priv->start + from;
  73. *retlen = len;
  74. return(0);
  75. }
  76. static int slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  77. {
  78. return 0;
  79. }
  80. static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
  81. size_t *retlen, u_char *buf)
  82. {
  83. slram_priv_t *priv = mtd->priv;
  84. memcpy(buf, priv->start + from, len);
  85. *retlen = len;
  86. return(0);
  87. }
  88. static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
  89. size_t *retlen, const u_char *buf)
  90. {
  91. slram_priv_t *priv = mtd->priv;
  92. memcpy(priv->start + to, buf, len);
  93. *retlen = len;
  94. return(0);
  95. }
  96. /*====================================================================*/
  97. static int register_device(char *name, unsigned long start, unsigned long length)
  98. {
  99. slram_mtd_list_t **curmtd;
  100. curmtd = &slram_mtdlist;
  101. while (*curmtd) {
  102. curmtd = &(*curmtd)->next;
  103. }
  104. *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
  105. if (!(*curmtd)) {
  106. E("slram: Cannot allocate new MTD device.\n");
  107. return(-ENOMEM);
  108. }
  109. (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  110. (*curmtd)->next = NULL;
  111. if ((*curmtd)->mtdinfo) {
  112. (*curmtd)->mtdinfo->priv =
  113. kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
  114. if (!(*curmtd)->mtdinfo->priv) {
  115. kfree((*curmtd)->mtdinfo);
  116. (*curmtd)->mtdinfo = NULL;
  117. }
  118. }
  119. if (!(*curmtd)->mtdinfo) {
  120. E("slram: Cannot allocate new MTD device.\n");
  121. return(-ENOMEM);
  122. }
  123. if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
  124. memremap(start, length,
  125. MEMREMAP_WB | MEMREMAP_WT | MEMREMAP_WC))) {
  126. E("slram: memremap failed\n");
  127. return -EIO;
  128. }
  129. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
  130. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
  131. (*curmtd)->mtdinfo->name = name;
  132. (*curmtd)->mtdinfo->size = length;
  133. (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
  134. (*curmtd)->mtdinfo->_erase = slram_erase;
  135. (*curmtd)->mtdinfo->_point = slram_point;
  136. (*curmtd)->mtdinfo->_unpoint = slram_unpoint;
  137. (*curmtd)->mtdinfo->_read = slram_read;
  138. (*curmtd)->mtdinfo->_write = slram_write;
  139. (*curmtd)->mtdinfo->owner = THIS_MODULE;
  140. (*curmtd)->mtdinfo->type = MTD_RAM;
  141. (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
  142. (*curmtd)->mtdinfo->writesize = 1;
  143. if (mtd_device_register((*curmtd)->mtdinfo, NULL, 0)) {
  144. E("slram: Failed to register new device\n");
  145. memunmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
  146. kfree((*curmtd)->mtdinfo->priv);
  147. kfree((*curmtd)->mtdinfo);
  148. return(-EAGAIN);
  149. }
  150. T("slram: Registered device %s from %luKiB to %luKiB\n", name,
  151. (start / 1024), ((start + length) / 1024));
  152. T("slram: Mapped from 0x%p to 0x%p\n",
  153. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
  154. ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
  155. return(0);
  156. }
  157. static void unregister_devices(void)
  158. {
  159. slram_mtd_list_t *nextitem;
  160. while (slram_mtdlist) {
  161. nextitem = slram_mtdlist->next;
  162. mtd_device_unregister(slram_mtdlist->mtdinfo);
  163. memunmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
  164. kfree(slram_mtdlist->mtdinfo->priv);
  165. kfree(slram_mtdlist->mtdinfo);
  166. kfree(slram_mtdlist);
  167. slram_mtdlist = nextitem;
  168. }
  169. }
  170. static unsigned long handle_unit(unsigned long value, char *unit)
  171. {
  172. if ((*unit == 'M') || (*unit == 'm')) {
  173. return(value * 1024 * 1024);
  174. } else if ((*unit == 'K') || (*unit == 'k')) {
  175. return(value * 1024);
  176. }
  177. return(value);
  178. }
  179. static int parse_cmdline(char *devname, char *szstart, char *szlength)
  180. {
  181. char *buffer;
  182. unsigned long devstart;
  183. unsigned long devlength;
  184. if (kernel_is_locked_down("Command line-specified device addresses"))
  185. return -EPERM;
  186. if ((!devname) || (!szstart) || (!szlength)) {
  187. unregister_devices();
  188. return(-EINVAL);
  189. }
  190. devstart = simple_strtoul(szstart, &buffer, 0);
  191. devstart = handle_unit(devstart, buffer);
  192. if (*(szlength) != '+') {
  193. devlength = simple_strtoul(szlength, &buffer, 0);
  194. devlength = handle_unit(devlength, buffer);
  195. if (devlength < devstart)
  196. goto err_out;
  197. devlength -= devstart;
  198. } else {
  199. devlength = simple_strtoul(szlength + 1, &buffer, 0);
  200. devlength = handle_unit(devlength, buffer);
  201. }
  202. T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
  203. devname, devstart, devlength);
  204. if (devlength % SLRAM_BLK_SZ != 0)
  205. goto err_out;
  206. if ((devstart = register_device(devname, devstart, devlength))){
  207. unregister_devices();
  208. return((int)devstart);
  209. }
  210. return(0);
  211. err_out:
  212. E("slram: Illegal length parameter.\n");
  213. return(-EINVAL);
  214. }
  215. #ifndef MODULE
  216. static int __init mtd_slram_setup(char *str)
  217. {
  218. map = str;
  219. return(1);
  220. }
  221. __setup("slram=", mtd_slram_setup);
  222. #endif
  223. static int __init init_slram(void)
  224. {
  225. char *devname;
  226. #ifndef MODULE
  227. char *devstart;
  228. char *devlength;
  229. if (!map) {
  230. E("slram: not enough parameters.\n");
  231. return(-EINVAL);
  232. }
  233. while (map) {
  234. devname = devstart = devlength = NULL;
  235. if (!(devname = strsep(&map, ","))) {
  236. E("slram: No devicename specified.\n");
  237. break;
  238. }
  239. T("slram: devname = %s\n", devname);
  240. if ((!map) || (!(devstart = strsep(&map, ",")))) {
  241. E("slram: No devicestart specified.\n");
  242. }
  243. T("slram: devstart = %s\n", devstart);
  244. if ((!map) || (!(devlength = strsep(&map, ",")))) {
  245. E("slram: No devicelength / -end specified.\n");
  246. }
  247. T("slram: devlength = %s\n", devlength);
  248. if (parse_cmdline(devname, devstart, devlength) != 0) {
  249. return(-EINVAL);
  250. }
  251. }
  252. #else
  253. int count;
  254. int i;
  255. for (count = 0; count < SLRAM_MAX_DEVICES_PARAMS && map[count];
  256. count++) {
  257. }
  258. if ((count % 3 != 0) || (count == 0)) {
  259. E("slram: not enough parameters.\n");
  260. return(-EINVAL);
  261. }
  262. for (i = 0; i < (count / 3); i++) {
  263. devname = map[i * 3];
  264. if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
  265. return(-EINVAL);
  266. }
  267. }
  268. #endif /* !MODULE */
  269. return(0);
  270. }
  271. static void __exit cleanup_slram(void)
  272. {
  273. unregister_devices();
  274. }
  275. module_init(init_slram);
  276. module_exit(cleanup_slram);
  277. MODULE_LICENSE("GPL");
  278. MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
  279. MODULE_DESCRIPTION("MTD driver for uncached system RAM");