smp.c 8.2 KB

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