blob: bfbf4bc19fa0a525fa89c00c32e52d9f170e3b83 [file] [log] [blame]
Matt Wagantall39f90cb2012-02-08 14:09:11 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
Stephen Boyd3f4da322011-08-30 01:03:23 -070015#include <linux/device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070016#include <linux/firmware.h>
17#include <linux/io.h>
18#include <linux/debugfs.h>
19#include <linux/elf.h>
20#include <linux/mutex.h>
21#include <linux/memblock.h>
Stephen Boyd3f4da322011-08-30 01:03:23 -070022#include <linux/slab.h>
Stephen Boyd6d67d252011-09-27 11:50:05 -070023#include <linux/atomic.h>
Stephen Boyd80bde032012-03-16 00:14:42 -070024#include <linux/suspend.h>
25#include <linux/rwsem.h>
Stephen Boyd20ad8102011-10-09 21:28:01 -070026#include <linux/sysfs.h>
Stephen Boyd36974ec2012-03-22 01:30:59 -070027#include <linux/workqueue.h>
28#include <linux/jiffies.h>
29#include <linux/wakelock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <asm/uaccess.h>
32#include <asm/setup.h>
Stephen Boyd6d67d252011-09-27 11:50:05 -070033#include <mach/peripheral-loader.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#include "peripheral-loader.h"
36
Stephen Boyd20ad8102011-10-09 21:28:01 -070037enum pil_state {
38 PIL_OFFLINE,
39 PIL_ONLINE,
40};
41
42static const char *pil_states[] = {
43 [PIL_OFFLINE] = "OFFLINE",
44 [PIL_ONLINE] = "ONLINE",
45};
46
Stephen Boyd3f4da322011-08-30 01:03:23 -070047struct pil_device {
48 struct pil_desc *desc;
49 int count;
Stephen Boyd20ad8102011-10-09 21:28:01 -070050 enum pil_state state;
Stephen Boyd3f4da322011-08-30 01:03:23 -070051 struct mutex lock;
Stephen Boyd6d67d252011-09-27 11:50:05 -070052 struct device dev;
53 struct module *owner;
54#ifdef CONFIG_DEBUG_FS
55 struct dentry *dentry;
56#endif
Stephen Boyd36974ec2012-03-22 01:30:59 -070057 struct delayed_work proxy;
58 struct wake_lock wlock;
59 char wake_name[32];
Stephen Boyd3f4da322011-08-30 01:03:23 -070060};
61
Stephen Boyd6d67d252011-09-27 11:50:05 -070062#define to_pil_device(d) container_of(d, struct pil_device, dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063
Stephen Boyd20ad8102011-10-09 21:28:01 -070064static ssize_t name_show(struct device *dev, struct device_attribute *attr,
65 char *buf)
66{
67 return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name);
68}
69
70static ssize_t state_show(struct device *dev, struct device_attribute *attr,
71 char *buf)
72{
73 enum pil_state state = to_pil_device(dev)->state;
74 return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]);
75}
76
77static struct device_attribute pil_attrs[] = {
78 __ATTR_RO(name),
79 __ATTR_RO(state),
80 { },
81};
82
Stephen Boyd6d67d252011-09-27 11:50:05 -070083struct bus_type pil_bus_type = {
84 .name = "pil",
Stephen Boyd20ad8102011-10-09 21:28:01 -070085 .dev_attrs = pil_attrs,
Stephen Boyd6d67d252011-09-27 11:50:05 -070086};
87
88static int __find_peripheral(struct device *dev, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089{
Stephen Boyd6d67d252011-09-27 11:50:05 -070090 struct pil_device *pdev = to_pil_device(dev);
91 return !strncmp(pdev->desc->name, data, INT_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092}
93
94static struct pil_device *find_peripheral(const char *str)
95{
Stephen Boyd6d67d252011-09-27 11:50:05 -070096 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097
98 if (!str)
99 return NULL;
100
Stephen Boyd6d67d252011-09-27 11:50:05 -0700101 dev = bus_find_device(&pil_bus_type, NULL, (void *)str,
102 __find_peripheral);
103 return dev ? to_pil_device(dev) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104}
105
Stephen Boyd36974ec2012-03-22 01:30:59 -0700106static void pil_proxy_work(struct work_struct *work)
107{
108 struct pil_device *pil;
109
110 pil = container_of(work, struct pil_device, proxy.work);
111 pil->desc->ops->proxy_unvote(pil->desc);
112 wake_unlock(&pil->wlock);
113}
114
115static int pil_proxy_vote(struct pil_device *pil)
116{
117 if (pil->desc->ops->proxy_vote) {
118 wake_lock(&pil->wlock);
119 return pil->desc->ops->proxy_vote(pil->desc);
120 }
121 return 0;
122}
123
124static void pil_proxy_unvote(struct pil_device *pil, unsigned long timeout)
125{
126 if (pil->desc->ops->proxy_unvote)
127 schedule_delayed_work(&pil->proxy, msecs_to_jiffies(timeout));
128}
129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130#define IOMAP_SIZE SZ_4M
131
132static int load_segment(const struct elf32_phdr *phdr, unsigned num,
133 struct pil_device *pil)
134{
Stephen Boydb0f1f802012-02-03 11:28:08 -0800135 int ret = 0, count, paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 char fw_name[30];
137 const struct firmware *fw = NULL;
138 const u8 *data;
139
Stephen Boydf79af532012-05-14 18:57:26 -0700140 if (memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700141 dev_err(&pil->dev, "%s: kernel memory would be overwritten "
142 "[%#08lx, %#08lx)\n", pil->desc->name,
Stephen Boydf79af532012-05-14 18:57:26 -0700143 (unsigned long)phdr->p_paddr,
144 (unsigned long)(phdr->p_paddr + phdr->p_memsz));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 return -EPERM;
146 }
147
148 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700149 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
150 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700151 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700153 dev_err(&pil->dev, "%s: Failed to locate blob %s\n",
154 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 return ret;
156 }
157
158 if (fw->size != phdr->p_filesz) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700159 dev_err(&pil->dev, "%s: Blob size %u doesn't match "
160 "%u\n", pil->desc->name, fw->size,
161 phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 ret = -EPERM;
163 goto release_fw;
164 }
165 }
166
167 /* Load the segment into memory */
168 count = phdr->p_filesz;
169 paddr = phdr->p_paddr;
170 data = fw ? fw->data : NULL;
171 while (count > 0) {
172 int size;
173 u8 __iomem *buf;
174
175 size = min_t(size_t, IOMAP_SIZE, count);
176 buf = ioremap(paddr, size);
177 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700178 dev_err(&pil->dev, "%s: Failed to map memory\n",
179 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 ret = -ENOMEM;
181 goto release_fw;
182 }
183 memcpy(buf, data, size);
184 iounmap(buf);
185
186 count -= size;
187 paddr += size;
188 data += size;
189 }
190
191 /* Zero out trailing memory */
192 count = phdr->p_memsz - phdr->p_filesz;
193 while (count > 0) {
194 int size;
195 u8 __iomem *buf;
196
197 size = min_t(size_t, IOMAP_SIZE, count);
198 buf = ioremap(paddr, size);
199 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700200 dev_err(&pil->dev, "%s: Failed to map memory\n",
201 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 ret = -ENOMEM;
203 goto release_fw;
204 }
205 memset(buf, 0, size);
206 iounmap(buf);
207
208 count -= size;
209 paddr += size;
210 }
211
Stephen Boydb0f1f802012-02-03 11:28:08 -0800212 if (pil->desc->ops->verify_blob) {
213 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700214 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800215 if (ret)
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700216 dev_err(&pil->dev, "%s: Blob%u failed verification\n",
217 pil->desc->name, num);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800218 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
220release_fw:
221 release_firmware(fw);
222 return ret;
223}
224
225#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
226
227static int segment_is_loadable(const struct elf32_phdr *p)
228{
229 return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags);
230}
231
Stephen Boyd80bde032012-03-16 00:14:42 -0700232/* Sychronize request_firmware() with suspend */
233static DECLARE_RWSEM(pil_pm_rwsem);
234
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235static int load_image(struct pil_device *pil)
236{
237 int i, ret;
238 char fw_name[30];
239 struct elf32_hdr *ehdr;
240 const struct elf32_phdr *phdr;
241 const struct firmware *fw;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700242 unsigned long proxy_timeout = pil->desc->proxy_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243
Stephen Boyd80bde032012-03-16 00:14:42 -0700244 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700245 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700246 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700248 dev_err(&pil->dev, "%s: Failed to locate %s\n",
249 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 goto out;
251 }
252
253 if (fw->size < sizeof(*ehdr)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700254 dev_err(&pil->dev, "%s: Not big enough to be an elf header\n",
255 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 ret = -EIO;
257 goto release_fw;
258 }
259
260 ehdr = (struct elf32_hdr *)fw->data;
261 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700262 dev_err(&pil->dev, "%s: Not an elf header\n", pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 ret = -EIO;
264 goto release_fw;
265 }
266
267 if (ehdr->e_phnum == 0) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700268 dev_err(&pil->dev, "%s: No loadable segments\n",
269 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 ret = -EIO;
271 goto release_fw;
272 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700273 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
274 sizeof(struct elf32_hdr) > fw->size) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700275 dev_err(&pil->dev, "%s: Program headers not within mdt\n",
276 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 ret = -EIO;
278 goto release_fw;
279 }
280
Stephen Boyd3f4da322011-08-30 01:03:23 -0700281 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700283 dev_err(&pil->dev, "%s: Invalid firmware metadata\n",
284 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 goto release_fw;
286 }
287
Stephen Boydc9753e12011-07-13 17:58:48 -0700288 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
290 if (!segment_is_loadable(phdr))
291 continue;
292
293 ret = load_segment(phdr, i, pil);
294 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700295 dev_err(&pil->dev, "%s: Failed to load segment %d\n",
296 pil->desc->name, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 goto release_fw;
298 }
299 }
300
Stephen Boyd36974ec2012-03-22 01:30:59 -0700301 ret = pil_proxy_vote(pil);
302 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700303 dev_err(&pil->dev, "%s: Failed to proxy vote\n",
304 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700305 goto release_fw;
306 }
307
Stephen Boyd3f4da322011-08-30 01:03:23 -0700308 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700310 dev_err(&pil->dev, "%s: Failed to bring out of reset\n",
311 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700312 proxy_timeout = 0; /* Remove proxy vote immediately on error */
313 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314 }
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700315 dev_info(&pil->dev, "%s: Brought out of reset\n", pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700316err_boot:
317 pil_proxy_unvote(pil, proxy_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318release_fw:
319 release_firmware(fw);
320out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700321 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 return ret;
323}
324
Stephen Boyd20ad8102011-10-09 21:28:01 -0700325static void pil_set_state(struct pil_device *pil, enum pil_state state)
326{
327 if (pil->state != state) {
328 pil->state = state;
329 sysfs_notify(&pil->dev.kobj, NULL, "state");
330 }
331}
332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333/**
334 * pil_get() - Load a peripheral into memory and take it out of reset
335 * @name: pointer to a string containing the name of the peripheral to load
336 *
337 * This function returns a pointer if it succeeds. If an error occurs an
338 * ERR_PTR is returned.
339 *
340 * If PIL is not enabled in the kernel, the value %NULL will be returned.
341 */
342void *pil_get(const char *name)
343{
344 int ret;
345 struct pil_device *pil;
346 struct pil_device *pil_d;
347 void *retval;
348
Stephen Boyd6d67d252011-09-27 11:50:05 -0700349 if (!name)
350 return NULL;
351
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 pil = retval = find_peripheral(name);
353 if (!pil)
354 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700355 if (!try_module_get(pil->owner)) {
356 put_device(&pil->dev);
357 return ERR_PTR(-ENODEV);
358 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359
Stephen Boyd6d67d252011-09-27 11:50:05 -0700360 pil_d = pil_get(pil->desc->depends_on);
361 if (IS_ERR(pil_d)) {
362 retval = pil_d;
363 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 }
365
366 mutex_lock(&pil->lock);
Stephen Boyde4861c02012-05-14 12:57:40 -0700367 if (!pil->count) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700368 ret = load_image(pil);
369 if (ret) {
370 retval = ERR_PTR(ret);
371 goto err_load;
372 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 }
Stephen Boyde4861c02012-05-14 12:57:40 -0700374 pil->count++;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700375 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700377out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700379err_load:
380 mutex_unlock(&pil->lock);
381 pil_put(pil_d);
382err_depends:
383 put_device(&pil->dev);
384 module_put(pil->owner);
385 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386}
387EXPORT_SYMBOL(pil_get);
388
Stephen Boyd36974ec2012-03-22 01:30:59 -0700389static void pil_shutdown(struct pil_device *pil)
390{
391 pil->desc->ops->shutdown(pil->desc);
392 flush_delayed_work(&pil->proxy);
393 pil_set_state(pil, PIL_OFFLINE);
394}
395
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396/**
397 * pil_put() - Inform PIL the peripheral no longer needs to be active
398 * @peripheral_handle: pointer from a previous call to pil_get()
399 *
400 * This doesn't imply that a peripheral is shutdown or in reset since another
401 * driver could be using the peripheral.
402 */
403void pil_put(void *peripheral_handle)
404{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700405 struct pil_device *pil_d, *pil = peripheral_handle;
406
407 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409
410 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700411 if (WARN(!pil->count, "%s: %s: Reference count mismatch\n",
412 pil->desc->name, __func__))
Stephen Boyd6d67d252011-09-27 11:50:05 -0700413 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700414 if (!--pil->count)
415 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416 mutex_unlock(&pil->lock);
417
Stephen Boyd3f4da322011-08-30 01:03:23 -0700418 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700419 module_put(pil->owner);
420 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700422 put_device(&pil_d->dev);
423 }
424 put_device(&pil->dev);
425 return;
426err_out:
427 mutex_unlock(&pil->lock);
428 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429}
430EXPORT_SYMBOL(pil_put);
431
432void pil_force_shutdown(const char *name)
433{
434 struct pil_device *pil;
435
436 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700437 if (!pil) {
438 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700440 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
442 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700443 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
444 pil->desc->name, __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700445 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700447
Stephen Boyd6d67d252011-09-27 11:50:05 -0700448 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449}
450EXPORT_SYMBOL(pil_force_shutdown);
451
452int pil_force_boot(const char *name)
453{
454 int ret = -EINVAL;
455 struct pil_device *pil;
456
457 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700458 if (!pil) {
459 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700461 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462
463 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700464 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
465 pil->desc->name, __func__))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700467 if (!ret)
468 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700470 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471
472 return ret;
473}
474EXPORT_SYMBOL(pil_force_boot);
475
476#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700477static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478{
479 filp->private_data = inode->i_private;
480 return 0;
481}
482
483static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
484 size_t cnt, loff_t *ppos)
485{
486 int r;
487 char buf[40];
488 struct pil_device *pil = filp->private_data;
489
490 mutex_lock(&pil->lock);
491 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
492 mutex_unlock(&pil->lock);
493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
494}
495
496static ssize_t msm_pil_debugfs_write(struct file *filp,
497 const char __user *ubuf, size_t cnt, loff_t *ppos)
498{
499 struct pil_device *pil = filp->private_data;
500 char buf[4];
501
502 if (cnt > sizeof(buf))
503 return -EINVAL;
504
505 if (copy_from_user(&buf, ubuf, cnt))
506 return -EFAULT;
507
508 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700509 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 return -EIO;
511 } else if (!strncmp(buf, "put", 3))
512 pil_put(pil);
513 else
514 return -EINVAL;
515
516 return cnt;
517}
518
519static const struct file_operations msm_pil_debugfs_fops = {
520 .open = msm_pil_debugfs_open,
521 .read = msm_pil_debugfs_read,
522 .write = msm_pil_debugfs_write,
523};
524
525static struct dentry *pil_base_dir;
526
Stephen Boyd6d67d252011-09-27 11:50:05 -0700527static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528{
529 pil_base_dir = debugfs_create_dir("pil", NULL);
530 if (!pil_base_dir) {
531 pil_base_dir = NULL;
532 return -ENOMEM;
533 }
534
535 return 0;
536}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700537
538static void __exit msm_pil_debugfs_exit(void)
539{
540 debugfs_remove_recursive(pil_base_dir);
541}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
543static int msm_pil_debugfs_add(struct pil_device *pil)
544{
545 if (!pil_base_dir)
546 return -ENOMEM;
547
Stephen Boyd6d67d252011-09-27 11:50:05 -0700548 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
549 pil_base_dir, pil, &msm_pil_debugfs_fops);
550 return !pil->dentry ? -ENOMEM : 0;
551}
552
553static void msm_pil_debugfs_remove(struct pil_device *pil)
554{
555 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556}
557#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700558static int __init msm_pil_debugfs_init(void) { return 0; };
559static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700561static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562#endif
563
Stephen Boyd6d67d252011-09-27 11:50:05 -0700564static int __msm_pil_shutdown(struct device *dev, void *data)
565{
Stephen Boyd36974ec2012-03-22 01:30:59 -0700566 pil_shutdown(to_pil_device(dev));
Stephen Boyd6d67d252011-09-27 11:50:05 -0700567 return 0;
568}
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570static int msm_pil_shutdown_at_boot(void)
571{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700572 return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573}
574late_initcall(msm_pil_shutdown_at_boot);
575
Stephen Boyd6d67d252011-09-27 11:50:05 -0700576static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700578 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700579 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700580 mutex_destroy(&pil->lock);
581 kfree(pil);
582}
583
584struct pil_device *msm_pil_register(struct pil_desc *desc)
585{
586 int err;
587 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700588 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700589
Stephen Boyd36974ec2012-03-22 01:30:59 -0700590 /* Ignore users who don't make any sense */
591 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
592 "invalid proxy voting. ignoring\n"))
593 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
594
Matt Wagantall2475e312012-05-25 19:56:33 -0700595 WARN(desc->ops->proxy_unvote && !desc->proxy_timeout,
596 "A proxy timeout of 0 ms was specified for %s. Specify one in "
597 "desc->proxy_timeout.\n", desc->name);
598
Stephen Boyd36974ec2012-03-22 01:30:59 -0700599 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700600 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700601 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700604 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700605 pil->owner = desc->owner;
606 pil->dev.parent = desc->dev;
607 pil->dev.bus = &pil_bus_type;
608 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Stephen Boyd36974ec2012-03-22 01:30:59 -0700610 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
611 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
612 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
613
Stephen Boyd6d67d252011-09-27 11:50:05 -0700614 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
615 err = device_register(&pil->dev);
616 if (err) {
617 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700618 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700619 mutex_destroy(&pil->lock);
620 kfree(pil);
621 return ERR_PTR(err);
622 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623
Stephen Boyd6d67d252011-09-27 11:50:05 -0700624 err = msm_pil_debugfs_add(pil);
625 if (err) {
626 device_unregister(&pil->dev);
627 return ERR_PTR(err);
628 }
629
630 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700632EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633
Stephen Boyd6d67d252011-09-27 11:50:05 -0700634void msm_pil_unregister(struct pil_device *pil)
635{
636 if (IS_ERR_OR_NULL(pil))
637 return;
638
639 if (get_device(&pil->dev)) {
640 mutex_lock(&pil->lock);
641 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700642 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700643 msm_pil_debugfs_remove(pil);
644 device_unregister(&pil->dev);
645 mutex_unlock(&pil->lock);
646 put_device(&pil->dev);
647 }
648}
649EXPORT_SYMBOL(msm_pil_unregister);
650
Stephen Boyd80bde032012-03-16 00:14:42 -0700651static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
652{
653 switch (event) {
654 case PM_SUSPEND_PREPARE:
655 down_write(&pil_pm_rwsem);
656 break;
657 case PM_POST_SUSPEND:
658 up_write(&pil_pm_rwsem);
659 break;
660 }
661 return NOTIFY_DONE;
662}
663
664static struct notifier_block pil_pm_notifier = {
665 .notifier_call = pil_pm_notify,
666};
667
Stephen Boyd6d67d252011-09-27 11:50:05 -0700668static int __init msm_pil_init(void)
669{
670 int ret = msm_pil_debugfs_init();
671 if (ret)
672 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700673 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700674 return bus_register(&pil_bus_type);
675}
676subsys_initcall(msm_pil_init);
677
678static void __exit msm_pil_exit(void)
679{
680 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700681 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700682 msm_pil_debugfs_exit();
683}
684module_exit(msm_pil_exit);
685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686MODULE_LICENSE("GPL v2");
687MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");