1/*
2 * Copyright (c) 2014 ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetsstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <barrelfish/barrelfish.h>
11
12#include <dma_internal.h>
13#include <dma_device_internal.h>
14
15/*
16 * ===========================================================================
17 * Public Interface
18 * ===========================================================================
19 */
20
21/**
22 * \brief polls the channels of the IOAT DMA device
23 *
24 * \param dev   IOAT DMA device
25 *
26 * \returns SYS_ERR_OK on success
27 *          DMA_ERR_DEVICE_IDLE if there is nothing completed on the channels
28 *          errval on error
29 */
30errval_t dma_device_poll_channels(struct dma_device *dev)
31{
32    if (dev->f.poll) {
33        return dev->f.poll(dev);
34    }
35
36    return DMA_ERR_DEVICE_UNSUPPORTED;
37}
38
39/*
40 * ----------------------------------------------------------------------------
41 * Getters / Setters
42 * ----------------------------------------------------------------------------
43 */
44
45/**
46 * \brief gets the ID of the DMA device
47 *
48 * \param dev   DMA device
49 *
50 * \returns DMA device ID
51 */
52inline dma_dev_id_t dma_device_get_id(struct dma_device *dev)
53{
54    return dev->id;
55}
56
57/**
58 * \brief gets the DMA device type of the DMA device
59 *
60 * \param dev   DMA device
61 *
62 * \returns DMA device type
63 */
64inline dma_dev_id_t dma_device_get_type(struct dma_device *dev)
65{
66    return dev->type;
67}
68
69/**
70 * \brief gets the state of the DMA device
71 *
72 * \param dev   DMA device
73 *
74 * \returns DMA device state
75 */
76inline dma_dev_st_t dma_device_get_state(struct dma_device *dev)
77{
78    return dev->state;
79}
80
81
82/**
83 * \brief gets the virtual address of the mapped MMIO register space
84 *
85 * \param dev   DMA device
86 *
87 * \returns MMIO register vbase
88 */
89inline void *dma_device_get_mmio_vbase(struct dma_device *dev)
90{
91    return (void *)dev->mmio.vaddr;
92}
93
94
95/**
96 * \brief gets the number of channels this device has
97 *
98 * \param dev   DMA device
99 *
100 * \returns DMA channel count
101 */
102inline uint8_t dma_device_get_channel_count(struct dma_device *dev)
103{
104    return dev->channels.count;
105}
106
107
108/**
109 * \brief obtains the channel associated with the given index
110 *
111 * \param dev   DMA device
112 *
113 * \returns DMA channel if index exists
114 *          NULL if index exceeds channel count
115 */
116
117struct dma_channel *dma_device_get_channe_by_idx(struct dma_device *dev,
118                                                 uint8_t idx)
119{
120    if (idx < dev->channels.count) {
121        return dev->channels.c[idx];
122    }
123    return NULL;
124}
125
126/**
127 * \brief gets a DMA channel from this device in a round robin fashion
128 *
129 * \param dev   DMA device
130 *
131 * \returns DMA channel
132 */
133inline struct dma_channel *dma_device_get_channel(struct dma_device *dev)
134{
135    if (dev->channels.next >= dev->channels.count) {
136        dev->channels.next = 0;
137    }
138    return dev->channels.c[dev->channels.next++];
139}
140
141/*
142 * ----------------------------------------------------------------------------
143 * Interrupt Management
144 * ----------------------------------------------------------------------------
145 */
146
147