ispstat.c 30 KB

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