xref: /openbmc/linux/arch/um/drivers/mmapper_kern.c (revision 0352f880)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch/um/drivers/mmapper_kern.c
4  *
5  * BRIEF MODULE DESCRIPTION
6  *
7  * Copyright (C) 2000 RidgeRun, Inc.
8  * Author: RidgeRun, Inc.
9  *         Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
10  *
11  */
12 
13 #include <linux/stddef.h>
14 #include <linux/types.h>
15 #include <linux/fs.h>
16 #include <linux/init.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 
21 #include <linux/uaccess.h>
22 #include <mem_user.h>
23 
24 /* These are set in mmapper_init, which is called at boot time */
25 static unsigned long mmapper_size;
26 static unsigned long p_buf;
27 static char *v_buf;
28 
29 static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
30 			    loff_t *ppos)
31 {
32 	return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
33 }
34 
35 static ssize_t mmapper_write(struct file *file, const char __user *buf,
36 			     size_t count, loff_t *ppos)
37 {
38 	if (*ppos > mmapper_size)
39 		return -EINVAL;
40 
41 	return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count);
42 }
43 
44 static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
45 {
46 	return -ENOIOCTLCMD;
47 }
48 
49 static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
50 {
51 	int ret = -EINVAL;
52 	int size;
53 
54 	if (vma->vm_pgoff != 0)
55 		goto out;
56 
57 	size = vma->vm_end - vma->vm_start;
58 	if (size > mmapper_size)
59 		return -EFAULT;
60 
61 	/*
62 	 * XXX A comment above remap_pfn_range says it should only be
63 	 * called when the mm semaphore is held
64 	 */
65 	if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
66 			    vma->vm_page_prot))
67 		goto out;
68 	ret = 0;
69 out:
70 	return ret;
71 }
72 
73 static int mmapper_open(struct inode *inode, struct file *file)
74 {
75 	return 0;
76 }
77 
78 static int mmapper_release(struct inode *inode, struct file *file)
79 {
80 	return 0;
81 }
82 
83 static const struct file_operations mmapper_fops = {
84 	.owner		= THIS_MODULE,
85 	.read		= mmapper_read,
86 	.write		= mmapper_write,
87 	.unlocked_ioctl	= mmapper_ioctl,
88 	.mmap		= mmapper_mmap,
89 	.open		= mmapper_open,
90 	.release	= mmapper_release,
91 	.llseek		= default_llseek,
92 };
93 
94 /*
95  * No locking needed - only used (and modified) by below initcall and exitcall.
96  */
97 static struct miscdevice mmapper_dev = {
98 	.minor		= MISC_DYNAMIC_MINOR,
99 	.name		= "mmapper",
100 	.fops		= &mmapper_fops
101 };
102 
103 static int __init mmapper_init(void)
104 {
105 	int err;
106 
107 	printk(KERN_INFO "Mapper v0.1\n");
108 
109 	v_buf = (char *) find_iomem("mmapper", &mmapper_size);
110 	if (mmapper_size == 0) {
111 		printk(KERN_ERR "mmapper_init - find_iomem failed\n");
112 		return -ENODEV;
113 	}
114 	p_buf = __pa(v_buf);
115 
116 	err = misc_register(&mmapper_dev);
117 	if (err) {
118 		printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
119 		       err);
120 		return err;
121 	}
122 	return 0;
123 }
124 
125 static void __exit mmapper_exit(void)
126 {
127 	misc_deregister(&mmapper_dev);
128 }
129 
130 module_init(mmapper_init);
131 module_exit(mmapper_exit);
132 
133 MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
134 MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
135 MODULE_LICENSE("GPL");
136