📄 smp.c
字号:
data.func = func; data.info = info; atomic_set(&data.finished, 0); data.wait = wait; spin_lock(&call_lock); call_data = &data; smp_cross_call(&xcall_call_function, 0, 0, 0); /* * Wait for other cpus to complete function or at * least snap the call data. */ timeout = 1000000; while (atomic_read(&data.finished) != cpus) { if (--timeout <= 0) goto out_timeout; barrier(); udelay(1); } spin_unlock(&call_lock); return 0;out_timeout: spin_unlock(&call_lock); printk("XCALL: Remote cpus not responding, ncpus=%ld finished=%ld\n", (long) num_online_cpus() - 1L, (long) atomic_read(&data.finished)); return 0;}void smp_call_function_client(int irq, struct pt_regs *regs){ void (*func) (void *info) = call_data->func; void *info = call_data->info; clear_softint(1 << irq); if (call_data->wait) { /* let initiator proceed only after completion */ func(info); atomic_inc(&call_data->finished); } else { /* let initiator proceed after getting data */ atomic_inc(&call_data->finished); func(info); }}extern unsigned long xcall_flush_tlb_mm;extern unsigned long xcall_flush_tlb_pending;extern unsigned long xcall_flush_tlb_kernel_range;extern unsigned long xcall_flush_tlb_all_spitfire;extern unsigned long xcall_flush_tlb_all_cheetah;extern unsigned long xcall_report_regs;extern unsigned long xcall_receive_signal;#ifdef DCACHE_ALIASING_POSSIBLEextern unsigned long xcall_flush_dcache_page_cheetah;#endifextern unsigned long xcall_flush_dcache_page_spitfire;#ifdef CONFIG_DEBUG_DCFLUSHextern atomic_t dcpage_flushes;extern atomic_t dcpage_flushes_xcall;#endifstatic __inline__ void __local_flush_dcache_page(struct page *page){#ifdef DCACHE_ALIASING_POSSIBLE __flush_dcache_page(page_address(page), ((tlb_type == spitfire) && page_mapping(page) != NULL));#else if (page_mapping(page) != NULL && tlb_type == spitfire) __flush_icache_page(__pa(page_address(page)));#endif}void smp_flush_dcache_page_impl(struct page *page, int cpu){ cpumask_t mask = cpumask_of_cpu(cpu); int this_cpu = get_cpu();#ifdef CONFIG_DEBUG_DCFLUSH atomic_inc(&dcpage_flushes);#endif if (cpu == this_cpu) { __local_flush_dcache_page(page); } else if (cpu_online(cpu)) { void *pg_addr = page_address(page); u64 data0; if (tlb_type == spitfire) { data0 = ((u64)&xcall_flush_dcache_page_spitfire); if (page_mapping(page) != NULL) data0 |= ((u64)1 << 32); spitfire_xcall_deliver(data0, __pa(pg_addr), (u64) pg_addr, mask); } else {#ifdef DCACHE_ALIASING_POSSIBLE data0 = ((u64)&xcall_flush_dcache_page_cheetah); cheetah_xcall_deliver(data0, __pa(pg_addr), 0, mask);#endif }#ifdef CONFIG_DEBUG_DCFLUSH atomic_inc(&dcpage_flushes_xcall);#endif } put_cpu();}void flush_dcache_page_all(struct mm_struct *mm, struct page *page){ void *pg_addr = page_address(page); cpumask_t mask = cpu_online_map; u64 data0; int this_cpu = get_cpu(); cpu_clear(this_cpu, mask);#ifdef CONFIG_DEBUG_DCFLUSH atomic_inc(&dcpage_flushes);#endif if (cpus_empty(mask)) goto flush_self; if (tlb_type == spitfire) { data0 = ((u64)&xcall_flush_dcache_page_spitfire); if (page_mapping(page) != NULL) data0 |= ((u64)1 << 32); spitfire_xcall_deliver(data0, __pa(pg_addr), (u64) pg_addr, mask); } else {#ifdef DCACHE_ALIASING_POSSIBLE data0 = ((u64)&xcall_flush_dcache_page_cheetah); cheetah_xcall_deliver(data0, __pa(pg_addr), 0, mask);#endif }#ifdef CONFIG_DEBUG_DCFLUSH atomic_inc(&dcpage_flushes_xcall);#endif flush_self: __local_flush_dcache_page(page); put_cpu();}void smp_receive_signal(int cpu){ cpumask_t mask = cpumask_of_cpu(cpu); if (cpu_online(cpu)) { u64 data0 = (((u64)&xcall_receive_signal) & 0xffffffff); if (tlb_type == spitfire) spitfire_xcall_deliver(data0, 0, 0, mask); else cheetah_xcall_deliver(data0, 0, 0, mask); }}void smp_receive_signal_client(int irq, struct pt_regs *regs){ /* Just return, rtrap takes care of the rest. */ clear_softint(1 << irq);}void smp_report_regs(void){ smp_cross_call(&xcall_report_regs, 0, 0, 0);}void smp_flush_tlb_all(void){ if (tlb_type == spitfire) smp_cross_call(&xcall_flush_tlb_all_spitfire, 0, 0, 0); else smp_cross_call(&xcall_flush_tlb_all_cheetah, 0, 0, 0); __flush_tlb_all();}/* We know that the window frames of the user have been flushed * to the stack before we get here because all callers of us * are flush_tlb_*() routines, and these run after flush_cache_*() * which performs the flushw. * * The SMP TLB coherency scheme we use works as follows: * * 1) mm->cpu_vm_mask is a bit mask of which cpus an address * space has (potentially) executed on, this is the heuristic * we use to avoid doing cross calls. * * Also, for flushing from kswapd and also for clones, we * use cpu_vm_mask as the list of cpus to make run the TLB. * * 2) TLB context numbers are shared globally across all processors * in the system, this allows us to play several games to avoid * cross calls. * * One invariant is that when a cpu switches to a process, and * that processes tsk->active_mm->cpu_vm_mask does not have the * current cpu's bit set, that tlb context is flushed locally. * * If the address space is non-shared (ie. mm->count == 1) we avoid * cross calls when we want to flush the currently running process's * tlb state. This is done by clearing all cpu bits except the current * processor's in current->active_mm->cpu_vm_mask and performing the * flush locally only. This will force any subsequent cpus which run * this task to flush the context from the local tlb if the process * migrates to another cpu (again). * * 3) For shared address spaces (threads) and swapping we bite the * bullet for most cases and perform the cross call (but only to * the cpus listed in cpu_vm_mask). * * The performance gain from "optimizing" away the cross call for threads is * questionable (in theory the big win for threads is the massive sharing of * address space state across processors). *//* This currently is only used by the hugetlb arch pre-fault * hook on UltraSPARC-III+ and later when changing the pagesize * bits of the context register for an address space. */void smp_flush_tlb_mm(struct mm_struct *mm){ u32 ctx = CTX_HWBITS(mm->context); int cpu = get_cpu(); if (atomic_read(&mm->mm_users) == 1) { mm->cpu_vm_mask = cpumask_of_cpu(cpu); goto local_flush_and_out; } smp_cross_call_masked(&xcall_flush_tlb_mm, ctx, 0, 0, mm->cpu_vm_mask);local_flush_and_out: __flush_tlb_mm(ctx, SECONDARY_CONTEXT); put_cpu();}void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs){ u32 ctx = CTX_HWBITS(mm->context); int cpu = get_cpu(); if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1) mm->cpu_vm_mask = cpumask_of_cpu(cpu); else smp_cross_call_masked(&xcall_flush_tlb_pending, ctx, nr, (unsigned long) vaddrs, mm->cpu_vm_mask); __flush_tlb_pending(ctx, nr, vaddrs); put_cpu();}void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end){ start &= PAGE_MASK; end = PAGE_ALIGN(end); if (start != end) { smp_cross_call(&xcall_flush_tlb_kernel_range, 0, start, end); __flush_tlb_kernel_range(start, end); }}/* CPU capture. *//* #define CAPTURE_DEBUG */extern unsigned long xcall_capture;static atomic_t smp_capture_depth = ATOMIC_INIT(0);static atomic_t smp_capture_registry = ATOMIC_INIT(0);static unsigned long penguins_are_doing_time;void smp_capture(void){ int result = atomic_add_ret(1, &smp_capture_depth); if (result == 1) { int ncpus = num_online_cpus();#ifdef CAPTURE_DEBUG printk("CPU[%d]: Sending penguins to jail...", smp_processor_id());#endif penguins_are_doing_time = 1; membar_storestore_loadstore(); atomic_inc(&smp_capture_registry); smp_cross_call(&xcall_capture, 0, 0, 0); while (atomic_read(&smp_capture_registry) != ncpus) rmb();#ifdef CAPTURE_DEBUG printk("done\n");#endif }}void smp_release(void){ if (atomic_dec_and_test(&smp_capture_depth)) {#ifdef CAPTURE_DEBUG printk("CPU[%d]: Giving pardon to " "imprisoned penguins\n", smp_processor_id());#endif penguins_are_doing_time = 0; membar_storeload_storestore(); atomic_dec(&smp_capture_registry); }}/* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they * can service tlb flush xcalls... */extern void prom_world(int);extern void save_alternate_globals(unsigned long *);extern void restore_alternate_globals(unsigned long *);void smp_penguin_jailcell(int irq, struct pt_regs *regs){ unsigned long global_save[24]; clear_softint(1 << irq); preempt_disable(); __asm__ __volatile__("flushw"); save_alternate_globals(global_save); prom_world(1); atomic_inc(&smp_capture_registry); membar_storeload_storestore(); while (penguins_are_doing_time) rmb(); restore_alternate_globals(global_save); atomic_dec(&smp_capture_registry); prom_world(0); preempt_enable();}#define prof_multiplier(__cpu) cpu_data(__cpu).multiplier#define prof_counter(__cpu) cpu_data(__cpu).countervoid smp_percpu_timer_interrupt(struct pt_regs *regs){ unsigned long compare, tick, pstate; int cpu = smp_processor_id(); int user = user_mode(regs); /* * Check for level 14 softint. */ { unsigned long tick_mask = tick_ops->softint_mask; if (!(get_softint() & tick_mask)) { extern void handler_irq(int, struct pt_regs *); handler_irq(14, regs); return; } clear_softint(tick_mask); } do { profile_tick(CPU_PROFILING, regs); if (!--prof_counter(cpu)) { irq_enter(); if (cpu == boot_cpu_id) { kstat_this_cpu.irqs[0]++; timer_tick_interrupt(regs); } update_process_times(user); irq_exit(); prof_counter(cpu) = prof_multiplier(cpu); } /* Guarantee that the following sequences execute * uninterrupted. */ __asm__ __volatile__("rdpr %%pstate, %0\n\t" "wrpr %0, %1, %%pstate" : "=r" (pstate) : "i" (PSTATE_IE)); compare = tick_ops->add_compare(current_tick_offset); tick = tick_ops->get_tick(); /* Restore PSTATE_IE. */ __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : /* no outputs */ : "r" (pstate)); } while (time_after_eq(tick, compare));}static void __init smp_setup_percpu_timer(void){ int cpu = smp_processor_id(); unsigned long pstate; prof_counter(cpu) = prof_multiplier(cpu) = 1; /* Guarantee that the following sequences execute * uninterrupted. */ __asm__ __volatile__("rdpr %%pstate, %0\n\t" "wrpr %0, %1, %%pstate" : "=r" (pstate) : "i" (PSTATE_IE)); tick_ops->init_tick(current_tick_offset); /* Restore PSTATE_IE. */ __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : /* no outputs */ : "r" (pstate));}void __init smp_tick_init(void){ boot_cpu_id = hard_smp_processor_id(); current_tick_offset = timer_tick_offset; cpu_set(boot_cpu_id, cpu_online_map); prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;}/* /proc/profile writes can call this, don't __init it please. */static DEFINE_SPINLOCK(prof_setup_lock);int setup_profiling_timer(unsigned int multiplier){ unsigned long flags; int i; if ((!multiplier) || (timer_tick_offset / multiplier) < 1000) return -EINVAL; spin_lock_irqsave(&prof_setup_lock, flags); for (i = 0; i < NR_CPUS; i++) prof_multiplier(i) = multiplier; current_tick_offset = (timer_tick_offset / multiplier); spin_unlock_irqrestore(&prof_setup_lock, flags); return 0;}void __init smp_prepare_cpus(unsigned int max_cpus){ int instance, mid; instance = 0; while (!cpu_find_by_instance(instance, NULL, &mid)) { if (mid < max_cpus) cpu_set(mid, phys_cpu_present_map); instance++; } if (num_possible_cpus() > max_cpus) { instance = 0; while (!cpu_find_by_instance(instance, NULL, &mid)) { if (mid != boot_cpu_id) { cpu_clear(mid, phys_cpu_present_map); if (num_possible_cpus() <= max_cpus) break; } instance++; } } smp_store_cpu_info(boot_cpu_id);}void __devinit smp_prepare_boot_cpu(void){ if (hard_smp_processor_id() >= NR_CPUS) { prom_printf("Serious problem, boot cpu id >= NR_CPUS\n"); prom_halt(); } current_thread_info()->cpu = hard_smp_processor_id(); cpu_set(smp_processor_id(), cpu_online_map); cpu_set(smp_processor_id(), phys_cpu_present_map);}int __devinit __cpu_up(unsigned int cpu){ int ret = smp_boot_one_cpu(cpu); if (!ret) { cpu_set(cpu, smp_commenced_mask); while (!cpu_isset(cpu, cpu_online_map)) mb(); if (!cpu_isset(cpu, cpu_online_map)) { ret = -ENODEV; } else { smp_synchronize_one_tick(cpu); } } return ret;}void __init smp_cpus_done(unsigned int max_cpus){ unsigned long bogosum = 0; int i; for (i = 0; i < NR_CPUS; i++) { if (cpu_online(i)) bogosum += cpu_data(i).udelay_val; } printk("Total of %ld processors activated " "(%lu.%02lu BogoMIPS).\n", (long) num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);}void smp_send_reschedule(int cpu){ smp_receive_signal(cpu);}/* This is a nop because we capture all other cpus * anyways when making the PROM active. */void smp_send_stop(void){}unsigned long __per_cpu_base __read_mostly;unsigned long __per_cpu_shift __read_mostly;EXPORT_SYMBOL(__per_cpu_base);EXPORT_SYMBOL(__per_cpu_shift);void __init setup_per_cpu_areas(void){ unsigned long goal, size, i; char *ptr; /* Created by linker magic */ extern char __per_cpu_start[], __per_cpu_end[]; /* Copy section for each CPU (we discard the original) */ goal = ALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE);#ifdef CONFIG_MODULES if (goal < PERCPU_ENOUGH_ROOM) goal = PERCPU_ENOUGH_ROOM;#endif __per_cpu_shift = 0; for (size = 1UL; size < goal; size <<= 1UL) __per_cpu_shift++; /* Make sure the resulting __per_cpu_base value * will fit in the 43-bit sign extended IMMU * TSB register. */ ptr = __alloc_bootmem(size * NR_CPUS, PAGE_SIZE, (unsigned long) __per_cpu_start); __per_cpu_base = ptr - __per_cpu_start; if ((__per_cpu_shift < PAGE_SHIFT) || (__per_cpu_base & ~PAGE_MASK) || (__per_cpu_base != (((long) __per_cpu_base << 20) >> 20))) { prom_printf("PER_CPU: Invalid layout, " "ptr[%p] shift[%lx] base[%lx]\n", ptr, __per_cpu_shift, __per_cpu_base); prom_halt(); } for (i = 0; i < NR_CPUS; i++, ptr += size) memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start); /* Finally, load in the boot cpu's base value. * We abuse the IMMU TSB register for trap handler * entry and exit loading of %g5. That is why it * has to be page aligned. */ cpu_setup_percpu_base(hard_smp_processor_id());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -