ispstat.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 <[email protected]>
  10. * Laurent Pinchart <[email protected]>
  11. * Sakari Ailus <[email protected]>
  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. stat->subdev.dev = vdev->mdev->dev;
  856. return v4l2_device_register_subdev(vdev, &stat->subdev);
  857. }
  858. static int isp_stat_init_entities(struct ispstat *stat, const char *name,
  859. const struct v4l2_subdev_ops *sd_ops)
  860. {
  861. struct v4l2_subdev *subdev = &stat->subdev;
  862. struct media_entity *me = &subdev->entity;
  863. v4l2_subdev_init(subdev, sd_ops);
  864. snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
  865. subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
  866. subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
  867. v4l2_set_subdevdata(subdev, stat);
  868. stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
  869. me->ops = NULL;
  870. return media_entity_pads_init(me, 1, &stat->pad);
  871. }
  872. int omap3isp_stat_init(struct ispstat *stat, const char *name,
  873. const struct v4l2_subdev_ops *sd_ops)
  874. {
  875. int ret;
  876. stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
  877. if (!stat->buf)
  878. return -ENOMEM;
  879. isp_stat_buf_clear(stat);
  880. mutex_init(&stat->ioctl_lock);
  881. atomic_set(&stat->buf_err, 0);
  882. ret = isp_stat_init_entities(stat, name, sd_ops);
  883. if (ret < 0) {
  884. mutex_destroy(&stat->ioctl_lock);
  885. kfree(stat->buf);
  886. }
  887. return ret;
  888. }
  889. void omap3isp_stat_cleanup(struct ispstat *stat)
  890. {
  891. media_entity_cleanup(&stat->subdev.entity);
  892. mutex_destroy(&stat->ioctl_lock);
  893. isp_stat_bufs_free(stat);
  894. kfree(stat->buf);
  895. }