Blame view

drivers/block/z2ram.c 9.18 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  /*
  ** z2ram - Amiga pseudo-driver to access 16bit-RAM in ZorroII space
  **         as a block device, to be used as a RAM disk or swap space
  ** 
  ** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  **
  ** ++Geert: support for zorro_unused_z2ram, better range checking
  ** ++roman: translate accesses via an array
  ** ++Milan: support for ChipRAM usage
  ** ++yambo: converted to 2.0 kernel
  ** ++yambo: modularized and support added for 3 minor devices including:
  **          MAJOR  MINOR  DESCRIPTION
  **          -----  -----  ----------------------------------------------
  **          37     0       Use Zorro II and Chip ram
  **          37     1       Use only Zorro II ram
  **          37     2       Use only Chip ram
  **          37     4-7     Use memory list entry 1-4 (first is 0)
  ** ++jskov: support for 1-4th memory list entry.
  **
  ** Permission to use, copy, modify, and distribute this software and its
  ** documentation for any purpose and without fee is hereby granted, provided
  ** that the above copyright notice appear in all copies and that both that
  ** copyright notice and this permission notice appear in supporting
  ** documentation.  This software is provided "as is" without express or
  ** implied warranty.
  */
  
  #define DEVICE_NAME "Z2RAM"
  
  #include <linux/major.h>
  #include <linux/vmalloc.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/blkdev.h>
  #include <linux/bitops.h>
2a48fc0ab   Arnd Bergmann   block: autoconver...
36
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
37
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
42
43
44
45
46
47
  
  #include <asm/setup.h>
  #include <asm/amigahw.h>
  #include <asm/pgtable.h>
  
  #include <linux/zorro.h>
  
  
  extern int m68k_realnum_memory;
  extern struct mem_info m68k_memory[NUM_MEMINFO];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
53
54
55
56
57
  #define Z2MINOR_COMBINED      (0)
  #define Z2MINOR_Z2ONLY        (1)
  #define Z2MINOR_CHIPONLY      (2)
  #define Z2MINOR_MEMLIST1      (4)
  #define Z2MINOR_MEMLIST2      (5)
  #define Z2MINOR_MEMLIST3      (6)
  #define Z2MINOR_MEMLIST4      (7)
  #define Z2MINOR_COUNT         (8) /* Move this down when adding a new minor */
  
  #define Z2RAM_CHUNK1024       ( Z2RAM_CHUNKSIZE >> 10 )
