| 1 | |
| 2 | |
| 3 | * Mailbox: Common code for Mailbox controllers and users
|
| 4 | *
|
| 5 | * Copyright (C) 2013-2014 Linaro Ltd.
|
| 6 | * Author: Jassi Brar <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/cleanup.h>
|
| 10 | #include <linux/debugfs.h>
|
| 11 | #include <linux/delay.h>
|
| 12 | #include <linux/device.h>
|
| 13 | #include <linux/err.h>
|
| 14 | #include <linux/mailbox_client.h>
|
| 15 | #include <linux/mailbox_controller.h>
|
| 16 | #include <linux/module.h>
|
| 17 | #include <linux/mutex.h>
|
| 18 | #include <linux/of.h>
|
| 19 | #include <linux/property.h>
|
| 20 | #include <linux/seq_file.h>
|
| 21 | #include <linux/spinlock.h>
|
| 22 |
|
| 23 | static LIST_HEAD(mbox_cons);
|
| 24 | static DEFINE_MUTEX(con_mutex);
|
| 25 |
|
| 26 | static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
|
| 27 | {
|
| 28 | int idx;
|
| 29 |
|
| 30 | guard(spinlock_irqsave)(&chan->lock);
|
| 31 |
|
| 32 |
|
| 33 | if (chan->msg_count == MBOX_TX_QUEUE_LEN)
|
| 34 | return -ENOBUFS;
|
| 35 |
|
| 36 | idx = chan->msg_free;
|
| 37 | chan->msg_data[idx] = mssg;
|
| 38 | chan->msg_count++;
|
| 39 |
|
| 40 | if (idx == MBOX_TX_QUEUE_LEN - 1)
|
| 41 | chan->msg_free = 0;
|
| 42 | else
|
| 43 | chan->msg_free++;
|
| 44 |
|
| 45 | return idx;
|
| 46 | }
|
| 47 |
|
| 48 | static void msg_submit(struct mbox_chan *chan)
|
| 49 | {
|
| 50 | unsigned count, idx;
|
| 51 | void *data;
|
| 52 | int err = -EBUSY;
|
| 53 |
|
| 54 | scoped_guard(spinlock_irqsave, &chan->lock) {
|
| 55 | if (!chan->msg_count || chan->active_req != MBOX_NO_MSG)
|
| 56 | break;
|
| 57 |
|
| 58 | count = chan->msg_count;
|
| 59 | idx = chan->msg_free;
|
| 60 | if (idx >= count)
|
| 61 | idx -= count;
|
| 62 | else
|
| 63 | idx += MBOX_TX_QUEUE_LEN - count;
|
| 64 |
|
| 65 | data = chan->msg_data[idx];
|
| 66 |
|
| 67 | if (chan->cl->tx_prepare)
|
| 68 | chan->cl->tx_prepare(chan->cl, data);
|
| 69 |
|
| 70 | err = chan->mbox->ops->send_data(chan, data);
|
| 71 | if (!err) {
|
| 72 | chan->active_req = data;
|
| 73 | chan->msg_count--;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | if (!err && (chan->txdone_method & MBOX_TXDONE_BY_POLL)) {
|
| 78 |
|
| 79 | scoped_guard(spinlock_irqsave, &chan->mbox->poll_hrt_lock)
|
| 80 | hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | static void tx_tick(struct mbox_chan *chan, int r)
|
| 85 | {
|
| 86 | void *mssg;
|
| 87 |
|
| 88 | scoped_guard(spinlock_irqsave, &chan->lock) {
|
| 89 | mssg = chan->active_req;
|
| 90 | chan->active_req = MBOX_NO_MSG;
|
| 91 | }
|
| 92 |
|
| 93 |
|
| 94 | msg_submit(chan);
|
| 95 |
|
| 96 | if (mssg == MBOX_NO_MSG)
|
| 97 | return;
|
| 98 |
|
| 99 |
|
| 100 | if (chan->cl->tx_done)
|
| 101 | chan->cl->tx_done(chan->cl, mssg, r);
|
| 102 |
|
| 103 | if (r != -ETIME && chan->cl->tx_block) {
|
| 104 | chan->tx_status = r;
|
| 105 | complete(&chan->tx_complete);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
|
| 110 | {
|
| 111 | struct mbox_controller *mbox =
|
| 112 | container_of(hrtimer, struct mbox_controller, poll_hrt);
|
| 113 | bool txdone, resched = false;
|
| 114 | int i;
|
| 115 |
|
| 116 | for (i = 0; i < mbox->num_chans; i++) {
|
| 117 | struct mbox_chan *chan = &mbox->chans[i];
|
| 118 |
|
| 119 | if (chan->active_req != MBOX_NO_MSG && chan->cl) {
|
| 120 | txdone = chan->mbox->ops->last_tx_done(chan);
|
| 121 | if (txdone)
|
| 122 | tx_tick(chan, 0);
|
| 123 | else
|
| 124 | resched = true;
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | if (resched) {
|
| 129 | scoped_guard(spinlock_irqsave, &mbox->poll_hrt_lock) {
|
| 130 | if (!hrtimer_is_queued(hrtimer))
|
| 131 | hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
|
| 132 | }
|
| 133 |
|
| 134 | return HRTIMER_RESTART;
|
| 135 | }
|
| 136 | return HRTIMER_NORESTART;
|
| 137 | }
|
| 138 |
|
| 139 | |
| 140 | * mbox_chan_received_data - A way for controller driver to push data
|
| 141 | * received from remote to the upper layer.
|
| 142 | * @chan: Pointer to the mailbox channel on which RX happened.
|
| 143 | * @mssg: Client specific message typecasted as void *
|
| 144 | *
|
| 145 | * After startup and before shutdown any data received on the chan
|
| 146 | * is passed on to the API via atomic mbox_chan_received_data().
|
| 147 | * The controller should ACK the RX only after this call returns.
|
| 148 |
|
| 149 | void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
|
| 150 | {
|
| 151 |
|
| 152 | if (chan->cl->rx_callback)
|
| 153 | chan->cl->rx_callback(chan->cl, mssg);
|
| 154 | }
|
| 155 | EXPORT_SYMBOL_GPL(mbox_chan_received_data);
|
| 156 |
|
| 157 | |
| 158 | * mbox_chan_txdone - A way for controller driver to notify the
|
| 159 | * framework that the last TX has completed.
|
| 160 | * @chan: Pointer to the mailbox chan on which TX happened.
|
| 161 | * @r: Status of last TX - OK or ERROR
|
| 162 | *
|
| 163 | * The controller that has IRQ for TX ACK calls this atomic API
|
| 164 | * to tick the TX state machine. It works only if txdone_irq
|
| 165 | * is set by the controller.
|
| 166 |
|
| 167 | void mbox_chan_txdone(struct mbox_chan *chan, int r)
|
| 168 | {
|
| 169 | if (unlikely(!(chan->txdone_method & MBOX_TXDONE_BY_IRQ))) {
|
| 170 | dev_err(chan->mbox->dev,
|
| 171 | "Controller can't run the TX ticker\n");
|
| 172 | return;
|
| 173 | }
|
| 174 |
|
| 175 | tx_tick(chan, r);
|
| 176 | }
|
| 177 | EXPORT_SYMBOL_GPL(mbox_chan_txdone);
|
| 178 |
|
| 179 | |
| 180 | * mbox_client_txdone - The way for a client to run the TX state machine.
|
| 181 | * @chan: Mailbox channel assigned to this client.
|
| 182 | * @r: Success status of last transmission.
|
| 183 | *
|
| 184 | * The client/protocol had received some 'ACK' packet and it notifies
|
| 185 | * the API that the last packet was sent successfully. This only works
|
| 186 | * if the controller can't sense TX-Done.
|
| 187 |
|
| 188 | void mbox_client_txdone(struct mbox_chan *chan, int r)
|
| 189 | {
|
| 190 | if (unlikely(!(chan->txdone_method & MBOX_TXDONE_BY_ACK))) {
|
| 191 | dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
|
| 192 | return;
|
| 193 | }
|
| 194 |
|
| 195 | tx_tick(chan, r);
|
| 196 | }
|
| 197 | EXPORT_SYMBOL_GPL(mbox_client_txdone);
|
| 198 |
|
| 199 | |
| 200 | * mbox_client_peek_data - A way for client driver to pull data
|
| 201 | * received from remote by the controller.
|
| 202 | * @chan: Mailbox channel assigned to this client.
|
| 203 | *
|
| 204 | * A poke to controller driver for any received data.
|
| 205 | * The data is actually passed onto client via the
|
| 206 | * mbox_chan_received_data()
|
| 207 | * The call can be made from atomic context, so the controller's
|
| 208 | * implementation of peek_data() must not sleep.
|
| 209 | *
|
| 210 | * Return: True, if controller has, and is going to push after this,
|
| 211 | * some data.
|
| 212 | * False, if controller doesn't have any data to be read.
|
| 213 |
|
| 214 | bool mbox_client_peek_data(struct mbox_chan *chan)
|
| 215 | {
|
| 216 | if (chan->mbox->ops->peek_data)
|
| 217 | return chan->mbox->ops->peek_data(chan);
|
| 218 |
|
| 219 | return false;
|
| 220 | }
|
| 221 | EXPORT_SYMBOL_GPL(mbox_client_peek_data);
|
| 222 |
|
| 223 | |
| 224 | * mbox_chan_tx_slots_available - Query the number of available TX queue slots.
|
| 225 | * @chan: Mailbox channel to query.
|
| 226 | *
|
| 227 | * Clients may call this to check how many messages can be queued via
|
| 228 | * mbox_send_message() before the channel's TX queue is full. This helps
|
| 229 | * clients avoid the -ENOBUFS error without needing to increase
|
| 230 | * MBOX_TX_QUEUE_LEN.
|
| 231 | * This can be called from atomic context.
|
| 232 | *
|
| 233 | * Return: Number of available slots in the channel's TX queue.
|
| 234 |
|
| 235 | unsigned int mbox_chan_tx_slots_available(struct mbox_chan *chan)
|
| 236 | {
|
| 237 | unsigned int ret;
|
| 238 |
|
| 239 | guard(spinlock_irqsave)(&chan->lock);
|
| 240 | ret = MBOX_TX_QUEUE_LEN - chan->msg_count;
|
| 241 |
|
| 242 | return ret;
|
| 243 | }
|
| 244 | EXPORT_SYMBOL_GPL(mbox_chan_tx_slots_available);
|
| 245 |
|
| 246 | |
| 247 | * mbox_send_message - For client to submit a message to be
|
| 248 | * sent to the remote.
|
| 249 | * @chan: Mailbox channel assigned to this client.
|
| 250 | * @mssg: Client specific message typecasted.
|
| 251 | *
|
| 252 | * For client to submit data to the controller destined for a remote
|
| 253 | * processor. If the client had set 'tx_block', the call will return
|
| 254 | * either when the remote receives the data or when 'tx_tout' millisecs
|
| 255 | * run out.
|
| 256 | * In non-blocking mode, the requests are buffered by the API and a
|
| 257 | * non-negative token is returned for each queued request. If the request
|
| 258 | * is not queued, a negative token is returned. Upon failure or successful
|
| 259 | * TX, the API calls 'tx_done' from atomic context, from which the client
|
| 260 | * could submit yet another request.
|
| 261 | * The pointer to message should be preserved until it is sent
|
| 262 | * over the chan, i.e, tx_done() is made.
|
| 263 | * This function could be called from atomic context as it simply
|
| 264 | * queues the data and returns a token against the request.
|
| 265 | * In blocking mode, it is caller's responsibility to serialize threads'
|
| 266 | * access to a channel if multi-threads are to send messages through the
|
| 267 | * same channel, i.e. caller should not call this function until any
|
| 268 | * previous call returns.
|
| 269 | *
|
| 270 | * Return: Non-negative integer for successful submission (non-blocking mode)
|
| 271 | * or transmission over chan (blocking mode).
|
| 272 | * Negative value denotes failure.
|
| 273 |
|
| 274 | int mbox_send_message(struct mbox_chan *chan, void *mssg)
|
| 275 | {
|
| 276 | int t;
|
| 277 |
|
| 278 | if (!chan || !chan->cl || mssg == MBOX_NO_MSG)
|
| 279 | return -EINVAL;
|
| 280 |
|
| 281 | t = add_to_rbuf(chan, mssg);
|
| 282 | if (t < 0) {
|
| 283 | dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
|
| 284 | return t;
|
| 285 | }
|
| 286 |
|
| 287 | msg_submit(chan);
|
| 288 |
|
| 289 | if (chan->cl->tx_block) {
|
| 290 | unsigned long wait;
|
| 291 | int ret;
|
| 292 |
|
| 293 | if (!chan->cl->tx_tout)
|
| 294 | wait = msecs_to_jiffies(3600000);
|
| 295 | else
|
| 296 | wait = msecs_to_jiffies(chan->cl->tx_tout);
|
| 297 |
|
| 298 | ret = wait_for_completion_timeout(&chan->tx_complete, wait);
|
| 299 | if (ret == 0) {
|
| 300 | t = -ETIME;
|
| 301 | tx_tick(chan, t);
|
| 302 | } else if (chan->tx_status < 0) {
|
| 303 | t = chan->tx_status;
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | return t;
|
| 308 | }
|
| 309 | EXPORT_SYMBOL_GPL(mbox_send_message);
|
| 310 |
|
| 311 | |
| 312 | * mbox_flush - flush a mailbox channel
|
| 313 | * @chan: mailbox channel to flush
|
| 314 | * @timeout: time, in milliseconds, to allow the flush operation to succeed
|
| 315 | *
|
| 316 | * Mailbox controllers that need to work in atomic context can implement the
|
| 317 | * ->flush() callback to busy loop until a transmission has been completed.
|
| 318 | * The implementation must call mbox_chan_txdone() upon success. Clients can
|
| 319 | * call the mbox_flush() function at any time after mbox_send_message() to
|
| 320 | * flush the transmission. After the function returns success, the mailbox
|
| 321 | * transmission is guaranteed to have completed.
|
| 322 | *
|
| 323 | * Returns: 0 on success or a negative error code on failure.
|
| 324 |
|
| 325 | int mbox_flush(struct mbox_chan *chan, unsigned long timeout)
|
| 326 | {
|
| 327 | int ret;
|
| 328 |
|
| 329 | if (!chan->mbox->ops->flush)
|
| 330 | return -ENOTSUPP;
|
| 331 |
|
| 332 | ret = chan->mbox->ops->flush(chan, timeout);
|
| 333 | if (ret < 0)
|
| 334 | tx_tick(chan, ret);
|
| 335 |
|
| 336 | return ret;
|
| 337 | }
|
| 338 | EXPORT_SYMBOL_GPL(mbox_flush);
|
| 339 |
|
| 340 | static void mbox_clean_and_put_channel(struct mbox_chan *chan)
|
| 341 | {
|
| 342 |
|
| 343 | scoped_guard(spinlock_irqsave, &chan->lock) {
|
| 344 | chan->cl = NULL;
|
| 345 | chan->active_req = MBOX_NO_MSG;
|
| 346 | if (chan->txdone_method == MBOX_TXDONE_BY_ACK)
|
| 347 | chan->txdone_method = MBOX_TXDONE_BY_POLL;
|
| 348 | }
|
| 349 |
|
| 350 | module_put(chan->mbox->dev->driver->owner);
|
| 351 | }
|
| 352 |
|
| 353 | static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
|
| 354 | {
|
| 355 | struct device *dev = cl->dev;
|
| 356 | int ret;
|
| 357 |
|
| 358 | if (chan->cl || !try_module_get(chan->mbox->dev->driver->owner)) {
|
| 359 | dev_err(dev, "%s: mailbox not free\n", __func__);
|
| 360 | return -EBUSY;
|
| 361 | }
|
| 362 |
|
| 363 | scoped_guard(spinlock_irqsave, &chan->lock) {
|
| 364 | chan->msg_free = 0;
|
| 365 | chan->msg_count = 0;
|
| 366 | chan->active_req = MBOX_NO_MSG;
|
| 367 | chan->cl = cl;
|
| 368 | init_completion(&chan->tx_complete);
|
| 369 |
|
| 370 | if (chan->txdone_method == MBOX_TXDONE_BY_POLL && cl->knows_txdone)
|
| 371 | chan->txdone_method = MBOX_TXDONE_BY_ACK;
|
| 372 | }
|
| 373 |
|
| 374 | if (chan->mbox->ops->startup) {
|
| 375 | ret = chan->mbox->ops->startup(chan);
|
| 376 | if (ret) {
|
| 377 | dev_err(dev, "Unable to startup the chan (%d)\n", ret);
|
| 378 | mbox_clean_and_put_channel(chan);
|
| 379 | return ret;
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | return 0;
|
| 384 | }
|
| 385 |
|
| 386 | |
| 387 | * mbox_bind_client - Bind client to a mailbox channel.
|
| 388 | * @chan: The mailbox channel to bind the client to.
|
| 389 | * @cl: Identity of the client requesting the channel.
|
| 390 | *
|
| 391 | * The Client specifies its requirements and capabilities while asking for
|
| 392 | * a mailbox channel. It can't be called from atomic context.
|
| 393 | * The channel is exclusively allocated and can't be used by another
|
| 394 | * client before the owner calls mbox_free_channel.
|
| 395 | * After assignment, any packet received on this channel will be
|
| 396 | * handed over to the client via the 'rx_callback'.
|
| 397 | * The framework holds reference to the client, so the mbox_client
|
| 398 | * structure shouldn't be modified until the mbox_free_channel returns.
|
| 399 | *
|
| 400 | * Return: 0 if the channel was assigned to the client successfully.
|
| 401 | * <0 for request failure.
|
| 402 |
|
| 403 | int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
|
| 404 | {
|
| 405 | guard(mutex)(&con_mutex);
|
| 406 |
|
| 407 | return __mbox_bind_client(chan, cl);
|
| 408 | }
|
| 409 | EXPORT_SYMBOL_GPL(mbox_bind_client);
|
| 410 |
|
| 411 | |
| 412 | * mbox_request_channel - Request a mailbox channel.
|
| 413 | * @cl: Identity of the client requesting the channel.
|
| 414 | * @index: Index of mailbox specifier in 'mboxes' property.
|
| 415 | *
|
| 416 | * The Client specifies its requirements and capabilities while asking for
|
| 417 | * a mailbox channel. It can't be called from atomic context.
|
| 418 | * The channel is exclusively allocated and can't be used by another
|
| 419 | * client before the owner calls mbox_free_channel.
|
| 420 | * After assignment, any packet received on this channel will be
|
| 421 | * handed over to the client via the 'rx_callback'.
|
| 422 | * The framework holds reference to the client, so the mbox_client
|
| 423 | * structure shouldn't be modified until the mbox_free_channel returns.
|
| 424 | *
|
| 425 | * Return: Pointer to the channel assigned to the client if successful.
|
| 426 | * ERR_PTR for request failure.
|
| 427 |
|
| 428 | struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
|
| 429 | {
|
| 430 | struct fwnode_reference_args fwspec;
|
| 431 | struct fwnode_handle *fwnode;
|
| 432 | struct mbox_controller *mbox;
|
| 433 | struct of_phandle_args spec;
|
| 434 | struct mbox_chan *chan;
|
| 435 | struct device *dev;
|
| 436 | unsigned int i;
|
| 437 | int ret;
|
| 438 |
|
| 439 | dev = cl->dev;
|
| 440 | if (!dev) {
|
| 441 | pr_debug("No owner device\n");
|
| 442 | return ERR_PTR(-ENODEV);
|
| 443 | }
|
| 444 |
|
| 445 | fwnode = dev_fwnode(dev);
|
| 446 | if (!fwnode) {
|
| 447 | dev_dbg(dev, "No owner fwnode\n");
|
| 448 | return ERR_PTR(-ENODEV);
|
| 449 | }
|
| 450 |
|
| 451 | ret = fwnode_property_get_reference_args(fwnode, "mboxes", "#mbox-cells",
|
| 452 | 0, index, &fwspec);
|
| 453 | if (ret) {
|
| 454 | dev_err(dev, "%s: can't parse \"%s\" property\n", __func__, "mboxes");
|
| 455 | return ERR_PTR(ret);
|
| 456 | }
|
| 457 |
|
| 458 | spec.np = to_of_node(fwspec.fwnode);
|
| 459 | spec.args_count = fwspec.nargs;
|
| 460 | for (i = 0; i < spec.args_count; i++)
|
| 461 | spec.args[i] = fwspec.args[i];
|
| 462 |
|
| 463 | scoped_guard(mutex, &con_mutex) {
|
| 464 | chan = ERR_PTR(-EPROBE_DEFER);
|
| 465 | list_for_each_entry(mbox, &mbox_cons, node) {
|
| 466 | if (device_match_fwnode(mbox->dev, fwspec.fwnode)) {
|
| 467 | if (mbox->fw_xlate) {
|
| 468 | chan = mbox->fw_xlate(mbox, &fwspec);
|
| 469 | if (!IS_ERR(chan))
|
| 470 | break;
|
| 471 | } else if (mbox->of_xlate) {
|
| 472 | chan = mbox->of_xlate(mbox, &spec);
|
| 473 | if (!IS_ERR(chan))
|
| 474 | break;
|
| 475 | }
|
| 476 | }
|
| 477 | }
|
| 478 |
|
| 479 | fwnode_handle_put(fwspec.fwnode);
|
| 480 |
|
| 481 | if (IS_ERR(chan))
|
| 482 | return chan;
|
| 483 |
|
| 484 | ret = __mbox_bind_client(chan, cl);
|
| 485 | if (ret)
|
| 486 | chan = ERR_PTR(ret);
|
| 487 | }
|
| 488 |
|
| 489 | return chan;
|
| 490 | }
|
| 491 | EXPORT_SYMBOL_GPL(mbox_request_channel);
|
| 492 |
|
| 493 | struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
|
| 494 | const char *name)
|
| 495 | {
|
| 496 | int index = device_property_match_string(cl->dev, "mbox-names", name);
|
| 497 |
|
| 498 | if (index < 0) {
|
| 499 | dev_err(cl->dev, "%s() could not locate channel named \"%s\"\n",
|
| 500 | __func__, name);
|
| 501 | return ERR_PTR(index);
|
| 502 | }
|
| 503 | return mbox_request_channel(cl, index);
|
| 504 | }
|
| 505 | EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
|
| 506 |
|
| 507 | |
| 508 | * mbox_free_channel - The client relinquishes control of a mailbox
|
| 509 | * channel by this call.
|
| 510 | * @chan: The mailbox channel to be freed.
|
| 511 |
|
| 512 | void mbox_free_channel(struct mbox_chan *chan)
|
| 513 | {
|
| 514 | if (!chan || !chan->cl)
|
| 515 | return;
|
| 516 |
|
| 517 | if (chan->mbox->ops->shutdown)
|
| 518 | chan->mbox->ops->shutdown(chan);
|
| 519 |
|
| 520 | mbox_clean_and_put_channel(chan);
|
| 521 | }
|
| 522 | EXPORT_SYMBOL_GPL(mbox_free_channel);
|
| 523 |
|
| 524 | static struct mbox_chan *fw_mbox_index_xlate(struct mbox_controller *mbox,
|
| 525 | const struct fwnode_reference_args *sp)
|
| 526 | {
|
| 527 | if (sp->nargs < 1 || sp->args[0] >= mbox->num_chans)
|
| 528 | return ERR_PTR(-EINVAL);
|
| 529 |
|
| 530 | return &mbox->chans[sp->args[0]];
|
| 531 | }
|
| 532 |
|
| 533 | |
| 534 | * mbox_controller_register - Register the mailbox controller
|
| 535 | * @mbox: Pointer to the mailbox controller.
|
| 536 | *
|
| 537 | * The controller driver registers its communication channels
|
| 538 |
|
| 539 | int mbox_controller_register(struct mbox_controller *mbox)
|
| 540 | {
|
| 541 | int i, txdone;
|
| 542 |
|
| 543 | if (!mbox || !mbox->dev || !mbox->ops || !mbox->chans || !mbox->num_chans)
|
| 544 | return -EINVAL;
|
| 545 |
|
| 546 | if (mbox->txdone_irq)
|
| 547 | txdone = MBOX_TXDONE_BY_IRQ;
|
| 548 | else if (mbox->txdone_poll)
|
| 549 | txdone = MBOX_TXDONE_BY_POLL;
|
| 550 | else
|
| 551 | txdone = MBOX_TXDONE_BY_ACK;
|
| 552 |
|
| 553 | if (txdone == MBOX_TXDONE_BY_POLL) {
|
| 554 |
|
| 555 | if (!mbox->ops->last_tx_done) {
|
| 556 | dev_err(mbox->dev, "last_tx_done method is absent\n");
|
| 557 | return -EINVAL;
|
| 558 | }
|
| 559 |
|
| 560 | hrtimer_setup(&mbox->poll_hrt, txdone_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
| 561 | spin_lock_init(&mbox->poll_hrt_lock);
|
| 562 | }
|
| 563 |
|
| 564 | for (i = 0; i < mbox->num_chans; i++) {
|
| 565 | struct mbox_chan *chan = &mbox->chans[i];
|
| 566 |
|
| 567 | chan->cl = NULL;
|
| 568 | chan->mbox = mbox;
|
| 569 | chan->active_req = MBOX_NO_MSG;
|
| 570 | chan->txdone_method = txdone;
|
| 571 | spin_lock_init(&chan->lock);
|
| 572 | }
|
| 573 |
|
| 574 | if (!mbox->fw_xlate && !mbox->of_xlate)
|
| 575 | mbox->fw_xlate = fw_mbox_index_xlate;
|
| 576 |
|
| 577 | scoped_guard(mutex, &con_mutex)
|
| 578 | list_add_tail(&mbox->node, &mbox_cons);
|
| 579 |
|
| 580 | return 0;
|
| 581 | }
|
| 582 | EXPORT_SYMBOL_GPL(mbox_controller_register);
|
| 583 |
|
| 584 | |
| 585 | * mbox_controller_unregister - Unregister the mailbox controller
|
| 586 | * @mbox: Pointer to the mailbox controller.
|
| 587 |
|
| 588 | void mbox_controller_unregister(struct mbox_controller *mbox)
|
| 589 | {
|
| 590 | int i;
|
| 591 |
|
| 592 | if (!mbox)
|
| 593 | return;
|
| 594 |
|
| 595 | scoped_guard(mutex, &con_mutex) {
|
| 596 | list_del(&mbox->node);
|
| 597 |
|
| 598 | for (i = 0; i < mbox->num_chans; i++)
|
| 599 | mbox_free_channel(&mbox->chans[i]);
|
| 600 |
|
| 601 | if (mbox->txdone_poll)
|
| 602 | hrtimer_cancel(&mbox->poll_hrt);
|
| 603 | }
|
| 604 | }
|
| 605 | EXPORT_SYMBOL_GPL(mbox_controller_unregister);
|
| 606 |
|
| 607 | static void __devm_mbox_controller_unregister(struct device *dev, void *res)
|
| 608 | {
|
| 609 | struct mbox_controller **mbox = res;
|
| 610 |
|
| 611 | mbox_controller_unregister(*mbox);
|
| 612 | }
|
| 613 |
|
| 614 | |
| 615 | * devm_mbox_controller_register() - managed mbox_controller_register()
|
| 616 | * @dev: device owning the mailbox controller being registered
|
| 617 | * @mbox: mailbox controller being registered
|
| 618 | *
|
| 619 | * This function adds a device-managed resource that will make sure that the
|
| 620 | * mailbox controller, which is registered using mbox_controller_register()
|
| 621 | * as part of this function, will be unregistered along with the rest of
|
| 622 | * device-managed resources upon driver probe failure or driver removal.
|
| 623 | *
|
| 624 | * Returns 0 on success or a negative error code on failure.
|
| 625 |
|
| 626 | int devm_mbox_controller_register(struct device *dev,
|
| 627 | struct mbox_controller *mbox)
|
| 628 | {
|
| 629 | struct mbox_controller **ptr;
|
| 630 | int err;
|
| 631 |
|
| 632 | ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
|
| 633 | GFP_KERNEL);
|
| 634 | if (!ptr)
|
| 635 | return -ENOMEM;
|
| 636 |
|
| 637 | err = mbox_controller_register(mbox);
|
| 638 | if (err < 0) {
|
| 639 | devres_free(ptr);
|
| 640 | return err;
|
| 641 | }
|
| 642 |
|
| 643 | devres_add(dev, ptr);
|
| 644 | *ptr = mbox;
|
| 645 |
|
| 646 | return 0;
|
| 647 | }
|
| 648 | EXPORT_SYMBOL_GPL(devm_mbox_controller_register);
|
| 649 |
|
| 650 | #ifdef CONFIG_DEBUG_FS
|
| 651 | static void *mbox_seq_start(struct seq_file *s, loff_t *pos)
|
| 652 | {
|
| 653 | mutex_lock(&con_mutex);
|
| 654 | return seq_list_start(&mbox_cons, *pos);
|
| 655 | }
|
| 656 |
|
| 657 | static void *mbox_seq_next(struct seq_file *s, void *v, loff_t *pos)
|
| 658 | {
|
| 659 | return seq_list_next(v, &mbox_cons, pos);
|
| 660 | }
|
| 661 |
|
| 662 | static void mbox_seq_stop(struct seq_file *s, void *v)
|
| 663 | {
|
| 664 | mutex_unlock(&con_mutex);
|
| 665 | }
|
| 666 |
|
| 667 | static int mbox_seq_show(struct seq_file *seq, void *v)
|
| 668 | {
|
| 669 | const struct mbox_controller *mbox = list_entry(v, struct mbox_controller, node);
|
| 670 |
|
| 671 | seq_printf(seq, "%s:\n", dev_name(mbox->dev));
|
| 672 |
|
| 673 | for (unsigned int i = 0; i < mbox->num_chans; i++) {
|
| 674 | struct mbox_chan *chan = &mbox->chans[i];
|
| 675 |
|
| 676 | scoped_guard(spinlock_irqsave, &chan->lock) {
|
| 677 | if (chan->cl) {
|
| 678 | struct device *cl_dev = chan->cl->dev;
|
| 679 |
|
| 680 | seq_printf(seq, " %3u: %s\n", i,
|
| 681 | cl_dev ? dev_name(cl_dev) : "NULL device");
|
| 682 | }
|
| 683 | }
|
| 684 | }
|
| 685 |
|
| 686 | return 0;
|
| 687 | }
|
| 688 |
|
| 689 | static const struct seq_operations mbox_sops = {
|
| 690 | .start = mbox_seq_start,
|
| 691 | .next = mbox_seq_next,
|
| 692 | .stop = mbox_seq_stop,
|
| 693 | .show = mbox_seq_show,
|
| 694 | };
|
| 695 | DEFINE_SEQ_ATTRIBUTE(mbox);
|
| 696 |
|
| 697 | |
| 698 | * subsys_initcall() is used here but controllers may already have been
|
| 699 | * registered earlier or will be later. The rationale is that debugfs is
|
| 700 | * accessed only late, i.e. from userspace. So, files created here must make no
|
| 701 | * assumptions about initcall ordering.
|
| 702 |
|
| 703 | static int __init mbox_init(void)
|
| 704 | {
|
| 705 | struct dentry *mbox_debugfs = debugfs_create_dir("mailbox", NULL);
|
| 706 |
|
| 707 | debugfs_create_file("mailbox_summary", 0444, mbox_debugfs, NULL, &mbox_fops);
|
| 708 | return 0;
|
| 709 | }
|
| 710 | subsys_initcall(mbox_init);
|
| 711 | #endif
|