122944501Smrg/*
222944501Smrg * Copyright © 2008 Intel Corporation
322944501Smrg *
422944501Smrg * Permission is hereby granted, free of charge, to any person obtaining a
522944501Smrg * copy of this software and associated documentation files (the "Software"),
622944501Smrg * to deal in the Software without restriction, including without limitation
722944501Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
822944501Smrg * and/or sell copies of the Software, and to permit persons to whom the
922944501Smrg * Software is furnished to do so, subject to the following conditions:
1022944501Smrg *
1122944501Smrg * The above copyright notice and this permission notice (including the next
1222944501Smrg * paragraph) shall be included in all copies or substantial portions of the
1322944501Smrg * Software.
1422944501Smrg *
1522944501Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1622944501Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1722944501Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1822944501Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1922944501Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2022944501Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2122944501Smrg * IN THE SOFTWARE.
2222944501Smrg *
2322944501Smrg * Authors:
2422944501Smrg *    Eric Anholt <eric@anholt.net>
2522944501Smrg *
2622944501Smrg */
2722944501Smrg
2822944501Smrg/**
2922944501Smrg * @file intel_bufmgr_priv.h
3022944501Smrg *
3122944501Smrg * Private definitions of Intel-specific bufmgr functions and structures.
3222944501Smrg */
3322944501Smrg
3422944501Smrg#ifndef INTEL_BUFMGR_PRIV_H
3522944501Smrg#define INTEL_BUFMGR_PRIV_H
3622944501Smrg
3722944501Smrg/**
3822944501Smrg * Context for a buffer manager instance.
3922944501Smrg *
4022944501Smrg * Contains public methods followed by private storage for the buffer manager.
4122944501Smrg */
4222944501Smrgstruct _drm_intel_bufmgr {
4322944501Smrg	/**
4422944501Smrg	 * Allocate a buffer object.
4522944501Smrg	 *
4622944501Smrg	 * Buffer objects are not necessarily initially mapped into CPU virtual
4722944501Smrg	 * address space or graphics device aperture.  They must be mapped
4822944501Smrg	 * using bo_map() or drm_intel_gem_bo_map_gtt() to be used by the CPU.
4922944501Smrg	 */
5022944501Smrg	drm_intel_bo *(*bo_alloc) (drm_intel_bufmgr *bufmgr, const char *name,
5122944501Smrg				   unsigned long size, unsigned int alignment);
5222944501Smrg
5322944501Smrg	/**
5422944501Smrg	 * Allocate a buffer object, hinting that it will be used as a
5522944501Smrg	 * render target.
5622944501Smrg	 *
5722944501Smrg	 * This is otherwise the same as bo_alloc.
5822944501Smrg	 */
5922944501Smrg	drm_intel_bo *(*bo_alloc_for_render) (drm_intel_bufmgr *bufmgr,
6022944501Smrg					      const char *name,
6122944501Smrg					      unsigned long size,
6222944501Smrg					      unsigned int alignment);
6322944501Smrg
64baaff307Smrg	/**
65baaff307Smrg	 * Allocate a buffer object from an existing user accessible
66baaff307Smrg	 * address malloc'd with the provided size.
67baaff307Smrg	 * Alignment is used when mapping to the gtt.
68baaff307Smrg	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
69baaff307Smrg	 */
70baaff307Smrg	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
71baaff307Smrg					  const char *name, void *addr,
72baaff307Smrg					  uint32_t tiling_mode, uint32_t stride,
73baaff307Smrg					  unsigned long size,
74baaff307Smrg					  unsigned long flags);
75baaff307Smrg
7622944501Smrg	/**
7722944501Smrg	 * Allocate a tiled buffer object.
7822944501Smrg	 *
7922944501Smrg	 * Alignment for tiled objects is set automatically; the 'flags'
8022944501Smrg	 * argument provides a hint about how the object will be used initially.
8122944501Smrg	 *
8222944501Smrg	 * Valid tiling formats are:
8322944501Smrg	 *  I915_TILING_NONE
8422944501Smrg	 *  I915_TILING_X
8522944501Smrg	 *  I915_TILING_Y
8622944501Smrg	 *
8722944501Smrg	 * Note the tiling format may be rejected; callers should check the
8822944501Smrg	 * 'tiling_mode' field on return, as well as the pitch value, which
8922944501Smrg	 * may have been rounded up to accommodate for tiling restrictions.
9022944501Smrg	 */
9122944501Smrg	drm_intel_bo *(*bo_alloc_tiled) (drm_intel_bufmgr *bufmgr,
9222944501Smrg					 const char *name,
9322944501Smrg					 int x, int y, int cpp,
9422944501Smrg					 uint32_t *tiling_mode,
9522944501Smrg					 unsigned long *pitch,
9622944501Smrg					 unsigned long flags);
9722944501Smrg
9822944501Smrg	/** Takes a reference on a buffer object */
9922944501Smrg	void (*bo_reference) (drm_intel_bo *bo);
10022944501Smrg
10122944501Smrg	/**
10222944501Smrg	 * Releases a reference on a buffer object, freeing the data if
10322944501Smrg	 * no references remain.
10422944501Smrg	 */
10522944501Smrg	void (*bo_unreference) (drm_intel_bo *bo);
10622944501Smrg
10722944501Smrg	/**
10822944501Smrg	 * Maps the buffer into userspace.
10922944501Smrg	 *
11022944501Smrg	 * This function will block waiting for any existing execution on the
11122944501Smrg	 * buffer to complete, first.  The resulting mapping is available at
11222944501Smrg	 * buf->virtual.
11322944501Smrg	 */
11422944501Smrg	int (*bo_map) (drm_intel_bo *bo, int write_enable);
11522944501Smrg
11622944501Smrg	/**
11722944501Smrg	 * Reduces the refcount on the userspace mapping of the buffer
11822944501Smrg	 * object.
11922944501Smrg	 */
12022944501Smrg	int (*bo_unmap) (drm_intel_bo *bo);
12122944501Smrg
12222944501Smrg	/**
12322944501Smrg	 * Write data into an object.
12422944501Smrg	 *
12522944501Smrg	 * This is an optional function, if missing,
12622944501Smrg	 * drm_intel_bo will map/memcpy/unmap.
12722944501Smrg	 */
12822944501Smrg	int (*bo_subdata) (drm_intel_bo *bo, unsigned long offset,
12922944501Smrg			   unsigned long size, const void *data);
13022944501Smrg
13122944501Smrg	/**
13222944501Smrg	 * Read data from an object
13322944501Smrg	 *
13422944501Smrg	 * This is an optional function, if missing,
13522944501Smrg	 * drm_intel_bo will map/memcpy/unmap.
13622944501Smrg	 */
13722944501Smrg	int (*bo_get_subdata) (drm_intel_bo *bo, unsigned long offset,
13822944501Smrg			       unsigned long size, void *data);
13922944501Smrg
14022944501Smrg	/**
14122944501Smrg	 * Waits for rendering to an object by the GPU to have completed.
14222944501Smrg	 *
14322944501Smrg	 * This is not required for any access to the BO by bo_map,
14422944501Smrg	 * bo_subdata, etc.  It is merely a way for the driver to implement
14522944501Smrg	 * glFinish.
14622944501Smrg	 */
14722944501Smrg	void (*bo_wait_rendering) (drm_intel_bo *bo);
14822944501Smrg
14922944501Smrg	/**
15022944501Smrg	 * Tears down the buffer manager instance.
15122944501Smrg	 */
15222944501Smrg	void (*destroy) (drm_intel_bufmgr *bufmgr);
15322944501Smrg
1543f012e29Smrg	/**
1553f012e29Smrg	 * Indicate if the buffer can be placed anywhere in the full ppgtt
1563f012e29Smrg	 * address range (2^48).
1573f012e29Smrg	 *
1583f012e29Smrg	 * Any resource used with flat/heapless (0x00000000-0xfffff000)
1595324fb0dSmrg	 * General State Heap (GSH) or Instructions State Heap (ISH) must
1603f012e29Smrg	 * be in a 32-bit range. 48-bit range will only be used when explicitly
1613f012e29Smrg	 * requested.
1623f012e29Smrg	 *
1633f012e29Smrg	 * \param bo Buffer to set the use_48b_address_range flag.
1643f012e29Smrg	 * \param enable The flag value.
1653f012e29Smrg	 */
1663f012e29Smrg	void (*bo_use_48b_address_range) (drm_intel_bo *bo, uint32_t enable);
1673f012e29Smrg
16822944501Smrg	/**
16922944501Smrg	 * Add relocation entry in reloc_buf, which will be updated with the
17022944501Smrg	 * target buffer's real offset on on command submission.
17122944501Smrg	 *
17222944501Smrg	 * Relocations remain in place for the lifetime of the buffer object.
17322944501Smrg	 *
17422944501Smrg	 * \param bo Buffer to write the relocation into.
17522944501Smrg	 * \param offset Byte offset within reloc_bo of the pointer to
17622944501Smrg	 *			target_bo.
17722944501Smrg	 * \param target_bo Buffer whose offset should be written into the
17822944501Smrg	 *                  relocation entry.
17922944501Smrg	 * \param target_offset Constant value to be added to target_bo's
18022944501Smrg	 *			offset in relocation entry.
18122944501Smrg	 * \param read_domains GEM read domains which the buffer will be
18222944501Smrg	 *			read into by the command that this relocation
18322944501Smrg	 *			is part of.
18422944501Smrg	 * \param write_domains GEM read domains which the buffer will be
18522944501Smrg	 *			dirtied in by the command that this
18622944501Smrg	 *			relocation is part of.
18722944501Smrg	 */
18822944501Smrg	int (*bo_emit_reloc) (drm_intel_bo *bo, uint32_t offset,
18922944501Smrg			      drm_intel_bo *target_bo, uint32_t target_offset,
19022944501Smrg			      uint32_t read_domains, uint32_t write_domain);
19122944501Smrg	int (*bo_emit_reloc_fence)(drm_intel_bo *bo, uint32_t offset,
19222944501Smrg				   drm_intel_bo *target_bo,
19322944501Smrg				   uint32_t target_offset,
19422944501Smrg				   uint32_t read_domains,
19522944501Smrg				   uint32_t write_domain);
19622944501Smrg
19722944501Smrg	/** Executes the command buffer pointed to by bo. */
19822944501Smrg	int (*bo_exec) (drm_intel_bo *bo, int used,
19922944501Smrg			drm_clip_rect_t *cliprects, int num_cliprects,
20022944501Smrg			int DR4);
20122944501Smrg
20213d1d17dSmrg	/** Executes the command buffer pointed to by bo on the selected
20313d1d17dSmrg	 * ring buffer
20413d1d17dSmrg	 */
20513d1d17dSmrg	int (*bo_mrb_exec) (drm_intel_bo *bo, int used,
206e88f27b3Smrg			    drm_clip_rect_t *cliprects, int num_cliprects,
207e88f27b3Smrg			    int DR4, unsigned flags);
20813d1d17dSmrg
20922944501Smrg	/**
21022944501Smrg	 * Pin a buffer to the aperture and fix the offset until unpinned
21122944501Smrg	 *
21222944501Smrg	 * \param buf Buffer to pin
21322944501Smrg	 * \param alignment Required alignment for aperture, in bytes
21422944501Smrg	 */
21522944501Smrg	int (*bo_pin) (drm_intel_bo *bo, uint32_t alignment);
21622944501Smrg
21722944501Smrg	/**
21822944501Smrg	 * Unpin a buffer from the aperture, allowing it to be removed
21922944501Smrg	 *
22022944501Smrg	 * \param buf Buffer to unpin
22122944501Smrg	 */
22222944501Smrg	int (*bo_unpin) (drm_intel_bo *bo);
22322944501Smrg
22422944501Smrg	/**
22522944501Smrg	 * Ask that the buffer be placed in tiling mode
22622944501Smrg	 *
22722944501Smrg	 * \param buf Buffer to set tiling mode for
22822944501Smrg	 * \param tiling_mode desired, and returned tiling mode
22922944501Smrg	 */
23022944501Smrg	int (*bo_set_tiling) (drm_intel_bo *bo, uint32_t * tiling_mode,
23122944501Smrg			      uint32_t stride);
23222944501Smrg
23322944501Smrg	/**
23422944501Smrg	 * Get the current tiling (and resulting swizzling) mode for the bo.
23522944501Smrg	 *
23622944501Smrg	 * \param buf Buffer to get tiling mode for
23722944501Smrg	 * \param tiling_mode returned tiling mode
23822944501Smrg	 * \param swizzle_mode returned swizzling mode
23922944501Smrg	 */
24022944501Smrg	int (*bo_get_tiling) (drm_intel_bo *bo, uint32_t * tiling_mode,
24122944501Smrg			      uint32_t * swizzle_mode);
24222944501Smrg
2433f012e29Smrg	/**
2443f012e29Smrg	 * Set the offset at which this buffer will be softpinned
2453f012e29Smrg	 * \param bo Buffer to set the softpin offset for
2463f012e29Smrg	 * \param offset Softpin offset
2473f012e29Smrg	 */
2483f012e29Smrg	int (*bo_set_softpin_offset) (drm_intel_bo *bo, uint64_t offset);
2493f012e29Smrg
25022944501Smrg	/**
25122944501Smrg	 * Create a visible name for a buffer which can be used by other apps
25222944501Smrg	 *
25322944501Smrg	 * \param buf Buffer to create a name for
25422944501Smrg	 * \param name Returned name
25522944501Smrg	 */
25622944501Smrg	int (*bo_flink) (drm_intel_bo *bo, uint32_t * name);
25722944501Smrg
25822944501Smrg	/**
25922944501Smrg	 * Returns 1 if mapping the buffer for write could cause the process
26022944501Smrg	 * to block, due to the object being active in the GPU.
26122944501Smrg	 */
26222944501Smrg	int (*bo_busy) (drm_intel_bo *bo);
26322944501Smrg
26422944501Smrg	/**
26522944501Smrg	 * Specify the volatility of the buffer.
26622944501Smrg	 * \param bo Buffer to create a name for
26722944501Smrg	 * \param madv The purgeable status
26822944501Smrg	 *
26922944501Smrg	 * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
27022944501Smrg	 * reclaimed under memory pressure. If you subsequently require the buffer,
27122944501Smrg	 * then you must pass I915_MADV_WILLNEED to mark the buffer as required.
27222944501Smrg	 *
27322944501Smrg	 * Returns 1 if the buffer was retained, or 0 if it was discarded whilst
27422944501Smrg	 * marked as I915_MADV_DONTNEED.
27522944501Smrg	 */
27622944501Smrg	int (*bo_madvise) (drm_intel_bo *bo, int madv);
27722944501Smrg
27822944501Smrg	int (*check_aperture_space) (drm_intel_bo ** bo_array, int count);
27922944501Smrg
28022944501Smrg	/**
28122944501Smrg	 * Disable buffer reuse for buffers which will be shared in some way,
28222944501Smrg	 * as with scanout buffers. When the buffer reference count goes to
28322944501Smrg	 * zero, it will be freed and not placed in the reuse list.
28422944501Smrg	 *
28522944501Smrg	 * \param bo Buffer to disable reuse for
28622944501Smrg	 */
28722944501Smrg	int (*bo_disable_reuse) (drm_intel_bo *bo);
28822944501Smrg
28913d1d17dSmrg	/**
29013d1d17dSmrg	 * Query whether a buffer is reusable.
29113d1d17dSmrg	 *
29213d1d17dSmrg	 * \param bo Buffer to query
29313d1d17dSmrg	 */
29413d1d17dSmrg	int (*bo_is_reusable) (drm_intel_bo *bo);
29513d1d17dSmrg
29622944501Smrg	/**
29722944501Smrg	 *
29822944501Smrg	 * Return the pipe associated with a crtc_id so that vblank
29922944501Smrg	 * synchronization can use the correct data in the request.
30022944501Smrg	 * This is only supported for KMS and gem at this point, when
30122944501Smrg	 * unsupported, this function returns -1 and leaves the decision
30222944501Smrg	 * of what to do in that case to the caller
30322944501Smrg	 *
30422944501Smrg	 * \param bufmgr the associated buffer manager
30522944501Smrg	 * \param crtc_id the crtc identifier
30622944501Smrg	 */
30722944501Smrg	int (*get_pipe_from_crtc_id) (drm_intel_bufmgr *bufmgr, int crtc_id);
30822944501Smrg
30922944501Smrg	/** Returns true if target_bo is in the relocation tree rooted at bo. */
31022944501Smrg	int (*bo_references) (drm_intel_bo *bo, drm_intel_bo *target_bo);
31122944501Smrg
31222944501Smrg	/**< Enables verbose debugging printouts */
31322944501Smrg	int debug;
31422944501Smrg};
31522944501Smrg
316e88f27b3Smrgstruct _drm_intel_context {
317e88f27b3Smrg	unsigned int ctx_id;
318e88f27b3Smrg	struct _drm_intel_bufmgr *bufmgr;
319e88f27b3Smrg};
320e88f27b3Smrg
32122944501Smrg#define ALIGN(value, alignment)	((value + alignment - 1) & ~(alignment - 1))
32222944501Smrg#define ROUND_UP_TO(x, y)	(((x) + (y) - 1) / (y) * (y))
32322944501Smrg#define ROUND_UP_TO_MB(x)	ROUND_UP_TO((x), 1024*1024)
32422944501Smrg
32522944501Smrg#endif /* INTEL_BUFMGR_PRIV_H */
326