Blame view

drivers/firmware/ti_sci.c 99.9 KB
1e0a60143   Lokesh Vutla   firmware: ti_sci:...
1
  // SPDX-License-Identifier: GPL-2.0
aa276781a   Nishanth Menon   firmware: Add bas...
2
3
4
  /*
   * Texas Instruments System Control Interface Protocol Driver
   *
a6df49f42   Alexander A. Klimov   firmware: ti_sci:...
5
   * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
aa276781a   Nishanth Menon   firmware: Add bas...
6
   *	Nishanth Menon
aa276781a   Nishanth Menon   firmware: Add bas...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   */
  
  #define pr_fmt(fmt) "%s: " fmt, __func__
  
  #include <linux/bitmap.h>
  #include <linux/debugfs.h>
  #include <linux/export.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/mailbox_client.h>
  #include <linux/module.h>
  #include <linux/of_device.h>
  #include <linux/semaphore.h>
  #include <linux/slab.h>
  #include <linux/soc/ti/ti-msgmgr.h>
  #include <linux/soc/ti/ti_sci_protocol.h>
912cffb4e   Nishanth Menon   firmware: ti_sci:...
23
  #include <linux/reboot.h>
aa276781a   Nishanth Menon   firmware: Add bas...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  #include "ti_sci.h"
  
  /* List of all TI SCI devices active in system */
  static LIST_HEAD(ti_sci_list);
  /* Protection for the entire list */
  static DEFINE_MUTEX(ti_sci_list_mutex);
  
  /**
   * struct ti_sci_xfer - Structure representing a message flow
   * @tx_message:	Transmit message
   * @rx_len:	Receive message length
   * @xfer_buf:	Preallocated buffer to store receive message
   *		Since we work with request-ACK protocol, we can
   *		reuse the same buffer for the rx path as we
   *		use for the tx path.
   * @done:	completion event
   */
  struct ti_sci_xfer {
  	struct ti_msgmgr_message tx_message;
  	u8 rx_len;
  	u8 *xfer_buf;
  	struct completion done;
  };
  
  /**
   * struct ti_sci_xfers_info - Structure to manage transfer information
   * @sem_xfer_count:	Counting Semaphore for managing max simultaneous
   *			Messages.
   * @xfer_block:		Preallocated Message array
   * @xfer_alloc_table:	Bitmap table for allocated messages.
   *			Index of this bitmap table is also used for message
   *			sequence identifier.
   * @xfer_lock:		Protection for message allocation
   */
  struct ti_sci_xfers_info {
  	struct semaphore sem_xfer_count;
  	struct ti_sci_xfer *xfer_block;
  	unsigned long *xfer_alloc_table;
  	/* protect transfer allocation */
  	spinlock_t xfer_lock;
  };
  
  /**
   * struct ti_sci_desc - Description of SoC integration
e69a35531   Nishanth Menon   firmware: ti_sci:...
69
   * @default_host_id:	Host identifier representing the compute entity
aa276781a   Nishanth Menon   firmware: Add bas...
70
71
72
73
74
75
   * @max_rx_timeout_ms:	Timeout for communication with SoC (in Milliseconds)
   * @max_msgs: Maximum number of messages that can be pending
   *		  simultaneously in the system
   * @max_msg_size: Maximum size of data per message that can be handled.
   */
  struct ti_sci_desc {
e69a35531   Nishanth Menon   firmware: ti_sci:...
76
  	u8 default_host_id;
aa276781a   Nishanth Menon   firmware: Add bas...
77
78
79
80
81
82
83
84
85
  	int max_rx_timeout_ms;
  	int max_msgs;
  	int max_msg_size;
  };
  
  /**
   * struct ti_sci_info - Structure representing a TI SCI instance
   * @dev:	Device pointer
   * @desc:	SoC description for this instance
912cffb4e   Nishanth Menon   firmware: ti_sci:...
86
   * @nb:	Reboot Notifier block
aa276781a   Nishanth Menon   firmware: Add bas...
87
88
89
90
91
92
93
94
95
96
   * @d:		Debugfs file entry
   * @debug_region: Memory region where the debug message are available
   * @debug_region_size: Debug region size
   * @debug_buffer: Buffer allocated to copy debug messages.
   * @handle:	Instance of TI SCI handle to send to clients.
   * @cl:		Mailbox Client
   * @chan_tx:	Transmit mailbox channel
   * @chan_rx:	Receive mailbox channel
   * @minfo:	Message info
   * @node:	list head
e69a35531   Nishanth Menon   firmware: ti_sci:...
97
   * @host_id:	Host ID
aa276781a   Nishanth Menon   firmware: Add bas...
98
99
100
101
   * @users:	Number of users of this instance
   */
  struct ti_sci_info {
  	struct device *dev;
912cffb4e   Nishanth Menon   firmware: ti_sci:...
102
  	struct notifier_block nb;
aa276781a   Nishanth Menon   firmware: Add bas...
103
104
105
106
107
108
109
110
111
112
113
  	const struct ti_sci_desc *desc;
  	struct dentry *d;
  	void __iomem *debug_region;
  	char *debug_buffer;
  	size_t debug_region_size;
  	struct ti_sci_handle handle;
  	struct mbox_client cl;
  	struct mbox_chan *chan_tx;
  	struct mbox_chan *chan_rx;
  	struct ti_sci_xfers_info minfo;
  	struct list_head node;
e69a35531   Nishanth Menon   firmware: ti_sci:...
114
  	u8 host_id;
aa276781a   Nishanth Menon   firmware: Add bas...
115
116
  	/* protected by ti_sci_list_mutex */
  	int users;
912cffb4e   Nishanth Menon   firmware: ti_sci:...
117

aa276781a   Nishanth Menon   firmware: Add bas...
118
119
120
121
  };
  
  #define cl_to_ti_sci_info(c)	container_of(c, struct ti_sci_info, cl)
  #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
912cffb4e   Nishanth Menon   firmware: ti_sci:...
122
  #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
aa276781a   Nishanth Menon   firmware: Add bas...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  
  #ifdef CONFIG_DEBUG_FS
  
  /**
   * ti_sci_debug_show() - Helper to dump the debug log
   * @s:	sequence file pointer
   * @unused:	unused.
   *
   * Return: 0
   */
  static int ti_sci_debug_show(struct seq_file *s, void *unused)
  {
  	struct ti_sci_info *info = s->private;
  
  	memcpy_fromio(info->debug_buffer, info->debug_region,
  		      info->debug_region_size);
  	/*
  	 * We don't trust firmware to leave NULL terminated last byte (hence
  	 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
  	 * specific data format for debug messages, We just present the data
  	 * in the buffer as is - we expect the messages to be self explanatory.
  	 */
  	seq_puts(s, info->debug_buffer);
  	return 0;
  }
5953c887f   Yangtao Li   firmware: ti_sci:...
148
149
  /* Provide the log file operations interface*/
  DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
aa276781a   Nishanth Menon   firmware: Add bas...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  
  /**
   * ti_sci_debugfs_create() - Create log debug file
   * @pdev:	platform device pointer
   * @info:	Pointer to SCI entity information
   *
   * Return: 0 if all went fine, else corresponding error.
   */
  static int ti_sci_debugfs_create(struct platform_device *pdev,
  				 struct ti_sci_info *info)
  {
  	struct device *dev = &pdev->dev;
  	struct resource *res;
  	char debug_name[50] = "ti_sci_debug@";
  
  	/* Debug region is optional */
  	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  					   "debug_messages");
  	info->debug_region = devm_ioremap_resource(dev, res);
  	if (IS_ERR(info->debug_region))
  		return 0;
  	info->debug_region_size = resource_size(res);
  
  	info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
  					  sizeof(char), GFP_KERNEL);
  	if (!info->debug_buffer)
  		return -ENOMEM;
  	/* Setup NULL termination */
  	info->debug_buffer[info->debug_region_size] = 0;
  
  	info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
76cefef8e   Arnd Bergmann   firmware: ti_sci:...
181
182
  					      sizeof(debug_name) -
  					      sizeof("ti_sci_debug@")),
