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#include <linux/types.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/errno.h>
32#include <linux/miscdevice.h>
33#include <linux/spinlock.h>
34#include <linux/mm.h>
35#include <linux/fs.h>
36#include <linux/vmalloc.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/numa.h>
40#include <linux/refcount.h>
41#include <asm/page.h>
42#include <linux/atomic.h>
43#include <asm/tlbflush.h>
44#include <asm/uncached.h>
45
46
47#define CACHED_ID "Cached,"
48#define UNCACHED_ID "Uncached"
49#define REVISION "4.0"
50#define MSPEC_BASENAME "mspec"
51
52
53
54
55enum mspec_page_type {
56 MSPEC_CACHED = 2,
57 MSPEC_UNCACHED
58};
59
60
61
62
63
64
65
66
67
68
69
70struct vma_data {
71 refcount_t refcnt;
72 spinlock_t lock;
73 int count;
74 enum mspec_page_type type;
75 unsigned long vm_start;
76 unsigned long vm_end;
77 unsigned long maddr[];
78};
79
80
81
82
83
84
85
86
87static void
88mspec_open(struct vm_area_struct *vma)
89{
90 struct vma_data *vdata;
91
92 vdata = vma->vm_private_data;
93 refcount_inc(&vdata->refcnt);
94}
95
96
97
98
99
100
101
102static void
103mspec_close(struct vm_area_struct *vma)
104{
105 struct vma_data *vdata;
106 int index, last_index;
107 unsigned long my_page;
108
109 vdata = vma->vm_private_data;
110
111 if (!refcount_dec_and_test(&vdata->refcnt))
112 return;
113
114 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
115 for (index = 0; index < last_index; index++) {
116 if (vdata->maddr[index] == 0)
117 continue;
118
119
120
121
122 my_page = vdata->maddr[index];
123 vdata->maddr[index] = 0;
124 memset((char *)my_page, 0, PAGE_SIZE);
125 uncached_free_page(my_page, 1);
126 }
127
128 kvfree(vdata);
129}
130
131
132
133
134
135
136static vm_fault_t
137mspec_fault(struct vm_fault *vmf)
138{
139 unsigned long paddr, maddr;
140 unsigned long pfn;
141 pgoff_t index = vmf->pgoff;
142 struct vma_data *vdata = vmf->vma->vm_private_data;
143
144 maddr = (volatile unsigned long) vdata->maddr[index];
145 if (maddr == 0) {
146 maddr = uncached_alloc_page(numa_node_id(), 1);
147 if (maddr == 0)
148 return VM_FAULT_OOM;
149
150 spin_lock(&vdata->lock);
151 if (vdata->maddr[index] == 0) {
152 vdata->count++;
153 vdata->maddr[index] = maddr;
154 } else {
155 uncached_free_page(maddr, 1);
156 maddr = vdata->maddr[index];
157 }
158 spin_unlock(&vdata->lock);
159 }
160
161 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
162 pfn = paddr >> PAGE_SHIFT;
163
164 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
165}
166
167static const struct vm_operations_struct mspec_vm_ops = {
168 .open = mspec_open,
169 .close = mspec_close,
170 .fault = mspec_fault,
171};
172
173
174
175
176
177
178
179
180static int
181mspec_mmap(struct file *file, struct vm_area_struct *vma,
182 enum mspec_page_type type)
183{
184 struct vma_data *vdata;
185 int pages, vdata_size;
186
187 if (vma->vm_pgoff != 0)
188 return -EINVAL;
189
190 if ((vma->vm_flags & VM_SHARED) == 0)
191 return -EINVAL;
192
193 if ((vma->vm_flags & VM_WRITE) == 0)
194 return -EPERM;
195
196 pages = vma_pages(vma);
197 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
198 if (vdata_size <= PAGE_SIZE)
199 vdata = kzalloc(vdata_size, GFP_KERNEL);
200 else
201 vdata = vzalloc(vdata_size);
202 if (!vdata)
203 return -ENOMEM;
204
205 vdata->vm_start = vma->vm_start;
206 vdata->vm_end = vma->vm_end;
207 vdata->type = type;
208 spin_lock_init(&vdata->lock);
209 refcount_set(&vdata->refcnt, 1);
210 vma->vm_private_data = vdata;
211
212 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
213 if (vdata->type == MSPEC_UNCACHED)
214 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
215 vma->vm_ops = &mspec_vm_ops;
216
217 return 0;
218}
219
220static int
221cached_mmap(struct file *file, struct vm_area_struct *vma)
222{
223 return mspec_mmap(file, vma, MSPEC_CACHED);
224}
225
226static int
227uncached_mmap(struct file *file, struct vm_area_struct *vma)
228{
229 return mspec_mmap(file, vma, MSPEC_UNCACHED);
230}
231
232static const struct file_operations cached_fops = {
233 .owner = THIS_MODULE,
234 .mmap = cached_mmap,
235 .llseek = noop_llseek,
236};
237
238static struct miscdevice cached_miscdev = {
239 .minor = MISC_DYNAMIC_MINOR,
240 .name = "mspec_cached",
241 .fops = &cached_fops
242};
243
244static const struct file_operations uncached_fops = {
245 .owner = THIS_MODULE,
246 .mmap = uncached_mmap,
247 .llseek = noop_llseek,
248};
249
250static struct miscdevice uncached_miscdev = {
251 .minor = MISC_DYNAMIC_MINOR,
252 .name = "mspec_uncached",
253 .fops = &uncached_fops
254};
255
256
257
258
259
260
261static int __init
262mspec_init(void)
263{
264 int ret;
265
266 ret = misc_register(&cached_miscdev);
267 if (ret) {
268 printk(KERN_ERR "%s: failed to register device %i\n",
269 CACHED_ID, ret);
270 return ret;
271 }
272 ret = misc_register(&uncached_miscdev);
273 if (ret) {
274 printk(KERN_ERR "%s: failed to register device %i\n",
275 UNCACHED_ID, ret);
276 misc_deregister(&cached_miscdev);
277 return ret;
278 }
279
280 printk(KERN_INFO "%s %s initialized devices: %s %s\n",
281 MSPEC_BASENAME, REVISION, CACHED_ID, UNCACHED_ID);
282
283 return 0;
284}
285
286static void __exit
287mspec_exit(void)
288{
289 misc_deregister(&uncached_miscdev);
290 misc_deregister(&cached_miscdev);
291}
292
293module_init(mspec_init);
294module_exit(mspec_exit);
295
296MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
297MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
298MODULE_LICENSE("GPL");
299