aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/peripheral-loader.c
blob: 17f2835fa50f4a2f206918400fc85e2105b88b5e (plain)
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
485
486
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
588
589
590
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/firmware.h>
#include <linux/io.h>
#include <linux/debugfs.h>
#include <linux/elf.h>
#include <linux/mutex.h>
#include <linux/memblock.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/suspend.h>
#include <linux/rwsem.h>
#include <linux/sysfs.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <linux/wakelock.h>

#include <asm/uaccess.h>
#include <asm/setup.h>
#include <mach/peripheral-loader.h>

#include "peripheral-loader.h"

/**
 * proxy_timeout - Override for proxy vote timeouts
 * -1: Use driver-specified timeout
 *  0: Hold proxy votes until shutdown
 * >0: Specify a custom timeout in ms
 */
static int proxy_timeout_ms = -1;
module_param(proxy_timeout_ms, int, S_IRUGO | S_IWUSR);

enum pil_state {
	PIL_OFFLINE,
	PIL_ONLINE,
};

static const char *pil_states[] = {
	[PIL_OFFLINE] = "OFFLINE",
	[PIL_ONLINE] = "ONLINE",
};

struct pil_device {
	struct pil_desc *desc;
	int count;
	enum pil_state state;
	struct mutex lock;
	struct device dev;
	struct module *owner;
#ifdef CONFIG_DEBUG_FS
	struct dentry *dentry;
#endif
	struct delayed_work proxy;
	struct wake_lock wlock;
	char wake_name[32];
};

#define to_pil_device(d) container_of(d, struct pil_device, dev)

static ssize_t name_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name);
}

static ssize_t state_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	enum pil_state state = to_pil_device(dev)->state;
	return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]);
}

static struct device_attribute pil_attrs[] = {
	__ATTR_RO(name),
	__ATTR_RO(state),
	{ },
};

struct bus_type pil_bus_type = {
	.name		= "pil",
	.dev_attrs	= pil_attrs,
};

static int __find_peripheral(struct device *dev, void *data)
{
	struct pil_device *pdev = to_pil_device(dev);
	return !strncmp(pdev->desc->name, data, INT_MAX);
}

static struct pil_device *find_peripheral(const char *str)
{
	struct device *dev;

	if (!str)
		return NULL;

	dev = bus_find_device(&pil_bus_type, NULL, (void *)str,
			__find_peripheral);
	return dev ? to_pil_device(dev) : NULL;
}

static void pil_proxy_work(struct work_struct *work)
{
	struct pil_device *pil;

	pil = container_of(work, struct pil_device, proxy.work);
	pil->desc->ops->proxy_unvote(pil->desc);
	wake_unlock(&pil->wlock);
}

static int pil_proxy_vote(struct pil_device *pil)
{
	int ret = 0;

	if (pil->desc->ops->proxy_vote) {
		wake_lock(&pil->wlock);
		ret = pil->desc->ops->proxy_vote(pil->desc);
		if (ret)
			wake_unlock(&pil->wlock);
	}
	return ret;
}

static void pil_proxy_unvote(struct pil_device *pil, unsigned long timeout)
{
	if (proxy_timeout_ms >= 0)
		timeout = proxy_timeout_ms;

	if (timeout && pil->desc->ops->proxy_unvote)
		schedule_delayed_work(&pil->proxy, msecs_to_jiffies(timeout));
}

#define IOMAP_SIZE SZ_4M

static int load_segment(const struct elf32_phdr *phdr, unsigned num,
		struct pil_device *pil)
{
	int ret = 0, count, paddr;
	char fw_name[30];
	const struct firmware *fw = NULL;
	const u8 *data;

	if (memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
		dev_err(&pil->dev, "%s: kernel memory would be overwritten "
			"[%#08lx, %#08lx)\n", pil->desc->name,
			(unsigned long)phdr->p_paddr,
			(unsigned long)(phdr->p_paddr + phdr->p_memsz));
		return -EPERM;
	}

	if (phdr->p_filesz) {
		snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
				pil->desc->name, num);
		ret = request_firmware(&fw, fw_name, &pil->dev);
		if (ret) {
			dev_err(&pil->dev, "%s: Failed to locate blob %s\n",
					pil->desc->name, fw_name);
			return ret;
		}

