xref: /linux/drivers/gpu/drm/i915/display/intel_dsb_buffer.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2023, Intel Corporation.
4  */
5 
6 #include "gem/i915_gem_internal.h"
7 #include "gem/i915_gem_lmem.h"
8 #include "i915_drv.h"
9 #include "i915_vma.h"
10 #include "intel_display_types.h"
11 #include "intel_dsb_buffer.h"
12 
13 u32 intel_dsb_buffer_ggtt_offset(struct intel_dsb_buffer *dsb_buf)
14 {
15 	return i915_ggtt_offset(dsb_buf->vma);
16 }
17 
18 void intel_dsb_buffer_write(struct intel_dsb_buffer *dsb_buf, u32 idx, u32 val)
19 {
20 	dsb_buf->cmd_buf[idx] = val;
21 }
22 
23 u32 intel_dsb_buffer_read(struct intel_dsb_buffer *dsb_buf, u32 idx)
24 {
25 	return dsb_buf->cmd_buf[idx];
26 }
27 
28 void intel_dsb_buffer_memset(struct intel_dsb_buffer *dsb_buf, u32 idx, u32 val, size_t size)
29 {
30 	WARN_ON(idx > (dsb_buf->buf_size - size) / sizeof(*dsb_buf->cmd_buf));
31 
32 	memset(&dsb_buf->cmd_buf[idx], val, size);
33 }
34 
35 bool intel_dsb_buffer_create(struct intel_crtc *crtc, struct intel_dsb_buffer *dsb_buf, size_t size)
36 {
37 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
38 	struct drm_i915_gem_object *obj;
39 	struct i915_vma *vma;
40 	u32 *buf;
41 
42 	if (HAS_LMEM(i915)) {
43 		obj = i915_gem_object_create_lmem(i915, PAGE_ALIGN(size),
44 						  I915_BO_ALLOC_CONTIGUOUS);
45 		if (IS_ERR(obj))
46 			return false;
47 	} else {
48 		obj = i915_gem_object_create_internal(i915, PAGE_ALIGN(size));
49 		if (IS_ERR(obj))
50 			return false;
51 
52 		i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
53 	}
54 
55 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
56 	if (IS_ERR(vma)) {
57 		i915_gem_object_put(obj);
58 		return false;
59 	}
60 
61 	buf = i915_gem_object_pin_map_unlocked(vma->obj, I915_MAP_WC);
62 	if (IS_ERR(buf)) {
63 		i915_vma_unpin_and_release(&vma, I915_VMA_RELEASE_MAP);
64 		return false;
65 	}
66 
67 	dsb_buf->vma = vma;
68 	dsb_buf->cmd_buf = buf;
69 	dsb_buf->buf_size = size;
70 
71 	return true;
72 }
73 
74 void intel_dsb_buffer_cleanup(struct intel_dsb_buffer *dsb_buf)
75 {
76 	i915_vma_unpin_and_release(&dsb_buf->vma, I915_VMA_RELEASE_MAP);
77 }
78 
79 void intel_dsb_buffer_flush_map(struct intel_dsb_buffer *dsb_buf)
80 {
81 	i915_gem_object_flush_map(dsb_buf->vma->obj);
82 }
83