2 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
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.
11 * SN Platform Special Memory (mspec) Support
13 * This driver exports the SN special memory (mspec) facility to user
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
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>
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>
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>
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 mspec_page_type
{
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.
86 * This structure is shared by all vma's that are split off from the
87 * original vma when split_vma()'s are done.
89 * The refcnt is incremented atomically because mm->mmap_sem does not
90 * protect in fork case where multiple tasks share the vma_data.
93 refcount_t refcnt
; /* Number of vmas sharing the data. */
94 spinlock_t lock
; /* Serialize access to this structure. */
95 int count
; /* Number of pages allocated. */
96 enum mspec_page_type type
; /* Type of pages allocated. */
97 unsigned long vm_start
; /* Original (unsplit) base. */
98 unsigned long vm_end
; /* Original (unsplit) end. */
99 unsigned long maddr
[0]; /* Array of MSPEC addresses. */
102 /* used on shub2 to clear FOP cache in the HUB */
103 static unsigned long scratch_page
[MAX_NUMNODES
];
104 #define SH2_AMO_CACHE_ENTRIES 4
107 mspec_zero_block(unsigned long addr
, int len
)
117 nid
= nasid_to_cnodeid(get_node_number(__pa(addr
)));
118 p
= (void *)TO_AMO(scratch_page
[nid
]);
120 for (i
=0; i
< SH2_AMO_CACHE_ENTRIES
; i
++) {
121 FETCHOP_LOAD_OP(p
, FETCHOP_LOAD
);
122 p
+= FETCHOP_VAR_SIZE
;
126 status
= bte_copy(0, addr
& ~__IA64_UNCACHED_OFFSET
, len
,
127 BTE_WACQUIRE
| BTE_ZERO_FILL
, NULL
);
129 memset((char *) addr
, 0, len
);
138 * Called when a device mapping is created by a means other than mmap
139 * (via fork, munmap, etc.). Increments the reference count on the
140 * underlying mspec data so it is not freed prematurely.
143 mspec_open(struct vm_area_struct
*vma
)
145 struct vma_data
*vdata
;
147 vdata
= vma
->vm_private_data
;
148 refcount_inc(&vdata
->refcnt
);
154 * Called when unmapping a device mapping. Frees all mspec pages
155 * belonging to all the vma's sharing this vma_data structure.
158 mspec_close(struct vm_area_struct
*vma
)
160 struct vma_data
*vdata
;
161 int index
, last_index
;
162 unsigned long my_page
;
164 vdata
= vma
->vm_private_data
;
166 if (!refcount_dec_and_test(&vdata
->refcnt
))
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)
174 * Clear the page before sticking it back
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);
182 printk(KERN_WARNING
"mspec_close(): "
183 "failed to zero page %ld\n", my_page
);
192 * Creates a mspec page and maps it to user space.
195 mspec_fault(struct vm_fault
*vmf
)
197 unsigned long paddr
, maddr
;
199 pgoff_t index
= vmf
->pgoff
;
200 struct vma_data
*vdata
= vmf
->vma
->vm_private_data
;
202 maddr
= (volatile unsigned long) vdata
->maddr
[index
];
204 maddr
= uncached_alloc_page(numa_node_id(), 1);
208 spin_lock(&vdata
->lock
);
209 if (vdata
->maddr
[index
] == 0) {
211 vdata
->maddr
[index
] = maddr
;
213 uncached_free_page(maddr
, 1);
214 maddr
= vdata
->maddr
[index
];
216 spin_unlock(&vdata
->lock
);
219 if (vdata
->type
== MSPEC_FETCHOP
)
220 paddr
= TO_AMO(maddr
);
222 paddr
= maddr
& ~__IA64_UNCACHED_OFFSET
;
224 pfn
= paddr
>> PAGE_SHIFT
;
226 return vmf_insert_pfn(vmf
->vma
, vmf
->address
, pfn
);
229 static const struct vm_operations_struct mspec_vm_ops
= {
231 .close
= mspec_close
,
232 .fault
= mspec_fault
,
238 * Called when mmapping the device. Initializes the vma with a fault handler
239 * and private data structure necessary to allocate, track, and free the
243 mspec_mmap(struct file
*file
, struct vm_area_struct
*vma
,
244 enum mspec_page_type type
)
246 struct vma_data
*vdata
;
247 int pages
, vdata_size
;
249 if (vma
->vm_pgoff
!= 0)
252 if ((vma
->vm_flags
& VM_SHARED
) == 0)
255 if ((vma
->vm_flags
& VM_WRITE
) == 0)
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
);
263 vdata
= vzalloc(vdata_size
);
267 vdata
->vm_start
= vma
->vm_start
;
268 vdata
->vm_end
= vma
->vm_end
;
270 spin_lock_init(&vdata
->lock
);
271 refcount_set(&vdata
->refcnt
, 1);
272 vma
->vm_private_data
= vdata
;
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
;
283 fetchop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
285 return mspec_mmap(file
, vma
, MSPEC_FETCHOP
);
289 cached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
291 return mspec_mmap(file
, vma
, MSPEC_CACHED
);
295 uncached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
297 return mspec_mmap(file
, vma
, MSPEC_UNCACHED
);
300 static const struct file_operations fetchop_fops
= {
301 .owner
= THIS_MODULE
,
302 .mmap
= fetchop_mmap
,
303 .llseek
= noop_llseek
,
306 static struct miscdevice fetchop_miscdev
= {
307 .minor
= MISC_DYNAMIC_MINOR
,
308 .name
= "sgi_fetchop",
309 .fops
= &fetchop_fops
312 static const struct file_operations cached_fops
= {
313 .owner
= THIS_MODULE
,
315 .llseek
= noop_llseek
,
318 static struct miscdevice cached_miscdev
= {
319 .minor
= MISC_DYNAMIC_MINOR
,
320 .name
= "mspec_cached",
324 static const struct file_operations uncached_fops
= {
325 .owner
= THIS_MODULE
,
326 .mmap
= uncached_mmap
,
327 .llseek
= noop_llseek
,
330 static struct miscdevice uncached_miscdev
= {
331 .minor
= MISC_DYNAMIC_MINOR
,
332 .name
= "mspec_uncached",
333 .fops
= &uncached_fops
339 * Called at boot time to initialize the mspec facility.
348 * The fetchop device only works on SN2 hardware, uncached and cached
349 * memory drivers should both be valid on all ia64 hardware
352 if (ia64_platform_is("sn2")) {
356 for_each_node_state(nid
, N_ONLINE
) {
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
;
372 ret
= misc_register(&fetchop_miscdev
);
375 "%s: failed to register device %i\n",
377 goto free_scratch_pages
;
381 ret
= misc_register(&cached_miscdev
);
383 printk(KERN_ERR
"%s: failed to register device %i\n",
386 misc_deregister(&fetchop_miscdev
);
387 goto free_scratch_pages
;
389 ret
= misc_register(&uncached_miscdev
);
391 printk(KERN_ERR
"%s: failed to register device %i\n",
393 misc_deregister(&cached_miscdev
);
395 misc_deregister(&fetchop_miscdev
);
396 goto free_scratch_pages
;
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
);
407 if (scratch_page
[nid
] != 0)
408 uncached_free_page(scratch_page
[nid
], 1);
418 misc_deregister(&uncached_miscdev
);
419 misc_deregister(&cached_miscdev
);
421 misc_deregister(&fetchop_miscdev
);
424 if (scratch_page
[nid
] != 0)
425 uncached_free_page(scratch_page
[nid
], 1);
430 module_init(mspec_init
);
431 module_exit(mspec_exit
);
433 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
434 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
435 MODULE_LICENSE("GPL");