1 /*
2 * Copyright (C) 2010 Google, Inc.
3 *
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.
7 *
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.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/percpu.h>
16 #include <linux/slab.h>
17 #include <asm/fiq.h>
18 #include <asm/fiq_glue.h>
19
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);
23
24 static struct fiq_handler fiq_debbuger_fiq_handler = {
25 .name = "fiq_glue",
26 };
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);
31
fiq_glue_setup_helper(void * info)32 static void fiq_glue_setup_helper(void *info)
33 {
34 struct fiq_glue_handler *handler = info;
35 fiq_glue_setup(handler->fiq, handler,
36 __get_cpu_var(fiq_stack) + THREAD_START_SP,
37 fiq_return_handler);
38 }
39
fiq_glue_register_handler(struct fiq_glue_handler * handler)40 int fiq_glue_register_handler(struct fiq_glue_handler *handler)
41 {
42 int ret;
43 int cpu;
44
45 if (!handler || !handler->fiq)
46 return -EINVAL;
47
48 mutex_lock(&fiq_glue_lock);
49 if (fiq_stack) {
50 ret = -EBUSY;
51 goto err_busy;
52 }
53
54 for_each_possible_cpu(cpu) {
55 void *stack;
56 stack = (void *)__get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
57 if (WARN_ON(!stack)) {
58 ret = -ENOMEM;
59 goto err_alloc_fiq_stack;
60 }
61 per_cpu(fiq_stack, cpu) = stack;
62 }
63
64 ret = claim_fiq(&fiq_debbuger_fiq_handler);
65 if (WARN_ON(ret))
66 goto err_claim_fiq;
67
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);
71
72 mutex_unlock(&fiq_glue_lock);
73 return 0;
74
75 err_claim_fiq:
76 err_alloc_fiq_stack:
77 for_each_possible_cpu(cpu) {
78 __free_pages(per_cpu(fiq_stack, cpu), THREAD_SIZE_ORDER);
79 per_cpu(fiq_stack, cpu) = NULL;
80 }
81 err_busy:
82 mutex_unlock(&fiq_glue_lock);
83 return ret;
84 }
85
fiq_glue_update_return_handler(void (* fiq_return)(void))86 static void fiq_glue_update_return_handler(void (*fiq_return)(void))
87 {
88 fiq_return_handler = fiq_return;
89 if (current_handler)
90 on_each_cpu(fiq_glue_setup_helper, current_handler, true);
91 }
92
fiq_glue_set_return_handler(void (* fiq_return)(void))93 int fiq_glue_set_return_handler(void (*fiq_return)(void))
94 {
95 int ret;
96
97 mutex_lock(&fiq_glue_lock);
98 if (fiq_return_handler) {
99 ret = -EBUSY;
100 goto err_busy;
101 }
102 fiq_glue_update_return_handler(fiq_return);
103 ret = 0;
104 err_busy:
105 mutex_unlock(&fiq_glue_lock);
106
107 return ret;
108 }
109 EXPORT_SYMBOL(fiq_glue_set_return_handler);
110
fiq_glue_clear_return_handler(void (* fiq_return)(void))111 int fiq_glue_clear_return_handler(void (*fiq_return)(void))
112 {
113 int ret;
114
115 mutex_lock(&fiq_glue_lock);
116 if (WARN_ON(fiq_return_handler != fiq_return)) {
117 ret = -EINVAL;
118 goto err_inval;
119 }
120 fiq_glue_update_return_handler(NULL);
121 ret = 0;
122 err_inval:
123 mutex_unlock(&fiq_glue_lock);
124
125 return ret;
126 }
127 EXPORT_SYMBOL(fiq_glue_clear_return_handler);
128
129 /**
130 * fiq_glue_resume - Restore fiqs after suspend or low power idle states
131 *
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.
135 */
136
fiq_glue_resume(void)137 void fiq_glue_resume(void)
138 {
139 if (!current_handler)
140 return;
141 fiq_glue_setup(current_handler->fiq, current_handler,
142 __get_cpu_var(fiq_stack) + THREAD_START_SP,
143 fiq_return_handler);
144 if (current_handler->resume)
145 current_handler->resume(current_handler);
146 }
147
148