2a48fc0ab   Arnd Bergmann   block: autoconver...
58
  static DEFINE_MUTEX(z2ram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
66
  static u_long *z2ram_map    = NULL;
  static u_long z2ram_size    = 0;
  static int z2_count         = 0;
  static int chip_count       = 0;
  static int list_count       = 0;
  static int current_device   = -1;
  
  static DEFINE_SPINLOCK(z2ram_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  static struct gendisk *z2ram_gendisk;
165125e1e   Jens Axboe   [BLOCK] Get rid o...
68
  static void do_z2_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  {
  	struct request *req;
fb3ac7f6b   Tejun Heo   z2ram: dequeue in...
71

9934c8c04   Tejun Heo   block: implement ...
72
  	req = blk_fetch_request(q);
fb3ac7f6b   Tejun Heo   z2ram: dequeue in...
73
  	while (req) {
83096ebf1   Tejun Heo   block: convert to...
74
  		unsigned long start = blk_rq_pos(req) << 9;
1011c1b9f   Tejun Heo   block: blk_rq_[cu...
75
  		unsigned long len  = blk_rq_cur_bytes(req);
fb3ac7f6b   Tejun Heo   z2ram: dequeue in...
76
  		int err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  
  		if (start + len > z2ram_size) {
e1fbd9210   Geert Uytterhoeven   drivers/block/z2r...
79
80
81
82
83
  			pr_err(DEVICE_NAME ": bad access: block=%llu, "
  			       "count=%u
  ",
  			       (unsigned long long)blk_rq_pos(req),
  			       blk_rq_cur_sectors(req));
fb3ac7f6b   Tejun Heo   z2ram: dequeue in...
84
85
  			err = -EIO;
  			goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  		}
  		while (len) {
  			unsigned long addr = start & Z2RAM_CHUNKMASK;
  			unsigned long size = Z2RAM_CHUNKSIZE - addr;
  			if (len < size)
  				size = len;
  			addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
  			if (rq_data_dir(req) == READ)
  				memcpy(req->buffer, (char *)addr, size);
  			else
  				memcpy((char *)addr, req->buffer, size);
  			start += size;
  			len -= size;
  		}
fb3ac7f6b   Tejun Heo   z2ram: dequeue in...
100
  	done:
9934c8c04   Tejun Heo   block: implement ...
101
102
  		if (!__blk_end_request_cur(req, err))
  			req = blk_fetch_request(q);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  	}
  }
  
  static void
  get_z2ram( void )
  {
      int i;
  
      for ( i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++ )
      {
  	if ( test_bit( i, zorro_unused_z2ram ) )
  	{
  	    z2_count++;
  	    z2ram_map[ z2ram_size++ ] = 
  		ZTWO_VADDR( Z2RAM_START ) + ( i << Z2RAM_CHUNKSHIFT );
  	    clear_bit( i, zorro_unused_z2ram );
  	}
      }
  
      return;
  }
  
  static void
  get_chipram( void )
  {
  
      while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE * 4 ) )
      {
  	chip_count++;
  	z2ram_map[ z2ram_size ] =
  	    (u_long)amiga_chip_alloc( Z2RAM_CHUNKSIZE, "z2ram" );
  
  	if ( z2ram_map[ z2ram_size ] == 0 )
  	{
  	    break;
  	}
  
  	z2ram_size++;
      }
  	
      return;
  }
ab746cb93   Al Viro   [PATCH] switch z2ram
145
  static int z2_open(struct block_device *bdev, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
  {
      int device;
      int max_z2_map = ( Z2RAM_SIZE / Z2RAM_CHUNKSIZE ) *
  	sizeof( z2ram_map[0] );
      int max_chip_map = ( amiga_chip_size / Z2RAM_CHUNKSIZE ) *
  	sizeof( z2ram_map[0] );
      int rc = -ENOMEM;
ab746cb93   Al Viro   [PATCH] switch z2ram
153
      device = MINOR(bdev->bd_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154

2a48fc0ab   Arnd Bergmann   block: autoconver...
155
      mutex_lock(&z2ram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
      if ( current_device != -1 && current_device != device )
      {
  	rc = -EBUSY;
  	goto err_out;
      }
  
      if ( current_device == -1 )
      {
  	z2_count   = 0;
  	chip_count = 0;
  	list_count = 0;
  	z2ram_size = 0;
  
  	/* Use a specific list entry. */
  	if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
  		int index = device - Z2MINOR_MEMLIST1 + 1;
  		unsigned long size, paddr, vaddr;
  
  		if (index >= m68k_realnum_memory) {
  			printk( KERN_ERR DEVICE_NAME
  				": no such entry in z2ram_map
  " );
  		        goto err_out;
  		}
  
  		paddr = m68k_memory[index].addr;
  		size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE-1);
  
  #ifdef __powerpc__
  		/* FIXME: ioremap doesn't build correct memory tables. */
  		{
  			vfree(vmalloc (size));
  		}
  
  		vaddr = (unsigned long) __ioremap (paddr, size, 
  						   _PAGE_WRITETHRU);
  
  #else
  		vaddr = (unsigned long)z_remap_nocache_nonser(paddr, size);
  #endif
  		z2ram_map = 
  			kmalloc((size/Z2RAM_CHUNKSIZE)*sizeof(z2ram_map[0]),
  				GFP_KERNEL);
  		if ( z2ram_map == NULL )
  		{
  		    printk( KERN_ERR DEVICE_NAME
  			": cannot get mem for z2ram_map
  " );
  		    goto err_out;
  		}
  
  		while (size) {
  			z2ram_map[ z2ram_size++ ] = vaddr;
  			size -= Z2RAM_CHUNKSIZE;
  			vaddr += Z2RAM_CHUNKSIZE;
  			list_count++;
  		}
  
  		if ( z2ram_size != 0 )
  		    printk( KERN_INFO DEVICE_NAME
  			": using %iK List Entry %d Memory
  ",
  			list_count * Z2RAM_CHUNK1024, index );
  	} else
  
  	switch ( device )
  	{
  	    case Z2MINOR_COMBINED:
  
  		z2ram_map = kmalloc( max_z2_map + max_chip_map, GFP_KERNEL );
  		if ( z2ram_map == NULL )
  		{
  		    printk( KERN_ERR DEVICE_NAME
  			": cannot get mem for z2ram_map
  " );
  		    goto err_out;
  		}
  
  		get_z2ram();
  		get_chipram();
  
  		if ( z2ram_size != 0 )
  		    printk( KERN_INFO DEVICE_NAME 
  			": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)
  ",
  			z2_count * Z2RAM_CHUNK1024,
  			chip_count * Z2RAM_CHUNK1024,
  			( z2_count + chip_count ) * Z2RAM_CHUNK1024 );
  
  	    break;
  
      	    case Z2MINOR_Z2ONLY:
  		z2ram_map = kmalloc( max_z2_map, GFP_KERNEL );
  		if ( z2ram_map == NULL )
  		{
  		    printk( KERN_ERR DEVICE_NAME
  			": cannot get mem for z2ram_map
  " );
  		    goto err_out;
  		}
  
  		get_z2ram();
  
  		if ( z2ram_size != 0 )
  		    printk( KERN_INFO DEVICE_NAME 
  			": using %iK of Zorro II RAM
  ",
  			z2_count * Z2RAM_CHUNK1024 );
  
  	    break;
  
  	    case Z2MINOR_CHIPONLY:
  		z2ram_map = kmalloc( max_chip_map, GFP_KERNEL );
  		if ( z2ram_map == NULL )
  		{
  		    printk( KERN_ERR DEVICE_NAME
  			": cannot get mem for z2ram_map
  " );
  		    goto err_out;
  		}
  
  		get_chipram();
  
  		if ( z2ram_size != 0 )
  		    printk( KERN_INFO DEVICE_NAME 
  			": using %iK Chip RAM
  ",
  			chip_count * Z2RAM_CHUNK1024 );
  		    
  	    break;
  
  	    default:
  		rc = -ENODEV;
  		goto err_out;
  	
  	    break;
  	}
  
  	if ( z2ram_size == 0 )
  	{
  	    printk( KERN_NOTICE DEVICE_NAME
  		": no unused ZII/Chip RAM found
  " );
  	    goto err_out_kfree;
  	}
  
  	current_device = device;
  	z2ram_size <<= Z2RAM_CHUNKSHIFT;
  	set_capacity(z2ram_gendisk, z2ram_size >> 9);
      }
2a48fc0ab   Arnd Bergmann   block: autoconver...
306
      mutex_unlock(&z2ram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
      return 0;
  
  err_out_kfree:
f9101210e   Jesper Juhl   [PATCH] vfree and...
310
      kfree(z2ram_map);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
  err_out:
2a48fc0ab   Arnd Bergmann   block: autoconver...
312
      mutex_unlock(&z2ram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
314
315
316
      return rc;
  }
  
  static int
ab746cb93   Al Viro   [PATCH] switch z2ram
317
  z2_release(struct gendisk *disk, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  {
2a48fc0ab   Arnd Bergmann   block: autoconver...
319
      mutex_lock(&z2ram_mutex);
6e9624b8c   Arnd Bergmann   block: push down ...
320
      if ( current_device == -1 ) {
2a48fc0ab   Arnd Bergmann   block: autoconver...
321
      	mutex_unlock(&z2ram_mutex);
6e9624b8c   Arnd Bergmann   block: push down ...
322
323
      	return 0;
      }
2a48fc0ab   Arnd Bergmann   block: autoconver...
324
      mutex_unlock(&z2ram_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
328
329
330
      /*
       * FIXME: unmap memory
       */
  
      return 0;
  }
83d5cde47   Alexey Dobriyan   const: make block...
331
  static const struct block_device_operations z2_fops =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
333
  {
  	.owner		= THIS_MODULE,
ab746cb93   Al Viro   [PATCH] switch z2ram
334
335
  	.open		= z2_open,
  	.release	= z2_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
342
343
344
  };
  
  static struct kobject *z2_find(dev_t dev, int *part, void *data)
  {
  	*part = 0;
  	return get_disk(z2ram_gendisk);
  }
  
  static struct request_queue *z2_queue;
f39d88adc   Al Viro   [PATCH] z2_init()...
345
  static int __init 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
350
  z2_init(void)
  {
      int ret;
  
      if (!MACH_IS_AMIGA)
fd5b462f0   Geert Uytterhoeven   m68k: Return -ENO...
351
  	return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  
      ret = -EBUSY;
      if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
  	goto err;
  
      ret = -ENOMEM;
      z2ram_gendisk = alloc_disk(1);
      if (!z2ram_gendisk)
  	goto out_disk;
  
      z2_queue = blk_init_queue(do_z2_request, &z2ram_lock);
      if (!z2_queue)
  	goto out_queue;
  
      z2ram_gendisk->major = Z2RAM_MAJOR;
      z2ram_gendisk->first_minor = 0;
      z2ram_gendisk->fops = &z2_fops;
      sprintf(z2ram_gendisk->disk_name, "z2ram");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  
      z2ram_gendisk->queue = z2_queue;
      add_disk(z2ram_gendisk);
      blk_register_region(MKDEV(Z2RAM_MAJOR, 0), Z2MINOR_COUNT, THIS_MODULE,
  				z2_find, NULL, NULL);
  
      return 0;
  
  out_queue:
      put_disk(z2ram_gendisk);
  out_disk:
      unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
  err:
      return ret;
  }
f39d88adc   Al Viro   [PATCH] z2_init()...
385
  static void __exit z2_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
  {
      int i, j;
c9d4bc289   Zhaolei   z2ram: Small clea...
388
      blk_unregister_region(MKDEV(Z2RAM_MAJOR, 0), Z2MINOR_COUNT);
00d59405c   Akinobu Mita   unregister_blkdev...
389
      unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
      del_gendisk(z2ram_gendisk);
      put_disk(z2ram_gendisk);
      blk_cleanup_queue(z2_queue);
  
      if ( current_device != -1 )
      {
  	i = 0;
  
  	for ( j = 0 ; j < z2_count; j++ )
  	{
  	    set_bit( i++, zorro_unused_z2ram ); 
  	}
  
  	for ( j = 0 ; j < chip_count; j++ )
  	{
  	    if ( z2ram_map[ i ] )
  	    {
  		amiga_chip_free( (void *) z2ram_map[ i++ ] );
  	    }
  	}
  
  	if ( z2ram_map != NULL )
  	{
  	    kfree( z2ram_map );
  	}
      }
  
      return;
  } 
f39d88adc   Al Viro   [PATCH] z2_init()...
419
420
421
422
  
  module_init(z2_init);
  module_exit(z2_exit);
  MODULE_LICENSE("GPL");