Commit 168174d7 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'drm-fixes-2024-01-27' of git://anongit.freedesktop.org/drm/drm

Pull drm fixes from Dave Airlie:
 "Lots going on for rc2, ivpu has a bunch of stabilisation and debugging
  work, then amdgpu and xe are the main fixes. i915, exynos have a few,
  then some misc panel and bridge fixes.

  Worth mentioning are three regressions. One of the nouveau fixes in
  6.7 for a serious deadlock had side effects, so I guess we will bring
  back the deadlock until I can figure out what should be done properly.
  There was a scheduler regression vs amdgpu which was reported in a few
  places and is now fixed. There was an i915 vs simpledrm problem
  resulting in black screens, that is reverted also.

  I'll be working on a proper nouveau fix, it kinda looks like one of
  those cases where someone tried to use an atomic where they should
  have probably used a lock, but I'll see.

  fb:
   - fix simpledrm/i915 regression by reverting change

  scheduler:
   - fix regression affecting amdgpu users due to sched draining

  nouveau:
   - revert 6.7 deadlock fix as it has side effects

  dp:
   - fix documentation warning

  ttm:
   - fix dummy page read on some platforms

  bridge:
   - anx7625 suspend fix
   - sii902x: fix probing and audio registration
   - parade-ps8640: fix suspend of bridge, aux fixes
   - samsung-dsim: avoid using FORCE_STOP_STATE

  panel:
   - simple add missing bus flags
   - fix samsung-s6d7aa0 flags

  amdgpu:
   - AC/DC power supply tracking fix
   - Don't show invalid vram vendor data
   - SMU 13.0.x fixes
   - GART fix for umr on systems without VRAM
   - GFX 10/11 UNORD_DISPATCH fixes
   - IPS display fixes (required for S0ix on some platforms)
   - Misc fixes

  i915:
   - DSI sequence revert to fix GitLab #10071 and DP test-pattern fix
   - Drop -Wstringop-overflow (broken on GCC11)

  ivpu:
   - fix recovery/reset support
   - improve submit ioctl stability
   - fix dev open/close races on unbind
   - PLL disable reset fix
   - deprecate context priority param
   - improve debug buffer logging
   - disable buffer sharing across VPU contexts
   - free buffer sgt on unbind
   - fix missing lock around shmem vmap
   - add better boot diagnostics
   - add more debug prints around mapping
   - dump MMU events in case of timeout

  v3d:
   - NULL ptr dereference fix

  exynos:
   - fix stack usage
   - fix incorrect type
   - fix dt typo
   - fix gsc runtime resume

  xe:
   - Make an ops struct static
   - Fix an implicit 0 to NULL conversion
   - A couple of 32-bit fixes
   - A migration coherency fix for Lunar Lake.
   - An error path vm id leak fix
   - Remove PVC references in kunit tests"

* tag 'drm-fixes-2024-01-27' of git://anongit.freedesktop.org/drm/drm: (66 commits)
  Revert "nouveau: push event block/allowing out of the fence context"
  drm: bridge: samsung-dsim: Don't use FORCE_STOP_STATE
  drm/sched: Drain all entities in DRM sched run job worker
  drm/amd/display: "Enable IPS by default"
  drm/amd: Add a DC debug mask for IPS
  drm/amd/display: Disable ips before dc interrupt setting
  drm/amd/display: Replay + IPS + ABM in Full Screen VPB
  drm/amd/display: Add IPS checks before dcn register access
  drm/amd/display: Add Replay IPS register for DMUB command table
  drm/amd/display: Allow IPS2 during Replay
  drm/amdgpu/gfx11: set UNORD_DISPATCH in compute MQDs
  drm/amdgpu/gfx10: set UNORD_DISPATCH in compute MQDs
  drm/amd/amdgpu: Assign GART pages to AMD device mapping
  drm/amd/pm: Fetch current power limit from FW
  drm/amdgpu: Fix null pointer dereference
  drm/amdgpu: Show vram vendor only if available
  drm/amd/pm: update the power cap setting
  drm/amdgpu: Avoid fetching vram vendor information
  drm/amdgpu/pm: Fix the power source flag error
  drm/amd/display: Fix uninitialized variable usage in core_link_ 'read_dpcd() & write_dpcd()' functions
  ...