aa276781a   Nishanth Menon   firmware: Add bas...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  				      0444, NULL, info, &ti_sci_debug_fops);
  	if (IS_ERR(info->d))
  		return PTR_ERR(info->d);
  
  	dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr
  ",
  		info->debug_region, info->debug_region_size, res);
  	return 0;
  }
  
  /**
   * ti_sci_debugfs_destroy() - clean up log debug file
   * @pdev:	platform device pointer
   * @info:	Pointer to SCI entity information
   */
  static void ti_sci_debugfs_destroy(struct platform_device *pdev,
  				   struct ti_sci_info *info)
  {
  	if (IS_ERR(info->debug_region))
  		return;
  
  	debugfs_remove(info->d);
  }
  #else /* CONFIG_DEBUG_FS */
  static inline int ti_sci_debugfs_create(struct platform_device *dev,
  					struct ti_sci_info *info)
  {
  	return 0;
  }
  
  static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
  					  struct ti_sci_info *info)
  {
  }
  #endif /* CONFIG_DEBUG_FS */
  
  /**
   * ti_sci_dump_header_dbg() - Helper to dump a message header.
   * @dev:	Device pointer corresponding to the SCI entity
   * @hdr:	pointer to header.
   */
  static inline void ti_sci_dump_header_dbg(struct device *dev,
  					  struct ti_sci_msg_hdr *hdr)
  {
  	dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x
  ",
  		hdr->type, hdr->host, hdr->seq, hdr->flags);
  }
  
  /**
   * ti_sci_rx_callback() - mailbox client callback for receive messages
   * @cl:	client pointer
   * @m:	mailbox message
   *
   * Processes one received message to appropriate transfer information and
   * signals completion of the transfer.
   *
   * NOTE: This function will be invoked in IRQ context, hence should be
   * as optimal as possible.
   */
  static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
  {
  	struct ti_sci_info *info = cl_to_ti_sci_info(cl);
  	struct device *dev = info->dev;
  	struct ti_sci_xfers_info *minfo = &info->minfo;
  	struct ti_msgmgr_message *mbox_msg = m;
  	struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
  	struct ti_sci_xfer *xfer;
  	u8 xfer_id;
  
  	xfer_id = hdr->seq;
  
  	/*
  	 * Are we even expecting this?
  	 * NOTE: barriers were implicit in locks used for modifying the bitmap
  	 */
  	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
  		dev_err(dev, "Message for %d is not expected!
  ", xfer_id);
  		return;
  	}
  
  	xfer = &minfo->xfer_block[xfer_id];
  
  	/* Is the message of valid length? */
  	if (mbox_msg->len > info->desc->max_msg_size) {
bd0fa74e7   Nishanth Menon   firmware: ti_sci:...
269
270
  		dev_err(dev, "Unable to handle %zu xfer(max %d)
  ",
aa276781a   Nishanth Menon   firmware: Add bas...
271
272
273
274
275
  			mbox_msg->len, info->desc->max_msg_size);
  		ti_sci_dump_header_dbg(dev, hdr);
  		return;
  	}
  	if (mbox_msg->len < xfer->rx_len) {
bd0fa74e7   Nishanth Menon   firmware: ti_sci:...
276
277
  		dev_err(dev, "Recv xfer %zu < expected %d length
  ",
aa276781a   Nishanth Menon   firmware: Add bas...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
  			mbox_msg->len, xfer->rx_len);
  		ti_sci_dump_header_dbg(dev, hdr);
  		return;
  	}
  
  	ti_sci_dump_header_dbg(dev, hdr);
  	/* Take a copy to the rx buffer.. */
  	memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
  	complete(&xfer->done);
  }
  
  /**
   * ti_sci_get_one_xfer() - Allocate one message
   * @info:	Pointer to SCI entity information
   * @msg_type:	Message type
   * @msg_flags:	Flag to set for the message
   * @tx_message_size: transmit message size
   * @rx_message_size: receive message size
   *
   * Helper function which is used by various command functions that are
   * exposed to clients of this driver for allocating a message traffic event.
   *
   * This function can sleep depending on pending requests already in the system
   * for the SCI entity. Further, this also holds a spinlock to maintain integrity
   * of internal data structures.
   *
   * Return: 0 if all went fine, else corresponding error.
   */
  static struct ti_sci_xfer *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)
  {
  	struct ti_sci_xfers_info *minfo = &info->minfo;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_msg_hdr *hdr;
  	unsigned long flags;
  	unsigned long bit_pos;
  	u8 xfer_id;
  	int ret;
  	int timeout;
  
  	/* Ensure we have sane transfer sizes */
  	if (rx_message_size > info->desc->max_msg_size ||
  	    tx_message_size > info->desc->max_msg_size ||
  	    rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
  		return ERR_PTR(-ERANGE);
  
  	/*
  	 * Ensure we have only controlled number of pending messages.
  	 * Ideally, we might just have to wait a single message, be
  	 * conservative and wait 5 times that..
  	 */
  	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
  	ret = down_timeout(&minfo->sem_xfer_count, timeout);
  	if (ret < 0)
  		return ERR_PTR(ret);
  
  	/* Keep the locked section as small as possible */
  	spin_lock_irqsave(&minfo->xfer_lock, flags);
  	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
  				      info->desc->max_msgs);
  	set_bit(bit_pos, minfo->xfer_alloc_table);
  	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  
  	/*
  	 * We already ensured in probe that we can have max messages that can
  	 * fit in  hdr.seq - NOTE: this improves access latencies
  	 * to predictable O(1) access, BUT, it opens us to risk if
  	 * remote misbehaves with corrupted message sequence responses.
  	 * If that happens, we are going to be messed up anyways..
  	 */
  	xfer_id = (u8)bit_pos;
  
  	xfer = &minfo->xfer_block[xfer_id];
  
  	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  	xfer->tx_message.len = tx_message_size;
  	xfer->rx_len = (u8)rx_message_size;
  
  	reinit_completion(&xfer->done);
  
  	hdr->seq = xfer_id;
  	hdr->type = msg_type;
e69a35531   Nishanth Menon   firmware: ti_sci:...
362
  	hdr->host = info->host_id;
aa276781a   Nishanth Menon   firmware: Add bas...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  	hdr->flags = msg_flags;
  
  	return xfer;
  }
  
  /**
   * ti_sci_put_one_xfer() - Release a message
   * @minfo:	transfer info pointer
   * @xfer:	message that was reserved by ti_sci_get_one_xfer
   *
   * This holds a spinlock to maintain integrity of internal data structures.
   */
  static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
  				struct ti_sci_xfer *xfer)
  {
  	unsigned long flags;
  	struct ti_sci_msg_hdr *hdr;
  	u8 xfer_id;
  
  	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  	xfer_id = hdr->seq;
  
  	/*
  	 * Keep the locked section as small as possible
  	 * NOTE: we might escape with smp_mb and no lock here..
  	 * but just be conservative and symmetric.
  	 */
  	spin_lock_irqsave(&minfo->xfer_lock, flags);
  	clear_bit(xfer_id, minfo->xfer_alloc_table);
  	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  
  	/* Increment the count for the next user to get through */
  	up(&minfo->sem_xfer_count);
  }
  
  /**
   * ti_sci_do_xfer() - Do one transfer
   * @info:	Pointer to SCI entity information
   * @xfer:	Transfer to initiate and wait for response
   *
   * Return: -ETIMEDOUT in case of no response, if transmit error,
   *	   return corresponding error, else if all goes well,
   *	   return 0.
   */
  static inline int ti_sci_do_xfer(struct ti_sci_info *info,
  				 struct ti_sci_xfer *xfer)
  {
  	int ret;
  	int timeout;
  	struct device *dev = info->dev;
  
  	ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
  	if (ret < 0)
  		return ret;
  
  	ret = 0;
  
  	/* And we wait for the response. */
  	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
  	if (!wait_for_completion_timeout(&xfer->done, timeout)) {
595f3a9de   Helge Deller   ti_sci: Use %pS p...
423
424
  		dev_err(dev, "Mbox timedout in resp(caller: %pS)
  ",
aa276781a   Nishanth Menon   firmware: Add bas...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  			(void *)_RET_IP_);
  		ret = -ETIMEDOUT;
  	}
  	/*
  	 * NOTE: we might prefer not to need the mailbox ticker to manage the
  	 * transfer queueing since the protocol layer queues things by itself.
  	 * Unfortunately, we have to kick the mailbox framework after we have
  	 * received our message.
  	 */
  	mbox_client_txdone(info->chan_tx, ret);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
   * @info:	Pointer to SCI entity information
   *
   * Updates the SCI information in the internal data structure.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
  {
  	struct device *dev = info->dev;
  	struct ti_sci_handle *handle = &info->handle;
  	struct ti_sci_version_info *ver = &handle->version;
  	struct ti_sci_msg_resp_version *rev_info;
  	struct ti_sci_xfer *xfer;
  	int ret;
aa276781a   Nishanth Menon   firmware: Add bas...
455
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
66f030eac   Andrew F. Davis   firmware: ti_sci:...
456
457
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(struct ti_sci_msg_hdr),
aa276781a   Nishanth Menon   firmware: Add bas...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  				   sizeof(*rev_info));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  
  	rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	ver->abi_major = rev_info->abi_major;
  	ver->abi_minor = rev_info->abi_minor;
  	ver->firmware_revision = rev_info->firmware_revision;
  	strncpy(ver->firmware_description, rev_info->firmware_description,
  		sizeof(ver->firmware_description));
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  	return ret;
  }
  
  /**
9e7d756da   Nishanth Menon   firmware: ti_sci:...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
   * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
   * @r:	pointer to response buffer
   *
   * Return: true if the response was an ACK, else returns false.
   */
  static inline bool ti_sci_is_response_ack(void *r)
  {
  	struct ti_sci_msg_hdr *hdr = r;
  
  	return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
  }
  
  /**
   * ti_sci_set_device_state() - Set device state helper
   * @handle:	pointer to TI SCI handle
   * @id:		Device identifier
   * @flags:	flags to setup for the device
   * @state:	State to move the device to
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
  				   u32 id, u32 flags, u8 state)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_set_device_state *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
  				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
  	req->id = id;
  	req->state = state;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_get_device_state() - Get device state helper
   * @handle:	Handle to the device
   * @id:		Device Identifier
   * @clcnt:	Pointer to Context Loss Count
   * @resets:	pointer to resets
   * @p_state:	pointer to p_state
   * @c_state:	pointer to c_state
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
  				   u32 id,  u32 *clcnt,  u32 *resets,
  				    u8 *p_state,  u8 *c_state)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_get_device_state *req;
  	struct ti_sci_msg_resp_get_device_state *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	if (!clcnt && !resets && !p_state && !c_state)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
588
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
66f030eac   Andrew F. Davis   firmware: ti_sci:...
589
590
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
9e7d756da   Nishanth Menon   firmware: ti_sci:...
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
  	req->id = id;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
  	if (!ti_sci_is_response_ack(resp)) {
  		ret = -ENODEV;
  		goto fail;
  	}
  
  	if (clcnt)
  		*clcnt = resp->context_loss_count;
  	if (resets)
  		*resets = resp->resets;
  	if (p_state)
  		*p_state = resp->programmed_state;
  	if (c_state)
  		*c_state = resp->current_state;
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_device() - command to request for device managed by TISCI
45b659ee7   Lokesh Vutla   firmware: ti_sci:...
629
   *			     that can be shared with other hosts.
9e7d756da   Nishanth Menon   firmware: ti_sci:...
630
631
632
633
634
635
636
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Request for the device - NOTE: the client MUST maintain integrity of
   * usage count by balancing get_device with put_device. No refcounting is
   * managed by driver for that purpose.
   *
9e7d756da   Nishanth Menon   firmware: ti_sci:...
637
638
639
640
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
  {
45b659ee7   Lokesh Vutla   firmware: ti_sci:...
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  	return ti_sci_set_device_state(handle, id, 0,
  				       MSG_DEVICE_SW_STATE_ON);
  }
  
  /**
   * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
   *				       TISCI that is exclusively owned by the
   *				       requesting host.
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Request for the device - NOTE: the client MUST maintain integrity of
   * usage count by balancing get_device with put_device. No refcounting is
   * managed by driver for that purpose.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
  					   u32 id)
  {
9e7d756da   Nishanth Menon   firmware: ti_sci:...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  	return ti_sci_set_device_state(handle, id,
  				       MSG_FLAG_DEVICE_EXCLUSIVE,
  				       MSG_DEVICE_SW_STATE_ON);
  }
  
  /**
   * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Request for the device - NOTE: the client MUST maintain integrity of
   * usage count by balancing get_device with put_device. No refcounting is
   * managed by driver for that purpose.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
  {
45b659ee7   Lokesh Vutla   firmware: ti_sci:...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  	return ti_sci_set_device_state(handle, id, 0,
  				       MSG_DEVICE_SW_STATE_RETENTION);
  }
  
  /**
   * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
   *					TISCI that is exclusively owned by
   *					requesting host.
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Request for the device - NOTE: the client MUST maintain integrity of
   * usage count by balancing get_device with put_device. No refcounting is
   * managed by driver for that purpose.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
  					    u32 id)
  {
9e7d756da   Nishanth Menon   firmware: ti_sci:...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
  	return ti_sci_set_device_state(handle, id,
  				       MSG_FLAG_DEVICE_EXCLUSIVE,
  				       MSG_DEVICE_SW_STATE_RETENTION);
  }
  
  /**
   * ti_sci_cmd_put_device() - command to release a device managed by TISCI
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Request for the device - NOTE: the client MUST maintain integrity of
   * usage count by balancing get_device with put_device. No refcounting is
   * managed by driver for that purpose.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
  {
  	return ti_sci_set_device_state(handle, id,
  				       0, MSG_DEVICE_SW_STATE_AUTO_OFF);
  }
  
  /**
   * ti_sci_cmd_dev_is_valid() - Is the device valid
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   *
   * Return: 0 if all went fine and the device ID is valid, else return
   * appropriate error.
   */
  static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
  {
  	u8 unused;
  
  	/* check the device state which will also tell us if the ID is valid */
  	return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
  }
  
  /**
   * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
   * @handle:	Pointer to TISCI handle
   * @id:		Device Identifier
   * @count:	Pointer to Context Loss counter to populate
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
  				    u32 *count)
  {
  	return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
  }
  
  /**
   * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
   * @handle:	Pointer to TISCI handle
   * @id:		Device Identifier
   * @r_state:	true if requested to be idle
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
  				  bool *r_state)
  {
  	int ret;
  	u8 state;
  
  	if (!r_state)
  		return -EINVAL;
  
  	ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
  	if (ret)
  		return ret;
  
  	*r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
  
  	return 0;
  }
  
  /**
   * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
   * @handle:	Pointer to TISCI handle
   * @id:		Device Identifier
   * @r_state:	true if requested to be stopped
   * @curr_state:	true if currently stopped.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
  				  bool *r_state,  bool *curr_state)
  {
  	int ret;
  	u8 p_state, c_state;
  
  	if (!r_state && !curr_state)
  		return -EINVAL;
  
  	ret =
  	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  	if (ret)
  		return ret;
  
  	if (r_state)
  		*r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
  	if (curr_state)
  		*curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
  
  	return 0;
  }
  
  /**
   * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
   * @handle:	Pointer to TISCI handle
   * @id:		Device Identifier
   * @r_state:	true if requested to be ON
   * @curr_state:	true if currently ON and active
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
  				bool *r_state,  bool *curr_state)
  {
  	int ret;
  	u8 p_state, c_state;
  
  	if (!r_state && !curr_state)
  		return -EINVAL;
  
  	ret =
  	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  	if (ret)
  		return ret;
  
  	if (r_state)
  		*r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
  	if (curr_state)
  		*curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
  
  	return 0;
  }
  
  /**
   * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
   * @handle:	Pointer to TISCI handle
   * @id:		Device Identifier
   * @curr_state:	true if currently transitioning.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
  				   bool *curr_state)
  {
  	int ret;
  	u8 state;
  
  	if (!curr_state)
  		return -EINVAL;
  
  	ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
  	if (ret)
  		return ret;
  
  	*curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
  
  	return 0;
  }
  
  /**
   * ti_sci_cmd_set_device_resets() - command to set resets for device managed
   *				    by TISCI
   * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
   * @id:		Device Identifier
   * @reset_state: Device specific reset bit field
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
  					u32 id, u32 reset_state)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_set_device_resets *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
  	req->id = id;
  	req->resets = reset_state;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_device_resets() - Get reset state for device managed
   *				    by TISCI
   * @handle:		Pointer to TISCI handle
   * @id:			Device Identifier
   * @reset_state:	Pointer to reset state to populate
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
  					u32 id, u32 *reset_state)
  {
  	return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
  				       NULL);
  }
9f7232205   Nishanth Menon   firmware: ti_sci:...
937
938
939
940
941
942
943
944
945
946
947
948
949
  /**
   * ti_sci_set_clock_state() - Set clock state helper
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @flags:	Header flags as needed
   * @state:	State to request for the clock.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
950
  				  u32 dev_id, u32 clk_id,
9f7232205   Nishanth Menon   firmware: ti_sci:...
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
  				  u32 flags, u8 state)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_set_clock_state *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
  				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
979
980
981
982
983
984
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  	req->request_state = state;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_clock_state() - Get clock state helper
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @programmed_state:	State requested for clock to move to
   * @current_state:	State that the clock is currently in
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1017
  				      u32 dev_id, u32 clk_id,
9f7232205   Nishanth Menon   firmware: ti_sci:...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  				      u8 *programmed_state, u8 *current_state)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_get_clock_state *req;
  	struct ti_sci_msg_resp_get_clock_state *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	if (!programmed_state && !current_state)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1049
1050
1051
1052
1053
1054
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
  
  	if (!ti_sci_is_response_ack(resp)) {
  		ret = -ENODEV;
  		goto fail;
  	}
  
  	if (programmed_state)
  		*programmed_state = resp->programmed_state;
  	if (current_state)
  		*current_state = resp->current_state;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
   * @can_change_freq: 'true' if frequency change is desired, else 'false'
   * @enable_input_term: 'true' if input termination is desired, else 'false'
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1095
1096
  				u32 clk_id, bool needs_ssc,
  				bool can_change_freq, bool enable_input_term)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  {
  	u32 flags = 0;
  
  	flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
  	flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
  	flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
  
  	return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
  				      MSG_CLOCK_SW_STATE_REQ);
  }
  
  /**
   * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   *
   * NOTE: This clock must have been requested by get_clock previously.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1121
  				 u32 dev_id, u32 clk_id)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1122
  {
71b610825   Tero Kristo   firmware: ti_sci:...
1123
1124
  	return ti_sci_set_clock_state(handle, dev_id, clk_id,
  				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
9f7232205   Nishanth Menon   firmware: ti_sci:...
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  				      MSG_CLOCK_SW_STATE_UNREQ);
  }
  
  /**
   * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   *
   * NOTE: This clock must have been requested by get_clock previously.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1141
  				u32 dev_id, u32 clk_id)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1142
  {
71b610825   Tero Kristo   firmware: ti_sci:...
1143
1144
  	return ti_sci_set_clock_state(handle, dev_id, clk_id,
  				      MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
9f7232205   Nishanth Menon   firmware: ti_sci:...
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
  				      MSG_CLOCK_SW_STATE_AUTO);
  }
  
  /**
   * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @req_state: state indicating if the clock is auto managed
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1160
  				  u32 dev_id, u32 clk_id, bool *req_state)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
  {
  	u8 state = 0;
  	int ret;
  
  	if (!req_state)
  		return -EINVAL;
  
  	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
  	if (ret)
  		return ret;
  
  	*req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
  	return 0;
  }
  
  /**
   * ti_sci_cmd_clk_is_on() - Is the clock ON
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @req_state: state indicating if the clock is managed by us and enabled
   * @curr_state: state indicating if the clock is ready for operation
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1189
  				u32 clk_id, bool *req_state, bool *curr_state)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
  {
  	u8 c_state = 0, r_state = 0;
  	int ret;
  
  	if (!req_state && !curr_state)
  		return -EINVAL;
  
  	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  					 &r_state, &c_state);
  	if (ret)
  		return ret;
  
  	if (req_state)
  		*req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
  	if (curr_state)
  		*curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
  	return 0;
  }
  
  /**
   * ti_sci_cmd_clk_is_off() - Is the clock OFF
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @req_state: state indicating if the clock is managed by us and disabled
   * @curr_state: state indicating if the clock is NOT ready for operation
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1222
  				 u32 clk_id, bool *req_state, bool *curr_state)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
  {
  	u8 c_state = 0, r_state = 0;
  	int ret;
  
  	if (!req_state && !curr_state)
  		return -EINVAL;
  
  	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  					 &r_state, &c_state);
  	if (ret)
  		return ret;
  
  	if (req_state)
  		*req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
  	if (curr_state)
  		*curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
  	return 0;
  }
  
  /**
   * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @parent_id:	Parent clock identifier to set
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1254
  				     u32 dev_id, u32 clk_id, u32 parent_id)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_set_clock_parent *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
  	if (parent_id < 255) {
  		req->parent_id = parent_id;
  	} else {
  		req->parent_id = 255;
  		req->parent_id_32 = parent_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_clk_get_parent() - Get current parent clock source
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @parent_id:	Current clock parent
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1324
  				     u32 dev_id, u32 clk_id, u32 *parent_id)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_get_clock_parent *req;
  	struct ti_sci_msg_resp_get_clock_parent *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle || !parent_id)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1352
1353
1354
1355
1356
1357
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1358
1359
1360
1361
1362
1363
1364
1365
1366
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1367
  	if (!ti_sci_is_response_ack(resp)) {
9f7232205   Nishanth Menon   firmware: ti_sci:...
1368
  		ret = -ENODEV;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1369
1370
1371
1372
1373
1374
  	} else {
  		if (resp->parent_id < 255)
  			*parent_id = resp->parent_id;
  		else
  			*parent_id = resp->parent_id_32;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @num_parents: Returns he number of parents to the current clock.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1394
1395
  					  u32 dev_id, u32 clk_id,
  					  u32 *num_parents)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_get_clock_num_parents *req;
  	struct ti_sci_msg_resp_get_clock_num_parents *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle || !num_parents)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1423
1424
1425
1426
1427
1428
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1429
1430
1431
1432
1433
1434
1435
1436
1437
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1438
  	if (!ti_sci_is_response_ack(resp)) {
9f7232205   Nishanth Menon   firmware: ti_sci:...
1439
  		ret = -ENODEV;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1440
1441
1442
1443
1444
1445
  	} else {
  		if (resp->num_parents < 255)
  			*num_parents = resp->num_parents;
  		else
  			*num_parents = resp->num_parents_32;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
   *		allowable programmed frequency and does not account for clock
   *		tolerances and jitter.
   * @target_freq: The target clock frequency in Hz. A frequency will be
   *		processed as close to this target frequency as possible.
   * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
   *		allowable programmed frequency and does not account for clock
   *		tolerances and jitter.
   * @match_freq:	Frequency match in Hz response.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1473
  					 u32 dev_id, u32 clk_id, u64 min_freq,
9f7232205   Nishanth Menon   firmware: ti_sci:...
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
  					 u64 target_freq, u64 max_freq,
  					 u64 *match_freq)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_query_clock_freq *req;
  	struct ti_sci_msg_resp_query_clock_freq *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle || !match_freq)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1503
1504
1505
1506
1507
1508
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
  	req->min_freq_hz = min_freq;
  	req->target_freq_hz = target_freq;
  	req->max_freq_hz = max_freq;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
  
  	if (!ti_sci_is_response_ack(resp))
  		ret = -ENODEV;
  	else
  		*match_freq = resp->freq_hz;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
   *		allowable programmed frequency and does not account for clock
   *		tolerances and jitter.
   * @target_freq: The target clock frequency in Hz. A frequency will be
   *		processed as close to this target frequency as possible.
   * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
   *		allowable programmed frequency and does not account for clock
   *		tolerances and jitter.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1552
  				   u32 dev_id, u32 clk_id, u64 min_freq,
9f7232205   Nishanth Menon   firmware: ti_sci:...
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
  				   u64 target_freq, u64 max_freq)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_set_clock_freq *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1581
1582
1583
1584
1585
1586
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
  	req->min_freq_hz = min_freq;
  	req->target_freq_hz = target_freq;
  	req->max_freq_hz = max_freq;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_clk_get_freq() - Get current frequency
   * @handle:	pointer to TI SCI handle
   * @dev_id:	Device identifier this request is for
   * @clk_id:	Clock identifier for the device for this request.
   *		Each device has it's own set of clock inputs. This indexes
   *		which clock input to modify.
   * @freq:	Currently frequency in Hz
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
81f4458c9   Tero Kristo   firmware: ti_sci:...
1620
  				   u32 dev_id, u32 clk_id, u64 *freq)
9f7232205   Nishanth Menon   firmware: ti_sci:...
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_get_clock_freq *req;
  	struct ti_sci_msg_resp_get_clock_freq *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle || !freq)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
  	req->dev_id = dev_id;
81f4458c9   Tero Kristo   firmware: ti_sci:...
1648
1649
1650
1651
1652
1653
  	if (clk_id < 255) {
  		req->clk_id = clk_id;
  	} else {
  		req->clk_id = 255;
  		req->clk_id_32 = clk_id;
  	}
9f7232205   Nishanth Menon   firmware: ti_sci:...
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
  
  	if (!ti_sci_is_response_ack(resp))
  		ret = -ENODEV;
  	else
  		*freq = resp->freq_hz;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
912cffb4e   Nishanth Menon   firmware: ti_sci:...
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
  static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
  {
  	struct ti_sci_info *info;
  	struct ti_sci_msg_req_reboot *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	if (!ti_sci_is_response_ack(resp))
  		ret = -ENODEV;
  	else
  		ret = 0;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
  /**
   * ti_sci_get_resource_range - Helper to get a range of resources assigned
   *			       to a host. Resource is uniquely identified by
   *			       type and subtype.
   * @handle:		Pointer to TISCI handle.
   * @dev_id:		TISCI device ID.
   * @subtype:		Resource assignment subtype that is being requested
   *			from the given device.
   * @s_host:		Host processor ID to which the resources are allocated
   * @range_start:	Start index of the resource range
   * @range_num:		Number of resources in the range
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
  				     u32 dev_id, u8 subtype, u8 s_host,
  				     u16 *range_start, u16 *range_num)
  {
  	struct ti_sci_msg_resp_get_resource_range *resp;
  	struct ti_sci_msg_req_get_resource_range *req;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
1763
1764
  	req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
  	req->secondary_host = s_host;
9b98e02a3   Lokesh Vutla   firmware: ti_sci:...
1765
  	req->type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
  	req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
  
  	if (!ti_sci_is_response_ack(resp)) {
  		ret = -ENODEV;
  	} else if (!resp->range_start && !resp->range_num) {
  		ret = -ENODEV;
  	} else {
  		*range_start = resp->range_start;
  		*range_num = resp->range_num;
  	};
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
   *				   that is same as ti sci interface host.
   * @handle:		Pointer to TISCI handle.
   * @dev_id:		TISCI device ID.
   * @subtype:		Resource assignment subtype that is being requested
   *			from the given device.
   * @range_start:	Start index of the resource range
   * @range_num:		Number of resources in the range
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
  					 u32 dev_id, u8 subtype,
  					 u16 *range_start, u16 *range_num)
  {
  	return ti_sci_get_resource_range(handle, dev_id, subtype,
  					 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
  					 range_start, range_num);
  }
  
  /**
   * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
   *					      assigned to a specified host.
   * @handle:		Pointer to TISCI handle.
   * @dev_id:		TISCI device ID.
   * @subtype:		Resource assignment subtype that is being requested
   *			from the given device.
   * @s_host:		Host processor ID to which the resources are allocated
   * @range_start:	Start index of the resource range
   * @range_num:		Number of resources in the range
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static
  int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
  					     u32 dev_id, u8 subtype, u8 s_host,
  					     u16 *range_start, u16 *range_num)
  {
  	return ti_sci_get_resource_range(handle, dev_id, subtype, s_host,
  					 range_start, range_num);
  }
997b001f6   Lokesh Vutla   firmware: ti_sci:...
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
  /**
   * ti_sci_manage_irq() - Helper api to configure/release the irq route between
   *			 the requested source and destination
   * @handle:		Pointer to TISCI handle.
   * @valid_params:	Bit fields defining the validity of certain params
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @dst_id:		Device ID of the IRQ destination
   * @dst_host_irq:	IRQ number of the destination device
   * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
   * @vint:		Virtual interrupt to be used within the IA
   * @global_event:	Global event number to be used for the requesting event
   * @vint_status_bit:	Virtual interrupt status bit to be used for the event
   * @s_host:		Secondary host ID to which the irq/event is being
   *			requested for.
   * @type:		Request type irq set or release.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int 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)
  {
  	struct ti_sci_msg_req_manage_irq *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
  	req->valid_params = valid_params;
  	req->src_id = src_id;
  	req->src_index = src_index;
  	req->dst_id = dst_id;
  	req->dst_host_irq = dst_host_irq;
  	req->ia_id = ia_id;
  	req->vint = vint;
  	req->global_event = global_event;
  	req->vint_status_bit = vint_status_bit;
  	req->secondary_host = s_host;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_set_irq() - Helper api to configure the irq route between the
   *		      requested source and destination
   * @handle:		Pointer to TISCI handle.
   * @valid_params:	Bit fields defining the validity of certain params
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @dst_id:		Device ID of the IRQ destination
   * @dst_host_irq:	IRQ number of the destination device
   * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
   * @vint:		Virtual interrupt to be used within the IA
   * @global_event:	Global event number to be used for the requesting event
   * @vint_status_bit:	Virtual interrupt status bit to be used for the event
   * @s_host:		Secondary host ID to which the irq/event is being
   *			requested for.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int 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)
  {
  	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
  ",
  		 __func__, valid_params, src_id, src_index,
  		 dst_id, dst_host_irq, ia_id, vint, global_event,
  		 vint_status_bit);
  
  	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
  				 dst_id, dst_host_irq, ia_id, vint,
  				 global_event, vint_status_bit, s_host,
  				 TI_SCI_MSG_SET_IRQ);
  }
  
  /**
   * ti_sci_free_irq() - Helper api to free the irq route between the
   *			   requested source and destination
   * @handle:		Pointer to TISCI handle.
   * @valid_params:	Bit fields defining the validity of certain params
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @dst_id:		Device ID of the IRQ destination
   * @dst_host_irq:	IRQ number of the destination device
   * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
   * @vint:		Virtual interrupt to be used within the IA
   * @global_event:	Global event number to be used for the requesting event
   * @vint_status_bit:	Virtual interrupt status bit to be used for the event
   * @s_host:		Secondary host ID to which the irq/event is being
   *			requested for.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int 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)
  {
  	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
  ",
  		 __func__, valid_params, src_id, src_index,
  		 dst_id, dst_host_irq, ia_id, vint, global_event,
  		 vint_status_bit);
  
  	return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
  				 dst_id, dst_host_irq, ia_id, vint,
  				 global_event, vint_status_bit, s_host,
  				 TI_SCI_MSG_FREE_IRQ);
  }
  
  /**
   * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
   *			  source and destination.
   * @handle:		Pointer to TISCI handle.
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @dst_id:		Device ID of the IRQ destination
   * @dst_host_irq:	IRQ number of the destination device
   * @vint_irq:		Boolean specifying if this interrupt belongs to
   *			Interrupt Aggregator.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
  			      u16 src_index, u16 dst_id, u16 dst_host_irq)
  {
  	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
  
  	return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
  			      dst_host_irq, 0, 0, 0, 0, 0);
  }
  
  /**
   * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
   *				requested source and Interrupt Aggregator.
   * @handle:		Pointer to TISCI handle.
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
   * @vint:		Virtual interrupt to be used within the IA
   * @global_event:	Global event number to be used for the requesting event
   * @vint_status_bit:	Virtual interrupt status bit to be used for the event
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int 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)
  {
  	u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
  			   MSG_FLAG_GLB_EVNT_VALID |
  			   MSG_FLAG_VINT_STS_BIT_VALID;
  
  	return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
  			      ia_id, vint, global_event, vint_status_bit, 0);
  }
  
  /**
   * ti_sci_cmd_free_irq() - Free a host irq route between the between the
   *			   requested source and destination.
   * @handle:		Pointer to TISCI handle.
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @dst_id:		Device ID of the IRQ destination
   * @dst_host_irq:	IRQ number of the destination device
   * @vint_irq:		Boolean specifying if this interrupt belongs to
   *			Interrupt Aggregator.
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
  			       u16 src_index, u16 dst_id, u16 dst_host_irq)
  {
  	u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
  
  	return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
  			       dst_host_irq, 0, 0, 0, 0, 0);
  }
  
  /**
   * ti_sci_cmd_free_event_map() - Free an event map between the requested source
   *				 and Interrupt Aggregator.
   * @handle:		Pointer to TISCI handle.
   * @src_id:		Device ID of the IRQ source
   * @src_index:		IRQ source index within the source device
   * @ia_id:		Device ID of the IA, if the IRQ flows through this IA
   * @vint:		Virtual interrupt to be used within the IA
   * @global_event:	Global event number to be used for the requesting event
   * @vint_status_bit:	Virtual interrupt status bit to be used for the event
   *
   * Return: 0 if all went fine, else return appropriate error.
   */
  static int 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)
  {
  	u32 valid_params = MSG_FLAG_IA_ID_VALID |
  			   MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
  			   MSG_FLAG_VINT_STS_BIT_VALID;
  
  	return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
  			       ia_id, vint, global_event, vint_status_bit, 0);
  }
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
  /**
   * ti_sci_cmd_ring_config() - configure RA ring
   * @handle:		Pointer to TI SCI handle.
   * @valid_params:	Bitfield defining validity of ring configuration
   *			parameters
   * @nav_id:		Device ID of Navigator Subsystem from which the ring is
   *			allocated
   * @index:		Ring index
   * @addr_lo:		The ring base address lo 32 bits
   * @addr_hi:		The ring base address hi 32 bits
   * @count:		Number of ring elements
   * @mode:		The mode of the ring
   * @size:		The ring element size.
   * @order_id:		Specifies the ring's bus order ID
   *
   * Return: 0 if all went well, else returns appropriate error value.
   *
   * See @ti_sci_msg_rm_ring_cfg_req for more info.
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2094
   */
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2095
2096
2097
2098
  static int ti_sci_cmd_ring_config(const struct ti_sci_handle *handle,
  				  u32 valid_params, u16 nav_id, u16 index,
  				  u32 addr_lo, u32 addr_hi, u32 count,
  				  u8 mode, u8 size, u8 order_id)
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2099
  {
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2100
2101
2102
2103
2104
2105
  	struct ti_sci_msg_rm_ring_cfg_req *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
  	int ret = 0;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2106

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2107
2108
  	if (IS_ERR_OR_NULL(handle))
  		return -EINVAL;
9f7232205   Nishanth Menon   firmware: ti_sci:...
2109

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2110
2111
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
9f7232205   Nishanth Menon   firmware: ti_sci:...
2112

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2113
2114
2115
2116
2117
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2118
2119
  		dev_err(dev, "RM_RA:Message config failed(%d)
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
  		return ret;
  	}
  	req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
  	req->valid_params = valid_params;
  	req->nav_id = nav_id;
  	req->index = index;
  	req->addr_lo = addr_lo;
  	req->addr_hi = addr_hi;
  	req->count = count;
  	req->mode = mode;
  	req->size = size;
  	req->order_id = order_id;
9f7232205   Nishanth Menon   firmware: ti_sci:...
2132

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2133
2134
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
4c960505d   YueHaibing   firmware: ti_sci:...
2135
2136
  		dev_err(dev, "RM_RA:Mbox config send fail %d
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2137
2138
  		goto fail;
  	}
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
2139

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2140
2141
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
997b001f6   Lokesh Vutla   firmware: ti_sci:...
2142

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2143
2144
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2145
2146
  	dev_dbg(dev, "RM_RA:config ring %u ret:%d
  ", index, ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2147
  	return ret;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2148
2149
2150
  }
  
  /**
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
   * ti_sci_cmd_ring_get_config() - get RA ring configuration
   * @handle:	Pointer to TI SCI handle.
   * @nav_id:	Device ID of Navigator Subsystem from which the ring is
   *		allocated
   * @index:	Ring index
   * @addr_lo:	Returns ring's base address lo 32 bits
   * @addr_hi:	Returns ring's base address hi 32 bits
   * @count:	Returns number of ring elements
   * @mode:	Returns mode of the ring
   * @size:	Returns ring element size
   * @order_id:	Returns ring's bus order ID
aa276781a   Nishanth Menon   firmware: Add bas...
2162
   *
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2163
2164
2165
   * Return: 0 if all went well, else returns appropriate error value.
   *
   * See @ti_sci_msg_rm_ring_get_cfg_req for more info.
aa276781a   Nishanth Menon   firmware: Add bas...
2166
   */
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2167
2168
2169
2170
  static int ti_sci_cmd_ring_get_config(const struct ti_sci_handle *handle,
  				      u32 nav_id, u32 index, u8 *mode,
  				      u32 *addr_lo, u32 *addr_hi,
  				      u32 *count, u8 *size, u8 *order_id)
aa276781a   Nishanth Menon   firmware: Add bas...
2171
  {
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2172
2173
2174
  	struct ti_sci_msg_rm_ring_get_cfg_resp *resp;
  	struct ti_sci_msg_rm_ring_get_cfg_req *req;
  	struct ti_sci_xfer *xfer;
aa276781a   Nishanth Menon   firmware: Add bas...
2175
  	struct ti_sci_info *info;
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2176
2177
  	struct device *dev;
  	int ret = 0;
aa276781a   Nishanth Menon   firmware: Add bas...
2178

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
  	if (IS_ERR_OR_NULL(handle))
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_GET_CFG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2190
  		dev_err(dev,
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2191
2192
2193
  			"RM_RA:Message get config failed(%d)
  ", ret);
  		return ret;
aa276781a   Nishanth Menon   firmware: Add bas...
2194
  	}
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2195
2196
2197
  	req = (struct ti_sci_msg_rm_ring_get_cfg_req *)xfer->xfer_buf;
  	req->nav_id = nav_id;
  	req->index = index;
aa276781a   Nishanth Menon   firmware: Add bas...
2198

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2199
2200
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
4c960505d   YueHaibing   firmware: ti_sci:...
2201
2202
  		dev_err(dev, "RM_RA:Mbox get config send fail %d
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2203
  		goto fail;
aa276781a   Nishanth Menon   firmware: Add bas...
2204
  	}
aa276781a   Nishanth Menon   firmware: Add bas...
2205

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2206
  	resp = (struct ti_sci_msg_rm_ring_get_cfg_resp *)xfer->xfer_buf;
aa276781a   Nishanth Menon   firmware: Add bas...
2207

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
  	if (!ti_sci_is_response_ack(resp)) {
  		ret = -ENODEV;
  	} else {
  		if (mode)
  			*mode = resp->mode;
  		if (addr_lo)
  			*addr_lo = resp->addr_lo;
  		if (addr_hi)
  			*addr_hi = resp->addr_hi;
  		if (count)
  			*count = resp->count;
  		if (size)
  			*size = resp->size;
  		if (order_id)
  			*order_id = resp->order_id;
  	};
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2227
2228
  	dev_dbg(dev, "RM_RA:get config ring %u ret:%d
  ", index, ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2229
  	return ret;
aa276781a   Nishanth Menon   firmware: Add bas...
2230
  }
aa276781a   Nishanth Menon   firmware: Add bas...
2231
2232
  
  /**
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2233
2234
2235
2236
2237
2238
   * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
   * @handle:	Pointer to TI SCI handle.
   * @nav_id:	Device ID of Navigator Subsystem which should be used for
   *		pairing
   * @src_thread:	Source PSI-L thread ID
   * @dst_thread: Destination PSI-L thread ID
aa276781a   Nishanth Menon   firmware: Add bas...
2239
   *
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2240
   * Return: 0 if all went well, else returns appropriate error value.
aa276781a   Nishanth Menon   firmware: Add bas...
2241
   */
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2242
2243
  static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
  				   u32 nav_id, u32 src_thread, u32 dst_thread)
aa276781a   Nishanth Menon   firmware: Add bas...
2244
  {
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2245
2246
2247
  	struct ti_sci_msg_psil_pair *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
aa276781a   Nishanth Menon   firmware: Add bas...
2248
  	struct ti_sci_info *info;
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2249
2250
  	struct device *dev;
  	int ret = 0;
aa276781a   Nishanth Menon   firmware: Add bas...
2251
2252
2253
2254
2255
2256
2257
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2258
  	dev = info->dev;
aa276781a   Nishanth Menon   firmware: Add bas...
2259

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
  	req->nav_id = nav_id;
  	req->src_thread = src_thread;
  	req->dst_thread = dst_thread;
aa276781a   Nishanth Menon   firmware: Add bas...
2273

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2274
2275
2276
2277
2278
2279
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "RM_PSIL:Mbox send fail %d
  ", ret);
  		goto fail;
  	}
aa276781a   Nishanth Menon   firmware: Add bas...
2280

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2281
2282
2283
2284
2285
2286
2287
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
aa276781a   Nishanth Menon   firmware: Add bas...
2288
2289
2290
  }
  
  /**
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2291
2292
2293
2294
2295
2296
   * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
   * @handle:	Pointer to TI SCI handle.
   * @nav_id:	Device ID of Navigator Subsystem which should be used for
   *		unpairing
   * @src_thread:	Source PSI-L thread ID
   * @dst_thread:	Destination PSI-L thread ID
aa276781a   Nishanth Menon   firmware: Add bas...
2297
   *
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2298
   * Return: 0 if all went well, else returns appropriate error value.
aa276781a   Nishanth Menon   firmware: Add bas...
2299
   */
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2300
2301
  static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
  				     u32 nav_id, u32 src_thread, u32 dst_thread)
