2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
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.
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
16 * Implementation of mpipe gxio calls.
19 #include <linux/errno.h>
21 #include <linux/module.h>
23 #include <gxio/iorpc_globals.h>
24 #include <gxio/iorpc_mpipe.h>
25 #include <gxio/iorpc_mpipe_info.h>
26 #include <gxio/kiorpc.h>
27 #include <gxio/mpipe.h>
29 /* HACK: Avoid pointless "shadow" warnings. */
30 #define link link_shadow
32 int gxio_mpipe_init(gxio_mpipe_context_t *context, unsigned int mpipe_index)
39 if (mpipe_index >= GXIO_MPIPE_INSTANCE_MAX)
42 snprintf(file, sizeof(file), "mpipe/%d/iorpc", mpipe_index);
43 fd = hv_dev_open((HV_VirtAddr) file, 0);
48 if (fd >= GXIO_ERR_MIN && fd <= GXIO_ERR_MAX)
54 /* Map in the MMIO space. */
55 context->mmio_cfg_base = (void __force *)
56 iorpc_ioremap(fd, HV_MPIPE_CONFIG_MMIO_OFFSET,
57 HV_MPIPE_CONFIG_MMIO_SIZE);
58 if (context->mmio_cfg_base == NULL)
61 context->mmio_fast_base = (void __force *)
62 iorpc_ioremap(fd, HV_MPIPE_FAST_MMIO_OFFSET,
63 HV_MPIPE_FAST_MMIO_SIZE);
64 if (context->mmio_fast_base == NULL)
67 /* Initialize the stacks. */
68 for (i = 0; i < 8; i++)
69 context->__stacks.stacks[i] = 255;
71 context->instance = mpipe_index;
76 iounmap((void __force __iomem *)(context->mmio_cfg_base));
78 hv_dev_close(context->fd);
83 EXPORT_SYMBOL_GPL(gxio_mpipe_init);
85 int gxio_mpipe_destroy(gxio_mpipe_context_t *context)
87 iounmap((void __force __iomem *)(context->mmio_cfg_base));
88 iounmap((void __force __iomem *)(context->mmio_fast_base));
89 return hv_dev_close(context->fd);
92 EXPORT_SYMBOL_GPL(gxio_mpipe_destroy);
94 static int16_t gxio_mpipe_buffer_sizes[8] =
95 { 128, 256, 512, 1024, 1664, 4096, 10368, 16384 };
97 gxio_mpipe_buffer_size_enum_t gxio_mpipe_buffer_size_to_buffer_size_enum(size_t
101 for (i = 0; i < 7; i++)
102 if (size <= gxio_mpipe_buffer_sizes[i])
107 EXPORT_SYMBOL_GPL(gxio_mpipe_buffer_size_to_buffer_size_enum);
109 size_t gxio_mpipe_buffer_size_enum_to_buffer_size(gxio_mpipe_buffer_size_enum_t
112 if (buffer_size_enum > 7)
113 buffer_size_enum = 7;
115 return gxio_mpipe_buffer_sizes[buffer_size_enum];
118 EXPORT_SYMBOL_GPL(gxio_mpipe_buffer_size_enum_to_buffer_size);
120 size_t gxio_mpipe_calc_buffer_stack_bytes(unsigned long buffers)
122 const int BUFFERS_PER_LINE = 12;
124 /* Count the number of cachlines. */
125 unsigned long lines =
126 (buffers + BUFFERS_PER_LINE - 1) / BUFFERS_PER_LINE;
128 /* Convert to bytes. */
129 return lines * CHIP_L2_LINE_SIZE();
132 EXPORT_SYMBOL_GPL(gxio_mpipe_calc_buffer_stack_bytes);
134 int gxio_mpipe_init_buffer_stack(gxio_mpipe_context_t *context,
136 gxio_mpipe_buffer_size_enum_t
137 buffer_size_enum, void *mem, size_t mem_size,
138 unsigned int mem_flags)
142 memset(mem, 0, mem_size);
144 result = gxio_mpipe_init_buffer_stack_aux(context, mem, mem_size,
150 /* Save the stack. */
151 context->__stacks.stacks[buffer_size_enum] = stack;
156 EXPORT_SYMBOL_GPL(gxio_mpipe_init_buffer_stack);
158 int gxio_mpipe_init_notif_ring(gxio_mpipe_context_t *context,
160 void *mem, size_t mem_size,
161 unsigned int mem_flags)
163 return gxio_mpipe_init_notif_ring_aux(context, mem, mem_size,
167 EXPORT_SYMBOL_GPL(gxio_mpipe_init_notif_ring);
169 int gxio_mpipe_init_notif_group_and_buckets(gxio_mpipe_context_t *context,
172 unsigned int num_rings,
174 unsigned int num_buckets,
175 gxio_mpipe_bucket_mode_t mode)
180 gxio_mpipe_bucket_info_t bucket_info = { {
186 gxio_mpipe_notif_group_bits_t bits = { {0} };
188 for (i = 0; i < num_rings; i++)
189 gxio_mpipe_notif_group_add_ring(&bits, ring + i);
191 result = gxio_mpipe_init_notif_group(context, group, bits);
195 for (i = 0; i < num_buckets; i++) {
196 bucket_info.notifring = ring + (i % num_rings);
198 result = gxio_mpipe_init_bucket(context, bucket + i,
207 EXPORT_SYMBOL_GPL(gxio_mpipe_init_notif_group_and_buckets);
209 int gxio_mpipe_init_edma_ring(gxio_mpipe_context_t *context,
210 unsigned int ring, unsigned int channel,
211 void *mem, size_t mem_size,
212 unsigned int mem_flags)
214 memset(mem, 0, mem_size);
216 return gxio_mpipe_init_edma_ring_aux(context, mem, mem_size, mem_flags,
220 EXPORT_SYMBOL_GPL(gxio_mpipe_init_edma_ring);
222 void gxio_mpipe_rules_init(gxio_mpipe_rules_t *rules,
223 gxio_mpipe_context_t *context)
225 rules->context = context;
226 memset(&rules->list, 0, sizeof(rules->list));
229 EXPORT_SYMBOL_GPL(gxio_mpipe_rules_init);
231 int gxio_mpipe_rules_begin(gxio_mpipe_rules_t *rules,
232 unsigned int bucket, unsigned int num_buckets,
233 gxio_mpipe_rules_stacks_t *stacks)
238 gxio_mpipe_rules_list_t *list = &rules->list;
241 gxio_mpipe_rules_rule_t *rule =
242 (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
244 unsigned int head = list->tail;
247 * Align next rule properly.
248 *Note that "dmacs_and_vlans" will also be aligned.
250 unsigned int pad = 0;
251 while (((head + pad) % __alignof__(gxio_mpipe_rules_rule_t)) != 0)
256 * ISSUE: Mark rules as broken on error?
258 if (head + pad + sizeof(*rule) >= sizeof(list->rules))
259 return GXIO_MPIPE_ERR_RULES_FULL;
261 /* Verify num_buckets is a power of 2. */
262 if (__builtin_popcount(num_buckets) != 1)
263 return GXIO_MPIPE_ERR_RULES_INVALID;
265 /* Add padding to previous rule. */
268 /* Start a new rule. */
269 list->head = head + pad;
271 rule = (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
273 /* Default some values. */
276 rule->capacity = 16384;
278 /* Save the bucket info. */
279 rule->bucket_mask = num_buckets - 1;
280 rule->bucket_first = bucket;
282 for (i = 8 - 1; i >= 0; i--) {
284 stacks ? stacks->stacks[i] : rules->context->__stacks.
288 rule->stacks.stacks[i] = stack;
292 return GXIO_MPIPE_ERR_RULES_INVALID;
294 /* NOTE: Only entries at the end of the array can be 255. */
295 for (i = 8 - 1; i > 0; i--) {
296 if (rule->stacks.stacks[i] == 255) {
297 rule->stacks.stacks[i] = stack;
299 gxio_mpipe_buffer_size_enum_to_buffer_size(i -
304 rule->size = sizeof(*rule);
305 list->tail = list->head + rule->size;
310 EXPORT_SYMBOL_GPL(gxio_mpipe_rules_begin);
312 int gxio_mpipe_rules_add_channel(gxio_mpipe_rules_t *rules,
313 unsigned int channel)
315 gxio_mpipe_rules_list_t *list = &rules->list;
317 gxio_mpipe_rules_rule_t *rule =
318 (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
320 /* Verify channel. */
322 return GXIO_MPIPE_ERR_RULES_INVALID;
326 return GXIO_MPIPE_ERR_RULES_EMPTY;
328 rule->channel_bits |= (1UL << channel);
333 EXPORT_SYMBOL_GPL(gxio_mpipe_rules_add_channel);
335 int gxio_mpipe_rules_set_headroom(gxio_mpipe_rules_t *rules, uint8_t headroom)
337 gxio_mpipe_rules_list_t *list = &rules->list;
339 gxio_mpipe_rules_rule_t *rule =
340 (gxio_mpipe_rules_rule_t *) (list->rules + list->head);
344 return GXIO_MPIPE_ERR_RULES_EMPTY;
346 rule->headroom = headroom;
351 EXPORT_SYMBOL_GPL(gxio_mpipe_rules_set_headroom);
353 int gxio_mpipe_rules_commit(gxio_mpipe_rules_t *rules)
355 gxio_mpipe_rules_list_t *list = &rules->list;
357 offsetof(gxio_mpipe_rules_list_t, rules) + list->tail;
358 return gxio_mpipe_commit_rules(rules->context, list, size);
361 EXPORT_SYMBOL_GPL(gxio_mpipe_rules_commit);
363 int gxio_mpipe_iqueue_init(gxio_mpipe_iqueue_t *iqueue,
364 gxio_mpipe_context_t *context,
366 void *mem, size_t mem_size, unsigned int mem_flags)
368 /* The init call below will verify that "mem_size" is legal. */
369 unsigned int num_entries = mem_size / sizeof(gxio_mpipe_idesc_t);
371 iqueue->context = context;
372 iqueue->idescs = (gxio_mpipe_idesc_t *)mem;
374 iqueue->num_entries = num_entries;
375 iqueue->mask_num_entries = num_entries - 1;
376 iqueue->log2_num_entries = __builtin_ctz(num_entries);
378 #ifdef __BIG_ENDIAN__
382 /* Initialize the "tail". */
383 __gxio_mmio_write(mem, iqueue->head);
385 return gxio_mpipe_init_notif_ring(context, ring, mem, mem_size,
389 EXPORT_SYMBOL_GPL(gxio_mpipe_iqueue_init);
391 int gxio_mpipe_equeue_init(gxio_mpipe_equeue_t *equeue,
392 gxio_mpipe_context_t *context,
394 unsigned int channel,
395 void *mem, unsigned int mem_size,
396 unsigned int mem_flags)
398 /* The init call below will verify that "mem_size" is legal. */
399 unsigned int num_entries = mem_size / sizeof(gxio_mpipe_edesc_t);
401 /* Offset used to read number of completed commands. */
402 MPIPE_EDMA_POST_REGION_ADDR_t offset;
404 int result = gxio_mpipe_init_edma_ring(context, ering, channel,
405 mem, mem_size, mem_flags);
409 memset(equeue, 0, sizeof(*equeue));
413 MPIPE_MMIO_ADDR__REGION_VAL_EDMA -
414 MPIPE_MMIO_ADDR__REGION_VAL_IDMA;
417 __gxio_dma_queue_init(&equeue->dma_queue,
418 context->mmio_fast_base + offset.word,
420 equeue->edescs = mem;
421 equeue->mask_num_entries = num_entries - 1;
422 equeue->log2_num_entries = __builtin_ctz(num_entries);
423 equeue->context = context;
424 equeue->ering = ering;
425 equeue->channel = channel;
430 EXPORT_SYMBOL_GPL(gxio_mpipe_equeue_init);
432 int gxio_mpipe_set_timestamp(gxio_mpipe_context_t *context,
433 const struct timespec *ts)
435 cycles_t cycles = get_cycles();
436 return gxio_mpipe_set_timestamp_aux(context, (uint64_t)ts->tv_sec,
437 (uint64_t)ts->tv_nsec,
441 int gxio_mpipe_get_timestamp(gxio_mpipe_context_t *context,
445 cycles_t cycles_prev, cycles_now, clock_rate;
446 cycles_prev = get_cycles();
447 ret = gxio_mpipe_get_timestamp_aux(context, (uint64_t *)&ts->tv_sec,
448 (uint64_t *)&ts->tv_nsec,
449 (uint64_t *)&cycles_now);
454 clock_rate = get_clock_rate();
455 ts->tv_nsec -= (cycles_now - cycles_prev) * 1000000000LL / clock_rate;
456 if (ts->tv_nsec < 0) {
457 ts->tv_nsec += 1000000000LL;
463 int gxio_mpipe_adjust_timestamp(gxio_mpipe_context_t *context, int64_t delta)
465 return gxio_mpipe_adjust_timestamp_aux(context, delta);
468 /* Get our internal context used for link name access. This context is
469 * special in that it is not associated with an mPIPE service domain.
471 static gxio_mpipe_context_t *_gxio_get_link_context(void)
473 static gxio_mpipe_context_t context;
474 static gxio_mpipe_context_t *contextp;
475 static int tried_open = 0;
476 static DEFINE_MUTEX(mutex);
485 * "4" here is the maximum possible number of mPIPE shims; it's
486 * an exaggeration but we shouldn't ever go beyond 2 anyway.
488 for (i = 0; i < 4; i++) {
491 snprintf(file, sizeof(file), "mpipe/%d/iorpc_info", i);
492 context.fd = hv_dev_open((HV_VirtAddr) file, 0);
501 mutex_unlock(&mutex);
506 int gxio_mpipe_link_instance(const char *link_name)
508 _gxio_mpipe_link_name_t name;
509 gxio_mpipe_context_t *context = _gxio_get_link_context();
512 return GXIO_ERR_NO_DEVICE;
514 strncpy(name.name, link_name, sizeof(name.name));
515 name.name[GXIO_MPIPE_LINK_NAME_LEN - 1] = '\0';
517 return gxio_mpipe_info_instance_aux(context, name);
520 int gxio_mpipe_link_enumerate_mac(int idx, char *link_name, uint8_t *link_mac)
523 _gxio_mpipe_link_name_t name;
524 _gxio_mpipe_link_mac_t mac;
526 gxio_mpipe_context_t *context = _gxio_get_link_context();
528 return GXIO_ERR_NO_DEVICE;
530 rv = gxio_mpipe_info_enumerate_aux(context, idx, &name, &mac);
532 strncpy(link_name, name.name, sizeof(name.name));
533 memcpy(link_mac, mac.mac, sizeof(mac.mac));
539 EXPORT_SYMBOL_GPL(gxio_mpipe_link_enumerate_mac);
541 int gxio_mpipe_link_open(gxio_mpipe_link_t *link,
542 gxio_mpipe_context_t *context, const char *link_name,
545 _gxio_mpipe_link_name_t name;
548 strncpy(name.name, link_name, sizeof(name.name));
549 name.name[GXIO_MPIPE_LINK_NAME_LEN - 1] = '\0';
551 rv = gxio_mpipe_link_open_aux(context, name, flags);
555 link->context = context;
556 link->channel = rv >> 8;
557 link->mac = rv & 0xFF;
562 EXPORT_SYMBOL_GPL(gxio_mpipe_link_open);
564 int gxio_mpipe_link_close(gxio_mpipe_link_t *link)
566 return gxio_mpipe_link_close_aux(link->context, link->mac);
569 EXPORT_SYMBOL_GPL(gxio_mpipe_link_close);
571 int gxio_mpipe_link_set_attr(gxio_mpipe_link_t *link, uint32_t attr,
574 return gxio_mpipe_link_set_attr_aux(link->context, link->mac, attr,
578 EXPORT_SYMBOL_GPL(gxio_mpipe_link_set_attr);