blob: c7b25ccf6f07ce9de0b2f9bad0644eb27136dd42 [file] [log] [blame]
Nishanth Menonaa276782016-10-18 18:08:34 -05001/*
2 * Texas Instruments System Control Interface Protocol Driver
3 *
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Nishanth Menon
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#define pr_fmt(fmt) "%s: " fmt, __func__
18
19#include <linux/bitmap.h>
20#include <linux/debugfs.h>
21#include <linux/export.h>
22#include <linux/io.h>
23#include <linux/kernel.h>
24#include <linux/mailbox_client.h>
25#include <linux/module.h>
26#include <linux/of_device.h>
27#include <linux/semaphore.h>
28#include <linux/slab.h>
29#include <linux/soc/ti/ti-msgmgr.h>
30#include <linux/soc/ti/ti_sci_protocol.h>
31
32#include "ti_sci.h"
33
34/* List of all TI SCI devices active in system */
35static LIST_HEAD(ti_sci_list);
36/* Protection for the entire list */
37static DEFINE_MUTEX(ti_sci_list_mutex);
38
39/**
40 * struct ti_sci_xfer - Structure representing a message flow
41 * @tx_message: Transmit message
42 * @rx_len: Receive message length
43 * @xfer_buf: Preallocated buffer to store receive message
44 * Since we work with request-ACK protocol, we can
45 * reuse the same buffer for the rx path as we
46 * use for the tx path.
47 * @done: completion event
48 */
49struct ti_sci_xfer {
50 struct ti_msgmgr_message tx_message;
51 u8 rx_len;
52 u8 *xfer_buf;
53 struct completion done;
54};
55
56/**
57 * struct ti_sci_xfers_info - Structure to manage transfer information
58 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
59 * Messages.
60 * @xfer_block: Preallocated Message array
61 * @xfer_alloc_table: Bitmap table for allocated messages.
62 * Index of this bitmap table is also used for message
63 * sequence identifier.
64 * @xfer_lock: Protection for message allocation
65 */
66struct ti_sci_xfers_info {
67 struct semaphore sem_xfer_count;
68 struct ti_sci_xfer *xfer_block;
69 unsigned long *xfer_alloc_table;
70 /* protect transfer allocation */
71 spinlock_t xfer_lock;
72};
73
74/**
75 * struct ti_sci_desc - Description of SoC integration
76 * @host_id: Host identifier representing the compute entity
77 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
78 * @max_msgs: Maximum number of messages that can be pending
79 * simultaneously in the system
80 * @max_msg_size: Maximum size of data per message that can be handled.
81 */
82struct ti_sci_desc {
83 u8 host_id;
84 int max_rx_timeout_ms;
85 int max_msgs;
86 int max_msg_size;
87};
88
89/**
90 * struct ti_sci_info - Structure representing a TI SCI instance
91 * @dev: Device pointer
92 * @desc: SoC description for this instance
93 * @d: Debugfs file entry
94 * @debug_region: Memory region where the debug message are available
95 * @debug_region_size: Debug region size
96 * @debug_buffer: Buffer allocated to copy debug messages.
97 * @handle: Instance of TI SCI handle to send to clients.
98 * @cl: Mailbox Client
99 * @chan_tx: Transmit mailbox channel
100 * @chan_rx: Receive mailbox channel
101 * @minfo: Message info
102 * @node: list head
103 * @users: Number of users of this instance
104 */
105struct ti_sci_info {
106 struct device *dev;
107 const struct ti_sci_desc *desc;
108 struct dentry *d;
109 void __iomem *debug_region;
110 char *debug_buffer;
111 size_t debug_region_size;
112 struct ti_sci_handle handle;
113 struct mbox_client cl;
114 struct mbox_chan *chan_tx;
115 struct mbox_chan *chan_rx;
116 struct ti_sci_xfers_info minfo;
117 struct list_head node;
118 /* protected by ti_sci_list_mutex */
119 int users;
120};
121
122#define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
123#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
124
125#ifdef CONFIG_DEBUG_FS
126
127/**
128 * ti_sci_debug_show() - Helper to dump the debug log
129 * @s: sequence file pointer
130 * @unused: unused.
131 *
132 * Return: 0
133 */
134static int ti_sci_debug_show(struct seq_file *s, void *unused)
135{
136 struct ti_sci_info *info = s->private;
137
138 memcpy_fromio(info->debug_buffer, info->debug_region,
139 info->debug_region_size);
140 /*
141 * We don't trust firmware to leave NULL terminated last byte (hence
142 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
143 * specific data format for debug messages, We just present the data
144 * in the buffer as is - we expect the messages to be self explanatory.
145 */
146 seq_puts(s, info->debug_buffer);
147 return 0;
148}
149
150/**
151 * ti_sci_debug_open() - debug file open
152 * @inode: inode pointer
153 * @file: file pointer
154 *
155 * Return: result of single_open
156 */
157static int ti_sci_debug_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, ti_sci_debug_show, inode->i_private);
160}
161
162/* log file operations */
163static const struct file_operations ti_sci_debug_fops = {
164 .open = ti_sci_debug_open,
165 .read = seq_read,
166 .llseek = seq_lseek,
167 .release = single_release,
168};
169
170/**
171 * ti_sci_debugfs_create() - Create log debug file
172 * @pdev: platform device pointer
173 * @info: Pointer to SCI entity information
174 *
175 * Return: 0 if all went fine, else corresponding error.
176 */
177static int ti_sci_debugfs_create(struct platform_device *pdev,
178 struct ti_sci_info *info)
179{
180 struct device *dev = &pdev->dev;
181 struct resource *res;
182 char debug_name[50] = "ti_sci_debug@";
183
184 /* Debug region is optional */
185 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
186 "debug_messages");
187 info->debug_region = devm_ioremap_resource(dev, res);
188 if (IS_ERR(info->debug_region))
189 return 0;
190 info->debug_region_size = resource_size(res);
191
192 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
193 sizeof(char), GFP_KERNEL);
194 if (!info->debug_buffer)
195 return -ENOMEM;
196 /* Setup NULL termination */
197 info->debug_buffer[info->debug_region_size] = 0;
198
199 info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
200 sizeof(debug_name)),
201 0444, NULL, info, &ti_sci_debug_fops);
202 if (IS_ERR(info->d))
203 return PTR_ERR(info->d);
204
205 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
206 info->debug_region, info->debug_region_size, res);
207 return 0;
208}
209
210/**
211 * ti_sci_debugfs_destroy() - clean up log debug file
212 * @pdev: platform device pointer
213 * @info: Pointer to SCI entity information
214 */
215static void ti_sci_debugfs_destroy(struct platform_device *pdev,
216 struct ti_sci_info *info)
217{
218 if (IS_ERR(info->debug_region))
219 return;
220
221 debugfs_remove(info->d);
222}
223#else /* CONFIG_DEBUG_FS */
224static inline int ti_sci_debugfs_create(struct platform_device *dev,
225 struct ti_sci_info *info)
226{
227 return 0;
228}
229
230static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
231 struct ti_sci_info *info)
232{
233}
234#endif /* CONFIG_DEBUG_FS */
235
236/**
237 * ti_sci_dump_header_dbg() - Helper to dump a message header.
238 * @dev: Device pointer corresponding to the SCI entity
239 * @hdr: pointer to header.
240 */
241static inline void ti_sci_dump_header_dbg(struct device *dev,
242 struct ti_sci_msg_hdr *hdr)
243{
244 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
245 hdr->type, hdr->host, hdr->seq, hdr->flags);
246}
247
248/**
249 * ti_sci_rx_callback() - mailbox client callback for receive messages
250 * @cl: client pointer
251 * @m: mailbox message
252 *
253 * Processes one received message to appropriate transfer information and
254 * signals completion of the transfer.
255 *
256 * NOTE: This function will be invoked in IRQ context, hence should be
257 * as optimal as possible.
258 */
259static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
260{
261 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
262 struct device *dev = info->dev;
263 struct ti_sci_xfers_info *minfo = &info->minfo;
264 struct ti_msgmgr_message *mbox_msg = m;
265 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
266 struct ti_sci_xfer *xfer;
267 u8 xfer_id;
268
269 xfer_id = hdr->seq;
270
271 /*
272 * Are we even expecting this?
273 * NOTE: barriers were implicit in locks used for modifying the bitmap
274 */
275 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
276 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
277 return;
278 }
279
280 xfer = &minfo->xfer_block[xfer_id];
281
282 /* Is the message of valid length? */
283 if (mbox_msg->len > info->desc->max_msg_size) {
284 dev_err(dev, "Unable to handle %d xfer(max %d)\n",
285 mbox_msg->len, info->desc->max_msg_size);
286 ti_sci_dump_header_dbg(dev, hdr);
287 return;
288 }
289 if (mbox_msg->len < xfer->rx_len) {
290 dev_err(dev, "Recv xfer %d < expected %d length\n",
291 mbox_msg->len, xfer->rx_len);
292 ti_sci_dump_header_dbg(dev, hdr);
293 return;
294 }
295
296 ti_sci_dump_header_dbg(dev, hdr);
297 /* Take a copy to the rx buffer.. */
298 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
299 complete(&xfer->done);
300}
301
302/**
303 * ti_sci_get_one_xfer() - Allocate one message
304 * @info: Pointer to SCI entity information
305 * @msg_type: Message type
306 * @msg_flags: Flag to set for the message
307 * @tx_message_size: transmit message size
308 * @rx_message_size: receive message size
309 *
310 * Helper function which is used by various command functions that are
311 * exposed to clients of this driver for allocating a message traffic event.
312 *
313 * This function can sleep depending on pending requests already in the system
314 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
315 * of internal data structures.
316 *
317 * Return: 0 if all went fine, else corresponding error.
318 */
319static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
320 u16 msg_type, u32 msg_flags,
321 size_t tx_message_size,
322 size_t rx_message_size)
323{
324 struct ti_sci_xfers_info *minfo = &info->minfo;
325 struct ti_sci_xfer *xfer;
326 struct ti_sci_msg_hdr *hdr;
327 unsigned long flags;
328 unsigned long bit_pos;
329 u8 xfer_id;
330 int ret;
331 int timeout;
332
333 /* Ensure we have sane transfer sizes */
334 if (rx_message_size > info->desc->max_msg_size ||
335 tx_message_size > info->desc->max_msg_size ||
336 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
337 return ERR_PTR(-ERANGE);
338
339 /*
340 * Ensure we have only controlled number of pending messages.
341 * Ideally, we might just have to wait a single message, be
342 * conservative and wait 5 times that..
343 */
344 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
345 ret = down_timeout(&minfo->sem_xfer_count, timeout);
346 if (ret < 0)
347 return ERR_PTR(ret);
348
349 /* Keep the locked section as small as possible */
350 spin_lock_irqsave(&minfo->xfer_lock, flags);
351 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
352 info->desc->max_msgs);
353 set_bit(bit_pos, minfo->xfer_alloc_table);
354 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
355
356 /*
357 * We already ensured in probe that we can have max messages that can
358 * fit in hdr.seq - NOTE: this improves access latencies
359 * to predictable O(1) access, BUT, it opens us to risk if
360 * remote misbehaves with corrupted message sequence responses.
361 * If that happens, we are going to be messed up anyways..
362 */
363 xfer_id = (u8)bit_pos;
364
365 xfer = &minfo->xfer_block[xfer_id];
366
367 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368 xfer->tx_message.len = tx_message_size;
369 xfer->rx_len = (u8)rx_message_size;
370
371 reinit_completion(&xfer->done);
372
373 hdr->seq = xfer_id;
374 hdr->type = msg_type;
375 hdr->host = info->desc->host_id;
376 hdr->flags = msg_flags;
377
378 return xfer;
379}
380
381/**
382 * ti_sci_put_one_xfer() - Release a message
383 * @minfo: transfer info pointer
384 * @xfer: message that was reserved by ti_sci_get_one_xfer
385 *
386 * This holds a spinlock to maintain integrity of internal data structures.
387 */
388static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
389 struct ti_sci_xfer *xfer)
390{
391 unsigned long flags;
392 struct ti_sci_msg_hdr *hdr;
393 u8 xfer_id;
394
395 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
396 xfer_id = hdr->seq;
397
398 /*
399 * Keep the locked section as small as possible
400 * NOTE: we might escape with smp_mb and no lock here..
401 * but just be conservative and symmetric.
402 */
403 spin_lock_irqsave(&minfo->xfer_lock, flags);
404 clear_bit(xfer_id, minfo->xfer_alloc_table);
405 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
406
407 /* Increment the count for the next user to get through */
408 up(&minfo->sem_xfer_count);
409}
410
411/**
412 * ti_sci_do_xfer() - Do one transfer
413 * @info: Pointer to SCI entity information
414 * @xfer: Transfer to initiate and wait for response
415 *
416 * Return: -ETIMEDOUT in case of no response, if transmit error,
417 * return corresponding error, else if all goes well,
418 * return 0.
419 */
420static inline int ti_sci_do_xfer(struct ti_sci_info *info,
421 struct ti_sci_xfer *xfer)
422{
423 int ret;
424 int timeout;
425 struct device *dev = info->dev;
426
427 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
428 if (ret < 0)
429 return ret;
430
431 ret = 0;
432
433 /* And we wait for the response. */
434 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
435 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
436 dev_err(dev, "Mbox timedout in resp(caller: %pF)\n",
437 (void *)_RET_IP_);
438 ret = -ETIMEDOUT;
439 }
440 /*
441 * NOTE: we might prefer not to need the mailbox ticker to manage the
442 * transfer queueing since the protocol layer queues things by itself.
443 * Unfortunately, we have to kick the mailbox framework after we have
444 * received our message.
445 */
446 mbox_client_txdone(info->chan_tx, ret);
447
448 return ret;
449}
450
451/**
452 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
453 * @info: Pointer to SCI entity information
454 *
455 * Updates the SCI information in the internal data structure.
456 *
457 * Return: 0 if all went fine, else return appropriate error.
458 */
459static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
460{
461 struct device *dev = info->dev;
462 struct ti_sci_handle *handle = &info->handle;
463 struct ti_sci_version_info *ver = &handle->version;
464 struct ti_sci_msg_resp_version *rev_info;
465 struct ti_sci_xfer *xfer;
466 int ret;
467
468 /* No need to setup flags since it is expected to respond */
469 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
470 0x0, sizeof(struct ti_sci_msg_hdr),
471 sizeof(*rev_info));
472 if (IS_ERR(xfer)) {
473 ret = PTR_ERR(xfer);
474 dev_err(dev, "Message alloc failed(%d)\n", ret);
475 return ret;
476 }
477
478 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
479
480 ret = ti_sci_do_xfer(info, xfer);
481 if (ret) {
482 dev_err(dev, "Mbox send fail %d\n", ret);
483 goto fail;
484 }
485
486 ver->abi_major = rev_info->abi_major;
487 ver->abi_minor = rev_info->abi_minor;
488 ver->firmware_revision = rev_info->firmware_revision;
489 strncpy(ver->firmware_description, rev_info->firmware_description,
490 sizeof(ver->firmware_description));
491
492fail:
493 ti_sci_put_one_xfer(&info->minfo, xfer);
494 return ret;
495}
496
497/**
Nishanth Menon9e7d7562016-10-18 18:08:35 -0500498 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
499 * @r: pointer to response buffer
500 *
501 * Return: true if the response was an ACK, else returns false.
502 */
503static inline bool ti_sci_is_response_ack(void *r)
504{
505 struct ti_sci_msg_hdr *hdr = r;
506
507 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
508}
509
510/**
511 * ti_sci_set_device_state() - Set device state helper
512 * @handle: pointer to TI SCI handle
513 * @id: Device identifier
514 * @flags: flags to setup for the device
515 * @state: State to move the device to
516 *
517 * Return: 0 if all went well, else returns appropriate error value.
518 */
519static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
520 u32 id, u32 flags, u8 state)
521{
522 struct ti_sci_info *info;
523 struct ti_sci_msg_req_set_device_state *req;
524 struct ti_sci_msg_hdr *resp;
525 struct ti_sci_xfer *xfer;
526 struct device *dev;
527 int ret = 0;
528
529 if (IS_ERR(handle))
530 return PTR_ERR(handle);
531 if (!handle)
532 return -EINVAL;
533
534 info = handle_to_ti_sci_info(handle);
535 dev = info->dev;
536
537 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
538 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
539 sizeof(*req), sizeof(*resp));
540 if (IS_ERR(xfer)) {
541 ret = PTR_ERR(xfer);
542 dev_err(dev, "Message alloc failed(%d)\n", ret);
543 return ret;
544 }
545 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
546 req->id = id;
547 req->state = state;
548
549 ret = ti_sci_do_xfer(info, xfer);
550 if (ret) {
551 dev_err(dev, "Mbox send fail %d\n", ret);
552 goto fail;
553 }
554
555 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
556
557 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
558
559fail:
560 ti_sci_put_one_xfer(&info->minfo, xfer);
561
562 return ret;
563}
564
565/**
566 * ti_sci_get_device_state() - Get device state helper
567 * @handle: Handle to the device
568 * @id: Device Identifier
569 * @clcnt: Pointer to Context Loss Count
570 * @resets: pointer to resets
571 * @p_state: pointer to p_state
572 * @c_state: pointer to c_state
573 *
574 * Return: 0 if all went fine, else return appropriate error.
575 */
576static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
577 u32 id, u32 *clcnt, u32 *resets,
578 u8 *p_state, u8 *c_state)
579{
580 struct ti_sci_info *info;
581 struct ti_sci_msg_req_get_device_state *req;
582 struct ti_sci_msg_resp_get_device_state *resp;
583 struct ti_sci_xfer *xfer;
584 struct device *dev;
585 int ret = 0;
586
587 if (IS_ERR(handle))
588 return PTR_ERR(handle);
589 if (!handle)
590 return -EINVAL;
591
592 if (!clcnt && !resets && !p_state && !c_state)
593 return -EINVAL;
594
595 info = handle_to_ti_sci_info(handle);
596 dev = info->dev;
597
598 /* Response is expected, so need of any flags */
599 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
600 0, sizeof(*req), sizeof(*resp));
601 if (IS_ERR(xfer)) {
602 ret = PTR_ERR(xfer);
603 dev_err(dev, "Message alloc failed(%d)\n", ret);
604 return ret;
605 }
606 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
607 req->id = id;
608
609 ret = ti_sci_do_xfer(info, xfer);
610 if (ret) {
611 dev_err(dev, "Mbox send fail %d\n", ret);
612 goto fail;
613 }
614
615 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
616 if (!ti_sci_is_response_ack(resp)) {
617 ret = -ENODEV;
618 goto fail;
619 }
620
621 if (clcnt)
622 *clcnt = resp->context_loss_count;
623 if (resets)
624 *resets = resp->resets;
625 if (p_state)
626 *p_state = resp->programmed_state;
627 if (c_state)
628 *c_state = resp->current_state;
629fail:
630 ti_sci_put_one_xfer(&info->minfo, xfer);
631
632 return ret;
633}
634
635/**
636 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
637 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
638 * @id: Device Identifier
639 *
640 * Request for the device - NOTE: the client MUST maintain integrity of
641 * usage count by balancing get_device with put_device. No refcounting is
642 * managed by driver for that purpose.
643 *
644 * NOTE: The request is for exclusive access for the processor.
645 *
646 * Return: 0 if all went fine, else return appropriate error.
647 */
648static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
649{
650 return ti_sci_set_device_state(handle, id,
651 MSG_FLAG_DEVICE_EXCLUSIVE,
652 MSG_DEVICE_SW_STATE_ON);
653}
654
655/**
656 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
657 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
658 * @id: Device Identifier
659 *
660 * Request for the device - NOTE: the client MUST maintain integrity of
661 * usage count by balancing get_device with put_device. No refcounting is
662 * managed by driver for that purpose.
663 *
664 * Return: 0 if all went fine, else return appropriate error.
665 */
666static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
667{
668 return ti_sci_set_device_state(handle, id,
669 MSG_FLAG_DEVICE_EXCLUSIVE,
670 MSG_DEVICE_SW_STATE_RETENTION);
671}
672
673/**
674 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
675 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
676 * @id: Device Identifier
677 *
678 * Request for the device - NOTE: the client MUST maintain integrity of
679 * usage count by balancing get_device with put_device. No refcounting is
680 * managed by driver for that purpose.
681 *
682 * Return: 0 if all went fine, else return appropriate error.
683 */
684static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
685{
686 return ti_sci_set_device_state(handle, id,
687 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
688}
689
690/**
691 * ti_sci_cmd_dev_is_valid() - Is the device valid
692 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
693 * @id: Device Identifier
694 *
695 * Return: 0 if all went fine and the device ID is valid, else return
696 * appropriate error.
697 */
698static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
699{
700 u8 unused;
701
702 /* check the device state which will also tell us if the ID is valid */
703 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
704}
705
706/**
707 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
708 * @handle: Pointer to TISCI handle
709 * @id: Device Identifier
710 * @count: Pointer to Context Loss counter to populate
711 *
712 * Return: 0 if all went fine, else return appropriate error.
713 */
714static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
715 u32 *count)
716{
717 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
718}
719
720/**
721 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
722 * @handle: Pointer to TISCI handle
723 * @id: Device Identifier
724 * @r_state: true if requested to be idle
725 *
726 * Return: 0 if all went fine, else return appropriate error.
727 */
728static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
729 bool *r_state)
730{
731 int ret;
732 u8 state;
733
734 if (!r_state)
735 return -EINVAL;
736
737 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
738 if (ret)
739 return ret;
740
741 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
742
743 return 0;
744}
745
746/**
747 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
748 * @handle: Pointer to TISCI handle
749 * @id: Device Identifier
750 * @r_state: true if requested to be stopped
751 * @curr_state: true if currently stopped.
752 *
753 * Return: 0 if all went fine, else return appropriate error.
754 */
755static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
756 bool *r_state, bool *curr_state)
757{
758 int ret;
759 u8 p_state, c_state;
760
761 if (!r_state && !curr_state)
762 return -EINVAL;
763
764 ret =
765 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
766 if (ret)
767 return ret;
768
769 if (r_state)
770 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
771 if (curr_state)
772 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
773
774 return 0;
775}
776
777/**
778 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
779 * @handle: Pointer to TISCI handle
780 * @id: Device Identifier
781 * @r_state: true if requested to be ON
782 * @curr_state: true if currently ON and active
783 *
784 * Return: 0 if all went fine, else return appropriate error.
785 */
786static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
787 bool *r_state, bool *curr_state)
788{
789 int ret;
790 u8 p_state, c_state;
791
792 if (!r_state && !curr_state)
793 return -EINVAL;
794
795 ret =
796 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
797 if (ret)
798 return ret;
799
800 if (r_state)
801 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
802 if (curr_state)
803 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
804
805 return 0;
806}
807
808/**
809 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
810 * @handle: Pointer to TISCI handle
811 * @id: Device Identifier
812 * @curr_state: true if currently transitioning.
813 *
814 * Return: 0 if all went fine, else return appropriate error.
815 */
816static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
817 bool *curr_state)
818{
819 int ret;
820 u8 state;
821
822 if (!curr_state)
823 return -EINVAL;
824
825 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
826 if (ret)
827 return ret;
828
829 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
830
831 return 0;
832}
833
834/**
835 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
836 * by TISCI
837 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
838 * @id: Device Identifier
839 * @reset_state: Device specific reset bit field
840 *
841 * Return: 0 if all went fine, else return appropriate error.
842 */
843static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
844 u32 id, u32 reset_state)
845{
846 struct ti_sci_info *info;
847 struct ti_sci_msg_req_set_device_resets *req;
848 struct ti_sci_msg_hdr *resp;
849 struct ti_sci_xfer *xfer;
850 struct device *dev;
851 int ret = 0;
852
853 if (IS_ERR(handle))
854 return PTR_ERR(handle);
855 if (!handle)
856 return -EINVAL;
857
858 info = handle_to_ti_sci_info(handle);
859 dev = info->dev;
860
861 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
862 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
863 sizeof(*req), sizeof(*resp));
864 if (IS_ERR(xfer)) {
865 ret = PTR_ERR(xfer);
866 dev_err(dev, "Message alloc failed(%d)\n", ret);
867 return ret;
868 }
869 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
870 req->id = id;
871 req->resets = reset_state;
872
873 ret = ti_sci_do_xfer(info, xfer);
874 if (ret) {
875 dev_err(dev, "Mbox send fail %d\n", ret);
876 goto fail;
877 }
878
879 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
880
881 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
882
883fail:
884 ti_sci_put_one_xfer(&info->minfo, xfer);
885
886 return ret;
887}
888
889/**
890 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
891 * by TISCI
892 * @handle: Pointer to TISCI handle
893 * @id: Device Identifier
894 * @reset_state: Pointer to reset state to populate
895 *
896 * Return: 0 if all went fine, else return appropriate error.
897 */
898static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
899 u32 id, u32 *reset_state)
900{
901 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
902 NULL);
903}
904
905/*
906 * ti_sci_setup_ops() - Setup the operations structures
907 * @info: pointer to TISCI pointer
908 */
909static void ti_sci_setup_ops(struct ti_sci_info *info)
910{
911 struct ti_sci_ops *ops = &info->handle.ops;
912 struct ti_sci_dev_ops *dops = &ops->dev_ops;
913
914 dops->get_device = ti_sci_cmd_get_device;
915 dops->idle_device = ti_sci_cmd_idle_device;
916 dops->put_device = ti_sci_cmd_put_device;
917
918 dops->is_valid = ti_sci_cmd_dev_is_valid;
919 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
920 dops->is_idle = ti_sci_cmd_dev_is_idle;
921 dops->is_stop = ti_sci_cmd_dev_is_stop;
922 dops->is_on = ti_sci_cmd_dev_is_on;
923 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
924 dops->set_device_resets = ti_sci_cmd_set_device_resets;
925 dops->get_device_resets = ti_sci_cmd_get_device_resets;
926}
927
928/**
Nishanth Menonaa276782016-10-18 18:08:34 -0500929 * ti_sci_get_handle() - Get the TI SCI handle for a device
930 * @dev: Pointer to device for which we want SCI handle
931 *
932 * NOTE: The function does not track individual clients of the framework
933 * and is expected to be maintained by caller of TI SCI protocol library.
934 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
935 * Return: pointer to handle if successful, else:
936 * -EPROBE_DEFER if the instance is not ready
937 * -ENODEV if the required node handler is missing
938 * -EINVAL if invalid conditions are encountered.
939 */
940const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
941{
942 struct device_node *ti_sci_np;
943 struct list_head *p;
944 struct ti_sci_handle *handle = NULL;
945 struct ti_sci_info *info;
946
947 if (!dev) {
948 pr_err("I need a device pointer\n");
949 return ERR_PTR(-EINVAL);
950 }
951 ti_sci_np = of_get_parent(dev->of_node);
952 if (!ti_sci_np) {
953 dev_err(dev, "No OF information\n");
954 return ERR_PTR(-EINVAL);
955 }
956
957 mutex_lock(&ti_sci_list_mutex);
958 list_for_each(p, &ti_sci_list) {
959 info = list_entry(p, struct ti_sci_info, node);
960 if (ti_sci_np == info->dev->of_node) {
961 handle = &info->handle;
962 info->users++;
963 break;
964 }
965 }
966 mutex_unlock(&ti_sci_list_mutex);
967 of_node_put(ti_sci_np);
968
969 if (!handle)
970 return ERR_PTR(-EPROBE_DEFER);
971
972 return handle;
973}
974EXPORT_SYMBOL_GPL(ti_sci_get_handle);
975
976/**
977 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
978 * @handle: Handle acquired by ti_sci_get_handle
979 *
980 * NOTE: The function does not track individual clients of the framework
981 * and is expected to be maintained by caller of TI SCI protocol library.
982 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
983 *
984 * Return: 0 is successfully released
985 * if an error pointer was passed, it returns the error value back,
986 * if null was passed, it returns -EINVAL;
987 */
988int ti_sci_put_handle(const struct ti_sci_handle *handle)
989{
990 struct ti_sci_info *info;
991
992 if (IS_ERR(handle))
993 return PTR_ERR(handle);
994 if (!handle)
995 return -EINVAL;
996
997 info = handle_to_ti_sci_info(handle);
998 mutex_lock(&ti_sci_list_mutex);
999 if (!WARN_ON(!info->users))
1000 info->users--;
1001 mutex_unlock(&ti_sci_list_mutex);
1002
1003 return 0;
1004}
1005EXPORT_SYMBOL_GPL(ti_sci_put_handle);
1006
1007static void devm_ti_sci_release(struct device *dev, void *res)
1008{
1009 const struct ti_sci_handle **ptr = res;
1010 const struct ti_sci_handle *handle = *ptr;
1011 int ret;
1012
1013 ret = ti_sci_put_handle(handle);
1014 if (ret)
1015 dev_err(dev, "failed to put handle %d\n", ret);
1016}
1017
1018/**
1019 * devm_ti_sci_get_handle() - Managed get handle
1020 * @dev: device for which we want SCI handle for.
1021 *
1022 * NOTE: This releases the handle once the device resources are
1023 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1024 * The function does not track individual clients of the framework
1025 * and is expected to be maintained by caller of TI SCI protocol library.
1026 *
1027 * Return: 0 if all went fine, else corresponding error.
1028 */
1029const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
1030{
1031 const struct ti_sci_handle **ptr;
1032 const struct ti_sci_handle *handle;
1033
1034 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
1035 if (!ptr)
1036 return ERR_PTR(-ENOMEM);
1037 handle = ti_sci_get_handle(dev);
1038
1039 if (!IS_ERR(handle)) {
1040 *ptr = handle;
1041 devres_add(dev, ptr);
1042 } else {
1043 devres_free(ptr);
1044 }
1045
1046 return handle;
1047}
1048EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
1049
1050/* Description for K2G */
1051static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
1052 .host_id = 2,
1053 /* Conservative duration */
1054 .max_rx_timeout_ms = 1000,
1055 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1056 .max_msgs = 20,
1057 .max_msg_size = 64,
1058};
1059
1060static const struct of_device_id ti_sci_of_match[] = {
1061 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
1062 { /* Sentinel */ },
1063};
1064MODULE_DEVICE_TABLE(of, ti_sci_of_match);
1065
1066static int ti_sci_probe(struct platform_device *pdev)
1067{
1068 struct device *dev = &pdev->dev;
1069 const struct of_device_id *of_id;
1070 const struct ti_sci_desc *desc;
1071 struct ti_sci_xfer *xfer;
1072 struct ti_sci_info *info = NULL;
1073 struct ti_sci_xfers_info *minfo;
1074 struct mbox_client *cl;
1075 int ret = -EINVAL;
1076 int i;
1077
1078 of_id = of_match_device(ti_sci_of_match, dev);
1079 if (!of_id) {
1080 dev_err(dev, "OF data missing\n");
1081 return -EINVAL;
1082 }
1083 desc = of_id->data;
1084
1085 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1086 if (!info)
1087 return -ENOMEM;
1088
1089 info->dev = dev;
1090 info->desc = desc;
1091 INIT_LIST_HEAD(&info->node);
1092 minfo = &info->minfo;
1093
1094 /*
1095 * Pre-allocate messages
1096 * NEVER allocate more than what we can indicate in hdr.seq
1097 * if we have data description bug, force a fix..
1098 */
1099 if (WARN_ON(desc->max_msgs >=
1100 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
1101 return -EINVAL;
1102
1103 minfo->xfer_block = devm_kcalloc(dev,
1104 desc->max_msgs,
1105 sizeof(*minfo->xfer_block),
1106 GFP_KERNEL);
1107 if (!minfo->xfer_block)
1108 return -ENOMEM;
1109
1110 minfo->xfer_alloc_table = devm_kzalloc(dev,
1111 BITS_TO_LONGS(desc->max_msgs)
1112 * sizeof(unsigned long),
1113 GFP_KERNEL);
1114 if (!minfo->xfer_alloc_table)
1115 return -ENOMEM;
1116 bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
1117
1118 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1119 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
1120 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
1121 GFP_KERNEL);
1122 if (!xfer->xfer_buf)
1123 return -ENOMEM;
1124
1125 xfer->tx_message.buf = xfer->xfer_buf;
1126 init_completion(&xfer->done);
1127 }
1128
1129 ret = ti_sci_debugfs_create(pdev, info);
1130 if (ret)
1131 dev_warn(dev, "Failed to create debug file\n");
1132
1133 platform_set_drvdata(pdev, info);
1134
1135 cl = &info->cl;
1136 cl->dev = dev;
1137 cl->tx_block = false;
1138 cl->rx_callback = ti_sci_rx_callback;
1139 cl->knows_txdone = true;
1140
1141 spin_lock_init(&minfo->xfer_lock);
1142 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
1143
1144 info->chan_rx = mbox_request_channel_byname(cl, "rx");
1145 if (IS_ERR(info->chan_rx)) {
1146 ret = PTR_ERR(info->chan_rx);
1147 goto out;
1148 }
1149
1150 info->chan_tx = mbox_request_channel_byname(cl, "tx");
1151 if (IS_ERR(info->chan_tx)) {
1152 ret = PTR_ERR(info->chan_tx);
1153 goto out;
1154 }
1155 ret = ti_sci_cmd_get_revision(info);
1156 if (ret) {
1157 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
1158 goto out;
1159 }
1160
Nishanth Menon9e7d7562016-10-18 18:08:35 -05001161 ti_sci_setup_ops(info);
1162
Nishanth Menonaa276782016-10-18 18:08:34 -05001163 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1164 info->handle.version.abi_major, info->handle.version.abi_minor,
1165 info->handle.version.firmware_revision,
1166 info->handle.version.firmware_description);
1167
1168 mutex_lock(&ti_sci_list_mutex);
1169 list_add_tail(&info->node, &ti_sci_list);
1170 mutex_unlock(&ti_sci_list_mutex);
1171
1172 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1173out:
1174 if (!IS_ERR(info->chan_tx))
1175 mbox_free_channel(info->chan_tx);
1176 if (!IS_ERR(info->chan_rx))
1177 mbox_free_channel(info->chan_rx);
1178 debugfs_remove(info->d);
1179 return ret;
1180}
1181
1182static int ti_sci_remove(struct platform_device *pdev)
1183{
1184 struct ti_sci_info *info;
1185 struct device *dev = &pdev->dev;
1186 int ret = 0;
1187
1188 of_platform_depopulate(dev);
1189
1190 info = platform_get_drvdata(pdev);
1191
1192 mutex_lock(&ti_sci_list_mutex);
1193 if (info->users)
1194 ret = -EBUSY;
1195 else
1196 list_del(&info->node);
1197 mutex_unlock(&ti_sci_list_mutex);
1198
1199 if (!ret) {
1200 ti_sci_debugfs_destroy(pdev, info);
1201
1202 /* Safe to free channels since no more users */
1203 mbox_free_channel(info->chan_tx);
1204 mbox_free_channel(info->chan_rx);
1205 }
1206
1207 return ret;
1208}
1209
1210static struct platform_driver ti_sci_driver = {
1211 .probe = ti_sci_probe,
1212 .remove = ti_sci_remove,
1213 .driver = {
1214 .name = "ti-sci",
1215 .of_match_table = of_match_ptr(ti_sci_of_match),
1216 },
1217};
1218module_platform_driver(ti_sci_driver);
1219
1220MODULE_LICENSE("GPL v2");
1221MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1222MODULE_AUTHOR("Nishanth Menon");
1223MODULE_ALIAS("platform:ti-sci");