		if (fw->size != phdr->p_filesz) {
			dev_err(&pil->dev, "%s: Blob size %u doesn't match "
					"%u\n", pil->desc->name, fw->size,
					phdr->p_filesz);
			ret = -EPERM;
			goto release_fw;
		}
	}

	/* Load the segment into memory */
	count = phdr->p_filesz;
	paddr = phdr->p_paddr;
	data = fw ? fw->data : NULL;
	while (count > 0) {
		int size;
		u8 __iomem *buf;

		size = min_t(size_t, IOMAP_SIZE, count);
		buf = ioremap(paddr, size);
		if (!buf) {
			dev_err(&pil->dev, "%s: Failed to map memory\n",
					pil->desc->name);
			ret = -ENOMEM;
			goto release_fw;
		}
		memcpy(buf, data, size);
		iounmap(buf);

		count -= size;
		paddr += size;
		data += size;
	}

	/* Zero out trailing memory */
	count = phdr->p_memsz - phdr->p_filesz;
	while (count > 0) {
		int size;
		u8 __iomem *buf;

		size = min_t(size_t, IOMAP_SIZE, count);
		buf = ioremap(paddr, size);
		if (!buf) {
			dev_err(&pil->dev, "%s: Failed to map memory\n",
					pil->desc->name);
			ret = -ENOMEM;
			goto release_fw;
		}
		memset(buf, 0, size);
		iounmap(buf);

		count -= size;
		paddr += size;
	}

	if (pil->desc->ops->verify_blob) {
		ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
					  phdr->p_memsz);
		if (ret)
			dev_err(&pil->dev, "%s: Blob%u failed verification\n",
				pil->desc->name, num);
	}

release_fw:
	release_firmware(fw);
	return ret;
}

#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))

static int segment_is_loadable(const struct elf32_phdr *p)
{
	return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags);
}

/* Sychronize request_firmware() with suspend */
static DECLARE_RWSEM(pil_pm_rwsem);

static int load_image(struct pil_device *pil)
{
	int i, ret;
	char fw_name[30];
	struct elf32_hdr *ehdr;
	const struct elf32_phdr *phdr;
	const struct firmware *fw;
	unsigned long proxy_timeout = pil->desc->proxy_timeout;

	down_read(&pil_pm_rwsem);
	snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
	ret = request_firmware(&fw, fw_name, &pil->dev);
	if (ret) {
		dev_err(&pil->dev, "%s: Failed to locate %s\n",
				pil->desc->name, fw_name);
		goto out;
	}

	if (fw->size < sizeof(*ehdr)) {
		dev_err(&pil->dev, "%s: Not big enough to be an elf header\n",
				pil->desc->name);
		ret = -EIO;
		goto release_fw;
	}

	ehdr = (struct elf32_hdr *)fw->data;
	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
		dev_err(&pil->dev, "%s: Not an elf header\n", pil->desc->name);
		ret = -EIO;
		goto release_fw;
	}

	if (ehdr->e_phnum == 0) {
		dev_err(&pil->dev, "%s: No loadable segments\n",
				pil->desc->name);
		ret = -EIO;
		goto release_fw;
	}
	if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
	    sizeof(struct elf32_hdr) > fw->size) {
		dev_err(&pil->dev, "%s: Program headers not within mdt\n",
				pil->desc->name);
		ret = -EIO;
		goto release_fw;
	}

	ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
	if (ret) {
		dev_err(&pil->dev, "%s: Invalid firmware metadata\n",
				pil->desc->name);
		goto release_fw;
	}

	phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
		if (!segment_is_loadable(phdr))
			continue;

		ret = load_segment(phdr, i, pil);
		if (ret) {
			dev_err(&pil->dev, "%s: Failed to load segment %d\n",
					pil->desc->name, i);
			goto release_fw;
		}
	}

	ret = pil_proxy_vote(pil);
	if (ret) {
		dev_err(&pil->dev, "%s: Failed to proxy vote\n",
					pil->desc->name);
		goto release_fw;
	}

	ret = pil->desc->ops->auth_and_reset(pil->desc);
	if (ret) {
		dev_err(&pil->dev, "%s: Failed to bring out of reset\n",
				pil->desc->name);
		proxy_timeout = 0; /* Remove proxy vote immediately on error */
		goto err_boot;
	}
	dev_info(&pil->dev, "%s: Brought out of reset\n", pil->desc->name);
err_boot:
	pil_proxy_unvote(pil, proxy_timeout);
release_fw:
	release_firmware(fw);
out:
	up_read(&pil_pm_rwsem);
	return ret;
}

static void pil_set_state(struct pil_device *pil, enum pil_state state)
{
	if (pil->state != state) {
		pil->state = state;
		sysfs_notify(&pil->dev.kobj, NULL, "state");
	}
}

