Skip to content

Commit 7f5933f

Browse files
davejiangsuryasaimadhu
authored andcommitted
x86/asm: Add an enqcmds() wrapper for the ENQCMDS instruction
Currently, the MOVDIR64B instruction is used to atomically submit 64-byte work descriptors to devices. Although it can encounter errors like device queue full, command not accepted, device not ready, etc when writing to a device MMIO, MOVDIR64B can not report back on errors from the device itself. This means that MOVDIR64B users need to separately interact with a device to see if a descriptor was successfully queued, which slows down device interactions. ENQCMD and ENQCMDS also atomically submit 64-byte work descriptors to devices. But, they *can* report back errors directly from the device, such as if the device was busy, or device not enabled or does not support the command. This immediate feedback from the submission instruction itself reduces the number of interactions with the device and can greatly increase efficiency. ENQCMD can be used at any privilege level, but can effectively only submit work on behalf of the current process. ENQCMDS is a ring0-only instruction and can explicitly specify a process context instead of being tied to the current process or needing to reprogram the IA32_PASID MSR. Use ENQCMDS for work submission within the kernel because a Process Address ID (PASID) is setup to translate the kernel virtual address space. This PASID is provided to ENQCMDS from the descriptor structure submitted to the device and not retrieved from IA32_PASID MSR, which is setup for the current user address space. See Intel Software Developer’s Manual for more information on the instructions. [ bp: - Make operand constraints like movdir64b() because both insns are basically doing the same thing, more or less. - Fixup comments and cleanup. ] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Dave Jiang <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Tony Luck <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0888e10 commit 7f5933f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

arch/x86/include/asm/special_insns.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,48 @@ static inline void movdir64b(void *dst, const void *src)
256256
: "m" (*__src), "a" (__dst), "d" (__src));
257257
}
258258

259+
/**
260+
* enqcmds - Enqueue a command in supervisor (CPL0) mode
261+
* @dst: destination, in MMIO space (must be 512-bit aligned)
262+
* @src: 512 bits memory operand
263+
*
264+
* The ENQCMDS instruction allows software to write a 512-bit command to
265+
* a 512-bit-aligned special MMIO region that supports the instruction.
266+
* A return status is loaded into the ZF flag in the RFLAGS register.
267+
* ZF = 0 equates to success, and ZF = 1 indicates retry or error.
268+
*
269+
* This function issues the ENQCMDS instruction to submit data from
270+
* kernel space to MMIO space, in a unit of 512 bits. Order of data access
271+
* is not guaranteed, nor is a memory barrier performed afterwards. It
272+
* returns 0 on success and -EAGAIN on failure.
273+
*
274+
* Warning: Do not use this helper unless your driver has checked that the
275+
* ENQCMDS instruction is supported on the platform and the device accepts
276+
* ENQCMDS.
277+
*/
278+
static inline int enqcmds(void __iomem *dst, const void *src)
279+
{
280+
const struct { char _[64]; } *__src = src;
281+
struct { char _[64]; } *__dst = dst;
282+
int zf;
283+
284+
/*
285+
* ENQCMDS %(rdx), rax
286+
*
287+
* See movdir64b()'s comment on operand specification.
288+
*/
289+
asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
290+
CC_SET(z)
291+
: CC_OUT(z) (zf), "+m" (*__dst)
292+
: "m" (*__src), "a" (__dst), "d" (__src));
293+
294+
/* Submission failure is indicated via EFLAGS.ZF=1 */
295+
if (zf)
296+
return -EAGAIN;
297+
298+
return 0;
299+
}
300+
259301
#endif /* __KERNEL__ */
260302

261303
#endif /* _ASM_X86_SPECIAL_INSNS_H */

0 commit comments

Comments
 (0)