Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Counter interface
0004  * Copyright (C) 2018 William Breathitt Gray
0005  */
0006 #ifndef _COUNTER_H_
0007 #define _COUNTER_H_
0008 
0009 #include <linux/cdev.h>
0010 #include <linux/device.h>
0011 #include <linux/kernel.h>
0012 #include <linux/kfifo.h>
0013 #include <linux/mutex.h>
0014 #include <linux/spinlock_types.h>
0015 #include <linux/types.h>
0016 #include <linux/wait.h>
0017 #include <uapi/linux/counter.h>
0018 
0019 struct counter_device;
0020 struct counter_count;
0021 struct counter_synapse;
0022 struct counter_signal;
0023 
0024 enum counter_comp_type {
0025     COUNTER_COMP_U8,
0026     COUNTER_COMP_U64,
0027     COUNTER_COMP_BOOL,
0028     COUNTER_COMP_SIGNAL_LEVEL,
0029     COUNTER_COMP_FUNCTION,
0030     COUNTER_COMP_SYNAPSE_ACTION,
0031     COUNTER_COMP_ENUM,
0032     COUNTER_COMP_COUNT_DIRECTION,
0033     COUNTER_COMP_COUNT_MODE,
0034 };
0035 
0036 /**
0037  * struct counter_comp - Counter component node
0038  * @type:       Counter component data type
0039  * @name:       device-specific component name
0040  * @priv:       component-relevant data
0041  * @action_read:        Synapse action mode read callback. The read value of the
0042  *          respective Synapse action mode should be passed back via
0043  *          the action parameter.
0044  * @device_u8_read:     Device u8 component read callback. The read value of the
0045  *          respective Device u8 component should be passed back via
0046  *          the val parameter.
0047  * @count_u8_read:      Count u8 component read callback. The read value of the
0048  *          respective Count u8 component should be passed back via
0049  *          the val parameter.
0050  * @signal_u8_read:     Signal u8 component read callback. The read value of the
0051  *          respective Signal u8 component should be passed back via
0052  *          the val parameter.
0053  * @device_u32_read:        Device u32 component read callback. The read value of
0054  *          the respective Device u32 component should be passed
0055  *          back via the val parameter.
0056  * @count_u32_read:     Count u32 component read callback. The read value of the
0057  *          respective Count u32 component should be passed back via
0058  *          the val parameter.
0059  * @signal_u32_read:        Signal u32 component read callback. The read value of
0060  *          the respective Signal u32 component should be passed
0061  *          back via the val parameter.
0062  * @device_u64_read:        Device u64 component read callback. The read value of
0063  *          the respective Device u64 component should be passed
0064  *          back via the val parameter.
0065  * @count_u64_read:     Count u64 component read callback. The read value of the
0066  *          respective Count u64 component should be passed back via
0067  *          the val parameter.
0068  * @signal_u64_read:        Signal u64 component read callback. The read value of
0069  *          the respective Signal u64 component should be passed
0070  *          back via the val parameter.
0071  * @action_write:       Synapse action mode write callback. The write value of
0072  *          the respective Synapse action mode is passed via the
0073  *          action parameter.
0074  * @device_u8_write:        Device u8 component write callback. The write value of
0075  *          the respective Device u8 component is passed via the val
0076  *          parameter.
0077  * @count_u8_write:     Count u8 component write callback. The write value of
0078  *          the respective Count u8 component is passed via the val
0079  *          parameter.
0080  * @signal_u8_write:        Signal u8 component write callback. The write value of
0081  *          the respective Signal u8 component is passed via the val
0082  *          parameter.
0083  * @device_u32_write:       Device u32 component write callback. The write value of
0084  *          the respective Device u32 component is passed via the
0085  *          val parameter.
0086  * @count_u32_write:        Count u32 component write callback. The write value of
0087  *          the respective Count u32 component is passed via the val
0088  *          parameter.
0089  * @signal_u32_write:       Signal u32 component write callback. The write value of
0090  *          the respective Signal u32 component is passed via the
0091  *          val parameter.
0092  * @device_u64_write:       Device u64 component write callback. The write value of
0093  *          the respective Device u64 component is passed via the
0094  *          val parameter.
0095  * @count_u64_write:        Count u64 component write callback. The write value of
0096  *          the respective Count u64 component is passed via the val
0097  *          parameter.
0098  * @signal_u64_write:       Signal u64 component write callback. The write value of
0099  *          the respective Signal u64 component is passed via the
0100  *          val parameter.
0101  */
0102 struct counter_comp {
0103     enum counter_comp_type type;
0104     const char *name;
0105     void *priv;
0106     union {
0107         int (*action_read)(struct counter_device *counter,
0108                    struct counter_count *count,
0109                    struct counter_synapse *synapse,
0110                    enum counter_synapse_action *action);
0111         int (*device_u8_read)(struct counter_device *counter, u8 *val);
0112         int (*count_u8_read)(struct counter_device *counter,
0113                      struct counter_count *count, u8 *val);
0114         int (*signal_u8_read)(struct counter_device *counter,
0115                       struct counter_signal *signal, u8 *val);
0116         int (*device_u32_read)(struct counter_device *counter,
0117                        u32 *val);
0118         int (*count_u32_read)(struct counter_device *counter,
0119                       struct counter_count *count, u32 *val);
0120         int (*signal_u32_read)(struct counter_device *counter,
0121                        struct counter_signal *signal, u32 *val);
0122         int (*device_u64_read)(struct counter_device *counter,
0123                        u64 *val);
0124         int (*count_u64_read)(struct counter_device *counter,
0125                       struct counter_count *count, u64 *val);
0126         int (*signal_u64_read)(struct counter_device *counter,
0127                        struct counter_signal *signal, u64 *val);
0128     };
0129     union {
0130         int (*action_write)(struct counter_device *counter,
0131                     struct counter_count *count,
0132                     struct counter_synapse *synapse,
0133                     enum counter_synapse_action action);
0134         int (*device_u8_write)(struct counter_device *counter, u8 val);
0135         int (*count_u8_write)(struct counter_device *counter,
0136                       struct counter_count *count, u8 val);
0137         int (*signal_u8_write)(struct counter_device *counter,
0138                        struct counter_signal *signal, u8 val);
0139         int (*device_u32_write)(struct counter_device *counter,
0140                     u32 val);
0141         int (*count_u32_write)(struct counter_device *counter,
0142                        struct counter_count *count, u32 val);
0143         int (*signal_u32_write)(struct counter_device *counter,
0144                     struct counter_signal *signal, u32 val);
0145         int (*device_u64_write)(struct counter_device *counter,
0146                     u64 val);
0147         int (*count_u64_write)(struct counter_device *counter,
0148                        struct counter_count *count, u64 val);
0149         int (*signal_u64_write)(struct counter_device *counter,
0150                     struct counter_signal *signal, u64 val);
0151     };
0152 };
0153 
0154 /**
0155  * struct counter_signal - Counter Signal node
0156  * @id:     unique ID used to identify the Signal
0157  * @name:   device-specific Signal name
0158  * @ext:    optional array of Signal extensions
0159  * @num_ext:    number of Signal extensions specified in @ext
0160  */
0161 struct counter_signal {
0162     int id;
0163     const char *name;
0164 
0165     struct counter_comp *ext;
0166     size_t num_ext;
0167 };
0168 
0169 /**
0170  * struct counter_synapse - Counter Synapse node
0171  * @actions_list:   array of available action modes
0172  * @num_actions:    number of action modes specified in @actions_list
0173  * @signal:     pointer to the associated Signal
0174  */
0175 struct counter_synapse {
0176     const enum counter_synapse_action *actions_list;
0177     size_t num_actions;
0178 
0179     struct counter_signal *signal;
0180 };
0181 
0182 /**
0183  * struct counter_count - Counter Count node
0184  * @id:         unique ID used to identify the Count
0185  * @name:       device-specific Count name
0186  * @functions_list: array of available function modes
0187  * @num_functions:  number of function modes specified in @functions_list
0188  * @synapses:       array of Synapses for initialization
0189  * @num_synapses:   number of Synapses specified in @synapses
0190  * @ext:        optional array of Count extensions
0191  * @num_ext:        number of Count extensions specified in @ext
0192  */
0193 struct counter_count {
0194     int id;
0195     const char *name;
0196 
0197     const enum counter_function *functions_list;
0198     size_t num_functions;
0199 
0200     struct counter_synapse *synapses;
0201     size_t num_synapses;
0202 
0203     struct counter_comp *ext;
0204     size_t num_ext;
0205 };
0206 
0207 /**
0208  * struct counter_event_node - Counter Event node
0209  * @l:      list of current watching Counter events
0210  * @event:  event that triggers
0211  * @channel:    event channel
0212  * @comp_list:  list of components to watch when event triggers
0213  */
0214 struct counter_event_node {
0215     struct list_head l;
0216     u8 event;
0217     u8 channel;
0218     struct list_head comp_list;
0219 };
0220 
0221 /**
0222  * struct counter_ops - Callbacks from driver
0223  * @signal_read:    optional read callback for Signals. The read level of
0224  *          the respective Signal should be passed back via the
0225  *          level parameter.
0226  * @count_read:     read callback for Counts. The read value of the
0227  *          respective Count should be passed back via the value
0228  *          parameter.
0229  * @count_write:    optional write callback for Counts. The write value for
0230  *          the respective Count is passed in via the value
0231  *          parameter.
0232  * @function_read:  read callback the Count function modes. The read
0233  *          function mode of the respective Count should be passed
0234  *          back via the function parameter.
0235  * @function_write: optional write callback for Count function modes. The
0236  *          function mode to write for the respective Count is
0237  *          passed in via the function parameter.
0238  * @action_read:    optional read callback the Synapse action modes. The
0239  *          read action mode of the respective Synapse should be
0240  *          passed back via the action parameter.
0241  * @action_write:   optional write callback for Synapse action modes. The
0242  *          action mode to write for the respective Synapse is
0243  *          passed in via the action parameter.
0244  * @events_configure:   optional write callback to configure events. The list of
0245  *          struct counter_event_node may be accessed via the
0246  *          events_list member of the counter parameter.
0247  * @watch_validate: optional callback to validate a watch. The Counter
0248  *          component watch configuration is passed in via the watch
0249  *          parameter. A return value of 0 indicates a valid Counter
0250  *          component watch configuration.
0251  */
0252 struct counter_ops {
0253     int (*signal_read)(struct counter_device *counter,
0254                struct counter_signal *signal,
0255                enum counter_signal_level *level);
0256     int (*count_read)(struct counter_device *counter,
0257               struct counter_count *count, u64 *value);
0258     int (*count_write)(struct counter_device *counter,
0259                struct counter_count *count, u64 value);
0260     int (*function_read)(struct counter_device *counter,
0261                  struct counter_count *count,
0262                  enum counter_function *function);
0263     int (*function_write)(struct counter_device *counter,
0264                   struct counter_count *count,
0265                   enum counter_function function);
0266     int (*action_read)(struct counter_device *counter,
0267                struct counter_count *count,
0268                struct counter_synapse *synapse,
0269                enum counter_synapse_action *action);
0270     int (*action_write)(struct counter_device *counter,
0271                 struct counter_count *count,
0272                 struct counter_synapse *synapse,
0273                 enum counter_synapse_action action);
0274     int (*events_configure)(struct counter_device *counter);
0275     int (*watch_validate)(struct counter_device *counter,
0276                   const struct counter_watch *watch);
0277 };
0278 
0279 /**
0280  * struct counter_device - Counter data structure
0281  * @name:       name of the device
0282  * @parent:     optional parent device providing the counters
0283  * @ops:        callbacks from driver
0284  * @signals:        array of Signals
0285  * @num_signals:    number of Signals specified in @signals
0286  * @counts:     array of Counts
0287  * @num_counts:     number of Counts specified in @counts
0288  * @ext:        optional array of Counter device extensions
0289  * @num_ext:        number of Counter device extensions specified in @ext
0290  * @priv:       optional private data supplied by driver
0291  * @dev:        internal device structure
0292  * @chrdev:     internal character device structure
0293  * @events_list:    list of current watching Counter events
0294  * @events_list_lock:   lock to protect Counter events list operations
0295  * @next_events_list:   list of next watching Counter events
0296  * @n_events_list_lock: lock to protect Counter next events list operations
0297  * @events:     queue of detected Counter events
0298  * @events_wait:    wait queue to allow blocking reads of Counter events
0299  * @events_in_lock: lock to protect Counter events queue in operations
0300  * @events_out_lock:    lock to protect Counter events queue out operations
0301  * @ops_exist_lock: lock to prevent use during removal
0302  */
0303 struct counter_device {
0304     const char *name;
0305     struct device *parent;
0306 
0307     const struct counter_ops *ops;
0308 
0309     struct counter_signal *signals;
0310     size_t num_signals;
0311     struct counter_count *counts;
0312     size_t num_counts;
0313 
0314     struct counter_comp *ext;
0315     size_t num_ext;
0316 
0317     struct device dev;
0318     struct cdev chrdev;
0319     struct list_head events_list;
0320     spinlock_t events_list_lock;
0321     struct list_head next_events_list;
0322     struct mutex n_events_list_lock;
0323     DECLARE_KFIFO_PTR(events, struct counter_event);
0324     wait_queue_head_t events_wait;
0325     spinlock_t events_in_lock;
0326     struct mutex events_out_lock;
0327     struct mutex ops_exist_lock;
0328 };
0329 
0330 void *counter_priv(const struct counter_device *const counter);
0331 
0332 struct counter_device *counter_alloc(size_t sizeof_priv);
0333 void counter_put(struct counter_device *const counter);
0334 int counter_add(struct counter_device *const counter);
0335 
0336 void counter_unregister(struct counter_device *const counter);
0337 struct counter_device *devm_counter_alloc(struct device *dev,
0338                       size_t sizeof_priv);
0339 int devm_counter_add(struct device *dev,
0340              struct counter_device *const counter);
0341 void counter_push_event(struct counter_device *const counter, const u8 event,
0342             const u8 channel);
0343 
0344 #define COUNTER_COMP_DEVICE_U8(_name, _read, _write) \
0345 { \
0346     .type = COUNTER_COMP_U8, \
0347     .name = (_name), \
0348     .device_u8_read = (_read), \
0349     .device_u8_write = (_write), \
0350 }
0351 #define COUNTER_COMP_COUNT_U8(_name, _read, _write) \
0352 { \
0353     .type = COUNTER_COMP_U8, \
0354     .name = (_name), \
0355     .count_u8_read = (_read), \
0356     .count_u8_write = (_write), \
0357 }
0358 #define COUNTER_COMP_SIGNAL_U8(_name, _read, _write) \
0359 { \
0360     .type = COUNTER_COMP_U8, \
0361     .name = (_name), \
0362     .signal_u8_read = (_read), \
0363     .signal_u8_write = (_write), \
0364 }
0365 
0366 #define COUNTER_COMP_DEVICE_U64(_name, _read, _write) \
0367 { \
0368     .type = COUNTER_COMP_U64, \
0369     .name = (_name), \
0370     .device_u64_read = (_read), \
0371     .device_u64_write = (_write), \
0372 }
0373 #define COUNTER_COMP_COUNT_U64(_name, _read, _write) \
0374 { \
0375     .type = COUNTER_COMP_U64, \
0376     .name = (_name), \
0377     .count_u64_read = (_read), \
0378     .count_u64_write = (_write), \
0379 }
0380 #define COUNTER_COMP_SIGNAL_U64(_name, _read, _write) \
0381 { \
0382     .type = COUNTER_COMP_U64, \
0383     .name = (_name), \
0384     .signal_u64_read = (_read), \
0385     .signal_u64_write = (_write), \
0386 }
0387 
0388 #define COUNTER_COMP_DEVICE_BOOL(_name, _read, _write) \
0389 { \
0390     .type = COUNTER_COMP_BOOL, \
0391     .name = (_name), \
0392     .device_u8_read = (_read), \
0393     .device_u8_write = (_write), \
0394 }
0395 #define COUNTER_COMP_COUNT_BOOL(_name, _read, _write) \
0396 { \
0397     .type = COUNTER_COMP_BOOL, \
0398     .name = (_name), \
0399     .count_u8_read = (_read), \
0400     .count_u8_write = (_write), \
0401 }
0402 #define COUNTER_COMP_SIGNAL_BOOL(_name, _read, _write) \
0403 { \
0404     .type = COUNTER_COMP_BOOL, \
0405     .name = (_name), \
0406     .signal_u8_read = (_read), \
0407     .signal_u8_write = (_write), \
0408 }
0409 
0410 struct counter_available {
0411     union {
0412         const u32 *enums;
0413         const char *const *strs;
0414     };
0415     size_t num_items;
0416 };
0417 
0418 #define DEFINE_COUNTER_AVAILABLE(_name, _enums) \
0419     struct counter_available _name = { \
0420         .enums = (_enums), \
0421         .num_items = ARRAY_SIZE(_enums), \
0422     }
0423 
0424 #define DEFINE_COUNTER_ENUM(_name, _strs) \
0425     struct counter_available _name = { \
0426         .strs = (_strs), \
0427         .num_items = ARRAY_SIZE(_strs), \
0428     }
0429 
0430 #define COUNTER_COMP_DEVICE_ENUM(_name, _get, _set, _available) \
0431 { \
0432     .type = COUNTER_COMP_ENUM, \
0433     .name = (_name), \
0434     .device_u32_read = (_get), \
0435     .device_u32_write = (_set), \
0436     .priv = &(_available), \
0437 }
0438 #define COUNTER_COMP_COUNT_ENUM(_name, _get, _set, _available) \
0439 { \
0440     .type = COUNTER_COMP_ENUM, \
0441     .name = (_name), \
0442     .count_u32_read = (_get), \
0443     .count_u32_write = (_set), \
0444     .priv = &(_available), \
0445 }
0446 #define COUNTER_COMP_SIGNAL_ENUM(_name, _get, _set, _available) \
0447 { \
0448     .type = COUNTER_COMP_ENUM, \
0449     .name = (_name), \
0450     .signal_u32_read = (_get), \
0451     .signal_u32_write = (_set), \
0452     .priv = &(_available), \
0453 }
0454 
0455 #define COUNTER_COMP_CEILING(_read, _write) \
0456     COUNTER_COMP_COUNT_U64("ceiling", _read, _write)
0457 
0458 #define COUNTER_COMP_COUNT_MODE(_read, _write, _available) \
0459 { \
0460     .type = COUNTER_COMP_COUNT_MODE, \
0461     .name = "count_mode", \
0462     .count_u32_read = (_read), \
0463     .count_u32_write = (_write), \
0464     .priv = &(_available), \
0465 }
0466 
0467 #define COUNTER_COMP_DIRECTION(_read) \
0468 { \
0469     .type = COUNTER_COMP_COUNT_DIRECTION, \
0470     .name = "direction", \
0471     .count_u32_read = (_read), \
0472 }
0473 
0474 #define COUNTER_COMP_ENABLE(_read, _write) \
0475     COUNTER_COMP_COUNT_BOOL("enable", _read, _write)
0476 
0477 #define COUNTER_COMP_FLOOR(_read, _write) \
0478     COUNTER_COMP_COUNT_U64("floor", _read, _write)
0479 
0480 #define COUNTER_COMP_PRESET(_read, _write) \
0481     COUNTER_COMP_COUNT_U64("preset", _read, _write)
0482 
0483 #define COUNTER_COMP_PRESET_ENABLE(_read, _write) \
0484     COUNTER_COMP_COUNT_BOOL("preset_enable", _read, _write)
0485 
0486 #endif /* _COUNTER_H_ */