ispstat.c 29 KB

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