Remove all inclusions of <linux/config.h>
[pandora-kernel.git] / drivers / char / mspec.c
1 /*
2  * Copyright (C) 2001-2006 Silicon Graphics, Inc.  All rights
3  * reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 /*
11  * SN Platform Special Memory (mspec) Support
12  *
13  * This driver exports the SN special memory (mspec) facility to user
14  * processes.
15  * There are three types of memory made available thru this driver:
16  * fetchops, uncached and cached.
17  *
18  * Fetchops are atomic memory operations that are implemented in the
19  * memory controller on SGI SN hardware.
20  *
21  * Uncached are used for memory write combining feature of the ia64
22  * cpu.
23  *
24  * Cached are used for areas of memory that are used as cached addresses
25  * on our partition and used as uncached addresses from other partitions.
26  * Due to a design constraint of the SN2 Shub, you can not have processors
27  * on the same FSB perform both a cached and uncached reference to the
28  * same cache line.  These special memory cached regions prevent the
29  * kernel from ever dropping in a TLB entry and therefore prevent the
30  * processor from ever speculating a cache line from this page.
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/vmalloc.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
44 #include <linux/numa.h>
45 #include <asm/page.h>
46 #include <asm/system.h>
47 #include <asm/pgtable.h>
48 #include <asm/atomic.h>
49 #include <asm/tlbflush.h>
50 #include <asm/uncached.h>
51 #include <asm/sn/addrs.h>
52 #include <asm/sn/arch.h>
53 #include <asm/sn/mspec.h>
54 #include <asm/sn/sn_cpuid.h>
55 #include <asm/sn/io.h>
56 #include <asm/sn/bte.h>
57 #include <asm/sn/shubio.h>
58
59
60 #define FETCHOP_ID      "SGI Fetchop,"
61 #define CACHED_ID       "Cached,"
62 #define UNCACHED_ID     "Uncached"
63 #define REVISION        "4.0"
64 #define MSPEC_BASENAME  "mspec"
65
66 /*
67  * Page types allocated by the device.
68  */
69 enum {
70         MSPEC_FETCHOP = 1,
71         MSPEC_CACHED,
72         MSPEC_UNCACHED
73 };
74
75 static int is_sn2;
76
77 /*
78  * One of these structures is allocated when an mspec region is mmaped. The
79  * structure is pointed to by the vma->vm_private_data field in the vma struct.
80  * This structure is used to record the addresses of the mspec pages.
81  */
82 struct vma_data {
83         atomic_t refcnt;        /* Number of vmas sharing the data. */
84         spinlock_t lock;        /* Serialize access to the vma. */
85         int count;              /* Number of pages allocated. */
86         int type;               /* Type of pages allocated. */
87         unsigned long maddr[0]; /* Array of MSPEC addresses. */
88 };
89
90 /* used on shub2 to clear FOP cache in the HUB */
91 static unsigned long scratch_page[MAX_NUMNODES];
92 #define SH2_AMO_CACHE_ENTRIES   4
93
94 static inline int
95 mspec_zero_block(unsigned long addr, int len)
96 {
97         int status;
98
99         if (is_sn2) {
100                 if (is_shub2()) {
101                         int nid;
102                         void *p;
103                         int i;
104
105                         nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
106                         p = (void *)TO_AMO(scratch_page[nid]);
107
108                         for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
109                                 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
110                                 p += FETCHOP_VAR_SIZE;
111                         }
112                 }
113
114                 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
115                                   BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
116         } else {
117                 memset((char *) addr, 0, len);
118                 status = 0;
119         }
120         return status;
121 }
122
123 /*
124  * mspec_open
125  *
126  * Called when a device mapping is created by a means other than mmap
127  * (via fork, etc.).  Increments the reference count on the underlying
128  * mspec data so it is not freed prematurely.
129  */
130 static void
131 mspec_open(struct vm_area_struct *vma)
132 {
133         struct vma_data *vdata;
134
135         vdata = vma->vm_private_data;
136         atomic_inc(&vdata->refcnt);
137 }
138
139 /*
140  * mspec_close
141  *
142  * Called when unmapping a device mapping. Frees all mspec pages
143  * belonging to the vma.
144  */
145 static void
146 mspec_close(struct vm_area_struct *vma)
147 {
148         struct vma_data *vdata;
149         int i, pages, result, vdata_size;
150
151         vdata = vma->vm_private_data;
152         if (!atomic_dec_and_test(&vdata->refcnt))
153                 return;
154
155         pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
156         vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
157         for (i = 0; i < pages; i++) {
158                 if (vdata->maddr[i] == 0)
159                         continue;
160                 /*
161                  * Clear the page before sticking it back
162                  * into the pool.
163                  */
164                 result = mspec_zero_block(vdata->maddr[i], PAGE_SIZE);
165                 if (!result)
166                         uncached_free_page(vdata->maddr[i]);
167                 else
168                         printk(KERN_WARNING "mspec_close(): "
169                                "failed to zero page %i\n",
170                                result);
171         }
172
173         if (vdata_size <= PAGE_SIZE)
174                 kfree(vdata);
175         else
176                 vfree(vdata);
177 }
178
179
180 /*
181  * mspec_nopfn
182  *
183  * Creates a mspec page and maps it to user space.
184  */
185 static unsigned long
186 mspec_nopfn(struct vm_area_struct *vma, unsigned long address)
187 {
188         unsigned long paddr, maddr;
189         unsigned long pfn;
190         int index;
191         struct vma_data *vdata = vma->vm_private_data;
192
193         index = (address - vma->vm_start) >> PAGE_SHIFT;
194         maddr = (volatile unsigned long) vdata->maddr[index];
195         if (maddr == 0) {
196                 maddr = uncached_alloc_page(numa_node_id());
197                 if (maddr == 0)
198                         return NOPFN_OOM;
199
200                 spin_lock(&vdata->lock);
201                 if (vdata->maddr[index] == 0) {
202                         vdata->count++;
203                         vdata->maddr[index] = maddr;
204                 } else {
205                         uncached_free_page(maddr);
206                         maddr = vdata->maddr[index];
207                 }
208                 spin_unlock(&vdata->lock);
209         }
210
211         if (vdata->type == MSPEC_FETCHOP)
212                 paddr = TO_AMO(maddr);
213         else
214                 paddr = __pa(TO_CAC(maddr));
215
216         pfn = paddr >> PAGE_SHIFT;
217
218         return pfn;
219 }
220
221 static struct vm_operations_struct mspec_vm_ops = {
222         .open = mspec_open,
223         .close = mspec_close,
224         .nopfn = mspec_nopfn
225 };
226
227 /*
228  * mspec_mmap
229  *
230  * Called when mmaping the device.  Initializes the vma with a fault handler
231  * and private data structure necessary to allocate, track, and free the
232  * underlying pages.
233  */
234 static int
235 mspec_mmap(struct file *file, struct vm_area_struct *vma, int type)
236 {
237         struct vma_data *vdata;
238         int pages, vdata_size;
239
240         if (vma->vm_pgoff != 0)
241                 return -EINVAL;
242
243         if ((vma->vm_flags & VM_SHARED) == 0)
244                 return -EINVAL;
245
246         if ((vma->vm_flags & VM_WRITE) == 0)
247                 return -EPERM;
248
249         pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
250         vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
251         if (vdata_size <= PAGE_SIZE)
252                 vdata = kmalloc(vdata_size, GFP_KERNEL);
253         else
254                 vdata = vmalloc(vdata_size);
255         if (!vdata)
256                 return -ENOMEM;
257         memset(vdata, 0, vdata_size);
258
259         vdata->type = type;
260         spin_lock_init(&vdata->lock);
261         vdata->refcnt = ATOMIC_INIT(1);
262         vma->vm_private_data = vdata;
263
264         vma->vm_flags |= (VM_IO | VM_LOCKED | VM_RESERVED | VM_PFNMAP);
265         if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
266                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
267         vma->vm_ops = &mspec_vm_ops;
268
269         return 0;
270 }
271
272 static int
273 fetchop_mmap(struct file *file, struct vm_area_struct *vma)
274 {
275         return mspec_mmap(file, vma, MSPEC_FETCHOP);
276 }
277
278 static int
279 cached_mmap(struct file *file, struct vm_area_struct *vma)
280 {
281         return mspec_mmap(file, vma, MSPEC_CACHED);
282 }
283
284 static int
285 uncached_mmap(struct file *file, struct vm_area_struct *vma)
286 {
287         return mspec_mmap(file, vma, MSPEC_UNCACHED);
288 }
289
290 static struct file_operations fetchop_fops = {
291         .owner = THIS_MODULE,
292         .mmap = fetchop_mmap
293 };
294
295 static struct miscdevice fetchop_miscdev = {
296         .minor = MISC_DYNAMIC_MINOR,
297         .name = "sgi_fetchop",
298         .fops = &fetchop_fops
299 };
300
301 static struct file_operations cached_fops = {
302         .owner = THIS_MODULE,
303         .mmap = cached_mmap
304 };
305
306 static struct miscdevice cached_miscdev = {
307         .minor = MISC_DYNAMIC_MINOR,
308         .name = "mspec_cached",
309         .fops = &cached_fops
310 };
311
312 static struct file_operations uncached_fops = {
313         .owner = THIS_MODULE,
314         .mmap = uncached_mmap
315 };
316
317 static struct miscdevice uncached_miscdev = {
318         .minor = MISC_DYNAMIC_MINOR,
319         .name = "mspec_uncached",
320         .fops = &uncached_fops
321 };
322
323 /*
324  * mspec_init
325  *
326  * Called at boot time to initialize the mspec facility.
327  */
328 static int __init
329 mspec_init(void)
330 {
331         int ret;
332         int nid;
333
334         /*
335          * The fetchop device only works on SN2 hardware, uncached and cached
336          * memory drivers should both be valid on all ia64 hardware
337          */
338         if (ia64_platform_is("sn2")) {
339                 is_sn2 = 1;
340                 if (is_shub2()) {
341                         ret = -ENOMEM;
342                         for_each_online_node(nid) {
343                                 int actual_nid;
344                                 int nasid;
345                                 unsigned long phys;
346
347                                 scratch_page[nid] = uncached_alloc_page(nid);
348                                 if (scratch_page[nid] == 0)
349                                         goto free_scratch_pages;
350                                 phys = __pa(scratch_page[nid]);
351                                 nasid = get_node_number(phys);
352                                 actual_nid = nasid_to_cnodeid(nasid);
353                                 if (actual_nid != nid)
354                                         goto free_scratch_pages;
355                         }
356                 }
357
358                 ret = misc_register(&fetchop_miscdev);
359                 if (ret) {
360                         printk(KERN_ERR
361                                "%s: failed to register device %i\n",
362                                FETCHOP_ID, ret);
363                         goto free_scratch_pages;
364                 }
365         }
366         ret = misc_register(&cached_miscdev);
367         if (ret) {
368                 printk(KERN_ERR "%s: failed to register device %i\n",
369                        CACHED_ID, ret);
370                 if (is_sn2)
371                         misc_deregister(&fetchop_miscdev);
372                 goto free_scratch_pages;
373         }
374         ret = misc_register(&uncached_miscdev);
375         if (ret) {
376                 printk(KERN_ERR "%s: failed to register device %i\n",
377                        UNCACHED_ID, ret);
378                 misc_deregister(&cached_miscdev);
379                 if (is_sn2)
380                         misc_deregister(&fetchop_miscdev);
381                 goto free_scratch_pages;
382         }
383
384         printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
385                MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
386                CACHED_ID, UNCACHED_ID);
387
388         return 0;
389
390  free_scratch_pages:
391         for_each_node(nid) {
392                 if (scratch_page[nid] != 0)
393                         uncached_free_page(scratch_page[nid]);
394         }
395         return ret;
396 }
397
398 static void __exit
399 mspec_exit(void)
400 {
401         int nid;
402
403         misc_deregister(&uncached_miscdev);
404         misc_deregister(&cached_miscdev);
405         if (is_sn2) {
406                 misc_deregister(&fetchop_miscdev);
407
408                 for_each_node(nid) {
409                         if (scratch_page[nid] != 0)
410                                 uncached_free_page(scratch_page[nid]);
411                 }
412         }
413 }
414
415 module_init(mspec_init);
416 module_exit(mspec_exit);
417
418 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
419 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
420 MODULE_LICENSE("GPL");