xref: /openbmc/linux/drivers/firmware/ti_sci.c (revision 7615536a)
11e0a6014SLokesh Vutla // SPDX-License-Identifier: GPL-2.0
2aa276781SNishanth Menon /*
3aa276781SNishanth Menon  * Texas Instruments System Control Interface Protocol Driver
4aa276781SNishanth Menon  *
5b9e8a7d9SDave Gerlach  * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://www.ti.com/
6aa276781SNishanth Menon  *	Nishanth Menon
7aa276781SNishanth Menon  */
8aa276781SNishanth Menon 
9aa276781SNishanth Menon #define pr_fmt(fmt) "%s: " fmt, __func__
10aa276781SNishanth Menon 
11aa276781SNishanth Menon #include <linux/bitmap.h>
12aa276781SNishanth Menon #include <linux/debugfs.h>
13aa276781SNishanth Menon #include <linux/export.h>
14aa276781SNishanth Menon #include <linux/io.h>
15b9e8a7d9SDave Gerlach #include <linux/iopoll.h>
16aa276781SNishanth Menon #include <linux/kernel.h>
17aa276781SNishanth Menon #include <linux/mailbox_client.h>
18aa276781SNishanth Menon #include <linux/module.h>
19aa276781SNishanth Menon #include <linux/of_device.h>
20aa276781SNishanth Menon #include <linux/semaphore.h>
21aa276781SNishanth Menon #include <linux/slab.h>
22aa276781SNishanth Menon #include <linux/soc/ti/ti-msgmgr.h>
23aa276781SNishanth Menon #include <linux/soc/ti/ti_sci_protocol.h>
24912cffb4SNishanth Menon #include <linux/reboot.h>
25aa276781SNishanth Menon 
26aa276781SNishanth Menon #include "ti_sci.h"
27aa276781SNishanth Menon 
28aa276781SNishanth Menon /* List of all TI SCI devices active in system */
29aa276781SNishanth Menon static LIST_HEAD(ti_sci_list);
30aa276781SNishanth Menon /* Protection for the entire list */
31aa276781SNishanth Menon static DEFINE_MUTEX(ti_sci_list_mutex);
32aa276781SNishanth Menon 
33aa276781SNishanth Menon /**
34aa276781SNishanth Menon  * struct ti_sci_xfer - Structure representing a message flow
35aa276781SNishanth Menon  * @tx_message:	Transmit message
36aa276781SNishanth Menon  * @rx_len:	Receive message length
37aa276781SNishanth Menon  * @xfer_buf:	Preallocated buffer to store receive message
38aa276781SNishanth Menon  *		Since we work with request-ACK protocol, we can
39aa276781SNishanth Menon  *		reuse the same buffer for the rx path as we
40aa276781SNishanth Menon  *		use for the tx path.
41aa276781SNishanth Menon  * @done:	completion event
42aa276781SNishanth Menon  */
43aa276781SNishanth Menon struct ti_sci_xfer {
44aa276781SNishanth Menon 	struct ti_msgmgr_message tx_message;
45aa276781SNishanth Menon 	u8 rx_len;
46aa276781SNishanth Menon 	u8 *xfer_buf;
47aa276781SNishanth Menon 	struct completion done;
48aa276781SNishanth Menon };
49aa276781SNishanth Menon 
50aa276781SNishanth Menon /**
51aa276781SNishanth Menon  * struct ti_sci_xfers_info - Structure to manage transfer information
52aa276781SNishanth Menon  * @sem_xfer_count:	Counting Semaphore for managing max simultaneous
53aa276781SNishanth Menon  *			Messages.
54aa276781SNishanth Menon  * @xfer_block:		Preallocated Message array
55aa276781SNishanth Menon  * @xfer_alloc_table:	Bitmap table for allocated messages.
56aa276781SNishanth Menon  *			Index of this bitmap table is also used for message
57aa276781SNishanth Menon  *			sequence identifier.
58aa276781SNishanth Menon  * @xfer_lock:		Protection for message allocation
59aa276781SNishanth Menon  */
60aa276781SNishanth Menon struct ti_sci_xfers_info {
61aa276781SNishanth Menon 	struct semaphore sem_xfer_count;
62aa276781SNishanth Menon 	struct ti_sci_xfer *xfer_block;
63aa276781SNishanth Menon 	unsigned long *xfer_alloc_table;
64aa276781SNishanth Menon 	/* protect transfer allocation */
65aa276781SNishanth Menon 	spinlock_t xfer_lock;
66aa276781SNishanth Menon };
67aa276781SNishanth Menon 
68aa276781SNishanth Menon /**
69aa276781SNishanth Menon  * struct ti_sci_desc - Description of SoC integration
70e69a3553SNishanth Menon  * @default_host_id:	Host identifier representing the compute entity
71aa276781SNishanth Menon  * @max_rx_timeout_ms:	Timeout for communication with SoC (in Milliseconds)
72aa276781SNishanth Menon  * @max_msgs: Maximum number of messages that can be pending
73aa276781SNishanth Menon  *		  simultaneously in the system
74aa276781SNishanth Menon  * @max_msg_size: Maximum size of data per message that can be handled.
75aa276781SNishanth Menon  */
76aa276781SNishanth Menon struct ti_sci_desc {
77e69a3553SNishanth Menon 	u8 default_host_id;
78aa276781SNishanth Menon 	int max_rx_timeout_ms;
79aa276781SNishanth Menon 	int max_msgs;
80aa276781SNishanth Menon 	int max_msg_size;
81aa276781SNishanth Menon };
82aa276781SNishanth Menon 
83aa276781SNishanth Menon /**
84aa276781SNishanth Menon  * struct ti_sci_info - Structure representing a TI SCI instance
85aa276781SNishanth Menon  * @dev:	Device pointer
86aa276781SNishanth Menon  * @desc:	SoC description for this instance
87912cffb4SNishanth Menon  * @nb:	Reboot Notifier block
88aa276781SNishanth Menon  * @d:		Debugfs file entry
89aa276781SNishanth Menon  * @debug_region: Memory region where the debug message are available
90aa276781SNishanth Menon  * @debug_region_size: Debug region size
91aa276781SNishanth Menon  * @debug_buffer: Buffer allocated to copy debug messages.
92aa276781SNishanth Menon  * @handle:	Instance of TI SCI handle to send to clients.
93aa276781SNishanth Menon  * @cl:		Mailbox Client
94aa276781SNishanth Menon  * @chan_tx:	Transmit mailbox channel
95aa276781SNishanth Menon  * @chan_rx:	Receive mailbox channel
96aa276781SNishanth Menon  * @minfo:	Message info
97aa276781SNishanth Menon  * @node:	list head
98e69a3553SNishanth Menon  * @host_id:	Host ID
99aa276781SNishanth Menon  * @users:	Number of users of this instance
100aa276781SNishanth Menon  */
101aa276781SNishanth Menon struct ti_sci_info {
102aa276781SNishanth Menon 	struct device *dev;
103912cffb4SNishanth Menon 	struct notifier_block nb;
104aa276781SNishanth Menon 	const struct ti_sci_desc *desc;
105aa276781SNishanth Menon 	struct dentry *d;
106aa276781SNishanth Menon 	void __iomem *debug_region;
107aa276781SNishanth Menon 	char *debug_buffer;
108aa276781SNishanth Menon 	size_t debug_region_size;
109aa276781SNishanth Menon 	struct ti_sci_handle handle;
110aa276781SNishanth Menon 	struct mbox_client cl;
111aa276781SNishanth Menon 	struct mbox_chan *chan_tx;
112aa276781SNishanth Menon 	struct mbox_chan *chan_rx;
113aa276781SNishanth Menon 	struct ti_sci_xfers_info minfo;
114aa276781SNishanth Menon 	struct list_head node;
115e69a3553SNishanth Menon 	u8 host_id;
116aa276781SNishanth Menon 	/* protected by ti_sci_list_mutex */
117aa276781SNishanth Menon 	int users;
118aa276781SNishanth Menon };
119aa276781SNishanth Menon 
120aa276781SNishanth Menon #define cl_to_ti_sci_info(c)	container_of(c, struct ti_sci_info, cl)
121aa276781SNishanth Menon #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
122912cffb4SNishanth Menon #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
123aa276781SNishanth Menon 
124aa276781SNishanth Menon #ifdef CONFIG_DEBUG_FS
125aa276781SNishanth Menon 
126aa276781SNishanth Menon /**
127aa276781SNishanth Menon  * ti_sci_debug_show() - Helper to dump the debug log
128aa276781SNishanth Menon  * @s:	sequence file pointer
129aa276781SNishanth Menon  * @unused:	unused.
130aa276781SNishanth Menon  *
131aa276781SNishanth Menon  * Return: 0
132aa276781SNishanth Menon  */
ti_sci_debug_show(struct seq_file * s,void * unused)133aa276781SNishanth Menon static int ti_sci_debug_show(struct seq_file *s, void *unused)
134aa276781SNishanth Menon {
135aa276781SNishanth Menon 	struct ti_sci_info *info = s->private;
136aa276781SNishanth Menon 
137aa276781SNishanth Menon 	memcpy_fromio(info->debug_buffer, info->debug_region,
138aa276781SNishanth Menon 		      info->debug_region_size);
139aa276781SNishanth Menon 	/*
140aa276781SNishanth Menon 	 * We don't trust firmware to leave NULL terminated last byte (hence
141aa276781SNishanth Menon 	 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142aa276781SNishanth Menon 	 * specific data format for debug messages, We just present the data
143aa276781SNishanth Menon 	 * in the buffer as is - we expect the messages to be self explanatory.
144aa276781SNishanth Menon 	 */
145aa276781SNishanth Menon 	seq_puts(s, info->debug_buffer);
146aa276781SNishanth Menon 	return 0;
147aa276781SNishanth Menon }
148aa276781SNishanth Menon 
1495953c887SYangtao Li /* Provide the log file operations interface*/
1505953c887SYangtao Li DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
151aa276781SNishanth Menon 
152aa276781SNishanth Menon /**
153aa276781SNishanth Menon  * ti_sci_debugfs_create() - Create log debug file
154aa276781SNishanth Menon  * @pdev:	platform device pointer
155aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
156aa276781SNishanth Menon  *
157aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
158aa276781SNishanth Menon  */
ti_sci_debugfs_create(struct platform_device * pdev,struct ti_sci_info * info)159aa276781SNishanth Menon static int ti_sci_debugfs_create(struct platform_device *pdev,
160aa276781SNishanth Menon 				 struct ti_sci_info *info)
161aa276781SNishanth Menon {
162aa276781SNishanth Menon 	struct device *dev = &pdev->dev;
163aa276781SNishanth Menon 	struct resource *res;
164*7615536aSChristophe JAILLET 	char debug_name[50];
165aa276781SNishanth Menon 
166aa276781SNishanth Menon 	/* Debug region is optional */
167aa276781SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
168aa276781SNishanth Menon 					   "debug_messages");
169aa276781SNishanth Menon 	info->debug_region = devm_ioremap_resource(dev, res);
170aa276781SNishanth Menon 	if (IS_ERR(info->debug_region))
171aa276781SNishanth Menon 		return 0;
172aa276781SNishanth Menon 	info->debug_region_size = resource_size(res);
173aa276781SNishanth Menon 
174aa276781SNishanth Menon 	info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
175aa276781SNishanth Menon 					  sizeof(char), GFP_KERNEL);
176aa276781SNishanth Menon 	if (!info->debug_buffer)
177aa276781SNishanth Menon 		return -ENOMEM;
178aa276781SNishanth Menon 	/* Setup NULL termination */
179aa276781SNishanth Menon 	info->debug_buffer[info->debug_region_size] = 0;
180aa276781SNishanth Menon 
181*7615536aSChristophe JAILLET 	snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s",
182*7615536aSChristophe JAILLET 		 dev_name(dev));
183*7615536aSChristophe JAILLET 	info->d = debugfs_create_file(debug_name, 0444, NULL, info,
184*7615536aSChristophe JAILLET 				      &ti_sci_debug_fops);
185aa276781SNishanth Menon 	if (IS_ERR(info->d))
186aa276781SNishanth Menon 		return PTR_ERR(info->d);
187aa276781SNishanth Menon 
188aa276781SNishanth Menon 	dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
189aa276781SNishanth Menon 		info->debug_region, info->debug_region_size, res);
190aa276781SNishanth Menon 	return 0;
191aa276781SNishanth Menon }
192aa276781SNishanth Menon 
193aa276781SNishanth Menon #else /* CONFIG_DEBUG_FS */
ti_sci_debugfs_create(struct platform_device * dev,struct ti_sci_info * info)194aa276781SNishanth Menon static inline int ti_sci_debugfs_create(struct platform_device *dev,
195aa276781SNishanth Menon 					struct ti_sci_info *info)
196aa276781SNishanth Menon {
197aa276781SNishanth Menon 	return 0;
198aa276781SNishanth Menon }
199aa276781SNishanth Menon 
ti_sci_debugfs_destroy(struct platform_device * dev,struct ti_sci_info * info)200aa276781SNishanth Menon static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
201aa276781SNishanth Menon 					  struct ti_sci_info *info)
202aa276781SNishanth Menon {
203aa276781SNishanth Menon }
204aa276781SNishanth Menon #endif /* CONFIG_DEBUG_FS */
205aa276781SNishanth Menon 
206aa276781SNishanth Menon /**
207aa276781SNishanth Menon  * ti_sci_dump_header_dbg() - Helper to dump a message header.
208aa276781SNishanth Menon  * @dev:	Device pointer corresponding to the SCI entity
209aa276781SNishanth Menon  * @hdr:	pointer to header.
210aa276781SNishanth Menon  */
ti_sci_dump_header_dbg(struct device * dev,struct ti_sci_msg_hdr * hdr)211aa276781SNishanth Menon static inline void ti_sci_dump_header_dbg(struct device *dev,
212aa276781SNishanth Menon 					  struct ti_sci_msg_hdr *hdr)
213aa276781SNishanth Menon {
214aa276781SNishanth Menon 	dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
215aa276781SNishanth Menon 		hdr->type, hdr->host, hdr->seq, hdr->flags);
216aa276781SNishanth Menon }
217aa276781SNishanth Menon 
218aa276781SNishanth Menon /**
219aa276781SNishanth Menon  * ti_sci_rx_callback() - mailbox client callback for receive messages
220aa276781SNishanth Menon  * @cl:	client pointer
221aa276781SNishanth Menon  * @m:	mailbox message
222aa276781SNishanth Menon  *
223aa276781SNishanth Menon  * Processes one received message to appropriate transfer information and
224aa276781SNishanth Menon  * signals completion of the transfer.
225aa276781SNishanth Menon  *
226aa276781SNishanth Menon  * NOTE: This function will be invoked in IRQ context, hence should be
227aa276781SNishanth Menon  * as optimal as possible.
228aa276781SNishanth Menon  */
ti_sci_rx_callback(struct mbox_client * cl,void * m)229aa276781SNishanth Menon static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
230aa276781SNishanth Menon {
231aa276781SNishanth Menon 	struct ti_sci_info *info = cl_to_ti_sci_info(cl);
232aa276781SNishanth Menon 	struct device *dev = info->dev;
233aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo = &info->minfo;
234aa276781SNishanth Menon 	struct ti_msgmgr_message *mbox_msg = m;
235aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
236aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
237aa276781SNishanth Menon 	u8 xfer_id;
238aa276781SNishanth Menon 
239aa276781SNishanth Menon 	xfer_id = hdr->seq;
240aa276781SNishanth Menon 
241aa276781SNishanth Menon 	/*
242aa276781SNishanth Menon 	 * Are we even expecting this?
243aa276781SNishanth Menon 	 * NOTE: barriers were implicit in locks used for modifying the bitmap
244aa276781SNishanth Menon 	 */
245aa276781SNishanth Menon 	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
246aa276781SNishanth Menon 		dev_err(dev, "Message for %d is not expected!\n", xfer_id);
247aa276781SNishanth Menon 		return;
248aa276781SNishanth Menon 	}
249aa276781SNishanth Menon 
250aa276781SNishanth Menon 	xfer = &minfo->xfer_block[xfer_id];
251aa276781SNishanth Menon 
252aa276781SNishanth Menon 	/* Is the message of valid length? */
253aa276781SNishanth Menon 	if (mbox_msg->len > info->desc->max_msg_size) {
254bd0fa74eSNishanth Menon 		dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
255aa276781SNishanth Menon 			mbox_msg->len, info->desc->max_msg_size);
256aa276781SNishanth Menon 		ti_sci_dump_header_dbg(dev, hdr);
257aa276781SNishanth Menon 		return;
258aa276781SNishanth Menon 	}
259aa276781SNishanth Menon 	if (mbox_msg->len < xfer->rx_len) {
260bd0fa74eSNishanth Menon 		dev_err(dev, "Recv xfer %zu < expected %d length\n",
261aa276781SNishanth Menon 			mbox_msg->len, xfer->rx_len);
262aa276781SNishanth Menon 		ti_sci_dump_header_dbg(dev, hdr);
263aa276781SNishanth Menon 		return;
264aa276781SNishanth Menon 	}
265aa276781SNishanth Menon 
266aa276781SNishanth Menon 	ti_sci_dump_header_dbg(dev, hdr);
267aa276781SNishanth Menon 	/* Take a copy to the rx buffer.. */
268aa276781SNishanth Menon 	memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
269aa276781SNishanth Menon 	complete(&xfer->done);
270aa276781SNishanth Menon }
271aa276781SNishanth Menon 
272aa276781SNishanth Menon /**
273aa276781SNishanth Menon  * ti_sci_get_one_xfer() - Allocate one message
274aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
275aa276781SNishanth Menon  * @msg_type:	Message type
276aa276781SNishanth Menon  * @msg_flags:	Flag to set for the message
277aa276781SNishanth Menon  * @tx_message_size: transmit message size
278aa276781SNishanth Menon  * @rx_message_size: receive message size
279aa276781SNishanth Menon  *
280aa276781SNishanth Menon  * Helper function which is used by various command functions that are
281aa276781SNishanth Menon  * exposed to clients of this driver for allocating a message traffic event.
282aa276781SNishanth Menon  *
283aa276781SNishanth Menon  * This function can sleep depending on pending requests already in the system
284aa276781SNishanth Menon  * for the SCI entity. Further, this also holds a spinlock to maintain integrity
285aa276781SNishanth Menon  * of internal data structures.
286aa276781SNishanth Menon  *
287aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
288aa276781SNishanth Menon  */
ti_sci_get_one_xfer(struct ti_sci_info * info,u16 msg_type,u32 msg_flags,size_t tx_message_size,size_t rx_message_size)289aa276781SNishanth Menon static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
290aa276781SNishanth Menon 					       u16 msg_type, u32 msg_flags,
291aa276781SNishanth Menon 					       size_t tx_message_size,
292aa276781SNishanth Menon 					       size_t rx_message_size)
293aa276781SNishanth Menon {
294aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo = &info->minfo;
295aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
296aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr;
297aa276781SNishanth Menon 	unsigned long flags;
298aa276781SNishanth Menon 	unsigned long bit_pos;
299aa276781SNishanth Menon 	u8 xfer_id;
300aa276781SNishanth Menon 	int ret;
301aa276781SNishanth Menon 	int timeout;
302aa276781SNishanth Menon 
303aa276781SNishanth Menon 	/* Ensure we have sane transfer sizes */
304aa276781SNishanth Menon 	if (rx_message_size > info->desc->max_msg_size ||
305aa276781SNishanth Menon 	    tx_message_size > info->desc->max_msg_size ||
306aa276781SNishanth Menon 	    rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
307aa276781SNishanth Menon 		return ERR_PTR(-ERANGE);
308aa276781SNishanth Menon 
309aa276781SNishanth Menon 	/*
310aa276781SNishanth Menon 	 * Ensure we have only controlled number of pending messages.
311aa276781SNishanth Menon 	 * Ideally, we might just have to wait a single message, be
312aa276781SNishanth Menon 	 * conservative and wait 5 times that..
313aa276781SNishanth Menon 	 */
314aa276781SNishanth Menon 	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
315aa276781SNishanth Menon 	ret = down_timeout(&minfo->sem_xfer_count, timeout);
316aa276781SNishanth Menon 	if (ret < 0)
317aa276781SNishanth Menon 		return ERR_PTR(ret);
318aa276781SNishanth Menon 
319aa276781SNishanth Menon 	/* Keep the locked section as small as possible */
320aa276781SNishanth Menon 	spin_lock_irqsave(&minfo->xfer_lock, flags);
321aa276781SNishanth Menon 	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
322aa276781SNishanth Menon 				      info->desc->max_msgs);
323aa276781SNishanth Menon 	set_bit(bit_pos, minfo->xfer_alloc_table);
324aa276781SNishanth Menon 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
325aa276781SNishanth Menon 
326aa276781SNishanth Menon 	/*
327aa276781SNishanth Menon 	 * We already ensured in probe that we can have max messages that can
328aa276781SNishanth Menon 	 * fit in  hdr.seq - NOTE: this improves access latencies
329aa276781SNishanth Menon 	 * to predictable O(1) access, BUT, it opens us to risk if
330aa276781SNishanth Menon 	 * remote misbehaves with corrupted message sequence responses.
331aa276781SNishanth Menon 	 * If that happens, we are going to be messed up anyways..
332aa276781SNishanth Menon 	 */
333aa276781SNishanth Menon 	xfer_id = (u8)bit_pos;
334aa276781SNishanth Menon 
335aa276781SNishanth Menon 	xfer = &minfo->xfer_block[xfer_id];
336aa276781SNishanth Menon 
337aa276781SNishanth Menon 	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
338aa276781SNishanth Menon 	xfer->tx_message.len = tx_message_size;
339b9e8a7d9SDave Gerlach 	xfer->tx_message.chan_rx = info->chan_rx;
340b9e8a7d9SDave Gerlach 	xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms;
341aa276781SNishanth Menon 	xfer->rx_len = (u8)rx_message_size;
342aa276781SNishanth Menon 
343aa276781SNishanth Menon 	reinit_completion(&xfer->done);
344aa276781SNishanth Menon 
345aa276781SNishanth Menon 	hdr->seq = xfer_id;
346aa276781SNishanth Menon 	hdr->type = msg_type;
347e69a3553SNishanth Menon 	hdr->host = info->host_id;
348aa276781SNishanth Menon 	hdr->flags = msg_flags;
349aa276781SNishanth Menon 
350aa276781SNishanth Menon 	return xfer;
351aa276781SNishanth Menon }
352aa276781SNishanth Menon 
353aa276781SNishanth Menon /**
354aa276781SNishanth Menon  * ti_sci_put_one_xfer() - Release a message
355aa276781SNishanth Menon  * @minfo:	transfer info pointer
356aa276781SNishanth Menon  * @xfer:	message that was reserved by ti_sci_get_one_xfer
357aa276781SNishanth Menon  *
358aa276781SNishanth Menon  * This holds a spinlock to maintain integrity of internal data structures.
359aa276781SNishanth Menon  */
ti_sci_put_one_xfer(struct ti_sci_xfers_info * minfo,struct ti_sci_xfer * xfer)360aa276781SNishanth Menon static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
361aa276781SNishanth Menon 				struct ti_sci_xfer *xfer)
362aa276781SNishanth Menon {
363aa276781SNishanth Menon 	unsigned long flags;
364aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr;
365aa276781SNishanth Menon 	u8 xfer_id;
366aa276781SNishanth Menon 
367aa276781SNishanth Menon 	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368aa276781SNishanth Menon 	xfer_id = hdr->seq;
369aa276781SNishanth Menon 
370aa276781SNishanth Menon 	/*
371aa276781SNishanth Menon 	 * Keep the locked section as small as possible
372aa276781SNishanth Menon 	 * NOTE: we might escape with smp_mb and no lock here..
373aa276781SNishanth Menon 	 * but just be conservative and symmetric.
374aa276781SNishanth Menon 	 */
375aa276781SNishanth Menon 	spin_lock_irqsave(&minfo->xfer_lock, flags);
376aa276781SNishanth Menon 	clear_bit(xfer_id, minfo->xfer_alloc_table);
377aa276781SNishanth Menon 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
378aa276781SNishanth Menon 
379aa276781SNishanth Menon 	/* Increment the count for the next user to get through */
380aa276781SNishanth Menon 	up(&minfo->sem_xfer_count);
381aa276781SNishanth Menon }
382aa276781SNishanth Menon 
383aa276781SNishanth Menon /**
384aa276781SNishanth Menon  * ti_sci_do_xfer() - Do one transfer
385aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
386aa276781SNishanth Menon  * @xfer:	Transfer to initiate and wait for response
387aa276781SNishanth Menon  *
388aa276781SNishanth Menon  * Return: -ETIMEDOUT in case of no response, if transmit error,
389aa276781SNishanth Menon  *	   return corresponding error, else if all goes well,
390aa276781SNishanth Menon  *	   return 0.
391aa276781SNishanth Menon  */
ti_sci_do_xfer(struct ti_sci_info * info,struct ti_sci_xfer * xfer)392aa276781SNishanth Menon static inline int ti_sci_do_xfer(struct ti_sci_info *info,
393aa276781SNishanth Menon 				 struct ti_sci_xfer *xfer)
394aa276781SNishanth Menon {
395aa276781SNishanth Menon 	int ret;
396aa276781SNishanth Menon 	int timeout;
397aa276781SNishanth Menon 	struct device *dev = info->dev;
398b9e8a7d9SDave Gerlach 	bool done_state = true;
399aa276781SNishanth Menon 
400aa276781SNishanth Menon 	ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
401aa276781SNishanth Menon 	if (ret < 0)
402aa276781SNishanth Menon 		return ret;
403aa276781SNishanth Menon 
404aa276781SNishanth Menon 	ret = 0;
405aa276781SNishanth Menon 
4069225bcdeSNishanth Menon 	if (system_state <= SYSTEM_RUNNING) {
407aa276781SNishanth Menon 		/* And we wait for the response. */
408aa276781SNishanth Menon 		timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
409b9e8a7d9SDave Gerlach 		if (!wait_for_completion_timeout(&xfer->done, timeout))
410b9e8a7d9SDave Gerlach 			ret = -ETIMEDOUT;
411b9e8a7d9SDave Gerlach 	} else {
412b9e8a7d9SDave Gerlach 		/*
4139225bcdeSNishanth Menon 		 * If we are !running, we cannot use wait_for_completion_timeout
414b9e8a7d9SDave Gerlach 		 * during noirq phase, so we must manually poll the completion.
415b9e8a7d9SDave Gerlach 		 */
416b9e8a7d9SDave Gerlach 		ret = read_poll_timeout_atomic(try_wait_for_completion, done_state,
417b13b2c3eSGeorgi Vlaev 					       done_state, 1,
418b9e8a7d9SDave Gerlach 					       info->desc->max_rx_timeout_ms * 1000,
419b9e8a7d9SDave Gerlach 					       false, &xfer->done);
420b9e8a7d9SDave Gerlach 	}
421b9e8a7d9SDave Gerlach 
422b13b2c3eSGeorgi Vlaev 	if (ret == -ETIMEDOUT)
423595f3a9dSHelge Deller 		dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
424aa276781SNishanth Menon 			(void *)_RET_IP_);
425b9e8a7d9SDave Gerlach 
426aa276781SNishanth Menon 	/*
427aa276781SNishanth Menon 	 * NOTE: we might prefer not to need the mailbox ticker to manage the
428aa276781SNishanth Menon 	 * transfer queueing since the protocol layer queues things by itself.
429aa276781SNishanth Menon 	 * Unfortunately, we have to kick the mailbox framework after we have
430aa276781SNishanth Menon 	 * received our message.
431aa276781SNishanth Menon 	 */
432aa276781SNishanth Menon 	mbox_client_txdone(info->chan_tx, ret);
433aa276781SNishanth Menon 
434aa276781SNishanth Menon 	return ret;
435aa276781SNishanth Menon }
436aa276781SNishanth Menon 
437aa276781SNishanth Menon /**
438aa276781SNishanth Menon  * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
439aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
440aa276781SNishanth Menon  *
441aa276781SNishanth Menon  * Updates the SCI information in the internal data structure.
442aa276781SNishanth Menon  *
443aa276781SNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
444aa276781SNishanth Menon  */
ti_sci_cmd_get_revision(struct ti_sci_info * info)445aa276781SNishanth Menon static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
446aa276781SNishanth Menon {
447aa276781SNishanth Menon 	struct device *dev = info->dev;
448aa276781SNishanth Menon 	struct ti_sci_handle *handle = &info->handle;
449aa276781SNishanth Menon 	struct ti_sci_version_info *ver = &handle->version;
450aa276781SNishanth Menon 	struct ti_sci_msg_resp_version *rev_info;
451aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
452aa276781SNishanth Menon 	int ret;
453aa276781SNishanth Menon 
454aa276781SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
45566f030eaSAndrew F. Davis 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
45666f030eaSAndrew F. Davis 				   sizeof(struct ti_sci_msg_hdr),
457aa276781SNishanth Menon 				   sizeof(*rev_info));
458aa276781SNishanth Menon 	if (IS_ERR(xfer)) {
459aa276781SNishanth Menon 		ret = PTR_ERR(xfer);
460aa276781SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
461aa276781SNishanth Menon 		return ret;
462aa276781SNishanth Menon 	}
463aa276781SNishanth Menon 
464aa276781SNishanth Menon 	rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
465aa276781SNishanth Menon 
466aa276781SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
467aa276781SNishanth Menon 	if (ret) {
468aa276781SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
469aa276781SNishanth Menon 		goto fail;
470aa276781SNishanth Menon 	}
471aa276781SNishanth Menon 
472aa276781SNishanth Menon 	ver->abi_major = rev_info->abi_major;
473aa276781SNishanth Menon 	ver->abi_minor = rev_info->abi_minor;
474aa276781SNishanth Menon 	ver->firmware_revision = rev_info->firmware_revision;
475aa276781SNishanth Menon 	strncpy(ver->firmware_description, rev_info->firmware_description,
476aa276781SNishanth Menon 		sizeof(ver->firmware_description));
477aa276781SNishanth Menon 
478aa276781SNishanth Menon fail:
479aa276781SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
480aa276781SNishanth Menon 	return ret;
481aa276781SNishanth Menon }
482aa276781SNishanth Menon 
483aa276781SNishanth Menon /**
4849e7d756dSNishanth Menon  * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
4859e7d756dSNishanth Menon  * @r:	pointer to response buffer
4869e7d756dSNishanth Menon  *
4879e7d756dSNishanth Menon  * Return: true if the response was an ACK, else returns false.
4889e7d756dSNishanth Menon  */
ti_sci_is_response_ack(void * r)4899e7d756dSNishanth Menon static inline bool ti_sci_is_response_ack(void *r)
4909e7d756dSNishanth Menon {
4919e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *hdr = r;
4929e7d756dSNishanth Menon 
4939e7d756dSNishanth Menon 	return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
4949e7d756dSNishanth Menon }
4959e7d756dSNishanth Menon 
4969e7d756dSNishanth Menon /**
4979e7d756dSNishanth Menon  * ti_sci_set_device_state() - Set device state helper
4989e7d756dSNishanth Menon  * @handle:	pointer to TI SCI handle
4999e7d756dSNishanth Menon  * @id:		Device identifier
5009e7d756dSNishanth Menon  * @flags:	flags to setup for the device
5019e7d756dSNishanth Menon  * @state:	State to move the device to
5029e7d756dSNishanth Menon  *
5039e7d756dSNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
5049e7d756dSNishanth Menon  */
ti_sci_set_device_state(const struct ti_sci_handle * handle,u32 id,u32 flags,u8 state)5059e7d756dSNishanth Menon static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
5069e7d756dSNishanth Menon 				   u32 id, u32 flags, u8 state)
5079e7d756dSNishanth Menon {
5089e7d756dSNishanth Menon 	struct ti_sci_info *info;
5099e7d756dSNishanth Menon 	struct ti_sci_msg_req_set_device_state *req;
5109e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *resp;
5119e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
5129e7d756dSNishanth Menon 	struct device *dev;
5139e7d756dSNishanth Menon 	int ret = 0;
5149e7d756dSNishanth Menon 
5159e7d756dSNishanth Menon 	if (IS_ERR(handle))
5169e7d756dSNishanth Menon 		return PTR_ERR(handle);
5179e7d756dSNishanth Menon 	if (!handle)
5189e7d756dSNishanth Menon 		return -EINVAL;
5199e7d756dSNishanth Menon 
5209e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
5219e7d756dSNishanth Menon 	dev = info->dev;
5229e7d756dSNishanth Menon 
5239e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
5249e7d756dSNishanth Menon 				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
5259e7d756dSNishanth Menon 				   sizeof(*req), sizeof(*resp));
5269e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
5279e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
5289e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
5299e7d756dSNishanth Menon 		return ret;
5309e7d756dSNishanth Menon 	}
5319e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
5329e7d756dSNishanth Menon 	req->id = id;
5339e7d756dSNishanth Menon 	req->state = state;
5349e7d756dSNishanth Menon 
5359e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
5369e7d756dSNishanth Menon 	if (ret) {
5379e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
5389e7d756dSNishanth Menon 		goto fail;
5399e7d756dSNishanth Menon 	}
5409e7d756dSNishanth Menon 
5419e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
5429e7d756dSNishanth Menon 
5439e7d756dSNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
5449e7d756dSNishanth Menon 
5459e7d756dSNishanth Menon fail:
5469e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
5479e7d756dSNishanth Menon 
5489e7d756dSNishanth Menon 	return ret;
5499e7d756dSNishanth Menon }
5509e7d756dSNishanth Menon 
5519e7d756dSNishanth Menon /**
5529e7d756dSNishanth Menon  * ti_sci_get_device_state() - Get device state helper
5539e7d756dSNishanth Menon  * @handle:	Handle to the device
5549e7d756dSNishanth Menon  * @id:		Device Identifier
5559e7d756dSNishanth Menon  * @clcnt:	Pointer to Context Loss Count
5569e7d756dSNishanth Menon  * @resets:	pointer to resets
5579e7d756dSNishanth Menon  * @p_state:	pointer to p_state
5589e7d756dSNishanth Menon  * @c_state:	pointer to c_state
5599e7d756dSNishanth Menon  *
5609e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
5619e7d756dSNishanth Menon  */
ti_sci_get_device_state(const struct ti_sci_handle * handle,u32 id,u32 * clcnt,u32 * resets,u8 * p_state,u8 * c_state)5629e7d756dSNishanth Menon static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
5639e7d756dSNishanth Menon 				   u32 id,  u32 *clcnt,  u32 *resets,
5649e7d756dSNishanth Menon 				    u8 *p_state,  u8 *c_state)
5659e7d756dSNishanth Menon {
5669e7d756dSNishanth Menon 	struct ti_sci_info *info;
5679e7d756dSNishanth Menon 	struct ti_sci_msg_req_get_device_state *req;
5689e7d756dSNishanth Menon 	struct ti_sci_msg_resp_get_device_state *resp;
5699e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
5709e7d756dSNishanth Menon 	struct device *dev;
5719e7d756dSNishanth Menon 	int ret = 0;
5729e7d756dSNishanth Menon 
5739e7d756dSNishanth Menon 	if (IS_ERR(handle))
5749e7d756dSNishanth Menon 		return PTR_ERR(handle);
5759e7d756dSNishanth Menon 	if (!handle)
5769e7d756dSNishanth Menon 		return -EINVAL;
5779e7d756dSNishanth Menon 
5789e7d756dSNishanth Menon 	if (!clcnt && !resets && !p_state && !c_state)
5799e7d756dSNishanth Menon 		return -EINVAL;
5809e7d756dSNishanth Menon 
5819e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
5829e7d756dSNishanth Menon 	dev = info->dev;
5839e7d756dSNishanth Menon 
5849e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
58566f030eaSAndrew F. Davis 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
58666f030eaSAndrew F. Davis 				   sizeof(*req), sizeof(*resp));
5879e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
5889e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
5899e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
5909e7d756dSNishanth Menon 		return ret;
5919e7d756dSNishanth Menon 	}
5929e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
5939e7d756dSNishanth Menon 	req->id = id;
5949e7d756dSNishanth Menon 
5959e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
5969e7d756dSNishanth Menon 	if (ret) {
5979e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
5989e7d756dSNishanth Menon 		goto fail;
5999e7d756dSNishanth Menon 	}
6009e7d756dSNishanth Menon 
6019e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
6029e7d756dSNishanth Menon 	if (!ti_sci_is_response_ack(resp)) {
6039e7d756dSNishanth Menon 		ret = -ENODEV;
6049e7d756dSNishanth Menon 		goto fail;
6059e7d756dSNishanth Menon 	}
6069e7d756dSNishanth Menon 
6079e7d756dSNishanth Menon 	if (clcnt)
6089e7d756dSNishanth Menon 		*clcnt = resp->context_loss_count;
6099e7d756dSNishanth Menon 	if (resets)
6109e7d756dSNishanth Menon 		*resets = resp->resets;
6119e7d756dSNishanth Menon 	if (p_state)
6129e7d756dSNishanth Menon 		*p_state = resp->programmed_state;
6139e7d756dSNishanth Menon 	if (c_state)
6149e7d756dSNishanth Menon 		*c_state = resp->current_state;
6159e7d756dSNishanth Menon fail:
6169e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
6179e7d756dSNishanth Menon 
6189e7d756dSNishanth Menon 	return ret;
6199e7d756dSNishanth Menon }
6209e7d756dSNishanth Menon 
6219e7d756dSNishanth Menon /**
6229e7d756dSNishanth Menon  * ti_sci_cmd_get_device() - command to request for device managed by TISCI
62345b659eeSLokesh Vutla  *			     that can be shared with other hosts.
6249e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6259e7d756dSNishanth Menon  * @id:		Device Identifier
6269e7d756dSNishanth Menon  *
6279e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
6289e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
6299e7d756dSNishanth Menon  * managed by driver for that purpose.
6309e7d756dSNishanth Menon  *
6319e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
6329e7d756dSNishanth Menon  */
ti_sci_cmd_get_device(const struct ti_sci_handle * handle,u32 id)6339e7d756dSNishanth Menon static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
6349e7d756dSNishanth Menon {
63545b659eeSLokesh Vutla 	return ti_sci_set_device_state(handle, id, 0,
63645b659eeSLokesh Vutla 				       MSG_DEVICE_SW_STATE_ON);
63745b659eeSLokesh Vutla }
63845b659eeSLokesh Vutla 
63945b659eeSLokesh Vutla /**
64045b659eeSLokesh Vutla  * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
64145b659eeSLokesh Vutla  *				       TISCI that is exclusively owned by the
64245b659eeSLokesh Vutla  *				       requesting host.
64345b659eeSLokesh Vutla  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
64445b659eeSLokesh Vutla  * @id:		Device Identifier
64545b659eeSLokesh Vutla  *
64645b659eeSLokesh Vutla  * Request for the device - NOTE: the client MUST maintain integrity of
64745b659eeSLokesh Vutla  * usage count by balancing get_device with put_device. No refcounting is
64845b659eeSLokesh Vutla  * managed by driver for that purpose.
64945b659eeSLokesh Vutla  *
65045b659eeSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
65145b659eeSLokesh Vutla  */
ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle * handle,u32 id)65245b659eeSLokesh Vutla static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
65345b659eeSLokesh Vutla 					   u32 id)
65445b659eeSLokesh Vutla {
6559e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
6569e7d756dSNishanth Menon 				       MSG_FLAG_DEVICE_EXCLUSIVE,
6579e7d756dSNishanth Menon 				       MSG_DEVICE_SW_STATE_ON);
6589e7d756dSNishanth Menon }
6599e7d756dSNishanth Menon 
6609e7d756dSNishanth Menon /**
6619e7d756dSNishanth Menon  * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
6629e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6639e7d756dSNishanth Menon  * @id:		Device Identifier
6649e7d756dSNishanth Menon  *
6659e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
6669e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
6679e7d756dSNishanth Menon  * managed by driver for that purpose.
6689e7d756dSNishanth Menon  *
6699e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
6709e7d756dSNishanth Menon  */
ti_sci_cmd_idle_device(const struct ti_sci_handle * handle,u32 id)6719e7d756dSNishanth Menon static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
6729e7d756dSNishanth Menon {
67345b659eeSLokesh Vutla 	return ti_sci_set_device_state(handle, id, 0,
67445b659eeSLokesh Vutla 				       MSG_DEVICE_SW_STATE_RETENTION);
67545b659eeSLokesh Vutla }
67645b659eeSLokesh Vutla 
67745b659eeSLokesh Vutla /**
67845b659eeSLokesh Vutla  * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
67945b659eeSLokesh Vutla  *					TISCI that is exclusively owned by
68045b659eeSLokesh Vutla  *					requesting host.
68145b659eeSLokesh Vutla  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
68245b659eeSLokesh Vutla  * @id:		Device Identifier
68345b659eeSLokesh Vutla  *
68445b659eeSLokesh Vutla  * Request for the device - NOTE: the client MUST maintain integrity of
68545b659eeSLokesh Vutla  * usage count by balancing get_device with put_device. No refcounting is
68645b659eeSLokesh Vutla  * managed by driver for that purpose.
68745b659eeSLokesh Vutla  *
68845b659eeSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
68945b659eeSLokesh Vutla  */
ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle * handle,u32 id)69045b659eeSLokesh Vutla static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
69145b659eeSLokesh Vutla 					    u32 id)
69245b659eeSLokesh Vutla {
6939e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
6949e7d756dSNishanth Menon 				       MSG_FLAG_DEVICE_EXCLUSIVE,
6959e7d756dSNishanth Menon 				       MSG_DEVICE_SW_STATE_RETENTION);
6969e7d756dSNishanth Menon }
6979e7d756dSNishanth Menon 
6989e7d756dSNishanth Menon /**
6999e7d756dSNishanth Menon  * ti_sci_cmd_put_device() - command to release a device managed by TISCI
7009e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
7019e7d756dSNishanth Menon  * @id:		Device Identifier
7029e7d756dSNishanth Menon  *
7039e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
7049e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
7059e7d756dSNishanth Menon  * managed by driver for that purpose.
7069e7d756dSNishanth Menon  *
7079e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7089e7d756dSNishanth Menon  */
ti_sci_cmd_put_device(const struct ti_sci_handle * handle,u32 id)7099e7d756dSNishanth Menon static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
7109e7d756dSNishanth Menon {
7119e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
7129e7d756dSNishanth Menon 				       0, MSG_DEVICE_SW_STATE_AUTO_OFF);
7139e7d756dSNishanth Menon }
7149e7d756dSNishanth Menon 
7159e7d756dSNishanth Menon /**
7169e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_valid() - Is the device valid
7179e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
7189e7d756dSNishanth Menon  * @id:		Device Identifier
7199e7d756dSNishanth Menon  *
7209e7d756dSNishanth Menon  * Return: 0 if all went fine and the device ID is valid, else return
7219e7d756dSNishanth Menon  * appropriate error.
7229e7d756dSNishanth Menon  */
ti_sci_cmd_dev_is_valid(const struct ti_sci_handle * handle,u32 id)7239e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
7249e7d756dSNishanth Menon {
7259e7d756dSNishanth Menon 	u8 unused;
7269e7d756dSNishanth Menon 
7279e7d756dSNishanth Menon 	/* check the device state which will also tell us if the ID is valid */
7289e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
7299e7d756dSNishanth Menon }
7309e7d756dSNishanth Menon 
7319e7d756dSNishanth Menon /**
7329e7d756dSNishanth Menon  * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
7339e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7349e7d756dSNishanth Menon  * @id:		Device Identifier
7359e7d756dSNishanth Menon  * @count:	Pointer to Context Loss counter to populate
7369e7d756dSNishanth Menon  *
7379e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7389e7d756dSNishanth Menon  */
ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle * handle,u32 id,u32 * count)7399e7d756dSNishanth Menon static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
7409e7d756dSNishanth Menon 				    u32 *count)
7419e7d756dSNishanth Menon {
7429e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
7439e7d756dSNishanth Menon }
7449e7d756dSNishanth Menon 
7459e7d756dSNishanth Menon /**
7469e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
7479e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7489e7d756dSNishanth Menon  * @id:		Device Identifier
7499e7d756dSNishanth Menon  * @r_state:	true if requested to be idle
7509e7d756dSNishanth Menon  *
7519e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7529e7d756dSNishanth Menon  */
ti_sci_cmd_dev_is_idle(const struct ti_sci_handle * handle,u32 id,bool * r_state)7539e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
7549e7d756dSNishanth Menon 				  bool *r_state)
7559e7d756dSNishanth Menon {
7569e7d756dSNishanth Menon 	int ret;
7579e7d756dSNishanth Menon 	u8 state;
7589e7d756dSNishanth Menon 
7599e7d756dSNishanth Menon 	if (!r_state)
7609e7d756dSNishanth Menon 		return -EINVAL;
7619e7d756dSNishanth Menon 
7629e7d756dSNishanth Menon 	ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
7639e7d756dSNishanth Menon 	if (ret)
7649e7d756dSNishanth Menon 		return ret;
7659e7d756dSNishanth Menon 
7669e7d756dSNishanth Menon 	*r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
7679e7d756dSNishanth Menon 
7689e7d756dSNishanth Menon 	return 0;
7699e7d756dSNishanth Menon }
7709e7d756dSNishanth Menon 
7719e7d756dSNishanth Menon /**
7729e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
7739e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7749e7d756dSNishanth Menon  * @id:		Device Identifier
7759e7d756dSNishanth Menon  * @r_state:	true if requested to be stopped
7769e7d756dSNishanth Menon  * @curr_state:	true if currently stopped.
7779e7d756dSNishanth Menon  *
7789e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7799e7d756dSNishanth Menon  */
ti_sci_cmd_dev_is_stop(const struct ti_sci_handle * handle,u32 id,bool * r_state,bool * curr_state)7809e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
7819e7d756dSNishanth Menon 				  bool *r_state,  bool *curr_state)
7829e7d756dSNishanth Menon {
7839e7d756dSNishanth Menon 	int ret;
7849e7d756dSNishanth Menon 	u8 p_state, c_state;
7859e7d756dSNishanth Menon 
7869e7d756dSNishanth Menon 	if (!r_state && !curr_state)
7879e7d756dSNishanth Menon 		return -EINVAL;
7889e7d756dSNishanth Menon 
7899e7d756dSNishanth Menon 	ret =
7909e7d756dSNishanth Menon 	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
7919e7d756dSNishanth Menon 	if (ret)
7929e7d756dSNishanth Menon 		return ret;
7939e7d756dSNishanth Menon 
7949e7d756dSNishanth Menon 	if (r_state)
7959e7d756dSNishanth Menon 		*r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
7969e7d756dSNishanth Menon 	if (curr_state)
7979e7d756dSNishanth Menon 		*curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
7989e7d756dSNishanth Menon 
7999e7d756dSNishanth Menon 	return 0;
8009e7d756dSNishanth Menon }
8019e7d756dSNishanth Menon 
8029e7d756dSNishanth Menon /**
8039e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
8049e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
8059e7d756dSNishanth Menon  * @id:		Device Identifier
8069e7d756dSNishanth Menon  * @r_state:	true if requested to be ON
8079e7d756dSNishanth Menon  * @curr_state:	true if currently ON and active
8089e7d756dSNishanth Menon  *
8099e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8109e7d756dSNishanth Menon  */
ti_sci_cmd_dev_is_on(const struct ti_sci_handle * handle,u32 id,bool * r_state,bool * curr_state)8119e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
8129e7d756dSNishanth Menon 				bool *r_state,  bool *curr_state)
8139e7d756dSNishanth Menon {
8149e7d756dSNishanth Menon 	int ret;
8159e7d756dSNishanth Menon 	u8 p_state, c_state;
8169e7d756dSNishanth Menon 
8179e7d756dSNishanth Menon 	if (!r_state && !curr_state)
8189e7d756dSNishanth Menon 		return -EINVAL;
8199e7d756dSNishanth Menon 
8209e7d756dSNishanth Menon 	ret =
8219e7d756dSNishanth Menon 	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
8229e7d756dSNishanth Menon 	if (ret)
8239e7d756dSNishanth Menon 		return ret;
8249e7d756dSNishanth Menon 
8259e7d756dSNishanth Menon 	if (r_state)
8269e7d756dSNishanth Menon 		*r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
8279e7d756dSNishanth Menon 	if (curr_state)
8289e7d756dSNishanth Menon 		*curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
8299e7d756dSNishanth Menon 
8309e7d756dSNishanth Menon 	return 0;
8319e7d756dSNishanth Menon }
8329e7d756dSNishanth Menon 
8339e7d756dSNishanth Menon /**
8349e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
8359e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
8369e7d756dSNishanth Menon  * @id:		Device Identifier
8379e7d756dSNishanth Menon  * @curr_state:	true if currently transitioning.
8389e7d756dSNishanth Menon  *
8399e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8409e7d756dSNishanth Menon  */
ti_sci_cmd_dev_is_trans(const struct ti_sci_handle * handle,u32 id,bool * curr_state)8419e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
8429e7d756dSNishanth Menon 				   bool *curr_state)
8439e7d756dSNishanth Menon {
8449e7d756dSNishanth Menon 	int ret;
8459e7d756dSNishanth Menon 	u8 state;
8469e7d756dSNishanth Menon 
8479e7d756dSNishanth Menon 	if (!curr_state)
8489e7d756dSNishanth Menon 		return -EINVAL;
8499e7d756dSNishanth Menon 
8509e7d756dSNishanth Menon 	ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
8519e7d756dSNishanth Menon 	if (ret)
8529e7d756dSNishanth Menon 		return ret;
8539e7d756dSNishanth Menon 
8549e7d756dSNishanth Menon 	*curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
8559e7d756dSNishanth Menon 
8569e7d756dSNishanth Menon 	return 0;
8579e7d756dSNishanth Menon }
8589e7d756dSNishanth Menon 
8599e7d756dSNishanth Menon /**
8609e7d756dSNishanth Menon  * ti_sci_cmd_set_device_resets() - command to set resets for device managed
8619e7d756dSNishanth Menon  *				    by TISCI
8629e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
8639e7d756dSNishanth Menon  * @id:		Device Identifier
8649e7d756dSNishanth Menon  * @reset_state: Device specific reset bit field
8659e7d756dSNishanth Menon  *
8669e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8679e7d756dSNishanth Menon  */
ti_sci_cmd_set_device_resets(const struct ti_sci_handle * handle,u32 id,u32 reset_state)8689e7d756dSNishanth Menon static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
8699e7d756dSNishanth Menon 					u32 id, u32 reset_state)
8709e7d756dSNishanth Menon {
8719e7d756dSNishanth Menon 	struct ti_sci_info *info;
8729e7d756dSNishanth Menon 	struct ti_sci_msg_req_set_device_resets *req;
8739e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *resp;
8749e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
8759e7d756dSNishanth Menon 	struct device *dev;
8769e7d756dSNishanth Menon 	int ret = 0;
8779e7d756dSNishanth Menon 
8789e7d756dSNishanth Menon 	if (IS_ERR(handle))
8799e7d756dSNishanth Menon 		return PTR_ERR(handle);
8809e7d756dSNishanth Menon 	if (!handle)
8819e7d756dSNishanth Menon 		return -EINVAL;
8829e7d756dSNishanth Menon 
8839e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
8849e7d756dSNishanth Menon 	dev = info->dev;
8859e7d756dSNishanth Menon 
8869e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
8879e7d756dSNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
8889e7d756dSNishanth Menon 				   sizeof(*req), sizeof(*resp));
8899e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
8909e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
8919e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
8929e7d756dSNishanth Menon 		return ret;
8939e7d756dSNishanth Menon 	}
8949e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
8959e7d756dSNishanth Menon 	req->id = id;
8969e7d756dSNishanth Menon 	req->resets = reset_state;
8979e7d756dSNishanth Menon 
8989e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
8999e7d756dSNishanth Menon 	if (ret) {
9009e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
9019e7d756dSNishanth Menon 		goto fail;
9029e7d756dSNishanth Menon 	}
9039e7d756dSNishanth Menon 
9049e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
9059e7d756dSNishanth Menon 
9069e7d756dSNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
9079e7d756dSNishanth Menon 
9089e7d756dSNishanth Menon fail:
9099e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
9109e7d756dSNishanth Menon 
9119e7d756dSNishanth Menon 	return ret;
9129e7d756dSNishanth Menon }
9139e7d756dSNishanth Menon 
9149e7d756dSNishanth Menon /**
9159e7d756dSNishanth Menon  * ti_sci_cmd_get_device_resets() - Get reset state for device managed
9169e7d756dSNishanth Menon  *				    by TISCI
9179e7d756dSNishanth Menon  * @handle:		Pointer to TISCI handle
9189e7d756dSNishanth Menon  * @id:			Device Identifier
9199e7d756dSNishanth Menon  * @reset_state:	Pointer to reset state to populate
9209e7d756dSNishanth Menon  *
9219e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
9229e7d756dSNishanth Menon  */
ti_sci_cmd_get_device_resets(const struct ti_sci_handle * handle,u32 id,u32 * reset_state)9239e7d756dSNishanth Menon static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
9249e7d756dSNishanth Menon 					u32 id, u32 *reset_state)
9259e7d756dSNishanth Menon {
9269e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
9279e7d756dSNishanth Menon 				       NULL);
9289e7d756dSNishanth Menon }
9299e7d756dSNishanth Menon 
9309f723220SNishanth Menon /**
9319f723220SNishanth Menon  * ti_sci_set_clock_state() - Set clock state helper
9329f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
9339f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
9349f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
9359f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
9369f723220SNishanth Menon  *		which clock input to modify.
9379f723220SNishanth Menon  * @flags:	Header flags as needed
9389f723220SNishanth Menon  * @state:	State to request for the clock.
9399f723220SNishanth Menon  *
9409f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
9419f723220SNishanth Menon  */
ti_sci_set_clock_state(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 flags,u8 state)9429f723220SNishanth Menon static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
94381f4458cSTero Kristo 				  u32 dev_id, u32 clk_id,
9449f723220SNishanth Menon 				  u32 flags, u8 state)
9459f723220SNishanth Menon {
9469f723220SNishanth Menon 	struct ti_sci_info *info;
9479f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_state *req;
9489f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
9499f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
9509f723220SNishanth Menon 	struct device *dev;
9519f723220SNishanth Menon 	int ret = 0;
9529f723220SNishanth Menon 
9539f723220SNishanth Menon 	if (IS_ERR(handle))
9549f723220SNishanth Menon 		return PTR_ERR(handle);
9559f723220SNishanth Menon 	if (!handle)
9569f723220SNishanth Menon 		return -EINVAL;
9579f723220SNishanth Menon 
9589f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
9599f723220SNishanth Menon 	dev = info->dev;
9609f723220SNishanth Menon 
9619f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
9629f723220SNishanth Menon 				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
9639f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
9649f723220SNishanth Menon 	if (IS_ERR(xfer)) {
9659f723220SNishanth Menon 		ret = PTR_ERR(xfer);
9669f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
9679f723220SNishanth Menon 		return ret;
9689f723220SNishanth Menon 	}
9699f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
9709f723220SNishanth Menon 	req->dev_id = dev_id;
97181f4458cSTero Kristo 	if (clk_id < 255) {
9729f723220SNishanth Menon 		req->clk_id = clk_id;
97381f4458cSTero Kristo 	} else {
97481f4458cSTero Kristo 		req->clk_id = 255;
97581f4458cSTero Kristo 		req->clk_id_32 = clk_id;
97681f4458cSTero Kristo 	}
9779f723220SNishanth Menon 	req->request_state = state;
9789f723220SNishanth Menon 
9799f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
9809f723220SNishanth Menon 	if (ret) {
9819f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
9829f723220SNishanth Menon 		goto fail;
9839f723220SNishanth Menon 	}
9849f723220SNishanth Menon 
9859f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
9869f723220SNishanth Menon 
9879f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
9889f723220SNishanth Menon 
9899f723220SNishanth Menon fail:
9909f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
9919f723220SNishanth Menon 
9929f723220SNishanth Menon 	return ret;
9939f723220SNishanth Menon }
9949f723220SNishanth Menon 
9959f723220SNishanth Menon /**
9969f723220SNishanth Menon  * ti_sci_cmd_get_clock_state() - Get clock state helper
9979f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
9989f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
9999f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
10009f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
10019f723220SNishanth Menon  *		which clock input to modify.
10029f723220SNishanth Menon  * @programmed_state:	State requested for clock to move to
10039f723220SNishanth Menon  * @current_state:	State that the clock is currently in
10049f723220SNishanth Menon  *
10059f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
10069f723220SNishanth Menon  */
ti_sci_cmd_get_clock_state(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u8 * programmed_state,u8 * current_state)10079f723220SNishanth Menon static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
100881f4458cSTero Kristo 				      u32 dev_id, u32 clk_id,
10099f723220SNishanth Menon 				      u8 *programmed_state, u8 *current_state)
10109f723220SNishanth Menon {
10119f723220SNishanth Menon 	struct ti_sci_info *info;
10129f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_state *req;
10139f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_state *resp;
10149f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
10159f723220SNishanth Menon 	struct device *dev;
10169f723220SNishanth Menon 	int ret = 0;
10179f723220SNishanth Menon 
10189f723220SNishanth Menon 	if (IS_ERR(handle))
10199f723220SNishanth Menon 		return PTR_ERR(handle);
10209f723220SNishanth Menon 	if (!handle)
10219f723220SNishanth Menon 		return -EINVAL;
10229f723220SNishanth Menon 
10239f723220SNishanth Menon 	if (!programmed_state && !current_state)
10249f723220SNishanth Menon 		return -EINVAL;
10259f723220SNishanth Menon 
10269f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
10279f723220SNishanth Menon 	dev = info->dev;
10289f723220SNishanth Menon 
10299f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
10309f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
10319f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
10329f723220SNishanth Menon 	if (IS_ERR(xfer)) {
10339f723220SNishanth Menon 		ret = PTR_ERR(xfer);
10349f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
10359f723220SNishanth Menon 		return ret;
10369f723220SNishanth Menon 	}
10379f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
10389f723220SNishanth Menon 	req->dev_id = dev_id;
103981f4458cSTero Kristo 	if (clk_id < 255) {
10409f723220SNishanth Menon 		req->clk_id = clk_id;
104181f4458cSTero Kristo 	} else {
104281f4458cSTero Kristo 		req->clk_id = 255;
104381f4458cSTero Kristo 		req->clk_id_32 = clk_id;
104481f4458cSTero Kristo 	}
10459f723220SNishanth Menon 
10469f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
10479f723220SNishanth Menon 	if (ret) {
10489f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
10499f723220SNishanth Menon 		goto fail;
10509f723220SNishanth Menon 	}
10519f723220SNishanth Menon 
10529f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
10539f723220SNishanth Menon 
10549f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp)) {
10559f723220SNishanth Menon 		ret = -ENODEV;
10569f723220SNishanth Menon 		goto fail;
10579f723220SNishanth Menon 	}
10589f723220SNishanth Menon 
10599f723220SNishanth Menon 	if (programmed_state)
10609f723220SNishanth Menon 		*programmed_state = resp->programmed_state;
10619f723220SNishanth Menon 	if (current_state)
10629f723220SNishanth Menon 		*current_state = resp->current_state;
10639f723220SNishanth Menon 
10649f723220SNishanth Menon fail:
10659f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
10669f723220SNishanth Menon 
10679f723220SNishanth Menon 	return ret;
10689f723220SNishanth Menon }
10699f723220SNishanth Menon 
10709f723220SNishanth Menon /**
10719f723220SNishanth Menon  * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
10729f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
10739f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
10749f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
10759f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
10769f723220SNishanth Menon  *		which clock input to modify.
10779f723220SNishanth Menon  * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
10789f723220SNishanth Menon  * @can_change_freq: 'true' if frequency change is desired, else 'false'
10799f723220SNishanth Menon  * @enable_input_term: 'true' if input termination is desired, else 'false'
10809f723220SNishanth Menon  *
10819f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
10829f723220SNishanth Menon  */
ti_sci_cmd_get_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool needs_ssc,bool can_change_freq,bool enable_input_term)10839f723220SNishanth Menon static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
108481f4458cSTero Kristo 				u32 clk_id, bool needs_ssc,
108581f4458cSTero Kristo 				bool can_change_freq, bool enable_input_term)
10869f723220SNishanth Menon {
10879f723220SNishanth Menon 	u32 flags = 0;
10889f723220SNishanth Menon 
10899f723220SNishanth Menon 	flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
10909f723220SNishanth Menon 	flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
10919f723220SNishanth Menon 	flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
10929f723220SNishanth Menon 
10939f723220SNishanth Menon 	return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
10949f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_REQ);
10959f723220SNishanth Menon }
10969f723220SNishanth Menon 
10979f723220SNishanth Menon /**
10989f723220SNishanth Menon  * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
10999f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11009f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11019f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11029f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11039f723220SNishanth Menon  *		which clock input to modify.
11049f723220SNishanth Menon  *
11059f723220SNishanth Menon  * NOTE: This clock must have been requested by get_clock previously.
11069f723220SNishanth Menon  *
11079f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11089f723220SNishanth Menon  */
ti_sci_cmd_idle_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id)11099f723220SNishanth Menon static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
111081f4458cSTero Kristo 				 u32 dev_id, u32 clk_id)
11119f723220SNishanth Menon {
111271b61082STero Kristo 	return ti_sci_set_clock_state(handle, dev_id, clk_id,
111371b61082STero Kristo 				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
11149f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_UNREQ);
11159f723220SNishanth Menon }
11169f723220SNishanth Menon 
11179f723220SNishanth Menon /**
11189f723220SNishanth Menon  * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
11199f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11209f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11219f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11229f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11239f723220SNishanth Menon  *		which clock input to modify.
11249f723220SNishanth Menon  *
11259f723220SNishanth Menon  * NOTE: This clock must have been requested by get_clock previously.
11269f723220SNishanth Menon  *
11279f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11289f723220SNishanth Menon  */
ti_sci_cmd_put_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id)11299f723220SNishanth Menon static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
113081f4458cSTero Kristo 				u32 dev_id, u32 clk_id)
11319f723220SNishanth Menon {
113271b61082STero Kristo 	return ti_sci_set_clock_state(handle, dev_id, clk_id,
113371b61082STero Kristo 				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
11349f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_AUTO);
11359f723220SNishanth Menon }
11369f723220SNishanth Menon 
11379f723220SNishanth Menon /**
11389f723220SNishanth Menon  * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
11399f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11409f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11419f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11429f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11439f723220SNishanth Menon  *		which clock input to modify.
11449f723220SNishanth Menon  * @req_state: state indicating if the clock is auto managed
11459f723220SNishanth Menon  *
11469f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11479f723220SNishanth Menon  */
ti_sci_cmd_clk_is_auto(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state)11489f723220SNishanth Menon static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
114981f4458cSTero Kristo 				  u32 dev_id, u32 clk_id, bool *req_state)
11509f723220SNishanth Menon {
11519f723220SNishanth Menon 	u8 state = 0;
11529f723220SNishanth Menon 	int ret;
11539f723220SNishanth Menon 
11549f723220SNishanth Menon 	if (!req_state)
11559f723220SNishanth Menon 		return -EINVAL;
11569f723220SNishanth Menon 
11579f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
11589f723220SNishanth Menon 	if (ret)
11599f723220SNishanth Menon 		return ret;
11609f723220SNishanth Menon 
11619f723220SNishanth Menon 	*req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
11629f723220SNishanth Menon 	return 0;
11639f723220SNishanth Menon }
11649f723220SNishanth Menon 
11659f723220SNishanth Menon /**
11669f723220SNishanth Menon  * ti_sci_cmd_clk_is_on() - Is the clock ON
11679f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11689f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11699f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11709f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11719f723220SNishanth Menon  *		which clock input to modify.
11729f723220SNishanth Menon  * @req_state: state indicating if the clock is managed by us and enabled
11739f723220SNishanth Menon  * @curr_state: state indicating if the clock is ready for operation
11749f723220SNishanth Menon  *
11759f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11769f723220SNishanth Menon  */
ti_sci_cmd_clk_is_on(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state,bool * curr_state)11779f723220SNishanth Menon static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
117881f4458cSTero Kristo 				u32 clk_id, bool *req_state, bool *curr_state)
11799f723220SNishanth Menon {
11809f723220SNishanth Menon 	u8 c_state = 0, r_state = 0;
11819f723220SNishanth Menon 	int ret;
11829f723220SNishanth Menon 
11839f723220SNishanth Menon 	if (!req_state && !curr_state)
11849f723220SNishanth Menon 		return -EINVAL;
11859f723220SNishanth Menon 
11869f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
11879f723220SNishanth Menon 					 &r_state, &c_state);
11889f723220SNishanth Menon 	if (ret)
11899f723220SNishanth Menon 		return ret;
11909f723220SNishanth Menon 
11919f723220SNishanth Menon 	if (req_state)
11929f723220SNishanth Menon 		*req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
11939f723220SNishanth Menon 	if (curr_state)
11949f723220SNishanth Menon 		*curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
11959f723220SNishanth Menon 	return 0;
11969f723220SNishanth Menon }
11979f723220SNishanth Menon 
11989f723220SNishanth Menon /**
11999f723220SNishanth Menon  * ti_sci_cmd_clk_is_off() - Is the clock OFF
12009f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
12019f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
12029f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
12039f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
12049f723220SNishanth Menon  *		which clock input to modify.
12059f723220SNishanth Menon  * @req_state: state indicating if the clock is managed by us and disabled
12069f723220SNishanth Menon  * @curr_state: state indicating if the clock is NOT ready for operation
12079f723220SNishanth Menon  *
12089f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
12099f723220SNishanth Menon  */
ti_sci_cmd_clk_is_off(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state,bool * curr_state)12109f723220SNishanth Menon static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
121181f4458cSTero Kristo 				 u32 clk_id, bool *req_state, bool *curr_state)
12129f723220SNishanth Menon {
12139f723220SNishanth Menon 	u8 c_state = 0, r_state = 0;
12149f723220SNishanth Menon 	int ret;
12159f723220SNishanth Menon 
12169f723220SNishanth Menon 	if (!req_state && !curr_state)
12179f723220SNishanth Menon 		return -EINVAL;
12189f723220SNishanth Menon 
12199f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
12209f723220SNishanth Menon 					 &r_state, &c_state);
12219f723220SNishanth Menon 	if (ret)
12229f723220SNishanth Menon 		return ret;
12239f723220SNishanth Menon 
12249f723220SNishanth Menon 	if (req_state)
12259f723220SNishanth Menon 		*req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
12269f723220SNishanth Menon 	if (curr_state)
12279f723220SNishanth Menon 		*curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
12289f723220SNishanth Menon 	return 0;
12299f723220SNishanth Menon }
12309f723220SNishanth Menon 
12319f723220SNishanth Menon /**
12329f723220SNishanth Menon  * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
12339f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
12349f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
12359f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
12369f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
12379f723220SNishanth Menon  *		which clock input to modify.
12389f723220SNishanth Menon  * @parent_id:	Parent clock identifier to set
12399f723220SNishanth Menon  *
12409f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
12419f723220SNishanth Menon  */
ti_sci_cmd_clk_set_parent(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 parent_id)12429f723220SNishanth Menon static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
124381f4458cSTero Kristo 				     u32 dev_id, u32 clk_id, u32 parent_id)
12449f723220SNishanth Menon {
12459f723220SNishanth Menon 	struct ti_sci_info *info;
12469f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_parent *req;
12479f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
12489f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
12499f723220SNishanth Menon 	struct device *dev;
12509f723220SNishanth Menon 	int ret = 0;
12519f723220SNishanth Menon 
12529f723220SNishanth Menon 	if (IS_ERR(handle))
12539f723220SNishanth Menon 		return PTR_ERR(handle);
12549f723220SNishanth Menon 	if (!handle)
12559f723220SNishanth Menon 		return -EINVAL;
12569f723220SNishanth Menon 
12579f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
12589f723220SNishanth Menon 	dev = info->dev;
12599f723220SNishanth Menon 
12609f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
12619f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
12629f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
12639f723220SNishanth Menon 	if (IS_ERR(xfer)) {
12649f723220SNishanth Menon 		ret = PTR_ERR(xfer);
12659f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
12669f723220SNishanth Menon 		return ret;
12679f723220SNishanth Menon 	}
12689f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
12699f723220SNishanth Menon 	req->dev_id = dev_id;
127081f4458cSTero Kristo 	if (clk_id < 255) {
12719f723220SNishanth Menon 		req->clk_id = clk_id;
127281f4458cSTero Kristo 	} else {
127381f4458cSTero Kristo 		req->clk_id = 255;
127481f4458cSTero Kristo 		req->clk_id_32 = clk_id;
127581f4458cSTero Kristo 	}
127681f4458cSTero Kristo 	if (parent_id < 255) {
12779f723220SNishanth Menon 		req->parent_id = parent_id;
127881f4458cSTero Kristo 	} else {
127981f4458cSTero Kristo 		req->parent_id = 255;
128081f4458cSTero Kristo 		req->parent_id_32 = parent_id;
128181f4458cSTero Kristo 	}
12829f723220SNishanth Menon 
12839f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
12849f723220SNishanth Menon 	if (ret) {
12859f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
12869f723220SNishanth Menon 		goto fail;
12879f723220SNishanth Menon 	}
12889f723220SNishanth Menon 
12899f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
12909f723220SNishanth Menon 
12919f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
12929f723220SNishanth Menon 
12939f723220SNishanth Menon fail:
12949f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
12959f723220SNishanth Menon 
12969f723220SNishanth Menon 	return ret;
12979f723220SNishanth Menon }
12989f723220SNishanth Menon 
12999f723220SNishanth Menon /**
13009f723220SNishanth Menon  * ti_sci_cmd_clk_get_parent() - Get current parent clock source
13019f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
13029f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
13039f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
13049f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
13059f723220SNishanth Menon  *		which clock input to modify.
13069f723220SNishanth Menon  * @parent_id:	Current clock parent
13079f723220SNishanth Menon  *
13089f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
13099f723220SNishanth Menon  */
ti_sci_cmd_clk_get_parent(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 * parent_id)13109f723220SNishanth Menon static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
131181f4458cSTero Kristo 				     u32 dev_id, u32 clk_id, u32 *parent_id)
13129f723220SNishanth Menon {
13139f723220SNishanth Menon 	struct ti_sci_info *info;
13149f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_parent *req;
13159f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_parent *resp;
13169f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
13179f723220SNishanth Menon 	struct device *dev;
13189f723220SNishanth Menon 	int ret = 0;
13199f723220SNishanth Menon 
13209f723220SNishanth Menon 	if (IS_ERR(handle))
13219f723220SNishanth Menon 		return PTR_ERR(handle);
13229f723220SNishanth Menon 	if (!handle || !parent_id)
13239f723220SNishanth Menon 		return -EINVAL;
13249f723220SNishanth Menon 
13259f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
13269f723220SNishanth Menon 	dev = info->dev;
13279f723220SNishanth Menon 
13289f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
13299f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
13309f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
13319f723220SNishanth Menon 	if (IS_ERR(xfer)) {
13329f723220SNishanth Menon 		ret = PTR_ERR(xfer);
13339f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
13349f723220SNishanth Menon 		return ret;
13359f723220SNishanth Menon 	}
13369f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
13379f723220SNishanth Menon 	req->dev_id = dev_id;
133881f4458cSTero Kristo 	if (clk_id < 255) {
13399f723220SNishanth Menon 		req->clk_id = clk_id;
134081f4458cSTero Kristo 	} else {
134181f4458cSTero Kristo 		req->clk_id = 255;
134281f4458cSTero Kristo 		req->clk_id_32 = clk_id;
134381f4458cSTero Kristo 	}
13449f723220SNishanth Menon 
13459f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
13469f723220SNishanth Menon 	if (ret) {
13479f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
13489f723220SNishanth Menon 		goto fail;
13499f723220SNishanth Menon 	}
13509f723220SNishanth Menon 
13519f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
13529f723220SNishanth Menon 
135381f4458cSTero Kristo 	if (!ti_sci_is_response_ack(resp)) {
13549f723220SNishanth Menon 		ret = -ENODEV;
135581f4458cSTero Kristo 	} else {
135681f4458cSTero Kristo 		if (resp->parent_id < 255)
13579f723220SNishanth Menon 			*parent_id = resp->parent_id;
135881f4458cSTero Kristo 		else
135981f4458cSTero Kristo 			*parent_id = resp->parent_id_32;
136081f4458cSTero Kristo 	}
13619f723220SNishanth Menon 
13629f723220SNishanth Menon fail:
13639f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
13649f723220SNishanth Menon 
13659f723220SNishanth Menon 	return ret;
13669f723220SNishanth Menon }
13679f723220SNishanth Menon 
13689f723220SNishanth Menon /**
13699f723220SNishanth Menon  * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
13709f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
13719f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
13729f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
13739f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
13749f723220SNishanth Menon  *		which clock input to modify.
13759f723220SNishanth Menon  * @num_parents: Returns he number of parents to the current clock.
13769f723220SNishanth Menon  *
13779f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
13789f723220SNishanth Menon  */
ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 * num_parents)13799f723220SNishanth Menon static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
138081f4458cSTero Kristo 					  u32 dev_id, u32 clk_id,
138181f4458cSTero Kristo 					  u32 *num_parents)
13829f723220SNishanth Menon {
13839f723220SNishanth Menon 	struct ti_sci_info *info;
13849f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_num_parents *req;
13859f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_num_parents *resp;
13869f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
13879f723220SNishanth Menon 	struct device *dev;
13889f723220SNishanth Menon 	int ret = 0;
13899f723220SNishanth Menon 
13909f723220SNishanth Menon 	if (IS_ERR(handle))
13919f723220SNishanth Menon 		return PTR_ERR(handle);
13929f723220SNishanth Menon 	if (!handle || !num_parents)
13939f723220SNishanth Menon 		return -EINVAL;
13949f723220SNishanth Menon 
13959f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
13969f723220SNishanth Menon 	dev = info->dev;
13979f723220SNishanth Menon 
13989f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
13999f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
14009f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
14019f723220SNishanth Menon 	if (IS_ERR(xfer)) {
14029f723220SNishanth Menon 		ret = PTR_ERR(xfer);
14039f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
14049f723220SNishanth Menon 		return ret;
14059f723220SNishanth Menon 	}
14069f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
14079f723220SNishanth Menon 	req->dev_id = dev_id;
140881f4458cSTero Kristo 	if (clk_id < 255) {
14099f723220SNishanth Menon 		req->clk_id = clk_id;
141081f4458cSTero Kristo 	} else {
141181f4458cSTero Kristo 		req->clk_id = 255;
141281f4458cSTero Kristo 		req->clk_id_32 = clk_id;
141381f4458cSTero Kristo 	}
14149f723220SNishanth Menon 
14159f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
14169f723220SNishanth Menon 	if (ret) {
14179f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
14189f723220SNishanth Menon 		goto fail;
14199f723220SNishanth Menon 	}
14209f723220SNishanth Menon 
14219f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
14229f723220SNishanth Menon 
142381f4458cSTero Kristo 	if (!ti_sci_is_response_ack(resp)) {
14249f723220SNishanth Menon 		ret = -ENODEV;
142581f4458cSTero Kristo 	} else {
142681f4458cSTero Kristo 		if (resp->num_parents < 255)
14279f723220SNishanth Menon 			*num_parents = resp->num_parents;
142881f4458cSTero Kristo 		else
142981f4458cSTero Kristo 			*num_parents = resp->num_parents_32;
143081f4458cSTero Kristo 	}
14319f723220SNishanth Menon 
14329f723220SNishanth Menon fail:
14339f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
14349f723220SNishanth Menon 
14359f723220SNishanth Menon 	return ret;
14369f723220SNishanth Menon }
14379f723220SNishanth Menon 
14389f723220SNishanth Menon /**
14399f723220SNishanth Menon  * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
14409f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
14419f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
14429f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
14439f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
14449f723220SNishanth Menon  *		which clock input to modify.
14459f723220SNishanth Menon  * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
14469f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
14479f723220SNishanth Menon  *		tolerances and jitter.
14489f723220SNishanth Menon  * @target_freq: The target clock frequency in Hz. A frequency will be
14499f723220SNishanth Menon  *		processed as close to this target frequency as possible.
14509f723220SNishanth Menon  * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
14519f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
14529f723220SNishanth Menon  *		tolerances and jitter.
14539f723220SNishanth Menon  * @match_freq:	Frequency match in Hz response.
14549f723220SNishanth Menon  *
14559f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
14569f723220SNishanth Menon  */
ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 min_freq,u64 target_freq,u64 max_freq,u64 * match_freq)14579f723220SNishanth Menon static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
145881f4458cSTero Kristo 					 u32 dev_id, u32 clk_id, u64 min_freq,
14599f723220SNishanth Menon 					 u64 target_freq, u64 max_freq,
14609f723220SNishanth Menon 					 u64 *match_freq)
14619f723220SNishanth Menon {
14629f723220SNishanth Menon 	struct ti_sci_info *info;
14639f723220SNishanth Menon 	struct ti_sci_msg_req_query_clock_freq *req;
14649f723220SNishanth Menon 	struct ti_sci_msg_resp_query_clock_freq *resp;
14659f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
14669f723220SNishanth Menon 	struct device *dev;
14679f723220SNishanth Menon 	int ret = 0;
14689f723220SNishanth Menon 
14699f723220SNishanth Menon 	if (IS_ERR(handle))
14709f723220SNishanth Menon 		return PTR_ERR(handle);
14719f723220SNishanth Menon 	if (!handle || !match_freq)
14729f723220SNishanth Menon 		return -EINVAL;
14739f723220SNishanth Menon 
14749f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
14759f723220SNishanth Menon 	dev = info->dev;
14769f723220SNishanth Menon 
14779f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
14789f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
14799f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
14809f723220SNishanth Menon 	if (IS_ERR(xfer)) {
14819f723220SNishanth Menon 		ret = PTR_ERR(xfer);
14829f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
14839f723220SNishanth Menon 		return ret;
14849f723220SNishanth Menon 	}
14859f723220SNishanth Menon 	req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
14869f723220SNishanth Menon 	req->dev_id = dev_id;
148781f4458cSTero Kristo 	if (clk_id < 255) {
14889f723220SNishanth Menon 		req->clk_id = clk_id;
148981f4458cSTero Kristo 	} else {
149081f4458cSTero Kristo 		req->clk_id = 255;
149181f4458cSTero Kristo 		req->clk_id_32 = clk_id;
149281f4458cSTero Kristo 	}
14939f723220SNishanth Menon 	req->min_freq_hz = min_freq;
14949f723220SNishanth Menon 	req->target_freq_hz = target_freq;
14959f723220SNishanth Menon 	req->max_freq_hz = max_freq;
14969f723220SNishanth Menon 
14979f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
14989f723220SNishanth Menon 	if (ret) {
14999f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
15009f723220SNishanth Menon 		goto fail;
15019f723220SNishanth Menon 	}
15029f723220SNishanth Menon 
15039f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
15049f723220SNishanth Menon 
15059f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
15069f723220SNishanth Menon 		ret = -ENODEV;
15079f723220SNishanth Menon 	else
15089f723220SNishanth Menon 		*match_freq = resp->freq_hz;
15099f723220SNishanth Menon 
15109f723220SNishanth Menon fail:
15119f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
15129f723220SNishanth Menon 
15139f723220SNishanth Menon 	return ret;
15149f723220SNishanth Menon }
15159f723220SNishanth Menon 
15169f723220SNishanth Menon /**
15179f723220SNishanth Menon  * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
15189f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
15199f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
15209f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
15219f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
15229f723220SNishanth Menon  *		which clock input to modify.
15239f723220SNishanth Menon  * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
15249f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
15259f723220SNishanth Menon  *		tolerances and jitter.
15269f723220SNishanth Menon  * @target_freq: The target clock frequency in Hz. A frequency will be
15279f723220SNishanth Menon  *		processed as close to this target frequency as possible.
15289f723220SNishanth Menon  * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
15299f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
15309f723220SNishanth Menon  *		tolerances and jitter.
15319f723220SNishanth Menon  *
15329f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
15339f723220SNishanth Menon  */
ti_sci_cmd_clk_set_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 min_freq,u64 target_freq,u64 max_freq)15349f723220SNishanth Menon static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
153581f4458cSTero Kristo 				   u32 dev_id, u32 clk_id, u64 min_freq,
15369f723220SNishanth Menon 				   u64 target_freq, u64 max_freq)
15379f723220SNishanth Menon {
15389f723220SNishanth Menon 	struct ti_sci_info *info;
15399f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_freq *req;
15409f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
15419f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
15429f723220SNishanth Menon 	struct device *dev;
15439f723220SNishanth Menon 	int ret = 0;
15449f723220SNishanth Menon 
15459f723220SNishanth Menon 	if (IS_ERR(handle))
15469f723220SNishanth Menon 		return PTR_ERR(handle);
15479f723220SNishanth Menon 	if (!handle)
15489f723220SNishanth Menon 		return -EINVAL;
15499f723220SNishanth Menon 
15509f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
15519f723220SNishanth Menon 	dev = info->dev;
15529f723220SNishanth Menon 
15539f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
15549f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
15559f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
15569f723220SNishanth Menon 	if (IS_ERR(xfer)) {
15579f723220SNishanth Menon 		ret = PTR_ERR(xfer);
15589f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
15599f723220SNishanth Menon 		return ret;
15609f723220SNishanth Menon 	}
15619f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
15629f723220SNishanth Menon 	req->dev_id = dev_id;
156381f4458cSTero Kristo 	if (clk_id < 255) {
15649f723220SNishanth Menon 		req->clk_id = clk_id;
156581f4458cSTero Kristo 	} else {
156681f4458cSTero Kristo 		req->clk_id = 255;
156781f4458cSTero Kristo 		req->clk_id_32 = clk_id;
156881f4458cSTero Kristo 	}
15699f723220SNishanth Menon 	req->min_freq_hz = min_freq;
15709f723220SNishanth Menon 	req->target_freq_hz = target_freq;
15719f723220SNishanth Menon 	req->max_freq_hz = max_freq;
15729f723220SNishanth Menon 
15739f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
15749f723220SNishanth Menon 	if (ret) {
15759f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
15769f723220SNishanth Menon 		goto fail;
15779f723220SNishanth Menon 	}
15789f723220SNishanth Menon 
15799f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
15809f723220SNishanth Menon 
15819f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
15829f723220SNishanth Menon 
15839f723220SNishanth Menon fail:
15849f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
15859f723220SNishanth Menon 
15869f723220SNishanth Menon 	return ret;
15879f723220SNishanth Menon }
15889f723220SNishanth Menon 
15899f723220SNishanth Menon /**
15909f723220SNishanth Menon  * ti_sci_cmd_clk_get_freq() - Get current frequency
15919f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
15929f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
15939f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
15949f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
15959f723220SNishanth Menon  *		which clock input to modify.
15969f723220SNishanth Menon  * @freq:	Currently frequency in Hz
15979f723220SNishanth Menon  *
15989f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
15999f723220SNishanth Menon  */
ti_sci_cmd_clk_get_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 * freq)16009f723220SNishanth Menon static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
160181f4458cSTero Kristo 				   u32 dev_id, u32 clk_id, u64 *freq)
16029f723220SNishanth Menon {
16039f723220SNishanth Menon 	struct ti_sci_info *info;
16049f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_freq *req;
16059f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_freq *resp;
16069f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
16079f723220SNishanth Menon 	struct device *dev;
16089f723220SNishanth Menon 	int ret = 0;
16099f723220SNishanth Menon 
16109f723220SNishanth Menon 	if (IS_ERR(handle))
16119f723220SNishanth Menon 		return PTR_ERR(handle);
16129f723220SNishanth Menon 	if (!handle || !freq)
16139f723220SNishanth Menon 		return -EINVAL;
16149f723220SNishanth Menon 
16159f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
16169f723220SNishanth Menon 	dev = info->dev;
16179f723220SNishanth Menon 
16189f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
16199f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
16209f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
16219f723220SNishanth Menon 	if (IS_ERR(xfer)) {
16229f723220SNishanth Menon 		ret = PTR_ERR(xfer);
16239f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
16249f723220SNishanth Menon 		return ret;
16259f723220SNishanth Menon 	}
16269f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
16279f723220SNishanth Menon 	req->dev_id = dev_id;
162881f4458cSTero Kristo 	if (clk_id < 255) {
16299f723220SNishanth Menon 		req->clk_id = clk_id;
163081f4458cSTero Kristo 	} else {
163181f4458cSTero Kristo 		req->clk_id = 255;
163281f4458cSTero Kristo 		req->clk_id_32 = clk_id;
163381f4458cSTero Kristo 	}
16349f723220SNishanth Menon 
16359f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
16369f723220SNishanth Menon 	if (ret) {
16379f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
16389f723220SNishanth Menon 		goto fail;
16399f723220SNishanth Menon 	}
16409f723220SNishanth Menon 
16419f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
16429f723220SNishanth Menon 
16439f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
16449f723220SNishanth Menon 		ret = -ENODEV;
16459f723220SNishanth Menon 	else
16469f723220SNishanth Menon 		*freq = resp->freq_hz;
16479f723220SNishanth Menon 
16489f723220SNishanth Menon fail:
16499f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
16509f723220SNishanth Menon 
16519f723220SNishanth Menon 	return ret;
16529f723220SNishanth Menon }
16539f723220SNishanth Menon 
ti_sci_cmd_core_reboot(const struct ti_sci_handle * handle)1654912cffb4SNishanth Menon static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1655912cffb4SNishanth Menon {
1656912cffb4SNishanth Menon 	struct ti_sci_info *info;
1657912cffb4SNishanth Menon 	struct ti_sci_msg_req_reboot *req;
1658912cffb4SNishanth Menon 	struct ti_sci_msg_hdr *resp;
1659912cffb4SNishanth Menon 	struct ti_sci_xfer *xfer;
1660912cffb4SNishanth Menon 	struct device *dev;
1661912cffb4SNishanth Menon 	int ret = 0;
1662912cffb4SNishanth Menon 
1663912cffb4SNishanth Menon 	if (IS_ERR(handle))
1664912cffb4SNishanth Menon 		return PTR_ERR(handle);
1665912cffb4SNishanth Menon 	if (!handle)
1666912cffb4SNishanth Menon 		return -EINVAL;
1667912cffb4SNishanth Menon 
1668912cffb4SNishanth Menon 	info = handle_to_ti_sci_info(handle);
1669912cffb4SNishanth Menon 	dev = info->dev;
1670912cffb4SNishanth Menon 
1671912cffb4SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1672912cffb4SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1673912cffb4SNishanth Menon 				   sizeof(*req), sizeof(*resp));
1674912cffb4SNishanth Menon 	if (IS_ERR(xfer)) {
1675912cffb4SNishanth Menon 		ret = PTR_ERR(xfer);
1676912cffb4SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
1677912cffb4SNishanth Menon 		return ret;
1678912cffb4SNishanth Menon 	}
1679912cffb4SNishanth Menon 	req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1680912cffb4SNishanth Menon 
1681912cffb4SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
1682912cffb4SNishanth Menon 	if (ret) {
1683912cffb4SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
1684912cffb4SNishanth Menon 		goto fail;
1685912cffb4SNishanth Menon 	}
1686912cffb4SNishanth Menon 
1687912cffb4SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1688912cffb4SNishanth Menon 
1689912cffb4SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
1690912cffb4SNishanth Menon 		ret = -ENODEV;
1691912cffb4SNishanth Menon 	else
1692912cffb4SNishanth Menon 		ret = 0;
1693912cffb4SNishanth Menon 
1694912cffb4SNishanth Menon fail:
1695912cffb4SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
1696912cffb4SNishanth Menon 
1697912cffb4SNishanth Menon 	return ret;
1698912cffb4SNishanth Menon }
1699912cffb4SNishanth Menon 
17009c19fb68SLokesh Vutla /**
17019c19fb68SLokesh Vutla  * ti_sci_get_resource_range - Helper to get a range of resources assigned
17029c19fb68SLokesh Vutla  *			       to a host. Resource is uniquely identified by
17039c19fb68SLokesh Vutla  *			       type and subtype.
17049c19fb68SLokesh Vutla  * @handle:		Pointer to TISCI handle.
17059c19fb68SLokesh Vutla  * @dev_id:		TISCI device ID.
17069c19fb68SLokesh Vutla  * @subtype:		Resource assignment subtype that is being requested
17079c19fb68SLokesh Vutla  *			from the given device.
17089c19fb68SLokesh Vutla  * @s_host:		Host processor ID to which the resources are allocated
1709967a020bSPeter Ujfalusi  * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1710967a020bSPeter Ujfalusi  *			resource range start index and number of resources
17119c19fb68SLokesh Vutla  *
17129c19fb68SLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
17139c19fb68SLokesh Vutla  */
ti_sci_get_resource_range(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,u8 s_host,struct ti_sci_resource_desc * desc)17149c19fb68SLokesh Vutla static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
17159c19fb68SLokesh Vutla 				     u32 dev_id, u8 subtype, u8 s_host,
1716967a020bSPeter Ujfalusi 				     struct ti_sci_resource_desc *desc)
17179c19fb68SLokesh Vutla {
17189c19fb68SLokesh Vutla 	struct ti_sci_msg_resp_get_resource_range *resp;
17199c19fb68SLokesh Vutla 	struct ti_sci_msg_req_get_resource_range *req;
17209c19fb68SLokesh Vutla 	struct ti_sci_xfer *xfer;
17219c19fb68SLokesh Vutla 	struct ti_sci_info *info;
17229c19fb68SLokesh Vutla 	struct device *dev;
17239c19fb68SLokesh Vutla 	int ret = 0;
17249c19fb68SLokesh Vutla 
17259c19fb68SLokesh Vutla 	if (IS_ERR(handle))
17269c19fb68SLokesh Vutla 		return PTR_ERR(handle);
1727967a020bSPeter Ujfalusi 	if (!handle || !desc)
17289c19fb68SLokesh Vutla 		return -EINVAL;
17299c19fb68SLokesh Vutla 
17309c19fb68SLokesh Vutla 	info = handle_to_ti_sci_info(handle);
17319c19fb68SLokesh Vutla 	dev = info->dev;
17329c19fb68SLokesh Vutla 
17339c19fb68SLokesh Vutla 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
17349c19fb68SLokesh Vutla 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
17359c19fb68SLokesh Vutla 				   sizeof(*req), sizeof(*resp));
17369c19fb68SLokesh Vutla 	if (IS_ERR(xfer)) {
17379c19fb68SLokesh Vutla 		ret = PTR_ERR(xfer);
17389c19fb68SLokesh Vutla 		dev_err(dev, "Message alloc failed(%d)\n", ret);
17399c19fb68SLokesh Vutla 		return ret;
17409c19fb68SLokesh Vutla 	}
17419c19fb68SLokesh Vutla 
17429c19fb68SLokesh Vutla 	req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
17439c19fb68SLokesh Vutla 	req->secondary_host = s_host;
17449b98e02aSLokesh Vutla 	req->type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
17459c19fb68SLokesh Vutla 	req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
17469c19fb68SLokesh Vutla 
17479c19fb68SLokesh Vutla 	ret = ti_sci_do_xfer(info, xfer);
17489c19fb68SLokesh Vutla 	if (ret) {
17499c19fb68SLokesh Vutla 		dev_err(dev, "Mbox send fail %d\n", ret);
17509c19fb68SLokesh Vutla 		goto fail;
17519c19fb68SLokesh Vutla 	}
17529c19fb68SLokesh Vutla 
17539c19fb68SLokesh Vutla 	resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
17549c19fb68SLokesh Vutla 
17559c19fb68SLokesh Vutla 	if (!ti_sci_is_response_ack(resp)) {
17569c19fb68SLokesh Vutla 		ret = -ENODEV;
1757519c5c0cSPeter Ujfalusi 	} else if (!resp->range_num && !resp->range_num_sec) {
1758519c5c0cSPeter Ujfalusi 		/* Neither of the two resource range is valid */
17599c19fb68SLokesh Vutla 		ret = -ENODEV;
17609c19fb68SLokesh Vutla 	} else {
1761967a020bSPeter Ujfalusi 		desc->start = resp->range_start;
1762967a020bSPeter Ujfalusi 		desc->num = resp->range_num;
1763519c5c0cSPeter Ujfalusi 		desc->start_sec = resp->range_start_sec;
1764519c5c0cSPeter Ujfalusi 		desc->num_sec = resp->range_num_sec;
17657c1c1d36SJason Wang 	}
17669c19fb68SLokesh Vutla 
17679c19fb68SLokesh Vutla fail:
17689c19fb68SLokesh Vutla 	ti_sci_put_one_xfer(&info->minfo, xfer);
17699c19fb68SLokesh Vutla 
17709c19fb68SLokesh Vutla 	return ret;
17719c19fb68SLokesh Vutla }
17729c19fb68SLokesh Vutla 
17739c19fb68SLokesh Vutla /**
17749c19fb68SLokesh Vutla  * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
17759c19fb68SLokesh Vutla  *				   that is same as ti sci interface host.
17769c19fb68SLokesh Vutla  * @handle:		Pointer to TISCI handle.
17779c19fb68SLokesh Vutla  * @dev_id:		TISCI device ID.
17789c19fb68SLokesh Vutla  * @subtype:		Resource assignment subtype that is being requested
17799c19fb68SLokesh Vutla  *			from the given device.
1780967a020bSPeter Ujfalusi  * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1781967a020bSPeter Ujfalusi  *			resource range start index and number of resources
17829c19fb68SLokesh Vutla  *
17839c19fb68SLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
17849c19fb68SLokesh Vutla  */
ti_sci_cmd_get_resource_range(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,struct ti_sci_resource_desc * desc)17859c19fb68SLokesh Vutla static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
17869c19fb68SLokesh Vutla 					 u32 dev_id, u8 subtype,
1787967a020bSPeter Ujfalusi 					 struct ti_sci_resource_desc *desc)
17889c19fb68SLokesh Vutla {
17899c19fb68SLokesh Vutla 	return ti_sci_get_resource_range(handle, dev_id, subtype,
17909c19fb68SLokesh Vutla 					 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1791967a020bSPeter Ujfalusi 					 desc);
17929c19fb68SLokesh Vutla }
17939c19fb68SLokesh Vutla 
17949c19fb68SLokesh Vutla /**
17959c19fb68SLokesh Vutla  * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
17969c19fb68SLokesh Vutla  *					      assigned to a specified host.
17979c19fb68SLokesh Vutla  * @handle:		Pointer to TISCI handle.
17989c19fb68SLokesh Vutla  * @dev_id:		TISCI device ID.
17999c19fb68SLokesh Vutla  * @subtype:		Resource assignment subtype that is being requested
18009c19fb68SLokesh Vutla  *			from the given device.
18019c19fb68SLokesh Vutla  * @s_host:		Host processor ID to which the resources are allocated
1802967a020bSPeter Ujfalusi  * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1803967a020bSPeter Ujfalusi  *			resource range start index and number of resources
18049c19fb68SLokesh Vutla  *
18059c19fb68SLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
18069c19fb68SLokesh Vutla  */
18079c19fb68SLokesh Vutla static
ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,u8 s_host,struct ti_sci_resource_desc * desc)18089c19fb68SLokesh Vutla int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
18099c19fb68SLokesh Vutla 					     u32 dev_id, u8 subtype, u8 s_host,
1810967a020bSPeter Ujfalusi 					     struct ti_sci_resource_desc *desc)
18119c19fb68SLokesh Vutla {
1812967a020bSPeter Ujfalusi 	return ti_sci_get_resource_range(handle, dev_id, subtype, s_host, desc);
18139c19fb68SLokesh Vutla }
18149c19fb68SLokesh Vutla 
1815997b001fSLokesh Vutla /**
1816997b001fSLokesh Vutla  * ti_sci_manage_irq() - Helper api to configure/release the irq route between
1817997b001fSLokesh Vutla  *			 the requested source and destination
1818997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
1819997b001fSLokesh Vutla  * @valid_params:	Bit fields defining the validity of certain params
1820997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
1821997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
1822997b001fSLokesh Vutla  * @dst_id:		Device ID of the IRQ destination
1823997b001fSLokesh Vutla  * @dst_host_irq:	IRQ number of the destination device
1824997b001fSLokesh Vutla  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1825997b001fSLokesh Vutla  * @vint:		Virtual interrupt to be used within the IA
1826997b001fSLokesh Vutla  * @global_event:	Global event number to be used for the requesting event
1827997b001fSLokesh Vutla  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1828997b001fSLokesh Vutla  * @s_host:		Secondary host ID to which the irq/event is being
1829997b001fSLokesh Vutla  *			requested for.
1830997b001fSLokesh Vutla  * @type:		Request type irq set or release.
1831997b001fSLokesh Vutla  *
1832997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
1833997b001fSLokesh Vutla  */
ti_sci_manage_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host,u16 type)1834997b001fSLokesh Vutla static int ti_sci_manage_irq(const struct ti_sci_handle *handle,
1835997b001fSLokesh Vutla 			     u32 valid_params, u16 src_id, u16 src_index,
1836997b001fSLokesh Vutla 			     u16 dst_id, u16 dst_host_irq, u16 ia_id, u16 vint,
1837997b001fSLokesh Vutla 			     u16 global_event, u8 vint_status_bit, u8 s_host,
1838997b001fSLokesh Vutla 			     u16 type)
1839997b001fSLokesh Vutla {
1840997b001fSLokesh Vutla 	struct ti_sci_msg_req_manage_irq *req;
1841997b001fSLokesh Vutla 	struct ti_sci_msg_hdr *resp;
1842997b001fSLokesh Vutla 	struct ti_sci_xfer *xfer;
1843997b001fSLokesh Vutla 	struct ti_sci_info *info;
1844997b001fSLokesh Vutla 	struct device *dev;
1845997b001fSLokesh Vutla 	int ret = 0;
1846997b001fSLokesh Vutla 
1847997b001fSLokesh Vutla 	if (IS_ERR(handle))
1848997b001fSLokesh Vutla 		return PTR_ERR(handle);
1849997b001fSLokesh Vutla 	if (!handle)
1850997b001fSLokesh Vutla 		return -EINVAL;
1851997b001fSLokesh Vutla 
1852997b001fSLokesh Vutla 	info = handle_to_ti_sci_info(handle);
1853997b001fSLokesh Vutla 	dev = info->dev;
1854997b001fSLokesh Vutla 
1855997b001fSLokesh Vutla 	xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1856997b001fSLokesh Vutla 				   sizeof(*req), sizeof(*resp));
1857997b001fSLokesh Vutla 	if (IS_ERR(xfer)) {
1858997b001fSLokesh Vutla 		ret = PTR_ERR(xfer);
1859997b001fSLokesh Vutla 		dev_err(dev, "Message alloc failed(%d)\n", ret);
1860997b001fSLokesh Vutla 		return ret;
1861997b001fSLokesh Vutla 	}
1862997b001fSLokesh Vutla 	req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
1863997b001fSLokesh Vutla 	req->valid_params = valid_params;
1864997b001fSLokesh Vutla 	req->src_id = src_id;
1865997b001fSLokesh Vutla 	req->src_index = src_index;
1866997b001fSLokesh Vutla 	req->dst_id = dst_id;
1867997b001fSLokesh Vutla 	req->dst_host_irq = dst_host_irq;
1868997b001fSLokesh Vutla 	req->ia_id = ia_id;
1869997b001fSLokesh Vutla 	req->vint = vint;
1870997b001fSLokesh Vutla 	req->global_event = global_event;
1871997b001fSLokesh Vutla 	req->vint_status_bit = vint_status_bit;
1872997b001fSLokesh Vutla 	req->secondary_host = s_host;
1873997b001fSLokesh Vutla 
1874997b001fSLokesh Vutla 	ret = ti_sci_do_xfer(info, xfer);
1875997b001fSLokesh Vutla 	if (ret) {
1876997b001fSLokesh Vutla 		dev_err(dev, "Mbox send fail %d\n", ret);
1877997b001fSLokesh Vutla 		goto fail;
1878997b001fSLokesh Vutla 	}
1879997b001fSLokesh Vutla 
1880997b001fSLokesh Vutla 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1881997b001fSLokesh Vutla 
1882997b001fSLokesh Vutla 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1883997b001fSLokesh Vutla 
1884997b001fSLokesh Vutla fail:
1885997b001fSLokesh Vutla 	ti_sci_put_one_xfer(&info->minfo, xfer);
1886997b001fSLokesh Vutla 
1887997b001fSLokesh Vutla 	return ret;
1888997b001fSLokesh Vutla }
1889997b001fSLokesh Vutla 
1890997b001fSLokesh Vutla /**
1891997b001fSLokesh Vutla  * ti_sci_set_irq() - Helper api to configure the irq route between the
1892997b001fSLokesh Vutla  *		      requested source and destination
1893997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
1894997b001fSLokesh Vutla  * @valid_params:	Bit fields defining the validity of certain params
1895997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
1896997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
1897997b001fSLokesh Vutla  * @dst_id:		Device ID of the IRQ destination
1898997b001fSLokesh Vutla  * @dst_host_irq:	IRQ number of the destination device
1899997b001fSLokesh Vutla  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1900997b001fSLokesh Vutla  * @vint:		Virtual interrupt to be used within the IA
1901997b001fSLokesh Vutla  * @global_event:	Global event number to be used for the requesting event
1902997b001fSLokesh Vutla  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1903997b001fSLokesh Vutla  * @s_host:		Secondary host ID to which the irq/event is being
1904997b001fSLokesh Vutla  *			requested for.
1905997b001fSLokesh Vutla  *
1906997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
1907997b001fSLokesh Vutla  */
ti_sci_set_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host)1908997b001fSLokesh Vutla static int ti_sci_set_irq(const struct ti_sci_handle *handle, u32 valid_params,
1909997b001fSLokesh Vutla 			  u16 src_id, u16 src_index, u16 dst_id,
1910997b001fSLokesh Vutla 			  u16 dst_host_irq, u16 ia_id, u16 vint,
1911997b001fSLokesh Vutla 			  u16 global_event, u8 vint_status_bit, u8 s_host)
1912997b001fSLokesh Vutla {
1913997b001fSLokesh Vutla 	pr_debug("%s: IRQ set with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1914997b001fSLokesh Vutla 		 __func__, valid_params, src_id, src_index,
1915997b001fSLokesh Vutla 		 dst_id, dst_host_irq, ia_id, vint, global_event,
1916997b001fSLokesh Vutla 		 vint_status_bit);
1917997b001fSLokesh Vutla 
1918997b001fSLokesh Vutla 	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1919997b001fSLokesh Vutla 				 dst_id, dst_host_irq, ia_id, vint,
1920997b001fSLokesh Vutla 				 global_event, vint_status_bit, s_host,
1921997b001fSLokesh Vutla 				 TI_SCI_MSG_SET_IRQ);
1922997b001fSLokesh Vutla }
1923997b001fSLokesh Vutla 
1924997b001fSLokesh Vutla /**
1925997b001fSLokesh Vutla  * ti_sci_free_irq() - Helper api to free the irq route between the
1926997b001fSLokesh Vutla  *			   requested source and destination
1927997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
1928997b001fSLokesh Vutla  * @valid_params:	Bit fields defining the validity of certain params
1929997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
1930997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
1931997b001fSLokesh Vutla  * @dst_id:		Device ID of the IRQ destination
1932997b001fSLokesh Vutla  * @dst_host_irq:	IRQ number of the destination device
1933997b001fSLokesh Vutla  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1934997b001fSLokesh Vutla  * @vint:		Virtual interrupt to be used within the IA
1935997b001fSLokesh Vutla  * @global_event:	Global event number to be used for the requesting event
1936997b001fSLokesh Vutla  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1937997b001fSLokesh Vutla  * @s_host:		Secondary host ID to which the irq/event is being
1938997b001fSLokesh Vutla  *			requested for.
1939997b001fSLokesh Vutla  *
1940997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
1941997b001fSLokesh Vutla  */
ti_sci_free_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host)1942997b001fSLokesh Vutla static int ti_sci_free_irq(const struct ti_sci_handle *handle, u32 valid_params,
1943997b001fSLokesh Vutla 			   u16 src_id, u16 src_index, u16 dst_id,
1944997b001fSLokesh Vutla 			   u16 dst_host_irq, u16 ia_id, u16 vint,
1945997b001fSLokesh Vutla 			   u16 global_event, u8 vint_status_bit, u8 s_host)
1946997b001fSLokesh Vutla {
1947997b001fSLokesh Vutla 	pr_debug("%s: IRQ release with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1948997b001fSLokesh Vutla 		 __func__, valid_params, src_id, src_index,
1949997b001fSLokesh Vutla 		 dst_id, dst_host_irq, ia_id, vint, global_event,
1950997b001fSLokesh Vutla 		 vint_status_bit);
1951997b001fSLokesh Vutla 
1952997b001fSLokesh Vutla 	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1953997b001fSLokesh Vutla 				 dst_id, dst_host_irq, ia_id, vint,
1954997b001fSLokesh Vutla 				 global_event, vint_status_bit, s_host,
1955997b001fSLokesh Vutla 				 TI_SCI_MSG_FREE_IRQ);
1956997b001fSLokesh Vutla }
1957997b001fSLokesh Vutla 
1958997b001fSLokesh Vutla /**
1959997b001fSLokesh Vutla  * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
1960997b001fSLokesh Vutla  *			  source and destination.
1961997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
1962997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
1963997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
1964997b001fSLokesh Vutla  * @dst_id:		Device ID of the IRQ destination
1965997b001fSLokesh Vutla  * @dst_host_irq:	IRQ number of the destination device
1966997b001fSLokesh Vutla  *
1967997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
1968997b001fSLokesh Vutla  */
ti_sci_cmd_set_irq(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq)1969997b001fSLokesh Vutla static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
1970997b001fSLokesh Vutla 			      u16 src_index, u16 dst_id, u16 dst_host_irq)
1971997b001fSLokesh Vutla {
1972997b001fSLokesh Vutla 	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
1973997b001fSLokesh Vutla 
1974997b001fSLokesh Vutla 	return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
1975997b001fSLokesh Vutla 			      dst_host_irq, 0, 0, 0, 0, 0);
1976997b001fSLokesh Vutla }
1977997b001fSLokesh Vutla 
1978997b001fSLokesh Vutla /**
1979997b001fSLokesh Vutla  * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
1980997b001fSLokesh Vutla  *				requested source and Interrupt Aggregator.
1981997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
1982997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
1983997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
1984997b001fSLokesh Vutla  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1985997b001fSLokesh Vutla  * @vint:		Virtual interrupt to be used within the IA
1986997b001fSLokesh Vutla  * @global_event:	Global event number to be used for the requesting event
1987997b001fSLokesh Vutla  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1988997b001fSLokesh Vutla  *
1989997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
1990997b001fSLokesh Vutla  */
ti_sci_cmd_set_event_map(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit)1991997b001fSLokesh Vutla static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
1992997b001fSLokesh Vutla 				    u16 src_id, u16 src_index, u16 ia_id,
1993997b001fSLokesh Vutla 				    u16 vint, u16 global_event,
1994997b001fSLokesh Vutla 				    u8 vint_status_bit)
1995997b001fSLokesh Vutla {
1996997b001fSLokesh Vutla 	u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
1997997b001fSLokesh Vutla 			   MSG_FLAG_GLB_EVNT_VALID |
1998997b001fSLokesh Vutla 			   MSG_FLAG_VINT_STS_BIT_VALID;
1999997b001fSLokesh Vutla 
2000997b001fSLokesh Vutla 	return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2001997b001fSLokesh Vutla 			      ia_id, vint, global_event, vint_status_bit, 0);
2002997b001fSLokesh Vutla }
2003997b001fSLokesh Vutla 
2004997b001fSLokesh Vutla /**
2005997b001fSLokesh Vutla  * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2006997b001fSLokesh Vutla  *			   requested source and destination.
2007997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
2008997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
2009997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
2010997b001fSLokesh Vutla  * @dst_id:		Device ID of the IRQ destination
2011997b001fSLokesh Vutla  * @dst_host_irq:	IRQ number of the destination device
2012997b001fSLokesh Vutla  *
2013997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
2014997b001fSLokesh Vutla  */
ti_sci_cmd_free_irq(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq)2015997b001fSLokesh Vutla static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2016997b001fSLokesh Vutla 			       u16 src_index, u16 dst_id, u16 dst_host_irq)
2017997b001fSLokesh Vutla {
2018997b001fSLokesh Vutla 	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2019997b001fSLokesh Vutla 
2020997b001fSLokesh Vutla 	return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2021997b001fSLokesh Vutla 			       dst_host_irq, 0, 0, 0, 0, 0);
2022997b001fSLokesh Vutla }
2023997b001fSLokesh Vutla 
2024997b001fSLokesh Vutla /**
2025997b001fSLokesh Vutla  * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2026997b001fSLokesh Vutla  *				 and Interrupt Aggregator.
2027997b001fSLokesh Vutla  * @handle:		Pointer to TISCI handle.
2028997b001fSLokesh Vutla  * @src_id:		Device ID of the IRQ source
2029997b001fSLokesh Vutla  * @src_index:		IRQ source index within the source device
2030997b001fSLokesh Vutla  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
2031997b001fSLokesh Vutla  * @vint:		Virtual interrupt to be used within the IA
2032997b001fSLokesh Vutla  * @global_event:	Global event number to be used for the requesting event
2033997b001fSLokesh Vutla  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
2034997b001fSLokesh Vutla  *
2035997b001fSLokesh Vutla  * Return: 0 if all went fine, else return appropriate error.
2036997b001fSLokesh Vutla  */
ti_sci_cmd_free_event_map(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit)2037997b001fSLokesh Vutla static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2038997b001fSLokesh Vutla 				     u16 src_id, u16 src_index, u16 ia_id,
2039997b001fSLokesh Vutla 				     u16 vint, u16 global_event,
2040997b001fSLokesh Vutla 				     u8 vint_status_bit)
2041997b001fSLokesh Vutla {
2042997b001fSLokesh Vutla 	u32 valid_params = MSG_FLAG_IA_ID_VALID |
2043997b001fSLokesh Vutla 			   MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2044997b001fSLokesh Vutla 			   MSG_FLAG_VINT_STS_BIT_VALID;
2045997b001fSLokesh Vutla 
2046997b001fSLokesh Vutla 	return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2047997b001fSLokesh Vutla 			       ia_id, vint, global_event, vint_status_bit, 0);
2048997b001fSLokesh Vutla }
2049997b001fSLokesh Vutla 
205068608b5eSPeter Ujfalusi /**
20513c201753SPeter Ujfalusi  * ti_sci_cmd_rm_ring_cfg() - Configure a NAVSS ring
20523c201753SPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
20533c201753SPeter Ujfalusi  * @params:	Pointer to ti_sci_msg_rm_ring_cfg ring config structure
20543c201753SPeter Ujfalusi  *
20553c201753SPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
20563c201753SPeter Ujfalusi  *
20573c201753SPeter Ujfalusi  * See @ti_sci_msg_rm_ring_cfg and @ti_sci_msg_rm_ring_cfg_req for
20583c201753SPeter Ujfalusi  * more info.
20593c201753SPeter Ujfalusi  */
ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_ring_cfg * params)20603c201753SPeter Ujfalusi static int ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle *handle,
20613c201753SPeter Ujfalusi 				  const struct ti_sci_msg_rm_ring_cfg *params)
20623c201753SPeter Ujfalusi {
20633c201753SPeter Ujfalusi 	struct ti_sci_msg_rm_ring_cfg_req *req;
20643c201753SPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
20653c201753SPeter Ujfalusi 	struct ti_sci_xfer *xfer;
20663c201753SPeter Ujfalusi 	struct ti_sci_info *info;
20673c201753SPeter Ujfalusi 	struct device *dev;
20683c201753SPeter Ujfalusi 	int ret = 0;
20693c201753SPeter Ujfalusi 
20703c201753SPeter Ujfalusi 	if (IS_ERR_OR_NULL(handle))
20713c201753SPeter Ujfalusi 		return -EINVAL;
20723c201753SPeter Ujfalusi 
20733c201753SPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
20743c201753SPeter Ujfalusi 	dev = info->dev;
20753c201753SPeter Ujfalusi 
20763c201753SPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
20773c201753SPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
20783c201753SPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
20793c201753SPeter Ujfalusi 	if (IS_ERR(xfer)) {
20803c201753SPeter Ujfalusi 		ret = PTR_ERR(xfer);
20813c201753SPeter Ujfalusi 		dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
20823c201753SPeter Ujfalusi 		return ret;
20833c201753SPeter Ujfalusi 	}
20843c201753SPeter Ujfalusi 	req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
20853c201753SPeter Ujfalusi 	req->valid_params = params->valid_params;
20863c201753SPeter Ujfalusi 	req->nav_id = params->nav_id;
20873c201753SPeter Ujfalusi 	req->index = params->index;
20883c201753SPeter Ujfalusi 	req->addr_lo = params->addr_lo;
20893c201753SPeter Ujfalusi 	req->addr_hi = params->addr_hi;
20903c201753SPeter Ujfalusi 	req->count = params->count;
20913c201753SPeter Ujfalusi 	req->mode = params->mode;
20923c201753SPeter Ujfalusi 	req->size = params->size;
20933c201753SPeter Ujfalusi 	req->order_id = params->order_id;
20943c201753SPeter Ujfalusi 	req->virtid = params->virtid;
20953c201753SPeter Ujfalusi 	req->asel = params->asel;
20963c201753SPeter Ujfalusi 
20973c201753SPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
20983c201753SPeter Ujfalusi 	if (ret) {
20993c201753SPeter Ujfalusi 		dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
21003c201753SPeter Ujfalusi 		goto fail;
21013c201753SPeter Ujfalusi 	}
21023c201753SPeter Ujfalusi 
21033c201753SPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
21043c201753SPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
21053c201753SPeter Ujfalusi 
21063c201753SPeter Ujfalusi fail:
21073c201753SPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
21083c201753SPeter Ujfalusi 	dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", params->index, ret);
21093c201753SPeter Ujfalusi 	return ret;
21103c201753SPeter Ujfalusi }
21113c201753SPeter Ujfalusi 
21123c201753SPeter Ujfalusi /**
211368608b5eSPeter Ujfalusi  * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
211468608b5eSPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
211568608b5eSPeter Ujfalusi  * @nav_id:	Device ID of Navigator Subsystem which should be used for
211668608b5eSPeter Ujfalusi  *		pairing
211768608b5eSPeter Ujfalusi  * @src_thread:	Source PSI-L thread ID
211868608b5eSPeter Ujfalusi  * @dst_thread: Destination PSI-L thread ID
211968608b5eSPeter Ujfalusi  *
212068608b5eSPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
212168608b5eSPeter Ujfalusi  */
ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle * handle,u32 nav_id,u32 src_thread,u32 dst_thread)212268608b5eSPeter Ujfalusi static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
212368608b5eSPeter Ujfalusi 				   u32 nav_id, u32 src_thread, u32 dst_thread)
212468608b5eSPeter Ujfalusi {
212568608b5eSPeter Ujfalusi 	struct ti_sci_msg_psil_pair *req;
212668608b5eSPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
212768608b5eSPeter Ujfalusi 	struct ti_sci_xfer *xfer;
212868608b5eSPeter Ujfalusi 	struct ti_sci_info *info;
212968608b5eSPeter Ujfalusi 	struct device *dev;
213068608b5eSPeter Ujfalusi 	int ret = 0;
213168608b5eSPeter Ujfalusi 
213268608b5eSPeter Ujfalusi 	if (IS_ERR(handle))
213368608b5eSPeter Ujfalusi 		return PTR_ERR(handle);
213468608b5eSPeter Ujfalusi 	if (!handle)
213568608b5eSPeter Ujfalusi 		return -EINVAL;
213668608b5eSPeter Ujfalusi 
213768608b5eSPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
213868608b5eSPeter Ujfalusi 	dev = info->dev;
213968608b5eSPeter Ujfalusi 
214068608b5eSPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
214168608b5eSPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
214268608b5eSPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
214368608b5eSPeter Ujfalusi 	if (IS_ERR(xfer)) {
214468608b5eSPeter Ujfalusi 		ret = PTR_ERR(xfer);
214568608b5eSPeter Ujfalusi 		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
214668608b5eSPeter Ujfalusi 		return ret;
214768608b5eSPeter Ujfalusi 	}
214868608b5eSPeter Ujfalusi 	req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
214968608b5eSPeter Ujfalusi 	req->nav_id = nav_id;
215068608b5eSPeter Ujfalusi 	req->src_thread = src_thread;
215168608b5eSPeter Ujfalusi 	req->dst_thread = dst_thread;
215268608b5eSPeter Ujfalusi 
215368608b5eSPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
215468608b5eSPeter Ujfalusi 	if (ret) {
215568608b5eSPeter Ujfalusi 		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
215668608b5eSPeter Ujfalusi 		goto fail;
215768608b5eSPeter Ujfalusi 	}
215868608b5eSPeter Ujfalusi 
215968608b5eSPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
216068608b5eSPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
216168608b5eSPeter Ujfalusi 
216268608b5eSPeter Ujfalusi fail:
216368608b5eSPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
216468608b5eSPeter Ujfalusi 
216568608b5eSPeter Ujfalusi 	return ret;
216668608b5eSPeter Ujfalusi }
216768608b5eSPeter Ujfalusi 
216868608b5eSPeter Ujfalusi /**
216968608b5eSPeter Ujfalusi  * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
217068608b5eSPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
217168608b5eSPeter Ujfalusi  * @nav_id:	Device ID of Navigator Subsystem which should be used for
217268608b5eSPeter Ujfalusi  *		unpairing
217368608b5eSPeter Ujfalusi  * @src_thread:	Source PSI-L thread ID
217468608b5eSPeter Ujfalusi  * @dst_thread:	Destination PSI-L thread ID
217568608b5eSPeter Ujfalusi  *
217668608b5eSPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
217768608b5eSPeter Ujfalusi  */
ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle * handle,u32 nav_id,u32 src_thread,u32 dst_thread)217868608b5eSPeter Ujfalusi static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
217968608b5eSPeter Ujfalusi 				     u32 nav_id, u32 src_thread, u32 dst_thread)
218068608b5eSPeter Ujfalusi {
218168608b5eSPeter Ujfalusi 	struct ti_sci_msg_psil_unpair *req;
218268608b5eSPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
218368608b5eSPeter Ujfalusi 	struct ti_sci_xfer *xfer;
218468608b5eSPeter Ujfalusi 	struct ti_sci_info *info;
218568608b5eSPeter Ujfalusi 	struct device *dev;
218668608b5eSPeter Ujfalusi 	int ret = 0;
218768608b5eSPeter Ujfalusi 
218868608b5eSPeter Ujfalusi 	if (IS_ERR(handle))
218968608b5eSPeter Ujfalusi 		return PTR_ERR(handle);
219068608b5eSPeter Ujfalusi 	if (!handle)
219168608b5eSPeter Ujfalusi 		return -EINVAL;
219268608b5eSPeter Ujfalusi 
219368608b5eSPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
219468608b5eSPeter Ujfalusi 	dev = info->dev;
219568608b5eSPeter Ujfalusi 
219668608b5eSPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
219768608b5eSPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
219868608b5eSPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
219968608b5eSPeter Ujfalusi 	if (IS_ERR(xfer)) {
220068608b5eSPeter Ujfalusi 		ret = PTR_ERR(xfer);
220168608b5eSPeter Ujfalusi 		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
220268608b5eSPeter Ujfalusi 		return ret;
220368608b5eSPeter Ujfalusi 	}
220468608b5eSPeter Ujfalusi 	req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
220568608b5eSPeter Ujfalusi 	req->nav_id = nav_id;
220668608b5eSPeter Ujfalusi 	req->src_thread = src_thread;
220768608b5eSPeter Ujfalusi 	req->dst_thread = dst_thread;
220868608b5eSPeter Ujfalusi 
220968608b5eSPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
221068608b5eSPeter Ujfalusi 	if (ret) {
221168608b5eSPeter Ujfalusi 		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
221268608b5eSPeter Ujfalusi 		goto fail;
221368608b5eSPeter Ujfalusi 	}
221468608b5eSPeter Ujfalusi 
221568608b5eSPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
221668608b5eSPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
221768608b5eSPeter Ujfalusi 
221868608b5eSPeter Ujfalusi fail:
221968608b5eSPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
222068608b5eSPeter Ujfalusi 
222168608b5eSPeter Ujfalusi 	return ret;
222268608b5eSPeter Ujfalusi }
222368608b5eSPeter Ujfalusi 
222468608b5eSPeter Ujfalusi /**
222568608b5eSPeter Ujfalusi  * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
222668608b5eSPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
222768608b5eSPeter Ujfalusi  * @params:	Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
222868608b5eSPeter Ujfalusi  *		structure
222968608b5eSPeter Ujfalusi  *
223068608b5eSPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
223168608b5eSPeter Ujfalusi  *
223268608b5eSPeter Ujfalusi  * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
223368608b5eSPeter Ujfalusi  * more info.
223468608b5eSPeter Ujfalusi  */
ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_tx_ch_cfg * params)223568608b5eSPeter Ujfalusi static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
223668608b5eSPeter Ujfalusi 			const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
223768608b5eSPeter Ujfalusi {
223868608b5eSPeter Ujfalusi 	struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
223968608b5eSPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
224068608b5eSPeter Ujfalusi 	struct ti_sci_xfer *xfer;
224168608b5eSPeter Ujfalusi 	struct ti_sci_info *info;
224268608b5eSPeter Ujfalusi 	struct device *dev;
224368608b5eSPeter Ujfalusi 	int ret = 0;
224468608b5eSPeter Ujfalusi 
224568608b5eSPeter Ujfalusi 	if (IS_ERR_OR_NULL(handle))
224668608b5eSPeter Ujfalusi 		return -EINVAL;
224768608b5eSPeter Ujfalusi 
224868608b5eSPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
224968608b5eSPeter Ujfalusi 	dev = info->dev;
225068608b5eSPeter Ujfalusi 
225168608b5eSPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
225268608b5eSPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
225368608b5eSPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
225468608b5eSPeter Ujfalusi 	if (IS_ERR(xfer)) {
225568608b5eSPeter Ujfalusi 		ret = PTR_ERR(xfer);
22564c960505SYueHaibing 		dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
225768608b5eSPeter Ujfalusi 		return ret;
225868608b5eSPeter Ujfalusi 	}
225968608b5eSPeter Ujfalusi 	req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
226068608b5eSPeter Ujfalusi 	req->valid_params = params->valid_params;
226168608b5eSPeter Ujfalusi 	req->nav_id = params->nav_id;
226268608b5eSPeter Ujfalusi 	req->index = params->index;
226368608b5eSPeter Ujfalusi 	req->tx_pause_on_err = params->tx_pause_on_err;
226468608b5eSPeter Ujfalusi 	req->tx_filt_einfo = params->tx_filt_einfo;
226568608b5eSPeter Ujfalusi 	req->tx_filt_pswords = params->tx_filt_pswords;
226668608b5eSPeter Ujfalusi 	req->tx_atype = params->tx_atype;
226768608b5eSPeter Ujfalusi 	req->tx_chan_type = params->tx_chan_type;
226868608b5eSPeter Ujfalusi 	req->tx_supr_tdpkt = params->tx_supr_tdpkt;
226968608b5eSPeter Ujfalusi 	req->tx_fetch_size = params->tx_fetch_size;
227068608b5eSPeter Ujfalusi 	req->tx_credit_count = params->tx_credit_count;
227168608b5eSPeter Ujfalusi 	req->txcq_qnum = params->txcq_qnum;
227268608b5eSPeter Ujfalusi 	req->tx_priority = params->tx_priority;
227368608b5eSPeter Ujfalusi 	req->tx_qos = params->tx_qos;
227468608b5eSPeter Ujfalusi 	req->tx_orderid = params->tx_orderid;
227568608b5eSPeter Ujfalusi 	req->fdepth = params->fdepth;
227668608b5eSPeter Ujfalusi 	req->tx_sched_priority = params->tx_sched_priority;
227768608b5eSPeter Ujfalusi 	req->tx_burst_size = params->tx_burst_size;
22781c552e08SPeter Ujfalusi 	req->tx_tdtype = params->tx_tdtype;
2279ce1feed5SPeter Ujfalusi 	req->extended_ch_type = params->extended_ch_type;
228068608b5eSPeter Ujfalusi 
228168608b5eSPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
228268608b5eSPeter Ujfalusi 	if (ret) {
22834c960505SYueHaibing 		dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
228468608b5eSPeter Ujfalusi 		goto fail;
228568608b5eSPeter Ujfalusi 	}
228668608b5eSPeter Ujfalusi 
228768608b5eSPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
228868608b5eSPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
228968608b5eSPeter Ujfalusi 
229068608b5eSPeter Ujfalusi fail:
229168608b5eSPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
22924c960505SYueHaibing 	dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
229368608b5eSPeter Ujfalusi 	return ret;
229468608b5eSPeter Ujfalusi }
229568608b5eSPeter Ujfalusi 
229668608b5eSPeter Ujfalusi /**
229768608b5eSPeter Ujfalusi  * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
229868608b5eSPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
229968608b5eSPeter Ujfalusi  * @params:	Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
230068608b5eSPeter Ujfalusi  *		structure
230168608b5eSPeter Ujfalusi  *
230268608b5eSPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
230368608b5eSPeter Ujfalusi  *
230468608b5eSPeter Ujfalusi  * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
230568608b5eSPeter Ujfalusi  * more info.
230668608b5eSPeter Ujfalusi  */
ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_rx_ch_cfg * params)230768608b5eSPeter Ujfalusi static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
230868608b5eSPeter Ujfalusi 			const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
230968608b5eSPeter Ujfalusi {
231068608b5eSPeter Ujfalusi 	struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
231168608b5eSPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
231268608b5eSPeter Ujfalusi 	struct ti_sci_xfer *xfer;
231368608b5eSPeter Ujfalusi 	struct ti_sci_info *info;
231468608b5eSPeter Ujfalusi 	struct device *dev;
231568608b5eSPeter Ujfalusi 	int ret = 0;
231668608b5eSPeter Ujfalusi 
231768608b5eSPeter Ujfalusi 	if (IS_ERR_OR_NULL(handle))
231868608b5eSPeter Ujfalusi 		return -EINVAL;
231968608b5eSPeter Ujfalusi 
232068608b5eSPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
232168608b5eSPeter Ujfalusi 	dev = info->dev;
232268608b5eSPeter Ujfalusi 
232368608b5eSPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
232468608b5eSPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
232568608b5eSPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
232668608b5eSPeter Ujfalusi 	if (IS_ERR(xfer)) {
232768608b5eSPeter Ujfalusi 		ret = PTR_ERR(xfer);
23284c960505SYueHaibing 		dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
232968608b5eSPeter Ujfalusi 		return ret;
233068608b5eSPeter Ujfalusi 	}
233168608b5eSPeter Ujfalusi 	req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
233268608b5eSPeter Ujfalusi 	req->valid_params = params->valid_params;
233368608b5eSPeter Ujfalusi 	req->nav_id = params->nav_id;
233468608b5eSPeter Ujfalusi 	req->index = params->index;
233568608b5eSPeter Ujfalusi 	req->rx_fetch_size = params->rx_fetch_size;
233668608b5eSPeter Ujfalusi 	req->rxcq_qnum = params->rxcq_qnum;
233768608b5eSPeter Ujfalusi 	req->rx_priority = params->rx_priority;
233868608b5eSPeter Ujfalusi 	req->rx_qos = params->rx_qos;
233968608b5eSPeter Ujfalusi 	req->rx_orderid = params->rx_orderid;
234068608b5eSPeter Ujfalusi 	req->rx_sched_priority = params->rx_sched_priority;
234168608b5eSPeter Ujfalusi 	req->flowid_start = params->flowid_start;
234268608b5eSPeter Ujfalusi 	req->flowid_cnt = params->flowid_cnt;
234368608b5eSPeter Ujfalusi 	req->rx_pause_on_err = params->rx_pause_on_err;
234468608b5eSPeter Ujfalusi 	req->rx_atype = params->rx_atype;
234568608b5eSPeter Ujfalusi 	req->rx_chan_type = params->rx_chan_type;
234668608b5eSPeter Ujfalusi 	req->rx_ignore_short = params->rx_ignore_short;
234768608b5eSPeter Ujfalusi 	req->rx_ignore_long = params->rx_ignore_long;
234868608b5eSPeter Ujfalusi 	req->rx_burst_size = params->rx_burst_size;
234968608b5eSPeter Ujfalusi 
235068608b5eSPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
235168608b5eSPeter Ujfalusi 	if (ret) {
23524c960505SYueHaibing 		dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
235368608b5eSPeter Ujfalusi 		goto fail;
235468608b5eSPeter Ujfalusi 	}
235568608b5eSPeter Ujfalusi 
235668608b5eSPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
235768608b5eSPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
235868608b5eSPeter Ujfalusi 
235968608b5eSPeter Ujfalusi fail:
236068608b5eSPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
23614c960505SYueHaibing 	dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
236268608b5eSPeter Ujfalusi 	return ret;
236368608b5eSPeter Ujfalusi }
236468608b5eSPeter Ujfalusi 
236568608b5eSPeter Ujfalusi /**
236668608b5eSPeter Ujfalusi  * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
236768608b5eSPeter Ujfalusi  * @handle:	Pointer to TI SCI handle.
236868608b5eSPeter Ujfalusi  * @params:	Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
236968608b5eSPeter Ujfalusi  *		structure
237068608b5eSPeter Ujfalusi  *
237168608b5eSPeter Ujfalusi  * Return: 0 if all went well, else returns appropriate error value.
237268608b5eSPeter Ujfalusi  *
237368608b5eSPeter Ujfalusi  * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
237468608b5eSPeter Ujfalusi  * more info.
237568608b5eSPeter Ujfalusi  */
ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_flow_cfg * params)237668608b5eSPeter Ujfalusi static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
237768608b5eSPeter Ujfalusi 			const struct ti_sci_msg_rm_udmap_flow_cfg *params)
237868608b5eSPeter Ujfalusi {
237968608b5eSPeter Ujfalusi 	struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
238068608b5eSPeter Ujfalusi 	struct ti_sci_msg_hdr *resp;
238168608b5eSPeter Ujfalusi 	struct ti_sci_xfer *xfer;
238268608b5eSPeter Ujfalusi 	struct ti_sci_info *info;
238368608b5eSPeter Ujfalusi 	struct device *dev;
238468608b5eSPeter Ujfalusi 	int ret = 0;
238568608b5eSPeter Ujfalusi 
238668608b5eSPeter Ujfalusi 	if (IS_ERR_OR_NULL(handle))
238768608b5eSPeter Ujfalusi 		return -EINVAL;
238868608b5eSPeter Ujfalusi 
238968608b5eSPeter Ujfalusi 	info = handle_to_ti_sci_info(handle);
239068608b5eSPeter Ujfalusi 	dev = info->dev;
239168608b5eSPeter Ujfalusi 
239268608b5eSPeter Ujfalusi 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
239368608b5eSPeter Ujfalusi 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
239468608b5eSPeter Ujfalusi 				   sizeof(*req), sizeof(*resp));
239568608b5eSPeter Ujfalusi 	if (IS_ERR(xfer)) {
239668608b5eSPeter Ujfalusi 		ret = PTR_ERR(xfer);
239768608b5eSPeter Ujfalusi 		dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
239868608b5eSPeter Ujfalusi 		return ret;
239968608b5eSPeter Ujfalusi 	}
240068608b5eSPeter Ujfalusi 	req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
240168608b5eSPeter Ujfalusi 	req->valid_params = params->valid_params;
240268608b5eSPeter Ujfalusi 	req->nav_id = params->nav_id;
240368608b5eSPeter Ujfalusi 	req->flow_index = params->flow_index;
240468608b5eSPeter Ujfalusi 	req->rx_einfo_present = params->rx_einfo_present;
240568608b5eSPeter Ujfalusi 	req->rx_psinfo_present = params->rx_psinfo_present;
240668608b5eSPeter Ujfalusi 	req->rx_error_handling = params->rx_error_handling;
240768608b5eSPeter Ujfalusi 	req->rx_desc_type = params->rx_desc_type;
240868608b5eSPeter Ujfalusi 	req->rx_sop_offset = params->rx_sop_offset;
240968608b5eSPeter Ujfalusi 	req->rx_dest_qnum = params->rx_dest_qnum;
241068608b5eSPeter Ujfalusi 	req->rx_src_tag_hi = params->rx_src_tag_hi;
241168608b5eSPeter Ujfalusi 	req->rx_src_tag_lo = params->rx_src_tag_lo;
241268608b5eSPeter Ujfalusi 	req->rx_dest_tag_hi = params->rx_dest_tag_hi;
241368608b5eSPeter Ujfalusi 	req->rx_dest_tag_lo = params->rx_dest_tag_lo;
241468608b5eSPeter Ujfalusi 	req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
241568608b5eSPeter Ujfalusi 	req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
241668608b5eSPeter Ujfalusi 	req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
241768608b5eSPeter Ujfalusi 	req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
241868608b5eSPeter Ujfalusi 	req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
241968608b5eSPeter Ujfalusi 	req->rx_fdq1_qnum = params->rx_fdq1_qnum;
242068608b5eSPeter Ujfalusi 	req->rx_fdq2_qnum = params->rx_fdq2_qnum;
242168608b5eSPeter Ujfalusi 	req->rx_fdq3_qnum = params->rx_fdq3_qnum;
242268608b5eSPeter Ujfalusi 	req->rx_ps_location = params->rx_ps_location;
242368608b5eSPeter Ujfalusi 
242468608b5eSPeter Ujfalusi 	ret = ti_sci_do_xfer(info, xfer);
242568608b5eSPeter Ujfalusi 	if (ret) {
242668608b5eSPeter Ujfalusi 		dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
242768608b5eSPeter Ujfalusi 		goto fail;
242868608b5eSPeter Ujfalusi 	}
242968608b5eSPeter Ujfalusi 
243068608b5eSPeter Ujfalusi 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
243168608b5eSPeter Ujfalusi 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
243268608b5eSPeter Ujfalusi 
243368608b5eSPeter Ujfalusi fail:
243468608b5eSPeter Ujfalusi 	ti_sci_put_one_xfer(&info->minfo, xfer);
243568608b5eSPeter Ujfalusi 	dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
243668608b5eSPeter Ujfalusi 	return ret;
243768608b5eSPeter Ujfalusi }
243868608b5eSPeter Ujfalusi 
24391e407f33SSuman Anna /**
24401e407f33SSuman Anna  * ti_sci_cmd_proc_request() - Command to request a physical processor control
24411e407f33SSuman Anna  * @handle:	Pointer to TI SCI handle
24421e407f33SSuman Anna  * @proc_id:	Processor ID this request is for
24431e407f33SSuman Anna  *
24441e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
24451e407f33SSuman Anna  */
ti_sci_cmd_proc_request(const struct ti_sci_handle * handle,u8 proc_id)24461e407f33SSuman Anna static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
24471e407f33SSuman Anna 				   u8 proc_id)
24481e407f33SSuman Anna {
24491e407f33SSuman Anna 	struct ti_sci_msg_req_proc_request *req;
24501e407f33SSuman Anna 	struct ti_sci_msg_hdr *resp;
24511e407f33SSuman Anna 	struct ti_sci_info *info;
24521e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
24531e407f33SSuman Anna 	struct device *dev;
24541e407f33SSuman Anna 	int ret = 0;
24551e407f33SSuman Anna 
24561e407f33SSuman Anna 	if (!handle)
24571e407f33SSuman Anna 		return -EINVAL;
24581e407f33SSuman Anna 	if (IS_ERR(handle))
24591e407f33SSuman Anna 		return PTR_ERR(handle);
24601e407f33SSuman Anna 
24611e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
24621e407f33SSuman Anna 	dev = info->dev;
24631e407f33SSuman Anna 
24641e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
24651e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
24661e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
24671e407f33SSuman Anna 	if (IS_ERR(xfer)) {
24681e407f33SSuman Anna 		ret = PTR_ERR(xfer);
24691e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
24701e407f33SSuman Anna 		return ret;
24711e407f33SSuman Anna 	}
24721e407f33SSuman Anna 	req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
24731e407f33SSuman Anna 	req->processor_id = proc_id;
24741e407f33SSuman Anna 
24751e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
24761e407f33SSuman Anna 	if (ret) {
24771e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
24781e407f33SSuman Anna 		goto fail;
24791e407f33SSuman Anna 	}
24801e407f33SSuman Anna 
24811e407f33SSuman Anna 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
24821e407f33SSuman Anna 
24831e407f33SSuman Anna 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
24841e407f33SSuman Anna 
24851e407f33SSuman Anna fail:
24861e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
24871e407f33SSuman Anna 
24881e407f33SSuman Anna 	return ret;
24891e407f33SSuman Anna }
24901e407f33SSuman Anna 
24911e407f33SSuman Anna /**
24921e407f33SSuman Anna  * ti_sci_cmd_proc_release() - Command to release a physical processor control
24931e407f33SSuman Anna  * @handle:	Pointer to TI SCI handle
24941e407f33SSuman Anna  * @proc_id:	Processor ID this request is for
24951e407f33SSuman Anna  *
24961e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
24971e407f33SSuman Anna  */
ti_sci_cmd_proc_release(const struct ti_sci_handle * handle,u8 proc_id)24981e407f33SSuman Anna static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
24991e407f33SSuman Anna 				   u8 proc_id)
25001e407f33SSuman Anna {
25011e407f33SSuman Anna 	struct ti_sci_msg_req_proc_release *req;
25021e407f33SSuman Anna 	struct ti_sci_msg_hdr *resp;
25031e407f33SSuman Anna 	struct ti_sci_info *info;
25041e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
25051e407f33SSuman Anna 	struct device *dev;
25061e407f33SSuman Anna 	int ret = 0;
25071e407f33SSuman Anna 
25081e407f33SSuman Anna 	if (!handle)
25091e407f33SSuman Anna 		return -EINVAL;
25101e407f33SSuman Anna 	if (IS_ERR(handle))
25111e407f33SSuman Anna 		return PTR_ERR(handle);
25121e407f33SSuman Anna 
25131e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
25141e407f33SSuman Anna 	dev = info->dev;
25151e407f33SSuman Anna 
25161e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
25171e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
25181e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
25191e407f33SSuman Anna 	if (IS_ERR(xfer)) {
25201e407f33SSuman Anna 		ret = PTR_ERR(xfer);
25211e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
25221e407f33SSuman Anna 		return ret;
25231e407f33SSuman Anna 	}
25241e407f33SSuman Anna 	req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
25251e407f33SSuman Anna 	req->processor_id = proc_id;
25261e407f33SSuman Anna 
25271e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
25281e407f33SSuman Anna 	if (ret) {
25291e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
25301e407f33SSuman Anna 		goto fail;
25311e407f33SSuman Anna 	}
25321e407f33SSuman Anna 
25331e407f33SSuman Anna 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
25341e407f33SSuman Anna 
25351e407f33SSuman Anna 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
25361e407f33SSuman Anna 
25371e407f33SSuman Anna fail:
25381e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
25391e407f33SSuman Anna 
25401e407f33SSuman Anna 	return ret;
25411e407f33SSuman Anna }
25421e407f33SSuman Anna 
25431e407f33SSuman Anna /**
25441e407f33SSuman Anna  * ti_sci_cmd_proc_handover() - Command to handover a physical processor
25451e407f33SSuman Anna  *				control to a host in the processor's access
25461e407f33SSuman Anna  *				control list.
25471e407f33SSuman Anna  * @handle:	Pointer to TI SCI handle
25481e407f33SSuman Anna  * @proc_id:	Processor ID this request is for
25491e407f33SSuman Anna  * @host_id:	Host ID to get the control of the processor
25501e407f33SSuman Anna  *
25511e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
25521e407f33SSuman Anna  */
ti_sci_cmd_proc_handover(const struct ti_sci_handle * handle,u8 proc_id,u8 host_id)25531e407f33SSuman Anna static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
25541e407f33SSuman Anna 				    u8 proc_id, u8 host_id)
25551e407f33SSuman Anna {
25561e407f33SSuman Anna 	struct ti_sci_msg_req_proc_handover *req;
25571e407f33SSuman Anna 	struct ti_sci_msg_hdr *resp;
25581e407f33SSuman Anna 	struct ti_sci_info *info;
25591e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
25601e407f33SSuman Anna 	struct device *dev;
25611e407f33SSuman Anna 	int ret = 0;
25621e407f33SSuman Anna 
25631e407f33SSuman Anna 	if (!handle)
25641e407f33SSuman Anna 		return -EINVAL;
25651e407f33SSuman Anna 	if (IS_ERR(handle))
25661e407f33SSuman Anna 		return PTR_ERR(handle);
25671e407f33SSuman Anna 
25681e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
25691e407f33SSuman Anna 	dev = info->dev;
25701e407f33SSuman Anna 
25711e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
25721e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
25731e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
25741e407f33SSuman Anna 	if (IS_ERR(xfer)) {
25751e407f33SSuman Anna 		ret = PTR_ERR(xfer);
25761e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
25771e407f33SSuman Anna 		return ret;
25781e407f33SSuman Anna 	}
25791e407f33SSuman Anna 	req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
25801e407f33SSuman Anna 	req->processor_id = proc_id;
25811e407f33SSuman Anna 	req->host_id = host_id;
25821e407f33SSuman Anna 
25831e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
25841e407f33SSuman Anna 	if (ret) {
25851e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
25861e407f33SSuman Anna 		goto fail;
25871e407f33SSuman Anna 	}
25881e407f33SSuman Anna 
25891e407f33SSuman Anna 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
25901e407f33SSuman Anna 
25911e407f33SSuman Anna 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
25921e407f33SSuman Anna 
25931e407f33SSuman Anna fail:
25941e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
25951e407f33SSuman Anna 
25961e407f33SSuman Anna 	return ret;
25971e407f33SSuman Anna }
25981e407f33SSuman Anna 
25991e407f33SSuman Anna /**
26001e407f33SSuman Anna  * ti_sci_cmd_proc_set_config() - Command to set the processor boot
26011e407f33SSuman Anna  *				    configuration flags
26021e407f33SSuman Anna  * @handle:		Pointer to TI SCI handle
26031e407f33SSuman Anna  * @proc_id:		Processor ID this request is for
26043e67fd8dSNishanth Menon  * @bootvector:		Processor Boot vector (start address)
26051e407f33SSuman Anna  * @config_flags_set:	Configuration flags to be set
26061e407f33SSuman Anna  * @config_flags_clear:	Configuration flags to be cleared.
26071e407f33SSuman Anna  *
26081e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
26091e407f33SSuman Anna  */
ti_sci_cmd_proc_set_config(const struct ti_sci_handle * handle,u8 proc_id,u64 bootvector,u32 config_flags_set,u32 config_flags_clear)26101e407f33SSuman Anna static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
26111e407f33SSuman Anna 				      u8 proc_id, u64 bootvector,
26121e407f33SSuman Anna 				      u32 config_flags_set,
26131e407f33SSuman Anna 				      u32 config_flags_clear)
26141e407f33SSuman Anna {
26151e407f33SSuman Anna 	struct ti_sci_msg_req_set_config *req;
26161e407f33SSuman Anna 	struct ti_sci_msg_hdr *resp;
26171e407f33SSuman Anna 	struct ti_sci_info *info;
26181e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
26191e407f33SSuman Anna 	struct device *dev;
26201e407f33SSuman Anna 	int ret = 0;
26211e407f33SSuman Anna 
26221e407f33SSuman Anna 	if (!handle)
26231e407f33SSuman Anna 		return -EINVAL;
26241e407f33SSuman Anna 	if (IS_ERR(handle))
26251e407f33SSuman Anna 		return PTR_ERR(handle);
26261e407f33SSuman Anna 
26271e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
26281e407f33SSuman Anna 	dev = info->dev;
26291e407f33SSuman Anna 
26301e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
26311e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
26321e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
26331e407f33SSuman Anna 	if (IS_ERR(xfer)) {
26341e407f33SSuman Anna 		ret = PTR_ERR(xfer);
26351e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
26361e407f33SSuman Anna 		return ret;
26371e407f33SSuman Anna 	}
26381e407f33SSuman Anna 	req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
26391e407f33SSuman Anna 	req->processor_id = proc_id;
26401e407f33SSuman Anna 	req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
26411e407f33SSuman Anna 	req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
26421e407f33SSuman Anna 				TI_SCI_ADDR_HIGH_SHIFT;
26431e407f33SSuman Anna 	req->config_flags_set = config_flags_set;
26441e407f33SSuman Anna 	req->config_flags_clear = config_flags_clear;
26451e407f33SSuman Anna 
26461e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
26471e407f33SSuman Anna 	if (ret) {
26481e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
26491e407f33SSuman Anna 		goto fail;
26501e407f33SSuman Anna 	}
26511e407f33SSuman Anna 
26521e407f33SSuman Anna 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
26531e407f33SSuman Anna 
26541e407f33SSuman Anna 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
26551e407f33SSuman Anna 
26561e407f33SSuman Anna fail:
26571e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
26581e407f33SSuman Anna 
26591e407f33SSuman Anna 	return ret;
26601e407f33SSuman Anna }
26611e407f33SSuman Anna 
26621e407f33SSuman Anna /**
26631e407f33SSuman Anna  * ti_sci_cmd_proc_set_control() - Command to set the processor boot
26641e407f33SSuman Anna  *				     control flags
26651e407f33SSuman Anna  * @handle:			Pointer to TI SCI handle
26661e407f33SSuman Anna  * @proc_id:			Processor ID this request is for
26671e407f33SSuman Anna  * @control_flags_set:		Control flags to be set
26681e407f33SSuman Anna  * @control_flags_clear:	Control flags to be cleared
26691e407f33SSuman Anna  *
26701e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
26711e407f33SSuman Anna  */
ti_sci_cmd_proc_set_control(const struct ti_sci_handle * handle,u8 proc_id,u32 control_flags_set,u32 control_flags_clear)26721e407f33SSuman Anna static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
26731e407f33SSuman Anna 				       u8 proc_id, u32 control_flags_set,
26741e407f33SSuman Anna 				       u32 control_flags_clear)
26751e407f33SSuman Anna {
26761e407f33SSuman Anna 	struct ti_sci_msg_req_set_ctrl *req;
26771e407f33SSuman Anna 	struct ti_sci_msg_hdr *resp;
26781e407f33SSuman Anna 	struct ti_sci_info *info;
26791e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
26801e407f33SSuman Anna 	struct device *dev;
26811e407f33SSuman Anna 	int ret = 0;
26821e407f33SSuman Anna 
26831e407f33SSuman Anna 	if (!handle)
26841e407f33SSuman Anna 		return -EINVAL;
26851e407f33SSuman Anna 	if (IS_ERR(handle))
26861e407f33SSuman Anna 		return PTR_ERR(handle);
26871e407f33SSuman Anna 
26881e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
26891e407f33SSuman Anna 	dev = info->dev;
26901e407f33SSuman Anna 
26911e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
26921e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
26931e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
26941e407f33SSuman Anna 	if (IS_ERR(xfer)) {
26951e407f33SSuman Anna 		ret = PTR_ERR(xfer);
26961e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
26971e407f33SSuman Anna 		return ret;
26981e407f33SSuman Anna 	}
26991e407f33SSuman Anna 	req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
27001e407f33SSuman Anna 	req->processor_id = proc_id;
27011e407f33SSuman Anna 	req->control_flags_set = control_flags_set;
27021e407f33SSuman Anna 	req->control_flags_clear = control_flags_clear;
27031e407f33SSuman Anna 
27041e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
27051e407f33SSuman Anna 	if (ret) {
27061e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
27071e407f33SSuman Anna 		goto fail;
27081e407f33SSuman Anna 	}
27091e407f33SSuman Anna 
27101e407f33SSuman Anna 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
27111e407f33SSuman Anna 
27121e407f33SSuman Anna 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
27131e407f33SSuman Anna 
27141e407f33SSuman Anna fail:
27151e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
27161e407f33SSuman Anna 
27171e407f33SSuman Anna 	return ret;
27181e407f33SSuman Anna }
27191e407f33SSuman Anna 
27201e407f33SSuman Anna /**
27213e67fd8dSNishanth Menon  * ti_sci_cmd_proc_get_status() - Command to get the processor boot status
27221e407f33SSuman Anna  * @handle:	Pointer to TI SCI handle
27231e407f33SSuman Anna  * @proc_id:	Processor ID this request is for
27243e67fd8dSNishanth Menon  * @bv:		Processor Boot vector (start address)
27253e67fd8dSNishanth Menon  * @cfg_flags:	Processor specific configuration flags
27263e67fd8dSNishanth Menon  * @ctrl_flags:	Processor specific control flags
27273e67fd8dSNishanth Menon  * @sts_flags:	Processor specific status flags
27281e407f33SSuman Anna  *
27291e407f33SSuman Anna  * Return: 0 if all went well, else returns appropriate error value.
27301e407f33SSuman Anna  */
ti_sci_cmd_proc_get_status(const struct ti_sci_handle * handle,u8 proc_id,u64 * bv,u32 * cfg_flags,u32 * ctrl_flags,u32 * sts_flags)27311e407f33SSuman Anna static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
27321e407f33SSuman Anna 				      u8 proc_id, u64 *bv, u32 *cfg_flags,
27331e407f33SSuman Anna 				      u32 *ctrl_flags, u32 *sts_flags)
27341e407f33SSuman Anna {
27351e407f33SSuman Anna 	struct ti_sci_msg_resp_get_status *resp;
27361e407f33SSuman Anna 	struct ti_sci_msg_req_get_status *req;
27371e407f33SSuman Anna 	struct ti_sci_info *info;
27381e407f33SSuman Anna 	struct ti_sci_xfer *xfer;
27391e407f33SSuman Anna 	struct device *dev;
27401e407f33SSuman Anna 	int ret = 0;
27411e407f33SSuman Anna 
27421e407f33SSuman Anna 	if (!handle)
27431e407f33SSuman Anna 		return -EINVAL;
27441e407f33SSuman Anna 	if (IS_ERR(handle))
27451e407f33SSuman Anna 		return PTR_ERR(handle);
27461e407f33SSuman Anna 
27471e407f33SSuman Anna 	info = handle_to_ti_sci_info(handle);
27481e407f33SSuman Anna 	dev = info->dev;
27491e407f33SSuman Anna 
27501e407f33SSuman Anna 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
27511e407f33SSuman Anna 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
27521e407f33SSuman Anna 				   sizeof(*req), sizeof(*resp));
27531e407f33SSuman Anna 	if (IS_ERR(xfer)) {
27541e407f33SSuman Anna 		ret = PTR_ERR(xfer);
27551e407f33SSuman Anna 		dev_err(dev, "Message alloc failed(%d)\n", ret);
27561e407f33SSuman Anna 		return ret;
27571e407f33SSuman Anna 	}
27581e407f33SSuman Anna 	req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
27591e407f33SSuman Anna 	req->processor_id = proc_id;
27601e407f33SSuman Anna 
27611e407f33SSuman Anna 	ret = ti_sci_do_xfer(info, xfer);
27621e407f33SSuman Anna 	if (ret) {
27631e407f33SSuman Anna 		dev_err(dev, "Mbox send fail %d\n", ret);
27641e407f33SSuman Anna 		goto fail;
27651e407f33SSuman Anna 	}
27661e407f33SSuman Anna 
27671e407f33SSuman Anna 	resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
27681e407f33SSuman Anna 
27691e407f33SSuman Anna 	if (!ti_sci_is_response_ack(resp)) {
27701e407f33SSuman Anna 		ret = -ENODEV;
27711e407f33SSuman Anna 	} else {
27721e407f33SSuman Anna 		*bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
27731e407f33SSuman Anna 		      (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
27741e407f33SSuman Anna 		       TI_SCI_ADDR_HIGH_MASK);
27751e407f33SSuman Anna 		*cfg_flags = resp->config_flags;
27761e407f33SSuman Anna 		*ctrl_flags = resp->control_flags;
27771e407f33SSuman Anna 		*sts_flags = resp->status_flags;
27781e407f33SSuman Anna 	}
27791e407f33SSuman Anna 
27801e407f33SSuman Anna fail:
27811e407f33SSuman Anna 	ti_sci_put_one_xfer(&info->minfo, xfer);
27821e407f33SSuman Anna 
27831e407f33SSuman Anna 	return ret;
27841e407f33SSuman Anna }
27851e407f33SSuman Anna 
27869e7d756dSNishanth Menon /*
27879e7d756dSNishanth Menon  * ti_sci_setup_ops() - Setup the operations structures
27889e7d756dSNishanth Menon  * @info:	pointer to TISCI pointer
27899e7d756dSNishanth Menon  */
ti_sci_setup_ops(struct ti_sci_info * info)27909e7d756dSNishanth Menon static void ti_sci_setup_ops(struct ti_sci_info *info)
27919e7d756dSNishanth Menon {
27929e7d756dSNishanth Menon 	struct ti_sci_ops *ops = &info->handle.ops;
2793912cffb4SNishanth Menon 	struct ti_sci_core_ops *core_ops = &ops->core_ops;
27949e7d756dSNishanth Menon 	struct ti_sci_dev_ops *dops = &ops->dev_ops;
27959f723220SNishanth Menon 	struct ti_sci_clk_ops *cops = &ops->clk_ops;
27969c19fb68SLokesh Vutla 	struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2797997b001fSLokesh Vutla 	struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
279868608b5eSPeter Ujfalusi 	struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
279968608b5eSPeter Ujfalusi 	struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
280068608b5eSPeter Ujfalusi 	struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
28011e407f33SSuman Anna 	struct ti_sci_proc_ops *pops = &ops->proc_ops;
28029e7d756dSNishanth Menon 
2803912cffb4SNishanth Menon 	core_ops->reboot_device = ti_sci_cmd_core_reboot;
2804912cffb4SNishanth Menon 
28059e7d756dSNishanth Menon 	dops->get_device = ti_sci_cmd_get_device;
280645b659eeSLokesh Vutla 	dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
28079e7d756dSNishanth Menon 	dops->idle_device = ti_sci_cmd_idle_device;
280845b659eeSLokesh Vutla 	dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
28099e7d756dSNishanth Menon 	dops->put_device = ti_sci_cmd_put_device;
28109e7d756dSNishanth Menon 
28119e7d756dSNishanth Menon 	dops->is_valid = ti_sci_cmd_dev_is_valid;
28129e7d756dSNishanth Menon 	dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
28139e7d756dSNishanth Menon 	dops->is_idle = ti_sci_cmd_dev_is_idle;
28149e7d756dSNishanth Menon 	dops->is_stop = ti_sci_cmd_dev_is_stop;
28159e7d756dSNishanth Menon 	dops->is_on = ti_sci_cmd_dev_is_on;
28169e7d756dSNishanth Menon 	dops->is_transitioning = ti_sci_cmd_dev_is_trans;
28179e7d756dSNishanth Menon 	dops->set_device_resets = ti_sci_cmd_set_device_resets;
28189e7d756dSNishanth Menon 	dops->get_device_resets = ti_sci_cmd_get_device_resets;
28199f723220SNishanth Menon 
28209f723220SNishanth Menon 	cops->get_clock = ti_sci_cmd_get_clock;
28219f723220SNishanth Menon 	cops->idle_clock = ti_sci_cmd_idle_clock;
28229f723220SNishanth Menon 	cops->put_clock = ti_sci_cmd_put_clock;
28239f723220SNishanth Menon 	cops->is_auto = ti_sci_cmd_clk_is_auto;
28249f723220SNishanth Menon 	cops->is_on = ti_sci_cmd_clk_is_on;
28259f723220SNishanth Menon 	cops->is_off = ti_sci_cmd_clk_is_off;
28269f723220SNishanth Menon 
28279f723220SNishanth Menon 	cops->set_parent = ti_sci_cmd_clk_set_parent;
28289f723220SNishanth Menon 	cops->get_parent = ti_sci_cmd_clk_get_parent;
28299f723220SNishanth Menon 	cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
28309f723220SNishanth Menon 
28319f723220SNishanth Menon 	cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
28329f723220SNishanth Menon 	cops->set_freq = ti_sci_cmd_clk_set_freq;
28339f723220SNishanth Menon 	cops->get_freq = ti_sci_cmd_clk_get_freq;
28349c19fb68SLokesh Vutla 
28359c19fb68SLokesh Vutla 	rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
28369c19fb68SLokesh Vutla 	rm_core_ops->get_range_from_shost =
28379c19fb68SLokesh Vutla 				ti_sci_cmd_get_resource_range_from_shost;
2838997b001fSLokesh Vutla 
2839997b001fSLokesh Vutla 	iops->set_irq = ti_sci_cmd_set_irq;
2840997b001fSLokesh Vutla 	iops->set_event_map = ti_sci_cmd_set_event_map;
2841997b001fSLokesh Vutla 	iops->free_irq = ti_sci_cmd_free_irq;
2842997b001fSLokesh Vutla 	iops->free_event_map = ti_sci_cmd_free_event_map;
284368608b5eSPeter Ujfalusi 
28443c201753SPeter Ujfalusi 	rops->set_cfg = ti_sci_cmd_rm_ring_cfg;
284568608b5eSPeter Ujfalusi 
284668608b5eSPeter Ujfalusi 	psilops->pair = ti_sci_cmd_rm_psil_pair;
284768608b5eSPeter Ujfalusi 	psilops->unpair = ti_sci_cmd_rm_psil_unpair;
284868608b5eSPeter Ujfalusi 
284968608b5eSPeter Ujfalusi 	udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
285068608b5eSPeter Ujfalusi 	udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
285168608b5eSPeter Ujfalusi 	udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
28521e407f33SSuman Anna 
28531e407f33SSuman Anna 	pops->request = ti_sci_cmd_proc_request;
28541e407f33SSuman Anna 	pops->release = ti_sci_cmd_proc_release;
28551e407f33SSuman Anna 	pops->handover = ti_sci_cmd_proc_handover;
28561e407f33SSuman Anna 	pops->set_config = ti_sci_cmd_proc_set_config;
28571e407f33SSuman Anna 	pops->set_control = ti_sci_cmd_proc_set_control;
28581e407f33SSuman Anna 	pops->get_status = ti_sci_cmd_proc_get_status;
28599e7d756dSNishanth Menon }
28609e7d756dSNishanth Menon 
28619e7d756dSNishanth Menon /**
2862aa276781SNishanth Menon  * ti_sci_get_handle() - Get the TI SCI handle for a device
2863aa276781SNishanth Menon  * @dev:	Pointer to device for which we want SCI handle
2864aa276781SNishanth Menon  *
2865aa276781SNishanth Menon  * NOTE: The function does not track individual clients of the framework
2866aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
2867aa276781SNishanth Menon  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2868aa276781SNishanth Menon  * Return: pointer to handle if successful, else:
2869aa276781SNishanth Menon  * -EPROBE_DEFER if the instance is not ready
2870aa276781SNishanth Menon  * -ENODEV if the required node handler is missing
2871aa276781SNishanth Menon  * -EINVAL if invalid conditions are encountered.
2872aa276781SNishanth Menon  */
ti_sci_get_handle(struct device * dev)2873aa276781SNishanth Menon const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2874aa276781SNishanth Menon {
2875aa276781SNishanth Menon 	struct device_node *ti_sci_np;
2876aa276781SNishanth Menon 	struct list_head *p;
2877aa276781SNishanth Menon 	struct ti_sci_handle *handle = NULL;
2878aa276781SNishanth Menon 	struct ti_sci_info *info;
2879aa276781SNishanth Menon 
2880aa276781SNishanth Menon 	if (!dev) {
2881aa276781SNishanth Menon 		pr_err("I need a device pointer\n");
2882aa276781SNishanth Menon 		return ERR_PTR(-EINVAL);
2883aa276781SNishanth Menon 	}
2884aa276781SNishanth Menon 	ti_sci_np = of_get_parent(dev->of_node);
2885aa276781SNishanth Menon 	if (!ti_sci_np) {
2886aa276781SNishanth Menon 		dev_err(dev, "No OF information\n");
2887aa276781SNishanth Menon 		return ERR_PTR(-EINVAL);
2888aa276781SNishanth Menon 	}
2889aa276781SNishanth Menon 
2890aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
2891aa276781SNishanth Menon 	list_for_each(p, &ti_sci_list) {
2892aa276781SNishanth Menon 		info = list_entry(p, struct ti_sci_info, node);
2893aa276781SNishanth Menon 		if (ti_sci_np == info->dev->of_node) {
2894aa276781SNishanth Menon 			handle = &info->handle;
2895aa276781SNishanth Menon 			info->users++;
2896aa276781SNishanth Menon 			break;
2897aa276781SNishanth Menon 		}
2898aa276781SNishanth Menon 	}
2899aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
2900aa276781SNishanth Menon 	of_node_put(ti_sci_np);
2901aa276781SNishanth Menon 
2902aa276781SNishanth Menon 	if (!handle)
2903aa276781SNishanth Menon 		return ERR_PTR(-EPROBE_DEFER);
2904aa276781SNishanth Menon 
2905aa276781SNishanth Menon 	return handle;
2906aa276781SNishanth Menon }
2907aa276781SNishanth Menon EXPORT_SYMBOL_GPL(ti_sci_get_handle);
2908aa276781SNishanth Menon 
2909aa276781SNishanth Menon /**
2910aa276781SNishanth Menon  * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
2911aa276781SNishanth Menon  * @handle:	Handle acquired by ti_sci_get_handle
2912aa276781SNishanth Menon  *
2913aa276781SNishanth Menon  * NOTE: The function does not track individual clients of the framework
2914aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
2915aa276781SNishanth Menon  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2916aa276781SNishanth Menon  *
2917aa276781SNishanth Menon  * Return: 0 is successfully released
2918aa276781SNishanth Menon  * if an error pointer was passed, it returns the error value back,
2919aa276781SNishanth Menon  * if null was passed, it returns -EINVAL;
2920aa276781SNishanth Menon  */
ti_sci_put_handle(const struct ti_sci_handle * handle)2921aa276781SNishanth Menon int ti_sci_put_handle(const struct ti_sci_handle *handle)
2922aa276781SNishanth Menon {
2923aa276781SNishanth Menon 	struct ti_sci_info *info;
2924aa276781SNishanth Menon 
2925aa276781SNishanth Menon 	if (IS_ERR(handle))
2926aa276781SNishanth Menon 		return PTR_ERR(handle);
2927aa276781SNishanth Menon 	if (!handle)
2928aa276781SNishanth Menon 		return -EINVAL;
2929aa276781SNishanth Menon 
2930aa276781SNishanth Menon 	info = handle_to_ti_sci_info(handle);
2931aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
2932aa276781SNishanth Menon 	if (!WARN_ON(!info->users))
2933aa276781SNishanth Menon 		info->users--;
2934aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
2935aa276781SNishanth Menon 
2936aa276781SNishanth Menon 	return 0;
2937aa276781SNishanth Menon }
2938aa276781SNishanth Menon EXPORT_SYMBOL_GPL(ti_sci_put_handle);
2939aa276781SNishanth Menon 
devm_ti_sci_release(struct device * dev,void * res)2940aa276781SNishanth Menon static void devm_ti_sci_release(struct device *dev, void *res)
2941aa276781SNishanth Menon {
2942aa276781SNishanth Menon 	const struct ti_sci_handle **ptr = res;
2943aa276781SNishanth Menon 	const struct ti_sci_handle *handle = *ptr;
2944aa276781SNishanth Menon 	int ret;
2945aa276781SNishanth Menon 
2946aa276781SNishanth Menon 	ret = ti_sci_put_handle(handle);
2947aa276781SNishanth Menon 	if (ret)
2948aa276781SNishanth Menon 		dev_err(dev, "failed to put handle %d\n", ret);
2949aa276781SNishanth Menon }
2950aa276781SNishanth Menon 
2951aa276781SNishanth Menon /**
2952aa276781SNishanth Menon  * devm_ti_sci_get_handle() - Managed get handle
2953aa276781SNishanth Menon  * @dev:	device for which we want SCI handle for.
2954aa276781SNishanth Menon  *
2955aa276781SNishanth Menon  * NOTE: This releases the handle once the device resources are
2956aa276781SNishanth Menon  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
2957aa276781SNishanth Menon  * The function does not track individual clients of the framework
2958aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
2959aa276781SNishanth Menon  *
2960aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
2961aa276781SNishanth Menon  */
devm_ti_sci_get_handle(struct device * dev)2962aa276781SNishanth Menon const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
2963aa276781SNishanth Menon {
2964aa276781SNishanth Menon 	const struct ti_sci_handle **ptr;
2965aa276781SNishanth Menon 	const struct ti_sci_handle *handle;
2966aa276781SNishanth Menon 
2967aa276781SNishanth Menon 	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
2968aa276781SNishanth Menon 	if (!ptr)
2969aa276781SNishanth Menon 		return ERR_PTR(-ENOMEM);
2970aa276781SNishanth Menon 	handle = ti_sci_get_handle(dev);
2971aa276781SNishanth Menon 
2972aa276781SNishanth Menon 	if (!IS_ERR(handle)) {
2973aa276781SNishanth Menon 		*ptr = handle;
2974aa276781SNishanth Menon 		devres_add(dev, ptr);
2975aa276781SNishanth Menon 	} else {
2976aa276781SNishanth Menon 		devres_free(ptr);
2977aa276781SNishanth Menon 	}
2978aa276781SNishanth Menon 
2979aa276781SNishanth Menon 	return handle;
2980aa276781SNishanth Menon }
2981aa276781SNishanth Menon EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
2982aa276781SNishanth Menon 
2983905c3047SGrygorii Strashko /**
2984905c3047SGrygorii Strashko  * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2985905c3047SGrygorii Strashko  * @np:		device node
2986905c3047SGrygorii Strashko  * @property:	property name containing phandle on TISCI node
2987905c3047SGrygorii Strashko  *
2988905c3047SGrygorii Strashko  * NOTE: The function does not track individual clients of the framework
2989905c3047SGrygorii Strashko  * and is expected to be maintained by caller of TI SCI protocol library.
2990905c3047SGrygorii Strashko  * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
2991905c3047SGrygorii Strashko  * Return: pointer to handle if successful, else:
2992905c3047SGrygorii Strashko  * -EPROBE_DEFER if the instance is not ready
2993905c3047SGrygorii Strashko  * -ENODEV if the required node handler is missing
2994905c3047SGrygorii Strashko  * -EINVAL if invalid conditions are encountered.
2995905c3047SGrygorii Strashko  */
ti_sci_get_by_phandle(struct device_node * np,const char * property)2996905c3047SGrygorii Strashko const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
2997905c3047SGrygorii Strashko 						  const char *property)
2998905c3047SGrygorii Strashko {
2999905c3047SGrygorii Strashko 	struct ti_sci_handle *handle = NULL;
3000905c3047SGrygorii Strashko 	struct device_node *ti_sci_np;
3001905c3047SGrygorii Strashko 	struct ti_sci_info *info;
3002905c3047SGrygorii Strashko 	struct list_head *p;
3003905c3047SGrygorii Strashko 
3004905c3047SGrygorii Strashko 	if (!np) {
3005905c3047SGrygorii Strashko 		pr_err("I need a device pointer\n");
3006905c3047SGrygorii Strashko 		return ERR_PTR(-EINVAL);
3007905c3047SGrygorii Strashko 	}
3008905c3047SGrygorii Strashko 
3009905c3047SGrygorii Strashko 	ti_sci_np = of_parse_phandle(np, property, 0);
3010905c3047SGrygorii Strashko 	if (!ti_sci_np)
3011905c3047SGrygorii Strashko 		return ERR_PTR(-ENODEV);
3012905c3047SGrygorii Strashko 
3013905c3047SGrygorii Strashko 	mutex_lock(&ti_sci_list_mutex);
3014905c3047SGrygorii Strashko 	list_for_each(p, &ti_sci_list) {
3015905c3047SGrygorii Strashko 		info = list_entry(p, struct ti_sci_info, node);
3016905c3047SGrygorii Strashko 		if (ti_sci_np == info->dev->of_node) {
3017905c3047SGrygorii Strashko 			handle = &info->handle;
3018905c3047SGrygorii Strashko 			info->users++;
3019905c3047SGrygorii Strashko 			break;
3020905c3047SGrygorii Strashko 		}
3021905c3047SGrygorii Strashko 	}
3022905c3047SGrygorii Strashko 	mutex_unlock(&ti_sci_list_mutex);
3023905c3047SGrygorii Strashko 	of_node_put(ti_sci_np);
3024905c3047SGrygorii Strashko 
3025905c3047SGrygorii Strashko 	if (!handle)
3026905c3047SGrygorii Strashko 		return ERR_PTR(-EPROBE_DEFER);
3027905c3047SGrygorii Strashko 
3028905c3047SGrygorii Strashko 	return handle;
3029905c3047SGrygorii Strashko }
3030905c3047SGrygorii Strashko EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3031905c3047SGrygorii Strashko 
3032905c3047SGrygorii Strashko /**
3033905c3047SGrygorii Strashko  * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3034905c3047SGrygorii Strashko  * @dev:	Device pointer requesting TISCI handle
3035905c3047SGrygorii Strashko  * @property:	property name containing phandle on TISCI node
3036905c3047SGrygorii Strashko  *
3037905c3047SGrygorii Strashko  * NOTE: This releases the handle once the device resources are
3038905c3047SGrygorii Strashko  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3039905c3047SGrygorii Strashko  * The function does not track individual clients of the framework
3040905c3047SGrygorii Strashko  * and is expected to be maintained by caller of TI SCI protocol library.
3041905c3047SGrygorii Strashko  *
3042905c3047SGrygorii Strashko  * Return: 0 if all went fine, else corresponding error.
3043905c3047SGrygorii Strashko  */
devm_ti_sci_get_by_phandle(struct device * dev,const char * property)3044905c3047SGrygorii Strashko const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3045905c3047SGrygorii Strashko 						       const char *property)
3046905c3047SGrygorii Strashko {
3047905c3047SGrygorii Strashko 	const struct ti_sci_handle *handle;
3048905c3047SGrygorii Strashko 	const struct ti_sci_handle **ptr;
3049905c3047SGrygorii Strashko 
3050905c3047SGrygorii Strashko 	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3051905c3047SGrygorii Strashko 	if (!ptr)
3052905c3047SGrygorii Strashko 		return ERR_PTR(-ENOMEM);
3053905c3047SGrygorii Strashko 	handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3054905c3047SGrygorii Strashko 
3055905c3047SGrygorii Strashko 	if (!IS_ERR(handle)) {
3056905c3047SGrygorii Strashko 		*ptr = handle;
3057905c3047SGrygorii Strashko 		devres_add(dev, ptr);
3058905c3047SGrygorii Strashko 	} else {
3059905c3047SGrygorii Strashko 		devres_free(ptr);
3060905c3047SGrygorii Strashko 	}
3061905c3047SGrygorii Strashko 
3062905c3047SGrygorii Strashko 	return handle;
3063905c3047SGrygorii Strashko }
3064905c3047SGrygorii Strashko EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3065905c3047SGrygorii Strashko 
3066032a1ec5SLokesh Vutla /**
3067032a1ec5SLokesh Vutla  * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3068032a1ec5SLokesh Vutla  * @res:	Pointer to the TISCI resource
3069032a1ec5SLokesh Vutla  *
3070032a1ec5SLokesh Vutla  * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3071032a1ec5SLokesh Vutla  */
ti_sci_get_free_resource(struct ti_sci_resource * res)3072032a1ec5SLokesh Vutla u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3073032a1ec5SLokesh Vutla {
3074032a1ec5SLokesh Vutla 	unsigned long flags;
3075032a1ec5SLokesh Vutla 	u16 set, free_bit;
3076032a1ec5SLokesh Vutla 
3077032a1ec5SLokesh Vutla 	raw_spin_lock_irqsave(&res->lock, flags);
3078032a1ec5SLokesh Vutla 	for (set = 0; set < res->sets; set++) {
3079519c5c0cSPeter Ujfalusi 		struct ti_sci_resource_desc *desc = &res->desc[set];
3080519c5c0cSPeter Ujfalusi 		int res_count = desc->num + desc->num_sec;
3081519c5c0cSPeter Ujfalusi 
3082519c5c0cSPeter Ujfalusi 		free_bit = find_first_zero_bit(desc->res_map, res_count);
3083519c5c0cSPeter Ujfalusi 		if (free_bit != res_count) {
30844dc38832SChristophe JAILLET 			__set_bit(free_bit, desc->res_map);
3085032a1ec5SLokesh Vutla 			raw_spin_unlock_irqrestore(&res->lock, flags);
3086519c5c0cSPeter Ujfalusi 
3087519c5c0cSPeter Ujfalusi 			if (desc->num && free_bit < desc->num)
3088519c5c0cSPeter Ujfalusi 				return desc->start + free_bit;
3089519c5c0cSPeter Ujfalusi 			else
3090519c5c0cSPeter Ujfalusi 				return desc->start_sec + free_bit;
3091032a1ec5SLokesh Vutla 		}
3092032a1ec5SLokesh Vutla 	}
3093032a1ec5SLokesh Vutla 	raw_spin_unlock_irqrestore(&res->lock, flags);
3094032a1ec5SLokesh Vutla 
3095032a1ec5SLokesh Vutla 	return TI_SCI_RESOURCE_NULL;
3096032a1ec5SLokesh Vutla }
3097032a1ec5SLokesh Vutla EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3098032a1ec5SLokesh Vutla 
3099032a1ec5SLokesh Vutla /**
3100032a1ec5SLokesh Vutla  * ti_sci_release_resource() - Release a resource from TISCI resource.
3101032a1ec5SLokesh Vutla  * @res:	Pointer to the TISCI resource
3102032a1ec5SLokesh Vutla  * @id:		Resource id to be released.
3103032a1ec5SLokesh Vutla  */
ti_sci_release_resource(struct ti_sci_resource * res,u16 id)3104032a1ec5SLokesh Vutla void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3105032a1ec5SLokesh Vutla {
3106032a1ec5SLokesh Vutla 	unsigned long flags;
3107032a1ec5SLokesh Vutla 	u16 set;
3108032a1ec5SLokesh Vutla 
3109032a1ec5SLokesh Vutla 	raw_spin_lock_irqsave(&res->lock, flags);
3110032a1ec5SLokesh Vutla 	for (set = 0; set < res->sets; set++) {
3111519c5c0cSPeter Ujfalusi 		struct ti_sci_resource_desc *desc = &res->desc[set];
3112519c5c0cSPeter Ujfalusi 
3113519c5c0cSPeter Ujfalusi 		if (desc->num && desc->start <= id &&
3114519c5c0cSPeter Ujfalusi 		    (desc->start + desc->num) > id)
31154dc38832SChristophe JAILLET 			__clear_bit(id - desc->start, desc->res_map);
3116519c5c0cSPeter Ujfalusi 		else if (desc->num_sec && desc->start_sec <= id &&
3117519c5c0cSPeter Ujfalusi 			 (desc->start_sec + desc->num_sec) > id)
31184dc38832SChristophe JAILLET 			__clear_bit(id - desc->start_sec, desc->res_map);
3119032a1ec5SLokesh Vutla 	}
3120032a1ec5SLokesh Vutla 	raw_spin_unlock_irqrestore(&res->lock, flags);
3121032a1ec5SLokesh Vutla }
3122032a1ec5SLokesh Vutla EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3123032a1ec5SLokesh Vutla 
3124032a1ec5SLokesh Vutla /**
3125032a1ec5SLokesh Vutla  * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3126032a1ec5SLokesh Vutla  * @res:	Pointer to the TISCI resource
3127032a1ec5SLokesh Vutla  *
3128032a1ec5SLokesh Vutla  * Return: Total number of available resources.
3129032a1ec5SLokesh Vutla  */
ti_sci_get_num_resources(struct ti_sci_resource * res)3130032a1ec5SLokesh Vutla u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3131032a1ec5SLokesh Vutla {
3132032a1ec5SLokesh Vutla 	u32 set, count = 0;
3133032a1ec5SLokesh Vutla 
3134032a1ec5SLokesh Vutla 	for (set = 0; set < res->sets; set++)
3135519c5c0cSPeter Ujfalusi 		count += res->desc[set].num + res->desc[set].num_sec;
3136032a1ec5SLokesh Vutla 
3137032a1ec5SLokesh Vutla 	return count;
3138032a1ec5SLokesh Vutla }
3139032a1ec5SLokesh Vutla EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3140032a1ec5SLokesh Vutla 
3141032a1ec5SLokesh Vutla /**
314253bf2b0eSLokesh Vutla  * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
3143032a1ec5SLokesh Vutla  * @handle:	TISCI handle
3144032a1ec5SLokesh Vutla  * @dev:	Device pointer to which the resource is assigned
3145032a1ec5SLokesh Vutla  * @dev_id:	TISCI device id to which the resource is assigned
314653bf2b0eSLokesh Vutla  * @sub_types:	Array of sub_types assigned corresponding to device
314753bf2b0eSLokesh Vutla  * @sets:	Number of sub_types
3148032a1ec5SLokesh Vutla  *
3149032a1ec5SLokesh Vutla  * Return: Pointer to ti_sci_resource if all went well else appropriate
3150032a1ec5SLokesh Vutla  *	   error pointer.
3151032a1ec5SLokesh Vutla  */
315253bf2b0eSLokesh Vutla static struct ti_sci_resource *
devm_ti_sci_get_resource_sets(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,u32 * sub_types,u32 sets)315353bf2b0eSLokesh Vutla devm_ti_sci_get_resource_sets(const struct ti_sci_handle *handle,
315453bf2b0eSLokesh Vutla 			      struct device *dev, u32 dev_id, u32 *sub_types,
315553bf2b0eSLokesh Vutla 			      u32 sets)
3156032a1ec5SLokesh Vutla {
3157032a1ec5SLokesh Vutla 	struct ti_sci_resource *res;
3158fa42da11SPeter Ujfalusi 	bool valid_set = false;
3159519c5c0cSPeter Ujfalusi 	int i, ret, res_count;
3160032a1ec5SLokesh Vutla 
3161032a1ec5SLokesh Vutla 	res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3162032a1ec5SLokesh Vutla 	if (!res)
3163032a1ec5SLokesh Vutla 		return ERR_PTR(-ENOMEM);
3164032a1ec5SLokesh Vutla 
316553bf2b0eSLokesh Vutla 	res->sets = sets;
3166032a1ec5SLokesh Vutla 	res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3167032a1ec5SLokesh Vutla 				 GFP_KERNEL);
3168032a1ec5SLokesh Vutla 	if (!res->desc)
3169032a1ec5SLokesh Vutla 		return ERR_PTR(-ENOMEM);
3170032a1ec5SLokesh Vutla 
3171032a1ec5SLokesh Vutla 	for (i = 0; i < res->sets; i++) {
3172032a1ec5SLokesh Vutla 		ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
317353bf2b0eSLokesh Vutla 							sub_types[i],
3174967a020bSPeter Ujfalusi 							&res->desc[i]);
3175032a1ec5SLokesh Vutla 		if (ret) {
3176fa42da11SPeter Ujfalusi 			dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
317753bf2b0eSLokesh Vutla 				dev_id, sub_types[i]);
3178519c5c0cSPeter Ujfalusi 			memset(&res->desc[i], 0, sizeof(res->desc[i]));
3179fa42da11SPeter Ujfalusi 			continue;
3180032a1ec5SLokesh Vutla 		}
3181032a1ec5SLokesh Vutla 
3182519c5c0cSPeter Ujfalusi 		dev_dbg(dev, "dev/sub_type: %d/%d, start/num: %d/%d | %d/%d\n",
318353bf2b0eSLokesh Vutla 			dev_id, sub_types[i], res->desc[i].start,
3184519c5c0cSPeter Ujfalusi 			res->desc[i].num, res->desc[i].start_sec,
3185519c5c0cSPeter Ujfalusi 			res->desc[i].num_sec);
3186032a1ec5SLokesh Vutla 
3187fa42da11SPeter Ujfalusi 		valid_set = true;
3188519c5c0cSPeter Ujfalusi 		res_count = res->desc[i].num + res->desc[i].num_sec;
31892f9b0402SChristophe JAILLET 		res->desc[i].res_map = devm_bitmap_zalloc(dev, res_count,
31902f9b0402SChristophe JAILLET 							  GFP_KERNEL);
3191032a1ec5SLokesh Vutla 		if (!res->desc[i].res_map)
3192032a1ec5SLokesh Vutla 			return ERR_PTR(-ENOMEM);
3193032a1ec5SLokesh Vutla 	}
3194032a1ec5SLokesh Vutla 	raw_spin_lock_init(&res->lock);
3195032a1ec5SLokesh Vutla 
3196fa42da11SPeter Ujfalusi 	if (valid_set)
3197032a1ec5SLokesh Vutla 		return res;
3198fa42da11SPeter Ujfalusi 
3199fa42da11SPeter Ujfalusi 	return ERR_PTR(-EINVAL);
3200032a1ec5SLokesh Vutla }
3201032a1ec5SLokesh Vutla 
320253bf2b0eSLokesh Vutla /**
320353bf2b0eSLokesh Vutla  * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
320453bf2b0eSLokesh Vutla  * @handle:	TISCI handle
320553bf2b0eSLokesh Vutla  * @dev:	Device pointer to which the resource is assigned
320653bf2b0eSLokesh Vutla  * @dev_id:	TISCI device id to which the resource is assigned
320753bf2b0eSLokesh Vutla  * @of_prop:	property name by which the resource are represented
320853bf2b0eSLokesh Vutla  *
320953bf2b0eSLokesh Vutla  * Return: Pointer to ti_sci_resource if all went well else appropriate
321053bf2b0eSLokesh Vutla  *	   error pointer.
321153bf2b0eSLokesh Vutla  */
321253bf2b0eSLokesh Vutla struct ti_sci_resource *
devm_ti_sci_get_of_resource(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,char * of_prop)321353bf2b0eSLokesh Vutla devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
321453bf2b0eSLokesh Vutla 			    struct device *dev, u32 dev_id, char *of_prop)
321553bf2b0eSLokesh Vutla {
321653bf2b0eSLokesh Vutla 	struct ti_sci_resource *res;
321753bf2b0eSLokesh Vutla 	u32 *sub_types;
321853bf2b0eSLokesh Vutla 	int sets;
321953bf2b0eSLokesh Vutla 
322053bf2b0eSLokesh Vutla 	sets = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
322153bf2b0eSLokesh Vutla 					       sizeof(u32));
322253bf2b0eSLokesh Vutla 	if (sets < 0) {
322353bf2b0eSLokesh Vutla 		dev_err(dev, "%s resource type ids not available\n", of_prop);
322453bf2b0eSLokesh Vutla 		return ERR_PTR(sets);
322553bf2b0eSLokesh Vutla 	}
322653bf2b0eSLokesh Vutla 
322753bf2b0eSLokesh Vutla 	sub_types = kcalloc(sets, sizeof(*sub_types), GFP_KERNEL);
322853bf2b0eSLokesh Vutla 	if (!sub_types)
322953bf2b0eSLokesh Vutla 		return ERR_PTR(-ENOMEM);
323053bf2b0eSLokesh Vutla 
323153bf2b0eSLokesh Vutla 	of_property_read_u32_array(dev_of_node(dev), of_prop, sub_types, sets);
323253bf2b0eSLokesh Vutla 	res = devm_ti_sci_get_resource_sets(handle, dev, dev_id, sub_types,
323353bf2b0eSLokesh Vutla 					    sets);
323453bf2b0eSLokesh Vutla 
323553bf2b0eSLokesh Vutla 	kfree(sub_types);
323653bf2b0eSLokesh Vutla 	return res;
323753bf2b0eSLokesh Vutla }
323853bf2b0eSLokesh Vutla EXPORT_SYMBOL_GPL(devm_ti_sci_get_of_resource);
323953bf2b0eSLokesh Vutla 
324053bf2b0eSLokesh Vutla /**
324153bf2b0eSLokesh Vutla  * devm_ti_sci_get_resource() - Get a resource range assigned to the device
324253bf2b0eSLokesh Vutla  * @handle:	TISCI handle
324353bf2b0eSLokesh Vutla  * @dev:	Device pointer to which the resource is assigned
324453bf2b0eSLokesh Vutla  * @dev_id:	TISCI device id to which the resource is assigned
32453e67fd8dSNishanth Menon  * @sub_type:	TISCI resource subytpe representing the resource.
324653bf2b0eSLokesh Vutla  *
324753bf2b0eSLokesh Vutla  * Return: Pointer to ti_sci_resource if all went well else appropriate
324853bf2b0eSLokesh Vutla  *	   error pointer.
324953bf2b0eSLokesh Vutla  */
325053bf2b0eSLokesh Vutla struct ti_sci_resource *
devm_ti_sci_get_resource(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,u32 sub_type)325153bf2b0eSLokesh Vutla devm_ti_sci_get_resource(const struct ti_sci_handle *handle, struct device *dev,
325253bf2b0eSLokesh Vutla 			 u32 dev_id, u32 sub_type)
325353bf2b0eSLokesh Vutla {
325453bf2b0eSLokesh Vutla 	return devm_ti_sci_get_resource_sets(handle, dev, dev_id, &sub_type, 1);
325553bf2b0eSLokesh Vutla }
325653bf2b0eSLokesh Vutla EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
325753bf2b0eSLokesh Vutla 
tisci_reboot_handler(struct notifier_block * nb,unsigned long mode,void * cmd)3258912cffb4SNishanth Menon static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
3259912cffb4SNishanth Menon 				void *cmd)
3260912cffb4SNishanth Menon {
3261912cffb4SNishanth Menon 	struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
3262912cffb4SNishanth Menon 	const struct ti_sci_handle *handle = &info->handle;
3263912cffb4SNishanth Menon 
3264912cffb4SNishanth Menon 	ti_sci_cmd_core_reboot(handle);
3265912cffb4SNishanth Menon 
3266912cffb4SNishanth Menon 	/* call fail OR pass, we should not be here in the first place */
3267912cffb4SNishanth Menon 	return NOTIFY_BAD;
3268912cffb4SNishanth Menon }
3269912cffb4SNishanth Menon 
3270aa276781SNishanth Menon /* Description for K2G */
3271aa276781SNishanth Menon static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3272e69a3553SNishanth Menon 	.default_host_id = 2,
3273aa276781SNishanth Menon 	/* Conservative duration */
3274aa276781SNishanth Menon 	.max_rx_timeout_ms = 1000,
3275aa276781SNishanth Menon 	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3276aa276781SNishanth Menon 	.max_msgs = 20,
3277aa276781SNishanth Menon 	.max_msg_size = 64,
3278754c9477SPeter Ujfalusi };
3279754c9477SPeter Ujfalusi 
3280754c9477SPeter Ujfalusi /* Description for AM654 */
3281754c9477SPeter Ujfalusi static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3282754c9477SPeter Ujfalusi 	.default_host_id = 12,
3283754c9477SPeter Ujfalusi 	/* Conservative duration */
3284754c9477SPeter Ujfalusi 	.max_rx_timeout_ms = 10000,
3285754c9477SPeter Ujfalusi 	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3286754c9477SPeter Ujfalusi 	.max_msgs = 20,
3287754c9477SPeter Ujfalusi 	.max_msg_size = 60,
3288aa276781SNishanth Menon };
3289aa276781SNishanth Menon 
3290aa276781SNishanth Menon static const struct of_device_id ti_sci_of_match[] = {
3291aa276781SNishanth Menon 	{.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
3292754c9477SPeter Ujfalusi 	{.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
3293aa276781SNishanth Menon 	{ /* Sentinel */ },
3294aa276781SNishanth Menon };
3295aa276781SNishanth Menon MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3296aa276781SNishanth Menon 
ti_sci_probe(struct platform_device * pdev)3297aa276781SNishanth Menon static int ti_sci_probe(struct platform_device *pdev)
3298aa276781SNishanth Menon {
3299aa276781SNishanth Menon 	struct device *dev = &pdev->dev;
3300aa276781SNishanth Menon 	const struct of_device_id *of_id;
3301aa276781SNishanth Menon 	const struct ti_sci_desc *desc;
3302aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
3303aa276781SNishanth Menon 	struct ti_sci_info *info = NULL;
3304aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo;
3305aa276781SNishanth Menon 	struct mbox_client *cl;
3306aa276781SNishanth Menon 	int ret = -EINVAL;
3307aa276781SNishanth Menon 	int i;
3308912cffb4SNishanth Menon 	int reboot = 0;
3309e69a3553SNishanth Menon 	u32 h_id;
3310aa276781SNishanth Menon 
3311aa276781SNishanth Menon 	of_id = of_match_device(ti_sci_of_match, dev);
3312aa276781SNishanth Menon 	if (!of_id) {
3313aa276781SNishanth Menon 		dev_err(dev, "OF data missing\n");
3314aa276781SNishanth Menon 		return -EINVAL;
3315aa276781SNishanth Menon 	}
3316aa276781SNishanth Menon 	desc = of_id->data;
3317aa276781SNishanth Menon 
3318aa276781SNishanth Menon 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3319aa276781SNishanth Menon 	if (!info)
3320aa276781SNishanth Menon 		return -ENOMEM;
3321aa276781SNishanth Menon 
3322aa276781SNishanth Menon 	info->dev = dev;
3323aa276781SNishanth Menon 	info->desc = desc;
3324e69a3553SNishanth Menon 	ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
3325e69a3553SNishanth Menon 	/* if the property is not present in DT, use a default from desc */
3326e69a3553SNishanth Menon 	if (ret < 0) {
3327e69a3553SNishanth Menon 		info->host_id = info->desc->default_host_id;
3328e69a3553SNishanth Menon 	} else {
3329e69a3553SNishanth Menon 		if (!h_id) {
3330e69a3553SNishanth Menon 			dev_warn(dev, "Host ID 0 is reserved for firmware\n");
3331e69a3553SNishanth Menon 			info->host_id = info->desc->default_host_id;
3332e69a3553SNishanth Menon 		} else {
3333e69a3553SNishanth Menon 			info->host_id = h_id;
3334e69a3553SNishanth Menon 		}
3335e69a3553SNishanth Menon 	}
3336e69a3553SNishanth Menon 
3337912cffb4SNishanth Menon 	reboot = of_property_read_bool(dev->of_node,
3338912cffb4SNishanth Menon 				       "ti,system-reboot-controller");
3339aa276781SNishanth Menon 	INIT_LIST_HEAD(&info->node);
3340aa276781SNishanth Menon 	minfo = &info->minfo;
3341aa276781SNishanth Menon 
3342aa276781SNishanth Menon 	/*
3343aa276781SNishanth Menon 	 * Pre-allocate messages
3344aa276781SNishanth Menon 	 * NEVER allocate more than what we can indicate in hdr.seq
3345aa276781SNishanth Menon 	 * if we have data description bug, force a fix..
3346aa276781SNishanth Menon 	 */
3347aa276781SNishanth Menon 	if (WARN_ON(desc->max_msgs >=
3348aa276781SNishanth Menon 		    1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
3349aa276781SNishanth Menon 		return -EINVAL;
3350aa276781SNishanth Menon 
3351aa276781SNishanth Menon 	minfo->xfer_block = devm_kcalloc(dev,
3352aa276781SNishanth Menon 					 desc->max_msgs,
3353aa276781SNishanth Menon 					 sizeof(*minfo->xfer_block),
3354aa276781SNishanth Menon 					 GFP_KERNEL);
3355aa276781SNishanth Menon 	if (!minfo->xfer_block)
3356aa276781SNishanth Menon 		return -ENOMEM;
3357aa276781SNishanth Menon 
335826507b03SChristophe JAILLET 	minfo->xfer_alloc_table = devm_bitmap_zalloc(dev,
335926507b03SChristophe JAILLET 						     desc->max_msgs,
3360aa276781SNishanth Menon 						     GFP_KERNEL);
3361aa276781SNishanth Menon 	if (!minfo->xfer_alloc_table)
3362aa276781SNishanth Menon 		return -ENOMEM;
3363aa276781SNishanth Menon 
3364aa276781SNishanth Menon 	/* Pre-initialize the buffer pointer to pre-allocated buffers */
3365aa276781SNishanth Menon 	for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3366aa276781SNishanth Menon 		xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3367aa276781SNishanth Menon 					      GFP_KERNEL);
3368aa276781SNishanth Menon 		if (!xfer->xfer_buf)
3369aa276781SNishanth Menon 			return -ENOMEM;
3370aa276781SNishanth Menon 
3371aa276781SNishanth Menon 		xfer->tx_message.buf = xfer->xfer_buf;
3372aa276781SNishanth Menon 		init_completion(&xfer->done);
3373aa276781SNishanth Menon 	}
3374aa276781SNishanth Menon 
3375aa276781SNishanth Menon 	ret = ti_sci_debugfs_create(pdev, info);
3376aa276781SNishanth Menon 	if (ret)
3377aa276781SNishanth Menon 		dev_warn(dev, "Failed to create debug file\n");
3378aa276781SNishanth Menon 
3379aa276781SNishanth Menon 	platform_set_drvdata(pdev, info);
3380aa276781SNishanth Menon 
3381aa276781SNishanth Menon 	cl = &info->cl;
3382aa276781SNishanth Menon 	cl->dev = dev;
3383aa276781SNishanth Menon 	cl->tx_block = false;
3384aa276781SNishanth Menon 	cl->rx_callback = ti_sci_rx_callback;
3385aa276781SNishanth Menon 	cl->knows_txdone = true;
3386aa276781SNishanth Menon 
3387aa276781SNishanth Menon 	spin_lock_init(&minfo->xfer_lock);
3388aa276781SNishanth Menon 	sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3389aa276781SNishanth Menon 
3390aa276781SNishanth Menon 	info->chan_rx = mbox_request_channel_byname(cl, "rx");
3391aa276781SNishanth Menon 	if (IS_ERR(info->chan_rx)) {
3392aa276781SNishanth Menon 		ret = PTR_ERR(info->chan_rx);
3393aa276781SNishanth Menon 		goto out;
3394aa276781SNishanth Menon 	}
3395aa276781SNishanth Menon 
3396aa276781SNishanth Menon 	info->chan_tx = mbox_request_channel_byname(cl, "tx");
3397aa276781SNishanth Menon 	if (IS_ERR(info->chan_tx)) {
3398aa276781SNishanth Menon 		ret = PTR_ERR(info->chan_tx);
3399aa276781SNishanth Menon 		goto out;
3400aa276781SNishanth Menon 	}
3401aa276781SNishanth Menon 	ret = ti_sci_cmd_get_revision(info);
3402aa276781SNishanth Menon 	if (ret) {
3403aa276781SNishanth Menon 		dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3404aa276781SNishanth Menon 		goto out;
3405aa276781SNishanth Menon 	}
3406aa276781SNishanth Menon 
34079e7d756dSNishanth Menon 	ti_sci_setup_ops(info);
34089e7d756dSNishanth Menon 
3409912cffb4SNishanth Menon 	if (reboot) {
3410912cffb4SNishanth Menon 		info->nb.notifier_call = tisci_reboot_handler;
3411912cffb4SNishanth Menon 		info->nb.priority = 128;
3412912cffb4SNishanth Menon 
3413912cffb4SNishanth Menon 		ret = register_restart_handler(&info->nb);
3414912cffb4SNishanth Menon 		if (ret) {
3415912cffb4SNishanth Menon 			dev_err(dev, "reboot registration fail(%d)\n", ret);
3416a181bcfcSPeiwei Hu 			goto out;
3417912cffb4SNishanth Menon 		}
3418912cffb4SNishanth Menon 	}
3419912cffb4SNishanth Menon 
3420aa276781SNishanth Menon 	dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3421aa276781SNishanth Menon 		 info->handle.version.abi_major, info->handle.version.abi_minor,
3422aa276781SNishanth Menon 		 info->handle.version.firmware_revision,
3423aa276781SNishanth Menon 		 info->handle.version.firmware_description);
3424aa276781SNishanth Menon 
3425aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
3426aa276781SNishanth Menon 	list_add_tail(&info->node, &ti_sci_list);
3427aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
3428aa276781SNishanth Menon 
3429aa276781SNishanth Menon 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
3430aa276781SNishanth Menon out:
3431aa276781SNishanth Menon 	if (!IS_ERR(info->chan_tx))
3432aa276781SNishanth Menon 		mbox_free_channel(info->chan_tx);
3433aa276781SNishanth Menon 	if (!IS_ERR(info->chan_rx))
3434aa276781SNishanth Menon 		mbox_free_channel(info->chan_rx);
3435aa276781SNishanth Menon 	debugfs_remove(info->d);
3436aa276781SNishanth Menon 	return ret;
3437aa276781SNishanth Menon }
3438aa276781SNishanth Menon 
3439aa276781SNishanth Menon static struct platform_driver ti_sci_driver = {
3440aa276781SNishanth Menon 	.probe = ti_sci_probe,
3441aa276781SNishanth Menon 	.driver = {
3442aa276781SNishanth Menon 		   .name = "ti-sci",
3443aa276781SNishanth Menon 		   .of_match_table = of_match_ptr(ti_sci_of_match),
344417f15d15SDhruva Gole 		   .suppress_bind_attrs = true,
3445aa276781SNishanth Menon 	},
3446aa276781SNishanth Menon };
3447aa276781SNishanth Menon module_platform_driver(ti_sci_driver);
3448aa276781SNishanth Menon 
3449aa276781SNishanth Menon MODULE_LICENSE("GPL v2");
3450aa276781SNishanth Menon MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3451aa276781SNishanth Menon MODULE_AUTHOR("Nishanth Menon");
3452aa276781SNishanth Menon MODULE_ALIAS("platform:ti-sci");
3453