cvmx-tim.c revision 259065
1164190Sjkoshy/***********************license start*************** 2164190Sjkoshy * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights 3164190Sjkoshy * reserved. 4164190Sjkoshy * 5164190Sjkoshy * 6164190Sjkoshy * Redistribution and use in source and binary forms, with or without 7164190Sjkoshy * modification, are permitted provided that the following conditions are 8164190Sjkoshy * met: 9164190Sjkoshy * 10164190Sjkoshy * * Redistributions of source code must retain the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer. 12164190Sjkoshy * 13164190Sjkoshy * * Redistributions in binary form must reproduce the above 14164190Sjkoshy * copyright notice, this list of conditions and the following 15164190Sjkoshy * disclaimer in the documentation and/or other materials provided 16164190Sjkoshy * with the distribution. 17164190Sjkoshy 18164190Sjkoshy * * Neither the name of Cavium Inc. nor the names of 19164190Sjkoshy * its contributors may be used to endorse or promote products 20164190Sjkoshy * derived from this software without specific prior written 21164190Sjkoshy * permission. 22164190Sjkoshy 23164190Sjkoshy * This Software, including technical data, may be subject to U.S. export control 24164190Sjkoshy * laws, including the U.S. Export Administration Act and its associated 25164190Sjkoshy * regulations, and may be subject to export or import regulations in other 26164190Sjkoshy * countries. 27164190Sjkoshy 28164190Sjkoshy * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29164190Sjkoshy * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 30164190Sjkoshy * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31164190Sjkoshy * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32164190Sjkoshy * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33164190Sjkoshy * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34164190Sjkoshy * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35164190Sjkoshy * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36164190Sjkoshy * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37164190Sjkoshy * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38164190Sjkoshy ***********************license end**************************************/ 39164190Sjkoshy 40164190Sjkoshy 41164190Sjkoshy 42164190Sjkoshy 43164190Sjkoshy 44164190Sjkoshy 45164190Sjkoshy 46164190Sjkoshy/** 47164190Sjkoshy * @file 48164190Sjkoshy * 49164190Sjkoshy * Support library for the hardware work queue timers. 50164190Sjkoshy * 51164190Sjkoshy * <hr>$Revision: 70030 $<hr> 52164190Sjkoshy */ 53164190Sjkoshy#include "executive-config.h" 54164190Sjkoshy#include "cvmx-config.h" 55164190Sjkoshy#include "cvmx.h" 56164190Sjkoshy#include "cvmx-sysinfo.h" 57164190Sjkoshy#include "cvmx-tim.h" 58164190Sjkoshy#include "cvmx-bootmem.h" 59164190Sjkoshy 60164190Sjkoshy/* CSR typedefs have been moved to cvmx-tim-defs.h */ 61164190Sjkoshy 62164190Sjkoshy/** 63164190Sjkoshy * Global structure holding the state of all timers. 64164190Sjkoshy */ 65164190SjkoshyCVMX_SHARED cvmx_tim_t cvmx_tim; 66164190Sjkoshy 67164190Sjkoshy 68164190Sjkoshy#ifdef CVMX_ENABLE_TIMER_FUNCTIONS 69164190Sjkoshy/** 70164190Sjkoshy * Setup a timer for use. Must be called before the timer 71164190Sjkoshy * can be used. 72164190Sjkoshy * 73164190Sjkoshy * @param tick Time between each bucket in microseconds. This must not be 74164190Sjkoshy * smaller than 1024/(clock frequency in MHz). 75164190Sjkoshy * @param max_ticks The maximum number of ticks the timer must be able 76164190Sjkoshy * to schedule in the future. There are guaranteed to be enough 77164190Sjkoshy * timer buckets such that: 78164190Sjkoshy * number of buckets >= max_ticks. 79164190Sjkoshy * @return Zero on success. Negative on error. Failures are possible 80164190Sjkoshy * if the number of buckets needed is too large or memory 81164190Sjkoshy * allocation fails for creating the buckets. 82164190Sjkoshy */ 83164190Sjkoshyint cvmx_tim_setup(uint64_t tick, uint64_t max_ticks) 84164190Sjkoshy{ 85164190Sjkoshy uint64_t timer_id; 86164190Sjkoshy int error = -1; 87164190Sjkoshy uint64_t tim_clock_hz = cvmx_clock_get_rate(CVMX_CLOCK_TIM); 88164190Sjkoshy uint64_t hw_tick_ns; 89164190Sjkoshy uint64_t hw_tick_ns_allowed; 90164190Sjkoshy uint64_t tick_ns = 1000 * tick; 91164190Sjkoshy int i; 92164190Sjkoshy uint32_t temp; 93164190Sjkoshy int timer_thr = 1024; 94164190Sjkoshy 95164190Sjkoshy /* for the simulator */ 96164190Sjkoshy if (tim_clock_hz == 0) 97164190Sjkoshy tim_clock_hz = 800000000; 98164190Sjkoshy 99164190Sjkoshy if (OCTEON_IS_MODEL(OCTEON_CN68XX)) 100164190Sjkoshy { 101164190Sjkoshy cvmx_tim_fr_rn_tt_t fr_tt; 102164190Sjkoshy fr_tt.u64 = cvmx_read_csr(CVMX_TIM_FR_RN_TT); 103164190Sjkoshy timer_thr = fr_tt.s.fr_rn_tt; 104164190Sjkoshy } 105164190Sjkoshy 106164190Sjkoshy hw_tick_ns = timer_thr * 1000000000ull / tim_clock_hz; 107164190Sjkoshy /* 108164190Sjkoshy * Double the minimal allowed tick to 2 * HW tick. tick between 109164190Sjkoshy * (hw_tick_ns, 2*hw_tick_ns) will set config_ring1.s.interval 110164190Sjkoshy * to zero, or 1024 cycles. This is not enough time for the timer unit 111164190Sjkoshy * to fetch the bucket data, Resulting in timer ring error interrupt 112164190Sjkoshy * be always generated. Avoid such setting in software. 113164190Sjkoshy */ 114164190Sjkoshy hw_tick_ns_allowed = hw_tick_ns * 2; 115164190Sjkoshy 116164190Sjkoshy /* Make sure the timers are stopped */ 117164190Sjkoshy cvmx_tim_stop(); 118164190Sjkoshy 119164190Sjkoshy /* Reinitialize out timer state */ 120164190Sjkoshy memset(&cvmx_tim, 0, sizeof(cvmx_tim)); 121164190Sjkoshy 122164190Sjkoshy if (tick_ns < hw_tick_ns_allowed) 123164190Sjkoshy { 124164190Sjkoshy cvmx_dprintf("ERROR: cvmx_tim_setup: Requested tick %lu(ns) is smaller than" 125164190Sjkoshy " the minimal ticks allowed by hardware %lu(ns)\n", 126164190Sjkoshy tick_ns, hw_tick_ns_allowed); 127164190Sjkoshy return error; 128164190Sjkoshy } 129164190Sjkoshy else if (tick_ns > 4194304 * hw_tick_ns) 130164190Sjkoshy { 131164190Sjkoshy cvmx_dprintf("ERROR: cvmx_tim_setup: Requested tick %lu(ns) is greater than" 132164190Sjkoshy " the max ticks %lu(ns)\n", tick_ns, hw_tick_ns); 133164190Sjkoshy return error; 134164190Sjkoshy } 135164190Sjkoshy 136164190Sjkoshy for (i=2; i<20; i++) 137164190Sjkoshy { 138164190Sjkoshy if (tick_ns < (hw_tick_ns << i)) 139164190Sjkoshy break; 140210325Skaiw } 141210325Skaiw 142164190Sjkoshy cvmx_tim.max_ticks = (uint32_t)max_ticks; 143164190Sjkoshy cvmx_tim.bucket_shift = (uint32_t)(i - 1 + 10); 144164190Sjkoshy cvmx_tim.tick_cycles = tick * tim_clock_hz / 1000000; 145164190Sjkoshy 146164190Sjkoshy temp = (max_ticks * cvmx_tim.tick_cycles) >> cvmx_tim.bucket_shift; 147164190Sjkoshy 148164190Sjkoshy /* round up to nearest power of 2 */ 149164190Sjkoshy temp -= 1; 150164190Sjkoshy temp = temp | (temp >> 1); 151164190Sjkoshy temp = temp | (temp >> 2); 152164190Sjkoshy temp = temp | (temp >> 4); 153164190Sjkoshy temp = temp | (temp >> 8); 154164190Sjkoshy temp = temp | (temp >> 16); 155164190Sjkoshy cvmx_tim.num_buckets = temp + 1; 156164190Sjkoshy 157164190Sjkoshy /* ensure input params fall into permitted ranges */ 158164190Sjkoshy if ((cvmx_tim.num_buckets < 3) || cvmx_tim.num_buckets > 1048576) 159164190Sjkoshy { 160164190Sjkoshy cvmx_dprintf("ERROR: cvmx_tim_setup: num_buckets out of range\n"); 161164190Sjkoshy return error; 162164190Sjkoshy } 163164190Sjkoshy 164164190Sjkoshy /* Allocate the timer buckets from hardware addressable memory */ 165164190Sjkoshy cvmx_tim.bucket = cvmx_bootmem_alloc(CVMX_TIM_NUM_TIMERS * cvmx_tim.num_buckets 166164190Sjkoshy * sizeof(cvmx_tim_bucket_entry_t), CVMX_CACHE_LINE_SIZE); 167164190Sjkoshy if (cvmx_tim.bucket == NULL) 168 { 169 cvmx_dprintf("ERROR: cvmx_tim_setup: allocation problem\n"); 170 return error; 171 } 172 memset(cvmx_tim.bucket, 0, CVMX_TIM_NUM_TIMERS * cvmx_tim.num_buckets * sizeof(cvmx_tim_bucket_entry_t)); 173 174 cvmx_tim.start_time = 0; 175 176 /* Loop through all timers */ 177 for (timer_id = 0; timer_id<CVMX_TIM_NUM_TIMERS; timer_id++) 178 { 179 int interval = ((1 << (cvmx_tim.bucket_shift - 10)) - 1); 180 cvmx_tim_bucket_entry_t *bucket = cvmx_tim.bucket + timer_id * cvmx_tim.num_buckets; 181 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) 182 { 183 cvmx_tim_ringx_ctl0_t ring_ctl0; 184 cvmx_tim_ringx_ctl1_t ring_ctl1; 185 cvmx_tim_ringx_ctl2_t ring_ctl2; 186 cvmx_tim_reg_flags_t reg_flags; 187 188 /* Tell the hardware where about the bucket array */ 189 ring_ctl2.u64 = 0; 190 ring_ctl2.s.csize = CVMX_FPA_TIMER_POOL_SIZE / 8; 191 ring_ctl2.s.base = cvmx_ptr_to_phys(bucket) >> 5; 192 cvmx_write_csr(CVMX_TIM_RINGX_CTL2(timer_id), ring_ctl2.u64); 193 194 reg_flags.u64 = cvmx_read_csr(CVMX_TIM_REG_FLAGS); 195 ring_ctl1.u64 = 0; 196 ring_ctl1.s.cpool = ((reg_flags.s.ena_dfb == 0) ? CVMX_FPA_TIMER_POOL : 0); 197 ring_ctl1.s.bsize = cvmx_tim.num_buckets - 1; 198 cvmx_write_csr(CVMX_TIM_RINGX_CTL1(timer_id), ring_ctl1.u64); 199 200 ring_ctl0.u64 = 0; 201 ring_ctl0.s.timercount = interval + timer_id * interval / CVMX_TIM_NUM_TIMERS; 202 cvmx_write_csr(CVMX_TIM_RINGX_CTL0(timer_id), ring_ctl0.u64); 203 204 ring_ctl0.u64 = cvmx_read_csr(CVMX_TIM_RINGX_CTL0(timer_id)); 205 ring_ctl0.s.ena = 1; 206 ring_ctl0.s.interval = interval; 207 cvmx_write_csr(CVMX_TIM_RINGX_CTL0(timer_id), ring_ctl0.u64); 208 ring_ctl0.u64 = cvmx_read_csr(CVMX_TIM_RINGX_CTL0(timer_id)); 209 } 210 else 211 { 212 cvmx_tim_mem_ring0_t config_ring0; 213 cvmx_tim_mem_ring1_t config_ring1; 214 /* Tell the hardware where about the bucket array */ 215 config_ring0.u64 = 0; 216 config_ring0.s.first_bucket = cvmx_ptr_to_phys(bucket) >> 5; 217 config_ring0.s.num_buckets = cvmx_tim.num_buckets - 1; 218 config_ring0.s.ring = timer_id; 219 cvmx_write_csr(CVMX_TIM_MEM_RING0, config_ring0.u64); 220 221 /* Tell the hardware the size of each chunk block in pointers */ 222 config_ring1.u64 = 0; 223 config_ring1.s.enable = 1; 224 config_ring1.s.pool = CVMX_FPA_TIMER_POOL; 225 config_ring1.s.words_per_chunk = CVMX_FPA_TIMER_POOL_SIZE / 8; 226 config_ring1.s.interval = interval; 227 config_ring1.s.ring = timer_id; 228 cvmx_write_csr(CVMX_TIM_MEM_RING1, config_ring1.u64); 229 } 230 } 231 232 return 0; 233} 234#endif 235 236/** 237 * Start the hardware timer processing 238 */ 239void cvmx_tim_start(void) 240{ 241 cvmx_tim_control_t control; 242 243 control.u64 = cvmx_read_csr(CVMX_TIM_REG_FLAGS); 244 control.s.enable_dwb = 1; 245 control.s.enable_timers = 1; 246 247 /* Remember when we started the timers */ 248 cvmx_tim.start_time = cvmx_clock_get_count(CVMX_CLOCK_TIM); 249 cvmx_write_csr(CVMX_TIM_REG_FLAGS, control.u64); 250} 251 252 253/** 254 * Stop the hardware timer processing. Timers stay configured. 255 */ 256void cvmx_tim_stop(void) 257{ 258 cvmx_tim_control_t control; 259 control.u64 = cvmx_read_csr(CVMX_TIM_REG_FLAGS); 260 control.s.enable_dwb = 0; 261 control.s.enable_timers = 0; 262 cvmx_write_csr(CVMX_TIM_REG_FLAGS, control.u64); 263} 264 265 266/** 267 * Stop the timer. After this the timer must be setup again 268 * before use. 269 */ 270#ifdef CVMX_ENABLE_TIMER_FUNCTIONS 271void cvmx_tim_shutdown(void) 272{ 273 uint32_t bucket; 274 uint64_t timer_id; 275 uint64_t entries_per_chunk; 276 277 /* Make sure the timers are stopped */ 278 cvmx_tim_stop(); 279 280 entries_per_chunk = CVMX_FPA_TIMER_POOL_SIZE/8 - 1; 281 282 /* Now walk all buckets freeing the chunks */ 283 for (timer_id = 0; timer_id<CVMX_TIM_NUM_TIMERS; timer_id++) 284 { 285 for (bucket=0; bucket<cvmx_tim.num_buckets; bucket++) 286 { 287 uint64_t chunk_addr; 288 uint64_t next_chunk_addr; 289 cvmx_tim_bucket_entry_t *bucket_ptr = cvmx_tim.bucket + timer_id * cvmx_tim.num_buckets + bucket; 290 CVMX_PREFETCH128(CAST64(bucket_ptr)); /* prefetch the next cacheline for future buckets */ 291 292 /* Each bucket contains a list of chunks */ 293 chunk_addr = bucket_ptr->first_chunk_addr; 294 while (bucket_ptr->num_entries) 295 { 296#ifdef DEBUG 297 cvmx_dprintf("Freeing Timer Chunk 0x%llx\n", CAST64(chunk_addr)); 298#endif 299 /* Read next chunk pointer from end of the current chunk */ 300 next_chunk_addr = cvmx_read_csr(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, chunk_addr + CVMX_FPA_TIMER_POOL_SIZE - 8)); 301 302 cvmx_fpa_free(cvmx_phys_to_ptr(chunk_addr), CVMX_FPA_TIMER_POOL, 0); 303 chunk_addr = next_chunk_addr; 304 if (bucket_ptr->num_entries > entries_per_chunk) 305 bucket_ptr->num_entries -= entries_per_chunk; 306 else 307 bucket_ptr->num_entries = 0; 308 } 309 } 310 } 311} 312 313#endif 314