]>
Commit | Line | Data |
---|---|---|
17a3b050 JS |
1 | /* |
2 | * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights | |
3 | * reserved. | |
4 | * | |
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 | */ | |
9 | ||
10 | /* | |
11 | * SN Platform Special Memory (mspec) Support | |
12 | * | |
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. | |
17 | * | |
18 | * Fetchops are atomic memory operations that are implemented in the | |
19 | * memory controller on SGI SN hardware. | |
20 | * | |
21 | * Uncached are used for memory write combining feature of the ia64 | |
22 | * cpu. | |
23 | * | |
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. | |
31 | */ | |
32 | ||
17a3b050 JS |
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> | |
4e950f6f | 41 | #include <linux/fs.h> |
17a3b050 JS |
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> | |
59 | ||
60 | ||
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" | |
66 | ||
67 | /* | |
68 | * Page types allocated by the device. | |
69 | */ | |
4191ba26 | 70 | enum mspec_page_type { |
17a3b050 JS |
71 | MSPEC_FETCHOP = 1, |
72 | MSPEC_CACHED, | |
73 | MSPEC_UNCACHED | |
74 | }; | |
75 | ||
1a4b0fc5 | 76 | #ifdef CONFIG_SGI_SN |
17a3b050 | 77 | static int is_sn2; |
1a4b0fc5 JS |
78 | #else |
79 | #define is_sn2 0 | |
80 | #endif | |
17a3b050 JS |
81 | |
82 | /* | |
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. | |
4191ba26 CW |
86 | * This structure is shared by all vma's that are split off from the |
87 | * original vma when split_vma()'s are done. | |
88 | * | |
89 | * The refcnt is incremented atomically because mm->mmap_sem does not | |
90 | * protect in fork case where multiple tasks share the vma_data. | |
17a3b050 JS |
91 | */ |
92 | struct vma_data { | |
93 | atomic_t refcnt; /* Number of vmas sharing the data. */ | |
4191ba26 | 94 | spinlock_t lock; /* Serialize access to this structure. */ |
17a3b050 | 95 | int count; /* Number of pages allocated. */ |
4191ba26 CW |
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. */ | |
17a3b050 JS |
100 | unsigned long maddr[0]; /* Array of MSPEC addresses. */ |
101 | }; | |
102 | ||
4191ba26 CW |
103 | #define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */ |
104 | ||
17a3b050 JS |
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 | |
108 | ||
109 | static inline int | |
110 | mspec_zero_block(unsigned long addr, int len) | |
111 | { | |
112 | int status; | |
113 | ||
114 | if (is_sn2) { | |
115 | if (is_shub2()) { | |
116 | int nid; | |
117 | void *p; | |
118 | int i; | |
119 | ||
120 | nid = nasid_to_cnodeid(get_node_number(__pa(addr))); | |
121 | p = (void *)TO_AMO(scratch_page[nid]); | |
122 | ||
123 | for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) { | |
124 | FETCHOP_LOAD_OP(p, FETCHOP_LOAD); | |
125 | p += FETCHOP_VAR_SIZE; | |
126 | } | |
127 | } | |
128 | ||
129 | status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len, | |
130 | BTE_WACQUIRE | BTE_ZERO_FILL, NULL); | |
131 | } else { | |
132 | memset((char *) addr, 0, len); | |
133 | status = 0; | |
134 | } | |
135 | return status; | |
136 | } | |
137 | ||
138 | /* | |
139 | * mspec_open | |
140 | * | |
141 | * Called when a device mapping is created by a means other than mmap | |
4191ba26 CW |
142 | * (via fork, munmap, etc.). Increments the reference count on the |
143 | * underlying mspec data so it is not freed prematurely. | |
17a3b050 JS |
144 | */ |
145 | static void | |
146 | mspec_open(struct vm_area_struct *vma) | |
147 | { | |
148 | struct vma_data *vdata; | |
149 | ||
150 | vdata = vma->vm_private_data; | |
151 | atomic_inc(&vdata->refcnt); | |
152 | } | |
153 | ||
154 | /* | |
155 | * mspec_close | |
156 | * | |
157 | * Called when unmapping a device mapping. Frees all mspec pages | |
afa684f6 | 158 | * belonging to all the vma's sharing this vma_data structure. |
17a3b050 JS |
159 | */ |
160 | static void | |
161 | mspec_close(struct vm_area_struct *vma) | |
162 | { | |
163 | struct vma_data *vdata; | |
afa684f6 | 164 | int index, last_index; |
4191ba26 | 165 | unsigned long my_page; |
17a3b050 JS |
166 | |
167 | vdata = vma->vm_private_data; | |
17a3b050 | 168 | |
afa684f6 CW |
169 | if (!atomic_dec_and_test(&vdata->refcnt)) |
170 | return; | |
4191ba26 | 171 | |
afa684f6 CW |
172 | last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT; |
173 | for (index = 0; index < last_index; index++) { | |
4191ba26 | 174 | if (vdata->maddr[index] == 0) |
17a3b050 JS |
175 | continue; |
176 | /* | |
177 | * Clear the page before sticking it back | |
178 | * into the pool. | |
179 | */ | |
4191ba26 CW |
180 | my_page = vdata->maddr[index]; |
181 | vdata->maddr[index] = 0; | |
afa684f6 | 182 | if (!mspec_zero_block(my_page, PAGE_SIZE)) |
e4a064df | 183 | uncached_free_page(my_page, 1); |
17a3b050 JS |
184 | else |
185 | printk(KERN_WARNING "mspec_close(): " | |
afa684f6 | 186 | "failed to zero page %ld\n", my_page); |
17a3b050 | 187 | } |
4191ba26 CW |
188 | |
189 | if (vdata->flags & VMD_VMALLOCED) | |
17a3b050 | 190 | vfree(vdata); |
4191ba26 CW |
191 | else |
192 | kfree(vdata); | |
17a3b050 JS |
193 | } |
194 | ||
17a3b050 | 195 | /* |
efe9e779 | 196 | * mspec_fault |
17a3b050 JS |
197 | * |
198 | * Creates a mspec page and maps it to user space. | |
199 | */ | |
efe9e779 NP |
200 | static int |
201 | mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
17a3b050 JS |
202 | { |
203 | unsigned long paddr, maddr; | |
204 | unsigned long pfn; | |
efe9e779 | 205 | pgoff_t index = vmf->pgoff; |
17a3b050 JS |
206 | struct vma_data *vdata = vma->vm_private_data; |
207 | ||
17a3b050 JS |
208 | maddr = (volatile unsigned long) vdata->maddr[index]; |
209 | if (maddr == 0) { | |
e4a064df | 210 | maddr = uncached_alloc_page(numa_node_id(), 1); |
17a3b050 | 211 | if (maddr == 0) |
efe9e779 | 212 | return VM_FAULT_OOM; |
17a3b050 JS |
213 | |
214 | spin_lock(&vdata->lock); | |
215 | if (vdata->maddr[index] == 0) { | |
216 | vdata->count++; | |
217 | vdata->maddr[index] = maddr; | |
218 | } else { | |
e4a064df | 219 | uncached_free_page(maddr, 1); |
17a3b050 JS |
220 | maddr = vdata->maddr[index]; |
221 | } | |
222 | spin_unlock(&vdata->lock); | |
223 | } | |
224 | ||
225 | if (vdata->type == MSPEC_FETCHOP) | |
226 | paddr = TO_AMO(maddr); | |
227 | else | |
1a4b0fc5 | 228 | paddr = maddr & ~__IA64_UNCACHED_OFFSET; |
17a3b050 JS |
229 | |
230 | pfn = paddr >> PAGE_SHIFT; | |
231 | ||
efe9e779 NP |
232 | /* |
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 | |
235 | * is no problem. | |
236 | */ | |
237 | vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); | |
238 | ||
239 | return VM_FAULT_NOPAGE; | |
17a3b050 JS |
240 | } |
241 | ||
242 | static struct vm_operations_struct mspec_vm_ops = { | |
243 | .open = mspec_open, | |
244 | .close = mspec_close, | |
efe9e779 | 245 | .fault = mspec_fault, |
17a3b050 JS |
246 | }; |
247 | ||
248 | /* | |
249 | * mspec_mmap | |
250 | * | |
251 | * Called when mmaping the device. Initializes the vma with a fault handler | |
252 | * and private data structure necessary to allocate, track, and free the | |
253 | * underlying pages. | |
254 | */ | |
255 | static int | |
4191ba26 CW |
256 | mspec_mmap(struct file *file, struct vm_area_struct *vma, |
257 | enum mspec_page_type type) | |
17a3b050 JS |
258 | { |
259 | struct vma_data *vdata; | |
4191ba26 | 260 | int pages, vdata_size, flags = 0; |
17a3b050 JS |
261 | |
262 | if (vma->vm_pgoff != 0) | |
263 | return -EINVAL; | |
264 | ||
265 | if ((vma->vm_flags & VM_SHARED) == 0) | |
266 | return -EINVAL; | |
267 | ||
268 | if ((vma->vm_flags & VM_WRITE) == 0) | |
269 | return -EPERM; | |
270 | ||
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 = kmalloc(vdata_size, GFP_KERNEL); | |
4191ba26 | 275 | else { |
17a3b050 | 276 | vdata = vmalloc(vdata_size); |
4191ba26 CW |
277 | flags = VMD_VMALLOCED; |
278 | } | |
17a3b050 JS |
279 | if (!vdata) |
280 | return -ENOMEM; | |
281 | memset(vdata, 0, vdata_size); | |
282 | ||
4191ba26 CW |
283 | vdata->vm_start = vma->vm_start; |
284 | vdata->vm_end = vma->vm_end; | |
285 | vdata->flags = flags; | |
17a3b050 JS |
286 | vdata->type = type; |
287 | spin_lock_init(&vdata->lock); | |
288 | vdata->refcnt = ATOMIC_INIT(1); | |
289 | vma->vm_private_data = vdata; | |
290 | ||
2f98735c | 291 | vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP | VM_DONTEXPAND); |
17a3b050 JS |
292 | if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED) |
293 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
294 | vma->vm_ops = &mspec_vm_ops; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | static int | |
300 | fetchop_mmap(struct file *file, struct vm_area_struct *vma) | |
301 | { | |
302 | return mspec_mmap(file, vma, MSPEC_FETCHOP); | |
303 | } | |
304 | ||
305 | static int | |
306 | cached_mmap(struct file *file, struct vm_area_struct *vma) | |
307 | { | |
308 | return mspec_mmap(file, vma, MSPEC_CACHED); | |
309 | } | |
310 | ||
311 | static int | |
312 | uncached_mmap(struct file *file, struct vm_area_struct *vma) | |
313 | { | |
314 | return mspec_mmap(file, vma, MSPEC_UNCACHED); | |
315 | } | |
316 | ||
2b8693c0 | 317 | static const struct file_operations fetchop_fops = { |
17a3b050 JS |
318 | .owner = THIS_MODULE, |
319 | .mmap = fetchop_mmap | |
320 | }; | |
321 | ||
322 | static struct miscdevice fetchop_miscdev = { | |
323 | .minor = MISC_DYNAMIC_MINOR, | |
324 | .name = "sgi_fetchop", | |
325 | .fops = &fetchop_fops | |
326 | }; | |
327 | ||
2b8693c0 | 328 | static const struct file_operations cached_fops = { |
17a3b050 JS |
329 | .owner = THIS_MODULE, |
330 | .mmap = cached_mmap | |
331 | }; | |
332 | ||
333 | static struct miscdevice cached_miscdev = { | |
334 | .minor = MISC_DYNAMIC_MINOR, | |
335 | .name = "mspec_cached", | |
336 | .fops = &cached_fops | |
337 | }; | |
338 | ||
2b8693c0 | 339 | static const struct file_operations uncached_fops = { |
17a3b050 JS |
340 | .owner = THIS_MODULE, |
341 | .mmap = uncached_mmap | |
342 | }; | |
343 | ||
344 | static struct miscdevice uncached_miscdev = { | |
345 | .minor = MISC_DYNAMIC_MINOR, | |
346 | .name = "mspec_uncached", | |
347 | .fops = &uncached_fops | |
348 | }; | |
349 | ||
350 | /* | |
351 | * mspec_init | |
352 | * | |
353 | * Called at boot time to initialize the mspec facility. | |
354 | */ | |
355 | static int __init | |
356 | mspec_init(void) | |
357 | { | |
358 | int ret; | |
359 | int nid; | |
360 | ||
361 | /* | |
362 | * The fetchop device only works on SN2 hardware, uncached and cached | |
363 | * memory drivers should both be valid on all ia64 hardware | |
364 | */ | |
1a4b0fc5 | 365 | #ifdef CONFIG_SGI_SN |
17a3b050 JS |
366 | if (ia64_platform_is("sn2")) { |
367 | is_sn2 = 1; | |
368 | if (is_shub2()) { | |
369 | ret = -ENOMEM; | |
2dca53a9 | 370 | for_each_node_state(nid, N_ONLINE) { |
17a3b050 JS |
371 | int actual_nid; |
372 | int nasid; | |
373 | unsigned long phys; | |
374 | ||
e4a064df | 375 | scratch_page[nid] = uncached_alloc_page(nid, 1); |
17a3b050 JS |
376 | if (scratch_page[nid] == 0) |
377 | goto free_scratch_pages; | |
378 | phys = __pa(scratch_page[nid]); | |
379 | nasid = get_node_number(phys); | |
380 | actual_nid = nasid_to_cnodeid(nasid); | |
381 | if (actual_nid != nid) | |
382 | goto free_scratch_pages; | |
383 | } | |
384 | } | |
385 | ||
386 | ret = misc_register(&fetchop_miscdev); | |
387 | if (ret) { | |
388 | printk(KERN_ERR | |
389 | "%s: failed to register device %i\n", | |
390 | FETCHOP_ID, ret); | |
391 | goto free_scratch_pages; | |
392 | } | |
393 | } | |
1a4b0fc5 | 394 | #endif |
17a3b050 JS |
395 | ret = misc_register(&cached_miscdev); |
396 | if (ret) { | |
397 | printk(KERN_ERR "%s: failed to register device %i\n", | |
398 | CACHED_ID, ret); | |
399 | if (is_sn2) | |
400 | misc_deregister(&fetchop_miscdev); | |
401 | goto free_scratch_pages; | |
402 | } | |
403 | ret = misc_register(&uncached_miscdev); | |
404 | if (ret) { | |
405 | printk(KERN_ERR "%s: failed to register device %i\n", | |
406 | UNCACHED_ID, ret); | |
407 | misc_deregister(&cached_miscdev); | |
408 | if (is_sn2) | |
409 | misc_deregister(&fetchop_miscdev); | |
410 | goto free_scratch_pages; | |
411 | } | |
412 | ||
413 | printk(KERN_INFO "%s %s initialized devices: %s %s %s\n", | |
414 | MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "", | |
415 | CACHED_ID, UNCACHED_ID); | |
416 | ||
417 | return 0; | |
418 | ||
419 | free_scratch_pages: | |
420 | for_each_node(nid) { | |
421 | if (scratch_page[nid] != 0) | |
e4a064df | 422 | uncached_free_page(scratch_page[nid], 1); |
17a3b050 JS |
423 | } |
424 | return ret; | |
425 | } | |
426 | ||
427 | static void __exit | |
428 | mspec_exit(void) | |
429 | { | |
430 | int nid; | |
431 | ||
432 | misc_deregister(&uncached_miscdev); | |
433 | misc_deregister(&cached_miscdev); | |
434 | if (is_sn2) { | |
435 | misc_deregister(&fetchop_miscdev); | |
436 | ||
437 | for_each_node(nid) { | |
438 | if (scratch_page[nid] != 0) | |
e4a064df | 439 | uncached_free_page(scratch_page[nid], 1); |
17a3b050 JS |
440 | } |
441 | } | |
442 | } | |
443 | ||
444 | module_init(mspec_init); | |
445 | module_exit(mspec_exit); | |
446 | ||
447 | MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>"); | |
448 | MODULE_DESCRIPTION("Driver for SGI SN special memory operations"); | |
449 | MODULE_LICENSE("GPL"); |