aa276781a   Nishanth Menon   firmware: Add bas...
2302
  {
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2303
2304
2305
2306
2307
2308
  	struct ti_sci_msg_psil_unpair *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
  	int ret = 0;
aa276781a   Nishanth Menon   firmware: Add bas...
2309

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2310
2311
2312
2313
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
aa276781a   Nishanth Menon   firmware: Add bas...
2314

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "RM_PSIL:Message reconfig failed(%d)
  ", ret);
  		return ret;
aa276781a   Nishanth Menon   firmware: Add bas...
2326
  	}
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2327
2328
2329
2330
  	req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
  	req->nav_id = nav_id;
  	req->src_thread = src_thread;
  	req->dst_thread = dst_thread;
aa276781a   Nishanth Menon   firmware: Add bas...
2331

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "RM_PSIL:Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
aa276781a   Nishanth Menon   firmware: Add bas...
2346
  }
aa276781a   Nishanth Menon   firmware: Add bas...
2347

905c30477   Grygorii Strashko   firmware: ti_sci:...
2348
  /**
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2349
2350
2351
2352
   * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
   * @handle:	Pointer to TI SCI handle.
   * @params:	Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
   *		structure
905c30477   Grygorii Strashko   firmware: ti_sci:...
2353
   *
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2354
2355
2356
2357
   * Return: 0 if all went well, else returns appropriate error value.
   *
   * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
   * more info.
905c30477   Grygorii Strashko   firmware: ti_sci:...
2358
   */
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2359
2360
  static int 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)
