in hwspinlock_core.c [91:168]
int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
{
int ret;
if (WARN_ON(!hwlock || (!flags && mode == HWLOCK_IRQSTATE)))
return -EINVAL;
/*
* This spin_lock{_irq, _irqsave} serves three purposes:
*
* 1. Disable preemption, in order to minimize the period of time
* in which the hwspinlock is taken. This is important in order
* to minimize the possible polling on the hardware interconnect
* by a remote user of this lock.
* 2. Make the hwspinlock SMP-safe (so we can take it from
* additional contexts on the local host).
* 3. Ensure that in_atomic/might_sleep checks catch potential
* problems with hwspinlock usage (e.g. scheduler checks like
* 'scheduling while atomic' etc.)
*/
switch (mode) {
case HWLOCK_IRQSTATE:
ret = spin_trylock_irqsave(&hwlock->lock, *flags);
break;
case HWLOCK_IRQ:
ret = spin_trylock_irq(&hwlock->lock);
break;
case HWLOCK_RAW:
case HWLOCK_IN_ATOMIC:
ret = 1;
break;
default:
ret = spin_trylock(&hwlock->lock);
break;
}
/* is lock already taken by another context on the local cpu ? */
if (!ret)
return -EBUSY;
/* try to take the hwspinlock device */
ret = hwlock->bank->ops->trylock(hwlock);
/* if hwlock is already taken, undo spin_trylock_* and exit */
if (!ret) {
switch (mode) {
case HWLOCK_IRQSTATE:
spin_unlock_irqrestore(&hwlock->lock, *flags);
break;
case HWLOCK_IRQ:
spin_unlock_irq(&hwlock->lock);
break;
case HWLOCK_RAW:
case HWLOCK_IN_ATOMIC:
/* Nothing to do */
break;
default:
spin_unlock(&hwlock->lock);
break;
}
return -EBUSY;
}
/*
* We can be sure the other core's memory operations
* are observable to us only _after_ we successfully take
* the hwspinlock, and we must make sure that subsequent memory
* operations (both reads and writes) will not be reordered before
* we actually took the hwspinlock.
*
* Note: the implicit memory barrier of the spinlock above is too
* early, so we need this additional explicit memory barrier.
*/
mb();
return 0;
}