2 * General MIPS MT support routines, usable in AP/SP, SMVP, or SMTC kernels
3 * Copyright (C) 2005 Mips Technologies, Inc
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/cpumask.h>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/security.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>
21 #include <asm/mipsmtregs.h>
22 #include <asm/r4kcache.h>
23 #include <asm/cacheflush.h>
26 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
29 cpumask_t mt_fpu_cpumask
;
31 #ifdef CONFIG_MIPS_MT_FPAFF
33 #include <linux/cpu.h>
34 #include <linux/delay.h>
35 #include <asm/uaccess.h>
37 unsigned long mt_fpemul_threshold
= 0;
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.
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
53 static inline struct task_struct
*find_process_by_pid(pid_t pid
)
55 return pid
? find_task_by_pid(pid
) : current
;
60 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
62 asmlinkage
long mipsmt_sys_sched_setaffinity(pid_t pid
, unsigned int len
,
63 unsigned long __user
*user_mask_ptr
)
66 cpumask_t effective_mask
;
68 struct task_struct
*p
;
70 if (len
< sizeof(new_mask
))
73 if (copy_from_user(&new_mask
, user_mask_ptr
, sizeof(new_mask
)))
77 read_lock(&tasklist_lock
);
79 p
= find_process_by_pid(pid
);
81 read_unlock(&tasklist_lock
);
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
95 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
96 !capable(CAP_SYS_NICE
)) {
97 read_unlock(&tasklist_lock
);
101 retval
= security_task_setscheduler(p
, 0, NULL
);
105 /* Record new user-specified CPU set for future reference */
106 p
->thread
.user_cpus_allowed
= new_mask
;
108 /* Unlock the task list */
109 read_unlock(&tasklist_lock
);
111 /* Compute new global allowed CPU set if necessary */
112 if( (p
->thread
.mflags
& MF_FPUBOUND
)
113 && 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
);
117 p
->thread
.mflags
&= ~MF_FPUBOUND
;
118 retval
= set_cpus_allowed(p
, new_mask
);
124 unlock_cpu_hotplug();
129 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
131 asmlinkage
long mipsmt_sys_sched_getaffinity(pid_t pid
, unsigned int len
,
132 unsigned long __user
*user_mask_ptr
)
134 unsigned int real_len
;
137 struct task_struct
*p
;
139 real_len
= sizeof(mask
);
144 read_lock(&tasklist_lock
);
147 p
= find_process_by_pid(pid
);
150 retval
= security_task_getscheduler(p
);
154 cpus_and(mask
, p
->thread
.user_cpus_allowed
, cpu_possible_map
);
157 read_unlock(&tasklist_lock
);
158 unlock_cpu_hotplug();
161 if (copy_to_user(user_mask_ptr
, &mask
, real_len
))
166 #endif /* CONFIG_MIPS_MT_FPAFF */
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.
173 void mips_mt_regdump(unsigned long mvpctl
)
176 unsigned long vpflags
;
177 unsigned long mvpconf0
;
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 */
188 local_irq_save(flags
);
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");
198 for(i
= 0; i
< nvpe
; i
++) {
199 for(tc
= 0; tc
< ntc
; tc
++) {
201 if((read_tc_c0_tcbind() & TCBIND_CURVPE
) == i
) {
202 printk(" VPE %d\n", i
);
203 printk(" VPEControl : %08lx\n", read_vpe_c0_vpecontrol());
204 printk(" VPEConf0 : %08lx\n", read_vpe_c0_vpeconf0());
205 printk(" VPE%d.Status : %08lx\n",
206 i
, read_vpe_c0_status());
207 printk(" VPE%d.EPC : %08lx\n", i
, read_vpe_c0_epc());
208 printk(" VPE%d.Cause : %08lx\n", i
, read_vpe_c0_cause());
209 printk(" VPE%d.Config7 : %08lx\n",
210 i
, read_vpe_c0_config7());
211 break; /* Next VPE */
215 printk("-- per-TC State --\n");
216 for(tc
= 0; tc
< ntc
; tc
++) {
218 if(read_tc_c0_tcbind() == read_c0_tcbind()) {
219 /* Are we dumping ourself? */
220 haltval
= 0; /* Then we're not halted, and mustn't be */
221 tcstatval
= flags
; /* And pre-dump TCStatus is flags */
222 printk(" TC %d (current TC with VPE EPC above)\n", tc
);
224 haltval
= read_tc_c0_tchalt();
225 write_tc_c0_tchalt(1);
226 tcstatval
= read_tc_c0_tcstatus();
227 printk(" TC %d\n", tc
);
229 printk(" TCStatus : %08lx\n", tcstatval
);
230 printk(" TCBind : %08lx\n", read_tc_c0_tcbind());
231 printk(" TCRestart : %08lx\n", read_tc_c0_tcrestart());
232 printk(" TCHalt : %08lx\n", haltval
);
233 printk(" TCContext : %08lx\n", read_tc_c0_tccontext());
235 write_tc_c0_tchalt(0);
237 #ifdef CONFIG_MIPS_MT_SMTC
239 #endif /* CONFIG_MIPT_MT_SMTC */
240 printk("===========================\n");
242 local_irq_restore(flags
);
245 static int mt_opt_norps
= 0;
246 static int mt_opt_rpsctl
= -1;
247 static int mt_opt_nblsu
= -1;
248 static int mt_opt_forceconfig7
= 0;
249 static int mt_opt_config7
= -1;
251 static int __init
rps_disable(char *s
)
256 __setup("norps", rps_disable
);
258 static int __init
rpsctl_set(char *str
)
260 get_option(&str
, &mt_opt_rpsctl
);
263 __setup("rpsctl=", rpsctl_set
);
265 static int __init
nblsu_set(char *str
)
267 get_option(&str
, &mt_opt_nblsu
);
270 __setup("nblsu=", nblsu_set
);
272 static int __init
config7_set(char *str
)
274 get_option(&str
, &mt_opt_config7
);
275 mt_opt_forceconfig7
= 1;
278 __setup("config7=", config7_set
);
280 /* Experimental cache flush control parameters that should go away some day */
281 int mt_protiflush
= 0;
282 int mt_protdflush
= 0;
283 int mt_n_iflushes
= 1;
284 int mt_n_dflushes
= 1;
286 static int __init
set_protiflush(char *s
)
291 __setup("protiflush", set_protiflush
);
293 static int __init
set_protdflush(char *s
)
298 __setup("protdflush", set_protdflush
);
300 static int __init
niflush(char *s
)
302 get_option(&s
, &mt_n_iflushes
);
305 __setup("niflush=", niflush
);
307 static int __init
ndflush(char *s
)
309 get_option(&s
, &mt_n_dflushes
);
312 __setup("ndflush=", ndflush
);
313 #ifdef CONFIG_MIPS_MT_FPAFF
314 static int fpaff_threshold
= -1;
316 static int __init
fpaff_thresh(char *str
)
318 get_option(&str
, &fpaff_threshold
);
322 __setup("fpaff=", fpaff_thresh
);
323 #endif /* CONFIG_MIPS_MT_FPAFF */
325 static unsigned int itc_base
= 0;
327 static int __init
set_itc_base(char *str
)
329 get_option(&str
, &itc_base
);
333 __setup("itcbase=", set_itc_base
);
335 void mips_mt_set_cpuoptions(void)
337 unsigned int oconfig7
= read_c0_config7();
338 unsigned int nconfig7
= oconfig7
;
341 printk("\"norps\" option deprectated: use \"rpsctl=\"\n");
343 if (mt_opt_rpsctl
>= 0) {
344 printk("34K return prediction stack override set to %d.\n",
347 nconfig7
|= (1 << 2);
349 nconfig7
&= ~(1 << 2);
351 if (mt_opt_nblsu
>= 0) {
352 printk("34K ALU/LSU sync override set to %d.\n", mt_opt_nblsu
);
354 nconfig7
|= (1 << 5);
356 nconfig7
&= ~(1 << 5);
358 if (mt_opt_forceconfig7
) {
359 printk("CP0.Config7 forced to 0x%08x.\n", mt_opt_config7
);
360 nconfig7
= mt_opt_config7
;
362 if (oconfig7
!= nconfig7
) {
363 __asm__
__volatile("sync");
364 write_c0_config7(nconfig7
);
366 printk("Config7: 0x%08x\n", read_c0_config7());
369 /* Report Cache management debug options */
371 printk("I-cache flushes single-threaded\n");
373 printk("D-cache flushes single-threaded\n");
374 if (mt_n_iflushes
!= 1)
375 printk("I-Cache Flushes Repeated %d times\n", mt_n_iflushes
);
376 if (mt_n_dflushes
!= 1)
377 printk("D-Cache Flushes Repeated %d times\n", mt_n_dflushes
);
379 #ifdef CONFIG_MIPS_MT_FPAFF
380 /* FPU Use Factor empirically derived from experiments on 34K */
381 #define FPUSEFACTOR 333
383 if (fpaff_threshold
>= 0) {
384 mt_fpemul_threshold
= fpaff_threshold
;
386 mt_fpemul_threshold
=
387 (FPUSEFACTOR
* (loops_per_jiffy
/(500000/HZ
))) / HZ
;
389 printk("FPU Affinity set after %ld emulations\n",
390 mt_fpemul_threshold
);
391 #endif /* CONFIG_MIPS_MT_FPAFF */
395 * Configure ITC mapping. This code is very
396 * specific to the 34K core family, which uses
397 * a special mode bit ("ITC") in the ErrCtl
398 * register to enable access to ITC control
399 * registers via cache "tag" operations.
401 unsigned long ectlval
;
402 unsigned long itcblkgrn
;
404 /* ErrCtl register is known as "ecc" to Linux */
405 ectlval
= read_c0_ecc();
406 write_c0_ecc(ectlval
| (0x1 << 26));
408 #define INDEX_0 (0x80000000)
409 #define INDEX_8 (0x80000008)
410 /* Read "cache tag" for Dcache pseudo-index 8 */
411 cache_op(Index_Load_Tag_D
, INDEX_8
);
413 itcblkgrn
= read_c0_dtaglo();
414 itcblkgrn
&= 0xfffe0000;
415 /* Set for 128 byte pitch of ITC cells */
416 itcblkgrn
|= 0x00000c00;
417 /* Stage in Tag register */
418 write_c0_dtaglo(itcblkgrn
);
420 /* Write out to ITU with CACHE op */
421 cache_op(Index_Store_Tag_D
, INDEX_8
);
422 /* Now set base address, and turn ITC on with 0x1 bit */
423 write_c0_dtaglo((itc_base
& 0xfffffc00) | 0x1 );
425 /* Write out to ITU with CACHE op */
426 cache_op(Index_Store_Tag_D
, INDEX_0
);
427 write_c0_ecc(ectlval
);
429 printk("Mapped %ld ITC cells starting at 0x%08x\n",
430 ((itcblkgrn
& 0x7fe00000) >> 20), itc_base
);
435 * Function to protect cache flushes from concurrent execution
436 * depends on MP software model chosen.
439 void mt_cflush_lockdown(void)
441 #ifdef CONFIG_MIPS_MT_SMTC
442 void smtc_cflush_lockdown(void);
444 smtc_cflush_lockdown();
445 #endif /* CONFIG_MIPS_MT_SMTC */
446 /* FILL IN VSMP and AP/SP VERSIONS HERE */
449 void mt_cflush_release(void)
451 #ifdef CONFIG_MIPS_MT_SMTC
452 void smtc_cflush_release(void);
454 smtc_cflush_release();
455 #endif /* CONFIG_MIPS_MT_SMTC */
456 /* FILL IN VSMP and AP/SP VERSIONS HERE */
459 struct class *mt_class
;
461 static int __init
mt_init(void)
465 mtc
= class_create(THIS_MODULE
, "mt");
474 subsys_initcall(mt_init
);