Path: blob/master/drivers/accel/habanalabs/common/hldio.h
29282 views
/* SPDX-License-Identifier: GPL-2.0-only */1/*2* hldio.h - NVMe Direct I/O (HLDIO) infrastructure for Habana Labs Driver3*4* This feature requires specific hardware setup and must not be built5* under COMPILE_TEST.6*/78#ifndef __HL_HLDIO_H__9#define __HL_HLDIO_H__1011#include <linux/types.h>12#include <linux/fs.h>13#include <linux/seq_file.h>14#include <linux/ktime.h> /* ktime functions */15#include <linux/delay.h> /* usleep_range */16#include <linux/kernel.h> /* might_sleep_if */17#include <linux/errno.h> /* error codes */1819/* Forward declarations */20struct hl_device;21struct file;2223/* Enable only if Kconfig selected */24#ifdef CONFIG_HL_HLDIO25/**26* struct hl_p2p_region - describes a single P2P memory region27* @p2ppages: array of page structs for the P2P memory28* @p2pmem: virtual address of the P2P memory region29* @device_pa: physical address on the device30* @bar_offset: offset within the BAR31* @size: size of the region in bytes32* @bar: BAR number containing this region33*/34struct hl_p2p_region {35struct page **p2ppages;36void *p2pmem;37u64 device_pa;38u64 bar_offset;39u64 size;40int bar;41};4243/**44* struct hl_dio_stats - Direct I/O statistics45* @total_ops: total number of operations attempted46* @successful_ops: number of successful operations47* @failed_ops: number of failed operations48* @bytes_transferred: total bytes successfully transferred49* @last_len_read: length of the last read operation50*/51struct hl_dio_stats {52u64 total_ops;53u64 successful_ops;54u64 failed_ops;55u64 bytes_transferred;56size_t last_len_read;57};5859/**60* struct hl_dio - describes habanalabs direct storage interaction interface61* @p2prs: array of p2p regions62* @inflight_ios: percpu counter for inflight ios63* @np2prs: number of elements in p2prs64* @io_enabled: 1 if io is enabled 0 otherwise65*/66struct hl_dio {67struct hl_p2p_region *p2prs;68s64 __percpu *inflight_ios;69u8 np2prs;70u8 io_enabled;71};7273int hl_dio_ssd2hl(struct hl_device *hdev, struct hl_ctx *ctx, int fd,74u64 device_va, off_t off_bytes, size_t len_bytes,75size_t *len_read);76void hl_p2p_region_fini_all(struct hl_device *hdev);77int hl_p2p_region_init(struct hl_device *hdev, struct hl_p2p_region *p2pr);78int hl_dio_start(struct hl_device *hdev);79void hl_dio_stop(struct hl_device *hdev);8081/* Init/teardown */82int hl_hldio_init(struct hl_device *hdev);83void hl_hldio_fini(struct hl_device *hdev);8485/* File operations */86long hl_hldio_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);8788/* DebugFS hooks */89#ifdef CONFIG_DEBUG_FS90void hl_hldio_debugfs_init(struct hl_device *hdev);91void hl_hldio_debugfs_fini(struct hl_device *hdev);92#else93static inline void hl_hldio_debugfs_init(struct hl_device *hdev) { }94static inline void hl_hldio_debugfs_fini(struct hl_device *hdev) { }95#endif9697#else /* !CONFIG_HL_HLDIO */9899struct hl_p2p_region;100/* Stubs when HLDIO is disabled */101static inline int hl_dio_ssd2hl(struct hl_device *hdev, struct hl_ctx *ctx, int fd,102u64 device_va, off_t off_bytes, size_t len_bytes,103size_t *len_read)104{ return -EOPNOTSUPP; }105static inline void hl_p2p_region_fini_all(struct hl_device *hdev) {}106static inline int hl_p2p_region_init(struct hl_device *hdev, struct hl_p2p_region *p2pr)107{ return -EOPNOTSUPP; }108static inline int hl_dio_start(struct hl_device *hdev) { return -EOPNOTSUPP; }109static inline void hl_dio_stop(struct hl_device *hdev) {}110111static inline int hl_hldio_init(struct hl_device *hdev) { return 0; }112static inline void hl_hldio_fini(struct hl_device *hdev) { }113static inline long hl_hldio_ioctl(struct file *f, unsigned int c,114unsigned long a)115{ return -ENOTTY; }116static inline void hl_hldio_debugfs_init(struct hl_device *hdev) { }117static inline void hl_hldio_debugfs_fini(struct hl_device *hdev) { }118119#endif /* CONFIG_HL_HLDIO */120121/* Simplified polling macro for HLDIO (no simulator support) */122#define hl_poll_timeout_condition(hdev, cond, sleep_us, timeout_us) \123({ \124ktime_t __timeout = ktime_add_us(ktime_get(), timeout_us); \125might_sleep_if(sleep_us); \126(void)(hdev); /* keep signature consistent, hdev unused */ \127for (;;) { \128mb(); /* ensure ordering of memory operations */ \129if (cond) \130break; \131if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) \132break; \133if (sleep_us) \134usleep_range((sleep_us >> 2) + 1, sleep_us); \135} \136(cond) ? 0 : -ETIMEDOUT; \137})138139#ifdef CONFIG_HL_HLDIO140bool hl_device_supports_nvme(struct hl_device *hdev);141#else142static inline bool hl_device_supports_nvme(struct hl_device *hdev) { return false; }143#endif144145#endif /* __HL_HLDIO_H__ */146147148