Expose DEV_QUERY.PERF_INFO and the five performance counter session ioctls with documentation. Introduce a small scope-based wrapper around drm_dev_enter() and drm_dev_exit() to ensure proper cleanup.
Signed-off-by: Lukas Zapolskas <[email protected]> --- drivers/gpu/drm/panthor/panthor_drv.c | 128 ++++++++++++++++- include/uapi/drm/panthor_drm.h | 254 +++++++++++++++++++++++++++++++--- 2 files changed, 361 insertions(+), 21 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_drv.c b/drivers/gpu/drm/panthor/panthor_drv.c index 09cc3f699154..e6e708d9e18f 100644 --- a/drivers/gpu/drm/panthor/panthor_drv.c +++ b/drivers/gpu/drm/panthor/panthor_drv.c @@ -7,6 +7,7 @@ #include <asm/arch_timer.h> #endif +#include <linux/cleanup.h> #include <linux/list.h> #include <linux/module.h> #include <linux/of_platform.h> @@ -182,7 +183,8 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \ PANTHOR_UOBJ_DECL(struct drm_panthor_queue_create, ringbuf_size), \ PANTHOR_UOBJ_DECL(struct drm_panthor_vm_bind_op, syncs), \ - PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size)) + PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_perf_info, shader_blocks)) /** * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object. @@ -959,6 +961,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size = sizeof(ptdev->mmu_info); return 0; + case DRM_PANTHOR_DEV_QUERY_PERF_INFO: + args->size = sizeof(ptdev->perf_info); + return 0; + default: return -EINVAL; } @@ -992,6 +998,9 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d case DRM_PANTHOR_DEV_QUERY_MMU_INFO: return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->mmu_info); + case DRM_PANTHOR_DEV_QUERY_PERF_INFO: + return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->perf_info); + default: return -EINVAL; } @@ -1590,6 +1599,110 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data, return 0; } +static int panthor_drm_dev_enter(struct drm_device *ddev) +{ + int cookie; + + if (!drm_dev_enter(ddev, &cookie)) + return -ENODEV; + + return cookie; +} + +static void panthor_drm_dev_exit(int cookie) +{ + if (cookie >= 0) + drm_dev_exit(cookie); +} + +DEFINE_CLASS(panthor_drm_dev, int, panthor_drm_dev_exit(_T), + panthor_drm_dev_enter(ddev), struct drm_device *ddev) + +static int panthor_ioctl_perf_session_create(struct drm_device *ddev, void *data, + struct drm_file *file) +{ + int ret; + unsigned long sid = 0; + struct drm_panthor_perf_session_create *args = data; + + CLASS(panthor_drm_dev, cookie)(ddev); + if (cookie < 0) + return cookie; + + if (args->pad) + return -EINVAL; + + ret = panthor_perf_session_create(file, args, &sid); + if (ret) + return ret; + + return sid; +} + +static int panthor_ioctl_perf_session_start(struct drm_device *ddev, void *data, + struct drm_file *file) +{ + struct panthor_file *pfile = file->driver_priv; + struct drm_panthor_perf_session_start *args = data; + + CLASS(panthor_drm_dev, cookie)(ddev); + if (cookie < 0) + return cookie; + + if (args->pad0) + return -EINVAL; + + return panthor_perf_session_start(pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_sample(struct drm_device *ddev, void *data, + struct drm_file *file) +{ + struct panthor_file *pfile = file->driver_priv; + struct drm_panthor_perf_session_sample *args = data; + + CLASS(panthor_drm_dev, cookie)(ddev); + if (cookie < 0) + return cookie; + + if (args->pad0) + return -EINVAL; + + return panthor_perf_session_sample(pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_stop(struct drm_device *ddev, void *data, + struct drm_file *file) +{ + struct panthor_file *pfile = file->driver_priv; + struct drm_panthor_perf_session_stop *args = data; + + CLASS(panthor_drm_dev, cookie)(ddev); + if (cookie < 0) + return cookie; + + if (args->pad0) + return -EINVAL; + + return panthor_perf_session_stop(pfile, args->handle, args->user_data); +} + +static int panthor_ioctl_perf_session_destroy(struct drm_device *ddev, void *data, + struct drm_file *file) +{ + struct panthor_file *pfile = file->driver_priv; + struct drm_panthor_perf_session_destroy *args = data; + + CLASS(panthor_drm_dev, cookie)(ddev); + if (cookie < 0) + return cookie; + + if (args->pad0) + return -EINVAL; + + return panthor_perf_session_destroy(pfile, args->handle); +} + static int panthor_open(struct drm_device *ddev, struct drm_file *file) { @@ -1674,6 +1787,11 @@ static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = { PANTHOR_IOCTL(SET_USER_MMIO_OFFSET, set_user_mmio_offset, DRM_RENDER_ALLOW), PANTHOR_IOCTL(BO_SYNC, bo_sync, DRM_RENDER_ALLOW), PANTHOR_IOCTL(BO_QUERY_INFO, bo_query_info, DRM_RENDER_ALLOW), + PANTHOR_IOCTL(PERF_SESSION_CREATE, perf_session_create, DRM_RENDER_ALLOW), + PANTHOR_IOCTL(PERF_SESSION_START, perf_session_start, DRM_RENDER_ALLOW), + PANTHOR_IOCTL(PERF_SESSION_SAMPLE, perf_session_sample, DRM_RENDER_ALLOW), + PANTHOR_IOCTL(PERF_SESSION_STOP, perf_session_stop, DRM_RENDER_ALLOW), + PANTHOR_IOCTL(PERF_SESSION_DESTROY, perf_session_destroy, DRM_RENDER_ALLOW), }; static int panthor_mmap(struct file *filp, struct vm_area_struct *vma) @@ -1794,6 +1912,12 @@ static void panthor_debugfs_init(struct drm_minor *minor) * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags * - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query * - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag + * - 1.10 - add DRM_PANTHOR_DEV_QUERY_PERF_INFO query + * - adds PERF_SESSION_CREATE ioctl + * - adds PERF_SESSION_START ioctl + * - adds PERF_SESSION_SAMPLE ioctl + * - adds PERF_SESSION_STOP ioctl + * - adds PERF_SESSION_DESTROY ioctl */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ | @@ -1807,7 +1931,7 @@ static const struct drm_driver panthor_drm_driver = { .name = "panthor", .desc = "Panthor DRM driver", .major = 1, - .minor = 9, + .minor = 10, .gem_prime_import_sg_table = panthor_gem_prime_import_sg_table, .gem_prime_import = panthor_gem_prime_import, diff --git a/include/uapi/drm/panthor_drm.h b/include/uapi/drm/panthor_drm.h index dfa852aa3690..2a6018ff17df 100644 --- a/include/uapi/drm/panthor_drm.h +++ b/include/uapi/drm/panthor_drm.h @@ -154,6 +154,23 @@ enum drm_panthor_ioctl_id { * This is useful for imported BOs. */ DRM_PANTHOR_BO_QUERY_INFO, + + /** @DRM_PANTHOR_PERF_SESSION_CREATE: Create a performance counter session. */ + DRM_PANTHOR_PERF_SESSION_CREATE, + + /** @DRM_PANTHOR_PERF_SESSION_START: Start a performance counter session. */ + DRM_PANTHOR_PERF_SESSION_START, + + /** @DRM_PANTHOR_PERF_SESSION_SAMPLE: Request a manual sample on a performance counter + * session. + */ + DRM_PANTHOR_PERF_SESSION_SAMPLE, + + /** @DRM_PANTHOR_PERF_SESSION_STOP: Stop a performance counter session. */ + DRM_PANTHOR_PERF_SESSION_STOP, + + /** @DRM_PANTHOR_PERF_SESSION_DESTROY: Destroy a performance counter session. */ + DRM_PANTHOR_PERF_SESSION_DESTROY, }; /** @@ -256,6 +273,9 @@ enum drm_panthor_dev_query_type { /** @DRM_PANTHOR_DEV_QUERY_MMU_INFO: Query MMU information. */ DRM_PANTHOR_DEV_QUERY_MMU_INFO, + + /** @DRM_PANTHOR_DEV_QUERY_PERF_INFO: Query performance counter interface information. */ + DRM_PANTHOR_DEV_QUERY_PERF_INFO, }; /** @@ -1357,6 +1377,67 @@ struct drm_panthor_bo_query_info { __u32 pad; }; +/** + * DOC: Performance counters + * + * The PERF_SESSION ioctls enable sampling from device-wide counters. + * + * Using the interface + * ------------------- + * + * 1. Call DRM_IOCTL_PANTHOR_DEV_QUERY with DRM_PANTHOR_DEV_QUERY_PERF_INFO + * to obtain &struct drm_panthor_perf_info, including sample and header sizes. + * 2. Use DRM_IOCTL_PANTHOR_BO_CREATE with DRM_PANTHOR_BO_WB_MMAP to create two + * BOs: a ring buffer of at least ``sample_slots * sample_size`` bytes and a + * control buffer for &struct drm_panthor_perf_ringbuf_control. Both start at + * BO offset zero. Obtain their offsets with DRM_IOCTL_PANTHOR_BO_MMAP_OFFSET + * and mmap() them with MAP_SHARED. + * 3. Pass an eventfd, the BO handles, counter set, enable masks and + * slot count to DRM_IOCTL_PANTHOR_PERF_SESSION_CREATE. The ioctl returns a + * nonnegative session handle. The session starts stopped, with + * both control indices initialized to zero. + * 4. Call DRM_IOCTL_PANTHOR_PERF_SESSION_START. It waits for and discards a + * clearing sample to exclude counter data from before the start command. Then + * use DRM_IOCTL_PANTHOR_PERF_SESSION_SAMPLE to request samples asynchronously. + * Only one SAMPLE request may be pending. Wait on and read the eventfd, or + * poll insert_idx, then consume samples as below. + * 5. Once done sampling, call DRM_IOCTL_PANTHOR_PERF_SESSION_STOP. It waits for + * a final sample to be published. Once the final sample is published, and the + * session is stopped, either call DRM_IOCTL_PANTHOR_PERF_SESSION_DESTROY + * before releasing the BOs and eventfd, or restart sampling with + * DRM_IOCTL_PANTHOR_PERF_SESSION_START. + * + * Reading samples + * --------------- + * + * The indices count samples modulo 2^64. ``insert_idx - extract_idx`` gives the + * number of samples available to userspace, with sample i at byte offset + * ``(i % sample_slots) * sample_size`` in the ring buffer BO. For example:: + * + * extract_idx % sample_slots insert_idx % sample_slots + * | | + * v v + * +--------------+----------------------------+--------------+ + * | | published, unread samples | | + * +--------------+----------------------------+--------------+ + * + * See &struct drm_panthor_perf_ringbuf_control for the memory ordering requirements. + * + * Each sample has the following layout:: + * + * +--------+--------+------------------+--------+------------------+-----+ + * | Sample | Block | Block of | Block | Block of | ... | + * | header | header | 64-bit counters | header | 64-bit counters | | + * +--------+--------+------------------+--------+------------------+-----+ + * + * Each sample starts with a &struct drm_panthor_perf_sample_header containing + * timestamps, sample flags and the command's user data. + * + * Each block starts with a &struct drm_panthor_perf_block_header identifying its + * type and index, followed by counters_per_block 64-bit counters, as reported by + * &struct drm_panthor_perf_info. + */ + /** * struct drm_panthor_perf_block_header - Header present before every block in the * sample ringbuffer. @@ -1380,6 +1461,9 @@ enum drm_panthor_perf_sample_flags { /** * @DRM_PANTHOR_PERF_SAMPLE_OVERFLOW: This sample contains overflows due to the duration * of the sampling period. + * + * Counter collection has lost data. Further SAMPLE requests fail with -EACCES + * until the session is stopped and restarted. */ DRM_PANTHOR_PERF_SAMPLE_OVERFLOW = 1 << 0, }; @@ -1415,11 +1499,7 @@ struct drm_panthor_perf_sample_header { /** * enum drm_panthor_perf_counter_set - The counter set to be requested from the hardware. * - * The hardware supports a single performance counter set at a time, so requesting any set other - * than the primary may fail if another process is sampling at the same time. - * - * If in doubt, the primary counter set has the most commonly used counters and requires no - * additional permissions to open. + * If in doubt, the primary counter set has the most commonly used counters. */ enum drm_panthor_perf_counter_set { /** @@ -1437,52 +1517,79 @@ enum drm_panthor_perf_counter_set { }; /** - * struct drm_panthor_perf_ringbuf_control - Struct used to map in the ring buffer control indices - * into memory shared between user and kernel. + * struct drm_panthor_perf_ringbuf_control - Shared ring buffer control indices. * + * After CREATE initializes both indices, userspace owns extract_idx and the + * kernel owns insert_idx, with the expected memory barrier semantics outlined below:: + * + * Kernel Userspace + * + * Write sample counters and headers + * store_release(insert_idx) + * wmb() + * eventfd_signal() Wait/read eventfd + * load_acquire(insert_idx) + * Read/copy published samples + * load_acquire(extract_idx) store_release(extract_idx) + * Reuse consumed slots + * + * The kernel writes the sample, then store-releases the insert_idx to publish it. + * The following wmb() orders this publication before the eventfd notification. + * Userspace must load-acquire the insert_idx before reading samples to ensure + * the published sample writes are visible. + * + * To release slots, userspace store-releases the new extract_idx. The kernel load-acquires + * the extract_idx before reusing those slots. This prevents the kernel from overwriting data + * userspace is still reading. */ struct drm_panthor_perf_ringbuf_control { /** * @extract_idx: Number of samples consumed or discarded by userspace. Only * modifiable by userspace. + * + * Userspace updates this after it has consumed or to discard samples from the ring buffer. */ __u64 extract_idx; /** * @insert_idx: Number of samples emitted by the kernel. Only modifiable by * the kernel. + * + * Userspace must read this before reading samples from the ring buffer. */ __u64 insert_idx; }; /** * struct drm_panthor_perf_session_create - Arguments passed to DRM_PANTHOR_PERF_SESSION_CREATE. + * + * The ioctl may return: + * - Nonnegative session handle on success. + * - -EACCES if neither CAP_PERFMON nor CAP_SYS_ADMIN is held. + * - -EINVAL for an invalid set, or @sample_slots below two or not a power of two. + * - -EBUSY when the device-wide limit of sessions has been reached, or when there is already + * a conflicting block set configured. + * - -EOPNOTSUPP if performance counters are unavailable. + * - Other error codes are possible. */ struct drm_panthor_perf_session_create { /** * @block_set: Set of performance counter blocks, member of * enum drm_panthor_perf_counter_set. * - * This is a global configuration and only one set can be active at a time. If - * another client has already requested a counter set, any further requests - * for a different counter set will fail and return an -EBUSY. - * - * If the requested set does not exist, the request will fail and return an -EINVAL. - * - * Some sets have additional requirements to be enabled, and the setup request will - * fail with an -EACCES if these requirements are not satisfied. + * If the requested set is unavailable, the request will fail and return an -EINVAL. */ __u32 block_set; /** @fd: eventfd for signalling the availability of a new sample. */ __u32 fd; - /** @ringbuf_handle: Handle to the BO to write perf counter sample to. */ + /** @ringbuf_handle: Handle to the BO containing samples, starting at offset zero. */ __u32 ringbuf_handle; /** - * @control_handle: Handle to the BO containing a contiguous 16 byte range, used for the - * insert and extract indices for the ringbuffer. + * @control_handle: Handle to a separate BO containing + * &struct drm_panthor_perf_ringbuf_control at offset zero. */ __u32 control_handle; @@ -1490,7 +1597,7 @@ struct drm_panthor_perf_session_create { * @sample_slots: The number of slots available in the userspace-provided BO. Must be * a power of 2 and at least 2, to leave space for a sample and the final STOP sample. * - * If sample_slots * sample_size is smaller than the ringbuf BO, the setup will fail. + * If sample_slots * sample_size is larger than the ringbuf BO, the setup will fail. */ __u32 sample_slots; @@ -1533,6 +1640,105 @@ struct drm_panthor_perf_session_create { __u64 shader_enable_mask[2]; }; +/** + * struct drm_panthor_perf_session_start - Arguments passed to DRM_PANTHOR_PERF_SESSION_START. + * + * Starts a stopped session and waits for a clearing sample, which is discarded. + * An already active session is unchanged. Ring indices and unread samples are + * preserved. + * + * The ioctl may return: + * - 0 on success. + * - -EINVAL for an invalid @handle or nonzero padding. + * - -ENOSPC if fewer than two ring buffer slots are free. + * - -EBUSY if the session is stopping. + * - -ETIMEDOUT if starting has not completed. The transition is not cancelled + * and may complete later, so resuming the operation by calling the ioctl again is possible. + * - Other error codes are possible. + */ +struct drm_panthor_perf_session_start { + /** @handle: Performance counter session handle. */ + __u32 handle; + + /** @pad0: MBZ. */ + __u32 pad0; + + /** @user_data: User provided data for automatic samples. */ + __u64 user_data; +}; + +/** + * struct drm_panthor_perf_session_sample - Arguments passed to DRM_PANTHOR_PERF_SESSION_SAMPLE. + * + * Requests a sample from an active session. Sample availability is signaled via the + * eventfd passed during session setup. + * + * The ioctl may return: + * - 0 when the request is accepted. + * - -EINVAL for an invalid @handle, nonzero padding, or a session that is not active. + * - -EBUSY if a sample request is already pending. + * - -ENOSPC if fewer than two ring buffer slots are free. + * - -EACCES after overflow; stop and restart the session to resume sampling. + * - Other error codes are possible. + */ +struct drm_panthor_perf_session_sample { + /** @handle: Performance counter session handle. */ + __u32 handle; + + /** @pad0: MBZ. */ + __u32 pad0; + + /** @user_data: User provided data that will be attached to the sample.*/ + __u64 user_data; +}; + +/** + * struct drm_panthor_perf_session_stop - Arguments passed to DRM_PANTHOR_PERF_SESSION_STOP. + * + * Stops an active session and waits for a final sample to be published. A stop request + * on an already stopped session is ignored and produces no sample. + * + * The ioctl may return: + * - 0 on success. + * - -EINVAL for an invalid @handle or nonzero padding. + * - -EBUSY if the session is starting, a sample request is pending, or no ring + * buffer slot is free. + * - -ETIMEDOUT if stopping has not completed. The transition is not cancelled + * and may complete later. + * - Other error codes are possible. + */ +struct drm_panthor_perf_session_stop { + /** @handle: Performance counter session handle. */ + __u32 handle; + + /** @pad0: MBZ. */ + __u32 pad0; + + /** + * @user_data: User provided data attached to the final STOP sample. + */ + __u64 user_data; +}; + +/** + * struct drm_panthor_perf_session_destroy - Arguments passed to DRM_PANTHOR_PERF_SESSION_DESTROY. + * + * Destroys a stopped session. The handle is no longer valid after this call. + * + * The ioctl may return: + * - 0 on success. + * - -EINVAL for an invalid @handle, nonzero padding, or an active session. + * - -EBUSY if the session is starting or stopping. + * - Other error codes are possible. + */ +struct drm_panthor_perf_session_destroy { + /** @handle: Performance counter session handle. */ + __u32 handle; + + /** @pad0: MBZ. */ + __u32 pad0; +}; + /** * DRM_IOCTL_PANTHOR() - Build a Panthor IOCTL number * @__access: Access type. Must be R, W or RW. @@ -1583,6 +1789,16 @@ enum { DRM_IOCTL_PANTHOR(WR, BO_SYNC, bo_sync), DRM_IOCTL_PANTHOR_BO_QUERY_INFO = DRM_IOCTL_PANTHOR(WR, BO_QUERY_INFO, bo_query_info), + DRM_IOCTL_PANTHOR_PERF_SESSION_CREATE = + DRM_IOCTL_PANTHOR(WR, PERF_SESSION_CREATE, perf_session_create), + DRM_IOCTL_PANTHOR_PERF_SESSION_START = + DRM_IOCTL_PANTHOR(WR, PERF_SESSION_START, perf_session_start), + DRM_IOCTL_PANTHOR_PERF_SESSION_SAMPLE = + DRM_IOCTL_PANTHOR(WR, PERF_SESSION_SAMPLE, perf_session_sample), + DRM_IOCTL_PANTHOR_PERF_SESSION_STOP = + DRM_IOCTL_PANTHOR(WR, PERF_SESSION_STOP, perf_session_stop), + DRM_IOCTL_PANTHOR_PERF_SESSION_DESTROY = + DRM_IOCTL_PANTHOR(WR, PERF_SESSION_DESTROY, perf_session_destroy), }; #if defined(__cplusplus) -- 2.43.0
