z2ram.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. ** z2ram - Amiga pseudo-driver to access 16bit-RAM in ZorroII space
  3. ** as a block device, to be used as a RAM disk or swap space
  4. **
  5. ** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  6. **
  7. ** ++Geert: support for zorro_unused_z2ram, better range checking
  8. ** ++roman: translate accesses via an array
  9. ** ++Milan: support for ChipRAM usage
  10. ** ++yambo: converted to 2.0 kernel
  11. ** ++yambo: modularized and support added for 3 minor devices including:
  12. ** MAJOR MINOR DESCRIPTION
  13. ** ----- ----- ----------------------------------------------
  14. ** 37 0 Use Zorro II and Chip ram
  15. ** 37 1 Use only Zorro II ram
  16. ** 37 2 Use only Chip ram
  17. ** 37 4-7 Use memory list entry 1-4 (first is 0)
  18. ** ++jskov: support for 1-4th memory list entry.
  19. **
  20. ** Permission to use, copy, modify, and distribute this software and its
  21. ** documentation for any purpose and without fee is hereby granted, provided
  22. ** that the above copyright notice appear in all copies and that both that
  23. ** copyright notice and this permission notice appear in supporting
  24. ** documentation. This software is provided "as is" without express or
  25. ** implied warranty.
  26. */
  27. #define DEVICE_NAME "Z2RAM"
  28. #include <linux/major.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/blk-mq.h>
  33. #include <linux/bitops.h>
  34. #include <linux/mutex.h>
  35. #include <linux/slab.h>
  36. #include <linux/pgtable.h>
  37. #include <asm/setup.h>
  38. #include <asm/amigahw.h>
  39. #include <linux/zorro.h>
  40. #define Z2MINOR_COMBINED (0)
  41. #define Z2MINOR_Z2ONLY (1)
  42. #define Z2MINOR_CHIPONLY (2)
  43. #define Z2MINOR_MEMLIST1 (4)
  44. #define Z2MINOR_MEMLIST2 (5)
  45. #define Z2MINOR_MEMLIST3 (6)
  46. #define Z2MINOR_MEMLIST4 (7)
  47. #define Z2MINOR_COUNT (8) /* Move this down when adding a new minor */
  48. #define Z2RAM_CHUNK1024 ( Z2RAM_CHUNKSIZE >> 10 )
  49. static DEFINE_MUTEX(z2ram_mutex);
  50. static u_long *z2ram_map = NULL;
  51. static u_long z2ram_size = 0;
  52. static int z2_count = 0;
  53. static int chip_count = 0;
  54. static int list_count = 0;
  55. static int current_device = -1;
  56. static DEFINE_SPINLOCK(z2ram_lock);
  57. static struct gendisk *z2ram_gendisk[Z2MINOR_COUNT];
  58. static blk_status_t z2_queue_rq(struct blk_mq_hw_ctx *hctx,
  59. const struct blk_mq_queue_data *bd)
  60. {
  61. struct request *req = bd->rq;
  62. unsigned long start = blk_rq_pos(req) << 9;
  63. unsigned long len = blk_rq_cur_bytes(req);
  64. blk_mq_start_request(req);
  65. if (start + len > z2ram_size) {
  66. pr_err(DEVICE_NAME ": bad access: block=%llu, "
  67. "count=%u\n",
  68. (unsigned long long)blk_rq_pos(req),
  69. blk_rq_cur_sectors(req));
  70. return BLK_STS_IOERR;
  71. }
  72. spin_lock_irq(&z2ram_lock);
  73. while (len) {
  74. unsigned long addr = start & Z2RAM_CHUNKMASK;
  75. unsigned long size = Z2RAM_CHUNKSIZE - addr;
  76. void *buffer = bio_data(req->bio);
  77. if (len < size)
  78. size = len;
  79. addr += z2ram_map[start >> Z2RAM_CHUNKSHIFT];
  80. if (rq_data_dir(req) == READ)
  81. memcpy(buffer, (char *)addr, size);
  82. else
  83. memcpy((char *)addr, buffer, size);
  84. start += size;
  85. len -= size;
  86. }
  87. spin_unlock_irq(&z2ram_lock);
  88. blk_mq_end_request(req, BLK_STS_OK);
  89. return BLK_STS_OK;
  90. }
  91. static void get_z2ram(void)
  92. {
  93. int i;
  94. for (i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++) {
  95. if (test_bit(i, zorro_unused_z2ram)) {
  96. z2_count++;
  97. z2ram_map[z2ram_size++] =
  98. (unsigned long)ZTWO_VADDR(Z2RAM_START) +
  99. (i << Z2RAM_CHUNKSHIFT);
  100. clear_bit(i, zorro_unused_z2ram);
  101. }
  102. }
  103. return;
  104. }
  105. static void get_chipram(void)
  106. {
  107. while (amiga_chip_avail() > (Z2RAM_CHUNKSIZE * 4)) {
  108. chip_count++;
  109. z2ram_map[z2ram_size] =
  110. (u_long) amiga_chip_alloc(Z2RAM_CHUNKSIZE, "z2ram");
  111. if (z2ram_map[z2ram_size] == 0) {
  112. break;
  113. }
  114. z2ram_size++;
  115. }
  116. return;
  117. }
  118. static int z2_open(struct gendisk *disk, blk_mode_t mode)
  119. {
  120. int device = disk->first_minor;
  121. int max_z2_map = (Z2RAM_SIZE / Z2RAM_CHUNKSIZE) * sizeof(z2ram_map[0]);
  122. int max_chip_map = (amiga_chip_size / Z2RAM_CHUNKSIZE) *
  123. sizeof(z2ram_map[0]);
  124. int rc = -ENOMEM;
  125. mutex_lock(&z2ram_mutex);
  126. if (current_device != -1 && current_device != device) {
  127. rc = -EBUSY;
  128. goto err_out;
  129. }
  130. if (current_device == -1) {
  131. z2_count = 0;
  132. chip_count = 0;
  133. list_count = 0;
  134. z2ram_size = 0;
  135. /* Use a specific list entry. */
  136. if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
  137. int index = device - Z2MINOR_MEMLIST1 + 1;
  138. unsigned long size, paddr, vaddr;
  139. if (index >= m68k_realnum_memory) {
  140. printk(KERN_ERR DEVICE_NAME
  141. ": no such entry in z2ram_map\n");
  142. goto err_out;
  143. }
  144. paddr = m68k_memory[index].addr;
  145. size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE - 1);
  146. #ifdef __powerpc__
  147. /* FIXME: ioremap doesn't build correct memory tables. */
  148. {
  149. vfree(vmalloc(size));
  150. }
  151. vaddr = (unsigned long)ioremap_wt(paddr, size);
  152. #else
  153. vaddr =
  154. (unsigned long)z_remap_nocache_nonser(paddr, size);
  155. #endif
  156. z2ram_map =
  157. kmalloc_objs(z2ram_map[0], size / Z2RAM_CHUNKSIZE);
  158. if (z2ram_map == NULL) {
  159. printk(KERN_ERR DEVICE_NAME
  160. ": cannot get mem for z2ram_map\n");
  161. goto err_out;
  162. }
  163. while (size) {
  164. z2ram_map[z2ram_size++] = vaddr;
  165. size -= Z2RAM_CHUNKSIZE;
  166. vaddr += Z2RAM_CHUNKSIZE;
  167. list_count++;
  168. }
  169. if (z2ram_size != 0)
  170. printk(KERN_INFO DEVICE_NAME
  171. ": using %iK List Entry %d Memory\n",
  172. list_count * Z2RAM_CHUNK1024, index);
  173. } else
  174. switch (device) {
  175. case Z2MINOR_COMBINED:
  176. z2ram_map =
  177. kmalloc(max_z2_map + max_chip_map,
  178. GFP_KERNEL);
  179. if (z2ram_map == NULL) {
  180. printk(KERN_ERR DEVICE_NAME
  181. ": cannot get mem for z2ram_map\n");
  182. goto err_out;
  183. }
  184. get_z2ram();
  185. get_chipram();
  186. if (z2ram_size != 0)
  187. printk(KERN_INFO DEVICE_NAME
  188. ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
  189. z2_count * Z2RAM_CHUNK1024,
  190. chip_count * Z2RAM_CHUNK1024,
  191. (z2_count +
  192. chip_count) * Z2RAM_CHUNK1024);
  193. break;
  194. case Z2MINOR_Z2ONLY:
  195. z2ram_map = kmalloc(max_z2_map, GFP_KERNEL);
  196. if (!z2ram_map)
  197. goto err_out;
  198. get_z2ram();
  199. if (z2ram_size != 0)
  200. printk(KERN_INFO DEVICE_NAME
  201. ": using %iK of Zorro II RAM\n",
  202. z2_count * Z2RAM_CHUNK1024);
  203. break;
  204. case Z2MINOR_CHIPONLY:
  205. z2ram_map = kmalloc(max_chip_map, GFP_KERNEL);
  206. if (!z2ram_map)
  207. goto err_out;
  208. get_chipram();
  209. if (z2ram_size != 0)
  210. printk(KERN_INFO DEVICE_NAME
  211. ": using %iK Chip RAM\n",
  212. chip_count * Z2RAM_CHUNK1024);
  213. break;
  214. default:
  215. rc = -ENODEV;
  216. goto err_out;
  217. break;
  218. }
  219. if (z2ram_size == 0) {
  220. printk(KERN_NOTICE DEVICE_NAME
  221. ": no unused ZII/Chip RAM found\n");
  222. goto err_out_kfree;
  223. }
  224. current_device = device;
  225. z2ram_size <<= Z2RAM_CHUNKSHIFT;
  226. set_capacity(z2ram_gendisk[device], z2ram_size >> 9);
  227. }
  228. mutex_unlock(&z2ram_mutex);
  229. return 0;
  230. err_out_kfree:
  231. kfree(z2ram_map);
  232. err_out:
  233. mutex_unlock(&z2ram_mutex);
  234. return rc;
  235. }
  236. static void z2_release(struct gendisk *disk)
  237. {
  238. mutex_lock(&z2ram_mutex);
  239. if (current_device == -1) {
  240. mutex_unlock(&z2ram_mutex);
  241. return;
  242. }
  243. mutex_unlock(&z2ram_mutex);
  244. /*
  245. * FIXME: unmap memory
  246. */
  247. }
  248. static const struct block_device_operations z2_fops = {
  249. .owner = THIS_MODULE,
  250. .open = z2_open,
  251. .release = z2_release,
  252. };
  253. static struct blk_mq_tag_set tag_set;
  254. static const struct blk_mq_ops z2_mq_ops = {
  255. .queue_rq = z2_queue_rq,
  256. };
  257. static int z2ram_register_disk(int minor)
  258. {
  259. struct gendisk *disk;
  260. int err;
  261. disk = blk_mq_alloc_disk(&tag_set, NULL, NULL);
  262. if (IS_ERR(disk))
  263. return PTR_ERR(disk);
  264. disk->major = Z2RAM_MAJOR;
  265. disk->first_minor = minor;
  266. disk->minors = 1;
  267. disk->flags |= GENHD_FL_NO_PART;
  268. disk->fops = &z2_fops;
  269. if (minor)
  270. sprintf(disk->disk_name, "z2ram%d", minor);
  271. else
  272. sprintf(disk->disk_name, "z2ram");
  273. z2ram_gendisk[minor] = disk;
  274. err = add_disk(disk);
  275. if (err)
  276. put_disk(disk);
  277. return err;
  278. }
  279. static int __init z2_init(void)
  280. {
  281. int ret, i;
  282. if (!MACH_IS_AMIGA)
  283. return -ENODEV;
  284. if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
  285. return -EBUSY;
  286. tag_set.ops = &z2_mq_ops;
  287. tag_set.nr_hw_queues = 1;
  288. tag_set.nr_maps = 1;
  289. tag_set.queue_depth = 16;
  290. tag_set.numa_node = NUMA_NO_NODE;
  291. ret = blk_mq_alloc_tag_set(&tag_set);
  292. if (ret)
  293. goto out_unregister_blkdev;
  294. for (i = 0; i < Z2MINOR_COUNT; i++) {
  295. ret = z2ram_register_disk(i);
  296. if (ret && i == 0)
  297. goto out_free_tagset;
  298. }
  299. return 0;
  300. out_free_tagset:
  301. blk_mq_free_tag_set(&tag_set);
  302. out_unregister_blkdev:
  303. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  304. return ret;
  305. }
  306. static void __exit z2_exit(void)
  307. {
  308. int i, j;
  309. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  310. for (i = 0; i < Z2MINOR_COUNT; i++) {
  311. del_gendisk(z2ram_gendisk[i]);
  312. put_disk(z2ram_gendisk[i]);
  313. }
  314. blk_mq_free_tag_set(&tag_set);
  315. if (current_device != -1) {
  316. i = 0;
  317. for (j = 0; j < z2_count; j++) {
  318. set_bit(i++, zorro_unused_z2ram);
  319. }
  320. for (j = 0; j < chip_count; j++) {
  321. if (z2ram_map[i]) {
  322. amiga_chip_free((void *)z2ram_map[i++]);
  323. }
  324. }
  325. if (z2ram_map != NULL) {
  326. kfree(z2ram_map);
  327. }
  328. }
  329. return;
  330. }
  331. module_init(z2_init);
  332. module_exit(z2_exit);
  333. MODULE_DESCRIPTION("Amiga Zorro II ramdisk driver");
  334. MODULE_LICENSE("GPL");