905c30477   Grygorii Strashko   firmware: ti_sci:...
2361
  {
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2362
2363
2364
  	struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
905c30477   Grygorii Strashko   firmware: ti_sci:...
2365
  	struct ti_sci_info *info;
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2366
2367
  	struct device *dev;
  	int ret = 0;
905c30477   Grygorii Strashko   firmware: ti_sci:...
2368

68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
  	if (IS_ERR_OR_NULL(handle))
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2380
2381
  		dev_err(dev, "Message TX_CH_CFG alloc failed(%d)
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
  		return ret;
  	}
  	req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
  	req->valid_params = params->valid_params;
  	req->nav_id = params->nav_id;
  	req->index = params->index;
  	req->tx_pause_on_err = params->tx_pause_on_err;
  	req->tx_filt_einfo = params->tx_filt_einfo;
  	req->tx_filt_pswords = params->tx_filt_pswords;
  	req->tx_atype = params->tx_atype;
  	req->tx_chan_type = params->tx_chan_type;
  	req->tx_supr_tdpkt = params->tx_supr_tdpkt;
  	req->tx_fetch_size = params->tx_fetch_size;
  	req->tx_credit_count = params->tx_credit_count;
  	req->txcq_qnum = params->txcq_qnum;
  	req->tx_priority = params->tx_priority;
  	req->tx_qos = params->tx_qos;
  	req->tx_orderid = params->tx_orderid;
  	req->fdepth = params->fdepth;
  	req->tx_sched_priority = params->tx_sched_priority;
  	req->tx_burst_size = params->tx_burst_size;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
4c960505d   YueHaibing   firmware: ti_sci:...
2406
2407
  		dev_err(dev, "Mbox send TX_CH_CFG fail %d
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2408
2409
2410
2411
2412
2413
2414
2415
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2416
2417
  	dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u
  ", params->index, ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
  	return ret;
  }
  
  /**
   * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
   * @handle:	Pointer to TI SCI handle.
   * @params:	Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
   *		structure
   *
   * Return: 0 if all went well, else returns appropriate error value.
   *
   * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
   * more info.
   */
  static int 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)
  {
  	struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR_OR_NULL(handle))
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2453
2454
  		dev_err(dev, "Message RX_CH_CFG alloc failed(%d)
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
  		return ret;
  	}
  	req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
  	req->valid_params = params->valid_params;
  	req->nav_id = params->nav_id;
  	req->index = params->index;
  	req->rx_fetch_size = params->rx_fetch_size;
  	req->rxcq_qnum = params->rxcq_qnum;
  	req->rx_priority = params->rx_priority;
  	req->rx_qos = params->rx_qos;
  	req->rx_orderid = params->rx_orderid;
  	req->rx_sched_priority = params->rx_sched_priority;
  	req->flowid_start = params->flowid_start;
  	req->flowid_cnt = params->flowid_cnt;
  	req->rx_pause_on_err = params->rx_pause_on_err;
  	req->rx_atype = params->rx_atype;
  	req->rx_chan_type = params->rx_chan_type;
  	req->rx_ignore_short = params->rx_ignore_short;
  	req->rx_ignore_long = params->rx_ignore_long;
  	req->rx_burst_size = params->rx_burst_size;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
