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, "%s: beginning magic check does not "
 117                                        "match.\n", stat->subdev.name);
 118                return ret;
 119        }
 120
 121        /* Checking magic numbers at the end. They must be still here. */
 122        for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
 123             w < end; w++) {
 124                if (unlikely(*w != MAGIC_NUM)) {
 125                        dev_dbg(stat->isp->dev, "%s: ending magic check does "
 126                                "not match.\n", 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
 137static 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
 158static 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
 168static 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
 178static 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
 186static 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
 221static inline struct ispstat_buffer *
 222isp_stat_buf_find_oldest(struct ispstat *stat)
 223{
 224        return __isp_stat_buf_find(stat, 0);
 225}
 226
 227static inline struct ispstat_buffer *
 228isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
 229{
 230        return __isp_stat_buf_find(stat, 1);
 231}
 232
 233static int isp_stat_buf_queue(struct ispstat *stat)
 234{
 235        if (!stat->active_buf)
 236                return STAT_NO_BUF;
 237
 238        v4l2_get_timestamp(&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. */
 255static 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, "%s: new buffer requested without "
 260                                        "queuing active one.\n",
 261                                        stat->subdev.name);
 262        else
 263                stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
 264}
 265
 266static 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. */
 277static 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, "%s: current buffer has "
 296                                "corrupted data\n.", stat->subdev.name);
 297                        /* Mark empty because it doesn't have valid data. */
 298                        buf->empty = 1;
 299                } else {
 300                        /* Buffer isn't corrupted. */
 301                        break;
 302                }
 303        }
 304
 305        stat->locked_buf = buf;
 306
 307        spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 308
 309        if (buf->buf_size > data->buf_size) {
 310                dev_warn(stat->isp->dev, "%s: userspace's buffer size is "
 311                                         "not enough.\n", stat->subdev.name);
 312                isp_stat_buf_release(stat);
 313                return ERR_PTR(-EINVAL);
 314        }
 315
 316        isp_stat_buf_sync_for_cpu(stat, buf);
 317
 318        rval = copy_to_user(data->buf,
 319                            buf->virt_addr,
 320                            buf->buf_size);
 321
 322        if (rval) {
 323                dev_info(stat->isp->dev,
 324                         "%s: failed copying %d bytes of stat data\n",
 325                         stat->subdev.name, rval);
 326                buf = ERR_PTR(-EFAULT);
 327                isp_stat_buf_release(stat);
 328        }
 329
 330        return buf;
 331}
 332
 333static void isp_stat_bufs_free(struct ispstat *stat)
 334{
 335        struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
 336                           ? NULL : stat->isp->dev;
 337        unsigned int i;
 338
 339        for (i = 0; i < STAT_MAX_BUFS; i++) {
 340                struct ispstat_buffer *buf = &stat->buf[i];
 341
 342                if (!buf->virt_addr)
 343                        continue;
 344
 345                sg_free_table(&buf->sgt);
 346
 347                dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
 348                                  buf->dma_addr);
 349
 350                buf->dma_addr = 0;
 351                buf->virt_addr = NULL;
 352                buf->empty = 1;
 353        }
 354
 355        dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
 356                stat->subdev.name);
 357
 358        stat->buf_alloc_size = 0;
 359        stat->active_buf = NULL;
 360}
 361
 362static int isp_stat_bufs_alloc_one(struct device *dev,
 363                                   struct ispstat_buffer *buf,
 364                                   unsigned int size)
 365{
 366        int ret;
 367
 368        buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
 369                                            GFP_KERNEL | GFP_DMA);
 370        if (!buf->virt_addr)
 371                return -ENOMEM;
 372
 373        ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
 374                              size);
 375        if (ret < 0) {
 376                dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
 377                buf->virt_addr = NULL;
 378                buf->dma_addr = 0;
 379                return ret;
 380        }
 381
 382        return 0;
 383}
 384
 385/*
 386 * The device passed to the DMA API depends on whether the statistics block uses
 387 * ISP DMA, external DMA or PIO to transfer data.
 388 *
 389 * The first case (for the AEWB and AF engines) passes the ISP device, resulting
 390 * in the DMA buffers being mapped through the ISP IOMMU.
 391 *
 392 * The second case (for the histogram engine) should pass the DMA engine device.
 393 * As that device isn't accessible through the OMAP DMA engine API the driver
 394 * passes NULL instead, resulting in the buffers being mapped directly as
 395 * physical pages.
 396 *
 397 * The third case (for the histogram engine) doesn't require any mapping. The
 398 * buffers could be allocated with kmalloc/vmalloc, but we still use
 399 * dma_alloc_coherent() for consistency purpose.
 400 */
 401static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
 402{
 403        struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
 404                           ? NULL : stat->isp->dev;
 405        unsigned long flags;
 406        unsigned int i;
 407
 408        spin_lock_irqsave(&stat->isp->stat_lock, flags);
 409
 410        BUG_ON(stat->locked_buf != NULL);
 411
 412        /* Are the old buffers big enough? */
 413        if (stat->buf_alloc_size >= size) {
 414                spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 415                return 0;
 416        }
 417
 418        if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
 419                dev_info(stat->isp->dev,
 420                         "%s: trying to allocate memory when busy\n",
 421                         stat->subdev.name);
 422                spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 423                return -EBUSY;
 424        }
 425
 426        spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 427
 428        isp_stat_bufs_free(stat);
 429
 430        stat->buf_alloc_size = size;
 431
 432        for (i = 0; i < STAT_MAX_BUFS; i++) {
 433                struct ispstat_buffer *buf = &stat->buf[i];
 434                int ret;
 435
 436                ret = isp_stat_bufs_alloc_one(dev, buf, size);
 437                if (ret < 0) {
 438                        dev_err(stat->isp->dev,
 439                                "%s: Failed to allocate DMA buffer %u\n",
 440                                stat->subdev.name, i);
 441                        isp_stat_bufs_free(stat);
 442                        return ret;
 443                }
 444
 445                buf->empty = 1;
 446
 447                dev_dbg(stat->isp->dev,
 448                        "%s: buffer[%u] allocated. dma=0x%08lx virt=0x%08lx",
 449                        stat->subdev.name, i,
 450                        (unsigned long)buf->dma_addr,
 451                        (unsigned long)buf->virt_addr);
 452        }
 453
 454        return 0;
 455}
 456
 457static 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 */
 481int 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 = buf->ts;
 500        data->config_counter = buf->config_counter;
 501        data->frame_number = buf->frame_number;
 502        data->buf_size = buf->buf_size;
 503
 504        buf->empty = 1;
 505        isp_stat_buf_release(stat);
 506        mutex_unlock(&stat->ioctl_lock);
 507
 508        return 0;
 509}
 510
 511/*
 512 * omap3isp_stat_config - Receives new statistic engine configuration.
 513 * @new_conf: Pointer to config structure.
 514 *
 515 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
 516 * was unable to allocate memory for the buffer, or other errors if parameters
 517 * are invalid.
 518 */
 519int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
 520{
 521        int ret;
 522        unsigned long irqflags;
 523        struct ispstat_generic_config *user_cfg = new_conf;
 524        u32 buf_size = user_cfg->buf_size;
 525
 526        if (!new_conf) {
 527                dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
 528                        stat->subdev.name);
 529                return -EINVAL;
 530        }
 531
 532        mutex_lock(&stat->ioctl_lock);
 533
 534        dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
 535                "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
 536
 537        ret = stat->ops->validate_params(stat, new_conf);
 538        if (ret) {
 539                mutex_unlock(&stat->ioctl_lock);
 540                dev_dbg(stat->isp->dev, "%s: configuration values are "
 541                                        "invalid.\n", stat->subdev.name);
 542                return ret;
 543        }
 544
 545        if (buf_size != user_cfg->buf_size)
 546                dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
 547                        "request to 0x%08lx\n", stat->subdev.name,
 548                        (unsigned long)user_cfg->buf_size);
 549
 550        /*
 551         * Hack: H3A modules may need a doubled buffer size to avoid access
 552         * to a invalid memory address after a SBL overflow.
 553         * The buffer size is always PAGE_ALIGNED.
 554         * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
 555         * inserted at the end to data integrity check purpose.
 556         * Hack 3: AF module writes one paxel data more than it should, so
 557         * the buffer allocation must consider it to avoid invalid memory
 558         * access.
 559         * Hack 4: H3A need to allocate extra space for the recover state.
 560         */
 561        if (IS_H3A(stat)) {
 562                buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
 563                if (IS_H3A_AF(stat))
 564                        /*
 565                         * Adding one extra paxel data size for each recover
 566                         * buffer + 2 regular ones.
 567                         */
 568                        buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
 569                if (stat->recover_priv) {
 570                        struct ispstat_generic_config *recover_cfg =
 571                                stat->recover_priv;
 572                        buf_size += recover_cfg->buf_size *
 573                                    NUM_H3A_RECOVER_BUFS;
 574                }
 575                buf_size = PAGE_ALIGN(buf_size);
 576        } else { /* Histogram */
 577                buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
 578        }
 579
 580        ret = isp_stat_bufs_alloc(stat, buf_size);
 581        if (ret) {
 582                mutex_unlock(&stat->ioctl_lock);
 583                return ret;
 584        }
 585
 586        spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 587        stat->ops->set_params(stat, new_conf);
 588        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 589
 590        /*
 591         * Returning the right future config_counter for this setup, so
 592         * userspace can *know* when it has been applied.
 593         */
 594        user_cfg->config_counter = stat->config_counter + stat->inc_config;
 595
 596        /* Module has a valid configuration. */
 597        stat->configured = 1;
 598        dev_dbg(stat->isp->dev, "%s: module has been successfully "
 599                "configured.\n", stat->subdev.name);
 600
 601        mutex_unlock(&stat->ioctl_lock);
 602
 603        return 0;
 604}
 605
 606/*
 607 * isp_stat_buf_process - Process statistic buffers.
 608 * @buf_state: points out if buffer is ready to be processed. It's necessary
 609 *             because histogram needs to copy the data from internal memory
 610 *             before be able to process the buffer.
 611 */
 612static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
 613{
 614        int ret = STAT_NO_BUF;
 615
 616        if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
 617            buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
 618                ret = isp_stat_buf_queue(stat);
 619                isp_stat_buf_next(stat);
 620        }
 621
 622        return ret;
 623}
 624
 625int omap3isp_stat_pcr_busy(struct ispstat *stat)
 626{
 627        return stat->ops->busy(stat);
 628}
 629
 630int omap3isp_stat_busy(struct ispstat *stat)
 631{
 632        return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
 633                (stat->state != ISPSTAT_DISABLED);
 634}
 635
 636/*
 637 * isp_stat_pcr_enable - Disables/Enables statistic engines.
 638 * @pcr_enable: 0/1 - Disables/Enables the engine.
 639 *
 640 * Must be called from ISP driver when the module is idle and synchronized
 641 * with CCDC.
 642 */
 643static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
 644{
 645        if ((stat->state != ISPSTAT_ENABLING &&
 646             stat->state != ISPSTAT_ENABLED) && pcr_enable)
 647                /* Userspace has disabled the module. Aborting. */
 648                return;
 649
 650        stat->ops->enable(stat, pcr_enable);
 651        if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
 652                stat->state = ISPSTAT_DISABLED;
 653        else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
 654                stat->state = ISPSTAT_ENABLED;
 655}
 656
 657void omap3isp_stat_suspend(struct ispstat *stat)
 658{
 659        unsigned long flags;
 660
 661        spin_lock_irqsave(&stat->isp->stat_lock, flags);
 662
 663        if (stat->state != ISPSTAT_DISABLED)
 664                stat->ops->enable(stat, 0);
 665        if (stat->state == ISPSTAT_ENABLED)
 666                stat->state = ISPSTAT_SUSPENDED;
 667
 668        spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 669}
 670
 671void omap3isp_stat_resume(struct ispstat *stat)
 672{
 673        /* Module will be re-enabled with its pipeline */
 674        if (stat->state == ISPSTAT_SUSPENDED)
 675                stat->state = ISPSTAT_ENABLING;
 676}
 677
 678static void isp_stat_try_enable(struct ispstat *stat)
 679{
 680        unsigned long irqflags;
 681
 682        if (stat->priv == NULL)
 683                /* driver wasn't initialised */
 684                return;
 685
 686        spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 687        if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
 688            stat->buf_alloc_size) {
 689                /*
 690                 * Userspace's requested to enable the engine but it wasn't yet.
 691                 * Let's do that now.
 692                 */
 693                stat->update = 1;
 694                isp_stat_buf_next(stat);
 695                stat->ops->setup_regs(stat, stat->priv);
 696                isp_stat_buf_insert_magic(stat, stat->active_buf);
 697
 698                /*
 699                 * H3A module has some hw issues which forces the driver to
 700                 * ignore next buffers even if it was disabled in the meantime.
 701                 * On the other hand, Histogram shouldn't ignore buffers anymore
 702                 * if it's being enabled.
 703                 */
 704                if (!IS_H3A(stat))
 705                        atomic_set(&stat->buf_err, 0);
 706
 707                isp_stat_pcr_enable(stat, 1);
 708                spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 709                dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
 710                        stat->subdev.name);
 711        } else {
 712                spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 713        }
 714}
 715
 716void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
 717{
 718        isp_stat_try_enable(stat);
 719}
 720
 721void omap3isp_stat_sbl_overflow(struct ispstat *stat)
 722{
 723        unsigned long irqflags;
 724
 725        spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 726        /*
 727         * Due to a H3A hw issue which prevents the next buffer to start from
 728         * the correct memory address, 2 buffers must be ignored.
 729         */
 730        atomic_set(&stat->buf_err, 2);
 731
 732        /*
 733         * If more than one SBL overflow happen in a row, H3A module may access
 734         * invalid memory region.
 735         * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
 736         * a soft configuration which helps to avoid consecutive overflows.
 737         */
 738        if (stat->recover_priv)
 739                stat->sbl_ovl_recover = 1;
 740        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 741}
 742
 743/*
 744 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
 745 * @enable: 0/1 - Disables/Enables the engine.
 746 *
 747 * Client should configure all the module registers before this.
 748 * This function can be called from a userspace request.
 749 */
 750int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
 751{
 752        unsigned long irqflags;
 753
 754        dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
 755                stat->subdev.name, enable ? "enable" : "disable");
 756
 757        /* Prevent enabling while configuring */
 758        mutex_lock(&stat->ioctl_lock);
 759
 760        spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 761
 762        if (!stat->configured && enable) {
 763                spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 764                mutex_unlock(&stat->ioctl_lock);
 765                dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
 766                        "never been successfully configured so far.\n",
 767                        stat->subdev.name);
 768                return -EINVAL;
 769        }
 770
 771        if (enable) {
 772                if (stat->state == ISPSTAT_DISABLING)
 773                        /* Previous disabling request wasn't done yet */
 774                        stat->state = ISPSTAT_ENABLED;
 775                else if (stat->state == ISPSTAT_DISABLED)
 776                        /* Module is now being enabled */
 777                        stat->state = ISPSTAT_ENABLING;
 778        } else {
 779                if (stat->state == ISPSTAT_ENABLING) {
 780                        /* Previous enabling request wasn't done yet */
 781                        stat->state = ISPSTAT_DISABLED;
 782                } else if (stat->state == ISPSTAT_ENABLED) {
 783                        /* Module is now being disabled */
 784                        stat->state = ISPSTAT_DISABLING;
 785                        isp_stat_buf_clear(stat);
 786                }
 787        }
 788
 789        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 790        mutex_unlock(&stat->ioctl_lock);
 791
 792        return 0;
 793}
 794
 795int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
 796{
 797        struct ispstat *stat = v4l2_get_subdevdata(subdev);
 798
 799        if (enable) {
 800                /*
 801                 * Only set enable PCR bit if the module was previously
 802                 * enabled through ioctl.
 803                 */
 804                isp_stat_try_enable(stat);
 805        } else {
 806                unsigned long flags;
 807                /* Disable PCR bit and config enable field */
 808                omap3isp_stat_enable(stat, 0);
 809                spin_lock_irqsave(&stat->isp->stat_lock, flags);
 810                stat->ops->enable(stat, 0);
 811                spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 812
 813                /*
 814                 * If module isn't busy, a new interrupt may come or not to
 815                 * set the state to DISABLED. As Histogram needs to read its
 816                 * internal memory to clear it, let interrupt handler
 817                 * responsible of changing state to DISABLED. If the last
 818                 * interrupt is coming, it's still safe as the handler will
 819                 * ignore the second time when state is already set to DISABLED.
 820                 * It's necessary to synchronize Histogram with streamoff, once
 821                 * the module may be considered idle before last SDMA transfer
 822                 * starts if we return here.
 823                 */
 824                if (!omap3isp_stat_pcr_busy(stat))
 825                        omap3isp_stat_isr(stat);
 826
 827                dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
 828                        stat->subdev.name);
 829        }
 830
 831        return 0;
 832}
 833
 834/*
 835 * __stat_isr - Interrupt handler for statistic drivers
 836 */
 837static void __stat_isr(struct ispstat *stat, int from_dma)
 838{
 839        int ret = STAT_BUF_DONE;
 840        int buf_processing;
 841        unsigned long irqflags;
 842        struct isp_pipeline *pipe;
 843
 844        /*
 845         * stat->buf_processing must be set before disable module. It's
 846         * necessary to not inform too early the buffers aren't busy in case
 847         * of SDMA is going to be used.
 848         */
 849        spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 850        if (stat->state == ISPSTAT_DISABLED) {
 851                spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 852                return;
 853        }
 854        buf_processing = stat->buf_processing;
 855        stat->buf_processing = 1;
 856        stat->ops->enable(stat, 0);
 857
 858        if (buf_processing && !from_dma) {
 859                if (stat->state == ISPSTAT_ENABLED) {
 860                        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 861                        dev_err(stat->isp->dev,
 862                                "%s: interrupt occurred when module was still "
 863                                "processing a buffer.\n", stat->subdev.name);
 864                        ret = STAT_NO_BUF;
 865                        goto out;
 866                } else {
 867                        /*
 868                         * Interrupt handler was called from streamoff when
 869                         * the module wasn't busy anymore to ensure it is being
 870                         * disabled after process last buffer. If such buffer
 871                         * processing has already started, no need to do
 872                         * anything else.
 873                         */
 874                        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 875                        return;
 876                }
 877        }
 878        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 879
 880        /* If it's busy we can't process this buffer anymore */
 881        if (!omap3isp_stat_pcr_busy(stat)) {
 882                if (!from_dma && stat->ops->buf_process)
 883                        /* Module still need to copy data to buffer. */
 884                        ret = stat->ops->buf_process(stat);
 885                if (ret == STAT_BUF_WAITING_DMA)
 886                        /* Buffer is not ready yet */
 887                        return;
 888
 889                spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 890
 891                /*
 892                 * Histogram needs to read its internal memory to clear it
 893                 * before be disabled. For that reason, common statistic layer
 894                 * can return only after call stat's buf_process() operator.
 895                 */
 896                if (stat->state == ISPSTAT_DISABLING) {
 897                        stat->state = ISPSTAT_DISABLED;
 898                        spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 899                        stat->buf_processing = 0;
 900                        return;
 901                }
 902                pipe = to_isp_pipeline(&stat->subdev.entity);
 903                stat->frame_number = atomic_read(&pipe->frame_number);
 904
 905                /*
 906                 * Before this point, 'ret' stores the buffer's status if it's
 907                 * ready to be processed. Afterwards, it holds the status if
 908                 * it was processed successfully.
 909                 */
 910                ret = isp_stat_buf_process(stat, ret);
 911
 912                if (likely(!stat->sbl_ovl_recover)) {
 913                        stat->ops->setup_regs(stat, stat->priv);
 914                } else {
 915                        /*
 916                         * Using recover config to increase the chance to have
 917                         * a good buffer processing and make the H3A module to
 918                         * go back to a valid state.
 919                         */
 920                        stat->update = 1;
 921                        stat->ops->setup_regs(stat, stat->recover_priv);
 922                        stat->sbl_ovl_recover = 0;
 923
 924                        /*
 925                         * Set 'update' in case of the module needs to use
 926                         * regular configuration after next buffer.
 927                         */
 928                        stat->update = 1;
 929                }
 930
 931                isp_stat_buf_insert_magic(stat, stat->active_buf);
 932
 933                /*
 934                 * Hack: H3A modules may access invalid memory address or send
 935                 * corrupted data to userspace if more than 1 SBL overflow
 936                 * happens in a row without re-writing its buffer's start memory
 937                 * address in the meantime. Such situation is avoided if the
 938                 * module is not immediately re-enabled when the ISR misses the
 939                 * timing to process the buffer and to setup the registers.
 940                 * Because of that, pcr_enable(1) was moved to inside this 'if'
 941                 * block. But the next interruption will still happen as during
 942                 * pcr_enable(0) the module was busy.
 943                 */
 944                isp_stat_pcr_enable(stat, 1);
 945                spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 946        } else {
 947                /*
 948                 * If a SBL overflow occurs and the H3A driver misses the timing
 949                 * to process the buffer, stat->buf_err is set and won't be
 950                 * cleared now. So the next buffer will be correctly ignored.
 951                 * It's necessary due to a hw issue which makes the next H3A
 952                 * buffer to start from the memory address where the previous
 953                 * one stopped, instead of start where it was configured to.
 954                 * Do not "stat->buf_err = 0" here.
 955                 */
 956
 957                if (stat->ops->buf_process)
 958                        /*
 959                         * Driver may need to erase current data prior to
 960                         * process a new buffer. If it misses the timing, the
 961                         * next buffer might be wrong. So should be ignored.
 962                         * It happens only for Histogram.
 963                         */
 964                        atomic_set(&stat->buf_err, 1);
 965
 966                ret = STAT_NO_BUF;
 967                dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
 968                                        "device is busy.\n", stat->subdev.name);
 969        }
 970
 971out:
 972        stat->buf_processing = 0;
 973        isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
 974}
 975
 976void omap3isp_stat_isr(struct ispstat *stat)
 977{
 978        __stat_isr(stat, 0);
 979}
 980
 981void omap3isp_stat_dma_isr(struct ispstat *stat)
 982{
 983        __stat_isr(stat, 1);
 984}
 985
 986int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
 987                                  struct v4l2_fh *fh,
 988                                  struct v4l2_event_subscription *sub)
 989{
 990        struct ispstat *stat = v4l2_get_subdevdata(subdev);
 991
 992        if (sub->type != stat->event_type)
 993                return -EINVAL;
 994
 995        return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
 996}
 997
 998int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
 999                                    struct v4l2_fh *fh,
