18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public 38c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive 48c2ecf20Sopenharmony_ci * for more details. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 1998, 1999, 2003 by Ralf Baechle 78c2ecf20Sopenharmony_ci * Copyright (C) 2014 by Maciej W. Rozycki 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#ifndef _ASM_TIMEX_H 108c2ecf20Sopenharmony_ci#define _ASM_TIMEX_H 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#ifdef __KERNEL__ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/compiler.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <asm/cpu.h> 178c2ecf20Sopenharmony_ci#include <asm/cpu-features.h> 188c2ecf20Sopenharmony_ci#include <asm/mipsregs.h> 198c2ecf20Sopenharmony_ci#include <asm/cpu-type.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * This is the clock rate of the i8253 PIT. A MIPS system may not have 238c2ecf20Sopenharmony_ci * a PIT by the symbol is used all over the kernel including some APIs. 248c2ecf20Sopenharmony_ci * So keeping it defined to the number for the PIT is the only sane thing 258c2ecf20Sopenharmony_ci * for now. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci#define CLOCK_TICK_RATE 1193182 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* 308c2ecf20Sopenharmony_ci * Standard way to access the cycle counter. 318c2ecf20Sopenharmony_ci * Currently only used on SMP for scheduling. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * Only the low 32 bits are available as a continuously counting entity. 348c2ecf20Sopenharmony_ci * But this only means we'll force a reschedule every 8 seconds or so, 358c2ecf20Sopenharmony_ci * which isn't an evil thing. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * We know that all SMP capable CPUs have cycle counters. 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_citypedef unsigned int cycles_t; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* 438c2ecf20Sopenharmony_ci * On R4000/R4400 an erratum exists such that if the cycle counter is 448c2ecf20Sopenharmony_ci * read in the exact moment that it is matching the compare register, 458c2ecf20Sopenharmony_ci * no interrupt will be generated. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * There is a suggested workaround and also the erratum can't strike if 488c2ecf20Sopenharmony_ci * the compare interrupt isn't being used as the clock source device. 498c2ecf20Sopenharmony_ci * However for now the implementaton of this function doesn't get these 508c2ecf20Sopenharmony_ci * fine details right. 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_cistatic inline int can_use_mips_counter(unsigned int prid) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci int comp = (prid & PRID_COMP_MASK) != PRID_COMP_LEGACY; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci if (__builtin_constant_p(cpu_has_counter) && !cpu_has_counter) 578c2ecf20Sopenharmony_ci return 0; 588c2ecf20Sopenharmony_ci else if (__builtin_constant_p(cpu_has_mips_r) && cpu_has_mips_r) 598c2ecf20Sopenharmony_ci return 1; 608c2ecf20Sopenharmony_ci else if (likely(!__builtin_constant_p(cpu_has_mips_r) && comp)) 618c2ecf20Sopenharmony_ci return 1; 628c2ecf20Sopenharmony_ci /* Make sure we don't peek at cpu_data[0].options in the fast path! */ 638c2ecf20Sopenharmony_ci if (!__builtin_constant_p(cpu_has_counter)) 648c2ecf20Sopenharmony_ci asm volatile("" : "=m" (cpu_data[0].options)); 658c2ecf20Sopenharmony_ci if (likely(cpu_has_counter && 668c2ecf20Sopenharmony_ci prid > (PRID_IMP_R4000 | PRID_REV_ENCODE_44(15, 15)))) 678c2ecf20Sopenharmony_ci return 1; 688c2ecf20Sopenharmony_ci else 698c2ecf20Sopenharmony_ci return 0; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic inline cycles_t get_cycles(void) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci if (can_use_mips_counter(read_c0_prid())) 758c2ecf20Sopenharmony_ci return read_c0_count(); 768c2ecf20Sopenharmony_ci else 778c2ecf20Sopenharmony_ci return 0; /* no usable counter */ 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci#define get_cycles get_cycles 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* 828c2ecf20Sopenharmony_ci * Like get_cycles - but where c0_count is not available we desperately 838c2ecf20Sopenharmony_ci * use c0_random in an attempt to get at least a little bit of entropy. 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_cistatic inline unsigned long random_get_entropy(void) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci unsigned int c0_random; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (can_use_mips_counter(read_c0_prid())) 908c2ecf20Sopenharmony_ci return read_c0_count(); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (cpu_has_3kex) 938c2ecf20Sopenharmony_ci c0_random = (read_c0_random() >> 8) & 0x3f; 948c2ecf20Sopenharmony_ci else 958c2ecf20Sopenharmony_ci c0_random = read_c0_random() & 0x3f; 968c2ecf20Sopenharmony_ci return (random_get_entropy_fallback() << 6) | (0x3f - c0_random); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci#define random_get_entropy random_get_entropy 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */ 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci#endif /* _ASM_TIMEX_H */ 103