1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Texas Instruments System Control Interface Protocol Driver
4 *
5 * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://www.ti.com/
6 *	Nishanth Menon
7 */
8
9#define pr_fmt(fmt) "%s: " fmt, __func__
10
11#include <linux/bitmap.h>
12#include <linux/debugfs.h>
13#include <linux/export.h>
14#include <linux/io.h>
15#include <linux/iopoll.h>
16#include <linux/kernel.h>
17#include <linux/mailbox_client.h>
18#include <linux/module.h>
19#include <linux/of_device.h>
20#include <linux/semaphore.h>
21#include <linux/slab.h>
22#include <linux/soc/ti/ti-msgmgr.h>
23#include <linux/soc/ti/ti_sci_protocol.h>
24#include <linux/reboot.h>
25
26#include "ti_sci.h"
27
28/* List of all TI SCI devices active in system */
29static LIST_HEAD(ti_sci_list);
30/* Protection for the entire list */
31static DEFINE_MUTEX(ti_sci_list_mutex);
32
33/**
34 * struct ti_sci_xfer - Structure representing a message flow
35 * @tx_message:	Transmit message
36 * @rx_len:	Receive message length
37 * @xfer_buf:	Preallocated buffer to store receive message
38 *		Since we work with request-ACK protocol, we can
39 *		reuse the same buffer for the rx path as we
40 *		use for the tx path.
41 * @done:	completion event
42 */
43struct ti_sci_xfer {
44	struct ti_msgmgr_message tx_message;
45	u8 rx_len;
46	u8 *xfer_buf;
47	struct completion done;
48};
49
50/**
51 * struct ti_sci_xfers_info - Structure to manage transfer information
52 * @sem_xfer_count:	Counting Semaphore for managing max simultaneous
53 *			Messages.
54 * @xfer_block:		Preallocated Message array
55 * @xfer_alloc_table:	Bitmap table for allocated messages.
56 *			Index of this bitmap table is also used for message
57 *			sequence identifier.
58 * @xfer_lock:		Protection for message allocation
59 */
60struct ti_sci_xfers_info {
61	struct semaphore sem_xfer_count;
62	struct ti_sci_xfer *xfer_block;
63	unsigned long *xfer_alloc_table;
64	/* protect transfer allocation */
65	spinlock_t xfer_lock;
66};
67
68/**
69 * struct ti_sci_desc - Description of SoC integration
70 * @default_host_id:	Host identifier representing the compute entity
71 * @max_rx_timeout_ms:	Timeout for communication with SoC (in Milliseconds)
72 * @max_msgs: Maximum number of messages that can be pending
73 *		  simultaneously in the system
74 * @max_msg_size: Maximum size of data per message that can be handled.
75 */
76struct ti_sci_desc {
77	u8 default_host_id;
78	int max_rx_timeout_ms;
79	int max_msgs;
80	int max_msg_size;
81};
82
83/**
84 * struct ti_sci_info - Structure representing a TI SCI instance
85 * @dev:	Device pointer
86 * @desc:	SoC description for this instance
87 * @nb:	Reboot Notifier block
88 * @d:		Debugfs file entry
89 * @debug_region: Memory region where the debug message are available
90 * @debug_region_size: Debug region size
91 * @debug_buffer: Buffer allocated to copy debug messages.
92 * @handle:	Instance of TI SCI handle to send to clients.
93 * @cl:		Mailbox Client
94 * @chan_tx:	Transmit mailbox channel
95 * @chan_rx:	Receive mailbox channel
96 * @minfo:	Message info
97 * @node:	list head
98 * @host_id:	Host ID
99 * @users:	Number of users of this instance
100 */
101struct ti_sci_info {
102	struct device *dev;
103	struct notifier_block nb;
104	const struct ti_sci_desc *desc;
105	struct dentry *d;
106	void __iomem *debug_region;
107	char *debug_buffer;
108	size_t debug_region_size;
109	struct ti_sci_handle handle;
110	struct mbox_client cl;
111	struct mbox_chan *chan_tx;
112	struct mbox_chan *chan_rx;
113	struct ti_sci_xfers_info minfo;
114	struct list_head node;
115	u8 host_id;
116	/* protected by ti_sci_list_mutex */
117	int users;
118};
119
120#define cl_to_ti_sci_info(c)	container_of(c, struct ti_sci_info, cl)
121#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
122#define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
123
124#ifdef CONFIG_DEBUG_FS
125
126/**
127 * ti_sci_debug_show() - Helper to dump the debug log
128 * @s:	sequence file pointer
129 * @unused:	unused.
130 *
131 * Return: 0
132 */
133static int ti_sci_debug_show(struct seq_file *s, void *unused)
134{
135	struct ti_sci_info *info = s->private;
136
137	memcpy_fromio(info->debug_buffer, info->debug_region,
138		      info->debug_region_size);
139	/*
140	 * We don't trust firmware to leave NULL terminated last byte (hence
141	 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142	 * specific data format for debug messages, We just present the data
143	 * in the buffer as is - we expect the messages to be self explanatory.
144	 */
145	seq_puts(s, info->debug_buffer);
146	return 0;
147}
148
149/* Provide the log file operations interface*/
150DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
151
152/**
153 * ti_sci_debugfs_create() - Create log debug file
154 * @pdev:	platform device pointer
155 * @info:	Pointer to SCI entity information
156 *
157 * Return: 0 if all went fine, else corresponding error.
158 */
159static int ti_sci_debugfs_create(struct platform_device *pdev,
160				 struct ti_sci_info *info)
161{
162	struct device *dev = &pdev->dev;
163	struct resource *res;
164	char debug_name[50];
165
166	/* Debug region is optional */
167	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
168					   "debug_messages");
169	info->debug_region = devm_ioremap_resource(dev, res);
170	if (IS_ERR(info->debug_region))
171		return 0;
172	info->debug_region_size = resource_size(res);
173
174	info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
175					  sizeof(char), GFP_KERNEL);
176	if (!info->debug_buffer)
177		return -ENOMEM;
178	/* Setup NULL termination */
179	info->debug_buffer[info->debug_region_size] = 0;
180
181	snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s",
182		 dev_name(dev));
183	info->d = debugfs_create_file(debug_name, 0444, NULL, info,
184				      &ti_sci_debug_fops);
185	if (IS_ERR(info->d))
186		return PTR_ERR(info->d);
187
188	dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
189		info->debug_region, info->debug_region_size, res);
190	return 0;
191}
192
193#else /* CONFIG_DEBUG_FS */
194static inline int ti_sci_debugfs_create(struct platform_device *dev,
195					struct ti_sci_info *info)
196{
197	return 0;
198}
199
200static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
201					  struct ti_sci_info *info)
202{
203}
204#endif /* CONFIG_DEBUG_FS */
205
206/**
207 * ti_sci_dump_header_dbg() - Helper to dump a message header.
208 * @dev:	Device pointer corresponding to the SCI entity
209 * @hdr:	pointer to header.
210 */
211static inline void ti_sci_dump_header_dbg(struct device *dev,
212					  struct ti_sci_msg_hdr *hdr)
213{
214	dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
215		hdr->type, hdr->host, hdr->seq, hdr->flags);
216}
217
218/**
219 * ti_sci_rx_callback() - mailbox client callback for receive messages
220 * @cl:	client pointer
221 * @m:	mailbox message
222 *
223 * Processes one received message to appropriate transfer information and
224 * signals completion of the transfer.
225 *
226 * NOTE: This function will be invoked in IRQ context, hence should be
227 * as optimal as possible.
228 */
229static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
230{
231	struct ti_sci_info *info = cl_to_ti_sci_info(cl);
232	struct device *dev = info->dev;
233	struct ti_sci_xfers_info *minfo = &info->minfo;
234	struct ti_msgmgr_message *mbox_msg = m;
235	struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
236	struct ti_sci_xfer *xfer;
237	u8 xfer_id;
238
239	xfer_id = hdr->seq;
240
241	/*
242	 * Are we even expecting this?
243	 * NOTE: barriers were implicit in locks used for modifying the bitmap
244	 */
245	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
246		dev_err(dev, "Message for %d is not expected!\n", xfer_id);
247		return;
248	}
249
250	xfer = &minfo->xfer_block[xfer_id];
251
252	/* Is the message of valid length? */
253	if (mbox_msg->len > info->desc->max_msg_size) {
254		dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
255			mbox_msg->len, info->desc->max_msg_size);
256		ti_sci_dump_header_dbg(dev, hdr);
257		return;
258	}
259	if (mbox_msg->len < xfer->rx_len) {
260		dev_err(dev, "Recv xfer %zu < expected %d length\n",
261			mbox_msg->len, xfer->rx_len);
262		ti_sci_dump_header_dbg(dev, hdr);
263		return;
264	}
265
266	ti_sci_dump_header_dbg(dev, hdr);
267	/* Take a copy to the rx buffer.. */
268	memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
269	complete(&xfer->done);
270}
271
272/**
273 * ti_sci_get_one_xfer() - Allocate one message
274 * @info:	Pointer to SCI entity information
275 * @msg_type:	Message type
276 * @msg_flags:	Flag to set for the message
277 * @tx_message_size: transmit message size
278 * @rx_message_size: receive message size
279 *
280 * Helper function which is used by various command functions that are
281 * exposed to clients of this driver for allocating a message traffic event.
282 *
283 * This function can sleep depending on pending requests already in the system
284 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
285 * of internal data structures.
286 *
287 * Return: 0 if all went fine, else corresponding error.
288 */
289static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
290					       u16 msg_type, u32 msg_flags,
291					       size_t tx_message_size,
292					       size_t rx_message_size)
293{
294	struct ti_sci_xfers_info *minfo = &info->minfo;
295	struct ti_sci_xfer *xfer;
296	struct ti_sci_msg_hdr *hdr;
297	unsigned long flags;
298	unsigned long bit_pos;
299	u8 xfer_id;
300	int ret;
301	int timeout;
302
303	/* Ensure we have sane transfer sizes */
304	if (rx_message_size > info->desc->max_msg_size ||
305	    tx_message_size > info->desc->max_msg_size ||
306	    rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
307		return ERR_PTR(-ERANGE);
308
309	/*
310	 * Ensure we have only controlled number of pending messages.
311	 * Ideally, we might just have to wait a single message, be
312	 * conservative and wait 5 times that..
313	 */
314	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
315	ret = down_timeout(&minfo->sem_xfer_count, timeout);
316	if (ret < 0)
317		return ERR_PTR(ret);
318
319	/* Keep the locked section as small as possible */
320	spin_lock_irqsave(&minfo->xfer_lock, flags);
321	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
322				      info->desc->max_msgs);
323	set_bit(bit_pos, minfo->xfer_alloc_table);
324	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
325
326	/*
327	 * We already ensured in probe that we can have max messages that can
328	 * fit in  hdr.seq - NOTE: this improves access latencies
329	 * to predictable O(1) access, BUT, it opens us to risk if
330	 * remote misbehaves with corrupted message sequence responses.
331	 * If that happens, we are going to be messed up anyways..
332	 */
333	xfer_id = (u8)bit_pos;
334
335	xfer = &minfo->xfer_block[xfer_id];
336
337	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
338	xfer->tx_message.len = tx_message_size;
339	xfer->tx_message.chan_rx = info->chan_rx;
340	xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms;
341	xfer->rx_len = (u8)rx_message_size;
342
343	reinit_completion(&xfer->done);
344
345	hdr->seq = xfer_id;
346	hdr->type = msg_type;
347	hdr->host = info->host_id;
348	hdr->flags = msg_flags;
349
350	return xfer;
351}
352
353/**
354 * ti_sci_put_one_xfer() - Release a message
355 * @minfo:	transfer info pointer
356 * @xfer:	message that was reserved by ti_sci_get_one_xfer
357 *
358 * This holds a spinlock to maintain integrity of internal data structures.
359 */
360static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
361				struct ti_sci_xfer *xfer)
362{
363	unsigned long flags;
364	struct ti_sci_msg_hdr *hdr;
365	u8 xfer_id;
366
367	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368	xfer_id = hdr->seq;
369
370	/*
371	 * Keep the locked section as small as possible
372	 * NOTE: we might escape with smp_mb and no lock here..
373	 * but just be conservative and symmetric.
374	 */
375	spin_lock_irqsave(&minfo->xfer_lock, flags);
376	clear_bit(xfer_id, minfo->xfer_alloc_table);
377	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
378
379	/* Increment the count for the next user to get through */
380	up(&minfo->sem_xfer_count);
381}
382
383/**
384 * ti_sci_do_xfer() - Do one transfer
385 * @info:	Pointer to SCI entity information
386 * @xfer:	Transfer to initiate and wait for response
387 *
388 * Return: -ETIMEDOUT in case of no response, if transmit error,
389 *	   return corresponding error, else if all goes well,
390 *	   return 0.
391 */
392static inline int ti_sci_do_xfer(struct ti_sci_info *info,
393				 struct ti_sci_xfer *xfer)
394{
395	int ret;
396	int timeout;
397	struct device *dev = info->dev;
398	bool done_state = true;
399
400	ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
401	if (ret < 0)
402		return ret;
403
404	ret = 0;
405
406	if (system_state <= SYSTEM_RUNNING) {
407		/* And we wait for the response. */
408		timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
409		if (!wait_for_completion_timeout(&xfer->done, timeout))
410			ret = -ETIMEDOUT;
411	} else {
412		/*
413		 * If we are !running, we cannot use wait_for_completion_timeout
414		 * during noirq phase, so we must manually poll the completion.
415		 */
416		ret = read_poll_timeout_atomic(try_wait_for_completion, done_state,
417					       done_state, 1,
418					       info->desc->max_rx_timeout_ms * 1000,
419					       false, &xfer->done);
420	}
421
422	if (ret == -ETIMEDOUT)
423		dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
424			(void *)_RET_IP_);
425
426	/*
427	 * NOTE: we might prefer not to need the mailbox ticker to manage the
428	 * transfer queueing since the protocol layer queues things by itself.
429	 * Unfortunately, we have to kick the mailbox framework after we have
430	 * received our message.
431	 */
432	mbox_client_txdone(info->chan_tx, ret);
433
434	return ret;
435}
436
437/**
438 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
439 * @info:	Pointer to SCI entity information
440 *
441 * Updates the SCI information in the internal data structure.
442 *
443 * Return: 0 if all went fine, else return appropriate error.
444 */
445static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
446{
447	struct device *dev = info->dev;
448	struct ti_sci_handle *handle = &info->handle;
449	struct ti_sci_version_info *ver = &handle->version;
450	struct ti_sci_msg_resp_version *rev_info;
451	struct ti_sci_xfer *xfer;
452	int ret;
453
454	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
455				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
456				   sizeof(struct ti_sci_msg_hdr),
457				   sizeof(*rev_info));
458	if (IS_ERR(xfer)) {
459		ret = PTR_ERR(xfer);
460		dev_err(dev, "Message alloc failed(%d)\n", ret);
461		return ret;
462	}
463
464	rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
465
466	ret = ti_sci_do_xfer(info, xfer);
467	if (ret) {
468		dev_err(dev, "Mbox send fail %d\n", ret);
469		goto fail;
470	}
471
472	ver->abi_major = rev_info->abi_major;
473	ver->abi_minor = rev_info->abi_minor;
474	ver->firmware_revision = rev_info->firmware_revision;
475	strncpy(ver->firmware_description, rev_info->firmware_description,
476		sizeof(ver->firmware_description));
477
478fail:
479	ti_sci_put_one_xfer(&info->minfo, xfer);
480	return ret;
481}
482
483/**
484 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
485 * @r:	pointer to response buffer
486 *
487 * Return: true if the response was an ACK, else returns false.
488 */
489static inline bool ti_sci_is_response_ack(void *r)
490{
491	struct ti_sci_msg_hdr *hdr = r;
492
493	return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
494}
495
496/**
497 * ti_sci_set_device_state() - Set device state helper
498 * @handle:	pointer to TI SCI handle
499 * @id:		Device identifier
500 * @flags:	flags to setup for the device
501 * @state:	State to move the device to
502 *
503 * Return: 0 if all went well, else returns appropriate error value.
504 */
505static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
506				   u32 id, u32 flags, u8 state)
507{
508	struct ti_sci_info *info;
509	struct ti_sci_msg_req_set_device_state *req;
510	struct ti_sci_msg_hdr *resp;
511	struct ti_sci_xfer *xfer;
512	struct device *dev;
513	int ret = 0;
514
515	if (IS_ERR(handle))
516		return PTR_ERR(handle);
517	if (!handle)
518		return -EINVAL;
519
520	info = handle_to_ti_sci_info(handle);
521	dev = info->dev;
522
523	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
524				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
525				   sizeof(*req), sizeof(*resp));
526	if (IS_ERR(xfer)) {
527		ret = PTR_ERR(xfer);
528		dev_err(dev, "Message alloc failed(%d)\n", ret);
529		return ret;
530	}
531	req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
532	req->id = id;
533	req->state = state;
534
535	ret = ti_sci_do_xfer(info, xfer);
536	if (ret) {
537		dev_err(dev, "Mbox send fail %d\n", ret);
538		goto fail;
539	}
540
541	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
542
543	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
544
545fail:
546	ti_sci_put_one_xfer(&info->minfo, xfer);
547
548	return ret;
549}
550
551/**
552 * ti_sci_get_device_state() - Get device state helper
553 * @handle:	Handle to the device
554 * @id:		Device Identifier
555 * @clcnt:	Pointer to Context Loss Count
556 * @resets:	pointer to resets
557 * @p_state:	pointer to p_state
558 * @c_state:	pointer to c_state
559 *
560 * Return: 0 if all went fine, else return appropriate error.
561 */
562static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
563				   u32 id,  u32 *clcnt,  u32 *resets,
564				    u8 *p_state,  u8 *c_state)
565{
566	struct ti_sci_info *info;
567	struct ti_sci_msg_req_get_device_state *req;
568	struct ti_sci_msg_resp_get_device_state *resp;
569	struct ti_sci_xfer *xfer;
570	struct device *dev;
571	int ret = 0;
572
573	if (IS_ERR(handle))
574		return PTR_ERR(handle);
575	if (!handle)
576		return -EINVAL;
577
578	if (!clcnt && !resets && !p_state && !c_state)
579		return -EINVAL;
580
581	info = handle_to_ti_sci_info(handle);
582	dev = info->dev;
583
584	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
585				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
586				   sizeof(*req), sizeof(*resp));
587	if (IS_ERR(xfer)) {
588		ret = PTR_ERR(xfer);
589		dev_err(dev, "Message alloc failed(%d)\n", ret);
590		return ret;
591	}
592	req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
593	req->id = id;
594
595	ret = ti_sci_do_xfer(info, xfer);
596	if (ret) {
597		dev_err(dev, "Mbox send fail %d\n", ret);
598		goto fail;
599	}
600
601	resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
602	if (!ti_sci_is_response_ack(resp)) {
603		ret = -ENODEV;
604		goto fail;
605	}
606
607	if (clcnt)
608		*clcnt = resp->context_loss_count;
609	if (resets)
610		*resets = resp->resets;
611	if (p_state)
612		*p_state = resp->programmed_state;
613	if (c_state)
614		*c_state = resp->current_state;
615fail:
616	ti_sci_put_one_xfer(&info->minfo, xfer);
617
618	return ret;
619}
620
621/**
622 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
623 *			     that can be shared with other hosts.
624 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
625 * @id:		Device Identifier
626 *
627 * Request for the device - NOTE: the client MUST maintain integrity of
628 * usage count by balancing get_device with put_device. No refcounting is
629 * managed by driver for that purpose.
630 *
631 * Return: 0 if all went fine, else return appropriate error.
632 */
633static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
634{
635	return ti_sci_set_device_state(handle, id, 0,
636				       MSG_DEVICE_SW_STATE_ON);
637}
638
639/**
640 * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
641 *				       TISCI that is exclusively owned by the
642 *				       requesting host.
643 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
644 * @id:		Device Identifier
645 *
646 * Request for the device - NOTE: the client MUST maintain integrity of
647 * usage count by balancing get_device with put_device. No refcounting is
648 * managed by driver for that purpose.
649 *
650 * Return: 0 if all went fine, else return appropriate error.
651 */
652static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
653					   u32 id)
654{
655	return ti_sci_set_device_state(handle, id,
656				       MSG_FLAG_DEVICE_EXCLUSIVE,
657				       MSG_DEVICE_SW_STATE_ON);
658}
659
660/**
661 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
662 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
663 * @id:		Device Identifier
664 *
665 * Request for the device - NOTE: the client MUST maintain integrity of
666 * usage count by balancing get_device with put_device. No refcounting is
667 * managed by driver for that purpose.
668 *
669 * Return: 0 if all went fine, else return appropriate error.
670 */
671static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
672{
673	return ti_sci_set_device_state(handle, id, 0,
674				       MSG_DEVICE_SW_STATE_RETENTION);
675}
676
677/**
678 * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
679 *					TISCI that is exclusively owned by
680 *					requesting host.
681 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
682 * @id:		Device Identifier
683 *
684 * Request for the device - NOTE: the client MUST maintain integrity of
685 * usage count by balancing get_device with put_device. No refcounting is
686 * managed by driver for that purpose.
687 *
688 * Return: 0 if all went fine, else return appropriate error.
689 */
690static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
691					    u32 id)
692{
693	return ti_sci_set_device_state(handle, id,
694				       MSG_FLAG_DEVICE_EXCLUSIVE,
695				       MSG_DEVICE_SW_STATE_RETENTION);
696}
697
698/**
699 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
700 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
701 * @id:		Device Identifier
702 *
703 * Request for the device - NOTE: the client MUST maintain integrity of
704 * usage count by balancing get_device with put_device. No refcounting is
705 * managed by driver for that purpose.
706 *
707 * Return: 0 if all went fine, else return appropriate error.
708 */
709static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
710{
711	return ti_sci_set_device_state(handle, id,
712				       0, MSG_DEVICE_SW_STATE_AUTO_OFF);
713}
714
715/**
716 * ti_sci_cmd_dev_is_valid() - Is the device valid
717 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
718 * @id:		Device Identifier
719 *
720 * Return: 0 if all went fine and the device ID is valid, else return
721 * appropriate error.
722 */
723static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
724{
725	u8 unused;
726
727	/* check the device state which will also tell us if the ID is valid */
728	return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
729}
730
731/**
732 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
733 * @handle:	Pointer to TISCI handle
734 * @id:		Device Identifier
735 * @count:	Pointer to Context Loss counter to populate
736 *
737 * Return: 0 if all went fine, else return appropriate error.
738 */
739static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
740				    u32 *count)
741{
742	return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
743}
744
745/**
746 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
747 * @handle:	Pointer to TISCI handle
748 * @id:		Device Identifier
749 * @r_state:	true if requested to be idle
750 *
751 * Return: 0 if all went fine, else return appropriate error.
752 */
753static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
754				  bool *r_state)
755{
756	int ret;
757	u8 state;
758
759	if (!r_state)
760		return -EINVAL;
761
762	ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
763	if (ret)
764		return ret;
765
766	*r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
767
768	return 0;
769}
770
771/**
772 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
773 * @handle:	Pointer to TISCI handle
774 * @id:		Device Identifier
775 * @r_state:	true if requested to be stopped
776 * @curr_state:	true if currently stopped.
777 *
778 * Return: 0 if all went fine, else return appropriate error.
779 */
780static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
781				  bool *r_state,  bool *curr_state)
782{
783	int ret;
784	u8 p_state, c_state;
785
786	if (!r_state && !curr_state)
787		return -EINVAL;
788
789	ret =
790	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
791	if (ret)
792		return ret;
793
794	if (r_state)
795		*r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
796	if (curr_state)
797		*curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
798
799	return 0;
800}
801
802/**
803 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
804 * @handle:	Pointer to TISCI handle
805 * @id:		Device Identifier
806 * @r_state:	true if requested to be ON
807 * @curr_state:	true if currently ON and active
808 *
809 * Return: 0 if all went fine, else return appropriate error.
810 */
811static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
812				bool *r_state,  bool *curr_state)
813{
814	int ret;
815	u8 p_state, c_state;
816
817	if (!r_state && !curr_state)
818		return -EINVAL;
819
820	ret =
821	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
822	if (ret)
823		return ret;
824
825	if (r_state)
826		*r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
827	if (curr_state)
828		*curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
829
830	return 0;
831}
832
833/**
834 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
835 * @handle:	Pointer to TISCI handle
836 * @id:		Device Identifier
837 * @curr_state:	true if currently transitioning.
838 *
839 * Return: 0 if all went fine, else return appropriate error.
840 */
841static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
842				   bool *curr_state)
843{
844	int ret;
845	u8 state;
846
847	if (!curr_state)
848		return -EINVAL;
849
850	ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
851	if (ret)
852		return ret;
853
854	*curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
855
856	return 0;
857}
858
859/**
860 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
861 *				    by TISCI
862 * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
863 * @id:		Device Identifier
864 * @reset_state: Device specific reset bit field
865 *
866 * Return: 0 if all went fine, else return appropriate error.
867 */
868static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
869					u32 id, u32 reset_state)
870{
871	struct ti_sci_info *info;
872	struct ti_sci_msg_req_set_device_resets *req;
873	struct ti_sci_msg_hdr *resp;
874	struct ti_sci_xfer *xfer;
875	struct device *dev;
876	int ret = 0;
877
878	if (IS_ERR(handle))
879		return PTR_ERR(handle);
880	if (!handle)
881		return -EINVAL;
882
883	info = handle_to_ti_sci_info(handle);
884	dev = info->dev;
885
886	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
887				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
888				   sizeof(*req), sizeof(*resp));
889	if (IS_ERR(xfer)) {
890		ret = PTR_ERR(xfer);
891		dev_err(dev, "Message alloc failed(%d)\n", ret);
892		return ret;
893	}
894	req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
895	req->id = id;
896	req->resets = reset_state;
897
898	ret = ti_sci_do_xfer(info, xfer);
899	if (ret) {
900		dev_err(dev, "Mbox send fail %d\n", ret);
901		goto fail;
902	}
903
904	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
905
906	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
907
908fail:
909	ti_sci_put_one_xfer(&info->minfo, xfer);
910
911	return ret;
912}
913
914/**
915 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
916 *				    by TISCI
917 * @handle:		Pointer to TISCI handle
918 * @id:			Device Identifier
919 * @reset_state:	Pointer to reset state to populate
920 *
921 * Return: 0 if all went fine, else return appropriate error.
922 */
923static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
924					u32 id, u32 *reset_state)
925{
926	return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
927				       NULL);
928}
929
930/**
931 * ti_sci_set_clock_state() - Set clock state helper
932 * @handle:	pointer to TI SCI handle
933 * @dev_id:	Device identifier this request is for
934 * @clk_id:	Clock identifier for the device for this request.
935 *		Each device has it's own set of clock inputs. This indexes
936 *		which clock input to modify.
937 * @flags:	Header flags as needed
938 * @state:	State to request for the clock.
939 *
940 * Return: 0 if all went well, else returns appropriate error value.
941 */
942static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
943				  u32 dev_id, u32 clk_id,
944				  u32 flags, u8 state)
945{
946	struct ti_sci_info *info;
947	struct ti_sci_msg_req_set_clock_state *req;
948	struct ti_sci_msg_hdr *resp;
949	struct ti_sci_xfer *xfer;
950	struct device *dev;
951	int ret = 0;
952
953	if (IS_ERR(handle))
954		return PTR_ERR(handle);
955	if (!handle)
956		return -EINVAL;
957
958	info = handle_to_ti_sci_info(handle);
959	dev = info->dev;
960
961	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
962				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
963				   sizeof(*req), sizeof(*resp));
964	if (IS_ERR(xfer)) {
965		ret = PTR_ERR(xfer);
966		dev_err(dev, "Message alloc failed(%d)\n", ret);
967		return ret;
968	}
969	req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
970	req->dev_id = dev_id;
971	if (clk_id < 255) {
972		req->clk_id = clk_id;
973	} else {
974		req->clk_id = 255;
975		req->clk_id_32 = clk_id;
976	}
977	req->request_state = state;
978
979	ret = ti_sci_do_xfer(info, xfer);
980	if (ret) {
981		dev_err(dev, "Mbox send fail %d\n", ret);
982		goto fail;
983	}
984
985	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
986
987	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
988
989fail:
990	ti_sci_put_one_xfer(&info->minfo, xfer);
991
992	return ret;
993}
994
995/**
996 * ti_sci_cmd_get_clock_state() - Get clock state helper
997 * @handle:	pointer to TI SCI handle
998 * @dev_id:	Device identifier this request is for
999 * @clk_id:	Clock identifier for the device for this request.
1000 *		Each device has it's own set of clock inputs. This indexes
1001 *		which clock input to modify.
1002 * @programmed_state:	State requested for clock to move to
1003 * @current_state:	State that the clock is currently in
1004 *
1005 * Return: 0 if all went well, else returns appropriate error value.
1006 */
1007static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
1008				      u32 dev_id, u32 clk_id,
1009				      u8 *programmed_state, u8 *current_state)
1010{
1011	struct ti_sci_info *info;
1012	struct ti_sci_msg_req_get_clock_state *req;
1013	struct ti_sci_msg_resp_get_clock_state *resp;
1014	struct ti_sci_xfer *xfer;
1015	struct device *dev;
1016	int ret = 0;
1017
1018	if (IS_ERR(handle))
1019		return PTR_ERR(handle);
1020	if (!handle)
1021		return -EINVAL;
1022
1023	if (!programmed_state && !current_state)
1024		return -EINVAL;
1025
1026	info = handle_to_ti_sci_info(handle);
1027	dev = info->dev;
1028
1029	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1030				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1031				   sizeof(*req), sizeof(*resp));
1032	if (IS_ERR(xfer)) {
1033		ret = PTR_ERR(xfer);
1034		dev_err(dev, "Message alloc failed(%d)\n", ret);
1035		return ret;
1036	}
1037	req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1038	req->dev_id = dev_id;
1039	if (clk_id < 255) {
1040		req->clk_id = clk_id;
1041	} else {
1042		req->clk_id = 255;
1043		req->clk_id_32 = clk_id;
1044	}
1045
1046	ret = ti_sci_do_xfer(info, xfer);
1047	if (ret) {
1048		dev_err(dev, "Mbox send fail %d\n", ret);
1049		goto fail;
1050	}
1051
1052	resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1053
1054	if (!ti_sci_is_response_ack(resp)) {
1055		ret = -ENODEV;
1056		goto fail;
1057	}
1058
1059	if (programmed_state)
1060		*programmed_state = resp->programmed_state;
1061	if (current_state)
1062		*current_state = resp->current_state;
1063
1064fail:
1065	ti_sci_put_one_xfer(&info->minfo, xfer);
1066
1067	return ret;
1068}
1069
1070/**
1071 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1072 * @handle:	pointer to TI SCI handle
1073 * @dev_id:	Device identifier this request is for
1074 * @clk_id:	Clock identifier for the device for this request.
1075 *		Each device has it's own set of clock inputs. This indexes
1076 *		which clock input to modify.
1077 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1078 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1079 * @enable_input_term: 'true' if input termination is desired, else 'false'
1080 *
1081 * Return: 0 if all went well, else returns appropriate error value.
1082 */
1083static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1084				u32 clk_id, bool needs_ssc,
1085				bool can_change_freq, bool enable_input_term)
1086{
1087	u32 flags = 0;
1088
1089	flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1090	flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1091	flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1092
1093	return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1094				      MSG_CLOCK_SW_STATE_REQ);
1095}
1096
1097/**
1098 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1099 * @handle:	pointer to TI SCI handle
1100 * @dev_id:	Device identifier this request is for
1101 * @clk_id:	Clock identifier for the device for this request.
1102 *		Each device has it's own set of clock inputs. This indexes
1103 *		which clock input to modify.
1104 *
1105 * NOTE: This clock must have been requested by get_clock previously.
1106 *
1107 * Return: 0 if all went well, else returns appropriate error value.
1108 */
1109static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1110				 u32 dev_id, u32 clk_id)
1111{
1112	return ti_sci_set_clock_state(handle, dev_id, clk_id,
1113				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1114				      MSG_CLOCK_SW_STATE_UNREQ);
1115}
1116
1117/**
1118 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1119 * @handle:	pointer to TI SCI handle
1120 * @dev_id:	Device identifier this request is for
1121 * @clk_id:	Clock identifier for the device for this request.
1122 *		Each device has it's own set of clock inputs. This indexes
1123 *		which clock input to modify.
1124 *
1125 * NOTE: This clock must have been requested by get_clock previously.
1126 *
1127 * Return: 0 if all went well, else returns appropriate error value.
1128 */
1129static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1130				u32 dev_id, u32 clk_id)
1131{
1132	return ti_sci_set_clock_state(handle, dev_id, clk_id,
1133				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1134				      MSG_CLOCK_SW_STATE_AUTO);
1135}
1136
1137/**
1138 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1139 * @handle:	pointer to TI SCI handle
1140 * @dev_id:	Device identifier this request is for
1141 * @clk_id:	Clock identifier for the device for this request.
1142 *		Each device has it's own set of clock inputs. This indexes
1143 *		which clock input to modify.
1144 * @req_state: state indicating if the clock is auto managed
1145 *
1146 * Return: 0 if all went well, else returns appropriate error value.
1147 */
1148static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1149				  u32 dev_id, u32 clk_id, bool *req_state)
1150{
1151	u8 state = 0;
1152	int ret;
1153
1154	if (!req_state)
1155		return -EINVAL;
1156
1157	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1158	if (ret)
1159		return ret;
1160
1161	*req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1162	return 0;
1163}
1164
1165/**
1166 * ti_sci_cmd_clk_is_on() - Is the clock ON
1167 * @handle:	pointer to TI SCI handle
1168 * @dev_id:	Device identifier this request is for
1169 * @clk_id:	Clock identifier for the device for this request.
1170 *		Each device has it's own set of clock inputs. This indexes
1171 *		which clock input to modify.
1172 * @req_state: state indicating if the clock is managed by us and enabled
1173 * @curr_state: state indicating if the clock is ready for operation
1174 *
1175 * Return: 0 if all went well, else returns appropriate error value.
1176 */
1177static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1178				u32 clk_id, bool *req_state, bool *curr_state)
1179{
1180	u8 c_state = 0, r_state = 0;
1181	int ret;
1182
1183	if (!req_state && !curr_state)
1184		return -EINVAL;
1185
1186	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1187					 &r_state, &c_state);
1188	if (ret)
1189		return ret;
1190
1191	if (req_state)
1192		*req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1193	if (curr_state)
1194		*curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1195	return 0;
1196}
1197
1198/**
1199 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1200 * @handle:	pointer to TI SCI handle
1201 * @dev_id:	Device identifier this request is for
1202 * @clk_id:	Clock identifier for the device for this request.
1203 *		Each device has it's own set of clock inputs. This indexes
1204 *		which clock input to modify.
1205 * @req_state: state indicating if the clock is managed by us and disabled
1206 * @curr_state: state indicating if the clock is NOT ready for operation
1207 *
1208 * Return: 0 if all went well, else returns appropriate error value.
1209 */
1210static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1211				 u32 clk_id, bool *req_state, bool *curr_state)
1212{
1213	u8 c_state = 0, r_state = 0;
1214	int ret;
1215
1216	if (!req_state && !curr_state)
1217		return -EINVAL;
1218
1219	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1220					 &r_state, &c_state);
1221	if (ret)
1222		return ret;
1223
1224	if (req_state)
1225		*req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1226	if (curr_state)
1227		*curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1228	return 0;
1229}
1230
1231/**
1232 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1233 * @handle:	pointer to TI SCI handle
1234 * @dev_id:	Device identifier this request is for
1235 * @clk_id:	Clock identifier for the device for this request.
1236 *		Each device has it's own set of clock inputs. This indexes
1237 *		which clock input to modify.
1238 * @parent_id:	Parent clock identifier to set
1239 *
1240 * Return: 0 if all went well, else returns appropriate error value.
1241 */
1242static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1243				     u32 dev_id, u32 clk_id, u32 parent_id)
1244{
1245	struct ti_sci_info *info;
1246	struct ti_sci_msg_req_set_clock_parent *req;
1247	struct ti_sci_msg_hdr *resp;
1248	struct ti_sci_xfer *xfer;
1249	struct device *dev;
1250	int ret = 0;
1251
1252	if (IS_ERR(handle))
1253		return PTR_ERR(handle);
1254	if (!handle)
1255		return -EINVAL;
1256
1257	info = handle_to_ti_sci_info(handle);
1258	dev = info->dev;
1259
1260	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1261				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1262				   sizeof(*req), sizeof(*resp));
1263	if (IS_ERR(xfer)) {
1264		ret = PTR_ERR(xfer);
1265		dev_err(dev, "Message alloc failed(%d)\n", ret);
1266		return ret;
1267	}
1268	req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1269	req->dev_id = dev_id;
1270	if (clk_id < 255) {
1271		req->clk_id = clk_id;
1272	} else {
1273		req->clk_id = 255;
1274		req->clk_id_32 = clk_id;
1275	}
1276	if (parent_id < 255) {
1277		req->parent_id = parent_id;
1278	} else {
1279		req->parent_id = 255;
1280		req->parent_id_32 = parent_id;
1281	}
1282
1283	ret = ti_sci_do_xfer(info, xfer);
1284	if (ret) {
1285		dev_err(dev, "Mbox send fail %d\n", ret);
1286		goto fail;
1287	}
1288
1289	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1290
1291	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1292
1293fail:
1294	ti_sci_put_one_xfer(&info->minfo, xfer);
1295
1296	return ret;
1297}
1298
1299/**
1300 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1301 * @handle:	pointer to TI SCI handle
1302 * @dev_id:	Device identifier this request is for
1303 * @clk_id:	Clock identifier for the device for this request.
1304 *		Each device has it's own set of clock inputs. This indexes
1305 *		which clock input to modify.
1306 * @parent_id:	Current clock parent
1307 *
1308 * Return: 0 if all went well, else returns appropriate error value.
1309 */
1310static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1311				     u32 dev_id, u32 clk_id, u32 *parent_id)
1312{
1313	struct ti_sci_info *info;
1314	struct ti_sci_msg_req_get_clock_parent *req;
1315	struct ti_sci_msg_resp_get_clock_parent *resp;
1316	struct ti_sci_xfer *xfer;
1317	struct device *dev;
1318	int ret = 0;
1319
1320	if (IS_ERR(handle))
1321		return PTR_ERR(handle);
1322	if (!handle || !parent_id)
1323		return -EINVAL;
1324
1325	info = handle_to_ti_sci_info(handle);
1326	dev = info->dev;
1327
1328	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1329				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1330				   sizeof(*req), sizeof(*resp));
1331	if (IS_ERR(xfer)) {
1332		ret = PTR_ERR(xfer);
1333		dev_err(dev, "Message alloc failed(%d)\n", ret);
1334		return ret;
1335	}
1336	req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1337	req->dev_id = dev_id;
1338	if (clk_id < 255) {
1339		req->clk_id = clk_id;
1340	} else {
1341		req->clk_id = 255;
1342		req->clk_id_32 = clk_id;
1343	}
1344
1345	ret = ti_sci_do_xfer(info, xfer);
1346	if (ret) {
1347		dev_err(dev, "Mbox send fail %d\n", ret);
1348		goto fail;
1349	}
1350
1351	resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1352
1353	if (!ti_sci_is_response_ack(resp)) {
1354		ret = -ENODEV;
1355	} else {
1356		if (resp->parent_id < 255)
1357			*parent_id = resp->parent_id;
1358		else
1359			*parent_id = resp->parent_id_32;
1360	}
1361
1362fail:
1363	ti_sci_put_one_xfer(&info->minfo, xfer);
1364
1365	return ret;
1366}
1367
1368/**
1369 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1370 * @handle:	pointer to TI SCI handle
1371 * @dev_id:	Device identifier this request is for
1372 * @clk_id:	Clock identifier for the device for this request.
1373 *		Each device has it's own set of clock inputs. This indexes
1374 *		which clock input to modify.
1375 * @num_parents: Returns he number of parents to the current clock.
1376 *
1377 * Return: 0 if all went well, else returns appropriate error value.
1378 */
1379static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1380					  u32 dev_id, u32 clk_id,
1381					  u32 *num_parents)
1382{
1383	struct ti_sci_info *info;
1384	struct ti_sci_msg_req_get_clock_num_parents *req;
1385	struct ti_sci_msg_resp_get_clock_num_parents *resp;
1386	struct ti_sci_xfer *xfer;
1387	struct device *dev;
1388	int ret = 0;
1389
1390	if (IS_ERR(handle))
1391		return PTR_ERR(handle);
1392	if (!handle || !num_parents)
1393		return -EINVAL;
1394
1395	info = handle_to_ti_sci_info(handle);
1396	dev = info->dev;
1397
1398	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1399				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1400				   sizeof(*req), sizeof(*resp));
1401	if (IS_ERR(xfer)) {
1402		ret = PTR_ERR(xfer);
1403		dev_err(dev, "Message alloc failed(%d)\n", ret);
1404		return ret;
1405	}
1406	req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1407	req->dev_id = dev_id;
1408	if (clk_id < 255) {
1409		req->clk_id = clk_id;
1410	} else {
1411		req->clk_id = 255;
1412		req->clk_id_32 = clk_id;
1413	}
1414
1415	ret = ti_sci_do_xfer(info, xfer);
1416	if (ret) {
1417		dev_err(dev, "Mbox send fail %d\n", ret);
1418		goto fail;
1419	}
1420
1421	resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1422
1423	if (!ti_sci_is_response_ack(resp)) {
1424		ret = -ENODEV;
1425	} else {
1426		if (resp->num_parents < 255)
1427			*num_parents = resp->num_parents;
1428		else
1429			*num_parents = resp->num_parents_32;
1430	}
1431
1432fail:
1433	ti_sci_put_one_xfer(&info->minfo, xfer);
1434
1435	return ret;
1436}
1437
1438/**
1439 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1440 * @handle:	pointer to TI SCI handle
1441 * @dev_id:	Device identifier this request is for
1442 * @clk_id:	Clock identifier for the device for this request.
1443 *		Each device has it's own set of clock inputs. This indexes
1444 *		which clock input to modify.
1445 * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
1446 *		allowable programmed frequency and does not account for clock
1447 *		tolerances and jitter.
1448 * @target_freq: The target clock frequency in Hz. A frequency will be
1449 *		processed as close to this target frequency as possible.
1450 * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
1451 *		allowable programmed frequency and does not account for clock
1452 *		tolerances and jitter.
1453 * @match_freq:	Frequency match in Hz response.
1454 *
1455 * Return: 0 if all went well, else returns appropriate error value.
1456 */
1457static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1458					 u32 dev_id, u32 clk_id, u64 min_freq,
1459					 u64 target_freq, u64 max_freq,
1460					 u64 *match_freq)
1461{
1462	struct ti_sci_info *info;
1463	struct ti_sci_msg_req_query_clock_freq *req;
1464	struct ti_sci_msg_resp_query_clock_freq *resp;
1465	struct ti_sci_xfer *xfer;
1466	struct device *dev;
1467	int ret = 0;
1468
1469	if (IS_ERR(handle))
1470		return PTR_ERR(handle);
1471	if (!handle || !match_freq)
1472		return -EINVAL;
1473
1474	info = handle_to_ti_sci_info(handle);
1475	dev = info->dev;
1476
1477	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1478				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1479				   sizeof(*req), sizeof(*resp));
1480	if (IS_ERR(xfer)) {
1481		ret = PTR_ERR(xfer);
1482		dev_err(dev, "Message alloc failed(%d)\n", ret);
1483		return ret;
1484	}
1485	req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1486	req->dev_id = dev_id;
1487	if (clk_id < 255) {
1488		req->clk_id = clk_id;
1489	} else {
1490		req->clk_id = 255;
1491		req->clk_id_32 = clk_id;
1492	}
1493	req->min_freq_hz = min_freq;
1494	req->target_freq_hz = target_freq;
1495	req->max_freq_hz = max_freq;
1496
1497	ret = ti_sci_do_xfer(info, xfer);
1498	if (ret) {
1499		dev_err(dev, "Mbox send fail %d\n", ret);
1500		goto fail;
1501	}
1502
1503	resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1504
1505	if (!ti_sci_is_response_ack(resp))
1506		ret = -ENODEV;
1507	else
1508		*match_freq = resp->freq_hz;
1509
1510fail:
1511	ti_sci_put_one_xfer(&info->minfo, xfer);
1512
1513	return ret;
1514}
1515
1516/**
1517 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1518 * @handle:	pointer to TI SCI handle
1519 * @dev_id:	Device identifier this request is for
1520 * @clk_id:	Clock identifier for the device for this request.
1521 *		Each device has it's own set of clock inputs. This indexes
1522 *		which clock input to modify.
1523 * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
1524 *		allowable programmed frequency and does not account for clock
1525 *		tolerances and jitter.
1526 * @target_freq: The target clock frequency in Hz. A frequency will be
1527 *		processed as close to this target frequency as possible.
1528 * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
1529 *		allowable programmed frequency and does not account for clock
1530 *		tolerances and jitter.
1531 *
1532 * Return: 0 if all went well, else returns appropriate error value.
1533 */
1534static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1535				   u32 dev_id, u32 clk_id, u64 min_freq,
1536				   u64 target_freq, u64 max_freq)
1537{
1538	struct ti_sci_info *info;
1539	struct ti_sci_msg_req_set_clock_freq *req;
1540	struct ti_sci_msg_hdr *resp;
1541	struct ti_sci_xfer *xfer;
1542	struct device *dev;
1543	int ret = 0;
1544
1545	if (IS_ERR(handle))
1546		return PTR_ERR(handle);
1547	if (!handle)
1548		return -EINVAL;
1549
1550	info = handle_to_ti_sci_info(handle);
1551	dev = info->dev;
1552
1553	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1554				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1555				   sizeof(*req), sizeof(*resp));
1556	if (IS_ERR(xfer)) {
1557		ret = PTR_ERR(xfer);
1558		dev_err(dev, "Message alloc failed(%d)\n", ret);
1559		return ret;
1560	}
1561	req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1562	req->dev_id = dev_id;
1563	if (clk_id < 255) {
1564		req->clk_id = clk_id;
1565	} else {
1566		req->clk_id = 255;
1567		req->clk_id_32 = clk_id;
1568	}
1569	req->min_freq_hz = min_freq;
1570	req->target_freq_hz = target_freq;
1571	req->max_freq_hz = max_freq;
1572
1573	ret = ti_sci_do_xfer(info, xfer);
1574	if (ret) {
1575		dev_err(dev, "Mbox send fail %d\n", ret);
1576		goto fail;
1577	}
1578
1579	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1580
1581	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1582
1583fail:
1584	ti_sci_put_one_xfer(&info->minfo, xfer);
1585
1586	return ret;
1587}
1588
1589/**
1590 * ti_sci_cmd_clk_get_freq() - Get current frequency
1591 * @handle:	pointer to TI SCI handle
1592 * @dev_id:	Device identifier this request is for
1593 * @clk_id:	Clock identifier for the device for this request.
1594 *		Each device has it's own set of clock inputs. This indexes
1595 *		which clock input to modify.
1596 * @freq:	Currently frequency in Hz
1597 *
1598 * Return: 0 if all went well, else returns appropriate error value.
1599 */
1600static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1601				   u32 dev_id, u32 clk_id, u64 *freq)
1602{
1603	struct ti_sci_info *info;
1604	struct ti_sci_msg_req_get_clock_freq *req;
1605	struct ti_sci_msg_resp_get_clock_freq *resp;
1606	struct ti_sci_xfer *xfer;
1607	struct device *dev;
1608	int ret = 0;
1609
1610	if (IS_ERR(handle))
1611		return PTR_ERR(handle);
1612	if (!handle || !freq)
1613		return -EINVAL;
1614
1615	info = handle_to_ti_sci_info(handle);
1616	dev = info->dev;
1617
1618	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1619				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1620				   sizeof(*req), sizeof(*resp));
1621	if (IS_ERR(xfer)) {
1622		ret = PTR_ERR(xfer);
1623		dev_err(dev, "Message alloc failed(%d)\n", ret);
1624		return ret;
1625	}
1626	req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1627	req->dev_id = dev_id;
1628	if (clk_id < 255) {
1629		req->clk_id = clk_id;
1630	} else {
1631		req->clk_id = 255;
1632		req->clk_id_32 = clk_id;
1633	}
1634
1635	ret = ti_sci_do_xfer(info, xfer);
1636	if (ret) {
1637		dev_err(dev, "Mbox send fail %d\n", ret);
1638		goto fail;
1639	}
1640
1641	resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1642
1643	if (!ti_sci_is_response_ack(resp))
1644		ret = -ENODEV;
1645	else
1646		*freq = resp->freq_hz;
1647
1648fail:
1649	ti_sci_put_one_xfer(&info->minfo, xfer);
1650
1651	return ret;
1652}
1653
1654static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1655{
1656	struct ti_sci_info *info;
1657	struct ti_sci_msg_req_reboot *req;
1658	struct ti_sci_msg_hdr *resp;
1659	struct ti_sci_xfer *xfer;
1660	struct device *dev;
1661	int ret = 0;
1662
1663	if (IS_ERR(handle))
1664		return PTR_ERR(handle);
1665	if (!handle)
1666		return -EINVAL;
1667
1668	info = handle_to_ti_sci_info(handle);
1669	dev = info->dev;
1670
1671	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1672				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1673				   sizeof(*req), sizeof(*resp));
1674	if (IS_ERR(xfer)) {
1675		ret = PTR_ERR(xfer);
1676		dev_err(dev, "Message alloc failed(%d)\n", ret);
1677		return ret;
1678	}
1679	req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1680
1681	ret = ti_sci_do_xfer(info, xfer);
1682	if (ret) {
1683		dev_err(dev, "Mbox send fail %d\n", ret);
1684		goto fail;
1685	}
1686
1687	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1688
1689	if (!ti_sci_is_response_ack(resp))
1690		ret = -ENODEV;
1691	else
1692		ret = 0;
1693
1694fail:
1695	ti_sci_put_one_xfer(&info->minfo, xfer);
1696
1697	return ret;
1698}
1699
1700/**
1701 * ti_sci_get_resource_range - Helper to get a range of resources assigned
1702 *			       to a host. Resource is uniquely identified by
1703 *			       type and subtype.
1704 * @handle:		Pointer to TISCI handle.
1705 * @dev_id:		TISCI device ID.
1706 * @subtype:		Resource assignment subtype that is being requested
1707 *			from the given device.
1708 * @s_host:		Host processor ID to which the resources are allocated
1709 * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1710 *			resource range start index and number of resources
1711 *
1712 * Return: 0 if all went fine, else return appropriate error.
1713 */
1714static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
1715				     u32 dev_id, u8 subtype, u8 s_host,
1716				     struct ti_sci_resource_desc *desc)
1717{
1718	struct ti_sci_msg_resp_get_resource_range *resp;
1719	struct ti_sci_msg_req_get_resource_range *req;
1720	struct ti_sci_xfer *xfer;
1721	struct ti_sci_info *info;
1722	struct device *dev;
1723	int ret = 0;
1724
1725	if (IS_ERR(handle))
1726		return PTR_ERR(handle);
1727	if (!handle || !desc)
1728		return -EINVAL;
1729
1730	info = handle_to_ti_sci_info(handle);
1731	dev = info->dev;
1732
1733	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
1734				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1735				   sizeof(*req), sizeof(*resp));
1736	if (IS_ERR(xfer)) {
1737		ret = PTR_ERR(xfer);
1738		dev_err(dev, "Message alloc failed(%d)\n", ret);
1739		return ret;
1740	}
1741
1742	req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
1743	req->secondary_host = s_host;
1744	req->type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
1745	req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
1746
1747	ret = ti_sci_do_xfer(info, xfer);
1748	if (ret) {
1749		dev_err(dev, "Mbox send fail %d\n", ret);
1750		goto fail;
1751	}
1752
1753	resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
1754
1755	if (!ti_sci_is_response_ack(resp)) {
1756		ret = -ENODEV;
1757	} else if (!resp->range_num && !resp->range_num_sec) {
1758		/* Neither of the two resource range is valid */
1759		ret = -ENODEV;
1760	} else {
1761		desc->start = resp->range_start;
1762		desc->num = resp->range_num;
1763		desc->start_sec = resp->range_start_sec;
1764		desc->num_sec = resp->range_num_sec;
1765	}
1766
1767fail:
1768	ti_sci_put_one_xfer(&info->minfo, xfer);
1769
1770	return ret;
1771}
1772
1773/**
1774 * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
1775 *				   that is same as ti sci interface host.
1776 * @handle:		Pointer to TISCI handle.
1777 * @dev_id:		TISCI device ID.
1778 * @subtype:		Resource assignment subtype that is being requested
1779 *			from the given device.
1780 * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1781 *			resource range start index and number of resources
1782 *
1783 * Return: 0 if all went fine, else return appropriate error.
1784 */
1785static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
1786					 u32 dev_id, u8 subtype,
1787					 struct ti_sci_resource_desc *desc)
1788{
1789	return ti_sci_get_resource_range(handle, dev_id, subtype,
1790					 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1791					 desc);
1792}
1793
1794/**
1795 * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
1796 *					      assigned to a specified host.
1797 * @handle:		Pointer to TISCI handle.
1798 * @dev_id:		TISCI device ID.
1799 * @subtype:		Resource assignment subtype that is being requested
1800 *			from the given device.
1801 * @s_host:		Host processor ID to which the resources are allocated
1802 * @desc:		Pointer to ti_sci_resource_desc to be updated with the
1803 *			resource range start index and number of resources
1804 *
1805 * Return: 0 if all went fine, else return appropriate error.
1806 */
1807static
1808int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
1809					     u32 dev_id, u8 subtype, u8 s_host,
1810					     struct ti_sci_resource_desc *desc)
1811{
1812	return ti_sci_get_resource_range(handle, dev_id, subtype, s_host, desc);
1813}
1814
1815/**
1816 * ti_sci_manage_irq() - Helper api to configure/release the irq route between
1817 *			 the requested source and destination
1818 * @handle:		Pointer to TISCI handle.
1819 * @valid_params:	Bit fields defining the validity of certain params
1820 * @src_id:		Device ID of the IRQ source
1821 * @src_index:		IRQ source index within the source device
1822 * @dst_id:		Device ID of the IRQ destination
1823 * @dst_host_irq:	IRQ number of the destination device
1824 * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1825 * @vint:		Virtual interrupt to be used within the IA
1826 * @global_event:	Global event number to be used for the requesting event
1827 * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1828 * @s_host:		Secondary host ID to which the irq/event is being
1829 *			requested for.
1830 * @type:		Request type irq set or release.
1831 *
1832 * Return: 0 if all went fine, else return appropriate error.
1833 */
1834static int ti_sci_manage_irq(const struct ti_sci_handle *handle,
1835			     u32 valid_params, u16 src_id, u16 src_index,
1836			     u16 dst_id, u16 dst_host_irq, u16 ia_id, u16 vint,
1837			     u16 global_event, u8 vint_status_bit, u8 s_host,
1838			     u16 type)
1839{
1840	struct ti_sci_msg_req_manage_irq *req;
1841	struct ti_sci_msg_hdr *resp;
1842	struct ti_sci_xfer *xfer;
1843	struct ti_sci_info *info;
1844	struct device *dev;
1845	int ret = 0;
1846
1847	if (IS_ERR(handle))
1848		return PTR_ERR(handle);
1849	if (!handle)
1850		return -EINVAL;
1851
1852	info = handle_to_ti_sci_info(handle);
1853	dev = info->dev;
1854
1855	xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1856				   sizeof(*req), sizeof(*resp));
1857	if (IS_ERR(xfer)) {
1858		ret = PTR_ERR(xfer);
1859		dev_err(dev, "Message alloc failed(%d)\n", ret);
1860		return ret;
1861	}
1862	req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
1863	req->valid_params = valid_params;
1864	req->src_id = src_id;
1865	req->src_index = src_index;
1866	req->dst_id = dst_id;
1867	req->dst_host_irq = dst_host_irq;
1868	req->ia_id = ia_id;
1869	req->vint = vint;
1870	req->global_event = global_event;
1871	req->vint_status_bit = vint_status_bit;
1872	req->secondary_host = s_host;
1873
1874	ret = ti_sci_do_xfer(info, xfer);
1875	if (ret) {
1876		dev_err(dev, "Mbox send fail %d\n", ret);
1877		goto fail;
1878	}
1879
1880	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1881
1882	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1883
1884fail:
1885	ti_sci_put_one_xfer(&info->minfo, xfer);
1886
1887	return ret;
1888}
1889
1890/**
1891 * ti_sci_set_irq() - Helper api to configure the irq route between the
1892 *		      requested source and destination
1893 * @handle:		Pointer to TISCI handle.
1894 * @valid_params:	Bit fields defining the validity of certain params
1895 * @src_id:		Device ID of the IRQ source
1896 * @src_index:		IRQ source index within the source device
1897 * @dst_id:		Device ID of the IRQ destination
1898 * @dst_host_irq:	IRQ number of the destination device
1899 * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1900 * @vint:		Virtual interrupt to be used within the IA
1901 * @global_event:	Global event number to be used for the requesting event
1902 * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1903 * @s_host:		Secondary host ID to which the irq/event is being
1904 *			requested for.
1905 *
1906 * Return: 0 if all went fine, else return appropriate error.
1907 */
1908static int ti_sci_set_irq(const struct ti_sci_handle *handle, u32 valid_params,
1909			  u16 src_id, u16 src_index, u16 dst_id,
1910			  u16 dst_host_irq, u16 ia_id, u16 vint,
1911			  u16 global_event, u8 vint_status_bit, u8 s_host)
1912{
1913	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",
1914		 __func__, valid_params, src_id, src_index,
1915		 dst_id, dst_host_irq, ia_id, vint, global_event,
1916		 vint_status_bit);
1917
1918	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1919				 dst_id, dst_host_irq, ia_id, vint,
1920				 global_event, vint_status_bit, s_host,
1921				 TI_SCI_MSG_SET_IRQ);
1922}
1923
1924/**
1925 * ti_sci_free_irq() - Helper api to free the irq route between the
1926 *			   requested source and destination
1927 * @handle:		Pointer to TISCI handle.
1928 * @valid_params:	Bit fields defining the validity of certain params
1929 * @src_id:		Device ID of the IRQ source
1930 * @src_index:		IRQ source index within the source device
1931 * @dst_id:		Device ID of the IRQ destination
1932 * @dst_host_irq:	IRQ number of the destination device
1933 * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1934 * @vint:		Virtual interrupt to be used within the IA
1935 * @global_event:	Global event number to be used for the requesting event
1936 * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1937 * @s_host:		Secondary host ID to which the irq/event is being
1938 *			requested for.
1939 *
1940 * Return: 0 if all went fine, else return appropriate error.
1941 */
1942static int ti_sci_free_irq(const struct ti_sci_handle *handle, u32 valid_params,
1943			   u16 src_id, u16 src_index, u16 dst_id,
1944			   u16 dst_host_irq, u16 ia_id, u16 vint,
1945			   u16 global_event, u8 vint_status_bit, u8 s_host)
1946{
1947	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",
1948		 __func__, valid_params, src_id, src_index,
1949		 dst_id, dst_host_irq, ia_id, vint, global_event,
1950		 vint_status_bit);
1951
1952	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1953				 dst_id, dst_host_irq, ia_id, vint,
1954				 global_event, vint_status_bit, s_host,
1955				 TI_SCI_MSG_FREE_IRQ);
1956}
1957
1958/**
1959 * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
1960 *			  source and destination.
1961 * @handle:		Pointer to TISCI handle.
1962 * @src_id:		Device ID of the IRQ source
1963 * @src_index:		IRQ source index within the source device
1964 * @dst_id:		Device ID of the IRQ destination
1965 * @dst_host_irq:	IRQ number of the destination device
1966 *
1967 * Return: 0 if all went fine, else return appropriate error.
1968 */
1969static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
1970			      u16 src_index, u16 dst_id, u16 dst_host_irq)
1971{
1972	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
1973
1974	return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
1975			      dst_host_irq, 0, 0, 0, 0, 0);
1976}
1977
1978/**
1979 * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
1980 *				requested source and Interrupt Aggregator.
1981 * @handle:		Pointer to TISCI handle.
1982 * @src_id:		Device ID of the IRQ source
1983 * @src_index:		IRQ source index within the source device
1984 * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1985 * @vint:		Virtual interrupt to be used within the IA
1986 * @global_event:	Global event number to be used for the requesting event
1987 * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1988 *
1989 * Return: 0 if all went fine, else return appropriate error.
1990 */
1991static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
1992				    u16 src_id, u16 src_index, u16 ia_id,
1993				    u16 vint, u16 global_event,
1994				    u8 vint_status_bit)
1995{
1996	u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
1997			   MSG_FLAG_GLB_EVNT_VALID |
1998			   MSG_FLAG_VINT_STS_BIT_VALID;
1999
2000	return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2001			      ia_id, vint, global_event, vint_status_bit, 0);
2002}
2003
2004/**
2005 * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2006 *			   requested source and destination.
2007 * @handle:		Pointer to TISCI handle.
2008 * @src_id:		Device ID of the IRQ source
2009 * @src_index:		IRQ source index within the source device
2010 * @dst_id:		Device ID of the IRQ destination
2011 * @dst_host_irq:	IRQ number of the destination device
2012 *
2013 * Return: 0 if all went fine, else return appropriate error.
2014 */
2015static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2016			       u16 src_index, u16 dst_id, u16 dst_host_irq)
2017{
2018	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2019
2020	return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2021			       dst_host_irq, 0, 0, 0, 0, 0);
2022}
2023
2024/**
2025 * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2026 *				 and Interrupt Aggregator.
2027 * @handle:		Pointer to TISCI handle.
2028 * @src_id:		Device ID of the IRQ source
2029 * @src_index:		IRQ source index within the source device
2030 * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
2031 * @vint:		Virtual interrupt to be used within the IA
2032 * @global_event:	Global event number to be used for the requesting event
2033 * @vint_status_bit:	Virtual interrupt status bit to be used for the event
2034 *
2035 * Return: 0 if all went fine, else return appropriate error.
2036 */
2037static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2038				     u16 src_id, u16 src_index, u16 ia_id,
2039				     u16 vint, u16 global_event,
2040				     u8 vint_status_bit)
2041{
2042	u32 valid_params = MSG_FLAG_IA_ID_VALID |
2043			   MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2044			   MSG_FLAG_VINT_STS_BIT_VALID;
2045
2046	return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2047			       ia_id, vint, global_event, vint_status_bit, 0);
2048}
2049
2050/**
2051 * ti_sci_cmd_rm_ring_cfg() - Configure a NAVSS ring
2052 * @handle:	Pointer to TI SCI handle.
2053 * @params:	Pointer to ti_sci_msg_rm_ring_cfg ring config structure
2054 *
2055 * Return: 0 if all went well, else returns appropriate error value.
2056 *
2057 * See @ti_sci_msg_rm_ring_cfg and @ti_sci_msg_rm_ring_cfg_req for
2058 * more info.
2059 */
2060static int ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle *handle,
2061				  const struct ti_sci_msg_rm_ring_cfg *params)
2062{
2063	struct ti_sci_msg_rm_ring_cfg_req *req;
2064	struct ti_sci_msg_hdr *resp;
2065	struct ti_sci_xfer *xfer;
2066	struct ti_sci_info *info;
2067	struct device *dev;
2068	int ret = 0;
2069
2070	if (IS_ERR_OR_NULL(handle))
2071		return -EINVAL;
2072
2073	info = handle_to_ti_sci_info(handle);
2074	dev = info->dev;
2075
2076	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2077				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2078				   sizeof(*req), sizeof(*resp));
2079	if (IS_ERR(xfer)) {
2080		ret = PTR_ERR(xfer);
2081		dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
2082		return ret;
2083	}
2084	req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
2085	req->valid_params = params->valid_params;
2086	req->nav_id = params->nav_id;
2087	req->index = params->index;
2088	req->addr_lo = params->addr_lo;
2089	req->addr_hi = params->addr_hi;
2090	req->count = params->count;
2091	req->mode = params->mode;
2092	req->size = params->size;
2093	req->order_id = params->order_id;
2094	req->virtid = params->virtid;
2095	req->asel = params->asel;
2096
2097	ret = ti_sci_do_xfer(info, xfer);
2098	if (ret) {
2099		dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
2100		goto fail;
2101	}
2102
2103	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2104	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2105
2106fail:
2107	ti_sci_put_one_xfer(&info->minfo, xfer);
2108	dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", params->index, ret);
2109	return ret;
2110}
2111
2112/**
2113 * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
2114 * @handle:	Pointer to TI SCI handle.
2115 * @nav_id:	Device ID of Navigator Subsystem which should be used for
2116 *		pairing
2117 * @src_thread:	Source PSI-L thread ID
2118 * @dst_thread: Destination PSI-L thread ID
2119 *
2120 * Return: 0 if all went well, else returns appropriate error value.
2121 */
2122static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2123				   u32 nav_id, u32 src_thread, u32 dst_thread)
2124{
2125	struct ti_sci_msg_psil_pair *req;
2126	struct ti_sci_msg_hdr *resp;
2127	struct ti_sci_xfer *xfer;
2128	struct ti_sci_info *info;
2129	struct device *dev;
2130	int ret = 0;
2131
2132	if (IS_ERR(handle))
2133		return PTR_ERR(handle);
2134	if (!handle)
2135		return -EINVAL;
2136
2137	info = handle_to_ti_sci_info(handle);
2138	dev = info->dev;
2139
2140	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2141				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2142				   sizeof(*req), sizeof(*resp));
2143	if (IS_ERR(xfer)) {
2144		ret = PTR_ERR(xfer);
2145		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2146		return ret;
2147	}
2148	req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
2149	req->nav_id = nav_id;
2150	req->src_thread = src_thread;
2151	req->dst_thread = dst_thread;
2152
2153	ret = ti_sci_do_xfer(info, xfer);
2154	if (ret) {
2155		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2156		goto fail;
2157	}
2158
2159	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2160	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2161
2162fail:
2163	ti_sci_put_one_xfer(&info->minfo, xfer);
2164
2165	return ret;
2166}
2167
2168/**
2169 * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
2170 * @handle:	Pointer to TI SCI handle.
2171 * @nav_id:	Device ID of Navigator Subsystem which should be used for
2172 *		unpairing
2173 * @src_thread:	Source PSI-L thread ID
2174 * @dst_thread:	Destination PSI-L thread ID
2175 *
2176 * Return: 0 if all went well, else returns appropriate error value.
2177 */
2178static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2179				     u32 nav_id, u32 src_thread, u32 dst_thread)
2180{
2181	struct ti_sci_msg_psil_unpair *req;
2182	struct ti_sci_msg_hdr *resp;
2183	struct ti_sci_xfer *xfer;
2184	struct ti_sci_info *info;
2185	struct device *dev;
2186	int ret = 0;
2187
2188	if (IS_ERR(handle))
2189		return PTR_ERR(handle);
2190	if (!handle)
2191		return -EINVAL;
2192
2193	info = handle_to_ti_sci_info(handle);
2194	dev = info->dev;
2195
2196	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2197				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2198				   sizeof(*req), sizeof(*resp));
2199	if (IS_ERR(xfer)) {
2200		ret = PTR_ERR(xfer);
2201		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2202		return ret;
2203	}
2204	req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
2205	req->nav_id = nav_id;
2206	req->src_thread = src_thread;
2207	req->dst_thread = dst_thread;
2208
2209	ret = ti_sci_do_xfer(info, xfer);
2210	if (ret) {
2211		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2212		goto fail;
2213	}
2214
2215	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2216	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2217
2218fail:
2219	ti_sci_put_one_xfer(&info->minfo, xfer);
2220
2221	return ret;
2222}
2223
2224/**
2225 * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
2226 * @handle:	Pointer to TI SCI handle.
2227 * @params:	Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
2228 *		structure
2229 *
2230 * Return: 0 if all went well, else returns appropriate error value.
2231 *
2232 * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
2233 * more info.
2234 */
2235static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
2236			const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2237{
2238	struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
2239	struct ti_sci_msg_hdr *resp;
2240	struct ti_sci_xfer *xfer;
2241	struct ti_sci_info *info;
2242	struct device *dev;
2243	int ret = 0;
2244
2245	if (IS_ERR_OR_NULL(handle))
2246		return -EINVAL;
2247
2248	info = handle_to_ti_sci_info(handle);
2249	dev = info->dev;
2250
2251	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2252				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2253				   sizeof(*req), sizeof(*resp));
2254	if (IS_ERR(xfer)) {
2255		ret = PTR_ERR(xfer);
2256		dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2257		return ret;
2258	}
2259	req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
2260	req->valid_params = params->valid_params;
2261	req->nav_id = params->nav_id;
2262	req->index = params->index;
2263	req->tx_pause_on_err = params->tx_pause_on_err;
2264	req->tx_filt_einfo = params->tx_filt_einfo;
2265	req->tx_filt_pswords = params->tx_filt_pswords;
2266	req->tx_atype = params->tx_atype;
2267	req->tx_chan_type = params->tx_chan_type;
2268	req->tx_supr_tdpkt = params->tx_supr_tdpkt;
2269	req->tx_fetch_size = params->tx_fetch_size;
2270	req->tx_credit_count = params->tx_credit_count;
2271	req->txcq_qnum = params->txcq_qnum;
2272	req->tx_priority = params->tx_priority;
2273	req->tx_qos = params->tx_qos;
2274	req->tx_orderid = params->tx_orderid;
2275	req->fdepth = params->fdepth;
2276	req->tx_sched_priority = params->tx_sched_priority;
2277	req->tx_burst_size = params->tx_burst_size;
2278	req->tx_tdtype = params->tx_tdtype;
2279	req->extended_ch_type = params->extended_ch_type;
2280
2281	ret = ti_sci_do_xfer(info, xfer);
2282	if (ret) {
2283		dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2284		goto fail;
2285	}
2286
2287	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2288	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2289
2290fail:
2291	ti_sci_put_one_xfer(&info->minfo, xfer);
2292	dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2293	return ret;
2294}
2295
2296/**
2297 * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
2298 * @handle:	Pointer to TI SCI handle.
2299 * @params:	Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
2300 *		structure
2301 *
2302 * Return: 0 if all went well, else returns appropriate error value.
2303 *
2304 * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
2305 * more info.
2306 */
2307static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
2308			const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2309{
2310	struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
2311	struct ti_sci_msg_hdr *resp;
2312	struct ti_sci_xfer *xfer;
2313	struct ti_sci_info *info;
2314	struct device *dev;
2315	int ret = 0;
2316
2317	if (IS_ERR_OR_NULL(handle))
2318		return -EINVAL;
2319
2320	info = handle_to_ti_sci_info(handle);
2321	dev = info->dev;
2322
2323	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2324				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2325				   sizeof(*req), sizeof(*resp));
2326	if (IS_ERR(xfer)) {
2327		ret = PTR_ERR(xfer);
2328		dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2329		return ret;
2330	}
2331	req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
2332	req->valid_params = params->valid_params;
2333	req->nav_id = params->nav_id;
2334	req->index = params->index;
2335	req->rx_fetch_size = params->rx_fetch_size;
2336	req->rxcq_qnum = params->rxcq_qnum;
2337	req->rx_priority = params->rx_priority;
2338	req->rx_qos = params->rx_qos;
2339	req->rx_orderid = params->rx_orderid;
2340	req->rx_sched_priority = params->rx_sched_priority;
2341	req->flowid_start = params->flowid_start;
2342	req->flowid_cnt = params->flowid_cnt;
2343	req->rx_pause_on_err = params->rx_pause_on_err;
2344	req->rx_atype = params->rx_atype;
2345	req->rx_chan_type = params->rx_chan_type;
2346	req->rx_ignore_short = params->rx_ignore_short;
2347	req->rx_ignore_long = params->rx_ignore_long;
2348	req->rx_burst_size = params->rx_burst_size;
2349
2350	ret = ti_sci_do_xfer(info, xfer);
2351	if (ret) {
2352		dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2353		goto fail;
2354	}
2355
2356	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2357	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2358
2359fail:
2360	ti_sci_put_one_xfer(&info->minfo, xfer);
2361	dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2362	return ret;
2363}
2364
2365/**
2366 * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
2367 * @handle:	Pointer to TI SCI handle.
2368 * @params:	Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
2369 *		structure
2370 *
2371 * Return: 0 if all went well, else returns appropriate error value.
2372 *
2373 * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
2374 * more info.
2375 */
2376static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
2377			const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2378{
2379	struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
2380	struct ti_sci_msg_hdr *resp;
2381	struct ti_sci_xfer *xfer;
2382	struct ti_sci_info *info;
2383	struct device *dev;
2384	int ret = 0;
2385
2386	if (IS_ERR_OR_NULL(handle))
2387		return -EINVAL;
2388
2389	info = handle_to_ti_sci_info(handle);
2390	dev = info->dev;
2391
2392	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2393				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2394				   sizeof(*req), sizeof(*resp));
2395	if (IS_ERR(xfer)) {
2396		ret = PTR_ERR(xfer);
2397		dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
2398		return ret;
2399	}
2400	req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
2401	req->valid_params = params->valid_params;
2402	req->nav_id = params->nav_id;
2403	req->flow_index = params->flow_index;
2404	req->rx_einfo_present = params->rx_einfo_present;
2405	req->rx_psinfo_present = params->rx_psinfo_present;
2406	req->rx_error_handling = params->rx_error_handling;
2407	req->rx_desc_type = params->rx_desc_type;
2408	req->rx_sop_offset = params->rx_sop_offset;
2409	req->rx_dest_qnum = params->rx_dest_qnum;
2410	req->rx_src_tag_hi = params->rx_src_tag_hi;
2411	req->rx_src_tag_lo = params->rx_src_tag_lo;
2412	req->rx_dest_tag_hi = params->rx_dest_tag_hi;
2413	req->rx_dest_tag_lo = params->rx_dest_tag_lo;
2414	req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2415	req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2416	req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2417	req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2418	req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2419	req->rx_fdq1_qnum = params->rx_fdq1_qnum;
2420	req->rx_fdq2_qnum = params->rx_fdq2_qnum;
2421	req->rx_fdq3_qnum = params->rx_fdq3_qnum;
2422	req->rx_ps_location = params->rx_ps_location;
2423
2424	ret = ti_sci_do_xfer(info, xfer);
2425	if (ret) {
2426		dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
2427		goto fail;
2428	}
2429
2430	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2431	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2432
2433fail:
2434	ti_sci_put_one_xfer(&info->minfo, xfer);
2435	dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2436	return ret;
2437}
2438
2439/**
2440 * ti_sci_cmd_proc_request() - Command to request a physical processor control
2441 * @handle:	Pointer to TI SCI handle
2442 * @proc_id:	Processor ID this request is for
2443 *
2444 * Return: 0 if all went well, else returns appropriate error value.
2445 */
2446static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
2447				   u8 proc_id)
2448{
2449	struct ti_sci_msg_req_proc_request *req;
2450	struct ti_sci_msg_hdr *resp;
2451	struct ti_sci_info *info;
2452	struct ti_sci_xfer *xfer;
2453	struct device *dev;
2454	int ret = 0;
2455
2456	if (!handle)
2457		return -EINVAL;
2458	if (IS_ERR(handle))
2459		return PTR_ERR(handle);
2460
2461	info = handle_to_ti_sci_info(handle);
2462	dev = info->dev;
2463
2464	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
2465				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2466				   sizeof(*req), sizeof(*resp));
2467	if (IS_ERR(xfer)) {
2468		ret = PTR_ERR(xfer);
2469		dev_err(dev, "Message alloc failed(%d)\n", ret);
2470		return ret;
2471	}
2472	req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
2473	req->processor_id = proc_id;
2474
2475	ret = ti_sci_do_xfer(info, xfer);
2476	if (ret) {
2477		dev_err(dev, "Mbox send fail %d\n", ret);
2478		goto fail;
2479	}
2480
2481	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2482
2483	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2484
2485fail:
2486	ti_sci_put_one_xfer(&info->minfo, xfer);
2487
2488	return ret;
2489}
2490
2491/**
2492 * ti_sci_cmd_proc_release() - Command to release a physical processor control
2493 * @handle:	Pointer to TI SCI handle
2494 * @proc_id:	Processor ID this request is for
2495 *
2496 * Return: 0 if all went well, else returns appropriate error value.
2497 */
2498static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
2499				   u8 proc_id)
2500{
2501	struct ti_sci_msg_req_proc_release *req;
2502	struct ti_sci_msg_hdr *resp;
2503	struct ti_sci_info *info;
2504	struct ti_sci_xfer *xfer;
2505	struct device *dev;
2506	int ret = 0;
2507
2508	if (!handle)
2509		return -EINVAL;
2510	if (IS_ERR(handle))
2511		return PTR_ERR(handle);
2512
2513	info = handle_to_ti_sci_info(handle);
2514	dev = info->dev;
2515
2516	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
2517				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2518				   sizeof(*req), sizeof(*resp));
2519	if (IS_ERR(xfer)) {
2520		ret = PTR_ERR(xfer);
2521		dev_err(dev, "Message alloc failed(%d)\n", ret);
2522		return ret;
2523	}
2524	req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
2525	req->processor_id = proc_id;
2526
2527	ret = ti_sci_do_xfer(info, xfer);
2528	if (ret) {
2529		dev_err(dev, "Mbox send fail %d\n", ret);
2530		goto fail;
2531	}
2532
2533	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2534
2535	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2536
2537fail:
2538	ti_sci_put_one_xfer(&info->minfo, xfer);
2539
2540	return ret;
2541}
2542
2543/**
2544 * ti_sci_cmd_proc_handover() - Command to handover a physical processor
2545 *				control to a host in the processor's access
2546 *				control list.
2547 * @handle:	Pointer to TI SCI handle
2548 * @proc_id:	Processor ID this request is for
2549 * @host_id:	Host ID to get the control of the processor
2550 *
2551 * Return: 0 if all went well, else returns appropriate error value.
2552 */
2553static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
2554				    u8 proc_id, u8 host_id)
2555{
2556	struct ti_sci_msg_req_proc_handover *req;
2557	struct ti_sci_msg_hdr *resp;
2558	struct ti_sci_info *info;
2559	struct ti_sci_xfer *xfer;
2560	struct device *dev;
2561	int ret = 0;
2562
2563	if (!handle)
2564		return -EINVAL;
2565	if (IS_ERR(handle))
2566		return PTR_ERR(handle);
2567
2568	info = handle_to_ti_sci_info(handle);
2569	dev = info->dev;
2570
2571	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
2572				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2573				   sizeof(*req), sizeof(*resp));
2574	if (IS_ERR(xfer)) {
2575		ret = PTR_ERR(xfer);
2576		dev_err(dev, "Message alloc failed(%d)\n", ret);
2577		return ret;
2578	}
2579	req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
2580	req->processor_id = proc_id;
2581	req->host_id = host_id;
2582
2583	ret = ti_sci_do_xfer(info, xfer);
2584	if (ret) {
2585		dev_err(dev, "Mbox send fail %d\n", ret);
2586		goto fail;
2587	}
2588
2589	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2590
2591	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2592
2593fail:
2594	ti_sci_put_one_xfer(&info->minfo, xfer);
2595
2596	return ret;
2597}
2598
2599/**
2600 * ti_sci_cmd_proc_set_config() - Command to set the processor boot
2601 *				    configuration flags
2602 * @handle:		Pointer to TI SCI handle
2603 * @proc_id:		Processor ID this request is for
2604 * @bootvector:		Processor Boot vector (start address)
2605 * @config_flags_set:	Configuration flags to be set
2606 * @config_flags_clear:	Configuration flags to be cleared.
2607 *
2608 * Return: 0 if all went well, else returns appropriate error value.
2609 */
2610static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
2611				      u8 proc_id, u64 bootvector,
2612				      u32 config_flags_set,
2613				      u32 config_flags_clear)
2614{
2615	struct ti_sci_msg_req_set_config *req;
2616	struct ti_sci_msg_hdr *resp;
2617	struct ti_sci_info *info;
2618	struct ti_sci_xfer *xfer;
2619	struct device *dev;
2620	int ret = 0;
2621
2622	if (!handle)
2623		return -EINVAL;
2624	if (IS_ERR(handle))
2625		return PTR_ERR(handle);
2626
2627	info = handle_to_ti_sci_info(handle);
2628	dev = info->dev;
2629
2630	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
2631				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2632				   sizeof(*req), sizeof(*resp));
2633	if (IS_ERR(xfer)) {
2634		ret = PTR_ERR(xfer);
2635		dev_err(dev, "Message alloc failed(%d)\n", ret);
2636		return ret;
2637	}
2638	req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
2639	req->processor_id = proc_id;
2640	req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
2641	req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
2642				TI_SCI_ADDR_HIGH_SHIFT;
2643	req->config_flags_set = config_flags_set;
2644	req->config_flags_clear = config_flags_clear;
2645
2646	ret = ti_sci_do_xfer(info, xfer);
2647	if (ret) {
2648		dev_err(dev, "Mbox send fail %d\n", ret);
2649		goto fail;
2650	}
2651
2652	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2653
2654	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2655
2656fail:
2657	ti_sci_put_one_xfer(&info->minfo, xfer);
2658
2659	return ret;
2660}
2661
2662/**
2663 * ti_sci_cmd_proc_set_control() - Command to set the processor boot
2664 *				     control flags
2665 * @handle:			Pointer to TI SCI handle
2666 * @proc_id:			Processor ID this request is for
2667 * @control_flags_set:		Control flags to be set
2668 * @control_flags_clear:	Control flags to be cleared
2669 *
2670 * Return: 0 if all went well, else returns appropriate error value.
2671 */
2672static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
2673				       u8 proc_id, u32 control_flags_set,
2674				       u32 control_flags_clear)
2675{
2676	struct ti_sci_msg_req_set_ctrl *req;
2677	struct ti_sci_msg_hdr *resp;
2678	struct ti_sci_info *info;
2679	struct ti_sci_xfer *xfer;
2680	struct device *dev;
2681	int ret = 0;
2682
2683	if (!handle)
2684		return -EINVAL;
2685	if (IS_ERR(handle))
2686		return PTR_ERR(handle);
2687
2688	info = handle_to_ti_sci_info(handle);
2689	dev = info->dev;
2690
2691	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
2692				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2693				   sizeof(*req), sizeof(*resp));
2694	if (IS_ERR(xfer)) {
2695		ret = PTR_ERR(xfer);
2696		dev_err(dev, "Message alloc failed(%d)\n", ret);
2697		return ret;
2698	}
2699	req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
2700	req->processor_id = proc_id;
2701	req->control_flags_set = control_flags_set;
2702	req->control_flags_clear = control_flags_clear;
2703
2704	ret = ti_sci_do_xfer(info, xfer);
2705	if (ret) {
2706		dev_err(dev, "Mbox send fail %d\n", ret);
2707		goto fail;
2708	}
2709
2710	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2711
2712	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2713
2714fail:
2715	ti_sci_put_one_xfer(&info->minfo, xfer);
2716
2717	return ret;
2718}
2719
2720/**
2721 * ti_sci_cmd_proc_get_status() - Command to get the processor boot status
2722 * @handle:	Pointer to TI SCI handle
2723 * @proc_id:	Processor ID this request is for
2724 * @bv:		Processor Boot vector (start address)
2725 * @cfg_flags:	Processor specific configuration flags
2726 * @ctrl_flags:	Processor specific control flags
2727 * @sts_flags:	Processor specific status flags
2728 *
2729 * Return: 0 if all went well, else returns appropriate error value.
2730 */
2731static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
2732				      u8 proc_id, u64 *bv, u32 *cfg_flags,
2733				      u32 *ctrl_flags, u32 *sts_flags)
2734{
2735	struct ti_sci_msg_resp_get_status *resp;
2736	struct ti_sci_msg_req_get_status *req;
2737	struct ti_sci_info *info;
2738	struct ti_sci_xfer *xfer;
2739	struct device *dev;
2740	int ret = 0;
2741
2742	if (!handle)
2743		return -EINVAL;
2744	if (IS_ERR(handle))
2745		return PTR_ERR(handle);
2746
2747	info = handle_to_ti_sci_info(handle);
2748	dev = info->dev;
2749
2750	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
2751				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2752				   sizeof(*req), sizeof(*resp));
2753	if (IS_ERR(xfer)) {
2754		ret = PTR_ERR(xfer);
2755		dev_err(dev, "Message alloc failed(%d)\n", ret);
2756		return ret;
2757	}
2758	req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
2759	req->processor_id = proc_id;
2760
2761	ret = ti_sci_do_xfer(info, xfer);
2762	if (ret) {
2763		dev_err(dev, "Mbox send fail %d\n", ret);
2764		goto fail;
2765	}
2766
2767	resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
2768
2769	if (!ti_sci_is_response_ack(resp)) {
2770		ret = -ENODEV;
2771	} else {
2772		*bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
2773		      (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
2774		       TI_SCI_ADDR_HIGH_MASK);
2775		*cfg_flags = resp->config_flags;
2776		*ctrl_flags = resp->control_flags;
2777		*sts_flags = resp->status_flags;
2778	}
2779
2780fail:
2781	ti_sci_put_one_xfer(&info->minfo, xfer);
2782
2783	return ret;
2784}
2785
2786/*
2787 * ti_sci_setup_ops() - Setup the operations structures
2788 * @info:	pointer to TISCI pointer
2789 */
2790static void ti_sci_setup_ops(struct ti_sci_info *info)
2791{
2792	struct ti_sci_ops *ops = &info->handle.ops;
2793	struct ti_sci_core_ops *core_ops = &ops->core_ops;
2794	struct ti_sci_dev_ops *dops = &ops->dev_ops;
2795	struct ti_sci_clk_ops *cops = &ops->clk_ops;
2796	struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2797	struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
2798	struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2799	struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2800	struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
2801	struct ti_sci_proc_ops *pops = &ops->proc_ops;
2802
2803	core_ops->reboot_device = ti_sci_cmd_core_reboot;
2804
2805	dops->get_device = ti_sci_cmd_get_device;
2806	dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
2807	dops->idle_device = ti_sci_cmd_idle_device;
2808	dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
2809	dops->put_device = ti_sci_cmd_put_device;
2810
2811	dops->is_valid = ti_sci_cmd_dev_is_valid;
2812	dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2813	dops->is_idle = ti_sci_cmd_dev_is_idle;
2814	dops->is_stop = ti_sci_cmd_dev_is_stop;
2815	dops->is_on = ti_sci_cmd_dev_is_on;
2816	dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2817	dops->set_device_resets = ti_sci_cmd_set_device_resets;
2818	dops->get_device_resets = ti_sci_cmd_get_device_resets;
2819
2820	cops->get_clock = ti_sci_cmd_get_clock;
2821	cops->idle_clock = ti_sci_cmd_idle_clock;
2822	cops->put_clock = ti_sci_cmd_put_clock;
2823	cops->is_auto = ti_sci_cmd_clk_is_auto;
2824	cops->is_on = ti_sci_cmd_clk_is_on;
2825	cops->is_off = ti_sci_cmd_clk_is_off;
2826
2827	cops->set_parent = ti_sci_cmd_clk_set_parent;
2828	cops->get_parent = ti_sci_cmd_clk_get_parent;
2829	cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2830
2831	cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2832	cops->set_freq = ti_sci_cmd_clk_set_freq;
2833	cops->get_freq = ti_sci_cmd_clk_get_freq;
2834
2835	rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2836	rm_core_ops->get_range_from_shost =
2837				ti_sci_cmd_get_resource_range_from_shost;
2838
2839	iops->set_irq = ti_sci_cmd_set_irq;
2840	iops->set_event_map = ti_sci_cmd_set_event_map;
2841	iops->free_irq = ti_sci_cmd_free_irq;
2842	iops->free_event_map = ti_sci_cmd_free_event_map;
2843
2844	rops->set_cfg = ti_sci_cmd_rm_ring_cfg;
2845
2846	psilops->pair = ti_sci_cmd_rm_psil_pair;
2847	psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2848
2849	udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2850	udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2851	udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
2852
2853	pops->request = ti_sci_cmd_proc_request;
2854	pops->release = ti_sci_cmd_proc_release;
2855	pops->handover = ti_sci_cmd_proc_handover;
2856	pops->set_config = ti_sci_cmd_proc_set_config;
2857	pops->set_control = ti_sci_cmd_proc_set_control;
2858	pops->get_status = ti_sci_cmd_proc_get_status;
2859}
2860
2861/**
2862 * ti_sci_get_handle() - Get the TI SCI handle for a device
2863 * @dev:	Pointer to device for which we want SCI handle
2864 *
2865 * NOTE: The function does not track individual clients of the framework
2866 * and is expected to be maintained by caller of TI SCI protocol library.
2867 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2868 * Return: pointer to handle if successful, else:
2869 * -EPROBE_DEFER if the instance is not ready
2870 * -ENODEV if the required node handler is missing
2871 * -EINVAL if invalid conditions are encountered.
2872 */
2873const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2874{
2875	struct device_node *ti_sci_np;
2876	struct list_head *p;
2877	struct ti_sci_handle *handle = NULL;
2878	struct ti_sci_info *info;
2879
2880	if (!dev) {
2881		pr_err("I need a device pointer\n");
2882		return ERR_PTR(-EINVAL);
2883	}
2884	ti_sci_np = of_get_parent(dev->of_node);
2885	if (!ti_sci_np) {
2886		dev_err(dev, "No OF information\n");
2887		return ERR_PTR(-EINVAL);
2888	}
2889
2890	mutex_lock(&ti_sci_list_mutex);
2891	list_for_each(p, &ti_sci_list) {
2892		info = list_entry(p, struct ti_sci_info, node);
2893		if (ti_sci_np == info->dev->of_node) {
2894			handle = &info->handle;
2895			info->users++;
2896			break;
2897		}
2898	}
2899	mutex_unlock(&ti_sci_list_mutex);
2900	of_node_put(ti_sci_np);
2901
2902	if (!handle)
2903		return ERR_PTR(-EPROBE_DEFER);
2904
2905	return handle;
2906}
2907EXPORT_SYMBOL_GPL(ti_sci_get_handle);
2908
2909/**
2910 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
2911 * @handle:	Handle acquired by ti_sci_get_handle
2912 *
2913 * NOTE: The function does not track individual clients of the framework
2914 * and is expected to be maintained by caller of TI SCI protocol library.
2915 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2916 *
2917 * Return: 0 is successfully released
2918 * if an error pointer was passed, it returns the error value back,
2919 * if null was passed, it returns -EINVAL;
2920 */
2921int ti_sci_put_handle(const struct ti_sci_handle *handle)
2922{
2923	struct ti_sci_info *info;
2924
2925	if (IS_ERR(handle))
2926		return PTR_ERR(handle);
2927	if (!handle)
2928		return -EINVAL;
2929
2930	info = handle_to_ti_sci_info(handle);
2931	mutex_lock(&ti_sci_list_mutex);
2932	if (!WARN_ON(!info->users))
2933		info->users--;
2934	mutex_unlock(&ti_sci_list_mutex);
2935
2936	return 0;
2937}
2938EXPORT_SYMBOL_GPL(ti_sci_put_handle);
2939
2940static void devm_ti_sci_release(struct device *dev, void *res)
2941{
2942	const struct ti_sci_handle **ptr = res;
2943	const struct ti_sci_handle *handle = *ptr;
2944	int ret;
2945
2946	ret = ti_sci_put_handle(handle);
2947	if (ret)
2948		dev_err(dev, "failed to put handle %d\n", ret);
2949}
2950
2951/**
2952 * devm_ti_sci_get_handle() - Managed get handle
2953 * @dev:	device for which we want SCI handle for.
2954 *
2955 * NOTE: This releases the handle once the device resources are
2956 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
2957 * The function does not track individual clients of the framework
2958 * and is expected to be maintained by caller of TI SCI protocol library.
2959 *
2960 * Return: 0 if all went fine, else corresponding error.
2961 */
2962const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
2963{
2964	const struct ti_sci_handle **ptr;
2965	const struct ti_sci_handle *handle;
2966
2967	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
2968	if (!ptr)
2969		return ERR_PTR(-ENOMEM);
2970	handle = ti_sci_get_handle(dev);
2971
2972	if (!IS_ERR(handle)) {
2973		*ptr = handle;
2974		devres_add(dev, ptr);
2975	} else {
2976		devres_free(ptr);
2977	}
2978
2979	return handle;
2980}
2981EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
2982
2983/**
2984 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2985 * @np:		device node
2986 * @property:	property name containing phandle on TISCI node
2987 *
2988 * NOTE: The function does not track individual clients of the framework
2989 * and is expected to be maintained by caller of TI SCI protocol library.
2990 * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
2991 * Return: pointer to handle if successful, else:
2992 * -EPROBE_DEFER if the instance is not ready
2993 * -ENODEV if the required node handler is missing
2994 * -EINVAL if invalid conditions are encountered.
2995 */
2996const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
2997						  const char *property)
2998{
2999	struct ti_sci_handle *handle = NULL;
3000	struct device_node *ti_sci_np;
3001	struct ti_sci_info *info;
3002	struct list_head *p;
3003
3004	if (!np) {
3005		pr_err("I need a device pointer\n");
3006		return ERR_PTR(-EINVAL);
3007	}
3008
3009	ti_sci_np = of_parse_phandle(np, property, 0);
3010	if (!ti_sci_np)
3011		return ERR_PTR(-ENODEV);
3012
3013	mutex_lock(&ti_sci_list_mutex);
3014	list_for_each(p, &ti_sci_list) {
3015		info = list_entry(p, struct ti_sci_info, node);
3016		if (ti_sci_np == info->dev->of_node) {
3017			handle = &info->handle;
3018			info->users++;
3019			break;
3020		}
3021	}
3022	mutex_unlock(&ti_sci_list_mutex);
3023	of_node_put(ti_sci_np);
3024
3025	if (!handle)
3026		return ERR_PTR(-EPROBE_DEFER);
3027
3028	return handle;
3029}
3030EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3031
3032/**
3033 * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3034 * @dev:	Device pointer requesting TISCI handle
3035 * @property:	property name containing phandle on TISCI node
3036 *
3037 * NOTE: This releases the handle once the device resources are
3038 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3039 * The function does not track individual clients of the framework
3040 * and is expected to be maintained by caller of TI SCI protocol library.
3041 *
3042 * Return: 0 if all went fine, else corresponding error.
3043 */
3044const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3045						       const char *property)
3046{
3047	const struct ti_sci_handle *handle;
3048	const struct ti_sci_handle **ptr;
3049
3050	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3051	if (!ptr)
3052		return ERR_PTR(-ENOMEM);
3053	handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3054
3055	if (!IS_ERR(handle)) {
3056		*ptr = handle;
3057		devres_add(dev, ptr);
3058	} else {
3059		devres_free(ptr);
3060	}
3061
3062	return handle;
3063}
3064EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3065
3066/**
3067 * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3068 * @res:	Pointer to the TISCI resource
3069 *
3070 * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3071 */
3072u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3073{
3074	unsigned long flags;
3075	u16 set, free_bit;
3076
3077	raw_spin_lock_irqsave(&res->lock, flags);
3078	for (set = 0; set < res->sets; set++) {
3079		struct ti_sci_resource_desc *desc = &res->desc[set];
3080		int res_count = desc->num + desc->num_sec;
3081
3082		free_bit = find_first_zero_bit(desc->res_map, res_count);
3083		if (free_bit != res_count) {
3084			__set_bit(free_bit, desc->res_map);
3085			raw_spin_unlock_irqrestore(&res->lock, flags);
3086
3087			if (desc->num && free_bit < desc->num)
3088				return desc->start + free_bit;
3089			else
3090				return desc->start_sec + free_bit;
3091		}
3092	}
3093	raw_spin_unlock_irqrestore(&res->lock, flags);
3094
3095	return TI_SCI_RESOURCE_NULL;
3096}
3097EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3098
3099/**
3100 * ti_sci_release_resource() - Release a resource from TISCI resource.
3101 * @res:	Pointer to the TISCI resource
3102 * @id:		Resource id to be released.
3103 */
3104void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3105{
3106	unsigned long flags;
3107	u16 set;
3108
3109	raw_spin_lock_irqsave(&res->lock, flags);
3110	for (set = 0; set < res->sets; set++) {
3111		struct ti_sci_resource_desc *desc = &res->desc[set];
3112
3113		if (desc->num && desc->start <= id &&
3114		    (desc->start + desc->num) > id)
3115			__clear_bit(id - desc->start, desc->res_map);
3116		else if (desc->num_sec && desc->start_sec <= id &&
3117			 (desc->start_sec + desc->num_sec) > id)
3118			__clear_bit(id - desc->start_sec, desc->res_map);
3119	}
3120	raw_spin_unlock_irqrestore(&res->lock, flags);
3121}
3122EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3123
3124/**
3125 * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3126 * @res:	Pointer to the TISCI resource
3127 *
3128 * Return: Total number of available resources.
3129 */
3130u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3131{
3132	u32 set, count = 0;
3133
3134	for (set = 0; set < res->sets; set++)
3135		count += res->desc[set].num + res->desc[set].num_sec;
3136
3137	return count;
3138}
3139EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3140
3141/**
3142 * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
3143 * @handle:	TISCI handle
3144 * @dev:	Device pointer to which the resource is assigned
3145 * @dev_id:	TISCI device id to which the resource is assigned
3146 * @sub_types:	Array of sub_types assigned corresponding to device
3147 * @sets:	Number of sub_types
3148 *
3149 * Return: Pointer to ti_sci_resource if all went well else appropriate
3150 *	   error pointer.
3151 */
3152static struct ti_sci_resource *
3153devm_ti_sci_get_resource_sets(const struct ti_sci_handle *handle,
3154			      struct device *dev, u32 dev_id, u32 *sub_types,
3155			      u32 sets)
3156{
3157	struct ti_sci_resource *res;
3158	bool valid_set = false;
3159	int i, ret, res_count;
3160
3161	res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3162	if (!res)
3163		return ERR_PTR(-ENOMEM);
3164
3165	res->sets = sets;
3166	res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3167				 GFP_KERNEL);
3168	if (!res->desc)
3169		return ERR_PTR(-ENOMEM);
3170
3171	for (i = 0; i < res->sets; i++) {
3172		ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3173							sub_types[i],
3174							&res->desc[i]);
3175		if (ret) {
3176			dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
3177				dev_id, sub_types[i]);
3178			memset(&res->desc[i], 0, sizeof(res->desc[i]));
3179			continue;
3180		}
3181
3182		dev_dbg(dev, "dev/sub_type: %d/%d, start/num: %d/%d | %d/%d\n",
3183			dev_id, sub_types[i], res->desc[i].start,
3184			res->desc[i].num, res->desc[i].start_sec,
3185			res->desc[i].num_sec);
3186
3187		valid_set = true;
3188		res_count = res->desc[i].num + res->desc[i].num_sec;
3189		res->desc[i].res_map = devm_bitmap_zalloc(dev, res_count,
3190							  GFP_KERNEL);
3191		if (!res->desc[i].res_map)
3192			return ERR_PTR(-ENOMEM);
3193	}
3194	raw_spin_lock_init(&res->lock);
3195
3196	if (valid_set)
3197		return res;
3198
3199	return ERR_PTR(-EINVAL);
3200}
3201
3202/**
3203 * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
3204 * @handle:	TISCI handle
3205 * @dev:	Device pointer to which the resource is assigned
3206 * @dev_id:	TISCI device id to which the resource is assigned
3207 * @of_prop:	property name by which the resource are represented
3208 *
3209 * Return: Pointer to ti_sci_resource if all went well else appropriate
3210 *	   error pointer.
3211 */
3212struct ti_sci_resource *
3213devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
3214			    struct device *dev, u32 dev_id, char *of_prop)
3215{
3216	struct ti_sci_resource *res;
3217	u32 *sub_types;
3218	int sets;
3219
3220	sets = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
3221					       sizeof(u32));
3222	if (sets < 0) {
3223		dev_err(dev, "%s resource type ids not available\n", of_prop);
3224		return ERR_PTR(sets);
3225	}
3226
3227	sub_types = kcalloc(sets, sizeof(*sub_types), GFP_KERNEL);
3228	if (!sub_types)
3229		return ERR_PTR(-ENOMEM);
3230
3231	of_property_read_u32_array(dev_of_node(dev), of_prop, sub_types, sets);
3232	res = devm_ti_sci_get_resource_sets(handle, dev, dev_id, sub_types,
3233					    sets);
3234
3235	kfree(sub_types);
3236	return res;
3237}
3238EXPORT_SYMBOL_GPL(devm_ti_sci_get_of_resource);
3239
3240/**
3241 * devm_ti_sci_get_resource() - Get a resource range assigned to the device
3242 * @handle:	TISCI handle
3243 * @dev:	Device pointer to which the resource is assigned
3244 * @dev_id:	TISCI device id to which the resource is assigned
3245 * @sub_type:	TISCI resource subytpe representing the resource.
3246 *
3247 * Return: Pointer to ti_sci_resource if all went well else appropriate
3248 *	   error pointer.
3249 */
3250struct ti_sci_resource *
3251devm_ti_sci_get_resource(const struct ti_sci_handle *handle, struct device *dev,
3252			 u32 dev_id, u32 sub_type)
3253{
3254	return devm_ti_sci_get_resource_sets(handle, dev, dev_id, &sub_type, 1);
3255}
3256EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
3257
3258static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
3259				void *cmd)
3260{
3261	struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
3262	const struct ti_sci_handle *handle = &info->handle;
3263
3264	ti_sci_cmd_core_reboot(handle);
3265
3266	/* call fail OR pass, we should not be here in the first place */
3267	return NOTIFY_BAD;
3268}
3269
3270/* Description for K2G */
3271static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3272	.default_host_id = 2,
3273	/* Conservative duration */
3274	.max_rx_timeout_ms = 1000,
3275	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3276	.max_msgs = 20,
3277	.max_msg_size = 64,
3278};
3279
3280/* Description for AM654 */
3281static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3282	.default_host_id = 12,
3283	/* Conservative duration */
3284	.max_rx_timeout_ms = 10000,
3285	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3286	.max_msgs = 20,
3287	.max_msg_size = 60,
3288};
3289
3290static const struct of_device_id ti_sci_of_match[] = {
3291	{.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
3292	{.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
3293	{ /* Sentinel */ },
3294};
3295MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3296
3297static int ti_sci_probe(struct platform_device *pdev)
3298{
3299	struct device *dev = &pdev->dev;
3300	const struct of_device_id *of_id;
3301	const struct ti_sci_desc *desc;
3302	struct ti_sci_xfer *xfer;
3303	struct ti_sci_info *info = NULL;
3304	struct ti_sci_xfers_info *minfo;
3305	struct mbox_client *cl;
3306	int ret = -EINVAL;
3307	int i;
3308	int reboot = 0;
3309	u32 h_id;
3310
3311	of_id = of_match_device(ti_sci_of_match, dev);
3312	if (!of_id) {
3313		dev_err(dev, "OF data missing\n");
3314		return -EINVAL;
3315	}
3316	desc = of_id->data;
3317
3318	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3319	if (!info)
3320		return -ENOMEM;
3321
3322	info->dev = dev;
3323	info->desc = desc;
3324	ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
3325	/* if the property is not present in DT, use a default from desc */
3326	if (ret < 0) {
3327		info->host_id = info->desc->default_host_id;
3328	} else {
3329		if (!h_id) {
3330			dev_warn(dev, "Host ID 0 is reserved for firmware\n");
3331			info->host_id = info->desc->default_host_id;
3332		} else {
3333			info->host_id = h_id;
3334		}
3335	}
3336
3337	reboot = of_property_read_bool(dev->of_node,
3338				       "ti,system-reboot-controller");
3339	INIT_LIST_HEAD(&info->node);
3340	minfo = &info->minfo;
3341
3342	/*
3343	 * Pre-allocate messages
3344	 * NEVER allocate more than what we can indicate in hdr.seq
3345	 * if we have data description bug, force a fix..
3346	 */
3347	if (WARN_ON(desc->max_msgs >=
3348		    1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
3349		return -EINVAL;
3350
3351	minfo->xfer_block = devm_kcalloc(dev,
3352					 desc->max_msgs,
3353					 sizeof(*minfo->xfer_block),
3354					 GFP_KERNEL);
3355	if (!minfo->xfer_block)
3356		return -ENOMEM;
3357
3358	minfo->xfer_alloc_table = devm_bitmap_zalloc(dev,
3359						     desc->max_msgs,
3360						     GFP_KERNEL);
3361	if (!minfo->xfer_alloc_table)
3362		return -ENOMEM;
3363
3364	/* Pre-initialize the buffer pointer to pre-allocated buffers */
3365	for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3366		xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3367					      GFP_KERNEL);
3368		if (!xfer->xfer_buf)
3369			return -ENOMEM;
3370
3371		xfer->tx_message.buf = xfer->xfer_buf;
3372		init_completion(&xfer->done);
3373	}
3374
3375	ret = ti_sci_debugfs_create(pdev, info);
3376	if (ret)
3377		dev_warn(dev, "Failed to create debug file\n");
3378
3379	platform_set_drvdata(pdev, info);
3380
3381	cl = &info->cl;
3382	cl->dev = dev;
3383	cl->tx_block = false;
3384	cl->rx_callback = ti_sci_rx_callback;
3385	cl->knows_txdone = true;
3386
3387	spin_lock_init(&minfo->xfer_lock);
3388	sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3389
3390	info->chan_rx = mbox_request_channel_byname(cl, "rx");
3391	if (IS_ERR(info->chan_rx)) {
3392		ret = PTR_ERR(info->chan_rx);
3393		goto out;
3394	}
3395
3396	info->chan_tx = mbox_request_channel_byname(cl, "tx");
3397	if (IS_ERR(info->chan_tx)) {
3398		ret = PTR_ERR(info->chan_tx);
3399		goto out;
3400	}
3401	ret = ti_sci_cmd_get_revision(info);
3402	if (ret) {
3403		dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3404		goto out;
3405	}
3406
3407	ti_sci_setup_ops(info);
3408
3409	if (reboot) {
3410		info->nb.notifier_call = tisci_reboot_handler;
3411		info->nb.priority = 128;
3412
3413		ret = register_restart_handler(&info->nb);
3414		if (ret) {
3415			dev_err(dev, "reboot registration fail(%d)\n", ret);
3416			goto out;
3417		}
3418	}
3419
3420	dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3421		 info->handle.version.abi_major, info->handle.version.abi_minor,
3422		 info->handle.version.firmware_revision,
3423		 info->handle.version.firmware_description);
3424
3425	mutex_lock(&ti_sci_list_mutex);
3426	list_add_tail(&info->node, &ti_sci_list);
3427	mutex_unlock(&ti_sci_list_mutex);
3428
3429	return of_platform_populate(dev->of_node, NULL, NULL, dev);
3430out:
3431	if (!IS_ERR(info->chan_tx))
3432		mbox_free_channel(info->chan_tx);
3433	if (!IS_ERR(info->chan_rx))
3434		mbox_free_channel(info->chan_rx);
3435	debugfs_remove(info->d);
3436	return ret;
3437}
3438
3439static struct platform_driver ti_sci_driver = {
3440	.probe = ti_sci_probe,
3441	.driver = {
3442		   .name = "ti-sci",
3443		   .of_match_table = of_match_ptr(ti_sci_of_match),
3444		   .suppress_bind_attrs = true,
3445	},
3446};
3447module_platform_driver(ti_sci_driver);
3448
3449MODULE_LICENSE("GPL v2");
3450MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3451MODULE_AUTHOR("Nishanth Menon");
3452MODULE_ALIAS("platform:ti-sci");
3453