Skip to content

Commit d802912

Browse files
jihandonganchao
authored andcommitted
nuttx ai driver update
Signed-off-by: jihandong <jihandong@xiaomi.com>
1 parent 6aeb2e2 commit d802912

File tree

2 files changed

+51
-131
lines changed

2 files changed

+51
-131
lines changed

drivers/aie/ai_engine.c

Lines changed: 38 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,12 @@
3535
* Private Type Definitions
3636
****************************************************************************/
3737

38-
enum aie_state_e
39-
{
40-
AIE_STATE_NOP = 0,
41-
AIE_STATE_INITED,
42-
AIE_STATE_FEEDED,
43-
AIE_STATE_INFERENCED,
44-
};
45-
4638
/* This structure describes the state of the upper half driver */
4739

4840
struct aie_upperhalf_s
4941
{
5042
FAR struct aie_lowerhalf_s *lower; /* The handle of lower half driver */
51-
volatile enum aie_state_e state; /* The device state */
5243
mutex_t lock; /* Mutual exclusion */
53-
uint8_t crefs; /* The number of times the engine
54-
* has been opend. */
5544
};
5645

5746
/****************************************************************************
@@ -87,52 +76,37 @@ static const struct file_operations g_aie_ops =
8776
* Name: aie_open
8877
*
8978
* Description:
90-
* A open method to increase the crefs.
79+
* A open method to open the ai engine.
9180
*
9281
****************************************************************************/
9382

9483
static int aie_open(FAR struct file *filep)
9584
{
96-
FAR struct inode *inode = filep->f_inode;
97-
FAR struct aie_upperhalf_s *upper = inode->i_private;
98-
int ret;
85+
FAR struct inode *inode = filep->f_inode;
86+
FAR struct aie_upperhalf_s *upper = inode->i_private;
87+
FAR struct aie_lowerhalf_s *lower;
9988

10089
DEBUGASSERT(upper != NULL);
101-
ret = nxmutex_lock(&upper->lock);
102-
if (ret < 0)
103-
{
104-
return ret;
105-
}
106-
107-
if (upper->crefs == 0)
108-
{
109-
upper->crefs++;
110-
ret = OK;
111-
}
112-
else
113-
{
114-
ret = -EBUSY;
115-
}
116-
117-
nxmutex_unlock(&upper->lock);
90+
lower = upper->lower;
91+
DEBUGASSERT(lower != NULL);
11892

119-
return ret;
93+
return OK;
12094
}
12195

12296
/****************************************************************************
12397
* Name: aie_close
12498
*
12599
* Description:
126-
* A close method to decrease the crefs.
100+
* A close method to close the ai engine.
127101
*
128102
****************************************************************************/
129103

130104
static int aie_close(FAR struct file *filep)
131105
{
132-
FAR struct inode *inode = filep->f_inode;
133-
FAR struct aie_upperhalf_s *upper = inode->i_private;
134-
FAR struct aie_lowerhalf_s *lower = NULL;
135-
int ret;
106+
FAR struct inode *inode = filep->f_inode;
107+
FAR struct aie_upperhalf_s *upper = inode->i_private;
108+
FAR struct aie_lowerhalf_s *lower;
109+
int ret;
136110

137111
DEBUGASSERT(upper != NULL);
138112
lower = upper->lower;
@@ -144,23 +118,15 @@ static int aie_close(FAR struct file *filep)
144118
return ret;
145119
}
146120

147-
if (upper->crefs == 1)
121+
ret = (int)(intptr_t)filep->f_priv;
122+
if (ret > 0)
148123
{
149-
ret = lower->ops->deinit(lower, filep);
150-
if (ret == OK)
151-
{
152-
upper->state = AIE_STATE_NOP;
153-
upper->crefs--;
154-
}
155-
}
156-
else
157-
{
158-
ret = OK;
124+
lower->ops->deinit(lower, ret);
159125
}
160126

161127
nxmutex_unlock(&upper->lock);
162128

163-
return ret;
129+
return OK;
164130
}
165131

166132
/****************************************************************************
@@ -174,10 +140,10 @@ static int aie_close(FAR struct file *filep)
174140

175141
static int aie_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
176142
{
177-
FAR struct inode *inode = filep->f_inode;
178-
FAR struct aie_upperhalf_s *upper = inode->i_private;
179-
FAR struct aie_lowerhalf_s *lower = NULL;
180-
int ret;
143+
FAR struct inode *inode = filep->f_inode;
144+
FAR struct aie_upperhalf_s *upper = inode->i_private;
145+
FAR struct aie_lowerhalf_s *lower = NULL;
146+
int ret;
181147

182148
DEBUGASSERT(upper != NULL);
183149
lower = upper->lower;
@@ -193,66 +159,38 @@ static int aie_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
193159

194160
switch (cmd)
195161
{
196-
case AIE_CMD_INIT:
197-
ret = lower->ops->init(lower, filep, arg);
198-
if (ret != OK)
162+
case AIE_CMD_LOAD:
163+
ret = (int)(intptr_t)filep->f_priv;
164+
if (ret > 0)
199165
{
200-
break;
166+
ret = -EINVAL; /* Double load is not allowed */
201167
}
202-
203-
if (lower->workspace_len)
168+
else
204169
{
205-
lower->workspace = kmm_malloc(lower->workspace_len);
206-
if (!lower->workspace)
170+
ret = lower->ops->init(lower, arg /* model */);
171+
if (ret > 0)
207172
{
208-
ret = -ENOMEM;
209-
break;
173+
filep->f_priv = (void *)(intptr_t)ret;
174+
ret = OK;
210175
}
211176
}
212-
213-
upper->state = AIE_STATE_INITED;
214177
break;
215178

