1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2020-2023 Intel Corporation
4 */
5#ifndef __IVPU_GEM_H__
6#define __IVPU_GEM_H__
7
8#include <drm/drm_gem.h>
9#include <drm/drm_mm.h>
10
11struct dma_buf;
12struct ivpu_bo_ops;
13struct ivpu_file_priv;
14
15struct ivpu_bo {
16 struct drm_gem_object base;
17 const struct ivpu_bo_ops *ops;
18
19 struct ivpu_mmu_context *ctx;
20 struct list_head ctx_node;
21 struct drm_mm_node mm_node;
22
23 struct mutex lock; /* Protects: pages, sgt, mmu_mapped */
24 struct sg_table *sgt;
25 struct page **pages;
26 bool mmu_mapped;
27
28 void *kvaddr;
29 u64 vpu_addr;
30 u32 handle;
31 u32 flags;
32 uintptr_t user_ptr;
33 u32 job_status;
34};
35
36enum ivpu_bo_type {
37 IVPU_BO_TYPE_SHMEM = 1,
38 IVPU_BO_TYPE_INTERNAL,
39 IVPU_BO_TYPE_PRIME,
40};
41
42struct ivpu_bo_ops {
43 enum ivpu_bo_type type;
44 const char *name;
45 int (*alloc_pages)(struct ivpu_bo *bo);
46 void (*free_pages)(struct ivpu_bo *bo);
47 int (*map_pages)(struct ivpu_bo *bo);
48 void (*unmap_pages)(struct ivpu_bo *bo);
49};
50
51int ivpu_bo_pin(struct ivpu_bo *bo);
52void ivpu_bo_remove_all_bos_from_context(struct ivpu_mmu_context *ctx);
53void ivpu_bo_list(struct drm_device *dev, struct drm_printer *p);
54void ivpu_bo_list_print(struct drm_device *dev);
55
56struct ivpu_bo *
57ivpu_bo_alloc_internal(struct ivpu_device *vdev, u64 vpu_addr, u64 size, u32 flags);
58void ivpu_bo_free_internal(struct ivpu_bo *bo);
59struct drm_gem_object *ivpu_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
60void ivpu_bo_unmap_sgt_and_remove_from_context(struct ivpu_bo *bo);
61
62int ivpu_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
63int ivpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
64int ivpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file);
65
66static inline struct ivpu_bo *to_ivpu_bo(struct drm_gem_object *obj)
67{
68 return container_of(obj, struct ivpu_bo, base);
69}
70
71static inline void *ivpu_bo_vaddr(struct ivpu_bo *bo)
72{
73 return bo->kvaddr;
74}
75
76static inline size_t ivpu_bo_size(struct ivpu_bo *bo)
77{
78 return bo->base.size;
79}
80
81static inline struct page *ivpu_bo_get_page(struct ivpu_bo *bo, u64 offset)
82{
83 if (offset > ivpu_bo_size(bo) || !bo->pages)
84 return NULL;
85
86 return bo->pages[offset / PAGE_SIZE];
87}
88
89static inline u32 ivpu_bo_cache_mode(struct ivpu_bo *bo)
90{
91 return bo->flags & DRM_IVPU_BO_CACHE_MASK;
92}
93
94static inline bool ivpu_bo_is_snooped(struct ivpu_bo *bo)
95{
96 return ivpu_bo_cache_mode(bo) == DRM_IVPU_BO_CACHED;
97}
98
99static inline pgprot_t ivpu_bo_pgprot(struct ivpu_bo *bo, pgprot_t prot)
100{
101 if (bo->flags & DRM_IVPU_BO_WC)
102 return pgprot_writecombine(prot);
103
104 if (bo->flags & DRM_IVPU_BO_UNCACHED)
105 return pgprot_noncached(prot);
106
107 return prot;
108}
109
110static inline struct ivpu_device *ivpu_bo_to_vdev(struct ivpu_bo *bo)
111{
112 return to_ivpu_device(dev: bo->base.dev);
113}
114
115static inline void *ivpu_to_cpu_addr(struct ivpu_bo *bo, u32 vpu_addr)
116{
117 if (vpu_addr < bo->vpu_addr)
118 return NULL;
119
120 if (vpu_addr >= (bo->vpu_addr + ivpu_bo_size(bo)))
121 return NULL;
122
123 return ivpu_bo_vaddr(bo) + (vpu_addr - bo->vpu_addr);
124}
125
126static inline u32 cpu_to_vpu_addr(struct ivpu_bo *bo, void *cpu_addr)
127{
128 if (cpu_addr < ivpu_bo_vaddr(bo))
129 return 0;
130
131 if (cpu_addr >= (ivpu_bo_vaddr(bo) + ivpu_bo_size(bo)))
132 return 0;
133
134 return bo->vpu_addr + (cpu_addr - ivpu_bo_vaddr(bo));
135}
136
137#endif /* __IVPU_GEM_H__ */
138

source code of linux/drivers/accel/ivpu/ivpu_gem.h