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