216179
case AIE_CMD_FEED_INPUT:
217-
if (upper->state == AIE_STATE_INITED ||
218-
upper->state == AIE_STATE_INFERENCED)
219-
{
220-
lower->input = (uintptr_t)arg;
221-
ret = lower->ops->feed_input(lower, filep);
222-
if (ret == OK)
223-
{
224-
upper->state = AIE_STATE_FEEDED;
225-
}
226-
}
227-
else
228-
{
229-
ret = -EPERM;
230-
}
180+
ret = lower->ops->feed_input(lower, (int)(intptr_t)filep->f_priv,
181+
arg /* input */);
231182
break;
232183

233184
case AIE_CMD_GET_OUTPUT:
234-
if (upper->state == AIE_STATE_FEEDED)
235-
{
236-
lower->output = (uintptr_t)arg;
237-
ret = lower->ops->get_output(lower, filep);
238-
if (ret == OK)
239-
{
240-
upper->state = AIE_STATE_INFERENCED;
241-
}
242-
}
243-
else
244-
{
245-
ret = -EPERM;
246-
}
185+
ret = lower->ops->get_output(lower, (int)(intptr_t)filep->f_priv,
186+
arg /* output */);
247187
break;
248188

249189
default:
250-
251-
/* Lowerhalf driver process other cmd. */
252-
253190
if (lower->ops->control)
254191
{
255-
ret = lower->ops->control(lower, filep, cmd, arg);
192+
ret = lower->ops->control(lower, (int)(intptr_t)filep->f_priv,
193+
cmd, arg);
256194
}
257195
else
258196
{
@@ -281,8 +219,8 @@ static int aie_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
281219
int aie_register(FAR const char *path,
282220
FAR struct aie_lowerhalf_s *lower)
283221
{
284-
FAR struct aie_upperhalf_s *upper = NULL;
285-
int ret = -ENOMEM;
222+
FAR struct aie_upperhalf_s *upper = NULL;
223+
int ret = -ENOMEM;
286224

287225
DEBUGASSERT(path);
288226

@@ -298,11 +236,7 @@ int aie_register(FAR const char *path,
298236

299237
/* Initialize the aie device structure */
300238

301-
upper->lower = lower;
302-
upper->state = AIE_STATE_NOP;
303-
upper->crefs = 0;
304-
lower->priv = upper;
305-
239+
upper->lower = lower;
306240
nxmutex_init(&upper->lock);
307241

308242
/* Register the aie device */

include/nuttx/aie/ai_engine.h

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
enum aie_cmd_e
4141
{
42-
AIE_CMD_INIT = 0,
42+
AIE_CMD_LOAD = 0,
4343
AIE_CMD_FEED_INPUT,
4444
AIE_CMD_GET_OUTPUT
4545
};
@@ -51,25 +51,17 @@ enum aie_cmd_e
5151
struct aie_lowerhalf_s;
5252
struct aie_ops_s
5353
{
54-
/* Common routes */
54+
CODE int (*init)(FAR struct aie_lowerhalf_s *lower, uintptr_t model);
5555

56-
CODE int (*init)(FAR struct aie_lowerhalf_s *lower,
57-
FAR struct file *filep,
58-
uintptr_t bin_model);
56+
CODE int (*deinit)(FAR struct aie_lowerhalf_s *lower, int id);
5957

60-
CODE int (*feed_input)(FAR struct aie_lowerhalf_s *lower,
61-
FAR struct file *filep);
58+
CODE int (*feed_input)(FAR struct aie_lowerhalf_s *lower, int id,
59+
uintptr_t input);
6260

63-
CODE int (*get_output)(FAR struct aie_lowerhalf_s *lower,
64-
FAR struct file *filep);
61+
CODE int (*get_output)(FAR struct aie_lowerhalf_s *lower, int id,
62+
uintptr_t output);
6563

66-
CODE int (*deinit)(FAR struct aie_lowerhalf_s *lower,
67-
FAR struct file *filep);
68-
69-
/* Custom route */
70-
71-
CODE int (*control)(FAR struct aie_lowerhalf_s *lower,
72-
FAR struct file *filep,
64+
CODE int (*control)(FAR struct aie_lowerhalf_s *lower, int id,
7365
int cmd, unsigned long arg);
7466
};
7567

@@ -81,17 +73,11 @@ struct aie_ops_s
8173

8274
struct aie_lowerhalf_s
8375
{
84-
/* The private opaque pointer to be passed to upper-layer */
85-
86-
FAR void *priv;
87-
FAR const struct aie_ops_s *ops; /* Lower half operations */
88-
const void *bin_model; /* The model address */
89-
const void *input; /* The input buffer */
90-
void *output; /* The output buffer */
91-
void *workspace; /* Workspace free to use */
92-
size_t workspace_len; /* Length of workspace.
93-
* >0: need the workspace
94-
* 0: no need */
76+
/* The heading fields of this struct must be as follow. */
77+
78+
FAR const struct aie_ops_s *ops;
79+
80+
/* The custom AI Engine may include additional fields after here. */
9581
};
9682

9783
/****************************************************************************

0 commit comments

Comments
 (0)