smp.c 7.9 KB

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