2 * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
4 * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
6 * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/sched.h>
25 #include <linux/device.h>
26 #include <asm/arch/mailbox.h>
27 #include "dsp_mbcmd.h"
31 static struct ipbuf_head *g_ipbuf;
33 struct ipbuf_sys *ipbuf_sys_da, *ipbuf_sys_ad;
34 static struct ipblink ipb_free = IPBLINK_INIT;
35 static int ipbuf_sys_hold_mem_active;
37 static ssize_t ipbuf_show(struct device *dev, struct device_attribute *attr,
39 static struct device_attribute dev_attr_ipbuf = __ATTR_RO(ipbuf);
45 device_remove_file(&dsp_device.dev, &dev_attr_ipbuf);
47 spin_lock(&ipb_free.lock);
48 RESET_IPBLINK(&ipb_free);
49 spin_unlock(&ipb_free.lock);
56 for (i = 0; i < ipbuf_sys_hold_mem_active; i++) {
57 dsp_mem_disable((void *)daram_base);
59 ipbuf_sys_hold_mem_active = 0;
62 int ipbuf_config(u16 ln, u16 lsz, void *base)
64 size_t lsz_byte = ((size_t)lsz) << 1;
72 if (((unsigned long)base) & 0x3) {
74 "omapdsp: global ipbuf address(0x%p) is not "
75 "32-bit aligned!\n", base);
79 if (dsp_address_validate(base, size, "global ipbuf") < 0)
82 g_ipbuf = kmalloc(sizeof(struct ipbuf_head) * ln, GFP_KERNEL);
83 if (g_ipbuf == NULL) {
85 "omapdsp: memory allocation for ipbuf failed.\n");
88 for (i = 0; i < ln; i++) {
91 top = base + (sizeof(struct ipbuf) + lsz_byte) * i;
92 btm = base + (sizeof(struct ipbuf) + lsz_byte) * (i+1) - 1;
93 g_ipbuf[i].p = (struct ipbuf *)top;
95 if (((unsigned long)top & 0xfffe0000) !=
96 ((unsigned long)btm & 0xfffe0000)) {
98 * an ipbuf line should not cross
102 "omapdsp: ipbuf[%d] crosses 64k-word boundary!\n"
103 " @0x%p, size=0x%08x\n", i, top, lsz_byte);
111 ipbcfg.bsycnt = ln; /* DSP holds all ipbufs initially. */
115 "omapdsp: IPBUF configuration\n"
116 " %d words * %d lines at 0x%p.\n",
117 ipbcfg.lsz, ipbcfg.ln, ipbcfg.base);
119 device_create_file(&dsp_device.dev, &dev_attr_ipbuf);
129 int ipbuf_sys_config(void *p, enum arm_dsp_dir_e dir)
131 char *dir_str = (dir == DIR_D2A) ? "D2A" : "A2D";
133 if (((unsigned long)p) & 0x3) {
135 "omapdsp: system ipbuf(%s) address(0x%p) is "
136 "not 32-bit aligned!\n", dir_str, p);
139 if (dsp_address_validate(p, sizeof(struct ipbuf_sys),
140 "system ipbuf(%s)", dir_str) < 0)
142 if (dsp_mem_type(p, sizeof(struct ipbuf_sys)) != MEM_TYPE_EXTERN) {
144 "omapdsp: system ipbuf(%s) is placed in"
145 " DSP internal memory.\n"
146 " It will prevent DSP from idling.\n", dir_str);
147 ipbuf_sys_hold_mem_active++;
149 * dsp_mem_enable() never fails because
150 * it has been already enabled in dspcfg process and
151 * this will just increment the usecount.
153 dsp_mem_enable((void *)daram_base);
164 int ipbuf_p_validate(void *p, enum arm_dsp_dir_e dir)
166 char *dir_str = (dir == DIR_D2A) ? "D2A" : "A2D";
168 if (((unsigned long)p) & 0x3) {
170 "omapdsp: private ipbuf(%s) address(0x%p) is "
171 "not 32-bit aligned!\n", dir_str, p);
174 return dsp_address_validate(p, sizeof(struct ipbuf_p),
175 "private ipbuf(%s)", dir_str);
179 * Global IPBUF operations
181 struct ipbuf_head *bid_to_ipbuf(u16 bid)
183 return &g_ipbuf[bid];
186 struct ipbuf_head *get_free_ipbuf(u8 tid)
188 struct ipbuf_head *ipb_h;
190 if (dsp_mem_enable_ipbuf() < 0)
193 spin_lock(&ipb_free.lock);
195 if (ipblink_empty(&ipb_free)) {
196 /* FIXME: wait on queue when not available. */
200 ipb_h = &g_ipbuf[ipb_free.top];
201 ipb_h->p->la = tid; /* lock */
202 __ipblink_del_top(&ipb_free);
204 spin_unlock(&ipb_free.lock);
205 dsp_mem_disable_ipbuf();
210 void release_ipbuf(struct ipbuf_head *ipb_h)
212 if (ipb_h->p->la == TID_FREE) {
214 "omapdsp: attempt to release unlocked IPBUF[%d].\n",
217 * FIXME: re-calc bsycnt
221 ipb_h->p->la = TID_FREE;
222 ipb_h->p->sa = TID_FREE;
223 ipblink_add_tail(&ipb_free, ipb_h->bid);
226 static int try_yld(struct ipbuf_head *ipb_h)
230 ipb_h->p->sa = TID_ANON;
231 status = mbcompose_send(BKYLD, 0, ipb_h->bid);
233 /* DSP is busy and ARM keeps this line. */
234 release_ipbuf(ipb_h);
238 ipb_bsycnt_inc(&ipbcfg);
243 * balancing ipbuf lines with DSP
245 static void do_balance_ipbuf(void)
247 while (ipbcfg.bsycnt <= ipbcfg.ln / 4) {
248 struct ipbuf_head *ipb_h;
250 if ((ipb_h = get_free_ipbuf(TID_ANON)) == NULL)
252 if (try_yld(ipb_h) < 0)
257 static DECLARE_WORK(balance_ipbuf_work, (void (*)(void *))do_balance_ipbuf,
260 void balance_ipbuf(void)
262 schedule_work(&balance_ipbuf_work);
265 /* for process context */
266 void unuse_ipbuf(struct ipbuf_head *ipb_h)
268 if (ipbcfg.bsycnt > ipbcfg.ln / 4) {
269 /* we don't have enough IPBUF lines. let's keep it. */
270 release_ipbuf(ipb_h);
272 /* we have enough IPBUF lines. let's return this line to DSP. */
273 ipb_h->p->la = TID_ANON;
279 /* for interrupt context */
280 void unuse_ipbuf_nowait(struct ipbuf_head *ipb_h)
282 release_ipbuf(ipb_h);
287 * functions called from mailbox interrupt routine
290 void mbox_err_ipbfull(void)
298 static ssize_t ipbuf_show(struct device *dev, struct device_attribute *attr,
304 for (bid = 0; bid < ipbcfg.ln; bid++) {
305 struct ipbuf_head *ipb_h = &g_ipbuf[bid];
306 u16 la = ipb_h->p->la;
307 u16 ld = ipb_h->p->ld;
310 if (len > PAGE_SIZE - 100) {
311 len += sprintf(buf + len, "out of buffer.\n");
315 len += sprintf(buf + len, "ipbuf[%d]: adr = 0x%p\n",
317 if (la == TID_FREE) {
318 len += sprintf(buf + len,
319 " DSPtask[%d]->Linux "
320 "(already read and now free for Linux)\n",
322 } else if (ld == TID_FREE) {
323 len += sprintf(buf + len,
324 " Linux->DSPtask[%d] "
325 "(already read and now free for DSP)\n",
327 } else if (ipbuf_is_held(ld, bid)) {
328 len += sprintf(buf + len,
329 " DSPtask[%d]->Linux "
330 "(waiting to be read)\n"
331 " count = %d\n", ld, c);
333 len += sprintf(buf + len,
334 " Linux->DSPtask[%d] "
335 "(waiting to be read)\n"
336 " count = %d\n", la, c);
340 len += sprintf(buf + len, "\nFree IPBUF link: ");
341 spin_lock(&ipb_free.lock);
342 ipblink_for_each(bid, &ipb_free) {
343 len += sprintf(buf + len, "%d ", bid);
345 spin_unlock(&ipb_free.lock);
346 len += sprintf(buf + len, "\n");
347 len += sprintf(buf + len, "IPBFULL error count: %ld\n",