]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/arm/plat-omap/mailbox.c
Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / arch / arm / plat-omap / mailbox.c
1 /*
2 * OMAP mailbox driver
3 *
4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
5 *
6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/mutex.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/kfifo.h>
30 #include <linux/err.h>
31 #include <linux/notifier.h>
32
33 #include <plat/mailbox.h>
34
35 static struct omap_mbox **mboxes;
36
37 static int mbox_configured;
38 static DEFINE_MUTEX(mbox_configured_lock);
39
40 static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
41 module_param(mbox_kfifo_size, uint, S_IRUGO);
42 MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
43
44 /* Mailbox FIFO handle functions */
45 static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
46 {
47 return mbox->ops->fifo_read(mbox);
48 }
49 static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
50 {
51 mbox->ops->fifo_write(mbox, msg);
52 }
53 static inline int mbox_fifo_empty(struct omap_mbox *mbox)
54 {
55 return mbox->ops->fifo_empty(mbox);
56 }
57 static inline int mbox_fifo_full(struct omap_mbox *mbox)
58 {
59 return mbox->ops->fifo_full(mbox);
60 }
61
62 /* Mailbox IRQ handle functions */
63 static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
64 {
65 if (mbox->ops->ack_irq)
66 mbox->ops->ack_irq(mbox, irq);
67 }
68 static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
69 {
70 return mbox->ops->is_irq(mbox, irq);
71 }
72
73 /*
74 * message sender
75 */
76 static int __mbox_poll_for_space(struct omap_mbox *mbox)
77 {
78 int ret = 0, i = 1000;
79
80 while (mbox_fifo_full(mbox)) {
81 if (mbox->ops->type == OMAP_MBOX_TYPE2)
82 return -1;
83 if (--i == 0)
84 return -1;
85 udelay(1);
86 }
87 return ret;
88 }
89
90 int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
91 {
92 struct omap_mbox_queue *mq = mbox->txq;
93 int ret = 0, len;
94
95 spin_lock_bh(&mq->lock);
96
97 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
98 ret = -ENOMEM;
99 goto out;
100 }
101
102 if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
103 mbox_fifo_write(mbox, msg);
104 goto out;
105 }
106
107 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
108 WARN_ON(len != sizeof(msg));
109
110 tasklet_schedule(&mbox->txq->tasklet);
111
112 out:
113 spin_unlock_bh(&mq->lock);
114 return ret;
115 }
116 EXPORT_SYMBOL(omap_mbox_msg_send);
117
118 static void mbox_tx_tasklet(unsigned long tx_data)
119 {
120 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
121 struct omap_mbox_queue *mq = mbox->txq;
122 mbox_msg_t msg;
123 int ret;
124
125 while (kfifo_len(&mq->fifo)) {
126 if (__mbox_poll_for_space(mbox)) {
127 omap_mbox_enable_irq(mbox, IRQ_TX);
128 break;
129 }
130
131 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
132 sizeof(msg));
133 WARN_ON(ret != sizeof(msg));
134
135 mbox_fifo_write(mbox, msg);
136 }
137 }
138
139 /*
140 * Message receiver(workqueue)
141 */
142 static void mbox_rx_work(struct work_struct *work)
143 {
144 struct omap_mbox_queue *mq =
145 container_of(work, struct omap_mbox_queue, work);
146 mbox_msg_t msg;
147 int len;
148
149 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
150 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
151 WARN_ON(len != sizeof(msg));
152
153 blocking_notifier_call_chain(&mq->mbox->notifier, len,
154 (void *)msg);
155 spin_lock_irq(&mq->lock);
156 if (mq->full) {
157 mq->full = false;
158 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
159 }
160 spin_unlock_irq(&mq->lock);
161 }
162 }
163
164 /*
165 * Mailbox interrupt handler
166 */
167 static void __mbox_tx_interrupt(struct omap_mbox *mbox)
168 {
169 omap_mbox_disable_irq(mbox, IRQ_TX);
170 ack_mbox_irq(mbox, IRQ_TX);
171 tasklet_schedule(&mbox->txq->tasklet);
172 }
173
174 static void __mbox_rx_interrupt(struct omap_mbox *mbox)
175 {
176 struct omap_mbox_queue *mq = mbox->rxq;
177 mbox_msg_t msg;
178 int len;
179
180 while (!mbox_fifo_empty(mbox)) {
181 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
182 omap_mbox_disable_irq(mbox, IRQ_RX);
183 mq->full = true;
184 goto nomem;
185 }
186
187 msg = mbox_fifo_read(mbox);
188
189 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
190 WARN_ON(len != sizeof(msg));
191
192 if (mbox->ops->type == OMAP_MBOX_TYPE1)
193 break;
194 }
195
196 /* no more messages in the fifo. clear IRQ source. */
197 ack_mbox_irq(mbox, IRQ_RX);
198 nomem:
199 schedule_work(&mbox->rxq->work);
200 }
201
202 static irqreturn_t mbox_interrupt(int irq, void *p)
203 {
204 struct omap_mbox *mbox = p;
205
206 if (is_mbox_irq(mbox, IRQ_TX))
207 __mbox_tx_interrupt(mbox);
208
209 if (is_mbox_irq(mbox, IRQ_RX))
210 __mbox_rx_interrupt(mbox);
211
212 return IRQ_HANDLED;
213 }
214
215 static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
216 void (*work) (struct work_struct *),
217 void (*tasklet)(unsigned long))
218 {
219 struct omap_mbox_queue *mq;
220
221 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
222 if (!mq)
223 return NULL;
224
225 spin_lock_init(&mq->lock);
226
227 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
228 goto error;
229
230 if (work)
231 INIT_WORK(&mq->work, work);
232
233 if (tasklet)
234 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
235 return mq;
236 error:
237 kfree(mq);
238 return NULL;
239 }
240
241 static void mbox_queue_free(struct omap_mbox_queue *q)
242 {
243 kfifo_free(&q->fifo);
244 kfree(q);
245 }
246
247 static int omap_mbox_startup(struct omap_mbox *mbox)
248 {
249 int ret = 0;
250 struct omap_mbox_queue *mq;
251
252 mutex_lock(&mbox_configured_lock);
253 if (!mbox_configured++) {
254 if (likely(mbox->ops->startup)) {
255 ret = mbox->ops->startup(mbox);
256 if (unlikely(ret))
257 goto fail_startup;
258 } else
259 goto fail_startup;
260 }
261
262 if (!mbox->use_count++) {
263 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
264 mbox->name, mbox);
265 if (unlikely(ret)) {
266 pr_err("failed to register mailbox interrupt:%d\n",
267 ret);
268 goto fail_request_irq;
269 }
270 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
271 if (!mq) {
272 ret = -ENOMEM;
273 goto fail_alloc_txq;
274 }
275 mbox->txq = mq;
276
277 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
278 if (!mq) {
279 ret = -ENOMEM;
280 goto fail_alloc_rxq;
281 }
282 mbox->rxq = mq;
283 mq->mbox = mbox;
284 }
285 mutex_unlock(&mbox_configured_lock);
286 return 0;
287
288 fail_alloc_rxq:
289 mbox_queue_free(mbox->txq);
290 fail_alloc_txq:
291 free_irq(mbox->irq, mbox);
292 fail_request_irq:
293 if (mbox->ops->shutdown)
294 mbox->ops->shutdown(mbox);
295 mbox->use_count--;
296 fail_startup:
297 mbox_configured--;
298 mutex_unlock(&mbox_configured_lock);
299 return ret;
300 }
301
302 static void omap_mbox_fini(struct omap_mbox *mbox)
303 {
304 mutex_lock(&mbox_configured_lock);
305
306 if (!--mbox->use_count) {
307 free_irq(mbox->irq, mbox);
308 tasklet_kill(&mbox->txq->tasklet);
309 flush_work_sync(&mbox->rxq->work);
310 mbox_queue_free(mbox->txq);
311 mbox_queue_free(mbox->rxq);
312 }
313
314 if (likely(mbox->ops->shutdown)) {
315 if (!--mbox_configured)
316 mbox->ops->shutdown(mbox);
317 }
318
319 mutex_unlock(&mbox_configured_lock);
320 }
321
322 struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
323 {
324 struct omap_mbox *_mbox, *mbox = NULL;
325 int i, ret;
326
327 if (!mboxes)
328 return ERR_PTR(-EINVAL);
329
330 for (i = 0; (_mbox = mboxes[i]); i++) {
331 if (!strcmp(_mbox->name, name)) {
332 mbox = _mbox;
333 break;
334 }
335 }
336
337 if (!mbox)
338 return ERR_PTR(-ENOENT);
339
340 ret = omap_mbox_startup(mbox);
341 if (ret)
342 return ERR_PTR(-ENODEV);
343
344 if (nb)
345 blocking_notifier_chain_register(&mbox->notifier, nb);
346
347 return mbox;
348 }
349 EXPORT_SYMBOL(omap_mbox_get);
350
351 void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
352 {
353 blocking_notifier_chain_unregister(&mbox->notifier, nb);
354 omap_mbox_fini(mbox);
355 }
356 EXPORT_SYMBOL(omap_mbox_put);
357
358 static struct class omap_mbox_class = { .name = "mbox", };
359
360 int omap_mbox_register(struct device *parent, struct omap_mbox **list)
361 {
362 int ret;
363 int i;
364
365 mboxes = list;
366 if (!mboxes)
367 return -EINVAL;
368
369 for (i = 0; mboxes[i]; i++) {
370 struct omap_mbox *mbox = mboxes[i];
371 mbox->dev = device_create(&omap_mbox_class,
372 parent, 0, mbox, "%s", mbox->name);
373 if (IS_ERR(mbox->dev)) {
374 ret = PTR_ERR(mbox->dev);
375 goto err_out;
376 }
377
378 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
379 }
380 return 0;
381
382 err_out:
383 while (i--)
384 device_unregister(mboxes[i]->dev);
385 return ret;
386 }
387 EXPORT_SYMBOL(omap_mbox_register);
388
389 int omap_mbox_unregister(void)
390 {
391 int i;
392
393 if (!mboxes)
394 return -EINVAL;
395
396 for (i = 0; mboxes[i]; i++)
397 device_unregister(mboxes[i]->dev);
398 mboxes = NULL;
399 return 0;
400 }
401 EXPORT_SYMBOL(omap_mbox_unregister);
402
403 static int __init omap_mbox_init(void)
404 {
405 int err;
406
407 err = class_register(&omap_mbox_class);
408 if (err)
409 return err;
410
411 /* kfifo size sanity check: alignment and minimal size */
412 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
413 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
414 sizeof(mbox_msg_t));
415
416 return 0;
417 }
418 subsys_initcall(omap_mbox_init);
419
420 static void __exit omap_mbox_exit(void)
421 {
422 class_unregister(&omap_mbox_class);
423 }
424 module_exit(omap_mbox_exit);
425
426 MODULE_LICENSE("GPL v2");
427 MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
428 MODULE_AUTHOR("Toshihiro Kobayashi");
429 MODULE_AUTHOR("Hiroshi DOYU");