blob: dba713f2a0d0256eb860dccfc4e9a7363c5f75d2 [file] [log] [blame]
David Cohen68e342b2011-02-12 18:05:06 -03001/*
2 * ispstat.c
3 *
4 * TI OMAP3 ISP - Statistics core
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc
8 *
9 * Contacts: David Cohen <dacohen@gmail.com>
10 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 */
27
28#include <linux/dma-mapping.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070029#include <linux/omap-iommu.h>
David Cohen68e342b2011-02-12 18:05:06 -030030#include <linux/slab.h>
31#include <linux/uaccess.h>
32
33#include "isp.h"
34
Laurent Pinchart63746b52014-01-02 09:47:32 -030035#define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch >= 0)
David Cohen68e342b2011-02-12 18:05:06 -030036
37/*
38 * MAGIC_SIZE must always be the greatest common divisor of
39 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
40 */
41#define MAGIC_SIZE 16
42#define MAGIC_NUM 0x55
43
44/* HACK: AF module seems to be writing one more paxel data than it should. */
45#define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
46
47/*
48 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
49 * the next buffer to start to be written in the same point where the overflow
50 * occurred instead of the configured address. The only known way to make it to
51 * go back to a valid state is having a valid buffer processing. Of course it
52 * requires at least a doubled buffer size to avoid an access to invalid memory
53 * region. But it does not fix everything. It may happen more than one
54 * consecutive SBL overflows. In that case, it might be unpredictable how many
55 * buffers the allocated memory should fit. For that case, a recover
56 * configuration was created. It produces the minimum buffer size for each H3A
57 * module and decrease the change for more SBL overflows. This recover state
58 * will be enabled every time a SBL overflow occur. As the output buffer size
59 * isn't big, it's possible to have an extra size able to fit many recover
60 * buffers making it extreamily unlikely to have an access to invalid memory
61 * region.
62 */
63#define NUM_H3A_RECOVER_BUFS 10
64
65/*
66 * HACK: Because of HW issues the generic layer sometimes need to have
67 * different behaviour for different statistic modules.
68 */
69#define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
70#define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
71#define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
72
73static void __isp_stat_buf_sync_magic(struct ispstat *stat,
74 struct ispstat_buffer *buf,
75 u32 buf_size, enum dma_data_direction dir,
76 void (*dma_sync)(struct device *,
77 dma_addr_t, unsigned long, size_t,
78 enum dma_data_direction))
79{
80 struct device *dev = stat->isp->dev;
81 struct page *pg;
82 dma_addr_t dma_addr;
83 u32 offset;
84
85 /* Initial magic words */
86 pg = vmalloc_to_page(buf->virt_addr);
87 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
88 dma_sync(dev, dma_addr, 0, MAGIC_SIZE, dir);
89
90 /* Final magic words */
91 pg = vmalloc_to_page(buf->virt_addr + buf_size);
92 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
93 offset = ((u32)buf->virt_addr + buf_size) & ~PAGE_MASK;
94 dma_sync(dev, dma_addr, offset, MAGIC_SIZE, dir);
95}
96
97static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
98 struct ispstat_buffer *buf,
99 u32 buf_size,
100 enum dma_data_direction dir)
101{
Laurent Pinchart63746b52014-01-02 09:47:32 -0300102 if (ISP_STAT_USES_DMAENGINE(stat))
David Cohen68e342b2011-02-12 18:05:06 -0300103 return;
104
105 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
106 dma_sync_single_range_for_device);
107}
108
109static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
110 struct ispstat_buffer *buf,
111 u32 buf_size,
112 enum dma_data_direction dir)
113{
Laurent Pinchart63746b52014-01-02 09:47:32 -0300114 if (ISP_STAT_USES_DMAENGINE(stat))
David Cohen68e342b2011-02-12 18:05:06 -0300115 return;
116
117 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
118 dma_sync_single_range_for_cpu);
119}
120
121static int isp_stat_buf_check_magic(struct ispstat *stat,
122 struct ispstat_buffer *buf)
123{
124 const u32 buf_size = IS_H3A_AF(stat) ?
125 buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
126 u8 *w;
127 u8 *end;
128 int ret = -EINVAL;
129
130 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
131
132 /* Checking initial magic numbers. They shouldn't be here anymore. */
133 for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
134 if (likely(*w != MAGIC_NUM))
135 ret = 0;
136
137 if (ret) {
138 dev_dbg(stat->isp->dev, "%s: beginning magic check does not "
139 "match.\n", stat->subdev.name);
140 return ret;
141 }
142
143 /* Checking magic numbers at the end. They must be still here. */
144 for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
145 w < end; w++) {
146 if (unlikely(*w != MAGIC_NUM)) {
Lad, Prabhakar25aeb412014-02-21 09:07:21 -0300147 dev_dbg(stat->isp->dev, "%s: ending magic check does "
David Cohen68e342b2011-02-12 18:05:06 -0300148 "not match.\n", stat->subdev.name);
149 return -EINVAL;
150 }
151 }
152
153 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
154 DMA_FROM_DEVICE);
155
156 return 0;
157}
158
159static void isp_stat_buf_insert_magic(struct ispstat *stat,
160 struct ispstat_buffer *buf)
161{
162 const u32 buf_size = IS_H3A_AF(stat) ?
163 stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
164
165 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
166
167 /*
168 * Inserting MAGIC_NUM at the beginning and end of the buffer.
169 * buf->buf_size is set only after the buffer is queued. For now the
170 * right buf_size for the current configuration is pointed by
171 * stat->buf_size.
172 */
173 memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
174 memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
175
176 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
177 DMA_BIDIRECTIONAL);
178}
179
180static void isp_stat_buf_sync_for_device(struct ispstat *stat,
181 struct ispstat_buffer *buf)
182{
Laurent Pinchart63746b52014-01-02 09:47:32 -0300183 if (ISP_STAT_USES_DMAENGINE(stat))
David Cohen68e342b2011-02-12 18:05:06 -0300184 return;
185
186 dma_sync_sg_for_device(stat->isp->dev, buf->iovm->sgt->sgl,
187 buf->iovm->sgt->nents, DMA_FROM_DEVICE);
188}
189
190static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
191 struct ispstat_buffer *buf)
192{
Laurent Pinchart63746b52014-01-02 09:47:32 -0300193 if (ISP_STAT_USES_DMAENGINE(stat))
David Cohen68e342b2011-02-12 18:05:06 -0300194 return;
195
196 dma_sync_sg_for_cpu(stat->isp->dev, buf->iovm->sgt->sgl,
197 buf->iovm->sgt->nents, DMA_FROM_DEVICE);
198}
199
200static void isp_stat_buf_clear(struct ispstat *stat)
201{
202 int i;
203
204 for (i = 0; i < STAT_MAX_BUFS; i++)
205 stat->buf[i].empty = 1;
206}
207
208static struct ispstat_buffer *
209__isp_stat_buf_find(struct ispstat *stat, int look_empty)
210{
211 struct ispstat_buffer *found = NULL;
212 int i;
213
214 for (i = 0; i < STAT_MAX_BUFS; i++) {
215 struct ispstat_buffer *curr = &stat->buf[i];
216
217 /*
218 * Don't select the buffer which is being copied to
219 * userspace or used by the module.
220 */
221 if (curr == stat->locked_buf || curr == stat->active_buf)
222 continue;
223
224 /* Don't select uninitialised buffers if it's not required */
225 if (!look_empty && curr->empty)
226 continue;
227
228 /* Pick uninitialised buffer over anything else if look_empty */
229 if (curr->empty) {
230 found = curr;
231 break;
232 }
233
234 /* Choose the oldest buffer */
235 if (!found ||
236 (s32)curr->frame_number - (s32)found->frame_number < 0)
237 found = curr;
238 }
239
240 return found;
241}
242
243static inline struct ispstat_buffer *
244isp_stat_buf_find_oldest(struct ispstat *stat)
245{
246 return __isp_stat_buf_find(stat, 0);
247}
248
249static inline struct ispstat_buffer *
250isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
251{
252 return __isp_stat_buf_find(stat, 1);
253}
254
255static int isp_stat_buf_queue(struct ispstat *stat)
256{
257 if (!stat->active_buf)
258 return STAT_NO_BUF;
259
Laurent Pinchart07d19e32012-09-12 22:10:40 -0300260 ktime_get_ts(&stat->active_buf->ts);
David Cohen68e342b2011-02-12 18:05:06 -0300261
262 stat->active_buf->buf_size = stat->buf_size;
263 if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
264 dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
265 stat->subdev.name);
266 return STAT_NO_BUF;
267 }
268 stat->active_buf->config_counter = stat->config_counter;
269 stat->active_buf->frame_number = stat->frame_number;
270 stat->active_buf->empty = 0;
271 stat->active_buf = NULL;
272
273 return STAT_BUF_DONE;
274}
275
276/* Get next free buffer to write the statistics to and mark it active. */
277static void isp_stat_buf_next(struct ispstat *stat)
278{
279 if (unlikely(stat->active_buf))
280 /* Overwriting unused active buffer */
281 dev_dbg(stat->isp->dev, "%s: new buffer requested without "
282 "queuing active one.\n",
283 stat->subdev.name);
284 else
285 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
286}
287
288static void isp_stat_buf_release(struct ispstat *stat)
289{
290 unsigned long flags;
291
292 isp_stat_buf_sync_for_device(stat, stat->locked_buf);
293 spin_lock_irqsave(&stat->isp->stat_lock, flags);
294 stat->locked_buf = NULL;
295 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
296}
297
298/* Get buffer to userspace. */
299static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
300 struct omap3isp_stat_data *data)
301{
302 int rval = 0;
303 unsigned long flags;
304 struct ispstat_buffer *buf;
305
306 spin_lock_irqsave(&stat->isp->stat_lock, flags);
307
308 while (1) {
309 buf = isp_stat_buf_find_oldest(stat);
310 if (!buf) {
311 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
312 dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
313 stat->subdev.name);
314 return ERR_PTR(-EBUSY);
315 }
316 if (isp_stat_buf_check_magic(stat, buf)) {
317 dev_dbg(stat->isp->dev, "%s: current buffer has "
318 "corrupted data\n.", stat->subdev.name);
319 /* Mark empty because it doesn't have valid data. */
320 buf->empty = 1;
321 } else {
322 /* Buffer isn't corrupted. */
323 break;
324 }
325 }
326
327 stat->locked_buf = buf;
328
329 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
330
331 if (buf->buf_size > data->buf_size) {
332 dev_warn(stat->isp->dev, "%s: userspace's buffer size is "
333 "not enough.\n", stat->subdev.name);
334 isp_stat_buf_release(stat);
335 return ERR_PTR(-EINVAL);
336 }
337
338 isp_stat_buf_sync_for_cpu(stat, buf);
339
340 rval = copy_to_user(data->buf,
341 buf->virt_addr,
342 buf->buf_size);
343
344 if (rval) {
345 dev_info(stat->isp->dev,
346 "%s: failed copying %d bytes of stat data\n",
347 stat->subdev.name, rval);
348 buf = ERR_PTR(-EFAULT);
349 isp_stat_buf_release(stat);
350 }
351
352 return buf;
353}
354
355static void isp_stat_bufs_free(struct ispstat *stat)
356{
357 struct isp_device *isp = stat->isp;
358 int i;
359
360 for (i = 0; i < STAT_MAX_BUFS; i++) {
361 struct ispstat_buffer *buf = &stat->buf[i];
362
Laurent Pinchart63746b52014-01-02 09:47:32 -0300363 if (!ISP_STAT_USES_DMAENGINE(stat)) {
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300364 if (IS_ERR_OR_NULL((void *)buf->dma_addr))
David Cohen68e342b2011-02-12 18:05:06 -0300365 continue;
366 if (buf->iovm)
367 dma_unmap_sg(isp->dev, buf->iovm->sgt->sgl,
368 buf->iovm->sgt->nents,
369 DMA_FROM_DEVICE);
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300370 omap_iommu_vfree(isp->domain, isp->dev, buf->dma_addr);
David Cohen68e342b2011-02-12 18:05:06 -0300371 } else {
372 if (!buf->virt_addr)
373 continue;
374 dma_free_coherent(stat->isp->dev, stat->buf_alloc_size,
375 buf->virt_addr, buf->dma_addr);
376 }
David Cohen68e342b2011-02-12 18:05:06 -0300377 buf->iovm = NULL;
378 buf->dma_addr = 0;
379 buf->virt_addr = NULL;
380 buf->empty = 1;
381 }
382
383 dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
384 stat->subdev.name);
385
386 stat->buf_alloc_size = 0;
387 stat->active_buf = NULL;
388}
389
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300390static int isp_stat_bufs_alloc_iommu(struct ispstat *stat,
391 struct ispstat_buffer *buf,
392 unsigned int size)
David Cohen68e342b2011-02-12 18:05:06 -0300393{
394 struct isp_device *isp = stat->isp;
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300395 struct iovm_struct *iovm;
David Cohen68e342b2011-02-12 18:05:06 -0300396
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300397 buf->dma_addr = omap_iommu_vmalloc(isp->domain, isp->dev, 0,
398 size, IOMMU_FLAG);
399 if (IS_ERR_VALUE(buf->dma_addr))
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300400 return -ENOMEM;
David Cohen68e342b2011-02-12 18:05:06 -0300401
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300402 iovm = omap_find_iovm_area(isp->dev, buf->dma_addr);
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300403 if (!iovm)
404 return -ENOMEM;
David Cohen68e342b2011-02-12 18:05:06 -0300405
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300406 if (!dma_map_sg(isp->dev, iovm->sgt->sgl, iovm->sgt->nents,
407 DMA_FROM_DEVICE))
408 return -ENOMEM;
David Cohen68e342b2011-02-12 18:05:06 -0300409
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300410 buf->iovm = iovm;
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300411 buf->virt_addr = omap_da_to_va(stat->isp->dev, buf->dma_addr);
David Cohen68e342b2011-02-12 18:05:06 -0300412
413 return 0;
414}
415
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300416static int isp_stat_bufs_alloc_dma(struct ispstat *stat,
417 struct ispstat_buffer *buf,
418 unsigned int size)
David Cohen68e342b2011-02-12 18:05:06 -0300419{
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300420 buf->virt_addr = dma_alloc_coherent(stat->isp->dev, size,
421 &buf->dma_addr, GFP_KERNEL | GFP_DMA);
David Cohen68e342b2011-02-12 18:05:06 -0300422
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300423 if (!buf->virt_addr || !buf->dma_addr)
424 return -ENOMEM;
David Cohen68e342b2011-02-12 18:05:06 -0300425
426 return 0;
427}
428
429static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
430{
431 unsigned long flags;
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300432 unsigned int i;
David Cohen68e342b2011-02-12 18:05:06 -0300433
434 spin_lock_irqsave(&stat->isp->stat_lock, flags);
435
436 BUG_ON(stat->locked_buf != NULL);
437
438 /* Are the old buffers big enough? */
439 if (stat->buf_alloc_size >= size) {
440 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
441 return 0;
442 }
443
444 if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
445 dev_info(stat->isp->dev,
446 "%s: trying to allocate memory when busy\n",
447 stat->subdev.name);
448 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
449 return -EBUSY;
450 }
451
452 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
453
454 isp_stat_bufs_free(stat);
455
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300456 stat->buf_alloc_size = size;
457
458 for (i = 0; i < STAT_MAX_BUFS; i++) {
459 struct ispstat_buffer *buf = &stat->buf[i];
460 int ret;
461
462 if (ISP_STAT_USES_DMAENGINE(stat))
463 ret = isp_stat_bufs_alloc_dma(stat, buf, size);
464 else
465 ret = isp_stat_bufs_alloc_iommu(stat, buf, size);
466
467 if (ret < 0) {
468 dev_err(stat->isp->dev,
469 "%s: Failed to allocate DMA buffer %u\n",
470 stat->subdev.name, i);
471 isp_stat_bufs_free(stat);
472 return ret;
473 }
474
475 buf->empty = 1;
476
477 dev_dbg(stat->isp->dev,
Laurent Pinchartcbde9e92014-01-02 10:27:12 -0300478 "%s: buffer[%u] allocated. dma=0x%08lx virt=0x%08lx",
479 stat->subdev.name, i,
Laurent Pinchart4d4c00d2014-01-02 10:09:00 -0300480 (unsigned long)buf->dma_addr,
481 (unsigned long)buf->virt_addr);
482 }
483
484 return 0;
David Cohen68e342b2011-02-12 18:05:06 -0300485}
486
487static void isp_stat_queue_event(struct ispstat *stat, int err)
488{
Laurent Pinchartc070e382011-11-16 10:54:02 -0300489 struct video_device *vdev = stat->subdev.devnode;
David Cohen68e342b2011-02-12 18:05:06 -0300490 struct v4l2_event event;
491 struct omap3isp_stat_event_status *status = (void *)event.u.data;
492
493 memset(&event, 0, sizeof(event));
494 if (!err) {
495 status->frame_number = stat->frame_number;
496 status->config_counter = stat->config_counter;
497 } else {
498 status->buf_err = 1;
499 }
500 event.type = stat->event_type;
501 v4l2_event_queue(vdev, &event);
502}
503
504
505/*
506 * omap3isp_stat_request_statistics - Request statistics.
507 * @data: Pointer to return statistics data.
508 *
509 * Returns 0 if successful.
510 */
511int omap3isp_stat_request_statistics(struct ispstat *stat,
512 struct omap3isp_stat_data *data)
513{
514 struct ispstat_buffer *buf;
515
516 if (stat->state != ISPSTAT_ENABLED) {
517 dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
518 stat->subdev.name);
519 return -EINVAL;
520 }
521
522 mutex_lock(&stat->ioctl_lock);
523 buf = isp_stat_buf_get(stat, data);
524 if (IS_ERR(buf)) {
525 mutex_unlock(&stat->ioctl_lock);
526 return PTR_ERR(buf);
527 }
528
Laurent Pinchart07d19e32012-09-12 22:10:40 -0300529 data->ts.tv_sec = buf->ts.tv_sec;
530 data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
David Cohen68e342b2011-02-12 18:05:06 -0300531 data->config_counter = buf->config_counter;
532 data->frame_number = buf->frame_number;
533 data->buf_size = buf->buf_size;
534
535 buf->empty = 1;
536 isp_stat_buf_release(stat);
537 mutex_unlock(&stat->ioctl_lock);
538
539 return 0;
540}
541
542/*
543 * omap3isp_stat_config - Receives new statistic engine configuration.
544 * @new_conf: Pointer to config structure.
545 *
546 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
547 * was unable to allocate memory for the buffer, or other errors if parameters
548 * are invalid.
549 */
550int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
551{
552 int ret;
553 unsigned long irqflags;
554 struct ispstat_generic_config *user_cfg = new_conf;
555 u32 buf_size = user_cfg->buf_size;
556
557 if (!new_conf) {
558 dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
559 stat->subdev.name);
560 return -EINVAL;
561 }
562
563 mutex_lock(&stat->ioctl_lock);
564
565 dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
566 "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
567
568 ret = stat->ops->validate_params(stat, new_conf);
569 if (ret) {
570 mutex_unlock(&stat->ioctl_lock);
571 dev_dbg(stat->isp->dev, "%s: configuration values are "
572 "invalid.\n", stat->subdev.name);
573 return ret;
574 }
575
576 if (buf_size != user_cfg->buf_size)
577 dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
578 "request to 0x%08lx\n", stat->subdev.name,
579 (unsigned long)user_cfg->buf_size);
580
581 /*
582 * Hack: H3A modules may need a doubled buffer size to avoid access
583 * to a invalid memory address after a SBL overflow.
584 * The buffer size is always PAGE_ALIGNED.
585 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
586 * inserted at the end to data integrity check purpose.
587 * Hack 3: AF module writes one paxel data more than it should, so
588 * the buffer allocation must consider it to avoid invalid memory
589 * access.
590 * Hack 4: H3A need to allocate extra space for the recover state.
591 */
592 if (IS_H3A(stat)) {
593 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
594 if (IS_H3A_AF(stat))
595 /*
596 * Adding one extra paxel data size for each recover
597 * buffer + 2 regular ones.
598 */
599 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
600 if (stat->recover_priv) {
601 struct ispstat_generic_config *recover_cfg =
602 stat->recover_priv;
603 buf_size += recover_cfg->buf_size *
604 NUM_H3A_RECOVER_BUFS;
605 }
606 buf_size = PAGE_ALIGN(buf_size);
607 } else { /* Histogram */
608 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
609 }
610
611 ret = isp_stat_bufs_alloc(stat, buf_size);
612 if (ret) {
613 mutex_unlock(&stat->ioctl_lock);
614 return ret;
615 }
616
617 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
618 stat->ops->set_params(stat, new_conf);
619 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
620
621 /*
622 * Returning the right future config_counter for this setup, so
623 * userspace can *know* when it has been applied.
624 */
625 user_cfg->config_counter = stat->config_counter + stat->inc_config;
626
627 /* Module has a valid configuration. */
628 stat->configured = 1;
629 dev_dbg(stat->isp->dev, "%s: module has been successfully "
630 "configured.\n", stat->subdev.name);
631
632 mutex_unlock(&stat->ioctl_lock);
633
634 return 0;
635}
636
637/*
638 * isp_stat_buf_process - Process statistic buffers.
639 * @buf_state: points out if buffer is ready to be processed. It's necessary
640 * because histogram needs to copy the data from internal memory
641 * before be able to process the buffer.
642 */
643static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
644{
645 int ret = STAT_NO_BUF;
646
647 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
648 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
649 ret = isp_stat_buf_queue(stat);
650 isp_stat_buf_next(stat);
651 }
652
653 return ret;
654}
655
656int omap3isp_stat_pcr_busy(struct ispstat *stat)
657{
658 return stat->ops->busy(stat);
659}
660
661int omap3isp_stat_busy(struct ispstat *stat)
662{
663 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
664 (stat->state != ISPSTAT_DISABLED);
665}
666
667/*
668 * isp_stat_pcr_enable - Disables/Enables statistic engines.
669 * @pcr_enable: 0/1 - Disables/Enables the engine.
670 *
671 * Must be called from ISP driver when the module is idle and synchronized
672 * with CCDC.
673 */
674static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
675{
676 if ((stat->state != ISPSTAT_ENABLING &&
677 stat->state != ISPSTAT_ENABLED) && pcr_enable)
678 /* Userspace has disabled the module. Aborting. */
679 return;
680
681 stat->ops->enable(stat, pcr_enable);
682 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
683 stat->state = ISPSTAT_DISABLED;
684 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
685 stat->state = ISPSTAT_ENABLED;
686}
687
688void omap3isp_stat_suspend(struct ispstat *stat)
689{
690 unsigned long flags;
691
692 spin_lock_irqsave(&stat->isp->stat_lock, flags);
693
694 if (stat->state != ISPSTAT_DISABLED)
695 stat->ops->enable(stat, 0);
696 if (stat->state == ISPSTAT_ENABLED)
697 stat->state = ISPSTAT_SUSPENDED;
698
699 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
700}
701
702void omap3isp_stat_resume(struct ispstat *stat)
703{
704 /* Module will be re-enabled with its pipeline */
705 if (stat->state == ISPSTAT_SUSPENDED)
706 stat->state = ISPSTAT_ENABLING;
707}
708
709static void isp_stat_try_enable(struct ispstat *stat)
710{
711 unsigned long irqflags;
712
713 if (stat->priv == NULL)
714 /* driver wasn't initialised */
715 return;
716
717 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
718 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
719 stat->buf_alloc_size) {
720 /*
721 * Userspace's requested to enable the engine but it wasn't yet.
722 * Let's do that now.
723 */
724 stat->update = 1;
725 isp_stat_buf_next(stat);
726 stat->ops->setup_regs(stat, stat->priv);
727 isp_stat_buf_insert_magic(stat, stat->active_buf);
728
729 /*
730 * H3A module has some hw issues which forces the driver to
731 * ignore next buffers even if it was disabled in the meantime.
732 * On the other hand, Histogram shouldn't ignore buffers anymore
733 * if it's being enabled.
734 */
735 if (!IS_H3A(stat))
736 atomic_set(&stat->buf_err, 0);
737
738 isp_stat_pcr_enable(stat, 1);
739 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
740 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
741 stat->subdev.name);
742 } else {
743 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
744 }
745}
746
747void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
748{
749 isp_stat_try_enable(stat);
750}
751
752void omap3isp_stat_sbl_overflow(struct ispstat *stat)
753{
754 unsigned long irqflags;
755
756 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
757 /*
758 * Due to a H3A hw issue which prevents the next buffer to start from
759 * the correct memory address, 2 buffers must be ignored.
760 */
761 atomic_set(&stat->buf_err, 2);
762
763 /*
764 * If more than one SBL overflow happen in a row, H3A module may access
765 * invalid memory region.
766 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
767 * a soft configuration which helps to avoid consecutive overflows.
768 */
769 if (stat->recover_priv)
770 stat->sbl_ovl_recover = 1;
771 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
772}
773
774/*
775 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
776 * @enable: 0/1 - Disables/Enables the engine.
777 *
778 * Client should configure all the module registers before this.
779 * This function can be called from a userspace request.
780 */
781int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
782{
783 unsigned long irqflags;
784
785 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
786 stat->subdev.name, enable ? "enable" : "disable");
787
788 /* Prevent enabling while configuring */
789 mutex_lock(&stat->ioctl_lock);
790
791 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
792
793 if (!stat->configured && enable) {
794 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
795 mutex_unlock(&stat->ioctl_lock);
796 dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
797 "never been successfully configured so far.\n",
798 stat->subdev.name);
799 return -EINVAL;
800 }
801
802 if (enable) {
803 if (stat->state == ISPSTAT_DISABLING)
804 /* Previous disabling request wasn't done yet */
805 stat->state = ISPSTAT_ENABLED;
806 else if (stat->state == ISPSTAT_DISABLED)
807 /* Module is now being enabled */
808 stat->state = ISPSTAT_ENABLING;
809 } else {
810 if (stat->state == ISPSTAT_ENABLING) {
811 /* Previous enabling request wasn't done yet */
812 stat->state = ISPSTAT_DISABLED;
813 } else if (stat->state == ISPSTAT_ENABLED) {
814 /* Module is now being disabled */
815 stat->state = ISPSTAT_DISABLING;
816 isp_stat_buf_clear(stat);
817 }
818 }
819
820 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
821 mutex_unlock(&stat->ioctl_lock);
822
823 return 0;
824}
825
826int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
827{
828 struct ispstat *stat = v4l2_get_subdevdata(subdev);
829
830 if (enable) {
831 /*
832 * Only set enable PCR bit if the module was previously
Lad, Prabhakar25aeb412014-02-21 09:07:21 -0300833 * enabled through ioctl.
David Cohen68e342b2011-02-12 18:05:06 -0300834 */
835 isp_stat_try_enable(stat);
836 } else {
837 unsigned long flags;
838 /* Disable PCR bit and config enable field */
839 omap3isp_stat_enable(stat, 0);
840 spin_lock_irqsave(&stat->isp->stat_lock, flags);
841 stat->ops->enable(stat, 0);
842 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
843
844 /*
845 * If module isn't busy, a new interrupt may come or not to
846 * set the state to DISABLED. As Histogram needs to read its
847 * internal memory to clear it, let interrupt handler
848 * responsible of changing state to DISABLED. If the last
849 * interrupt is coming, it's still safe as the handler will
850 * ignore the second time when state is already set to DISABLED.
851 * It's necessary to synchronize Histogram with streamoff, once
852 * the module may be considered idle before last SDMA transfer
853 * starts if we return here.
854 */
855 if (!omap3isp_stat_pcr_busy(stat))
856 omap3isp_stat_isr(stat);
857
858 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
859 stat->subdev.name);
860 }
861
862 return 0;
863}
864
865/*
866 * __stat_isr - Interrupt handler for statistic drivers
867 */
868static void __stat_isr(struct ispstat *stat, int from_dma)
869{
870 int ret = STAT_BUF_DONE;
871 int buf_processing;
872 unsigned long irqflags;
873 struct isp_pipeline *pipe;
874
875 /*
876 * stat->buf_processing must be set before disable module. It's
877 * necessary to not inform too early the buffers aren't busy in case
878 * of SDMA is going to be used.
879 */
880 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
881 if (stat->state == ISPSTAT_DISABLED) {
882 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
883 return;
884 }
885 buf_processing = stat->buf_processing;
886 stat->buf_processing = 1;
887 stat->ops->enable(stat, 0);
888
889 if (buf_processing && !from_dma) {
890 if (stat->state == ISPSTAT_ENABLED) {
891 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
892 dev_err(stat->isp->dev,
893 "%s: interrupt occurred when module was still "
894 "processing a buffer.\n", stat->subdev.name);
895 ret = STAT_NO_BUF;
896 goto out;
897 } else {
898 /*
899 * Interrupt handler was called from streamoff when
900 * the module wasn't busy anymore to ensure it is being
901 * disabled after process last buffer. If such buffer
902 * processing has already started, no need to do
903 * anything else.
904 */
905 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
906 return;
907 }
908 }
909 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
910
911 /* If it's busy we can't process this buffer anymore */
912 if (!omap3isp_stat_pcr_busy(stat)) {
913 if (!from_dma && stat->ops->buf_process)
914 /* Module still need to copy data to buffer. */
915 ret = stat->ops->buf_process(stat);
916 if (ret == STAT_BUF_WAITING_DMA)
917 /* Buffer is not ready yet */
918 return;
919
920 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
921
922 /*
923 * Histogram needs to read its internal memory to clear it
924 * before be disabled. For that reason, common statistic layer
925 * can return only after call stat's buf_process() operator.
926 */
927 if (stat->state == ISPSTAT_DISABLING) {
928 stat->state = ISPSTAT_DISABLED;
929 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
930 stat->buf_processing = 0;
931 return;
932 }
933 pipe = to_isp_pipeline(&stat->subdev.entity);
934 stat->frame_number = atomic_read(&pipe->frame_number);
935
936 /*
937 * Before this point, 'ret' stores the buffer's status if it's
938 * ready to be processed. Afterwards, it holds the status if
939 * it was processed successfully.
940 */
941 ret = isp_stat_buf_process(stat, ret);
942
943 if (likely(!stat->sbl_ovl_recover)) {
944 stat->ops->setup_regs(stat, stat->priv);
945 } else {
946 /*
947 * Using recover config to increase the chance to have
948 * a good buffer processing and make the H3A module to
949 * go back to a valid state.
950 */
951 stat->update = 1;
952 stat->ops->setup_regs(stat, stat->recover_priv);
953 stat->sbl_ovl_recover = 0;
954
955 /*
956 * Set 'update' in case of the module needs to use
957 * regular configuration after next buffer.
958 */
959 stat->update = 1;
960 }
961
962 isp_stat_buf_insert_magic(stat, stat->active_buf);
963
964 /*
965 * Hack: H3A modules may access invalid memory address or send
966 * corrupted data to userspace if more than 1 SBL overflow
967 * happens in a row without re-writing its buffer's start memory
968 * address in the meantime. Such situation is avoided if the
969 * module is not immediately re-enabled when the ISR misses the
970 * timing to process the buffer and to setup the registers.
971 * Because of that, pcr_enable(1) was moved to inside this 'if'
972 * block. But the next interruption will still happen as during
973 * pcr_enable(0) the module was busy.
974 */
975 isp_stat_pcr_enable(stat, 1);
976 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
977 } else {
978 /*
979 * If a SBL overflow occurs and the H3A driver misses the timing
980 * to process the buffer, stat->buf_err is set and won't be
981 * cleared now. So the next buffer will be correctly ignored.
982 * It's necessary due to a hw issue which makes the next H3A
983 * buffer to start from the memory address where the previous
984 * one stopped, instead of start where it was configured to.
985 * Do not "stat->buf_err = 0" here.
986 */
987
988 if (stat->ops->buf_process)
989 /*
990 * Driver may need to erase current data prior to
991 * process a new buffer. If it misses the timing, the
992 * next buffer might be wrong. So should be ignored.
993 * It happens only for Histogram.
994 */
995 atomic_set(&stat->buf_err, 1);
996
997 ret = STAT_NO_BUF;
998 dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
999 "device is busy.\n", stat->subdev.name);
1000 }
1001
1002out:
1003 stat->buf_processing = 0;
1004 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
1005}
1006
1007void omap3isp_stat_isr(struct ispstat *stat)
1008{
1009 __stat_isr(stat, 0);
1010}
1011
1012void omap3isp_stat_dma_isr(struct ispstat *stat)
1013{
1014 __stat_isr(stat, 1);
1015}
1016
David Cohen68e342b2011-02-12 18:05:06 -03001017int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1018 struct v4l2_fh *fh,
Laurent Pinchart55c85042012-10-12 11:20:10 -03001019 struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001020{
1021 struct ispstat *stat = v4l2_get_subdevdata(subdev);
1022
1023 if (sub->type != stat->event_type)
1024 return -EINVAL;
1025
Hans de Goedec53c2542012-04-08 12:59:46 -03001026 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
David Cohen68e342b2011-02-12 18:05:06 -03001027}
1028
1029int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1030 struct v4l2_fh *fh,
Laurent Pinchart55c85042012-10-12 11:20:10 -03001031 struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001032{
1033 return v4l2_event_unsubscribe(fh, sub);
1034}
1035
1036void omap3isp_stat_unregister_entities(struct ispstat *stat)
1037{
David Cohen68e342b2011-02-12 18:05:06 -03001038 v4l2_device_unregister_subdev(&stat->subdev);
1039}
1040
1041int omap3isp_stat_register_entities(struct ispstat *stat,
1042 struct v4l2_device *vdev)
1043{
1044 return v4l2_device_register_subdev(vdev, &stat->subdev);
1045}
1046
Laurent Pinchart39099d02011-09-22 16:59:26 -03001047static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1048 const struct v4l2_subdev_ops *sd_ops)
1049{
1050 struct v4l2_subdev *subdev = &stat->subdev;
1051 struct media_entity *me = &subdev->entity;
1052
1053 v4l2_subdev_init(subdev, sd_ops);
1054 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1055 subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
1056 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1057 v4l2_set_subdevdata(subdev, stat);
1058
Sakari Ailus8dad9362013-10-02 20:17:52 -03001059 stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
Laurent Pinchart39099d02011-09-22 16:59:26 -03001060 me->ops = NULL;
1061
1062 return media_entity_init(me, 1, &stat->pad, 0);
1063}
1064
David Cohen68e342b2011-02-12 18:05:06 -03001065int omap3isp_stat_init(struct ispstat *stat, const char *name,
1066 const struct v4l2_subdev_ops *sd_ops)
1067{
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001068 int ret;
1069
David Cohen68e342b2011-02-12 18:05:06 -03001070 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1071 if (!stat->buf)
1072 return -ENOMEM;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001073
David Cohen68e342b2011-02-12 18:05:06 -03001074 isp_stat_buf_clear(stat);
1075 mutex_init(&stat->ioctl_lock);
1076 atomic_set(&stat->buf_err, 0);
1077
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001078 ret = isp_stat_init_entities(stat, name, sd_ops);
1079 if (ret < 0) {
1080 mutex_destroy(&stat->ioctl_lock);
1081 kfree(stat->buf);
1082 }
1083
1084 return ret;
David Cohen68e342b2011-02-12 18:05:06 -03001085}
1086
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001087void omap3isp_stat_cleanup(struct ispstat *stat)
David Cohen68e342b2011-02-12 18:05:06 -03001088{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001089 media_entity_cleanup(&stat->subdev.entity);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001090 mutex_destroy(&stat->ioctl_lock);
David Cohen68e342b2011-02-12 18:05:06 -03001091 isp_stat_bufs_free(stat);
1092 kfree(stat->buf);
1093}