smp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include <linux/types.h>
  2. #include <asm/delay.h>
  3. #include <irq.h>
  4. #include <hwregs/intr_vect.h>
  5. #include <hwregs/intr_vect_defs.h>
  6. #include <asm/tlbflush.h>
  7. #include <asm/mmu_context.h>
  8. #include <hwregs/asm/mmu_defs_asm.h>
  9. #include <hwregs/supp_reg.h>
  10. #include <linux/atomic.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/timex.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #define IPI_SCHEDULE 1
  20. #define IPI_CALL 2
  21. #define IPI_FLUSH_TLB 4
  22. #define IPI_BOOT 8
  23. #define FLUSH_ALL (void*)0xffffffff
  24. /* Vector of locks used for various atomic operations */
  25. spinlock_t cris_atomic_locks[] = {
  26. [0 ... LOCK_COUNT - 1] = __SPIN_LOCK_UNLOCKED(cris_atomic_locks)
  27. };
  28. /* CPU masks */
  29. cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
  30. EXPORT_SYMBOL(phys_cpu_present_map);
  31. /* Variables used during SMP boot */
  32. volatile int cpu_now_booting = 0;
  33. volatile struct thread_info *smp_init_current_idle_thread;
  34. /* Variables used during IPI */
  35. static DEFINE_SPINLOCK(call_lock);
  36. static DEFINE_SPINLOCK(tlbstate_lock);
  37. struct call_data_struct {
  38. void (*func) (void *info);
  39. void *info;
  40. int wait;
  41. };
  42. static struct call_data_struct * call_data;
  43. static struct mm_struct* flush_mm;
  44. static struct vm_area_struct* flush_vma;
  45. static unsigned long flush_addr;
  46. /* Mode registers */
  47. static unsigned long irq_regs[NR_CPUS] = {
  48. regi_irq,
  49. regi_irq2
  50. };
  51. static irqreturn_t crisv32_ipi_interrupt(int irq, void *dev_id);
  52. static int send_ipi(int vector, int wait, cpumask_t cpu_mask);
  53. static struct irqaction irq_ipi = {
  54. .handler = crisv32_ipi_interrupt,
  55. .flags = IRQF_DISABLED,
  56. .name = "ipi",
  57. };
  58. extern void cris_mmu_init(void);
  59. extern void cris_timer_init(void);
  60. /* SMP initialization */
  61. void __init smp_prepare_cpus(unsigned int max_cpus)
  62. {
  63. int i;
  64. /* From now on we can expect IPIs so set them up */
  65. setup_irq(IPI_INTR_VECT, &irq_ipi);
  66. /* Mark all possible CPUs as present */
  67. for (i = 0; i < max_cpus; i++)
  68. cpumask_set_cpu(i, &phys_cpu_present_map);
  69. }
  70. void __devinit smp_prepare_boot_cpu(void)
  71. {
  72. /* PGD pointer has moved after per_cpu initialization so
  73. * update the MMU.
  74. */
  75. pgd_t **pgd;
  76. pgd = (pgd_t**)&per_cpu(current_pgd, smp_processor_id());
  77. SUPP_BANK_SEL(1);
  78. SUPP_REG_WR(RW_MM_TLB_PGD, pgd);
  79. SUPP_BANK_SEL(2);
  80. SUPP_REG_WR(RW_MM_TLB_PGD, pgd);
  81. set_cpu_online(0, true);
  82. cpumask_set_cpu(0, &phys_cpu_present_map);
  83. set_cpu_possible(0, true);
  84. }
  85. void __init smp_cpus_done(unsigned int max_cpus)
  86. {
  87. }
  88. /* Bring one cpu online.*/
  89. static int __init
  90. smp_boot_one_cpu(int cpuid)
  91. {
  92. unsigned timeout;
  93. struct task_struct *idle;
  94. cpumask_t cpu_mask;
  95. cpumask_clear(&cpu_mask);
  96. idle = fork_idle(cpuid);
  97. if (IS_ERR(idle))
  98. panic("SMP: fork failed for CPU:%d", cpuid);
  99. task_thread_info(idle)->cpu = cpuid;
  100. /* Information to the CPU that is about to boot */
  101. smp_init_current_idle_thread = task_thread_info(idle);
  102. cpu_now_booting = cpuid;
  103. /* Kick it */
  104. set_cpu_online(cpuid, true);
  105. cpumask_set_cpu(cpuid, &cpu_mask);
  106. send_ipi(IPI_BOOT, 0, cpu_mask);
  107. set_cpu_online(cpuid, false);
  108. /* Wait for CPU to come online */
  109. for (timeout = 0; timeout < 10000; timeout++) {
  110. if(cpu_online(cpuid)) {
  111. cpu_now_booting = 0;
  112. smp_init_current_idle_thread = NULL;
  113. return 0; /* CPU online */
  114. }
  115. udelay(100);
  116. barrier();
  117. }
  118. put_task_struct(idle);
  119. idle = NULL;
  120. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  121. return -1;
  122. }
  123. /* Secondary CPUs starts using C here. Here we need to setup CPU
  124. * specific stuff such as the local timer and the MMU. */
  125. void __init smp_callin(void)
  126. {
  127. extern void cpu_idle(void);
  128. int cpu = cpu_now_booting;
  129. reg_intr_vect_rw_mask vect_mask = {0};
  130. /* Initialise the idle task for this CPU */
  131. atomic_inc(&init_mm.mm_count);
  132. current->active_mm = &init_mm;
  133. /* Set up MMU */
  134. cris_mmu_init();
  135. __flush_tlb_all();
  136. /* Setup local timer. */
  137. cris_timer_init();
  138. /* Enable IRQ and idle */
  139. REG_WR(intr_vect, irq_regs[cpu], rw_mask, vect_mask);
  140. crisv32_unmask_irq(IPI_INTR_VECT);
  141. crisv32_unmask_irq(TIMER0_INTR_VECT);
  142. preempt_disable();
  143. notify_cpu_starting(cpu);
  144. local_irq_enable();
  145. set_cpu_online(cpu, true);
  146. cpu_idle();
  147. }
  148. /* Stop execution on this CPU.*/
  149. void stop_this_cpu(void* dummy)
  150. {
  151. local_irq_disable();
  152. asm volatile("halt");
  153. }
  154. /* Other calls */
  155. void smp_send_stop(void)
  156. {
  157. smp_call_function(stop_this_cpu, NULL, 0);
  158. }
  159. int setup_profiling_timer(unsigned int multiplier)
  160. {
  161. return -EINVAL;
  162. }
  163. /* cache_decay_ticks is used by the scheduler to decide if a process
  164. * is "hot" on one CPU. A higher value means a higher penalty to move
  165. * a process to another CPU. Our cache is rather small so we report
  166. * 1 tick.
  167. */
  168. unsigned long cache_decay_ticks = 1;
  169. int __cpuinit __cpu_up(unsigned int cpu)
  170. {
  171. smp_boot_one_cpu(cpu);
  172. return cpu_online(cpu) ? 0 : -ENOSYS;
  173. }
  174. void smp_send_reschedule(int cpu)
  175. {
  176. cpumask_t cpu_mask;
  177. cpumask_clear(&cpu_mask);
  178. cpumask_set_cpu(cpu, &cpu_mask);
  179. send_ipi(IPI_SCHEDULE, 0, cpu_mask);
  180. }
  181. /* TLB flushing
  182. *
  183. * Flush needs to be done on the local CPU and on any other CPU that
  184. * may have the same mapping. The mm->cpu_vm_mask is used to keep track
  185. * of which CPUs that a specific process has been executed on.
  186. */
  187. void flush_tlb_common(struct mm_struct* mm, struct vm_area_struct* vma, unsigned long addr)
  188. {
  189. unsigned long flags;
  190. cpumask_t cpu_mask;
  191. spin_lock_irqsave(&tlbstate_lock, flags);
  192. cpu_mask = (mm == FLUSH_ALL ? cpu_all_mask : *mm_cpumask(mm));
  193. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  194. flush_mm = mm;
  195. flush_vma = vma;
  196. flush_addr = addr;
  197. send_ipi(IPI_FLUSH_TLB, 1, cpu_mask);
  198. spin_unlock_irqrestore(&tlbstate_lock, flags);
  199. }
  200. void flush_tlb_all(void)
  201. {
  202. __flush_tlb_all();
  203. flush_tlb_common(FLUSH_ALL, FLUSH_ALL, 0);
  204. }
  205. void flush_tlb_mm(struct mm_struct *mm)
  206. {
  207. __flush_tlb_mm(mm);
  208. flush_tlb_common(mm, FLUSH_ALL, 0);
  209. /* No more mappings in other CPUs */
  210. cpumask_clear(mm_cpumask(mm));
  211. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  212. }
  213. void flush_tlb_page(struct vm_area_struct *vma,
  214. unsigned long addr)
  215. {
  216. __flush_tlb_page(vma, addr);
  217. flush_tlb_common(vma->vm_mm, vma, addr);
  218. }
  219. /* Inter processor interrupts
  220. *
  221. * The IPIs are used for:
  222. * * Force a schedule on a CPU
  223. * * FLush TLB on other CPUs
  224. * * Call a function on other CPUs
  225. */
  226. int send_ipi(int vector, int wait, cpumask_t cpu_mask)
  227. {
  228. int i = 0;
  229. reg_intr_vect_rw_ipi ipi = REG_RD(intr_vect, irq_regs[i], rw_ipi);
  230. int ret = 0;
  231. /* Calculate CPUs to send to. */
  232. cpumask_and(&cpu_mask, &cpu_mask, cpu_online_mask);
  233. /* Send the IPI. */
  234. for_each_cpu(i, &cpu_mask)
  235. {
  236. ipi.vector |= vector;
  237. REG_WR(intr_vect, irq_regs[i], rw_ipi, ipi);
  238. }
  239. /* Wait for IPI to finish on other CPUS */
  240. if (wait) {
  241. for_each_cpu(i, &cpu_mask) {
  242. int j;
  243. for (j = 0 ; j < 1000; j++) {
  244. ipi = REG_RD(intr_vect, irq_regs[i], rw_ipi);
  245. if (!ipi.vector)
  246. break;
  247. udelay(100);
  248. }
  249. /* Timeout? */
  250. if (ipi.vector) {
  251. printk("SMP call timeout from %d to %d\n", smp_processor_id(), i);
  252. ret = -ETIMEDOUT;
  253. dump_stack();
  254. }
  255. }
  256. }
  257. return ret;
  258. }
  259. /*
  260. * You must not call this function with disabled interrupts or from a
  261. * hardware interrupt handler or from a bottom half handler.
  262. */
  263. int smp_call_function(void (*func)(void *info), void *info, int wait)
  264. {
  265. cpumask_t cpu_mask;
  266. struct call_data_struct data;
  267. int ret;
  268. cpumask_setall(&cpu_mask);
  269. cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
  270. WARN_ON(irqs_disabled());
  271. data.func = func;
  272. data.info = info;
  273. data.wait = wait;
  274. spin_lock(&call_lock);
  275. call_data = &data;
  276. ret = send_ipi(IPI_CALL, wait, cpu_mask);
  277. spin_unlock(&call_lock);
  278. return ret;
  279. }
  280. irqreturn_t crisv32_ipi_interrupt(int irq, void *dev_id)
  281. {
  282. void (*func) (void *info) = call_data->func;
  283. void *info = call_data->info;
  284. reg_intr_vect_rw_ipi ipi;
  285. ipi = REG_RD(intr_vect, irq_regs[smp_processor_id()], rw_ipi);
  286. if (ipi.vector & IPI_SCHEDULE) {
  287. scheduler_ipi();
  288. }
  289. if (ipi.vector & IPI_CALL) {
  290. func(info);
  291. }
  292. if (ipi.vector & IPI_FLUSH_TLB) {
  293. if (flush_mm == FLUSH_ALL)
  294. __flush_tlb_all();
  295. else if (flush_vma == FLUSH_ALL)
  296. __flush_tlb_mm(flush_mm);
  297. else
  298. __flush_tlb_page(flush_vma, flush_addr);
  299. }
  300. ipi.vector = 0;
  301. REG_WR(intr_vect, irq_regs[smp_processor_id()], rw_ipi, ipi);
  302. return IRQ_HANDLED;
  303. }