Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 2 | /* |
| 3 | * Driver for the NXP SAA7164 PCIe bridge |
| 4 | * |
Steven Toth | 63a412e | 2015-03-23 16:08:15 -0300 | [diff] [blame] | 5 | * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com> |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
| 9 | |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 10 | #include "saa7164.h" |
| 11 | |
| 12 | /* The PCI address space for buffer handling looks like this: |
Steven Toth | bc25068 | 2010-11-12 18:32:36 -0300 | [diff] [blame] | 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 |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 53 | */ |
| 54 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 55 | void 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 Chehab | 5cecdc8 | 2010-10-11 16:26:04 -0300 | [diff] [blame] | 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); |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 66 | |
| 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 Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 75 | /* Allocate a new buffer structure and associated PCI space in bytes. |
| 76 | * len must be a multiple of sizeof(u64) |
| 77 | */ |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 78 | struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port, |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 79 | u32 len) |
| 80 | { |
Mauro Carvalho Chehab | 4d270cf | 2010-10-11 17:17:45 -0300 | [diff] [blame] | 81 | struct tmHWStreamParameters *params = &port->hw_streamingparams; |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 82 | struct saa7164_buffer *buf = NULL; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 83 | 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 Elfring | 2d3da59 | 2017-08-28 05:55:16 -0400 | [diff] [blame] | 91 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
Markus Elfring | c38e865 | 2017-08-28 05:46:57 -0400 | [diff] [blame] | 92 | if (!buf) |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 93 | goto ret; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 94 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 95 | buf->idx = -1; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 96 | buf->port = port; |
| 97 | buf->flags = SAA7164_BUFFER_FREE; |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 98 | buf->pos = 0; |
| 99 | buf->actual_size = params->pitch * params->numberoflines; |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 100 | buf->crc = 0; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 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; |
| 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 Verkuil | 065e147 | 2014-11-07 11:39:46 -0300 | [diff] [blame] | 117 | memset(buf->cpu, 0xff, buf->pci_size); |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 118 | buf->crc = crc32(0, buf->cpu, buf->actual_size); |
Hans Verkuil | 065e147 | 2014-11-07 11:39:46 -0300 | [diff] [blame] | 119 | memset(buf->pt_cpu, 0xff, buf->pt_size); |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 120 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 121 | dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n", |
| 122 | __func__, buf, params->numpagetables); |
Mauro Carvalho Chehab | ab20585 | 2009-09-19 00:41:18 -0300 | [diff] [blame] | 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); |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 127 | |
| 128 | /* Format the Page Table Entries to point into the data buffer */ |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 129 | for (i = 0 ; i < params->numpagetables; i++) { |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 130 | |
| 131 | *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */ |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 132 | dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n", |
| 133 | i, buf->pt_cpu, (u64)*(buf->pt_cpu)); |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 134 | |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | goto ret; |
| 138 | |
| 139 | fail2: |
| 140 | pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma); |
| 141 | fail1: |
| 142 | kfree(buf); |
| 143 | |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 144 | buf = NULL; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 145 | ret: |
| 146 | return buf; |
| 147 | } |
| 148 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 149 | int saa7164_buffer_dealloc(struct saa7164_buffer *buf) |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 150 | { |
Dan Carpenter | 23e64d5 | 2010-08-19 07:00:22 -0300 | [diff] [blame] | 151 | struct saa7164_dev *dev; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 152 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 153 | if (!buf || !buf->port) |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 154 | return SAA_ERR_BAD_PARAMETER; |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 155 | dev = buf->port->dev; |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 156 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 157 | dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", |
| 158 | __func__, buf); |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 159 | |
| 160 | if (buf->flags != SAA7164_BUFFER_FREE) |
| 161 | log_warn(" freeing a non-free buffer\n"); |
| 162 | |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 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); |
Steven Toth | 443c1228 | 2009-05-09 21:17:28 -0300 | [diff] [blame] | 165 | |
| 166 | kfree(buf); |
| 167 | |
| 168 | return SAA_OK; |
| 169 | } |
| 170 | |
Steven Toth | 9230aca | 2010-07-31 15:06:49 -0300 | [diff] [blame] | 171 | int 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 Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 185 | /* Write a buffer into the hardware */ |
| 186 | int 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 Chehab | 24f711c | 2016-10-18 17:44:07 -0200 | [diff] [blame] | 205 | dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n", |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 206 | 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 | |
| 218 | int saa7164_buffer_cfg_port(struct saa7164_port *port) |
| 219 | { |
Mauro Carvalho Chehab | 4d270cf | 2010-10-11 17:17:45 -0300 | [diff] [blame] | 220 | struct tmHWStreamParameters *params = &port->hw_streamingparams; |
Steven Toth | add3f58 | 2010-07-31 14:43:07 -0300 | [diff] [blame] | 221 | 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 Toth | bc25068 | 2010-11-12 18:32:36 -0300 | [diff] [blame] | 269 | struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev, |
| 270 | u32 len) |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 271 | { |
| 272 | struct saa7164_user_buffer *buf; |
| 273 | |
Markus Elfring | 2d3da59 | 2017-08-28 05:55:16 -0400 | [diff] [blame] | 274 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 275 | if (!buf) |
| 276 | return NULL; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 277 | |
| 278 | buf->data = kzalloc(len, GFP_KERNEL); |
| 279 | |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 280 | if (!buf->data) { |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 281 | kfree(buf); |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 282 | return NULL; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | buf->actual_size = len; |
| 286 | buf->pos = 0; |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 287 | buf->crc = 0; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 288 | |
| 289 | dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n", |
| 290 | __func__, buf); |
| 291 | |
| 292 | return buf; |
| 293 | } |
| 294 | |
| 295 | void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf) |
| 296 | { |
| 297 | if (!buf) |
| 298 | return; |
| 299 | |
Steven Toth | bc25068 | 2010-11-12 18:32:36 -0300 | [diff] [blame] | 300 | kfree(buf->data); |
Peter Huewe | 61ca1500 | 2011-01-30 16:33:01 -0300 | [diff] [blame] | 301 | buf->data = NULL; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 302 | |
Steven Toth | bc25068 | 2010-11-12 18:32:36 -0300 | [diff] [blame] | 303 | kfree(buf); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 304 | } |
| 305 | |