mpipe.c 14 KB

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