@@ -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}
258258EXPORT_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
299280out :
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+ }
340391EXPORT_SYMBOL_GPL (hwspin_lock_unregister );
341392
342393/**
@@ -351,24 +402,25 @@ EXPORT_SYMBOL_GPL(hwspin_lock_unregister);
351402 */
352403static 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}
395447EXPORT_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 */
501553int 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
537590out :
538591 mutex_unlock (& hwspinlock_tree_lock );
0 commit comments