2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/slab.h>
26 /* The PCI address space for buffer handling looks like this:
28 +-u32 wide-------------+
30 +-u64 wide------------------------------------+
32 +----------------------+
33 | CurrentBufferPtr + Pointer to current PCI buffer >-+
34 +----------------------+ |
36 +----------------------+ |
37 | Pitch + = 188 (bytes) |
38 +----------------------+ |
39 | PCI buffer size + = pitch * number of lines (312) |
40 +----------------------+ |
41 |0| Buf0 Write Offset + |
42 +----------------------+ v
43 |1| Buf1 Write Offset + |
44 +----------------------+ |
45 |2| Buf2 Write Offset + |
46 +----------------------+ |
47 |3| Buf3 Write Offset + |
48 +----------------------+ |
49 ... More write offsets |
50 +---------------------------------------------+ |
51 +0| set of ptrs to PCI pagetables + |
52 +---------------------------------------------+ |
53 +1| set of ptrs to PCI pagetables + <--------+
54 +---------------------------------------------+
55 +2| set of ptrs to PCI pagetables +
56 +---------------------------------------------+
57 +3| set of ptrs to PCI pagetables + >--+
58 +---------------------------------------------+ |
59 ... More buffer pointers | +----------------+
69 /* Allocate a new buffer structure and associated PCI space in bytes.
70 * len must be a multiple of sizeof(u64)
72 struct saa7164_buffer
*saa7164_buffer_alloc(struct saa7164_tsport
*port
,
75 struct saa7164_buffer
*buf
= 0;
76 struct saa7164_dev
*dev
= port
->dev
;
79 if ((len
== 0) || (len
>= 65536) || (len
% sizeof(u64
))) {
80 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__
);
84 buf
= kzalloc(sizeof(struct saa7164_buffer
), GFP_KERNEL
);
86 log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__
);
91 buf
->flags
= SAA7164_BUFFER_FREE
;
92 /* TODO: arg len is being ignored */
93 buf
->pci_size
= SAA7164_PT_ENTRIES
* 0x1000;
94 buf
->pt_size
= (SAA7164_PT_ENTRIES
* sizeof(u64
)) + 0x1000;
96 /* Allocate contiguous memory */
97 buf
->cpu
= pci_alloc_consistent(port
->dev
->pci
, buf
->pci_size
,
102 buf
->pt_cpu
= pci_alloc_consistent(port
->dev
->pci
, buf
->pt_size
,
107 /* init the buffers to a known pattern, easier during debugging */
108 memset(buf
->cpu
, 0xff, buf
->pci_size
);
109 memset(buf
->pt_cpu
, 0xff, buf
->pt_size
);
111 dprintk(DBGLVL_BUF
, "%s() allocated buffer @ 0x%p\n", __func__
, buf
);
112 dprintk(DBGLVL_BUF
, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
113 buf
->cpu
, (long)buf
->dma
, buf
->pci_size
);
114 dprintk(DBGLVL_BUF
, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
115 buf
->pt_cpu
, (long)buf
->pt_dma
, buf
->pt_size
);
117 /* Format the Page Table Entries to point into the data buffer */
118 for (i
= 0 ; i
< SAA7164_PT_ENTRIES
; i
++) {
120 *(buf
->pt_cpu
+ i
) = buf
->dma
+ (i
* 0x1000); /* TODO */
127 pci_free_consistent(port
->dev
->pci
, buf
->pci_size
, buf
->cpu
, buf
->dma
);
136 int saa7164_buffer_dealloc(struct saa7164_tsport
*port
,
137 struct saa7164_buffer
*buf
)
139 struct saa7164_dev
*dev
;
142 return SAA_ERR_BAD_PARAMETER
;
145 dprintk(DBGLVL_BUF
, "%s() deallocating buffer @ 0x%p\n", __func__
, buf
);
147 if (buf
->flags
!= SAA7164_BUFFER_FREE
)
148 log_warn(" freeing a non-free buffer\n");
150 pci_free_consistent(port
->dev
->pci
, buf
->pci_size
, buf
->cpu
, buf
->dma
);
151 pci_free_consistent(port
->dev
->pci
, buf
->pt_size
, buf
->pt_cpu
,