1 /*
2  * Copyright © 2008-2020 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 /**
29  * @file mos_bufmgr_priv.h
30  *
31  * Private definitions of Intel-specific bufmgr functions and structures.
32  */
33 
34 #ifndef MOS_BUFMGR_PRIV_H
35 #define MOS_BUFMGR_PRIV_H
36 
37 /**
38  * Context for a buffer manager instance.
39  *
40  * Contains public methods followed by private storage for the buffer manager.
41  */
42 struct mos_bufmgr {
43     /**
44      * Allocate a buffer object.
45      *
46      * Buffer objects are not necessarily initially mapped into CPU virtual
47      * address space or graphics device aperture.  They must be mapped
48      * using bo_map() or drm_intel_gem_bo_map_gtt() to be used by the CPU.
49      */
50     struct mos_linux_bo *(*bo_alloc) (struct mos_bufmgr *bufmgr, const char *name,
51                    unsigned long size, unsigned int alignment, int mem_type);
52 
53     /**
54      * Allocate a buffer object, hinting that it will be used as a
55      * render target.
56      *
57      * This is otherwise the same as bo_alloc.
58      */
59     struct mos_linux_bo *(*bo_alloc_for_render) (struct mos_bufmgr *bufmgr,
60                           const char *name,
61                           unsigned long size,
62                           unsigned int alignment,
63                           int mem_type);
64 
65     /**
66      * Allocate a buffer object from an existing user accessible
67      * address malloc'd with the provided size.
68      * Alignment is used when mapping to the gtt.
69      * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
70      */
71     struct mos_linux_bo *(*bo_alloc_userptr)(struct mos_bufmgr *bufmgr,
72                       const char *name, void *addr,
73                       uint32_t tiling_mode, uint32_t stride,
74                       unsigned long size,
75                       unsigned long flags);
76 
77     /**
78      * Allocate a tiled buffer object.
79      *
80      * Alignment for tiled objects is set automatically; the 'flags'
81      * argument provides a hint about how the object will be used initially.
82      *
83      * Valid tiling formats are:
84      *  I915_TILING_NONE
85      *  I915_TILING_X
86      *  I915_TILING_Y
87      *
88      * Note the tiling format may be rejected; callers should check the
89      * 'tiling_mode' field on return, as well as the pitch value, which
90      * may have been rounded up to accommodate for tiling restrictions.
91      */
92     struct mos_linux_bo *(*bo_alloc_tiled) (struct mos_bufmgr *bufmgr,
93                      const char *name,
94                      int x, int y, int cpp,
95                      uint32_t *tiling_mode,
96                      unsigned long *pitch,
97                      unsigned long flags,
98                      int mem_type);
99 
100     /** Takes a reference on a buffer object */
101     void (*bo_reference) (struct mos_linux_bo *bo);
102 
103     /**
104      * Releases a reference on a buffer object, freeing the data if
105      * no references remain.
106      */
107     void (*bo_unreference) (struct mos_linux_bo *bo);
108 
109     /**
110      * Maps the buffer into userspace.
111      *
112      * This function will block waiting for any existing execution on the
113      * buffer to complete, first.  The resulting mapping is available at
114      * buf->virtual.
115      */
116     int (*bo_map) (struct mos_linux_bo *bo, int write_enable);
117 
118     /**
119      * Reduces the refcount on the userspace mapping of the buffer
120      * object.
121      */
122     int (*bo_unmap) (struct mos_linux_bo *bo);
123 
124     /**
125      * Waits for rendering to an object by the GPU to have completed.
126      *
127      * This is not required for any access to the BO by bo_map,
128      * bo_subdata, etc.  It is merely a way for the driver to implement
129      * glFinish.
130      */
131     void (*bo_wait_rendering) (struct mos_linux_bo *bo);
132 
133     /**
134      * Tears down the buffer manager instance.
135      */
136     void (*destroy) (struct mos_bufmgr *bufmgr);
137 
138     /**
139      * Indicate if the buffer can be placed anywhere in the full ppgtt
140      * address range (2^48).
141      *
142      * Any resource used with flat/heapless (0x00000000-0xfffff000)
143      * General State Heap (GSH) or Intructions State Heap (ISH) must
144      * be in a 32-bit range. 48-bit range will only be used when explicitly
145      * requested.
146      *
147      * \param bo Buffer to set the use_48b_address_range flag.
148      * \param enable The flag value.
149      */
150     void (*bo_use_48b_address_range) (struct mos_linux_bo *bo, uint32_t enable);
151 
152     /**
153      * Sets buffer total padded size when buffer is used by the GPU.
154      *
155      * This enables dynamic padding to be added without using any backing
156      * storage. For example handling GPU padding requirements for buffers
157      * allocated by an entity unaware of the same.
158      *
159      * Set padded size remains active until reset (to zero or actual object
160      * size).
161      *
162      * Returns 0 on success or an error code.
163      *
164      * \param bo Buffer to set total padded size for
165      * \param pad_to_size Total size in bytes of object plus padding
166      */
167     int (*bo_pad_to_size) (struct mos_linux_bo *bo, uint64_t pad_to_size);
168 
169     /**
170      * Add relocation entry in reloc_buf, which will be updated with the
171      * target buffer's real offset on on command submission.
172      *
173      * Relocations remain in place for the lifetime of the buffer object.
174      *
175      * \param bo Buffer to write the relocation into.
176      * \param offset Byte offset within reloc_bo of the pointer to
177      *            target_bo.
178      * \param target_bo Buffer whose offset should be written into the
179      *                  relocation entry.
180      * \param target_offset Constant value to be added to target_bo's
181      *            offset in relocation entry.
182      * \param read_domains GEM read domains which the buffer will be
183      *            read into by the command that this relocation
184      *            is part of.
185      * \param write_domains GEM read domains which the buffer will be
186      *            dirtied in by the command that this
187      *            relocation is part of.
188      */
189     int (*bo_emit_reloc) (struct mos_linux_bo *bo, uint32_t offset,
190                   struct mos_linux_bo *target_bo, uint32_t target_offset,
191                   uint32_t read_domains, uint32_t write_domain);
192     int (*bo_emit_reloc2) (struct mos_linux_bo *bo, uint32_t offset,
193                   struct mos_linux_bo *target_bo, uint32_t target_offset,
194                   uint32_t read_domains, uint32_t write_domain,
195                   uint64_t presumed_offset);
196     int (*bo_emit_reloc_fence)(struct mos_linux_bo *bo, uint32_t offset,
197                    struct mos_linux_bo *target_bo,
198                    uint32_t target_offset,
199                    uint32_t read_domains,
200                    uint32_t write_domain);
201 
202     /** Executes the command buffer pointed to by bo. */
203     int (*bo_exec) (struct mos_linux_bo *bo, int used,
204             drm_clip_rect_t *cliprects, int num_cliprects,
205             int DR4);
206     /** Executes the command buffer pointed to by bo on the selected
207      * ring buffer
208      */
209     int (*bo_mrb_exec) (struct mos_linux_bo *bo, int used,
210                 drm_clip_rect_t *cliprects, int num_cliprects,
211                 int DR4, unsigned flags);
212 
213     /**
214      * Pin a buffer to the aperture and fix the offset until unpinned
215      *
216      * \param buf Buffer to pin
217      * \param alignment Required alignment for aperture, in bytes
218      */
219     int (*bo_pin) (struct mos_linux_bo *bo, uint32_t alignment);
220 
221     /**
222      * Unpin a buffer from the aperture, allowing it to be removed
223      *
224      * \param buf Buffer to unpin
225      */
226     int (*bo_unpin) (struct mos_linux_bo *bo);
227 
228     /**
229      * Ask that the buffer be placed in tiling mode
230      *
231      * \param buf Buffer to set tiling mode for
232      * \param tiling_mode desired, and returned tiling mode
233      */
234     int (*bo_set_tiling) (struct mos_linux_bo *bo, uint32_t * tiling_mode,
235                   uint32_t stride);
236 
237     /**
238      * Get the current tiling (and resulting swizzling) mode for the bo.
239      *
240      * \param buf Buffer to get tiling mode for
241      * \param tiling_mode returned tiling mode
242      * \param swizzle_mode returned swizzling mode
243      */
244     int (*bo_get_tiling) (struct mos_linux_bo *bo, uint32_t * tiling_mode,
245                   uint32_t * swizzle_mode);
246 
247     /**
248      * Softpin the buffer object
249      * \param bo Buffer to set the softpin
250      */
251     int (*bo_set_softpin) (struct mos_linux_bo *bo);
252 
253     /**
254      * Add softpin object to softpin target list of the command buffer
255      * \param bo Command buffer which store the softpin target list
256      * \param target_bo  Softpin target to be added
257      * \param write_flag  Whether write flag is needed
258      */
259     int (*bo_add_softpin_target) (struct mos_linux_bo *bo, struct mos_linux_bo *target_bo, bool write_flag);
260 
261     /**
262      * Create a visible name for a buffer which can be used by other apps
263      *
264      * \param buf Buffer to create a name for
265      * \param name Returned name
266      */
267     int (*bo_flink) (struct mos_linux_bo *bo, uint32_t * name);
268 
269     /**
270      * Returns 1 if mapping the buffer for write could cause the process
271      * to block, due to the object being active in the GPU.
272      */
273     int (*bo_busy) (struct mos_linux_bo *bo);
274 
275     /**
276      * Specify the volatility of the buffer.
277      * \param bo Buffer to create a name for
278      * \param madv The purgeable status
279      *
280      * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
281      * reclaimed under memory pressure. If you subsequently require the buffer,
282      * then you must pass I915_MADV_WILLNEED to mark the buffer as required.
283      *
284      * Returns 1 if the buffer was retained, or 0 if it was discarded whilst
285      * marked as I915_MADV_DONTNEED.
286      */
287     int (*bo_madvise) (struct mos_linux_bo *bo, int madv);
288 
289     int (*check_aperture_space) (struct mos_linux_bo ** bo_array, int count);
290 
291     /**
292      * Disable buffer reuse for buffers which will be shared in some way,
293      * as with scanout buffers. When the buffer reference count goes to
294      * zero, it will be freed and not placed in the reuse list.
295      *
296      * \param bo Buffer to disable reuse for
297      */
298     int (*bo_disable_reuse) (struct mos_linux_bo *bo);
299 
300     /**
301      * Query whether a buffer is reusable.
302      *
303      * \param bo Buffer to query
304      */
305     int (*bo_is_reusable) (struct mos_linux_bo *bo);
306 
307     /**
308      *
309      * Return the pipe associated with a crtc_id so that vblank
310      * synchronization can use the correct data in the request.
311      * This is only supported for KMS and gem at this point, when
312      * unsupported, this function returns -1 and leaves the decision
313      * of what to do in that case to the caller
314      *
315      * \param bufmgr the associated buffer manager
316      * \param crtc_id the crtc identifier
317      */
318     int (*get_pipe_from_crtc_id) (struct mos_bufmgr *bufmgr, int crtc_id);
319 
320     /** Returns true if target_bo is in the relocation tree rooted at bo. */
321     int (*bo_references) (struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
322 
323     /**
324      * Set async flag for a buffer object.
325      *
326      * \param bo Buffer to set async
327      */
328     void (*set_object_async) (struct mos_linux_bo *bo);
329 
330     /**
331      * Set execution async flag for a buffer object.
332      *
333      * \param bo Command buffer
334      * \param target_bo Buffer to set async
335      */
336     void (*set_exec_object_async) (struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
337 
338      /**
339      * Set capture flag for a buffer object.
340      *
341      * \param bo Buffer to set capture
342      */
343     void (*set_object_capture)(struct mos_linux_bo *bo);
344 
345     /**< Enables verbose debugging printouts */
346     int debug;
347     uint32_t *get_reserved = nullptr;
348     bool     has_full_vd   = true;
349 };
350 
351 #ifdef ALIGN
352 /* <sys/param.h> via <pthread_np.h> on FreeBSD also defines ALIGN */
353 #undef ALIGN
354 #endif
355 #define ALIGN(value, alignment)    ((value + alignment - 1) & ~(alignment - 1))
356 #define ROUND_UP_TO(x, y)    (((x) + (y) - 1) / (y) * (y))
357 #define ROUND_UP_TO_MB(x)    ROUND_UP_TO((x), 1024*1024)
358 
359 #endif /* INTEL_BUFMGR_PRIV_H */
360