2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include "hns_roce_device.h"
35 #include "hns_roce_hem.h"
36 #include "hns_roce_common.h"
38 #define HEM_INDEX_BUF BIT(0)
39 #define HEM_INDEX_L0 BIT(1)
40 #define HEM_INDEX_L1 BIT(2)
41 struct hns_roce_hem_index {
45 u32 inited; /* indicate which index is available */
48 bool hns_roce_check_whether_mhop(struct hns_roce_dev *hr_dev, u32 type)
54 hop_num = hr_dev->caps.qpc_hop_num;
57 hop_num = hr_dev->caps.mpt_hop_num;
60 hop_num = hr_dev->caps.cqc_hop_num;
63 hop_num = hr_dev->caps.srqc_hop_num;
66 hop_num = hr_dev->caps.sccc_hop_num;
68 case HEM_TYPE_QPC_TIMER:
69 hop_num = hr_dev->caps.qpc_timer_hop_num;
71 case HEM_TYPE_CQC_TIMER:
72 hop_num = hr_dev->caps.cqc_timer_hop_num;
75 hop_num = hr_dev->caps.gmv_hop_num;
81 return hop_num ? true : false;
84 static bool hns_roce_check_hem_null(struct hns_roce_hem **hem, u64 hem_idx,
85 u32 bt_chunk_num, u64 hem_max_num)
87 u64 start_idx = round_down(hem_idx, bt_chunk_num);
88 u64 check_max_num = start_idx + bt_chunk_num;
91 for (i = start_idx; (i < check_max_num) && (i < hem_max_num); i++)
92 if (i != hem_idx && hem[i])
98 static bool hns_roce_check_bt_null(u64 **bt, u64 ba_idx, u32 bt_chunk_num)
100 u64 start_idx = round_down(ba_idx, bt_chunk_num);
103 for (i = 0; i < bt_chunk_num; i++)
104 if (i != ba_idx && bt[start_idx + i])
110 static int hns_roce_get_bt_num(u32 table_type, u32 hop_num)
112 if (check_whether_bt_num_3(table_type, hop_num))
114 else if (check_whether_bt_num_2(table_type, hop_num))
116 else if (check_whether_bt_num_1(table_type, hop_num))
122 static int get_hem_table_config(struct hns_roce_dev *hr_dev,
123 struct hns_roce_hem_mhop *mhop,
126 struct device *dev = hr_dev->dev;
130 mhop->buf_chunk_size = 1 << (hr_dev->caps.qpc_buf_pg_sz
132 mhop->bt_chunk_size = 1 << (hr_dev->caps.qpc_ba_pg_sz
134 mhop->ba_l0_num = hr_dev->caps.qpc_bt_num;
135 mhop->hop_num = hr_dev->caps.qpc_hop_num;
138 mhop->buf_chunk_size = 1 << (hr_dev->caps.mpt_buf_pg_sz
140 mhop->bt_chunk_size = 1 << (hr_dev->caps.mpt_ba_pg_sz
142 mhop->ba_l0_num = hr_dev->caps.mpt_bt_num;
143 mhop->hop_num = hr_dev->caps.mpt_hop_num;
146 mhop->buf_chunk_size = 1 << (hr_dev->caps.cqc_buf_pg_sz
148 mhop->bt_chunk_size = 1 << (hr_dev->caps.cqc_ba_pg_sz
150 mhop->ba_l0_num = hr_dev->caps.cqc_bt_num;
151 mhop->hop_num = hr_dev->caps.cqc_hop_num;
154 mhop->buf_chunk_size = 1 << (hr_dev->caps.sccc_buf_pg_sz
156 mhop->bt_chunk_size = 1 << (hr_dev->caps.sccc_ba_pg_sz
158 mhop->ba_l0_num = hr_dev->caps.sccc_bt_num;
159 mhop->hop_num = hr_dev->caps.sccc_hop_num;
161 case HEM_TYPE_QPC_TIMER:
162 mhop->buf_chunk_size = 1 << (hr_dev->caps.qpc_timer_buf_pg_sz
164 mhop->bt_chunk_size = 1 << (hr_dev->caps.qpc_timer_ba_pg_sz
166 mhop->ba_l0_num = hr_dev->caps.qpc_timer_bt_num;
167 mhop->hop_num = hr_dev->caps.qpc_timer_hop_num;
169 case HEM_TYPE_CQC_TIMER:
170 mhop->buf_chunk_size = 1 << (hr_dev->caps.cqc_timer_buf_pg_sz
172 mhop->bt_chunk_size = 1 << (hr_dev->caps.cqc_timer_ba_pg_sz
174 mhop->ba_l0_num = hr_dev->caps.cqc_timer_bt_num;
175 mhop->hop_num = hr_dev->caps.cqc_timer_hop_num;
178 mhop->buf_chunk_size = 1 << (hr_dev->caps.srqc_buf_pg_sz
180 mhop->bt_chunk_size = 1 << (hr_dev->caps.srqc_ba_pg_sz
182 mhop->ba_l0_num = hr_dev->caps.srqc_bt_num;
183 mhop->hop_num = hr_dev->caps.srqc_hop_num;
186 mhop->buf_chunk_size = 1 << (hr_dev->caps.gmv_buf_pg_sz +
188 mhop->bt_chunk_size = 1 << (hr_dev->caps.gmv_ba_pg_sz +
190 mhop->ba_l0_num = hr_dev->caps.gmv_bt_num;
191 mhop->hop_num = hr_dev->caps.gmv_hop_num;
194 dev_err(dev, "table %u not support multi-hop addressing!\n",
202 int hns_roce_calc_hem_mhop(struct hns_roce_dev *hr_dev,
203 struct hns_roce_hem_table *table, unsigned long *obj,
204 struct hns_roce_hem_mhop *mhop)
206 struct device *dev = hr_dev->dev;
212 if (get_hem_table_config(hr_dev, mhop, table->type))
219 * QPC/MTPT/CQC/SRQC/SCCC alloc hem for buffer pages.
220 * MTT/CQE alloc hem for bt pages.
222 bt_num = hns_roce_get_bt_num(table->type, mhop->hop_num);
223 chunk_ba_num = mhop->bt_chunk_size / BA_BYTE_LEN;
224 chunk_size = table->type < HEM_TYPE_MTT ? mhop->buf_chunk_size :
226 table_idx = *obj / (chunk_size / table->obj_size);
229 mhop->l2_idx = table_idx & (chunk_ba_num - 1);
230 mhop->l1_idx = table_idx / chunk_ba_num & (chunk_ba_num - 1);
231 mhop->l0_idx = (table_idx / chunk_ba_num) / chunk_ba_num;
234 mhop->l1_idx = table_idx & (chunk_ba_num - 1);
235 mhop->l0_idx = table_idx / chunk_ba_num;
238 mhop->l0_idx = table_idx;
241 dev_err(dev, "table %u not support hop_num = %u!\n",
242 table->type, mhop->hop_num);
245 if (mhop->l0_idx >= mhop->ba_l0_num)
246 mhop->l0_idx %= mhop->ba_l0_num;
251 static struct hns_roce_hem *hns_roce_alloc_hem(struct hns_roce_dev *hr_dev,
253 unsigned long hem_alloc_size,
256 struct hns_roce_hem_chunk *chunk = NULL;
257 struct hns_roce_hem *hem;
258 struct scatterlist *mem;
262 WARN_ON(gfp_mask & __GFP_HIGHMEM);
264 hem = kmalloc(sizeof(*hem),
265 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
269 INIT_LIST_HEAD(&hem->chunk_list);
271 order = get_order(hem_alloc_size);
275 chunk = kmalloc(sizeof(*chunk),
276 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
280 sg_init_table(chunk->mem, HNS_ROCE_HEM_CHUNK_LEN);
283 memset(chunk->buf, 0, sizeof(chunk->buf));
284 list_add_tail(&chunk->list, &hem->chunk_list);
287 while (1 << order > npages)
291 * Alloc memory one time. If failed, don't alloc small block
292 * memory, directly return fail.
294 mem = &chunk->mem[chunk->npages];
295 buf = dma_alloc_coherent(hr_dev->dev, PAGE_SIZE << order,
296 &sg_dma_address(mem), gfp_mask);
300 chunk->buf[chunk->npages] = buf;
301 sg_dma_len(mem) = PAGE_SIZE << order;
305 npages -= 1 << order;
311 hns_roce_free_hem(hr_dev, hem);
315 void hns_roce_free_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem *hem)
317 struct hns_roce_hem_chunk *chunk, *tmp;
323 list_for_each_entry_safe(chunk, tmp, &hem->chunk_list, list) {
324 for (i = 0; i < chunk->npages; ++i)
325 dma_free_coherent(hr_dev->dev,
326 sg_dma_len(&chunk->mem[i]),
328 sg_dma_address(&chunk->mem[i]));
335 static int calc_hem_config(struct hns_roce_dev *hr_dev,
336 struct hns_roce_hem_table *table, unsigned long obj,
337 struct hns_roce_hem_mhop *mhop,
338 struct hns_roce_hem_index *index)
340 struct ib_device *ibdev = &hr_dev->ib_dev;
341 unsigned long mhop_obj = obj;
342 u32 l0_idx, l1_idx, l2_idx;
347 ret = hns_roce_calc_hem_mhop(hr_dev, table, &mhop_obj, mhop);
351 l0_idx = mhop->l0_idx;
352 l1_idx = mhop->l1_idx;
353 l2_idx = mhop->l2_idx;
354 chunk_ba_num = mhop->bt_chunk_size / BA_BYTE_LEN;
355 bt_num = hns_roce_get_bt_num(table->type, mhop->hop_num);
358 index->l1 = l0_idx * chunk_ba_num + l1_idx;
360 index->buf = l0_idx * chunk_ba_num * chunk_ba_num +
361 l1_idx * chunk_ba_num + l2_idx;
365 index->buf = l0_idx * chunk_ba_num + l1_idx;
371 ibdev_err(ibdev, "table %u not support mhop.hop_num = %u!\n",
372 table->type, mhop->hop_num);
376 if (unlikely(index->buf >= table->num_hem)) {
377 ibdev_err(ibdev, "table %u exceed hem limt idx %llu, max %lu!\n",
378 table->type, index->buf, table->num_hem);
385 static void free_mhop_hem(struct hns_roce_dev *hr_dev,
386 struct hns_roce_hem_table *table,
387 struct hns_roce_hem_mhop *mhop,
388 struct hns_roce_hem_index *index)
390 u32 bt_size = mhop->bt_chunk_size;
391 struct device *dev = hr_dev->dev;
393 if (index->inited & HEM_INDEX_BUF) {
394 hns_roce_free_hem(hr_dev, table->hem[index->buf]);
395 table->hem[index->buf] = NULL;
398 if (index->inited & HEM_INDEX_L1) {
399 dma_free_coherent(dev, bt_size, table->bt_l1[index->l1],
400 table->bt_l1_dma_addr[index->l1]);
401 table->bt_l1[index->l1] = NULL;
404 if (index->inited & HEM_INDEX_L0) {
405 dma_free_coherent(dev, bt_size, table->bt_l0[index->l0],
406 table->bt_l0_dma_addr[index->l0]);
407 table->bt_l0[index->l0] = NULL;
411 static int alloc_mhop_hem(struct hns_roce_dev *hr_dev,
412 struct hns_roce_hem_table *table,
413 struct hns_roce_hem_mhop *mhop,
414 struct hns_roce_hem_index *index)
416 u32 bt_size = mhop->bt_chunk_size;
417 struct device *dev = hr_dev->dev;
418 struct hns_roce_hem_iter iter;
424 /* alloc L1 BA's chunk */
425 if ((check_whether_bt_num_3(table->type, mhop->hop_num) ||
426 check_whether_bt_num_2(table->type, mhop->hop_num)) &&
427 !table->bt_l0[index->l0]) {
428 table->bt_l0[index->l0] = dma_alloc_coherent(dev, bt_size,
429 &table->bt_l0_dma_addr[index->l0],
431 if (!table->bt_l0[index->l0]) {
435 index->inited |= HEM_INDEX_L0;
438 /* alloc L2 BA's chunk */
439 if (check_whether_bt_num_3(table->type, mhop->hop_num) &&
440 !table->bt_l1[index->l1]) {
441 table->bt_l1[index->l1] = dma_alloc_coherent(dev, bt_size,
442 &table->bt_l1_dma_addr[index->l1],
444 if (!table->bt_l1[index->l1]) {
448 index->inited |= HEM_INDEX_L1;
449 *(table->bt_l0[index->l0] + mhop->l1_idx) =
450 table->bt_l1_dma_addr[index->l1];
454 * alloc buffer space chunk for QPC/MTPT/CQC/SRQC/SCCC.
455 * alloc bt space chunk for MTT/CQE.
457 size = table->type < HEM_TYPE_MTT ? mhop->buf_chunk_size : bt_size;
458 flag = GFP_KERNEL | __GFP_NOWARN;
459 table->hem[index->buf] = hns_roce_alloc_hem(hr_dev, size >> PAGE_SHIFT,
461 if (!table->hem[index->buf]) {
466 index->inited |= HEM_INDEX_BUF;
467 hns_roce_hem_first(table->hem[index->buf], &iter);
468 bt_ba = hns_roce_hem_addr(&iter);
469 if (table->type < HEM_TYPE_MTT) {
470 if (mhop->hop_num == 2)
471 *(table->bt_l1[index->l1] + mhop->l2_idx) = bt_ba;
472 else if (mhop->hop_num == 1)
473 *(table->bt_l0[index->l0] + mhop->l1_idx) = bt_ba;
474 } else if (mhop->hop_num == 2) {
475 *(table->bt_l0[index->l0] + mhop->l1_idx) = bt_ba;
480 free_mhop_hem(hr_dev, table, mhop, index);
485 static int set_mhop_hem(struct hns_roce_dev *hr_dev,
486 struct hns_roce_hem_table *table, unsigned long obj,
487 struct hns_roce_hem_mhop *mhop,
488 struct hns_roce_hem_index *index)
490 struct ib_device *ibdev = &hr_dev->ib_dev;
494 if (index->inited & HEM_INDEX_L0) {
495 ret = hr_dev->hw->set_hem(hr_dev, table, obj, 0);
497 ibdev_err(ibdev, "set HEM step 0 failed!\n");
502 if (index->inited & HEM_INDEX_L1) {
503 ret = hr_dev->hw->set_hem(hr_dev, table, obj, 1);
505 ibdev_err(ibdev, "set HEM step 1 failed!\n");
510 if (index->inited & HEM_INDEX_BUF) {
511 if (mhop->hop_num == HNS_ROCE_HOP_NUM_0)
514 step_idx = mhop->hop_num;
515 ret = hr_dev->hw->set_hem(hr_dev, table, obj, step_idx);
517 ibdev_err(ibdev, "set HEM step last failed!\n");
523 static int hns_roce_table_mhop_get(struct hns_roce_dev *hr_dev,
524 struct hns_roce_hem_table *table,
527 struct ib_device *ibdev = &hr_dev->ib_dev;
528 struct hns_roce_hem_index index = {};
529 struct hns_roce_hem_mhop mhop = {};
532 ret = calc_hem_config(hr_dev, table, obj, &mhop, &index);
534 ibdev_err(ibdev, "calc hem config failed!\n");
538 mutex_lock(&table->mutex);
539 if (table->hem[index.buf]) {
540 refcount_inc(&table->hem[index.buf]->refcount);
544 ret = alloc_mhop_hem(hr_dev, table, &mhop, &index);
546 ibdev_err(ibdev, "alloc mhop hem failed!\n");
550 /* set HEM base address to hardware */
551 if (table->type < HEM_TYPE_MTT) {
552 ret = set_mhop_hem(hr_dev, table, obj, &mhop, &index);
554 ibdev_err(ibdev, "set HEM address to HW failed!\n");
559 refcount_set(&table->hem[index.buf]->refcount, 1);
563 free_mhop_hem(hr_dev, table, &mhop, &index);
565 mutex_unlock(&table->mutex);
569 int hns_roce_table_get(struct hns_roce_dev *hr_dev,
570 struct hns_roce_hem_table *table, unsigned long obj)
572 struct device *dev = hr_dev->dev;
576 if (hns_roce_check_whether_mhop(hr_dev, table->type))
577 return hns_roce_table_mhop_get(hr_dev, table, obj);
579 i = obj / (table->table_chunk_size / table->obj_size);
581 mutex_lock(&table->mutex);
584 refcount_inc(&table->hem[i]->refcount);
588 table->hem[i] = hns_roce_alloc_hem(hr_dev,
589 table->table_chunk_size >> PAGE_SHIFT,
590 table->table_chunk_size,
591 GFP_KERNEL | __GFP_NOWARN);
592 if (!table->hem[i]) {
597 /* Set HEM base address(128K/page, pa) to Hardware */
598 if (hr_dev->hw->set_hem(hr_dev, table, obj, HEM_HOP_STEP_DIRECT)) {
599 hns_roce_free_hem(hr_dev, table->hem[i]);
600 table->hem[i] = NULL;
602 dev_err(dev, "set HEM base address to HW failed.\n");
606 refcount_set(&table->hem[i]->refcount, 1);
608 mutex_unlock(&table->mutex);
612 static void clear_mhop_hem(struct hns_roce_dev *hr_dev,
613 struct hns_roce_hem_table *table, unsigned long obj,
614 struct hns_roce_hem_mhop *mhop,
615 struct hns_roce_hem_index *index)
617 struct ib_device *ibdev = &hr_dev->ib_dev;
618 u32 hop_num = mhop->hop_num;
622 index->inited = HEM_INDEX_BUF;
623 chunk_ba_num = mhop->bt_chunk_size / BA_BYTE_LEN;
624 if (check_whether_bt_num_2(table->type, hop_num)) {
625 if (hns_roce_check_hem_null(table->hem, index->buf,
626 chunk_ba_num, table->num_hem))
627 index->inited |= HEM_INDEX_L0;
628 } else if (check_whether_bt_num_3(table->type, hop_num)) {
629 if (hns_roce_check_hem_null(table->hem, index->buf,
630 chunk_ba_num, table->num_hem)) {
631 index->inited |= HEM_INDEX_L1;
632 if (hns_roce_check_bt_null(table->bt_l1, index->l1,
634 index->inited |= HEM_INDEX_L0;
638 if (table->type < HEM_TYPE_MTT) {
639 if (hop_num == HNS_ROCE_HOP_NUM_0)
644 if (hr_dev->hw->clear_hem(hr_dev, table, obj, step_idx))
645 ibdev_warn(ibdev, "failed to clear hop%u HEM.\n", hop_num);
647 if (index->inited & HEM_INDEX_L1)
648 if (hr_dev->hw->clear_hem(hr_dev, table, obj, 1))
649 ibdev_warn(ibdev, "failed to clear HEM step 1.\n");
651 if (index->inited & HEM_INDEX_L0)
652 if (hr_dev->hw->clear_hem(hr_dev, table, obj, 0))
653 ibdev_warn(ibdev, "failed to clear HEM step 0.\n");
657 static void hns_roce_table_mhop_put(struct hns_roce_dev *hr_dev,
658 struct hns_roce_hem_table *table,
662 struct ib_device *ibdev = &hr_dev->ib_dev;
663 struct hns_roce_hem_index index = {};
664 struct hns_roce_hem_mhop mhop = {};
667 ret = calc_hem_config(hr_dev, table, obj, &mhop, &index);
669 ibdev_err(ibdev, "calc hem config failed!\n");
674 mutex_lock(&table->mutex);
675 else if (!refcount_dec_and_mutex_lock(&table->hem[index.buf]->refcount,
679 clear_mhop_hem(hr_dev, table, obj, &mhop, &index);
680 free_mhop_hem(hr_dev, table, &mhop, &index);
682 mutex_unlock(&table->mutex);
685 void hns_roce_table_put(struct hns_roce_dev *hr_dev,
686 struct hns_roce_hem_table *table, unsigned long obj)
688 struct device *dev = hr_dev->dev;
691 if (hns_roce_check_whether_mhop(hr_dev, table->type)) {
692 hns_roce_table_mhop_put(hr_dev, table, obj, 1);
696 i = obj / (table->table_chunk_size / table->obj_size);
698 if (!refcount_dec_and_mutex_lock(&table->hem[i]->refcount,
702 if (hr_dev->hw->clear_hem(hr_dev, table, obj, HEM_HOP_STEP_DIRECT))
703 dev_warn(dev, "failed to clear HEM base address.\n");
705 hns_roce_free_hem(hr_dev, table->hem[i]);
706 table->hem[i] = NULL;
708 mutex_unlock(&table->mutex);
711 void *hns_roce_table_find(struct hns_roce_dev *hr_dev,
712 struct hns_roce_hem_table *table,
713 unsigned long obj, dma_addr_t *dma_handle)
715 struct hns_roce_hem_chunk *chunk;
716 struct hns_roce_hem_mhop mhop;
717 struct hns_roce_hem *hem;
718 unsigned long mhop_obj = obj;
719 unsigned long obj_per_chunk;
720 unsigned long idx_offset;
721 int offset, dma_offset;
727 mutex_lock(&table->mutex);
729 if (!hns_roce_check_whether_mhop(hr_dev, table->type)) {
730 obj_per_chunk = table->table_chunk_size / table->obj_size;
731 hem = table->hem[obj / obj_per_chunk];
732 idx_offset = obj % obj_per_chunk;
733 dma_offset = offset = idx_offset * table->obj_size;
735 u32 seg_size = 64; /* 8 bytes per BA and 8 BA per segment */
737 if (hns_roce_calc_hem_mhop(hr_dev, table, &mhop_obj, &mhop))
742 if (mhop.hop_num == 2)
743 hem_idx = i * (mhop.bt_chunk_size / BA_BYTE_LEN) + j;
744 else if (mhop.hop_num == 1 ||
745 mhop.hop_num == HNS_ROCE_HOP_NUM_0)
748 hem = table->hem[hem_idx];
749 dma_offset = offset = obj * seg_size % mhop.bt_chunk_size;
750 if (mhop.hop_num == 2)
751 dma_offset = offset = 0;
757 list_for_each_entry(chunk, &hem->chunk_list, list) {
758 for (i = 0; i < chunk->npages; ++i) {
759 length = sg_dma_len(&chunk->mem[i]);
760 if (dma_handle && dma_offset >= 0) {
761 if (length > (u32)dma_offset)
762 *dma_handle = sg_dma_address(
763 &chunk->mem[i]) + dma_offset;
764 dma_offset -= length;
767 if (length > (u32)offset) {
768 addr = chunk->buf[i] + offset;
776 mutex_unlock(&table->mutex);
780 int hns_roce_init_hem_table(struct hns_roce_dev *hr_dev,
781 struct hns_roce_hem_table *table, u32 type,
782 unsigned long obj_size, unsigned long nobj)
784 unsigned long obj_per_chunk;
785 unsigned long num_hem;
787 if (!hns_roce_check_whether_mhop(hr_dev, type)) {
788 table->table_chunk_size = hr_dev->caps.chunk_sz;
789 obj_per_chunk = table->table_chunk_size / obj_size;
790 num_hem = DIV_ROUND_UP(nobj, obj_per_chunk);
792 table->hem = kcalloc(num_hem, sizeof(*table->hem), GFP_KERNEL);
796 struct hns_roce_hem_mhop mhop = {};
797 unsigned long buf_chunk_size;
798 unsigned long bt_chunk_size;
799 unsigned long bt_chunk_num;
800 unsigned long num_bt_l0;
803 if (get_hem_table_config(hr_dev, &mhop, type))
806 buf_chunk_size = mhop.buf_chunk_size;
807 bt_chunk_size = mhop.bt_chunk_size;
808 num_bt_l0 = mhop.ba_l0_num;
809 hop_num = mhop.hop_num;
811 obj_per_chunk = buf_chunk_size / obj_size;
812 num_hem = DIV_ROUND_UP(nobj, obj_per_chunk);
813 bt_chunk_num = bt_chunk_size / BA_BYTE_LEN;
815 if (type >= HEM_TYPE_MTT)
816 num_bt_l0 = bt_chunk_num;
818 table->hem = kcalloc(num_hem, sizeof(*table->hem),
821 goto err_kcalloc_hem_buf;
823 if (check_whether_bt_num_3(type, hop_num)) {
824 unsigned long num_bt_l1;
826 num_bt_l1 = DIV_ROUND_UP(num_hem, bt_chunk_num);
827 table->bt_l1 = kcalloc(num_bt_l1,
828 sizeof(*table->bt_l1),
831 goto err_kcalloc_bt_l1;
833 table->bt_l1_dma_addr = kcalloc(num_bt_l1,
834 sizeof(*table->bt_l1_dma_addr),
837 if (!table->bt_l1_dma_addr)
838 goto err_kcalloc_l1_dma;
841 if (check_whether_bt_num_2(type, hop_num) ||
842 check_whether_bt_num_3(type, hop_num)) {
843 table->bt_l0 = kcalloc(num_bt_l0, sizeof(*table->bt_l0),
846 goto err_kcalloc_bt_l0;
848 table->bt_l0_dma_addr = kcalloc(num_bt_l0,
849 sizeof(*table->bt_l0_dma_addr),
851 if (!table->bt_l0_dma_addr)
852 goto err_kcalloc_l0_dma;
857 table->num_hem = num_hem;
858 table->obj_size = obj_size;
859 mutex_init(&table->mutex);
868 kfree(table->bt_l1_dma_addr);
869 table->bt_l1_dma_addr = NULL;
883 static void hns_roce_cleanup_mhop_hem_table(struct hns_roce_dev *hr_dev,
884 struct hns_roce_hem_table *table)
886 struct hns_roce_hem_mhop mhop;
891 if (hns_roce_calc_hem_mhop(hr_dev, table, NULL, &mhop))
893 buf_chunk_size = table->type < HEM_TYPE_MTT ? mhop.buf_chunk_size :
896 for (i = 0; i < table->num_hem; ++i) {
897 obj = i * buf_chunk_size / table->obj_size;
899 hns_roce_table_mhop_put(hr_dev, table, obj, 0);
906 kfree(table->bt_l1_dma_addr);
907 table->bt_l1_dma_addr = NULL;
910 kfree(table->bt_l0_dma_addr);
911 table->bt_l0_dma_addr = NULL;
914 void hns_roce_cleanup_hem_table(struct hns_roce_dev *hr_dev,
915 struct hns_roce_hem_table *table)
917 struct device *dev = hr_dev->dev;
920 if (hns_roce_check_whether_mhop(hr_dev, table->type)) {
921 hns_roce_cleanup_mhop_hem_table(hr_dev, table);
925 for (i = 0; i < table->num_hem; ++i)
927 if (hr_dev->hw->clear_hem(hr_dev, table,
928 i * table->table_chunk_size / table->obj_size, 0))
929 dev_err(dev, "clear HEM base address failed.\n");
931 hns_roce_free_hem(hr_dev, table->hem[i]);
937 void hns_roce_cleanup_hem(struct hns_roce_dev *hr_dev)
939 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
940 hns_roce_cleanup_hem_table(hr_dev,
941 &hr_dev->srq_table.table);
942 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->cq_table.table);
943 if (hr_dev->caps.qpc_timer_entry_sz)
944 hns_roce_cleanup_hem_table(hr_dev,
945 &hr_dev->qpc_timer_table);
946 if (hr_dev->caps.cqc_timer_entry_sz)
947 hns_roce_cleanup_hem_table(hr_dev,
948 &hr_dev->cqc_timer_table);
949 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL)
950 hns_roce_cleanup_hem_table(hr_dev,
951 &hr_dev->qp_table.sccc_table);
952 if (hr_dev->caps.trrl_entry_sz)
953 hns_roce_cleanup_hem_table(hr_dev,
954 &hr_dev->qp_table.trrl_table);
956 if (hr_dev->caps.gmv_entry_sz)
957 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->gmv_table);
959 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.irrl_table);
960 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->qp_table.qp_table);
961 hns_roce_cleanup_hem_table(hr_dev, &hr_dev->mr_table.mtpt_table);
964 struct hns_roce_hem_item {
965 struct list_head list; /* link all hems in the same bt level */
966 struct list_head sibling; /* link all hems in last hop for mtt */
969 size_t count; /* max ba numbers */
970 int start; /* start buf offset in this hem */
971 int end; /* end buf offset in this hem */
974 /* All HEM items are linked in a tree structure */
975 struct hns_roce_hem_head {
976 struct list_head branch[HNS_ROCE_MAX_BT_REGION];
977 struct list_head root;
978 struct list_head leaf;
981 static struct hns_roce_hem_item *
982 hem_list_alloc_item(struct hns_roce_dev *hr_dev, int start, int end, int count,
985 struct hns_roce_hem_item *hem;
987 hem = kzalloc(sizeof(*hem), GFP_KERNEL);
992 hem->addr = dma_alloc_coherent(hr_dev->dev, count * BA_BYTE_LEN,
993 &hem->dma_addr, GFP_KERNEL);
1003 INIT_LIST_HEAD(&hem->list);
1004 INIT_LIST_HEAD(&hem->sibling);
1009 static void hem_list_free_item(struct hns_roce_dev *hr_dev,
1010 struct hns_roce_hem_item *hem, bool exist_bt)
1013 dma_free_coherent(hr_dev->dev, hem->count * BA_BYTE_LEN,
1014 hem->addr, hem->dma_addr);
1018 static void hem_list_free_all(struct hns_roce_dev *hr_dev,
1019 struct list_head *head, bool exist_bt)
1021 struct hns_roce_hem_item *hem, *temp_hem;
1023 list_for_each_entry_safe(hem, temp_hem, head, list) {
1024 list_del(&hem->list);
1025 hem_list_free_item(hr_dev, hem, exist_bt);
1029 static void hem_list_link_bt(struct hns_roce_dev *hr_dev, void *base_addr,
1032 *(u64 *)(base_addr) = table_addr;
1035 /* assign L0 table address to hem from root bt */
1036 static void hem_list_assign_bt(struct hns_roce_dev *hr_dev,
1037 struct hns_roce_hem_item *hem, void *cpu_addr,
1040 hem->addr = cpu_addr;
1041 hem->dma_addr = (dma_addr_t)phy_addr;
1044 static inline bool hem_list_page_is_in_range(struct hns_roce_hem_item *hem,
1047 return (hem->start <= offset && offset <= hem->end);
1050 static struct hns_roce_hem_item *hem_list_search_item(struct list_head *ba_list,
1053 struct hns_roce_hem_item *hem, *temp_hem;
1054 struct hns_roce_hem_item *found = NULL;
1056 list_for_each_entry_safe(hem, temp_hem, ba_list, list) {
1057 if (hem_list_page_is_in_range(hem, page_offset)) {
1066 static bool hem_list_is_bottom_bt(int hopnum, int bt_level)
1069 * hopnum base address table levels
1073 * 3 L0 -> L1 -> L2 -> buf
1075 return bt_level >= (hopnum ? hopnum - 1 : hopnum);
1079 * calc base address entries num
1080 * @hopnum: num of mutihop addressing
1081 * @bt_level: base address table level
1082 * @unit: ba entries per bt page
1084 static u32 hem_list_calc_ba_range(int hopnum, int bt_level, int unit)
1090 if (hopnum <= bt_level)
1093 * hopnum bt_level range
1099 * 3 0 unit * unit * unit
1104 max = hopnum - bt_level;
1105 for (i = 0; i < max; i++)
1112 * calc the root ba entries which could cover all regions
1113 * @regions: buf region array
1114 * @region_cnt: array size of @regions
1115 * @unit: ba entries per bt page
1117 int hns_roce_hem_list_calc_root_ba(const struct hns_roce_buf_region *regions,
1118 int region_cnt, int unit)
1120 struct hns_roce_buf_region *r;
1125 for (i = 0; i < region_cnt; i++) {
1126 r = (struct hns_roce_buf_region *)®ions[i];
1127 if (r->hopnum > 1) {
1128 step = hem_list_calc_ba_range(r->hopnum, 1, unit);
1130 total += (r->count + step - 1) / step;
1139 static int hem_list_alloc_mid_bt(struct hns_roce_dev *hr_dev,
1140 const struct hns_roce_buf_region *r, int unit,
1141 int offset, struct list_head *mid_bt,
1142 struct list_head *btm_bt)
1144 struct hns_roce_hem_item *hem_ptrs[HNS_ROCE_MAX_BT_LEVEL] = { NULL };
1145 struct list_head temp_list[HNS_ROCE_MAX_BT_LEVEL];
1146 struct hns_roce_hem_item *cur, *pre;
1147 const int hopnum = r->hopnum;
1159 if (hopnum > HNS_ROCE_MAX_BT_LEVEL) {
1160 dev_err(hr_dev->dev, "invalid hopnum %d!\n", hopnum);
1164 if (offset < r->offset) {
1165 dev_err(hr_dev->dev, "invalid offset %d, min %u!\n",
1170 distance = offset - r->offset;
1171 max_ofs = r->offset + r->count - 1;
1172 for (level = 0; level < hopnum; level++)
1173 INIT_LIST_HEAD(&temp_list[level]);
1175 /* config L1 bt to last bt and link them to corresponding parent */
1176 for (level = 1; level < hopnum; level++) {
1177 cur = hem_list_search_item(&mid_bt[level], offset);
1179 hem_ptrs[level] = cur;
1183 step = hem_list_calc_ba_range(hopnum, level, unit);
1189 start_aligned = (distance / step) * step + r->offset;
1190 end = min_t(int, start_aligned + step - 1, max_ofs);
1191 cur = hem_list_alloc_item(hr_dev, start_aligned, end, unit,
1197 hem_ptrs[level] = cur;
1198 list_add(&cur->list, &temp_list[level]);
1199 if (hem_list_is_bottom_bt(hopnum, level))
1200 list_add(&cur->sibling, &temp_list[0]);
1202 /* link bt to parent bt */
1204 pre = hem_ptrs[level - 1];
1205 step = (cur->start - pre->start) / step * BA_BYTE_LEN;
1206 hem_list_link_bt(hr_dev, pre->addr + step,
1211 list_splice(&temp_list[0], btm_bt);
1212 for (level = 1; level < hopnum; level++)
1213 list_splice(&temp_list[level], &mid_bt[level]);
1218 for (level = 1; level < hopnum; level++)
1219 hem_list_free_all(hr_dev, &temp_list[level], true);
1224 static struct hns_roce_hem_item *
1225 alloc_root_hem(struct hns_roce_dev *hr_dev, int unit, int *max_ba_num,
1226 const struct hns_roce_buf_region *regions, int region_cnt)
1228 const struct hns_roce_buf_region *r;
1229 struct hns_roce_hem_item *hem;
1233 ba_num = hns_roce_hem_list_calc_root_ba(regions, region_cnt, unit);
1235 return ERR_PTR(-ENOMEM);
1238 return ERR_PTR(-ENOBUFS);
1240 offset = regions[0].offset;
1241 /* indicate to last region */
1242 r = ®ions[region_cnt - 1];
1243 hem = hem_list_alloc_item(hr_dev, offset, r->offset + r->count - 1,
1246 return ERR_PTR(-ENOMEM);
1248 *max_ba_num = ba_num;
1253 static int alloc_fake_root_bt(struct hns_roce_dev *hr_dev, void *cpu_base,
1254 u64 phy_base, const struct hns_roce_buf_region *r,
1255 struct list_head *branch_head,
1256 struct list_head *leaf_head)
1258 struct hns_roce_hem_item *hem;
1260 hem = hem_list_alloc_item(hr_dev, r->offset, r->offset + r->count - 1,
1265 hem_list_assign_bt(hr_dev, hem, cpu_base, phy_base);
1266 list_add(&hem->list, branch_head);
1267 list_add(&hem->sibling, leaf_head);
1272 static int setup_middle_bt(struct hns_roce_dev *hr_dev, void *cpu_base,
1273 int unit, const struct hns_roce_buf_region *r,
1274 const struct list_head *branch_head)
1276 struct hns_roce_hem_item *hem, *temp_hem;
1281 step = hem_list_calc_ba_range(r->hopnum, 1, unit);
1285 /* if exist mid bt, link L1 to L0 */
1286 list_for_each_entry_safe(hem, temp_hem, branch_head, list) {
1287 offset = (hem->start - r->offset) / step * BA_BYTE_LEN;
1288 hem_list_link_bt(hr_dev, cpu_base + offset, hem->dma_addr);
1296 setup_root_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem_list *hem_list,
1297 int unit, int max_ba_num, struct hns_roce_hem_head *head,
1298 const struct hns_roce_buf_region *regions, int region_cnt)
1300 const struct hns_roce_buf_region *r;
1301 struct hns_roce_hem_item *root_hem;
1307 root_hem = list_first_entry(&head->root,
1308 struct hns_roce_hem_item, list);
1313 for (i = 0; i < region_cnt && total < max_ba_num; i++) {
1318 /* all regions's mid[x][0] shared the root_bt's trunk */
1319 cpu_base = root_hem->addr + total * BA_BYTE_LEN;
1320 phy_base = root_hem->dma_addr + total * BA_BYTE_LEN;
1322 /* if hopnum is 0 or 1, cut a new fake hem from the root bt
1323 * which's address share to all regions.
1325 if (hem_list_is_bottom_bt(r->hopnum, 0))
1326 ret = alloc_fake_root_bt(hr_dev, cpu_base, phy_base, r,
1327 &head->branch[i], &head->leaf);
1329 ret = setup_middle_bt(hr_dev, cpu_base, unit, r,
1330 &hem_list->mid_bt[i][1]);
1338 list_splice(&head->leaf, &hem_list->btm_bt);
1339 list_splice(&head->root, &hem_list->root_bt);
1340 for (i = 0; i < region_cnt; i++)
1341 list_splice(&head->branch[i], &hem_list->mid_bt[i][0]);
1346 static int hem_list_alloc_root_bt(struct hns_roce_dev *hr_dev,
1347 struct hns_roce_hem_list *hem_list, int unit,
1348 const struct hns_roce_buf_region *regions,
1351 struct hns_roce_hem_item *root_hem;
1352 struct hns_roce_hem_head head;
1357 root_hem = hem_list_search_item(&hem_list->root_bt, regions[0].offset);
1362 root_hem = alloc_root_hem(hr_dev, unit, &max_ba_num, regions,
1364 if (IS_ERR(root_hem))
1365 return PTR_ERR(root_hem);
1367 /* List head for storing all allocated HEM items */
1368 INIT_LIST_HEAD(&head.root);
1369 INIT_LIST_HEAD(&head.leaf);
1370 for (i = 0; i < region_cnt; i++)
1371 INIT_LIST_HEAD(&head.branch[i]);
1373 hem_list->root_ba = root_hem->dma_addr;
1374 list_add(&root_hem->list, &head.root);
1375 ret = setup_root_hem(hr_dev, hem_list, unit, max_ba_num, &head, regions,
1378 for (i = 0; i < region_cnt; i++)
1379 hem_list_free_all(hr_dev, &head.branch[i], false);
1381 hem_list_free_all(hr_dev, &head.root, true);
1387 /* construct the base address table and link them by address hop config */
1388 int hns_roce_hem_list_request(struct hns_roce_dev *hr_dev,
1389 struct hns_roce_hem_list *hem_list,
1390 const struct hns_roce_buf_region *regions,
1391 int region_cnt, unsigned int bt_pg_shift)
1393 const struct hns_roce_buf_region *r;
1399 if (region_cnt > HNS_ROCE_MAX_BT_REGION) {
1400 dev_err(hr_dev->dev, "invalid region region_cnt %d!\n",
1405 unit = (1 << bt_pg_shift) / BA_BYTE_LEN;
1406 for (i = 0; i < region_cnt; i++) {
1411 end = r->offset + r->count;
1412 for (ofs = r->offset; ofs < end; ofs += unit) {
1413 ret = hem_list_alloc_mid_bt(hr_dev, r, unit, ofs,
1414 hem_list->mid_bt[i],
1417 dev_err(hr_dev->dev,
1418 "alloc hem trunk fail ret = %d!\n", ret);
1424 ret = hem_list_alloc_root_bt(hr_dev, hem_list, unit, regions,
1427 dev_err(hr_dev->dev, "alloc hem root fail ret = %d!\n", ret);
1432 hns_roce_hem_list_release(hr_dev, hem_list);
1437 void hns_roce_hem_list_release(struct hns_roce_dev *hr_dev,
1438 struct hns_roce_hem_list *hem_list)
1442 for (i = 0; i < HNS_ROCE_MAX_BT_REGION; i++)
1443 for (j = 0; j < HNS_ROCE_MAX_BT_LEVEL; j++)
1444 hem_list_free_all(hr_dev, &hem_list->mid_bt[i][j],
1447 hem_list_free_all(hr_dev, &hem_list->root_bt, true);
1448 INIT_LIST_HEAD(&hem_list->btm_bt);
1449 hem_list->root_ba = 0;
1452 void hns_roce_hem_list_init(struct hns_roce_hem_list *hem_list)
1456 INIT_LIST_HEAD(&hem_list->root_bt);
1457 INIT_LIST_HEAD(&hem_list->btm_bt);
1458 for (i = 0; i < HNS_ROCE_MAX_BT_REGION; i++)
1459 for (j = 0; j < HNS_ROCE_MAX_BT_LEVEL; j++)
1460 INIT_LIST_HEAD(&hem_list->mid_bt[i][j]);
1463 void *hns_roce_hem_list_find_mtt(struct hns_roce_dev *hr_dev,
1464 struct hns_roce_hem_list *hem_list,
1465 int offset, int *mtt_cnt)
1467 struct list_head *head = &hem_list->btm_bt;
1468 struct hns_roce_hem_item *hem, *temp_hem;
1469 void *cpu_base = NULL;
1472 list_for_each_entry_safe(hem, temp_hem, head, sibling) {
1473 if (hem_list_page_is_in_range(hem, offset)) {
1474 nr = offset - hem->start;
1475 cpu_base = hem->addr + nr * BA_BYTE_LEN;
1476 nr = hem->end + 1 - offset;