Path: blob/master/drivers/accel/habanalabs/common/command_buffer.c
26436 views
// SPDX-License-Identifier: GPL-2.012/*3* Copyright 2016-2019 HabanaLabs, Ltd.4* All Rights Reserved.5*/67#include <uapi/drm/habanalabs_accel.h>8#include "habanalabs.h"910#include <linux/mm.h>11#include <linux/slab.h>12#include <linux/uaccess.h>1314#define CB_VA_POOL_SIZE (4UL * SZ_1G)1516static int cb_map_mem(struct hl_ctx *ctx, struct hl_cb *cb)17{18struct hl_device *hdev = ctx->hdev;19struct asic_fixed_properties *prop = &hdev->asic_prop;20u32 page_size = prop->pmmu.page_size;21int rc;2223if (!hdev->supports_cb_mapping) {24dev_err_ratelimited(hdev->dev,25"Mapping a CB to the device's MMU is not supported\n");26return -EINVAL;27}2829if (cb->is_mmu_mapped)30return 0;3132cb->roundup_size = roundup(cb->size, page_size);3334cb->virtual_addr = (u64) gen_pool_alloc(ctx->cb_va_pool, cb->roundup_size);35if (!cb->virtual_addr) {36dev_err(hdev->dev, "Failed to allocate device virtual address for CB\n");37return -ENOMEM;38}3940mutex_lock(&hdev->mmu_lock);4142rc = hl_mmu_map_contiguous(ctx, cb->virtual_addr, cb->bus_address, cb->roundup_size);43if (rc) {44dev_err(hdev->dev, "Failed to map VA %#llx to CB\n", cb->virtual_addr);45goto err_va_pool_free;46}4748rc = hl_mmu_invalidate_cache(hdev, false, MMU_OP_USERPTR | MMU_OP_SKIP_LOW_CACHE_INV);49if (rc)50goto err_mmu_unmap;5152mutex_unlock(&hdev->mmu_lock);5354cb->is_mmu_mapped = true;5556return 0;5758err_mmu_unmap:59hl_mmu_unmap_contiguous(ctx, cb->virtual_addr, cb->roundup_size);60err_va_pool_free:61mutex_unlock(&hdev->mmu_lock);62gen_pool_free(ctx->cb_va_pool, cb->virtual_addr, cb->roundup_size);6364return rc;65}6667static void cb_unmap_mem(struct hl_ctx *ctx, struct hl_cb *cb)68{69struct hl_device *hdev = ctx->hdev;7071mutex_lock(&hdev->mmu_lock);72hl_mmu_unmap_contiguous(ctx, cb->virtual_addr, cb->roundup_size);73hl_mmu_invalidate_cache(hdev, true, MMU_OP_USERPTR);74mutex_unlock(&hdev->mmu_lock);7576gen_pool_free(ctx->cb_va_pool, cb->virtual_addr, cb->roundup_size);77}7879static void cb_fini(struct hl_device *hdev, struct hl_cb *cb)80{81if (cb->is_internal)82gen_pool_free(hdev->internal_cb_pool,83(uintptr_t)cb->kernel_address, cb->size);84else85hl_asic_dma_free_coherent(hdev, cb->size, cb->kernel_address, cb->bus_address);8687kfree(cb);88}8990static void cb_do_release(struct hl_device *hdev, struct hl_cb *cb)91{92if (cb->is_pool) {93atomic_set(&cb->is_handle_destroyed, 0);94spin_lock(&hdev->cb_pool_lock);95list_add(&cb->pool_list, &hdev->cb_pool);96spin_unlock(&hdev->cb_pool_lock);97} else {98cb_fini(hdev, cb);99}100}101102static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,103int ctx_id, bool internal_cb)104{105struct hl_cb *cb = NULL;106u32 cb_offset;107void *p;108109/*110* We use of GFP_ATOMIC here because this function can be called from111* the latency-sensitive code path for command submission. Due to H/W112* limitations in some of the ASICs, the kernel must copy the user CB113* that is designated for an external queue and actually enqueue114* the kernel's copy. Hence, we must never sleep in this code section115* and must use GFP_ATOMIC for all memory allocations.116*/117if (ctx_id == HL_KERNEL_ASID_ID && !hdev->disabled)118cb = kzalloc(sizeof(*cb), GFP_ATOMIC);119120if (!cb)121cb = kzalloc(sizeof(*cb), GFP_KERNEL);122123if (!cb)124return NULL;125126if (internal_cb) {127p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size);128if (!p) {129kfree(cb);130return NULL;131}132133cb_offset = p - hdev->internal_cb_pool_virt_addr;134cb->is_internal = true;135cb->bus_address = hdev->internal_cb_va_base + cb_offset;136} else if (ctx_id == HL_KERNEL_ASID_ID) {137p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address, GFP_ATOMIC);138if (!p)139p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address, GFP_KERNEL);140} else {141p = hl_asic_dma_alloc_coherent(hdev, cb_size, &cb->bus_address,142GFP_USER | __GFP_ZERO);143}144145if (!p) {146dev_err(hdev->dev,147"failed to allocate %d of dma memory for CB\n",148cb_size);149kfree(cb);150return NULL;151}152153cb->kernel_address = p;154cb->size = cb_size;155156return cb;157}158159struct hl_cb_mmap_mem_alloc_args {160struct hl_device *hdev;161struct hl_ctx *ctx;162u32 cb_size;163bool internal_cb;164bool map_cb;165};166167static void hl_cb_mmap_mem_release(struct hl_mmap_mem_buf *buf)168{169struct hl_cb *cb = buf->private;170171hl_debugfs_remove_cb(cb);172173if (cb->is_mmu_mapped)174cb_unmap_mem(cb->ctx, cb);175176hl_ctx_put(cb->ctx);177178cb_do_release(cb->hdev, cb);179}180181static int hl_cb_mmap_mem_alloc(struct hl_mmap_mem_buf *buf, gfp_t gfp, void *args)182{183struct hl_cb_mmap_mem_alloc_args *cb_args = args;184struct hl_cb *cb;185int rc, ctx_id = cb_args->ctx->asid;186bool alloc_new_cb = true;187188if (!cb_args->internal_cb) {189/* Minimum allocation must be PAGE SIZE */190if (cb_args->cb_size < PAGE_SIZE)191cb_args->cb_size = PAGE_SIZE;192193if (ctx_id == HL_KERNEL_ASID_ID &&194cb_args->cb_size <= cb_args->hdev->asic_prop.cb_pool_cb_size) {195196spin_lock(&cb_args->hdev->cb_pool_lock);197if (!list_empty(&cb_args->hdev->cb_pool)) {198cb = list_first_entry(&cb_args->hdev->cb_pool,199typeof(*cb), pool_list);200list_del(&cb->pool_list);201spin_unlock(&cb_args->hdev->cb_pool_lock);202alloc_new_cb = false;203} else {204spin_unlock(&cb_args->hdev->cb_pool_lock);205dev_dbg(cb_args->hdev->dev, "CB pool is empty\n");206}207}208}209210if (alloc_new_cb) {211cb = hl_cb_alloc(cb_args->hdev, cb_args->cb_size, ctx_id, cb_args->internal_cb);212if (!cb)213return -ENOMEM;214}215216cb->hdev = cb_args->hdev;217cb->ctx = cb_args->ctx;218cb->buf = buf;219cb->buf->mappable_size = cb->size;220cb->buf->private = cb;221222hl_ctx_get(cb->ctx);223224if (cb_args->map_cb) {225if (ctx_id == HL_KERNEL_ASID_ID) {226dev_err(cb_args->hdev->dev,227"CB mapping is not supported for kernel context\n");228rc = -EINVAL;229goto release_cb;230}231232rc = cb_map_mem(cb_args->ctx, cb);233if (rc)234goto release_cb;235}236237hl_debugfs_add_cb(cb);238239return 0;240241release_cb:242hl_ctx_put(cb->ctx);243cb_do_release(cb_args->hdev, cb);244245return rc;246}247248static int hl_cb_mmap(struct hl_mmap_mem_buf *buf,249struct vm_area_struct *vma, void *args)250{251struct hl_cb *cb = buf->private;252253return cb->hdev->asic_funcs->mmap(cb->hdev, vma, cb->kernel_address,254cb->bus_address, cb->size);255}256257static struct hl_mmap_mem_buf_behavior cb_behavior = {258.topic = "CB",259.mem_id = HL_MMAP_TYPE_CB,260.alloc = hl_cb_mmap_mem_alloc,261.release = hl_cb_mmap_mem_release,262.mmap = hl_cb_mmap,263};264265int hl_cb_create(struct hl_device *hdev, struct hl_mem_mgr *mmg,266struct hl_ctx *ctx, u32 cb_size, bool internal_cb,267bool map_cb, u64 *handle)268{269struct hl_cb_mmap_mem_alloc_args args = {270.hdev = hdev,271.ctx = ctx,272.cb_size = cb_size,273.internal_cb = internal_cb,274.map_cb = map_cb,275};276struct hl_mmap_mem_buf *buf;277int ctx_id = ctx->asid;278279if ((hdev->disabled) || (hdev->reset_info.in_reset && (ctx_id != HL_KERNEL_ASID_ID))) {280dev_warn_ratelimited(hdev->dev,281"Device is disabled or in reset. Can't create new CBs\n");282return -EBUSY;283}284285if (cb_size > SZ_2M) {286dev_err(hdev->dev, "CB size %d must be less than %d\n",287cb_size, SZ_2M);288return -EINVAL;289}290291buf = hl_mmap_mem_buf_alloc(292mmg, &cb_behavior,293ctx_id == HL_KERNEL_ASID_ID ? GFP_ATOMIC : GFP_KERNEL, &args);294if (!buf)295return -ENOMEM;296297*handle = buf->handle;298299return 0;300}301302int hl_cb_destroy(struct hl_mem_mgr *mmg, u64 cb_handle)303{304struct hl_cb *cb;305int rc;306307cb = hl_cb_get(mmg, cb_handle);308if (!cb) {309dev_dbg(mmg->dev, "CB destroy failed, no CB was found for handle %#llx\n",310cb_handle);311return -EINVAL;312}313314/* Make sure that CB handle isn't destroyed more than once */315rc = atomic_cmpxchg(&cb->is_handle_destroyed, 0, 1);316hl_cb_put(cb);317if (rc) {318dev_dbg(mmg->dev, "CB destroy failed, handle %#llx was already destroyed\n",319cb_handle);320return -EINVAL;321}322323rc = hl_mmap_mem_buf_put_handle(mmg, cb_handle);324if (rc < 0)325return rc; /* Invalid handle */326327if (rc == 0)328dev_dbg(mmg->dev, "CB 0x%llx is destroyed while still in use\n", cb_handle);329330return 0;331}332333static int hl_cb_info(struct hl_mem_mgr *mmg,334u64 handle, u32 flags, u32 *usage_cnt, u64 *device_va)335{336struct hl_cb *cb;337int rc = 0;338339cb = hl_cb_get(mmg, handle);340if (!cb) {341dev_err(mmg->dev,342"CB info failed, no match to handle 0x%llx\n", handle);343return -EINVAL;344}345346if (flags & HL_CB_FLAGS_GET_DEVICE_VA) {347if (cb->is_mmu_mapped) {348*device_va = cb->virtual_addr;349} else {350dev_err(mmg->dev, "CB is not mapped to the device's MMU\n");351rc = -EINVAL;352goto out;353}354} else {355*usage_cnt = atomic_read(&cb->cs_cnt);356}357358out:359hl_cb_put(cb);360return rc;361}362363int hl_cb_ioctl(struct drm_device *ddev, void *data, struct drm_file *file_priv)364{365struct hl_fpriv *hpriv = file_priv->driver_priv;366struct hl_device *hdev = hpriv->hdev;367union hl_cb_args *args = data;368u64 handle = 0, device_va = 0;369enum hl_device_status status;370u32 usage_cnt = 0;371int rc;372373if (!hl_device_operational(hdev, &status)) {374dev_dbg_ratelimited(hdev->dev,375"Device is %s. Can't execute CB IOCTL\n",376hdev->status[status]);377return -EBUSY;378}379380switch (args->in.op) {381case HL_CB_OP_CREATE:382if (args->in.cb_size > HL_MAX_CB_SIZE) {383dev_err(hdev->dev,384"User requested CB size %d must be less than %d\n",385args->in.cb_size, HL_MAX_CB_SIZE);386rc = -EINVAL;387} else {388rc = hl_cb_create(hdev, &hpriv->mem_mgr, hpriv->ctx,389args->in.cb_size, false,390!!(args->in.flags & HL_CB_FLAGS_MAP),391&handle);392}393394memset(args, 0, sizeof(*args));395args->out.cb_handle = handle;396break;397398case HL_CB_OP_DESTROY:399rc = hl_cb_destroy(&hpriv->mem_mgr,400args->in.cb_handle);401break;402403case HL_CB_OP_INFO:404rc = hl_cb_info(&hpriv->mem_mgr, args->in.cb_handle,405args->in.flags,406&usage_cnt,407&device_va);408if (rc)409break;410411memset(&args->out, 0, sizeof(args->out));412413if (args->in.flags & HL_CB_FLAGS_GET_DEVICE_VA)414args->out.device_va = device_va;415else416args->out.usage_cnt = usage_cnt;417break;418419default:420rc = -EINVAL;421break;422}423424return rc;425}426427struct hl_cb *hl_cb_get(struct hl_mem_mgr *mmg, u64 handle)428{429struct hl_mmap_mem_buf *buf;430431buf = hl_mmap_mem_buf_get(mmg, handle);432if (!buf)433return NULL;434return buf->private;435436}437438void hl_cb_put(struct hl_cb *cb)439{440hl_mmap_mem_buf_put(cb->buf);441}442443struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,444bool internal_cb)445{446u64 cb_handle;447struct hl_cb *cb;448int rc;449450rc = hl_cb_create(hdev, &hdev->kernel_mem_mgr, hdev->kernel_ctx, cb_size,451internal_cb, false, &cb_handle);452if (rc) {453dev_err(hdev->dev,454"Failed to allocate CB for the kernel driver %d\n", rc);455return NULL;456}457458cb = hl_cb_get(&hdev->kernel_mem_mgr, cb_handle);459/* hl_cb_get should never fail here */460if (!cb) {461dev_crit(hdev->dev, "Kernel CB handle invalid 0x%x\n",462(u32) cb_handle);463goto destroy_cb;464}465466return cb;467468destroy_cb:469hl_cb_destroy(&hdev->kernel_mem_mgr, cb_handle);470471return NULL;472}473474int hl_cb_pool_init(struct hl_device *hdev)475{476struct hl_cb *cb;477int i;478479INIT_LIST_HEAD(&hdev->cb_pool);480spin_lock_init(&hdev->cb_pool_lock);481482for (i = 0 ; i < hdev->asic_prop.cb_pool_cb_cnt ; i++) {483cb = hl_cb_alloc(hdev, hdev->asic_prop.cb_pool_cb_size,484HL_KERNEL_ASID_ID, false);485if (cb) {486cb->is_pool = true;487list_add(&cb->pool_list, &hdev->cb_pool);488} else {489hl_cb_pool_fini(hdev);490return -ENOMEM;491}492}493494return 0;495}496497int hl_cb_pool_fini(struct hl_device *hdev)498{499struct hl_cb *cb, *tmp;500501list_for_each_entry_safe(cb, tmp, &hdev->cb_pool, pool_list) {502list_del(&cb->pool_list);503cb_fini(hdev, cb);504}505506return 0;507}508509int hl_cb_va_pool_init(struct hl_ctx *ctx)510{511struct hl_device *hdev = ctx->hdev;512struct asic_fixed_properties *prop = &hdev->asic_prop;513int rc;514515if (!hdev->supports_cb_mapping)516return 0;517518ctx->cb_va_pool = gen_pool_create(__ffs(prop->pmmu.page_size), -1);519if (!ctx->cb_va_pool) {520dev_err(hdev->dev,521"Failed to create VA gen pool for CB mapping\n");522return -ENOMEM;523}524525ctx->cb_va_pool_base = hl_reserve_va_block(hdev, ctx, HL_VA_RANGE_TYPE_HOST,526CB_VA_POOL_SIZE, HL_MMU_VA_ALIGNMENT_NOT_NEEDED);527if (!ctx->cb_va_pool_base) {528rc = -ENOMEM;529goto err_pool_destroy;530}531rc = gen_pool_add(ctx->cb_va_pool, ctx->cb_va_pool_base, CB_VA_POOL_SIZE, -1);532if (rc) {533dev_err(hdev->dev,534"Failed to add memory to VA gen pool for CB mapping\n");535goto err_unreserve_va_block;536}537538return 0;539540err_unreserve_va_block:541hl_unreserve_va_block(hdev, ctx, ctx->cb_va_pool_base, CB_VA_POOL_SIZE);542err_pool_destroy:543gen_pool_destroy(ctx->cb_va_pool);544545return rc;546}547548void hl_cb_va_pool_fini(struct hl_ctx *ctx)549{550struct hl_device *hdev = ctx->hdev;551552if (!hdev->supports_cb_mapping)553return;554555gen_pool_destroy(ctx->cb_va_pool);556hl_unreserve_va_block(hdev, ctx, ctx->cb_va_pool_base, CB_VA_POOL_SIZE);557}558559560