Path: blob/master/arch/ia64/sn/kernel/sn2/sn2_smp.c
17602 views
/*1* SN2 Platform specific SMP Support2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file "COPYING" in the main directory of this archive5* for more details.6*7* Copyright (C) 2000-2006 Silicon Graphics, Inc. All rights reserved.8*/910#include <linux/init.h>11#include <linux/kernel.h>12#include <linux/spinlock.h>13#include <linux/threads.h>14#include <linux/sched.h>15#include <linux/smp.h>16#include <linux/interrupt.h>17#include <linux/irq.h>18#include <linux/mmzone.h>19#include <linux/module.h>20#include <linux/bitops.h>21#include <linux/nodemask.h>22#include <linux/proc_fs.h>23#include <linux/seq_file.h>2425#include <asm/processor.h>26#include <asm/irq.h>27#include <asm/sal.h>28#include <asm/system.h>29#include <asm/delay.h>30#include <asm/io.h>31#include <asm/smp.h>32#include <asm/tlb.h>33#include <asm/numa.h>34#include <asm/hw_irq.h>35#include <asm/current.h>36#include <asm/sn/sn_cpuid.h>37#include <asm/sn/sn_sal.h>38#include <asm/sn/addrs.h>39#include <asm/sn/shub_mmr.h>40#include <asm/sn/nodepda.h>41#include <asm/sn/rw_mmr.h>42#include <asm/sn/sn_feature_sets.h>4344DEFINE_PER_CPU(struct ptc_stats, ptcstats);45DECLARE_PER_CPU(struct ptc_stats, ptcstats);4647static __cacheline_aligned DEFINE_SPINLOCK(sn2_global_ptc_lock);4849/* 0 = old algorithm (no IPI flushes), 1 = ipi deadlock flush, 2 = ipi instead of SHUB ptc, >2 = always ipi */50static int sn2_flush_opt = 0;5152extern unsigned long53sn2_ptc_deadlock_recovery_core(volatile unsigned long *, unsigned long,54volatile unsigned long *, unsigned long,55volatile unsigned long *, unsigned long);56void57sn2_ptc_deadlock_recovery(short *, short, short, int,58volatile unsigned long *, unsigned long,59volatile unsigned long *, unsigned long);6061/*62* Note: some is the following is captured here to make degugging easier63* (the macros make more sense if you see the debug patch - not posted)64*/65#define sn2_ptctest 066#define local_node_uses_ptc_ga(sh1) ((sh1) ? 1 : 0)67#define max_active_pio(sh1) ((sh1) ? 32 : 7)68#define reset_max_active_on_deadlock() 169#define PTC_LOCK(sh1) ((sh1) ? &sn2_global_ptc_lock : &sn_nodepda->ptc_lock)7071struct ptc_stats {72unsigned long ptc_l;73unsigned long change_rid;74unsigned long shub_ptc_flushes;75unsigned long nodes_flushed;76unsigned long deadlocks;77unsigned long deadlocks2;78unsigned long lock_itc_clocks;79unsigned long shub_itc_clocks;80unsigned long shub_itc_clocks_max;81unsigned long shub_ptc_flushes_not_my_mm;82unsigned long shub_ipi_flushes;83unsigned long shub_ipi_flushes_itc_clocks;84};8586#define sn2_ptctest 08788static inline unsigned long wait_piowc(void)89{90volatile unsigned long *piows;91unsigned long zeroval, ws;9293piows = pda->pio_write_status_addr;94zeroval = pda->pio_write_status_val;95do {96cpu_relax();97} while (((ws = *piows) & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK) != zeroval);98return (ws & SH_PIO_WRITE_STATUS_WRITE_DEADLOCK_MASK) != 0;99}100101/**102* sn_migrate - SN-specific task migration actions103* @task: Task being migrated to new CPU104*105* SN2 PIO writes from separate CPUs are not guaranteed to arrive in order.106* Context switching user threads which have memory-mapped MMIO may cause107* PIOs to issue from separate CPUs, thus the PIO writes must be drained108* from the previous CPU's Shub before execution resumes on the new CPU.109*/110void sn_migrate(struct task_struct *task)111{112pda_t *last_pda = pdacpu(task_thread_info(task)->last_cpu);113volatile unsigned long *adr = last_pda->pio_write_status_addr;114unsigned long val = last_pda->pio_write_status_val;115116/* Drain PIO writes from old CPU's Shub */117while (unlikely((*adr & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK)118!= val))119cpu_relax();120}121122void sn_tlb_migrate_finish(struct mm_struct *mm)123{124/* flush_tlb_mm is inefficient if more than 1 users of mm */125if (mm == current->mm && mm && atomic_read(&mm->mm_users) == 1)126flush_tlb_mm(mm);127}128129static void130sn2_ipi_flush_all_tlb(struct mm_struct *mm)131{132unsigned long itc;133134itc = ia64_get_itc();135smp_flush_tlb_cpumask(*mm_cpumask(mm));136itc = ia64_get_itc() - itc;137__get_cpu_var(ptcstats).shub_ipi_flushes_itc_clocks += itc;138__get_cpu_var(ptcstats).shub_ipi_flushes++;139}140141/**142* sn2_global_tlb_purge - globally purge translation cache of virtual address range143* @mm: mm_struct containing virtual address range144* @start: start of virtual address range145* @end: end of virtual address range146* @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))147*148* Purges the translation caches of all processors of the given virtual address149* range.150*151* Note:152* - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.153* - cpu_vm_mask is converted into a nodemask of the nodes containing the154* cpus in cpu_vm_mask.155* - if only one bit is set in cpu_vm_mask & it is the current cpu & the156* process is purging its own virtual address range, then only the157* local TLB needs to be flushed. This flushing can be done using158* ptc.l. This is the common case & avoids the global spinlock.159* - if multiple cpus have loaded the context, then flushing has to be160* done with ptc.g/MMRs under protection of the global ptc_lock.161*/162163void164sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,165unsigned long end, unsigned long nbits)166{167int i, ibegin, shub1, cnode, mynasid, cpu, lcpu = 0, nasid;168int mymm = (mm == current->active_mm && mm == current->mm);169int use_cpu_ptcga;170volatile unsigned long *ptc0, *ptc1;171unsigned long itc, itc2, flags, data0 = 0, data1 = 0, rr_value, old_rr = 0;172short nasids[MAX_NUMNODES], nix;173nodemask_t nodes_flushed;174int active, max_active, deadlock, flush_opt = sn2_flush_opt;175176if (flush_opt > 2) {177sn2_ipi_flush_all_tlb(mm);178return;179}180181nodes_clear(nodes_flushed);182i = 0;183184for_each_cpu(cpu, mm_cpumask(mm)) {185cnode = cpu_to_node(cpu);186node_set(cnode, nodes_flushed);187lcpu = cpu;188i++;189}190191if (i == 0)192return;193194preempt_disable();195196if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {197do {198ia64_ptcl(start, nbits << 2);199start += (1UL << nbits);200} while (start < end);201ia64_srlz_i();202__get_cpu_var(ptcstats).ptc_l++;203preempt_enable();204return;205}206207if (atomic_read(&mm->mm_users) == 1 && mymm) {208flush_tlb_mm(mm);209__get_cpu_var(ptcstats).change_rid++;210preempt_enable();211return;212}213214if (flush_opt == 2) {215sn2_ipi_flush_all_tlb(mm);216preempt_enable();217return;218}219220itc = ia64_get_itc();221nix = 0;222for_each_node_mask(cnode, nodes_flushed)223nasids[nix++] = cnodeid_to_nasid(cnode);224225rr_value = (mm->context << 3) | REGION_NUMBER(start);226227shub1 = is_shub1();228if (shub1) {229data0 = (1UL << SH1_PTC_0_A_SHFT) |230(nbits << SH1_PTC_0_PS_SHFT) |231(rr_value << SH1_PTC_0_RID_SHFT) |232(1UL << SH1_PTC_0_START_SHFT);233ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_0);234ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_1);235} else {236data0 = (1UL << SH2_PTC_A_SHFT) |237(nbits << SH2_PTC_PS_SHFT) |238(1UL << SH2_PTC_START_SHFT);239ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH2_PTC +240(rr_value << SH2_PTC_RID_SHFT));241ptc1 = NULL;242}243244245mynasid = get_nasid();246use_cpu_ptcga = local_node_uses_ptc_ga(shub1);247max_active = max_active_pio(shub1);248249itc = ia64_get_itc();250spin_lock_irqsave(PTC_LOCK(shub1), flags);251itc2 = ia64_get_itc();252253__get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;254__get_cpu_var(ptcstats).shub_ptc_flushes++;255__get_cpu_var(ptcstats).nodes_flushed += nix;256if (!mymm)257__get_cpu_var(ptcstats).shub_ptc_flushes_not_my_mm++;258259if (use_cpu_ptcga && !mymm) {260old_rr = ia64_get_rr(start);261ia64_set_rr(start, (old_rr & 0xff) | (rr_value << 8));262ia64_srlz_d();263}264265wait_piowc();266do {267if (shub1)268data1 = start | (1UL << SH1_PTC_1_START_SHFT);269else270data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);271deadlock = 0;272active = 0;273for (ibegin = 0, i = 0; i < nix; i++) {274nasid = nasids[i];275if (use_cpu_ptcga && unlikely(nasid == mynasid)) {276ia64_ptcga(start, nbits << 2);277ia64_srlz_i();278} else {279ptc0 = CHANGE_NASID(nasid, ptc0);280if (ptc1)281ptc1 = CHANGE_NASID(nasid, ptc1);282pio_atomic_phys_write_mmrs(ptc0, data0, ptc1, data1);283active++;284}285if (active >= max_active || i == (nix - 1)) {286if ((deadlock = wait_piowc())) {287if (flush_opt == 1)288goto done;289sn2_ptc_deadlock_recovery(nasids, ibegin, i, mynasid, ptc0, data0, ptc1, data1);290if (reset_max_active_on_deadlock())291max_active = 1;292}293active = 0;294ibegin = i + 1;295}296}297start += (1UL << nbits);298} while (start < end);299300done:301itc2 = ia64_get_itc() - itc2;302__get_cpu_var(ptcstats).shub_itc_clocks += itc2;303if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)304__get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;305306if (old_rr) {307ia64_set_rr(start, old_rr);308ia64_srlz_d();309}310311spin_unlock_irqrestore(PTC_LOCK(shub1), flags);312313if (flush_opt == 1 && deadlock) {314__get_cpu_var(ptcstats).deadlocks++;315sn2_ipi_flush_all_tlb(mm);316}317318preempt_enable();319}320321/*322* sn2_ptc_deadlock_recovery323*324* Recover from PTC deadlocks conditions. Recovery requires stepping thru each325* TLB flush transaction. The recovery sequence is somewhat tricky & is326* coded in assembly language.327*/328329void330sn2_ptc_deadlock_recovery(short *nasids, short ib, short ie, int mynasid,331volatile unsigned long *ptc0, unsigned long data0,332volatile unsigned long *ptc1, unsigned long data1)333{334short nasid, i;335unsigned long *piows, zeroval, n;336337__get_cpu_var(ptcstats).deadlocks++;338339piows = (unsigned long *) pda->pio_write_status_addr;340zeroval = pda->pio_write_status_val;341342343for (i=ib; i <= ie; i++) {344nasid = nasids[i];345if (local_node_uses_ptc_ga(is_shub1()) && nasid == mynasid)346continue;347ptc0 = CHANGE_NASID(nasid, ptc0);348if (ptc1)349ptc1 = CHANGE_NASID(nasid, ptc1);350351n = sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);352__get_cpu_var(ptcstats).deadlocks2 += n;353}354355}356357/**358* sn_send_IPI_phys - send an IPI to a Nasid and slice359* @nasid: nasid to receive the interrupt (may be outside partition)360* @physid: physical cpuid to receive the interrupt.361* @vector: command to send362* @delivery_mode: delivery mechanism363*364* Sends an IPI (interprocessor interrupt) to the processor specified by365* @physid366*367* @delivery_mode can be one of the following368*369* %IA64_IPI_DM_INT - pend an interrupt370* %IA64_IPI_DM_PMI - pend a PMI371* %IA64_IPI_DM_NMI - pend an NMI372* %IA64_IPI_DM_INIT - pend an INIT interrupt373*/374void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)375{376long val;377unsigned long flags = 0;378volatile long *p;379380p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);381val = (1UL << SH_IPI_INT_SEND_SHFT) |382(physid << SH_IPI_INT_PID_SHFT) |383((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |384((long)vector << SH_IPI_INT_IDX_SHFT) |385(0x000feeUL << SH_IPI_INT_BASE_SHFT);386387mb();388if (enable_shub_wars_1_1()) {389spin_lock_irqsave(&sn2_global_ptc_lock, flags);390}391pio_phys_write_mmr(p, val);392if (enable_shub_wars_1_1()) {393wait_piowc();394spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);395}396397}398399EXPORT_SYMBOL(sn_send_IPI_phys);400401/**402* sn2_send_IPI - send an IPI to a processor403* @cpuid: target of the IPI404* @vector: command to send405* @delivery_mode: delivery mechanism406* @redirect: redirect the IPI?407*408* Sends an IPI (InterProcessor Interrupt) to the processor specified by409* @cpuid. @vector specifies the command to send, while @delivery_mode can410* be one of the following411*412* %IA64_IPI_DM_INT - pend an interrupt413* %IA64_IPI_DM_PMI - pend a PMI414* %IA64_IPI_DM_NMI - pend an NMI415* %IA64_IPI_DM_INIT - pend an INIT interrupt416*/417void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)418{419long physid;420int nasid;421422physid = cpu_physical_id(cpuid);423nasid = cpuid_to_nasid(cpuid);424425/* the following is used only when starting cpus at boot time */426if (unlikely(nasid == -1))427ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);428429sn_send_IPI_phys(nasid, physid, vector, delivery_mode);430}431432#ifdef CONFIG_HOTPLUG_CPU433/**434* sn_cpu_disable_allowed - Determine if a CPU can be disabled.435* @cpu - CPU that is requested to be disabled.436*437* CPU disable is only allowed on SHub2 systems running with a PROM438* that supports CPU disable. It is not permitted to disable the boot processor.439*/440bool sn_cpu_disable_allowed(int cpu)441{442if (is_shub2() && sn_prom_feature_available(PRF_CPU_DISABLE_SUPPORT)) {443if (cpu != 0)444return true;445else446printk(KERN_WARNING447"Disabling the boot processor is not allowed.\n");448449} else450printk(KERN_WARNING451"CPU disable is not supported on this system.\n");452453return false;454}455#endif /* CONFIG_HOTPLUG_CPU */456457#ifdef CONFIG_PROC_FS458459#define PTC_BASENAME "sgi_sn/ptc_statistics"460461static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)462{463if (*offset < nr_cpu_ids)464return offset;465return NULL;466}467468static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)469{470(*offset)++;471if (*offset < nr_cpu_ids)472return offset;473return NULL;474}475476static void sn2_ptc_seq_stop(struct seq_file *file, void *data)477{478}479480static int sn2_ptc_seq_show(struct seq_file *file, void *data)481{482struct ptc_stats *stat;483int cpu;484485cpu = *(loff_t *) data;486487if (!cpu) {488seq_printf(file,489"# cpu ptc_l newrid ptc_flushes nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max not_my_mm deadlock2 ipi_fluches ipi_nsec\n");490seq_printf(file, "# ptctest %d, flushopt %d\n", sn2_ptctest, sn2_flush_opt);491}492493if (cpu < nr_cpu_ids && cpu_online(cpu)) {494stat = &per_cpu(ptcstats, cpu);495seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,496stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,497stat->deadlocks,4981000 * stat->lock_itc_clocks / per_cpu(ia64_cpu_info, cpu).cyc_per_usec,4991000 * stat->shub_itc_clocks / per_cpu(ia64_cpu_info, cpu).cyc_per_usec,5001000 * stat->shub_itc_clocks_max / per_cpu(ia64_cpu_info, cpu).cyc_per_usec,501stat->shub_ptc_flushes_not_my_mm,502stat->deadlocks2,503stat->shub_ipi_flushes,5041000 * stat->shub_ipi_flushes_itc_clocks / per_cpu(ia64_cpu_info, cpu).cyc_per_usec);505}506return 0;507}508509static ssize_t sn2_ptc_proc_write(struct file *file, const char __user *user, size_t count, loff_t *data)510{511int cpu;512char optstr[64];513514if (count == 0 || count > sizeof(optstr))515return -EINVAL;516if (copy_from_user(optstr, user, count))517return -EFAULT;518optstr[count - 1] = '\0';519sn2_flush_opt = simple_strtoul(optstr, NULL, 0);520521for_each_online_cpu(cpu)522memset(&per_cpu(ptcstats, cpu), 0, sizeof(struct ptc_stats));523524return count;525}526527static const struct seq_operations sn2_ptc_seq_ops = {528.start = sn2_ptc_seq_start,529.next = sn2_ptc_seq_next,530.stop = sn2_ptc_seq_stop,531.show = sn2_ptc_seq_show532};533534static int sn2_ptc_proc_open(struct inode *inode, struct file *file)535{536return seq_open(file, &sn2_ptc_seq_ops);537}538539static const struct file_operations proc_sn2_ptc_operations = {540.open = sn2_ptc_proc_open,541.read = seq_read,542.write = sn2_ptc_proc_write,543.llseek = seq_lseek,544.release = seq_release,545};546547static struct proc_dir_entry *proc_sn2_ptc;548549static int __init sn2_ptc_init(void)550{551if (!ia64_platform_is("sn2"))552return 0;553554proc_sn2_ptc = proc_create(PTC_BASENAME, 0444,555NULL, &proc_sn2_ptc_operations);556if (!proc_sn2_ptc) {557printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);558return -EINVAL;559}560spin_lock_init(&sn2_global_ptc_lock);561return 0;562}563564static void __exit sn2_ptc_exit(void)565{566remove_proc_entry(PTC_BASENAME, NULL);567}568569module_init(sn2_ptc_init);570module_exit(sn2_ptc_exit);571#endif /* CONFIG_PROC_FS */572573574575