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 <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.
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.
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
100 mspec_zero_block(unsigned long addr
, int len
)
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
);
122 memset((char *) addr
, 0, len
);
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.
136 mspec_open(struct vm_area_struct
*vma
)
138 struct vma_data
*vdata
;
140 vdata
= vma
->vm_private_data
;
141 atomic_inc(&vdata
->refcnt
);
147 * Called when unmapping a device mapping. Frees all mspec pages
148 * belonging to the vma.
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
))
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)
166 * Clear the page before sticking it back
169 result
= mspec_zero_block(vdata
->maddr
[i
], PAGE_SIZE
);
171 uncached_free_page(vdata
->maddr
[i
]);
173 printk(KERN_WARNING
"mspec_close(): "
174 "failed to zero page %i\n",
178 if (vdata_size
<= PAGE_SIZE
)
188 * Creates a mspec page and maps it to user space.
191 mspec_nopfn(struct vm_area_struct
*vma
, unsigned long address
)
193 unsigned long paddr
, maddr
;
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
];
201 maddr
= uncached_alloc_page(numa_node_id());
205 spin_lock(&vdata
->lock
);
206 if (vdata
->maddr
[index
] == 0) {
208 vdata
->maddr
[index
] = maddr
;
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
);
219 paddr
= maddr
& ~__IA64_UNCACHED_OFFSET
;
221 pfn
= paddr
>> PAGE_SHIFT
;
226 static struct vm_operations_struct mspec_vm_ops
= {
228 .close
= mspec_close
,
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
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)
248 if ((vma
->vm_flags
& VM_SHARED
) == 0)
251 if ((vma
->vm_flags
& VM_WRITE
) == 0)
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
);
259 vdata
= vmalloc(vdata_size
);
262 memset(vdata
, 0, vdata_size
);
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
;
278 fetchop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
280 return mspec_mmap(file
, vma
, MSPEC_FETCHOP
);
284 cached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
286 return mspec_mmap(file
, vma
, MSPEC_CACHED
);
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
,
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
,
311 static struct miscdevice cached_miscdev
= {
312 .minor
= MISC_DYNAMIC_MINOR
,
313 .name
= "mspec_cached",
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
331 * Called at boot time to initialize the mspec facility.
340 * The fetchop device only works on SN2 hardware, uncached and cached
341 * memory drivers should both be valid on all ia64 hardware
344 if (ia64_platform_is("sn2")) {
348 for_each_online_node(nid
) {
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
);
367 "%s: failed to register device %i\n",
369 goto free_scratch_pages
;
373 ret
= misc_register(&cached_miscdev
);
375 printk(KERN_ERR
"%s: failed to register device %i\n",
378 misc_deregister(&fetchop_miscdev
);
379 goto free_scratch_pages
;
381 ret
= misc_register(&uncached_miscdev
);
383 printk(KERN_ERR
"%s: failed to register device %i\n",
385 misc_deregister(&cached_miscdev
);
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
);
399 if (scratch_page
[nid
] != 0)
400 uncached_free_page(scratch_page
[nid
]);
410 misc_deregister(&uncached_miscdev
);
411 misc_deregister(&cached_miscdev
);
413 misc_deregister(&fetchop_miscdev
);
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");