fpga-mgr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 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 *dev)
  565. {
  566. struct fpga_manager *mgr;
  567. mgr = to_fpga_manager(dev);
  568. if (!try_module_get(dev->parent->driver->owner))
  569. goto err_dev;
  570. return mgr;
  571. err_dev:
  572. put_device(dev);
  573. return ERR_PTR(-ENODEV);
  574. }
  575. static int fpga_mgr_dev_match(struct device *dev, const void *data)
  576. {
  577. return dev->parent == data;
  578. }
  579. /**
  580. * fpga_mgr_get - Given a device, get a reference to an fpga mgr.
  581. * @dev: parent device that fpga mgr was registered with
  582. *
  583. * Return: fpga manager struct or IS_ERR() condition containing error code.
  584. */
  585. struct fpga_manager *fpga_mgr_get(struct device *dev)
  586. {
  587. struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
  588. fpga_mgr_dev_match);
  589. if (!mgr_dev)
  590. return ERR_PTR(-ENODEV);
  591. return __fpga_mgr_get(mgr_dev);
  592. }
  593. EXPORT_SYMBOL_GPL(fpga_mgr_get);
  594. /**
  595. * of_fpga_mgr_get - Given a device node, get a reference to an fpga mgr.
  596. *
  597. * @node: device node
  598. *
  599. * Return: fpga manager struct or IS_ERR() condition containing error code.
  600. */
  601. struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
  602. {
  603. struct device *dev;
  604. dev = class_find_device_by_of_node(fpga_mgr_class, node);
  605. if (!dev)
  606. return ERR_PTR(-ENODEV);
  607. return __fpga_mgr_get(dev);
  608. }
  609. EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
  610. /**
  611. * fpga_mgr_put - release a reference to an fpga manager
  612. * @mgr: fpga manager structure
  613. */
  614. void fpga_mgr_put(struct fpga_manager *mgr)
  615. {
  616. module_put(mgr->dev.parent->driver->owner);
  617. put_device(&mgr->dev);
  618. }
  619. EXPORT_SYMBOL_GPL(fpga_mgr_put);
  620. /**
  621. * fpga_mgr_lock - Lock FPGA manager for exclusive use
  622. * @mgr: fpga manager
  623. *
  624. * Given a pointer to FPGA Manager (from fpga_mgr_get() or
  625. * of_fpga_mgr_put()) attempt to get the mutex. The user should call
  626. * fpga_mgr_lock() and verify that it returns 0 before attempting to
  627. * program the FPGA. Likewise, the user should call fpga_mgr_unlock
  628. * when done programming the FPGA.
  629. *
  630. * Return: 0 for success or -EBUSY
  631. */
  632. int fpga_mgr_lock(struct fpga_manager *mgr)
  633. {
  634. if (!mutex_trylock(&mgr->ref_mutex)) {
  635. dev_err(&mgr->dev, "FPGA manager is in use.\n");
  636. return -EBUSY;
  637. }
  638. return 0;
  639. }
  640. EXPORT_SYMBOL_GPL(fpga_mgr_lock);
  641. /**
  642. * fpga_mgr_unlock - Unlock FPGA manager after done programming
  643. * @mgr: fpga manager
  644. */
  645. void fpga_mgr_unlock(struct fpga_manager *mgr)
  646. {
  647. mutex_unlock(&mgr->ref_mutex);
  648. }
  649. EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
  650. /**
  651. * fpga_mgr_register_full - create and register an FPGA Manager device
  652. * @parent: fpga manager device from pdev
  653. * @info: parameters for fpga manager
  654. *
  655. * The caller of this function is responsible for calling fpga_mgr_unregister().
  656. * Using devm_fpga_mgr_register_full() instead is recommended.
  657. *
  658. * Return: pointer to struct fpga_manager pointer or ERR_PTR()
  659. */
  660. struct fpga_manager *
  661. fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
  662. {
  663. const struct fpga_manager_ops *mops = info->mops;
  664. struct fpga_manager *mgr;
  665. int id, ret;
  666. if (!mops) {
  667. dev_err(parent, "Attempt to register without fpga_manager_ops\n");
  668. return ERR_PTR(-EINVAL);
  669. }
  670. if (!info->name || !strlen(info->name)) {
  671. dev_err(parent, "Attempt to register with no name!\n");
  672. return ERR_PTR(-EINVAL);
  673. }
  674. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  675. if (!mgr)
  676. return ERR_PTR(-ENOMEM);
  677. id = ida_alloc(&fpga_mgr_ida, GFP_KERNEL);
  678. if (id < 0) {
  679. ret = id;
  680. goto error_kfree;
  681. }
  682. mutex_init(&mgr->ref_mutex);
  683. mgr->name = info->name;
  684. mgr->mops = info->mops;
  685. mgr->priv = info->priv;
  686. mgr->compat_id = info->compat_id;
  687. mgr->dev.class = fpga_mgr_class;
  688. mgr->dev.groups = mops->groups;
  689. mgr->dev.parent = parent;
  690. mgr->dev.of_node = parent->of_node;
  691. mgr->dev.id = id;
  692. ret = dev_set_name(&mgr->dev, "fpga%d", id);
  693. if (ret)
  694. goto error_device;
  695. /*
  696. * Initialize framework state by requesting low level driver read state
  697. * from device. FPGA may be in reset mode or may have been programmed
  698. * by bootloader or EEPROM.
  699. */
  700. mgr->state = fpga_mgr_state(mgr);
  701. ret = device_register(&mgr->dev);
  702. if (ret) {
  703. put_device(&mgr->dev);
  704. return ERR_PTR(ret);
  705. }
  706. return mgr;
  707. error_device:
  708. ida_free(&fpga_mgr_ida, id);
  709. error_kfree:
  710. kfree(mgr);
  711. return ERR_PTR(ret);
  712. }
  713. EXPORT_SYMBOL_GPL(fpga_mgr_register_full);
  714. /**
  715. * fpga_mgr_register - create and register an FPGA Manager device
  716. * @parent: fpga manager device from pdev
  717. * @name: fpga manager name
  718. * @mops: pointer to structure of fpga manager ops
  719. * @priv: fpga manager private data
  720. *
  721. * The caller of this function is responsible for calling fpga_mgr_unregister().
  722. * Using devm_fpga_mgr_register() instead is recommended. This simple
  723. * version of the register function should be sufficient for most users. The
  724. * fpga_mgr_register_full() function is available for users that need to pass
  725. * additional, optional parameters.
  726. *
  727. * Return: pointer to struct fpga_manager pointer or ERR_PTR()
  728. */
  729. struct fpga_manager *
  730. fpga_mgr_register(struct device *parent, const char *name,
  731. const struct fpga_manager_ops *mops, void *priv)
  732. {
  733. struct fpga_manager_info info = { 0 };
  734. info.name = name;
  735. info.mops = mops;
  736. info.priv = priv;
  737. return fpga_mgr_register_full(parent, &info);
  738. }
  739. EXPORT_SYMBOL_GPL(fpga_mgr_register);
  740. /**
  741. * fpga_mgr_unregister - unregister an FPGA manager
  742. * @mgr: fpga manager struct
  743. *
  744. * This function is intended for use in an FPGA manager driver's remove function.
  745. */
  746. void fpga_mgr_unregister(struct fpga_manager *mgr)
  747. {
  748. dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
  749. /*
  750. * If the low level driver provides a method for putting fpga into
  751. * a desired state upon unregister, do it.
  752. */
  753. fpga_mgr_fpga_remove(mgr);
  754. device_unregister(&mgr->dev);
  755. }
  756. EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
  757. static void devm_fpga_mgr_unregister(struct device *dev, void *res)
  758. {
  759. struct fpga_mgr_devres *dr = res;
  760. fpga_mgr_unregister(dr->mgr);
  761. }
  762. /**
  763. * devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
  764. * @parent: fpga manager device from pdev
  765. * @info: parameters for fpga manager
  766. *
  767. * Return: fpga manager pointer on success, negative error code otherwise.
  768. *
  769. * This is the devres variant of fpga_mgr_register_full() for which the unregister
  770. * function will be called automatically when the managing device is detached.
  771. */
  772. struct fpga_manager *
  773. devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
  774. {
  775. struct fpga_mgr_devres *dr;
  776. struct fpga_manager *mgr;
  777. dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
  778. if (!dr)
  779. return ERR_PTR(-ENOMEM);
  780. mgr = fpga_mgr_register_full(parent, info);
  781. if (IS_ERR(mgr)) {
  782. devres_free(dr);
  783. return mgr;
  784. }
  785. dr->mgr = mgr;
  786. devres_add(parent, dr);
  787. return mgr;
  788. }
  789. EXPORT_SYMBOL_GPL(devm_fpga_mgr_register_full);
  790. /**
  791. * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
  792. * @parent: fpga manager device from pdev
  793. * @name: fpga manager name
  794. * @mops: pointer to structure of fpga manager ops
  795. * @priv: fpga manager private data
  796. *
  797. * Return: fpga manager pointer on success, negative error code otherwise.
  798. *
  799. * This is the devres variant of fpga_mgr_register() for which the
  800. * unregister function will be called automatically when the managing
  801. * device is detached.
  802. */
  803. struct fpga_manager *
  804. devm_fpga_mgr_register(struct device *parent, const char *name,
  805. const struct fpga_manager_ops *mops, void *priv)
  806. {
  807. struct fpga_manager_info info = { 0 };
  808. info.name = name;
  809. info.mops = mops;
  810. info.priv = priv;
  811. return devm_fpga_mgr_register_full(parent, &info);
  812. }
  813. EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
  814. static void fpga_mgr_dev_release(struct device *dev)
  815. {
  816. struct fpga_manager *mgr = to_fpga_manager(dev);
  817. ida_free(&fpga_mgr_ida, mgr->dev.id);
  818. kfree(mgr);
  819. }
  820. static int __init fpga_mgr_class_init(void)
  821. {
  822. pr_info("FPGA manager framework\n");
  823. fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
  824. if (IS_ERR(fpga_mgr_class))
  825. return PTR_ERR(fpga_mgr_class);
  826. fpga_mgr_class->dev_groups = fpga_mgr_groups;
  827. fpga_mgr_class->dev_release = fpga_mgr_dev_release;
  828. return 0;
  829. }
  830. static void __exit fpga_mgr_class_exit(void)
  831. {
  832. class_destroy(fpga_mgr_class);
  833. ida_destroy(&fpga_mgr_ida);
  834. }
  835. MODULE_AUTHOR("Alan Tull <[email protected]>");
  836. MODULE_DESCRIPTION("FPGA manager framework");
  837. MODULE_LICENSE("GPL v2");
  838. subsys_initcall(fpga_mgr_class_init);
  839. module_exit(fpga_mgr_class_exit);