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