1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
8 #include <linux/slab.h>
12 /* The PCI address space for buffer handling looks like this:
14 * +-u32 wide-------------+
16 * +-u64 wide------------------------------------+
18 * +----------------------+
19 * | CurrentBufferPtr + Pointer to current PCI buffer >-+
20 * +----------------------+ |
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 * | +----------------+
49 * | +----------------+
50 * +->| pt[1] TS data |
51 * | +----------------+
55 void saa7164_buffer_display(struct saa7164_buffer
*buf
)
57 struct saa7164_dev
*dev
= buf
->port
->dev
;
60 dprintk(DBGLVL_BUF
, "%s() buffer @ 0x%p nr=%d\n",
61 __func__
, buf
, buf
->idx
);
62 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
);
67 /* Format the Page Table Entries to point into the data buffer */
68 for (i
= 0 ; i
< SAA7164_PT_ENTRIES
; i
++) {
70 dprintk(DBGLVL_BUF
, " pt[%02d] = 0x%p -> 0x%llx\n",
71 i
, buf
->pt_cpu
, (u64
)*(buf
->pt_cpu
));
75 /* Allocate a new buffer structure and associated PCI space in bytes.
76 * len must be a multiple of sizeof(u64)
78 struct saa7164_buffer
*saa7164_buffer_alloc(struct saa7164_port
*port
,
81 struct tmHWStreamParameters
*params
= &port
->hw_streamingparams
;
82 struct saa7164_buffer
*buf
= NULL
;
83 struct saa7164_dev
*dev
= port
->dev
;
86 if ((len
== 0) || (len
>= 65536) || (len
% sizeof(u64
))) {
87 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__
);
91 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
97 buf
->flags
= SAA7164_BUFFER_FREE
;
99 buf
->actual_size
= params
->pitch
* params
->numberoflines
;
101 /* TODO: arg len is being ignored */
102 buf
->pci_size
= SAA7164_PT_ENTRIES
* 0x1000;
103 buf
->pt_size
= (SAA7164_PT_ENTRIES
* sizeof(u64
)) + 0x1000;
105 /* Allocate contiguous memory */
106 buf
->cpu
= pci_alloc_consistent(port
->dev
->pci
, buf
->pci_size
,
111 buf
->pt_cpu
= pci_alloc_consistent(port
->dev
->pci
, buf
->pt_size
,
116 /* init the buffers to a known pattern, easier during debugging */
117 memset(buf
->cpu
, 0xff, buf
->pci_size
);
118 buf
->crc
= crc32(0, buf
->cpu
, buf
->actual_size
);
119 memset(buf
->pt_cpu
, 0xff, buf
->pt_size
);
121 dprintk(DBGLVL_BUF
, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
122 __func__
, buf
, params
->numpagetables
);
123 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
);
128 /* Format the Page Table Entries to point into the data buffer */
129 for (i
= 0 ; i
< params
->numpagetables
; i
++) {
131 *(buf
->pt_cpu
+ i
) = buf
->dma
+ (i
* 0x1000); /* TODO */
132 dprintk(DBGLVL_BUF
, " pt[%02d] = 0x%p -> 0x%llx\n",
133 i
, buf
->pt_cpu
, (u64
)*(buf
->pt_cpu
));
140 pci_free_consistent(port
->dev
->pci
, buf
->pci_size
, buf
->cpu
, buf
->dma
);
149 int saa7164_buffer_dealloc(struct saa7164_buffer
*buf
)
151 struct saa7164_dev
*dev
;
153 if (!buf
|| !buf
->port
)
154 return SAA_ERR_BAD_PARAMETER
;
155 dev
= buf
->port
->dev
;
157 dprintk(DBGLVL_BUF
, "%s() deallocating buffer @ 0x%p\n",
160 if (buf
->flags
!= SAA7164_BUFFER_FREE
)
161 log_warn(" freeing a non-free buffer\n");
163 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
);
171 int saa7164_buffer_zero_offsets(struct saa7164_port
*port
, int i
)
173 struct saa7164_dev
*dev
= port
->dev
;
175 if ((i
< 0) || (i
>= port
->hwcfg
.buffercount
))
178 dprintk(DBGLVL_BUF
, "%s(idx = %d)\n", __func__
, i
);
180 saa7164_writel(port
->bufoffset
+ (sizeof(u32
) * i
), 0);
185 /* Write a buffer into the hardware */
186 int saa7164_buffer_activate(struct saa7164_buffer
*buf
, int i
)
188 struct saa7164_port
*port
= buf
->port
;
189 struct saa7164_dev
*dev
= port
->dev
;
191 if ((i
< 0) || (i
>= port
->hwcfg
.buffercount
))
194 dprintk(DBGLVL_BUF
, "%s(idx = %d)\n", __func__
, i
);
196 buf
->idx
= i
; /* Note of which buffer list index position we occupy */
197 buf
->flags
= SAA7164_BUFFER_BUSY
;
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);
205 dprintk(DBGLVL_BUF
, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
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)),
218 int saa7164_buffer_cfg_port(struct saa7164_port
*port
)
220 struct tmHWStreamParameters
*params
= &port
->hw_streamingparams
;
221 struct saa7164_dev
*dev
= port
->dev
;
222 struct saa7164_buffer
*buf
;
223 struct list_head
*c
, *n
;
226 dprintk(DBGLVL_BUF
, "%s(port=%d)\n", __func__
, port
->nr
);
228 saa7164_writel(port
->bufcounter
, 0);
229 saa7164_writel(port
->pitch
, params
->pitch
);
230 saa7164_writel(port
->bufsize
, params
->pitch
* params
->numberoflines
);
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
));
237 dprintk(DBGLVL_BUF
, " pitch 0x%x = %d\n", port
->pitch
,
238 saa7164_readl(port
->pitch
));
240 dprintk(DBGLVL_BUF
, " bufsize 0x%x = %d\n", port
->bufsize
,
241 saa7164_readl(port
->bufsize
));
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
);
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
);
253 if (buf
->flags
!= SAA7164_BUFFER_FREE
)
256 /* Place the buffer in the h/w queue */
257 saa7164_buffer_activate(buf
, i
);
259 /* Don't exceed the device maximum # bufs */
260 if (i
++ > port
->hwcfg
.buffercount
)
264 mutex_unlock(&port
->dmaqueue_lock
);
269 struct saa7164_user_buffer
*saa7164_buffer_alloc_user(struct saa7164_dev
*dev
,
272 struct saa7164_user_buffer
*buf
;
274 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
278 buf
->data
= kzalloc(len
, GFP_KERNEL
);
285 buf
->actual_size
= len
;
289 dprintk(DBGLVL_BUF
, "%s() allocated user buffer @ 0x%p\n",
295 void saa7164_buffer_dealloc_user(struct saa7164_user_buffer
*buf
)