1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
3 * Copyright (c) 2017 Hisilicon Limited.
4 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
7 #include <linux/platform_device.h>
8 #include <rdma/ib_umem.h>
9 #include "hns_roce_device.h"
11 int hns_roce_db_map_user(struct hns_roce_ucontext *context,
12 struct ib_udata *udata, unsigned long virt,
13 struct hns_roce_db *db)
15 struct hns_roce_user_db_page *page;
18 mutex_lock(&context->page_mutex);
20 list_for_each_entry(page, &context->page_list, list)
21 if (page->user_virt == (virt & PAGE_MASK))
24 page = kmalloc(sizeof(*page), GFP_KERNEL);
30 refcount_set(&page->refcount, 1);
31 page->user_virt = (virt & PAGE_MASK);
32 page->umem = ib_umem_get(udata, virt & PAGE_MASK, PAGE_SIZE, 0, 0);
33 if (IS_ERR(page->umem)) {
34 ret = PTR_ERR(page->umem);
39 list_add(&page->list, &context->page_list);
42 db->dma = sg_dma_address(page->umem->sg_head.sgl) +
44 page->umem->sg_head.sgl->offset = virt & ~PAGE_MASK;
45 db->virt_addr = sg_virt(page->umem->sg_head.sgl);
46 db->u.user_page = page;
47 refcount_inc(&page->refcount);
50 mutex_unlock(&context->page_mutex);
55 void hns_roce_db_unmap_user(struct hns_roce_ucontext *context,
56 struct hns_roce_db *db)
58 mutex_lock(&context->page_mutex);
60 refcount_dec(&db->u.user_page->refcount);
61 if (refcount_dec_if_one(&db->u.user_page->refcount)) {
62 list_del(&db->u.user_page->list);
63 ib_umem_release(db->u.user_page->umem);
64 kfree(db->u.user_page);
67 mutex_unlock(&context->page_mutex);
70 static struct hns_roce_db_pgdir *hns_roce_alloc_db_pgdir(
71 struct device *dma_device)
73 struct hns_roce_db_pgdir *pgdir;
75 pgdir = kzalloc(sizeof(*pgdir), GFP_KERNEL);
79 bitmap_fill(pgdir->order1,
80 HNS_ROCE_DB_PER_PAGE / HNS_ROCE_DB_TYPE_COUNT);
81 pgdir->bits[0] = pgdir->order0;
82 pgdir->bits[1] = pgdir->order1;
83 pgdir->page = dma_alloc_coherent(dma_device, PAGE_SIZE,
84 &pgdir->db_dma, GFP_KERNEL);
93 static int hns_roce_alloc_db_from_pgdir(struct hns_roce_db_pgdir *pgdir,
94 struct hns_roce_db *db, int order)
99 for (o = order; o <= 1; ++o) {
100 i = find_first_bit(pgdir->bits[o], HNS_ROCE_DB_PER_PAGE >> o);
101 if (i < HNS_ROCE_DB_PER_PAGE >> o)
108 clear_bit(i, pgdir->bits[o]);
113 set_bit(i ^ 1, pgdir->bits[order]);
117 db->db_record = pgdir->page + db->index;
118 db->dma = pgdir->db_dma + db->index * HNS_ROCE_DB_UNIT_SIZE;
124 int hns_roce_alloc_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db,
127 struct hns_roce_db_pgdir *pgdir;
130 mutex_lock(&hr_dev->pgdir_mutex);
132 list_for_each_entry(pgdir, &hr_dev->pgdir_list, list)
133 if (!hns_roce_alloc_db_from_pgdir(pgdir, db, order))
136 pgdir = hns_roce_alloc_db_pgdir(hr_dev->dev);
142 list_add(&pgdir->list, &hr_dev->pgdir_list);
144 /* This should never fail -- we just allocated an empty page: */
145 WARN_ON(hns_roce_alloc_db_from_pgdir(pgdir, db, order));
148 mutex_unlock(&hr_dev->pgdir_mutex);
153 void hns_roce_free_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db)
158 mutex_lock(&hr_dev->pgdir_mutex);
163 if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
164 clear_bit(i ^ 1, db->u.pgdir->order0);
169 set_bit(i, db->u.pgdir->bits[o]);
171 if (bitmap_full(db->u.pgdir->order1,
172 HNS_ROCE_DB_PER_PAGE / HNS_ROCE_DB_TYPE_COUNT)) {
173 dma_free_coherent(hr_dev->dev, PAGE_SIZE, db->u.pgdir->page,
174 db->u.pgdir->db_dma);
175 list_del(&db->u.pgdir->list);
179 mutex_unlock(&hr_dev->pgdir_mutex);