123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/init.h>
- #include <linux/export.h>
- #include <linux/timer.h>
- #include <linux/acpi_pmtmr.h>
- #include <linux/cpufreq.h>
- #include <linux/delay.h>
- #include <linux/clocksource.h>
- #include <linux/percpu.h>
- #include <linux/timex.h>
- #include <linux/static_key.h>
- #include <asm/hpet.h>
- #include <asm/timer.h>
- #include <asm/vgtod.h>
- #include <asm/time.h>
- #include <asm/delay.h>
- #include <asm/hypervisor.h>
- #include <asm/nmi.h>
- #include <asm/x86_init.h>
- #include <asm/geode.h>
- #include <asm/apic.h>
- #include <asm/intel-family.h>
- unsigned int __read_mostly cpu_khz;
- EXPORT_SYMBOL(cpu_khz);
- unsigned int __read_mostly tsc_khz;
- EXPORT_SYMBOL(tsc_khz);
- static int __read_mostly tsc_unstable;
- static int __read_mostly tsc_disabled = -1;
- static DEFINE_STATIC_KEY_FALSE(__use_tsc);
- int tsc_clocksource_reliable;
- static u32 art_to_tsc_numerator;
- static u32 art_to_tsc_denominator;
- static u64 art_to_tsc_offset;
- struct clocksource *art_related_clocksource;
- struct cyc2ns {
- struct cyc2ns_data data[2];
- struct cyc2ns_data *head;
- struct cyc2ns_data *tail;
- };
- static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns);
- struct cyc2ns_data *cyc2ns_read_begin(void)
- {
- struct cyc2ns_data *head;
- preempt_disable();
- head = this_cpu_read(cyc2ns.head);
-
- smp_read_barrier_depends();
- head->__count++;
- barrier();
- return head;
- }
- void cyc2ns_read_end(struct cyc2ns_data *head)
- {
- barrier();
-
- if (!--head->__count) {
-
- this_cpu_write(cyc2ns.tail, head);
- }
- preempt_enable();
- }
- static struct cyc2ns_data *cyc2ns_write_begin(int cpu)
- {
- struct cyc2ns *c2n = &per_cpu(cyc2ns, cpu);
- struct cyc2ns_data *data = c2n->data;
- if (data == c2n->head)
- data++;
-
-
- while (c2n->tail == data)
- cpu_relax();
- return data;
- }
- static void cyc2ns_write_end(int cpu, struct cyc2ns_data *data)
- {
- struct cyc2ns *c2n = &per_cpu(cyc2ns, cpu);
-
- smp_wmb();
- ACCESS_ONCE(c2n->head) = data;
- }
- static void cyc2ns_data_init(struct cyc2ns_data *data)
- {
- data->cyc2ns_mul = 0;
- data->cyc2ns_shift = 0;
- data->cyc2ns_offset = 0;
- data->__count = 0;
- }
- static void cyc2ns_init(int cpu)
- {
- struct cyc2ns *c2n = &per_cpu(cyc2ns, cpu);
- cyc2ns_data_init(&c2n->data[0]);
- cyc2ns_data_init(&c2n->data[1]);
- c2n->head = c2n->data;
- c2n->tail = c2n->data;
- }
- static inline unsigned long long cycles_2_ns(unsigned long long cyc)
- {
- struct cyc2ns_data *data, *tail;
- unsigned long long ns;
-
- preempt_disable_notrace();
- data = this_cpu_read(cyc2ns.head);
- tail = this_cpu_read(cyc2ns.tail);
- if (likely(data == tail)) {
- ns = data->cyc2ns_offset;
- ns += mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
- } else {
- data->__count++;
- barrier();
- ns = data->cyc2ns_offset;
- ns += mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
- barrier();
- if (!--data->__count)
- this_cpu_write(cyc2ns.tail, data);
- }
- preempt_enable_notrace();
- return ns;
- }
- static void set_cyc2ns_scale(unsigned long khz, int cpu)
- {
- unsigned long long tsc_now, ns_now;
- struct cyc2ns_data *data;
- unsigned long flags;
- local_irq_save(flags);
- sched_clock_idle_sleep_event();
- if (!khz)
- goto done;
- data = cyc2ns_write_begin(cpu);
- tsc_now = rdtsc();
- ns_now = cycles_2_ns(tsc_now);
-
- clocks_calc_mult_shift(&data->cyc2ns_mul, &data->cyc2ns_shift, khz,
- NSEC_PER_MSEC, 0);
-
- if (data->cyc2ns_shift == 32) {
- data->cyc2ns_shift = 31;
- data->cyc2ns_mul >>= 1;
- }
- data->cyc2ns_offset = ns_now -
- mul_u64_u32_shr(tsc_now, data->cyc2ns_mul, data->cyc2ns_shift);
- cyc2ns_write_end(cpu, data);
- done:
- sched_clock_idle_wakeup_event(0);
- local_irq_restore(flags);
- }
- u64 native_sched_clock(void)
- {
- if (static_branch_likely(&__use_tsc)) {
- u64 tsc_now = rdtsc();
-
- return cycles_2_ns(tsc_now);
- }
-
-
- return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
- }
- u64 native_sched_clock_from_tsc(u64 tsc)
- {
- return cycles_2_ns(tsc);
- }
- #ifdef CONFIG_PARAVIRT
- unsigned long long sched_clock(void)
- {
- return paravirt_sched_clock();
- }
- #else
- unsigned long long
- sched_clock(void) __attribute__((alias("native_sched_clock")));
- #endif
- int check_tsc_unstable(void)
- {
- return tsc_unstable;
- }
- EXPORT_SYMBOL_GPL(check_tsc_unstable);
- #ifdef CONFIG_X86_TSC
- int __init notsc_setup(char *str)
- {
- pr_warn("Kernel compiled with CONFIG_X86_TSC, cannot disable TSC completely\n");
- tsc_disabled = 1;
- return 1;
- }
- #else
- int __init notsc_setup(char *str)
- {
- setup_clear_cpu_cap(X86_FEATURE_TSC);
- return 1;
- }
- #endif
- __setup("notsc", notsc_setup);
- static int no_sched_irq_time;
- static int __init tsc_setup(char *str)
- {
- if (!strcmp(str, "reliable"))
- tsc_clocksource_reliable = 1;
- if (!strncmp(str, "noirqtime", 9))
- no_sched_irq_time = 1;
- return 1;
- }
- __setup("tsc=", tsc_setup);
- #define MAX_RETRIES 5
- #define SMI_TRESHOLD 50000
- static u64 tsc_read_refs(u64 *p, int hpet)
- {
- u64 t1, t2;
- int i;
- for (i = 0; i < MAX_RETRIES; i++) {
- t1 = get_cycles();
- if (hpet)
- *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
- else
- *p = acpi_pm_read_early();
- t2 = get_cycles();
- if ((t2 - t1) < SMI_TRESHOLD)
- return t2;
- }
- return ULLONG_MAX;
- }
- static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
- {
- u64 tmp;
- if (hpet2 < hpet1)
- hpet2 += 0x100000000ULL;
- hpet2 -= hpet1;
- tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
- do_div(tmp, 1000000);
- do_div(deltatsc, tmp);
- return (unsigned long) deltatsc;
- }
- static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
- {
- u64 tmp;
- if (!pm1 && !pm2)
- return ULONG_MAX;
- if (pm2 < pm1)
- pm2 += (u64)ACPI_PM_OVRRUN;
- pm2 -= pm1;
- tmp = pm2 * 1000000000LL;
- do_div(tmp, PMTMR_TICKS_PER_SEC);
- do_div(deltatsc, tmp);
- return (unsigned long) deltatsc;
- }
- #define CAL_MS 10
- #define CAL_LATCH (PIT_TICK_RATE / (1000 / CAL_MS))
- #define CAL_PIT_LOOPS 1000
- #define CAL2_MS 50
- #define CAL2_LATCH (PIT_TICK_RATE / (1000 / CAL2_MS))
- #define CAL2_PIT_LOOPS 5000
- static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
- {
- u64 tsc, t1, t2, delta;
- unsigned long tscmin, tscmax;
- int pitcnt;
-
- outb((inb(0x61) & ~0x02) | 0x01, 0x61);
-
- outb(0xb0, 0x43);
- outb(latch & 0xff, 0x42);
- outb(latch >> 8, 0x42);
- tsc = t1 = t2 = get_cycles();
- pitcnt = 0;
- tscmax = 0;
- tscmin = ULONG_MAX;
- while ((inb(0x61) & 0x20) == 0) {
- t2 = get_cycles();
- delta = t2 - tsc;
- tsc = t2;
- if ((unsigned long) delta < tscmin)
- tscmin = (unsigned int) delta;
- if ((unsigned long) delta > tscmax)
- tscmax = (unsigned int) delta;
- pitcnt++;
- }
-
- if (pitcnt < loopmin || tscmax > 10 * tscmin)
- return ULONG_MAX;
-
- delta = t2 - t1;
- do_div(delta, ms);
- return delta;
- }
- static inline int pit_verify_msb(unsigned char val)
- {
-
- inb(0x42);
- return inb(0x42) == val;
- }
- static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap)
- {
- int count;
- u64 tsc = 0, prev_tsc = 0;
- for (count = 0; count < 50000; count++) {
- if (!pit_verify_msb(val))
- break;
- prev_tsc = tsc;
- tsc = get_cycles();
- }
- *deltap = get_cycles() - prev_tsc;
- *tscp = tsc;
-
- return count > 5;
- }
- #define MAX_QUICK_PIT_MS 50
- #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
- static unsigned long quick_pit_calibrate(void)
- {
- int i;
- u64 tsc, delta;
- unsigned long d1, d2;
-
- outb((inb(0x61) & ~0x02) | 0x01, 0x61);
-
- outb(0xb0, 0x43);
-
- outb(0xff, 0x42);
- outb(0xff, 0x42);
-
- pit_verify_msb(0);
- if (pit_expect_msb(0xff, &tsc, &d1)) {
- for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
- if (!pit_expect_msb(0xff-i, &delta, &d2))
- break;
- delta -= tsc;
-
- if (i == 1 &&
- d1 + d2 >= (delta * MAX_QUICK_PIT_ITERATIONS) >> 11)
- return 0;
-
- if (d1+d2 >= delta >> 11)
- continue;
-
- if (!pit_verify_msb(0xfe - i))
- break;
- goto success;
- }
- }
- pr_info("Fast TSC calibration failed\n");
- return 0;
- success:
-
- delta *= PIT_TICK_RATE;
- do_div(delta, i*256*1000);
- pr_info("Fast TSC calibration using PIT\n");
- return delta;
- }
- unsigned long native_calibrate_tsc(void)
- {
- unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
- unsigned int crystal_khz;
- if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
- return 0;
- if (boot_cpu_data.cpuid_level < 0x15)
- return 0;
- eax_denominator = ebx_numerator = ecx_hz = edx = 0;
-
- cpuid(0x15, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);
- if (ebx_numerator == 0 || eax_denominator == 0)
- return 0;
- crystal_khz = ecx_hz / 1000;
- if (crystal_khz == 0) {
- switch (boot_cpu_data.x86_model) {
- case INTEL_FAM6_SKYLAKE_MOBILE:
- case INTEL_FAM6_SKYLAKE_DESKTOP:
- case INTEL_FAM6_KABYLAKE_MOBILE:
- case INTEL_FAM6_KABYLAKE_DESKTOP:
- crystal_khz = 24000;
- break;
- case INTEL_FAM6_SKYLAKE_X:
- case INTEL_FAM6_ATOM_DENVERTON:
- crystal_khz = 25000;
- break;
- case INTEL_FAM6_ATOM_GOLDMONT:
- crystal_khz = 19200;
- break;
- }
- }
- return crystal_khz * ebx_numerator / eax_denominator;
- }
- static unsigned long cpu_khz_from_cpuid(void)
- {
- unsigned int eax_base_mhz, ebx_max_mhz, ecx_bus_mhz, edx;
- if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
- return 0;
- if (boot_cpu_data.cpuid_level < 0x16)
- return 0;
- eax_base_mhz = ebx_max_mhz = ecx_bus_mhz = edx = 0;
- cpuid(0x16, &eax_base_mhz, &ebx_max_mhz, &ecx_bus_mhz, &edx);
- return eax_base_mhz * 1000;
- }
- unsigned long native_calibrate_cpu(void)
- {
- u64 tsc1, tsc2, delta, ref1, ref2;
- unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
- unsigned long flags, latch, ms, fast_calibrate;
- int hpet = is_hpet_enabled(), i, loopmin;
- fast_calibrate = cpu_khz_from_cpuid();
- if (fast_calibrate)
- return fast_calibrate;
- fast_calibrate = cpu_khz_from_msr();
- if (fast_calibrate)
- return fast_calibrate;
- local_irq_save(flags);
- fast_calibrate = quick_pit_calibrate();
- local_irq_restore(flags);
- if (fast_calibrate)
- return fast_calibrate;
-
-
- latch = CAL_LATCH;
- ms = CAL_MS;
- loopmin = CAL_PIT_LOOPS;
- for (i = 0; i < 3; i++) {
- unsigned long tsc_pit_khz;
-
- local_irq_save(flags);
- tsc1 = tsc_read_refs(&ref1, hpet);
- tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin);
- tsc2 = tsc_read_refs(&ref2, hpet);
- local_irq_restore(flags);
-
- tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
-
- if (ref1 == ref2)
- continue;
-
- if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
- continue;
- tsc2 = (tsc2 - tsc1) * 1000000LL;
- if (hpet)
- tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
- else
- tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
- tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
-
- delta = ((u64) tsc_pit_min) * 100;
- do_div(delta, tsc_ref_min);
-
- if (delta >= 90 && delta <= 110) {
- pr_info("PIT calibration matches %s. %d loops\n",
- hpet ? "HPET" : "PMTIMER", i + 1);
- return tsc_ref_min;
- }
-
- if (i == 1 && tsc_pit_min == ULONG_MAX) {
- latch = CAL2_LATCH;
- ms = CAL2_MS;
- loopmin = CAL2_PIT_LOOPS;
- }
- }
-
- if (tsc_pit_min == ULONG_MAX) {
-
- pr_warn("Unable to calibrate against PIT\n");
-
- if (!hpet && !ref1 && !ref2) {
- pr_notice("No reference (HPET/PMTIMER) available\n");
- return 0;
- }
-
- if (tsc_ref_min == ULONG_MAX) {
- pr_warn("HPET/PMTIMER calibration failed\n");
- return 0;
- }
-
- pr_info("using %s reference calibration\n",
- hpet ? "HPET" : "PMTIMER");
- return tsc_ref_min;
- }
-
- if (!hpet && !ref1 && !ref2) {
- pr_info("Using PIT calibration value\n");
- return tsc_pit_min;
- }
-
- if (tsc_ref_min == ULONG_MAX) {
- pr_warn("HPET/PMTIMER calibration failed. Using PIT calibration.\n");
- return tsc_pit_min;
- }
-
- pr_warn("PIT calibration deviates from %s: %lu %lu\n",
- hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
- pr_info("Using PIT calibration value\n");
- return tsc_pit_min;
- }
- int recalibrate_cpu_khz(void)
- {
- #ifndef CONFIG_SMP
- unsigned long cpu_khz_old = cpu_khz;
- if (!boot_cpu_has(X86_FEATURE_TSC))
- return -ENODEV;
- cpu_khz = x86_platform.calibrate_cpu();
- tsc_khz = x86_platform.calibrate_tsc();
- if (tsc_khz == 0)
- tsc_khz = cpu_khz;
- else if (abs(cpu_khz - tsc_khz) * 10 > tsc_khz)
- cpu_khz = tsc_khz;
- cpu_data(0).loops_per_jiffy = cpufreq_scale(cpu_data(0).loops_per_jiffy,
- cpu_khz_old, cpu_khz);
- return 0;
- #else
- return -ENODEV;
- #endif
- }
- EXPORT_SYMBOL(recalibrate_cpu_khz);
- static unsigned long long cyc2ns_suspend;
- void tsc_save_sched_clock_state(void)
- {
- if (!sched_clock_stable())
- return;
- cyc2ns_suspend = sched_clock();
- }
- void tsc_restore_sched_clock_state(void)
- {
- unsigned long long offset;
- unsigned long flags;
- int cpu;
- if (!sched_clock_stable())
- return;
- local_irq_save(flags);
-
- this_cpu_write(cyc2ns.data[0].cyc2ns_offset, 0);
- this_cpu_write(cyc2ns.data[1].cyc2ns_offset, 0);
- offset = cyc2ns_suspend - sched_clock();
- for_each_possible_cpu(cpu) {
- per_cpu(cyc2ns.data[0].cyc2ns_offset, cpu) = offset;
- per_cpu(cyc2ns.data[1].cyc2ns_offset, cpu) = offset;
- }
- local_irq_restore(flags);
- }
- #ifdef CONFIG_CPU_FREQ
- static unsigned int ref_freq;
- static unsigned long loops_per_jiffy_ref;
- static unsigned long tsc_khz_ref;
- static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
- void *data)
- {
- struct cpufreq_freqs *freq = data;
- unsigned long *lpj;
- lpj = &boot_cpu_data.loops_per_jiffy;
- #ifdef CONFIG_SMP
- if (!(freq->flags & CPUFREQ_CONST_LOOPS))
- lpj = &cpu_data(freq->cpu).loops_per_jiffy;
- #endif
- if (!ref_freq) {
- ref_freq = freq->old;
- loops_per_jiffy_ref = *lpj;
- tsc_khz_ref = tsc_khz;
- }
- if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
- (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
- *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
- tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
- if (!(freq->flags & CPUFREQ_CONST_LOOPS))
- mark_tsc_unstable("cpufreq changes");
- set_cyc2ns_scale(tsc_khz, freq->cpu);
- }
- return 0;
- }
- static struct notifier_block time_cpufreq_notifier_block = {
- .notifier_call = time_cpufreq_notifier
- };
- static int __init cpufreq_register_tsc_scaling(void)
- {
- if (!boot_cpu_has(X86_FEATURE_TSC))
- return 0;
- if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
- return 0;
- cpufreq_register_notifier(&time_cpufreq_notifier_block,
- CPUFREQ_TRANSITION_NOTIFIER);
- return 0;
- }
- core_initcall(cpufreq_register_tsc_scaling);
- #endif
- #define ART_CPUID_LEAF (0x15)
- #define ART_MIN_DENOMINATOR (1)
- static void detect_art(void)
- {
- unsigned int unused[2];
- if (boot_cpu_data.cpuid_level < ART_CPUID_LEAF)
- return;
- cpuid(ART_CPUID_LEAF, &art_to_tsc_denominator,
- &art_to_tsc_numerator, unused, unused+1);
-
- if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
- !boot_cpu_has(X86_FEATURE_NONSTOP_TSC) ||
- art_to_tsc_denominator < ART_MIN_DENOMINATOR)
- return;
- if (rdmsrl_safe(MSR_IA32_TSC_ADJUST, &art_to_tsc_offset))
- return;
-
- setup_force_cpu_cap(X86_FEATURE_ART);
- }
- static struct clocksource clocksource_tsc;
- static cycle_t read_tsc(struct clocksource *cs)
- {
- return (cycle_t)rdtsc_ordered();
- }
- static struct clocksource clocksource_tsc = {
- .name = "tsc",
- .rating = 300,
- .read = read_tsc,
- .mask = CLOCKSOURCE_MASK(64),
- .flags = CLOCK_SOURCE_IS_CONTINUOUS |
- CLOCK_SOURCE_MUST_VERIFY,
- .archdata = { .vclock_mode = VCLOCK_TSC },
- };
- void mark_tsc_unstable(char *reason)
- {
- if (!tsc_unstable) {
- tsc_unstable = 1;
- clear_sched_clock_stable();
- disable_sched_clock_irqtime();
- pr_info("Marking TSC unstable due to %s\n", reason);
-
- if (clocksource_tsc.mult)
- clocksource_mark_unstable(&clocksource_tsc);
- else {
- clocksource_tsc.flags |= CLOCK_SOURCE_UNSTABLE;
- clocksource_tsc.rating = 0;
- }
- }
- }
- EXPORT_SYMBOL_GPL(mark_tsc_unstable);
- static void __init check_system_tsc_reliable(void)
- {
- #if defined(CONFIG_MGEODEGX1) || defined(CONFIG_MGEODE_LX) || defined(CONFIG_X86_GENERIC)
- if (is_geode_lx()) {
-
- #define RTSC_SUSP 0x100
- unsigned long res_low, res_high;
- rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
-
- if (res_low & RTSC_SUSP)
- tsc_clocksource_reliable = 1;
- }
- #endif
- if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE))
- tsc_clocksource_reliable = 1;
- }
- int unsynchronized_tsc(void)
- {
- if (!boot_cpu_has(X86_FEATURE_TSC) || tsc_unstable)
- return 1;
- #ifdef CONFIG_SMP
- if (apic_is_clustered_box())
- return 1;
- #endif
- if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
- return 0;
- if (tsc_clocksource_reliable)
- return 0;
-
- if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
-
- if (num_possible_cpus() > 1)
- return 1;
- }
- return 0;
- }
- struct system_counterval_t convert_art_to_tsc(cycle_t art)
- {
- u64 tmp, res, rem;
- rem = do_div(art, art_to_tsc_denominator);
- res = art * art_to_tsc_numerator;
- tmp = rem * art_to_tsc_numerator;
- do_div(tmp, art_to_tsc_denominator);
- res += tmp + art_to_tsc_offset;
- return (struct system_counterval_t) {.cs = art_related_clocksource,
- .cycles = res};
- }
- EXPORT_SYMBOL(convert_art_to_tsc);
- static void tsc_refine_calibration_work(struct work_struct *work);
- static DECLARE_DELAYED_WORK(tsc_irqwork, tsc_refine_calibration_work);
- static void tsc_refine_calibration_work(struct work_struct *work)
- {
- static u64 tsc_start = -1, ref_start;
- static int hpet;
- u64 tsc_stop, ref_stop, delta;
- unsigned long freq;
-
- if (check_tsc_unstable())
- goto out;
-
- if (tsc_start == -1) {
-
- hpet = is_hpet_enabled();
- schedule_delayed_work(&tsc_irqwork, HZ);
- tsc_start = tsc_read_refs(&ref_start, hpet);
- return;
- }
- tsc_stop = tsc_read_refs(&ref_stop, hpet);
-
- if (ref_start == ref_stop)
- goto out;
-
- if (tsc_start == ULLONG_MAX || tsc_stop == ULLONG_MAX)
- goto out;
- delta = tsc_stop - tsc_start;
- delta *= 1000000LL;
- if (hpet)
- freq = calc_hpet_ref(delta, ref_start, ref_stop);
- else
- freq = calc_pmtimer_ref(delta, ref_start, ref_stop);
-
- if (abs(tsc_khz - freq) > tsc_khz/100)
- goto out;
- tsc_khz = freq;
- pr_info("Refined TSC clocksource calibration: %lu.%03lu MHz\n",
- (unsigned long)tsc_khz / 1000,
- (unsigned long)tsc_khz % 1000);
-
- lapic_update_tsc_freq();
- out:
- if (boot_cpu_has(X86_FEATURE_ART))
- art_related_clocksource = &clocksource_tsc;
- clocksource_register_khz(&clocksource_tsc, tsc_khz);
- }
- static int __init init_tsc_clocksource(void)
- {
- if (!boot_cpu_has(X86_FEATURE_TSC) || tsc_disabled > 0 || !tsc_khz)
- return 0;
- if (tsc_clocksource_reliable)
- clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
-
- if (check_tsc_unstable()) {
- clocksource_tsc.rating = 0;
- clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
- }
- if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC_S3))
- clocksource_tsc.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
-
- if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) {
- if (boot_cpu_has(X86_FEATURE_ART))
- art_related_clocksource = &clocksource_tsc;
- clocksource_register_khz(&clocksource_tsc, tsc_khz);
- return 0;
- }
- schedule_delayed_work(&tsc_irqwork, 0);
- return 0;
- }
- device_initcall(init_tsc_clocksource);
- void __init tsc_init(void)
- {
- u64 lpj;
- int cpu;
- if (!boot_cpu_has(X86_FEATURE_TSC)) {
- setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
- return;
- }
- cpu_khz = x86_platform.calibrate_cpu();
- tsc_khz = x86_platform.calibrate_tsc();
-
- if (tsc_khz == 0)
- tsc_khz = cpu_khz;
- else if (abs(cpu_khz - tsc_khz) * 10 > tsc_khz)
- cpu_khz = tsc_khz;
- if (!tsc_khz) {
- mark_tsc_unstable("could not calculate TSC khz");
- setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
- return;
- }
- pr_info("Detected %lu.%03lu MHz processor\n",
- (unsigned long)cpu_khz / 1000,
- (unsigned long)cpu_khz % 1000);
-
- for_each_possible_cpu(cpu) {
- cyc2ns_init(cpu);
- set_cyc2ns_scale(tsc_khz, cpu);
- }
- if (tsc_disabled > 0)
- return;
-
- tsc_disabled = 0;
- static_branch_enable(&__use_tsc);
- if (!no_sched_irq_time)
- enable_sched_clock_irqtime();
- lpj = ((u64)tsc_khz * 1000);
- do_div(lpj, HZ);
- lpj_fine = lpj;
- use_tsc_delay();
- if (unsynchronized_tsc())
- mark_tsc_unstable("TSCs unsynchronized");
- check_system_tsc_reliable();
- detect_art();
- }
- #ifdef CONFIG_SMP
- unsigned long calibrate_delay_is_known(void)
- {
- int sibling, cpu = smp_processor_id();
- struct cpumask *mask = topology_core_cpumask(cpu);
- if (!tsc_disabled && !cpu_has(&cpu_data(cpu), X86_FEATURE_CONSTANT_TSC))
- return 0;
- if (!mask)
- return 0;
- sibling = cpumask_any_but(mask, cpu);
- if (sibling < nr_cpu_ids)
- return cpu_data(sibling).loops_per_jiffy;
- return 0;
- }
- #endif
|