Merge tag 'drm-intel-next-2018-04-13' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-2.6-block.git] / drivers / staging / fsl-mc / include / dpaa2-fd.h
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /*
3  * Copyright 2014-2016 Freescale Semiconductor Inc.
4  * Copyright 2016 NXP
5  *
6  */
7 #ifndef __FSL_DPAA2_FD_H
8 #define __FSL_DPAA2_FD_H
9
10 #include <linux/kernel.h>
11
12 /**
13  * DOC: DPAA2 FD - Frame Descriptor APIs for DPAA2
14  *
15  * Frame Descriptors (FDs) are used to describe frame data in the DPAA2.
16  * Frames can be enqueued and dequeued to Frame Queues (FQs) which are consumed
17  * by the various DPAA accelerators (WRIOP, SEC, PME, DCE)
18  *
19  * There are three types of frames: single, scatter gather, and frame lists.
20  *
21  * The set of APIs in this file must be used to create, manipulate and
22  * query Frame Descriptors.
23  */
24
25 /**
26  * struct dpaa2_fd - Struct describing FDs
27  * @words:         for easier/faster copying the whole FD structure
28  * @addr:          address in the FD
29  * @len:           length in the FD
30  * @bpid:          buffer pool ID
31  * @format_offset: format, offset, and short-length fields
32  * @frc:           frame context
33  * @ctrl:          control bits...including dd, sc, va, err, etc
34  * @flc:           flow context address
35  *
36  * This structure represents the basic Frame Descriptor used in the system.
37  */
38 struct dpaa2_fd {
39         union {
40                 u32 words[8];
41                 struct dpaa2_fd_simple {
42                         __le64 addr;
43                         __le32 len;
44                         __le16 bpid;
45                         __le16 format_offset;
46                         __le32 frc;
47                         __le32 ctrl;
48                         __le64 flc;
49                 } simple;
50         };
51 };
52
53 #define FD_SHORT_LEN_FLAG_MASK  0x1
54 #define FD_SHORT_LEN_FLAG_SHIFT 14
55 #define FD_SHORT_LEN_MASK       0x3FFFF
56 #define FD_OFFSET_MASK          0x0FFF
57 #define FD_FORMAT_MASK          0x3
58 #define FD_FORMAT_SHIFT         12
59 #define FD_BPID_MASK            0x3FFF
60 #define SG_SHORT_LEN_FLAG_MASK  0x1
61 #define SG_SHORT_LEN_FLAG_SHIFT 14
62 #define SG_SHORT_LEN_MASK       0x1FFFF
63 #define SG_OFFSET_MASK          0x0FFF
64 #define SG_FORMAT_MASK          0x3
65 #define SG_FORMAT_SHIFT         12
66 #define SG_BPID_MASK            0x3FFF
67 #define SG_FINAL_FLAG_MASK      0x1
68 #define SG_FINAL_FLAG_SHIFT     15
69
70 enum dpaa2_fd_format {
71         dpaa2_fd_single = 0,
72         dpaa2_fd_list,
73         dpaa2_fd_sg
74 };
75
76 /**
77  * dpaa2_fd_get_addr() - get the addr field of frame descriptor
78  * @fd: the given frame descriptor
79  *
80  * Return the address in the frame descriptor.
81  */
82 static inline dma_addr_t dpaa2_fd_get_addr(const struct dpaa2_fd *fd)
83 {
84         return (dma_addr_t)le64_to_cpu(fd->simple.addr);
85 }
86
87 /**
88  * dpaa2_fd_set_addr() - Set the addr field of frame descriptor
89  * @fd: the given frame descriptor
90  * @addr: the address needs to be set in frame descriptor
91  */
92 static inline void dpaa2_fd_set_addr(struct dpaa2_fd *fd, dma_addr_t addr)
93 {
94         fd->simple.addr = cpu_to_le64(addr);
95 }
96
97 /**
98  * dpaa2_fd_get_frc() - Get the frame context in the frame descriptor
99  * @fd: the given frame descriptor
100  *
101  * Return the frame context field in the frame descriptor.
102  */
103 static inline u32 dpaa2_fd_get_frc(const struct dpaa2_fd *fd)
104 {
105         return le32_to_cpu(fd->simple.frc);
106 }
107
108 /**
109  * dpaa2_fd_set_frc() - Set the frame context in the frame descriptor
110  * @fd: the given frame descriptor
111  * @frc: the frame context needs to be set in frame descriptor
112  */
113 static inline void dpaa2_fd_set_frc(struct dpaa2_fd *fd, u32 frc)
114 {
115         fd->simple.frc = cpu_to_le32(frc);
116 }
117
118 /**
119  * dpaa2_fd_get_ctrl() - Get the control bits in the frame descriptor
120  * @fd: the given frame descriptor
121  *
122  * Return the control bits field in the frame descriptor.
123  */
124 static inline u32 dpaa2_fd_get_ctrl(const struct dpaa2_fd *fd)
125 {
126         return le32_to_cpu(fd->simple.ctrl);
127 }
128
129 /**
130  * dpaa2_fd_set_ctrl() - Set the control bits in the frame descriptor
131  * @fd: the given frame descriptor
132  * @ctrl: the control bits to be set in the frame descriptor
133  */
134 static inline void dpaa2_fd_set_ctrl(struct dpaa2_fd *fd, u32 ctrl)
135 {
136         fd->simple.ctrl = cpu_to_le32(ctrl);
137 }
138
139 /**
140  * dpaa2_fd_get_flc() - Get the flow context in the frame descriptor
141  * @fd: the given frame descriptor
142  *
143  * Return the flow context in the frame descriptor.
144  */
145 static inline dma_addr_t dpaa2_fd_get_flc(const struct dpaa2_fd *fd)
146 {
147         return (dma_addr_t)le64_to_cpu(fd->simple.flc);
148 }
149
150 /**
151  * dpaa2_fd_set_flc() - Set the flow context field of frame descriptor
152  * @fd: the given frame descriptor
153  * @flc_addr: the flow context needs to be set in frame descriptor
154  */
155 static inline void dpaa2_fd_set_flc(struct dpaa2_fd *fd,  dma_addr_t flc_addr)
156 {
157         fd->simple.flc = cpu_to_le64(flc_addr);
158 }
159
160 static inline bool dpaa2_fd_short_len(const struct dpaa2_fd *fd)
161 {
162         return !!((le16_to_cpu(fd->simple.format_offset) >>
163                   FD_SHORT_LEN_FLAG_SHIFT) & FD_SHORT_LEN_FLAG_MASK);
164 }
165
166 /**
167  * dpaa2_fd_get_len() - Get the length in the frame descriptor
168  * @fd: the given frame descriptor
169  *
170  * Return the length field in the frame descriptor.
171  */
172 static inline u32 dpaa2_fd_get_len(const struct dpaa2_fd *fd)
173 {
174         if (dpaa2_fd_short_len(fd))
175                 return le32_to_cpu(fd->simple.len) & FD_SHORT_LEN_MASK;
176
177         return le32_to_cpu(fd->simple.len);
178 }
179
180 /**
181  * dpaa2_fd_set_len() - Set the length field of frame descriptor
182  * @fd: the given frame descriptor
183  * @len: the length needs to be set in frame descriptor
184  */
185 static inline void dpaa2_fd_set_len(struct dpaa2_fd *fd, u32 len)
186 {
187         fd->simple.len = cpu_to_le32(len);
188 }
189
190 /**
191  * dpaa2_fd_get_offset() - Get the offset field in the frame descriptor
192  * @fd: the given frame descriptor
193  *
194  * Return the offset.
195  */
196 static inline uint16_t dpaa2_fd_get_offset(const struct dpaa2_fd *fd)
197 {
198         return le16_to_cpu(fd->simple.format_offset) & FD_OFFSET_MASK;
199 }
200
201 /**
202  * dpaa2_fd_set_offset() - Set the offset field of frame descriptor
203  * @fd: the given frame descriptor
204  * @offset: the offset needs to be set in frame descriptor
205  */
206 static inline void dpaa2_fd_set_offset(struct dpaa2_fd *fd, uint16_t offset)
207 {
208         fd->simple.format_offset &= cpu_to_le16(~FD_OFFSET_MASK);
209         fd->simple.format_offset |= cpu_to_le16(offset);
210 }
211
212 /**
213  * dpaa2_fd_get_format() - Get the format field in the frame descriptor
214  * @fd: the given frame descriptor
215  *
216  * Return the format.
217  */
218 static inline enum dpaa2_fd_format dpaa2_fd_get_format(
219                                                 const struct dpaa2_fd *fd)
220 {
221         return (enum dpaa2_fd_format)((le16_to_cpu(fd->simple.format_offset)
222                                       >> FD_FORMAT_SHIFT) & FD_FORMAT_MASK);
223 }
224
225 /**
226  * dpaa2_fd_set_format() - Set the format field of frame descriptor
227  * @fd: the given frame descriptor
228  * @format: the format needs to be set in frame descriptor
229  */
230 static inline void dpaa2_fd_set_format(struct dpaa2_fd *fd,
231                                        enum dpaa2_fd_format format)
232 {
233         fd->simple.format_offset &=
234                 cpu_to_le16(~(FD_FORMAT_MASK << FD_FORMAT_SHIFT));
235         fd->simple.format_offset |= cpu_to_le16(format << FD_FORMAT_SHIFT);
236 }
237
238 /**
239  * dpaa2_fd_get_bpid() - Get the bpid field in the frame descriptor
240  * @fd: the given frame descriptor
241  *
242  * Return the buffer pool id.
243  */
244 static inline uint16_t dpaa2_fd_get_bpid(const struct dpaa2_fd *fd)
245 {
246         return le16_to_cpu(fd->simple.bpid) & FD_BPID_MASK;
247 }
248
249 /**
250  * dpaa2_fd_set_bpid() - Set the bpid field of frame descriptor
251  * @fd: the given frame descriptor
252  * @bpid: buffer pool id to be set
253  */
254 static inline void dpaa2_fd_set_bpid(struct dpaa2_fd *fd, uint16_t bpid)
255 {
256         fd->simple.bpid &= cpu_to_le16(~(FD_BPID_MASK));
257         fd->simple.bpid |= cpu_to_le16(bpid);
258 }
259
260 /**
261  * struct dpaa2_sg_entry - the scatter-gathering structure
262  * @addr: address of the sg entry
263  * @len: length in this sg entry
264  * @bpid: buffer pool id
265  * @format_offset: format and offset fields
266  */
267 struct dpaa2_sg_entry {
268         __le64 addr;
269         __le32 len;
270         __le16 bpid;
271         __le16 format_offset;
272 };
273
274 enum dpaa2_sg_format {
275         dpaa2_sg_single = 0,
276         dpaa2_sg_frame_data,
277         dpaa2_sg_sgt_ext
278 };
279
280 /* Accessors for SG entry fields */
281
282 /**
283  * dpaa2_sg_get_addr() - Get the address from SG entry
284  * @sg: the given scatter-gathering object
285  *
286  * Return the address.
287  */
288 static inline dma_addr_t dpaa2_sg_get_addr(const struct dpaa2_sg_entry *sg)
289 {
290         return (dma_addr_t)le64_to_cpu(sg->addr);
291 }
292
293 /**
294  * dpaa2_sg_set_addr() - Set the address in SG entry
295  * @sg: the given scatter-gathering object
296  * @addr: the address to be set
297  */
298 static inline void dpaa2_sg_set_addr(struct dpaa2_sg_entry *sg, dma_addr_t addr)
299 {
300         sg->addr = cpu_to_le64(addr);
301 }
302
303 static inline bool dpaa2_sg_short_len(const struct dpaa2_sg_entry *sg)
304 {
305         return !!((le16_to_cpu(sg->format_offset) >> SG_SHORT_LEN_FLAG_SHIFT)
306                 & SG_SHORT_LEN_FLAG_MASK);
307 }
308
309 /**
310  * dpaa2_sg_get_len() - Get the length in SG entry
311  * @sg: the given scatter-gathering object
312  *
313  * Return the length.
314  */
315 static inline u32 dpaa2_sg_get_len(const struct dpaa2_sg_entry *sg)
316 {
317         if (dpaa2_sg_short_len(sg))
318                 return le32_to_cpu(sg->len) & SG_SHORT_LEN_MASK;
319
320         return le32_to_cpu(sg->len);
321 }
322
323 /**
324  * dpaa2_sg_set_len() - Set the length in SG entry
325  * @sg: the given scatter-gathering object
326  * @len: the length to be set
327  */
328 static inline void dpaa2_sg_set_len(struct dpaa2_sg_entry *sg, u32 len)
329 {
330         sg->len = cpu_to_le32(len);
331 }
332
333 /**
334  * dpaa2_sg_get_offset() - Get the offset in SG entry
335  * @sg: the given scatter-gathering object
336  *
337  * Return the offset.
338  */
339 static inline u16 dpaa2_sg_get_offset(const struct dpaa2_sg_entry *sg)
340 {
341         return le16_to_cpu(sg->format_offset) & SG_OFFSET_MASK;
342 }
343
344 /**
345  * dpaa2_sg_set_offset() - Set the offset in SG entry
346  * @sg: the given scatter-gathering object
347  * @offset: the offset to be set
348  */
349 static inline void dpaa2_sg_set_offset(struct dpaa2_sg_entry *sg,
350                                        u16 offset)
351 {
352         sg->format_offset &= cpu_to_le16(~SG_OFFSET_MASK);
353         sg->format_offset |= cpu_to_le16(offset);
354 }
355
356 /**
357  * dpaa2_sg_get_format() - Get the SG format in SG entry
358  * @sg: the given scatter-gathering object
359  *
360  * Return the format.
361  */
362 static inline enum dpaa2_sg_format
363         dpaa2_sg_get_format(const struct dpaa2_sg_entry *sg)
364 {
365         return (enum dpaa2_sg_format)((le16_to_cpu(sg->format_offset)
366                                        >> SG_FORMAT_SHIFT) & SG_FORMAT_MASK);
367 }
368
369 /**
370  * dpaa2_sg_set_format() - Set the SG format in SG entry
371  * @sg: the given scatter-gathering object
372  * @format: the format to be set
373  */
374 static inline void dpaa2_sg_set_format(struct dpaa2_sg_entry *sg,
375                                        enum dpaa2_sg_format format)
376 {
377         sg->format_offset &= cpu_to_le16(~(SG_FORMAT_MASK << SG_FORMAT_SHIFT));
378         sg->format_offset |= cpu_to_le16(format << SG_FORMAT_SHIFT);
379 }
380
381 /**
382  * dpaa2_sg_get_bpid() - Get the buffer pool id in SG entry
383  * @sg: the given scatter-gathering object
384  *
385  * Return the bpid.
386  */
387 static inline u16 dpaa2_sg_get_bpid(const struct dpaa2_sg_entry *sg)
388 {
389         return le16_to_cpu(sg->bpid) & SG_BPID_MASK;
390 }
391
392 /**
393  * dpaa2_sg_set_bpid() - Set the buffer pool id in SG entry
394  * @sg: the given scatter-gathering object
395  * @bpid: the bpid to be set
396  */
397 static inline void dpaa2_sg_set_bpid(struct dpaa2_sg_entry *sg, u16 bpid)
398 {
399         sg->bpid &= cpu_to_le16(~(SG_BPID_MASK));
400         sg->bpid |= cpu_to_le16(bpid);
401 }
402
403 /**
404  * dpaa2_sg_is_final() - Check final bit in SG entry
405  * @sg: the given scatter-gathering object
406  *
407  * Return bool.
408  */
409 static inline bool dpaa2_sg_is_final(const struct dpaa2_sg_entry *sg)
410 {
411         return !!(le16_to_cpu(sg->format_offset) >> SG_FINAL_FLAG_SHIFT);
412 }
413
414 /**
415  * dpaa2_sg_set_final() - Set the final bit in SG entry
416  * @sg: the given scatter-gathering object
417  * @final: the final boolean to be set
418  */
419 static inline void dpaa2_sg_set_final(struct dpaa2_sg_entry *sg, bool final)
420 {
421         sg->format_offset &= cpu_to_le16((~(SG_FINAL_FLAG_MASK
422                                          << SG_FINAL_FLAG_SHIFT)) & 0xFFFF);
423         sg->format_offset |= cpu_to_le16(final << SG_FINAL_FLAG_SHIFT);
424 }
425
426 #endif /* __FSL_DPAA2_FD_H */