z2ram.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/blkdev.h>
  33. #include <linux/bitops.h>
  34. #include <linux/mutex.h>
  35. #include <linux/slab.h>
  36. #include <asm/setup.h>
  37. #include <asm/amigahw.h>
  38. #include <asm/pgtable.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;
  58. static void do_z2_request(struct request_queue *q)
  59. {
  60. struct request *req;
  61. req = blk_fetch_request(q);
  62. while (req) {
  63. unsigned long start = blk_rq_pos(req) << 9;
  64. unsigned long len = blk_rq_cur_bytes(req);
  65. int err = 0;
  66. if (start + len > z2ram_size) {
  67. pr_err(DEVICE_NAME ": bad access: block=%llu, "
  68. "count=%u\n",
  69. (unsigned long long)blk_rq_pos(req),
  70. blk_rq_cur_sectors(req));
  71. err = -EIO;
  72. goto done;
  73. }
  74. while (len) {
  75. unsigned long addr = start & Z2RAM_CHUNKMASK;
  76. unsigned long size = Z2RAM_CHUNKSIZE - addr;
  77. void *buffer = bio_data(req->bio);
  78. if (len < size)
  79. size = len;
  80. addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
  81. if (rq_data_dir(req) == READ)
  82. memcpy(buffer, (char *)addr, size);
  83. else
  84. memcpy((char *)addr, buffer, size);
  85. start += size;
  86. len -= size;
  87. }
  88. done:
  89. if (!__blk_end_request_cur(req, err))
  90. req = blk_fetch_request(q);
  91. }
  92. }
  93. static void
  94. get_z2ram( void )
  95. {
  96. int i;
  97. for ( i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++ )
  98. {
  99. if ( test_bit( i, zorro_unused_z2ram ) )
  100. {
  101. z2_count++;
  102. z2ram_map[z2ram_size++] = (unsigned long)ZTWO_VADDR(Z2RAM_START) +
  103. (i << Z2RAM_CHUNKSHIFT);
  104. clear_bit( i, zorro_unused_z2ram );
  105. }
  106. }
  107. return;
  108. }
  109. static void
  110. get_chipram( void )
  111. {
  112. while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE * 4 ) )
  113. {
  114. chip_count++;
  115. z2ram_map[ z2ram_size ] =
  116. (u_long)amiga_chip_alloc( Z2RAM_CHUNKSIZE, "z2ram" );
  117. if ( z2ram_map[ z2ram_size ] == 0 )
  118. {
  119. break;
  120. }
  121. z2ram_size++;
  122. }
  123. return;
  124. }
  125. static int z2_open(struct block_device *bdev, fmode_t mode)
  126. {
  127. int device;
  128. int max_z2_map = ( Z2RAM_SIZE / Z2RAM_CHUNKSIZE ) *
  129. sizeof( z2ram_map[0] );
  130. int max_chip_map = ( amiga_chip_size / Z2RAM_CHUNKSIZE ) *
  131. sizeof( z2ram_map[0] );
  132. int rc = -ENOMEM;
  133. device = MINOR(bdev->bd_dev);
  134. mutex_lock(&z2ram_mutex);
  135. if ( current_device != -1 && current_device != device )
  136. {
  137. rc = -EBUSY;
  138. goto err_out;
  139. }
  140. if ( current_device == -1 )
  141. {
  142. z2_count = 0;
  143. chip_count = 0;
  144. list_count = 0;
  145. z2ram_size = 0;
  146. /* Use a specific list entry. */
  147. if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
  148. int index = device - Z2MINOR_MEMLIST1 + 1;
  149. unsigned long size, paddr, vaddr;
  150. if (index >= m68k_realnum_memory) {
  151. printk( KERN_ERR DEVICE_NAME
  152. ": no such entry in z2ram_map\n" );
  153. goto err_out;
  154. }
  155. paddr = m68k_memory[index].addr;
  156. size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE-1);
  157. #ifdef __powerpc__
  158. /* FIXME: ioremap doesn't build correct memory tables. */
  159. {
  160. vfree(vmalloc (size));
  161. }
  162. vaddr = (unsigned long) __ioremap (paddr, size,
  163. _PAGE_WRITETHRU);
  164. #else
  165. vaddr = (unsigned long)z_remap_nocache_nonser(paddr, size);
  166. #endif
  167. z2ram_map =
  168. kmalloc((size/Z2RAM_CHUNKSIZE)*sizeof(z2ram_map[0]),
  169. GFP_KERNEL);
  170. if ( z2ram_map == NULL )
  171. {
  172. printk( KERN_ERR DEVICE_NAME
  173. ": cannot get mem for z2ram_map\n" );
  174. goto err_out;
  175. }
  176. while (size) {
  177. z2ram_map[ z2ram_size++ ] = vaddr;
  178. size -= Z2RAM_CHUNKSIZE;
  179. vaddr += Z2RAM_CHUNKSIZE;
  180. list_count++;
  181. }
  182. if ( z2ram_size != 0 )
  183. printk( KERN_INFO DEVICE_NAME
  184. ": using %iK List Entry %d Memory\n",
  185. list_count * Z2RAM_CHUNK1024, index );
  186. } else
  187. switch ( device )
  188. {
  189. case Z2MINOR_COMBINED:
  190. z2ram_map = kmalloc( max_z2_map + max_chip_map, GFP_KERNEL );
  191. if ( z2ram_map == NULL )
  192. {
  193. printk( KERN_ERR DEVICE_NAME
  194. ": cannot get mem for z2ram_map\n" );
  195. goto err_out;
  196. }
  197. get_z2ram();
  198. get_chipram();
  199. if ( z2ram_size != 0 )
  200. printk( KERN_INFO DEVICE_NAME
  201. ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
  202. z2_count * Z2RAM_CHUNK1024,
  203. chip_count * Z2RAM_CHUNK1024,
  204. ( z2_count + chip_count ) * Z2RAM_CHUNK1024 );
  205. break;
  206. case Z2MINOR_Z2ONLY:
  207. z2ram_map = kmalloc( max_z2_map, GFP_KERNEL );
  208. if ( z2ram_map == NULL )
  209. {
  210. printk( KERN_ERR DEVICE_NAME
  211. ": cannot get mem for z2ram_map\n" );
  212. goto err_out;
  213. }
  214. get_z2ram();
  215. if ( z2ram_size != 0 )
  216. printk( KERN_INFO DEVICE_NAME
  217. ": using %iK of Zorro II RAM\n",
  218. z2_count * Z2RAM_CHUNK1024 );
  219. break;
  220. case Z2MINOR_CHIPONLY:
  221. z2ram_map = kmalloc( max_chip_map, GFP_KERNEL );
  222. if ( z2ram_map == NULL )
  223. {
  224. printk( KERN_ERR DEVICE_NAME
  225. ": cannot get mem for z2ram_map\n" );
  226. goto err_out;
  227. }
  228. get_chipram();
  229. if ( z2ram_size != 0 )
  230. printk( KERN_INFO DEVICE_NAME
  231. ": using %iK Chip RAM\n",
  232. chip_count * Z2RAM_CHUNK1024 );
  233. break;
  234. default:
  235. rc = -ENODEV;
  236. goto err_out;
  237. break;
  238. }
  239. if ( z2ram_size == 0 )
  240. {
  241. printk( KERN_NOTICE DEVICE_NAME
  242. ": no unused ZII/Chip RAM found\n" );
  243. goto err_out_kfree;
  244. }
  245. current_device = device;
  246. z2ram_size <<= Z2RAM_CHUNKSHIFT;
  247. set_capacity(z2ram_gendisk, z2ram_size >> 9);
  248. }
  249. mutex_unlock(&z2ram_mutex);
  250. return 0;
  251. err_out_kfree:
  252. kfree(z2ram_map);
  253. err_out:
  254. mutex_unlock(&z2ram_mutex);
  255. return rc;
  256. }
  257. static void
  258. z2_release(struct gendisk *disk, fmode_t mode)
  259. {
  260. mutex_lock(&z2ram_mutex);
  261. if ( current_device == -1 ) {
  262. mutex_unlock(&z2ram_mutex);
  263. return;
  264. }
  265. mutex_unlock(&z2ram_mutex);
  266. /*
  267. * FIXME: unmap memory
  268. */
  269. }
  270. static const struct block_device_operations z2_fops =
  271. {
  272. .owner = THIS_MODULE,
  273. .open = z2_open,
  274. .release = z2_release,
  275. };
  276. static struct kobject *z2_find(dev_t dev, int *part, void *data)
  277. {
  278. *part = 0;
  279. return get_disk(z2ram_gendisk);
  280. }
  281. static struct request_queue *z2_queue;
  282. static int __init
  283. z2_init(void)
  284. {
  285. int ret;
  286. if (!MACH_IS_AMIGA)
  287. return -ENODEV;
  288. ret = -EBUSY;
  289. if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
  290. goto err;
  291. ret = -ENOMEM;
  292. z2ram_gendisk = alloc_disk(1);
  293. if (!z2ram_gendisk)
  294. goto out_disk;
  295. z2_queue = blk_init_queue(do_z2_request, &z2ram_lock);
  296. if (!z2_queue)
  297. goto out_queue;
  298. z2ram_gendisk->major = Z2RAM_MAJOR;
  299. z2ram_gendisk->first_minor = 0;
  300. z2ram_gendisk->fops = &z2_fops;
  301. sprintf(z2ram_gendisk->disk_name, "z2ram");
  302. z2ram_gendisk->queue = z2_queue;
  303. add_disk(z2ram_gendisk);
  304. blk_register_region(MKDEV(Z2RAM_MAJOR, 0), Z2MINOR_COUNT, THIS_MODULE,
  305. z2_find, NULL, NULL);
  306. return 0;
  307. out_queue:
  308. put_disk(z2ram_gendisk);
  309. out_disk:
  310. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  311. err:
  312. return ret;
  313. }
  314. static void __exit z2_exit(void)
  315. {
  316. int i, j;
  317. blk_unregister_region(MKDEV(Z2RAM_MAJOR, 0), Z2MINOR_COUNT);
  318. unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  319. del_gendisk(z2ram_gendisk);
  320. put_disk(z2ram_gendisk);
  321. blk_cleanup_queue(z2_queue);
  322. if ( current_device != -1 )
  323. {
  324. i = 0;
  325. for ( j = 0 ; j < z2_count; j++ )
  326. {
  327. set_bit( i++, zorro_unused_z2ram );
  328. }
  329. for ( j = 0 ; j < chip_count; j++ )
  330. {
  331. if ( z2ram_map[ i ] )
  332. {
  333. amiga_chip_free( (void *) z2ram_map[ i++ ] );
  334. }
  335. }
  336. if ( z2ram_map != NULL )
  337. {
  338. kfree( z2ram_map );
  339. }
  340. }
  341. return;
  342. }
  343. module_init(z2_init);
  344. module_exit(z2_exit);
  345. MODULE_LICENSE("GPL");