4c960505d   YueHaibing   firmware: ti_sci:...
2478
2479
  		dev_err(dev, "Mbox send RX_CH_CFG fail %d
  ", ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2480
2481
2482
2483
2484
2485
2486
2487
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
4c960505d   YueHaibing   firmware: ti_sci:...
2488
2489
  	dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d
  ", params->index, ret);
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
  	return ret;
  }
  
  /**
   * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
   * @handle:	Pointer to TI SCI handle.
   * @params:	Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
   *		structure
   *
   * Return: 0 if all went well, else returns appropriate error value.
   *
   * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
   * more info.
   */
  static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
  			const struct ti_sci_msg_rm_udmap_flow_cfg *params)
  {
  	struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info;
  	struct device *dev;
  	int ret = 0;
  
  	if (IS_ERR_OR_NULL(handle))
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
  	req->valid_params = params->valid_params;
  	req->nav_id = params->nav_id;
  	req->flow_index = params->flow_index;
  	req->rx_einfo_present = params->rx_einfo_present;
  	req->rx_psinfo_present = params->rx_psinfo_present;
  	req->rx_error_handling = params->rx_error_handling;
  	req->rx_desc_type = params->rx_desc_type;
  	req->rx_sop_offset = params->rx_sop_offset;
  	req->rx_dest_qnum = params->rx_dest_qnum;
  	req->rx_src_tag_hi = params->rx_src_tag_hi;
  	req->rx_src_tag_lo = params->rx_src_tag_lo;
  	req->rx_dest_tag_hi = params->rx_dest_tag_hi;
  	req->rx_dest_tag_lo = params->rx_dest_tag_lo;
  	req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
  	req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
  	req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
  	req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
  	req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
  	req->rx_fdq1_qnum = params->rx_fdq1_qnum;
  	req->rx_fdq2_qnum = params->rx_fdq2_qnum;
  	req->rx_fdq3_qnum = params->rx_fdq3_qnum;
  	req->rx_ps_location = params->rx_ps_location;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "RX_FL_CFG: Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  	ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  	dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d
  ", params->flow_index, ret);
  	return ret;
  }
