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