1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2011-2016 Freescale Semiconductor Inc.
10 #include <sys/ioctl.h>
16 /* As higher-level drivers will be built on top of this (dma_mem, qbman, ...),
17 * it's preferable that the process driver itself not provide any exported API.
18 * As such, combined with the fact that none of these operations are
19 * performance critical, it is justified to use lazy initialisation, so that's
20 * what the lock is for.
23 static pthread_mutex_t fd_init_lock
= PTHREAD_MUTEX_INITIALIZER
;
25 static int check_fd(void)
31 ret
= pthread_mutex_lock(&fd_init_lock
);
33 /* check again with the lock held */
35 fd
= open(PROCESS_PATH
, O_RDWR
);
36 ret
= pthread_mutex_unlock(&fd_init_lock
);
38 return (fd
>= 0) ? 0 : -ENODEV
;
41 #define DPAA_IOCTL_MAGIC 'u'
42 struct dpaa_ioctl_id_alloc
{
43 uint32_t base
; /* Return value, the start of the allocated range */
44 enum dpaa_id_type id_type
; /* what kind of resource(s) to allocate */
45 uint32_t num
; /* how many IDs to allocate (and return value) */
46 uint32_t align
; /* must be a power of 2, 0 is treated like 1 */
47 int partial
; /* whether to allow less than 'num' */
50 struct dpaa_ioctl_id_release
{
52 enum dpaa_id_type id_type
;
57 struct dpaa_ioctl_id_reserve
{
58 enum dpaa_id_type id_type
;
63 #define DPAA_IOCTL_ID_ALLOC \
64 _IOWR(DPAA_IOCTL_MAGIC, 0x01, struct dpaa_ioctl_id_alloc)
65 #define DPAA_IOCTL_ID_RELEASE \
66 _IOW(DPAA_IOCTL_MAGIC, 0x02, struct dpaa_ioctl_id_release)
67 #define DPAA_IOCTL_ID_RESERVE \
68 _IOW(DPAA_IOCTL_MAGIC, 0x0A, struct dpaa_ioctl_id_reserve)
70 int process_alloc(enum dpaa_id_type id_type
, uint32_t *base
, uint32_t num
,
71 uint32_t align
, int partial
)
73 struct dpaa_ioctl_id_alloc id
= {
83 ret
= ioctl(fd
, DPAA_IOCTL_ID_ALLOC
, &id
);
86 for (ret
= 0; ret
< (int)id
.num
; ret
++)
87 base
[ret
] = id
.base
+ ret
;
91 void process_release(enum dpaa_id_type id_type
, uint32_t base
, uint32_t num
)
93 struct dpaa_ioctl_id_release id
= {
101 fprintf(stderr
, "Process FD failure\n");
104 ret
= ioctl(fd
, DPAA_IOCTL_ID_RELEASE
, &id
);
106 fprintf(stderr
, "Process FD ioctl failure type %d base 0x%x num %d\n",
110 int process_reserve(enum dpaa_id_type id_type
, uint32_t base
, uint32_t num
)
112 struct dpaa_ioctl_id_reserve id
= {
117 int ret
= check_fd();
121 return ioctl(fd
, DPAA_IOCTL_ID_RESERVE
, &id
);
124 /***************************************/
125 /* Mapping and using QMan/BMan portals */
126 /***************************************/
128 #define DPAA_IOCTL_PORTAL_MAP \
129 _IOWR(DPAA_IOCTL_MAGIC, 0x07, struct dpaa_ioctl_portal_map)
130 #define DPAA_IOCTL_PORTAL_UNMAP \
131 _IOW(DPAA_IOCTL_MAGIC, 0x08, struct dpaa_portal_map)
133 int process_portal_map(struct dpaa_ioctl_portal_map
*params
)
135 int ret
= check_fd();
140 ret
= ioctl(fd
, DPAA_IOCTL_PORTAL_MAP
, params
);
142 perror("ioctl(DPAA_IOCTL_PORTAL_MAP)");
148 int process_portal_unmap(struct dpaa_portal_map
*map
)
150 int ret
= check_fd();
155 ret
= ioctl(fd
, DPAA_IOCTL_PORTAL_UNMAP
, map
);
157 perror("ioctl(DPAA_IOCTL_PORTAL_UNMAP)");
163 #define DPAA_IOCTL_PORTAL_IRQ_MAP \
164 _IOW(DPAA_IOCTL_MAGIC, 0x09, struct dpaa_ioctl_irq_map)
166 int process_portal_irq_map(int ifd
, struct dpaa_ioctl_irq_map
*map
)
169 return ioctl(ifd
, DPAA_IOCTL_PORTAL_IRQ_MAP
, map
);
172 int process_portal_irq_unmap(int ifd
)
177 struct dpaa_ioctl_raw_portal
{
179 enum dpaa_portal_type type
; /* Type of portal to allocate */
181 uint8_t enable_stash
; /* set to non zero to turn on stashing */
182 /* Stashing attributes for the portal */
186 /* Specifies the stash request queue this portal should use */
189 /* Specifes a specific portal index to map or QBMAN_ANY_PORTAL_IDX
190 * for don't care. The portal index will be populated by the
191 * driver when the ioctl() successfully completes.
200 #define DPAA_IOCTL_ALLOC_RAW_PORTAL \
201 _IOWR(DPAA_IOCTL_MAGIC, 0x0C, struct dpaa_ioctl_raw_portal)
203 #define DPAA_IOCTL_FREE_RAW_PORTAL \
204 _IOR(DPAA_IOCTL_MAGIC, 0x0D, struct dpaa_ioctl_raw_portal)
206 static int process_portal_allocate(struct dpaa_ioctl_raw_portal
*portal
)
208 int ret
= check_fd();
213 ret
= ioctl(fd
, DPAA_IOCTL_ALLOC_RAW_PORTAL
, portal
);
215 perror("ioctl(DPAA_IOCTL_ALLOC_RAW_PORTAL)");
221 static int process_portal_free(struct dpaa_ioctl_raw_portal
*portal
)
223 int ret
= check_fd();
228 ret
= ioctl(fd
, DPAA_IOCTL_FREE_RAW_PORTAL
, portal
);
230 perror("ioctl(DPAA_IOCTL_FREE_RAW_PORTAL)");
236 int qman_allocate_raw_portal(struct dpaa_raw_portal
*portal
)
238 struct dpaa_ioctl_raw_portal input
;
241 input
.type
= dpaa_portal_qman
;
242 input
.index
= portal
->index
;
243 input
.enable_stash
= portal
->enable_stash
;
244 input
.cpu
= portal
->cpu
;
245 input
.cache
= portal
->cache
;
246 input
.window
= portal
->window
;
247 input
.sdest
= portal
->sdest
;
249 ret
= process_portal_allocate(&input
);
252 portal
->index
= input
.index
;
253 portal
->cinh
= input
.cinh
;
254 portal
->cena
= input
.cena
;
258 int qman_free_raw_portal(struct dpaa_raw_portal
*portal
)
260 struct dpaa_ioctl_raw_portal input
;
262 input
.type
= dpaa_portal_qman
;
263 input
.index
= portal
->index
;
264 input
.cinh
= portal
->cinh
;
265 input
.cena
= portal
->cena
;
267 return process_portal_free(&input
);
270 int bman_allocate_raw_portal(struct dpaa_raw_portal
*portal
)
272 struct dpaa_ioctl_raw_portal input
;
275 input
.type
= dpaa_portal_bman
;
276 input
.index
= portal
->index
;
277 input
.enable_stash
= 0;
279 ret
= process_portal_allocate(&input
);
282 portal
->index
= input
.index
;
283 portal
->cinh
= input
.cinh
;
284 portal
->cena
= input
.cena
;
288 int bman_free_raw_portal(struct dpaa_raw_portal
*portal
)
290 struct dpaa_ioctl_raw_portal input
;
292 input
.type
= dpaa_portal_bman
;
293 input
.index
= portal
->index
;
294 input
.cinh
= portal
->cinh
;
295 input
.cena
= portal
->cena
;
297 return process_portal_free(&input
);