2 * General MIPS MT support routines, usable in AP/SP, SMVP, or SMTC kernels
3 * Copyright (C) 2005 Mips Technologies, Inc
6 #include <linux/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/cpumask.h>
9 #include <linux/interrupt.h>
12 #include <asm/processor.h>
13 #include <asm/atomic.h>
14 #include <asm/system.h>
15 #include <asm/hardirq.h>
16 #include <asm/mmu_context.h>
18 #include <asm/mipsmtregs.h>
19 #include <asm/r4kcache.h>
20 #include <asm/cacheflush.h>
23 * CPU mask used to set process affinity for MT VPEs/TCs with FPUs
26 cpumask_t mt_fpu_cpumask;
28 #ifdef CONFIG_MIPS_MT_FPAFF
30 #include <linux/cpu.h>
31 #include <linux/delay.h>
32 #include <asm/uaccess.h>
34 unsigned long mt_fpemul_threshold = 0;
37 * Replacement functions for the sys_sched_setaffinity() and
38 * sys_sched_getaffinity() system calls, so that we can integrate
39 * FPU affinity with the user's requested processor affinity.
40 * This code is 98% identical with the sys_sched_setaffinity()
41 * and sys_sched_getaffinity() system calls, and should be
42 * updated when kernel/sched.c changes.
46 * find_process_by_pid - find a process with a matching PID value.
47 * used in sys_sched_set/getaffinity() in kernel/sched.c, so
50 static inline task_t *find_process_by_pid(pid_t pid)
52 return pid ? find_task_by_pid(pid) : current;
57 * mipsmt_sys_sched_setaffinity - set the cpu affinity of a process
59 asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
60 unsigned long __user *user_mask_ptr)
63 cpumask_t effective_mask;
67 if (len < sizeof(new_mask))
70 if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
74 read_lock(&tasklist_lock);
76 p = find_process_by_pid(pid);
78 read_unlock(&tasklist_lock);
84 * It is not safe to call set_cpus_allowed with the
85 * tasklist_lock held. We will bump the task_struct's
86 * usage count and drop tasklist_lock before invoking
92 if ((current->euid != p->euid) && (current->euid != p->uid) &&
93 !capable(CAP_SYS_NICE)) {
94 read_unlock(&tasklist_lock);
98 /* Record new user-specified CPU set for future reference */
99 p->thread.user_cpus_allowed = new_mask;
101 /* Unlock the task list */
102 read_unlock(&tasklist_lock);
104 /* Compute new global allowed CPU set if necessary */
105 if( (p->thread.mflags & MF_FPUBOUND)
106 && cpus_intersects(new_mask, mt_fpu_cpumask)) {
107 cpus_and(effective_mask, new_mask, mt_fpu_cpumask);
108 retval = set_cpus_allowed(p, effective_mask);
110 p->thread.mflags &= ~MF_FPUBOUND;
111 retval = set_cpus_allowed(p, new_mask);
117 unlock_cpu_hotplug();
122 * mipsmt_sys_sched_getaffinity - get the cpu affinity of a process
124 asmlinkage long mipsmt_sys_sched_getaffinity(pid_t pid, unsigned int len,
125 unsigned long __user *user_mask_ptr)
127 unsigned int real_len;
132 real_len = sizeof(mask);
137 read_lock(&tasklist_lock);
140 p = find_process_by_pid(pid);
146 cpus_and(mask, p->thread.user_cpus_allowed, cpu_possible_map);
149 read_unlock(&tasklist_lock);
150 unlock_cpu_hotplug();
153 if (copy_to_user(user_mask_ptr, &mask, real_len))
158 #endif /* CONFIG_MIPS_MT_FPAFF */
161 * Dump new MIPS MT state for the core. Does not leave TCs halted.
162 * Takes an argument which taken to be a pre-call MVPControl value.
165 void mips_mt_regdump(unsigned long mvpctl)
168 unsigned long vpflags;
169 unsigned long mvpconf0;
174 unsigned long haltval;
175 unsigned long tcstatval;
176 #ifdef CONFIG_MIPS_MT_SMTC
177 void smtc_soft_dump(void);
178 #endif /* CONFIG_MIPT_MT_SMTC */
180 local_irq_save(flags);
182 printk("=== MIPS MT State Dump ===\n");
183 printk("-- Global State --\n");
184 printk(" MVPControl Passed: %08lx\n", mvpctl);
185 printk(" MVPControl Read: %08lx\n", vpflags);
186 printk(" MVPConf0 : %08lx\n", (mvpconf0 = read_c0_mvpconf0()));
187 nvpe = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
188 ntc = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
189 printk("-- per-VPE State --\n");
190 for(i = 0; i < nvpe; i++) {
191 for(tc = 0; tc < ntc; tc++) {
193 if((read_tc_c0_tcbind() & TCBIND_CURVPE) == i) {
194 printk(" VPE %d\n", i);
195 printk(" VPEControl : %08lx\n", read_vpe_c0_vpecontrol());
196 printk(" VPEConf0 : %08lx\n", read_vpe_c0_vpeconf0());
197 printk(" VPE%d.Status : %08lx\n",
198 i, read_vpe_c0_status());
199 printk(" VPE%d.EPC : %08lx\n", i, read_vpe_c0_epc());
200 printk(" VPE%d.Cause : %08lx\n", i, read_vpe_c0_cause());
201 printk(" VPE%d.Config7 : %08lx\n",
202 i, read_vpe_c0_config7());
203 break; /* Next VPE */
207 printk("-- per-TC State --\n");
208 for(tc = 0; tc < ntc; tc++) {
210 if(read_tc_c0_tcbind() == read_c0_tcbind()) {
211 /* Are we dumping ourself? */
212 haltval = 0; /* Then we're not halted, and mustn't be */
213 tcstatval = flags; /* And pre-dump TCStatus is flags */
214 printk(" TC %d (current TC with VPE EPC above)\n", tc);
216 haltval = read_tc_c0_tchalt();
217 write_tc_c0_tchalt(1);
218 tcstatval = read_tc_c0_tcstatus();
219 printk(" TC %d\n", tc);
221 printk(" TCStatus : %08lx\n", tcstatval);
222 printk(" TCBind : %08lx\n", read_tc_c0_tcbind());
223 printk(" TCRestart : %08lx\n", read_tc_c0_tcrestart());
224 printk(" TCHalt : %08lx\n", haltval);
225 printk(" TCContext : %08lx\n", read_tc_c0_tccontext());
227 write_tc_c0_tchalt(0);
229 #ifdef CONFIG_MIPS_MT_SMTC
231 #endif /* CONFIG_MIPT_MT_SMTC */
232 printk("===========================\n");
234 local_irq_restore(flags);
237 static int mt_opt_norps = 0;
238 static int mt_opt_rpsctl = -1;
239 static int mt_opt_nblsu = -1;
240 static int mt_opt_forceconfig7 = 0;
241 static int mt_opt_config7 = -1;
243 static int __init rps_disable(char *s)
248 __setup("norps", rps_disable);
250 static int __init rpsctl_set(char *str)
252 get_option(&str, &mt_opt_rpsctl);
255 __setup("rpsctl=", rpsctl_set);
257 static int __init nblsu_set(char *str)
259 get_option(&str, &mt_opt_nblsu);
262 __setup("nblsu=", nblsu_set);
264 static int __init config7_set(char *str)
266 get_option(&str, &mt_opt_config7);
267 mt_opt_forceconfig7 = 1;
270 __setup("config7=", config7_set);
272 /* Experimental cache flush control parameters that should go away some day */
273 int mt_protiflush = 0;
274 int mt_protdflush = 0;
275 int mt_n_iflushes = 1;
276 int mt_n_dflushes = 1;
278 static int __init set_protiflush(char *s)
283 __setup("protiflush", set_protiflush);
285 static int __init set_protdflush(char *s)
290 __setup("protdflush", set_protdflush);
292 static int __init niflush(char *s)
294 get_option(&s, &mt_n_iflushes);
297 __setup("niflush=", niflush);
299 static int __init ndflush(char *s)
301 get_option(&s, &mt_n_dflushes);
304 __setup("ndflush=", ndflush);
305 #ifdef CONFIG_MIPS_MT_FPAFF
306 static int fpaff_threshold = -1;
308 static int __init fpaff_thresh(char *str)
310 get_option(&str, &fpaff_threshold);
314 __setup("fpaff=", fpaff_thresh);
315 #endif /* CONFIG_MIPS_MT_FPAFF */
317 static unsigned int itc_base = 0;
319 static int __init set_itc_base(char *str)
321 get_option(&str, &itc_base);
325 __setup("itcbase=", set_itc_base);
327 void mips_mt_set_cpuoptions(void)
329 unsigned int oconfig7 = read_c0_config7();
330 unsigned int nconfig7 = oconfig7;
333 printk("\"norps\" option deprectated: use \"rpsctl=\"\n");
335 if (mt_opt_rpsctl >= 0) {
336 printk("34K return prediction stack override set to %d.\n",
339 nconfig7 |= (1 << 2);
341 nconfig7 &= ~(1 << 2);
343 if (mt_opt_nblsu >= 0) {
344 printk("34K ALU/LSU sync override set to %d.\n", mt_opt_nblsu);
346 nconfig7 |= (1 << 5);
348 nconfig7 &= ~(1 << 5);
350 if (mt_opt_forceconfig7) {
351 printk("CP0.Config7 forced to 0x%08x.\n", mt_opt_config7);
352 nconfig7 = mt_opt_config7;
354 if (oconfig7 != nconfig7) {
355 __asm__ __volatile("sync");
356 write_c0_config7(nconfig7);
358 printk("Config7: 0x%08x\n", read_c0_config7());
361 /* Report Cache management debug options */
363 printk("I-cache flushes single-threaded\n");
365 printk("D-cache flushes single-threaded\n");
366 if (mt_n_iflushes != 1)
367 printk("I-Cache Flushes Repeated %d times\n", mt_n_iflushes);
368 if (mt_n_dflushes != 1)
369 printk("D-Cache Flushes Repeated %d times\n", mt_n_dflushes);
371 #ifdef CONFIG_MIPS_MT_FPAFF
372 /* FPU Use Factor empirically derived from experiments on 34K */
373 #define FPUSEFACTOR 333
375 if (fpaff_threshold >= 0) {
376 mt_fpemul_threshold = fpaff_threshold;
378 mt_fpemul_threshold =
379 (FPUSEFACTOR * (loops_per_jiffy/(500000/HZ))) / HZ;
381 printk("FPU Affinity set after %ld emulations\n",
382 mt_fpemul_threshold);
383 #endif /* CONFIG_MIPS_MT_FPAFF */
387 * Configure ITC mapping. This code is very
388 * specific to the 34K core family, which uses
389 * a special mode bit ("ITC") in the ErrCtl
390 * register to enable access to ITC control
391 * registers via cache "tag" operations.
393 unsigned long ectlval;
394 unsigned long itcblkgrn;
396 /* ErrCtl register is known as "ecc" to Linux */
397 ectlval = read_c0_ecc();
398 write_c0_ecc(ectlval | (0x1 << 26));
400 #define INDEX_0 (0x80000000)
401 #define INDEX_8 (0x80000008)
402 /* Read "cache tag" for Dcache pseudo-index 8 */
403 cache_op(Index_Load_Tag_D, INDEX_8);
405 itcblkgrn = read_c0_dtaglo();
406 itcblkgrn &= 0xfffe0000;
407 /* Set for 128 byte pitch of ITC cells */
408 itcblkgrn |= 0x00000c00;
409 /* Stage in Tag register */
410 write_c0_dtaglo(itcblkgrn);
412 /* Write out to ITU with CACHE op */
413 cache_op(Index_Store_Tag_D, INDEX_8);
414 /* Now set base address, and turn ITC on with 0x1 bit */
415 write_c0_dtaglo((itc_base & 0xfffffc00) | 0x1 );
417 /* Write out to ITU with CACHE op */
418 cache_op(Index_Store_Tag_D, INDEX_0);
419 write_c0_ecc(ectlval);
421 printk("Mapped %ld ITC cells starting at 0x%08x\n",
422 ((itcblkgrn & 0x7fe00000) >> 20), itc_base);
427 * Function to protect cache flushes from concurrent execution
428 * depends on MP software model chosen.
431 void mt_cflush_lockdown(void)
433 #ifdef CONFIG_MIPS_MT_SMTC
434 void smtc_cflush_lockdown(void);
436 smtc_cflush_lockdown();
437 #endif /* CONFIG_MIPS_MT_SMTC */
438 /* FILL IN VSMP and AP/SP VERSIONS HERE */
441 void mt_cflush_release(void)
443 #ifdef CONFIG_MIPS_MT_SMTC
444 void smtc_cflush_release(void);
446 smtc_cflush_release();
447 #endif /* CONFIG_MIPS_MT_SMTC */
448 /* FILL IN VSMP and AP/SP VERSIONS HERE */