Skip to content

Commit 300bab9

Browse files
committed
hwspinlock/core: register a bank of hwspinlocks in a single API call
Hardware Spinlock devices usually contain numerous locks (known devices today support between 32 to 256 locks). Originally hwspinlock core required drivers to register (and later, when needed, unregister) each lock separately. That worked, but required hwspinlocks drivers to do a bit extra work when they were probed/removed. This patch changes hwspin_lock_{un}register() to allow a bank of hwspinlocks to be {un}registered in a single invocation. A new 'struct hwspinlock_device', which contains an array of 'struct hwspinlock's is now being passed to the core upon registration (so instead of wrapping each struct hwspinlock, a priv member has been added to allow drivers to piggyback their private data with each hwspinlock). While at it, several per-lock members were moved to be per-device: 1. struct device *dev 2. struct hwspinlock_ops *ops In addition, now that the array of locks is handled by the core, there's no reason to maintain a per-lock 'int id' member: the id of the lock anyway equals to its index in the bank's array plus the bank's base_id. Remove this per-lock id member too, and instead use a simple pointers arithmetic to derive it. As a result of this change, hwspinlocks drivers are now simpler and smaller (about %20 code reduction) and the memory footprint of the hwspinlock framework is reduced. Signed-off-by: Ohad Ben-Cohen <ohad@wizery.com>
1 parent c536abf commit 300bab9

5 files changed

Lines changed: 211 additions & 144 deletions

File tree

‎Documentation/hwspinlock.txt‎

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,42 +227,62 @@ int hwspinlock_example2(void)
227227

228228
4. API for implementors
229229

230-
int hwspin_lock_register(struct hwspinlock *hwlock);
230+
int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
231+
const struct hwspinlock_ops *ops, int base_id, int num_locks);
231232
- to be called from the underlying platform-specific implementation, in
232-
order to register a new hwspinlock instance. Should be called from
233-
a process context (this function might sleep).
233+
order to register a new hwspinlock device (which is usually a bank of
234+
numerous locks). Should be called from a process context (this function
235+
might sleep).
234236
Returns 0 on success, or appropriate error code on failure.
235237

236-
struct hwspinlock *hwspin_lock_unregister(unsigned int id);
238+
int hwspin_lock_unregister(struct hwspinlock_device *bank);
237239
- to be called from the underlying vendor-specific implementation, in order
238-
to unregister an existing (and unused) hwspinlock instance.
240+
to unregister an hwspinlock device (which is usually a bank of numerous
241+
locks).
239242
Should be called from a process context (this function might sleep).
240243
Returns the address of hwspinlock on success, or NULL on error (e.g.
241244
if the hwspinlock is sill in use).
242245

243-
5. struct hwspinlock
246+
5. Important structs
244247

245-
This struct represents an hwspinlock instance. It is registered by the
246-
underlying hwspinlock implementation using the hwspin_lock_register() API.
248+
struct hwspinlock_device is a device which usually contains a bank
249+
of hardware locks. It is registered by the underlying hwspinlock
250+
implementation using the hwspin_lock_register() API.
247251

