mips-mt.c 11 KB

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