2 * Copyright (C) 2010 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/percpu.h>
16 #include <linux/slab.h>
18 #include <asm/fiq_glue.h>
20 extern unsigned char fiq_glue, fiq_glue_end;
21 extern void fiq_glue_setup(void *func, void *data, void *sp,
22 fiq_return_handler_t fiq_return_handler);
24 static struct fiq_handler fiq_debbuger_fiq_handler = {
27 DEFINE_PER_CPU(void *, fiq_stack);
28 static struct fiq_glue_handler *current_handler;
29 static fiq_return_handler_t fiq_return_handler;
30 static DEFINE_MUTEX(fiq_glue_lock);
32 static void fiq_glue_setup_helper(void *info)
34 struct fiq_glue_handler *handler = info;
35 fiq_glue_setup(handler->fiq, handler,
36 __get_cpu_var(fiq_stack) + THREAD_START_SP,
40 int fiq_glue_register_handler(struct fiq_glue_handler *handler)
45 if (!handler || !handler->fiq)
48 mutex_lock(&fiq_glue_lock);
54 for_each_possible_cpu(cpu) {
56 stack = (void *)__get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
57 if (WARN_ON(!stack)) {
59 goto err_alloc_fiq_stack;
61 per_cpu(fiq_stack, cpu) = stack;
64 ret = claim_fiq(&fiq_debbuger_fiq_handler);
68 current_handler = handler;
69 on_each_cpu(fiq_glue_setup_helper, handler, true);
70 set_fiq_handler(&fiq_glue, &fiq_glue_end - &fiq_glue);
72 mutex_unlock(&fiq_glue_lock);
77 for_each_possible_cpu(cpu) {
78 __free_pages(per_cpu(fiq_stack, cpu), THREAD_SIZE_ORDER);
79 per_cpu(fiq_stack, cpu) = NULL;
82 mutex_unlock(&fiq_glue_lock);
86 static void fiq_glue_update_return_handler(void (*fiq_return)(void))
88 fiq_return_handler = fiq_return;
90 on_each_cpu(fiq_glue_setup_helper, current_handler, true);
93 int fiq_glue_set_return_handler(void (*fiq_return)(void))
97 mutex_lock(&fiq_glue_lock);
98 if (fiq_return_handler) {
102 fiq_glue_update_return_handler(fiq_return);
105 mutex_unlock(&fiq_glue_lock);
109 EXPORT_SYMBOL(fiq_glue_set_return_handler);
111 int fiq_glue_clear_return_handler(void (*fiq_return)(void))
115 mutex_lock(&fiq_glue_lock);
116 if (WARN_ON(fiq_return_handler != fiq_return)) {
120 fiq_glue_update_return_handler(NULL);
123 mutex_unlock(&fiq_glue_lock);
127 EXPORT_SYMBOL(fiq_glue_clear_return_handler);
130 * fiq_glue_resume - Restore fiqs after suspend or low power idle states
132 * This must be called before calling local_fiq_enable after returning from a
133 * power state where the fiq mode registers were lost. If a driver provided
134 * a resume hook when it registered the handler it will be called.
137 void fiq_glue_resume(void)
139 if (!current_handler)
141 fiq_glue_setup(current_handler->fiq, current_handler,
142 __get_cpu_var(fiq_stack) + THREAD_START_SP,
144 if (current_handler->resume)
145 current_handler->resume(current_handler);