1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
8 * SN Platform Special Memory (mspec) Support
10 * This driver exports the SN special memory (mspec) facility to user
12 * There are three types of memory made available thru this driver:
13 * fetchops, uncached and cached.
15 * Fetchops are atomic memory operations that are implemented in the
16 * memory controller on SGI SN hardware.
18 * Uncached are used for memory write combining feature of the ia64
21 * Cached are used for areas of memory that are used as cached addresses
22 * on our partition and used as uncached addresses from other partitions.
23 * Due to a design constraint of the SN2 Shub, you can not have processors
24 * on the same FSB perform both a cached and uncached reference to the
25 * same cache line. These special memory cached regions prevent the
26 * kernel from ever dropping in a TLB entry and therefore prevent the
27 * processor from ever speculating a cache line from this page.
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/errno.h>
35 #include <linux/miscdevice.h>
36 #include <linux/spinlock.h>
39 #include <linux/vmalloc.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/numa.h>
43 #include <linux/refcount.h>
45 #include <asm/pgtable.h>
46 #include <linux/atomic.h>
47 #include <asm/tlbflush.h>
48 #include <asm/uncached.h>
49 #include <asm/sn/addrs.h>
50 #include <asm/sn/arch.h>
51 #include <asm/sn/mspec.h>
52 #include <asm/sn/sn_cpuid.h>
53 #include <asm/sn/io.h>
54 #include <asm/sn/bte.h>
55 #include <asm/sn/shubio.h>
58 #define FETCHOP_ID "SGI Fetchop,"
59 #define CACHED_ID "Cached,"
60 #define UNCACHED_ID "Uncached"
61 #define REVISION "4.0"
62 #define MSPEC_BASENAME "mspec"
65 * Page types allocated by the device.
67 enum mspec_page_type
{
80 * One of these structures is allocated when an mspec region is mmaped. The
81 * structure is pointed to by the vma->vm_private_data field in the vma struct.
82 * This structure is used to record the addresses of the mspec pages.
83 * This structure is shared by all vma's that are split off from the
84 * original vma when split_vma()'s are done.
86 * The refcnt is incremented atomically because mm->mmap_sem does not
87 * protect in fork case where multiple tasks share the vma_data.
90 refcount_t refcnt
; /* Number of vmas sharing the data. */
91 spinlock_t lock
; /* Serialize access to this structure. */
92 int count
; /* Number of pages allocated. */
93 enum mspec_page_type type
; /* Type of pages allocated. */
94 unsigned long vm_start
; /* Original (unsplit) base. */
95 unsigned long vm_end
; /* Original (unsplit) end. */
96 unsigned long maddr
[0]; /* Array of MSPEC addresses. */
99 /* used on shub2 to clear FOP cache in the HUB */
100 static unsigned long scratch_page
[MAX_NUMNODES
];
101 #define SH2_AMO_CACHE_ENTRIES 4
104 mspec_zero_block(unsigned long addr
, int len
)
114 nid
= nasid_to_cnodeid(get_node_number(__pa(addr
)));
115 p
= (void *)TO_AMO(scratch_page
[nid
]);
117 for (i
=0; i
< SH2_AMO_CACHE_ENTRIES
; i
++) {
118 FETCHOP_LOAD_OP(p
, FETCHOP_LOAD
);
119 p
+= FETCHOP_VAR_SIZE
;
123 status
= bte_copy(0, addr
& ~__IA64_UNCACHED_OFFSET
, len
,
124 BTE_WACQUIRE
| BTE_ZERO_FILL
, NULL
);
126 memset((char *) addr
, 0, len
);
135 * Called when a device mapping is created by a means other than mmap
136 * (via fork, munmap, etc.). Increments the reference count on the
137 * underlying mspec data so it is not freed prematurely.
140 mspec_open(struct vm_area_struct
*vma
)
142 struct vma_data
*vdata
;
144 vdata
= vma
->vm_private_data
;
145 refcount_inc(&vdata
->refcnt
);
151 * Called when unmapping a device mapping. Frees all mspec pages
152 * belonging to all the vma's sharing this vma_data structure.
155 mspec_close(struct vm_area_struct
*vma
)
157 struct vma_data
*vdata
;
158 int index
, last_index
;
159 unsigned long my_page
;
161 vdata
= vma
->vm_private_data
;
163 if (!refcount_dec_and_test(&vdata
->refcnt
))
166 last_index
= (vdata
->vm_end
- vdata
->vm_start
) >> PAGE_SHIFT
;
167 for (index
= 0; index
< last_index
; index
++) {
168 if (vdata
->maddr
[index
] == 0)
171 * Clear the page before sticking it back
174 my_page
= vdata
->maddr
[index
];
175 vdata
->maddr
[index
] = 0;
176 if (!mspec_zero_block(my_page
, PAGE_SIZE
))
177 uncached_free_page(my_page
, 1);
179 printk(KERN_WARNING
"mspec_close(): "
180 "failed to zero page %ld\n", my_page
);
189 * Creates a mspec page and maps it to user space.
192 mspec_fault(struct vm_fault
*vmf
)
194 unsigned long paddr
, maddr
;
196 pgoff_t index
= vmf
->pgoff
;
197 struct vma_data
*vdata
= vmf
->vma
->vm_private_data
;
199 maddr
= (volatile unsigned long) vdata
->maddr
[index
];
201 maddr
= uncached_alloc_page(numa_node_id(), 1);
205 spin_lock(&vdata
->lock
);
206 if (vdata
->maddr
[index
] == 0) {
208 vdata
->maddr
[index
] = maddr
;
210 uncached_free_page(maddr
, 1);
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
;
223 return vmf_insert_pfn(vmf
->vma
, vmf
->address
, pfn
);
226 static const struct vm_operations_struct mspec_vm_ops
= {
228 .close
= mspec_close
,
229 .fault
= mspec_fault
,
235 * Called when mmapping 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
,
241 enum mspec_page_type type
)
243 struct vma_data
*vdata
;
244 int pages
, vdata_size
;
246 if (vma
->vm_pgoff
!= 0)
249 if ((vma
->vm_flags
& VM_SHARED
) == 0)
252 if ((vma
->vm_flags
& VM_WRITE
) == 0)
255 pages
= vma_pages(vma
);
256 vdata_size
= sizeof(struct vma_data
) + pages
* sizeof(long);
257 if (vdata_size
<= PAGE_SIZE
)
258 vdata
= kzalloc(vdata_size
, GFP_KERNEL
);
260 vdata
= vzalloc(vdata_size
);
264 vdata
->vm_start
= vma
->vm_start
;
265 vdata
->vm_end
= vma
->vm_end
;
267 spin_lock_init(&vdata
->lock
);
268 refcount_set(&vdata
->refcnt
, 1);
269 vma
->vm_private_data
= vdata
;
271 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
272 if (vdata
->type
== MSPEC_FETCHOP
|| vdata
->type
== MSPEC_UNCACHED
)
273 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
274 vma
->vm_ops
= &mspec_vm_ops
;
280 fetchop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
282 return mspec_mmap(file
, vma
, MSPEC_FETCHOP
);
286 cached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
288 return mspec_mmap(file
, vma
, MSPEC_CACHED
);
292 uncached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
294 return mspec_mmap(file
, vma
, MSPEC_UNCACHED
);
297 static const struct file_operations fetchop_fops
= {
298 .owner
= THIS_MODULE
,
299 .mmap
= fetchop_mmap
,
300 .llseek
= noop_llseek
,
303 static struct miscdevice fetchop_miscdev
= {
304 .minor
= MISC_DYNAMIC_MINOR
,
305 .name
= "sgi_fetchop",
306 .fops
= &fetchop_fops
309 static const struct file_operations cached_fops
= {
310 .owner
= THIS_MODULE
,
312 .llseek
= noop_llseek
,
315 static struct miscdevice cached_miscdev
= {
316 .minor
= MISC_DYNAMIC_MINOR
,
317 .name
= "mspec_cached",
321 static const struct file_operations uncached_fops
= {
322 .owner
= THIS_MODULE
,
323 .mmap
= uncached_mmap
,
324 .llseek
= noop_llseek
,
327 static struct miscdevice uncached_miscdev
= {
328 .minor
= MISC_DYNAMIC_MINOR
,
329 .name
= "mspec_uncached",
330 .fops
= &uncached_fops
336 * Called at boot time to initialize the mspec facility.
345 * The fetchop device only works on SN2 hardware, uncached and cached
346 * memory drivers should both be valid on all ia64 hardware
349 if (ia64_platform_is("sn2")) {
353 for_each_node_state(nid
, N_ONLINE
) {
358 scratch_page
[nid
] = uncached_alloc_page(nid
, 1);
359 if (scratch_page
[nid
] == 0)
360 goto free_scratch_pages
;
361 phys
= __pa(scratch_page
[nid
]);
362 nasid
= get_node_number(phys
);
363 actual_nid
= nasid_to_cnodeid(nasid
);
364 if (actual_nid
!= nid
)
365 goto free_scratch_pages
;
369 ret
= misc_register(&fetchop_miscdev
);
372 "%s: failed to register device %i\n",
374 goto free_scratch_pages
;
378 ret
= misc_register(&cached_miscdev
);
380 printk(KERN_ERR
"%s: failed to register device %i\n",
383 misc_deregister(&fetchop_miscdev
);
384 goto free_scratch_pages
;
386 ret
= misc_register(&uncached_miscdev
);
388 printk(KERN_ERR
"%s: failed to register device %i\n",
390 misc_deregister(&cached_miscdev
);
392 misc_deregister(&fetchop_miscdev
);
393 goto free_scratch_pages
;
396 printk(KERN_INFO
"%s %s initialized devices: %s %s %s\n",
397 MSPEC_BASENAME
, REVISION
, is_sn2
? FETCHOP_ID
: "",
398 CACHED_ID
, UNCACHED_ID
);
404 if (scratch_page
[nid
] != 0)
405 uncached_free_page(scratch_page
[nid
], 1);
415 misc_deregister(&uncached_miscdev
);
416 misc_deregister(&cached_miscdev
);
418 misc_deregister(&fetchop_miscdev
);
421 if (scratch_page
[nid
] != 0)
422 uncached_free_page(scratch_page
[nid
], 1);
427 module_init(mspec_init
);
428 module_exit(mspec_exit
);
430 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
431 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
432 MODULE_LICENSE("GPL");