/**
 * pil_get() - Load a peripheral into memory and take it out of reset
 * @name: pointer to a string containing the name of the peripheral to load
 *
 * This function returns a pointer if it succeeds. If an error occurs an
 * ERR_PTR is returned.
 *
 * If PIL is not enabled in the kernel, the value %NULL will be returned.
 */
void *pil_get(const char *name)
{
	int ret;
	struct pil_device *pil;
	struct pil_device *pil_d;
	void *retval;

	if (!name)
		return NULL;

	pil = retval = find_peripheral(name);
	if (!pil)
		return ERR_PTR(-ENODEV);
	if (!try_module_get(pil->owner)) {
		put_device(&pil->dev);
		return ERR_PTR(-ENODEV);
	}

	pil_d = pil_get(pil->desc->depends_on);
	if (IS_ERR(pil_d)) {
		retval = pil_d;
		goto err_depends;
	}

	mutex_lock(&pil->lock);
	if (!pil->count) {
		ret = load_image(pil);
		if (ret) {
			retval = ERR_PTR(ret);
			goto err_load;
		}
	}
	pil->count++;
	pil_set_state(pil, PIL_ONLINE);
	mutex_unlock(&pil->lock);
out:
	return retval;
err_load:
	mutex_unlock(&pil->lock);
	pil_put(pil_d);
err_depends:
	put_device(&pil->dev);
	module_put(pil->owner);
	goto out;
}
EXPORT_SYMBOL(pil_get);

static void pil_shutdown(struct pil_device *pil)
{
	pil->desc->ops->shutdown(pil->desc);
	if (proxy_timeout_ms == 0 && pil->desc->ops->proxy_unvote)
		pil->desc->ops->proxy_unvote(pil->desc);
	else
		flush_delayed_work(&pil->proxy);

	pil_set_state(pil, PIL_OFFLINE);
}

/**
 * pil_put() - Inform PIL the peripheral no longer needs to be active
 * @peripheral_handle: pointer from a previous call to pil_get()
 *
 * This doesn't imply that a peripheral is shutdown or in reset since another
 * driver could be using the peripheral.
 */
void pil_put(void *peripheral_handle)
{
	struct pil_device *pil_d, *pil = peripheral_handle;

	if (IS_ERR_OR_NULL(pil))
		return;

	mutex_lock(&pil->lock);
	if (WARN(!pil->count, "%s: %s: Reference count mismatch\n",
			pil->desc->name, __func__))
		goto err_out;
	if (!--pil->count)
		pil_shutdown(pil);
	mutex_unlock(&pil->lock);

	pil_d = find_peripheral(pil->desc->depends_on);
	module_put(pil->owner);
	if (pil_d) {
		pil_put(pil_d);
		put_device(&pil_d->dev);
	}
	put_device(&pil->dev);
	return;
err_out:
	mutex_unlock(&pil->lock);
	return;
}
EXPORT_SYMBOL(pil_put);

void pil_force_shutdown(const char *name)
{
	struct pil_device *pil;

	pil = find_peripheral(name);
	if (!pil) {
		pr_err("%s: Couldn't find %s\n", __func__, name);
		return;
	}

	mutex_lock(&pil->lock);
	if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
			pil->desc->name, __func__))
		pil_shutdown(pil);
	mutex_unlock(&pil->lock);

	put_device(&pil->dev);
}
EXPORT_SYMBOL(pil_force_shutdown);

int pil_force_boot(const char *name)
{
	int ret = -EINVAL;
	struct pil_device *pil;

	pil = find_peripheral(name);
	if (!pil) {
		pr_err("%s: Couldn't find %s\n", __func__, name);
		return -EINVAL;
	}

	mutex_lock(&pil->lock);
	if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
			pil->desc->name, __func__))
		ret = load_image(pil);
	if (!ret)
		pil_set_state(pil, PIL_ONLINE);
	mutex_unlock(&pil->lock);
	put_device(&pil->dev);

	return ret;
}
EXPORT_SYMBOL(pil_force_boot);

#ifdef CONFIG_DEBUG_FS
static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
{
	filp->private_data = inode->i_private;
	return 0;
}

static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
		size_t cnt, loff_t *ppos)
{
	int r;
	char buf[40];
	struct pil_device *pil = filp->private_data;

	mutex_lock(&pil->lock);
	r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
	mutex_unlock(&pil->lock);
	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
}

static ssize_t msm_pil_debugfs_write(struct file *filp,
		const char __user *ubuf, size_t cnt, loff_t *ppos)
{
	struct pil_device *pil = filp->private_data;
	char buf[4];

	if (cnt > sizeof(buf))
		return -EINVAL;

	if (copy_from_user(&buf, ubuf, cnt))
		return -EFAULT;

	if (!strncmp(buf, "get", 3)) {
		if (IS_ERR(pil_get(pil->desc->name)))
			return -EIO;
	} else if (!strncmp(buf, "put", 3))
		pil_put(pil);
	else
		return -EINVAL;

	return cnt;
}

