qemu/include/hw/ptimer.h
<<
>>
Prefs
   1/*
   2 * General purpose implementation of a simple periodic countdown timer.
   3 *
   4 * Copyright (c) 2007 CodeSourcery.
   5 *
   6 * This code is licensed under the GNU LGPL.
   7 */
   8#ifndef PTIMER_H
   9#define PTIMER_H
  10
  11#include "qemu/timer.h"
  12
  13/*
  14 * The ptimer API implements a simple periodic countdown timer.
  15 * The countdown timer has a value (which can be read and written via
  16 * ptimer_get_count() and ptimer_set_count()). When it is enabled
  17 * using ptimer_run(), the value will count downwards at the frequency
  18 * which has been configured using ptimer_set_period() or ptimer_set_freq().
  19 * When it reaches zero it will trigger a callback function, and
  20 * can be set to either reload itself from a specified limit value
  21 * and keep counting down, or to stop (as a one-shot timer).
  22 *
  23 * A transaction-based API is used for modifying ptimer state: all calls
  24 * to functions which modify ptimer state must be between matched calls to
  25 * ptimer_transaction_begin() and ptimer_transaction_commit().
  26 * When ptimer_transaction_commit() is called it will evaluate the state
  27 * of the timer after all the changes in the transaction, and call the
  28 * callback if necessary. (See the ptimer_init() documentation for the full
  29 * list of state-modifying functions and detailed semantics of the callback.)
  30 *
  31 * Forgetting to set the period/frequency (or setting it to zero) is a
  32 * bug in the QEMU device and will cause warning messages to be printed
  33 * to stderr when the guest attempts to enable the timer.
  34 */
  35
  36/* The default ptimer policy retains backward compatibility with the legacy
  37 * timers. Custom policies are adjusting the default one. Consider providing
  38 * a correct policy for your timer.
  39 *
  40 * The rough edges of the default policy:
  41 *  - Starting to run with a period = 0 emits error message and stops the
  42 *    timer without a trigger.
  43 *
  44 *  - Setting period to 0 of the running timer emits error message and
  45 *    stops the timer without a trigger.
  46 *
  47 *  - Starting to run with counter = 0 or setting it to "0" while timer
  48 *    is running causes a trigger and reloads counter with a limit value.
  49 *    If limit = 0, ptimer emits error message and stops the timer.
  50 *
  51 *  - Counter value of the running timer is one less than the actual value.
  52 *
  53 *  - Changing period/frequency of the running timer loses time elapsed
  54 *    since the last period, effectively restarting the timer with a
  55 *    counter = counter value at the moment of change (.i.e. one less).
  56 */
  57#define PTIMER_POLICY_DEFAULT               0
  58
  59/* Periodic timer counter stays with "0" for a one period before wrapping
  60 * around.  */
  61#define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
  62
  63/* Running periodic timer that has counter = limit = 0 would continuously
  64 * re-trigger every period.  */
  65#define PTIMER_POLICY_CONTINUOUS_TRIGGER    (1 << 1)
  66
  67/* Starting to run with/setting counter to "0" won't trigger immediately,
  68 * but after a one period for both oneshot and periodic modes.  */
  69#define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER  (1 << 2)
  70
  71/* Starting to run with/setting counter to "0" won't re-load counter
  72 * immediately, but after a one period.  */
  73#define PTIMER_POLICY_NO_IMMEDIATE_RELOAD   (1 << 3)
  74
  75/* Make counter value of the running timer represent the actual value and
  76 * not the one less.  */
  77#define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
  78
  79/*
  80 * Starting to run with a zero counter, or setting the counter to "0" via
  81 * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
  82 * (though it will cause a reload). Only a counter decrement to "0"
  83 * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
  84 * ptimer_init() will assert() that you don't set both.
  85 */
  86#define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
  87
  88/* ptimer.c */
  89typedef struct ptimer_state ptimer_state;
  90typedef void (*ptimer_cb)(void *opaque);
  91
  92/**
  93 * ptimer_init - Allocate and return a new ptimer
  94 * @callback: function to call on ptimer expiry
  95 * @callback_opaque: opaque pointer passed to @callback
  96 * @policy: PTIMER_POLICY_* bits specifying behaviour
  97 *
  98 * The ptimer returned must be freed using ptimer_free().
  99 *
 100 * If a ptimer is created using this API then will use the
 101 * transaction-based API for modifying ptimer state: all calls
 102 * to functions which modify ptimer state:
 103 *  - ptimer_set_period()
 104 *  - ptimer_set_freq()
 105 *  - ptimer_set_limit()
 106 *  - ptimer_set_count()
 107 *  - ptimer_run()
 108 *  - ptimer_stop()
 109 * must be between matched calls to ptimer_transaction_begin()
 110 * and ptimer_transaction_commit(). When ptimer_transaction_commit()
 111 * is called it will evaluate the state of the timer after all the
 112 * changes in the transaction, and call the callback if necessary.
 113 *
 114 * The callback function is always called from within a transaction
 115 * begin/commit block, so the callback should not call the
 116 * ptimer_transaction_begin() function itself. If the callback changes
 117 * the ptimer state such that another ptimer expiry is triggered, then
 118 * the callback will be called a second time after the first call returns.
 119 */
 120ptimer_state *ptimer_init(ptimer_cb callback,
 121                          void *callback_opaque,
 122                          uint8_t policy_mask);
 123
 124/**
 125 * ptimer_free - Free a ptimer
 126 * @s: timer to free
 127 *
 128 * Free a ptimer created using ptimer_init().
 129 */
 130void ptimer_free(ptimer_state *s);
 131
 132/**
 133 * ptimer_transaction_begin() - Start a ptimer modification transaction
 134 *
 135 * This function must be called before making any calls to functions
 136 * which modify the ptimer's state (see the ptimer_init() documentation
 137 * for a list of these), and must always have a matched call to
 138 * ptimer_transaction_commit().
 139 * It is an error to call this function for a BH-based ptimer;
 140 * attempting to do this will trigger an assert.
 141 */
 142void ptimer_transaction_begin(ptimer_state *s);
 143
 144/**
 145 * ptimer_transaction_commit() - Commit a ptimer modification transaction
 146 *
 147 * This function must be called after calls to functions which modify
 148 * the ptimer's state, and completes the update of the ptimer. If the
 149 * ptimer state now means that we should trigger the timer expiry
 150 * callback, it will be called directly.
 151 */
 152void ptimer_transaction_commit(ptimer_state *s);
 153
 154/**
 155 * ptimer_set_period - Set counter increment interval in nanoseconds
 156 * @s: ptimer to configure
 157 * @period: period of the counter in nanoseconds
 158 *
 159 * Note that if your counter behaviour is specified as having a
 160 * particular frequency rather than a period then ptimer_set_freq()
 161 * may be more appropriate.
 162 *
 163 * This function will assert if it is called outside a
 164 * ptimer_transaction_begin/commit block.
 165 */
 166void ptimer_set_period(ptimer_state *s, int64_t period);
 167
 168/**
 169 * ptimer_set_freq - Set counter frequency in Hz
 170 * @s: ptimer to configure
 171 * @freq: counter frequency in Hz
 172 *
 173 * This does the same thing as ptimer_set_period(), so you only
 174 * need to call one of them. If the counter behaviour is specified
 175 * as setting the frequency then this function is more appropriate,
 176 * because it allows specifying an effective period which is
 177 * precise to fractions of a nanosecond, avoiding rounding errors.
 178 *
 179 * This function will assert if it is called outside a
 180 * ptimer_transaction_begin/commit block.
 181 */
 182void ptimer_set_freq(ptimer_state *s, uint32_t freq);
 183
 184/**
 185 * ptimer_get_limit - Get the configured limit of the ptimer
 186 * @s: ptimer to query
 187 *
 188 * This function returns the current limit (reload) value
 189 * of the down-counter; that is, the value which it will be
 190 * reset to when it hits zero.
 191 *
 192 * Generally timer devices using ptimers should be able to keep
 193 * their reload register state inside the ptimer using the get
 194 * and set limit functions rather than needing to also track it
 195 * in their own state structure.
 196 */
 197uint64_t ptimer_get_limit(ptimer_state *s);
 198
 199/**
 200 * ptimer_set_limit - Set the limit of the ptimer
 201 * @s: ptimer
 202 * @limit: initial countdown value
 203 * @reload: if nonzero, then reset the counter to the new limit
 204 *
 205 * Set the limit value of the down-counter. The @reload flag can
 206 * be used to emulate the behaviour of timers which immediately
 207 * reload the counter when their reload register is written to.
 208 *
 209 * This function will assert if it is called outside a
 210 * ptimer_transaction_begin/commit block.
 211 */
 212void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
 213
 214/**
 215 * ptimer_get_count - Get the current value of the ptimer
 216 * @s: ptimer
 217 *
 218 * Return the current value of the down-counter. This will
 219 * return the correct value whether the counter is enabled or
 220 * disabled.
 221 */
 222uint64_t ptimer_get_count(ptimer_state *s);
 223
 224/**
 225 * ptimer_set_count - Set the current value of the ptimer
 226 * @s: ptimer
 227 * @count: count value to set
 228 *
 229 * Set the value of the down-counter. If the counter is currently
 230 * enabled this will arrange for a timer callback at the appropriate
 231 * point in the future.
 232 *
 233 * This function will assert if it is called outside a
 234 * ptimer_transaction_begin/commit block.
 235 */
 236void ptimer_set_count(ptimer_state *s, uint64_t count);
 237
 238/**
 239 * ptimer_run - Start a ptimer counting
 240 * @s: ptimer
 241 * @oneshot: non-zero if this timer should only count down once
 242 *
 243 * Start a ptimer counting down; when it reaches zero the callback function
 244 * passed to ptimer_init() will be invoked.
 245 * If the @oneshot argument is zero,
 246 * the counter value will then be reloaded from the limit and it will
 247 * start counting down again. If @oneshot is non-zero, then the counter
 248 * will disable itself when it reaches zero.
 249 *
 250 * This function will assert if it is called outside a
 251 * ptimer_transaction_begin/commit block.
 252 */
 253void ptimer_run(ptimer_state *s, int oneshot);
 254
 255/**
 256 * ptimer_stop - Stop a ptimer counting
 257 * @s: ptimer
 258 *
 259 * Pause a timer (the count stays at its current value until ptimer_run()
 260 * is called to start it counting again).
 261 *
 262 * Note that this can cause it to "lose" time, even if it is immediately
 263 * restarted.
 264 *
 265 * This function will assert if it is called outside a
 266 * ptimer_transaction_begin/commit block.
 267 */
 268void ptimer_stop(ptimer_state *s);
 269
 270extern const VMStateDescription vmstate_ptimer;
 271
 272#define VMSTATE_PTIMER(_field, _state) \
 273    VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
 274
 275#define VMSTATE_PTIMER_ARRAY(_f, _s, _n)                                \
 276    VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0,                   \
 277                                       vmstate_ptimer, ptimer_state)
 278
 279#endif
 280