OSDN Git Service

Merge "cnss2: Add support for genoa sdio"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / msm / kgsl.h
1 /* Copyright (c) 2008-2016,2018-2019, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  */
13 #ifndef __KGSL_H
14 #define __KGSL_H
15
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/msm_kgsl.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/interrupt.h>
23 #include <linux/mutex.h>
24 #include <linux/cdev.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/mm.h>
27 #include <linux/dma-attrs.h>
28 #include <linux/uaccess.h>
29 #include <linux/kthread.h>
30 #include <asm/cacheflush.h>
31
32 /*
33  * --- kgsl drawobj flags ---
34  * These flags are same as --- drawobj flags ---
35  * but renamed to reflect that cmdbatch is renamed to drawobj.
36  */
37 #define KGSL_DRAWOBJ_MEMLIST           KGSL_CMDBATCH_MEMLIST
38 #define KGSL_DRAWOBJ_MARKER            KGSL_CMDBATCH_MARKER
39 #define KGSL_DRAWOBJ_SUBMIT_IB_LIST    KGSL_CMDBATCH_SUBMIT_IB_LIST
40 #define KGSL_DRAWOBJ_CTX_SWITCH        KGSL_CMDBATCH_CTX_SWITCH
41 #define KGSL_DRAWOBJ_PROFILING         KGSL_CMDBATCH_PROFILING
42 #define KGSL_DRAWOBJ_PROFILING_KTIME   KGSL_CMDBATCH_PROFILING_KTIME
43 #define KGSL_DRAWOBJ_END_OF_FRAME      KGSL_CMDBATCH_END_OF_FRAME
44 #define KGSL_DRAWOBJ_SYNC              KGSL_CMDBATCH_SYNC
45 #define KGSL_DRAWOBJ_PWR_CONSTRAINT    KGSL_CMDBATCH_PWR_CONSTRAINT
46 #define KGSL_DRAWOBJ_SPARSE            KGSL_CMDBATCH_SPARSE
47
48 #define kgsl_drawobj_profiling_buffer kgsl_cmdbatch_profiling_buffer
49
50
51 /* The number of memstore arrays limits the number of contexts allowed.
52  * If more contexts are needed, update multiple for MEMSTORE_SIZE
53  */
54 #define KGSL_MEMSTORE_SIZE      ((int)(PAGE_SIZE * 8))
55 #define KGSL_MEMSTORE_GLOBAL    (0)
56 #define KGSL_PRIORITY_MAX_RB_LEVELS 4
57 #define KGSL_MEMSTORE_MAX       (KGSL_MEMSTORE_SIZE / \
58         sizeof(struct kgsl_devmemstore) - 1 - KGSL_PRIORITY_MAX_RB_LEVELS)
59 #define KGSL_MAX_CONTEXTS_PER_PROC 200
60
61 #define MEMSTORE_RB_OFFSET(rb, field)   \
62         KGSL_MEMSTORE_OFFSET(((rb)->id + KGSL_MEMSTORE_MAX), field)
63
64 #define MEMSTORE_ID_GPU_ADDR(dev, iter, field) \
65         ((dev)->memstore.gpuaddr + KGSL_MEMSTORE_OFFSET(iter, field))
66
67 #define MEMSTORE_RB_GPU_ADDR(dev, rb, field)    \
68         ((dev)->memstore.gpuaddr + \
69          KGSL_MEMSTORE_OFFSET(((rb)->id + KGSL_MEMSTORE_MAX), field))
70
71 /*
72  * SCRATCH MEMORY: The scratch memory is one page worth of data that
73  * is mapped into the GPU. This allows for some 'shared' data between
74  * the GPU and CPU. For example, it will be used by the GPU to write
75  * each updated RPTR for each RB.
76  *
77  * Used Data:
78  * Offset: Length(bytes): What
79  * 0x0: 4 * KGSL_PRIORITY_MAX_RB_LEVELS: RB0 RPTR
80  */
81
82 /* Shadow global helpers */
83 #define SCRATCH_RPTR_OFFSET(id) ((id) * sizeof(unsigned int))
84 #define SCRATCH_RPTR_GPU_ADDR(dev, id) \
85         ((dev)->scratch.gpuaddr + SCRATCH_RPTR_OFFSET(id))
86
87 /* Timestamp window used to detect rollovers (half of integer range) */
88 #define KGSL_TIMESTAMP_WINDOW 0x80000000
89
90 /* A macro for memory statistics - add the new size to the stat and if
91    the statisic is greater then _max, set _max
92 */
93
94 static inline void KGSL_STATS_ADD(uint64_t size, atomic_long_t *stat,
95                 atomic_long_t *max)
96 {
97         uint64_t ret = atomic_long_add_return(size, stat);
98
99         if (ret > atomic_long_read(max))
100                 atomic_long_set(max, ret);
101 }
102
103 #define KGSL_MAX_NUMIBS 100000
104 #define KGSL_MAX_SYNCPOINTS 32
105 #define KGSL_MAX_SPARSE 1000
106
107 struct kgsl_device;
108 struct kgsl_context;
109
110 /**
111  * struct kgsl_driver - main container for global KGSL things
112  * @cdev: Character device struct
113  * @major: Major ID for the KGSL device
114  * @class: Pointer to the class struct for the core KGSL sysfs entries
115  * @virtdev: Virtual device for managing the core
116  * @ptkobj: kobject for storing the pagetable statistics
117  * @prockobj: kobject for storing the process statistics
118  * @devp: Array of pointers to the individual KGSL device structs
119  * @process_list: List of open processes
120  * @pagetable_list: LIst of open pagetables
121  * @ptlock: Lock for accessing the pagetable list
122  * @process_mutex: Mutex for accessing the process list
123  * @devlock: Mutex protecting the device list
124  * @stats: Struct containing atomic memory statistics
125  * @full_cache_threshold: the threshold that triggers a full cache flush
126  * @workqueue: Pointer to a single threaded workqueue
127  * @mem_workqueue: Pointer to a workqueue for deferring memory entries
128  */
129 struct kgsl_driver {
130         struct cdev cdev;
131         dev_t major;
132         struct class *class;
133         struct device virtdev;
134         struct kobject *ptkobj;
135         struct kobject *prockobj;
136         struct kgsl_device *devp[KGSL_DEVICE_MAX];
137         struct list_head process_list;
138         struct list_head pagetable_list;
139         spinlock_t ptlock;
140         struct mutex process_mutex;
141         struct mutex devlock;
142         struct {
143                 atomic_long_t vmalloc;
144                 atomic_long_t vmalloc_max;
145                 atomic_long_t page_alloc;
146                 atomic_long_t page_alloc_max;
147                 atomic_long_t coherent;
148                 atomic_long_t coherent_max;
149                 atomic_long_t secure;
150                 atomic_long_t secure_max;
151                 atomic_long_t mapped;
152                 atomic_long_t mapped_max;
153         } stats;
154         unsigned int full_cache_threshold;
155         struct workqueue_struct *workqueue;
156         struct workqueue_struct *mem_workqueue;
157         struct kthread_worker worker;
158         struct task_struct *worker_thread;
159 };
160
161 extern struct kgsl_driver kgsl_driver;
162 extern struct mutex kgsl_mmu_sync;
163
164 struct kgsl_pagetable;
165 struct kgsl_memdesc;
166
167 struct kgsl_memdesc_ops {
168         unsigned int vmflags;
169         int (*vmfault)(struct kgsl_memdesc *, struct vm_area_struct *,
170                        struct vm_fault *);
171         void (*free)(struct kgsl_memdesc *memdesc);
172         int (*map_kernel)(struct kgsl_memdesc *);
173         void (*unmap_kernel)(struct kgsl_memdesc *);
174 };
175
176 /* Internal definitions for memdesc->priv */
177 #define KGSL_MEMDESC_GUARD_PAGE BIT(0)
178 /* Set if the memdesc is mapped into all pagetables */
179 #define KGSL_MEMDESC_GLOBAL BIT(1)
180 /* The memdesc is frozen during a snapshot */
181 #define KGSL_MEMDESC_FROZEN BIT(2)
182 /* The memdesc is mapped into a pagetable */
183 #define KGSL_MEMDESC_MAPPED BIT(3)
184 /* The memdesc is secured for content protection */
185 #define KGSL_MEMDESC_SECURE BIT(4)
186 /* Memory is accessible in privileged mode */
187 #define KGSL_MEMDESC_PRIVILEGED BIT(6)
188 /* The memdesc is TZ locked content protection */
189 #define KGSL_MEMDESC_TZ_LOCKED BIT(7)
190 /* The memdesc is allocated through contiguous memory */
191 #define KGSL_MEMDESC_CONTIG BIT(8)
192 /* For global buffers, randomly assign an address from the region */
193 #define KGSL_MEMDESC_RANDOM BIT(9)
194
195 /**
196  * struct kgsl_memdesc - GPU memory object descriptor
197  * @pagetable: Pointer to the pagetable that the object is mapped in
198  * @hostptr: Kernel virtual address
199  * @hostptr_count: Number of threads using hostptr
200  * @useraddr: User virtual address (if applicable)
201  * @gpuaddr: GPU virtual address
202  * @physaddr: Physical address of the memory object
203  * @size: Size of the memory object
204  * @mapsize: Size of memory mapped in userspace
205  * @priv: Internal flags and settings
206  * @sgt: Scatter gather table for allocated pages
207  * @ops: Function hooks for the memdesc memory type
208  * @flags: Flags set from userspace
209  * @dev: Pointer to the struct device that owns this memory
210  * @attrs: dma attributes for this memory
211  * @pages: An array of pointers to allocated pages
212  * @page_count: Total number of pages allocated
213  * @cur_bindings: Number of sparse pages actively bound
214  */
215 struct kgsl_memdesc {
216         struct kgsl_pagetable *pagetable;
217         void *hostptr;
218         unsigned int hostptr_count;
219         unsigned long useraddr;
220         uint64_t gpuaddr;
221         phys_addr_t physaddr;
222         uint64_t size;
223         uint64_t mapsize;
224         unsigned int priv;
225         struct sg_table *sgt;
226         struct kgsl_memdesc_ops *ops;
227         uint64_t flags;
228         struct device *dev;
229         struct dma_attrs attrs;
230         struct page **pages;
231         unsigned int page_count;
232         unsigned int cur_bindings;
233 };
234
235 /*
236  * List of different memory entry types. The usermem enum
237  * starts at 0, which we use for allocated memory, so 1 is
238  * added to the enum values.
239  */
240 #define KGSL_MEM_ENTRY_KERNEL 0
241 #define KGSL_MEM_ENTRY_USER (KGSL_USER_MEM_TYPE_ADDR + 1)
242 #define KGSL_MEM_ENTRY_ION (KGSL_USER_MEM_TYPE_ION + 1)
243 #define KGSL_MEM_ENTRY_MAX (KGSL_USER_MEM_TYPE_MAX + 1)
244
245 /* symbolic table for trace and debugfs */
246 #define KGSL_MEM_TYPES \
247         { KGSL_MEM_ENTRY_KERNEL, "gpumem" }, \
248         { KGSL_MEM_ENTRY_USER, "usermem" }, \
249         { KGSL_MEM_ENTRY_ION, "ion" }
250
251 /*
252  * struct kgsl_mem_entry - a userspace memory allocation
253  * @refcount: reference count. Currently userspace can only
254  *  hold a single reference count, but the kernel may hold more.
255  * @memdesc: description of the memory
256  * @priv_data: type-specific data, such as the dma-buf attachment pointer.
257  * @node: rb_node for the gpu address lookup rb tree
258  * @id: idr index for this entry, can be used to find memory that does not have
259  *  a valid GPU address.
260  * @priv: back pointer to the process that owns this memory
261  * @pending_free: if !0, userspace requested that his memory be freed, but there
262  *  are still references to it.
263  * @dev_priv: back pointer to the device file that created this entry.
264  * @metadata: String containing user specified metadata for the entry
265  * @work: Work struct used to schedule a kgsl_mem_entry_put in atomic contexts
266  * @bind_lock: Lock for sparse memory bindings
267  * @bind_tree: RB Tree for sparse memory bindings
268  */
269 struct kgsl_mem_entry {
270         struct kref refcount;
271         struct kgsl_memdesc memdesc;
272         void *priv_data;
273         struct rb_node node;
274         unsigned int id;
275         struct kgsl_process_private *priv;
276         int pending_free;
277         char metadata[KGSL_GPUOBJ_ALLOC_METADATA_MAX + 1];
278         struct work_struct work;
279         spinlock_t bind_lock;
280         struct rb_root bind_tree;
281 };
282
283 struct kgsl_device_private;
284 struct kgsl_event_group;
285
286 typedef void (*kgsl_event_func)(struct kgsl_device *, struct kgsl_event_group *,
287                 void *, int);
288
289 /**
290  * struct kgsl_event - KGSL GPU timestamp event
291  * @device: Pointer to the KGSL device that owns the event
292  * @context: Pointer to the context that owns the event
293  * @timestamp: Timestamp for the event to expire
294  * @func: Callback function for for the event when it expires
295  * @priv: Private data passed to the callback function
296  * @node: List node for the kgsl_event_group list
297  * @created: Jiffies when the event was created
298  * @work: Work struct for dispatching the callback
299  * @result: KGSL event result type to pass to the callback
300  * group: The event group this event belongs to
301  */
302 struct kgsl_event {
303         struct kgsl_device *device;
304         struct kgsl_context *context;
305         unsigned int timestamp;
306         kgsl_event_func func;
307         void *priv;
308         struct list_head node;
309         unsigned int created;
310         struct kthread_work work;
311         int result;
312         struct kgsl_event_group *group;
313 };
314
315 typedef int (*readtimestamp_func)(struct kgsl_device *, void *,
316         enum kgsl_timestamp_type, unsigned int *);
317
318 /**
319  * struct event_group - A list of GPU events
320  * @context: Pointer to the active context for the events
321  * @lock: Spinlock for protecting the list
322  * @events: List of active GPU events
323  * @group: Node for the master group list
324  * @processed: Last processed timestamp
325  * @name: String name for the group (for the debugfs file)
326  * @readtimestamp: Function pointer to read a timestamp
327  * @priv: Priv member to pass to the readtimestamp function
328  */
329 struct kgsl_event_group {
330         struct kgsl_context *context;
331         spinlock_t lock;
332         struct list_head events;
333         struct list_head group;
334         unsigned int processed;
335         char name[64];
336         readtimestamp_func readtimestamp;
337         void *priv;
338 };
339
340 /**
341  * struct kgsl_protected_registers - Protected register range
342  * @base: Offset of the range to be protected
343  * @range: Range (# of registers = 2 ** range)
344  */
345 struct kgsl_protected_registers {
346         unsigned int base;
347         int range;
348 };
349
350 /**
351  * struct sparse_bind_object - Bind metadata
352  * @node: Node for the rb tree
353  * @p_memdesc: Physical memdesc bound to
354  * @v_off: Offset of bind in the virtual entry
355  * @p_off: Offset of bind in the physical memdesc
356  * @size: Size of the bind
357  * @flags: Flags for the bind
358  */
359 struct sparse_bind_object {
360         struct rb_node node;
361         struct kgsl_memdesc *p_memdesc;
362         uint64_t v_off;
363         uint64_t p_off;
364         uint64_t size;
365         uint64_t flags;
366 };
367
368 long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
369                                           unsigned int cmd, void *data);
370 long kgsl_ioctl_device_setproperty(struct kgsl_device_private *dev_priv,
371                                         unsigned int cmd, void *data);
372 long kgsl_ioctl_device_waittimestamp_ctxtid(struct kgsl_device_private
373                                 *dev_priv, unsigned int cmd, void *data);
374 long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
375                                       unsigned int cmd, void *data);
376 long kgsl_ioctl_submit_commands(struct kgsl_device_private *dev_priv,
377                                 unsigned int cmd, void *data);
378 long kgsl_ioctl_cmdstream_readtimestamp_ctxtid(struct kgsl_device_private
379                                         *dev_priv, unsigned int cmd,
380                                         void *data);
381 long kgsl_ioctl_cmdstream_freememontimestamp_ctxtid(
382                                                 struct kgsl_device_private
383                                                 *dev_priv, unsigned int cmd,
384                                                 void *data);
385 long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
386                                         unsigned int cmd, void *data);
387 long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
388                                         unsigned int cmd, void *data);
389 long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
390                                         unsigned int cmd, void *data);
391 long kgsl_ioctl_gpumem_free_id(struct kgsl_device_private *dev_priv,
392                                         unsigned int cmd, void *data);
393 long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
394                                         unsigned int cmd, void *data);
395 long kgsl_ioctl_gpumem_sync_cache(struct kgsl_device_private *dev_priv,
396                                         unsigned int cmd, void *data);
397 long kgsl_ioctl_gpumem_sync_cache_bulk(struct kgsl_device_private *dev_priv,
398                                         unsigned int cmd, void *data);
399 long kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
400                                         unsigned int cmd, void *data);
401 long kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
402                                         unsigned int cmd, void *data);
403 long kgsl_ioctl_gpumem_alloc_id(struct kgsl_device_private *dev_priv,
404                                         unsigned int cmd, void *data);
405 long kgsl_ioctl_gpumem_get_info(struct kgsl_device_private *dev_priv,
406                                         unsigned int cmd, void *data);
407 long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv,
408                                         unsigned int cmd, void *data);
409 long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv,
410                                         unsigned int cmd, void *data);
411 long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
412                                         unsigned int cmd, void *data);
413 long kgsl_ioctl_cff_sync_gpuobj(struct kgsl_device_private *dev_priv,
414                                         unsigned int cmd, void *data);
415 long kgsl_ioctl_gpuobj_alloc(struct kgsl_device_private *dev_priv,
416                                         unsigned int cmd, void *data);
417 long kgsl_ioctl_gpuobj_free(struct kgsl_device_private *dev_priv,
418                                         unsigned int cmd, void *data);
419 long kgsl_ioctl_gpuobj_info(struct kgsl_device_private *dev_priv,
420                                         unsigned int cmd, void *data);
421 long kgsl_ioctl_gpuobj_import(struct kgsl_device_private *dev_priv,
422                                         unsigned int cmd, void *data);
423 long kgsl_ioctl_gpuobj_sync(struct kgsl_device_private *dev_priv,
424                                         unsigned int cmd, void *data);
425 long kgsl_ioctl_gpu_command(struct kgsl_device_private *dev_priv,
426                                 unsigned int cmd, void *data);
427 long kgsl_ioctl_gpuobj_set_info(struct kgsl_device_private *dev_priv,
428                                 unsigned int cmd, void *data);
429
430 long kgsl_ioctl_sparse_phys_alloc(struct kgsl_device_private *dev_priv,
431                                         unsigned int cmd, void *data);
432 long kgsl_ioctl_sparse_phys_free(struct kgsl_device_private *dev_priv,
433                                         unsigned int cmd, void *data);
434 long kgsl_ioctl_sparse_virt_alloc(struct kgsl_device_private *dev_priv,
435                                         unsigned int cmd, void *data);
436 long kgsl_ioctl_sparse_virt_free(struct kgsl_device_private *dev_priv,
437                                         unsigned int cmd, void *data);
438 long kgsl_ioctl_sparse_bind(struct kgsl_device_private *dev_priv,
439                                         unsigned int cmd, void *data);
440 long kgsl_ioctl_sparse_unbind(struct kgsl_device_private *dev_priv,
441                                         unsigned int cmd, void *data);
442 long kgsl_ioctl_gpu_sparse_command(struct kgsl_device_private *dev_priv,
443                                         unsigned int cmd, void *data);
444
445 void kgsl_mem_entry_destroy(struct kref *kref);
446
447 void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
448                         int *egl_surface_count, int *egl_image_count);
449
450 struct kgsl_mem_entry * __must_check
451 kgsl_sharedmem_find(struct kgsl_process_private *private, uint64_t gpuaddr);
452
453 struct kgsl_mem_entry * __must_check
454 kgsl_sharedmem_find_id(struct kgsl_process_private *process, unsigned int id);
455
456 extern const struct dev_pm_ops kgsl_pm_ops;
457
458 int kgsl_suspend_driver(struct platform_device *pdev, pm_message_t state);
459 int kgsl_resume_driver(struct platform_device *pdev);
460
461 static inline int kgsl_gpuaddr_in_memdesc(const struct kgsl_memdesc *memdesc,
462                                 uint64_t gpuaddr, uint64_t size)
463 {
464         /* set a minimum size to search for */
465         if (!size)
466                 size = 1;
467
468         /* don't overflow */
469         if (size > U64_MAX - gpuaddr)
470                 return 0;
471
472         if (gpuaddr >= memdesc->gpuaddr &&
473             ((gpuaddr + size) <= (memdesc->gpuaddr + memdesc->size))) {
474                 return 1;
475         }
476         return 0;
477 }
478
479 static inline void *kgsl_memdesc_map(struct kgsl_memdesc *memdesc)
480 {
481         if (memdesc->ops && memdesc->ops->map_kernel)
482                 memdesc->ops->map_kernel(memdesc);
483
484         return memdesc->hostptr;
485 }
486
487 static inline void kgsl_memdesc_unmap(struct kgsl_memdesc *memdesc)
488 {
489         if (memdesc->ops && memdesc->ops->unmap_kernel)
490                 memdesc->ops->unmap_kernel(memdesc);
491 }
492
493 static inline void *kgsl_gpuaddr_to_vaddr(struct kgsl_memdesc *memdesc,
494                                              uint64_t gpuaddr)
495 {
496         void *hostptr = NULL;
497
498         if ((gpuaddr >= memdesc->gpuaddr) &&
499                 (gpuaddr < (memdesc->gpuaddr + memdesc->size)))
500                 hostptr = kgsl_memdesc_map(memdesc);
501
502         return hostptr != NULL ? hostptr + (gpuaddr - memdesc->gpuaddr) : NULL;
503 }
504
505 static inline int timestamp_cmp(unsigned int a, unsigned int b)
506 {
507         /* check for equal */
508         if (a == b)
509                 return 0;
510
511         /* check for greater-than for non-rollover case */
512         if ((a > b) && (a - b < KGSL_TIMESTAMP_WINDOW))
513                 return 1;
514
515         /* check for greater-than for rollover case
516          * note that <= is required to ensure that consistent
517          * results are returned for values whose difference is
518          * equal to the window size
519          */
520         a += KGSL_TIMESTAMP_WINDOW;
521         b += KGSL_TIMESTAMP_WINDOW;
522         return ((a > b) && (a - b <= KGSL_TIMESTAMP_WINDOW)) ? 1 : -1;
523 }
524
525 /**
526  * kgsl_schedule_work() - Schedule a work item on the KGSL workqueue
527  * @work: work item to schedule
528  */
529 static inline void kgsl_schedule_work(struct work_struct *work)
530 {
531         queue_work(kgsl_driver.workqueue, work);
532 }
533
534 static inline int
535 kgsl_mem_entry_get(struct kgsl_mem_entry *entry)
536 {
537         if (entry)
538                 return kref_get_unless_zero(&entry->refcount);
539         return 0;
540 }
541
542 static inline void
543 kgsl_mem_entry_put(struct kgsl_mem_entry *entry)
544 {
545         if (entry)
546                 kref_put(&entry->refcount, kgsl_mem_entry_destroy);
547 }
548
549 /*
550  * kgsl_addr_range_overlap() - Checks if 2 ranges overlap
551  * @gpuaddr1: Start of first address range
552  * @size1: Size of first address range
553  * @gpuaddr2: Start of second address range
554  * @size2: Size of second address range
555  *
556  * Function returns true if the 2 given address ranges overlap
557  * else false
558  */
559 static inline bool kgsl_addr_range_overlap(uint64_t gpuaddr1,
560                 uint64_t size1, uint64_t gpuaddr2, uint64_t size2)
561 {
562         if ((size1 > (U64_MAX - gpuaddr1)) || (size2 > (U64_MAX - gpuaddr2)))
563                 return false;
564         return !(((gpuaddr1 + size1) <= gpuaddr2) ||
565                 (gpuaddr1 >= (gpuaddr2 + size2)));
566 }
567
568 /**
569  * kgsl_malloc() - Use either kzalloc or vmalloc to allocate memory
570  * @size: Size of the desired allocation
571  *
572  * Allocate a block of memory for the driver - if it is small try to allocate it
573  * from kmalloc (fast!) otherwise we need to go with vmalloc (safe!)
574  */
575 static inline void *kgsl_malloc(size_t size)
576 {
577         if (size <= PAGE_SIZE)
578                 return kzalloc(size, GFP_KERNEL);
579
580         return vmalloc(size);
581 }
582
583 /**
584  * kgsl_free() - Free memory allocated by kgsl_malloc()
585  * @ptr: Pointer to the memory to free
586  *
587  * Free the memory be it in vmalloc or kmalloc space
588  */
589 static inline void kgsl_free(void *ptr)
590 {
591         if (ptr != NULL && is_vmalloc_addr(ptr))
592                 return vfree(ptr);
593
594         kfree(ptr);
595 }
596
597 static inline int _copy_from_user(void *dest, void __user *src,
598                 unsigned int ksize, unsigned int usize)
599 {
600         unsigned int copy = ksize < usize ? ksize : usize;
601
602         if (copy == 0)
603                 return -EINVAL;
604
605         return copy_from_user(dest, src, copy) ? -EFAULT : 0;
606 }
607
608 static inline void __user *to_user_ptr(uint64_t address)
609 {
610         return (void __user *)(uintptr_t)address;
611 }
612
613 static inline void kgsl_gpu_sysfs_add_link(struct kobject *dst,
614                         struct kobject *src, const char *src_name,
615                         const char *dst_name)
616 {
617         struct kernfs_node *old;
618
619         if (dst == NULL || src == NULL)
620                 return;
621
622         old = sysfs_get_dirent(src->sd, src_name);
623         if (IS_ERR_OR_NULL(old))
624                 return;
625
626         kernfs_create_link(dst->sd, dst_name, old);
627 }
628 #endif /* __KGSL_H */