source: rtems/cpukit/libdrvmgr/drvmgr.h @ 30594a9

4.115
Last change on this file since 30594a9 was e7fade3, checked in by Daniel Hellstrom <daniel@…>, on 11/28/11 at 08:52:03

DRVMGR: added driver manager to cpukit/libdrvmgr

  • Property mode set to 100644
File size: 36.7 KB
Line 
1/* Driver Manager Interface.
2 *
3 * COPYRIGHT (c) 2009.
4 * Cobham Gaisler AB.
5 *
6 * The license and distribution terms for this file may be
7 * found in the file LICENSE in this distribution or at
8 * http://www.rtems.com/license/LICENSE.
9 */
10
11#ifndef _DRIVER_MANAGER_H_
12#define _DRIVER_MANAGER_H_
13
14#include <rtems.h>
15#include <drvmgr/drvmgr_list.h>
16#include <stdint.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/*** Configure Driver manager ***/
23
24/* Define the number of initialization levels of device drivers */
25#define DRVMGR_LEVEL_MAX 4
26
27/* Default to use semahpores for protection. Initialization works without
28 * locks and after initialization too if devices are not removed.
29 */
30#ifndef DRVMGR_USE_LOCKS
31#define DRVMGR_USE_LOCKS 1
32#endif
33
34struct drvmgr_dev;      /* Device */
35struct drvmgr_bus;      /* Bus */
36struct drvmgr_drv;      /* Driver */
37
38/*** List Interface shortcuts ***/
39#define BUS_LIST_HEAD(list) LIST_HEAD(list, struct drvmgr_bus)
40#define BUS_LIST_TAIL(list) LIST_TAIL(list, struct drvmgr_bus)
41#define DEV_LIST_HEAD(list) LIST_HEAD(list, struct drvmgr_dev)
42#define DEV_LIST_TAIL(list) LIST_TAIL(list, struct drvmgr_dev)
43#define DRV_LIST_HEAD(list) LIST_HEAD(list, struct drvmgr_drv)
44#define DRV_LIST_TAIL(list) LIST_TAIL(list, struct drvmgr_drv)
45
46/*** Bus indentification ***/
47#define DRVMGR_BUS_TYPE_NONE 0          /* Not a valid bus */
48#define DRVMGR_BUS_TYPE_ROOT 1          /* Hard coded bus */
49#define DRVMGR_BUS_TYPE_PCI 2           /* PCI bus */
50#define DRVMGR_BUS_TYPE_AMBAPP 3        /* AMBA Plug & Play bus */
51#define DRVMGR_BUS_TYPE_LEON2_AMBA 4    /* LEON2 hardcoded bus */
52#define DRVMGR_BUS_TYPE_AMBAPP_DIST 5   /* Distibuted AMBA Plug & Play bus accessed using a communication interface */
53#define DRVMGR_BUS_TYPE_SPW_RMAP 6      /* SpaceWire Network bus */
54#define DRVMGR_BUS_TYPE_AMBAPP_RMAP 7   /* SpaceWire RMAP accessed AMBA Plug & Play bus */
55
56enum {
57        DRVMGR_OBJ_NONE = 0,
58        DRVMGR_OBJ_DRV = 1,
59        DRVMGR_OBJ_BUS = 2,
60        DRVMGR_OBJ_DEV = 3,
61};
62
63/*** Driver indentification ***
64 *
65 * 64-bit identification integer definition
66 *  * Bus ID 8-bit [7..0]
67 *  * Reserved 8-bit field [63..56]
68 *  * Device ID specific for bus type 48-bit [55..8]  (Different buses have
69 *    different unique identifications for hardware/driver.)
70 *
71 * ID Rules
72 *  * A root bus driver must always have device ID set to 0. There can only by
73 *    one root bus driver for a certain bus type.
74 *  * A Driver ID must identify a unique hardware core
75 *
76 */
77
78/* Bus ID Mask */
79#define DRIVER_ID_BUS_MASK 0x00000000000000FFULL
80
81/* Reserved Mask for future use */
82#define DRIVER_ID_RSV_MASK 0xFF00000000000000ULL
83
84/* Reserved Mask for future use */
85#define DRIVER_ID_DEV_MASK 0x00FFFFFFFFFFFF00ULL
86
87/* Set Bus ID Mask. */
88#define DRIVER_ID(busid, devid) ((unsigned long long) \
89        ((((unsigned long long)(devid) << 8) & DRIVER_ID_DEV_MASK) | \
90         ((unsigned long long)(busid) & DRIVER_ID_BUS_MASK)))
91
92/* Get IDs */
93#define DRIVER_BUSID_GET(id)    ((unsigned long long)(id) & DRIVER_ID_BUS_MASK)
94#define DRIVER_DEVID_GET(id)    (((unsigned long long)(id) & DRIVER_ID_DEV_MASK) >> 8)
95
96#define DRIVER_ROOTBUS_ID(bus_type)     DRIVER_ID(bus_type, 0)
97
98/*** Root Bus drivers ***/
99
100/* Generic Hard coded Root bus: Driver ID */
101#define DRIVER_ROOT_ID          DRIVER_ROOTBUS_ID(DRVMGR_BUS_TYPE_ROOT)
102
103/* PCI Plug & Play bus: Driver ID */
104#define DRIVER_PCIBUS_ID        DRIVER_ROOTBUS_ID(DRVMGR_BUS_TYPE_PCI)
105
106/* AMBA Plug & Play bus: Driver ID */
107#define DRIVER_GRLIB_AMBAPP_ID  DRIVER_ROOTBUS_ID(DRVMGR_BUS_TYPE_AMBAPP)
108
109/* AMBA Hard coded bus: Driver ID */
110#define DRIVER_LEON2_AMBA_ID    DRIVER_ROOTBUS_ID(DRVMGR_BUS_TYPE_LEON2_AMBA)
111
112/* Distributed AMBA Plug & Play bus: Driver ID */
113#define DRIVER_AMBAPP_DIST_ID   DRIVER_ROOTBUS_ID(DRVMGR_BUS_TYPE_AMBAPP_DIST)
114
115/*! Bus parameters used by driver interface functions to aquire information
116 * about bus. All Bus drivers should implement the operation 'get_params' so
117 * that the driver interface routines can access bus dependent information in
118 * an non-dependent way.
119 */
120struct drvmgr_bus_params {
121        char            *dev_prefix;            /*!< Optional name prefix */
122};
123
124/* Interrupt Service Routine (ISR) */
125typedef void (*drvmgr_isr)(void *arg);
126
127/*! Bus operations */
128struct drvmgr_bus_ops {
129        /* Functions used internally within driver manager */
130        int     (*init[DRVMGR_LEVEL_MAX])(struct drvmgr_bus *);
131        int     (*remove)(struct drvmgr_bus *);
132        int     (*unite)(struct drvmgr_drv *, struct drvmgr_dev *);     /*!< Unite Hardware Device with Driver */
133
134        /* Functions called indirectly from drivers */
135        int     (*int_register)(struct drvmgr_dev *, int index, const char *info, drvmgr_isr isr, void *arg);
136        int     (*int_unregister)(struct drvmgr_dev *, int index, drvmgr_isr isr, void *arg);
137        int     (*int_clear)(struct drvmgr_dev *, int index);
138        int     (*int_mask)(struct drvmgr_dev *, int index);
139        int     (*int_unmask)(struct drvmgr_dev *, int index);
140
141        /* Get Parameters */
142        int     (*get_params)(struct drvmgr_dev *, struct drvmgr_bus_params *);
143        /* Get Frequency of Bus */
144        int     (*freq_get)(struct drvmgr_dev*, int, unsigned int*);
145        /*! Function called to request information about a device. The bus
146         *  driver interpret the bus-specific information about the device.
147         */
148        void    (*info_dev)(struct drvmgr_dev *, void (*print)(void *p, char *str), void *p);
149};
150#define BUS_OPS_NUM (sizeof(struct drvmgr_bus_ops)/sizeof(void (*)(void)))
151
152struct drvmgr_func {
153        int funcid;
154        void *func;
155};
156#define DRVMGR_FUNC(_ID_, _FUNC_) {(int)(_ID_), (void *)(_FUNC_)}
157#define DRVMGR_FUNC_END {0, NULL}
158
159/*** Resource definitions ***
160 *
161 * Overview of structures:
162 *  All bus resources entries (_bus_res) are linked together per bus
163 *  (bus_info->reslist). One bus resource entry has a pointer to an array of
164 *  driver resources (_drv_res). One driver resouces is made out of an array
165 *  of keys (drvmgr_key). All keys belongs to the same driver and harwdare
166 *  device. Each key has a Name, Type ID and Data interpreted differently
167 *  depending on the Type ID (union drvmgr_key_value).
168 *
169 */
170
171/* Key Data Types */
172#define KEY_TYPE_NONE           0
173#define KEY_TYPE_INT            1
174#define KEY_TYPE_STRING         2
175#define KEY_TYPE_POINTER        3
176
177#define KEY_EMPTY       {NULL, KEY_TYPE_NONE, {0}}
178#define RES_EMPTY       {0, 0, NULL}
179#define MMAP_EMPTY      {0, 0, 0}
180
181/*! Union of different values */
182union drvmgr_key_value {
183        unsigned int            i;      /*!< Key data type UNSIGNED INTEGER */
184        char                    *str;   /*!< Key data type STRING */
185        void                    *ptr;   /*!< Key data type ADDRESS/POINTER */
186};
187
188/* One key. One Value. Holding information relevant to the driver. */
189struct drvmgr_key {
190        char                    *key_name;      /* Name of key */
191        int                     key_type;       /* How to interpret key_value */
192        union drvmgr_key_value  key_value;      /* The value or pointer to value */
193};
194
195/*! Driver resource entry, Driver resources for a certain device instance,
196 *  containing a number of keys where each key hold the data of interest.
197 */
198struct drvmgr_drv_res {
199        uint64_t                drv_id;         /*!< Identifies the driver this resource is aiming */
200        int                     minor_bus;      /*!< Indentifies a specfic device */
201        struct drvmgr_key       *keys;          /*!< First key in key array, ended with KEY_EMPTY */
202};
203
204/*! Bus resource list node */
205struct drvmgr_bus_res {
206        struct drvmgr_bus_res   *next;          /*!< Next resource node in list */
207        struct drvmgr_drv_res   resource[];     /*!< Array of resources, one per device instance */
208};
209
210/*! MAP entry. Describes an linear address space translation. Untranslated
211 *  Start, Translated Start and length.
212 *
213 * Used by bus drivers to describe the address translation needed for
214 * the translation driver interface.
215 */
216struct drvmgr_map_entry {
217        char            *name;          /*!< Map Name */
218        unsigned int    size;           /*!< Size of map window */
219        char            *from_adr;      /*!< Start address of access window used
220                                         *   to reach into remote bus */
221        char            *to_adr;        /*!< Start address of remote system
222                                         *   address range */
223};
224#define DRVMGR_TRANSLATE_ONE2ONE        NULL
225#define DRVMGR_TRANSLATE_NO_BRIDGE      ((void *)1)  /* No bridge, error */
226
227/*! Bus information. Describes a bus. */
228struct drvmgr_bus {
229        int                     obj_type;       /*!< DRVMGR_OBJ_BUS */
230        unsigned char           bus_type;       /*!< Type of bus */
231        unsigned char           depth;          /*!< Bus level distance from root bus */
232        struct drvmgr_bus       *next;          /*!< Next Bus */
233        struct drvmgr_dev       *dev;           /*!< Bus device, the hardware... */
234        void                    *priv;          /*!< Private data structure used by BUS driver */
235        struct drvmgr_dev       *children;      /*!< Hardware devices on this bus */
236        struct drvmgr_bus_ops   *ops;           /*!< Bus operations supported by this bus driver */
237        struct drvmgr_func      *funcs;         /*!< Extra operations */
238        int                     dev_cnt;        /*!< Number of devices this bus has */
239        struct drvmgr_bus_res   *reslist;       /*!< Bus resources, head of a linked list of resources. */
240        struct drvmgr_map_entry *maps_up;       /*!< Map Translation, array of address spaces upstreams to CPU */
241        struct drvmgr_map_entry *maps_down;     /*!< Map Translation, array of address spaces downstreams to Hardware */
242
243        /* Bus status */
244        int                     level;          /*!< Initialization Level of Bus */
245        int                     state;          /*!< Init State of Bus, BUS_STATE_* */
246        int                     error;          /*!< Return code from bus->ops->initN() */
247};
248
249/* States of a bus */
250#define BUS_STATE_INIT_FAILED   0x00000001      /* Initialization Failed */
251#define BUS_STATE_LIST_INACTIVE 0x00001000      /* In inactive bus list */
252#define BUS_STATE_DEPEND_FAILED 0x00000004      /* Device init failed */
253
254/* States of a device */
255#define DEV_STATE_INIT_FAILED   0x00000001      /* Initialization Failed */
256#define DEV_STATE_INIT_DONE     0x00000002      /* All init levels completed */
257#define DEV_STATE_DEPEND_FAILED 0x00000004      /* Parent Bus init failed */
258#define DEV_STATE_UNITED        0x00000100      /* Device United with Device Driver */
259#define DEV_STATE_REMOVED       0x00000200      /* Device has been removed (unregistered) */
260#define DEV_STATE_IGNORED       0x00000400      /* Device was ignored according to user's request, the device
261                                                 * was never reported to it's driver (as expected).
262                                                 */
263#define DEV_STATE_LIST_INACTIVE 0x00001000      /* In inactive device list */
264
265/*! Device information */
266struct drvmgr_dev {
267        int                     obj_type;       /*!< DRVMGR_OBJ_DEV */
268        struct drvmgr_dev       *next;          /*!< Next device */
269        struct drvmgr_dev       *next_in_bus;   /*!< Next device on the same bus */
270        struct drvmgr_dev       *next_in_drv;   /*!< Next device using the same driver */
271
272        struct drvmgr_drv       *drv;           /*!< The driver owning this device */
273        struct drvmgr_bus       *parent;        /*!< Bus that this device resides on */
274        short                   minor_drv;      /*!< Device number within driver */
275        short                   minor_bus;      /*!< Device number on bus (for device separation) */
276        char                    *name;          /*!< Name of Device Hardware */
277        void                    *priv;          /*!< Pointer to driver private device structure */
278        void                    *businfo;       /*!< Host bus specific information */
279        struct drvmgr_bus       *bus;           /*!< Pointer to bus, set only if this is a bridge */
280
281        /* Device Status */
282        unsigned int            state;          /*!< State of device, see DEV_STATE_* */
283        int                     level;          /*!< Init Level */
284        int                     error;          /*!< Error state returned by driver */
285};
286
287/*! Driver operations, function pointers. */
288struct drvmgr_drv_ops {
289        int     (*init[DRVMGR_LEVEL_MAX])(struct drvmgr_dev *); /*! Function doing Init Stage 1 of a hardware device */
290        int     (*remove)(struct drvmgr_dev *); /*! Function called when device instance is to be removed */
291        int     (*info)(struct drvmgr_dev *, void (*print)(void *p, char *str), void *p, int, char *argv[]);/*! Function called to request information about a device or driver */
292};
293#define DRV_OPS_NUM (sizeof(struct drvmgr_drv_ops)/sizeof(void (*)(void)))
294
295/*! Device driver description */
296struct drvmgr_drv {
297        int                     obj_type;       /*!< DRVMGR_OBJ_DRV */
298        struct drvmgr_drv       *next;          /*!< Next Driver */
299        struct drvmgr_dev       *dev;           /*!< Devices using this driver */
300
301        uint64_t                drv_id;         /*!< Unique Driver ID */
302        char                    *name;          /*!< Name of Driver */
303        int                     bus_type;       /*!< Type of Bus this driver supports */
304        struct drvmgr_drv_ops   *ops;           /*!< Driver operations */
305        struct drvmgr_func      *funcs;         /*!< Extra Operations */
306        unsigned int            dev_cnt;        /*!< Number of devices in dev */
307        unsigned int            dev_priv_size;  /*!< If non-zero DRVMGR will allocate memory for dev->priv */
308};
309
310/*! Structure defines a function pointer called when driver manager is ready
311 *  for drivers to register themselfs. Used to select drivers available to the
312 *  driver manager.
313 */
314struct drvmgr_drv_reg_func {
315        void (*drv_reg)(void);
316};
317
318/*** DRIVER | DEVICE | BUS FUNCTIONS ***/
319
320/* Return Codes */
321enum {
322        DRVMGR_OK = 0,          /* Sucess */
323        DRVMGR_NOMEM = 1,       /* Memory allocation error */
324        DRVMGR_EIO = 2,         /* I/O error */
325        DRVMGR_EINVAL = 3,      /* Invalid parameter */
326        DRVMGR_ENOSYS = 4,
327        DRVMGR_TIMEDOUT = 5,    /* Operation timeout error */
328        DRVMGR_EBUSY = 6,
329        DRVMGR_ENORES = 7,      /* Not enough resources */
330        DRVMGR_FAIL = -1        /* Unspecified failure */
331};
332
333/*! Initialize data structures of the driver management system.
334 *  Calls predefined register driver functions so that drivers can
335 *  register themselves.
336 */
337extern void _DRV_Manager_initialization(void);
338
339/*! Take all devices into init level 'level', all devices registered later
340 *  will directly be taken into this level as well, ensuring that all
341 *  registerd devices has been taken into the level.
342 *
343 */
344extern void _DRV_Manager_init_level(int level);
345
346/*! This function must be defined by the BSP when the driver manager is enabled
347 * and initialized during BSP initialization. The function is called after a
348 * init level is reached the first time by the driver manager.
349 */
350extern void bsp_driver_level_hook(int level);
351
352/*! Init driver manager all in one go, will call _DRV_Manager_initialization(),
353 *  then _DRV_Manager_init_level([1..DRVMGR_LEVEL_MAX]).
354 *  Typically called from Init task when user wants to initilize driver
355 *  manager after startup, otherwise not used.
356 */
357extern int drvmgr_init(void);
358
359/* Take registered buses and devices into the correct init level,
360 * this function is called from _init_level() so normally
361 * we don't need to call it directly.
362 */
363extern void drvmgr_init_update(void);
364
365/*! Register Root Bus device driver */
366extern int drvmgr_root_drv_register(struct drvmgr_drv *drv);
367
368/*! Register a driver */
369extern int drvmgr_drv_register(struct drvmgr_drv *drv);
370
371/*! Register a device */
372extern int drvmgr_dev_register(struct drvmgr_dev *dev);
373
374/*! Remove a device, and all its children devices if device is a bus device. The
375 *  device driver will be requested to remove the device and once gone from bus,
376 *  device and driver list the device is put into a inactive list for debugging
377 *  (this is optional by using remove argument).
378 *
379 * Removing the Root Bus Device is not supported.
380 *
381 * \param remove If non-zero the device will be deallocated, and not put into
382 *               the inacitve list.
383 */
384extern int drvmgr_dev_unregister(struct drvmgr_dev *dev);
385
386/*! Register a bus */
387extern int drvmgr_bus_register(struct drvmgr_bus *bus);
388
389/*! Unregister a bus */
390extern int drvmgr_bus_unregister(struct drvmgr_bus *bus);
391
392/*! Unregister all child devices of a bus.
393 *
394 * This function is called from the bus driver, from a "safe" state where
395 * devices will not be added or removed on this particular bus at this time
396 */
397extern int drvmgr_children_unregister(struct drvmgr_bus *bus);
398
399/* Separate a device from the driver it has been united with */
400extern int drvmgr_dev_drv_separate(struct drvmgr_dev *dev);
401
402/*! Allocate a device structure, if no memory available
403 *  rtems_error_fatal_occurred is called.
404 * The 'extra' argment tells how many bytes extra space is to be allocated after
405 * the device structure, this is typically used for "businfo" structures. The extra
406 * space is always aligned to a 4-byte boundary.
407 */
408extern int drvmgr_alloc_dev(struct drvmgr_dev **pdev, int extra);
409
410/*! Allocate a bus structure, if no memory available rtems_error_fatal_occurred
411 * is called.
412 * The 'extra' argment tells how many bytes extra space is to be allocated after
413 * the device structure, this is typically used for "businfo" structures. The
414 * extra space is always aligned to a 4-byte boundary.
415 */
416extern int drvmgr_alloc_bus(struct drvmgr_bus **pbus, int extra);
417
418/*** DRIVER RESOURCE FUNCTIONS ***/
419
420/*! Add resources to a bus, typically used by a bus driver.
421 *
422 * \param bus   The Bus to add the resources to.
423 * \param res   An array with Driver resources, all together are called bus
424 *              resources.
425 */
426extern void drvmgr_bus_res_add(struct drvmgr_bus *bus,
427                                        struct drvmgr_bus_res *bres);
428
429/*! Find all the resource keys for a device among all driver resources on a
430 *  bus. Typically used by a device driver to get configuration options.
431 *
432 * \param dev   Device to find resources for
433 * \param key   Location where the pointer to the driver resource array (drvmgr_drv_res->keys) is stored.
434 */
435extern int drvmgr_keys_get(struct drvmgr_dev *dev, struct drvmgr_key **keys);
436
437/*! Return the one key that matches key name from a driver keys array. The keys
438 *  can be obtained using drvmgr_keys_get().
439 *
440 * \param keys       An array of keys ended with KEY_EMPTY to search among.
441 * \param key_name   Name of key to search for among the keys.
442 */
443extern struct drvmgr_key *drvmgr_key_get(struct drvmgr_key *keys, char *key_name);
444
445/*! Extract key value from the key in the keys array matching name and type.
446 *
447 *  This function calls drvmgr_keys_get to get the key requested (from key
448 *  name), then determines if the type is correct. A pointer to the key value
449 *  is returned.
450 *
451 *  \param keys       An array of keys ended with KEY_EMPTY to search among.
452 *  \param key_name   Name of key to search for among the keys.
453 *  \param key_type   Data Type of value. INTEGER, ADDRESS, STRING.
454 *  \return           Returns NULL if no value found matching Key Name and Key
455 *                    Type.
456 */
457extern union drvmgr_key_value *drvmgr_key_val_get(
458        struct drvmgr_key *keys,
459        char *key_name,
460        int key_type);
461
462/*! Get key value from the bus resources matching [device, key name, key type]
463 *  if no matching key is found NULL is returned.
464 *
465 * This is typically used by device drivers to find a particular device
466 * resource.
467 *
468 * \param dev         The device to search resource for.
469 * \param key_name    The key name to search for
470 * \param key_type    The key type expected.
471 * \return            Returns NULL if no value found matching Key Name and
472 *                    Key Type was found for device.
473 */
474extern union drvmgr_key_value *drvmgr_dev_key_get(
475        struct drvmgr_dev *dev,
476        char *key_name,
477        int key_type);
478
479/*** DRIVER INTERACE USED TO REQUEST INFORMATION/SERVICES FROM BUS DRIVER ***/
480
481/*! Get parent bus */
482static inline struct drvmgr_bus *drvmgr_get_parent(struct drvmgr_dev *dev)
483{
484        if (dev)
485                return dev->parent;
486        else
487                return NULL;
488}
489
490/*! Get Driver of device */
491static inline struct drvmgr_drv *drvmgr_get_drv(struct drvmgr_dev *dev)
492{
493        if (dev)
494                return dev->drv;
495        else
496                return NULL;
497}
498
499/*! Calls func() for every device found in the device tree, regardless of
500 * device state or if a driver is assigned. With the options argument the user
501 * can decide to do either a depth-first or a breadth-first search.
502 *
503 * If the function func() returns a non-zero value then for_each_dev will
504 * return imediatly with the same return value as func() returned.
505 *
506 * \param func       Function called on each device
507 * \param arg        Custom function argument
508 * \param options    Search Options, see DRVMGR_FED_*
509 *
510 */
511#define DRVMGR_FED_BF 1         /* Breadth-first search */
512#define DRVMGR_FED_DF 0         /* Depth first search */
513extern int drvmgr_for_each_dev(
514        int (*func)(struct drvmgr_dev *dev, void *arg),
515        void *arg,
516        int options);
517
518/*! Get Device pointer from Driver and Driver minor number
519 *
520 * \param drv         Driver the device is united with.
521 * \param minor       Driver minor number assigned to device.
522 * \param pdev        Location where the Device point will be stored.
523 * \return            Zero on success. -1 on failure, when device was not
524 *                    found in driver device list.
525 */
526extern int drvmgr_get_dev(
527        struct drvmgr_drv *drv,
528        int minor,
529        struct drvmgr_dev **pdev);
530
531/*! Get Bus frequency in Hertz. Frequency is stored into address of freq_hz.
532 *
533 * \param dev        The Device to get Bus frequency for.
534 * \param options    Bus-type specific options
535 * \param freq_hz    Location where Bus Frequency will be stored.
536 */
537extern int drvmgr_freq_get(
538        struct drvmgr_dev *dev,
539        int options,
540        unsigned int *freq_hz);
541
542/*! Return 0 if dev is not located on the root bus, 1 if on root bus */
543extern int drvmgr_on_rootbus(struct drvmgr_dev *dev);
544
545/*! Get device name prefix, this name can be used to register a unique name in
546 *  the bus->error filesystem or to get an idea where the device is located.
547 *
548 * \param dev         The Device to get the device Prefix for.
549 * \param dev_prefix  Location where the prefix will be stored.
550 */
551extern int drvmgr_get_dev_prefix(struct drvmgr_dev *dev, char *dev_prefix);
552
553/*! Register a shared interrupt handler. Since this service is shared among
554 *  interrupt drivers/handlers the handler[arg] must be installed before the
555 *  interrupt can be cleared or disabled. The handler is by default disabled
556 *  after registration.
557 *
558 *  \param index      Index is used to identify the IRQ number if hardware has
559 *                    multiple IRQ sources. Normally Index is set to 0 to
560 *                    indicated the first and only IRQ source.
561 *                    A negative index is interpreted as a absolute bus IRQ
562 *                    number.
563 *  \param isr        Interrupt Service Routine.
564 *  \param arg        Optional ISR argument.
565 */
566extern int drvmgr_interrupt_register(
567        struct drvmgr_dev *dev,
568        int index,
569        const char *info,
570        drvmgr_isr isr,
571        void *arg);
572
573/*! Unregister an interrupt handler. This also disables the interrupt before
574 *  unregistering the interrupt handler.
575 *  \param index      Index is used to identify the IRQ number if hardware has
576 *                    multiple IRQ sources. Normally Index is set to 0 to
577 *                    indicated the first and only IRQ source.
578 *                    A negative index is interpreted as a absolute bus IRQ
579 *                    number.
580 *  \param isr        Interrupt Service Routine, previously registered.
581 *  \param arg        Optional ISR argument, previously registered.
582 */
583extern int drvmgr_interrupt_unregister(
584        struct drvmgr_dev *dev,
585        int index,
586        drvmgr_isr isr,
587        void *arg);
588
589/*! Clear (ACK) pending interrupt
590 *
591 *  \param dev        Device to clear interrupt for.
592 *  \param index      Index is used to identify the IRQ number if hardware has multiple IRQ sources.
593 *                    Normally Index is set to 0 to indicated the first and only IRQ source.
594 *                    A negative index is interpreted as a absolute bus IRQ number.
595 *  \param isr        Interrupt Service Routine, previously registered.
596 *  \param arg        Optional ISR argument, previously registered.
597 */
598extern int drvmgr_interrupt_clear(
599        struct drvmgr_dev *dev,
600        int index);
601
602/*! Force unmasking/enableing an interrupt on the interrupt controller, this is not normally used,
603 *  if used the caller has masked/disabled the interrupt just before.
604 *
605 *  \param dev        Device to clear interrupt for.
606 *  \param index      Index is used to identify the IRQ number if hardware has multiple IRQ sources.
607 *                    Normally Index is set to 0 to indicated the first and only IRQ source.
608 *                    A negative index is interpreted as a absolute bus IRQ number.
609 *  \param isr        Interrupt Service Routine, previously registered.
610 *  \param arg        Optional ISR argument, previously registered.
611 */
612extern int drvmgr_interrupt_unmask(
613        struct drvmgr_dev *dev,
614        int index);
615
616/*! Force masking/disable an interrupt on the interrupt controller, this is not normally performed
617 *  since this will stop all other (shared) ISRs to be disabled until _unmask() is called.
618 *
619 *  \param dev        Device to mask interrupt for.
620 *  \param index      Index is used to identify the IRQ number if hardware has multiple IRQ sources.
621 *                    Normally Index is set to 0 to indicated the first and only IRQ source.
622 *                    A negative index is interpreted as a absolute bus IRQ number.
623 */
624extern int drvmgr_interrupt_mask(
625        struct drvmgr_dev *dev,
626        int index);
627
628/*! drvmgr_translate() translation options */
629enum drvmgr_tr_opts {
630        /* Translate CPU RAM Address (input) to DMA unit accessible address
631         * (output), this is an upstreams translation in reverse order.
632         *
633         * Typical Usage:
634         * It is common to translate a CPU accessible RAM address to an
635         * address that DMA units can access over bridges.
636         */
637        CPUMEM_TO_DMA = 0x0,
638
639        /* Translate DMA Unit Accessible address mapped to CPU RAM (input) to
640         * CPU accessible address (output). This is an upstreams translation.
641         *
642         * Typical Usage (not often used):
643         * The DMA unit descriptors contain pointers to DMA buffers located at
644         * CPU RAM addresses that the DMA unit can access, the CPU processes
645         * the descriptors and want to access the data but a translation back
646         * to CPU address is required.
647         */
648        CPUMEM_FROM_DMA = 0x1,
649
650        /* Translate DMA Memory Address (input) to CPU accessible address
651         * (output), this is a downstreams translation in reverse order.
652         *
653         * Typical Usage:
654         * A PCI network card puts packets into its memory not doing DMA over
655         * PCI, in order for the CPU to access them the PCI address must be
656         * translated.
657         */
658        DMAMEM_TO_CPU = 0x2,
659
660        /* Translate CPU accessible address (input) mapped to DMA Memory Address
661         * to DMA Unit accessible address (output). This is a downstreams
662         * translation.
663         */
664        DMAMEM_FROM_CPU = 0x3,
665};
666#define DRVMGR_TR_REVERSE 0x1   /* do reverse translation direction order */
667#define DRVMGR_TR_PATH 0x2      /* 0x0=down-stream 0x2=up-stream address path */
668 
669/*! Translate an address on one bus to an address on another bus.
670 *
671 *  The device determines source or destination bus, the root bus is always
672 *  the other bus. It is assumed that the CPU is located on the root bus or
673 *  that it can access it without address translation (mapped 1:1). The CPU
674 *  is thus assumed to be located on level 0 top most in the bus hierarchy.
675 *
676 *  If no map is present in the bus driver src_address is translated 1:1
677 *  (just copied).
678 *
679 *  Addresses are typically converted up-streams from the DMA unit towards the
680 *  CPU (DMAMEM_TO_CPU) or down-streams towards DMA hardware from the CPU
681 *  (CPUMEM_TO_DMA) over one or multiple bridges depending on bus architecture.
682 *  See 'enum drvmgr_tr_opts' for other translation direction options.
683 *  For example:
684 *  Two common operations is to translate a CPU accessible RAM address to an
685 *  address that DMA units can access (dev=DMA-unit, CPUMEM_TO_DMA,
686 *  src_address=CPU-RAM-ADR) and to translate an address of a PCI resource for
687 *  example RAM mapped into a PCI BAR to an CPU accessible address
688 *  (dev=PCI-device, DMAMEM_TO_CPU, src_address=PCI-BAR-ADR).
689 *
690 *  Source address is translated and the result is put into *dst_address, if
691 *  the address is not accessible on the other bus -1 is returned.
692 *
693 *  \param dev             Device to translate addresses for
694 *  \param options         Tanslation direction options, see enum drvmgr_tr_opts
695 *  \param src_address     Address to translate
696 *  \param dst_address     Location where translated address is stored
697 *
698 *  Returns 0 if unable to translate. The remaining length from the given
699 *  address of the map is returned on success, for example if a map starts
700 *  at 0x40000000 of size 0x100000 the result will be 0x40000 if the address
701 *  was translated into 0x400C0000.
702 *  If dev is on root-bus no translation is performed 0xffffffff is returned
703 *  and src_address is stored in *dst_address.
704 */
705extern unsigned int drvmgr_translate(
706        struct drvmgr_dev *dev,
707        unsigned int options,
708        void *src_address,
709        void **dst_address);
710
711/* Translate addresses between buses, used internally to implement
712 * drvmgr_translate. Function is not limited to translate from/to root bus
713 * where CPU is resident, however buses must be on a straight path relative
714 * to each other (parent of parent of parent and so on).
715 *
716 * \param from         src_address is given for this bus
717 * \param to           src_address is translated to this bus
718 * \param reverse      Selects translation method, if map entries are used in
719 *                     the reverse order (map_up->to is used as map_up->from)
720 * \param src_address  Address to be translated
721 * \param dst_address  Translated address is stored here on success (return=0)
722 *
723 *  Returns 0 if unable to translate. The remaining length from the given
724 *  address of the map is returned on success and the result is stored into
725 *  *dst_address. For example if a map starts at 0x40000000 of size 0x100000
726 *  the result will be 0x40000 if the address was translated into 0x400C0000.
727 *  If dev is on root-bus no translation is performed 0xffffffff is returned.
728 *  and src_address is stored in *dst_address.
729 */
730extern unsigned int drvmgr_translate_bus(
731        struct drvmgr_bus *from,
732        struct drvmgr_bus *to,
733        int reverse,
734        void *src_address,
735        void **dst_address);
736
737/* Calls drvmgr_translate() to translate an address range and checks the result,
738 * a printout is generated if the check fails. All parameters are passed on to
739 * drvmgr_translate() except for size, see paramters of drvmgr_translate().
740 *
741 * If size=0 only the starting address is not checked.
742 *
743 * If mapping failes a non-zero result is returned.
744 */
745extern int drvmgr_translate_check(
746        struct drvmgr_dev *dev,
747        unsigned int options,
748        void *src_address,
749        void **dst_address,
750        unsigned int size);
751
752/*! Get function pointer from Device Driver or Bus Driver.
753 *
754 *  Returns 0 if function is available
755 */
756extern int drvmgr_func_get(void *obj, int funcid, void **func);
757
758/*! Lookup function and call it directly with the four optional arguments */
759extern int drvmgr_func_call(void *obj, int funcid, void *a, void *b, void *c, void *d);
760
761/* Builds a Function ID.
762 *
763 * Used to request optional functions by a bus or device driver
764 */
765#define DRVMGR_FUNCID(major, minor) ((((major) & 0xfff) << 20) | ((minor) & 0xfffff))
766#define DRVMGR_FUNCID_NONE 0
767#define DRVMGR_FUNCID_END DRVMGR_FUNCID(DRVMGR_FUNCID_NONE, 0)
768
769/* Major Function ID. Most significant 12-bits. */
770enum {
771        FUNCID_NONE             = 0x000,
772        FUNCID_RW               = 0x001, /* Read/Write functions */
773};
774
775/* Select Sub-Function Read/Write function by ID */
776#define RW_SIZE_1   0x00001    /* Access Size */
777#define RW_SIZE_2   0x00002
778#define RW_SIZE_4   0x00004
779#define RW_SIZE_8   0x00008
780#define RW_SIZE_ANY 0x00000
781#define RW_SIZE(id) ((unsigned int)(id) & 0xf)
782
783#define RW_DIR_ANY  0x00000   /* Access Direction */
784#define RW_READ     0x00000   /* Read */
785#define RW_WRITE    0x00010   /* Write */
786#define RW_SET      0x00020   /* Write with same value (memset) */
787#define RW_DIR(id)  (((unsigned int)(id) >> 4) & 0x3)
788
789#define RW_RAW      0x00000  /* Raw access - no swapping (machine default) */
790#define RW_LITTLE   0x00040  /* Little Endian */
791#define RW_BIG      0x00080  /* Big Endian */
792#define RW_ENDIAN(id) (((unsigned int)(id) >> 6) & 0x3)
793
794#define RW_TYPE_ANY 0x00000  /* Access type */
795#define RW_REG      0x00100
796#define RW_MEM      0x00200
797#define RW_MEMREG   0x00300
798#define RW_CFG      0x00400
799#define RW_TYPE(id) (((unsigned int)(id) >> 8) & 0xf)
800
801#define RW_ARG      0x01000 /* Optional Argument */
802#define RW_ERR      0x02000 /* Optional Error Handler */
803
804/* Build a Read/Write function ID */
805#define DRVMGR_RWFUNC(minor) DRVMGR_FUNCID(FUNCID_RW, minor)
806
807/* Argument to Read/Write functions, the "void *arg" pointer is returned by
808 * RW_ARG. If NULL is returned no argument is needed.
809 */
810struct drvmgr_rw_arg {
811        void *arg;
812        struct drvmgr_dev *dev;
813};
814
815/* Standard Read/Write function types */
816typedef uint8_t (*drvmgr_r8)(uint8_t *srcadr);
817typedef uint16_t (*drvmgr_r16)(uint16_t *srcadr);
818typedef uint32_t (*drvmgr_r32)(uint32_t *srcadr);
819typedef uint64_t (*drvmgr_r64)(uint64_t *srcadr);
820typedef void (*drvmgr_w8)(uint8_t *dstadr, uint8_t data);
821typedef void (*drvmgr_w16)(uint16_t *dstadr, uint16_t data);
822typedef void (*drvmgr_w32)(uint32_t *dstadr, uint32_t data);
823typedef void (*drvmgr_w64)(uint64_t *dstadr, uint64_t data);
824/* READ/COPY a memory area located on bus into CPU memory.
825 * From 'src' (remote) to the destination 'dest' (local), n=number of bytes
826 */
827typedef int (*drvmgr_rmem)(void *dest, const void *src, int n);
828/* WRITE/COPY a user buffer located in CPU memory to a location on the bus.
829 * From 'src' (local) to the destination 'dest' (remote), n=number of bytes
830 */
831typedef int (*drvmgr_wmem)(void *dest, const void *src, int n);
832/* Set a memory area to the byte value given in c, see LIBC memset(). Memset is
833 * implemented by calling wmem() multiple times with a "large" buffer.
834 */
835typedef int (*drvmgr_memset)(void *dstadr, int c, size_t n);
836
837/* Read/Write function types with additional argument */
838typedef uint8_t (*drvmgr_r8_arg)(uint8_t *srcadr, void *a);
839typedef uint16_t (*drvmgr_r16_arg)(uint16_t *srcadr, void *a);
840typedef uint32_t (*drvmgr_r32_arg)(uint32_t *srcadr, void *a);
841typedef uint64_t (*drvmgr_r64_arg)(uint64_t *srcadr, void *a);
842typedef void (*drvmgr_w8_arg)(uint8_t *dstadr, uint8_t data, void *a);
843typedef void (*drvmgr_w16_arg)(uint16_t *dstadr, uint16_t data, void *a);
844typedef void (*drvmgr_w32_arg)(uint32_t *dstadr, uint32_t data, void *a);
845typedef void (*drvmgr_w64_arg)(uint64_t *dstadr, uint64_t data, void *a);
846typedef int (*drvmgr_rmem_arg)(void *dest, const void *src, int n, void *a);
847typedef int (*drvmgr_wmem_arg)(void *dest, const void *src, int n, void *a);
848typedef int (*drvmgr_memset_arg)(void *dstadr, int c, size_t n, void *a);
849
850/* Report an error to the parent bus of the device */
851typedef void (*drvmgr_rw_err)(struct drvmgr_rw_arg *a, struct drvmgr_bus *bus,
852                                int funcid, void *adr);
853
854/* Helper function for buses that implement the memset() over wmem() */
855extern void drvmgr_rw_memset(
856        void *dstadr,
857        int c,
858        size_t n,
859        void *a,
860        drvmgr_wmem_arg wmem
861        );
862
863/*** PRINT INFORMATION ABOUT DRIVER MANAGER ***/
864
865/*! Calls func() for every device found matching the search requirements of
866 * set_mask and clr_mask. Each bit set in set_mask must be set in the
867 * device state bit mask (dev->state), and Each bit in the clr_mask must
868 * be cleared in the device state bit mask (dev->state). There are three
869 * special cases:
870 *
871 * 1. If state_set_mask and state_clr_mask are zero the state bits are
872 *    ignored and all cores are treated as a match.
873 *
874 * 2. If state_set_mask is zero the function func will not be called due to
875 *    a bit being set in the state mask.
876 *
877 * 3. If state_clr_mask is zero the function func will not be called due to
878 *    a bit being cleared in the state mask.
879 *
880 * If the function func() returns a non-zero value then for_each_dev will
881 * return imediatly with the same return value as func() returned.
882 *
883 * \param devlist            The list to iterate though searching for devices.
884 * \param state_set_mask     Defines the bits that must be set in dev->state
885 * \param state_clr_mask     Defines the bits that must be cleared in dev->state
886 * \param func               Function called on each
887 *
888 */
889extern int drvmgr_for_each_listdev(
890        struct drvmgr_list *devlist,
891        unsigned int state_set_mask,
892        unsigned int state_clr_mask,
893        int (*func)(struct drvmgr_dev *dev, void *arg),
894        void *arg);
895
896/* Print all devices */
897#define PRINT_DEVS_FAILED       0x01    /* Failed during initialization */
898#define PRINT_DEVS_ASSIGNED     0x02    /* Driver assigned */
899#define PRINT_DEVS_UNASSIGNED   0x04    /* Driver not assigned */
900#define PRINT_DEVS_IGNORED      0x08    /* Device ignored on user's request */
901#define PRINT_DEVS_ALL          (PRINT_DEVS_FAILED | \
902                                PRINT_DEVS_ASSIGNED | \
903                                PRINT_DEVS_UNASSIGNED |\
904                                PRINT_DEVS_IGNORED)
905
906/*! Print number of devices, buses and drivers */
907extern void drvmgr_summary(void);
908
909/*! Print devices with certain condictions met according to 'options' */
910extern void drvmgr_print_devs(unsigned int options);
911
912/*! Print device/bus topology */
913extern void drvmgr_print_topo(void);
914
915/*! Print the memory usage
916 * Only accounts for data structures. Not for the text size.
917 */
918extern void drvmgr_print_mem(void);
919
920#define OPTION_DEV_GENINFO   0x00000001
921#define OPTION_DEV_BUSINFO   0x00000002
922#define OPTION_DEV_DRVINFO   0x00000004
923#define OPTION_DRV_DEVS      0x00000100
924#define OPTION_BUS_DEVS      0x00010000
925#define OPTION_RECURSIVE     0x01000000
926#define OPTION_INFO_ALL      0xffffffff
927
928/*! Print information about a driver manager object (device, driver, bus) */
929extern void drvmgr_info(void *id, unsigned int options);
930
931/*! Get information about a device */
932extern void drvmgr_info_dev(struct drvmgr_dev *dev, unsigned int options);
933
934/*! Get information about a bus */
935extern void drvmgr_info_bus(struct drvmgr_bus *bus, unsigned int options);
936
937/*! Get information about a driver */
938extern void drvmgr_info_drv(struct drvmgr_drv *drv, unsigned int options);
939
940/*! Get information about all devices on a bus */
941extern void drvmgr_info_devs_on_bus(struct drvmgr_bus *bus, unsigned int options);
942
943/*! Get information about all devices in the system (on all buses) */
944extern void drvmgr_info_devs(unsigned int options);
945
946/*! Get information about all drivers in the system */
947extern void drvmgr_info_drvs(unsigned int options);
948
949/*! Get information about all buses in the system */
950extern void drvmgr_info_buses(unsigned int options);
951
952/*! Get Driver by Driver ID */
953extern struct drvmgr_drv *drvmgr_drv_by_id(uint64_t id);
954
955/*! Get Driver by Driver Name */
956extern struct drvmgr_drv *drvmgr_drv_by_name(const char *name);
957
958/*! Get Device by Device Name */
959extern struct drvmgr_dev *drvmgr_dev_by_name(const char *name);
960
961#ifdef __cplusplus
962}
963#endif
964
965#endif
Note: See TracBrowser for help on using the repository browser.