parents 2047b0b2 987940f0
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ allOf:
        clocks:
          minItems: 6
          maxItems: 6
        regs:
        reg:
          minItems: 2
          maxItems: 2

@@ -99,7 +99,7 @@ allOf:
        clocks:
          minItems: 4
          maxItems: 4
        regs:
        reg:
          minItems: 2
          maxItems: 2

@@ -116,7 +116,7 @@ allOf:
        clocks:
          minItems: 3
          maxItems: 3
        regs:
        reg:
          minItems: 1
          maxItems: 1

+16 −4
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ static int reset_pending_show(struct seq_file *s, void *v)
{
	struct ivpu_device *vdev = seq_to_ivpu(s);

	seq_printf(s, "%d\n", atomic_read(&vdev->pm->in_reset));
	seq_printf(s, "%d\n", atomic_read(&vdev->pm->reset_pending));
	return 0;
}

@@ -130,7 +130,9 @@ dvfs_mode_fops_write(struct file *file, const char __user *user_buf, size_t size

	fw->dvfs_mode = dvfs_mode;

	ivpu_pm_schedule_recovery(vdev);
	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
	if (ret)
		return ret;

	return size;
}
@@ -190,7 +192,10 @@ fw_profiling_freq_fops_write(struct file *file, const char __user *user_buf,
		return ret;

	ivpu_hw_profiling_freq_drive(vdev, enable);
	ivpu_pm_schedule_recovery(vdev);

	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
	if (ret)
		return ret;

	return size;
}
@@ -301,11 +306,18 @@ static ssize_t
ivpu_force_recovery_fn(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
{
	struct ivpu_device *vdev = file->private_data;
	int ret;

	if (!size)
		return -EINVAL;

	ivpu_pm_schedule_recovery(vdev);
	ret = ivpu_rpm_get(vdev);
	if (ret)
		return ret;

	ivpu_pm_trigger_recovery(vdev, "debugfs");
	flush_work(&vdev->pm->recovery_work);
	ivpu_rpm_put(vdev);
	return size;
}

+71 −55
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/pm_runtime.h>

#include <drm/drm_accel.h>
#include <drm/drm_file.h>
@@ -17,6 +18,7 @@
#include "ivpu_debugfs.h"
#include "ivpu_drv.h"
#include "ivpu_fw.h"
#include "ivpu_fw_log.h"
#include "ivpu_gem.h"
#include "ivpu_hw.h"
#include "ivpu_ipc.h"
@@ -65,22 +67,20 @@ struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
	return file_priv;
}

struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id)
static void file_priv_unbind(struct ivpu_device *vdev, struct ivpu_file_priv *file_priv)
{
	struct ivpu_file_priv *file_priv;

	xa_lock_irq(&vdev->context_xa);
	file_priv = xa_load(&vdev->context_xa, id);
	/* file_priv may still be in context_xa during file_priv_release() */
	if (file_priv && !kref_get_unless_zero(&file_priv->ref))
		file_priv = NULL;
	xa_unlock_irq(&vdev->context_xa);

	if (file_priv)
		ivpu_dbg(vdev, KREF, "file_priv get by id: ctx %u refcount %u\n",
			 file_priv->ctx.id, kref_read(&file_priv->ref));
	mutex_lock(&file_priv->lock);
	if (file_priv->bound) {
		ivpu_dbg(vdev, FILE, "file_priv unbind: ctx %u\n", file_priv->ctx.id);

	return file_priv;
		ivpu_cmdq_release_all_locked(file_priv);
		ivpu_jsm_context_release(vdev, file_priv->ctx.id);
		ivpu_bo_unbind_all_bos_from_context(vdev, &file_priv->ctx);
		ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
		file_priv->bound = false;
		drm_WARN_ON(&vdev->drm, !xa_erase_irq(&vdev->context_xa, file_priv->ctx.id));
	}
	mutex_unlock(&file_priv->lock);
}

