Files
nvdriver/kernel-open/nvidia/nano_timer_adapter.c

131 lines
5.1 KiB
C

/* SPDX-License-Identifier: MIT */
/* Linux primitives only. Callback data and GPU/module lifetime belong to Rust. */
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include "nano_timer_adapter.h"
struct nv_rs_nano_timer {
struct hrtimer timer;
struct timer_list jiffy_timer;
bool jiffy;
struct mutex stop_lock;
spinlock_t state_lock;
nv_rs_nano_callback callback;
void *payload;
bool stopping,closed;
};
static inline int nv_rs_timer_delete_sync(struct timer_list *t)
{
#if defined(NV_RS_HAVE_TIMER_DELETE_SYNC) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
return timer_delete_sync(t);
#else
return del_timer_sync(t);
#endif
}
static bool process_context(void)
{ return !in_interrupt() && !irqs_disabled() && !in_atomic(); }
void *nv_rs_nano_payload_alloc(size_t bytes)
{
if (!process_context() || !bytes) return NULL;
return kzalloc(bytes,GFP_KERNEL);
}
void nv_rs_nano_payload_free(void *payload) { kfree(payload); }
static enum hrtimer_restart fire(struct hrtimer *timer)
{
struct nv_rs_nano_timer *owner=container_of(timer,struct nv_rs_nano_timer,timer);
/* No state lock is held through client code. cancel/destroy joins this
* callback before payload ownership can be returned to Rust. */
owner->callback(owner->payload);
return HRTIMER_NORESTART;
}
static void fire_jiffy(struct timer_list *timer)
{
/* Match stock 580.178.04 common/inc/nv-timer.h: container_of works on all kernel versions. */
struct nv_rs_nano_timer *owner=container_of(timer,struct nv_rs_nano_timer,jiffy_timer);
owner->callback(owner->payload);
}
static int create(nv_rs_nano_callback callback,void *payload,
struct nv_rs_nano_timer **out,bool jiffy)
{
struct nv_rs_nano_timer *owner;
if (!out) return -EINVAL;
*out=NULL;
if (!callback || !payload) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
owner=kzalloc(sizeof(*owner),GFP_KERNEL);
if (!owner) return -ENOMEM;
mutex_init(&owner->stop_lock);
spin_lock_init(&owner->state_lock);
owner->callback=callback;owner->payload=payload;
owner->jiffy=jiffy;
if(jiffy)timer_setup(&owner->jiffy_timer,fire_jiffy,0);
#if defined(NV_RS_HAVE_HRTIMER_SETUP) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
else hrtimer_setup(&owner->timer,fire,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
#else
else {hrtimer_init(&owner->timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);owner->timer.function=fire;}
#endif
*out=owner;return 0;
}
int nv_rs_nano_create(nv_rs_nano_callback callback,void *payload,struct nv_rs_nano_timer **out)
{return create(callback,payload,out,false);}
int nv_rs_nano_create_jiffy(nv_rs_nano_callback callback,void *payload,struct nv_rs_nano_timer **out)
{return create(callback,payload,out,true);}
int nv_rs_nano_start(struct nv_rs_nano_timer *owner,u64 ns)
{
unsigned long flags;
if (!owner) return -EINVAL;
spin_lock_irqsave(&owner->state_lock,flags);
if (owner->closed || owner->stopping) {
spin_unlock_irqrestore(&owner->state_lock,flags);return -EBUSY;
}
/* Closed580 selects jiffies: truncate quotient to u32 BEFORE converting.
* The separate hrtimer primitive clamps u64 ns to KTIME_MAX so values
* with bit 63 set cannot wrap into a negative ktime_t. */
if(owner->jiffy)mod_timer(&owner->jiffy_timer,jiffies+usecs_to_jiffies((u32)(ns/1000)));
else hrtimer_start(&owner->timer,ns_to_ktime(min_t(u64,ns,(u64)KTIME_MAX)),HRTIMER_MODE_REL);
spin_unlock_irqrestore(&owner->state_lock,flags);return 0;
}
int nv_rs_nano_cancel(struct nv_rs_nano_timer *owner,u32 *was_active)
{
unsigned long flags;
int active;
if (!owner || !was_active) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
mutex_lock(&owner->stop_lock);
spin_lock_irqsave(&owner->state_lock,flags);
if (owner->closed) {
spin_unlock_irqrestore(&owner->state_lock,flags);
mutex_unlock(&owner->stop_lock);return -EINVAL;
}
owner->stopping=true;
spin_unlock_irqrestore(&owner->state_lock,flags);
active=owner->jiffy?nv_rs_timer_delete_sync(&owner->jiffy_timer):hrtimer_cancel(&owner->timer);
spin_lock_irqsave(&owner->state_lock,flags);
owner->stopping=false;
spin_unlock_irqrestore(&owner->state_lock,flags);
mutex_unlock(&owner->stop_lock);
*was_active=!!active;return 0;
}
int nv_rs_nano_destroy(struct nv_rs_nano_timer *owner)
{
unsigned long flags;
if (!owner) return -EINVAL;
if (!process_context()) return -EWOULDBLOCK;
/* Owning Rust requires exclusive access and prevents further API calls
* after this point. Serialized cancel/destroy must not run from fire(). */
mutex_lock(&owner->stop_lock);
spin_lock_irqsave(&owner->state_lock,flags);
owner->closed=true;owner->stopping=true;
spin_unlock_irqrestore(&owner->state_lock,flags);
if(owner->jiffy)nv_rs_timer_delete_sync(&owner->jiffy_timer);else hrtimer_cancel(&owner->timer);
mutex_unlock(&owner->stop_lock);
kfree(owner);return 0;
}