blob: 4ff34bfebd8c67ccc0ca58bb675d49eb1f85e6ba [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
Matt Wagantall0aafa352012-05-14 16:40:41 -070037/**
38 * proxy_timeout - Override for proxy vote timeouts
39 * -1: Use driver-specified timeout
40 * 0: Hold proxy votes until shutdown
41 * >0: Specify a custom timeout in ms
42 */
43static int proxy_timeout_ms = -1;
44module_param(proxy_timeout_ms, int, S_IRUGO | S_IWUSR);
45
Stephen Boyd20ad8102011-10-09 21:28:01 -070046enum pil_state {
47 PIL_OFFLINE,
48 PIL_ONLINE,
49};
50
51static const char *pil_states[] = {
52 [PIL_OFFLINE] = "OFFLINE",
53 [PIL_ONLINE] = "ONLINE",
54};
55
Stephen Boyd3f4da322011-08-30 01:03:23 -070056struct pil_device {
57 struct pil_desc *desc;
58 int count;
Stephen Boyd20ad8102011-10-09 21:28:01 -070059 enum pil_state state;
Stephen Boyd3f4da322011-08-30 01:03:23 -070060 struct mutex lock;
Stephen Boyd6d67d252011-09-27 11:50:05 -070061 struct device dev;
62 struct module *owner;
63#ifdef CONFIG_DEBUG_FS
64 struct dentry *dentry;
65#endif
Stephen Boyd36974ec2012-03-22 01:30:59 -070066 struct delayed_work proxy;
67 struct wake_lock wlock;
68 char wake_name[32];
Stephen Boyd3f4da322011-08-30 01:03:23 -070069};
70
Stephen Boyd6d67d252011-09-27 11:50:05 -070071#define to_pil_device(d) container_of(d, struct pil_device, dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072
Stephen Boyd20ad8102011-10-09 21:28:01 -070073static ssize_t name_show(struct device *dev, struct device_attribute *attr,
74 char *buf)
75{
76 return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name);
77}
78
79static ssize_t state_show(struct device *dev, struct device_attribute *attr,
80 char *buf)
81{
82 enum pil_state state = to_pil_device(dev)->state;
83 return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]);
84}
85
86static struct device_attribute pil_attrs[] = {
87 __ATTR_RO(name),
88 __ATTR_RO(state),
89 { },
90};
91
Stephen Boyd6d67d252011-09-27 11:50:05 -070092struct bus_type pil_bus_type = {
93 .name = "pil",
Stephen Boyd20ad8102011-10-09 21:28:01 -070094 .dev_attrs = pil_attrs,
Stephen Boyd6d67d252011-09-27 11:50:05 -070095};
96
97static int __find_peripheral(struct device *dev, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098{
Stephen Boyd6d67d252011-09-27 11:50:05 -070099 struct pil_device *pdev = to_pil_device(dev);
100 return !strncmp(pdev->desc->name, data, INT_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101}
102
103static struct pil_device *find_peripheral(const char *str)
104{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700105 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
107 if (!str)
108 return NULL;
109
Stephen Boyd6d67d252011-09-27 11:50:05 -0700110 dev = bus_find_device(&pil_bus_type, NULL, (void *)str,
111 __find_peripheral);
112 return dev ? to_pil_device(dev) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113}
114
Stephen Boyd36974ec2012-03-22 01:30:59 -0700115static void pil_proxy_work(struct work_struct *work)
116{
117 struct pil_device *pil;
118
119 pil = container_of(work, struct pil_device, proxy.work);
120 pil->desc->ops->proxy_unvote(pil->desc);
121 wake_unlock(&pil->wlock);
122}
123
124static int pil_proxy_vote(struct pil_device *pil)
125{
Stephen Boyd0a563142012-07-02 13:33:31 -0700126 int ret = 0;
127
Stephen Boyd36974ec2012-03-22 01:30:59 -0700128 if (pil->desc->ops->proxy_vote) {
129 wake_lock(&pil->wlock);
Stephen Boyd0a563142012-07-02 13:33:31 -0700130 ret = pil->desc->ops->proxy_vote(pil->desc);
131 if (ret)
132 wake_unlock(&pil->wlock);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700133 }
Stephen Boyd0a563142012-07-02 13:33:31 -0700134 return ret;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700135}
136
137static void pil_proxy_unvote(struct pil_device *pil, unsigned long timeout)
138{
Matt Wagantall0aafa352012-05-14 16:40:41 -0700139 if (proxy_timeout_ms >= 0)
140 timeout = proxy_timeout_ms;
141
142 if (timeout && pil->desc->ops->proxy_unvote)
Stephen Boyd36974ec2012-03-22 01:30:59 -0700143 schedule_delayed_work(&pil->proxy, msecs_to_jiffies(timeout));
144}
145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146#define IOMAP_SIZE SZ_4M
147
148static int load_segment(const struct elf32_phdr *phdr, unsigned num,
149 struct pil_device *pil)
150{
Stephen Boydb0f1f802012-02-03 11:28:08 -0800151 int ret = 0, count, paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 char fw_name[30];
153 const struct firmware *fw = NULL;
154 const u8 *data;
155
Stephen Boydf79af532012-05-14 18:57:26 -0700156 if (memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700157 dev_err(&pil->dev, "%s: kernel memory would be overwritten "
158 "[%#08lx, %#08lx)\n", pil->desc->name,
Stephen Boydf79af532012-05-14 18:57:26 -0700159 (unsigned long)phdr->p_paddr,
160 (unsigned long)(phdr->p_paddr + phdr->p_memsz));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 return -EPERM;
162 }
163
164 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700165 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
166 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700167 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700169 dev_err(&pil->dev, "%s: Failed to locate blob %s\n",
170 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171 return ret;
172 }
173
174 if (fw->size != phdr->p_filesz) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700175 dev_err(&pil->dev, "%s: Blob size %u doesn't match "
176 "%u\n", pil->desc->name, fw->size,
177 phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 ret = -EPERM;
179 goto release_fw;
180 }
181 }
182
183 /* Load the segment into memory */
184 count = phdr->p_filesz;
185 paddr = phdr->p_paddr;
186 data = fw ? fw->data : NULL;
187 while (count > 0) {
188 int size;
189 u8 __iomem *buf;
190
191 size = min_t(size_t, IOMAP_SIZE, count);
192 buf = ioremap(paddr, size);
193 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700194 dev_err(&pil->dev, "%s: Failed to map memory\n",
195 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 ret = -ENOMEM;
197 goto release_fw;
198 }
199 memcpy(buf, data, size);
200 iounmap(buf);
201
202 count -= size;
203 paddr += size;
204 data += size;
205 }
206
207 /* Zero out trailing memory */
208 count = phdr->p_memsz - phdr->p_filesz;
209 while (count > 0) {
210 int size;
211 u8 __iomem *buf;
212
213 size = min_t(size_t, IOMAP_SIZE, count);
214 buf = ioremap(paddr, size);
215 if (!buf) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700216 dev_err(&pil->dev, "%s: Failed to map memory\n",
217 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 ret = -ENOMEM;
219 goto release_fw;
220 }
221 memset(buf, 0, size);
222 iounmap(buf);
223
224 count -= size;
225 paddr += size;
226 }
227
Stephen Boydb0f1f802012-02-03 11:28:08 -0800228 if (pil->desc->ops->verify_blob) {
229 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700230 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800231 if (ret)
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700232 dev_err(&pil->dev, "%s: Blob%u failed verification\n",
233 pil->desc->name, num);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800234 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
236release_fw:
237 release_firmware(fw);
238 return ret;
239}
240
241#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
242
243static int segment_is_loadable(const struct elf32_phdr *p)
244{
Tianyi Gou17eae0d2012-06-28 17:19:37 -0700245 return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246}
247
Stephen Boyd80bde032012-03-16 00:14:42 -0700248/* Sychronize request_firmware() with suspend */
249static DECLARE_RWSEM(pil_pm_rwsem);
250
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251static int load_image(struct pil_device *pil)
252{
253 int i, ret;
254 char fw_name[30];
255 struct elf32_hdr *ehdr;
256 const struct elf32_phdr *phdr;
257 const struct firmware *fw;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700258 unsigned long proxy_timeout = pil->desc->proxy_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259
Stephen Boyd80bde032012-03-16 00:14:42 -0700260 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700261 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700262 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700264 dev_err(&pil->dev, "%s: Failed to locate %s\n",
265 pil->desc->name, fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 goto out;
267 }
268
269 if (fw->size < sizeof(*ehdr)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700270 dev_err(&pil->dev, "%s: Not big enough to be an elf header\n",
271 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 ret = -EIO;
273 goto release_fw;
274 }
275
276 ehdr = (struct elf32_hdr *)fw->data;
277 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700278 dev_err(&pil->dev, "%s: Not an elf header\n", pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 ret = -EIO;
280 goto release_fw;
281 }
282
283 if (ehdr->e_phnum == 0) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700284 dev_err(&pil->dev, "%s: No loadable segments\n",
285 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 ret = -EIO;
287 goto release_fw;
288 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700289 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
290 sizeof(struct elf32_hdr) > fw->size) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700291 dev_err(&pil->dev, "%s: Program headers not within mdt\n",
292 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 ret = -EIO;
294 goto release_fw;
295 }
296
Stephen Boyd3f4da322011-08-30 01:03:23 -0700297 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700299 dev_err(&pil->dev, "%s: Invalid firmware metadata\n",
300 pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 goto release_fw;
302 }
303
Stephen Boydc9753e12011-07-13 17:58:48 -0700304 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
306 if (!segment_is_loadable(phdr))
307 continue;
308
309 ret = load_segment(phdr, i, pil);
310 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700311 dev_err(&pil->dev, "%s: Failed to load segment %d\n",
312 pil->desc->name, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 goto release_fw;
314 }
315 }
316
Stephen Boyd36974ec2012-03-22 01:30:59 -0700317 ret = pil_proxy_vote(pil);
318 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700319 dev_err(&pil->dev, "%s: Failed to proxy vote\n",
320 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700321 goto release_fw;
322 }
323
Stephen Boyd3f4da322011-08-30 01:03:23 -0700324 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 if (ret) {
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700326 dev_err(&pil->dev, "%s: Failed to bring out of reset\n",
327 pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700328 proxy_timeout = 0; /* Remove proxy vote immediately on error */
329 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 }
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700331 dev_info(&pil->dev, "%s: Brought out of reset\n", pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700332err_boot:
333 pil_proxy_unvote(pil, proxy_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334release_fw:
335 release_firmware(fw);
336out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700337 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 return ret;
339}
340
Stephen Boyd20ad8102011-10-09 21:28:01 -0700341static void pil_set_state(struct pil_device *pil, enum pil_state state)
342{
343 if (pil->state != state) {
344 pil->state = state;
345 sysfs_notify(&pil->dev.kobj, NULL, "state");
346 }
347}
348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349/**
350 * pil_get() - Load a peripheral into memory and take it out of reset
351 * @name: pointer to a string containing the name of the peripheral to load
352 *
353 * This function returns a pointer if it succeeds. If an error occurs an
354 * ERR_PTR is returned.
355 *
356 * If PIL is not enabled in the kernel, the value %NULL will be returned.
357 */
358void *pil_get(const char *name)
359{
360 int ret;
361 struct pil_device *pil;
362 struct pil_device *pil_d;
363 void *retval;
364
Stephen Boyd6d67d252011-09-27 11:50:05 -0700365 if (!name)
366 return NULL;
367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 pil = retval = find_peripheral(name);
369 if (!pil)
370 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700371 if (!try_module_get(pil->owner)) {
372 put_device(&pil->dev);
373 return ERR_PTR(-ENODEV);
374 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375
Stephen Boyd6d67d252011-09-27 11:50:05 -0700376 pil_d = pil_get(pil->desc->depends_on);
377 if (IS_ERR(pil_d)) {
378 retval = pil_d;
379 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 }
381
382 mutex_lock(&pil->lock);
Stephen Boyde4861c02012-05-14 12:57:40 -0700383 if (!pil->count) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700384 ret = load_image(pil);
385 if (ret) {
386 retval = ERR_PTR(ret);
387 goto err_load;
388 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 }
Stephen Boyde4861c02012-05-14 12:57:40 -0700390 pil->count++;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700391 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700393out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700395err_load:
396 mutex_unlock(&pil->lock);
397 pil_put(pil_d);
398err_depends:
399 put_device(&pil->dev);
400 module_put(pil->owner);
401 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402}
403EXPORT_SYMBOL(pil_get);
404
Stephen Boyd36974ec2012-03-22 01:30:59 -0700405static void pil_shutdown(struct pil_device *pil)
406{
407 pil->desc->ops->shutdown(pil->desc);
Matt Wagantall0aafa352012-05-14 16:40:41 -0700408 if (proxy_timeout_ms == 0 && pil->desc->ops->proxy_unvote)
409 pil->desc->ops->proxy_unvote(pil->desc);
410 else
411 flush_delayed_work(&pil->proxy);
412
Stephen Boyd36974ec2012-03-22 01:30:59 -0700413 pil_set_state(pil, PIL_OFFLINE);
414}
415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416/**
417 * pil_put() - Inform PIL the peripheral no longer needs to be active
418 * @peripheral_handle: pointer from a previous call to pil_get()
419 *
420 * This doesn't imply that a peripheral is shutdown or in reset since another
421 * driver could be using the peripheral.
422 */
423void pil_put(void *peripheral_handle)
424{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700425 struct pil_device *pil_d, *pil = peripheral_handle;
426
427 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429
430 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700431 if (WARN(!pil->count, "%s: %s: Reference count mismatch\n",
432 pil->desc->name, __func__))
Stephen Boyd6d67d252011-09-27 11:50:05 -0700433 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700434 if (!--pil->count)
435 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 mutex_unlock(&pil->lock);
437
Stephen Boyd3f4da322011-08-30 01:03:23 -0700438 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700439 module_put(pil->owner);
440 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700442 put_device(&pil_d->dev);
443 }
444 put_device(&pil->dev);
445 return;
446err_out:
447 mutex_unlock(&pil->lock);
448 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449}
450EXPORT_SYMBOL(pil_put);
451
452void pil_force_shutdown(const char *name)
453{
454 struct pil_device *pil;
455
456 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700457 if (!pil) {
458 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700460 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461
462 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700463 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
464 pil->desc->name, __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700465 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700467
Stephen Boyd6d67d252011-09-27 11:50:05 -0700468 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469}
470EXPORT_SYMBOL(pil_force_shutdown);
471
472int pil_force_boot(const char *name)
473{
474 int ret = -EINVAL;
475 struct pil_device *pil;
476
477 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700478 if (!pil) {
479 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700481 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482
483 mutex_lock(&pil->lock);
Matt Wagantalla4f0a062012-05-21 18:04:57 -0700484 if (!WARN(!pil->count, "%s: %s: Reference count mismatch\n",
485 pil->desc->name, __func__))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700487 if (!ret)
488 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700490 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491
492 return ret;
493}
494EXPORT_SYMBOL(pil_force_boot);
495
496#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700497static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498{
499 filp->private_data = inode->i_private;
500 return 0;
501}
502
503static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
504 size_t cnt, loff_t *ppos)
505{
506 int r;
507 char buf[40];
508 struct pil_device *pil = filp->private_data;
509
510 mutex_lock(&pil->lock);
511 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
512 mutex_unlock(&pil->lock);
513 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
514}
515
516static ssize_t msm_pil_debugfs_write(struct file *filp,
517 const char __user *ubuf, size_t cnt, loff_t *ppos)
518{
519 struct pil_device *pil = filp->private_data;
520 char buf[4];
521
522 if (cnt > sizeof(buf))
523 return -EINVAL;
524
525 if (copy_from_user(&buf, ubuf, cnt))
526 return -EFAULT;
527
528 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700529 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 return -EIO;
531 } else if (!strncmp(buf, "put", 3))
532 pil_put(pil);
533 else
534 return -EINVAL;
535
536 return cnt;
537}
538
539static const struct file_operations msm_pil_debugfs_fops = {
540 .open = msm_pil_debugfs_open,
541 .read = msm_pil_debugfs_read,
542 .write = msm_pil_debugfs_write,
543};
544
545static struct dentry *pil_base_dir;
546
Stephen Boyd6d67d252011-09-27 11:50:05 -0700547static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548{
549 pil_base_dir = debugfs_create_dir("pil", NULL);
550 if (!pil_base_dir) {
551 pil_base_dir = NULL;
552 return -ENOMEM;
553 }
554
555 return 0;
556}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700557
558static void __exit msm_pil_debugfs_exit(void)
559{
560 debugfs_remove_recursive(pil_base_dir);
561}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563static int msm_pil_debugfs_add(struct pil_device *pil)
564{
565 if (!pil_base_dir)
566 return -ENOMEM;
567
Stephen Boyd6d67d252011-09-27 11:50:05 -0700568 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
569 pil_base_dir, pil, &msm_pil_debugfs_fops);
570 return !pil->dentry ? -ENOMEM : 0;
571}
572
573static void msm_pil_debugfs_remove(struct pil_device *pil)
574{
575 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576}
577#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700578static int __init msm_pil_debugfs_init(void) { return 0; };
579static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700581static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582#endif
583
Stephen Boyd6d67d252011-09-27 11:50:05 -0700584static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700586 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700587 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700588 mutex_destroy(&pil->lock);
589 kfree(pil);
590}
591
592struct pil_device *msm_pil_register(struct pil_desc *desc)
593{
594 int err;
595 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700596 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700597
Stephen Boyd36974ec2012-03-22 01:30:59 -0700598 /* Ignore users who don't make any sense */
599 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
600 "invalid proxy voting. ignoring\n"))
601 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
602
Matt Wagantall2475e312012-05-25 19:56:33 -0700603 WARN(desc->ops->proxy_unvote && !desc->proxy_timeout,
604 "A proxy timeout of 0 ms was specified for %s. Specify one in "
605 "desc->proxy_timeout.\n", desc->name);
606
Stephen Boyd36974ec2012-03-22 01:30:59 -0700607 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700608 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700609 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
611 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700612 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700613 pil->owner = desc->owner;
614 pil->dev.parent = desc->dev;
615 pil->dev.bus = &pil_bus_type;
616 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617
Stephen Boyd36974ec2012-03-22 01:30:59 -0700618 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
619 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
620 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
621
Stephen Boyd6d67d252011-09-27 11:50:05 -0700622 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
623 err = device_register(&pil->dev);
624 if (err) {
625 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700626 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700627 mutex_destroy(&pil->lock);
628 kfree(pil);
629 return ERR_PTR(err);
630 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631
Stephen Boyd6d67d252011-09-27 11:50:05 -0700632 err = msm_pil_debugfs_add(pil);
633 if (err) {
634 device_unregister(&pil->dev);
635 return ERR_PTR(err);
636 }
637
638 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700640EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
Stephen Boyd6d67d252011-09-27 11:50:05 -0700642void msm_pil_unregister(struct pil_device *pil)
643{
644 if (IS_ERR_OR_NULL(pil))
645 return;
646
647 if (get_device(&pil->dev)) {
648 mutex_lock(&pil->lock);
649 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700650 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700651 msm_pil_debugfs_remove(pil);
652 device_unregister(&pil->dev);
653 mutex_unlock(&pil->lock);
654 put_device(&pil->dev);
655 }
656}
657EXPORT_SYMBOL(msm_pil_unregister);
658
Stephen Boyd80bde032012-03-16 00:14:42 -0700659static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
660{
661 switch (event) {
662 case PM_SUSPEND_PREPARE:
663 down_write(&pil_pm_rwsem);
664 break;
665 case PM_POST_SUSPEND:
666 up_write(&pil_pm_rwsem);
667 break;
668 }
669 return NOTIFY_DONE;
670}
671
672static struct notifier_block pil_pm_notifier = {
673 .notifier_call = pil_pm_notify,
674};
675
Stephen Boyd6d67d252011-09-27 11:50:05 -0700676static int __init msm_pil_init(void)
677{
678 int ret = msm_pil_debugfs_init();
679 if (ret)
680 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700681 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700682 return bus_register(&pil_bus_type);
683}
684subsys_initcall(msm_pil_init);
685
686static void __exit msm_pil_exit(void)
687{
688 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700689 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700690 msm_pil_debugfs_exit();
691}
692module_exit(msm_pil_exit);
693
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694MODULE_LICENSE("GPL v2");
695MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");