static void file_priv_release(struct kref *ref)
@@ -88,13 +88,15 @@ static void file_priv_release(struct kref *ref)
	struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
	struct ivpu_device *vdev = file_priv->vdev;

	ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
	ivpu_dbg(vdev, FILE, "file_priv release: ctx %u bound %d\n",
		 file_priv->ctx.id, (bool)file_priv->bound);

	pm_runtime_get_sync(vdev->drm.dev);
	mutex_lock(&vdev->context_list_lock);
	file_priv_unbind(vdev, file_priv);
	mutex_unlock(&vdev->context_list_lock);
	pm_runtime_put_autosuspend(vdev->drm.dev);

	ivpu_cmdq_release_all(file_priv);
	ivpu_jsm_context_release(vdev, file_priv->ctx.id);
	ivpu_bo_remove_all_bos_from_context(vdev, &file_priv->ctx);
	ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
	drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
	mutex_destroy(&file_priv->lock);
	kfree(file_priv);
}
@@ -176,9 +178,6 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f
	case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
		args->value = vdev->hw->ranges.user.start;
		break;
	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
		args->value = file_priv->priority;
		break;
	case DRM_IVPU_PARAM_CONTEXT_ID:
		args->value = file_priv->ctx.id;
		break;
@@ -218,17 +217,10 @@ static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_f

static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
{
	struct ivpu_file_priv *file_priv = file->driver_priv;
	struct drm_ivpu_param *args = data;
	int ret = 0;

	switch (args->param) {
	case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
		if (args->value <= DRM_IVPU_CONTEXT_PRIORITY_REALTIME)
			file_priv->priority = args->value;
		else
			ret = -EINVAL;
		break;
	default:
		ret = -EINVAL;
	}
@@ -241,50 +233,53 @@ static int ivpu_open(struct drm_device *dev, struct drm_file *file)
	struct ivpu_device *vdev = to_ivpu_device(dev);
	struct ivpu_file_priv *file_priv;
	u32 ctx_id;
	void *old;
	int ret;
	int idx, ret;

	ret = xa_alloc_irq(&vdev->context_xa, &ctx_id, NULL, vdev->context_xa_limit, GFP_KERNEL);
	if (ret) {
		ivpu_err(vdev, "Failed to allocate context id: %d\n", ret);
		return ret;
	}
	if (!drm_dev_enter(dev, &idx))
		return -ENODEV;

	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
	if (!file_priv) {
		ret = -ENOMEM;
		goto err_xa_erase;
		goto err_dev_exit;
	}

	file_priv->vdev = vdev;
	file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
	file_priv->bound = true;
	kref_init(&file_priv->ref);
	mutex_init(&file_priv->lock);

	mutex_lock(&vdev->context_list_lock);

	ret = xa_alloc_irq(&vdev->context_xa, &ctx_id, file_priv,
			   vdev->context_xa_limit, GFP_KERNEL);
	if (ret) {
		ivpu_err(vdev, "Failed to allocate context id: %d\n", ret);
		goto err_unlock;
	}

	ret = ivpu_mmu_user_context_init(vdev, &file_priv->ctx, ctx_id);
	if (ret)
		goto err_mutex_destroy;
		goto err_xa_erase;

	old = xa_store_irq(&vdev->context_xa, ctx_id, file_priv, GFP_KERNEL);
	if (xa_is_err(old)) {
		ret = xa_err(old);
		ivpu_err(vdev, "Failed to store context %u: %d\n", ctx_id, ret);
		goto err_ctx_fini;
	}
	mutex_unlock(&vdev->context_list_lock);
	drm_dev_exit(idx);

	file->driver_priv = file_priv;

	ivpu_dbg(vdev, FILE, "file_priv create: ctx %u process %s pid %d\n",
		 ctx_id, current->comm, task_pid_nr(current));

	file->driver_priv = file_priv;
	return 0;

err_ctx_fini:
	ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
err_mutex_destroy:
	mutex_destroy(&file_priv->lock);
	kfree(file_priv);
err_xa_erase:
	xa_erase_irq(&vdev->context_xa, ctx_id);
err_unlock:
	mutex_unlock(&vdev->context_list_lock);
	mutex_destroy(&file_priv->lock);
	kfree(file_priv);
err_dev_exit:
	drm_dev_exit(idx);
	return ret;
}

