mips-mt.c 11 KB

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