1e407f337   Suman Anna   firmware: ti_sci:...
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
  /**
   * ti_sci_cmd_proc_request() - Command to request a physical processor control
   * @handle:	Pointer to TI SCI handle
   * @proc_id:	Processor ID this request is for
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
  				   u8 proc_id)
  {
  	struct ti_sci_msg_req_proc_request *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_proc_release() - Command to release a physical processor control
   * @handle:	Pointer to TI SCI handle
   * @proc_id:	Processor ID this request is for
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
  				   u8 proc_id)
  {
  	struct ti_sci_msg_req_proc_release *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_proc_handover() - Command to handover a physical processor
   *				control to a host in the processor's access
   *				control list.
   * @handle:	Pointer to TI SCI handle
   * @proc_id:	Processor ID this request is for
   * @host_id:	Host ID to get the control of the processor
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
  				    u8 proc_id, u8 host_id)
  {
  	struct ti_sci_msg_req_proc_handover *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  	req->host_id = host_id;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_proc_set_config() - Command to set the processor boot
   *				    configuration flags
   * @handle:		Pointer to TI SCI handle
   * @proc_id:		Processor ID this request is for
   * @config_flags_set:	Configuration flags to be set
   * @config_flags_clear:	Configuration flags to be cleared.
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int 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)
  {
  	struct ti_sci_msg_req_set_config *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  	req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
  	req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
  				TI_SCI_ADDR_HIGH_SHIFT;
  	req->config_flags_set = config_flags_set;
  	req->config_flags_clear = config_flags_clear;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_proc_set_control() - Command to set the processor boot
   *				     control flags
   * @handle:			Pointer to TI SCI handle
   * @proc_id:			Processor ID this request is for
   * @control_flags_set:		Control flags to be set
   * @control_flags_clear:	Control flags to be cleared
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
  				       u8 proc_id, u32 control_flags_set,
  				       u32 control_flags_clear)
  {
  	struct ti_sci_msg_req_set_ctrl *req;
  	struct ti_sci_msg_hdr *resp;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  	req->control_flags_set = control_flags_set;
  	req->control_flags_clear = control_flags_clear;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  
  	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
  
  /**
   * ti_sci_cmd_get_boot_status() - Command to get the processor boot status
   * @handle:	Pointer to TI SCI handle
   * @proc_id:	Processor ID this request is for
   *
   * Return: 0 if all went well, else returns appropriate error value.
   */
  static int 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)
  {
  	struct ti_sci_msg_resp_get_status *resp;
  	struct ti_sci_msg_req_get_status *req;
  	struct ti_sci_info *info;
  	struct ti_sci_xfer *xfer;
  	struct device *dev;
  	int ret = 0;
  
  	if (!handle)
  		return -EINVAL;
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  
  	info = handle_to_ti_sci_info(handle);
  	dev = info->dev;
  
  	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
  				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  				   sizeof(*req), sizeof(*resp));
  	if (IS_ERR(xfer)) {
  		ret = PTR_ERR(xfer);
  		dev_err(dev, "Message alloc failed(%d)
  ", ret);
  		return ret;
  	}
  	req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
  	req->processor_id = proc_id;
  
  	ret = ti_sci_do_xfer(info, xfer);
  	if (ret) {
  		dev_err(dev, "Mbox send fail %d
  ", ret);
  		goto fail;
  	}
  
  	resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
  
  	if (!ti_sci_is_response_ack(resp)) {
  		ret = -ENODEV;
  	} else {
  		*bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
  		      (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
  		       TI_SCI_ADDR_HIGH_MASK);
  		*cfg_flags = resp->config_flags;
  		*ctrl_flags = resp->control_flags;
  		*sts_flags = resp->status_flags;
  	}
  
  fail:
  	ti_sci_put_one_xfer(&info->minfo, xfer);
  
  	return ret;
  }
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2922
2923
2924
2925
2926
2927
2928
  /*
   * ti_sci_setup_ops() - Setup the operations structures
   * @info:	pointer to TISCI pointer
   */
  static void ti_sci_setup_ops(struct ti_sci_info *info)
  {
  	struct ti_sci_ops *ops = &info->handle.ops;
912cffb4e   Nishanth Menon   firmware: ti_sci:...
2929
  	struct ti_sci_core_ops *core_ops = &ops->core_ops;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2930
  	struct ti_sci_dev_ops *dops = &ops->dev_ops;
9f7232205   Nishanth Menon   firmware: ti_sci:...
2931
  	struct ti_sci_clk_ops *cops = &ops->clk_ops;
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
2932
  	struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
997b001f6   Lokesh Vutla   firmware: ti_sci:...
2933
  	struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2934
2935
2936
  	struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
  	struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
  	struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
1e407f337   Suman Anna   firmware: ti_sci:...
2937
  	struct ti_sci_proc_ops *pops = &ops->proc_ops;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2938

912cffb4e   Nishanth Menon   firmware: ti_sci:...
2939
  	core_ops->reboot_device = ti_sci_cmd_core_reboot;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2940
  	dops->get_device = ti_sci_cmd_get_device;
45b659ee7   Lokesh Vutla   firmware: ti_sci:...
2941
  	dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2942
  	dops->idle_device = ti_sci_cmd_idle_device;
45b659ee7   Lokesh Vutla   firmware: ti_sci:...
2943
  	dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
  	dops->put_device = ti_sci_cmd_put_device;
  
  	dops->is_valid = ti_sci_cmd_dev_is_valid;
  	dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
  	dops->is_idle = ti_sci_cmd_dev_is_idle;
  	dops->is_stop = ti_sci_cmd_dev_is_stop;
  	dops->is_on = ti_sci_cmd_dev_is_on;
  	dops->is_transitioning = ti_sci_cmd_dev_is_trans;
  	dops->set_device_resets = ti_sci_cmd_set_device_resets;
  	dops->get_device_resets = ti_sci_cmd_get_device_resets;
9f7232205   Nishanth Menon   firmware: ti_sci:...
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
  
  	cops->get_clock = ti_sci_cmd_get_clock;
  	cops->idle_clock = ti_sci_cmd_idle_clock;
  	cops->put_clock = ti_sci_cmd_put_clock;
  	cops->is_auto = ti_sci_cmd_clk_is_auto;
  	cops->is_on = ti_sci_cmd_clk_is_on;
  	cops->is_off = ti_sci_cmd_clk_is_off;
  
  	cops->set_parent = ti_sci_cmd_clk_set_parent;
  	cops->get_parent = ti_sci_cmd_clk_get_parent;
  	cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
  
  	cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
  	cops->set_freq = ti_sci_cmd_clk_set_freq;
  	cops->get_freq = ti_sci_cmd_clk_get_freq;
9c19fb689   Lokesh Vutla   firmware: ti_sci:...
2969
2970
2971
2972
  
  	rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
  	rm_core_ops->get_range_from_shost =
  				ti_sci_cmd_get_resource_range_from_shost;
997b001f6   Lokesh Vutla   firmware: ti_sci:...
2973
2974
2975
2976
2977
  
  	iops->set_irq = ti_sci_cmd_set_irq;
  	iops->set_event_map = ti_sci_cmd_set_event_map;
  	iops->free_irq = ti_sci_cmd_free_irq;
  	iops->free_event_map = ti_sci_cmd_free_event_map;
68608b5e5   Peter Ujfalusi   firmware: ti_sci:...
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
  
  	rops->config = ti_sci_cmd_ring_config;
  	rops->get_config = ti_sci_cmd_ring_get_config;
  
  	psilops->pair = ti_sci_cmd_rm_psil_pair;
  	psilops->unpair = ti_sci_cmd_rm_psil_unpair;
  
  	udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
  	udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
  	udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
1e407f337   Suman Anna   firmware: ti_sci:...
2988
2989
2990
2991
2992
2993
2994
  
  	pops->request = ti_sci_cmd_proc_request;
  	pops->release = ti_sci_cmd_proc_release;
  	pops->handover = ti_sci_cmd_proc_handover;
  	pops->set_config = ti_sci_cmd_proc_set_config;
  	pops->set_control = ti_sci_cmd_proc_set_control;
  	pops->get_status = ti_sci_cmd_proc_get_status;
9e7d756da   Nishanth Menon   firmware: ti_sci:...
2995
2996
2997
  }
  
  /**
aa276781a   Nishanth Menon   firmware: Add bas...
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
   * ti_sci_get_handle() - Get the TI SCI handle for a device
   * @dev:	Pointer to device for which we want SCI handle
   *
   * NOTE: The function does not track individual clients of the framework
   * and is expected to be maintained by caller of TI SCI protocol library.
   * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
   * Return: pointer to handle if successful, else:
   * -EPROBE_DEFER if the instance is not ready
   * -ENODEV if the required node handler is missing
   * -EINVAL if invalid conditions are encountered.
   */
  const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
  {
  	struct device_node *ti_sci_np;
  	struct list_head *p;
  	struct ti_sci_handle *handle = NULL;
  	struct ti_sci_info *info;
  
  	if (!dev) {
  		pr_err("I need a device pointer
  ");
  		return ERR_PTR(-EINVAL);
  	}
  	ti_sci_np = of_get_parent(dev->of_node);
  	if (!ti_sci_np) {
  		dev_err(dev, "No OF information
  ");
  		return ERR_PTR(-EINVAL);
  	}
  
  	mutex_lock(&ti_sci_list_mutex);
  	list_for_each(p, &ti_sci_list) {
  		info = list_entry(p, struct ti_sci_info, node);
  		if (ti_sci_np == info->dev->of_node) {
  			handle = &info->handle;
  			info->users++;
  			break;
  		}
  	}
  	mutex_unlock(&ti_sci_list_mutex);
  	of_node_put(ti_sci_np);
  
  	if (!handle)
  		return ERR_PTR(-EPROBE_DEFER);
  
  	return handle;
  }
  EXPORT_SYMBOL_GPL(ti_sci_get_handle);
  
  /**
   * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
   * @handle:	Handle acquired by ti_sci_get_handle
   *
   * NOTE: The function does not track individual clients of the framework
   * and is expected to be maintained by caller of TI SCI protocol library.
   * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
   *
   * Return: 0 is successfully released
   * if an error pointer was passed, it returns the error value back,
   * if null was passed, it returns -EINVAL;
   */
  int ti_sci_put_handle(const struct ti_sci_handle *handle)
  {
  	struct ti_sci_info *info;
  
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	if (!handle)
  		return -EINVAL;
  
  	info = handle_to_ti_sci_info(handle);
  	mutex_lock(&ti_sci_list_mutex);
  	if (!WARN_ON(!info->users))
  		info->users--;
  	mutex_unlock(&ti_sci_list_mutex);
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ti_sci_put_handle);
  
  static void devm_ti_sci_release(struct device *dev, void *res)
  {
  	const struct ti_sci_handle **ptr = res;
  	const struct ti_sci_handle *handle = *ptr;
  	int ret;
  
  	ret = ti_sci_put_handle(handle);
  	if (ret)
  		dev_err(dev, "failed to put handle %d
  ", ret);
  }
  
  /**
   * devm_ti_sci_get_handle() - Managed get handle
   * @dev:	device for which we want SCI handle for.
   *
   * NOTE: This releases the handle once the device resources are
   * no longer needed. MUST NOT BE released with ti_sci_put_handle.
   * The function does not track individual clients of the framework
   * and is expected to be maintained by caller of TI SCI protocol library.
   *
   * Return: 0 if all went fine, else corresponding error.
   */
  const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
  {
  	const struct ti_sci_handle **ptr;
  	const struct ti_sci_handle *handle;
  
  	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
  	if (!ptr)
  		return ERR_PTR(-ENOMEM);
  	handle = ti_sci_get_handle(dev);
  
  	if (!IS_ERR(handle)) {
  		*ptr = handle;
  		devres_add(dev, ptr);
  	} else {
  		devres_free(ptr);
  	}
  
  	return handle;
  }
  EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
905c30477   Grygorii Strashko   firmware: ti_sci:...
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
  /**
   * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
   * @np:		device node
   * @property:	property name containing phandle on TISCI node
   *
   * NOTE: The function does not track individual clients of the framework
   * and is expected to be maintained by caller of TI SCI protocol library.
   * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
   * Return: pointer to handle if successful, else:
   * -EPROBE_DEFER if the instance is not ready
   * -ENODEV if the required node handler is missing
   * -EINVAL if invalid conditions are encountered.
   */
  const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
  						  const char *property)
  {
  	struct ti_sci_handle *handle = NULL;
  	struct device_node *ti_sci_np;
  	struct ti_sci_info *info;
  	struct list_head *p;
  
  	if (!np) {
  		pr_err("I need a device pointer
  ");
  		return ERR_PTR(-EINVAL);
  	}
  
  	ti_sci_np = of_parse_phandle(np, property, 0);
  	if (!ti_sci_np)
  		return ERR_PTR(-ENODEV);
  
  	mutex_lock(&ti_sci_list_mutex);
  	list_for_each(p, &ti_sci_list) {
  		info = list_entry(p, struct ti_sci_info, node);
  		if (ti_sci_np == info->dev->of_node) {
  			handle = &info->handle;
  			info->users++;
  			break;
  		}
  	}
  	mutex_unlock(&ti_sci_list_mutex);
  	of_node_put(ti_sci_np);
  
  	if (!handle)
  		return ERR_PTR(-EPROBE_DEFER);
  
  	return handle;
  }
  EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
  
  /**
   * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
   * @dev:	Device pointer requesting TISCI handle
   * @property:	property name containing phandle on TISCI node
   *
   * NOTE: This releases the handle once the device resources are
   * no longer needed. MUST NOT BE released with ti_sci_put_handle.
   * The function does not track individual clients of the framework
   * and is expected to be maintained by caller of TI SCI protocol library.
   *
   * Return: 0 if all went fine, else corresponding error.
   */
  const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
  						       const char *property)
  {
  	const struct ti_sci_handle *handle;
  	const struct ti_sci_handle **ptr;
  
  	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
  	if (!ptr)
  		return ERR_PTR(-ENOMEM);
  	handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
  
  	if (!IS_ERR(handle)) {
  		*ptr = handle;
  		devres_add(dev, ptr);
  	} else {
  		devres_free(ptr);
  	}
  
  	return handle;
  }
  EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
  /**
   * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
   * @res:	Pointer to the TISCI resource
   *
   * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
   */
  u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
  {
  	unsigned long flags;
  	u16 set, free_bit;
  
  	raw_spin_lock_irqsave(&res->lock, flags);
  	for (set = 0; set < res->sets; set++) {
  		free_bit = find_first_zero_bit(res->desc[set].res_map,
  					       res->desc[set].num);
  		if (free_bit != res->desc[set].num) {
  			set_bit(free_bit, res->desc[set].res_map);
  			raw_spin_unlock_irqrestore(&res->lock, flags);
  			return res->desc[set].start + free_bit;
  		}
  	}
  	raw_spin_unlock_irqrestore(&res->lock, flags);
  
  	return TI_SCI_RESOURCE_NULL;
  }
  EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
  
  /**
   * ti_sci_release_resource() - Release a resource from TISCI resource.
   * @res:	Pointer to the TISCI resource
   * @id:		Resource id to be released.
   */
  void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
  {
  	unsigned long flags;
  	u16 set;
  
  	raw_spin_lock_irqsave(&res->lock, flags);
  	for (set = 0; set < res->sets; set++) {
  		if (res->desc[set].start <= id &&
  		    (res->desc[set].num + res->desc[set].start) > id)
  			clear_bit(id - res->desc[set].start,
  				  res->desc[set].res_map);
  	}
  	raw_spin_unlock_irqrestore(&res->lock, flags);
  }
  EXPORT_SYMBOL_GPL(ti_sci_release_resource);
  
  /**
   * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
   * @res:	Pointer to the TISCI resource
   *
   * Return: Total number of available resources.
   */
  u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
  {
  	u32 set, count = 0;
  
  	for (set = 0; set < res->sets; set++)
  		count += res->desc[set].num;
  
  	return count;
  }
  EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
  
  /**
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3270
   * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3271
3272
3273
   * @handle:	TISCI handle
   * @dev:	Device pointer to which the resource is assigned
   * @dev_id:	TISCI device id to which the resource is assigned
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3274
3275
   * @sub_types:	Array of sub_types assigned corresponding to device
   * @sets:	Number of sub_types
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3276
3277
3278
3279
   *
   * Return: Pointer to ti_sci_resource if all went well else appropriate
   *	   error pointer.
   */
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3280
3281
3282
3283
  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)
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3284
3285
  {
  	struct ti_sci_resource *res;
fa42da11b   Peter Ujfalusi   firmware: ti_sci:...
3286
  	bool valid_set = false;
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3287
3288
3289
3290
3291
  	int i, ret;
  
  	res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
  	if (!res)
  		return ERR_PTR(-ENOMEM);
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3292
  	res->sets = sets;
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3293
3294
3295
3296
3297
3298
  	res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
  				 GFP_KERNEL);
  	if (!res->desc)
  		return ERR_PTR(-ENOMEM);
  
  	for (i = 0; i < res->sets; i++) {
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3299
  		ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3300
  							sub_types[i],
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3301
3302
3303
  							&res->desc[i].start,
  							&res->desc[i].num);
  		if (ret) {
fa42da11b   Peter Ujfalusi   firmware: ti_sci:...
3304
3305
  			dev_dbg(dev, "dev = %d subtype %d not allocated for this host
  ",
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3306
  				dev_id, sub_types[i]);
fa42da11b   Peter Ujfalusi   firmware: ti_sci:...
3307
3308
3309
  			res->desc[i].start = 0;
  			res->desc[i].num = 0;
  			continue;
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3310
3311
3312
3313
  		}
  
  		dev_dbg(dev, "dev = %d, subtype = %d, start = %d, num = %d
  ",
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3314
  			dev_id, sub_types[i], res->desc[i].start,
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3315
  			res->desc[i].num);
fa42da11b   Peter Ujfalusi   firmware: ti_sci:...
3316
  		valid_set = true;
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3317
3318
3319
3320
3321
3322
3323
  		res->desc[i].res_map =
  			devm_kzalloc(dev, BITS_TO_LONGS(res->desc[i].num) *
  				     sizeof(*res->desc[i].res_map), GFP_KERNEL);
  		if (!res->desc[i].res_map)
  			return ERR_PTR(-ENOMEM);
  	}
  	raw_spin_lock_init(&res->lock);
fa42da11b   Peter Ujfalusi   firmware: ti_sci:...
3324
3325
3326
3327
  	if (valid_set)
  		return res;
  
  	return ERR_PTR(-EINVAL);
032a1ec54   Lokesh Vutla   firmware: ti_sci:...
3328
  }
