1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2015-2018 Broadcom */
4 #include <linux/delay.h>
5 #include <linux/mutex.h>
6 #include <linux/spinlock_types.h>
7 #include <linux/workqueue.h>
9 #include <drm/drm_encoder.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_gem_shmem_helper.h>
12 #include <drm/gpu_scheduler.h>
14 #include "uapi/drm/v3d_drm.h"
18 struct platform_device
;
21 #define GMP_GRANULARITY (128 * 1024)
23 /* Enum for each of the V3D queues. */
32 #define V3D_MAX_QUEUES (V3D_CACHE_CLEAN + 1)
34 struct v3d_queue_state
{
35 struct drm_gpu_scheduler sched
;
42 struct drm_device drm
;
44 /* Short representation (e.g. 33, 41) of the V3D tech version
51 struct platform_device
*pdev
;
52 void __iomem
*hub_regs
;
53 void __iomem
*core_regs
[3];
54 void __iomem
*bridge_regs
;
55 void __iomem
*gca_regs
;
57 struct reset_control
*reset
;
59 /* Virtual and DMA addresses of the single shared page table. */
63 /* Virtual and DMA addresses of the MMU's scratch page. When
64 * a read or write is invalid in the MMU, it will be
68 dma_addr_t mmu_scratch_paddr
;
69 /* virtual address bits from V3D to the MMU. */
72 /* Number of V3D cores. */
75 /* Allocator managing the address space. All units are in
81 struct work_struct overflow_mem_work
;
83 struct v3d_bin_job
*bin_job
;
84 struct v3d_render_job
*render_job
;
85 struct v3d_tfu_job
*tfu_job
;
86 struct v3d_csd_job
*csd_job
;
88 struct v3d_queue_state queue
[V3D_MAX_QUEUES
];
90 /* Spinlock used to synchronize the overflow memory
91 * management against bin job submission.
95 /* Protects bo_stats */
98 /* Lock taken when resetting the GPU, to keep multiple
99 * processes from trying to park the scheduler threads and
102 struct mutex reset_lock
;
104 /* Lock taken when creating and pushing the GPU scheduler
105 * jobs, to keep the sched-fence seqnos in order.
107 struct mutex sched_lock
;
109 /* Lock taken during a cache clean and when initiating an L2
110 * flush, to keep L2 flushes from interfering with the
111 * synchronous L2 cleans.
113 struct mutex cache_clean_lock
;
121 static inline struct v3d_dev
*
122 to_v3d_dev(struct drm_device
*dev
)
124 return (struct v3d_dev
*)dev
->dev_private
;
128 v3d_has_csd(struct v3d_dev
*v3d
)
130 return v3d
->ver
>= 41;
133 /* The per-fd struct, which tracks the MMU mappings. */
134 struct v3d_file_priv
{
137 struct drm_sched_entity sched_entity
[V3D_MAX_QUEUES
];
141 struct drm_gem_shmem_object base
;
143 struct drm_mm_node node
;
145 /* List entry for the BO's position in
146 * v3d_render_job->unref_list
148 struct list_head unref_head
;
151 static inline struct v3d_bo
*
152 to_v3d_bo(struct drm_gem_object
*bo
)
154 return (struct v3d_bo
*)bo
;
158 struct dma_fence base
;
159 struct drm_device
*dev
;
160 /* v3d seqno for signaled() test */
162 enum v3d_queue queue
;
165 static inline struct v3d_fence
*
166 to_v3d_fence(struct dma_fence
*fence
)
168 return (struct v3d_fence
*)fence
;
171 #define V3D_READ(offset) readl(v3d->hub_regs + offset)
172 #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
174 #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
175 #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
177 #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
178 #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
180 #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
181 #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
184 struct drm_sched_job base
;
186 struct kref refcount
;
190 /* This is the array of BOs that were looked up at the start
193 struct drm_gem_object
**bo
;
196 /* Array of struct dma_fence * to block on before submitting this job.
199 unsigned long last_dep
;
201 /* v3d fence to be signaled by IRQ handler when the job is complete. */
202 struct dma_fence
*irq_fence
;
204 /* scheduler fence for when the job is considered complete and
205 * the BO reservations can be released.
207 struct dma_fence
*done_fence
;
209 /* Callback for the freeing of the job on refcount going to 0. */
210 void (*free
)(struct kref
*ref
);
216 /* GPU virtual addresses of the start/end of the CL job. */
219 u32 timedout_ctca
, timedout_ctra
;
221 /* Corresponding render job, for attaching our overflow memory. */
222 struct v3d_render_job
*render
;
224 /* Submitted tile memory allocation start/size, tile state. */
228 struct v3d_render_job
{
231 /* GPU virtual addresses of the start/end of the CL job. */
234 u32 timedout_ctca
, timedout_ctra
;
236 /* List of overflow BOs used in the job that need to be
237 * released once the job is complete.
239 struct list_head unref_list
;
245 struct drm_v3d_submit_tfu args
;
251 u32 timedout_batches
;
253 struct drm_v3d_submit_csd args
;
257 * _wait_for - magic (register) wait macro
259 * Does the right thing for modeset paths when run under kdgb or similar atomic
260 * contexts. Note that it's important that we check the condition again after
261 * having timed out, since the timeout could be due to preemption or similar and
262 * we've never had a chance to check the condition before the timeout.
264 #define wait_for(COND, MS) ({ \
265 unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
268 if (time_after(jiffies, timeout__)) { \
270 ret__ = -ETIMEDOUT; \
278 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n
)
280 /* nsecs_to_jiffies64() does not guard against overflow */
281 if (NSEC_PER_SEC
% HZ
&&
282 div_u64(n
, NSEC_PER_SEC
) >= MAX_JIFFY_OFFSET
/ HZ
)
283 return MAX_JIFFY_OFFSET
;
285 return min_t(u64
, MAX_JIFFY_OFFSET
, nsecs_to_jiffies64(n
) + 1);
289 struct drm_gem_object
*v3d_create_object(struct drm_device
*dev
, size_t size
);
290 void v3d_free_object(struct drm_gem_object
*gem_obj
);
291 struct v3d_bo
*v3d_bo_create(struct drm_device
*dev
, struct drm_file
*file_priv
,
293 int v3d_create_bo_ioctl(struct drm_device
*dev
, void *data
,
294 struct drm_file
*file_priv
);
295 int v3d_mmap_bo_ioctl(struct drm_device
*dev
, void *data
,
296 struct drm_file
*file_priv
);
297 int v3d_get_bo_offset_ioctl(struct drm_device
*dev
, void *data
,
298 struct drm_file
*file_priv
);
299 struct drm_gem_object
*v3d_prime_import_sg_table(struct drm_device
*dev
,
300 struct dma_buf_attachment
*attach
,
301 struct sg_table
*sgt
);
304 int v3d_debugfs_init(struct drm_minor
*minor
);
307 extern const struct dma_fence_ops v3d_fence_ops
;
308 struct dma_fence
*v3d_fence_create(struct v3d_dev
*v3d
, enum v3d_queue queue
);
311 int v3d_gem_init(struct drm_device
*dev
);
312 void v3d_gem_destroy(struct drm_device
*dev
);
313 int v3d_submit_cl_ioctl(struct drm_device
*dev
, void *data
,
314 struct drm_file
*file_priv
);
315 int v3d_submit_tfu_ioctl(struct drm_device
*dev
, void *data
,
316 struct drm_file
*file_priv
);
317 int v3d_submit_csd_ioctl(struct drm_device
*dev
, void *data
,
318 struct drm_file
*file_priv
);
319 int v3d_wait_bo_ioctl(struct drm_device
*dev
, void *data
,
320 struct drm_file
*file_priv
);
321 void v3d_job_put(struct v3d_job
*job
);
322 void v3d_reset(struct v3d_dev
*v3d
);
323 void v3d_invalidate_caches(struct v3d_dev
*v3d
);
324 void v3d_clean_caches(struct v3d_dev
*v3d
);
327 int v3d_irq_init(struct v3d_dev
*v3d
);
328 void v3d_irq_enable(struct v3d_dev
*v3d
);
329 void v3d_irq_disable(struct v3d_dev
*v3d
);
330 void v3d_irq_reset(struct v3d_dev
*v3d
);
333 int v3d_mmu_get_offset(struct drm_file
*file_priv
, struct v3d_bo
*bo
,
335 int v3d_mmu_set_page_table(struct v3d_dev
*v3d
);
336 void v3d_mmu_insert_ptes(struct v3d_bo
*bo
);
337 void v3d_mmu_remove_ptes(struct v3d_bo
*bo
);
340 int v3d_sched_init(struct v3d_dev
*v3d
);
341 void v3d_sched_fini(struct v3d_dev
*v3d
);