1000                                    struct v4l2_event_subscription *sub)
1001{
1002        return v4l2_event_unsubscribe(fh, sub);
1003}
1004
1005void omap3isp_stat_unregister_entities(struct ispstat *stat)
1006{
1007        v4l2_device_unregister_subdev(&stat->subdev);
1008}
1009
1010int omap3isp_stat_register_entities(struct ispstat *stat,
1011                                    struct v4l2_device *vdev)
1012{
1013        return v4l2_device_register_subdev(vdev, &stat->subdev);
1014}
1015
1016static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1017                                  const struct v4l2_subdev_ops *sd_ops)
1018{
1019        struct v4l2_subdev *subdev = &stat->subdev;
1020        struct media_entity *me = &subdev->entity;
1021
1022        v4l2_subdev_init(subdev, sd_ops);
1023        snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1024        subdev->grp_id = 1 << 16;       /* group ID for isp subdevs */
1025        subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1026        v4l2_set_subdevdata(subdev, stat);
1027
1028        stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1029        me->ops = NULL;
1030
1031        return media_entity_pads_init(me, 1, &stat->pad);
1032}
1033
1034int omap3isp_stat_init(struct ispstat *stat, const char *name,
1035                       const struct v4l2_subdev_ops *sd_ops)
1036{
1037        int ret;
1038
1039        stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1040        if (!stat->buf)
1041                return -ENOMEM;
1042
1043        isp_stat_buf_clear(stat);
1044        mutex_init(&stat->ioctl_lock);
1045        atomic_set(&stat->buf_err, 0);
1046
1047        ret = isp_stat_init_entities(stat, name, sd_ops);
1048        if (ret < 0) {
1049                mutex_destroy(&stat->ioctl_lock);
1050                kfree(stat->buf);
1051        }
1052
1053        return ret;
1054}
1055
1056void omap3isp_stat_cleanup(struct ispstat *stat)
1057{
1058        media_entity_cleanup(&stat->subdev.entity);
1059        mutex_destroy(&stat->ioctl_lock);
1060        isp_stat_bufs_free(stat);
1061        kfree(stat->buf);
1062}
1063