]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/metag/kernel/smp.c
Merge tag 'metag-for-v3.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / arch / metag / kernel / smp.c
1 /*
2  *  Copyright (C) 2009,2010,2011 Imagination Technologies Ltd.
3  *
4  *  Copyright (C) 2002 ARM Limited, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/atomic.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/cache.h>
17 #include <linux/profile.h>
18 #include <linux/errno.h>
19 #include <linux/mm.h>
20 #include <linux/err.h>
21 #include <linux/cpu.h>
22 #include <linux/smp.h>
23 #include <linux/seq_file.h>
24 #include <linux/irq.h>
25 #include <linux/bootmem.h>
26
27 #include <asm/cacheflush.h>
28 #include <asm/cachepart.h>
29 #include <asm/core_reg.h>
30 #include <asm/cpu.h>
31 #include <asm/global_lock.h>
32 #include <asm/metag_mem.h>
33 #include <asm/mmu_context.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/processor.h>
37 #include <asm/setup.h>
38 #include <asm/tlbflush.h>
39 #include <asm/hwthread.h>
40 #include <asm/traps.h>
41
42 #define SYSC_DCPART(n)  (SYSC_DCPART0 + SYSC_xCPARTn_STRIDE * (n))
43 #define SYSC_ICPART(n)  (SYSC_ICPART0 + SYSC_xCPARTn_STRIDE * (n))
44
45 DECLARE_PER_CPU(PTBI, pTBI);
46
47 void *secondary_data_stack;
48
49 /*
50  * structures for inter-processor calls
51  * - A collection of single bit ipi messages.
52  */
53 struct ipi_data {
54         spinlock_t lock;
55         unsigned long ipi_count;
56         unsigned long bits;
57 };
58
59 static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
60         .lock   = __SPIN_LOCK_UNLOCKED(ipi_data.lock),
61 };
62
63 static DEFINE_SPINLOCK(boot_lock);
64
65 /*
66  * "thread" is assumed to be a valid Meta hardware thread ID.
67  */
68 int __cpuinit boot_secondary(unsigned int thread, struct task_struct *idle)
69 {
70         u32 val;
71
72         /*
73          * set synchronisation state between this boot processor
74          * and the secondary one
75          */
76         spin_lock(&boot_lock);
77
78         core_reg_write(TXUPC_ID, 0, thread, (unsigned int)secondary_startup);
79         core_reg_write(TXUPC_ID, 1, thread, 0);
80
81         /*
82          * Give the thread privilege (PSTAT) and clear potentially problematic
83          * bits in the process (namely ISTAT, CBMarker, CBMarkerI, LSM_STEP).
84          */
85         core_reg_write(TXUCT_ID, TXSTATUS_REGNUM, thread, TXSTATUS_PSTAT_BIT);
86
87         /* Clear the minim enable bit. */
88         val = core_reg_read(TXUCT_ID, TXPRIVEXT_REGNUM, thread);
89         core_reg_write(TXUCT_ID, TXPRIVEXT_REGNUM, thread, val & ~0x80);
90
91         /*
92          * set the ThreadEnable bit (0x1) in the TXENABLE register
93          * for the specified thread - off it goes!
94          */
95         val = core_reg_read(TXUCT_ID, TXENABLE_REGNUM, thread);
96         core_reg_write(TXUCT_ID, TXENABLE_REGNUM, thread, val | 0x1);
97
98         /*
99          * now the secondary core is starting up let it run its
100          * calibrations, then wait for it to finish
101          */
102         spin_unlock(&boot_lock);
103
104         return 0;
105 }
106
107 /**
108  * describe_cachepart_change: describe a change to cache partitions.
109  * @thread:     Hardware thread number.
110  * @label:      Label of cache type, e.g. "dcache" or "icache".
111  * @sz:         Total size of the cache.
112  * @old:        Old cache partition configuration (*CPART* register).
113  * @new:        New cache partition configuration (*CPART* register).
114  *
115  * If the cache partition has changed, prints a message to the log describing
116  * those changes.
117  */
118 static __cpuinit void describe_cachepart_change(unsigned int thread,
119                                                 const char *label,
120                                                 unsigned int sz,
121                                                 unsigned int old,
122                                                 unsigned int new)
123 {
124         unsigned int lor1, land1, gor1, gand1;
125         unsigned int lor2, land2, gor2, gand2;
126         unsigned int diff = old ^ new;
127
128         if (!diff)
129                 return;
130
131         pr_info("Thread %d: %s partition changed:", thread, label);
132         if (diff & (SYSC_xCPARTL_OR_BITS | SYSC_xCPARTL_AND_BITS)) {
133                 lor1   = (old & SYSC_xCPARTL_OR_BITS)  >> SYSC_xCPARTL_OR_S;
134                 lor2   = (new & SYSC_xCPARTL_OR_BITS)  >> SYSC_xCPARTL_OR_S;
135                 land1  = (old & SYSC_xCPARTL_AND_BITS) >> SYSC_xCPARTL_AND_S;
136                 land2  = (new & SYSC_xCPARTL_AND_BITS) >> SYSC_xCPARTL_AND_S;
137                 pr_cont(" L:%#x+%#x->%#x+%#x",
138                         (lor1 * sz) >> 4,
139                         ((land1 + 1) * sz) >> 4,
140                         (lor2 * sz) >> 4,
141                         ((land2 + 1) * sz) >> 4);
142         }
143         if (diff & (SYSC_xCPARTG_OR_BITS | SYSC_xCPARTG_AND_BITS)) {
144                 gor1   = (old & SYSC_xCPARTG_OR_BITS)  >> SYSC_xCPARTG_OR_S;
145                 gor2   = (new & SYSC_xCPARTG_OR_BITS)  >> SYSC_xCPARTG_OR_S;
146                 gand1  = (old & SYSC_xCPARTG_AND_BITS) >> SYSC_xCPARTG_AND_S;
147                 gand2  = (new & SYSC_xCPARTG_AND_BITS) >> SYSC_xCPARTG_AND_S;
148                 pr_cont(" G:%#x+%#x->%#x+%#x",
149                         (gor1 * sz) >> 4,
150                         ((gand1 + 1) * sz) >> 4,
151                         (gor2 * sz) >> 4,
152                         ((gand2 + 1) * sz) >> 4);
153         }
154         if (diff & SYSC_CWRMODE_BIT)
155                 pr_cont(" %sWR",
156                         (new & SYSC_CWRMODE_BIT) ? "+" : "-");
157         if (diff & SYSC_DCPART_GCON_BIT)
158                 pr_cont(" %sGCOn",
159                         (new & SYSC_DCPART_GCON_BIT) ? "+" : "-");
160         pr_cont("\n");
161 }
162
163 /**
164  * setup_smp_cache: ensure cache coherency for new SMP thread.
165  * @thread:     New hardware thread number.
166  *
167  * Ensures that coherency is enabled and that the threads share the same cache
168  * partitions.
169  */
170 static __cpuinit void setup_smp_cache(unsigned int thread)
171 {
172         unsigned int this_thread, lflags;
173         unsigned int dcsz, dcpart_this, dcpart_old, dcpart_new;
174         unsigned int icsz, icpart_old, icpart_new;
175
176         /*
177          * Copy over the current thread's cache partition configuration to the
178          * new thread so that they share cache partitions.
179          */
180         __global_lock2(lflags);
181         this_thread = hard_processor_id();
182         /* Share dcache partition */
183         dcpart_this = metag_in32(SYSC_DCPART(this_thread));
184         dcpart_old = metag_in32(SYSC_DCPART(thread));
185         dcpart_new = dcpart_this;
186 #if PAGE_OFFSET < LINGLOBAL_BASE
187         /*
188          * For the local data cache to be coherent the threads must also have
189          * GCOn enabled.
190          */
191         dcpart_new |= SYSC_DCPART_GCON_BIT;
192         metag_out32(dcpart_new, SYSC_DCPART(this_thread));
193 #endif
194         metag_out32(dcpart_new, SYSC_DCPART(thread));
195         /* Share icache partition too */
196         icpart_new = metag_in32(SYSC_ICPART(this_thread));
197         icpart_old = metag_in32(SYSC_ICPART(thread));
198         metag_out32(icpart_new, SYSC_ICPART(thread));
199         __global_unlock2(lflags);
200
201         /*
202          * Log if the cache partitions were altered so the user is aware of any
203          * potential unintentional cache wastage.
204          */
205         dcsz = get_dcache_size();
206         icsz = get_dcache_size();
207         describe_cachepart_change(this_thread, "dcache", dcsz,
208                                   dcpart_this, dcpart_new);
209         describe_cachepart_change(thread, "dcache", dcsz,
210                                   dcpart_old, dcpart_new);
211         describe_cachepart_change(thread, "icache", icsz,
212                                   icpart_old, icpart_new);
213 }
214
215 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)
216 {
217         unsigned int thread = cpu_2_hwthread_id[cpu];
218         int ret;
219
220         load_pgd(swapper_pg_dir, thread);
221
222         flush_tlb_all();
223
224         setup_smp_cache(thread);
225
226         /*
227          * Tell the secondary CPU where to find its idle thread's stack.
228          */
229         secondary_data_stack = task_stack_page(idle);
230
231         wmb();
232
233         /*
234          * Now bring the CPU into our world.
235          */
236         ret = boot_secondary(thread, idle);
237         if (ret == 0) {
238                 unsigned long timeout;
239
240                 /*
241                  * CPU was successfully started, wait for it
242                  * to come online or time out.
243                  */
244                 timeout = jiffies + HZ;
245                 while (time_before(jiffies, timeout)) {
246                         if (cpu_online(cpu))
247                                 break;
248
249                         udelay(10);
250                         barrier();
251                 }
252
253                 if (!cpu_online(cpu))
254                         ret = -EIO;
255         }
256
257         secondary_data_stack = NULL;
258
259         if (ret) {
260                 pr_crit("CPU%u: processor failed to boot\n", cpu);
261
262                 /*
263                  * FIXME: We need to clean up the new idle thread. --rmk
264                  */
265         }
266
267         return ret;
268 }
269
270 #ifdef CONFIG_HOTPLUG_CPU
271 static DECLARE_COMPLETION(cpu_killed);
272
273 /*
274  * __cpu_disable runs on the processor to be shutdown.
275  */
276 int __cpuexit __cpu_disable(void)
277 {
278         unsigned int cpu = smp_processor_id();
279         struct task_struct *p;
280
281         /*
282          * Take this CPU offline.  Once we clear this, we can't return,
283          * and we must not schedule until we're ready to give up the cpu.
284          */
285         set_cpu_online(cpu, false);
286
287         /*
288          * OK - migrate IRQs away from this CPU
289          */
290         migrate_irqs();
291
292         /*
293          * Flush user cache and TLB mappings, and then remove this CPU
294          * from the vm mask set of all processes.
295          */
296         flush_cache_all();
297         local_flush_tlb_all();
298
299         read_lock(&tasklist_lock);
300         for_each_process(p) {
301                 if (p->mm)
302                         cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
303         }
304         read_unlock(&tasklist_lock);
305
306         return 0;
307 }
308
309 /*
310  * called on the thread which is asking for a CPU to be shutdown -
311  * waits until shutdown has completed, or it is timed out.
312  */
313 void __cpuexit __cpu_die(unsigned int cpu)
314 {
315         if (!wait_for_completion_timeout(&cpu_killed, msecs_to_jiffies(1)))
316                 pr_err("CPU%u: unable to kill\n", cpu);
317 }
318
319 /*
320  * Called from the idle thread for the CPU which has been shutdown.
321  *
322  * Note that we do not return from this function. If this cpu is
323  * brought online again it will need to run secondary_startup().
324  */
325 void __cpuexit cpu_die(void)
326 {
327         local_irq_disable();
328         idle_task_exit();
329
330         complete(&cpu_killed);
331
332         asm ("XOR       TXENABLE, D0Re0,D0Re0\n");
333 }
334 #endif /* CONFIG_HOTPLUG_CPU */
335
336 /*
337  * Called by both boot and secondaries to move global data into
338  * per-processor storage.
339  */
340 void __cpuinit smp_store_cpu_info(unsigned int cpuid)
341 {
342         struct cpuinfo_metag *cpu_info = &per_cpu(cpu_data, cpuid);
343
344         cpu_info->loops_per_jiffy = loops_per_jiffy;
345 }
346
347 /*
348  * This is the secondary CPU boot entry.  We're using this CPUs
349  * idle thread stack and the global page tables.
350  */
351 asmlinkage void secondary_start_kernel(void)
352 {
353         struct mm_struct *mm = &init_mm;
354         unsigned int cpu = smp_processor_id();
355
356         /*
357          * All kernel threads share the same mm context; grab a
358          * reference and switch to it.
359          */
360         atomic_inc(&mm->mm_users);
361         atomic_inc(&mm->mm_count);
362         current->active_mm = mm;
363         cpumask_set_cpu(cpu, mm_cpumask(mm));
364         enter_lazy_tlb(mm, current);
365         local_flush_tlb_all();
366
367         /*
368          * TODO: Some day it might be useful for each Linux CPU to
369          * have its own TBI structure. That would allow each Linux CPU
370          * to run different interrupt handlers for the same IRQ
371          * number.
372          *
373          * For now, simply copying the pointer to the boot CPU's TBI
374          * structure is sufficient because we always want to run the
375          * same interrupt handler whatever CPU takes the interrupt.
376          */
377         per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
378
379         if (!per_cpu(pTBI, cpu))
380                 panic("No TBI found!");
381
382         per_cpu_trap_init(cpu);
383
384         preempt_disable();
385
386         setup_priv();
387
388         /*
389          * Enable local interrupts.
390          */
391         tbi_startup_interrupt(TBID_SIGNUM_TRT);
392         notify_cpu_starting(cpu);
393         local_irq_enable();
394
395         pr_info("CPU%u (thread %u): Booted secondary processor\n",
396                 cpu, cpu_2_hwthread_id[cpu]);
397
398         calibrate_delay();
399         smp_store_cpu_info(cpu);
400
401         /*
402          * OK, now it's safe to let the boot CPU continue
403          */
404         set_cpu_online(cpu, true);
405
406         /*
407          * Check for cache aliasing.
408          * Preemption is disabled
409          */
410         check_for_cache_aliasing(cpu);
411
412         /*
413          * OK, it's off to the idle thread for us
414          */
415         cpu_startup_entry(CPUHP_ONLINE);
416 }
417
418 void __init smp_cpus_done(unsigned int max_cpus)
419 {
420         int cpu;
421         unsigned long bogosum = 0;
422
423         for_each_online_cpu(cpu)
424                 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
425
426         pr_info("SMP: Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
427                 num_online_cpus(),
428                 bogosum / (500000/HZ),
429                 (bogosum / (5000/HZ)) % 100);
430 }
431
432 void __init smp_prepare_cpus(unsigned int max_cpus)
433 {
434         unsigned int cpu = smp_processor_id();
435
436         init_new_context(current, &init_mm);
437         current_thread_info()->cpu = cpu;
438
439         smp_store_cpu_info(cpu);
440         init_cpu_present(cpu_possible_mask);
441 }
442
443 void __init smp_prepare_boot_cpu(void)
444 {
445         unsigned int cpu = smp_processor_id();
446
447         per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
448
449         if (!per_cpu(pTBI, cpu))
450                 panic("No TBI found!");
451 }
452
453 static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg);
454
455 static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
456 {
457         unsigned long flags;
458         unsigned int cpu;
459         cpumask_t map;
460
461         cpumask_clear(&map);
462         local_irq_save(flags);
463
464         for_each_cpu(cpu, mask) {
465                 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
466
467                 spin_lock(&ipi->lock);
468
469                 /*
470                  * KICK interrupts are queued in hardware so we'll get
471                  * multiple interrupts if we call smp_cross_call()
472                  * multiple times for one msg. The problem is that we
473                  * only have one bit for each message - we can't queue
474                  * them in software.
475                  *
476                  * The first time through ipi_handler() we'll clear
477                  * the msg bit, having done all the work. But when we
478                  * return we'll get _another_ interrupt (and another,
479                  * and another until we've handled all the queued
480                  * KICKs). Running ipi_handler() when there's no work
481                  * to do is bad because that's how kick handler
482                  * chaining detects who the KICK was intended for.
483                  * See arch/metag/kernel/kick.c for more details.
484                  *
485                  * So only add 'cpu' to 'map' if we haven't already
486                  * queued a KICK interrupt for 'msg'.
487                  */
488                 if (!(ipi->bits & (1 << msg))) {
489                         ipi->bits |= 1 << msg;
490                         cpumask_set_cpu(cpu, &map);
491                 }
492
493                 spin_unlock(&ipi->lock);
494         }
495
496         /*
497          * Call the platform specific cross-CPU call function.
498          */
499         smp_cross_call(map, msg);
500
501         local_irq_restore(flags);
502 }
503
504 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
505 {
506         send_ipi_message(mask, IPI_CALL_FUNC);
507 }
508
509 void arch_send_call_function_single_ipi(int cpu)
510 {
511         send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
512 }
513
514 void show_ipi_list(struct seq_file *p)
515 {
516         unsigned int cpu;
517
518         seq_puts(p, "IPI:");
519
520         for_each_present_cpu(cpu)
521                 seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
522
523         seq_putc(p, '\n');
524 }
525
526 static DEFINE_SPINLOCK(stop_lock);
527
528 /*
529  * Main handler for inter-processor interrupts
530  *
531  * For Meta, the ipimask now only identifies a single
532  * category of IPI (Bit 1 IPIs have been replaced by a
533  * different mechanism):
534  *
535  *  Bit 0 - Inter-processor function call
536  */
537 static int do_IPI(struct pt_regs *regs)
538 {
539         unsigned int cpu = smp_processor_id();
540         struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
541         struct pt_regs *old_regs = set_irq_regs(regs);
542         unsigned long msgs, nextmsg;
543         int handled = 0;
544
545         ipi->ipi_count++;
546
547         spin_lock(&ipi->lock);
548         msgs = ipi->bits;
549         nextmsg = msgs & -msgs;
550         ipi->bits &= ~nextmsg;
551         spin_unlock(&ipi->lock);
552
553         if (nextmsg) {
554                 handled = 1;
555
556                 nextmsg = ffz(~nextmsg);
557                 switch (nextmsg) {
558                 case IPI_RESCHEDULE:
559                         scheduler_ipi();
560                         break;
561
562                 case IPI_CALL_FUNC:
563                         generic_smp_call_function_interrupt();
564                         break;
565
566                 case IPI_CALL_FUNC_SINGLE:
567                         generic_smp_call_function_single_interrupt();
568                         break;
569
570                 default:
571                         pr_crit("CPU%u: Unknown IPI message 0x%lx\n",
572                                 cpu, nextmsg);
573                         break;
574                 }
575         }
576
577         set_irq_regs(old_regs);
578
579         return handled;
580 }
581
582 void smp_send_reschedule(int cpu)
583 {
584         send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
585 }
586
587 static void stop_this_cpu(void *data)
588 {
589         unsigned int cpu = smp_processor_id();
590
591         if (system_state == SYSTEM_BOOTING ||
592             system_state == SYSTEM_RUNNING) {
593                 spin_lock(&stop_lock);
594                 pr_crit("CPU%u: stopping\n", cpu);
595                 dump_stack();
596                 spin_unlock(&stop_lock);
597         }
598
599         set_cpu_online(cpu, false);
600
601         local_irq_disable();
602
603         hard_processor_halt(HALT_OK);
604 }
605
606 void smp_send_stop(void)
607 {
608         smp_call_function(stop_this_cpu, NULL, 0);
609 }
610
611 /*
612  * not supported here
613  */
614 int setup_profiling_timer(unsigned int multiplier)
615 {
616         return -EINVAL;
617 }
618
619 /*
620  * We use KICKs for inter-processor interrupts.
621  *
622  * For every CPU in "callmap" the IPI data must already have been
623  * stored in that CPU's "ipi_data" member prior to calling this
624  * function.
625  */
626 static void kick_raise_softirq(cpumask_t callmap, unsigned int irq)
627 {
628         int cpu;
629
630         for_each_cpu(cpu, &callmap) {
631                 unsigned int thread;
632
633                 thread = cpu_2_hwthread_id[cpu];
634
635                 BUG_ON(thread == BAD_HWTHREAD_ID);
636
637                 metag_out32(1, T0KICKI + (thread * TnXKICK_STRIDE));
638         }
639 }
640
641 static TBIRES ipi_handler(TBIRES State, int SigNum, int Triggers,
642                    int Inst, PTBI pTBI, int *handled)
643 {
644         *handled = do_IPI((struct pt_regs *)State.Sig.pCtx);
645
646         return State;
647 }
648
649 static struct kick_irq_handler ipi_irq = {
650         .func = ipi_handler,
651 };
652
653 static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg)
654 {
655         kick_raise_softirq(callmap, 1);
656 }
657
658 static inline unsigned int get_core_count(void)
659 {
660         int i;
661         unsigned int ret = 0;
662
663         for (i = 0; i < CONFIG_NR_CPUS; i++) {
664                 if (core_reg_read(TXUCT_ID, TXENABLE_REGNUM, i))
665                         ret++;
666         }
667
668         return ret;
669 }
670
671 /*
672  * Initialise the CPU possible map early - this describes the CPUs
673  * which may be present or become present in the system.
674  */
675 void __init smp_init_cpus(void)
676 {
677         unsigned int i, ncores = get_core_count();
678
679         /* If no hwthread_map early param was set use default mapping */
680         for (i = 0; i < NR_CPUS; i++)
681                 if (cpu_2_hwthread_id[i] == BAD_HWTHREAD_ID) {
682                         cpu_2_hwthread_id[i] = i;
683                         hwthread_id_2_cpu[i] = i;
684                 }
685
686         for (i = 0; i < ncores; i++)
687                 set_cpu_possible(i, true);
688
689         kick_register_func(&ipi_irq);
690 }