ispstat.c 30 KB

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