smp.c 8.0 KB

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