smp.c 8.0 KB

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