mips-mt.c 11 KB

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