1dff96888SDirk Hohndel (VMware) /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2543831cfSThomas Hellstrom /**************************************************************************
3543831cfSThomas Hellstrom  *
4dff96888SDirk Hohndel (VMware)  * Copyright 2012-2014 VMware, Inc., Palo Alto, CA., USA
5543831cfSThomas Hellstrom  *
6543831cfSThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
7543831cfSThomas Hellstrom  * copy of this software and associated documentation files (the
8543831cfSThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
9543831cfSThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
10543831cfSThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
11543831cfSThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
12543831cfSThomas Hellstrom  * the following conditions:
13543831cfSThomas Hellstrom  *
14543831cfSThomas Hellstrom  * The above copyright notice and this permission notice (including the
15543831cfSThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
16543831cfSThomas Hellstrom  * of the Software.
17543831cfSThomas Hellstrom  *
18543831cfSThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19543831cfSThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20543831cfSThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21543831cfSThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22543831cfSThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23543831cfSThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24543831cfSThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25543831cfSThomas Hellstrom  *
26543831cfSThomas Hellstrom  **************************************************************************/
27543831cfSThomas Hellstrom 
28543831cfSThomas Hellstrom #ifndef _VMWGFX_RESOURCE_PRIV_H_
29543831cfSThomas Hellstrom #define _VMWGFX_RESOURCE_PRIV_H_
30543831cfSThomas Hellstrom 
31543831cfSThomas Hellstrom #include "vmwgfx_drv.h"
32543831cfSThomas Hellstrom 
33c7eae626SThomas Hellstrom /*
34c7eae626SThomas Hellstrom  * Extra memory required by the resource id's ida storage, which is allocated
35c7eae626SThomas Hellstrom  * separately from the base object itself. We estimate an on-average 128 bytes
36c7eae626SThomas Hellstrom  * per ida.
37c7eae626SThomas Hellstrom  */
38d4d21902SThomas Hellstrom #define VMW_IDA_ACC_SIZE 128
39d4d21902SThomas Hellstrom 
40d80efd5cSThomas Hellstrom enum vmw_cmdbuf_res_state {
41d80efd5cSThomas Hellstrom 	VMW_CMDBUF_RES_COMMITTED,
42d80efd5cSThomas Hellstrom 	VMW_CMDBUF_RES_ADD,
43d80efd5cSThomas Hellstrom 	VMW_CMDBUF_RES_DEL
44d80efd5cSThomas Hellstrom };
45d80efd5cSThomas Hellstrom 
46543831cfSThomas Hellstrom /**
47543831cfSThomas Hellstrom  * struct vmw_user_resource_conv - Identify a derived user-exported resource
48543831cfSThomas Hellstrom  * type and provide a function to convert its ttm_base_object pointer to
49543831cfSThomas Hellstrom  * a struct vmw_resource
50543831cfSThomas Hellstrom  */
51543831cfSThomas Hellstrom struct vmw_user_resource_conv {
52543831cfSThomas Hellstrom 	enum ttm_object_type object_type;
53543831cfSThomas Hellstrom 	struct vmw_resource *(*base_obj_to_res)(struct ttm_base_object *base);
54543831cfSThomas Hellstrom 	void (*res_free) (struct vmw_resource *res);
55543831cfSThomas Hellstrom };
56543831cfSThomas Hellstrom 
57543831cfSThomas Hellstrom /**
58543831cfSThomas Hellstrom  * struct vmw_res_func - members and functions common for a resource type
59543831cfSThomas Hellstrom  *
60543831cfSThomas Hellstrom  * @res_type:          Enum that identifies the lru list to use for eviction.
61*668b2066SZack Rusin  * @needs_guest_memory:Whether the resource is guest-backed and needs
62543831cfSThomas Hellstrom  *                     persistent buffer storage.
63543831cfSThomas Hellstrom  * @type_name:         String that identifies the resource type.
64*668b2066SZack Rusin  * @domain:            TTM placement for guest memory buffers.
65*668b2066SZack Rusin  * @busy_domain:       TTM busy placement for guest memory buffers.
66543831cfSThomas Hellstrom  * @may_evict          Whether the resource may be evicted.
67543831cfSThomas Hellstrom  * @create:            Create a hardware resource.
68543831cfSThomas Hellstrom  * @destroy:           Destroy a hardware resource.
69543831cfSThomas Hellstrom  * @bind:              Bind a hardware resource to persistent buffer storage.
70543831cfSThomas Hellstrom  * @unbind:            Unbind a hardware resource from persistent
71543831cfSThomas Hellstrom  *                     buffer storage.
72d80efd5cSThomas Hellstrom  * @commit_notify:     If the resource is a command buffer managed resource,
73d80efd5cSThomas Hellstrom  *                     callback to notify that a define or remove command
74d80efd5cSThomas Hellstrom  *                     has been committed to the device.
75b7468b15SThomas Hellstrom  * @dirty_alloc:       Allocate a dirty tracker. NULL if dirty-tracking is not
76b7468b15SThomas Hellstrom  *                     supported.
77b7468b15SThomas Hellstrom  * @dirty_free:        Free the dirty tracker.
78b7468b15SThomas Hellstrom  * @dirty_sync:        Upload the dirty mob contents to the resource.
79b7468b15SThomas Hellstrom  * @dirty_add_range:   Add a sequential dirty range to the resource
80b7468b15SThomas Hellstrom  *                     dirty tracker.
81fb80edb0SThomas Hellstrom  * @clean:             Clean the resource.
82543831cfSThomas Hellstrom  */
83543831cfSThomas Hellstrom struct vmw_res_func {
84543831cfSThomas Hellstrom 	enum vmw_res_type res_type;
85*668b2066SZack Rusin 	bool needs_guest_memory;
86543831cfSThomas Hellstrom 	const char *type_name;
8739985eeaSZack Rusin 	u32 domain;
8839985eeaSZack Rusin 	u32 busy_domain;
89543831cfSThomas Hellstrom 	bool may_evict;
90a0a63940SThomas Hellstrom 	u32 prio;
91a0a63940SThomas Hellstrom 	u32 dirty_prio;
92543831cfSThomas Hellstrom 
93543831cfSThomas Hellstrom 	int (*create) (struct vmw_resource *res);
94543831cfSThomas Hellstrom 	int (*destroy) (struct vmw_resource *res);
95543831cfSThomas Hellstrom 	int (*bind) (struct vmw_resource *res,
96543831cfSThomas Hellstrom 		     struct ttm_validate_buffer *val_buf);
97543831cfSThomas Hellstrom 	int (*unbind) (struct vmw_resource *res,
98543831cfSThomas Hellstrom 		       bool readback,
99543831cfSThomas Hellstrom 		       struct ttm_validate_buffer *val_buf);
100d80efd5cSThomas Hellstrom 	void (*commit_notify)(struct vmw_resource *res,
101d80efd5cSThomas Hellstrom 			      enum vmw_cmdbuf_res_state state);
102b7468b15SThomas Hellstrom 	int (*dirty_alloc)(struct vmw_resource *res);
103b7468b15SThomas Hellstrom 	void (*dirty_free)(struct vmw_resource *res);
104b7468b15SThomas Hellstrom 	int (*dirty_sync)(struct vmw_resource *res);
105b7468b15SThomas Hellstrom 	void (*dirty_range_add)(struct vmw_resource *res, size_t start,
106b7468b15SThomas Hellstrom 				 size_t end);
107fb80edb0SThomas Hellstrom 	int (*clean)(struct vmw_resource *res);
108543831cfSThomas Hellstrom };
109543831cfSThomas Hellstrom 
110d4d21902SThomas Hellstrom /**
111d4d21902SThomas Hellstrom  * struct vmw_simple_resource_func - members and functions common for the
112d4d21902SThomas Hellstrom  * simple resource helpers.
113d4d21902SThomas Hellstrom  * @res_func:  struct vmw_res_func as described above.
114d4d21902SThomas Hellstrom  * @ttm_res_type:  TTM resource type used for handle recognition.
115d4d21902SThomas Hellstrom  * @size:  Size of the simple resource information struct.
116d4d21902SThomas Hellstrom  * @init:  Initialize the simple resource information.
117d4d21902SThomas Hellstrom  * @hw_destroy:  A resource hw_destroy function.
118d4d21902SThomas Hellstrom  * @set_arg_handle:  Set the handle output argument of the ioctl create struct.
119d4d21902SThomas Hellstrom  */
120d4d21902SThomas Hellstrom struct vmw_simple_resource_func {
121d4d21902SThomas Hellstrom 	const struct vmw_res_func res_func;
122d4d21902SThomas Hellstrom 	int ttm_res_type;
123d4d21902SThomas Hellstrom 	size_t size;
124d4d21902SThomas Hellstrom 	int (*init)(struct vmw_resource *res, void *data);
125d4d21902SThomas Hellstrom 	void (*hw_destroy)(struct vmw_resource *res);
126d4d21902SThomas Hellstrom 	void (*set_arg_handle)(void *data, u32 handle);
127d4d21902SThomas Hellstrom };
128d4d21902SThomas Hellstrom 
129d4d21902SThomas Hellstrom /**
130d4d21902SThomas Hellstrom  * struct vmw_simple_resource - Kernel only side simple resource
131d4d21902SThomas Hellstrom  * @res: The resource we derive from.
132d4d21902SThomas Hellstrom  * @func: The method and member virtual table.
133d4d21902SThomas Hellstrom  */
134d4d21902SThomas Hellstrom struct vmw_simple_resource {
135d4d21902SThomas Hellstrom 	struct vmw_resource res;
136d4d21902SThomas Hellstrom 	const struct vmw_simple_resource_func *func;
137d4d21902SThomas Hellstrom };
138d4d21902SThomas Hellstrom 
139543831cfSThomas Hellstrom int vmw_resource_alloc_id(struct vmw_resource *res);
140543831cfSThomas Hellstrom void vmw_resource_release_id(struct vmw_resource *res);
141543831cfSThomas Hellstrom int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
142543831cfSThomas Hellstrom 		      bool delay_id,
143543831cfSThomas Hellstrom 		      void (*res_free) (struct vmw_resource *res),
144543831cfSThomas Hellstrom 		      const struct vmw_res_func *func);
145d4d21902SThomas Hellstrom int
146d4d21902SThomas Hellstrom vmw_simple_resource_create_ioctl(struct drm_device *dev,
147d4d21902SThomas Hellstrom 				 void *data,
148d4d21902SThomas Hellstrom 				 struct drm_file *file_priv,
149d4d21902SThomas Hellstrom 				 const struct vmw_simple_resource_func *func);
150d4d21902SThomas Hellstrom struct vmw_resource *
151d4d21902SThomas Hellstrom vmw_simple_resource_lookup(struct ttm_object_file *tfile,
152d4d21902SThomas Hellstrom 			   uint32_t handle,
153d4d21902SThomas Hellstrom 			   const struct vmw_simple_resource_func *func);
154543831cfSThomas Hellstrom #endif
155