dma-heap: qcom: Store DMA-BUF fops on allocation

Certain drivers need to be able to determine if a given struct file is
for a DMA-BUF. Add a means of determining this by storing the fops
from the allocation path.

Change-Id: I70ebf9185ae74030d6064234fcee29682256ba2c
Signed-off-by: Chris Goldsworthy <quic_cgoldswo@quicinc.com>
This commit is contained in:
Chris Goldsworthy 2023-03-02 10:21:43 -08:00
parent 187582c3e0
commit 8d5d7c6e45
11 changed files with 58 additions and 12 deletions

View File

@ -7,7 +7,7 @@
*
* Copyright (C) 2011 Google, Inc.
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#include <linux/dma-mapping.h>
@ -191,7 +191,7 @@ static struct dma_buf *__carveout_heap_allocate(struct carveout_heap *carveout_h
exp_info.size = buffer->len;
exp_info.flags = fd_flags;
exp_info.priv = buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto err_free_vmperm;

View File

@ -7,6 +7,7 @@
* Copyright (C) 2012, 2019 Linaro Ltd.
* Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#include <linux/cma.h>
@ -129,7 +130,7 @@ struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
exp_info.size = helper_buffer->len;
exp_info.flags = fd_flags;
exp_info.priv = helper_buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto vmperm_release;

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#include <linux/module.h>
@ -16,6 +16,15 @@
#include "qcom_system_heap.h"
#include "qcom_carveout_heap.h"
#include "qcom_secure_system_heap.h"
#include "qcom_dma_heap_priv.h"
/*
* We cache the file ops used by DMA-BUFs so that a user with a struct file
* pointer can determine if that file is for a DMA-BUF. We can't initialize
* the pointer here at compile time as ERR_PTR() evaluates to a function.
*/
#define FOPS_INIT_VAL ERR_PTR(-EINVAL)
static const struct file_operations *dma_buf_cached_fops;
static int qcom_dma_heap_probe(struct platform_device *pdev)
{
@ -85,9 +94,24 @@ static int qcom_dma_heap_probe(struct platform_device *pdev)
free_pdata(heaps);
dma_buf_cached_fops = FOPS_INIT_VAL;
return ret;
}
void qcom_store_dma_buf_fops(struct file *file)
{
if (dma_buf_cached_fops == FOPS_INIT_VAL)
dma_buf_cached_fops = file->f_op;
}
EXPORT_SYMBOL(qcom_store_dma_buf_fops);
bool qcom_is_dma_buf_file(struct file *file)
{
return file->f_op == dma_buf_cached_fops;
}
EXPORT_SYMBOL(qcom_is_dma_buf_file);
static const struct of_device_id qcom_dma_heap_match_table[] = {
{.compatible = "qcom,dma-heaps"},
{},

View File

@ -3,6 +3,7 @@
* Copyright (C) 2011 Google, Inc.
* Copyright (C) 2019 Linaro Ltd.
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#ifndef _QCOM_DMA_HEAP_PRIV_H
@ -10,6 +11,23 @@
#include <linux/dma-heap.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/mem-buf-exporter.h>
void qcom_store_dma_buf_fops(struct file *file);
static inline struct dma_buf *qcom_dma_buf_export(struct dma_buf_export_info *exp_info,
struct mem_buf_dma_buf_ops *ops)
{
struct dma_buf *dmabuf;
dmabuf = mem_buf_dma_buf_export(exp_info, ops);
if (!IS_ERR(dmabuf))
qcom_store_dma_buf_fops(dmabuf->file);
return dmabuf;
}
/**
* struct heap_helper_buffer - helper buffer metadata

View File

@ -41,6 +41,7 @@
* Andrew F. Davis <afd@ti.com>
*
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#include <linux/dma-buf.h>
@ -577,7 +578,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
exp_info.size = buffer->len;
exp_info.flags = fd_flags;
exp_info.priv = buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto vmperm_release;

View File

@ -17,8 +17,8 @@
#include <linux/scatterlist.h>
#include <linux/dma-heap.h>
#include <linux/device.h>
#include <linux/mem-buf-exporter.h>
#include "deferred-free-helper.h"
#include "qcom_dma_heap_priv.h"
struct qcom_sg_buffer {
struct dma_heap *heap;

View File

@ -518,7 +518,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
exp_info.size = buffer->len;
exp_info.flags = fd_flags;
exp_info.priv = buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto free_vmperm;

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#define pr_fmt(fmt) "tui_heap: %s" fmt, __func__
@ -279,7 +279,7 @@ static struct dma_buf *tui_heap_allocate(struct dma_heap *dma_heap,
exp_info.size = buffer->len;
exp_info.flags = fd_flags;
exp_info.priv = buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto err_export;

View File

@ -328,7 +328,7 @@ static struct dma_buf *ubwcp_allocate(struct dma_heap *heap,
exp_info.size = buffer->qcom_sg_buf.len;
exp_info.flags = fd_flags;
exp_info.priv = &buffer->qcom_sg_buf;
dmabuf = mem_buf_dma_buf_export(&exp_info, &ubwcp_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &ubwcp_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto free_vmperm;

View File

@ -1125,7 +1125,7 @@ struct dma_buf *mem_buf_retrieve(struct mem_buf_retrieve_kernel_arg *arg)
exp_info.flags = arg->fd_flags;
exp_info.priv = buffer;
dmabuf = mem_buf_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
dmabuf = qcom_dma_buf_export(&exp_info, &qcom_sg_buf_ops);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
goto err_export_dma_buf;

View File

@ -1,7 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#ifndef _QCOM_DMA_HEAP_H
#define _QCOM_DMA_HEAP_H
@ -37,6 +37,8 @@
#define QCOM_DMA_HEAP_FLAG_SECURE BIT(31)
bool qcom_is_dma_buf_file(struct file *file);
struct dma_buf_heap_prefetch_region {
u64 size;
struct dma_heap *heap;