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