1*07d7fe7bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2719f82d3SEliot Blennerhassett /******************************************************************************
3719f82d3SEliot Blennerhassett
4719f82d3SEliot Blennerhassett AudioScience HPI driver
592fd918cSEliot Blennerhassett Copyright (C) 1997-2012 AudioScience Inc. <support@audioscience.com>
6719f82d3SEliot Blennerhassett
7719f82d3SEliot Blennerhassett
8719f82d3SEliot Blennerhassett HPI Operating System function implementation for Linux
9719f82d3SEliot Blennerhassett
10719f82d3SEliot Blennerhassett (C) Copyright AudioScience Inc. 1997-2003
11719f82d3SEliot Blennerhassett ******************************************************************************/
12719f82d3SEliot Blennerhassett #define SOURCEFILE_NAME "hpios.c"
13719f82d3SEliot Blennerhassett #include "hpi_internal.h"
14719f82d3SEliot Blennerhassett #include "hpidebug.h"
15719f82d3SEliot Blennerhassett #include <linux/delay.h>
16719f82d3SEliot Blennerhassett #include <linux/sched.h>
17719f82d3SEliot Blennerhassett
hpios_delay_micro_seconds(u32 num_micro_sec)18719f82d3SEliot Blennerhassett void hpios_delay_micro_seconds(u32 num_micro_sec)
19719f82d3SEliot Blennerhassett {
20719f82d3SEliot Blennerhassett if ((usecs_to_jiffies(num_micro_sec) > 1) && !in_interrupt()) {
21719f82d3SEliot Blennerhassett /* MUST NOT SCHEDULE IN INTERRUPT CONTEXT! */
22719f82d3SEliot Blennerhassett schedule_timeout_uninterruptible(usecs_to_jiffies
23719f82d3SEliot Blennerhassett (num_micro_sec));
24719f82d3SEliot Blennerhassett } else if (num_micro_sec <= 2000)
25719f82d3SEliot Blennerhassett udelay(num_micro_sec);
26719f82d3SEliot Blennerhassett else
27719f82d3SEliot Blennerhassett mdelay(num_micro_sec / 1000);
28719f82d3SEliot Blennerhassett
29719f82d3SEliot Blennerhassett }
30719f82d3SEliot Blennerhassett
3192fd918cSEliot Blennerhassett /** Allocate an area of locked memory for bus master DMA operations.
32719f82d3SEliot Blennerhassett
3392fd918cSEliot Blennerhassett If allocation fails, return 1, and *pMemArea.size = 0
34719f82d3SEliot Blennerhassett */
hpios_locked_mem_alloc(struct consistent_dma_area * p_mem_area,u32 size,struct pci_dev * pdev)3592fd918cSEliot Blennerhassett u16 hpios_locked_mem_alloc(struct consistent_dma_area *p_mem_area, u32 size,
36719f82d3SEliot Blennerhassett struct pci_dev *pdev)
37719f82d3SEliot Blennerhassett {
38719f82d3SEliot Blennerhassett /*?? any benefit in using managed dmam_alloc_coherent? */
39719f82d3SEliot Blennerhassett p_mem_area->vaddr =
40719f82d3SEliot Blennerhassett dma_alloc_coherent(&pdev->dev, size, &p_mem_area->dma_handle,
4123fdf223SChristoph Hellwig GFP_KERNEL);
42719f82d3SEliot Blennerhassett
43719f82d3SEliot Blennerhassett if (p_mem_area->vaddr) {
44719f82d3SEliot Blennerhassett HPI_DEBUG_LOG(DEBUG, "allocated %d bytes, dma 0x%x vma %p\n",
45719f82d3SEliot Blennerhassett size, (unsigned int)p_mem_area->dma_handle,
46719f82d3SEliot Blennerhassett p_mem_area->vaddr);
47719f82d3SEliot Blennerhassett p_mem_area->pdev = &pdev->dev;
48719f82d3SEliot Blennerhassett p_mem_area->size = size;
49719f82d3SEliot Blennerhassett return 0;
50719f82d3SEliot Blennerhassett } else {
51719f82d3SEliot Blennerhassett HPI_DEBUG_LOG(WARNING,
52719f82d3SEliot Blennerhassett "failed to allocate %d bytes locked memory\n", size);
53719f82d3SEliot Blennerhassett p_mem_area->size = 0;
5492fd918cSEliot Blennerhassett return 1;
55719f82d3SEliot Blennerhassett }
56719f82d3SEliot Blennerhassett }
57719f82d3SEliot Blennerhassett
hpios_locked_mem_free(struct consistent_dma_area * p_mem_area)58719f82d3SEliot Blennerhassett u16 hpios_locked_mem_free(struct consistent_dma_area *p_mem_area)
59719f82d3SEliot Blennerhassett {
60719f82d3SEliot Blennerhassett if (p_mem_area->size) {
61719f82d3SEliot Blennerhassett dma_free_coherent(p_mem_area->pdev, p_mem_area->size,
62719f82d3SEliot Blennerhassett p_mem_area->vaddr, p_mem_area->dma_handle);
63719f82d3SEliot Blennerhassett HPI_DEBUG_LOG(DEBUG, "freed %lu bytes, dma 0x%x vma %p\n",
64719f82d3SEliot Blennerhassett (unsigned long)p_mem_area->size,
65719f82d3SEliot Blennerhassett (unsigned int)p_mem_area->dma_handle,
66719f82d3SEliot Blennerhassett p_mem_area->vaddr);
67719f82d3SEliot Blennerhassett p_mem_area->size = 0;
68719f82d3SEliot Blennerhassett return 0;
69719f82d3SEliot Blennerhassett } else {
70719f82d3SEliot Blennerhassett return 1;
71719f82d3SEliot Blennerhassett }
72719f82d3SEliot Blennerhassett }
73