smp.c 7.8 KB

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