248252
/**
249-
* struct hwspinlock - vendor-specific hwspinlock implementation
250-
*
251-
* @dev: underlying device, will be used with runtime PM api
252-
* @ops: vendor-specific hwspinlock handlers
253-
* @id: a global, unique, system-wide, index of the lock.
254-
* @lock: initialized and used by hwspinlock core
253+
* struct hwspinlock_device - a device which usually spans numerous hwspinlocks
254+
* @dev: underlying device, will be used to invoke runtime PM api
255+
* @ops: platform-specific hwspinlock handlers
256+
* @base_id: id index of the first lock in this device
257+
* @num_locks: number of locks in this device
258+
* @lock: dynamically allocated array of 'struct hwspinlock'
255259
*/
256-
struct hwspinlock {
260+
struct hwspinlock_device {
257261
struct device *dev;
258262
const struct hwspinlock_ops *ops;
259-
int id;
263+
int base_id;
264+
int num_locks;
265+
struct hwspinlock lock[0];
266+
};
267+
268+
struct hwspinlock_device contains an array of hwspinlock structs, each
269+
of which represents a single hardware lock:
270+
271+
/**
272+
* struct hwspinlock - this struct represents a single hwspinlock instance
273+
* @bank: the hwspinlock_device structure which owns this lock
274+
* @lock: initialized and used by hwspinlock core
275+
* @priv: private data, owned by the underlying platform-specific hwspinlock drv
276+
*/
277+
struct hwspinlock {
278+
struct hwspinlock_device *bank;
260279
spinlock_t lock;
280+
void *priv;
261281
};
262282

263-
The underlying implementation is responsible to assign the dev, ops and id
264-
members. The lock member, OTOH, is initialized and used by the hwspinlock
265-
core.
283+
When registering a bank of locks, the hwspinlock driver only needs to
284+
set the priv members of the locks. The rest of the members are set and
285+
initialized by the hwspinlock core itself.
266286

267287
6. Implementation callbacks
268288

‎drivers/hwspinlock/hwspinlock_core.c‎

Lines changed: 109 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
117117
return -EBUSY;
118118

119119
/* try to take the hwspinlock device */
120-
ret = hwlock->ops->trylock(hwlock);
120+
ret = hwlock->bank->ops->trylock(hwlock);
121121

122122
/* if hwlock is already taken, undo spin_trylock_* and exit */
123123
if (!ret) {
@@ -199,8 +199,8 @@ int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to,
199199
* Allow platform-specific relax handlers to prevent
200200
* hogging the interconnect (no sleeping, though)
201201
*/
202-
if (hwlock->ops->relax)
203-
hwlock->ops->relax(hwlock);
202+
if (hwlock->bank->ops->relax)
203+
hwlock->bank->ops->relax(hwlock);
204204
}
205205

206206
return ret;
@@ -245,7 +245,7 @@ void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
245245
*/
246246
mb();
247247

248-
hwlock->ops->unlock(hwlock);
248+
hwlock->bank->ops->unlock(hwlock);
249249

250250
/* Undo the spin_trylock{_irq, _irqsave} called while locking */
251251
if (mode == HWLOCK_IRQSTATE)
@@ -257,63 +257,32 @@ void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
257257
}
258258
EXPORT_SYMBOL_GPL(__hwspin_unlock);
259259

260-
/**
261-
* hwspin_lock_register() - register a new hw spinlock
262-
* @hwlock: hwspinlock to register.
263-
*
264-
* This function should be called from the underlying platform-specific
265-
* implementation, to register a new hwspinlock instance.
266-
*
267-
* Should be called from a process context (might sleep)
268-
*
269-
* Returns 0 on success, or an appropriate error code on failure
270-
*/
271-
int hwspin_lock_register(struct hwspinlock *hwlock)
260+
static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
272261
{
273262
struct hwspinlock *tmp;
274263
int ret;
275264

276-
if (!hwlock || !hwlock->ops ||
277-
!hwlock->ops->trylock || !hwlock->ops->unlock) {
278-
pr_err("invalid parameters\n");
279-
return -EINVAL;
280-
}
281-
282-
spin_lock_init(&hwlock->lock);
283-
284265
mutex_lock(&hwspinlock_tree_lock);
285266

286-
ret = radix_tree_insert(&hwspinlock_tree, hwlock->id, hwlock);
287-
if (ret == -EEXIST)
288-
pr_err("hwspinlock id %d already exists!\n", hwlock->id);
289-
if (ret)
267+
ret = radix_tree_insert(&hwspinlock_tree, id, hwlock);
268+
if (ret) {
269+
if (ret == -EEXIST)
270+
pr_err("hwspinlock id %d already exists!\n", id);
290271
goto out;
272+
}
291273

292274
/* mark this hwspinlock as available */
293-
tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock->id,
294-
HWSPINLOCK_UNUSED);
275+
tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
295276

