fpga-mgr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FPGA Manager Core
  4. *
  5. * Copyright (C) 2013-2015 Altera Corporation
  6. * Copyright (C) 2017 Intel Corporation
  7. *
  8. * With code from the mailing list:
  9. * Copyright (C) 2013 Xilinx, Inc.
  10. */
  11. #include <linux/firmware.h>
  12. #include <linux/fpga/fpga-mgr.h>
  13. #include <linux/idr.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/highmem.h>
  20. static DEFINE_IDA(fpga_mgr_ida);
  21. static const struct class fpga_mgr_class;
  22. struct fpga_mgr_devres {
  23. struct fpga_manager *mgr;
  24. };
  25. static inline void fpga_mgr_fpga_remove(struct fpga_manager *mgr)
  26. {
  27. if (mgr->mops->fpga_remove)
  28. mgr->mops->fpga_remove(mgr);
  29. }
  30. static inline enum fpga_mgr_states fpga_mgr_state(struct fpga_manager *mgr)
  31. {
  32. if (mgr->mops->state)
  33. return mgr->mops->state(mgr);
  34. return FPGA_MGR_STATE_UNKNOWN;
  35. }
  36. static inline u64 fpga_mgr_status(struct fpga_manager *mgr)
  37. {
  38. if (mgr->mops->status)
  39. return mgr->mops->status(mgr);
  40. return 0;
  41. }
  42. static inline int fpga_mgr_write(struct fpga_manager *mgr, const char *buf, size_t count)
  43. {
  44. if (mgr->mops->write)
  45. return mgr->mops->write(mgr, buf, count);
  46. return -EOPNOTSUPP;
  47. }
  48. /*
  49. * After all the FPGA image has been written, do the device specific steps to
  50. * finish and set the FPGA into operating mode.
  51. */
  52. static inline int fpga_mgr_write_complete(struct fpga_manager *mgr,
  53. struct fpga_image_info *info)
  54. {
  55. int ret = 0;
  56. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
  57. if (mgr->mops->write_complete)
  58. ret = mgr->mops->write_complete(mgr, info);
  59. if (ret) {
  60. dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
  61. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
  62. return ret;
  63. }
  64. mgr->state = FPGA_MGR_STATE_OPERATING;
  65. return 0;
  66. }
  67. static inline int fpga_mgr_parse_header(struct fpga_manager *mgr,
  68. struct fpga_image_info *info,
  69. const char *buf, size_t count)
  70. {
  71. if (mgr->mops->parse_header)
  72. return mgr->mops->parse_header(mgr, info, buf, count);
  73. return 0;
  74. }
  75. static inline int fpga_mgr_write_init(struct fpga_manager *mgr,
  76. struct fpga_image_info *info,
  77. const char *buf, size_t count)
  78. {
  79. if (mgr->mops->write_init)
  80. return mgr->mops->write_init(mgr, info, buf, count);
  81. return 0;
  82. }
  83. static inline int fpga_mgr_write_sg(struct fpga_manager *mgr,
  84. struct sg_table *sgt)
  85. {
  86. if (mgr->mops->write_sg)
  87. return mgr->mops->write_sg(mgr, sgt);
  88. return -EOPNOTSUPP;
  89. }
  90. /**
  91. * fpga_image_info_alloc - Allocate an FPGA image info struct
  92. * @dev: owning device
  93. *
  94. * Return: struct fpga_image_info or NULL
  95. */
  96. struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
  97. {
  98. struct fpga_image_info *info;
  99. get_device(dev);
  100. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  101. if (!info) {
  102. put_device(dev);
  103. return NULL;
  104. }
  105. info->dev = dev;
  106. return info;
  107. }
  108. EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
  109. /**
  110. * fpga_image_info_free - Free an FPGA image info struct
  111. * @info: FPGA image info struct to free
  112. */
  113. void fpga_image_info_free(struct fpga_image_info *info)
  114. {
  115. struct device *dev;
  116. if (!info)
  117. return;
  118. dev = info->dev;
  119. if (info->firmware_name)
  120. devm_kfree(dev, info->firmware_name);
  121. devm_kfree(dev, info);
  122. put_device(dev);
  123. }
  124. EXPORT_SYMBOL_GPL(fpga_image_info_free);
  125. /*
  126. * Call the low level driver's parse_header function with entire FPGA image
  127. * buffer on the input. This will set info->header_size and info->data_size.
  128. */
  129. static int fpga_mgr_parse_header_mapped(struct fpga_manager *mgr,
  130. struct fpga_image_info *info,
  131. const char *buf, size_t count)
  132. {
  133. int ret;
  134. mgr->state = FPGA_MGR_STATE_PARSE_HEADER;
  135. ret = fpga_mgr_parse_header(mgr, info, buf, count);
  136. if (info->header_size + info->data_size > count) {
  137. dev_err(&mgr->dev, "Bitstream data outruns FPGA image\n");
  138. ret = -EINVAL;
  139. }
  140. if (ret) {
  141. dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
  142. mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
  143. }
  144. return ret;
  145. }
  146. /*
  147. * Call the low level driver's parse_header function with first fragment of
  148. * scattered FPGA image on the input. If header fits first fragment,
  149. * parse_header will set info->header_size and info->data_size. If it is not,
  150. * parse_header will set desired size to info->header_size and -EAGAIN will be
  151. * returned.
  152. */
  153. static int fpga_mgr_parse_header_sg_first(struct fpga_manager *mgr,
  154. struct fpga_image_info *info,
  155. struct sg_table *sgt)
  156. {
  157. struct sg_mapping_iter miter;
  158. int ret;
  159. mgr->state = FPGA_MGR_STATE_PARSE_HEADER;
  160. sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
  161. if (sg_miter_next(&miter) &&
  162. miter.length >= info->header_size)
  163. ret = fpga_mgr_parse_header(mgr, info, miter.addr, miter.length);
  164. else
  165. ret = -EAGAIN;
  166. sg_miter_stop(&miter);
  167. if (ret && ret != -EAGAIN) {
  168. dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
  169. mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
  170. }
  171. return ret;
  172. }
  173. /*
  174. * Copy scattered FPGA image fragments to temporary buffer and call the
  175. * low level driver's parse_header function. This should be called after
  176. * fpga_mgr_parse_header_sg_first() returned -EAGAIN. In case of success,
  177. * pointer to the newly allocated image header copy will be returned and
  178. * its size will be set into *ret_size. Returned buffer needs to be freed.
  179. */
  180. static void *fpga_mgr_parse_header_sg(struct fpga_manager *mgr,
  181. struct fpga_image_info *info,
  182. struct sg_table *sgt, size_t *ret_size)
  183. {
  184. size_t len, new_header_size, header_size = 0;
  185. char *new_buf, *buf = NULL;
  186. int ret;
  187. do {
  188. new_header_size = info->header_size;
  189. if (new_header_size <= header_size) {
  190. dev_err(&mgr->dev, "Requested invalid header size\n");
  191. ret = -EFAULT;
  192. break;
  193. }
  194. new_buf = krealloc(buf, new_header_size, GFP_KERNEL);
  195. if (!new_buf) {
  196. ret = -ENOMEM;
  197. break;
  198. }
  199. buf = new_buf;
  200. len = sg_pcopy_to_buffer(sgt->sgl, sgt->nents,
  201. buf + header_size,
  202. new_header_size - header_size,
  203. header_size);
  204. if (len != new_header_size - header_size) {
  205. ret = -EFAULT;
  206. break;
  207. }
  208. header_size = new_header_size;
  209. ret = fpga_mgr_parse_header(mgr, info, buf, header_size);
  210. } while (ret == -EAGAIN);
  211. if (ret) {
  212. dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
  213. mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
  214. kfree(buf);
  215. buf = ERR_PTR(ret);
  216. }
  217. *ret_size = header_size;
  218. return buf;
  219. }
  220. /*
  221. * Call the low level driver's write_init function. This will do the
  222. * device-specific things to get the FPGA into the state where it is ready to
  223. * receive an FPGA image. The low level driver gets to see at least first
  224. * info->header_size bytes in the buffer. If info->header_size is 0,
  225. * write_init will not get any bytes of image buffer.
  226. */
  227. static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
  228. struct fpga_image_info *info,
  229. const char *buf, size_t count)
  230. {
  231. size_t header_size = info->header_size;
  232. int ret;
  233. mgr->state = FPGA_MGR_STATE_WRITE_INIT;
  234. if (header_size > count)
  235. ret = -EINVAL;
  236. else if (!header_size)
  237. ret = fpga_mgr_write_init(mgr, info, NULL, 0);
  238. else
  239. ret = fpga_mgr_write_init(mgr, info, buf, count);
  240. if (ret) {
  241. dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
  242. mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. static int fpga_mgr_prepare_sg(struct fpga_manager *mgr,
  248. struct fpga_image_info *info,
  249. struct sg_table *sgt)
  250. {
  251. struct sg_mapping_iter miter;
  252. size_t len;
  253. char *buf;
  254. int ret;
  255. /* Short path. Low level driver don't care about image header. */
  256. if (!mgr->mops->initial_header_size && !mgr->mops->parse_header)
  257. return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
  258. /*
  259. * First try to use miter to map the first fragment to access the
  260. * header, this is the typical path.
  261. */
  262. ret = fpga_mgr_parse_header_sg_first(mgr, info, sgt);
  263. /* If 0, header fits first fragment, call write_init on it */
  264. if (!ret) {
  265. sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
  266. if (sg_miter_next(&miter)) {
  267. ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
  268. miter.length);
  269. sg_miter_stop(&miter);
  270. return ret;
  271. }
  272. sg_miter_stop(&miter);
  273. /*
  274. * If -EAGAIN, more sg buffer is needed,
  275. * otherwise an error has occurred.
  276. */
  277. } else if (ret != -EAGAIN) {
  278. return ret;
  279. }
  280. /*
  281. * Copy the fragments into temporary memory.
  282. * Copying is done inside fpga_mgr_parse_header_sg().
  283. */
  284. buf = fpga_mgr_parse_header_sg(mgr, info, sgt, &len);
  285. if (IS_ERR(buf))
  286. return PTR_ERR(buf);
  287. ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
  288. kfree(buf);
  289. return ret;
  290. }
  291. /**
  292. * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
  293. * @mgr: fpga manager
  294. * @info: fpga image specific information
  295. * @sgt: scatterlist table
  296. *
  297. * Step the low level fpga manager through the device-specific steps of getting
  298. * an FPGA ready to be configured, writing the image to it, then doing whatever
  299. * post-configuration steps necessary. This code assumes the caller got the
  300. * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
  301. * not an error code.
  302. *
  303. * This is the preferred entry point for FPGA programming, it does not require
  304. * any contiguous kernel memory.
  305. *
  306. * Return: 0 on success, negative error code otherwise.
  307. */
  308. static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
  309. struct fpga_image_info *info,
  310. struct sg_table *sgt)
  311. {
  312. int ret;
  313. ret = fpga_mgr_prepare_sg(mgr, info, sgt);
  314. if (ret)
  315. return ret;
  316. /* Write the FPGA image to the FPGA. */
  317. mgr->state = FPGA_MGR_STATE_WRITE;
  318. if (mgr->mops->write_sg) {
  319. ret = fpga_mgr_write_sg(mgr, sgt);
  320. } else {
  321. size_t length, count = 0, data_size = info->data_size;
  322. struct sg_mapping_iter miter;
  323. sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
  324. if (mgr->mops->skip_header &&
  325. !sg_miter_skip(&miter, info->header_size)) {
  326. ret = -EINVAL;
  327. goto out;
  328. }
  329. while (sg_miter_next(&miter)) {
  330. if (data_size)
  331. length = min(miter.length, data_size - count);
  332. else
  333. length = miter.length;
  334. ret = fpga_mgr_write(mgr, miter.addr, length);
  335. if (ret)
  336. break;
  337. count += length;
  338. if (data_size && count >= data_size)
  339. break;
  340. }
  341. sg_miter_stop(&miter);
  342. }
  343. out:
  344. if (ret) {
  345. dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
  346. mgr->state = FPGA_MGR_STATE_WRITE_ERR;
  347. return ret;
  348. }
  349. return fpga_mgr_write_complete(mgr, info);
  350. }
  351. static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
  352. struct fpga_image_info *info,
  353. const char *buf, size_t count)
  354. {
  355. int ret;
  356. ret = fpga_mgr_parse_header_mapped(mgr, info, buf, count);
  357. if (ret)
  358. return ret;
  359. ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
  360. if (ret)
  361. return ret;
  362. if (mgr->mops->skip_header) {
  363. buf += info->header_size;
  364. count -= info->header_size;
  365. }
  366. if (info->data_size)
  367. count = info->data_size;
  368. /*
  369. * Write the FPGA image to the FPGA.
  370. */
  371. mgr->state = FPGA_MGR_STATE_WRITE;
  372. ret = fpga_mgr_write(mgr, buf, count);
  373. if (ret) {
  374. dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
  375. mgr->state = FPGA_MGR_STATE_WRITE_ERR;
  376. return ret;
  377. }
  378. return fpga_mgr_write_complete(mgr, info);
  379. }
  380. /**
  381. * fpga_mgr_buf_load - load fpga from image in buffer
  382. * @mgr: fpga manager
  383. * @info: fpga image info
  384. * @buf: buffer contain fpga image
  385. * @count: byte count of buf
  386. *
  387. * Step the low level fpga manager through the device-specific steps of getting
  388. * an FPGA ready to be configured, writing the image to it, then doing whatever
  389. * post-configuration steps necessary. This code assumes the caller got the
  390. * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
  391. *
  392. * Return: 0 on success, negative error code otherwise.
  393. */
  394. static int fpga_mgr_buf_load(struct fpga_manager *mgr,
  395. struct fpga_image_info *info,
  396. const char *buf, size_t count)
  397. {
  398. struct page **pages;
  399. struct sg_table sgt;
  400. const void *p;
  401. int nr_pages;
  402. int index;
  403. int rc;
  404. /*
  405. * This is just a fast path if the caller has already created a
  406. * contiguous kernel buffer and the driver doesn't require SG, non-SG
  407. * drivers will still work on the slow path.
  408. */
  409. if (mgr->mops->write)
  410. return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
  411. /*
  412. * Convert the linear kernel pointer into a sg_table of pages for use
  413. * by the driver.
  414. */
  415. nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
  416. (unsigned long)buf / PAGE_SIZE;
  417. pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
  418. if (!pages)
  419. return -ENOMEM;
  420. p = buf - offset_in_page(buf);
  421. for (index = 0; index < nr_pages; index++) {
  422. if (is_vmalloc_addr(p))
  423. pages[index] = vmalloc_to_page(p);
  424. else
  425. pages[index] = kmap_to_page((void *)p);
  426. if (!pages[index]) {
  427. kfree(pages);
  428. return -EFAULT;
  429. }
  430. p += PAGE_SIZE;
  431. }
  432. /*
  433. * The temporary pages list is used to code share the merging algorithm
  434. * in sg_alloc_table_from_pages
  435. */
  436. rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
  437. count, GFP_KERNEL);
  438. kfree(pages);
  439. if (rc)
  440. return rc;
  441. rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
  442. sg_free_table(&sgt);
  443. return rc;
  444. }
  445. /**
  446. * fpga_mgr_firmware_load - request firmware and load to fpga
  447. * @mgr: fpga manager
  448. * @info: fpga image specific information
  449. * @image_name: name of image file on the firmware search path
  450. *
  451. * Request an FPGA image using the firmware class, then write out to the FPGA.
  452. * Update the state before each step to provide info on what step failed if
  453. * there is a failure. This code assumes the caller got the mgr pointer
  454. * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
  455. * code.
  456. *
  457. * Return: 0 on success, negative error code otherwise.
  458. */
  459. static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
  460. struct fpga_image_info *info,
  461. const char *image_name)
  462. {
  463. struct device *dev = &mgr->dev;
  464. const struct firmware *fw;
  465. int ret;
  466. dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
  467. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
  468. ret = request_firmware(&fw, image_name, dev);
  469. if (ret) {
  470. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
  471. dev_err(dev, "Error requesting firmware %s\n", image_name);
  472. return ret;
  473. }
  474. ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
  475. release_firmware(fw);
  476. return ret;
  477. }
  478. /**
  479. * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
  480. * @mgr: fpga manager
  481. * @info: fpga image information.
  482. *
  483. * Load the FPGA from an image which is indicated in @info. If successful, the
  484. * FPGA ends up in operating mode.
  485. *
  486. * Return: 0 on success, negative error code otherwise.
  487. */
  488. int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
  489. {
  490. info->header_size = mgr->mops->initial_header_size;
  491. if (info->sgt)
  492. return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
  493. if (info->buf && info->count)
  494. return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
  495. if (info->firmware_name)
  496. return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
  497. return -EINVAL;
  498. }
  499. EXPORT_SYMBOL_GPL(fpga_mgr_load);
  500. static const char * const state_str[] = {
  501. [FPGA_MGR_STATE_UNKNOWN] = "unknown",
  502. [FPGA_MGR_STATE_POWER_OFF] = "power off",
  503. [FPGA_MGR_STATE_POWER_UP] = "power up",
  504. [FPGA_MGR_STATE_RESET] = "reset",
  505. /* requesting FPGA image from firmware */
  506. [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
  507. [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
  508. /* Parse FPGA image header */
  509. [FPGA_MGR_STATE_PARSE_HEADER] = "parse header",
  510. [FPGA_MGR_STATE_PARSE_HEADER_ERR] = "parse header error",
  511. /* Preparing FPGA to receive image */
  512. [FPGA_MGR_STATE_WRITE_INIT] = "write init",
  513. [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
  514. /* Writing image to FPGA */
  515. [FPGA_MGR_STATE_WRITE] = "write",
  516. [FPGA_MGR_STATE_WRITE_ERR] = "write error",
  517. /* Finishing configuration after image has been written */
  518. [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
  519. [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
  520. /* FPGA reports to be in normal operating mode */
  521. [FPGA_MGR_STATE_OPERATING] = "operating",
  522. };
  523. static ssize_t name_show(struct device *dev,
  524. struct device_attribute *attr, char *buf)
  525. {
  526. struct fpga_manager *mgr = to_fpga_manager(dev);
  527. return sprintf(buf, "%s\n", mgr->name);
  528. }
  529. static ssize_t state_show(struct device *dev,
  530. struct device_attribute *attr, char *buf)
  531. {
  532. struct fpga_manager *mgr = to_fpga_manager(dev);
  533. return sprintf(buf, "%s\n", state_str[mgr->state]);
  534. }
  535. static ssize_t status_show(struct device *dev,
  536. struct device_attribute *attr, char *buf)
  537. {
  538. struct fpga_manager *mgr = to_fpga_manager(dev);
  539. u64 status;
  540. int len = 0;
  541. status = fpga_mgr_status(mgr);
  542. if (status & FPGA_MGR_STATUS_OPERATION_ERR)
  543. len += sprintf(buf + len, "reconfig operation error\n");
  544. if (status & FPGA_MGR_STATUS_CRC_ERR)
  545. len += sprintf(buf + len, "reconfig CRC error\n");
  546. if (status & FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR)
  547. len += sprintf(buf + len, "reconfig incompatible image\n");
  548. if (status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
  549. len += sprintf(buf + len, "reconfig IP protocol error\n");
  550. if (status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
  551. len += sprintf(buf + len, "reconfig fifo overflow error\n");
  552. return len;
  553. }
  554. static DEVICE_ATTR_RO(name);
  555. static DEVICE_ATTR_RO(state);
  556. static DEVICE_ATTR_RO(status);
  557. static struct attribute *fpga_mgr_attrs[] = {
  558. &dev_attr_name.attr,
  559. &dev_attr_state.attr,
  560. &dev_attr_status.attr,
  561. NULL,
  562. };
  563. ATTRIBUTE_GROUPS(fpga_mgr);
  564. static struct fpga_manager *__fpga_mgr_get(struct device *mgr_dev)
  565. {
  566. struct fpga_manager *mgr;
  567. mgr = to_fpga_manager(mgr_dev);
  568. if (!try_module_get(mgr->mops_owner))
  569. mgr = ERR_PTR(-ENODEV);
  570. return mgr;
  571. }
  572. static int fpga_mgr_dev_match(struct device *dev, const void *data)
  573. {
  574. return dev->parent == data;
  575. }
  576. /**
  577. * fpga_mgr_get - Given a device, get a reference to an fpga mgr.
  578. * @dev: parent device that fpga mgr was registered with
  579. *
  580. * Return: fpga manager struct or IS_ERR() condition containing error code.
  581. */
  582. struct fpga_manager *fpga_mgr_get(struct device *dev)
  583. {
  584. struct fpga_manager *mgr;
  585. struct device *mgr_dev;
  586. mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev, fpga_mgr_dev_match);
  587. if (!mgr_dev)
  588. return ERR_PTR(-ENODEV);
  589. mgr = __fpga_mgr_get(mgr_dev);
  590. if (IS_ERR(mgr))
  591. put_device(mgr_dev);
  592. return mgr;
  593. }
  594. EXPORT_SYMBOL_GPL(fpga_mgr_get);
  595. /**
  596. * of_fpga_mgr_get - Given a device node, get a reference to an fpga mgr.
  597. *
  598. * @node: device node
  599. *
  600. * Return: fpga manager struct or IS_ERR() condition containing error code.
  601. */
  602. struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
  603. {
  604. struct fpga_manager *mgr;
  605. struct device *mgr_dev;
  606. mgr_dev = class_find_device_by_of_node(&fpga_mgr_class, node);
  607. if (!mgr_dev)
  608. return ERR_PTR(-ENODEV);
  609. mgr = __fpga_mgr_get(mgr_dev);
  610. if (IS_ERR(mgr))
  611. put_device(mgr_dev);
  612. return mgr;
  613. }
  614. EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
  615. /**
  616. * fpga_mgr_put - release a reference to an fpga manager
  617. * @mgr: fpga manager structure
  618. */
  619. void fpga_mgr_put(struct fpga_manager *mgr)
  620. {
  621. module_put(mgr->mops_owner);
  622. put_device(&mgr->dev);
  623. }
  624. EXPORT_SYMBOL_GPL(fpga_mgr_put);
  625. /**
  626. * fpga_mgr_lock - Lock FPGA manager for exclusive use
  627. * @mgr: fpga manager
  628. *
  629. * Given a pointer to FPGA Manager (from fpga_mgr_get() or
  630. * of_fpga_mgr_put()) attempt to get the mutex. The user should call
  631. * fpga_mgr_lock() and verify that it returns 0 before attempting to
  632. * program the FPGA. Likewise, the user should call fpga_mgr_unlock
  633. * when done programming the FPGA.
  634. *
  635. * Return: 0 for success or -EBUSY
  636. */
  637. int fpga_mgr_lock(struct fpga_manager *mgr)
  638. {
  639. if (!mutex_trylock(&mgr->ref_mutex)) {
  640. dev_err(&mgr->dev, "FPGA manager is in use.\n");
  641. return -EBUSY;
  642. }
  643. return 0;
  644. }
  645. EXPORT_SYMBOL_GPL(fpga_mgr_lock);
  646. /**
  647. * fpga_mgr_unlock - Unlock FPGA manager after done programming
  648. * @mgr: fpga manager
  649. */
  650. void fpga_mgr_unlock(struct fpga_manager *mgr)
  651. {
  652. mutex_unlock(&mgr->ref_mutex);
  653. }
  654. EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
  655. /**
  656. * __fpga_mgr_register_full - create and register an FPGA Manager device
  657. * @parent: fpga manager device from pdev
  658. * @info: parameters for fpga manager
  659. * @owner: owner module containing the ops
  660. *
  661. * The caller of this function is responsible for calling fpga_mgr_unregister().
  662. * Using devm_fpga_mgr_register_full() instead is recommended.
  663. *
  664. * Return: pointer to struct fpga_manager pointer or ERR_PTR()
  665. */
  666. struct fpga_manager *
  667. __fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
  668. struct module *owner)
  669. {
  670. const struct fpga_manager_ops *mops = info->mops;
  671. struct fpga_manager *mgr;
  672. int id, ret;
  673. if (!mops) {
  674. dev_err(parent, "Attempt to register without fpga_manager_ops\n");
  675. return ERR_PTR(-EINVAL);
  676. }
  677. if (!info->name || !strlen(info->name)) {
  678. dev_err(parent, "Attempt to register with no name!\n");
  679. return ERR_PTR(-EINVAL);
  680. }
  681. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  682. if (!mgr)
  683. return ERR_PTR(-ENOMEM);
  684. id = ida_alloc(&fpga_mgr_ida, GFP_KERNEL);
  685. if (id < 0) {
  686. ret = id;
  687. goto error_kfree;
  688. }
  689. mutex_init(&mgr->ref_mutex);
  690. mgr->mops_owner = owner;
  691. mgr->name = info->name;
  692. mgr->mops = info->mops;
  693. mgr->priv = info->priv;
  694. mgr->compat_id = info->compat_id;
  695. mgr->dev.class = &fpga_mgr_class;
  696. mgr->dev.groups = mops->groups;
  697. mgr->dev.parent = parent;
  698. mgr->dev.of_node = parent->of_node;
  699. mgr->dev.id = id;
  700. ret = dev_set_name(&mgr->dev, "fpga%d", id);
  701. if (ret)
  702. goto error_device;
  703. /*
  704. * Initialize framework state by requesting low level driver read state
  705. * from device. FPGA may be in reset mode or may have been programmed
  706. * by bootloader or EEPROM.
  707. */
  708. mgr->state = fpga_mgr_state(mgr);
  709. ret = device_register(&mgr->dev);
  710. if (ret) {
  711. put_device(&mgr->dev);
  712. return ERR_PTR(ret);
  713. }
  714. return mgr;
  715. error_device:
  716. ida_free(&fpga_mgr_ida, id);
  717. error_kfree:
  718. kfree(mgr);
  719. return ERR_PTR(ret);
  720. }
  721. EXPORT_SYMBOL_GPL(__fpga_mgr_register_full);
  722. /**
  723. * __fpga_mgr_register - create and register an FPGA Manager device
  724. * @parent: fpga manager device from pdev
  725. * @name: fpga manager name
  726. * @mops: pointer to structure of fpga manager ops
  727. * @priv: fpga manager private data
  728. * @owner: owner module containing the ops
  729. *
  730. * The caller of this function is responsible for calling fpga_mgr_unregister().
  731. * Using devm_fpga_mgr_register() instead is recommended. This simple
  732. * version of the register function should be sufficient for most users. The
  733. * fpga_mgr_register_full() function is available for users that need to pass
  734. * additional, optional parameters.
  735. *
  736. * Return: pointer to struct fpga_manager pointer or ERR_PTR()
  737. */
  738. struct fpga_manager *
  739. __fpga_mgr_register(struct device *parent, const char *name,
  740. const struct fpga_manager_ops *mops, void *priv, struct module *owner)
  741. {
  742. struct fpga_manager_info info = { 0 };
  743. info.name = name;
  744. info.mops = mops;
  745. info.priv = priv;
  746. return __fpga_mgr_register_full(parent, &info, owner);
  747. }
  748. EXPORT_SYMBOL_GPL(__fpga_mgr_register);
  749. /**
  750. * fpga_mgr_unregister - unregister an FPGA manager
  751. * @mgr: fpga manager struct
  752. *
  753. * This function is intended for use in an FPGA manager driver's remove function.
  754. */
  755. void fpga_mgr_unregister(struct fpga_manager *mgr)
  756. {
  757. dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
  758. /*
  759. * If the low level driver provides a method for putting fpga into
  760. * a desired state upon unregister, do it.
  761. */
  762. fpga_mgr_fpga_remove(mgr);
  763. device_unregister(&mgr->dev);
  764. }
  765. EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
  766. static void devm_fpga_mgr_unregister(struct device *dev, void *res)
  767. {
  768. struct fpga_mgr_devres *dr = res;
  769. fpga_mgr_unregister(dr->mgr);
  770. }
  771. /**
  772. * __devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
  773. * @parent: fpga manager device from pdev
  774. * @info: parameters for fpga manager
  775. * @owner: owner module containing the ops
  776. *
  777. * Return: fpga manager pointer on success, negative error code otherwise.
  778. *
  779. * This is the devres variant of fpga_mgr_register_full() for which the unregister
  780. * function will be called automatically when the managing device is detached.
  781. */
  782. struct fpga_manager *
  783. __devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info,
  784. struct module *owner)
  785. {
  786. struct fpga_mgr_devres *dr;
  787. struct fpga_manager *mgr;
  788. dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
  789. if (!dr)
  790. return ERR_PTR(-ENOMEM);
  791. mgr = __fpga_mgr_register_full(parent, info, owner);
  792. if (IS_ERR(mgr)) {
  793. devres_free(dr);
  794. return mgr;
  795. }
  796. dr->mgr = mgr;
  797. devres_add(parent, dr);
  798. return mgr;
  799. }
  800. EXPORT_SYMBOL_GPL(__devm_fpga_mgr_register_full);
  801. /**
  802. * __devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
  803. * @parent: fpga manager device from pdev
  804. * @name: fpga manager name
  805. * @mops: pointer to structure of fpga manager ops
  806. * @priv: fpga manager private data
  807. * @owner: owner module containing the ops
  808. *
  809. * Return: fpga manager pointer on success, negative error code otherwise.
  810. *
  811. * This is the devres variant of fpga_mgr_register() for which the
  812. * unregister function will be called automatically when the managing
  813. * device is detached.
  814. */
  815. struct fpga_manager *
  816. __devm_fpga_mgr_register(struct device *parent, const char *name,
  817. const struct fpga_manager_ops *mops, void *priv,
  818. struct module *owner)
  819. {
  820. struct fpga_manager_info info = { 0 };
  821. info.name = name;
  822. info.mops = mops;
  823. info.priv = priv;
  824. return __devm_fpga_mgr_register_full(parent, &info, owner);
  825. }
  826. EXPORT_SYMBOL_GPL(__devm_fpga_mgr_register);
  827. static void fpga_mgr_dev_release(struct device *dev)
  828. {
  829. struct fpga_manager *mgr = to_fpga_manager(dev);
  830. ida_free(&fpga_mgr_ida, mgr->dev.id);
  831. kfree(mgr);
  832. }
  833. static const struct class fpga_mgr_class = {
  834. .name = "fpga_manager",
  835. .dev_groups = fpga_mgr_groups,
  836. .dev_release = fpga_mgr_dev_release,
  837. };
  838. static int __init fpga_mgr_class_init(void)
  839. {
  840. pr_info("FPGA manager framework\n");
  841. return class_register(&fpga_mgr_class);
  842. }
  843. static void __exit fpga_mgr_class_exit(void)
  844. {
  845. class_unregister(&fpga_mgr_class);
  846. ida_destroy(&fpga_mgr_ida);
  847. }
  848. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  849. MODULE_DESCRIPTION("FPGA manager framework");
  850. MODULE_LICENSE("GPL v2");
  851. subsys_initcall(fpga_mgr_class_init);
  852. module_exit(fpga_mgr_class_exit);