@@ -340,8 +335,6 @@ static int ivpu_wait_for_ready(struct ivpu_device *vdev)

	if (!ret)
		ivpu_dbg(vdev, PM, "VPU ready message received successfully\n");
	else
		ivpu_hw_diagnose_failure(vdev);

	return ret;
}
@@ -369,6 +362,9 @@ int ivpu_boot(struct ivpu_device *vdev)
	ret = ivpu_wait_for_ready(vdev);
	if (ret) {
		ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
		ivpu_hw_diagnose_failure(vdev);
		ivpu_mmu_evtq_dump(vdev);
		ivpu_fw_log_dump(vdev);
		return ret;
	}

@@ -540,6 +536,10 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
	lockdep_set_class(&vdev->submitted_jobs_xa.xa_lock, &submitted_jobs_xa_lock_class_key);
	INIT_LIST_HEAD(&vdev->bo_list);

	ret = drmm_mutex_init(&vdev->drm, &vdev->context_list_lock);
	if (ret)
		goto err_xa_destroy;

	ret = drmm_mutex_init(&vdev->drm, &vdev->bo_list_lock);
	if (ret)
		goto err_xa_destroy;
@@ -611,14 +611,30 @@ static int ivpu_dev_init(struct ivpu_device *vdev)
	return ret;
}

static void ivpu_bo_unbind_all_user_contexts(struct ivpu_device *vdev)
{
	struct ivpu_file_priv *file_priv;
	unsigned long ctx_id;

	mutex_lock(&vdev->context_list_lock);

	xa_for_each(&vdev->context_xa, ctx_id, file_priv)
		file_priv_unbind(vdev, file_priv);

	mutex_unlock(&vdev->context_list_lock);
}

static void ivpu_dev_fini(struct ivpu_device *vdev)
{
	ivpu_pm_disable(vdev);
	ivpu_shutdown(vdev);
	if (IVPU_WA(d3hot_after_power_off))
		pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);

	ivpu_jobs_abort_all(vdev);
	ivpu_job_done_consumer_fini(vdev);
	ivpu_pm_cancel_recovery(vdev);
	ivpu_bo_unbind_all_user_contexts(vdev);

	ivpu_ipc_fini(vdev);
	ivpu_fw_fini(vdev);
+3 −2
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@
#define IVPU_DBG_JSM	 BIT(10)
#define IVPU_DBG_KREF	 BIT(11)
#define IVPU_DBG_RPM	 BIT(12)
#define IVPU_DBG_MMU_MAP BIT(13)

