mpipe.c 15 KB

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