blob: c909c1a3f6507cf0817ef6f63a44d972bc8b3e2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
28#define DEVICE_NAME "Z2RAM"
29
30#include <linux/major.h>
31#include <linux/vmalloc.h>
32#include <linux/init.h>
33#include <linux/module.h>
34#include <linux/blkdev.h>
35#include <linux/bitops.h>
36
37#include <asm/setup.h>
38#include <asm/amigahw.h>
39#include <asm/pgtable.h>
40
41#include <linux/zorro.h>
42
43
44extern int m68k_realnum_memory;
45extern struct mem_info m68k_memory[NUM_MEMINFO];
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define Z2MINOR_COMBINED (0)
48#define Z2MINOR_Z2ONLY (1)
49#define Z2MINOR_CHIPONLY (2)
50#define Z2MINOR_MEMLIST1 (4)
51#define Z2MINOR_MEMLIST2 (5)
52#define Z2MINOR_MEMLIST3 (6)
53#define Z2MINOR_MEMLIST4 (7)
54#define Z2MINOR_COUNT (8) /* Move this down when adding a new minor */
55
56#define Z2RAM_CHUNK1024 ( Z2RAM_CHUNKSIZE >> 10 )
57
58static u_long *z2ram_map = NULL;
59static u_long z2ram_size = 0;
60static int z2_count = 0;
61static int chip_count = 0;
62static int list_count = 0;
63static int current_device = -1;
64
65static DEFINE_SPINLOCK(z2ram_lock);
66
67static struct block_device_operations z2_fops;
68static struct gendisk *z2ram_gendisk;
69
Jens Axboe165125e2007-07-24 09:28:11 +020070static void do_z2_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct request *req;
Tejun Heofb3ac7f6b2009-05-08 11:54:13 +090073
74 req = elv_next_request(q);
75 if (req)
76 blkdev_dequeue_request(req);
77
78 while (req) {
Tejun Heo83096eb2009-05-07 22:24:39 +090079 unsigned long start = blk_rq_pos(req) << 9;
Tejun Heo1011c1b2009-05-07 22:24:45 +090080 unsigned long len = blk_rq_cur_bytes(req);
Tejun Heofb3ac7f6b2009-05-08 11:54:13 +090081 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (start + len > z2ram_size) {
84 printk( KERN_ERR DEVICE_NAME ": bad access: block=%lu, count=%u\n",
Tejun Heo83096eb2009-05-07 22:24:39 +090085 blk_rq_pos(req), blk_rq_cur_sectors(req));
Tejun Heofb3ac7f6b2009-05-08 11:54:13 +090086 err = -EIO;
87 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 while (len) {
90 unsigned long addr = start & Z2RAM_CHUNKMASK;
91 unsigned long size = Z2RAM_CHUNKSIZE - addr;
92 if (len < size)
93 size = len;
94 addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
95 if (rq_data_dir(req) == READ)
96 memcpy(req->buffer, (char *)addr, size);
97 else
98 memcpy((char *)addr, req->buffer, size);
99 start += size;
100 len -= size;
101 }
Tejun Heofb3ac7f6b2009-05-08 11:54:13 +0900102 done:
103 if (!__blk_end_request_cur(req, err)) {
104 req = elv_next_request(q);
105 if (req)
106 blkdev_dequeue_request(req);
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109}
110
111static void
112get_z2ram( void )
113{
114 int i;
115
116 for ( i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++ )
117 {
118 if ( test_bit( i, zorro_unused_z2ram ) )
119 {
120 z2_count++;
121 z2ram_map[ z2ram_size++ ] =
122 ZTWO_VADDR( Z2RAM_START ) + ( i << Z2RAM_CHUNKSHIFT );
123 clear_bit( i, zorro_unused_z2ram );
124 }
125 }
126
127 return;
128}
129
130static void
131get_chipram( void )
132{
133
134 while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE * 4 ) )
135 {
136 chip_count++;
137 z2ram_map[ z2ram_size ] =
138 (u_long)amiga_chip_alloc( Z2RAM_CHUNKSIZE, "z2ram" );
139
140 if ( z2ram_map[ z2ram_size ] == 0 )
141 {
142 break;
143 }
144
145 z2ram_size++;
146 }
147
148 return;
149}
150
Al Viroab746cb2008-03-02 10:24:45 -0500151static int z2_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int device;
154 int max_z2_map = ( Z2RAM_SIZE / Z2RAM_CHUNKSIZE ) *
155 sizeof( z2ram_map[0] );
156 int max_chip_map = ( amiga_chip_size / Z2RAM_CHUNKSIZE ) *
157 sizeof( z2ram_map[0] );
158 int rc = -ENOMEM;
159
Al Viroab746cb2008-03-02 10:24:45 -0500160 device = MINOR(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if ( current_device != -1 && current_device != device )
163 {
164 rc = -EBUSY;
165 goto err_out;
166 }
167
168 if ( current_device == -1 )
169 {
170 z2_count = 0;
171 chip_count = 0;
172 list_count = 0;
173 z2ram_size = 0;
174
175 /* Use a specific list entry. */
176 if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
177 int index = device - Z2MINOR_MEMLIST1 + 1;
178 unsigned long size, paddr, vaddr;
179
180 if (index >= m68k_realnum_memory) {
181 printk( KERN_ERR DEVICE_NAME
182 ": no such entry in z2ram_map\n" );
183 goto err_out;
184 }
185
186 paddr = m68k_memory[index].addr;
187 size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE-1);
188
189#ifdef __powerpc__
190 /* FIXME: ioremap doesn't build correct memory tables. */
191 {
192 vfree(vmalloc (size));
193 }
194
195 vaddr = (unsigned long) __ioremap (paddr, size,
196 _PAGE_WRITETHRU);
197
198#else
199 vaddr = (unsigned long)z_remap_nocache_nonser(paddr, size);
200#endif
201 z2ram_map =
202 kmalloc((size/Z2RAM_CHUNKSIZE)*sizeof(z2ram_map[0]),
203 GFP_KERNEL);
204 if ( z2ram_map == NULL )
205 {
206 printk( KERN_ERR DEVICE_NAME
207 ": cannot get mem for z2ram_map\n" );
208 goto err_out;
209 }
210
211 while (size) {
212 z2ram_map[ z2ram_size++ ] = vaddr;
213 size -= Z2RAM_CHUNKSIZE;
214 vaddr += Z2RAM_CHUNKSIZE;
215 list_count++;
216 }
217
218 if ( z2ram_size != 0 )
219 printk( KERN_INFO DEVICE_NAME
220 ": using %iK List Entry %d Memory\n",
221 list_count * Z2RAM_CHUNK1024, index );
222 } else
223
224 switch ( device )
225 {
226 case Z2MINOR_COMBINED:
227
228 z2ram_map = kmalloc( max_z2_map + max_chip_map, GFP_KERNEL );
229 if ( z2ram_map == NULL )
230 {
231 printk( KERN_ERR DEVICE_NAME
232 ": cannot get mem for z2ram_map\n" );
233 goto err_out;
234 }
235
236 get_z2ram();
237 get_chipram();
238
239 if ( z2ram_size != 0 )
240 printk( KERN_INFO DEVICE_NAME
241 ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
242 z2_count * Z2RAM_CHUNK1024,
243 chip_count * Z2RAM_CHUNK1024,
244 ( z2_count + chip_count ) * Z2RAM_CHUNK1024 );
245
246 break;
247
248 case Z2MINOR_Z2ONLY:
249 z2ram_map = kmalloc( max_z2_map, GFP_KERNEL );
250 if ( z2ram_map == NULL )
251 {
252 printk( KERN_ERR DEVICE_NAME
253 ": cannot get mem for z2ram_map\n" );
254 goto err_out;
255 }
256
257 get_z2ram();
258
259 if ( z2ram_size != 0 )
260 printk( KERN_INFO DEVICE_NAME
261 ": using %iK of Zorro II RAM\n",
262 z2_count * Z2RAM_CHUNK1024 );
263
264 break;
265
266 case Z2MINOR_CHIPONLY:
267 z2ram_map = kmalloc( max_chip_map, GFP_KERNEL );
268 if ( z2ram_map == NULL )
269 {
270 printk( KERN_ERR DEVICE_NAME
271 ": cannot get mem for z2ram_map\n" );
272 goto err_out;
273 }
274
275 get_chipram();
276
277 if ( z2ram_size != 0 )
278 printk( KERN_INFO DEVICE_NAME
279 ": using %iK Chip RAM\n",
280 chip_count * Z2RAM_CHUNK1024 );
281
282 break;
283
284 default:
285 rc = -ENODEV;
286 goto err_out;
287
288 break;
289 }
290
291 if ( z2ram_size == 0 )
292 {
293 printk( KERN_NOTICE DEVICE_NAME
294 ": no unused ZII/Chip RAM found\n" );
295 goto err_out_kfree;
296 }
297
298 current_device = device;
299 z2ram_size <<= Z2RAM_CHUNKSHIFT;
300 set_capacity(z2ram_gendisk, z2ram_size >> 9);
301 }
302
303 return 0;
304
305err_out_kfree:
Jesper Juhlf91012102005-09-10 00:26:54 -0700306 kfree(z2ram_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307err_out:
308 return rc;
309}
310
311static int
Al Viroab746cb2008-03-02 10:24:45 -0500312z2_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 if ( current_device == -1 )
315 return 0;
316
317 /*
318 * FIXME: unmap memory
319 */
320
321 return 0;
322}
323
324static struct block_device_operations z2_fops =
325{
326 .owner = THIS_MODULE,
Al Viroab746cb2008-03-02 10:24:45 -0500327 .open = z2_open,
328 .release = z2_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
331static struct kobject *z2_find(dev_t dev, int *part, void *data)
332{
333 *part = 0;
334 return get_disk(z2ram_gendisk);
335}
336
337static struct request_queue *z2_queue;
338
Al Virof39d88a2006-10-11 17:28:47 +0100339static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340z2_init(void)
341{
342 int ret;
343
344 if (!MACH_IS_AMIGA)
Geert Uytterhoevenfd5b4622008-05-18 20:47:18 +0200345 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 ret = -EBUSY;
348 if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
349 goto err;
350
351 ret = -ENOMEM;
352 z2ram_gendisk = alloc_disk(1);
353 if (!z2ram_gendisk)
354 goto out_disk;
355
356 z2_queue = blk_init_queue(do_z2_request, &z2ram_lock);
357 if (!z2_queue)
358 goto out_queue;
359
360 z2ram_gendisk->major = Z2RAM_MAJOR;
361 z2ram_gendisk->first_minor = 0;
362 z2ram_gendisk->fops = &z2_fops;
363 sprintf(z2ram_gendisk->disk_name, "z2ram");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 z2ram_gendisk->queue = z2_queue;
366 add_disk(z2ram_gendisk);
367 blk_register_region(MKDEV(Z2RAM_MAJOR, 0), Z2MINOR_COUNT, THIS_MODULE,
368 z2_find, NULL, NULL);
369
370 return 0;
371
372out_queue:
373 put_disk(z2ram_gendisk);
374out_disk:
375 unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
376err:
377 return ret;
378}
379
Al Virof39d88a2006-10-11 17:28:47 +0100380static void __exit z2_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 int i, j;
383 blk_unregister_region(MKDEV(Z2RAM_MAJOR, 0), 256);
Akinobu Mita00d59402007-07-17 04:03:46 -0700384 unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 del_gendisk(z2ram_gendisk);
386 put_disk(z2ram_gendisk);
387 blk_cleanup_queue(z2_queue);
388
389 if ( current_device != -1 )
390 {
391 i = 0;
392
393 for ( j = 0 ; j < z2_count; j++ )
394 {
395 set_bit( i++, zorro_unused_z2ram );
396 }
397
398 for ( j = 0 ; j < chip_count; j++ )
399 {
400 if ( z2ram_map[ i ] )
401 {
402 amiga_chip_free( (void *) z2ram_map[ i++ ] );
403 }
404 }
405
406 if ( z2ram_map != NULL )
407 {
408 kfree( z2ram_map );
409 }
410 }
411
412 return;
413}
Al Virof39d88a2006-10-11 17:28:47 +0100414
415module_init(z2_init);
416module_exit(z2_exit);
417MODULE_LICENSE("GPL");