4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
6 * Processor Manager Driver for TI OMAP3430 EVM.
8 * Copyright (C) 2005-2006 Texas Instruments, Inc.
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <linux/types.h>
22 /* ----------------------------------- Host OS */
23 #include <dspbridge/host_os.h>
25 #include <linux/mmzone.h>
27 /* ----------------------------------- DSP/BIOS Bridge */
28 #include <dspbridge/dbdefs.h>
30 /* ----------------------------------- Trace & Debug */
31 #include <dspbridge/dbc.h>
33 /* ----------------------------------- OS Adaptation Layer */
34 #include <dspbridge/drv.h>
35 #include <dspbridge/sync.h>
37 /* ------------------------------------ Hardware Abstraction Layer */
41 /* ----------------------------------- Link Driver */
42 #include <dspbridge/dspdefs.h>
43 #include <dspbridge/dspchnl.h>
44 #include <dspbridge/dspdeh.h>
45 #include <dspbridge/dspio.h>
46 #include <dspbridge/dspmsg.h>
47 #include <dspbridge/pwr.h>
48 #include <dspbridge/io_sm.h>
50 /* ----------------------------------- Platform Manager */
51 #include <dspbridge/dev.h>
52 #include <dspbridge/dspapi.h>
53 #include <dspbridge/dmm.h>
54 #include <dspbridge/wdt.h>
56 /* ----------------------------------- Local */
58 #include "_tiomap_pwr.h"
59 #include "tiomap_io.h"
62 /* Offset in shared mem to write to in order to synchronize start with DSP */
63 #define SHMSYNCOFFSET 4 /* GPP byte offset */
65 #define BUFFERSIZE 1024
67 #define TIHELEN_ACKTIMEOUT 10000
69 #define MMU_SECTION_ADDR_MASK 0xFFF00000
70 #define MMU_SSECTION_ADDR_MASK 0xFF000000
71 #define MMU_LARGE_PAGE_MASK 0xFFFF0000
72 #define MMU_SMALL_PAGE_MASK 0xFFFFF000
73 #define OMAP3_IVA2_BOOTADDR_MASK 0xFFFFFC00
74 #define PAGES_II_LVL_TABLE 512
75 #define PHYS_TO_PAGE(phys) pfn_to_page((phys) >> PAGE_SHIFT)
78 * This is a totally ugly layer violation, but needed until
79 * omap_ctrl_set_dsp_boot*() are provided.
81 #define OMAP3_IVA2_BOOTMOD_IDLE 1
82 #define OMAP2_CONTROL_GENERAL 0x270
83 #define OMAP343X_CONTROL_IVA2_BOOTADDR (OMAP2_CONTROL_GENERAL + 0x0190)
84 #define OMAP343X_CONTROL_IVA2_BOOTMOD (OMAP2_CONTROL_GENERAL + 0x0194)
86 #define OMAP343X_CTRL_REGADDR(reg) \
87 OMAP2_L4_IO_ADDRESS(OMAP343X_CTRL_BASE + (reg))
90 /* Forward Declarations: */
91 static int bridge_brd_monitor(struct bridge_dev_context *dev_ctxt);
92 static int bridge_brd_read(struct bridge_dev_context *dev_ctxt,
94 u32 dsp_addr, u32 ul_num_bytes,
96 static int bridge_brd_start(struct bridge_dev_context *dev_ctxt,
98 static int bridge_brd_status(struct bridge_dev_context *dev_ctxt,
100 static int bridge_brd_stop(struct bridge_dev_context *dev_ctxt);
101 static int bridge_brd_write(struct bridge_dev_context *dev_ctxt,
103 u32 dsp_addr, u32 ul_num_bytes,
105 static int bridge_brd_set_state(struct bridge_dev_context *dev_ctxt,
107 static int bridge_brd_mem_copy(struct bridge_dev_context *dev_ctxt,
108 u32 dsp_dest_addr, u32 dsp_src_addr,
109 u32 ul_num_bytes, u32 mem_type);
110 static int bridge_brd_mem_write(struct bridge_dev_context *dev_ctxt,
111 u8 *host_buff, u32 dsp_addr,
112 u32 ul_num_bytes, u32 mem_type);
113 static int bridge_dev_create(struct bridge_dev_context
115 struct dev_object *hdev_obj,
116 struct cfg_hostres *config_param);
117 static int bridge_dev_ctrl(struct bridge_dev_context *dev_context,
118 u32 dw_cmd, void *pargs);
119 static int bridge_dev_destroy(struct bridge_dev_context *dev_ctxt);
120 bool wait_for_start(struct bridge_dev_context *dev_context, u32 dw_sync_addr);
123 * This Bridge driver's function interface table.
125 static struct bridge_drv_interface drv_interface_fxns = {
126 /* Bridge API ver. for which this bridge driver is built. */
127 BRD_API_MAJOR_VERSION,
128 BRD_API_MINOR_VERSION,
138 bridge_brd_set_state,
140 bridge_brd_mem_write,
141 /* The following CHNL functions are provided by chnl_io.lib: */
146 bridge_chnl_add_io_req,
148 bridge_chnl_cancel_io,
149 bridge_chnl_flush_io,
150 bridge_chnl_get_info,
151 bridge_chnl_get_mgr_info,
153 bridge_chnl_register_notify,
154 /* The following IO functions are provided by chnl_io.lib: */
158 bridge_io_get_proc_load,
159 /* The following msg_ctrl functions are provided by chnl_io.lib: */
161 bridge_msg_create_queue,
163 bridge_msg_delete_queue,
166 bridge_msg_register_notify,
167 bridge_msg_set_queue_id,
171 * ======== bridge_drv_entry ========
173 * Bridge Driver entry point.
175 void bridge_drv_entry(struct bridge_drv_interface **drv_intf,
176 const char *driver_file_name)
179 DBC_REQUIRE(driver_file_name != NULL);
181 io_sm_init(); /* Initialization of io_sm module */
183 if (strcmp(driver_file_name, "UMA") == 0)
184 *drv_intf = &drv_interface_fxns;
186 dev_dbg(bridge, "%s Unknown Bridge file name", __func__);
191 * ======== bridge_brd_monitor ========
193 * This bridge_brd_monitor puts DSP into a Loadable state.
194 * i.e Application can load and start the device.
197 * Device in 'OFF' state.
199 static int bridge_brd_monitor(struct bridge_dev_context *dev_ctxt)
201 struct bridge_dev_context *dev_context = dev_ctxt;
203 struct omap_dsp_platform_data *pdata =
204 omap_dspbridge_dev->dev.platform_data;
206 temp = (*pdata->dsp_prm_read)(OMAP3430_IVA2_MOD, OMAP2_PM_PWSTST) &
207 OMAP_POWERSTATEST_MASK;
208 if (!(temp & 0x02)) {
209 /* IVA2 is not in ON state */
210 /* Read and set PM_PWSTCTRL_IVA2 to ON */
211 (*pdata->dsp_prm_rmw_bits)(OMAP_POWERSTATEST_MASK,
212 PWRDM_POWER_ON, OMAP3430_IVA2_MOD, OMAP2_PM_PWSTCTRL);
213 /* Set the SW supervised state transition */
214 (*pdata->dsp_cm_write)(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP,
215 OMAP3430_IVA2_MOD, OMAP2_CM_CLKSTCTRL);
217 /* Wait until the state has moved to ON */
218 while ((*pdata->dsp_prm_read)(OMAP3430_IVA2_MOD, OMAP2_PM_PWSTST) &
219 OMAP_INTRANSITION_MASK)
221 /* Disable Automatic transition */
222 (*pdata->dsp_cm_write)(OMAP34XX_CLKSTCTRL_DISABLE_AUTO,
223 OMAP3430_IVA2_MOD, OMAP2_CM_CLKSTCTRL);
226 dsp_clk_enable(DSP_CLK_IVA2);
228 /* set the device state to IDLE */
229 dev_context->dw_brd_state = BRD_IDLE;
235 * ======== bridge_brd_read ========
237 * Reads buffers for DSP memory.
239 static int bridge_brd_read(struct bridge_dev_context *dev_ctxt,
240 u8 *host_buff, u32 dsp_addr,
241 u32 ul_num_bytes, u32 mem_type)
244 struct bridge_dev_context *dev_context = dev_ctxt;
246 u32 dsp_base_addr = dev_ctxt->dw_dsp_base_addr;
248 if (dsp_addr < dev_context->dw_dsp_start_add) {
252 /* change here to account for the 3 bands of the DSP internal memory */
253 if ((dsp_addr - dev_context->dw_dsp_start_add) <
254 dev_context->dw_internal_size) {
255 offset = dsp_addr - dev_context->dw_dsp_start_add;
257 status = read_ext_dsp_data(dev_context, host_buff, dsp_addr,
258 ul_num_bytes, mem_type);
261 /* copy the data from DSP memory, */
262 memcpy(host_buff, (void *)(dsp_base_addr + offset), ul_num_bytes);
267 * ======== bridge_brd_set_state ========
269 * This routine updates the Board status.
271 static int bridge_brd_set_state(struct bridge_dev_context *dev_ctxt,
275 struct bridge_dev_context *dev_context = dev_ctxt;
277 dev_context->dw_brd_state = brd_state;
282 * ======== bridge_brd_start ========
284 * Initializes DSP MMU and Starts DSP.
287 * a) DSP domain is 'ACTIVE'.
288 * b) DSP_RST1 is asserted.
289 * b) DSP_RST2 is released.
291 static int bridge_brd_start(struct bridge_dev_context *dev_ctxt,
295 struct bridge_dev_context *dev_context = dev_ctxt;
296 struct iommu *mmu = NULL;
297 struct shm_segs *sm_sg;
298 int l4_i = 0, tlb_i = 0;
299 u32 sg0_da = 0, sg1_da = 0;
300 struct bridge_ioctl_extproc *tlb = dev_context->atlb_entry;
301 u32 dw_sync_addr = 0;
302 u32 ul_shm_base; /* Gpp Phys SM base addr(byte) */
303 u32 ul_shm_base_virt; /* Dsp Virt SM base addr */
304 u32 ul_tlb_base_virt; /* Base of MMU TLB entry */
305 /* Offset of shm_base_virt from tlb_base_virt */
306 u32 ul_shm_offset_virt;
307 struct cfg_hostres *resources = NULL;
311 u32 ul_bios_gp_timer;
313 struct io_mgr *hio_mgr;
314 u32 ul_load_monitor_timer;
315 struct omap_dsp_platform_data *pdata =
316 omap_dspbridge_dev->dev.platform_data;
318 /* The device context contains all the mmu setup info from when the
319 * last dsp base image was loaded. The first entry is always
321 /* Get SHM_BEG - convert to byte address */
322 (void)dev_get_symbol(dev_context->hdev_obj, SHMBASENAME,
324 ul_shm_base_virt *= DSPWORDSIZE;
325 DBC_ASSERT(ul_shm_base_virt != 0);
326 /* DSP Virtual address */
327 ul_tlb_base_virt = dev_context->sh_s.seg0_da;
328 DBC_ASSERT(ul_tlb_base_virt <= ul_shm_base_virt);
330 ul_shm_base_virt - (ul_tlb_base_virt * DSPWORDSIZE);
331 /* Kernel logical address */
332 ul_shm_base = dev_context->sh_s.seg0_va + ul_shm_offset_virt;
334 DBC_ASSERT(ul_shm_base != 0);
335 /* 2nd wd is used as sync field */
336 dw_sync_addr = ul_shm_base + SHMSYNCOFFSET;
337 /* Write a signature into the shm base + offset; this will
338 * get cleared when the DSP program starts. */
339 if ((ul_shm_base_virt == 0) || (ul_shm_base == 0)) {
340 pr_err("%s: Illegal SM base\n", __func__);
343 __raw_writel(0xffffffff, dw_sync_addr);
346 resources = dev_context->resources;
350 /* Assert RST1 i.e only the RST only for DSP megacell */
352 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST1_IVA2_MASK,
353 OMAP3430_RST1_IVA2_MASK, OMAP3430_IVA2_MOD,
355 /* Mask address with 1K for compatibility */
356 __raw_writel(dsp_addr & OMAP3_IVA2_BOOTADDR_MASK,
357 OMAP343X_CTRL_REGADDR(
358 OMAP343X_CONTROL_IVA2_BOOTADDR));
360 * Set bootmode to self loop if dsp_debug flag is true
362 __raw_writel((dsp_debug) ? OMAP3_IVA2_BOOTMOD_IDLE : 0,
363 OMAP343X_CTRL_REGADDR(
364 OMAP343X_CONTROL_IVA2_BOOTMOD));
369 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST2_IVA2_MASK, 0,
370 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
371 mmu = dev_context->dsp_mmu;
374 mmu = iommu_get("iva2");
376 dev_err(bridge, "iommu_get failed!\n");
377 dev_context->dsp_mmu = NULL;
382 dev_context->dsp_mmu = mmu;
383 mmu->isr = mmu_fault_isr;
384 sm_sg = &dev_context->sh_s;
385 sg0_da = iommu_kmap(mmu, sm_sg->seg0_da, sm_sg->seg0_pa,
386 sm_sg->seg0_size, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32);
387 if (IS_ERR_VALUE(sg0_da)) {
388 status = (int)sg0_da;
393 sg1_da = iommu_kmap(mmu, sm_sg->seg1_da, sm_sg->seg1_pa,
394 sm_sg->seg1_size, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32);
395 if (IS_ERR_VALUE(sg1_da)) {
396 status = (int)sg1_da;
402 for (tlb_i = 0; tlb_i < BRDIOCTL_NUMOFMMUTLB; tlb_i++) {
403 if (!tlb[tlb_i].ul_gpp_pa)
406 dev_dbg(bridge, "IOMMU %d GppPa: 0x%x DspVa 0x%x Size"
407 " 0x%x\n", tlb_i, tlb[tlb_i].ul_gpp_pa,
408 tlb[tlb_i].ul_dsp_va, tlb[tlb_i].ul_size);
410 da = iommu_kmap(mmu, tlb[tlb_i].ul_dsp_va,
411 tlb[tlb_i].ul_gpp_pa, PAGE_SIZE,
412 IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32);
413 if (IS_ERR_VALUE(da)) {
422 while (l4_peripheral_table[l4_i].phys_addr) {
423 da = iommu_kmap(mmu, l4_peripheral_table[l4_i].
424 dsp_virt_addr, l4_peripheral_table[l4_i].
425 phys_addr, PAGE_SIZE,
426 IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32);
427 if (IS_ERR_VALUE(da)) {
435 /* Lock the above TLB entries and get the BIOS and load monitor timer
438 /* Enable the BIOS clock */
439 (void)dev_get_symbol(dev_context->hdev_obj,
440 BRIDGEINIT_BIOSGPTIMER, &ul_bios_gp_timer);
441 (void)dev_get_symbol(dev_context->hdev_obj,
442 BRIDGEINIT_LOADMON_GPTIMER,
443 &ul_load_monitor_timer);
445 if (ul_load_monitor_timer != 0xFFFF) {
446 clk_cmd = (BPWR_ENABLE_CLOCK << MBX_PM_CLK_CMDSHIFT) |
447 ul_load_monitor_timer;
448 dsp_peripheral_clk_ctrl(dev_context, &clk_cmd);
450 dev_dbg(bridge, "Not able to get the symbol for Load "
454 if (ul_bios_gp_timer != 0xFFFF) {
455 clk_cmd = (BPWR_ENABLE_CLOCK << MBX_PM_CLK_CMDSHIFT) |
457 dsp_peripheral_clk_ctrl(dev_context, &clk_cmd);
460 "Not able to get the symbol for BIOS Timer\n");
463 /* Set the DSP clock rate */
464 (void)dev_get_symbol(dev_context->hdev_obj,
465 "_BRIDGEINIT_DSP_FREQ", &ul_dsp_clk_addr);
466 /*Set Autoidle Mode for IVA2 PLL */
467 (*pdata->dsp_cm_write)(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
468 OMAP3430_IVA2_MOD, OMAP3430_CM_AUTOIDLE_PLL);
470 if ((unsigned int *)ul_dsp_clk_addr != NULL) {
471 /* Get the clock rate */
472 ul_dsp_clk_rate = dsp_clk_get_iva2_rate();
473 dev_dbg(bridge, "%s: DSP clock rate (KHZ): 0x%x \n",
474 __func__, ul_dsp_clk_rate);
475 (void)bridge_brd_write(dev_context,
476 (u8 *) &ul_dsp_clk_rate,
477 ul_dsp_clk_addr, sizeof(u32), 0);
480 * Enable Mailbox events and also drain any pending
483 dev_context->mbox = omap_mbox_get("dsp");
484 if (IS_ERR(dev_context->mbox)) {
485 dev_context->mbox = NULL;
486 pr_err("%s: Failed to get dsp mailbox handle\n",
493 dev_context->mbox->rxq->callback = (int (*)(void *))io_mbox_msg;
495 /*PM_IVA2GRPSEL_PER = 0xC0;*/
496 temp = readl(resources->dw_per_pm_base + 0xA8);
497 temp = (temp & 0xFFFFFF30) | 0xC0;
498 writel(temp, resources->dw_per_pm_base + 0xA8);
500 /*PM_MPUGRPSEL_PER &= 0xFFFFFF3F; */
501 temp = readl(resources->dw_per_pm_base + 0xA4);
502 temp = (temp & 0xFFFFFF3F);
503 writel(temp, resources->dw_per_pm_base + 0xA4);
504 /*CM_SLEEPDEP_PER |= 0x04; */
505 temp = readl(resources->dw_per_base + 0x44);
506 temp = (temp & 0xFFFFFFFB) | 0x04;
507 writel(temp, resources->dw_per_base + 0x44);
509 /*CM_CLKSTCTRL_IVA2 = 0x00000003 -To Allow automatic transitions */
510 (*pdata->dsp_cm_write)(OMAP34XX_CLKSTCTRL_ENABLE_AUTO,
511 OMAP3430_IVA2_MOD, OMAP2_CM_CLKSTCTRL);
514 dev_dbg(bridge, "%s Unreset\n", __func__);
515 /* release the RST1, DSP starts executing now .. */
516 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST1_IVA2_MASK, 0,
517 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
519 dev_dbg(bridge, "Waiting for Sync @ 0x%x\n", dw_sync_addr);
520 dev_dbg(bridge, "DSP c_int00 Address = 0x%x\n", dsp_addr);
522 while (__raw_readw(dw_sync_addr))
525 /* Wait for DSP to clear word in shared memory */
526 /* Read the Location */
527 if (!wait_for_start(dev_context, dw_sync_addr))
531 dsp_wdt_sm_set((void *)ul_shm_base);
532 dsp_wdt_enable(true);
534 status = dev_get_io_mgr(dev_context->hdev_obj, &hio_mgr);
536 io_sh_msetting(hio_mgr, SHM_OPPINFO, NULL);
537 /* Write the synchronization bit to indicate the
538 * completion of OPP table update to DSP
540 __raw_writel(0XCAFECAFE, dw_sync_addr);
542 /* update board state */
543 dev_context->dw_brd_state = BRD_RUNNING;
546 dev_context->dw_brd_state = BRD_UNKNOWN;
551 if (!tlb[tlb_i].ul_gpp_pa)
553 iommu_kunmap(mmu, tlb[tlb_i].ul_gpp_va);
556 iommu_kunmap(mmu, l4_peripheral_table[l4_i].dsp_virt_addr);
558 iommu_kunmap(mmu, sg0_da);
560 iommu_kunmap(mmu, sg1_da);
565 * ======== bridge_brd_stop ========
567 * Puts DSP in self loop.
572 static int bridge_brd_stop(struct bridge_dev_context *dev_ctxt)
575 struct bridge_dev_context *dev_context = dev_ctxt;
578 struct bridge_ioctl_extproc *tlb = dev_context->atlb_entry;
579 struct omap_dsp_platform_data *pdata =
580 omap_dspbridge_dev->dev.platform_data;
582 if (dev_context->dw_brd_state == BRD_STOPPED)
585 /* as per TRM, it is advised to first drive the IVA2 to 'Standby' mode,
586 * before turning off the clocks.. This is to ensure that there are no
587 * pending L3 or other transactons from IVA2 */
588 dsp_pwr_state = (*pdata->dsp_prm_read)(OMAP3430_IVA2_MOD, OMAP2_PM_PWSTST) &
589 OMAP_POWERSTATEST_MASK;
590 if (dsp_pwr_state != PWRDM_POWER_OFF) {
591 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST2_IVA2_MASK, 0,
592 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
593 sm_interrupt_dsp(dev_context, MBX_PM_DSPIDLE);
596 /* IVA2 is not in OFF state */
597 /* Set PM_PWSTCTRL_IVA2 to OFF */
598 (*pdata->dsp_prm_rmw_bits)(OMAP_POWERSTATEST_MASK,
599 PWRDM_POWER_OFF, OMAP3430_IVA2_MOD, OMAP2_PM_PWSTCTRL);
600 /* Set the SW supervised state transition for Sleep */
601 (*pdata->dsp_cm_write)(OMAP34XX_CLKSTCTRL_FORCE_SLEEP,
602 OMAP3430_IVA2_MOD, OMAP2_CM_CLKSTCTRL);
605 /* Release the Ext Base virtual Address as the next DSP Program
606 * may have a different load address */
607 if (dev_context->dw_dsp_ext_base_addr)
608 dev_context->dw_dsp_ext_base_addr = 0;
610 dev_context->dw_brd_state = BRD_STOPPED; /* update board state */
612 dsp_wdt_enable(false);
615 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST1_IVA2_MASK,
616 OMAP3430_RST1_IVA2_MASK, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
618 /* Disable the mailbox interrupts */
619 if (dev_context->mbox) {
620 omap_mbox_disable_irq(dev_context->mbox, IRQ_RX);
621 omap_mbox_put(dev_context->mbox);
622 dev_context->mbox = NULL;
624 if (dev_context->dsp_mmu) {
625 pr_err("Proc stop mmu if statement\n");
626 for (i = 0; i < BRDIOCTL_NUMOFMMUTLB; i++) {
627 if (!tlb[i].ul_gpp_pa)
629 iommu_kunmap(dev_context->dsp_mmu, tlb[i].ul_gpp_va);
632 while (l4_peripheral_table[i].phys_addr) {
633 iommu_kunmap(dev_context->dsp_mmu,
634 l4_peripheral_table[i].dsp_virt_addr);
637 iommu_kunmap(dev_context->dsp_mmu, dev_context->sh_s.seg0_da);
638 iommu_kunmap(dev_context->dsp_mmu, dev_context->sh_s.seg1_da);
639 iommu_put(dev_context->dsp_mmu);
640 dev_context->dsp_mmu = NULL;
643 (*pdata->dsp_prm_rmw_bits)(OMAP3430_RST2_IVA2_MASK,
644 OMAP3430_RST2_IVA2_MASK, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
646 dsp_clock_disable_all(dev_context->dsp_per_clks);
647 dsp_clk_disable(DSP_CLK_IVA2);
653 * ======== bridge_brd_status ========
654 * Returns the board status.
656 static int bridge_brd_status(struct bridge_dev_context *dev_ctxt,
659 struct bridge_dev_context *dev_context = dev_ctxt;
660 *board_state = dev_context->dw_brd_state;
665 * ======== bridge_brd_write ========
666 * Copies the buffers to DSP internal or external memory.
668 static int bridge_brd_write(struct bridge_dev_context *dev_ctxt,
669 u8 *host_buff, u32 dsp_addr,
670 u32 ul_num_bytes, u32 mem_type)
673 struct bridge_dev_context *dev_context = dev_ctxt;
675 if (dsp_addr < dev_context->dw_dsp_start_add) {
679 if ((dsp_addr - dev_context->dw_dsp_start_add) <
680 dev_context->dw_internal_size) {
681 status = write_dsp_data(dev_ctxt, host_buff, dsp_addr,
682 ul_num_bytes, mem_type);
684 status = write_ext_dsp_data(dev_context, host_buff, dsp_addr,
685 ul_num_bytes, mem_type, false);
692 * ======== bridge_dev_create ========
693 * Creates a driver object. Puts DSP in self loop.
695 static int bridge_dev_create(struct bridge_dev_context
697 struct dev_object *hdev_obj,
698 struct cfg_hostres *config_param)
701 struct bridge_dev_context *dev_context = NULL;
703 struct cfg_hostres *resources = config_param;
704 struct drv_data *drv_datap = dev_get_drvdata(bridge);
706 /* Allocate and initialize a data structure to contain the bridge driver
707 * state, which becomes the context for later calls into this driver */
708 dev_context = kzalloc(sizeof(struct bridge_dev_context), GFP_KERNEL);
714 dev_context->dw_dsp_start_add = (u32) OMAP_GEM_BASE;
715 dev_context->dw_self_loop = (u32) NULL;
716 dev_context->dsp_per_clks = 0;
717 dev_context->dw_internal_size = OMAP_DSP_SIZE;
718 /* Clear dev context MMU table entries.
719 * These get set on bridge_io_on_loaded() call after program loaded. */
720 for (entry_ndx = 0; entry_ndx < BRDIOCTL_NUMOFMMUTLB; entry_ndx++) {
721 dev_context->atlb_entry[entry_ndx].ul_gpp_pa =
722 dev_context->atlb_entry[entry_ndx].ul_dsp_va = 0;
724 dev_context->dw_dsp_base_addr = (u32) MEM_LINEAR_ADDRESS((void *)
731 if (!dev_context->dw_dsp_base_addr)
735 dev_context->tc_word_swap_on = drv_datap->tc_wordswapon;
736 dev_context->hdev_obj = hdev_obj;
737 /* Store current board state. */
738 dev_context->dw_brd_state = BRD_UNKNOWN;
739 dev_context->resources = resources;
740 dsp_clk_enable(DSP_CLK_IVA2);
741 bridge_brd_stop(dev_context);
742 /* Return ptr to our device state to the DSP API for storage */
743 *dev_cntxt = dev_context;
752 * ======== bridge_dev_ctrl ========
753 * Receives device specific commands.
755 static int bridge_dev_ctrl(struct bridge_dev_context *dev_context,
756 u32 dw_cmd, void *pargs)
759 struct bridge_ioctl_extproc *pa_ext_proc =
760 (struct bridge_ioctl_extproc *)pargs;
764 case BRDIOCTL_CHNLREAD:
766 case BRDIOCTL_CHNLWRITE:
768 case BRDIOCTL_SETMMUCONFIG:
769 /* store away dsp-mmu setup values for later use */
770 for (ndx = 0; ndx < BRDIOCTL_NUMOFMMUTLB; ndx++, pa_ext_proc++)
771 dev_context->atlb_entry[ndx] = *pa_ext_proc;
773 case BRDIOCTL_DEEPSLEEP:
774 case BRDIOCTL_EMERGENCYSLEEP:
775 /* Currently only DSP Idle is supported Need to update for
777 status = sleep_dsp(dev_context, PWR_DEEPSLEEP, pargs);
779 case BRDIOCTL_WAKEUP:
780 status = wake_dsp(dev_context, pargs);
782 case BRDIOCTL_CLK_CTRL:
784 /* Looking For Baseport Fix for Clocks */
785 status = dsp_peripheral_clk_ctrl(dev_context, pargs);
787 case BRDIOCTL_PWR_HIBERNATE:
788 status = handle_hibernation_from_dsp(dev_context);
790 case BRDIOCTL_PRESCALE_NOTIFY:
791 status = pre_scale_dsp(dev_context, pargs);
793 case BRDIOCTL_POSTSCALE_NOTIFY:
794 status = post_scale_dsp(dev_context, pargs);
796 case BRDIOCTL_CONSTRAINT_REQUEST:
797 status = handle_constraints_set(dev_context, pargs);
807 * ======== bridge_dev_destroy ========
808 * Destroys the driver object.
810 static int bridge_dev_destroy(struct bridge_dev_context *dev_ctxt)
813 struct bridge_dev_context *dev_context = (struct bridge_dev_context *)
815 struct cfg_hostres *host_res;
817 struct drv_data *drv_datap = dev_get_drvdata(bridge);
819 /* It should never happen */
823 /* first put the device to stop state */
824 bridge_brd_stop(dev_context);
826 if (dev_context->resources) {
827 host_res = dev_context->resources;
828 shm_size = drv_datap->shm_size;
829 if (shm_size >= 0x10000) {
830 if ((host_res->dw_mem_base[1]) &&
831 (host_res->dw_mem_phys[1])) {
832 mem_free_phys_mem((void *)
833 host_res->dw_mem_base
835 host_res->dw_mem_phys
839 dev_dbg(bridge, "%s: Error getting shm size "
840 "from registry: %x. Not calling "
841 "mem_free_phys_mem\n", __func__,
844 host_res->dw_mem_base[1] = 0;
845 host_res->dw_mem_phys[1] = 0;
847 if (host_res->dw_mem_base[0])
848 iounmap((void *)host_res->dw_mem_base[0]);
849 if (host_res->dw_mem_base[2])
850 iounmap((void *)host_res->dw_mem_base[2]);
851 if (host_res->dw_mem_base[3])
852 iounmap((void *)host_res->dw_mem_base[3]);
853 if (host_res->dw_mem_base[4])
854 iounmap((void *)host_res->dw_mem_base[4]);
855 if (host_res->dw_dmmu_base)
856 iounmap(host_res->dw_dmmu_base);
857 if (host_res->dw_per_base)
858 iounmap(host_res->dw_per_base);
859 if (host_res->dw_per_pm_base)
860 iounmap((void *)host_res->dw_per_pm_base);
861 if (host_res->dw_core_pm_base)
862 iounmap((void *)host_res->dw_core_pm_base);
863 if (host_res->dw_sys_ctrl_base)
864 iounmap(host_res->dw_sys_ctrl_base);
866 host_res->dw_mem_base[0] = (u32) NULL;
867 host_res->dw_mem_base[2] = (u32) NULL;
868 host_res->dw_mem_base[3] = (u32) NULL;
869 host_res->dw_mem_base[4] = (u32) NULL;
870 host_res->dw_dmmu_base = NULL;
871 host_res->dw_sys_ctrl_base = NULL;
876 /* Free the driver's device context: */
877 kfree(drv_datap->base_img);
879 dev_set_drvdata(bridge, NULL);
880 kfree((void *)dev_ctxt);
884 static int bridge_brd_mem_copy(struct bridge_dev_context *dev_ctxt,
885 u32 dsp_dest_addr, u32 dsp_src_addr,
886 u32 ul_num_bytes, u32 mem_type)
889 u32 src_addr = dsp_src_addr;
890 u32 dest_addr = dsp_dest_addr;
892 u32 total_bytes = ul_num_bytes;
893 u8 host_buf[BUFFERSIZE];
894 struct bridge_dev_context *dev_context = dev_ctxt;
895 while (total_bytes > 0 && !status) {
897 total_bytes > BUFFERSIZE ? BUFFERSIZE : total_bytes;
898 /* Read from External memory */
899 status = read_ext_dsp_data(dev_ctxt, host_buf, src_addr,
900 copy_bytes, mem_type);
902 if (dest_addr < (dev_context->dw_dsp_start_add +
903 dev_context->dw_internal_size)) {
904 /* Write to Internal memory */
905 status = write_dsp_data(dev_ctxt, host_buf,
906 dest_addr, copy_bytes,
909 /* Write to External memory */
911 write_ext_dsp_data(dev_ctxt, host_buf,
912 dest_addr, copy_bytes,
916 total_bytes -= copy_bytes;
917 src_addr += copy_bytes;
918 dest_addr += copy_bytes;
923 /* Mem Write does not halt the DSP to write unlike bridge_brd_write */
924 static int bridge_brd_mem_write(struct bridge_dev_context *dev_ctxt,
925 u8 *host_buff, u32 dsp_addr,
926 u32 ul_num_bytes, u32 mem_type)
929 struct bridge_dev_context *dev_context = dev_ctxt;
930 u32 ul_remain_bytes = 0;
932 ul_remain_bytes = ul_num_bytes;
933 while (ul_remain_bytes > 0 && !status) {
935 ul_remain_bytes > BUFFERSIZE ? BUFFERSIZE : ul_remain_bytes;
936 if (dsp_addr < (dev_context->dw_dsp_start_add +
937 dev_context->dw_internal_size)) {
939 write_dsp_data(dev_ctxt, host_buff, dsp_addr,
942 status = write_ext_dsp_data(dev_ctxt, host_buff,
946 ul_remain_bytes -= ul_bytes;
947 dsp_addr += ul_bytes;
948 host_buff = host_buff + ul_bytes;
954 * ======== user_va2_pa ========
956 * This function walks through the page tables to convert a userland
957 * virtual address to physical address
959 static u32 user_va2_pa(struct mm_struct *mm, u32 address)
965 pgd = pgd_offset(mm, address);
966 if (!(pgd_none(*pgd) || pgd_bad(*pgd))) {
967 pmd = pmd_offset(pgd, address);
968 if (!(pmd_none(*pmd) || pmd_bad(*pmd))) {
969 ptep = pte_offset_map(pmd, address);
972 if (pte_present(pte))
973 return pte & PAGE_MASK;
982 * get_io_pages() - pin and get pages of io user's buffer.
983 * @mm: mm_struct Pointer of the process.
984 * @uva: Virtual user space address.
985 * @pages Pages to be pined.
986 * @usr_pgs struct page array pointer where the user pages will be stored
989 static int get_io_pages(struct mm_struct *mm, u32 uva, unsigned pages,
990 struct page **usr_pgs)
996 for (i = 0; i < pages; i++) {
997 pa = user_va2_pa(mm, uva);
999 if (!pfn_valid(__phys_to_pfn(pa)))
1002 pg = PHYS_TO_PAGE(pa);
1010 * user_to_dsp_map() - maps user to dsp virtual address
1011 * @mmu: Pointer to iommu handle.
1012 * @uva: Virtual user space address.
1014 * @size Buffer size to map.
1015 * @usr_pgs struct page array pointer where the user pages will be stored
1017 * This function maps a user space buffer into DSP virtual address.
1020 u32 user_to_dsp_map(struct iommu *mmu, u32 uva, u32 da, u32 size,
1021 struct page **usr_pgs)
1025 struct vm_area_struct *vma;
1026 struct mm_struct *mm = current->mm;
1027 struct sg_table *sgt;
1028 struct scatterlist *sg;
1030 if (!size || !usr_pgs)
1033 pages = size / PG_SIZE4K;
1035 down_read(&mm->mmap_sem);
1036 vma = find_vma(mm, uva);
1037 while (vma && (uva + size > vma->vm_end))
1038 vma = find_vma(mm, vma->vm_end + 1);
1041 pr_err("%s: Failed to get VMA region for 0x%x (%d)\n",
1042 __func__, uva, size);
1043 up_read(&mm->mmap_sem);
1046 if (vma->vm_flags & (VM_WRITE | VM_MAYWRITE))
1049 if (vma->vm_flags & VM_IO)
1050 i = get_io_pages(mm, uva, pages, usr_pgs);
1052 i = get_user_pages(current, mm, uva, pages, w, 1,
1054 up_read(&mm->mmap_sem);
1064 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
1070 res = sg_alloc_table(sgt, pages, GFP_KERNEL);
1075 for_each_sg(sgt->sgl, sg, sgt->nents, i)
1076 sg_set_page(sg, usr_pgs[i], PAGE_SIZE, 0);
1078 da = iommu_vmap(mmu, da, sgt, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_32);
1080 if (!IS_ERR_VALUE(da))
1090 put_page(usr_pgs[i]);
1095 * user_to_dsp_unmap() - unmaps DSP virtual buffer.
1096 * @mmu: Pointer to iommu handle.
1099 * This function unmaps a user space buffer into DSP virtual address.
1102 int user_to_dsp_unmap(struct iommu *mmu, u32 da)
1105 struct sg_table *sgt;
1106 struct scatterlist *sg;
1108 sgt = iommu_vunmap(mmu, da);
1112 for_each_sg(sgt->sgl, sg, sgt->nents, i)
1113 put_page(sg_page(sg));
1121 * ======== wait_for_start ========
1122 * Wait for the singal from DSP that it has started, or time out.
1124 bool wait_for_start(struct bridge_dev_context *dev_context, u32 dw_sync_addr)
1126 u16 timeout = TIHELEN_ACKTIMEOUT;
1128 /* Wait for response from board */
1129 while (__raw_readw(dw_sync_addr) && --timeout)
1132 /* If timed out: return false */
1134 pr_err("%s: Timed out waiting DSP to Start\n", __func__);