Back to home page

OSCL-LXR

 
 

    


0001 /*
0002 ** z2ram - Amiga pseudo-driver to access 16bit-RAM in ZorroII space
0003 **         as a block device, to be used as a RAM disk or swap space
0004 ** 
0005 ** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
0006 **
0007 ** ++Geert: support for zorro_unused_z2ram, better range checking
0008 ** ++roman: translate accesses via an array
0009 ** ++Milan: support for ChipRAM usage
0010 ** ++yambo: converted to 2.0 kernel
0011 ** ++yambo: modularized and support added for 3 minor devices including:
0012 **          MAJOR  MINOR  DESCRIPTION
0013 **          -----  -----  ----------------------------------------------
0014 **          37     0       Use Zorro II and Chip ram
0015 **          37     1       Use only Zorro II ram
0016 **          37     2       Use only Chip ram
0017 **          37     4-7     Use memory list entry 1-4 (first is 0)
0018 ** ++jskov: support for 1-4th memory list entry.
0019 **
0020 ** Permission to use, copy, modify, and distribute this software and its
0021 ** documentation for any purpose and without fee is hereby granted, provided
0022 ** that the above copyright notice appear in all copies and that both that
0023 ** copyright notice and this permission notice appear in supporting
0024 ** documentation.  This software is provided "as is" without express or
0025 ** implied warranty.
0026 */
0027 
0028 #define DEVICE_NAME "Z2RAM"
0029 
0030 #include <linux/major.h>
0031 #include <linux/vmalloc.h>
0032 #include <linux/init.h>
0033 #include <linux/module.h>
0034 #include <linux/blk-mq.h>
0035 #include <linux/bitops.h>
0036 #include <linux/mutex.h>
0037 #include <linux/slab.h>
0038 #include <linux/pgtable.h>
0039 
0040 #include <asm/setup.h>
0041 #include <asm/amigahw.h>
0042 
0043 #include <linux/zorro.h>
0044 
0045 #define Z2MINOR_COMBINED      (0)
0046 #define Z2MINOR_Z2ONLY        (1)
0047 #define Z2MINOR_CHIPONLY      (2)
0048 #define Z2MINOR_MEMLIST1      (4)
0049 #define Z2MINOR_MEMLIST2      (5)
0050 #define Z2MINOR_MEMLIST3      (6)
0051 #define Z2MINOR_MEMLIST4      (7)
0052 #define Z2MINOR_COUNT         (8)   /* Move this down when adding a new minor */
0053 
0054 #define Z2RAM_CHUNK1024       ( Z2RAM_CHUNKSIZE >> 10 )
0055 
0056 static DEFINE_MUTEX(z2ram_mutex);
0057 static u_long *z2ram_map = NULL;
0058 static u_long z2ram_size = 0;
0059 static int z2_count = 0;
0060 static int chip_count = 0;
0061 static int list_count = 0;
0062 static int current_device = -1;
0063 
0064 static DEFINE_SPINLOCK(z2ram_lock);
0065 
0066 static struct gendisk *z2ram_gendisk[Z2MINOR_COUNT];
0067 
0068 static blk_status_t z2_queue_rq(struct blk_mq_hw_ctx *hctx,
0069                 const struct blk_mq_queue_data *bd)
0070 {
0071     struct request *req = bd->rq;
0072     unsigned long start = blk_rq_pos(req) << 9;
0073     unsigned long len = blk_rq_cur_bytes(req);
0074 
0075     blk_mq_start_request(req);
0076 
0077     if (start + len > z2ram_size) {
0078         pr_err(DEVICE_NAME ": bad access: block=%llu, "
0079                "count=%u\n",
0080                (unsigned long long)blk_rq_pos(req),
0081                blk_rq_cur_sectors(req));
0082         return BLK_STS_IOERR;
0083     }
0084 
0085     spin_lock_irq(&z2ram_lock);
0086 
0087     while (len) {
0088         unsigned long addr = start & Z2RAM_CHUNKMASK;
0089         unsigned long size = Z2RAM_CHUNKSIZE - addr;
0090         void *buffer = bio_data(req->bio);
0091 
0092         if (len < size)
0093             size = len;
0094         addr += z2ram_map[start >> Z2RAM_CHUNKSHIFT];
0095         if (rq_data_dir(req) == READ)
0096             memcpy(buffer, (char *)addr, size);
0097         else
0098             memcpy((char *)addr, buffer, size);
0099         start += size;
0100         len -= size;
0101     }
0102 
0103     spin_unlock_irq(&z2ram_lock);
0104     blk_mq_end_request(req, BLK_STS_OK);
0105     return BLK_STS_OK;
0106 }
0107 
0108 static void get_z2ram(void)
0109 {
0110     int i;
0111 
0112     for (i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++) {
0113         if (test_bit(i, zorro_unused_z2ram)) {
0114             z2_count++;
0115             z2ram_map[z2ram_size++] =
0116                 (unsigned long)ZTWO_VADDR(Z2RAM_START) +
0117                 (i << Z2RAM_CHUNKSHIFT);
0118             clear_bit(i, zorro_unused_z2ram);
0119         }
0120     }
0121 
0122     return;
0123 }
0124 
0125 static void get_chipram(void)
0126 {
0127 
0128     while (amiga_chip_avail() > (Z2RAM_CHUNKSIZE * 4)) {
0129         chip_count++;
0130         z2ram_map[z2ram_size] =
0131             (u_long) amiga_chip_alloc(Z2RAM_CHUNKSIZE, "z2ram");
0132 
0133         if (z2ram_map[z2ram_size] == 0) {
0134             break;
0135         }
0136 
0137         z2ram_size++;
0138     }
0139 
0140     return;
0141 }
0142 
0143 static int z2_open(struct block_device *bdev, fmode_t mode)
0144 {
0145     int device;
0146     int max_z2_map = (Z2RAM_SIZE / Z2RAM_CHUNKSIZE) * sizeof(z2ram_map[0]);
0147     int max_chip_map = (amiga_chip_size / Z2RAM_CHUNKSIZE) *
0148         sizeof(z2ram_map[0]);
0149     int rc = -ENOMEM;
0150 
0151     device = MINOR(bdev->bd_dev);
0152 
0153     mutex_lock(&z2ram_mutex);
0154     if (current_device != -1 && current_device != device) {
0155         rc = -EBUSY;
0156         goto err_out;
0157     }
0158 
0159     if (current_device == -1) {
0160         z2_count = 0;
0161         chip_count = 0;
0162         list_count = 0;
0163         z2ram_size = 0;
0164 
0165         /* Use a specific list entry. */
0166         if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
0167             int index = device - Z2MINOR_MEMLIST1 + 1;
0168             unsigned long size, paddr, vaddr;
0169 
0170             if (index >= m68k_realnum_memory) {
0171                 printk(KERN_ERR DEVICE_NAME
0172                        ": no such entry in z2ram_map\n");
0173                 goto err_out;
0174             }
0175 
0176             paddr = m68k_memory[index].addr;
0177             size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE - 1);
0178 
0179 #ifdef __powerpc__
0180             /* FIXME: ioremap doesn't build correct memory tables. */
0181             {
0182                 vfree(vmalloc(size));
0183             }
0184 
0185             vaddr = (unsigned long)ioremap_wt(paddr, size);
0186 
0187 #else
0188             vaddr =
0189                 (unsigned long)z_remap_nocache_nonser(paddr, size);
0190 #endif
0191             z2ram_map =
0192                 kmalloc_array(size / Z2RAM_CHUNKSIZE,
0193                       sizeof(z2ram_map[0]), GFP_KERNEL);
0194             if (z2ram_map == NULL) {
0195                 printk(KERN_ERR DEVICE_NAME
0196                        ": cannot get mem for z2ram_map\n");
0197                 goto err_out;
0198             }
0199 
0200             while (size) {
0201                 z2ram_map[z2ram_size++] = vaddr;
0202                 size -= Z2RAM_CHUNKSIZE;
0203                 vaddr += Z2RAM_CHUNKSIZE;
0204                 list_count++;
0205             }
0206 
0207             if (z2ram_size != 0)
0208                 printk(KERN_INFO DEVICE_NAME
0209                        ": using %iK List Entry %d Memory\n",
0210                        list_count * Z2RAM_CHUNK1024, index);
0211         } else
0212             switch (device) {
0213             case Z2MINOR_COMBINED:
0214 
0215                 z2ram_map =
0216                     kmalloc(max_z2_map + max_chip_map,
0217                         GFP_KERNEL);
0218                 if (z2ram_map == NULL) {
0219                     printk(KERN_ERR DEVICE_NAME
0220                            ": cannot get mem for z2ram_map\n");
0221                     goto err_out;
0222                 }
0223 
0224                 get_z2ram();
0225                 get_chipram();
0226 
0227                 if (z2ram_size != 0)
0228                     printk(KERN_INFO DEVICE_NAME
0229                            ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
0230                            z2_count * Z2RAM_CHUNK1024,
0231                            chip_count * Z2RAM_CHUNK1024,
0232                            (z2_count +
0233                         chip_count) * Z2RAM_CHUNK1024);
0234 
0235                 break;
0236 
0237             case Z2MINOR_Z2ONLY:
0238                 z2ram_map = kmalloc(max_z2_map, GFP_KERNEL);
0239                 if (!z2ram_map)
0240                     goto err_out;
0241 
0242                 get_z2ram();
0243 
0244                 if (z2ram_size != 0)
0245                     printk(KERN_INFO DEVICE_NAME
0246                            ": using %iK of Zorro II RAM\n",
0247                            z2_count * Z2RAM_CHUNK1024);
0248 
0249                 break;
0250 
0251             case Z2MINOR_CHIPONLY:
0252                 z2ram_map = kmalloc(max_chip_map, GFP_KERNEL);
0253                 if (!z2ram_map)
0254                     goto err_out;
0255 
0256                 get_chipram();
0257 
0258                 if (z2ram_size != 0)
0259                     printk(KERN_INFO DEVICE_NAME
0260                            ": using %iK Chip RAM\n",
0261                            chip_count * Z2RAM_CHUNK1024);
0262 
0263                 break;
0264 
0265             default:
0266                 rc = -ENODEV;
0267                 goto err_out;
0268 
0269                 break;
0270             }
0271 
0272         if (z2ram_size == 0) {
0273             printk(KERN_NOTICE DEVICE_NAME
0274                    ": no unused ZII/Chip RAM found\n");
0275             goto err_out_kfree;
0276         }
0277 
0278         current_device = device;
0279         z2ram_size <<= Z2RAM_CHUNKSHIFT;
0280         set_capacity(z2ram_gendisk[device], z2ram_size >> 9);
0281     }
0282 
0283     mutex_unlock(&z2ram_mutex);
0284     return 0;
0285 
0286 err_out_kfree:
0287     kfree(z2ram_map);
0288 err_out:
0289     mutex_unlock(&z2ram_mutex);
0290     return rc;
0291 }
0292 
0293 static void z2_release(struct gendisk *disk, fmode_t mode)
0294 {
0295     mutex_lock(&z2ram_mutex);
0296     if (current_device == -1) {
0297         mutex_unlock(&z2ram_mutex);
0298         return;
0299     }
0300     mutex_unlock(&z2ram_mutex);
0301     /*
0302      * FIXME: unmap memory
0303      */
0304 }
0305 
0306 static const struct block_device_operations z2_fops = {
0307     .owner = THIS_MODULE,
0308     .open = z2_open,
0309     .release = z2_release,
0310 };
0311 
0312 static struct blk_mq_tag_set tag_set;
0313 
0314 static const struct blk_mq_ops z2_mq_ops = {
0315     .queue_rq = z2_queue_rq,
0316 };
0317 
0318 static int z2ram_register_disk(int minor)
0319 {
0320     struct gendisk *disk;
0321     int err;
0322 
0323     disk = blk_mq_alloc_disk(&tag_set, NULL);
0324     if (IS_ERR(disk))
0325         return PTR_ERR(disk);
0326 
0327     disk->major = Z2RAM_MAJOR;
0328     disk->first_minor = minor;
0329     disk->minors = 1;
0330     disk->flags |= GENHD_FL_NO_PART;
0331     disk->fops = &z2_fops;
0332     if (minor)
0333         sprintf(disk->disk_name, "z2ram%d", minor);
0334     else
0335         sprintf(disk->disk_name, "z2ram");
0336 
0337     z2ram_gendisk[minor] = disk;
0338     err = add_disk(disk);
0339     if (err)
0340         put_disk(disk);
0341     return err;
0342 }
0343 
0344 static int __init z2_init(void)
0345 {
0346     int ret, i;
0347 
0348     if (!MACH_IS_AMIGA)
0349         return -ENODEV;
0350 
0351     if (register_blkdev(Z2RAM_MAJOR, DEVICE_NAME))
0352         return -EBUSY;
0353 
0354     tag_set.ops = &z2_mq_ops;
0355     tag_set.nr_hw_queues = 1;
0356     tag_set.nr_maps = 1;
0357     tag_set.queue_depth = 16;
0358     tag_set.numa_node = NUMA_NO_NODE;
0359     tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
0360     ret = blk_mq_alloc_tag_set(&tag_set);
0361     if (ret)
0362         goto out_unregister_blkdev;
0363 
0364     for (i = 0; i < Z2MINOR_COUNT; i++) {
0365         ret = z2ram_register_disk(i);
0366         if (ret && i == 0)
0367             goto out_free_tagset;
0368     }
0369 
0370     return 0;
0371 
0372 out_free_tagset:
0373     blk_mq_free_tag_set(&tag_set);
0374 out_unregister_blkdev:
0375     unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
0376     return ret;
0377 }
0378 
0379 static void __exit z2_exit(void)
0380 {
0381     int i, j;
0382 
0383     unregister_blkdev(Z2RAM_MAJOR, DEVICE_NAME);
0384 
0385     for (i = 0; i < Z2MINOR_COUNT; i++) {
0386         del_gendisk(z2ram_gendisk[i]);
0387         put_disk(z2ram_gendisk[i]);
0388     }
0389     blk_mq_free_tag_set(&tag_set);
0390 
0391     if (current_device != -1) {
0392         i = 0;
0393 
0394         for (j = 0; j < z2_count; j++) {
0395             set_bit(i++, zorro_unused_z2ram);
0396         }
0397 
0398         for (j = 0; j < chip_count; j++) {
0399             if (z2ram_map[i]) {
0400                 amiga_chip_free((void *)z2ram_map[i++]);
0401             }
0402         }
0403 
0404         if (z2ram_map != NULL) {
0405             kfree(z2ram_map);
0406         }
0407     }
0408 
0409     return;
0410 }
0411 
0412 module_init(z2_init);
0413 module_exit(z2_exit);
0414 MODULE_LICENSE("GPL");