#define ivpu_err(vdev, fmt, ...) \
	drm_err(&(vdev)->drm, "%s(): " fmt, __func__, ##__VA_ARGS__)
@@ -114,6 +115,7 @@ struct ivpu_device {

	struct ivpu_mmu_context gctx;
	struct ivpu_mmu_context rctx;
	struct mutex context_list_lock; /* Protects user context addition/removal */
	struct xarray context_xa;
	struct xa_limit context_xa_limit;

@@ -145,8 +147,8 @@ struct ivpu_file_priv {
	struct mutex lock; /* Protects cmdq */
	struct ivpu_cmdq *cmdq[IVPU_NUM_ENGINES];
	struct ivpu_mmu_context ctx;
	u32 priority;
	bool has_mmu_faults;
	bool bound;
};

extern int ivpu_dbg_mask;
@@ -162,7 +164,6 @@ extern bool ivpu_disable_mmu_cont_pages;
extern int ivpu_test_mode;

struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id);
void ivpu_file_priv_put(struct ivpu_file_priv **link);

int ivpu_boot(struct ivpu_device *vdev);
+52 −90
Original line number Diff line number Diff line
@@ -24,14 +24,11 @@ static const struct drm_gem_object_funcs ivpu_gem_funcs;

static inline void ivpu_dbg_bo(struct ivpu_device *vdev, struct ivpu_bo *bo, const char *action)
{
	if (bo->ctx)
		ivpu_dbg(vdev, BO, "%6s: size %zu has_pages %d dma_mapped %d handle %u ctx %d vpu_addr 0x%llx mmu_mapped %d\n",
			 action, ivpu_bo_size(bo), (bool)bo->base.pages, (bool)bo->base.sgt,
			 bo->handle, bo->ctx->id, bo->vpu_addr, bo->mmu_mapped);
	else
		ivpu_dbg(vdev, BO, "%6s: size %zu has_pages %d dma_mapped %d handle %u (not added to context)\n",
			 action, ivpu_bo_size(bo), (bool)bo->base.pages, (bool)bo->base.sgt,
			 bo->handle);
	ivpu_dbg(vdev, BO,
		 "%6s: bo %8p vpu_addr %9llx size %8zu ctx %d has_pages %d dma_mapped %d mmu_mapped %d wc %d imported %d\n",
		 action, bo, bo->vpu_addr, ivpu_bo_size(bo), bo->ctx ? bo->ctx->id : 0,
		 (bool)bo->base.pages, (bool)bo->base.sgt, bo->mmu_mapped, bo->base.map_wc,
		 (bool)bo->base.base.import_attach);
}

/*
@@ -49,12 +46,7 @@ int __must_check ivpu_bo_pin(struct ivpu_bo *bo)
	mutex_lock(&bo->lock);

	ivpu_dbg_bo(vdev, bo, "pin");

	if (!bo->ctx) {
		ivpu_err(vdev, "vpu_addr not allocated for BO %d\n", bo->handle);
		ret = -EINVAL;
		goto unlock;
	}
	drm_WARN_ON(&vdev->drm, !bo->ctx);

	if (!bo->mmu_mapped) {
		struct sg_table *sgt = drm_gem_shmem_get_pages_sgt(&bo->base);
@@ -85,7 +77,10 @@ ivpu_bo_alloc_vpu_addr(struct ivpu_bo *bo, struct ivpu_mmu_context *ctx,
		       const struct ivpu_addr_range *range)
{
	struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);
	int ret;
	int idx, ret;

	if (!drm_dev_enter(&vdev->drm, &idx))
		return -ENODEV;

	mutex_lock(&bo->lock);

@@ -101,6 +96,8 @@ ivpu_bo_alloc_vpu_addr(struct ivpu_bo *bo, struct ivpu_mmu_context *ctx,

	mutex_unlock(&bo->lock);

	drm_dev_exit(idx);

	return ret;
}

@@ -108,11 +105,7 @@ static void ivpu_bo_unbind_locked(struct ivpu_bo *bo)
{
	struct ivpu_device *vdev = ivpu_bo_to_vdev(bo);

	lockdep_assert_held(&bo->lock);

	ivpu_dbg_bo(vdev, bo, "unbind");

	/* TODO: dma_unmap */
	lockdep_assert(lockdep_is_held(&bo->lock) || !kref_read(&bo->base.base.refcount));

	if (bo->mmu_mapped) {
		drm_WARN_ON(&vdev->drm, !bo->ctx);
@@ -124,19 +117,23 @@ static void ivpu_bo_unbind_locked(struct ivpu_bo *bo)

	if (bo->ctx) {
		ivpu_mmu_context_remove_node(bo->ctx, &bo->mm_node);
		bo->vpu_addr = 0;
		bo->ctx = NULL;
	}
}

static void ivpu_bo_unbind(struct ivpu_bo *bo)
{
	mutex_lock(&bo->lock);
	ivpu_bo_unbind_locked(bo);
	mutex_unlock(&bo->lock);
	if (bo->base.base.import_attach)
		return;

	dma_resv_lock(bo->base.base.resv, NULL);
	if (bo->base.sgt) {
		dma_unmap_sgtable(vdev->drm.dev, bo->base.sgt, DMA_BIDIRECTIONAL, 0);
		sg_free_table(bo->base.sgt);
		kfree(bo->base.sgt);
		bo->base.sgt = NULL;
	}
	dma_resv_unlock(bo->base.base.resv);
}

void ivpu_bo_remove_all_bos_from_context(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
void ivpu_bo_unbind_all_bos_from_context(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
{
	struct ivpu_bo *bo;

@@ -146,8 +143,10 @@ void ivpu_bo_remove_all_bos_from_context(struct ivpu_device *vdev, struct ivpu_m
	mutex_lock(&vdev->bo_list_lock);
	list_for_each_entry(bo, &vdev->bo_list, bo_list_node) {
		mutex_lock(&bo->lock);
		if (bo->ctx == ctx)
		if (bo->ctx == ctx) {
			ivpu_dbg_bo(vdev, bo, "unbind");
			ivpu_bo_unbind_locked(bo);
		}
		mutex_unlock(&bo->lock);
	}
	mutex_unlock(&vdev->bo_list_lock);
@@ -199,9 +198,6 @@ ivpu_bo_create(struct ivpu_device *vdev, u64 size, u32 flags)
	list_add_tail(&bo->bo_list_node, &vdev->bo_list);
	mutex_unlock(&vdev->bo_list_lock);

	ivpu_dbg(vdev, BO, "create: vpu_addr 0x%llx size %zu flags 0x%x\n",
		 bo->vpu_addr, bo->base.base.size, flags);

	return bo;
}

@@ -212,6 +208,12 @@ static int ivpu_bo_open(struct drm_gem_object *obj, struct drm_file *file)
	struct ivpu_bo *bo = to_ivpu_bo(obj);
	struct ivpu_addr_range *range;

	if (bo->ctx) {
		ivpu_warn(vdev, "Can't add BO to ctx %u: already in ctx %u\n",
			  file_priv->ctx.id, bo->ctx->id);
		return -EALREADY;
	}

	if (bo->flags & DRM_IVPU_BO_SHAVE_MEM)
		range = &vdev->hw->ranges.shave;
	else if (bo->flags & DRM_IVPU_BO_DMA_MEM)
@@ -227,62 +229,24 @@ static void ivpu_bo_free(struct drm_gem_object *obj)
	struct ivpu_device *vdev = to_ivpu_device(obj->dev);
	struct ivpu_bo *bo = to_ivpu_bo(obj);

	ivpu_dbg_bo(vdev, bo, "free");

	mutex_lock(&vdev->bo_list_lock);
	list_del(&bo->bo_list_node);
	mutex_unlock(&vdev->bo_list_lock);

	drm_WARN_ON(&vdev->drm, !dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ));

	ivpu_dbg_bo(vdev, bo, "free");

	ivpu_bo_unbind(bo);
	ivpu_bo_unbind_locked(bo);
	mutex_destroy(&bo->lock);

	drm_WARN_ON(obj->dev, bo->base.pages_use_count > 1);
	drm_gem_shmem_free(&bo->base);
}

static const struct dma_buf_ops ivpu_bo_dmabuf_ops =  {
	.cache_sgt_mapping = true,
	.attach = drm_gem_map_attach,
	.detach = drm_gem_map_detach,
	.map_dma_buf = drm_gem_map_dma_buf,
	.unmap_dma_buf = drm_gem_unmap_dma_buf,
	.release = drm_gem_dmabuf_release,
	.mmap = drm_gem_dmabuf_mmap,
	.vmap = drm_gem_dmabuf_vmap,
	.vunmap = drm_gem_dmabuf_vunmap,
};

static struct dma_buf *ivpu_bo_export(struct drm_gem_object *obj, int flags)
{
	struct drm_device *dev = obj->dev;
	struct dma_buf_export_info exp_info = {
		.exp_name = KBUILD_MODNAME,
		.owner = dev->driver->fops->owner,
		.ops = &ivpu_bo_dmabuf_ops,
		.size = obj->size,
		.flags = flags,
		.priv = obj,
		.resv = obj->resv,
	};
	void *sgt;

	/*
	 * Make sure that pages are allocated and dma-mapped before exporting the bo.
	 * DMA-mapping is required if the bo will be imported to the same device.
	 */
	sgt = drm_gem_shmem_get_pages_sgt(to_drm_gem_shmem_obj(obj));
	if (IS_ERR(sgt))
		return sgt;

	return drm_gem_dmabuf_export(dev, &exp_info);
}

static const struct drm_gem_object_funcs ivpu_gem_funcs = {
	.free = ivpu_bo_free,
	.open = ivpu_bo_open,
	.export = ivpu_bo_export,
	.print_info = drm_gem_shmem_object_print_info,
	.pin = drm_gem_shmem_object_pin,
	.unpin = drm_gem_shmem_object_unpin,
@@ -315,11 +279,9 @@ int ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *fi
		return PTR_ERR(bo);
	}

	ret = drm_gem_handle_create(file, &bo->base.base, &bo->handle);
	if (!ret) {
	ret = drm_gem_handle_create(file, &bo->base.base, &args->handle);
	if (!ret)
		args->vpu_addr = bo->vpu_addr;
		args->handle = bo->handle;
	}

	drm_gem_object_put(&bo->base.base);

@@ -361,7 +323,9 @@ ivpu_bo_alloc_internal(struct ivpu_device *vdev, u64 vpu_addr, u64 size, u32 fla
	if (ret)
		goto err_put;

	dma_resv_lock(bo->base.base.resv, NULL);
	ret = drm_gem_shmem_vmap(&bo->base, &map);
	dma_resv_unlock(bo->base.base.resv);
	if (ret)
		goto err_put;

@@ -376,7 +340,10 @@ void ivpu_bo_free_internal(struct ivpu_bo *bo)
{
	struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->base.vaddr);

	dma_resv_lock(bo->base.base.resv, NULL);
	drm_gem_shmem_vunmap(&bo->base, &map);
	dma_resv_unlock(bo->base.base.resv);

	drm_gem_object_put(&bo->base.base);
}

@@ -432,19 +399,11 @@ int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file

static void ivpu_bo_print_info(struct ivpu_bo *bo, struct drm_printer *p)
{
	unsigned long dma_refcount = 0;

	mutex_lock(&bo->lock);

	if (bo->base.base.dma_buf && bo->base.base.dma_buf->file)
		dma_refcount = atomic_long_read(&bo->base.base.dma_buf->file->f_count);

	drm_printf(p, "%-3u %-6d 0x%-12llx %-10lu 0x%-8x %-4u %-8lu",
		   bo->ctx->id, bo->handle, bo->vpu_addr, bo->base.base.size,
		   bo->flags, kref_read(&bo->base.base.refcount), dma_refcount);

	if (bo->base.base.import_attach)
		drm_printf(p, " imported");
	drm_printf(p, "%-9p %-3u 0x%-12llx %-10lu 0x%-8x %-4u",
		   bo, bo->ctx->id, bo->vpu_addr, bo->base.base.size,
		   bo->flags, kref_read(&bo->base.base.refcount));

	if (bo->base.pages)
		drm_printf(p, " has_pages");
@@ -452,6 +411,9 @@ static void ivpu_bo_print_info(struct ivpu_bo *bo, struct drm_printer *p)
	if (bo->mmu_mapped)
		drm_printf(p, " mmu_mapped");

	if (bo->base.base.import_attach)
		drm_printf(p, " imported");

	drm_printf(p, "\n");

	mutex_unlock(&bo->lock);
@@ -462,8 +424,8 @@ void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p)
	struct ivpu_device *vdev = to_ivpu_device(dev);
	struct ivpu_bo *bo;

	drm_printf(p, "%-3s %-6s %-14s %-10s %-10s %-4s %-8s %s\n",
		   "ctx", "handle", "vpu_addr", "size", "flags", "refs", "dma_refs", "attribs");
	drm_printf(p, "%-9s %-3s %-14s %-10s %-10s %-4s %s\n",
		   "bo", "ctx", "vpu_addr", "size", "flags", "refs", "attribs");

	mutex_lock(&vdev->bo_list_lock);
	list_for_each_entry(bo, &vdev->bo_list, bo_list_node)
Loading