-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathvcio.c
484 lines (414 loc) · 11.7 KB
/
vcio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
148
149
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
181
182
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
269
270
271
272
273
274
275
276
277
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
362
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
423
424
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
455
456
457
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
/*
* linux/arch/arm/mach-bcm2708/vcio.c
*
* Copyright (C) 2010 Broadcom
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This device provides a shared mechanism for writing to the mailboxes,
* semaphores, doorbells etc. that are shared between the ARM and the
* VideoCore processor
*/
#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif
#include <linux/module.h>
#include <linux/console.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/sysrq.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <mach/vcio.h>
#include <mach/platform.h>
#include <asm/uaccess.h>
#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
/* ----------------------------------------------------------------------
* Mailbox
* -------------------------------------------------------------------- */
/* offsets from a mail box base address */
#define MAIL_WRT 0x00 /* write - and next 4 words */
#define MAIL_RD 0x00 /* read - and next 4 words */
#define MAIL_POL 0x10 /* read without popping the fifo */
#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
#define MAIL_STA 0x18 /* status */
#define MAIL_CNF 0x1C /* configuration */
#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
#define MBOX_CHAN(msg) ((msg) & 0xf)
#define MBOX_DATA28(msg) ((msg) & ~0xf)
#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
#define MBOX_MAGIC 0xd0d0c0de
static struct class *vcio_class = NULL;
struct vc_mailbox {
struct device *dev; /* parent device */
void __iomem *status;
void __iomem *config;
void __iomem *read;
void __iomem *write;
uint32_t msg[MBOX_CHAN_COUNT];
struct semaphore sema[MBOX_CHAN_COUNT];
uint32_t magic;
};
static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
uint32_t addr_mbox)
{
int i;
mbox_out->dev = dev;
mbox_out->status = __io_address(addr_mbox + MAIL_STA);
mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
mbox_out->read = __io_address(addr_mbox + MAIL_RD);
/* Write to the other mailbox */
mbox_out->write =
__io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
MAIL_WRT);
for (i = 0; i < MBOX_CHAN_COUNT; i++) {
mbox_out->msg[i] = 0;
sema_init(&mbox_out->sema[i], 0);
}
/* Enable the interrupt on data reception */
writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
mbox_out->magic = MBOX_MAGIC;
}
static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
{
int rc;
if (mbox->magic != MBOX_MAGIC)
rc = -EINVAL;
else {
/* wait for the mailbox FIFO to have some space in it */
while (0 != (readl(mbox->status) & ARM_MS_FULL))
cpu_relax();
writel(MBOX_MSG(chan, data28), mbox->write);
rc = 0;
}
return rc;
}
static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
{
int rc;
if (mbox->magic != MBOX_MAGIC)
rc = -EINVAL;
else {
down(&mbox->sema[chan]);
*data28 = MBOX_DATA28(mbox->msg[chan]);
mbox->msg[chan] = 0;
rc = 0;
}
return rc;
}
static irqreturn_t mbox_irq(int irq, void *dev_id)
{
/* wait for the mailbox FIFO to have some data in it */
struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
int status = readl(mbox->status);
int ret = IRQ_NONE;
while (!(status & ARM_MS_EMPTY)) {
uint32_t msg = readl(mbox->read);
int chan = MBOX_CHAN(msg);
if (chan < MBOX_CHAN_COUNT) {
if (mbox->msg[chan]) {
/* Overflow */
printk(KERN_ERR DRIVER_NAME
": mbox chan %d overflow - drop %08x\n",
chan, msg);
} else {
mbox->msg[chan] = (msg | 0xf);
up(&mbox->sema[chan]);
}
} else {
printk(KERN_ERR DRIVER_NAME
": invalid channel selector (msg %08x)\n", msg);
}
ret = IRQ_HANDLED;
status = readl(mbox->status);
}
return ret;
}
static struct irqaction mbox_irqaction = {
.name = "ARM Mailbox IRQ",
.flags = IRQF_DISABLED | IRQF_IRQPOLL,
.handler = mbox_irq,
};
/* ----------------------------------------------------------------------
* Mailbox Methods
* -------------------------------------------------------------------- */
static struct device *mbox_dev; /* we assume there's only one! */
static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
{
int rc;
struct vc_mailbox *mailbox = dev_get_drvdata(dev);
device_lock(dev);
rc = mbox_write(mailbox, chan, data28);
device_unlock(dev);
return rc;
}
static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
{
int rc;
struct vc_mailbox *mailbox = dev_get_drvdata(dev);
device_lock(dev);
rc = mbox_read(mailbox, chan, data28);
device_unlock(dev);
return rc;
}
extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
{
if (mbox_dev)
return dev_mbox_write(mbox_dev, chan, data28);
else
return -ENODEV;
}
EXPORT_SYMBOL_GPL(bcm_mailbox_write);
extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
{
if (mbox_dev)
return dev_mbox_read(mbox_dev, chan, data28);
else
return -ENODEV;
}
EXPORT_SYMBOL_GPL(bcm_mailbox_read);
static void dev_mbox_register(const char *dev_name, struct device *dev)
{
mbox_dev = dev;
}
static int mbox_copy_from_user(void *dst, const void *src, int size)
{
if ( (uint32_t)src < TASK_SIZE)
{
return copy_from_user(dst, src, size);
}
else
{
memcpy( dst, src, size );
return 0;
}
}
static int mbox_copy_to_user(void *dst, const void *src, int size)
{
if ( (uint32_t)dst < TASK_SIZE)
{
return copy_to_user(dst, src, size);
}
else
{
memcpy( dst, src, size );
return 0;
}
}
static DEFINE_MUTEX(mailbox_lock);
extern int bcm_mailbox_property(void *data, int size)
{
uint32_t success;
dma_addr_t mem_bus; /* the memory address accessed from videocore */
void *mem_kern; /* the memory address accessed from driver */
int s = 0;
mutex_lock(&mailbox_lock);
/* allocate some memory for the messages communicating with GPU */
mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
if (mem_kern) {
/* create the message */
mbox_copy_from_user(mem_kern, data, size);
/* send the message */
wmb();
s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
if (s == 0) {
s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
}
if (s == 0) {
/* copy the response */
rmb();
mbox_copy_to_user(data, mem_kern, size);
}
dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
} else {
s = -ENOMEM;
}
if (s != 0)
printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
mutex_unlock(&mailbox_lock);
return s;
}
EXPORT_SYMBOL_GPL(bcm_mailbox_property);
/* ----------------------------------------------------------------------
* Platform Device for Mailbox
* -------------------------------------------------------------------- */
/*
* Is the device open right now? Used to prevent
* concurent access into the same device
*/
static int Device_Open = 0;
/*
* This is called whenever a process attempts to open the device file
*/
static int device_open(struct inode *inode, struct file *file)
{
/*
* We don't want to talk to two processes at the same time
*/
if (Device_Open)
return -EBUSY;
Device_Open++;
/*
* Initialize the message
*/
try_module_get(THIS_MODULE);
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
/*
* We're now ready for our next caller
*/
Device_Open--;
module_put(THIS_MODULE);
return 0;
}
/*
* This function is called whenever a process tries to do an ioctl on our
* device file. We get two extra parameters (additional to the inode and file
* structures, which all device functions get): the number of the ioctl called
* and the parameter given to the ioctl function.
*
* If the ioctl is write or read/write (meaning output is returned to the
* calling process), the ioctl call returns the output of this function.
*
*/
static long device_ioctl(struct file *file, /* see include/linux/fs.h */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param)
{
unsigned size;
/*
* Switch according to the ioctl called
*/
switch (ioctl_num) {
case IOCTL_MBOX_PROPERTY:
/*
* Receive a pointer to a message (in user space) and set that
* to be the device's message. Get the parameter given to
* ioctl by the process.
*/
mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
return bcm_mailbox_property((void *)ioctl_param, size);
break;
default:
printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
return -EINVAL;
}
return 0;
}
/* Module Declarations */
/*
* This structure will hold the functions to be called
* when a process does something to the device we
* created. Since a pointer to this structure is kept in
* the devices table, it can't be local to
* init_module. NULL is for unimplemented functios.
*/
struct file_operations fops = {
.unlocked_ioctl = device_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
};
static int bcm_vcio_probe(struct platform_device *pdev)
{
int ret = 0;
struct vc_mailbox *mailbox;
mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
if (NULL == mailbox) {
printk(KERN_ERR DRIVER_NAME ": failed to allocate "
"mailbox memory\n");
ret = -ENOMEM;
} else {
struct resource *res;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
"resource\n");
ret = -ENODEV;
kfree(mailbox);
} else {
/* should be based on the registers from res really */
mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
platform_set_drvdata(pdev, mailbox);
dev_mbox_register(DRIVER_NAME, &pdev->dev);
mbox_irqaction.dev_id = mailbox;
setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
__io_address(ARM_0_MAIL0_RD));
}
}
if (ret == 0) {
/*
* Register the character device
*/
ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
/*
* Negative values signify an error
*/
if (ret < 0) {
printk(KERN_ERR DRIVER_NAME
"Failed registering the character device %d\n", ret);
return ret;
}
vcio_class = class_create(THIS_MODULE, BCM_VCIO_DRIVER_NAME);
if (IS_ERR(vcio_class)) {
ret = PTR_ERR(vcio_class);
return ret ;
}
device_create(vcio_class, NULL, MKDEV(MAJOR_NUM, 0), NULL,
"vcio");
}
return ret;
}
static int bcm_vcio_remove(struct platform_device *pdev)
{
struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
kfree(mailbox);
return 0;
}
static struct platform_driver bcm_mbox_driver = {
.probe = bcm_vcio_probe,
.remove = bcm_vcio_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
},
};
static int __init bcm_mbox_init(void)
{
int ret;
printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
ret = platform_driver_register(&bcm_mbox_driver);
if (ret != 0) {
printk(KERN_ERR DRIVER_NAME ": failed to register "
"on platform\n");
}
return ret;
}
static void __exit bcm_mbox_exit(void)
{
device_destroy(vcio_class,MKDEV(MAJOR_NUM, 0));
class_destroy(vcio_class);
unregister_chrdev(MAJOR_NUM, DEVICE_FILE_NAME);
platform_driver_unregister(&bcm_mbox_driver);
}
arch_initcall(bcm_mbox_init); /* Initialize early */
module_exit(bcm_mbox_exit);
MODULE_AUTHOR("Gray Girling");
MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:bcm-mbox");