53bf2b0e4   Lokesh Vutla   firmware: ti_sci:...
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
  /**
   * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
   * @handle:	TISCI handle
   * @dev:	Device pointer to which the resource is assigned
   * @dev_id:	TISCI device id to which the resource is assigned
   * @of_prop:	property name by which the resource are represented
   *
   * Return: Pointer to ti_sci_resource if all went well else appropriate
   *	   error pointer.
   */
  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)
  {
  	struct ti_sci_resource *res;
  	u32 *sub_types;
  	int sets;
  
  	sets = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
  					       sizeof(u32));
  	if (sets < 0) {
  		dev_err(dev, "%s resource type ids not available
  ", of_prop);
  		return ERR_PTR(sets);
  	}
  
  	sub_types = kcalloc(sets, sizeof(*sub_types), GFP_KERNEL);
  	if (!sub_types)
  		return ERR_PTR(-ENOMEM);
  
  	of_property_read_u32_array(dev_of_node(dev), of_prop, sub_types, sets);
  	res = devm_ti_sci_get_resource_sets(handle, dev, dev_id, sub_types,
  					    sets);
  
  	kfree(sub_types);
  	return res;
  }
  EXPORT_SYMBOL_GPL(devm_ti_sci_get_of_resource);
  
  /**
   * devm_ti_sci_get_resource() - Get a resource range assigned to the device
   * @handle:	TISCI handle
   * @dev:	Device pointer to which the resource is assigned
   * @dev_id:	TISCI device id to which the resource is assigned
   * @suub_type:	TISCI resource subytpe representing the resource.
   *
   * Return: Pointer to ti_sci_resource if all went well else appropriate
   *	   error pointer.
   */
  struct ti_sci_resource *
  devm_ti_sci_get_resource(const struct ti_sci_handle *handle, struct device *dev,
  			 u32 dev_id, u32 sub_type)
  {
  	return devm_ti_sci_get_resource_sets(handle, dev, dev_id, &sub_type, 1);
  }
  EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
