z2ram.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_array(size / Z2RAM_CHUNKSIZE,
  158. sizeof(z2ram_map[0]), GFP_KERNEL);
  159. if (z2ram_map == NULL) {
  160. printk(KERN_ERR DEVICE_NAME
  161. ": cannot get mem for z2ram_map\n");
  162. goto err_out;
  163. }
  164. while (size) {
  165. z2ram_map[z2ram_size++] = vaddr;
  166. size -= Z2RAM_CHUNKSIZE;
  167. vaddr += Z2RAM_CHUNKSIZE;
  168. list_count++;
  169. }
  170. if (z2ram_size != 0)
  171. printk(KERN_INFO DEVICE_NAME
  172. ": using %iK List Entry %d Memory\n",
  173. list_count * Z2RAM_CHUNK1024, index);
  174. } else
  175. switch (device) {
  176. case Z2MINOR_COMBINED:
  177. z2ram_map =
  178. kmalloc(max_z2_map + max_chip_map,
  179. GFP_KERNEL);
  180. if (z2ram_map == NULL) {
  181. printk(KERN_ERR DEVICE_NAME
  182. ": cannot get mem for z2ram_map\n");
  183. goto err_out;
  184. }
  185. get_z2ram();
  186. get_chipram();
  187. if (z2ram_size != 0)
  188. printk(KERN_INFO DEVICE_NAME
  189. ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
  190. z2_count * Z2RAM_CHUNK1024,
  191. chip_count * Z2RAM_CHUNK1024,
  192. (z2_count +
  193. chip_count) * Z2RAM_CHUNK1024);
  194. break;
  195. case Z2MINOR_Z2ONLY:
  196. z2ram_map = kmalloc(max_z2_map, GFP_KERNEL);
  197. if (!z2ram_map)
  198. goto err_out;
  199. get_z2ram();
  200. if (z2ram_size != 0)
  201. printk(KERN_INFO DEVICE_NAME
  202. ": using %iK of Zorro II RAM\n",
  203. z2_count * Z2RAM_CHUNK1024);
  204. break;
  205. case Z2MINOR_CHIPONLY:
  206. z2ram_map = kmalloc(max_chip_map, GFP_KERNEL);
  207. if (!z2ram_map)
  208. goto err_out;
  209. get_chipram();
  210. if (z2ram_size != 0)
  211. printk(KERN_INFO DEVICE_NAME
  212. ": using %iK Chip RAM\n",
  213. chip_count * Z2RAM_CHUNK1024);
  214. break;
  215. default:
  216. rc = -ENODEV;
  217. goto err_out;
  218. break;
  219. }
  220. if (z2ram_size == 0) {
  221. printk(KERN_NOTICE DEVICE_NAME
  222. ": no unused ZII/Chip RAM found\n");
  223. goto err_out_kfree;
  224. }
  225. current_device = device;
  226. z2ram_size <<= Z2RAM_CHUNKSHIFT;
  227. set_capacity(z2ram_gendisk[device], z2ram_size >> 9);
  228. }
  229. mutex_unlock(&z2ram_mutex);
  230. return 0;
  231. err_out_kfree:
  232. kfree(z2ram_map);
  233. err_out:
  234. mutex_unlock(&z2ram_mutex);
  235. return rc;
  236. }
  237. static void z2_release(struct gendisk *disk)
  238. {
  239. mutex_lock(&z2ram_mutex);
  240. if (current_device == -1) {
  241. mutex_unlock(&z2ram_mutex);
  242. return;
  243. }
  244. mutex_unlock(&z2ram_mutex);
  245. /*
  246. * FIXME: unmap memory
  247. */
  248. }
  249. static const struct block_device_operations z2_fops = {
  250. .owner = THIS_MODULE,
  251. .open = z2_open,
  252. .release = z2_release,
  253. };
  254. static struct blk_mq_tag_set tag_set;
  255. static const struct blk_mq_ops z2_mq_ops = {
  256. .queue_rq = z2_queue_rq,
  257. };
  258. static int z2ram_register_disk(int minor)
  259. {
  260. struct gendisk *disk;
  261. int err;
  262. disk = blk_mq_alloc_disk(&tag_set, NULL, NULL);
  263. if (IS_ERR(disk))
  264. return PTR_ERR(disk);
  265. disk->major = Z2RAM_MAJOR;
  266. disk->first_minor = minor;
  267. disk->minors = 1;
  268. disk->flags |= GENHD_FL_NO_PART;
  269. disk->fops = &z2_fops;
  270. if (minor)
  271. sprintf(disk->disk_name, "z2ram%d", minor);
  272. else
  273. sprintf(disk->disk_name, "z2ram");
  274. z2ram_gendisk[minor] = disk;
  275. err = add_disk(disk);
  276. if (err)
  277. put_disk(disk);
  278. return err;
  279. }
  280. static int __init z2_init(void)
  281. {
  282. int ret, i;
  283. if (!MACH_IS_AMIGA)
  284. return -ENODEV;
  285. if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
  286. return -EBUSY;
  287. tag_set.ops = &z2_mq_ops;
  288. tag_set.nr_hw_queues = 1;
  289. tag_set.nr_maps = 1;
  290. tag_set.queue_depth = 16;
  291. tag_set.numa_node = NUMA_NO_NODE;
  292. tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  293. ret = blk_mq_alloc_tag_set(&tag_set);
  294. if (ret)
  295. goto out_unregister_blkdev;
  296. for (i = 0; i < Z2MINOR_COUNT; i++) {
  297. ret = z2ram_register_disk(i);
  298. if (ret && i == 0)
  299. goto out_free_tagset;
  300. }
  301. return 0;
  302. out_free_tagset:
  303. blk_mq_free_tag_set(&tag_set);
  304. out_unregister_blkdev:
  305. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  306. return ret;
  307. }
  308. static void __exit z2_exit(void)
  309. {
  310. int i, j;
  311. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  312. for (i = 0; i < Z2MINOR_COUNT; i++) {
  313. del_gendisk(z2ram_gendisk[i]);
  314. put_disk(z2ram_gendisk[i]);
  315. }
  316. blk_mq_free_tag_set(&tag_set);
  317. if (current_device != -1) {
  318. i = 0;
  319. for (j = 0; j < z2_count; j++) {
  320. set_bit(i++, zorro_unused_z2ram);
  321. }
  322. for (j = 0; j < chip_count; j++) {
  323. if (z2ram_map[i]) {
  324. amiga_chip_free((void *)z2ram_map[i++]);
  325. }
  326. }
  327. if (z2ram_map != NULL) {
  328. kfree(z2ram_map);
  329. }
  330. }
  331. return;
  332. }
  333. module_init(z2_init);
  334. module_exit(z2_exit);
  335. MODULE_DESCRIPTION("Amiga Zorro II ramdisk driver");
  336. MODULE_LICENSE("GPL");