1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 
4     btcx-risc.c
5 
6     bt848/bt878/cx2388x risc code generator.
7 
8     (c) 2000-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 
10 
11 */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/videodev2.h>
20 #include <linux/pgtable.h>
21 #include <asm/page.h>
22 
23 #include "btcx-risc.h"
24 
25 static unsigned int btcx_debug;
26 module_param(btcx_debug, int, 0644);
27 MODULE_PARM_DESC(btcx_debug,"debug messages, default is 0 (no)");
28 
29 #define dprintk(fmt, arg...) do {				\
30 	if (btcx_debug)						\
31 		printk(KERN_DEBUG pr_fmt("%s: " fmt),		\
32 		       __func__, ##arg);			\
33 } while (0)
34 
35 
36 /* ---------------------------------------------------------- */
37 /* allocate/free risc memory                                  */
38 
39 static int memcnt;
40 
btcx_riscmem_free(struct pci_dev * pci,struct btcx_riscmem * risc)41 void btcx_riscmem_free(struct pci_dev *pci,
42 		       struct btcx_riscmem *risc)
43 {
44 	if (NULL == risc->cpu)
45 		return;
46 
47 	memcnt--;
48 	dprintk("btcx: riscmem free [%d] dma=%lx\n",
49 		memcnt, (unsigned long)risc->dma);
50 
51 	dma_free_coherent(&pci->dev, risc->size, risc->cpu, risc->dma);
52 	memset(risc,0,sizeof(*risc));
53 }
54 
btcx_riscmem_alloc(struct pci_dev * pci,struct btcx_riscmem * risc,unsigned int size)55 int btcx_riscmem_alloc(struct pci_dev *pci,
56 		       struct btcx_riscmem *risc,
57 		       unsigned int size)
58 {
59 	__le32 *cpu;
60 	dma_addr_t dma = 0;
61 
62 	if (NULL != risc->cpu && risc->size < size)
63 		btcx_riscmem_free(pci,risc);
64 	if (NULL == risc->cpu) {
65 		cpu = dma_alloc_coherent(&pci->dev, size, &dma, GFP_KERNEL);
66 		if (NULL == cpu)
67 			return -ENOMEM;
68 		risc->cpu  = cpu;
69 		risc->dma  = dma;
70 		risc->size = size;
71 
72 		memcnt++;
73 		dprintk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
74 			memcnt, (unsigned long)dma, cpu, size);
75 	}
76 	return 0;
77 }
78