mpipe.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. /*
  15. * Implementation of mpipe gxio calls.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <gxio/iorpc_globals.h>
  21. #include <gxio/iorpc_mpipe.h>
  22. #include <gxio/iorpc_mpipe_info.h>
  23. #include <gxio/kiorpc.h>
  24. #include <gxio/mpipe.h>
  25. /* HACK: Avoid pointless "shadow" warnings. */
  26. #define link link_shadow
  27. int gxio_mpipe_init(gxio_mpipe_context_t *context, unsigned int mpipe_index)
  28. {
  29. char file[32];
  30. int fd;
  31. int i;
  32. snprintf(file, sizeof(file), "mpipe/%d/iorpc", mpipe_index);
  33. fd = hv_dev_open((HV_VirtAddr) file, 0);
  34. if (fd < 0) {
  35. if (fd >= GXIO_ERR_MIN && fd <= GXIO_ERR_MAX)
  36. return fd;
  37. else
  38. return -ENODEV;
  39. }
  40. context->fd = fd;
  41. /* Map in the MMIO space. */
  42. context->mmio_cfg_base = (void __force *)
  43. iorpc_ioremap(fd, HV_MPIPE_CONFIG_MMIO_OFFSET,
  44. HV_MPIPE_CONFIG_MMIO_SIZE);
  45. if (context->mmio_cfg_base == NULL)
  46. goto cfg_failed;
  47. context->mmio_fast_base = (void __force *)
  48. iorpc_ioremap(fd, HV_MPIPE_FAST_MMIO_OFFSET,
  49. HV_MPIPE_FAST_MMIO_SIZE);
  50. if (context->mmio_fast_base == NULL)
  51. goto fast_failed;
  52. /* Initialize the stacks. */
  53. for (i = 0; i < 8; i++)
  54. context->__stacks.stacks[i] = 255;
  55. return 0;
  56. fast_failed:
  57. iounmap((void __force __iomem *)(context->mmio_cfg_base));
  58. cfg_failed:
  59. hv_dev_close(context->fd);
  60. return -ENODEV;
  61. }
  62. EXPORT_SYMBOL_GPL(gxio_mpipe_init);
  63. int gxio_mpipe_destroy(gxio_mpipe_context_t *context)
  64. {
  65. iounmap((void __force __iomem *)(context->mmio_cfg_base));
  66. iounmap((void __force __iomem *)(context->mmio_fast_base));
  67. return hv_dev_close(context->fd);
  68. }
  69. EXPORT_SYMBOL_GPL(gxio_mpipe_destroy);
  70. static int16_t gxio_mpipe_buffer_sizes[8] =
  71. { 128, 256, 512, 1024, 1664, 4096, 10368, 16384 };
  72. gxio_mpipe_buffer_size_enum_t gxio_mpipe_buffer_size_to_buffer_size_enum(size_t
  73. size)
  74. {
  75. int i;
  76. for (i = 0; i < 7; i++)
  77. if (size <= gxio_mpipe_buffer_sizes[i])
  78. break;
  79. return i;
  80. }
  81. EXPORT_SYMBOL_GPL(gxio_mpipe_buffer_size_to_buffer_size_enum);
  82. size_t gxio_mpipe_buffer_size_enum_to_buffer_size(gxio_mpipe_buffer_size_enum_t
  83. buffer_size_enum)
  84. {
  85. if (buffer_size_enum > 7)
  86. buffer_size_enum = 7;
  87. return gxio_mpipe_buffer_sizes[buffer_size_enum];
  88. }
  89. EXPORT_SYMBOL_GPL(gxio_mpipe_buffer_size_enum_to_buffer_size);
  90. size_t gxio_mpipe_calc_buffer_stack_bytes(unsigned long buffers)
  91. {
  92. const int BUFFERS_PER_LINE = 12;
  93. /* Count the number of cachlines. */
  94. unsigned long lines =
  95. (buffers + BUFFERS_PER_LINE - 1) / BUFFERS_PER_LINE;
  96. /* Convert to bytes. */
  97. return lines * CHIP_L2_LINE_SIZE();
  98. }
  99. EXPORT_SYMBOL_GPL(gxio_mpipe_calc_buffer_stack_bytes);
  100. int gxio_mpipe_init_buffer_stack(gxio_mpipe_context_t *context,
  101. unsigned int stack,
  102. gxio_mpipe_buffer_size_enum_t
  103. buffer_size_enum, void *mem, size_t mem_size,
  104. unsigned int mem_flags)
  105. {
  106. int result;
  107. memset(mem, 0, mem_size);
  108. result = gxio_mpipe_init_buffer_stack_aux(context, mem, mem_size,
  109. mem_flags, stack,
  110. buffer_size_enum);
  111. if (result < 0)
  112. return result;
  113. /* Save the stack. */
  114. context->__stacks.stacks[buffer_size_enum] = stack;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(gxio_mpipe_init_buffer_stack);
  118. int gxio_mpipe_init_notif_ring(gxio_mpipe_context_t *context,
  119. unsigned int ring,
  120. void *mem, size_t mem_size,
  121. unsigned int mem_flags)
  122. {
  123. return gxio_mpipe_init_notif_ring_aux(context, mem, mem_size,
  124. mem_flags, ring);
  125. }
  126. EXPORT_SYMBOL_GPL(gxio_mpipe_init_notif_ring);
  127. int gxio_mpipe_init_notif_group_and_buckets(gxio_mpipe_context_t *context,
  128. unsigned int group,
  129. unsigned int ring,
  130. unsigned int num_rings,
  131. unsigned int bucket,
  132. unsigned int num_buckets,
  133. gxio_mpipe_bucket_mode_t mode)
  134. {
  135. int i;
  136. int result;
  137. gxio_mpipe_bucket_info_t bucket_info = { {
  138. .group = group,
  139. .mode = mode,
  140. }
  141. };
  142. gxio_mpipe_notif_group_bits_t bits = { {0} };
  143. for (i = 0; i < num_rings; i++)
  144. gxio_mpipe_notif_group_add_ring(&bits, ring + i);
  145. result = gxio_mpipe_init_notif_group(context, group, bits);
  146. if (result != 0)
  147. return result;
  148. for (i = 0; i < num_buckets; i++) {
  149. bucket_info.notifring = ring + (i % num_rings);
  150. result = gxio_mpipe_init_bucket(context, bucket + i,
  151. bucket_info);
  152. if (result != 0)
  153. return result;
  154. }
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(gxio_mpipe_init_notif_group_and_buckets);
  158. int gxio_mpipe_init_edma_ring(gxio_mpipe_context_t *context,
  159. unsigned int ring, unsigned int channel,
  160. void *mem, size_t mem_size,
  161. unsigned int mem_flags)
  162. {
  163. memset(mem, 0, mem_size);
  164. return gxio_mpipe_init_edma_ring_aux(context, mem, mem_size, mem_flags,
  165. ring, channel);
  166. }
  167. EXPORT_SYMBOL_GPL(gxio_mpipe_init_edma_ring);
  168. void gxio_mpipe_rules_init(gxio_mpipe_rules_t *rules,
  169. gxio_mpipe_context_t *context)
  170. {
  171. rules->context = context;
  172. memset(&rules->list, 0, sizeof(rules->list));
  173. }
  174. EXPORT_SYMBOL_GPL(gxio_mpipe_rules_init);
  175. int gxio_mpipe_rules_begin(gxio_mpipe_rules_t *rules,
  176. unsigned int bucket, unsigned int num_buckets,
  177. gxio_mpipe_rules_stacks_t *stacks)
  178. {
  179. int i;
  180. int stack = 255;
  181. gxio_mpipe_rules_list_t *list = &rules->list;
  182. /* Current rule. */
  183. gxio_mpipe_rules_rule_t *rule =
  184. (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
  185. unsigned int head = list->tail;
  186. /*
  187. * Align next rule properly.
  188. *Note that "dmacs_and_vlans" will also be aligned.
  189. */
  190. unsigned int pad = 0;
  191. while (((head + pad) % __alignof__(gxio_mpipe_rules_rule_t)) != 0)
  192. pad++;
  193. /*
  194. * Verify room.
  195. * ISSUE: Mark rules as broken on error?
  196. */
  197. if (head + pad + sizeof(*rule) >= sizeof(list->rules))
  198. return GXIO_MPIPE_ERR_RULES_FULL;
  199. /* Verify num_buckets is a power of 2. */
  200. if (__builtin_popcount(num_buckets) != 1)
  201. return GXIO_MPIPE_ERR_RULES_INVALID;
  202. /* Add padding to previous rule. */
  203. rule->size += pad;
  204. /* Start a new rule. */
  205. list->head = head + pad;
  206. rule = (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
  207. /* Default some values. */
  208. rule->headroom = 2;
  209. rule->tailroom = 0;
  210. rule->capacity = 16384;
  211. /* Save the bucket info. */
  212. rule->bucket_mask = num_buckets - 1;
  213. rule->bucket_first = bucket;
  214. for (i = 8 - 1; i >= 0; i--) {
  215. int maybe =
  216. stacks ? stacks->stacks[i] : rules->context->__stacks.
  217. stacks[i];
  218. if (maybe != 255)
  219. stack = maybe;
  220. rule->stacks.stacks[i] = stack;
  221. }
  222. if (stack == 255)
  223. return GXIO_MPIPE_ERR_RULES_INVALID;
  224. /* NOTE: Only entries at the end of the array can be 255. */
  225. for (i = 8 - 1; i > 0; i--) {
  226. if (rule->stacks.stacks[i] == 255) {
  227. rule->stacks.stacks[i] = stack;
  228. rule->capacity =
  229. gxio_mpipe_buffer_size_enum_to_buffer_size(i -
  230. 1);
  231. }
  232. }
  233. rule->size = sizeof(*rule);
  234. list->tail = list->head + rule->size;
  235. return 0;
  236. }
  237. EXPORT_SYMBOL_GPL(gxio_mpipe_rules_begin);
  238. int gxio_mpipe_rules_add_channel(gxio_mpipe_rules_t *rules,
  239. unsigned int channel)
  240. {
  241. gxio_mpipe_rules_list_t *list = &rules->list;
  242. gxio_mpipe_rules_rule_t *rule =
  243. (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
  244. /* Verify channel. */
  245. if (channel >= 32)
  246. return GXIO_MPIPE_ERR_RULES_INVALID;
  247. /* Verify begun. */
  248. if (list->tail == 0)
  249. return GXIO_MPIPE_ERR_RULES_EMPTY;
  250. rule->channel_bits |= (1UL << channel);
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(gxio_mpipe_rules_add_channel);
  254. int gxio_mpipe_rules_set_headroom(gxio_mpipe_rules_t *rules, uint8_t headroom)
  255. {
  256. gxio_mpipe_rules_list_t *list = &rules->list;
  257. gxio_mpipe_rules_rule_t *rule =
  258. (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
  259. /* Verify begun. */
  260. if (list->tail == 0)
  261. return GXIO_MPIPE_ERR_RULES_EMPTY;
  262. rule->headroom = headroom;
  263. return 0;
  264. }
  265. EXPORT_SYMBOL_GPL(gxio_mpipe_rules_set_headroom);
  266. int gxio_mpipe_rules_commit(gxio_mpipe_rules_t *rules)
  267. {
  268. gxio_mpipe_rules_list_t *list = &rules->list;
  269. unsigned int size =
  270. offsetof(gxio_mpipe_rules_list_t, rules) + list->tail;
  271. return gxio_mpipe_commit_rules(rules->context, list, size);
  272. }
  273. EXPORT_SYMBOL_GPL(gxio_mpipe_rules_commit);
  274. int gxio_mpipe_iqueue_init(gxio_mpipe_iqueue_t *iqueue,
  275. gxio_mpipe_context_t *context,
  276. unsigned int ring,
  277. void *mem, size_t mem_size, unsigned int mem_flags)
  278. {
  279. /* The init call below will verify that "mem_size" is legal. */
  280. unsigned int num_entries = mem_size / sizeof(gxio_mpipe_idesc_t);
  281. iqueue->context = context;
  282. iqueue->idescs = (gxio_mpipe_idesc_t *)mem;
  283. iqueue->ring = ring;
  284. iqueue->num_entries = num_entries;
  285. iqueue->mask_num_entries = num_entries - 1;
  286. iqueue->log2_num_entries = __builtin_ctz(num_entries);
  287. iqueue->head = 1;
  288. #ifdef __BIG_ENDIAN__
  289. iqueue->swapped = 0;
  290. #endif
  291. /* Initialize the "tail". */
  292. __gxio_mmio_write(mem, iqueue->head);
  293. return gxio_mpipe_init_notif_ring(context, ring, mem, mem_size,
  294. mem_flags);
  295. }
  296. EXPORT_SYMBOL_GPL(gxio_mpipe_iqueue_init);
  297. int gxio_mpipe_equeue_init(gxio_mpipe_equeue_t *equeue,
  298. gxio_mpipe_context_t *context,
  299. unsigned int edma_ring_id,
  300. unsigned int channel,
  301. void *mem, unsigned int mem_size,
  302. unsigned int mem_flags)
  303. {
  304. /* The init call below will verify that "mem_size" is legal. */
  305. unsigned int num_entries = mem_size / sizeof(gxio_mpipe_edesc_t);
  306. /* Offset used to read number of completed commands. */
  307. MPIPE_EDMA_POST_REGION_ADDR_t offset;
  308. int result = gxio_mpipe_init_edma_ring(context, edma_ring_id, channel,
  309. mem, mem_size, mem_flags);
  310. if (result < 0)
  311. return result;
  312. memset(equeue, 0, sizeof(*equeue));
  313. offset.word = 0;
  314. offset.region =
  315. MPIPE_MMIO_ADDR__REGION_VAL_EDMA -
  316. MPIPE_MMIO_ADDR__REGION_VAL_IDMA;
  317. offset.ring = edma_ring_id;
  318. __gxio_dma_queue_init(&equeue->dma_queue,
  319. context->mmio_fast_base + offset.word,
  320. num_entries);
  321. equeue->edescs = mem;
  322. equeue->mask_num_entries = num_entries - 1;
  323. equeue->log2_num_entries = __builtin_ctz(num_entries);
  324. return 0;
  325. }
  326. EXPORT_SYMBOL_GPL(gxio_mpipe_equeue_init);
  327. int gxio_mpipe_set_timestamp(gxio_mpipe_context_t *context,
  328. const struct timespec *ts)
  329. {
  330. cycles_t cycles = get_cycles();
  331. return gxio_mpipe_set_timestamp_aux(context, (uint64_t)ts->tv_sec,
  332. (uint64_t)ts->tv_nsec,
  333. (uint64_t)cycles);
  334. }
  335. int gxio_mpipe_get_timestamp(gxio_mpipe_context_t *context,
  336. struct timespec *ts)
  337. {
  338. int ret;
  339. cycles_t cycles_prev, cycles_now, clock_rate;
  340. cycles_prev = get_cycles();
  341. ret = gxio_mpipe_get_timestamp_aux(context, (uint64_t *)&ts->tv_sec,
  342. (uint64_t *)&ts->tv_nsec,
  343. (uint64_t *)&cycles_now);
  344. if (ret < 0) {
  345. return ret;
  346. }
  347. clock_rate = get_clock_rate();
  348. ts->tv_nsec -= (cycles_now - cycles_prev) * 1000000000LL / clock_rate;
  349. if (ts->tv_nsec < 0) {
  350. ts->tv_nsec += 1000000000LL;
  351. ts->tv_sec -= 1;
  352. }
  353. return ret;
  354. }
  355. int gxio_mpipe_adjust_timestamp(gxio_mpipe_context_t *context, int64_t delta)
  356. {
  357. return gxio_mpipe_adjust_timestamp_aux(context, delta);
  358. }
  359. /* Get our internal context used for link name access. This context is
  360. * special in that it is not associated with an mPIPE service domain.
  361. */
  362. static gxio_mpipe_context_t *_gxio_get_link_context(void)
  363. {
  364. static gxio_mpipe_context_t context;
  365. static gxio_mpipe_context_t *contextp;
  366. static int tried_open = 0;
  367. static DEFINE_MUTEX(mutex);
  368. mutex_lock(&mutex);
  369. if (!tried_open) {
  370. int i = 0;
  371. tried_open = 1;
  372. /*
  373. * "4" here is the maximum possible number of mPIPE shims; it's
  374. * an exaggeration but we shouldn't ever go beyond 2 anyway.
  375. */
  376. for (i = 0; i < 4; i++) {
  377. char file[80];
  378. snprintf(file, sizeof(file), "mpipe/%d/iorpc_info", i);
  379. context.fd = hv_dev_open((HV_VirtAddr) file, 0);
  380. if (context.fd < 0)
  381. continue;
  382. contextp = &context;
  383. break;
  384. }
  385. }
  386. mutex_unlock(&mutex);
  387. return contextp;
  388. }
  389. int gxio_mpipe_link_enumerate_mac(int idx, char *link_name, uint8_t *link_mac)
  390. {
  391. int rv;
  392. _gxio_mpipe_link_name_t name;
  393. _gxio_mpipe_link_mac_t mac;
  394. gxio_mpipe_context_t *context = _gxio_get_link_context();
  395. if (!context)
  396. return GXIO_ERR_NO_DEVICE;
  397. rv = gxio_mpipe_info_enumerate_aux(context, idx, &name, &mac);
  398. if (rv >= 0) {
  399. strncpy(link_name, name.name, sizeof(name.name));
  400. memcpy(link_mac, mac.mac, sizeof(mac.mac));
  401. }
  402. return rv;
  403. }
  404. EXPORT_SYMBOL_GPL(gxio_mpipe_link_enumerate_mac);
  405. int gxio_mpipe_link_open(gxio_mpipe_link_t *link,
  406. gxio_mpipe_context_t *context, const char *link_name,
  407. unsigned int flags)
  408. {
  409. _gxio_mpipe_link_name_t name;
  410. int rv;
  411. strncpy(name.name, link_name, sizeof(name.name));
  412. name.name[GXIO_MPIPE_LINK_NAME_LEN - 1] = '\0';
  413. rv = gxio_mpipe_link_open_aux(context, name, flags);
  414. if (rv < 0)
  415. return rv;
  416. link->context = context;
  417. link->channel = rv >> 8;
  418. link->mac = rv & 0xFF;
  419. return 0;
  420. }
  421. EXPORT_SYMBOL_GPL(gxio_mpipe_link_open);
  422. int gxio_mpipe_link_close(gxio_mpipe_link_t *link)
  423. {
  424. return gxio_mpipe_link_close_aux(link->context, link->mac);
  425. }
  426. EXPORT_SYMBOL_GPL(gxio_mpipe_link_close);