blob: 289cb901985bc5038ac67723396d905767867be9 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steven Toth443c12282009-05-09 21:17:28 -03002/*
3 * Driver for the NXP SAA7164 PCIe bridge
4 *
Steven Toth63a412e2015-03-23 16:08:15 -03005 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
Steven Toth443c12282009-05-09 21:17:28 -03006 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
9
Steven Toth443c12282009-05-09 21:17:28 -030010#include "saa7164.h"
11
12/* The PCI address space for buffer handling looks like this:
Steven Tothbc250682010-11-12 18:32:36 -030013 *
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
Steven Toth443c12282009-05-09 21:17:28 -030053 */
54
Steven Tothadd3f582010-07-31 14:43:07 -030055void saa7164_buffer_display(struct saa7164_buffer *buf)
56{
57 struct saa7164_dev *dev = buf->port->dev;
58 int i;
59
60 dprintk(DBGLVL_BUF, "%s() buffer @ 0x%p nr=%d\n",
61 __func__, buf, buf->idx);
Mauro Carvalho Chehab5cecdc82010-10-11 16:26:04 -030062 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08llx len = 0x%x\n",
63 buf->cpu, (long long)buf->dma, buf->pci_size);
64 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08llx len = 0x%x\n",
65 buf->pt_cpu, (long long)buf->pt_dma, buf->pt_size);
Steven Tothadd3f582010-07-31 14:43:07 -030066
67 /* Format the Page Table Entries to point into the data buffer */
68 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
69
70 dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
71 i, buf->pt_cpu, (u64)*(buf->pt_cpu));
72
73 }
74}
Steven Toth443c12282009-05-09 21:17:28 -030075/* Allocate a new buffer structure and associated PCI space in bytes.
76 * len must be a multiple of sizeof(u64)
77 */
Steven Tothadd3f582010-07-31 14:43:07 -030078struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
Steven Toth443c12282009-05-09 21:17:28 -030079 u32 len)
80{
Mauro Carvalho Chehab4d270cf2010-10-11 17:17:45 -030081 struct tmHWStreamParameters *params = &port->hw_streamingparams;
Peter Huewe61ca15002011-01-30 16:33:01 -030082 struct saa7164_buffer *buf = NULL;
Steven Toth443c12282009-05-09 21:17:28 -030083 struct saa7164_dev *dev = port->dev;
84 int i;
85
86 if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
87 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
88 goto ret;
89 }
90
Markus Elfring2d3da592017-08-28 05:55:16 -040091 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
Markus Elfringc38e8652017-08-28 05:46:57 -040092 if (!buf)
Steven Toth443c12282009-05-09 21:17:28 -030093 goto ret;
Steven Toth443c12282009-05-09 21:17:28 -030094
Steven Tothadd3f582010-07-31 14:43:07 -030095 buf->idx = -1;
Steven Toth443c12282009-05-09 21:17:28 -030096 buf->port = port;
97 buf->flags = SAA7164_BUFFER_FREE;
Steven Tothadd3f582010-07-31 14:43:07 -030098 buf->pos = 0;
99 buf->actual_size = params->pitch * params->numberoflines;
Steven Toth12d32032010-07-31 15:13:45 -0300100 buf->crc = 0;
Steven Toth443c12282009-05-09 21:17:28 -0300101 /* TODO: arg len is being ignored */
102 buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
103 buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
104
105 /* Allocate contiguous memory */
106 buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
107 &buf->dma);
108 if (!buf->cpu)
109 goto fail1;
110
111 buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
112 &buf->pt_dma);
113 if (!buf->pt_cpu)
114 goto fail2;
115
116 /* init the buffers to a known pattern, easier during debugging */
Hans Verkuil065e1472014-11-07 11:39:46 -0300117 memset(buf->cpu, 0xff, buf->pci_size);
Steven Toth12d32032010-07-31 15:13:45 -0300118 buf->crc = crc32(0, buf->cpu, buf->actual_size);
Hans Verkuil065e1472014-11-07 11:39:46 -0300119 memset(buf->pt_cpu, 0xff, buf->pt_size);
Steven Toth443c12282009-05-09 21:17:28 -0300120
Steven Toth1b0e8e42010-07-31 16:01:00 -0300121 dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
122 __func__, buf, params->numpagetables);
Mauro Carvalho Chehabab205852009-09-19 00:41:18 -0300123 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
124 buf->cpu, (long)buf->dma, buf->pci_size);
125 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
126 buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
Steven Toth443c12282009-05-09 21:17:28 -0300127
128 /* Format the Page Table Entries to point into the data buffer */
Steven Toth1b0e8e42010-07-31 16:01:00 -0300129 for (i = 0 ; i < params->numpagetables; i++) {
Steven Toth443c12282009-05-09 21:17:28 -0300130
131 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
Steven Toth1b0e8e42010-07-31 16:01:00 -0300132 dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
133 i, buf->pt_cpu, (u64)*(buf->pt_cpu));
Steven Toth443c12282009-05-09 21:17:28 -0300134
Steven Toth443c12282009-05-09 21:17:28 -0300135 }
136
137 goto ret;
138
139fail2:
140 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
141fail1:
142 kfree(buf);
143
Peter Huewe61ca15002011-01-30 16:33:01 -0300144 buf = NULL;
Steven Toth443c12282009-05-09 21:17:28 -0300145ret:
146 return buf;
147}
148
Steven Tothadd3f582010-07-31 14:43:07 -0300149int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
Steven Toth443c12282009-05-09 21:17:28 -0300150{
Dan Carpenter23e64d52010-08-19 07:00:22 -0300151 struct saa7164_dev *dev;
Steven Toth443c12282009-05-09 21:17:28 -0300152
Steven Tothadd3f582010-07-31 14:43:07 -0300153 if (!buf || !buf->port)
Steven Toth443c12282009-05-09 21:17:28 -0300154 return SAA_ERR_BAD_PARAMETER;
Steven Tothadd3f582010-07-31 14:43:07 -0300155 dev = buf->port->dev;
Steven Toth443c12282009-05-09 21:17:28 -0300156
Steven Tothadd3f582010-07-31 14:43:07 -0300157 dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
158 __func__, buf);
Steven Toth443c12282009-05-09 21:17:28 -0300159
160 if (buf->flags != SAA7164_BUFFER_FREE)
161 log_warn(" freeing a non-free buffer\n");
162
Steven Tothadd3f582010-07-31 14:43:07 -0300163 pci_free_consistent(dev->pci, buf->pci_size, buf->cpu, buf->dma);
164 pci_free_consistent(dev->pci, buf->pt_size, buf->pt_cpu, buf->pt_dma);
Steven Toth443c12282009-05-09 21:17:28 -0300165
166 kfree(buf);
167
168 return SAA_OK;
169}
170
Steven Toth9230aca2010-07-31 15:06:49 -0300171int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
172{
173 struct saa7164_dev *dev = port->dev;
174
175 if ((i < 0) || (i >= port->hwcfg.buffercount))
176 return -EINVAL;
177
178 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
179
180 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
181
182 return 0;
183}
184
Steven Tothadd3f582010-07-31 14:43:07 -0300185/* Write a buffer into the hardware */
186int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
187{
188 struct saa7164_port *port = buf->port;
189 struct saa7164_dev *dev = port->dev;
190
191 if ((i < 0) || (i >= port->hwcfg.buffercount))
192 return -EINVAL;
193
194 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
195
196 buf->idx = i; /* Note of which buffer list index position we occupy */
197 buf->flags = SAA7164_BUFFER_BUSY;
198 buf->pos = 0;
199
200 /* TODO: Review this in light of 32v64 assignments */
201 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
202 saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
203 saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
204
Mauro Carvalho Chehab24f711c2016-10-18 17:44:07 -0200205 dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
Steven Tothadd3f582010-07-31 14:43:07 -0300206 buf->idx,
207 (u64)port->bufoffset + (i * sizeof(u32)),
208 saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
209 (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
210 (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
211 saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
212 saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
213 buf->idx);
214
215 return 0;
216}
217
218int saa7164_buffer_cfg_port(struct saa7164_port *port)
219{
Mauro Carvalho Chehab4d270cf2010-10-11 17:17:45 -0300220 struct tmHWStreamParameters *params = &port->hw_streamingparams;
Steven Tothadd3f582010-07-31 14:43:07 -0300221 struct saa7164_dev *dev = port->dev;
222 struct saa7164_buffer *buf;
223 struct list_head *c, *n;
224 int i = 0;
225
226 dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
227
228 saa7164_writel(port->bufcounter, 0);
229 saa7164_writel(port->pitch, params->pitch);
230 saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
231
232 dprintk(DBGLVL_BUF, " configured:\n");
233 dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
234 dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
235 saa7164_readl(port->bufcounter));
236
237 dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
238 saa7164_readl(port->pitch));
239
240 dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
241 saa7164_readl(port->bufsize));
242
243 dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
244 dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
245 dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
246 dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
247
248 /* Poke the buffers and offsets into PCI space */
249 mutex_lock(&port->dmaqueue_lock);
250 list_for_each_safe(c, n, &port->dmaqueue.list) {
251 buf = list_entry(c, struct saa7164_buffer, list);
252
253 if (buf->flags != SAA7164_BUFFER_FREE)
254 BUG();
255
256 /* Place the buffer in the h/w queue */
257 saa7164_buffer_activate(buf, i);
258
259 /* Don't exceed the device maximum # bufs */
260 if (i++ > port->hwcfg.buffercount)
261 BUG();
262
263 }
264 mutex_unlock(&port->dmaqueue_lock);
265
266 return 0;
267}
268
Steven Tothbc250682010-11-12 18:32:36 -0300269struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
270 u32 len)
Steven Toth7615e432010-07-31 14:44:53 -0300271{
272 struct saa7164_user_buffer *buf;
273
Markus Elfring2d3da592017-08-28 05:55:16 -0400274 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
Peter Huewe61ca15002011-01-30 16:33:01 -0300275 if (!buf)
276 return NULL;
Steven Toth7615e432010-07-31 14:44:53 -0300277
278 buf->data = kzalloc(len, GFP_KERNEL);
279
Peter Huewe61ca15002011-01-30 16:33:01 -0300280 if (!buf->data) {
Steven Toth7615e432010-07-31 14:44:53 -0300281 kfree(buf);
Peter Huewe61ca15002011-01-30 16:33:01 -0300282 return NULL;
Steven Toth7615e432010-07-31 14:44:53 -0300283 }
284
285 buf->actual_size = len;
286 buf->pos = 0;
Steven Toth12d32032010-07-31 15:13:45 -0300287 buf->crc = 0;
Steven Toth7615e432010-07-31 14:44:53 -0300288
289 dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
290 __func__, buf);
291
292 return buf;
293}
294
295void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
296{
297 if (!buf)
298 return;
299
Steven Tothbc250682010-11-12 18:32:36 -0300300 kfree(buf->data);
Peter Huewe61ca15002011-01-30 16:33:01 -0300301 buf->data = NULL;
Steven Toth7615e432010-07-31 14:44:53 -0300302
Steven Tothbc250682010-11-12 18:32:36 -0300303 kfree(buf);
Steven Toth7615e432010-07-31 14:44:53 -0300304}
305