mpipe.c 14 KB

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