912cffb4e   Nishanth Menon   firmware: ti_sci:...
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
  static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
  				void *cmd)
  {
  	struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
  	const struct ti_sci_handle *handle = &info->handle;
  
  	ti_sci_cmd_core_reboot(handle);
  
  	/* call fail OR pass, we should not be here in the first place */
  	return NOTIFY_BAD;
  }
aa276781a   Nishanth Menon   firmware: Add bas...
3396
3397
  /* Description for K2G */
  static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
e69a35531   Nishanth Menon   firmware: ti_sci:...
3398
  	.default_host_id = 2,
aa276781a   Nishanth Menon   firmware: Add bas...
3399
3400
3401
3402
3403
  	/* Conservative duration */
  	.max_rx_timeout_ms = 1000,
  	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
  	.max_msgs = 20,
  	.max_msg_size = 64,
754c9477a   Peter Ujfalusi   firmware: ti_sci:...
3404
  };
754c9477a   Peter Ujfalusi   firmware: ti_sci:...
3405
3406
3407
3408
3409
3410
3411
3412
  /* Description for AM654 */
  static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
  	.default_host_id = 12,
  	/* Conservative duration */
  	.max_rx_timeout_ms = 10000,
  	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
  	.max_msgs = 20,
  	.max_msg_size = 60,
aa276781a   Nishanth Menon   firmware: Add bas...
3413
3414
3415
3416
  };
  
  static const struct of_device_id ti_sci_of_match[] = {
  	{.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
754c9477a   Peter Ujfalusi   firmware: ti_sci:...
3417
  	{.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
aa276781a   Nishanth Menon   firmware: Add bas...
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
  	{ /* Sentinel */ },
  };
  MODULE_DEVICE_TABLE(of, ti_sci_of_match);
  
  static int ti_sci_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	const struct of_device_id *of_id;
  	const struct ti_sci_desc *desc;
  	struct ti_sci_xfer *xfer;
  	struct ti_sci_info *info = NULL;
  	struct ti_sci_xfers_info *minfo;
  	struct mbox_client *cl;
  	int ret = -EINVAL;
  	int i;
912cffb4e   Nishanth Menon   firmware: ti_sci:...
3433
  	int reboot = 0;
e69a35531   Nishanth Menon   firmware: ti_sci:...
3434
  	u32 h_id;
aa276781a   Nishanth Menon   firmware: Add bas...
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
  
  	of_id = of_match_device(ti_sci_of_match, dev);
  	if (!of_id) {
  		dev_err(dev, "OF data missing
  ");
  		return -EINVAL;
  	}
  	desc = of_id->data;
  
  	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  	if (!info)
  		return -ENOMEM;
  
  	info->dev = dev;
  	info->desc = desc;
e69a35531   Nishanth Menon   firmware: ti_sci:...
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
  	ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
  	/* if the property is not present in DT, use a default from desc */
  	if (ret < 0) {
  		info->host_id = info->desc->default_host_id;
  	} else {
  		if (!h_id) {
  			dev_warn(dev, "Host ID 0 is reserved for firmware
  ");
  			info->host_id = info->desc->default_host_id;
  		} else {
  			info->host_id = h_id;
  		}
  	}
912cffb4e   Nishanth Menon   firmware: ti_sci:...
3463
3464
  	reboot = of_property_read_bool(dev->of_node,
  				       "ti,system-reboot-controller");
aa276781a   Nishanth Menon   firmware: Add bas...
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
  	INIT_LIST_HEAD(&info->node);
  	minfo = &info->minfo;
  
  	/*
  	 * Pre-allocate messages
  	 * NEVER allocate more than what we can indicate in hdr.seq
  	 * if we have data description bug, force a fix..
  	 */
  	if (WARN_ON(desc->max_msgs >=
  		    1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
  		return -EINVAL;
  
  	minfo->xfer_block = devm_kcalloc(dev,
  					 desc->max_msgs,
  					 sizeof(*minfo->xfer_block),
  					 GFP_KERNEL);
  	if (!minfo->xfer_block)
  		return -ENOMEM;
a86854d0c   Kees Cook   treewide: devm_kz...
3483
3484
3485
  	minfo->xfer_alloc_table = devm_kcalloc(dev,
  					       BITS_TO_LONGS(desc->max_msgs),
  					       sizeof(unsigned long),
aa276781a   Nishanth Menon   firmware: Add bas...
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
  					       GFP_KERNEL);
  	if (!minfo->xfer_alloc_table)
  		return -ENOMEM;
  	bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
  
  	/* Pre-initialize the buffer pointer to pre-allocated buffers */
  	for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
  		xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
  					      GFP_KERNEL);
  		if (!xfer->xfer_buf)
  			return -ENOMEM;
  
  		xfer->tx_message.buf = xfer->xfer_buf;
  		init_completion(&xfer->done);
  	}
  
  	ret = ti_sci_debugfs_create(pdev, info);
  	if (ret)
  		dev_warn(dev, "Failed to create debug file
  ");
  
  	platform_set_drvdata(pdev, info);
  
  	cl = &info->cl;
  	cl->dev = dev;
  	cl->tx_block = false;
  	cl->rx_callback = ti_sci_rx_callback;
  	cl->knows_txdone = true;
  
  	spin_lock_init(&minfo->xfer_lock);
  	sema_init(&minfo->sem_xfer_count, desc->max_msgs);
  
  	info->chan_rx = mbox_request_channel_byname(cl, "rx");
  	if (IS_ERR(info->chan_rx)) {
  		ret = PTR_ERR(info->chan_rx);
  		goto out;
  	}
  
  	info->chan_tx = mbox_request_channel_byname(cl, "tx");
  	if (IS_ERR(info->chan_tx)) {
  		ret = PTR_ERR(info->chan_tx);
  		goto out;
  	}
  	ret = ti_sci_cmd_get_revision(info);
  	if (ret) {
  		dev_err(dev, "Unable to communicate with TISCI(%d)
  ", ret);
  		goto out;
  	}
9e7d756da   Nishanth Menon   firmware: ti_sci:...
3535
  	ti_sci_setup_ops(info);
912cffb4e   Nishanth Menon   firmware: ti_sci:...
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
  	if (reboot) {
  		info->nb.notifier_call = tisci_reboot_handler;
  		info->nb.priority = 128;
  
  		ret = register_restart_handler(&info->nb);
  		if (ret) {
  			dev_err(dev, "reboot registration fail(%d)
  ", ret);
  			return ret;
  		}
  	}
aa276781a   Nishanth Menon   firmware: Add bas...
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
  	dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')
  ",
  		 info->handle.version.abi_major, info->handle.version.abi_minor,
  		 info->handle.version.firmware_revision,
  		 info->handle.version.firmware_description);
  
  	mutex_lock(&ti_sci_list_mutex);
  	list_add_tail(&info->node, &ti_sci_list);
  	mutex_unlock(&ti_sci_list_mutex);
  
  	return of_platform_populate(dev->of_node, NULL, NULL, dev);
  out:
  	if (!IS_ERR(info->chan_tx))
  		mbox_free_channel(info->chan_tx);
  	if (!IS_ERR(info->chan_rx))
  		mbox_free_channel(info->chan_rx);
  	debugfs_remove(info->d);
  	return ret;
  }
  
  static int ti_sci_remove(struct platform_device *pdev)
  {
  	struct ti_sci_info *info;
  	struct device *dev = &pdev->dev;
  	int ret = 0;
  
  	of_platform_depopulate(dev);
  
  	info = platform_get_drvdata(pdev);
912cffb4e   Nishanth Menon   firmware: ti_sci:...
3576
3577
  	if (info->nb.notifier_call)
  		unregister_restart_handler(&info->nb);
aa276781a   Nishanth Menon   firmware: Add bas...
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
  	mutex_lock(&ti_sci_list_mutex);
  	if (info->users)
  		ret = -EBUSY;
  	else
  		list_del(&info->node);
  	mutex_unlock(&ti_sci_list_mutex);
  
  	if (!ret) {
  		ti_sci_debugfs_destroy(pdev, info);
  
  		/* Safe to free channels since no more users */
  		mbox_free_channel(info->chan_tx);
  		mbox_free_channel(info->chan_rx);
  	}
  
  	return ret;
  }
  
  static struct platform_driver ti_sci_driver = {
  	.probe = ti_sci_probe,
  	.remove = ti_sci_remove,
  	.driver = {
  		   .name = "ti-sci",
  		   .of_match_table = of_match_ptr(ti_sci_of_match),
  	},
  };
  module_platform_driver(ti_sci_driver);
  
  MODULE_LICENSE("GPL v2");
  MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
  MODULE_AUTHOR("Nishanth Menon");
  MODULE_ALIAS("platform:ti-sci");