• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
29 static LIST_HEAD(ti_sci_list);
30 /* Protection for the entire list */
31 static 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  */
43 struct 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  */
60 struct 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  */
76 struct 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  */
101 struct 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  */
ti_sci_debug_show(struct seq_file * s,void * unused)133 static 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*/
150 DEFINE_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  */
ti_sci_debugfs_create(struct platform_device * pdev,struct ti_sci_info * info)159 static 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 */
ti_sci_debugfs_create(struct platform_device * dev,struct ti_sci_info * info)194 static inline int ti_sci_debugfs_create(struct platform_device *dev,
195 					struct ti_sci_info *info)
196 {
197 	return 0;
198 }
199 
ti_sci_debugfs_destroy(struct platform_device * dev,struct ti_sci_info * info)200 static 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  */
ti_sci_dump_header_dbg(struct device * dev,struct ti_sci_msg_hdr * hdr)211 static 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  */
ti_sci_rx_callback(struct mbox_client * cl,void * m)229 static 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  */
ti_sci_get_one_xfer(struct ti_sci_info * info,u16 msg_type,u32 msg_flags,size_t tx_message_size,size_t rx_message_size)289 static 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  */
ti_sci_put_one_xfer(struct ti_sci_xfers_info * minfo,struct ti_sci_xfer * xfer)360 static 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  */
ti_sci_do_xfer(struct ti_sci_info * info,struct ti_sci_xfer * xfer)392 static 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  */
ti_sci_cmd_get_revision(struct ti_sci_info * info)445 static 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 
478 fail:
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  */
ti_sci_is_response_ack(void * r)489 static 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  */
ti_sci_set_device_state(const struct ti_sci_handle * handle,u32 id,u32 flags,u8 state)505 static 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 
545 fail:
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  */
ti_sci_get_device_state(const struct ti_sci_handle * handle,u32 id,u32 * clcnt,u32 * resets,u8 * p_state,u8 * c_state)562 static 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;
615 fail:
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  */
ti_sci_cmd_get_device(const struct ti_sci_handle * handle,u32 id)633 static 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  */
ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle * handle,u32 id)652 static 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  */
ti_sci_cmd_idle_device(const struct ti_sci_handle * handle,u32 id)671 static 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  */
ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle * handle,u32 id)690 static 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  */
ti_sci_cmd_put_device(const struct ti_sci_handle * handle,u32 id)709 static 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  */
ti_sci_cmd_dev_is_valid(const struct ti_sci_handle * handle,u32 id)723 static 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  */
ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle * handle,u32 id,u32 * count)739 static 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  */
ti_sci_cmd_dev_is_idle(const struct ti_sci_handle * handle,u32 id,bool * r_state)753 static 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  */
ti_sci_cmd_dev_is_stop(const struct ti_sci_handle * handle,u32 id,bool * r_state,bool * curr_state)780 static 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  */
ti_sci_cmd_dev_is_on(const struct ti_sci_handle * handle,u32 id,bool * r_state,bool * curr_state)811 static 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  */
ti_sci_cmd_dev_is_trans(const struct ti_sci_handle * handle,u32 id,bool * curr_state)841 static 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  */
ti_sci_cmd_set_device_resets(const struct ti_sci_handle * handle,u32 id,u32 reset_state)868 static 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 
908 fail:
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  */
ti_sci_cmd_get_device_resets(const struct ti_sci_handle * handle,u32 id,u32 * reset_state)923 static 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  */
ti_sci_set_clock_state(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 flags,u8 state)942 static 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 
989 fail:
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  */
ti_sci_cmd_get_clock_state(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u8 * programmed_state,u8 * current_state)1007 static 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 
1064 fail:
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  */
ti_sci_cmd_get_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool needs_ssc,bool can_change_freq,bool enable_input_term)1083 static 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  */
ti_sci_cmd_idle_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id)1109 static 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  */
ti_sci_cmd_put_clock(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id)1129 static 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  */
ti_sci_cmd_clk_is_auto(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state)1148 static 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  */
ti_sci_cmd_clk_is_on(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state,bool * curr_state)1177 static 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  */
ti_sci_cmd_clk_is_off(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,bool * req_state,bool * curr_state)1210 static 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  */
ti_sci_cmd_clk_set_parent(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 parent_id)1242 static 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 
1293 fail:
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  */
ti_sci_cmd_clk_get_parent(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 * parent_id)1310 static 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 
1362 fail:
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  */
ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u32 * num_parents)1379 static 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 
1432 fail:
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  */
ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 min_freq,u64 target_freq,u64 max_freq,u64 * match_freq)1457 static 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 
1510 fail:
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  */
ti_sci_cmd_clk_set_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 min_freq,u64 target_freq,u64 max_freq)1534 static 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 
1583 fail:
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  */
ti_sci_cmd_clk_get_freq(const struct ti_sci_handle * handle,u32 dev_id,u32 clk_id,u64 * freq)1600 static 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 
1648 fail:
1649 	ti_sci_put_one_xfer(&info->minfo, xfer);
1650 
1651 	return ret;
1652 }
1653 
ti_sci_cmd_core_reboot(const struct ti_sci_handle * handle)1654 static 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 
1694 fail:
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  */
ti_sci_get_resource_range(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,u8 s_host,struct ti_sci_resource_desc * desc)1714 static 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 
1767 fail:
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  */
ti_sci_cmd_get_resource_range(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,struct ti_sci_resource_desc * desc)1785 static 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  */
1807 static
ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle * handle,u32 dev_id,u8 subtype,u8 s_host,struct ti_sci_resource_desc * desc)1808 int 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  */
ti_sci_manage_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host,u16 type)1834 static 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 
1884 fail:
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  */
ti_sci_set_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host)1908 static 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  */
ti_sci_free_irq(const struct ti_sci_handle * handle,u32 valid_params,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit,u8 s_host)1942 static 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  * @vint_irq:		Boolean specifying if this interrupt belongs to
1967  *			Interrupt Aggregator.
1968  *
1969  * Return: 0 if all went fine, else return appropriate error.
1970  */
ti_sci_cmd_set_irq(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq)1971 static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
1972 			      u16 src_index, u16 dst_id, u16 dst_host_irq)
1973 {
1974 	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
1975 
1976 	return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
1977 			      dst_host_irq, 0, 0, 0, 0, 0);
1978 }
1979 
1980 /**
1981  * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
1982  *				requested source and Interrupt Aggregator.
1983  * @handle:		Pointer to TISCI handle.
1984  * @src_id:		Device ID of the IRQ source
1985  * @src_index:		IRQ source index within the source device
1986  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
1987  * @vint:		Virtual interrupt to be used within the IA
1988  * @global_event:	Global event number to be used for the requesting event
1989  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
1990  *
1991  * Return: 0 if all went fine, else return appropriate error.
1992  */
ti_sci_cmd_set_event_map(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit)1993 static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
1994 				    u16 src_id, u16 src_index, u16 ia_id,
1995 				    u16 vint, u16 global_event,
1996 				    u8 vint_status_bit)
1997 {
1998 	u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
1999 			   MSG_FLAG_GLB_EVNT_VALID |
2000 			   MSG_FLAG_VINT_STS_BIT_VALID;
2001 
2002 	return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2003 			      ia_id, vint, global_event, vint_status_bit, 0);
2004 }
2005 
2006 /**
2007  * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2008  *			   requested source and destination.
2009  * @handle:		Pointer to TISCI handle.
2010  * @src_id:		Device ID of the IRQ source
2011  * @src_index:		IRQ source index within the source device
2012  * @dst_id:		Device ID of the IRQ destination
2013  * @dst_host_irq:	IRQ number of the destination device
2014  * @vint_irq:		Boolean specifying if this interrupt belongs to
2015  *			Interrupt Aggregator.
2016  *
2017  * Return: 0 if all went fine, else return appropriate error.
2018  */
ti_sci_cmd_free_irq(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 dst_id,u16 dst_host_irq)2019 static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2020 			       u16 src_index, u16 dst_id, u16 dst_host_irq)
2021 {
2022 	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2023 
2024 	return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2025 			       dst_host_irq, 0, 0, 0, 0, 0);
2026 }
2027 
2028 /**
2029  * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2030  *				 and Interrupt Aggregator.
2031  * @handle:		Pointer to TISCI handle.
2032  * @src_id:		Device ID of the IRQ source
2033  * @src_index:		IRQ source index within the source device
2034  * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
2035  * @vint:		Virtual interrupt to be used within the IA
2036  * @global_event:	Global event number to be used for the requesting event
2037  * @vint_status_bit:	Virtual interrupt status bit to be used for the event
2038  *
2039  * Return: 0 if all went fine, else return appropriate error.
2040  */
ti_sci_cmd_free_event_map(const struct ti_sci_handle * handle,u16 src_id,u16 src_index,u16 ia_id,u16 vint,u16 global_event,u8 vint_status_bit)2041 static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2042 				     u16 src_id, u16 src_index, u16 ia_id,
2043 				     u16 vint, u16 global_event,
2044 				     u8 vint_status_bit)
2045 {
2046 	u32 valid_params = MSG_FLAG_IA_ID_VALID |
2047 			   MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2048 			   MSG_FLAG_VINT_STS_BIT_VALID;
2049 
2050 	return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2051 			       ia_id, vint, global_event, vint_status_bit, 0);
2052 }
2053 
2054 /**
2055  * ti_sci_cmd_rm_ring_cfg() - Configure a NAVSS ring
2056  * @handle:	Pointer to TI SCI handle.
2057  * @params:	Pointer to ti_sci_msg_rm_ring_cfg ring config structure
2058  *
2059  * Return: 0 if all went well, else returns appropriate error value.
2060  *
2061  * See @ti_sci_msg_rm_ring_cfg and @ti_sci_msg_rm_ring_cfg_req for
2062  * more info.
2063  */
ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_ring_cfg * params)2064 static int ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle *handle,
2065 				  const struct ti_sci_msg_rm_ring_cfg *params)
2066 {
2067 	struct ti_sci_msg_rm_ring_cfg_req *req;
2068 	struct ti_sci_msg_hdr *resp;
2069 	struct ti_sci_xfer *xfer;
2070 	struct ti_sci_info *info;
2071 	struct device *dev;
2072 	int ret = 0;
2073 
2074 	if (IS_ERR_OR_NULL(handle))
2075 		return -EINVAL;
2076 
2077 	info = handle_to_ti_sci_info(handle);
2078 	dev = info->dev;
2079 
2080 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2081 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2082 				   sizeof(*req), sizeof(*resp));
2083 	if (IS_ERR(xfer)) {
2084 		ret = PTR_ERR(xfer);
2085 		dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
2086 		return ret;
2087 	}
2088 	req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
2089 	req->valid_params = params->valid_params;
2090 	req->nav_id = params->nav_id;
2091 	req->index = params->index;
2092 	req->addr_lo = params->addr_lo;
2093 	req->addr_hi = params->addr_hi;
2094 	req->count = params->count;
2095 	req->mode = params->mode;
2096 	req->size = params->size;
2097 	req->order_id = params->order_id;
2098 	req->virtid = params->virtid;
2099 	req->asel = params->asel;
2100 
2101 	ret = ti_sci_do_xfer(info, xfer);
2102 	if (ret) {
2103 		dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
2104 		goto fail;
2105 	}
2106 
2107 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2108 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2109 
2110 fail:
2111 	ti_sci_put_one_xfer(&info->minfo, xfer);
2112 	dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", params->index, ret);
2113 	return ret;
2114 }
2115 
2116 /**
2117  * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
2118  * @handle:	Pointer to TI SCI handle.
2119  * @nav_id:	Device ID of Navigator Subsystem which should be used for
2120  *		pairing
2121  * @src_thread:	Source PSI-L thread ID
2122  * @dst_thread: Destination PSI-L thread ID
2123  *
2124  * Return: 0 if all went well, else returns appropriate error value.
2125  */
ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle * handle,u32 nav_id,u32 src_thread,u32 dst_thread)2126 static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2127 				   u32 nav_id, u32 src_thread, u32 dst_thread)
2128 {
2129 	struct ti_sci_msg_psil_pair *req;
2130 	struct ti_sci_msg_hdr *resp;
2131 	struct ti_sci_xfer *xfer;
2132 	struct ti_sci_info *info;
2133 	struct device *dev;
2134 	int ret = 0;
2135 
2136 	if (IS_ERR(handle))
2137 		return PTR_ERR(handle);
2138 	if (!handle)
2139 		return -EINVAL;
2140 
2141 	info = handle_to_ti_sci_info(handle);
2142 	dev = info->dev;
2143 
2144 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2145 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2146 				   sizeof(*req), sizeof(*resp));
2147 	if (IS_ERR(xfer)) {
2148 		ret = PTR_ERR(xfer);
2149 		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2150 		return ret;
2151 	}
2152 	req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
2153 	req->nav_id = nav_id;
2154 	req->src_thread = src_thread;
2155 	req->dst_thread = dst_thread;
2156 
2157 	ret = ti_sci_do_xfer(info, xfer);
2158 	if (ret) {
2159 		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2160 		goto fail;
2161 	}
2162 
2163 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2164 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2165 
2166 fail:
2167 	ti_sci_put_one_xfer(&info->minfo, xfer);
2168 
2169 	return ret;
2170 }
2171 
2172 /**
2173  * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
2174  * @handle:	Pointer to TI SCI handle.
2175  * @nav_id:	Device ID of Navigator Subsystem which should be used for
2176  *		unpairing
2177  * @src_thread:	Source PSI-L thread ID
2178  * @dst_thread:	Destination PSI-L thread ID
2179  *
2180  * Return: 0 if all went well, else returns appropriate error value.
2181  */
ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle * handle,u32 nav_id,u32 src_thread,u32 dst_thread)2182 static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2183 				     u32 nav_id, u32 src_thread, u32 dst_thread)
2184 {
2185 	struct ti_sci_msg_psil_unpair *req;
2186 	struct ti_sci_msg_hdr *resp;
2187 	struct ti_sci_xfer *xfer;
2188 	struct ti_sci_info *info;
2189 	struct device *dev;
2190 	int ret = 0;
2191 
2192 	if (IS_ERR(handle))
2193 		return PTR_ERR(handle);
2194 	if (!handle)
2195 		return -EINVAL;
2196 
2197 	info = handle_to_ti_sci_info(handle);
2198 	dev = info->dev;
2199 
2200 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2201 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2202 				   sizeof(*req), sizeof(*resp));
2203 	if (IS_ERR(xfer)) {
2204 		ret = PTR_ERR(xfer);
2205 		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2206 		return ret;
2207 	}
2208 	req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
2209 	req->nav_id = nav_id;
2210 	req->src_thread = src_thread;
2211 	req->dst_thread = dst_thread;
2212 
2213 	ret = ti_sci_do_xfer(info, xfer);
2214 	if (ret) {
2215 		dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2216 		goto fail;
2217 	}
2218 
2219 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2220 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2221 
2222 fail:
2223 	ti_sci_put_one_xfer(&info->minfo, xfer);
2224 
2225 	return ret;
2226 }
2227 
2228 /**
2229  * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
2230  * @handle:	Pointer to TI SCI handle.
2231  * @params:	Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
2232  *		structure
2233  *
2234  * Return: 0 if all went well, else returns appropriate error value.
2235  *
2236  * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
2237  * more info.
2238  */
ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_tx_ch_cfg * params)2239 static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
2240 			const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2241 {
2242 	struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
2243 	struct ti_sci_msg_hdr *resp;
2244 	struct ti_sci_xfer *xfer;
2245 	struct ti_sci_info *info;
2246 	struct device *dev;
2247 	int ret = 0;
2248 
2249 	if (IS_ERR_OR_NULL(handle))
2250 		return -EINVAL;
2251 
2252 	info = handle_to_ti_sci_info(handle);
2253 	dev = info->dev;
2254 
2255 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2256 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2257 				   sizeof(*req), sizeof(*resp));
2258 	if (IS_ERR(xfer)) {
2259 		ret = PTR_ERR(xfer);
2260 		dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2261 		return ret;
2262 	}
2263 	req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
2264 	req->valid_params = params->valid_params;
2265 	req->nav_id = params->nav_id;
2266 	req->index = params->index;
2267 	req->tx_pause_on_err = params->tx_pause_on_err;
2268 	req->tx_filt_einfo = params->tx_filt_einfo;
2269 	req->tx_filt_pswords = params->tx_filt_pswords;
2270 	req->tx_atype = params->tx_atype;
2271 	req->tx_chan_type = params->tx_chan_type;
2272 	req->tx_supr_tdpkt = params->tx_supr_tdpkt;
2273 	req->tx_fetch_size = params->tx_fetch_size;
2274 	req->tx_credit_count = params->tx_credit_count;
2275 	req->txcq_qnum = params->txcq_qnum;
2276 	req->tx_priority = params->tx_priority;
2277 	req->tx_qos = params->tx_qos;
2278 	req->tx_orderid = params->tx_orderid;
2279 	req->fdepth = params->fdepth;
2280 	req->tx_sched_priority = params->tx_sched_priority;
2281 	req->tx_burst_size = params->tx_burst_size;
2282 	req->tx_tdtype = params->tx_tdtype;
2283 	req->extended_ch_type = params->extended_ch_type;
2284 
2285 	ret = ti_sci_do_xfer(info, xfer);
2286 	if (ret) {
2287 		dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2288 		goto fail;
2289 	}
2290 
2291 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2292 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2293 
2294 fail:
2295 	ti_sci_put_one_xfer(&info->minfo, xfer);
2296 	dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2297 	return ret;
2298 }
2299 
2300 /**
2301  * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
2302  * @handle:	Pointer to TI SCI handle.
2303  * @params:	Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
2304  *		structure
2305  *
2306  * Return: 0 if all went well, else returns appropriate error value.
2307  *
2308  * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
2309  * more info.
2310  */
ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_rx_ch_cfg * params)2311 static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
2312 			const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2313 {
2314 	struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
2315 	struct ti_sci_msg_hdr *resp;
2316 	struct ti_sci_xfer *xfer;
2317 	struct ti_sci_info *info;
2318 	struct device *dev;
2319 	int ret = 0;
2320 
2321 	if (IS_ERR_OR_NULL(handle))
2322 		return -EINVAL;
2323 
2324 	info = handle_to_ti_sci_info(handle);
2325 	dev = info->dev;
2326 
2327 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2328 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2329 				   sizeof(*req), sizeof(*resp));
2330 	if (IS_ERR(xfer)) {
2331 		ret = PTR_ERR(xfer);
2332 		dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2333 		return ret;
2334 	}
2335 	req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
2336 	req->valid_params = params->valid_params;
2337 	req->nav_id = params->nav_id;
2338 	req->index = params->index;
2339 	req->rx_fetch_size = params->rx_fetch_size;
2340 	req->rxcq_qnum = params->rxcq_qnum;
2341 	req->rx_priority = params->rx_priority;
2342 	req->rx_qos = params->rx_qos;
2343 	req->rx_orderid = params->rx_orderid;
2344 	req->rx_sched_priority = params->rx_sched_priority;
2345 	req->flowid_start = params->flowid_start;
2346 	req->flowid_cnt = params->flowid_cnt;
2347 	req->rx_pause_on_err = params->rx_pause_on_err;
2348 	req->rx_atype = params->rx_atype;
2349 	req->rx_chan_type = params->rx_chan_type;
2350 	req->rx_ignore_short = params->rx_ignore_short;
2351 	req->rx_ignore_long = params->rx_ignore_long;
2352 	req->rx_burst_size = params->rx_burst_size;
2353 
2354 	ret = ti_sci_do_xfer(info, xfer);
2355 	if (ret) {
2356 		dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2357 		goto fail;
2358 	}
2359 
2360 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2361 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2362 
2363 fail:
2364 	ti_sci_put_one_xfer(&info->minfo, xfer);
2365 	dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2366 	return ret;
2367 }
2368 
2369 /**
2370  * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
2371  * @handle:	Pointer to TI SCI handle.
2372  * @params:	Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
2373  *		structure
2374  *
2375  * Return: 0 if all went well, else returns appropriate error value.
2376  *
2377  * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
2378  * more info.
2379  */
ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle * handle,const struct ti_sci_msg_rm_udmap_flow_cfg * params)2380 static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
2381 			const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2382 {
2383 	struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
2384 	struct ti_sci_msg_hdr *resp;
2385 	struct ti_sci_xfer *xfer;
2386 	struct ti_sci_info *info;
2387 	struct device *dev;
2388 	int ret = 0;
2389 
2390 	if (IS_ERR_OR_NULL(handle))
2391 		return -EINVAL;
2392 
2393 	info = handle_to_ti_sci_info(handle);
2394 	dev = info->dev;
2395 
2396 	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2397 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2398 				   sizeof(*req), sizeof(*resp));
2399 	if (IS_ERR(xfer)) {
2400 		ret = PTR_ERR(xfer);
2401 		dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
2402 		return ret;
2403 	}
2404 	req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
2405 	req->valid_params = params->valid_params;
2406 	req->nav_id = params->nav_id;
2407 	req->flow_index = params->flow_index;
2408 	req->rx_einfo_present = params->rx_einfo_present;
2409 	req->rx_psinfo_present = params->rx_psinfo_present;
2410 	req->rx_error_handling = params->rx_error_handling;
2411 	req->rx_desc_type = params->rx_desc_type;
2412 	req->rx_sop_offset = params->rx_sop_offset;
2413 	req->rx_dest_qnum = params->rx_dest_qnum;
2414 	req->rx_src_tag_hi = params->rx_src_tag_hi;
2415 	req->rx_src_tag_lo = params->rx_src_tag_lo;
2416 	req->rx_dest_tag_hi = params->rx_dest_tag_hi;
2417 	req->rx_dest_tag_lo = params->rx_dest_tag_lo;
2418 	req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2419 	req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2420 	req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2421 	req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2422 	req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2423 	req->rx_fdq1_qnum = params->rx_fdq1_qnum;
2424 	req->rx_fdq2_qnum = params->rx_fdq2_qnum;
2425 	req->rx_fdq3_qnum = params->rx_fdq3_qnum;
2426 	req->rx_ps_location = params->rx_ps_location;
2427 
2428 	ret = ti_sci_do_xfer(info, xfer);
2429 	if (ret) {
2430 		dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
2431 		goto fail;
2432 	}
2433 
2434 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2435 	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2436 
2437 fail:
2438 	ti_sci_put_one_xfer(&info->minfo, xfer);
2439 	dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2440 	return ret;
2441 }
2442 
2443 /**
2444  * ti_sci_cmd_proc_request() - Command to request a physical processor control
2445  * @handle:	Pointer to TI SCI handle
2446  * @proc_id:	Processor ID this request is for
2447  *
2448  * Return: 0 if all went well, else returns appropriate error value.
2449  */
ti_sci_cmd_proc_request(const struct ti_sci_handle * handle,u8 proc_id)2450 static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
2451 				   u8 proc_id)
2452 {
2453 	struct ti_sci_msg_req_proc_request *req;
2454 	struct ti_sci_msg_hdr *resp;
2455 	struct ti_sci_info *info;
2456 	struct ti_sci_xfer *xfer;
2457 	struct device *dev;
2458 	int ret = 0;
2459 
2460 	if (!handle)
2461 		return -EINVAL;
2462 	if (IS_ERR(handle))
2463 		return PTR_ERR(handle);
2464 
2465 	info = handle_to_ti_sci_info(handle);
2466 	dev = info->dev;
2467 
2468 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
2469 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2470 				   sizeof(*req), sizeof(*resp));
2471 	if (IS_ERR(xfer)) {
2472 		ret = PTR_ERR(xfer);
2473 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2474 		return ret;
2475 	}
2476 	req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
2477 	req->processor_id = proc_id;
2478 
2479 	ret = ti_sci_do_xfer(info, xfer);
2480 	if (ret) {
2481 		dev_err(dev, "Mbox send fail %d\n", ret);
2482 		goto fail;
2483 	}
2484 
2485 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2486 
2487 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2488 
2489 fail:
2490 	ti_sci_put_one_xfer(&info->minfo, xfer);
2491 
2492 	return ret;
2493 }
2494 
2495 /**
2496  * ti_sci_cmd_proc_release() - Command to release a physical processor control
2497  * @handle:	Pointer to TI SCI handle
2498  * @proc_id:	Processor ID this request is for
2499  *
2500  * Return: 0 if all went well, else returns appropriate error value.
2501  */
ti_sci_cmd_proc_release(const struct ti_sci_handle * handle,u8 proc_id)2502 static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
2503 				   u8 proc_id)
2504 {
2505 	struct ti_sci_msg_req_proc_release *req;
2506 	struct ti_sci_msg_hdr *resp;
2507 	struct ti_sci_info *info;
2508 	struct ti_sci_xfer *xfer;
2509 	struct device *dev;
2510 	int ret = 0;
2511 
2512 	if (!handle)
2513 		return -EINVAL;
2514 	if (IS_ERR(handle))
2515 		return PTR_ERR(handle);
2516 
2517 	info = handle_to_ti_sci_info(handle);
2518 	dev = info->dev;
2519 
2520 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
2521 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2522 				   sizeof(*req), sizeof(*resp));
2523 	if (IS_ERR(xfer)) {
2524 		ret = PTR_ERR(xfer);
2525 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2526 		return ret;
2527 	}
2528 	req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
2529 	req->processor_id = proc_id;
2530 
2531 	ret = ti_sci_do_xfer(info, xfer);
2532 	if (ret) {
2533 		dev_err(dev, "Mbox send fail %d\n", ret);
2534 		goto fail;
2535 	}
2536 
2537 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2538 
2539 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2540 
2541 fail:
2542 	ti_sci_put_one_xfer(&info->minfo, xfer);
2543 
2544 	return ret;
2545 }
2546 
2547 /**
2548  * ti_sci_cmd_proc_handover() - Command to handover a physical processor
2549  *				control to a host in the processor's access
2550  *				control list.
2551  * @handle:	Pointer to TI SCI handle
2552  * @proc_id:	Processor ID this request is for
2553  * @host_id:	Host ID to get the control of the processor
2554  *
2555  * Return: 0 if all went well, else returns appropriate error value.
2556  */
ti_sci_cmd_proc_handover(const struct ti_sci_handle * handle,u8 proc_id,u8 host_id)2557 static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
2558 				    u8 proc_id, u8 host_id)
2559 {
2560 	struct ti_sci_msg_req_proc_handover *req;
2561 	struct ti_sci_msg_hdr *resp;
2562 	struct ti_sci_info *info;
2563 	struct ti_sci_xfer *xfer;
2564 	struct device *dev;
2565 	int ret = 0;
2566 
2567 	if (!handle)
2568 		return -EINVAL;
2569 	if (IS_ERR(handle))
2570 		return PTR_ERR(handle);
2571 
2572 	info = handle_to_ti_sci_info(handle);
2573 	dev = info->dev;
2574 
2575 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
2576 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2577 				   sizeof(*req), sizeof(*resp));
2578 	if (IS_ERR(xfer)) {
2579 		ret = PTR_ERR(xfer);
2580 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2581 		return ret;
2582 	}
2583 	req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
2584 	req->processor_id = proc_id;
2585 	req->host_id = host_id;
2586 
2587 	ret = ti_sci_do_xfer(info, xfer);
2588 	if (ret) {
2589 		dev_err(dev, "Mbox send fail %d\n", ret);
2590 		goto fail;
2591 	}
2592 
2593 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2594 
2595 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2596 
2597 fail:
2598 	ti_sci_put_one_xfer(&info->minfo, xfer);
2599 
2600 	return ret;
2601 }
2602 
2603 /**
2604  * ti_sci_cmd_proc_set_config() - Command to set the processor boot
2605  *				    configuration flags
2606  * @handle:		Pointer to TI SCI handle
2607  * @proc_id:		Processor ID this request is for
2608  * @config_flags_set:	Configuration flags to be set
2609  * @config_flags_clear:	Configuration flags to be cleared.
2610  *
2611  * Return: 0 if all went well, else returns appropriate error value.
2612  */
ti_sci_cmd_proc_set_config(const struct ti_sci_handle * handle,u8 proc_id,u64 bootvector,u32 config_flags_set,u32 config_flags_clear)2613 static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
2614 				      u8 proc_id, u64 bootvector,
2615 				      u32 config_flags_set,
2616 				      u32 config_flags_clear)
2617 {
2618 	struct ti_sci_msg_req_set_config *req;
2619 	struct ti_sci_msg_hdr *resp;
2620 	struct ti_sci_info *info;
2621 	struct ti_sci_xfer *xfer;
2622 	struct device *dev;
2623 	int ret = 0;
2624 
2625 	if (!handle)
2626 		return -EINVAL;
2627 	if (IS_ERR(handle))
2628 		return PTR_ERR(handle);
2629 
2630 	info = handle_to_ti_sci_info(handle);
2631 	dev = info->dev;
2632 
2633 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
2634 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2635 				   sizeof(*req), sizeof(*resp));
2636 	if (IS_ERR(xfer)) {
2637 		ret = PTR_ERR(xfer);
2638 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2639 		return ret;
2640 	}
2641 	req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
2642 	req->processor_id = proc_id;
2643 	req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
2644 	req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
2645 				TI_SCI_ADDR_HIGH_SHIFT;
2646 	req->config_flags_set = config_flags_set;
2647 	req->config_flags_clear = config_flags_clear;
2648 
2649 	ret = ti_sci_do_xfer(info, xfer);
2650 	if (ret) {
2651 		dev_err(dev, "Mbox send fail %d\n", ret);
2652 		goto fail;
2653 	}
2654 
2655 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2656 
2657 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2658 
2659 fail:
2660 	ti_sci_put_one_xfer(&info->minfo, xfer);
2661 
2662 	return ret;
2663 }
2664 
2665 /**
2666  * ti_sci_cmd_proc_set_control() - Command to set the processor boot
2667  *				     control flags
2668  * @handle:			Pointer to TI SCI handle
2669  * @proc_id:			Processor ID this request is for
2670  * @control_flags_set:		Control flags to be set
2671  * @control_flags_clear:	Control flags to be cleared
2672  *
2673  * Return: 0 if all went well, else returns appropriate error value.
2674  */
ti_sci_cmd_proc_set_control(const struct ti_sci_handle * handle,u8 proc_id,u32 control_flags_set,u32 control_flags_clear)2675 static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
2676 				       u8 proc_id, u32 control_flags_set,
2677 				       u32 control_flags_clear)
2678 {
2679 	struct ti_sci_msg_req_set_ctrl *req;
2680 	struct ti_sci_msg_hdr *resp;
2681 	struct ti_sci_info *info;
2682 	struct ti_sci_xfer *xfer;
2683 	struct device *dev;
2684 	int ret = 0;
2685 
2686 	if (!handle)
2687 		return -EINVAL;
2688 	if (IS_ERR(handle))
2689 		return PTR_ERR(handle);
2690 
2691 	info = handle_to_ti_sci_info(handle);
2692 	dev = info->dev;
2693 
2694 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
2695 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2696 				   sizeof(*req), sizeof(*resp));
2697 	if (IS_ERR(xfer)) {
2698 		ret = PTR_ERR(xfer);
2699 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2700 		return ret;
2701 	}
2702 	req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
2703 	req->processor_id = proc_id;
2704 	req->control_flags_set = control_flags_set;
2705 	req->control_flags_clear = control_flags_clear;
2706 
2707 	ret = ti_sci_do_xfer(info, xfer);
2708 	if (ret) {
2709 		dev_err(dev, "Mbox send fail %d\n", ret);
2710 		goto fail;
2711 	}
2712 
2713 	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2714 
2715 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2716 
2717 fail:
2718 	ti_sci_put_one_xfer(&info->minfo, xfer);
2719 
2720 	return ret;
2721 }
2722 
2723 /**
2724  * ti_sci_cmd_get_boot_status() - Command to get the processor boot status
2725  * @handle:	Pointer to TI SCI handle
2726  * @proc_id:	Processor ID this request is for
2727  *
2728  * Return: 0 if all went well, else returns appropriate error value.
2729  */
ti_sci_cmd_proc_get_status(const struct ti_sci_handle * handle,u8 proc_id,u64 * bv,u32 * cfg_flags,u32 * ctrl_flags,u32 * sts_flags)2730 static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
2731 				      u8 proc_id, u64 *bv, u32 *cfg_flags,
2732 				      u32 *ctrl_flags, u32 *sts_flags)
2733 {
2734 	struct ti_sci_msg_resp_get_status *resp;
2735 	struct ti_sci_msg_req_get_status *req;
2736 	struct ti_sci_info *info;
2737 	struct ti_sci_xfer *xfer;
2738 	struct device *dev;
2739 	int ret = 0;
2740 
2741 	if (!handle)
2742 		return -EINVAL;
2743 	if (IS_ERR(handle))
2744 		return PTR_ERR(handle);
2745 
2746 	info = handle_to_ti_sci_info(handle);
2747 	dev = info->dev;
2748 
2749 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
2750 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2751 				   sizeof(*req), sizeof(*resp));
2752 	if (IS_ERR(xfer)) {
2753 		ret = PTR_ERR(xfer);
2754 		dev_err(dev, "Message alloc failed(%d)\n", ret);
2755 		return ret;
2756 	}
2757 	req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
2758 	req->processor_id = proc_id;
2759 
2760 	ret = ti_sci_do_xfer(info, xfer);
2761 	if (ret) {
2762 		dev_err(dev, "Mbox send fail %d\n", ret);
2763 		goto fail;
2764 	}
2765 
2766 	resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
2767 
2768 	if (!ti_sci_is_response_ack(resp)) {
2769 		ret = -ENODEV;
2770 	} else {
2771 		*bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
2772 		      (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
2773 		       TI_SCI_ADDR_HIGH_MASK);
2774 		*cfg_flags = resp->config_flags;
2775 		*ctrl_flags = resp->control_flags;
2776 		*sts_flags = resp->status_flags;
2777 	}
2778 
2779 fail:
2780 	ti_sci_put_one_xfer(&info->minfo, xfer);
2781 
2782 	return ret;
2783 }
2784 
2785 /*
2786  * ti_sci_setup_ops() - Setup the operations structures
2787  * @info:	pointer to TISCI pointer
2788  */
ti_sci_setup_ops(struct ti_sci_info * info)2789 static void ti_sci_setup_ops(struct ti_sci_info *info)
2790 {
2791 	struct ti_sci_ops *ops = &info->handle.ops;
2792 	struct ti_sci_core_ops *core_ops = &ops->core_ops;
2793 	struct ti_sci_dev_ops *dops = &ops->dev_ops;
2794 	struct ti_sci_clk_ops *cops = &ops->clk_ops;
2795 	struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2796 	struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
2797 	struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2798 	struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2799 	struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
2800 	struct ti_sci_proc_ops *pops = &ops->proc_ops;
2801 
2802 	core_ops->reboot_device = ti_sci_cmd_core_reboot;
2803 
2804 	dops->get_device = ti_sci_cmd_get_device;
2805 	dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
2806 	dops->idle_device = ti_sci_cmd_idle_device;
2807 	dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
2808 	dops->put_device = ti_sci_cmd_put_device;
2809 
2810 	dops->is_valid = ti_sci_cmd_dev_is_valid;
2811 	dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2812 	dops->is_idle = ti_sci_cmd_dev_is_idle;
2813 	dops->is_stop = ti_sci_cmd_dev_is_stop;
2814 	dops->is_on = ti_sci_cmd_dev_is_on;
2815 	dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2816 	dops->set_device_resets = ti_sci_cmd_set_device_resets;
2817 	dops->get_device_resets = ti_sci_cmd_get_device_resets;
2818 
2819 	cops->get_clock = ti_sci_cmd_get_clock;
2820 	cops->idle_clock = ti_sci_cmd_idle_clock;
2821 	cops->put_clock = ti_sci_cmd_put_clock;
2822 	cops->is_auto = ti_sci_cmd_clk_is_auto;
2823 	cops->is_on = ti_sci_cmd_clk_is_on;
2824 	cops->is_off = ti_sci_cmd_clk_is_off;
2825 
2826 	cops->set_parent = ti_sci_cmd_clk_set_parent;
2827 	cops->get_parent = ti_sci_cmd_clk_get_parent;
2828 	cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2829 
2830 	cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2831 	cops->set_freq = ti_sci_cmd_clk_set_freq;
2832 	cops->get_freq = ti_sci_cmd_clk_get_freq;
2833 
2834 	rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2835 	rm_core_ops->get_range_from_shost =
2836 				ti_sci_cmd_get_resource_range_from_shost;
2837 
2838 	iops->set_irq = ti_sci_cmd_set_irq;
2839 	iops->set_event_map = ti_sci_cmd_set_event_map;
2840 	iops->free_irq = ti_sci_cmd_free_irq;
2841 	iops->free_event_map = ti_sci_cmd_free_event_map;
2842 
2843 	rops->set_cfg = ti_sci_cmd_rm_ring_cfg;
2844 
2845 	psilops->pair = ti_sci_cmd_rm_psil_pair;
2846 	psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2847 
2848 	udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2849 	udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2850 	udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
2851 
2852 	pops->request = ti_sci_cmd_proc_request;
2853 	pops->release = ti_sci_cmd_proc_release;
2854 	pops->handover = ti_sci_cmd_proc_handover;
2855 	pops->set_config = ti_sci_cmd_proc_set_config;
2856 	pops->set_control = ti_sci_cmd_proc_set_control;
2857 	pops->get_status = ti_sci_cmd_proc_get_status;
2858 }
2859 
2860 /**
2861  * ti_sci_get_handle() - Get the TI SCI handle for a device
2862  * @dev:	Pointer to device for which we want SCI handle
2863  *
2864  * NOTE: The function does not track individual clients of the framework
2865  * and is expected to be maintained by caller of TI SCI protocol library.
2866  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2867  * Return: pointer to handle if successful, else:
2868  * -EPROBE_DEFER if the instance is not ready
2869  * -ENODEV if the required node handler is missing
2870  * -EINVAL if invalid conditions are encountered.
2871  */
ti_sci_get_handle(struct device * dev)2872 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2873 {
2874 	struct device_node *ti_sci_np;
2875 	struct list_head *p;
2876 	struct ti_sci_handle *handle = NULL;
2877 	struct ti_sci_info *info;
2878 
2879 	if (!dev) {
2880 		pr_err("I need a device pointer\n");
2881 		return ERR_PTR(-EINVAL);
2882 	}
2883 	ti_sci_np = of_get_parent(dev->of_node);
2884 	if (!ti_sci_np) {
2885 		dev_err(dev, "No OF information\n");
2886 		return ERR_PTR(-EINVAL);
2887 	}
2888 
2889 	mutex_lock(&ti_sci_list_mutex);
2890 	list_for_each(p, &ti_sci_list) {
2891 		info = list_entry(p, struct ti_sci_info, node);
2892 		if (ti_sci_np == info->dev->of_node) {
2893 			handle = &info->handle;
2894 			info->users++;
2895 			break;
2896 		}
2897 	}
2898 	mutex_unlock(&ti_sci_list_mutex);
2899 	of_node_put(ti_sci_np);
2900 
2901 	if (!handle)
2902 		return ERR_PTR(-EPROBE_DEFER);
2903 
2904 	return handle;
2905 }
2906 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
2907 
2908 /**
2909  * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
2910  * @handle:	Handle acquired by ti_sci_get_handle
2911  *
2912  * NOTE: The function does not track individual clients of the framework
2913  * and is expected to be maintained by caller of TI SCI protocol library.
2914  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2915  *
2916  * Return: 0 is successfully released
2917  * if an error pointer was passed, it returns the error value back,
2918  * if null was passed, it returns -EINVAL;
2919  */
ti_sci_put_handle(const struct ti_sci_handle * handle)2920 int ti_sci_put_handle(const struct ti_sci_handle *handle)
2921 {
2922 	struct ti_sci_info *info;
2923 
2924 	if (IS_ERR(handle))
2925 		return PTR_ERR(handle);
2926 	if (!handle)
2927 		return -EINVAL;
2928 
2929 	info = handle_to_ti_sci_info(handle);
2930 	mutex_lock(&ti_sci_list_mutex);
2931 	if (!WARN_ON(!info->users))
2932 		info->users--;
2933 	mutex_unlock(&ti_sci_list_mutex);
2934 
2935 	return 0;
2936 }
2937 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
2938 
devm_ti_sci_release(struct device * dev,void * res)2939 static void devm_ti_sci_release(struct device *dev, void *res)
2940 {
2941 	const struct ti_sci_handle **ptr = res;
2942 	const struct ti_sci_handle *handle = *ptr;
2943 	int ret;
2944 
2945 	ret = ti_sci_put_handle(handle);
2946 	if (ret)
2947 		dev_err(dev, "failed to put handle %d\n", ret);
2948 }
2949 
2950 /**
2951  * devm_ti_sci_get_handle() - Managed get handle
2952  * @dev:	device for which we want SCI handle for.
2953  *
2954  * NOTE: This releases the handle once the device resources are
2955  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
2956  * The function does not track individual clients of the framework
2957  * and is expected to be maintained by caller of TI SCI protocol library.
2958  *
2959  * Return: 0 if all went fine, else corresponding error.
2960  */
devm_ti_sci_get_handle(struct device * dev)2961 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
2962 {
2963 	const struct ti_sci_handle **ptr;
2964 	const struct ti_sci_handle *handle;
2965 
2966 	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
2967 	if (!ptr)
2968 		return ERR_PTR(-ENOMEM);
2969 	handle = ti_sci_get_handle(dev);
2970 
2971 	if (!IS_ERR(handle)) {
2972 		*ptr = handle;
2973 		devres_add(dev, ptr);
2974 	} else {
2975 		devres_free(ptr);
2976 	}
2977 
2978 	return handle;
2979 }
2980 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
2981 
2982 /**
2983  * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2984  * @np:		device node
2985  * @property:	property name containing phandle on TISCI node
2986  *
2987  * NOTE: The function does not track individual clients of the framework
2988  * and is expected to be maintained by caller of TI SCI protocol library.
2989  * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
2990  * Return: pointer to handle if successful, else:
2991  * -EPROBE_DEFER if the instance is not ready
2992  * -ENODEV if the required node handler is missing
2993  * -EINVAL if invalid conditions are encountered.
2994  */
ti_sci_get_by_phandle(struct device_node * np,const char * property)2995 const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
2996 						  const char *property)
2997 {
2998 	struct ti_sci_handle *handle = NULL;
2999 	struct device_node *ti_sci_np;
3000 	struct ti_sci_info *info;
3001 	struct list_head *p;
3002 
3003 	if (!np) {
3004 		pr_err("I need a device pointer\n");
3005 		return ERR_PTR(-EINVAL);
3006 	}
3007 
3008 	ti_sci_np = of_parse_phandle(np, property, 0);
3009 	if (!ti_sci_np)
3010 		return ERR_PTR(-ENODEV);
3011 
3012 	mutex_lock(&ti_sci_list_mutex);
3013 	list_for_each(p, &ti_sci_list) {
3014 		info = list_entry(p, struct ti_sci_info, node);
3015 		if (ti_sci_np == info->dev->of_node) {
3016 			handle = &info->handle;
3017 			info->users++;
3018 			break;
3019 		}
3020 	}
3021 	mutex_unlock(&ti_sci_list_mutex);
3022 	of_node_put(ti_sci_np);
3023 
3024 	if (!handle)
3025 		return ERR_PTR(-EPROBE_DEFER);
3026 
3027 	return handle;
3028 }
3029 EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3030 
3031 /**
3032  * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3033  * @dev:	Device pointer requesting TISCI handle
3034  * @property:	property name containing phandle on TISCI node
3035  *
3036  * NOTE: This releases the handle once the device resources are
3037  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3038  * The function does not track individual clients of the framework
3039  * and is expected to be maintained by caller of TI SCI protocol library.
3040  *
3041  * Return: 0 if all went fine, else corresponding error.
3042  */
devm_ti_sci_get_by_phandle(struct device * dev,const char * property)3043 const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3044 						       const char *property)
3045 {
3046 	const struct ti_sci_handle *handle;
3047 	const struct ti_sci_handle **ptr;
3048 
3049 	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3050 	if (!ptr)
3051 		return ERR_PTR(-ENOMEM);
3052 	handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3053 
3054 	if (!IS_ERR(handle)) {
3055 		*ptr = handle;
3056 		devres_add(dev, ptr);
3057 	} else {
3058 		devres_free(ptr);
3059 	}
3060 
3061 	return handle;
3062 }
3063 EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3064 
3065 /**
3066  * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3067  * @res:	Pointer to the TISCI resource
3068  *
3069  * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3070  */
ti_sci_get_free_resource(struct ti_sci_resource * res)3071 u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3072 {
3073 	unsigned long flags;
3074 	u16 set, free_bit;
3075 
3076 	raw_spin_lock_irqsave(&res->lock, flags);
3077 	for (set = 0; set < res->sets; set++) {
3078 		struct ti_sci_resource_desc *desc = &res->desc[set];
3079 		int res_count = desc->num + desc->num_sec;
3080 
3081 		free_bit = find_first_zero_bit(desc->res_map, res_count);
3082 		if (free_bit != res_count) {
3083 			set_bit(free_bit, desc->res_map);
3084 			raw_spin_unlock_irqrestore(&res->lock, flags);
3085 
3086 			if (desc->num && free_bit < desc->num)
3087 				return desc->start + free_bit;
3088 			else
3089 				return desc->start_sec + free_bit;
3090 		}
3091 	}
3092 	raw_spin_unlock_irqrestore(&res->lock, flags);
3093 
3094 	return TI_SCI_RESOURCE_NULL;
3095 }
3096 EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3097 
3098 /**
3099  * ti_sci_release_resource() - Release a resource from TISCI resource.
3100  * @res:	Pointer to the TISCI resource
3101  * @id:		Resource id to be released.
3102  */
ti_sci_release_resource(struct ti_sci_resource * res,u16 id)3103 void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3104 {
3105 	unsigned long flags;
3106 	u16 set;
3107 
3108 	raw_spin_lock_irqsave(&res->lock, flags);
3109 	for (set = 0; set < res->sets; set++) {
3110 		struct ti_sci_resource_desc *desc = &res->desc[set];
3111 
3112 		if (desc->num && desc->start <= id &&
3113 		    (desc->start + desc->num) > id)
3114 			clear_bit(id - desc->start, desc->res_map);
3115 		else if (desc->num_sec && desc->start_sec <= id &&
3116 			 (desc->start_sec + desc->num_sec) > id)
3117 			clear_bit(id - desc->start_sec, desc->res_map);
3118 	}
3119 	raw_spin_unlock_irqrestore(&res->lock, flags);
3120 }
3121 EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3122 
3123 /**
3124  * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3125  * @res:	Pointer to the TISCI resource
3126  *
3127  * Return: Total number of available resources.
3128  */
ti_sci_get_num_resources(struct ti_sci_resource * res)3129 u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3130 {
3131 	u32 set, count = 0;
3132 
3133 	for (set = 0; set < res->sets; set++)
3134 		count += res->desc[set].num + res->desc[set].num_sec;
3135 
3136 	return count;
3137 }
3138 EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3139 
3140 /**
3141  * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
3142  * @handle:	TISCI handle
3143  * @dev:	Device pointer to which the resource is assigned
3144  * @dev_id:	TISCI device id to which the resource is assigned
3145  * @sub_types:	Array of sub_types assigned corresponding to device
3146  * @sets:	Number of sub_types
3147  *
3148  * Return: Pointer to ti_sci_resource if all went well else appropriate
3149  *	   error pointer.
3150  */
3151 static struct ti_sci_resource *
devm_ti_sci_get_resource_sets(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,u32 * sub_types,u32 sets)3152 devm_ti_sci_get_resource_sets(const struct ti_sci_handle *handle,
3153 			      struct device *dev, u32 dev_id, u32 *sub_types,
3154 			      u32 sets)
3155 {
3156 	struct ti_sci_resource *res;
3157 	bool valid_set = false;
3158 	int i, ret, res_count;
3159 
3160 	res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3161 	if (!res)
3162 		return ERR_PTR(-ENOMEM);
3163 
3164 	res->sets = sets;
3165 	res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3166 				 GFP_KERNEL);
3167 	if (!res->desc)
3168 		return ERR_PTR(-ENOMEM);
3169 
3170 	for (i = 0; i < res->sets; i++) {
3171 		ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3172 							sub_types[i],
3173 							&res->desc[i]);
3174 		if (ret) {
3175 			dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
3176 				dev_id, sub_types[i]);
3177 			memset(&res->desc[i], 0, sizeof(res->desc[i]));
3178 			continue;
3179 		}
3180 
3181 		dev_dbg(dev, "dev/sub_type: %d/%d, start/num: %d/%d | %d/%d\n",
3182 			dev_id, sub_types[i], res->desc[i].start,
3183 			res->desc[i].num, res->desc[i].start_sec,
3184 			res->desc[i].num_sec);
3185 
3186 		valid_set = true;
3187 		res_count = res->desc[i].num + res->desc[i].num_sec;
3188 		res->desc[i].res_map =
3189 			devm_kzalloc(dev, BITS_TO_LONGS(res_count) *
3190 				     sizeof(*res->desc[i].res_map), 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  */
3212 struct ti_sci_resource *
devm_ti_sci_get_of_resource(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,char * of_prop)3213 devm_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 }
3238 EXPORT_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  * @suub_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  */
3250 struct ti_sci_resource *
devm_ti_sci_get_resource(const struct ti_sci_handle * handle,struct device * dev,u32 dev_id,u32 sub_type)3251 devm_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 }
3256 EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
3257 
tisci_reboot_handler(struct notifier_block * nb,unsigned long mode,void * cmd)3258 static 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 */
3271 static 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 */
3281 static 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 
3290 static 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 };
3295 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3296 
ti_sci_probe(struct platform_device * pdev)3297 static 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_kcalloc(dev,
3359 					       BITS_TO_LONGS(desc->max_msgs),
3360 					       sizeof(unsigned long),
3361 					       GFP_KERNEL);
3362 	if (!minfo->xfer_alloc_table)
3363 		return -ENOMEM;
3364 	bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
3365 
3366 	/* Pre-initialize the buffer pointer to pre-allocated buffers */
3367 	for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3368 		xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3369 					      GFP_KERNEL);
3370 		if (!xfer->xfer_buf)
3371 			return -ENOMEM;
3372 
3373 		xfer->tx_message.buf = xfer->xfer_buf;
3374 		init_completion(&xfer->done);
3375 	}
3376 
3377 	ret = ti_sci_debugfs_create(pdev, info);
3378 	if (ret)
3379 		dev_warn(dev, "Failed to create debug file\n");
3380 
3381 	platform_set_drvdata(pdev, info);
3382 
3383 	cl = &info->cl;
3384 	cl->dev = dev;
3385 	cl->tx_block = false;
3386 	cl->rx_callback = ti_sci_rx_callback;
3387 	cl->knows_txdone = true;
3388 
3389 	spin_lock_init(&minfo->xfer_lock);
3390 	sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3391 
3392 	info->chan_rx = mbox_request_channel_byname(cl, "rx");
3393 	if (IS_ERR(info->chan_rx)) {
3394 		ret = PTR_ERR(info->chan_rx);
3395 		goto out;
3396 	}
3397 
3398 	info->chan_tx = mbox_request_channel_byname(cl, "tx");
3399 	if (IS_ERR(info->chan_tx)) {
3400 		ret = PTR_ERR(info->chan_tx);
3401 		goto out;
3402 	}
3403 	ret = ti_sci_cmd_get_revision(info);
3404 	if (ret) {
3405 		dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3406 		goto out;
3407 	}
3408 
3409 	ti_sci_setup_ops(info);
3410 
3411 	if (reboot) {
3412 		info->nb.notifier_call = tisci_reboot_handler;
3413 		info->nb.priority = 128;
3414 
3415 		ret = register_restart_handler(&info->nb);
3416 		if (ret) {
3417 			dev_err(dev, "reboot registration fail(%d)\n", ret);
3418 			goto out;
3419 		}
3420 	}
3421 
3422 	dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3423 		 info->handle.version.abi_major, info->handle.version.abi_minor,
3424 		 info->handle.version.firmware_revision,
3425 		 info->handle.version.firmware_description);
3426 
3427 	mutex_lock(&ti_sci_list_mutex);
3428 	list_add_tail(&info->node, &ti_sci_list);
3429 	mutex_unlock(&ti_sci_list_mutex);
3430 
3431 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
3432 out:
3433 	if (!IS_ERR(info->chan_tx))
3434 		mbox_free_channel(info->chan_tx);
3435 	if (!IS_ERR(info->chan_rx))
3436 		mbox_free_channel(info->chan_rx);
3437 	debugfs_remove(info->d);
3438 	return ret;
3439 }
3440 
3441 static struct platform_driver ti_sci_driver = {
3442 	.probe = ti_sci_probe,
3443 	.driver = {
3444 		   .name = "ti-sci",
3445 		   .of_match_table = of_match_ptr(ti_sci_of_match),
3446 		   .suppress_bind_attrs = true,
3447 	},
3448 };
3449 module_platform_driver(ti_sci_driver);
3450 
3451 MODULE_LICENSE("GPL v2");
3452 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3453 MODULE_AUTHOR("Nishanth Menon");
3454 MODULE_ALIAS("platform:ti-sci");
3455