mips-mt.c 11 KB

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