296277
/* self-sanity check which should never fail */
297278
WARN_ON(tmp != hwlock);
298279

299280
out:
300281
mutex_unlock(&hwspinlock_tree_lock);
301-
return ret;
282+
return 0;
302283
}
303-
EXPORT_SYMBOL_GPL(hwspin_lock_register);
304284

305-
/**
306-
* hwspin_lock_unregister() - unregister an hw spinlock
307-
* @id: index of the specific hwspinlock to unregister
308-
*
309-
* This function should be called from the underlying platform-specific
310-
* implementation, to unregister an existing (and unused) hwspinlock.
311-
*
312-
* Should be called from a process context (might sleep)
313-
*
314-
* Returns the address of hwspinlock @id on success, or NULL on failure
315-
*/
316-
struct hwspinlock *hwspin_lock_unregister(unsigned int id)
285+
static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id)
317286
{
318287
struct hwspinlock *hwlock = NULL;
319288
int ret;
@@ -337,6 +306,88 @@ struct hwspinlock *hwspin_lock_unregister(unsigned int id)
337306
mutex_unlock(&hwspinlock_tree_lock);
338307
return hwlock;
339308
}
309+
310+
/**
311+
* hwspin_lock_register() - register a new hw spinlock device
312+
* @bank: the hwspinlock device, which usually provides numerous hw locks
313+
* @dev: the backing device
314+
* @ops: hwspinlock handlers for this device
315+
* @base_id: id of the first hardware spinlock in this bank
316+
* @num_locks: number of hwspinlocks provided by this device
317+
*
318+
* This function should be called from the underlying platform-specific
319+
* implementation, to register a new hwspinlock device instance.
320+
*
321+
* Should be called from a process context (might sleep)
322+
*
323+
* Returns 0 on success, or an appropriate error code on failure
324+
*/
325+
int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
326+
const struct hwspinlock_ops *ops, int base_id, int num_locks)
327+
{
328+
struct hwspinlock *hwlock;
329+
int ret = 0, i;
330+
331+
if (!bank || !ops || !dev || !num_locks || !ops->trylock ||
332+
!ops->unlock) {
333+
pr_err("invalid parameters\n");
334+
return -EINVAL;
335+
}
336+
337+
bank->dev = dev;
338+
bank->ops = ops;
339+
bank->base_id = base_id;
340+
bank->num_locks = num_locks;
341+
342+
for (i = 0; i < num_locks; i++) {
343+
hwlock = &bank->lock[i];
344+
345+
spin_lock_init(&hwlock->lock);
346+
hwlock->bank = bank;
347+
348+
ret = hwspin_lock_register_single(hwlock, i);
349+
if (ret)
350+
goto reg_failed;
351+
}
352+
353+
return 0;
354+
355+
reg_failed:
356+
while (--i >= 0)
357+
hwspin_lock_unregister_single(i);
358+
return ret;
359+
}
360+
EXPORT_SYMBOL_GPL(hwspin_lock_register);
361+
362+
/**
363+
* hwspin_lock_unregister() - unregister an hw spinlock device
364+
* @bank: the hwspinlock device, which usually provides numerous hw locks
365+
*
366+
* This function should be called from the underlying platform-specific
367+
* implementation, to unregister an existing (and unused) hwspinlock.
368+
*
369+
* Should be called from a process context (might sleep)
370+
*
371+
* Returns 0 on success, or an appropriate error code on failure
372+
*/
373+
int hwspin_lock_unregister(struct hwspinlock_device *bank)
374+
{
375+
struct hwspinlock *hwlock, *tmp;
376+
int i;
377+
378+
for (i = 0; i < bank->num_locks; i++) {
379+
hwlock = &bank->lock[i];
380+
381+
tmp = hwspin_lock_unregister_single(bank->base_id + i);
382+
if (!tmp)
383+
return -EBUSY;
384+
385+
/* self-sanity check that should never fail */
386+
WARN_ON(tmp != hwlock);
387+
}
388+
389+
return 0;
390+
}
340391
EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
341392

