162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hardware spinlocks internal header
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Contact: Ohad Ben-Cohen <ohad@wizery.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef __HWSPINLOCK_HWSPINLOCK_H
1162306a36Sopenharmony_ci#define __HWSPINLOCK_HWSPINLOCK_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/spinlock.h>
1462306a36Sopenharmony_ci#include <linux/device.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistruct hwspinlock_device;
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/**
1962306a36Sopenharmony_ci * struct hwspinlock_ops - platform-specific hwspinlock handlers
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * @trylock: make a single attempt to take the lock. returns 0 on
2262306a36Sopenharmony_ci *	     failure and true on success. may _not_ sleep.
2362306a36Sopenharmony_ci * @unlock:  release the lock. always succeed. may _not_ sleep.
2462306a36Sopenharmony_ci * @relax:   optional, platform-specific relax handler, called by hwspinlock
2562306a36Sopenharmony_ci *	     core while spinning on a lock, between two successive
2662306a36Sopenharmony_ci *	     invocations of @trylock. may _not_ sleep.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistruct hwspinlock_ops {
2962306a36Sopenharmony_ci	int (*trylock)(struct hwspinlock *lock);
3062306a36Sopenharmony_ci	void (*unlock)(struct hwspinlock *lock);
3162306a36Sopenharmony_ci	void (*relax)(struct hwspinlock *lock);
3262306a36Sopenharmony_ci};
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/**
3562306a36Sopenharmony_ci * struct hwspinlock - this struct represents a single hwspinlock instance
3662306a36Sopenharmony_ci * @bank: the hwspinlock_device structure which owns this lock
3762306a36Sopenharmony_ci * @lock: initialized and used by hwspinlock core
3862306a36Sopenharmony_ci * @priv: private data, owned by the underlying platform-specific hwspinlock drv
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_cistruct hwspinlock {
4162306a36Sopenharmony_ci	struct hwspinlock_device *bank;
4262306a36Sopenharmony_ci	spinlock_t lock;
4362306a36Sopenharmony_ci	void *priv;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/**
4762306a36Sopenharmony_ci * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
4862306a36Sopenharmony_ci * @dev: underlying device, will be used to invoke runtime PM api
4962306a36Sopenharmony_ci * @ops: platform-specific hwspinlock handlers
5062306a36Sopenharmony_ci * @base_id: id index of the first lock in this device
5162306a36Sopenharmony_ci * @num_locks: number of locks in this device
5262306a36Sopenharmony_ci * @lock: dynamically allocated array of 'struct hwspinlock'
5362306a36Sopenharmony_ci */
5462306a36Sopenharmony_cistruct hwspinlock_device {
5562306a36Sopenharmony_ci	struct device *dev;
5662306a36Sopenharmony_ci	const struct hwspinlock_ops *ops;
5762306a36Sopenharmony_ci	int base_id;
5862306a36Sopenharmony_ci	int num_locks;
5962306a36Sopenharmony_ci	struct hwspinlock lock[];
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic inline int hwlock_to_id(struct hwspinlock *hwlock)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	int local_id = hwlock - &hwlock->bank->lock[0];
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	return hwlock->bank->base_id + local_id;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#endif /* __HWSPINLOCK_HWSPINLOCK_H */
70