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#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38#include <linux/miscdevice.h>
39#include <linux/spinlock.h>
40#include <linux/mm.h>
41#include <linux/fs.h>
42#include <linux/vmalloc.h>
43#include <linux/string.h>
44#include <linux/slab.h>
45#include <linux/numa.h>
46#include <linux/refcount.h>
47#include <asm/page.h>
48#include <asm/pgtable.h>
49#include <linux/atomic.h>
50#include <asm/tlbflush.h>
51#include <asm/uncached.h>
52#include <asm/sn/addrs.h>
53#include <asm/sn/arch.h>
54#include <asm/sn/mspec.h>
55#include <asm/sn/sn_cpuid.h>
56#include <asm/sn/io.h>
57#include <asm/sn/bte.h>
58#include <asm/sn/shubio.h>
59
60
61#define FETCHOP_ID "SGI Fetchop,"
62#define CACHED_ID "Cached,"
63#define UNCACHED_ID "Uncached"
64#define REVISION "4.0"
65#define MSPEC_BASENAME "mspec"
66
67
68
69
70enum mspec_page_type {
71 MSPEC_FETCHOP = 1,
72 MSPEC_CACHED,
73 MSPEC_UNCACHED
74};
75
76#ifdef CONFIG_SGI_SN
77static int is_sn2;
78#else
79#define is_sn2 0
80#endif
81
82
83
84
85
86
87
88
89
90
91
92struct vma_data {
93 refcount_t refcnt;
94 spinlock_t lock;
95 int count;
96 enum mspec_page_type type;
97 unsigned long vm_start;
98 unsigned long vm_end;
99 unsigned long maddr[0];
100};
101
102
103static unsigned long scratch_page[MAX_NUMNODES];
104#define SH2_AMO_CACHE_ENTRIES 4
105
106static inline int
107mspec_zero_block(unsigned long addr, int len)
108{
109 int status;
110
111 if (is_sn2) {
112 if (is_shub2()) {
113 int nid;
114 void *p;
115 int i;
116
117 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
118 p = (void *)TO_AMO(scratch_page[nid]);
119
120 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
121 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
122 p += FETCHOP_VAR_SIZE;
123 }
124 }
125
126 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
127 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
128 } else {
129 memset((char *) addr, 0, len);
130 status = 0;
131 }
132 return status;
133}
134
135
136
137
138
139
140
141
142static void
143mspec_open(struct vm_area_struct *vma)
144{
145 struct vma_data *vdata;
146
147 vdata = vma->vm_private_data;
148 refcount_inc(&vdata->refcnt);
149}
150
151
152
153
154
155
156
157static void
158mspec_close(struct vm_area_struct *vma)
159{
160 struct vma_data *vdata;
161 int index, last_index;
162 unsigned long my_page;
163
164 vdata = vma->vm_private_data;
165
166 if (!refcount_dec_and_test(&vdata->refcnt))
167 return;
168
169 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
170 for (index = 0; index < last_index; index++) {
171 if (vdata->maddr[index] == 0)
172 continue;
173
174
175
176
177 my_page = vdata->maddr[index];
178 vdata->maddr[index] = 0;
179 if (!mspec_zero_block(my_page, PAGE_SIZE))
180 uncached_free_page(my_page, 1);
181 else
182 printk(KERN_WARNING "mspec_close(): "
183 "failed to zero page %ld\n", my_page);
184 }
185
186 kvfree(vdata);
187}
188
189
190
191
192
193
194static vm_fault_t
195mspec_fault(struct vm_fault *vmf)
196{
197 unsigned long paddr, maddr;
198 unsigned long pfn;
199 pgoff_t index = vmf->pgoff;
200 struct vma_data *vdata = vmf->vma->vm_private_data;
201
202 maddr = (volatile unsigned long) vdata->maddr[index];
203 if (maddr == 0) {
204 maddr = uncached_alloc_page(numa_node_id(), 1);
205 if (maddr == 0)
206 return VM_FAULT_OOM;
207
208 spin_lock(&vdata->lock);
209 if (vdata->maddr[index] == 0) {
210 vdata->count++;
211 vdata->maddr[index] = maddr;
212 } else {
213 uncached_free_page(maddr, 1);
214 maddr = vdata->maddr[index];
215 }
216 spin_unlock(&vdata->lock);
217 }
218
219 if (vdata->type == MSPEC_FETCHOP)
220 paddr = TO_AMO(maddr);
221 else
222 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
223
224 pfn = paddr >> PAGE_SHIFT;
225
226 return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
227}
228
229static const struct vm_operations_struct mspec_vm_ops = {
230 .open = mspec_open,
231 .close = mspec_close,
232 .fault = mspec_fault,
233};
234
235
236
237
238
239
240
241
242static int
243mspec_mmap(struct file *file, struct vm_area_struct *vma,
244 enum mspec_page_type type)
245{
246 struct vma_data *vdata;
247 int pages, vdata_size;
248
249 if (vma->vm_pgoff != 0)
250 return -EINVAL;
251
252 if ((vma->vm_flags & VM_SHARED) == 0)
253 return -EINVAL;
254
255 if ((vma->vm_flags & VM_WRITE) == 0)
256 return -EPERM;
257
258 pages = vma_pages(vma);
259 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
260 if (vdata_size <= PAGE_SIZE)
261 vdata = kzalloc(vdata_size, GFP_KERNEL);
262 else
263 vdata = vzalloc(vdata_size);
264 if (!vdata)
265 return -ENOMEM;
266
267 vdata->vm_start = vma->vm_start;
268 vdata->vm_end = vma->vm_end;
269 vdata->type = type;
270 spin_lock_init(&vdata->lock);
271 refcount_set(&vdata->refcnt, 1);
272 vma->vm_private_data = vdata;
273
274 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
275 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
276 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
277 vma->vm_ops = &mspec_vm_ops;
278
279 return 0;
280}
281
282static int
283fetchop_mmap(struct file *file, struct vm_area_struct *vma)
284{
285 return mspec_mmap(file, vma, MSPEC_FETCHOP);
286}
287
288static int
289cached_mmap(struct file *file, struct vm_area_struct *vma)
290{
291 return mspec_mmap(file, vma, MSPEC_CACHED);
292}
293
294static int
295uncached_mmap(struct file *file, struct vm_area_struct *vma)
296{
297 return mspec_mmap(file, vma, MSPEC_UNCACHED);
298}
299
300static const struct file_operations fetchop_fops = {
301 .owner = THIS_MODULE,
302 .mmap = fetchop_mmap,
303 .llseek = noop_llseek,
304};
305
306static struct miscdevice fetchop_miscdev = {
307 .minor = MISC_DYNAMIC_MINOR,
308 .name = "sgi_fetchop",
309 .fops = &fetchop_fops
310};
311
312static const struct file_operations cached_fops = {
313 .owner = THIS_MODULE,
314 .mmap = cached_mmap,
315 .llseek = noop_llseek,
316};
317
318static struct miscdevice cached_miscdev = {
319 .minor = MISC_DYNAMIC_MINOR,
320 .name = "mspec_cached",
321 .fops = &cached_fops
322};
323
324static const struct file_operations uncached_fops = {
325 .owner = THIS_MODULE,
326 .mmap = uncached_mmap,
327 .llseek = noop_llseek,
328};
329
330static struct miscdevice uncached_miscdev = {
331 .minor = MISC_DYNAMIC_MINOR,
332 .name = "mspec_uncached",
333 .fops = &uncached_fops
334};
335
336
337
338
339
340
341static int __init
342mspec_init(void)
343{
344 int ret;
345 int nid;
346
347
348
349
350
351#ifdef CONFIG_SGI_SN
352 if (ia64_platform_is("sn2")) {
353 is_sn2 = 1;
354 if (is_shub2()) {
355 ret = -ENOMEM;
356 for_each_node_state(nid, N_ONLINE) {
357 int actual_nid;
358 int nasid;
359 unsigned long phys;
360
361 scratch_page[nid] = uncached_alloc_page(nid, 1);
362 if (scratch_page[nid] == 0)
363 goto free_scratch_pages;
364 phys = __pa(scratch_page[nid]);
365 nasid = get_node_number(phys);
366 actual_nid = nasid_to_cnodeid(nasid);
367 if (actual_nid != nid)
368 goto free_scratch_pages;
369 }
370 }
371
372 ret = misc_register(&fetchop_miscdev);
373 if (ret) {
374 printk(KERN_ERR
375 "%s: failed to register device %i\n",
376 FETCHOP_ID, ret);
377 goto free_scratch_pages;
378 }
379 }
380#endif
381 ret = misc_register(&cached_miscdev);
382 if (ret) {
383 printk(KERN_ERR "%s: failed to register device %i\n",
384 CACHED_ID, ret);
385 if (is_sn2)
386 misc_deregister(&fetchop_miscdev);
387 goto free_scratch_pages;
388 }
389 ret = misc_register(&uncached_miscdev);
390 if (ret) {
391 printk(KERN_ERR "%s: failed to register device %i\n",
392 UNCACHED_ID, ret);
393 misc_deregister(&cached_miscdev);
394 if (is_sn2)
395 misc_deregister(&fetchop_miscdev);
396 goto free_scratch_pages;
397 }
398
399 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
400 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
401 CACHED_ID, UNCACHED_ID);
402
403 return 0;
404
405 free_scratch_pages:
406 for_each_node(nid) {
407 if (scratch_page[nid] != 0)
408 uncached_free_page(scratch_page[nid], 1);
409 }
410 return ret;
411}
412
413static void __exit
414mspec_exit(void)
415{
416 int nid;
417
418 misc_deregister(&uncached_miscdev);
419 misc_deregister(&cached_miscdev);
420 if (is_sn2) {
421 misc_deregister(&fetchop_miscdev);
422
423 for_each_node(nid) {
424 if (scratch_page[nid] != 0)
425 uncached_free_page(scratch_page[nid], 1);
426 }
427 }
428}
429
430module_init(mspec_init);
431module_exit(mspec_exit);
432
433MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
434MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
435MODULE_LICENSE("GPL");
436