blob: 540ffbbbb143a04a0263f6f2eb02a5e57160446e [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{
Stephen Boyd0a563142012-07-02 13:33:31 -0700117 int ret = 0;
118
Stephen Boyd36974ec2012-03-22 01:30:59 -0700119 if (pil->desc->ops->proxy_vote) {
120 wake_lock(&pil->wlock);
Stephen Boyd0a563142012-07-02 13:33:31 -0700121 ret = pil->desc->ops->proxy_vote(pil->desc);
122 if (ret)
123 wake_unlock(&pil->wlock);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700124 }
Stephen Boyd0a563142012-07-02 13:33:31 -0700125 return ret;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700126}
127
128static void pil_proxy_unvote(struct pil_device *pil, unsigned long timeout)
129{
130 if (pil->desc->ops->proxy_unvote)
131 schedule_delayed_work(&pil->proxy, msecs_to_jiffies(timeout));
132}
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134#define IOMAP_SIZE SZ_4M
135
136static int load_segment(const struct elf32_phdr *phdr, unsigned num,
137 struct pil_device *pil)
138{
Stephen Boydb0f1f802012-02-03 11:28:08 -0800139 int ret = 0, count, paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 char fw_name[30];
141 const struct firmware *fw = NULL;
142 const u8 *data;
143
Stephen Boydf79af532012-05-14 18:57:26 -0700144 if (memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700145 dev_err(&pil->dev, "%s: kernel memory would be overwritten "
146 "[%#08lx, %#08lx)\n", pil->desc->name,
Stephen Boydf79af532012-05-14 18:57:26 -0700147 (unsigned long)phdr->p_paddr,
148 (unsigned long)(phdr->p_paddr + phdr->p_memsz));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 return -EPERM;
150 }
151
152 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700153 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
154 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700155 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700157 dev_err(&pil->dev, "%s: Failed to locate blob %s\n",
158 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 return ret;
160 }
161
162 if (fw->size != phdr->p_filesz) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700163 dev_err(&pil->dev, "%s: Blob size %u doesn't match "
164 "%u\n", pil->desc->name, fw->size,
165 phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 ret = -EPERM;
167 goto release_fw;
168 }
169 }
170
171 /* Load the segment into memory */
172 count = phdr->p_filesz;
173 paddr = phdr->p_paddr;
174 data = fw ? fw->data : NULL;
175 while (count > 0) {
176 int size;
177 u8 __iomem *buf;
178
179 size = min_t(size_t, IOMAP_SIZE, count);
180 buf = ioremap(paddr, size);
181 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700182 dev_err(&pil->dev, "%s: Failed to map memory\n",
183 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 ret = -ENOMEM;
185 goto release_fw;
186 }
187 memcpy(buf, data, size);
188 iounmap(buf);
189
190 count -= size;
191 paddr += size;
192 data += size;
193 }
194
195 /* Zero out trailing memory */
196 count = phdr->p_memsz - phdr->p_filesz;
197 while (count > 0) {
198 int size;
199 u8 __iomem *buf;
200
201 size = min_t(size_t, IOMAP_SIZE, count);
202 buf = ioremap(paddr, size);
203 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700204 dev_err(&pil->dev, "%s: Failed to map memory\n",
205 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 ret = -ENOMEM;
207 goto release_fw;
208 }
209 memset(buf, 0, size);
210 iounmap(buf);
211
212 count -= size;
213 paddr += size;
214 }
215
Stephen Boydb0f1f802012-02-03 11:28:08 -0800216 if (pil->desc->ops->verify_blob) {
217 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700218 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800219 if (ret)
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700220 dev_err(&pil->dev, "%s: Blob%u failed verification\n",
221 pil->desc->name, num);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800222 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223
224release_fw:
225 release_firmware(fw);
226 return ret;
227}
228
229#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
230
231static int segment_is_loadable(const struct elf32_phdr *p)
232{
Tianyi Gou17eae0d2012-06-28 17:19:37 -0700233 return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234}
235
Stephen Boyd80bde032012-03-16 00:14:42 -0700236/* Sychronize request_firmware() with suspend */
237static DECLARE_RWSEM(pil_pm_rwsem);
238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239static int load_image(struct pil_device *pil)
240{
241 int i, ret;
242 char fw_name[30];
243 struct elf32_hdr *ehdr;
244 const struct elf32_phdr *phdr;
245 const struct firmware *fw;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700246 unsigned long proxy_timeout = pil->desc->proxy_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247
Stephen Boyd80bde032012-03-16 00:14:42 -0700248 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700249 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700250 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700252 dev_err(&pil->dev, "%s: Failed to locate %s\n",
253 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 goto out;
255 }
256
257 if (fw->size < sizeof(*ehdr)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700258 dev_err(&pil->dev, "%s: Not big enough to be an elf header\n",
259 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 ret = -EIO;
261 goto release_fw;
262 }
263
264 ehdr = (struct elf32_hdr *)fw->data;
265 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700266 dev_err(&pil->dev, "%s: Not an elf header\n", pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 ret = -EIO;
268 goto release_fw;
269 }
270
271 if (ehdr->e_phnum == 0) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700272 dev_err(&pil->dev, "%s: No loadable segments\n",
273 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 ret = -EIO;
275 goto release_fw;
276 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700277 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
278 sizeof(struct elf32_hdr) > fw->size) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700279 dev_err(&pil->dev, "%s: Program headers not within mdt\n",
280 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 ret = -EIO;
282 goto release_fw;
283 }
284
Stephen Boyd3f4da322011-08-30 01:03:23 -0700285 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700287 dev_err(&pil->dev, "%s: Invalid firmware metadata\n",
288 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 goto release_fw;
290 }
291
Stephen Boydc9753e12011-07-13 17:58:48 -0700292 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
294 if (!segment_is_loadable(phdr))
295 continue;
296
297 ret = load_segment(phdr, i, pil);
298 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700299 dev_err(&pil->dev, "%s: Failed to load segment %d\n",
300 pil->desc->name, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 goto release_fw;
302 }
303 }
304
Stephen Boyd36974ec2012-03-22 01:30:59 -0700305 ret = pil_proxy_vote(pil);
306 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700307 dev_err(&pil->dev, "%s: Failed to proxy vote\n",
308 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700309 goto release_fw;
310 }
311
Stephen Boyd3f4da322011-08-30 01:03:23 -0700312 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700314 dev_err(&pil->dev, "%s: Failed to bring out of reset\n",
315 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700316 proxy_timeout = 0; /* Remove proxy vote immediately on error */
317 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 }
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700319 dev_info(&pil->dev, "%s: Brought out of reset\n", pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700320err_boot:
321 pil_proxy_unvote(pil, proxy_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322release_fw:
323 release_firmware(fw);
324out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700325 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 return ret;
327}
328
Stephen Boyd20ad8102011-10-09 21:28:01 -0700329static void pil_set_state(struct pil_device *pil, enum pil_state state)
330{
331 if (pil->state != state) {
332 pil->state = state;
333 sysfs_notify(&pil->dev.kobj, NULL, "state");
334 }
335}
336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337/**
338 * pil_get() - Load a peripheral into memory and take it out of reset
339 * @name: pointer to a string containing the name of the peripheral to load
340 *
341 * This function returns a pointer if it succeeds. If an error occurs an
342 * ERR_PTR is returned.
343 *
344 * If PIL is not enabled in the kernel, the value %NULL will be returned.
345 */
346void *pil_get(const char *name)
347{
348 int ret;
349 struct pil_device *pil;
350 struct pil_device *pil_d;
351 void *retval;
352
Stephen Boyd6d67d252011-09-27 11:50:05 -0700353 if (!name)
354 return NULL;
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 pil = retval = find_peripheral(name);
357 if (!pil)
358 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700359 if (!try_module_get(pil->owner)) {
360 put_device(&pil->dev);
361 return ERR_PTR(-ENODEV);
362 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363
Stephen Boyd6d67d252011-09-27 11:50:05 -0700364 pil_d = pil_get(pil->desc->depends_on);
365 if (IS_ERR(pil_d)) {
366 retval = pil_d;
367 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 }
369
370 mutex_lock(&pil->lock);
Stephen Boyde4861c02012-05-14 12:57:40 -0700371 if (!pil->count) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700372 ret = load_image(pil);
373 if (ret) {
374 retval = ERR_PTR(ret);
375 goto err_load;
376 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 }
Stephen Boyde4861c02012-05-14 12:57:40 -0700378 pil->count++;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700379 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700381out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700383err_load:
384 mutex_unlock(&pil->lock);
385 pil_put(pil_d);
386err_depends:
387 put_device(&pil->dev);
388 module_put(pil->owner);
389 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390}
391EXPORT_SYMBOL(pil_get);
392
Stephen Boyd36974ec2012-03-22 01:30:59 -0700393static void pil_shutdown(struct pil_device *pil)
394{
395 pil->desc->ops->shutdown(pil->desc);
396 flush_delayed_work(&pil->proxy);
397 pil_set_state(pil, PIL_OFFLINE);
398}
399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400/**
401 * pil_put() - Inform PIL the peripheral no longer needs to be active
402 * @peripheral_handle: pointer from a previous call to pil_get()
403 *
404 * This doesn't imply that a peripheral is shutdown or in reset since another
405 * driver could be using the peripheral.
406 */
407void pil_put(void *peripheral_handle)
408{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700409 struct pil_device *pil_d, *pil = peripheral_handle;
410
411 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413
414 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700415 if (WARN(!pil->count, "%s: %s: Reference count mismatch\n",
416 pil->desc->name, __func__))
Stephen Boyd6d67d252011-09-27 11:50:05 -0700417 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700418 if (!--pil->count)
419 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 mutex_unlock(&pil->lock);
421
Stephen Boyd3f4da322011-08-30 01:03:23 -0700422 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700423 module_put(pil->owner);
424 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700426 put_device(&pil_d->dev);
427 }
428 put_device(&pil->dev);
429 return;
430err_out:
431 mutex_unlock(&pil->lock);
432 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433}
434EXPORT_SYMBOL(pil_put);
435
436void pil_force_shutdown(const char *name)
437{
438 struct pil_device *pil;
439
440 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700441 if (!pil) {
442 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700444 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
446 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700447 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
448 pil->desc->name, __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700449 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700451
Stephen Boyd6d67d252011-09-27 11:50:05 -0700452 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453}
454EXPORT_SYMBOL(pil_force_shutdown);
455
456int pil_force_boot(const char *name)
457{
458 int ret = -EINVAL;
459 struct pil_device *pil;
460
461 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700462 if (!pil) {
463 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700465 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466
467 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700468 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
469 pil->desc->name, __func__))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700471 if (!ret)
472 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700474 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475
476 return ret;
477}
478EXPORT_SYMBOL(pil_force_boot);
479
480#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700481static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482{
483 filp->private_data = inode->i_private;
484 return 0;
485}
486
487static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
488 size_t cnt, loff_t *ppos)
489{
490 int r;
491 char buf[40];
492 struct pil_device *pil = filp->private_data;
493
494 mutex_lock(&pil->lock);
495 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
496 mutex_unlock(&pil->lock);
497 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
498}
499
500static ssize_t msm_pil_debugfs_write(struct file *filp,
501 const char __user *ubuf, size_t cnt, loff_t *ppos)
502{
503 struct pil_device *pil = filp->private_data;
504 char buf[4];
505
506 if (cnt > sizeof(buf))
507 return -EINVAL;
508
509 if (copy_from_user(&buf, ubuf, cnt))
510 return -EFAULT;
511
512 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700513 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 return -EIO;
515 } else if (!strncmp(buf, "put", 3))
516 pil_put(pil);
517 else
518 return -EINVAL;
519
520 return cnt;
521}
522
523static const struct file_operations msm_pil_debugfs_fops = {
524 .open = msm_pil_debugfs_open,
525 .read = msm_pil_debugfs_read,
526 .write = msm_pil_debugfs_write,
527};
528
529static struct dentry *pil_base_dir;
530
Stephen Boyd6d67d252011-09-27 11:50:05 -0700531static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532{
533 pil_base_dir = debugfs_create_dir("pil", NULL);
534 if (!pil_base_dir) {
535 pil_base_dir = NULL;
536 return -ENOMEM;
537 }
538
539 return 0;
540}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700541
542static void __exit msm_pil_debugfs_exit(void)
543{
544 debugfs_remove_recursive(pil_base_dir);
545}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546
547static int msm_pil_debugfs_add(struct pil_device *pil)
548{
549 if (!pil_base_dir)
550 return -ENOMEM;
551
Stephen Boyd6d67d252011-09-27 11:50:05 -0700552 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
553 pil_base_dir, pil, &msm_pil_debugfs_fops);
554 return !pil->dentry ? -ENOMEM : 0;
555}
556
557static void msm_pil_debugfs_remove(struct pil_device *pil)
558{
559 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560}
561#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700562static int __init msm_pil_debugfs_init(void) { return 0; };
563static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700565static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566#endif
567
Stephen Boyd6d67d252011-09-27 11:50:05 -0700568static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700570 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700571 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700572 mutex_destroy(&pil->lock);
573 kfree(pil);
574}
575
576struct pil_device *msm_pil_register(struct pil_desc *desc)
577{
578 int err;
579 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700580 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700581
Stephen Boyd36974ec2012-03-22 01:30:59 -0700582 /* Ignore users who don't make any sense */
583 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
584 "invalid proxy voting. ignoring\n"))
585 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
586
Matt Wagantall2475e312012-05-25 19:56:33 -0700587 WARN(desc->ops->proxy_unvote && !desc->proxy_timeout,
588 "A proxy timeout of 0 ms was specified for %s. Specify one in "
589 "desc->proxy_timeout.\n", desc->name);
590
Stephen Boyd36974ec2012-03-22 01:30:59 -0700591 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700592 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700593 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594
595 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700596 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700597 pil->owner = desc->owner;
598 pil->dev.parent = desc->dev;
599 pil->dev.bus = &pil_bus_type;
600 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
Stephen Boyd36974ec2012-03-22 01:30:59 -0700602 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
603 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
604 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
605
Stephen Boyd6d67d252011-09-27 11:50:05 -0700606 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
607 err = device_register(&pil->dev);
608 if (err) {
609 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700610 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700611 mutex_destroy(&pil->lock);
612 kfree(pil);
613 return ERR_PTR(err);
614 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
Stephen Boyd6d67d252011-09-27 11:50:05 -0700616 err = msm_pil_debugfs_add(pil);
617 if (err) {
618 device_unregister(&pil->dev);
619 return ERR_PTR(err);
620 }
621
622 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700624EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
Stephen Boyd6d67d252011-09-27 11:50:05 -0700626void msm_pil_unregister(struct pil_device *pil)
627{
628 if (IS_ERR_OR_NULL(pil))
629 return;
630
631 if (get_device(&pil->dev)) {
632 mutex_lock(&pil->lock);
633 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700634 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700635 msm_pil_debugfs_remove(pil);
636 device_unregister(&pil->dev);
637 mutex_unlock(&pil->lock);
638 put_device(&pil->dev);
639 }
640}
641EXPORT_SYMBOL(msm_pil_unregister);
642
Stephen Boyd80bde032012-03-16 00:14:42 -0700643static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
644{
645 switch (event) {
646 case PM_SUSPEND_PREPARE:
647 down_write(&pil_pm_rwsem);
648 break;
649 case PM_POST_SUSPEND:
650 up_write(&pil_pm_rwsem);
651 break;
652 }
653 return NOTIFY_DONE;
654}
655
656static struct notifier_block pil_pm_notifier = {
657 .notifier_call = pil_pm_notify,
658};
659
Stephen Boyd6d67d252011-09-27 11:50:05 -0700660static int __init msm_pil_init(void)
661{
662 int ret = msm_pil_debugfs_init();
663 if (ret)
664 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700665 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700666 return bus_register(&pil_bus_type);
667}
668subsys_initcall(msm_pil_init);
669
670static void __exit msm_pil_exit(void)
671{
672 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700673 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700674 msm_pil_debugfs_exit();
675}
676module_exit(msm_pil_exit);
677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678MODULE_LICENSE("GPL v2");
679MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");