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