blob: b1b994dd41dbc1d0a722152ea1f8f0b4cfc894c9 [file] [log] [blame]
Ralf Baechle41c594a2006-04-05 09:45:45 +01001/*
2 * General MIPS MT support routines, usable in AP/SP, SMVP, or SMTC kernels
3 * Copyright (C) 2005 Mips Technologies, Inc
4 */
5
Ralf Baechle27a3bba2007-02-07 13:48:59 +00006#include <linux/device.h>
Ralf Baechle41c594a2006-04-05 09:45:45 +01007#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/cpumask.h>
Ralf Baechle27a3bba2007-02-07 13:48:59 +000010#include <linux/module.h>
Ralf Baechle41c594a2006-04-05 09:45:45 +010011#include <linux/interrupt.h>
Yoichi Yuasaf72af3c2006-07-04 22:16:28 +090012#include <linux/security.h>
Ralf Baechle41c594a2006-04-05 09:45:45 +010013
14#include <asm/cpu.h>
15#include <asm/processor.h>
16#include <asm/atomic.h>
17#include <asm/system.h>
18#include <asm/hardirq.h>
19#include <asm/mmu_context.h>
20#include <asm/smp.h>
21#include <asm/mipsmtregs.h>
22#include <asm/r4kcache.h>
23#include <asm/cacheflush.h>
24
25/*
26 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
27 */
28
29cpumask_t mt_fpu_cpumask;
30
31#ifdef CONFIG_MIPS_MT_FPAFF
32
33#include <linux/cpu.h>
34#include <linux/delay.h>
35#include <asm/uaccess.h>
36
37unsigned long mt_fpemul_threshold = 0;
38
39/*
40 * Replacement functions for the sys_sched_setaffinity() and
41 * sys_sched_getaffinity() system calls, so that we can integrate
42 * FPU affinity with the user's requested processor affinity.
43 * This code is 98% identical with the sys_sched_setaffinity()
44 * and sys_sched_getaffinity() system calls, and should be
45 * updated when kernel/sched.c changes.
46 */
47
48/*
49 * find_process_by_pid - find a process with a matching PID value.
50 * used in sys_sched_set/getaffinity() in kernel/sched.c, so
51 * cloned here.
52 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070053static inline struct task_struct *find_process_by_pid(pid_t pid)
Ralf Baechle41c594a2006-04-05 09:45:45 +010054{
55 return pid ? find_task_by_pid(pid) : current;
56}
57
58
59/*
60 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
61 */
62asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
63 unsigned long __user *user_mask_ptr)
64{
65 cpumask_t new_mask;
66 cpumask_t effective_mask;
67 int retval;
Ingo Molnar36c8b582006-07-03 00:25:41 -070068 struct task_struct *p;
Ralf Baechle41c594a2006-04-05 09:45:45 +010069
70 if (len < sizeof(new_mask))
71 return -EINVAL;
72
73 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
74 return -EFAULT;
75
76 lock_cpu_hotplug();
77 read_lock(&tasklist_lock);
78
79 p = find_process_by_pid(pid);
80 if (!p) {
81 read_unlock(&tasklist_lock);
82 unlock_cpu_hotplug();
83 return -ESRCH;
84 }
85
86 /*
87 * It is not safe to call set_cpus_allowed with the
88 * tasklist_lock held. We will bump the task_struct's
89 * usage count and drop tasklist_lock before invoking
90 * set_cpus_allowed.
91 */
92 get_task_struct(p);
93
94 retval = -EPERM;
95 if ((current->euid != p->euid) && (current->euid != p->uid) &&
96 !capable(CAP_SYS_NICE)) {
97 read_unlock(&tasklist_lock);
98 goto out_unlock;
99 }
100
David Quigley7418cb82006-06-28 09:36:46 -0400101 retval = security_task_setscheduler(p, 0, NULL);
102 if (retval)
103 goto out_unlock;
104
Ralf Baechle41c594a2006-04-05 09:45:45 +0100105 /* Record new user-specified CPU set for future reference */
106 p->thread.user_cpus_allowed = new_mask;
107
108 /* Unlock the task list */
109 read_unlock(&tasklist_lock);
110
111 /* Compute new global allowed CPU set if necessary */
Ralf Baechled223a8612007-07-10 17:33:02 +0100112 if ((p->thread.mflags & MF_FPUBOUND)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100113 && cpus_intersects(new_mask, mt_fpu_cpumask)) {
114 cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
115 retval = set_cpus_allowed(p, effective_mask);
116 } else {
117 p->thread.mflags &= ~MF_FPUBOUND;
118 retval = set_cpus_allowed(p, new_mask);
119 }
120
121
122out_unlock:
123 put_task_struct(p);
124 unlock_cpu_hotplug();
125 return retval;
126}
127
128/*
129 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
130 */
131asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
132 unsigned long __user *user_mask_ptr)
133{
134 unsigned int real_len;
135 cpumask_t mask;
136 int retval;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700137 struct task_struct *p;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100138
139 real_len = sizeof(mask);
140 if (len < real_len)
141 return -EINVAL;
142
143 lock_cpu_hotplug();
144 read_lock(&tasklist_lock);
145
146 retval = -ESRCH;
147 p = find_process_by_pid(pid);
148 if (!p)
149 goto out_unlock;
David Quigley7418cb82006-06-28 09:36:46 -0400150 retval = security_task_getscheduler(p);
151 if (retval)
152 goto out_unlock;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100153
154 cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
155
156out_unlock:
157 read_unlock(&tasklist_lock);
158 unlock_cpu_hotplug();
159 if (retval)
160 return retval;
161 if (copy_to_user(user_mask_ptr, &mask, real_len))
162 return -EFAULT;
163 return real_len;
164}
165
166#endif /* CONFIG_MIPS_MT_FPAFF */
167
168/*
169 * Dump new MIPS MT state for the core. Does not leave TCs halted.
170 * Takes an argument which taken to be a pre-call MVPControl value.
171 */
172
173void mips_mt_regdump(unsigned long mvpctl)
174{
175 unsigned long flags;
176 unsigned long vpflags;
177 unsigned long mvpconf0;
178 int nvpe;
179 int ntc;
180 int i;
181 int tc;
182 unsigned long haltval;
183 unsigned long tcstatval;
184#ifdef CONFIG_MIPS_MT_SMTC
185 void smtc_soft_dump(void);
186#endif /* CONFIG_MIPT_MT_SMTC */
187
188 local_irq_save(flags);
189 vpflags = dvpe();
190 printk("=== MIPS MT State Dump ===\n");
191 printk("-- Global State --\n");
192 printk(" MVPControl Passed: %08lx\n", mvpctl);
193 printk(" MVPControl Read: %08lx\n", vpflags);
194 printk(" MVPConf0 : %08lx\n", (mvpconf0 = read_c0_mvpconf0()));
195 nvpe = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
196 ntc = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
197 printk("-- per-VPE State --\n");
Ralf Baechled223a8612007-07-10 17:33:02 +0100198 for (i = 0; i < nvpe; i++) {
199 for (tc = 0; tc < ntc; tc++) {
Ralf Baechle41c594a2006-04-05 09:45:45 +0100200 settc(tc);
Ralf Baechled223a8612007-07-10 17:33:02 +0100201 if ((read_tc_c0_tcbind() & TCBIND_CURVPE) == i) {
202 printk(" VPE %d\n", i);
203 printk(" VPEControl : %08lx\n",
204 read_vpe_c0_vpecontrol());
205 printk(" VPEConf0 : %08lx\n",
206 read_vpe_c0_vpeconf0());
207 printk(" VPE%d.Status : %08lx\n",
208 i, read_vpe_c0_status());
209 printk(" VPE%d.EPC : %08lx\n",
210 i, read_vpe_c0_epc());
211 printk(" VPE%d.Cause : %08lx\n",
212 i, read_vpe_c0_cause());
213 printk(" VPE%d.Config7 : %08lx\n",
214 i, read_vpe_c0_config7());
215 break; /* Next VPE */
216 }
Ralf Baechle41c594a2006-04-05 09:45:45 +0100217 }
Ralf Baechle41c594a2006-04-05 09:45:45 +0100218 }
219 printk("-- per-TC State --\n");
Ralf Baechled223a8612007-07-10 17:33:02 +0100220 for (tc = 0; tc < ntc; tc++) {
Ralf Baechle41c594a2006-04-05 09:45:45 +0100221 settc(tc);
Ralf Baechled223a8612007-07-10 17:33:02 +0100222 if (read_tc_c0_tcbind() == read_c0_tcbind()) {
Ralf Baechle41c594a2006-04-05 09:45:45 +0100223 /* Are we dumping ourself? */
224 haltval = 0; /* Then we're not halted, and mustn't be */
225 tcstatval = flags; /* And pre-dump TCStatus is flags */
226 printk(" TC %d (current TC with VPE EPC above)\n", tc);
227 } else {
228 haltval = read_tc_c0_tchalt();
229 write_tc_c0_tchalt(1);
230 tcstatval = read_tc_c0_tcstatus();
231 printk(" TC %d\n", tc);
232 }
233 printk(" TCStatus : %08lx\n", tcstatval);
234 printk(" TCBind : %08lx\n", read_tc_c0_tcbind());
235 printk(" TCRestart : %08lx\n", read_tc_c0_tcrestart());
236 printk(" TCHalt : %08lx\n", haltval);
237 printk(" TCContext : %08lx\n", read_tc_c0_tccontext());
238 if (!haltval)
239 write_tc_c0_tchalt(0);
240 }
241#ifdef CONFIG_MIPS_MT_SMTC
242 smtc_soft_dump();
243#endif /* CONFIG_MIPT_MT_SMTC */
244 printk("===========================\n");
245 evpe(vpflags);
246 local_irq_restore(flags);
247}
248
249static int mt_opt_norps = 0;
250static int mt_opt_rpsctl = -1;
251static int mt_opt_nblsu = -1;
252static int mt_opt_forceconfig7 = 0;
253static int mt_opt_config7 = -1;
254
255static int __init rps_disable(char *s)
256{
257 mt_opt_norps = 1;
258 return 1;
259}
260__setup("norps", rps_disable);
261
262static int __init rpsctl_set(char *str)
263{
264 get_option(&str, &mt_opt_rpsctl);
265 return 1;
266}
267__setup("rpsctl=", rpsctl_set);
268
269static int __init nblsu_set(char *str)
270{
271 get_option(&str, &mt_opt_nblsu);
272 return 1;
273}
274__setup("nblsu=", nblsu_set);
275
276static int __init config7_set(char *str)
277{
278 get_option(&str, &mt_opt_config7);
279 mt_opt_forceconfig7 = 1;
280 return 1;
281}
282__setup("config7=", config7_set);
283
284/* Experimental cache flush control parameters that should go away some day */
285int mt_protiflush = 0;
286int mt_protdflush = 0;
287int mt_n_iflushes = 1;
288int mt_n_dflushes = 1;
289
290static int __init set_protiflush(char *s)
291{
292 mt_protiflush = 1;
293 return 1;
294}
295__setup("protiflush", set_protiflush);
296
297static int __init set_protdflush(char *s)
298{
299 mt_protdflush = 1;
300 return 1;
301}
302__setup("protdflush", set_protdflush);
303
304static int __init niflush(char *s)
305{
306 get_option(&s, &mt_n_iflushes);
307 return 1;
308}
309__setup("niflush=", niflush);
310
311static int __init ndflush(char *s)
312{
313 get_option(&s, &mt_n_dflushes);
314 return 1;
315}
316__setup("ndflush=", ndflush);
317#ifdef CONFIG_MIPS_MT_FPAFF
318static int fpaff_threshold = -1;
319
320static int __init fpaff_thresh(char *str)
321{
322 get_option(&str, &fpaff_threshold);
323 return 1;
324}
325
326__setup("fpaff=", fpaff_thresh);
327#endif /* CONFIG_MIPS_MT_FPAFF */
328
329static unsigned int itc_base = 0;
330
331static int __init set_itc_base(char *str)
332{
333 get_option(&str, &itc_base);
334 return 1;
335}
336
337__setup("itcbase=", set_itc_base);
338
339void mips_mt_set_cpuoptions(void)
340{
341 unsigned int oconfig7 = read_c0_config7();
342 unsigned int nconfig7 = oconfig7;
343
344 if (mt_opt_norps) {
345 printk("\"norps\" option deprectated: use \"rpsctl=\"\n");
346 }
347 if (mt_opt_rpsctl >= 0) {
348 printk("34K return prediction stack override set to %d.\n",
349 mt_opt_rpsctl);
350 if (mt_opt_rpsctl)
351 nconfig7 |= (1 << 2);
352 else
353 nconfig7 &= ~(1 << 2);
354 }
355 if (mt_opt_nblsu >= 0) {
356 printk("34K ALU/LSU sync override set to %d.\n", mt_opt_nblsu);
357 if (mt_opt_nblsu)
358 nconfig7 |= (1 << 5);
359 else
360 nconfig7 &= ~(1 << 5);
361 }
362 if (mt_opt_forceconfig7) {
363 printk("CP0.Config7 forced to 0x%08x.\n", mt_opt_config7);
364 nconfig7 = mt_opt_config7;
365 }
366 if (oconfig7 != nconfig7) {
367 __asm__ __volatile("sync");
368 write_c0_config7(nconfig7);
369 ehb ();
370 printk("Config7: 0x%08x\n", read_c0_config7());
371 }
372
373 /* Report Cache management debug options */
374 if (mt_protiflush)
375 printk("I-cache flushes single-threaded\n");
376 if (mt_protdflush)
377 printk("D-cache flushes single-threaded\n");
378 if (mt_n_iflushes != 1)
379 printk("I-Cache Flushes Repeated %d times\n", mt_n_iflushes);
380 if (mt_n_dflushes != 1)
381 printk("D-Cache Flushes Repeated %d times\n", mt_n_dflushes);
382
383#ifdef CONFIG_MIPS_MT_FPAFF
384 /* FPU Use Factor empirically derived from experiments on 34K */
385#define FPUSEFACTOR 333
386
387 if (fpaff_threshold >= 0) {
388 mt_fpemul_threshold = fpaff_threshold;
389 } else {
390 mt_fpemul_threshold =
Ralf Baechled223a8612007-07-10 17:33:02 +0100391 (FPUSEFACTOR * (loops_per_jiffy / (500000 / HZ))) / HZ;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100392 }
393 printk("FPU Affinity set after %ld emulations\n",
394 mt_fpemul_threshold);
395#endif /* CONFIG_MIPS_MT_FPAFF */
396
397 if (itc_base != 0) {
398 /*
399 * Configure ITC mapping. This code is very
400 * specific to the 34K core family, which uses
401 * a special mode bit ("ITC") in the ErrCtl
402 * register to enable access to ITC control
403 * registers via cache "tag" operations.
404 */
405 unsigned long ectlval;
406 unsigned long itcblkgrn;
407
408 /* ErrCtl register is known as "ecc" to Linux */
409 ectlval = read_c0_ecc();
410 write_c0_ecc(ectlval | (0x1 << 26));
411 ehb();
412#define INDEX_0 (0x80000000)
413#define INDEX_8 (0x80000008)
414 /* Read "cache tag" for Dcache pseudo-index 8 */
415 cache_op(Index_Load_Tag_D, INDEX_8);
416 ehb();
417 itcblkgrn = read_c0_dtaglo();
418 itcblkgrn &= 0xfffe0000;
419 /* Set for 128 byte pitch of ITC cells */
420 itcblkgrn |= 0x00000c00;
421 /* Stage in Tag register */
422 write_c0_dtaglo(itcblkgrn);
423 ehb();
424 /* Write out to ITU with CACHE op */
425 cache_op(Index_Store_Tag_D, INDEX_8);
426 /* Now set base address, and turn ITC on with 0x1 bit */
427 write_c0_dtaglo((itc_base & 0xfffffc00) | 0x1 );
428 ehb();
429 /* Write out to ITU with CACHE op */
430 cache_op(Index_Store_Tag_D, INDEX_0);
431 write_c0_ecc(ectlval);
432 ehb();
433 printk("Mapped %ld ITC cells starting at 0x%08x\n",
434 ((itcblkgrn & 0x7fe00000) >> 20), itc_base);
435 }
436}
437
438/*
439 * Function to protect cache flushes from concurrent execution
440 * depends on MP software model chosen.
441 */
442
443void mt_cflush_lockdown(void)
444{
445#ifdef CONFIG_MIPS_MT_SMTC
446 void smtc_cflush_lockdown(void);
447
448 smtc_cflush_lockdown();
449#endif /* CONFIG_MIPS_MT_SMTC */
450 /* FILL IN VSMP and AP/SP VERSIONS HERE */
451}
452
453void mt_cflush_release(void)
454{
455#ifdef CONFIG_MIPS_MT_SMTC
456 void smtc_cflush_release(void);
457
458 smtc_cflush_release();
459#endif /* CONFIG_MIPS_MT_SMTC */
460 /* FILL IN VSMP and AP/SP VERSIONS HERE */
461}
Ralf Baechle27a3bba2007-02-07 13:48:59 +0000462
463struct class *mt_class;
464
465static int __init mt_init(void)
466{
467 struct class *mtc;
468
469 mtc = class_create(THIS_MODULE, "mt");
470 if (IS_ERR(mtc))
471 return PTR_ERR(mtc);
472
473 mt_class = mtc;
474
475 return 0;
476}
477
478subsys_initcall(mt_init);