xref: /linux/drivers/media/pci/saa7164/saa7164-buffer.c (revision 0cdee263bc5e7b20f657ea09f9272f50c568f35b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for the NXP SAA7164 PCIe bridge
4  *
5  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6  */
7 
8 #include <linux/slab.h>
9 
10 #include "saa7164.h"
11 
12 /* The PCI address space for buffer handling looks like this:
13  *
14  * +-u32 wide-------------+
15  * |                      +
16  * +-u64 wide------------------------------------+
17  * +                                             +
18  * +----------------------+
19  * | CurrentBufferPtr     + Pointer to current PCI buffer >-+
20  * +----------------------+                                 |
21  * | Unused               +                                 |
22  * +----------------------+                                 |
23  * | Pitch                + = 188 (bytes)                   |
24  * +----------------------+                                 |
25  * | PCI buffer size      + = pitch * number of lines (312) |
26  * +----------------------+                                 |
27  * |0| Buf0 Write Offset  +                                 |
28  * +----------------------+                                 v
29  * |1| Buf1 Write Offset  +                                 |
30  * +----------------------+                                 |
31  * |2| Buf2 Write Offset  +                                 |
32  * +----------------------+                                 |
33  * |3| Buf3 Write Offset  +                                 |
34  * +----------------------+                                 |
35  * ... More write offsets                                   |
36  * +---------------------------------------------+          |
37  * +0| set of ptrs to PCI pagetables             +          |
38  * +---------------------------------------------+          |
39  * +1| set of ptrs to PCI pagetables             + <--------+
40  * +---------------------------------------------+
41  * +2| set of ptrs to PCI pagetables             +
42  * +---------------------------------------------+
43  * +3| set of ptrs to PCI pagetables             + >--+
44  * +---------------------------------------------+    |
45  * ... More buffer pointers                           |  +----------------+
46  *						    +->| pt[0] TS data  |
47  *						    |  +----------------+
48  *						    |
49  *						    |  +----------------+
50  *						    +->| pt[1] TS data  |
51  *						    |  +----------------+
52  *						    | etc
53  */
54 
55 /* Allocate a new buffer structure and associated PCI space in bytes.
56  * len must be a multiple of sizeof(u64)
57  */
saa7164_buffer_alloc(struct saa7164_port * port,u32 len)58 struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
59 	u32 len)
60 {
61 	struct tmHWStreamParameters *params = &port->hw_streamingparams;
62 	struct saa7164_buffer *buf = NULL;
63 	struct saa7164_dev *dev = port->dev;
64 	int i;
65 
66 	if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
67 		log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
68 		goto ret;
69 	}
70 
71 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
72 	if (!buf)
73 		goto ret;
74 
75 	buf->idx = -1;
76 	buf->port = port;
77 	buf->flags = SAA7164_BUFFER_FREE;
78 	buf->pos = 0;
79 	buf->actual_size = params->pitch * params->numberoflines;
80 	buf->crc = 0;
81 	/* TODO: arg len is being ignored */
82 	buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
83 	buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
84 
85 	/* Allocate contiguous memory */
86 	buf->cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pci_size,
87 				      &buf->dma, GFP_KERNEL);
88 	if (!buf->cpu)
89 		goto fail1;
90 
91 	buf->pt_cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pt_size,
92 					 &buf->pt_dma, GFP_KERNEL);
93 	if (!buf->pt_cpu)
94 		goto fail2;
95 
96 	/* init the buffers to a known pattern, easier during debugging */
97 	memset(buf->cpu, 0xff, buf->pci_size);
98 	buf->crc = crc32(0, buf->cpu, buf->actual_size);
99 	memset(buf->pt_cpu, 0xff, buf->pt_size);
100 
101 	dprintk(DBGLVL_BUF, "%s()   allocated buffer @ 0x%p (%d pageptrs)\n",
102 		__func__, buf, params->numpagetables);
103 	dprintk(DBGLVL_BUF, "  pci_cpu @ 0x%p    dma @ 0x%08lx len = 0x%x\n",
104 		buf->cpu, (long)buf->dma, buf->pci_size);
105 	dprintk(DBGLVL_BUF, "   pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
106 		buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
107 
108 	/* Format the Page Table Entries to point into the data buffer */
109 	for (i = 0 ; i < params->numpagetables; i++) {
110 
111 		*(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
112 		dprintk(DBGLVL_BUF, "    pt[%02d] = 0x%p -> 0x%llx\n",
113 			i, buf->pt_cpu, (u64)*(buf->pt_cpu));
114 
115 	}
116 
117 	goto ret;
118 
119 fail2:
120 	dma_free_coherent(&port->dev->pci->dev, buf->pci_size, buf->cpu,
121 			  buf->dma);
122 fail1:
123 	kfree(buf);
124 
125 	buf = NULL;
126 ret:
127 	return buf;
128 }
129 
saa7164_buffer_dealloc(struct saa7164_buffer * buf)130 int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
131 {
132 	struct saa7164_dev *dev;
133 
134 	if (!buf || !buf->port)
135 		return SAA_ERR_BAD_PARAMETER;
136 	dev = buf->port->dev;
137 
138 	dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
139 		__func__, buf);
140 
141 	if (buf->flags != SAA7164_BUFFER_FREE)
142 		log_warn(" freeing a non-free buffer\n");
143 
144 	dma_free_coherent(&dev->pci->dev, buf->pci_size, buf->cpu, buf->dma);
145 	dma_free_coherent(&dev->pci->dev, buf->pt_size, buf->pt_cpu,
146 			  buf->pt_dma);
147 
148 	kfree(buf);
149 
150 	return SAA_OK;
151 }
152 
saa7164_buffer_zero_offsets(struct saa7164_port * port,int i)153 int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
154 {
155 	struct saa7164_dev *dev = port->dev;
156 
157 	if ((i < 0) || (i >= port->hwcfg.buffercount))
158 		return -EINVAL;
159 
160 	dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
161 
162 	saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
163 
164 	return 0;
165 }
166 
167 /* Write a buffer into the hardware */
saa7164_buffer_activate(struct saa7164_buffer * buf,int i)168 int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
169 {
170 	struct saa7164_port *port = buf->port;
171 	struct saa7164_dev *dev = port->dev;
172 
173 	if ((i < 0) || (i >= port->hwcfg.buffercount))
174 		return -EINVAL;
175 
176 	dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
177 
178 	buf->idx = i; /* Note of which buffer list index position we occupy */
179 	buf->flags = SAA7164_BUFFER_BUSY;
180 	buf->pos = 0;
181 
182 	/* TODO: Review this in light of 32v64 assignments */
183 	saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
184 	saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
185 	saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
186 
187 	dprintk(DBGLVL_BUF, "	buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
188 		buf->idx,
189 		(u64)port->bufoffset + (i * sizeof(u32)),
190 		saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
191 		(u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
192 		(u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
193 		saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
194 		saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
195 		buf->idx);
196 
197 	return 0;
198 }
199 
saa7164_buffer_cfg_port(struct saa7164_port * port)200 int saa7164_buffer_cfg_port(struct saa7164_port *port)
201 {
202 	struct tmHWStreamParameters *params = &port->hw_streamingparams;
203 	struct saa7164_dev *dev = port->dev;
204 	struct saa7164_buffer *buf;
205 	struct list_head *c, *n;
206 	int i = 0;
207 
208 	dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
209 
210 	saa7164_writel(port->bufcounter, 0);
211 	saa7164_writel(port->pitch, params->pitch);
212 	saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
213 
214 	dprintk(DBGLVL_BUF, " configured:\n");
215 	dprintk(DBGLVL_BUF, "   lmmio       0x%p\n", dev->lmmio);
216 	dprintk(DBGLVL_BUF, "   bufcounter  0x%x = 0x%x\n", port->bufcounter,
217 		saa7164_readl(port->bufcounter));
218 
219 	dprintk(DBGLVL_BUF, "   pitch       0x%x = %d\n", port->pitch,
220 		saa7164_readl(port->pitch));
221 
222 	dprintk(DBGLVL_BUF, "   bufsize     0x%x = %d\n", port->bufsize,
223 		saa7164_readl(port->bufsize));
224 
225 	dprintk(DBGLVL_BUF, "   buffercount = %d\n", port->hwcfg.buffercount);
226 	dprintk(DBGLVL_BUF, "   bufoffset = 0x%x\n", port->bufoffset);
227 	dprintk(DBGLVL_BUF, "   bufptr32h = 0x%x\n", port->bufptr32h);
228 	dprintk(DBGLVL_BUF, "   bufptr32l = 0x%x\n", port->bufptr32l);
229 
230 	/* Poke the buffers and offsets into PCI space */
231 	mutex_lock(&port->dmaqueue_lock);
232 	list_for_each_safe(c, n, &port->dmaqueue.list) {
233 		buf = list_entry(c, struct saa7164_buffer, list);
234 
235 		BUG_ON(buf->flags != SAA7164_BUFFER_FREE);
236 
237 		/* Place the buffer in the h/w queue */
238 		saa7164_buffer_activate(buf, i);
239 
240 		/* Don't exceed the device maximum # bufs */
241 		BUG_ON(i > port->hwcfg.buffercount);
242 		i++;
243 
244 	}
245 	mutex_unlock(&port->dmaqueue_lock);
246 
247 	return 0;
248 }
249 
saa7164_buffer_alloc_user(struct saa7164_dev * dev,u32 len)250 struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
251 	u32 len)
252 {
253 	struct saa7164_user_buffer *buf;
254 
255 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
256 	if (!buf)
257 		return NULL;
258 
259 	buf->data = kzalloc(len, GFP_KERNEL);
260 
261 	if (!buf->data) {
262 		kfree(buf);
263 		return NULL;
264 	}
265 
266 	buf->actual_size = len;
267 	buf->pos = 0;
268 	buf->crc = 0;
269 
270 	dprintk(DBGLVL_BUF, "%s()   allocated user buffer @ 0x%p\n",
271 		__func__, buf);
272 
273 	return buf;
274 }
275 
saa7164_buffer_dealloc_user(struct saa7164_user_buffer * buf)276 void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
277 {
278 	if (!buf)
279 		return;
280 
281 	kfree(buf->data);
282 	buf->data = NULL;
283 
284 	kfree(buf);
285 }
286