342393
/**
@@ -351,24 +402,25 @@ EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
351402
*/
352403
static int __hwspin_lock_request(struct hwspinlock *hwlock)
353404
{
405+
struct device *dev = hwlock->bank->dev;
354406
struct hwspinlock *tmp;
355407
int ret;
356408

357409
/* prevent underlying implementation from being removed */
358-
if (!try_module_get(hwlock->dev->driver->owner)) {
359-
dev_err(hwlock->dev, "%s: can't get owner\n", __func__);
410+
if (!try_module_get(dev->driver->owner)) {
411+
dev_err(dev, "%s: can't get owner\n", __func__);
360412
return -EINVAL;
361413
}
362414

363415
/* notify PM core that power is now needed */
364-
ret = pm_runtime_get_sync(hwlock->dev);
416+
ret = pm_runtime_get_sync(dev);
365417
if (ret < 0) {
366-
dev_err(hwlock->dev, "%s: can't power on device\n", __func__);
418+
dev_err(dev, "%s: can't power on device\n", __func__);
367419
return ret;
368420
}
369421

370422
/* mark hwspinlock as used, should not fail */
371-
tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock->id,
423+
tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
372424
HWSPINLOCK_UNUSED);
373425

374426
/* self-sanity check that should never fail */
@@ -390,7 +442,7 @@ int hwspin_lock_get_id(struct hwspinlock *hwlock)
390442
return -EINVAL;
391443
}
392444

393-
return hwlock->id;
445+
return hwlock_to_id(hwlock);
394446
}
395447
EXPORT_SYMBOL_GPL(hwspin_lock_get_id);
396448

@@ -465,7 +517,7 @@ struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
465517
}
466518

467519
/* sanity check (this shouldn't happen) */
468-
WARN_ON(hwlock->id != id);
520+
WARN_ON(hwlock_to_id(hwlock) != id);
469521

470522
/* make sure this hwspinlock is unused */
471523
ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED);
@@ -500,6 +552,7 @@ EXPORT_SYMBOL_GPL(hwspin_lock_request_specific);
500552
*/
501553
int hwspin_lock_free(struct hwspinlock *hwlock)
502554
{
555+
struct device *dev = hwlock->bank->dev;
503556
struct hwspinlock *tmp;
504557
int ret;
505558

@@ -511,28 +564,28 @@ int hwspin_lock_free(struct hwspinlock *hwlock)
511564
mutex_lock(&hwspinlock_tree_lock);
512565

513566
/* make sure the hwspinlock is used */
514-
ret = radix_tree_tag_get(&hwspinlock_tree, hwlock->id,
567+
ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock),
515568
HWSPINLOCK_UNUSED);
516569
if (ret == 1) {
517-
dev_err(hwlock->dev, "%s: hwlock is already free\n", __func__);
570+
dev_err(dev, "%s: hwlock is already free\n", __func__);
518571
dump_stack();
519572
ret = -EINVAL;
520573
goto out;
521574
}
522575

523576
/* notify the underlying device that power is not needed */
524-
ret = pm_runtime_put(hwlock->dev);
577+
ret = pm_runtime_put(dev);
525578
if (ret < 0)
526579
goto out;
527580

528581
/* mark this hwspinlock as available */
529-
tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock->id,
582+
tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
530583
HWSPINLOCK_UNUSED);
531584

532585
/* sanity check (this shouldn't happen) */
533586
WARN_ON(tmp != hwlock);
534587

535-
module_put(hwlock->dev->driver->owner);
588+
module_put(dev->driver->owner);
536589

537590
out:
538591
mutex_unlock(&hwspinlock_tree_lock);

0 commit comments

Comments
 (0)