1*4882a593Smuzhiyun /**************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
7*4882a593Smuzhiyun * copy of this software and associated documentation files (the
8*4882a593Smuzhiyun * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun * without limitation the rights to use, copy, modify, merge, publish,
10*4882a593Smuzhiyun * distribute, sub license, and/or sell copies of the Software, and to
11*4882a593Smuzhiyun * permit persons to whom the Software is furnished to do so, subject to
12*4882a593Smuzhiyun * the following conditions:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun * next paragraph) shall be included in all copies or substantial portions
16*4882a593Smuzhiyun * of the Software.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*4882a593Smuzhiyun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*4882a593Smuzhiyun * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*4882a593Smuzhiyun * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun **************************************************************************/
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun /** @file ttm_object.h
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Base- and reference object implementation for the various
33*4882a593Smuzhiyun * ttm objects. Implements reference counting, minimal security checks
34*4882a593Smuzhiyun * and release on file close.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #ifndef _TTM_OBJECT_H_
38*4882a593Smuzhiyun #define _TTM_OBJECT_H_
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <linux/dma-buf.h>
41*4882a593Smuzhiyun #include <linux/kref.h>
42*4882a593Smuzhiyun #include <linux/list.h>
43*4882a593Smuzhiyun #include <linux/rcupdate.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include <drm/drm_hashtab.h>
46*4882a593Smuzhiyun #include <drm/ttm/ttm_memory.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /**
49*4882a593Smuzhiyun * enum ttm_ref_type
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Describes what type of reference a ref object holds.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * TTM_REF_USAGE is a simple refcount on a base object.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
56*4882a593Smuzhiyun * buffer object.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
59*4882a593Smuzhiyun * buffer object.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun enum ttm_ref_type {
64*4882a593Smuzhiyun TTM_REF_USAGE,
65*4882a593Smuzhiyun TTM_REF_SYNCCPU_READ,
66*4882a593Smuzhiyun TTM_REF_SYNCCPU_WRITE,
67*4882a593Smuzhiyun TTM_REF_NUM
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun * enum ttm_object_type
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * One entry per ttm object type.
74*4882a593Smuzhiyun * Device-specific types should use the
75*4882a593Smuzhiyun * ttm_driver_typex types.
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun enum ttm_object_type {
79*4882a593Smuzhiyun ttm_fence_type,
80*4882a593Smuzhiyun ttm_buffer_type,
81*4882a593Smuzhiyun ttm_lock_type,
82*4882a593Smuzhiyun ttm_prime_type,
83*4882a593Smuzhiyun ttm_driver_type0 = 256,
84*4882a593Smuzhiyun ttm_driver_type1,
85*4882a593Smuzhiyun ttm_driver_type2,
86*4882a593Smuzhiyun ttm_driver_type3,
87*4882a593Smuzhiyun ttm_driver_type4,
88*4882a593Smuzhiyun ttm_driver_type5
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct ttm_object_file;
92*4882a593Smuzhiyun struct ttm_object_device;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun * struct ttm_base_object
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * @hash: hash entry for the per-device object hash.
98*4882a593Smuzhiyun * @type: derived type this object is base class for.
99*4882a593Smuzhiyun * @shareable: Other ttm_object_files can access this object.
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * @tfile: Pointer to ttm_object_file of the creator.
102*4882a593Smuzhiyun * NULL if the object was not created by a user request.
103*4882a593Smuzhiyun * (kernel object).
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * @refcount: Number of references to this object, not
106*4882a593Smuzhiyun * including the hash entry. A reference to a base object can
107*4882a593Smuzhiyun * only be held by a ref object.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * @refcount_release: A function to be called when there are
110*4882a593Smuzhiyun * no more references to this object. This function should
111*4882a593Smuzhiyun * destroy the object (or make sure destruction eventually happens),
112*4882a593Smuzhiyun * and when it is called, the object has
113*4882a593Smuzhiyun * already been taken out of the per-device hash. The parameter
114*4882a593Smuzhiyun * "base" should be set to NULL by the function.
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * @ref_obj_release: A function to be called when a reference object
117*4882a593Smuzhiyun * with another ttm_ref_type than TTM_REF_USAGE is deleted.
118*4882a593Smuzhiyun * This function may, for example, release a lock held by a user-space
119*4882a593Smuzhiyun * process.
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * This struct is intended to be used as a base struct for objects that
122*4882a593Smuzhiyun * are visible to user-space. It provides a global name, race-safe
123*4882a593Smuzhiyun * access and refcounting, minimal access contol and hooks for unref actions.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun struct ttm_base_object {
127*4882a593Smuzhiyun struct rcu_head rhead;
128*4882a593Smuzhiyun struct ttm_object_file *tfile;
129*4882a593Smuzhiyun struct kref refcount;
130*4882a593Smuzhiyun void (*refcount_release) (struct ttm_base_object **base);
131*4882a593Smuzhiyun void (*ref_obj_release) (struct ttm_base_object *base,
132*4882a593Smuzhiyun enum ttm_ref_type ref_type);
133*4882a593Smuzhiyun u32 handle;
134*4882a593Smuzhiyun enum ttm_object_type object_type;
135*4882a593Smuzhiyun u32 shareable;
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun * struct ttm_prime_object - Modified base object that is prime-aware
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * @base: struct ttm_base_object that we derive from
143*4882a593Smuzhiyun * @mutex: Mutex protecting the @dma_buf member.
144*4882a593Smuzhiyun * @size: Size of the dma_buf associated with this object
145*4882a593Smuzhiyun * @real_type: Type of the underlying object. Needed since we're setting
146*4882a593Smuzhiyun * the value of @base::object_type to ttm_prime_type
147*4882a593Smuzhiyun * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
148*4882a593Smuzhiyun * object.
149*4882a593Smuzhiyun * @refcount_release: The underlying object's release method. Needed since
150*4882a593Smuzhiyun * we set @base::refcount_release to our own release method.
151*4882a593Smuzhiyun */
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun struct ttm_prime_object {
154*4882a593Smuzhiyun struct ttm_base_object base;
155*4882a593Smuzhiyun struct mutex mutex;
156*4882a593Smuzhiyun size_t size;
157*4882a593Smuzhiyun enum ttm_object_type real_type;
158*4882a593Smuzhiyun struct dma_buf *dma_buf;
159*4882a593Smuzhiyun void (*refcount_release) (struct ttm_base_object **);
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * ttm_base_object_init
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * @tfile: Pointer to a struct ttm_object_file.
166*4882a593Smuzhiyun * @base: The struct ttm_base_object to initialize.
167*4882a593Smuzhiyun * @shareable: This object is shareable with other applcations.
168*4882a593Smuzhiyun * (different @tfile pointers.)
169*4882a593Smuzhiyun * @type: The object type.
170*4882a593Smuzhiyun * @refcount_release: See the struct ttm_base_object description.
171*4882a593Smuzhiyun * @ref_obj_release: See the struct ttm_base_object description.
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Initializes a struct ttm_base_object.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun extern int ttm_base_object_init(struct ttm_object_file *tfile,
177*4882a593Smuzhiyun struct ttm_base_object *base,
178*4882a593Smuzhiyun bool shareable,
179*4882a593Smuzhiyun enum ttm_object_type type,
180*4882a593Smuzhiyun void (*refcount_release) (struct ttm_base_object
181*4882a593Smuzhiyun **),
182*4882a593Smuzhiyun void (*ref_obj_release) (struct ttm_base_object
183*4882a593Smuzhiyun *,
184*4882a593Smuzhiyun enum ttm_ref_type
185*4882a593Smuzhiyun ref_type));
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /**
188*4882a593Smuzhiyun * ttm_base_object_lookup
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * @tfile: Pointer to a struct ttm_object_file.
191*4882a593Smuzhiyun * @key: Hash key
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun * Looks up a struct ttm_base_object with the key @key.
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
197*4882a593Smuzhiyun *tfile, uint32_t key);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /**
200*4882a593Smuzhiyun * ttm_base_object_lookup_for_ref
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * @tdev: Pointer to a struct ttm_object_device.
203*4882a593Smuzhiyun * @key: Hash key
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * Looks up a struct ttm_base_object with the key @key.
206*4882a593Smuzhiyun * This function should only be used when the struct tfile associated with the
207*4882a593Smuzhiyun * caller doesn't yet have a reference to the base object.
208*4882a593Smuzhiyun */
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun extern struct ttm_base_object *
211*4882a593Smuzhiyun ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /**
214*4882a593Smuzhiyun * ttm_base_object_unref
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * Decrements the base object refcount and clears the pointer pointed to by
219*4882a593Smuzhiyun * p_base.
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun extern void ttm_base_object_unref(struct ttm_base_object **p_base);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * ttm_ref_object_add.
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * @tfile: A struct ttm_object_file representing the application owning the
228*4882a593Smuzhiyun * ref_object.
229*4882a593Smuzhiyun * @base: The base object to reference.
230*4882a593Smuzhiyun * @ref_type: The type of reference.
231*4882a593Smuzhiyun * @existed: Upon completion, indicates that an identical reference object
232*4882a593Smuzhiyun * already existed, and the refcount was upped on that object instead.
233*4882a593Smuzhiyun * @require_existed: Fail with -EPERM if an identical ref object didn't
234*4882a593Smuzhiyun * already exist.
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * Checks that the base object is shareable and adds a ref object to it.
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Adding a ref object to a base object is basically like referencing the
239*4882a593Smuzhiyun * base object, but a user-space application holds the reference. When the
240*4882a593Smuzhiyun * file corresponding to @tfile is closed, all its reference objects are
241*4882a593Smuzhiyun * deleted. A reference object can have different types depending on what
242*4882a593Smuzhiyun * it's intended for. It can be refcounting to prevent object destruction,
243*4882a593Smuzhiyun * When user-space takes a lock, it can add a ref object to that lock to
244*4882a593Smuzhiyun * make sure the lock is released if the application dies. A ref object
245*4882a593Smuzhiyun * will hold a single reference on a base object.
246*4882a593Smuzhiyun */
247*4882a593Smuzhiyun extern int ttm_ref_object_add(struct ttm_object_file *tfile,
248*4882a593Smuzhiyun struct ttm_base_object *base,
249*4882a593Smuzhiyun enum ttm_ref_type ref_type, bool *existed,
250*4882a593Smuzhiyun bool require_existed);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun extern bool ttm_ref_object_exists(struct ttm_object_file *tfile,
253*4882a593Smuzhiyun struct ttm_base_object *base);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun * ttm_ref_object_base_unref
257*4882a593Smuzhiyun *
258*4882a593Smuzhiyun * @key: Key representing the base object.
259*4882a593Smuzhiyun * @ref_type: Ref type of the ref object to be dereferenced.
260*4882a593Smuzhiyun *
261*4882a593Smuzhiyun * Unreference a ref object with type @ref_type
262*4882a593Smuzhiyun * on the base object identified by @key. If there are no duplicate
263*4882a593Smuzhiyun * references, the ref object will be destroyed and the base object
264*4882a593Smuzhiyun * will be unreferenced.
265*4882a593Smuzhiyun */
266*4882a593Smuzhiyun extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
267*4882a593Smuzhiyun unsigned long key,
268*4882a593Smuzhiyun enum ttm_ref_type ref_type);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /**
271*4882a593Smuzhiyun * ttm_object_file_init - initialize a struct ttm_object file
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * @tdev: A struct ttm_object device this file is initialized on.
274*4882a593Smuzhiyun * @hash_order: Order of the hash table used to hold the reference objects.
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * This is typically called by the file_ops::open function.
277*4882a593Smuzhiyun */
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
280*4882a593Smuzhiyun *tdev,
281*4882a593Smuzhiyun unsigned int hash_order);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /**
284*4882a593Smuzhiyun * ttm_object_file_release - release data held by a ttm_object_file
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
287*4882a593Smuzhiyun * *p_tfile will be set to NULL by this function.
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * Releases all data associated by a ttm_object_file.
290*4882a593Smuzhiyun * Typically called from file_ops::release. The caller must
291*4882a593Smuzhiyun * ensure that there are no concurrent users of tfile.
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /**
297*4882a593Smuzhiyun * ttm_object device init - initialize a struct ttm_object_device
298*4882a593Smuzhiyun *
299*4882a593Smuzhiyun * @mem_glob: struct ttm_mem_global for memory accounting.
300*4882a593Smuzhiyun * @hash_order: Order of hash table used to hash the base objects.
301*4882a593Smuzhiyun * @ops: DMA buf ops for prime objects of this device.
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * This function is typically called on device initialization to prepare
304*4882a593Smuzhiyun * data structures needed for ttm base and ref objects.
305*4882a593Smuzhiyun */
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun extern struct ttm_object_device *
308*4882a593Smuzhiyun ttm_object_device_init(struct ttm_mem_global *mem_glob,
309*4882a593Smuzhiyun unsigned int hash_order,
310*4882a593Smuzhiyun const struct dma_buf_ops *ops);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /**
313*4882a593Smuzhiyun * ttm_object_device_release - release data held by a ttm_object_device
314*4882a593Smuzhiyun *
315*4882a593Smuzhiyun * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
316*4882a593Smuzhiyun * *p_tdev will be set to NULL by this function.
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * Releases all data associated by a ttm_object_device.
319*4882a593Smuzhiyun * Typically called from driver::unload before the destruction of the
320*4882a593Smuzhiyun * device private data structure.
321*4882a593Smuzhiyun */
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun #define ttm_base_object_kfree(__object, __base)\
326*4882a593Smuzhiyun kfree_rcu(__object, __base.rhead)
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun extern int ttm_prime_object_init(struct ttm_object_file *tfile,
329*4882a593Smuzhiyun size_t size,
330*4882a593Smuzhiyun struct ttm_prime_object *prime,
331*4882a593Smuzhiyun bool shareable,
332*4882a593Smuzhiyun enum ttm_object_type type,
333*4882a593Smuzhiyun void (*refcount_release)
334*4882a593Smuzhiyun (struct ttm_base_object **),
335*4882a593Smuzhiyun void (*ref_obj_release)
336*4882a593Smuzhiyun (struct ttm_base_object *,
337*4882a593Smuzhiyun enum ttm_ref_type ref_type));
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun static inline enum ttm_object_type
ttm_base_object_type(struct ttm_base_object * base)340*4882a593Smuzhiyun ttm_base_object_type(struct ttm_base_object *base)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun return (base->object_type == ttm_prime_type) ?
343*4882a593Smuzhiyun container_of(base, struct ttm_prime_object, base)->real_type :
344*4882a593Smuzhiyun base->object_type;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
347*4882a593Smuzhiyun int fd, u32 *handle);
348*4882a593Smuzhiyun extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
349*4882a593Smuzhiyun uint32_t handle, uint32_t flags,
350*4882a593Smuzhiyun int *prime_fd);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun #define ttm_prime_object_kfree(__obj, __prime) \
353*4882a593Smuzhiyun kfree_rcu(__obj, __prime.base.rhead)
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /*
356*4882a593Smuzhiyun * Extra memory required by the base object's idr storage, which is allocated
357*4882a593Smuzhiyun * separately from the base object itself. We estimate an on-average 128 bytes
358*4882a593Smuzhiyun * per idr.
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun #define TTM_OBJ_EXTRA_SIZE 128
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun struct ttm_base_object *
363*4882a593Smuzhiyun ttm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /**
366*4882a593Smuzhiyun * ttm_base_object_noref_release - release a base object pointer looked up
367*4882a593Smuzhiyun * without reference
368*4882a593Smuzhiyun *
369*4882a593Smuzhiyun * Releases a base object pointer looked up with ttm_base_object_noref_lookup().
370*4882a593Smuzhiyun */
ttm_base_object_noref_release(void)371*4882a593Smuzhiyun static inline void ttm_base_object_noref_release(void)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun __acquire(RCU);
374*4882a593Smuzhiyun rcu_read_unlock();
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun #endif
377