Home | History | Annotate | Line # | Download | only in arm32
      1  1.8     skrll /*	$NetBSD: cortex_pmc.c,v 1.8 2020/06/20 07:10:36 skrll Exp $	*/
      2  1.3     skrll 
      3  1.1      matt /* Copyright (c) 2007 Microsoft
      4  1.1      matt  * All rights reserved.
      5  1.1      matt  *
      6  1.1      matt  * Redistribution and use in source and binary forms, with or without
      7  1.1      matt  * modification, are permitted provided that the following conditions
      8  1.1      matt  * are met:
      9  1.1      matt  * 1. Redistributions of source code must retain the above copyright
     10  1.1      matt  *    notice, this list of conditions and the following disclaimer.
     11  1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     13  1.1      matt  *    documentation and/or other materials provided with the distribution.
     14  1.1      matt  * 3. All advertising materials mentioning features or use of this software
     15  1.1      matt  *    must display the following acknowledgement:
     16  1.1      matt  *	This product includes software developed by Microsoft
     17  1.1      matt  *
     18  1.1      matt  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     19  1.1      matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     22  1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1      matt  */
     30  1.1      matt 
     31  1.1      matt 
     32  1.1      matt /*
     33  1.2      matt  * support for ARM cortex Performance Monitor Counters
     34  1.1      matt  * based on arm11_pmc.c
     35  1.1      matt  */
     36  1.1      matt 
     37  1.1      matt #include <sys/cdefs.h>
     38  1.8     skrll /* __KERNEL_RCSID(0, "$NetBSD: cortex_pmc.c,v 1.8 2020/06/20 07:10:36 skrll Exp $"); */
     39  1.8     skrll 
     40  1.8     skrll #include <sys/param.h>
     41  1.1      matt #include <sys/types.h>
     42  1.8     skrll 
     43  1.8     skrll #include <sys/kernel.h>
     44  1.1      matt #include <sys/systm.h>
     45  1.1      matt #include <sys/time.h>
     46  1.1      matt #include <sys/timetc.h>
     47  1.2      matt 
     48  1.1      matt #include <dev/clock_subr.h>
     49  1.2      matt 
     50  1.2      matt #include <uvm/uvm_extern.h>
     51  1.2      matt 
     52  1.1      matt #include <arm/armreg.h>
     53  1.1      matt #include <arm/cpufunc.h>
     54  1.2      matt #include <arm/arm32/machdep.h>
     55  1.1      matt 
     56  1.1      matt #ifndef CORTEX_PMC_CCNT_HZ
     57  1.1      matt # define CORTEX_PMC_CCNT_HZ	400000000	/* 400MHz */
     58  1.1      matt #endif
     59  1.1      matt 
     60  1.2      matt #define COUNTS_PER_USEC	(curcpu()->ci_data.cpu_cc_freq / (1000*1000))
     61  1.1      matt 
     62  1.2      matt static const uint32_t counts_per_wrap = ~0UL - 1;
     63  1.1      matt 
     64  1.1      matt /*
     65  1.1      matt  * enable the PMC CCNT for delay()
     66  1.1      matt  */
     67  1.1      matt void
     68  1.2      matt cortex_pmc_ccnt_init(void)
     69  1.1      matt {
     70  1.2      matt 	if (curcpu()->ci_data.cpu_cc_freq == 0) {
     71  1.2      matt 		curcpu()->ci_data.cpu_cc_freq = CORTEX_PMC_CCNT_HZ;
     72  1.2      matt 	}
     73  1.1      matt }
     74  1.1      matt 
     75  1.1      matt /*
     76  1.1      matt  * delay - for "at least" arg usec
     77  1.1      matt  *
     78  1.1      matt  *	NOTE: at 400MHz we are restricted to (uint32_t)~0 "counts"
     79  1.1      matt  *	if this is a problem, accumulate counts in LL vars
     80  1.1      matt  */
     81  1.1      matt void
     82  1.1      matt delay(u_int arg)
     83  1.1      matt {
     84  1.1      matt 	uint32_t ctrl;
     85  1.1      matt 	uint32_t cur;
     86  1.1      matt 	uint32_t last;
     87  1.1      matt 	uint32_t delta = 0;
     88  1.1      matt 	uint32_t usecs = 0;
     89  1.2      matt 	const uint32_t counts_per_usec = COUNTS_PER_USEC;
     90  1.2      matt 	const uint32_t delay_arg_limit = ~0UL / counts_per_usec; /* about 10 sec */
     91  1.1      matt 
     92  1.2      matt 	if (arg > delay_arg_limit)
     93  1.2      matt 		panic("%s: arg %u overflow, limit is %u usec\n",
     94  1.2      matt 		    __func__, arg, delay_arg_limit);
     95  1.1      matt 
     96  1.2      matt 	last = armreg_pmccntr_read();
     97  1.1      matt 	delta = usecs = 0;
     98  1.1      matt 	while (arg > usecs) {
     99  1.2      matt 		cur = armreg_pmccntr_read();
    100  1.3     skrll 
    101  1.5     skrll 		/*
    102  1.5     skrll 		 * overflow flag is moved to a separate register
    103  1.5     skrll 		 * and is not read from PMC Control Register
    104  1.5     skrll 		 */
    105  1.2      matt 		ctrl = armreg_pmovsr_read();
    106  1.2      matt 		if (ctrl & CORTEX_CNTOFL_C) {
    107  1.5     skrll 			/*
    108  1.5     skrll 			 * Reset overflow flag for cycle counter in overflow
    109  1.5     skrll 			 * register
    110  1.5     skrll 			 */
    111  1.2      matt 			armreg_pmovsr_write(CORTEX_CNTOFL_C);
    112  1.4  knakahar 			delta += (cur + (counts_per_wrap - last));
    113  1.1      matt 		} else {
    114  1.1      matt 			delta += (cur - last);
    115  1.1      matt 		}
    116  1.1      matt 		last = cur;
    117  1.2      matt 		if (delta >= counts_per_usec) {
    118  1.2      matt 			usecs += delta / counts_per_usec;
    119  1.2      matt 			delta %= counts_per_usec;
    120  1.1      matt 		}
    121  1.1      matt 	}
    122  1.1      matt }
    123