ispstat.c 29 KB

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