]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/dsp/dsp_mem.c
ARM: OMAP: Add interrupt handling interface in MMU FWK
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / dsp / dsp_mem.c
1 /*
2  * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
3  *
4  * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7  *
8  * Conversion to mempool API and ARM MMU section mapping
9  * by Paul Mundt <paul.mundt@nokia.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/fb.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/mempool.h>
34 #include <linux/clk.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/pgalloc.h>
39 #include <asm/pgtable.h>
40 #include <asm/arch/tc.h>
41 #include <asm/arch/omapfb.h>
42 #include <asm/arch/dsp.h>
43 #include <asm/arch/mailbox.h>
44 #include <asm/arch/mmu.h>
45 #include "dsp_mbcmd.h"
46 #include "dsp.h"
47 #include "ipbuf.h"
48
49 #if defined(CONFIG_ARCH_OMAP1)
50 #include "../../mach-omap1/mmu.h"
51 #elif defined(CONFIG_ARCH_OMAP2)
52 #include "../../mach-omap2/mmu.h"
53 #endif
54
55 #include "mmu.h"
56
57 static struct mem_sync_struct mem_sync;
58
59 int dsp_mem_sync_inc(void)
60 {
61         if (dsp_mem_enable((void *)dspmem_base) < 0)
62                 return -1;
63         if (mem_sync.DARAM)
64                 mem_sync.DARAM->ad_arm++;
65         if (mem_sync.SARAM)
66                 mem_sync.SARAM->ad_arm++;
67         if (mem_sync.SDRAM)
68                 mem_sync.SDRAM->ad_arm++;
69         dsp_mem_disable((void *)dspmem_base);
70
71         return 0;
72 }
73
74 /*
75  * dsp_mem_sync_config() is called from mbox1 workqueue
76  */
77 int dsp_mem_sync_config(struct mem_sync_struct *sync)
78 {
79         size_t sync_seq_sz = sizeof(struct sync_seq);
80
81 #ifdef OLD_BINARY_SUPPORT
82         if (sync == NULL) {
83                 memset(&mem_sync, 0, sizeof(struct mem_sync_struct));
84                 return 0;
85         }
86 #endif
87         if ((dsp_mem_type(sync->DARAM, sync_seq_sz) != MEM_TYPE_DARAM) ||
88             (dsp_mem_type(sync->SARAM, sync_seq_sz) != MEM_TYPE_SARAM) ||
89             (dsp_mem_type(sync->SDRAM, sync_seq_sz) != MEM_TYPE_EXTERN)) {
90                 printk(KERN_ERR
91                        "omapdsp: mem_sync address validation failure!\n"
92                        "  mem_sync.DARAM = 0x%p,\n"
93                        "  mem_sync.SARAM = 0x%p,\n"
94                        "  mem_sync.SDRAM = 0x%p,\n",
95                        sync->DARAM, sync->SARAM, sync->SDRAM);
96                 return -1;
97         }
98
99         memcpy(&mem_sync, sync, sizeof(struct mem_sync_struct));
100
101         return 0;
102 }
103
104
105 enum dsp_mem_type_e dsp_mem_type(void *vadr, size_t len)
106 {
107         void *ds = (void *)daram_base;
108         void *de = (void *)daram_base + daram_size;
109         void *ss = (void *)saram_base;
110         void *se = (void *)saram_base + saram_size;
111         int ret;
112
113         if ((vadr >= ds) && (vadr < de)) {
114                 if (vadr + len > de)
115                         return MEM_TYPE_CROSSING;
116                 else
117                         return MEM_TYPE_DARAM;
118         } else if ((vadr >= ss) && (vadr < se)) {
119                 if (vadr + len > se)
120                         return MEM_TYPE_CROSSING;
121                 else
122                         return MEM_TYPE_SARAM;
123         } else {
124                 down_read(&dsp_mmu.exmap_sem);
125                 if (exmap_valid(&dsp_mmu, vadr, len))
126                         ret = MEM_TYPE_EXTERN;
127                 else
128                         ret = MEM_TYPE_NONE;
129                 up_read(&dsp_mmu.exmap_sem);
130                 return ret;
131         }
132 }
133
134 int dsp_address_validate(void *p, size_t len, char *fmt, ...)
135 {
136         char s[64];
137         va_list args;
138
139         if (dsp_mem_type(p, len) > 0)
140                 return 0;
141
142         if (fmt == NULL)
143                 goto out;
144
145         va_start(args, fmt);
146         vsprintf(s, fmt, args);
147         va_end(args);
148         printk(KERN_ERR
149                "omapdsp: %s address(0x%p) and size(0x%x) is not valid!\n"
150                "(crossing different type of memories, or external memory\n"
151                "space where no actual memory is mapped)\n", s, p, len);
152  out:
153         return -1;
154 }
155
156 #ifdef CONFIG_OMAP_DSP_FBEXPORT
157
158 static inline unsigned long lineup_offset(unsigned long adr,
159                                           unsigned long ref,
160                                           unsigned long mask)
161 {
162         unsigned long newadr;
163
164         newadr = (adr & ~mask) | (ref & mask);
165         if (newadr < adr)
166                 newadr += mask + 1;
167         return newadr;
168 }
169
170 /*
171  * fb update functions:
172  * fbupd_response() is executed by the workqueue.
173  * fbupd_cb() is called when fb update is done, in interrupt context.
174  * mbox_fbupd() is called when KFUNC:FBCTL:UPD is received from DSP.
175  */
176 static void fbupd_response(struct work_struct *unused)
177 {
178         int status;
179
180         status = mbcompose_send(KFUNC, KFUNC_FBCTL, FBCTL_UPD);
181         if (status == 0)
182                 return;
183
184         /* FIXME: DSP is busy !! */
185         printk(KERN_ERR
186                "omapdsp:"
187                "DSP is busy when trying to send FBCTL:UPD response!\n");
188 }
189
190 static DECLARE_WORK(fbupd_response_work, fbupd_response);
191
192 static void fbupd_cb(void *arg)
193 {
194         schedule_work(&fbupd_response_work);
195 }
196
197 void mbox_fbctl_upd(void)
198 {
199         struct omapfb_update_window win;
200         volatile unsigned short *buf = ipbuf_sys_da->d;
201
202         if (sync_with_dsp(&ipbuf_sys_da->s, TID_ANON, 5000) < 0) {
203                 printk(KERN_ERR "mbox: FBCTL:UPD - IPBUF sync failed!\n");
204                 return;
205         }
206         win.x = buf[0];
207         win.y = buf[1];
208         win.width = buf[2];
209         win.height = buf[3];
210         win.format = buf[4];
211         release_ipbuf_pvt(ipbuf_sys_da);
212
213         if (!omapfb_ready) {
214                 printk(KERN_WARNING
215                        "omapdsp: fbupd() called while HWA742 is not ready!\n");
216                 return;
217         }
218         omapfb_update_window_async(registered_fb[0], &win, fbupd_cb, NULL);
219 }
220
221 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
222 static int omapfb_notifier_cb(struct notifier_block *omapfb_nb,
223                               unsigned long event, void *fbi)
224 {
225         pr_info("omapfb_notifier_cb(): event = %s\n",
226                 (event == OMAPFB_EVENT_READY)    ? "READY" :
227                 (event == OMAPFB_EVENT_DISABLED) ? "DISABLED" : "Unknown");
228         if (event == OMAPFB_EVENT_READY)
229                 omapfb_ready = 1;
230         else if (event == OMAPFB_EVENT_DISABLED)
231                 omapfb_ready = 0;
232         return 0;
233 }
234 #endif
235
236 static int dsp_fbexport(dsp_long_t *dspadr)
237 {
238         dsp_long_t dspadr_actual;
239         unsigned long padr_sys, padr, fbsz_sys, fbsz;
240         int cnt, status;
241
242         pr_debug( "omapdsp: frame buffer export\n");
243
244 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
245         if (omapfb_nb) {
246                 printk(KERN_WARNING
247                        "omapdsp: frame buffer has been exported already!\n");
248                 return -EBUSY;
249         }
250 #endif
251
252         if (num_registered_fb == 0) {
253                 pr_info("omapdsp: frame buffer not registered.\n");
254                 return -EINVAL;
255         }
256         if (num_registered_fb != 1) {
257                 pr_info("omapdsp: %d frame buffers found. we use first one.\n",
258                         num_registered_fb);
259         }
260         padr_sys = registered_fb[0]->fix.smem_start;
261         fbsz_sys = registered_fb[0]->fix.smem_len;
262         if (fbsz_sys == 0) {
263                 printk(KERN_ERR
264                        "omapdsp: framebuffer doesn't seem to be configured "
265                        "correctly! (size=0)\n");
266                 return -EINVAL;
267         }
268
269         /*
270          * align padr and fbsz to 4kB boundary
271          * (should be noted to the user afterwards!)
272          */
273         padr = padr_sys & ~(SZ_4K-1);
274         fbsz = (fbsz_sys + padr_sys - padr + SZ_4K-1) & ~(SZ_4K-1);
275
276         /* line up dspadr offset with padr */
277         dspadr_actual =
278                 (fbsz > SZ_1M) ?  lineup_offset(*dspadr, padr, SZ_1M-1) :
279                 (fbsz > SZ_64K) ? lineup_offset(*dspadr, padr, SZ_64K-1) :
280                 /* (fbsz > SZ_4KB) ? */ *dspadr;
281         if (dspadr_actual != *dspadr)
282                 pr_debug(
283                         "omapdsp: actual dspadr for FBEXPORT = %08x\n",
284                         dspadr_actual);
285         *dspadr = dspadr_actual;
286
287         cnt = omap_mmu_exmap(&dsp_mmu, dspadr_actual, padr, fbsz,
288                              EXMAP_TYPE_FB);
289         if (cnt < 0) {
290                 printk(KERN_ERR "omapdsp: exmap failure.\n");
291                 return cnt;
292         }
293
294         if ((padr != padr_sys) || (fbsz != fbsz_sys)) {
295                 printk(KERN_WARNING
296 "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
297 "  !!  screen base address or size is not aligned in 4kB:           !!\n"
298 "  !!    actual screen  adr = %08lx, size = %08lx                   !!\n"
299 "  !!    exporting      adr = %08lx, size = %08lx                   !!\n"
300 "  !!  Make sure that the framebuffer is allocated with 4kB-order!  !!\n"
301 "  !!  Otherwise DSP can corrupt the kernel memory.                 !!\n"
302 "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",
303                        padr_sys, fbsz_sys, padr, fbsz);
304         }
305
306         /* increase the DMA priority */
307         set_emiff_dma_prio(15);
308
309 #ifdef CONFIG_FB_OMAP_LCDC_EXTERNAL
310         omapfb_nb = kzalloc(sizeof(struct omapfb_notifier_block), GFP_KERNEL);
311         if (omapfb_nb == NULL) {
312                 printk(KERN_ERR
313                        "omapdsp: failed to allocate memory for omapfb_nb!\n");
314                 omap_mmu_exunmap(&dsp_mmu, (unsigned long)dspadr);
315                 return -ENOMEM;
316         }
317
318         status = omapfb_register_client(omapfb_nb, omapfb_notifier_cb, NULL);
319         if (status)
320                 pr_info("omapfb_register_client(): failure(%d)\n", status);
321 #endif
322
323         return cnt;
324 }
325 #else
326 void mbox_fbctl_upd(void) { }
327 #endif
328
329 /* dsp/mem fops: backward compatibility */
330 static ssize_t dsp_mem_read(struct file *file, char __user *buf, size_t count,
331                             loff_t *ppos)
332 {
333         return __omap_mmu_mem_read(&dsp_mmu, (char __user *)buf, *ppos, count);
334 }
335
336 static ssize_t dsp_mem_write(struct file *file, const char __user *buf,
337                              size_t count, loff_t *ppos)
338 {
339         return __omap_mmu_mem_write(&dsp_mmu,
340                                     (char __user *)buf, *ppos, count);
341 }
342
343 static int dsp_mem_ioctl(struct inode *inode, struct file *file,
344                          unsigned int cmd, unsigned long arg)
345 {
346         struct omap_dsp_mapinfo mapinfo;
347         dsp_long_t dspadr;
348         int ret;
349         __u32 size;
350
351         switch (cmd) {
352         case MEM_IOCTL_MMUINIT:
353                 if (dsp_mmu.exmap_tbl)
354                         omap_mmu_unregister(&dsp_mmu);
355                 dsp_mem_ipi_init();
356                 return omap_mmu_register(&dsp_mmu);
357
358         case MEM_IOCTL_EXMAP:
359                 if (copy_from_user(&mapinfo, (void __user *)arg,
360                                    sizeof(mapinfo)))
361                         return -EFAULT;
362                 return omap_mmu_exmap(&dsp_mmu, mapinfo.dspadr,
363                                       0, mapinfo.size, EXMAP_TYPE_MEM);
364
365         case MEM_IOCTL_EXUNMAP:
366                 return omap_mmu_exunmap(&dsp_mmu, (unsigned long)arg);
367
368         case MEM_IOCTL_EXMAP_FLUSH:
369                 omap_mmu_exmap_flush(&dsp_mmu);
370                 return 0;
371 #ifdef CONFIG_OMAP_DSP_FBEXPORT
372         case MEM_IOCTL_FBEXPORT:
373                 if (copy_from_user(&dspadr, (void __user *)arg,
374                                    sizeof(dsp_long_t)))
375                         return -EFAULT;
376                 ret = dsp_fbexport(&dspadr);
377                 if (copy_to_user((void __user *)arg, &dspadr,
378                                  sizeof(dsp_long_t)))
379                         return -EFAULT;
380                 return ret;
381 #endif
382         case MEM_IOCTL_MMUITACK:
383                 return dsp_mmu_itack();
384
385         case MEM_IOCTL_KMEM_RESERVE:
386
387                 if (copy_from_user(&size, (void __user *)arg,
388                                    sizeof(__u32)))
389                         return -EFAULT;
390                 return omap_mmu_kmem_reserve(&dsp_mmu, size);
391
392
393         case MEM_IOCTL_KMEM_RELEASE:
394                 omap_mmu_kmem_release();
395                 return 0;
396
397         default:
398                 return -ENOIOCTLCMD;
399         }
400 }
401
402 struct file_operations dsp_mem_fops = {
403         .owner   = THIS_MODULE,
404         .read    = dsp_mem_read,
405         .write   = dsp_mem_write,
406         .ioctl   = dsp_mem_ioctl,
407 };
408
409 void dsp_mem_start(void)
410 {
411         dsp_register_mem_cb(intmem_enable, intmem_disable);
412 }
413
414 void dsp_mem_stop(void)
415 {
416         memset(&mem_sync, 0, sizeof(struct mem_sync_struct));
417         dsp_unregister_mem_cb();
418 }
419
420 static void dsp_mmu_irq_work(struct work_struct *work)
421 {
422         struct omap_mmu *mmu = container_of(work, struct omap_mmu, irq_work);
423
424         if (dsp_cfgstat_get_stat() == CFGSTAT_READY) {
425                 dsp_err_set(ERRCODE_MMU, mmu->fault_address);
426                 return;
427         }
428         omap_mmu_itack(mmu);
429         pr_info("Resetting DSP...\n");
430         dsp_cpustat_request(CPUSTAT_RESET);
431         omap_mmu_enable(mmu, 0);
432 }
433
434 /*
435  * later half of dsp memory initialization
436  */
437 int dsp_mem_late_init(void)
438 {
439         int ret;
440
441         dsp_mem_ipi_init();
442
443         INIT_WORK(&dsp_mmu.irq_work, dsp_mmu_irq_work);
444         ret = omap_mmu_register(&dsp_mmu);
445         if (ret) {
446                 dsp_reset_idle_boot_base();
447                 goto out;
448         }
449         omap_dsp->mmu = &dsp_mmu;
450  out:
451         return ret;
452 }
453
454 int __init dsp_mem_init(void)
455 {
456 #ifdef CONFIG_ARCH_OMAP2
457         dsp_mmu.clk    = dsp_fck_handle;
458         dsp_mmu.memclk = dsp_ick_handle;
459 #elif defined(CONFIG_ARCH_OMAP1)
460         dsp_mmu.clk    = dsp_ck_handle;
461         dsp_mmu.memclk = api_ck_handle;
462 #endif
463         return 0;
464 }
465
466 void dsp_mem_exit(void)
467 {
468         dsp_reset_idle_boot_base();
469         omap_mmu_unregister(&dsp_mmu);
470 }