fmc-sdb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/fmc.h>
  13. #include <linux/sdb.h>
  14. #include <linux/err.h>
  15. #include <linux/fmc-sdb.h>
  16. #include <asm/byteorder.h>
  17. static uint32_t __sdb_rd(struct fmc_device *fmc, unsigned long address,
  18. int convert)
  19. {
  20. uint32_t res = fmc_readl(fmc, address);
  21. if (convert)
  22. return __be32_to_cpu(res);
  23. return res;
  24. }
  25. static struct sdb_array *__fmc_scan_sdb_tree(struct fmc_device *fmc,
  26. unsigned long sdb_addr,
  27. unsigned long reg_base, int level)
  28. {
  29. uint32_t onew;
  30. int i, j, n, convert = 0;
  31. struct sdb_array *arr, *sub;
  32. onew = fmc_readl(fmc, sdb_addr);
  33. if (onew == SDB_MAGIC) {
  34. /* Uh! If we are little-endian, we must convert */
  35. if (SDB_MAGIC != __be32_to_cpu(SDB_MAGIC))
  36. convert = 1;
  37. } else if (onew == __be32_to_cpu(SDB_MAGIC)) {
  38. /* ok, don't convert */
  39. } else {
  40. return ERR_PTR(-ENOENT);
  41. }
  42. /* So, the magic was there: get the count from offset 4*/
  43. onew = __sdb_rd(fmc, sdb_addr + 4, convert);
  44. n = __be16_to_cpu(*(uint16_t *)&onew);
  45. arr = kzalloc(sizeof(*arr), GFP_KERNEL);
  46. if (!arr)
  47. return ERR_PTR(-ENOMEM);
  48. arr->record = kzalloc(sizeof(arr->record[0]) * n, GFP_KERNEL);
  49. arr->subtree = kzalloc(sizeof(arr->subtree[0]) * n, GFP_KERNEL);
  50. if (!arr->record || !arr->subtree) {
  51. kfree(arr->record);
  52. kfree(arr->subtree);
  53. kfree(arr);
  54. return ERR_PTR(-ENOMEM);
  55. }
  56. arr->len = n;
  57. arr->level = level;
  58. arr->fmc = fmc;
  59. for (i = 0; i < n; i++) {
  60. union sdb_record *r;
  61. for (j = 0; j < sizeof(arr->record[0]); j += 4) {
  62. *(uint32_t *)((void *)(arr->record + i) + j) =
  63. __sdb_rd(fmc, sdb_addr + (i * 64) + j, convert);
  64. }
  65. r = &arr->record[i];
  66. arr->subtree[i] = ERR_PTR(-ENODEV);
  67. if (r->empty.record_type == sdb_type_bridge) {
  68. struct sdb_component *c = &r->bridge.sdb_component;
  69. uint64_t subaddr = __be64_to_cpu(r->bridge.sdb_child);
  70. uint64_t newbase = __be64_to_cpu(c->addr_first);
  71. subaddr += reg_base;
  72. newbase += reg_base;
  73. sub = __fmc_scan_sdb_tree(fmc, subaddr, newbase,
  74. level + 1);
  75. arr->subtree[i] = sub; /* may be error */
  76. if (IS_ERR(sub))
  77. continue;
  78. sub->parent = arr;
  79. sub->baseaddr = newbase;
  80. }
  81. }
  82. return arr;
  83. }
  84. int fmc_scan_sdb_tree(struct fmc_device *fmc, unsigned long address)
  85. {
  86. struct sdb_array *ret;
  87. if (fmc->sdb)
  88. return -EBUSY;
  89. ret = __fmc_scan_sdb_tree(fmc, address, 0 /* regs */, 0);
  90. if (IS_ERR(ret))
  91. return PTR_ERR(ret);
  92. fmc->sdb = ret;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(fmc_scan_sdb_tree);
  96. static void __fmc_sdb_free(struct sdb_array *arr)
  97. {
  98. int i, n;
  99. if (!arr)
  100. return;
  101. n = arr->len;
  102. for (i = 0; i < n; i++) {
  103. if (IS_ERR(arr->subtree[i]))
  104. continue;
  105. __fmc_sdb_free(arr->subtree[i]);
  106. }
  107. kfree(arr->record);
  108. kfree(arr->subtree);
  109. kfree(arr);
  110. }
  111. int fmc_free_sdb_tree(struct fmc_device *fmc)
  112. {
  113. __fmc_sdb_free(fmc->sdb);
  114. fmc->sdb = NULL;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(fmc_free_sdb_tree);
  118. /* This helper calls reprogram and inizialized sdb as well */
  119. int fmc_reprogram(struct fmc_device *fmc, struct fmc_driver *d, char *gw,
  120. int sdb_entry)
  121. {
  122. int ret;
  123. ret = fmc->op->reprogram(fmc, d, gw);
  124. if (ret < 0)
  125. return ret;
  126. if (sdb_entry < 0)
  127. return ret;
  128. /* We are required to find SDB at a given offset */
  129. ret = fmc_scan_sdb_tree(fmc, sdb_entry);
  130. if (ret < 0) {
  131. dev_err(&fmc->dev, "Can't find SDB at address 0x%x\n",
  132. sdb_entry);
  133. return -ENODEV;
  134. }
  135. fmc_dump_sdb(fmc);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(fmc_reprogram);
  139. static char *__strip_trailing_space(char *buf, char *str, int len)
  140. {
  141. int i = len - 1;
  142. memcpy(buf, str, len);
  143. while(i >= 0 && buf[i] == ' ')
  144. buf[i--] = '\0';
  145. return buf;
  146. }
  147. #define __sdb_string(buf, field) ({ \
  148. BUILD_BUG_ON(sizeof(buf) < sizeof(field)); \
  149. __strip_trailing_space(buf, (void *)(field), sizeof(field)); \
  150. })
  151. static void __fmc_show_sdb_tree(const struct fmc_device *fmc,
  152. const struct sdb_array *arr)
  153. {
  154. unsigned long base = arr->baseaddr;
  155. int i, j, n = arr->len, level = arr->level;
  156. char buf[64];
  157. for (i = 0; i < n; i++) {
  158. union sdb_record *r;
  159. struct sdb_product *p;
  160. struct sdb_component *c;
  161. r = &arr->record[i];
  162. c = &r->dev.sdb_component;
  163. p = &c->product;
  164. dev_info(&fmc->dev, "SDB: ");
  165. for (j = 0; j < level; j++)
  166. printk(KERN_CONT " ");
  167. switch (r->empty.record_type) {
  168. case sdb_type_interconnect:
  169. printk(KERN_CONT "%08llx:%08x %.19s\n",
  170. __be64_to_cpu(p->vendor_id),
  171. __be32_to_cpu(p->device_id),
  172. p->name);
  173. break;
  174. case sdb_type_device:
  175. printk(KERN_CONT "%08llx:%08x %.19s (%08llx-%08llx)\n",
  176. __be64_to_cpu(p->vendor_id),
  177. __be32_to_cpu(p->device_id),
  178. p->name,
  179. __be64_to_cpu(c->addr_first) + base,
  180. __be64_to_cpu(c->addr_last) + base);
  181. break;
  182. case sdb_type_bridge:
  183. printk(KERN_CONT "%08llx:%08x %.19s (bridge: %08llx)\n",
  184. __be64_to_cpu(p->vendor_id),
  185. __be32_to_cpu(p->device_id),
  186. p->name,
  187. __be64_to_cpu(c->addr_first) + base);
  188. if (IS_ERR(arr->subtree[i])) {
  189. dev_info(&fmc->dev, "SDB: (bridge error %li)\n",
  190. PTR_ERR(arr->subtree[i]));
  191. break;
  192. }
  193. __fmc_show_sdb_tree(fmc, arr->subtree[i]);
  194. break;
  195. case sdb_type_integration:
  196. printk(KERN_CONT "integration\n");
  197. break;
  198. case sdb_type_repo_url:
  199. printk(KERN_CONT "Synthesis repository: %s\n",
  200. __sdb_string(buf, r->repo_url.repo_url));
  201. break;
  202. case sdb_type_synthesis:
  203. printk(KERN_CONT "Bitstream '%s' ",
  204. __sdb_string(buf, r->synthesis.syn_name));
  205. printk(KERN_CONT "synthesized %08x by %s ",
  206. __be32_to_cpu(r->synthesis.date),
  207. __sdb_string(buf, r->synthesis.user_name));
  208. printk(KERN_CONT "(%s version %x), ",
  209. __sdb_string(buf, r->synthesis.tool_name),
  210. __be32_to_cpu(r->synthesis.tool_version));
  211. printk(KERN_CONT "commit %pm\n",
  212. r->synthesis.commit_id);
  213. break;
  214. case sdb_type_empty:
  215. printk(KERN_CONT "empty\n");
  216. break;
  217. default:
  218. printk(KERN_CONT "UNKNOWN TYPE 0x%02x\n",
  219. r->empty.record_type);
  220. break;
  221. }
  222. }
  223. }
  224. void fmc_show_sdb_tree(const struct fmc_device *fmc)
  225. {
  226. if (!fmc->sdb)
  227. return;
  228. __fmc_show_sdb_tree(fmc, fmc->sdb);
  229. }
  230. EXPORT_SYMBOL(fmc_show_sdb_tree);
  231. signed long fmc_find_sdb_device(struct sdb_array *tree,
  232. uint64_t vid, uint32_t did, unsigned long *sz)
  233. {
  234. signed long res = -ENODEV;
  235. union sdb_record *r;
  236. struct sdb_product *p;
  237. struct sdb_component *c;
  238. int i, n = tree->len;
  239. uint64_t last, first;
  240. /* FIXME: what if the first interconnect is not at zero? */
  241. for (i = 0; i < n; i++) {
  242. r = &tree->record[i];
  243. c = &r->dev.sdb_component;
  244. p = &c->product;
  245. if (!IS_ERR(tree->subtree[i]))
  246. res = fmc_find_sdb_device(tree->subtree[i],
  247. vid, did, sz);
  248. if (res >= 0)
  249. return res + tree->baseaddr;
  250. if (r->empty.record_type != sdb_type_device)
  251. continue;
  252. if (__be64_to_cpu(p->vendor_id) != vid)
  253. continue;
  254. if (__be32_to_cpu(p->device_id) != did)
  255. continue;
  256. /* found */
  257. last = __be64_to_cpu(c->addr_last);
  258. first = __be64_to_cpu(c->addr_first);
  259. if (sz)
  260. *sz = (typeof(*sz))(last + 1 - first);
  261. return first + tree->baseaddr;
  262. }
  263. return res;
  264. }
  265. EXPORT_SYMBOL(fmc_find_sdb_device);