mcb-parse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <linux/types.h>
  2. #include <linux/ioport.h>
  3. #include <linux/slab.h>
  4. #include <linux/export.h>
  5. #include <linux/io.h>
  6. #include <linux/mcb.h>
  7. #include "mcb-internal.h"
  8. struct mcb_parse_priv {
  9. phys_addr_t mapbase;
  10. void __iomem *base;
  11. };
  12. #define for_each_chameleon_cell(dtype, p) \
  13. for ((dtype) = get_next_dtype((p)); \
  14. (dtype) != CHAMELEON_DTYPE_END; \
  15. (dtype) = get_next_dtype((p)))
  16. static inline uint32_t get_next_dtype(void __iomem *p)
  17. {
  18. uint32_t dtype;
  19. dtype = readl(p);
  20. return dtype >> 28;
  21. }
  22. static int chameleon_parse_bdd(struct mcb_bus *bus,
  23. struct chameleon_bar *cb,
  24. void __iomem *base)
  25. {
  26. return 0;
  27. }
  28. static int chameleon_parse_gdd(struct mcb_bus *bus,
  29. struct chameleon_bar *cb,
  30. void __iomem *base, int bar_count)
  31. {
  32. struct chameleon_gdd __iomem *gdd =
  33. (struct chameleon_gdd __iomem *) base;
  34. struct mcb_device *mdev;
  35. u32 dev_mapbase;
  36. u32 offset;
  37. u32 size;
  38. int ret;
  39. __le32 reg1;
  40. __le32 reg2;
  41. mdev = mcb_alloc_dev(bus);
  42. if (!mdev)
  43. return -ENOMEM;
  44. reg1 = readl(&gdd->reg1);
  45. reg2 = readl(&gdd->reg2);
  46. offset = readl(&gdd->offset);
  47. size = readl(&gdd->size);
  48. mdev->id = GDD_DEV(reg1);
  49. mdev->rev = GDD_REV(reg1);
  50. mdev->var = GDD_VAR(reg1);
  51. mdev->bar = GDD_BAR(reg2);
  52. mdev->group = GDD_GRP(reg2);
  53. mdev->inst = GDD_INS(reg2);
  54. /*
  55. * If the BAR is missing, dev_mapbase is zero, or if the
  56. * device is IO mapped we just print a warning and go on with the
  57. * next device, instead of completely stop the gdd parser
  58. */
  59. if (mdev->bar > bar_count - 1) {
  60. pr_info("No BAR for 16z%03d\n", mdev->id);
  61. ret = 0;
  62. goto err;
  63. }
  64. dev_mapbase = cb[mdev->bar].addr;
  65. if (!dev_mapbase) {
  66. pr_info("BAR not assigned for 16z%03d\n", mdev->id);
  67. ret = 0;
  68. goto err;
  69. }
  70. if (dev_mapbase & 0x01) {
  71. pr_info("IO mapped Device (16z%03d) not yet supported\n",
  72. mdev->id);
  73. ret = 0;
  74. goto err;
  75. }
  76. pr_debug("Found a 16z%03d\n", mdev->id);
  77. mdev->irq.start = GDD_IRQ(reg1);
  78. mdev->irq.end = GDD_IRQ(reg1);
  79. mdev->irq.flags = IORESOURCE_IRQ;
  80. mdev->mem.start = dev_mapbase + offset;
  81. mdev->mem.end = mdev->mem.start + size - 1;
  82. mdev->mem.flags = IORESOURCE_MEM;
  83. mdev->is_added = false;
  84. ret = mcb_device_register(bus, mdev);
  85. if (ret < 0)
  86. goto err;
  87. return 0;
  88. err:
  89. mcb_free_dev(mdev);
  90. return ret;
  91. }
  92. static void chameleon_parse_bar(void __iomem *base,
  93. struct chameleon_bar *cb, int bar_count)
  94. {
  95. char __iomem *p = base;
  96. int i;
  97. /* skip reg1 */
  98. p += sizeof(__le32);
  99. for (i = 0; i < bar_count; i++) {
  100. cb[i].addr = readl(p);
  101. cb[i].size = readl(p + 4);
  102. p += sizeof(struct chameleon_bar);
  103. }
  104. }
  105. static int chameleon_get_bar(char __iomem **base, phys_addr_t mapbase,
  106. struct chameleon_bar **cb)
  107. {
  108. struct chameleon_bar *c;
  109. int bar_count;
  110. __le32 reg;
  111. u32 dtype;
  112. /*
  113. * For those devices which are not connected
  114. * to the PCI Bus (e.g. LPC) there is a bar
  115. * descriptor located directly after the
  116. * chameleon header. This header is comparable
  117. * to a PCI header.
  118. */
  119. dtype = get_next_dtype(*base);
  120. if (dtype == CHAMELEON_DTYPE_BAR) {
  121. reg = readl(*base);
  122. bar_count = BAR_CNT(reg);
  123. if (bar_count <= 0 && bar_count > CHAMELEON_BAR_MAX)
  124. return -ENODEV;
  125. c = kcalloc(bar_count, sizeof(struct chameleon_bar),
  126. GFP_KERNEL);
  127. if (!c)
  128. return -ENOMEM;
  129. chameleon_parse_bar(*base, c, bar_count);
  130. *base += BAR_DESC_SIZE(bar_count);
  131. } else {
  132. c = kzalloc(sizeof(struct chameleon_bar), GFP_KERNEL);
  133. if (!c)
  134. return -ENOMEM;
  135. bar_count = 1;
  136. c->addr = mapbase;
  137. }
  138. *cb = c;
  139. return bar_count;
  140. }
  141. int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
  142. void __iomem *base)
  143. {
  144. struct chameleon_fpga_header *header;
  145. struct chameleon_bar *cb;
  146. char __iomem *p = base;
  147. int num_cells = 0;
  148. uint32_t dtype;
  149. int bar_count;
  150. int ret = 0;
  151. u32 hsize;
  152. hsize = sizeof(struct chameleon_fpga_header);
  153. header = kzalloc(hsize, GFP_KERNEL);
  154. if (!header)
  155. return -ENOMEM;
  156. /* Extract header information */
  157. memcpy_fromio(header, p, hsize);
  158. /* We only support chameleon v2 at the moment */
  159. header->magic = le16_to_cpu(header->magic);
  160. if (header->magic != CHAMELEONV2_MAGIC) {
  161. pr_err("Unsupported chameleon version 0x%x\n",
  162. header->magic);
  163. ret = -ENODEV;
  164. goto free_header;
  165. }
  166. p += hsize;
  167. bus->revision = header->revision;
  168. bus->model = header->model;
  169. bus->minor = header->minor;
  170. snprintf(bus->name, CHAMELEON_FILENAME_LEN + 1, "%s",
  171. header->filename);
  172. bar_count = chameleon_get_bar(&p, mapbase, &cb);
  173. if (bar_count < 0)
  174. goto free_header;
  175. for_each_chameleon_cell(dtype, p) {
  176. switch (dtype) {
  177. case CHAMELEON_DTYPE_GENERAL:
  178. ret = chameleon_parse_gdd(bus, cb, p, bar_count);
  179. if (ret < 0)
  180. goto free_bar;
  181. p += sizeof(struct chameleon_gdd);
  182. break;
  183. case CHAMELEON_DTYPE_BRIDGE:
  184. chameleon_parse_bdd(bus, cb, p);
  185. p += sizeof(struct chameleon_bdd);
  186. break;
  187. case CHAMELEON_DTYPE_END:
  188. break;
  189. default:
  190. pr_err("Invalid chameleon descriptor type 0x%x\n",
  191. dtype);
  192. ret = -EINVAL;
  193. goto free_bar;
  194. }
  195. num_cells++;
  196. }
  197. if (num_cells == 0)
  198. num_cells = -EINVAL;
  199. kfree(cb);
  200. kfree(header);
  201. return num_cells;
  202. free_bar:
  203. kfree(cb);
  204. free_header:
  205. kfree(header);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(chameleon_parse_cells);