Hardware Spinlock Framework¶
Introduction¶
Hardware spinlock modules provide hardware assistance for synchronizationand mutual exclusion between heterogeneous processors and those not operatingunder a single, shared operating system.
For example, OMAP4 has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP,each of which is running a different Operating System (the master, A9,is usually running Linux and the slave processors, the M3 and the DSP,are running some flavor of RTOS).
A generic hwspinlock framework allows platform-independent drivers to usethe hwspinlock device in order to access data structures that are sharedbetween remote processors, that otherwise have no alternative mechanismto accomplish synchronization and mutual exclusion operations.
This is necessary, for example, for Inter-processor communications:on OMAP4, cpu-intensive multimedia tasks are offloaded by the host to theremote M3 and/or C64x+ slave processors (by an IPC subsystem called Syslink).
To achieve fast message-based communications, a minimal kernel supportis needed to deliver messages arriving from a remote processor to theappropriate user process.
This communication is based on simple data structures that is shared betweenthe remote processors, and access to it is synchronized using the hwspinlockmodule (remote processor directly places new messages in this shared datastructure).
A common hwspinlock interface makes it possible to have generic, platform-independent, drivers.
User API¶
struct hwspinlock *hwspin_lock_request(void);
Dynamically assign an hwspinlock and return its address, or NULLin case an unused hwspinlock isn’t available. Users of thisAPI will usually want to communicate the lock’s id to the remote corebefore it can be used to achieve synchronization.
Should be called from a process context (might sleep).
struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
Assign a specific hwspinlock id and return its address, or NULLif that hwspinlock is already in use. Usually board code willbe calling this function in order to reserve specific hwspinlockids for predefined purposes.
Should be called from a process context (might sleep).
int of_hwspin_lock_get_id(struct device_node *np, int index);
Retrieve the global lock id for an OF phandle-based specific lock.This function provides a means for DT users of a hwspinlock moduleto get the global lock id of a specific hwspinlock, so that it canbe requested using the normal hwspin_lock_request_specific() API.
The function returns a lock id number on success, -EPROBE_DEFER ifthe hwspinlock device is not yet registered with the core, or othererror values.
Should be called from a process context (might sleep).
int hwspin_lock_free(struct hwspinlock *hwlock);
Free a previously-assigned hwspinlock; returns 0 on success, or anappropriate error code on failure (e.g. -EINVAL if the hwspinlockis already free).
Should be called from a process context (might sleep).
int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int timeout);
Lock a previously-assigned hwspinlock with a timeout limit (specified inmsecs). If the hwspinlock is already taken, the function will busy loopwaiting for it to be released, but give up when the timeout elapses.Upon a successful return from this function, preemption is disabled sothe caller must not sleep, and is advised to release the hwspinlock assoon as possible, in order to minimize remote cores polling on thehardware interconnect.
Returns 0 when successful and an appropriate error code otherwise (mostnotably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).The function will never sleep.
int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int timeout);
Lock a previously-assigned hwspinlock with a timeout limit (specified inmsecs). If the hwspinlock is already taken, the function will busy loopwaiting for it to be released, but give up when the timeout elapses.Upon a successful return from this function, preemption and the localinterrupts are disabled, so the caller must not sleep, and is advised torelease the hwspinlock as soon as possible.
Returns 0 when successful and an appropriate error code otherwise (mostnotably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).The function will never sleep.
int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock, unsigned int to, unsigned long *flags);
Lock a previously-assigned hwspinlock with a timeout limit (specified inmsecs). If the hwspinlock is already taken, the function will busy loopwaiting for it to be released, but give up when the timeout elapses.Upon a successful return from this function, preemption is disabled,local interrupts are disabled and their previous state is saved at thegiven flags placeholder. The caller must not sleep, and is advised torelease the hwspinlock as soon as possible.
Returns 0 when successful and an appropriate error code otherwise (mostnotably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
The function will never sleep.
int hwspin_lock_timeout_raw(struct hwspinlock *hwlock, unsigned int timeout);
Lock a previously-assigned hwspinlock with a timeout limit (specified inmsecs). If the hwspinlock is already taken, the function will busy loopwaiting for it to be released, but give up when the timeout elapses.
Caution: User must protect the routine of getting hardware lock with mutexor spinlock to avoid dead-lock, that will let user can do some time-consumingor sleepable operations under the hardware lock.
Returns 0 when successful and an appropriate error code otherwise (mostnotably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
The function will never sleep.
int hwspin_lock_timeout_in_atomic(struct hwspinlock *hwlock, unsigned int to);
Lock a previously-assigned hwspinlock with a timeout limit (specified inmsecs). If the hwspinlock is already taken, the function will busy loopwaiting for it to be released, but give up when the timeout elapses.
This function shall be called only from an atomic context and the timeoutvalue shall not exceed a few msecs.
Returns 0 when successful and an appropriate error code otherwise (mostnotably -ETIMEDOUT if the hwspinlock is still busy after timeout msecs).
The function will never sleep.
int hwspin_trylock(struct hwspinlock *hwlock);
Attempt to lock a previously-assigned hwspinlock, but immediately fail ifit is already taken.
Upon a successful return from this function, preemption is disabled socaller must not sleep, and is advised to release the hwspinlock as soon aspossible, in order to minimize remote cores polling on the hardwareinterconnect.
Returns 0 on success and an appropriate error code otherwise (mostnotably -EBUSY if the hwspinlock was already taken).The function will never sleep.
int hwspin_trylock_irq(struct hwspinlock *hwlock);
Attempt to lock a previously-assigned hwspinlock, but immediately fail ifit is already taken.
Upon a successful return from this function, preemption and the localinterrupts are disabled so caller must not sleep, and is advised torelease the hwspinlock as soon as possible.
Returns 0 on success and an appropriate error code otherwise (mostnotably -EBUSY if the hwspinlock was already taken).
The function will never sleep.
int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags);
Attempt to lock a previously-assigned hwspinlock, but immediately fail ifit is already taken.
Upon a successful return from this function, preemption is disabled,the local interrupts are disabled and their previous state is savedat the given flags placeholder. The caller must not sleep, and is advisedto release the hwspinlock as soon as possible.
Returns 0 on success and an appropriate error code otherwise (mostnotably -EBUSY if the hwspinlock was already taken).The function will never sleep.
int hwspin_trylock_raw(struct hwspinlock *hwlock);
Attempt to lock a previously-assigned hwspinlock, but immediately fail ifit is already taken.
Caution: User must protect the routine of getting hardware lock with mutexor spinlock to avoid dead-lock, that will let user can do some time-consumingor sleepable operations under the hardware lock.
Returns 0 on success and an appropriate error code otherwise (mostnotably -EBUSY if the hwspinlock was already taken).The function will never sleep.
int hwspin_trylock_in_atomic(struct hwspinlock *hwlock);
Attempt to lock a previously-assigned hwspinlock, but immediately fail ifit is already taken.
This function shall be called only from an atomic context.
Returns 0 on success and an appropriate error code otherwise (mostnotably -EBUSY if the hwspinlock was already taken).The function will never sleep.
void hwspin_unlock(struct hwspinlock *hwlock);
Unlock a previously-locked hwspinlock. Always succeed, and can be calledfrom any context (the function never sleeps).
Note
code shouldnever unlock an hwspinlock which is already unlocked(there is no protection against this).
void hwspin_unlock_irq(struct hwspinlock *hwlock);
Unlock a previously-locked hwspinlock and enable local interrupts.The caller shouldnever unlock an hwspinlock which is already unlocked.
Doing so is considered a bug (there is no protection against this).Upon a successful return from this function, preemption and localinterrupts are enabled. This function will never sleep.
voidhwspin_unlock_irqrestore(struct hwspinlock *hwlock, unsigned long *flags);
Unlock a previously-locked hwspinlock.
The caller shouldnever unlock an hwspinlock which is already unlocked.Doing so is considered a bug (there is no protection against this).Upon a successful return from this function, preemption is reenabled,and the state of the local interrupts is restored to the state saved atthe given flags. This function will never sleep.
void hwspin_unlock_raw(struct hwspinlock *hwlock);
Unlock a previously-locked hwspinlock.
The caller shouldnever unlock an hwspinlock which is already unlocked.Doing so is considered a bug (there is no protection against this).This function will never sleep.
void hwspin_unlock_in_atomic(struct hwspinlock *hwlock);
Unlock a previously-locked hwspinlock.
The caller shouldnever unlock an hwspinlock which is already unlocked.Doing so is considered a bug (there is no protection against this).This function will never sleep.
int hwspin_lock_get_id(struct hwspinlock *hwlock);
Retrieve id number of a given hwspinlock. This is needed when anhwspinlock is dynamically assigned: before it can be used to achievemutual exclusion with a remote cpu, the id number should be communicatedto the remote task with which we want to synchronize.
Returns the hwspinlock id number, or -EINVAL if hwlock is null.
Typical usage¶
#include <linux/hwspinlock.h>#include <linux/err.h>int hwspinlock_example1(void){ struct hwspinlock *hwlock; int ret; /* dynamically assign a hwspinlock */ hwlock = hwspin_lock_request(); if (!hwlock) ... id = hwspin_lock_get_id(hwlock); /* probably need to communicate id to a remote processor now */ /* take the lock, spin for 1 sec if it's already taken */ ret = hwspin_lock_timeout(hwlock, 1000); if (ret) ... /* * we took the lock, do our thing now, but do NOT sleep */ /* release the lock */ hwspin_unlock(hwlock); /* free the lock */ ret = hwspin_lock_free(hwlock); if (ret) ... return ret;}int hwspinlock_example2(void){ struct hwspinlock *hwlock; int ret; /* * assign a specific hwspinlock id - this should be called early * by board init code. */ hwlock = hwspin_lock_request_specific(PREDEFINED_LOCK_ID); if (!hwlock) ... /* try to take it, but don't spin on it */ ret = hwspin_trylock(hwlock); if (!ret) { pr_info("lock is already taken\n"); return -EBUSY; } /* * we took the lock, do our thing now, but do NOT sleep */ /* release the lock */ hwspin_unlock(hwlock); /* free the lock */ ret = hwspin_lock_free(hwlock); if (ret) ... return ret;}API for implementors¶
int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev, const struct hwspinlock_ops *ops, int base_id, int num_locks);
To be called from the underlying platform-specific implementation, inorder to register a new hwspinlock device (which is usually a bank ofnumerous locks). Should be called from a process context (this functionmight sleep).
Returns 0 on success, or appropriate error code on failure.
int hwspin_lock_unregister(struct hwspinlock_device *bank);
To be called from the underlying vendor-specific implementation, in orderto unregister an hwspinlock device (which is usually a bank of numerouslocks).
Should be called from a process context (this function might sleep).
Returns the address of hwspinlock on success, or NULL on error (e.g.if the hwspinlock is still in use).
Important structs¶
struct hwspinlock_device is a device which usually contains a bankof hardware locks. It is registered by the underlying hwspinlockimplementation using the hwspin_lock_register() API.
/*** struct hwspinlock_device - a device which usually spans numerous hwspinlocks* @dev: underlying device, will be used to invoke runtime PM api* @ops: platform-specific hwspinlock handlers* @base_id: id index of the first lock in this device* @num_locks: number of locks in this device* @lock: dynamically allocated array of 'struct hwspinlock'*/struct hwspinlock_device { struct device *dev; const struct hwspinlock_ops *ops; int base_id; int num_locks; struct hwspinlock lock[0];};struct hwspinlock_device contains an array of hwspinlock structs, eachof which represents a single hardware lock:
/*** struct hwspinlock - this struct represents a single hwspinlock instance* @bank: the hwspinlock_device structure which owns this lock* @lock: initialized and used by hwspinlock core* @priv: private data, owned by the underlying platform-specific hwspinlock drv*/struct hwspinlock { struct hwspinlock_device *bank; spinlock_t lock; void *priv;};When registering a bank of locks, the hwspinlock driver only needs toset the priv members of the locks. The rest of the members are set andinitialized by the hwspinlock core itself.
Implementation callbacks¶
There are three possible callbacks defined in ‘struct hwspinlock_ops’:
struct hwspinlock_ops { int (*trylock)(struct hwspinlock *lock); void (*unlock)(struct hwspinlock *lock); void (*relax)(struct hwspinlock *lock);};The first two callbacks are mandatory:
The ->trylock() callback should make a single attempt to take the lock, andreturn 0 on failure and 1 on success. This callback maynot sleep.
The ->unlock() callback releases the lock. It always succeed, and it, too,maynot sleep.
The ->relax() callback is optional. It is called by hwspinlock core whilespinning on a lock, and can be used by the underlying implementation to forcea delay between two successive invocations of ->trylock(). It maynot sleep.