ue_deh.c revision 6c4c899ee27963357a7df1f5e15a5677978cd842
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#include <plat/dmtimer.h>
23
24#include <dspbridge/dbdefs.h>
25#include <dspbridge/dspdeh.h>
26#include <dspbridge/dev.h>
27#include "_tiomap.h"
28#include "_deh.h"
29
30#include <dspbridge/io_sm.h>
31#include <dspbridge/drv.h>
32#include <dspbridge/wdt.h>
33
34static u32 fault_addr;
35
36static void mmu_fault_dpc(unsigned long data)
37{
38	struct deh_mgr *deh = (void *)data;
39
40	if (!deh)
41		return;
42
43	bridge_deh_notify(deh, DSP_MMUFAULT, 0);
44}
45
46static irqreturn_t mmu_fault_isr(int irq, void *data)
47{
48	struct deh_mgr *deh = data;
49	struct cfg_hostres *resources;
50	u32 event;
51
52	if (!deh)
53		return IRQ_HANDLED;
54
55	resources = deh->hbridge_context->resources;
56	if (!resources) {
57		dev_dbg(bridge, "%s: Failed to get Host Resources\n",
58				__func__);
59		return IRQ_HANDLED;
60	}
61
62	hw_mmu_event_status(resources->dw_dmmu_base, &event);
63	if (event == HW_MMU_TRANSLATION_FAULT) {
64		hw_mmu_fault_addr_read(resources->dw_dmmu_base, &fault_addr);
65		dev_dbg(bridge, "%s: event=0x%x, fault_addr=0x%x\n", __func__,
66				event, fault_addr);
67		/*
68		 * Schedule a DPC directly. In the future, it may be
69		 * necessary to check if DSP MMU fault is intended for
70		 * Bridge.
71		 */
72		tasklet_schedule(&deh->dpc_tasklet);
73
74		/* Disable the MMU events, else once we clear it will
75		 * start to raise INTs again */
76		hw_mmu_event_disable(resources->dw_dmmu_base,
77				HW_MMU_TRANSLATION_FAULT);
78	} else {
79		hw_mmu_event_disable(resources->dw_dmmu_base,
80				HW_MMU_ALL_INTERRUPTS);
81	}
82	return IRQ_HANDLED;
83}
84
85int bridge_deh_create(struct deh_mgr **ret_deh,
86		struct dev_object *hdev_obj)
87{
88	int status;
89	struct deh_mgr *deh;
90	struct bridge_dev_context *hbridge_context = NULL;
91
92	/*  Message manager will be created when a file is loaded, since
93	 *  size of message buffer in shared memory is configurable in
94	 *  the base image. */
95	/* Get Bridge context info. */
96	dev_get_bridge_context(hdev_obj, &hbridge_context);
97	/* Allocate IO manager object: */
98	deh = kzalloc(sizeof(*deh), GFP_KERNEL);
99	if (!deh) {
100		status = -ENOMEM;
101		goto err;
102	}
103
104	/* Create an NTFY object to manage notifications */
105	deh->ntfy_obj = kmalloc(sizeof(struct ntfy_object), GFP_KERNEL);
106	if (!deh->ntfy_obj) {
107		status = -ENOMEM;
108		goto err;
109	}
110	ntfy_init(deh->ntfy_obj);
111
112	/* Create a MMUfault DPC */
113	tasklet_init(&deh->dpc_tasklet, mmu_fault_dpc, (u32) deh);
114
115	/* Fill in context structure */
116	deh->hbridge_context = hbridge_context;
117
118	*ret_deh = deh;
119	return 0;
120
121err:
122	bridge_deh_destroy(deh);
123	*ret_deh = NULL;
124	return status;
125}
126
127int bridge_deh_destroy(struct deh_mgr *deh)
128{
129	if (!deh)
130		return -EFAULT;
131
132	/* If notification object exists, delete it */
133	if (deh->ntfy_obj) {
134		ntfy_delete(deh->ntfy_obj);
135		kfree(deh->ntfy_obj);
136	}
137
138	/* Free DPC object */
139	tasklet_kill(&deh->dpc_tasklet);
140
141	/* Deallocate the DEH manager object */
142	kfree(deh);
143
144	return 0;
145}
146
147int bridge_deh_register_notify(struct deh_mgr *deh, u32 event_mask,
148		u32 notify_type,
149		struct dsp_notification *hnotification)
150{
151	if (!deh)
152		return -EFAULT;
153
154	if (event_mask)
155		return ntfy_register(deh->ntfy_obj, hnotification,
156				event_mask, notify_type);
157	else
158		return ntfy_unregister(deh->ntfy_obj, hnotification);
159}
160
161#ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
162static void mmu_fault_print_stack(struct bridge_dev_context *dev_context)
163{
164	struct cfg_hostres *resources;
165	struct hw_mmu_map_attrs_t map_attrs = {
166		.endianism = HW_LITTLE_ENDIAN,
167		.element_size = HW_ELEM_SIZE16BIT,
168		.mixed_size = HW_MMU_CPUES,
169	};
170	void *dummy_va_addr;
171
172	resources = dev_context->resources;
173	dummy_va_addr = (void*)__get_free_page(GFP_ATOMIC);
174
175	/*
176	 * Before acking the MMU fault, let's make sure MMU can only
177	 * access entry #0. Then add a new entry so that the DSP OS
178	 * can continue in order to dump the stack.
179	 */
180	hw_mmu_twl_disable(resources->dw_dmmu_base);
181	hw_mmu_tlb_flush_all(resources->dw_dmmu_base);
182
183	hw_mmu_tlb_add(resources->dw_dmmu_base,
184			virt_to_phys(dummy_va_addr), fault_addr,
185			HW_PAGE_SIZE4KB, 1,
186			&map_attrs, HW_SET, HW_SET);
187
188	dsp_clk_enable(DSP_CLK_GPT8);
189
190	dsp_gpt_wait_overflow(DSP_CLK_GPT8, 0xfffffffe);
191
192	/* Clear MMU interrupt */
193	hw_mmu_event_ack(resources->dw_dmmu_base,
194			HW_MMU_TRANSLATION_FAULT);
195	dump_dsp_stack(dev_context);
196	dsp_clk_disable(DSP_CLK_GPT8);
197
198	hw_mmu_disable(resources->dw_dmmu_base);
199	free_page((unsigned long)dummy_va_addr);
200}
201#endif
202
203static inline const char *event_to_string(int event)
204{
205	switch (event) {
206	case DSP_SYSERROR: return "DSP_SYSERROR"; break;
207	case DSP_MMUFAULT: return "DSP_MMUFAULT"; break;
208	case DSP_PWRERROR: return "DSP_PWRERROR"; break;
209	case DSP_WDTOVERFLOW: return "DSP_WDTOVERFLOW"; break;
210	default: return "unkown event"; break;
211	}
212}
213
214void bridge_deh_notify(struct deh_mgr *deh, int event, int info)
215{
216	struct bridge_dev_context *dev_context;
217	const char *str = event_to_string(event);
218
219	if (!deh)
220		return;
221
222	dev_dbg(bridge, "%s: device exception", __func__);
223	dev_context = deh->hbridge_context;
224
225	switch (event) {
226	case DSP_SYSERROR:
227		dev_err(bridge, "%s: %s, info=0x%x", __func__,
228				str, info);
229#ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
230		dump_dl_modules(dev_context);
231		dump_dsp_stack(dev_context);
232#endif
233		break;
234	case DSP_MMUFAULT:
235		dev_err(bridge, "%s: %s, addr=0x%x", __func__,
236				str, fault_addr);
237#ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
238		print_dsp_trace_buffer(dev_context);
239		dump_dl_modules(dev_context);
240		mmu_fault_print_stack(dev_context);
241#endif
242		break;
243	default:
244		dev_err(bridge, "%s: %s", __func__, str);
245		break;
246	}
247
248	/* Filter subsequent notifications when an error occurs */
249	if (dev_context->dw_brd_state != BRD_ERROR) {
250		ntfy_notify(deh->ntfy_obj, event);
251#ifdef CONFIG_TIDSPBRIDGE_RECOVERY
252		bridge_recover_schedule();
253#endif
254	}
255
256	/* Set the Board state as ERROR */
257	dev_context->dw_brd_state = BRD_ERROR;
258	/* Disable all the clocks that were enabled by DSP */
259	dsp_clock_disable_all(dev_context->dsp_per_clks);
260	/*
261	 * Avoid the subsequent WDT if it happens once,
262	 * also if fatal error occurs.
263	 */
264	dsp_wdt_enable(false);
265}
266