static const struct file_operations msm_pil_debugfs_fops = {
	.open	= msm_pil_debugfs_open,
	.read	= msm_pil_debugfs_read,
	.write	= msm_pil_debugfs_write,
};

static struct dentry *pil_base_dir;

static int __init msm_pil_debugfs_init(void)
{
	pil_base_dir = debugfs_create_dir("pil", NULL);
	if (!pil_base_dir) {
		pil_base_dir = NULL;
		return -ENOMEM;
	}

	return 0;
}

static void __exit msm_pil_debugfs_exit(void)
{
	debugfs_remove_recursive(pil_base_dir);
}

static int msm_pil_debugfs_add(struct pil_device *pil)
{
	if (!pil_base_dir)
		return -ENOMEM;

	pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
				pil_base_dir, pil, &msm_pil_debugfs_fops);
	return !pil->dentry ? -ENOMEM : 0;
}

static void msm_pil_debugfs_remove(struct pil_device *pil)
{
	debugfs_remove(pil->dentry);
}
#else
static int __init msm_pil_debugfs_init(void) { return 0; };
static void __exit msm_pil_debugfs_exit(void) { return 0; };
static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
static void msm_pil_debugfs_remove(struct pil_device *pil) { }
#endif

static void pil_device_release(struct device *dev)
{
	struct pil_device *pil = to_pil_device(dev);
	wake_lock_destroy(&pil->wlock);
	mutex_destroy(&pil->lock);
	kfree(pil);
}

struct pil_device *msm_pil_register(struct pil_desc *desc)
{
	int err;
	static atomic_t pil_count = ATOMIC_INIT(-1);
	struct pil_device *pil;

	/* Ignore users who don't make any sense */
	if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
				"invalid proxy voting. ignoring\n"))
		((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;

	WARN(desc->ops->proxy_unvote && !desc->proxy_timeout,
		"A proxy timeout of 0 ms was specified for %s. Specify one in "
		"desc->proxy_timeout.\n", desc->name);

	pil = kzalloc(sizeof(*pil), GFP_KERNEL);
	if (!pil)
		return ERR_PTR(-ENOMEM);

	mutex_init(&pil->lock);
	pil->desc = desc;
	pil->owner = desc->owner;
	pil->dev.parent = desc->dev;
	pil->dev.bus = &pil_bus_type;
	pil->dev.release = pil_device_release;

	snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
	wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
	INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);

	dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
	err = device_register(&pil->dev);
	if (err) {
		put_device(&pil->dev);
		wake_lock_destroy(&pil->wlock);
		mutex_destroy(&pil->lock);
		kfree(pil);
		return ERR_PTR(err);
	}

	err = msm_pil_debugfs_add(pil);
	if (err) {
		device_unregister(&pil->dev);
		return ERR_PTR(err);
	}

	return pil;
}
EXPORT_SYMBOL(msm_pil_register);

void msm_pil_unregister(struct pil_device *pil)
{
	if (IS_ERR_OR_NULL(pil))
		return;

	if (get_device(&pil->dev)) {
		mutex_lock(&pil->lock);
		WARN_ON(pil->count);
		flush_delayed_work_sync(&pil->proxy);
		msm_pil_debugfs_remove(pil);
		device_unregister(&pil->dev);
		mutex_unlock(&pil->lock);
		put_device(&pil->dev);
	}
}
EXPORT_SYMBOL(msm_pil_unregister);

static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
{
	switch (event) {
	case PM_SUSPEND_PREPARE:
		down_write(&pil_pm_rwsem);
		break;
	case PM_POST_SUSPEND:
		up_write(&pil_pm_rwsem);
		break;
	}
	return NOTIFY_DONE;
}

static struct notifier_block pil_pm_notifier = {
	.notifier_call = pil_pm_notify,
};

static int __init msm_pil_init(void)
{
	int ret = msm_pil_debugfs_init();
	if (ret)
		return ret;
	register_pm_notifier(&pil_pm_notifier);
	return bus_register(&pil_bus_type);
}
subsys_initcall(msm_pil_init);

static void __exit msm_pil_exit(void)
{
	bus_unregister(&pil_bus_type);
	unregister_pm_notifier(&pil_pm_notifier);
	msm_pil_debugfs_exit();
}
module_exit(msm_pil_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");