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>
47 #include <asm/system.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 atomic_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 int flags; /* See VMD_xxx below. */
98 unsigned long vm_start; /* Original (unsplit) base. */
99 unsigned long vm_end; /* Original (unsplit) end. */
100 unsigned long maddr[0]; /* Array of MSPEC addresses. */
103 #define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
105 /* used on shub2 to clear FOP cache in the HUB */
106 static unsigned long scratch_page[MAX_NUMNODES];
107 #define SH2_AMO_CACHE_ENTRIES 4
110 mspec_zero_block(unsigned long addr, int len)
120 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
121 p = (void *)TO_AMO(scratch_page[nid]);
123 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
124 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
125 p += FETCHOP_VAR_SIZE;
129 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
130 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
132 memset((char *) addr, 0, len);
141 * Called when a device mapping is created by a means other than mmap
142 * (via fork, munmap, etc.). Increments the reference count on the
143 * underlying mspec data so it is not freed prematurely.
146 mspec_open(struct vm_area_struct *vma)
148 struct vma_data *vdata;
150 vdata = vma->vm_private_data;
151 atomic_inc(&vdata->refcnt);
157 * Called when unmapping a device mapping. Frees all mspec pages
158 * belonging to all the vma's sharing this vma_data structure.
161 mspec_close(struct vm_area_struct *vma)
163 struct vma_data *vdata;
164 int index, last_index;
165 unsigned long my_page;
167 vdata = vma->vm_private_data;
169 if (!atomic_dec_and_test(&vdata->refcnt))
172 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
173 for (index = 0; index < last_index; index++) {
174 if (vdata->maddr[index] == 0)
177 * Clear the page before sticking it back
180 my_page = vdata->maddr[index];
181 vdata->maddr[index] = 0;
182 if (!mspec_zero_block(my_page, PAGE_SIZE))
183 uncached_free_page(my_page, 1);
185 printk(KERN_WARNING "mspec_close(): "
186 "failed to zero page %ld\n", my_page);
189 if (vdata->flags & VMD_VMALLOCED)
198 * Creates a mspec page and maps it to user space.
201 mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
203 unsigned long paddr, maddr;
205 pgoff_t index = vmf->pgoff;
206 struct vma_data *vdata = vma->vm_private_data;
208 maddr = (volatile unsigned long) vdata->maddr[index];
210 maddr = uncached_alloc_page(numa_node_id(), 1);
214 spin_lock(&vdata->lock);
215 if (vdata->maddr[index] == 0) {
217 vdata->maddr[index] = maddr;
219 uncached_free_page(maddr, 1);
220 maddr = vdata->maddr[index];
222 spin_unlock(&vdata->lock);
225 if (vdata->type == MSPEC_FETCHOP)
226 paddr = TO_AMO(maddr);
228 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
230 pfn = paddr >> PAGE_SHIFT;
233 * vm_insert_pfn can fail with -EBUSY, but in that case it will
234 * be because another thread has installed the pte first, so it
237 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
239 return VM_FAULT_NOPAGE;
242 static const struct vm_operations_struct mspec_vm_ops = {
244 .close = mspec_close,
245 .fault = mspec_fault,
251 * Called when mmapping the device. Initializes the vma with a fault handler
252 * and private data structure necessary to allocate, track, and free the
256 mspec_mmap(struct file *file, struct vm_area_struct *vma,
257 enum mspec_page_type type)
259 struct vma_data *vdata;
260 int pages, vdata_size, flags = 0;
262 if (vma->vm_pgoff != 0)
265 if ((vma->vm_flags & VM_SHARED) == 0)
268 if ((vma->vm_flags & VM_WRITE) == 0)
271 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
272 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
273 if (vdata_size <= PAGE_SIZE)
274 vdata = kzalloc(vdata_size, GFP_KERNEL);
276 vdata = vzalloc(vdata_size);
277 flags = VMD_VMALLOCED;
282 vdata->vm_start = vma->vm_start;
283 vdata->vm_end = vma->vm_end;
284 vdata->flags = flags;
286 spin_lock_init(&vdata->lock);
287 vdata->refcnt = ATOMIC_INIT(1);
288 vma->vm_private_data = vdata;
290 vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP | VM_DONTEXPAND);
291 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
292 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
293 vma->vm_ops = &mspec_vm_ops;
299 fetchop_mmap(struct file *file, struct vm_area_struct *vma)
301 return mspec_mmap(file, vma, MSPEC_FETCHOP);
305 cached_mmap(struct file *file, struct vm_area_struct *vma)
307 return mspec_mmap(file, vma, MSPEC_CACHED);
311 uncached_mmap(struct file *file, struct vm_area_struct *vma)
313 return mspec_mmap(file, vma, MSPEC_UNCACHED);
316 static const struct file_operations fetchop_fops = {
317 .owner = THIS_MODULE,
318 .mmap = fetchop_mmap,
319 .llseek = noop_llseek,
322 static struct miscdevice fetchop_miscdev = {
323 .minor = MISC_DYNAMIC_MINOR,
324 .name = "sgi_fetchop",
325 .fops = &fetchop_fops
328 static const struct file_operations cached_fops = {
329 .owner = THIS_MODULE,
331 .llseek = noop_llseek,
334 static struct miscdevice cached_miscdev = {
335 .minor = MISC_DYNAMIC_MINOR,
336 .name = "mspec_cached",
340 static const struct file_operations uncached_fops = {
341 .owner = THIS_MODULE,
342 .mmap = uncached_mmap,
343 .llseek = noop_llseek,
346 static struct miscdevice uncached_miscdev = {
347 .minor = MISC_DYNAMIC_MINOR,
348 .name = "mspec_uncached",
349 .fops = &uncached_fops
355 * Called at boot time to initialize the mspec facility.
364 * The fetchop device only works on SN2 hardware, uncached and cached
365 * memory drivers should both be valid on all ia64 hardware
368 if (ia64_platform_is("sn2")) {
372 for_each_node_state(nid, N_ONLINE) {
377 scratch_page[nid] = uncached_alloc_page(nid, 1);
378 if (scratch_page[nid] == 0)
379 goto free_scratch_pages;
380 phys = __pa(scratch_page[nid]);
381 nasid = get_node_number(phys);
382 actual_nid = nasid_to_cnodeid(nasid);
383 if (actual_nid != nid)
384 goto free_scratch_pages;
388 ret = misc_register(&fetchop_miscdev);
391 "%s: failed to register device %i\n",
393 goto free_scratch_pages;
397 ret = misc_register(&cached_miscdev);
399 printk(KERN_ERR "%s: failed to register device %i\n",
402 misc_deregister(&fetchop_miscdev);
403 goto free_scratch_pages;
405 ret = misc_register(&uncached_miscdev);
407 printk(KERN_ERR "%s: failed to register device %i\n",
409 misc_deregister(&cached_miscdev);
411 misc_deregister(&fetchop_miscdev);
412 goto free_scratch_pages;
415 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
416 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
417 CACHED_ID, UNCACHED_ID);
423 if (scratch_page[nid] != 0)
424 uncached_free_page(scratch_page[nid], 1);
434 misc_deregister(&uncached_miscdev);
435 misc_deregister(&cached_miscdev);
437 misc_deregister(&fetchop_miscdev);
440 if (scratch_page[nid] != 0)
441 uncached_free_page(scratch_page[nid], 1);
446 module_init(mspec_init);
447 module_exit(mspec_exit);
449 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
450 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
451 MODULE_LICENSE("GPL");