Remove fs.h from mm.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / mspec.c
blobc08a4152ee8fc2f5df6032221d519beafb4837f0
1 /*
2 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
3 * reserved.
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 */
11 * SN Platform Special Memory (mspec) Support
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.
18 * Fetchops are atomic memory operations that are implemented in the
19 * memory controller on SGI SN hardware.
21 * Uncached are used for memory write combining feature of the ia64
22 * cpu.
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.
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 <asm/page.h>
47 #include <asm/system.h>
48 #include <asm/pgtable.h>
49 #include <asm/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>
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"
68 * Page types allocated by the device.
70 enum {
71 MSPEC_FETCHOP = 1,
72 MSPEC_CACHED,
73 MSPEC_UNCACHED
76 #ifdef CONFIG_SGI_SN
77 static int is_sn2;
78 #else
79 #define is_sn2 0
80 #endif
83 * One of these structures is allocated when an mspec region is mmaped. The
84 * structure is pointed to by the vma->vm_private_data field in the vma struct.
85 * This structure is used to record the addresses of the mspec pages.
87 struct vma_data {
88 atomic_t refcnt; /* Number of vmas sharing the data. */
89 spinlock_t lock; /* Serialize access to the vma. */
90 int count; /* Number of pages allocated. */
91 int type; /* Type of pages allocated. */
92 unsigned long maddr[0]; /* Array of MSPEC addresses. */
95 /* used on shub2 to clear FOP cache in the HUB */
96 static unsigned long scratch_page[MAX_NUMNODES];
97 #define SH2_AMO_CACHE_ENTRIES 4
99 static inline int
100 mspec_zero_block(unsigned long addr, int len)
102 int status;
104 if (is_sn2) {
105 if (is_shub2()) {
106 int nid;
107 void *p;
108 int i;
110 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
111 p = (void *)TO_AMO(scratch_page[nid]);
113 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
114 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
115 p += FETCHOP_VAR_SIZE;
119 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
120 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
121 } else {
122 memset((char *) addr, 0, len);
123 status = 0;
125 return status;
129 * mspec_open
131 * Called when a device mapping is created by a means other than mmap
132 * (via fork, etc.). Increments the reference count on the underlying
133 * mspec data so it is not freed prematurely.
135 static void
136 mspec_open(struct vm_area_struct *vma)
138 struct vma_data *vdata;
140 vdata = vma->vm_private_data;
141 atomic_inc(&vdata->refcnt);
145 * mspec_close
147 * Called when unmapping a device mapping. Frees all mspec pages
148 * belonging to the vma.
150 static void
151 mspec_close(struct vm_area_struct *vma)
153 struct vma_data *vdata;
154 int i, pages, result, vdata_size;
156 vdata = vma->vm_private_data;
157 if (!atomic_dec_and_test(&vdata->refcnt))
158 return;
160 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
161 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
162 for (i = 0; i < pages; i++) {
163 if (vdata->maddr[i] == 0)
164 continue;
166 * Clear the page before sticking it back
167 * into the pool.
169 result = mspec_zero_block(vdata->maddr[i], PAGE_SIZE);
170 if (!result)
171 uncached_free_page(vdata->maddr[i]);
172 else
173 printk(KERN_WARNING "mspec_close(): "
174 "failed to zero page %i\n",
175 result);
178 if (vdata_size <= PAGE_SIZE)
179 kfree(vdata);
180 else
181 vfree(vdata);
186 * mspec_nopfn
188 * Creates a mspec page and maps it to user space.
190 static unsigned long
191 mspec_nopfn(struct vm_area_struct *vma, unsigned long address)
193 unsigned long paddr, maddr;
194 unsigned long pfn;
195 int index;
196 struct vma_data *vdata = vma->vm_private_data;
198 index = (address - vma->vm_start) >> PAGE_SHIFT;
199 maddr = (volatile unsigned long) vdata->maddr[index];
200 if (maddr == 0) {
201 maddr = uncached_alloc_page(numa_node_id());
202 if (maddr == 0)
203 return NOPFN_OOM;
205 spin_lock(&vdata->lock);
206 if (vdata->maddr[index] == 0) {
207 vdata->count++;
208 vdata->maddr[index] = maddr;
209 } else {
210 uncached_free_page(maddr);
211 maddr = vdata->maddr[index];
213 spin_unlock(&vdata->lock);
216 if (vdata->type == MSPEC_FETCHOP)
217 paddr = TO_AMO(maddr);
218 else
219 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
221 pfn = paddr >> PAGE_SHIFT;
223 return pfn;
226 static struct vm_operations_struct mspec_vm_ops = {
227 .open = mspec_open,
228 .close = mspec_close,
229 .nopfn = mspec_nopfn
233 * mspec_mmap
235 * Called when mmaping the device. Initializes the vma with a fault handler
236 * and private data structure necessary to allocate, track, and free the
237 * underlying pages.
239 static int
240 mspec_mmap(struct file *file, struct vm_area_struct *vma, int type)
242 struct vma_data *vdata;
243 int pages, vdata_size;
245 if (vma->vm_pgoff != 0)
246 return -EINVAL;
248 if ((vma->vm_flags & VM_SHARED) == 0)
249 return -EINVAL;
251 if ((vma->vm_flags & VM_WRITE) == 0)
252 return -EPERM;
254 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
255 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
256 if (vdata_size <= PAGE_SIZE)
257 vdata = kmalloc(vdata_size, GFP_KERNEL);
258 else
259 vdata = vmalloc(vdata_size);
260 if (!vdata)
261 return -ENOMEM;
262 memset(vdata, 0, vdata_size);
264 vdata->type = type;
265 spin_lock_init(&vdata->lock);
266 vdata->refcnt = ATOMIC_INIT(1);
267 vma->vm_private_data = vdata;
269 vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP);
270 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
271 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
272 vma->vm_ops = &mspec_vm_ops;
274 return 0;
277 static int
278 fetchop_mmap(struct file *file, struct vm_area_struct *vma)
280 return mspec_mmap(file, vma, MSPEC_FETCHOP);
283 static int
284 cached_mmap(struct file *file, struct vm_area_struct *vma)
286 return mspec_mmap(file, vma, MSPEC_CACHED);
289 static int
290 uncached_mmap(struct file *file, struct vm_area_struct *vma)
292 return mspec_mmap(file, vma, MSPEC_UNCACHED);
295 static const struct file_operations fetchop_fops = {
296 .owner = THIS_MODULE,
297 .mmap = fetchop_mmap
300 static struct miscdevice fetchop_miscdev = {
301 .minor = MISC_DYNAMIC_MINOR,
302 .name = "sgi_fetchop",
303 .fops = &fetchop_fops
306 static const struct file_operations cached_fops = {
307 .owner = THIS_MODULE,
308 .mmap = cached_mmap
311 static struct miscdevice cached_miscdev = {
312 .minor = MISC_DYNAMIC_MINOR,
313 .name = "mspec_cached",
314 .fops = &cached_fops
317 static const struct file_operations uncached_fops = {
318 .owner = THIS_MODULE,
319 .mmap = uncached_mmap
322 static struct miscdevice uncached_miscdev = {
323 .minor = MISC_DYNAMIC_MINOR,
324 .name = "mspec_uncached",
325 .fops = &uncached_fops
329 * mspec_init
331 * Called at boot time to initialize the mspec facility.
333 static int __init
334 mspec_init(void)
336 int ret;
337 int nid;
340 * The fetchop device only works on SN2 hardware, uncached and cached
341 * memory drivers should both be valid on all ia64 hardware
343 #ifdef CONFIG_SGI_SN
344 if (ia64_platform_is("sn2")) {
345 is_sn2 = 1;
346 if (is_shub2()) {
347 ret = -ENOMEM;
348 for_each_online_node(nid) {
349 int actual_nid;
350 int nasid;
351 unsigned long phys;
353 scratch_page[nid] = uncached_alloc_page(nid);
354 if (scratch_page[nid] == 0)
355 goto free_scratch_pages;
356 phys = __pa(scratch_page[nid]);
357 nasid = get_node_number(phys);
358 actual_nid = nasid_to_cnodeid(nasid);
359 if (actual_nid != nid)
360 goto free_scratch_pages;
364 ret = misc_register(&fetchop_miscdev);
365 if (ret) {
366 printk(KERN_ERR
367 "%s: failed to register device %i\n",
368 FETCHOP_ID, ret);
369 goto free_scratch_pages;
372 #endif
373 ret = misc_register(&cached_miscdev);
374 if (ret) {
375 printk(KERN_ERR "%s: failed to register device %i\n",
376 CACHED_ID, ret);
377 if (is_sn2)
378 misc_deregister(&fetchop_miscdev);
379 goto free_scratch_pages;
381 ret = misc_register(&uncached_miscdev);
382 if (ret) {
383 printk(KERN_ERR "%s: failed to register device %i\n",
384 UNCACHED_ID, ret);
385 misc_deregister(&cached_miscdev);
386 if (is_sn2)
387 misc_deregister(&fetchop_miscdev);
388 goto free_scratch_pages;
391 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
392 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
393 CACHED_ID, UNCACHED_ID);
395 return 0;
397 free_scratch_pages:
398 for_each_node(nid) {
399 if (scratch_page[nid] != 0)
400 uncached_free_page(scratch_page[nid]);
402 return ret;
405 static void __exit
406 mspec_exit(void)
408 int nid;
410 misc_deregister(&uncached_miscdev);
411 misc_deregister(&cached_miscdev);
412 if (is_sn2) {
413 misc_deregister(&fetchop_miscdev);
415 for_each_node(nid) {
416 if (scratch_page[nid] != 0)
417 uncached_free_page(scratch_page[nid]);
422 module_init(mspec_init);
423 module_exit(mspec_exit);
425 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
426 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
427 MODULE_LICENSE("GPL");