]> Pileus Git - ~andy/linux/blob - drivers/staging/tidspbridge/core/ue_deh.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-next
[~andy/linux] / drivers / staging / tidspbridge / core / ue_deh.c
1 /*
2  * ue_deh.c
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * Implements upper edge DSP exception handling (DEH) functions.
7  *
8  * Copyright (C) 2005-2006 Texas Instruments, Inc.
9  * Copyright (C) 2010 Felipe Contreras
10  *
11  * This package is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/interrupt.h>
22
23 #include <dspbridge/dbdefs.h>
24 #include <dspbridge/dspdeh.h>
25 #include <dspbridge/dev.h>
26 #include "_tiomap.h"
27 #include "_deh.h"
28
29 #include <dspbridge/io_sm.h>
30 #include <dspbridge/drv.h>
31 #include <dspbridge/wdt.h>
32
33 static u32 fault_addr;
34
35 static void mmu_fault_dpc(unsigned long data)
36 {
37         struct deh_mgr *deh = (void *)data;
38
39         if (!deh)
40                 return;
41
42         bridge_deh_notify(deh, DSP_MMUFAULT, 0);
43 }
44
45 static irqreturn_t mmu_fault_isr(int irq, void *data)
46 {
47         struct deh_mgr *deh = data;
48         struct cfg_hostres *resources;
49         u32 event;
50
51         if (!deh)
52                 return IRQ_HANDLED;
53
54         resources = deh->bridge_context->resources;
55         if (!resources) {
56                 dev_dbg(bridge, "%s: Failed to get Host Resources\n",
57                                 __func__);
58                 return IRQ_HANDLED;
59         }
60
61         hw_mmu_event_status(resources->dmmu_base, &event);
62         if (event == HW_MMU_TRANSLATION_FAULT) {
63                 hw_mmu_fault_addr_read(resources->dmmu_base, &fault_addr);
64                 dev_dbg(bridge, "%s: event=0x%x, fault_addr=0x%x\n", __func__,
65                                 event, fault_addr);
66                 /*
67                  * Schedule a DPC directly. In the future, it may be
68                  * necessary to check if DSP MMU fault is intended for
69                  * Bridge.
70                  */
71                 tasklet_schedule(&deh->dpc_tasklet);
72
73                 /* Disable the MMU events, else once we clear it will
74                  * start to raise INTs again */
75                 hw_mmu_event_disable(resources->dmmu_base,
76                                 HW_MMU_TRANSLATION_FAULT);
77         } else {
78                 hw_mmu_event_disable(resources->dmmu_base,
79                                 HW_MMU_ALL_INTERRUPTS);
80         }
81         return IRQ_HANDLED;
82 }
83
84 int bridge_deh_create(struct deh_mgr **ret_deh,
85                 struct dev_object *hdev_obj)
86 {
87         int status;
88         struct deh_mgr *deh;
89         struct bridge_dev_context *hbridge_context = NULL;
90
91         /*  Message manager will be created when a file is loaded, since
92          *  size of message buffer in shared memory is configurable in
93          *  the base image. */
94         /* Get Bridge context info. */
95         dev_get_bridge_context(hdev_obj, &hbridge_context);
96         /* Allocate IO manager object: */
97         deh = kzalloc(sizeof(*deh), GFP_KERNEL);
98         if (!deh) {
99                 status = -ENOMEM;
100                 goto err;
101         }
102
103         /* Create an NTFY object to manage notifications */
104         deh->ntfy_obj = kmalloc(sizeof(struct ntfy_object), GFP_KERNEL);
105         if (!deh->ntfy_obj) {
106                 status = -ENOMEM;
107                 goto err;
108         }
109         ntfy_init(deh->ntfy_obj);
110
111         /* Create a MMUfault DPC */
112         tasklet_init(&deh->dpc_tasklet, mmu_fault_dpc, (u32) deh);
113
114         /* Fill in context structure */
115         deh->bridge_context = hbridge_context;
116
117         /* Install ISR function for DSP MMU fault */
118         status = request_irq(INT_DSP_MMU_IRQ, mmu_fault_isr, 0,
119                         "DspBridge\tiommu fault", deh);
120         if (status < 0)
121                 goto err;
122
123         *ret_deh = deh;
124         return 0;
125
126 err:
127         bridge_deh_destroy(deh);
128         *ret_deh = NULL;
129         return status;
130 }
131
132 int bridge_deh_destroy(struct deh_mgr *deh)
133 {
134         if (!deh)
135                 return -EFAULT;
136
137         /* If notification object exists, delete it */
138         if (deh->ntfy_obj) {
139                 ntfy_delete(deh->ntfy_obj);
140                 kfree(deh->ntfy_obj);
141         }
142         /* Disable DSP MMU fault */
143         free_irq(INT_DSP_MMU_IRQ, deh);
144
145         /* Free DPC object */
146         tasklet_kill(&deh->dpc_tasklet);
147
148         /* Deallocate the DEH manager object */
149         kfree(deh);
150
151         return 0;
152 }
153
154 int bridge_deh_register_notify(struct deh_mgr *deh, u32 event_mask,
155                 u32 notify_type,
156                 struct dsp_notification *hnotification)
157 {
158         if (!deh)
159                 return -EFAULT;
160
161         if (event_mask)
162                 return ntfy_register(deh->ntfy_obj, hnotification,
163                                 event_mask, notify_type);
164         else
165                 return ntfy_unregister(deh->ntfy_obj, hnotification);
166 }
167
168 #ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
169 static void mmu_fault_print_stack(struct bridge_dev_context *dev_context)
170 {
171         struct cfg_hostres *resources;
172         struct hw_mmu_map_attrs_t map_attrs = {
173                 .endianism = HW_LITTLE_ENDIAN,
174                 .element_size = HW_ELEM_SIZE16BIT,
175                 .mixed_size = HW_MMU_CPUES,
176         };
177         void *dummy_va_addr;
178
179         resources = dev_context->resources;
180         dummy_va_addr = (void *)__get_free_page(GFP_ATOMIC);
181
182         /*
183          * Before acking the MMU fault, let's make sure MMU can only
184          * access entry #0. Then add a new entry so that the DSP OS
185          * can continue in order to dump the stack.
186          */
187         hw_mmu_twl_disable(resources->dmmu_base);
188         hw_mmu_tlb_flush_all(resources->dmmu_base);
189
190         hw_mmu_tlb_add(resources->dmmu_base,
191                         virt_to_phys(dummy_va_addr), fault_addr,
192                         HW_PAGE_SIZE4KB, 1,
193                         &map_attrs, HW_SET, HW_SET);
194
195         dsp_clk_enable(DSP_CLK_GPT8);
196
197         dsp_gpt_wait_overflow(DSP_CLK_GPT8, 0xfffffffe);
198
199         /* Clear MMU interrupt */
200         hw_mmu_event_ack(resources->dmmu_base,
201                         HW_MMU_TRANSLATION_FAULT);
202         dump_dsp_stack(dev_context);
203         dsp_clk_disable(DSP_CLK_GPT8);
204
205         hw_mmu_disable(resources->dmmu_base);
206         free_page((unsigned long)dummy_va_addr);
207 }
208 #endif
209
210 static inline const char *event_to_string(int event)
211 {
212         switch (event) {
213         case DSP_SYSERROR: return "DSP_SYSERROR"; break;
214         case DSP_MMUFAULT: return "DSP_MMUFAULT"; break;
215         case DSP_PWRERROR: return "DSP_PWRERROR"; break;
216         case DSP_WDTOVERFLOW: return "DSP_WDTOVERFLOW"; break;
217         default: return "unknown event"; break;
218         }
219 }
220
221 void bridge_deh_notify(struct deh_mgr *deh, int event, int info)
222 {
223         struct bridge_dev_context *dev_context;
224         const char *str = event_to_string(event);
225
226         if (!deh)
227                 return;
228
229         dev_dbg(bridge, "%s: device exception", __func__);
230         dev_context = deh->bridge_context;
231
232         switch (event) {
233         case DSP_SYSERROR:
234                 dev_err(bridge, "%s: %s, info=0x%x", __func__,
235                                 str, info);
236 #ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
237                 dump_dl_modules(dev_context);
238                 dump_dsp_stack(dev_context);
239 #endif
240                 break;
241         case DSP_MMUFAULT:
242                 dev_err(bridge, "%s: %s, addr=0x%x", __func__,
243                                 str, fault_addr);
244 #ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
245                 print_dsp_trace_buffer(dev_context);
246                 dump_dl_modules(dev_context);
247                 mmu_fault_print_stack(dev_context);
248 #endif
249                 break;
250         default:
251                 dev_err(bridge, "%s: %s", __func__, str);
252                 break;
253         }
254
255         /* Filter subsequent notifications when an error occurs */
256         if (dev_context->brd_state != BRD_ERROR) {
257                 ntfy_notify(deh->ntfy_obj, event);
258 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
259                 bridge_recover_schedule();
260 #endif
261         }
262
263         /* Set the Board state as ERROR */
264         dev_context->brd_state = BRD_ERROR;
265         /* Disable all the clocks that were enabled by DSP */
266         dsp_clock_disable_all(dev_context->dsp_per_clks);
267         /*
268          * Avoid the subsequent WDT if it happens once,
269          * also if fatal error occurs.
270          */
271         dsp_wdt_enable(false);
272 }