blob: 66102af233561e61f9714672e7f76b15d5f42372 [file] [log] [blame]
Rajesh Kemisettif7a4df12019-04-30 19:04:30 +05301/* Copyright (c) 2008-2019, The Linux Foundation. All rights reserved.
Shrenuj Bansala419c792016-10-20 14:05:11 -07002 *
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#include <linux/module.h>
14#include <linux/fb.h>
15#include <linux/file.h>
16#include <linux/fs.h>
17#include <linux/fdtable.h>
18#include <linux/list.h>
19#include <linux/debugfs.h>
20#include <linux/uaccess.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/dma-buf.h>
24#include <linux/pm_runtime.h>
25#include <linux/rbtree.h>
26#include <linux/major.h>
27#include <linux/io.h>
28#include <linux/mman.h>
29#include <linux/sort.h>
30#include <linux/security.h>
31#include <linux/compat.h>
32#include <linux/ctype.h>
33#include <linux/mm.h>
34#include <asm/cacheflush.h>
35
36#include "kgsl.h"
37#include "kgsl_debugfs.h"
38#include "kgsl_log.h"
39#include "kgsl_sharedmem.h"
40#include "kgsl_drawobj.h"
41#include "kgsl_device.h"
42#include "kgsl_trace.h"
43#include "kgsl_sync.h"
44#include "kgsl_compat.h"
45#include "kgsl_pool.h"
46
47#undef MODULE_PARAM_PREFIX
48#define MODULE_PARAM_PREFIX "kgsl."
49
50#ifndef arch_mmap_check
51#define arch_mmap_check(addr, len, flags) (0)
52#endif
53
54#ifndef pgprot_writebackcache
55#define pgprot_writebackcache(_prot) (_prot)
56#endif
57
58#ifndef pgprot_writethroughcache
59#define pgprot_writethroughcache(_prot) (_prot)
60#endif
61
62#ifdef CONFIG_ARM_LPAE
63#define KGSL_DMA_BIT_MASK DMA_BIT_MASK(64)
64#else
65#define KGSL_DMA_BIT_MASK DMA_BIT_MASK(32)
66#endif
67
68static char *kgsl_mmu_type;
69module_param_named(mmutype, kgsl_mmu_type, charp, 0000);
70MODULE_PARM_DESC(kgsl_mmu_type, "Type of MMU to be used for graphics");
71
72/* Mutex used for the IOMMU sync quirk */
73DEFINE_MUTEX(kgsl_mmu_sync);
74EXPORT_SYMBOL(kgsl_mmu_sync);
75
76struct kgsl_dma_buf_meta {
77 struct dma_buf_attachment *attach;
78 struct dma_buf *dmabuf;
79 struct sg_table *table;
80};
81
82static inline struct kgsl_pagetable *_get_memdesc_pagetable(
83 struct kgsl_pagetable *pt, struct kgsl_mem_entry *entry)
84{
85 /* if a secured buffer, map it to secure global pagetable */
86 if (kgsl_memdesc_is_secured(&entry->memdesc))
87 return pt->mmu->securepagetable;
88
89 return pt;
90}
91
92static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry);
93
94static const struct file_operations kgsl_fops;
95
96/*
97 * The memfree list contains the last N blocks of memory that have been freed.
98 * On a GPU fault we walk the list to see if the faulting address had been
99 * recently freed and print out a message to that effect
100 */
101
102#define MEMFREE_ENTRIES 512
103
104static DEFINE_SPINLOCK(memfree_lock);
105
106struct memfree_entry {
107 pid_t ptname;
108 uint64_t gpuaddr;
109 uint64_t size;
110 pid_t pid;
111 uint64_t flags;
112};
113
114static struct {
115 struct memfree_entry *list;
116 int head;
117 int tail;
118} memfree;
119
120static int kgsl_memfree_init(void)
121{
122 memfree.list = kcalloc(MEMFREE_ENTRIES, sizeof(struct memfree_entry),
123 GFP_KERNEL);
124
125 return (memfree.list) ? 0 : -ENOMEM;
126}
127
128static void kgsl_memfree_exit(void)
129{
130 kfree(memfree.list);
131 memset(&memfree, 0, sizeof(memfree));
132}
133
134static inline bool match_memfree_addr(struct memfree_entry *entry,
135 pid_t ptname, uint64_t gpuaddr)
136{
137 return ((entry->ptname == ptname) &&
138 (entry->size > 0) &&
139 (gpuaddr >= entry->gpuaddr &&
140 gpuaddr < (entry->gpuaddr + entry->size)));
141}
142int kgsl_memfree_find_entry(pid_t ptname, uint64_t *gpuaddr,
143 uint64_t *size, uint64_t *flags, pid_t *pid)
144{
145 int ptr;
146
147 if (memfree.list == NULL)
148 return 0;
149
150 spin_lock(&memfree_lock);
151
152 ptr = memfree.head - 1;
153 if (ptr < 0)
154 ptr = MEMFREE_ENTRIES - 1;
155
156 /* Walk backwards through the list looking for the last match */
157 while (ptr != memfree.tail) {
158 struct memfree_entry *entry = &memfree.list[ptr];
159
160 if (match_memfree_addr(entry, ptname, *gpuaddr)) {
161 *gpuaddr = entry->gpuaddr;
162 *flags = entry->flags;
163 *size = entry->size;
164 *pid = entry->pid;
165
166 spin_unlock(&memfree_lock);
167 return 1;
168 }
169
170 ptr = ptr - 1;
171
172 if (ptr < 0)
173 ptr = MEMFREE_ENTRIES - 1;
174 }
175
176 spin_unlock(&memfree_lock);
177 return 0;
178}
179
180static void kgsl_memfree_purge(struct kgsl_pagetable *pagetable,
181 uint64_t gpuaddr, uint64_t size)
182{
183 pid_t ptname = pagetable ? pagetable->name : 0;
184 int i;
185
186 if (memfree.list == NULL)
187 return;
188
189 spin_lock(&memfree_lock);
190
191 for (i = 0; i < MEMFREE_ENTRIES; i++) {
192 struct memfree_entry *entry = &memfree.list[i];
193
194 if (entry->ptname != ptname || entry->size == 0)
195 continue;
196
197 if (gpuaddr > entry->gpuaddr &&
198 gpuaddr < entry->gpuaddr + entry->size) {
199 /* truncate the end of the entry */
200 entry->size = gpuaddr - entry->gpuaddr;
201 } else if (gpuaddr <= entry->gpuaddr) {
202 if (gpuaddr + size > entry->gpuaddr &&
203 gpuaddr + size < entry->gpuaddr + entry->size)
204 /* Truncate the beginning of the entry */
205 entry->gpuaddr = gpuaddr + size;
206 else if (gpuaddr + size >= entry->gpuaddr + entry->size)
207 /* Remove the entire entry */
208 entry->size = 0;
209 }
210 }
211 spin_unlock(&memfree_lock);
212}
213
214static void kgsl_memfree_add(pid_t pid, pid_t ptname, uint64_t gpuaddr,
215 uint64_t size, uint64_t flags)
216
217{
218 struct memfree_entry *entry;
219
220 if (memfree.list == NULL)
221 return;
222
223 spin_lock(&memfree_lock);
224
225 entry = &memfree.list[memfree.head];
226
227 entry->pid = pid;
228 entry->ptname = ptname;
229 entry->gpuaddr = gpuaddr;
230 entry->size = size;
231 entry->flags = flags;
232
233 memfree.head = (memfree.head + 1) % MEMFREE_ENTRIES;
234
235 if (memfree.head == memfree.tail)
236 memfree.tail = (memfree.tail + 1) % MEMFREE_ENTRIES;
237
238 spin_unlock(&memfree_lock);
239}
240
241int kgsl_readtimestamp(struct kgsl_device *device, void *priv,
242 enum kgsl_timestamp_type type, unsigned int *timestamp)
243{
244 return device->ftbl->readtimestamp(device, priv, type, timestamp);
245}
246EXPORT_SYMBOL(kgsl_readtimestamp);
247
Shrenuj Bansala419c792016-10-20 14:05:11 -0700248/* Scheduled by kgsl_mem_entry_put_deferred() */
249static void _deferred_put(struct work_struct *work)
250{
251 struct kgsl_mem_entry *entry =
252 container_of(work, struct kgsl_mem_entry, work);
253
254 kgsl_mem_entry_put(entry);
255}
256
257static inline struct kgsl_mem_entry *
258kgsl_mem_entry_create(void)
259{
260 struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
261
Tarun Karra24d3fe12017-04-05 15:23:03 -0700262 if (entry != NULL) {
Shrenuj Bansala419c792016-10-20 14:05:11 -0700263 kref_init(&entry->refcount);
Tarun Karra24d3fe12017-04-05 15:23:03 -0700264 /* put this ref in userspace memory alloc and map ioctls */
265 kref_get(&entry->refcount);
266 }
Shrenuj Bansala419c792016-10-20 14:05:11 -0700267
268 return entry;
269}
270#ifdef CONFIG_DMA_SHARED_BUFFER
271static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
272{
273 if (meta != NULL) {
274 dma_buf_unmap_attachment(meta->attach, meta->table,
275 DMA_FROM_DEVICE);
276 dma_buf_detach(meta->dmabuf, meta->attach);
277 dma_buf_put(meta->dmabuf);
278 kfree(meta);
279 }
280}
281#else
282static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
283{
284
285}
286#endif
287
288void
289kgsl_mem_entry_destroy(struct kref *kref)
290{
291 struct kgsl_mem_entry *entry = container_of(kref,
292 struct kgsl_mem_entry,
293 refcount);
294 unsigned int memtype;
295
296 if (entry == NULL)
297 return;
298
299 /* pull out the memtype before the flags get cleared */
300 memtype = kgsl_memdesc_usermem_type(&entry->memdesc);
301
302 /* Detach from process list */
303 kgsl_mem_entry_detach_process(entry);
304
305 if (memtype != KGSL_MEM_ENTRY_KERNEL)
306 atomic_long_sub(entry->memdesc.size,
307 &kgsl_driver.stats.mapped);
308
309 /*
310 * Ion takes care of freeing the sg_table for us so
311 * clear the sg table before freeing the sharedmem
312 * so kgsl_sharedmem_free doesn't try to free it again
313 */
314 if (memtype == KGSL_MEM_ENTRY_ION)
315 entry->memdesc.sgt = NULL;
316
317 if ((memtype == KGSL_MEM_ENTRY_USER)
318 && !(entry->memdesc.flags & KGSL_MEMFLAGS_GPUREADONLY)) {
319 int i = 0, j;
320 struct scatterlist *sg;
321 struct page *page;
322 /*
323 * Mark all of pages in the scatterlist as dirty since they
324 * were writable by the GPU.
325 */
326 for_each_sg(entry->memdesc.sgt->sgl, sg,
327 entry->memdesc.sgt->nents, i) {
328 page = sg_page(sg);
329 for (j = 0; j < (sg->length >> PAGE_SHIFT); j++)
Rajesh Kemisettif7a4df12019-04-30 19:04:30 +0530330 set_page_dirty_lock(nth_page(page, j));
Shrenuj Bansala419c792016-10-20 14:05:11 -0700331 }
332 }
333
334 kgsl_sharedmem_free(&entry->memdesc);
335
336 switch (memtype) {
337 case KGSL_MEM_ENTRY_ION:
338 kgsl_destroy_ion(entry->priv_data);
339 break;
340 default:
341 break;
342 }
343
344 kfree(entry);
345}
346EXPORT_SYMBOL(kgsl_mem_entry_destroy);
347
348/* Allocate a IOVA for memory objects that don't use SVM */
349static int kgsl_mem_entry_track_gpuaddr(struct kgsl_device *device,
350 struct kgsl_process_private *process,
351 struct kgsl_mem_entry *entry)
352{
353 struct kgsl_pagetable *pagetable;
354
355 /*
356 * If SVM is enabled for this object then the address needs to be
357 * assigned elsewhere
358 * Also do not proceed further in case of NoMMU.
359 */
360 if (kgsl_memdesc_use_cpu_map(&entry->memdesc) ||
361 (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_NONE))
362 return 0;
363
364 pagetable = kgsl_memdesc_is_secured(&entry->memdesc) ?
365 device->mmu.securepagetable : process->pagetable;
366
367 return kgsl_mmu_get_gpuaddr(pagetable, &entry->memdesc);
368}
369
370/* Commit the entry to the process so it can be accessed by other operations */
371static void kgsl_mem_entry_commit_process(struct kgsl_mem_entry *entry)
372{
373 if (!entry)
374 return;
375
376 spin_lock(&entry->priv->mem_lock);
377 idr_replace(&entry->priv->mem_idr, entry, entry->id);
378 spin_unlock(&entry->priv->mem_lock);
379}
380
381/*
382 * Attach the memory object to a process by (possibly) getting a GPU address and
383 * (possibly) mapping it
384 */
385static int kgsl_mem_entry_attach_process(struct kgsl_device *device,
386 struct kgsl_process_private *process,
387 struct kgsl_mem_entry *entry)
388{
389 int id, ret;
390
391 ret = kgsl_process_private_get(process);
392 if (!ret)
393 return -EBADF;
394
395 ret = kgsl_mem_entry_track_gpuaddr(device, process, entry);
396 if (ret) {
397 kgsl_process_private_put(process);
398 return ret;
399 }
400
401 idr_preload(GFP_KERNEL);
402 spin_lock(&process->mem_lock);
403 /* Allocate the ID but don't attach the pointer just yet */
404 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
405 spin_unlock(&process->mem_lock);
406 idr_preload_end();
407
408 if (id < 0) {
409 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc))
410 kgsl_mmu_put_gpuaddr(&entry->memdesc);
411 kgsl_process_private_put(process);
412 return id;
413 }
414
415 entry->id = id;
416 entry->priv = process;
417
418 /*
419 * Map the memory if a GPU address is already assigned, either through
420 * kgsl_mem_entry_track_gpuaddr() or via some other SVM process
421 */
422 if (entry->memdesc.gpuaddr) {
423 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_VIRT)
424 ret = kgsl_mmu_sparse_dummy_map(
425 entry->memdesc.pagetable,
426 &entry->memdesc, 0,
427 entry->memdesc.size);
428 else if (entry->memdesc.gpuaddr)
429 ret = kgsl_mmu_map(entry->memdesc.pagetable,
430 &entry->memdesc);
431
432 if (ret)
433 kgsl_mem_entry_detach_process(entry);
434 }
435
436 kgsl_memfree_purge(entry->memdesc.pagetable, entry->memdesc.gpuaddr,
437 entry->memdesc.size);
438
439 return ret;
440}
441
442/* Detach a memory entry from a process and unmap it from the MMU */
443static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry)
444{
445 unsigned int type;
446
447 if (entry == NULL)
448 return;
449
450 /*
451 * First remove the entry from mem_idr list
452 * so that no one can operate on obsolete values
453 */
454 spin_lock(&entry->priv->mem_lock);
455 if (entry->id != 0)
456 idr_remove(&entry->priv->mem_idr, entry->id);
457 entry->id = 0;
458
459 type = kgsl_memdesc_usermem_type(&entry->memdesc);
460 entry->priv->stats[type].cur -= entry->memdesc.size;
Amit Kushwaha7c843c22018-04-09 20:41:14 +0530461
462 if (type != KGSL_MEM_ENTRY_ION)
463 entry->priv->gpumem_mapped -= entry->memdesc.mapsize;
464
Shrenuj Bansala419c792016-10-20 14:05:11 -0700465 spin_unlock(&entry->priv->mem_lock);
466
467 kgsl_mmu_put_gpuaddr(&entry->memdesc);
468
469 kgsl_process_private_put(entry->priv);
470
471 entry->priv = NULL;
472}
473
474/**
475 * kgsl_context_dump() - dump information about a draw context
476 * @device: KGSL device that owns the context
477 * @context: KGSL context to dump information about
478 *
479 * Dump specific information about the context to the kernel log. Used for
480 * fence timeout callbacks
481 */
482void kgsl_context_dump(struct kgsl_context *context)
483{
484 struct kgsl_device *device;
485
486 if (_kgsl_context_get(context) == 0)
487 return;
488
489 device = context->device;
490
491 if (kgsl_context_detached(context)) {
492 dev_err(device->dev, " context[%d]: context detached\n",
493 context->id);
494 } else if (device->ftbl->drawctxt_dump != NULL)
495 device->ftbl->drawctxt_dump(device, context);
496
497 kgsl_context_put(context);
498}
499EXPORT_SYMBOL(kgsl_context_dump);
500
501/* Allocate a new context ID */
502static int _kgsl_get_context_id(struct kgsl_device *device)
503{
504 int id;
505
506 idr_preload(GFP_KERNEL);
507 write_lock(&device->context_lock);
508 /* Allocate the slot but don't put a pointer in it yet */
509 id = idr_alloc(&device->context_idr, NULL, 1,
510 KGSL_MEMSTORE_MAX, GFP_NOWAIT);
511 write_unlock(&device->context_lock);
512 idr_preload_end();
513
514 return id;
515}
516
517/**
518 * kgsl_context_init() - helper to initialize kgsl_context members
519 * @dev_priv: the owner of the context
520 * @context: the newly created context struct, should be allocated by
521 * the device specific drawctxt_create function.
522 *
523 * This is a helper function for the device specific drawctxt_create
524 * function to initialize the common members of its context struct.
525 * If this function succeeds, reference counting is active in the context
526 * struct and the caller should kgsl_context_put() it on error.
527 * If it fails, the caller should just free the context structure
528 * it passed in.
529 */
530int kgsl_context_init(struct kgsl_device_private *dev_priv,
531 struct kgsl_context *context)
532{
533 struct kgsl_device *device = dev_priv->device;
534 char name[64];
535 int ret = 0, id;
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700536 struct kgsl_process_private *proc_priv = dev_priv->process_priv;
537
Deepak Kumare0d19f92018-03-05 16:51:25 +0530538 /*
539 * Read and increment the context count under lock to make sure
540 * no process goes beyond the specified context limit.
541 */
542 spin_lock(&proc_priv->ctxt_count_lock);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700543 if (atomic_read(&proc_priv->ctxt_count) > KGSL_MAX_CONTEXTS_PER_PROC) {
Rajesh Kemisetti7ac20532018-08-10 13:30:07 +0530544 KGSL_DRV_ERR_RATELIMIT(device,
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700545 "Per process context limit reached for pid %u",
546 dev_priv->process_priv->pid);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530547 spin_unlock(&proc_priv->ctxt_count_lock);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700548 return -ENOSPC;
549 }
550
551 atomic_inc(&proc_priv->ctxt_count);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530552 spin_unlock(&proc_priv->ctxt_count_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700553
554 id = _kgsl_get_context_id(device);
555 if (id == -ENOSPC) {
556 /*
557 * Before declaring that there are no contexts left try
558 * flushing the event workqueue just in case there are
559 * detached contexts waiting to finish
560 */
561
562 flush_workqueue(device->events_wq);
563 id = _kgsl_get_context_id(device);
564 }
565
566 if (id < 0) {
567 if (id == -ENOSPC)
568 KGSL_DRV_INFO(device,
569 "cannot have more than %zu contexts due to memstore limitation\n",
570 KGSL_MEMSTORE_MAX);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700571 atomic_dec(&proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700572 return id;
573 }
574
575 context->id = id;
576
577 kref_init(&context->refcount);
578 /*
579 * Get a refernce to the process private so its not destroyed, until
580 * the context is destroyed. This will also prevent the pagetable
581 * from being destroyed
582 */
583 if (!kgsl_process_private_get(dev_priv->process_priv)) {
584 ret = -EBADF;
585 goto out;
586 }
587 context->device = dev_priv->device;
588 context->dev_priv = dev_priv;
589 context->proc_priv = dev_priv->process_priv;
590 context->tid = task_pid_nr(current);
591
592 ret = kgsl_sync_timeline_create(context);
Harshdeep Dhattb1273892017-06-01 13:12:07 -0600593 if (ret) {
594 kgsl_process_private_put(dev_priv->process_priv);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700595 goto out;
Harshdeep Dhattb1273892017-06-01 13:12:07 -0600596 }
Shrenuj Bansala419c792016-10-20 14:05:11 -0700597
598 snprintf(name, sizeof(name), "context-%d", id);
599 kgsl_add_event_group(&context->events, context, name,
600 kgsl_readtimestamp, context);
601
602out:
603 if (ret) {
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700604 atomic_dec(&proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700605 write_lock(&device->context_lock);
606 idr_remove(&dev_priv->device->context_idr, id);
607 write_unlock(&device->context_lock);
608 }
609
610 return ret;
611}
612EXPORT_SYMBOL(kgsl_context_init);
613
614/**
615 * kgsl_context_detach() - Release the "master" context reference
616 * @context: The context that will be detached
617 *
618 * This is called when a context becomes unusable, because userspace
619 * has requested for it to be destroyed. The context itself may
620 * exist a bit longer until its reference count goes to zero.
621 * Other code referencing the context can detect that it has been
622 * detached by checking the KGSL_CONTEXT_PRIV_DETACHED bit in
623 * context->priv.
624 */
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -0600625void kgsl_context_detach(struct kgsl_context *context)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700626{
627 struct kgsl_device *device;
628
629 if (context == NULL)
630 return;
631
632 /*
633 * Mark the context as detached to keep others from using
634 * the context before it gets fully removed, and to make sure
635 * we don't try to detach twice.
636 */
637 if (test_and_set_bit(KGSL_CONTEXT_PRIV_DETACHED, &context->priv))
638 return;
639
640 device = context->device;
641
642 trace_kgsl_context_detach(device, context);
643
644 context->device->ftbl->drawctxt_detach(context);
645
646 /*
647 * Cancel all pending events after the device-specific context is
648 * detached, to avoid possibly freeing memory while it is still
649 * in use by the GPU.
650 */
651 kgsl_cancel_events(device, &context->events);
652
653 /* Remove the event group from the list */
654 kgsl_del_event_group(&context->events);
655
Lynus Vazc031a9b2017-01-25 13:00:13 +0530656 kgsl_sync_timeline_put(context->ktimeline);
657
Shrenuj Bansala419c792016-10-20 14:05:11 -0700658 kgsl_context_put(context);
659}
660
661void
662kgsl_context_destroy(struct kref *kref)
663{
664 struct kgsl_context *context = container_of(kref, struct kgsl_context,
665 refcount);
666 struct kgsl_device *device = context->device;
667
668 trace_kgsl_context_destroy(device, context);
669
670 /*
671 * It's not safe to destroy the context if it's not detached as GPU
672 * may still be executing commands
673 */
674 BUG_ON(!kgsl_context_detached(context));
675
676 write_lock(&device->context_lock);
677 if (context->id != KGSL_CONTEXT_INVALID) {
678
679 /* Clear the timestamps in the memstore during destroy */
680 kgsl_sharedmem_writel(device, &device->memstore,
681 KGSL_MEMSTORE_OFFSET(context->id, soptimestamp), 0);
682 kgsl_sharedmem_writel(device, &device->memstore,
683 KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp), 0);
684
685 /* clear device power constraint */
686 if (context->id == device->pwrctrl.constraint.owner_id) {
687 trace_kgsl_constraint(device,
688 device->pwrctrl.constraint.type,
689 device->pwrctrl.active_pwrlevel,
690 0);
691 device->pwrctrl.constraint.type = KGSL_CONSTRAINT_NONE;
692 }
693
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700694 atomic_dec(&context->proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700695 idr_remove(&device->context_idr, context->id);
696 context->id = KGSL_CONTEXT_INVALID;
697 }
698 write_unlock(&device->context_lock);
699 kgsl_sync_timeline_destroy(context);
700 kgsl_process_private_put(context->proc_priv);
701
702 device->ftbl->drawctxt_destroy(context);
703}
704
705struct kgsl_device *kgsl_get_device(int dev_idx)
706{
707 int i;
708 struct kgsl_device *ret = NULL;
709
710 mutex_lock(&kgsl_driver.devlock);
711
712 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
713 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
714 ret = kgsl_driver.devp[i];
715 break;
716 }
717 }
718
719 mutex_unlock(&kgsl_driver.devlock);
720 return ret;
721}
722EXPORT_SYMBOL(kgsl_get_device);
723
724static struct kgsl_device *kgsl_get_minor(int minor)
725{
726 struct kgsl_device *ret = NULL;
727
728 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
729 return NULL;
730
731 mutex_lock(&kgsl_driver.devlock);
732 ret = kgsl_driver.devp[minor];
733 mutex_unlock(&kgsl_driver.devlock);
734
735 return ret;
736}
737
738/**
739 * kgsl_check_timestamp() - return true if the specified timestamp is retired
740 * @device: Pointer to the KGSL device to check
741 * @context: Pointer to the context for the timestamp
742 * @timestamp: The timestamp to compare
743 */
744int kgsl_check_timestamp(struct kgsl_device *device,
745 struct kgsl_context *context, unsigned int timestamp)
746{
747 unsigned int ts_processed;
748
749 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
750 &ts_processed);
751
752 return (timestamp_cmp(ts_processed, timestamp) >= 0);
753}
754EXPORT_SYMBOL(kgsl_check_timestamp);
755
756static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
757{
758 int status = -EINVAL;
759
760 if (!device)
761 return -EINVAL;
762
763 KGSL_PWR_WARN(device, "suspend start\n");
764
765 mutex_lock(&device->mutex);
766 status = kgsl_pwrctrl_change_state(device, KGSL_STATE_SUSPEND);
Deepak Kumar10e2bc52018-08-01 11:57:33 +0530767 if (status == 0 && device->state == KGSL_STATE_SUSPEND)
Deepak Kumar79908f52018-02-28 11:06:38 +0530768 device->ftbl->dispatcher_halt(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700769 mutex_unlock(&device->mutex);
770
771 KGSL_PWR_WARN(device, "suspend end\n");
772 return status;
773}
774
775static int kgsl_resume_device(struct kgsl_device *device)
776{
777 if (!device)
778 return -EINVAL;
779
780 KGSL_PWR_WARN(device, "resume start\n");
781 mutex_lock(&device->mutex);
782 if (device->state == KGSL_STATE_SUSPEND) {
Deepak Kumar79908f52018-02-28 11:06:38 +0530783 device->ftbl->dispatcher_unhalt(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700784 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
785 } else if (device->state != KGSL_STATE_INIT) {
786 /*
787 * This is an error situation,so wait for the device
788 * to idle and then put the device to SLUMBER state.
789 * This will put the device to the right state when
790 * we resume.
791 */
792 if (device->state == KGSL_STATE_ACTIVE)
793 device->ftbl->idle(device);
794 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
795 KGSL_PWR_ERR(device,
796 "resume invoked without a suspend\n");
797 }
798
799 mutex_unlock(&device->mutex);
800 KGSL_PWR_WARN(device, "resume end\n");
801 return 0;
802}
803
804static int kgsl_suspend(struct device *dev)
805{
806
807 pm_message_t arg = {0};
808 struct kgsl_device *device = dev_get_drvdata(dev);
809
810 return kgsl_suspend_device(device, arg);
811}
812
813static int kgsl_resume(struct device *dev)
814{
815 struct kgsl_device *device = dev_get_drvdata(dev);
816
817 return kgsl_resume_device(device);
818}
819
820static int kgsl_runtime_suspend(struct device *dev)
821{
822 return 0;
823}
824
825static int kgsl_runtime_resume(struct device *dev)
826{
827 return 0;
828}
829
830const struct dev_pm_ops kgsl_pm_ops = {
831 .suspend = kgsl_suspend,
832 .resume = kgsl_resume,
833 .runtime_suspend = kgsl_runtime_suspend,
834 .runtime_resume = kgsl_runtime_resume,
835};
836EXPORT_SYMBOL(kgsl_pm_ops);
837
838int kgsl_suspend_driver(struct platform_device *pdev,
839 pm_message_t state)
840{
841 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
842
843 return kgsl_suspend_device(device, state);
844}
845EXPORT_SYMBOL(kgsl_suspend_driver);
846
847int kgsl_resume_driver(struct platform_device *pdev)
848{
849 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
850
851 return kgsl_resume_device(device);
852}
853EXPORT_SYMBOL(kgsl_resume_driver);
854
855/**
856 * kgsl_destroy_process_private() - Cleanup function to free process private
857 * @kref: - Pointer to object being destroyed's kref struct
858 * Free struct object and all other resources attached to it.
859 * Since the function can be used when not all resources inside process
860 * private have been allocated, there is a check to (before each resource
861 * cleanup) see if the struct member being cleaned is in fact allocated or not.
862 * If the value is not NULL, resource is freed.
863 */
864static void kgsl_destroy_process_private(struct kref *kref)
865{
866 struct kgsl_process_private *private = container_of(kref,
867 struct kgsl_process_private, refcount);
868
869 idr_destroy(&private->mem_idr);
870 idr_destroy(&private->syncsource_idr);
871
872 /* When using global pagetables, do not detach global pagetable */
873 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
874 kgsl_mmu_putpagetable(private->pagetable);
875
876 kfree(private);
877}
878
879void
880kgsl_process_private_put(struct kgsl_process_private *private)
881{
882 if (private)
883 kref_put(&private->refcount, kgsl_destroy_process_private);
884}
885
886/**
887 * kgsl_process_private_find() - Find the process associated with the specified
888 * name
889 * @name: pid_t of the process to search for
890 * Return the process struct for the given ID.
891 */
892struct kgsl_process_private *kgsl_process_private_find(pid_t pid)
893{
894 struct kgsl_process_private *p, *private = NULL;
895
896 mutex_lock(&kgsl_driver.process_mutex);
897 list_for_each_entry(p, &kgsl_driver.process_list, list) {
898 if (p->pid == pid) {
899 if (kgsl_process_private_get(p))
900 private = p;
901 break;
902 }
903 }
904 mutex_unlock(&kgsl_driver.process_mutex);
905 return private;
906}
907
908static struct kgsl_process_private *kgsl_process_private_new(
909 struct kgsl_device *device)
910{
911 struct kgsl_process_private *private;
912 pid_t tgid = task_tgid_nr(current);
913
914 /* Search in the process list */
915 list_for_each_entry(private, &kgsl_driver.process_list, list) {
916 if (private->pid == tgid) {
917 if (!kgsl_process_private_get(private))
918 private = ERR_PTR(-EINVAL);
919 return private;
920 }
921 }
922
923 /* Create a new object */
924 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
925 if (private == NULL)
926 return ERR_PTR(-ENOMEM);
927
928 kref_init(&private->refcount);
929
930 private->pid = tgid;
931 get_task_comm(private->comm, current->group_leader);
932
933 spin_lock_init(&private->mem_lock);
934 spin_lock_init(&private->syncsource_lock);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530935 spin_lock_init(&private->ctxt_count_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700936
937 idr_init(&private->mem_idr);
938 idr_init(&private->syncsource_idr);
939
940 /* Allocate a pagetable for the new process object */
941 private->pagetable = kgsl_mmu_getpagetable(&device->mmu, tgid);
942 if (IS_ERR(private->pagetable)) {
943 int err = PTR_ERR(private->pagetable);
944
945 idr_destroy(&private->mem_idr);
946 idr_destroy(&private->syncsource_idr);
947
948 kfree(private);
949 private = ERR_PTR(err);
950 }
951
952 return private;
953}
954
955static void process_release_memory(struct kgsl_process_private *private)
956{
957 struct kgsl_mem_entry *entry;
958 int next = 0;
959
960 while (1) {
961 spin_lock(&private->mem_lock);
962 entry = idr_get_next(&private->mem_idr, &next);
963 if (entry == NULL) {
964 spin_unlock(&private->mem_lock);
965 break;
966 }
967 /*
968 * If the free pending flag is not set it means that user space
969 * did not free it's reference to this entry, in that case
970 * free a reference to this entry, other references are from
971 * within kgsl so they will be freed eventually by kgsl
972 */
973 if (!entry->pending_free) {
974 entry->pending_free = 1;
975 spin_unlock(&private->mem_lock);
976 kgsl_mem_entry_put(entry);
977 } else {
978 spin_unlock(&private->mem_lock);
979 }
980 next = next + 1;
981 }
982}
983
Shrenuj Bansala419c792016-10-20 14:05:11 -0700984static void kgsl_process_private_close(struct kgsl_device_private *dev_priv,
985 struct kgsl_process_private *private)
986{
987 mutex_lock(&kgsl_driver.process_mutex);
988
989 if (--private->fd_count > 0) {
990 mutex_unlock(&kgsl_driver.process_mutex);
991 kgsl_process_private_put(private);
992 return;
993 }
994
995 /*
996 * If this is the last file on the process take down the debug
997 * directories and garbage collect any outstanding resources
998 */
999
1000 kgsl_process_uninit_sysfs(private);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001001
Akhil P Oommenb5f1b822018-03-08 20:10:52 +05301002 /* Release all syncsource objects from process private */
1003 kgsl_syncsource_process_release_syncsources(private);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001004
1005 /* When using global pagetables, do not detach global pagetable */
1006 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
1007 kgsl_mmu_detach_pagetable(private->pagetable);
1008
1009 /* Remove the process struct from the master list */
1010 list_del(&private->list);
1011
1012 /*
Lynus Vaz98ffb322017-07-27 15:13:09 +05301013 * Unlock the mutex before releasing the memory and the debugfs
1014 * nodes - this prevents deadlocks with the IOMMU and debugfs
1015 * locks.
Shrenuj Bansala419c792016-10-20 14:05:11 -07001016 */
1017 mutex_unlock(&kgsl_driver.process_mutex);
1018
1019 process_release_memory(private);
Lynus Vaz98ffb322017-07-27 15:13:09 +05301020 debugfs_remove_recursive(private->debug_root);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001021
1022 kgsl_process_private_put(private);
1023}
1024
1025
1026static struct kgsl_process_private *kgsl_process_private_open(
1027 struct kgsl_device *device)
1028{
1029 struct kgsl_process_private *private;
1030
1031 mutex_lock(&kgsl_driver.process_mutex);
1032 private = kgsl_process_private_new(device);
1033
1034 if (IS_ERR(private))
1035 goto done;
1036
1037 /*
1038 * If this is a new process create the debug directories and add it to
1039 * the process list
1040 */
1041
1042 if (private->fd_count++ == 0) {
1043 kgsl_process_init_sysfs(device, private);
1044 kgsl_process_init_debugfs(private);
1045
1046 list_add(&private->list, &kgsl_driver.process_list);
1047 }
1048
1049done:
1050 mutex_unlock(&kgsl_driver.process_mutex);
1051 return private;
1052}
1053
1054static int kgsl_close_device(struct kgsl_device *device)
1055{
1056 int result = 0;
1057
1058 mutex_lock(&device->mutex);
Kyle Piefer89d64fe2017-05-15 09:15:43 -07001059 device->open_count--;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001060 if (device->open_count == 0) {
1061
1062 /* Wait for the active count to go to 0 */
1063 kgsl_active_count_wait(device, 0);
1064
1065 /* Fail if the wait times out */
1066 BUG_ON(atomic_read(&device->active_cnt) > 0);
1067
1068 result = kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1069 }
1070 mutex_unlock(&device->mutex);
1071 return result;
1072
1073}
1074
1075static void device_release_contexts(struct kgsl_device_private *dev_priv)
1076{
1077 struct kgsl_device *device = dev_priv->device;
1078 struct kgsl_context *context;
1079 int next = 0;
1080 int result = 0;
1081
1082 while (1) {
1083 read_lock(&device->context_lock);
1084 context = idr_get_next(&device->context_idr, &next);
1085
1086 if (context == NULL) {
1087 read_unlock(&device->context_lock);
1088 break;
1089 } else if (context->dev_priv == dev_priv) {
1090 /*
1091 * Hold a reference to the context in case somebody
1092 * tries to put it while we are detaching
1093 */
1094 result = _kgsl_context_get(context);
1095 }
1096 read_unlock(&device->context_lock);
1097
1098 if (result) {
1099 kgsl_context_detach(context);
1100 kgsl_context_put(context);
1101 result = 0;
1102 }
1103
1104 next = next + 1;
1105 }
1106}
1107
1108static int kgsl_release(struct inode *inodep, struct file *filep)
1109{
1110 struct kgsl_device_private *dev_priv = filep->private_data;
1111 struct kgsl_device *device = dev_priv->device;
1112 int result;
1113
1114 filep->private_data = NULL;
1115
1116 /* Release the contexts for the file */
1117 device_release_contexts(dev_priv);
1118
1119 /* Close down the process wide resources for the file */
1120 kgsl_process_private_close(dev_priv, dev_priv->process_priv);
1121
Harshdeep Dhattf61e3b52014-12-15 13:45:19 -07001122 /* Destroy the device-specific structure */
1123 device->ftbl->device_private_destroy(dev_priv);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001124
1125 result = kgsl_close_device(device);
1126 pm_runtime_put(&device->pdev->dev);
1127
1128 return result;
1129}
1130
1131static int kgsl_open_device(struct kgsl_device *device)
1132{
1133 int result = 0;
1134
1135 mutex_lock(&device->mutex);
1136 if (device->open_count == 0) {
1137 /*
1138 * active_cnt special case: we are starting up for the first
1139 * time, so use this sequence instead of the kgsl_pwrctrl_wake()
1140 * which will be called by kgsl_active_count_get().
1141 */
1142 atomic_inc(&device->active_cnt);
1143 kgsl_sharedmem_set(device, &device->memstore, 0, 0,
1144 device->memstore.size);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001145
1146 result = device->ftbl->init(device);
1147 if (result)
1148 goto err;
1149
1150 result = device->ftbl->start(device, 0);
1151 if (result)
1152 goto err;
1153 /*
1154 * Make sure the gates are open, so they don't block until
1155 * we start suspend or FT.
1156 */
1157 complete_all(&device->hwaccess_gate);
1158 kgsl_pwrctrl_change_state(device, KGSL_STATE_ACTIVE);
1159 kgsl_active_count_put(device);
1160 }
1161 device->open_count++;
1162err:
1163 if (result) {
1164 kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1165 atomic_dec(&device->active_cnt);
1166 }
1167
1168 mutex_unlock(&device->mutex);
1169 return result;
1170}
1171
1172static int kgsl_open(struct inode *inodep, struct file *filep)
1173{
1174 int result;
1175 struct kgsl_device_private *dev_priv;
1176 struct kgsl_device *device;
1177 unsigned int minor = iminor(inodep);
1178
1179 device = kgsl_get_minor(minor);
1180 BUG_ON(device == NULL);
1181
1182 result = pm_runtime_get_sync(&device->pdev->dev);
1183 if (result < 0) {
1184 KGSL_DRV_ERR(device,
1185 "Runtime PM: Unable to wake up the device, rc = %d\n",
1186 result);
1187 return result;
1188 }
1189 result = 0;
1190
Harshdeep Dhattf61e3b52014-12-15 13:45:19 -07001191 dev_priv = device->ftbl->device_private_create();
Shrenuj Bansala419c792016-10-20 14:05:11 -07001192 if (dev_priv == NULL) {
1193 result = -ENOMEM;
1194 goto err;
1195 }
1196
1197 dev_priv->device = device;
1198 filep->private_data = dev_priv;
1199
1200 result = kgsl_open_device(device);
1201 if (result)
1202 goto err;
1203
1204 /*
1205 * Get file (per process) private struct. This must be done
1206 * after the first start so that the global pagetable mappings
1207 * are set up before we create the per-process pagetable.
1208 */
1209 dev_priv->process_priv = kgsl_process_private_open(device);
1210 if (IS_ERR(dev_priv->process_priv)) {
1211 result = PTR_ERR(dev_priv->process_priv);
1212 kgsl_close_device(device);
1213 goto err;
1214 }
1215
1216err:
1217 if (result) {
1218 filep->private_data = NULL;
1219 kfree(dev_priv);
1220 pm_runtime_put(&device->pdev->dev);
1221 }
1222 return result;
1223}
1224
1225#define GPUADDR_IN_MEMDESC(_val, _memdesc) \
1226 (((_val) >= (_memdesc)->gpuaddr) && \
1227 ((_val) < ((_memdesc)->gpuaddr + (_memdesc)->size)))
1228
1229/**
1230 * kgsl_sharedmem_find() - Find a gpu memory allocation
1231 *
1232 * @private: private data for the process to check.
1233 * @gpuaddr: start address of the region
1234 *
1235 * Find a gpu allocation. Caller must kgsl_mem_entry_put()
1236 * the returned entry when finished using it.
1237 */
1238struct kgsl_mem_entry * __must_check
1239kgsl_sharedmem_find(struct kgsl_process_private *private, uint64_t gpuaddr)
1240{
1241 int ret = 0, id;
1242 struct kgsl_mem_entry *entry = NULL;
1243
1244 if (!private)
1245 return NULL;
1246
1247 if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, gpuaddr))
1248 return NULL;
1249
1250 spin_lock(&private->mem_lock);
1251 idr_for_each_entry(&private->mem_idr, entry, id) {
1252 if (GPUADDR_IN_MEMDESC(gpuaddr, &entry->memdesc)) {
Deepak Kumar0a4ef64e2017-05-08 15:29:03 +05301253 if (!entry->pending_free)
1254 ret = kgsl_mem_entry_get(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001255 break;
1256 }
1257 }
1258 spin_unlock(&private->mem_lock);
1259
1260 return (ret == 0) ? NULL : entry;
1261}
1262EXPORT_SYMBOL(kgsl_sharedmem_find);
1263
1264struct kgsl_mem_entry * __must_check
1265kgsl_sharedmem_find_id_flags(struct kgsl_process_private *process,
1266 unsigned int id, uint64_t flags)
1267{
1268 int count = 0;
1269 struct kgsl_mem_entry *entry;
1270
1271 spin_lock(&process->mem_lock);
1272 entry = idr_find(&process->mem_idr, id);
1273 if (entry)
1274 if (!entry->pending_free &&
1275 (flags & entry->memdesc.flags) == flags)
1276 count = kgsl_mem_entry_get(entry);
1277 spin_unlock(&process->mem_lock);
1278
1279 return (count == 0) ? NULL : entry;
1280}
1281
1282/**
1283 * kgsl_sharedmem_find_id() - find a memory entry by id
1284 * @process: the owning process
1285 * @id: id to find
1286 *
1287 * @returns - the mem_entry or NULL
1288 *
1289 * Caller must kgsl_mem_entry_put() the returned entry, when finished using
1290 * it.
1291 */
1292struct kgsl_mem_entry * __must_check
1293kgsl_sharedmem_find_id(struct kgsl_process_private *process, unsigned int id)
1294{
1295 return kgsl_sharedmem_find_id_flags(process, id, 0);
1296}
1297
1298/**
1299 * kgsl_mem_entry_unset_pend() - Unset the pending free flag of an entry
1300 * @entry - The memory entry
1301 */
1302static inline void kgsl_mem_entry_unset_pend(struct kgsl_mem_entry *entry)
1303{
1304 if (entry == NULL)
1305 return;
1306 spin_lock(&entry->priv->mem_lock);
1307 entry->pending_free = 0;
1308 spin_unlock(&entry->priv->mem_lock);
1309}
1310
1311/**
1312 * kgsl_mem_entry_set_pend() - Set the pending free flag of a memory entry
1313 * @entry - The memory entry
1314 *
1315 * @returns - true if pending flag was 0 else false
1316 *
1317 * This function will set the pending free flag if it is previously unset. Used
1318 * to prevent race condition between ioctls calling free/freememontimestamp
1319 * on the same entry. Whichever thread set's the flag first will do the free.
1320 */
1321static inline bool kgsl_mem_entry_set_pend(struct kgsl_mem_entry *entry)
1322{
1323 bool ret = false;
1324
1325 if (entry == NULL)
1326 return false;
1327
1328 spin_lock(&entry->priv->mem_lock);
1329 if (!entry->pending_free) {
1330 entry->pending_free = 1;
1331 ret = true;
1332 }
1333 spin_unlock(&entry->priv->mem_lock);
1334 return ret;
1335}
1336
1337/*call all ioctl sub functions with driver locked*/
1338long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
1339 unsigned int cmd, void *data)
1340{
1341 int result = 0;
1342 struct kgsl_device_getproperty *param = data;
1343
1344 switch (param->type) {
1345 case KGSL_PROP_VERSION:
1346 {
1347 struct kgsl_version version;
1348
1349 if (param->sizebytes != sizeof(version)) {
1350 result = -EINVAL;
1351 break;
1352 }
1353
1354 version.drv_major = KGSL_VERSION_MAJOR;
1355 version.drv_minor = KGSL_VERSION_MINOR;
1356 version.dev_major = dev_priv->device->ver_major;
1357 version.dev_minor = dev_priv->device->ver_minor;
1358
1359 if (copy_to_user(param->value, &version, sizeof(version)))
1360 result = -EFAULT;
1361
1362 break;
1363 }
1364 case KGSL_PROP_GPU_RESET_STAT:
1365 {
1366 /* Return reset status of given context and clear it */
1367 uint32_t id;
1368 struct kgsl_context *context;
1369
1370 if (param->sizebytes != sizeof(unsigned int)) {
1371 result = -EINVAL;
1372 break;
1373 }
1374 /* We expect the value passed in to contain the context id */
1375 if (copy_from_user(&id, param->value,
1376 sizeof(unsigned int))) {
1377 result = -EFAULT;
1378 break;
1379 }
1380 context = kgsl_context_get_owner(dev_priv, id);
1381 if (!context) {
1382 result = -EINVAL;
1383 break;
1384 }
1385 /*
1386 * Copy the reset status to value which also serves as
1387 * the out parameter
1388 */
1389 if (copy_to_user(param->value, &(context->reset_status),
1390 sizeof(unsigned int)))
1391 result = -EFAULT;
1392 else {
1393 /* Clear reset status once its been queried */
1394 context->reset_status = KGSL_CTX_STAT_NO_ERROR;
1395 }
1396
1397 kgsl_context_put(context);
1398 break;
1399 }
Sunil Khatrib055b6b2018-07-19 17:10:39 +05301400 case KGSL_PROP_SECURE_BUFFER_ALIGNMENT:
1401 {
1402 unsigned int align;
1403
1404 if (param->sizebytes != sizeof(unsigned int)) {
1405 result = -EINVAL;
1406 break;
1407 }
1408 /*
1409 * XPUv2 impose the constraint of 1MB memory alignment,
1410 * on the other hand Hypervisor does not have such
1411 * constraints. So driver should fulfill such
1412 * requirements when allocating secure memory.
1413 */
1414 align = MMU_FEATURE(&dev_priv->device->mmu,
1415 KGSL_MMU_HYP_SECURE_ALLOC) ? PAGE_SIZE : SZ_1M;
1416
1417 if (copy_to_user(param->value, &align, sizeof(align)))
1418 result = -EFAULT;
1419
1420 break;
1421 }
Sunil Khatric4d4c2d2018-08-10 11:46:58 +05301422 case KGSL_PROP_SECURE_CTXT_SUPPORT:
1423 {
1424 unsigned int secure_ctxt;
1425
1426 if (param->sizebytes != sizeof(unsigned int)) {
1427 result = -EINVAL;
1428 break;
1429 }
1430
1431 secure_ctxt = dev_priv->device->mmu.secured ? 1 : 0;
1432
1433 if (copy_to_user(param->value, &secure_ctxt,
1434 sizeof(secure_ctxt)))
1435 result = -EFAULT;
1436
1437 break;
1438 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07001439 default:
1440 if (is_compat_task())
1441 result = dev_priv->device->ftbl->getproperty_compat(
1442 dev_priv->device, param->type,
1443 param->value, param->sizebytes);
1444 else
1445 result = dev_priv->device->ftbl->getproperty(
1446 dev_priv->device, param->type,
1447 param->value, param->sizebytes);
1448 }
1449
1450
1451 return result;
1452}
1453
1454long kgsl_ioctl_device_setproperty(struct kgsl_device_private *dev_priv,
1455 unsigned int cmd, void *data)
1456{
1457 int result = 0;
1458 /* The getproperty struct is reused for setproperty too */
1459 struct kgsl_device_getproperty *param = data;
1460
1461 /* Reroute to compat version if coming from compat_ioctl */
1462 if (is_compat_task())
1463 result = dev_priv->device->ftbl->setproperty_compat(
1464 dev_priv, param->type, param->value,
1465 param->sizebytes);
1466 else if (dev_priv->device->ftbl->setproperty)
1467 result = dev_priv->device->ftbl->setproperty(
1468 dev_priv, param->type, param->value,
1469 param->sizebytes);
1470
1471 return result;
1472}
1473
1474long kgsl_ioctl_device_waittimestamp_ctxtid(
1475 struct kgsl_device_private *dev_priv, unsigned int cmd,
1476 void *data)
1477{
1478 struct kgsl_device_waittimestamp_ctxtid *param = data;
1479 struct kgsl_device *device = dev_priv->device;
1480 long result = -EINVAL;
1481 unsigned int temp_cur_ts = 0;
1482 struct kgsl_context *context;
1483
1484 context = kgsl_context_get_owner(dev_priv, param->context_id);
1485 if (context == NULL)
1486 return result;
1487
1488 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1489 &temp_cur_ts);
1490
1491 trace_kgsl_waittimestamp_entry(device, context->id, temp_cur_ts,
1492 param->timestamp, param->timeout);
1493
1494 result = device->ftbl->waittimestamp(device, context, param->timestamp,
1495 param->timeout);
1496
1497 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1498 &temp_cur_ts);
1499 trace_kgsl_waittimestamp_exit(device, temp_cur_ts, result);
1500
1501 kgsl_context_put(context);
1502
1503 return result;
1504}
1505
Tarun Karra2b8b3632016-11-14 16:38:27 -08001506static inline bool _check_context_is_sparse(struct kgsl_context *context,
1507 uint64_t flags)
1508{
1509 if ((context->flags & KGSL_CONTEXT_SPARSE) ||
1510 (flags & KGSL_DRAWOBJ_SPARSE))
1511 return true;
1512
1513 return false;
1514}
1515
1516
Shrenuj Bansala419c792016-10-20 14:05:11 -07001517long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
1518 unsigned int cmd, void *data)
1519{
1520 struct kgsl_ringbuffer_issueibcmds *param = data;
1521 struct kgsl_device *device = dev_priv->device;
1522 struct kgsl_context *context;
1523 struct kgsl_drawobj *drawobj;
1524 struct kgsl_drawobj_cmd *cmdobj;
1525 long result = -EINVAL;
1526
1527 /* The legacy functions don't support synchronization commands */
1528 if ((param->flags & (KGSL_DRAWOBJ_SYNC | KGSL_DRAWOBJ_MARKER)))
1529 return -EINVAL;
1530
1531 /* Sanity check the number of IBs */
1532 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST &&
1533 (param->numibs == 0 || param->numibs > KGSL_MAX_NUMIBS))
1534 return -EINVAL;
1535
1536 /* Get the context */
1537 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1538 if (context == NULL)
1539 return -EINVAL;
1540
Tarun Karra2b8b3632016-11-14 16:38:27 -08001541 if (_check_context_is_sparse(context, param->flags)) {
1542 kgsl_context_put(context);
1543 return -EINVAL;
1544 }
1545
Shrenuj Bansala419c792016-10-20 14:05:11 -07001546 cmdobj = kgsl_drawobj_cmd_create(device, context, param->flags,
1547 CMDOBJ_TYPE);
1548 if (IS_ERR(cmdobj)) {
1549 kgsl_context_put(context);
1550 return PTR_ERR(cmdobj);
1551 }
1552
1553 drawobj = DRAWOBJ(cmdobj);
1554
1555 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST)
1556 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1557 (void __user *) param->ibdesc_addr,
1558 param->numibs);
1559 else {
1560 struct kgsl_ibdesc ibdesc;
1561 /* Ultra legacy path */
1562
1563 ibdesc.gpuaddr = param->ibdesc_addr;
1564 ibdesc.sizedwords = param->numibs;
1565 ibdesc.ctrl = 0;
1566
1567 result = kgsl_drawobj_cmd_add_ibdesc(device, cmdobj, &ibdesc);
1568 }
1569
1570 if (result == 0)
1571 result = dev_priv->device->ftbl->queue_cmds(dev_priv, context,
1572 &drawobj, 1, &param->timestamp);
1573
1574 /*
1575 * -EPROTO is a "success" error - it just tells the user that the
1576 * context had previously faulted
1577 */
1578 if (result && result != -EPROTO)
1579 kgsl_drawobj_destroy(drawobj);
1580
1581 kgsl_context_put(context);
1582 return result;
1583}
1584
1585/* Returns 0 on failure. Returns command type(s) on success */
1586static unsigned int _process_command_input(struct kgsl_device *device,
1587 unsigned int flags, unsigned int numcmds,
1588 unsigned int numobjs, unsigned int numsyncs)
1589{
1590 if (numcmds > KGSL_MAX_NUMIBS ||
1591 numobjs > KGSL_MAX_NUMIBS ||
1592 numsyncs > KGSL_MAX_SYNCPOINTS)
1593 return 0;
1594
1595 /*
1596 * The SYNC bit is supposed to identify a dummy sync object
1597 * so warn the user if they specified any IBs with it.
1598 * A MARKER command can either have IBs or not but if the
1599 * command has 0 IBs it is automatically assumed to be a marker.
1600 */
1601
1602 /* If they specify the flag, go with what they say */
1603 if (flags & KGSL_DRAWOBJ_MARKER)
1604 return MARKEROBJ_TYPE;
1605 else if (flags & KGSL_DRAWOBJ_SYNC)
1606 return SYNCOBJ_TYPE;
1607
1608 /* If not, deduce what they meant */
1609 if (numsyncs && numcmds)
1610 return SYNCOBJ_TYPE | CMDOBJ_TYPE;
1611 else if (numsyncs)
1612 return SYNCOBJ_TYPE;
1613 else if (numcmds)
1614 return CMDOBJ_TYPE;
1615 else if (numcmds == 0)
1616 return MARKEROBJ_TYPE;
1617
1618 return 0;
1619}
1620
1621long kgsl_ioctl_submit_commands(struct kgsl_device_private *dev_priv,
1622 unsigned int cmd, void *data)
1623{
1624 struct kgsl_submit_commands *param = data;
1625 struct kgsl_device *device = dev_priv->device;
1626 struct kgsl_context *context;
1627 struct kgsl_drawobj *drawobj[2];
1628 unsigned int type;
1629 long result;
1630 unsigned int i = 0;
1631
1632 type = _process_command_input(device, param->flags, param->numcmds, 0,
1633 param->numsyncs);
1634 if (!type)
1635 return -EINVAL;
1636
1637 context = kgsl_context_get_owner(dev_priv, param->context_id);
1638 if (context == NULL)
1639 return -EINVAL;
1640
Tarun Karra2b8b3632016-11-14 16:38:27 -08001641 if (_check_context_is_sparse(context, param->flags)) {
1642 kgsl_context_put(context);
1643 return -EINVAL;
1644 }
1645
Shrenuj Bansala419c792016-10-20 14:05:11 -07001646 if (type & SYNCOBJ_TYPE) {
1647 struct kgsl_drawobj_sync *syncobj =
1648 kgsl_drawobj_sync_create(device, context);
1649 if (IS_ERR(syncobj)) {
1650 result = PTR_ERR(syncobj);
1651 goto done;
1652 }
1653
1654 drawobj[i++] = DRAWOBJ(syncobj);
1655
1656 result = kgsl_drawobj_sync_add_syncpoints(device, syncobj,
1657 param->synclist, param->numsyncs);
1658 if (result)
1659 goto done;
1660 }
1661
1662 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1663 struct kgsl_drawobj_cmd *cmdobj =
1664 kgsl_drawobj_cmd_create(device,
1665 context, param->flags, type);
1666 if (IS_ERR(cmdobj)) {
1667 result = PTR_ERR(cmdobj);
1668 goto done;
1669 }
1670
1671 drawobj[i++] = DRAWOBJ(cmdobj);
1672
1673 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1674 param->cmdlist, param->numcmds);
1675 if (result)
1676 goto done;
1677
1678 /* If no profiling buffer was specified, clear the flag */
1679 if (cmdobj->profiling_buf_entry == NULL)
Lynus Vazeb7af682017-04-17 18:36:01 +05301680 DRAWOBJ(cmdobj)->flags &=
1681 ~(unsigned long)KGSL_DRAWOBJ_PROFILING;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001682 }
1683
1684 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1685 i, &param->timestamp);
1686
1687done:
1688 /*
1689 * -EPROTO is a "success" error - it just tells the user that the
1690 * context had previously faulted
1691 */
1692 if (result && result != -EPROTO)
1693 while (i--)
1694 kgsl_drawobj_destroy(drawobj[i]);
1695
1696
1697 kgsl_context_put(context);
1698 return result;
1699}
1700
1701long kgsl_ioctl_gpu_command(struct kgsl_device_private *dev_priv,
1702 unsigned int cmd, void *data)
1703{
1704 struct kgsl_gpu_command *param = data;
1705 struct kgsl_device *device = dev_priv->device;
1706 struct kgsl_context *context;
1707 struct kgsl_drawobj *drawobj[2];
1708 unsigned int type;
1709 long result;
1710 unsigned int i = 0;
1711
1712 type = _process_command_input(device, param->flags, param->numcmds,
1713 param->numobjs, param->numsyncs);
1714 if (!type)
1715 return -EINVAL;
1716
1717 context = kgsl_context_get_owner(dev_priv, param->context_id);
1718 if (context == NULL)
1719 return -EINVAL;
1720
Tarun Karra2b8b3632016-11-14 16:38:27 -08001721 if (_check_context_is_sparse(context, param->flags)) {
1722 kgsl_context_put(context);
1723 return -EINVAL;
1724 }
1725
Shrenuj Bansala419c792016-10-20 14:05:11 -07001726 if (type & SYNCOBJ_TYPE) {
1727 struct kgsl_drawobj_sync *syncobj =
1728 kgsl_drawobj_sync_create(device, context);
1729
1730 if (IS_ERR(syncobj)) {
1731 result = PTR_ERR(syncobj);
1732 goto done;
1733 }
1734
1735 drawobj[i++] = DRAWOBJ(syncobj);
1736
1737 result = kgsl_drawobj_sync_add_synclist(device, syncobj,
1738 to_user_ptr(param->synclist),
1739 param->syncsize, param->numsyncs);
1740 if (result)
1741 goto done;
1742 }
1743
1744 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1745 struct kgsl_drawobj_cmd *cmdobj =
1746 kgsl_drawobj_cmd_create(device,
1747 context, param->flags, type);
1748
1749 if (IS_ERR(cmdobj)) {
1750 result = PTR_ERR(cmdobj);
1751 goto done;
1752 }
1753
1754 drawobj[i++] = DRAWOBJ(cmdobj);
1755
1756 result = kgsl_drawobj_cmd_add_cmdlist(device, cmdobj,
1757 to_user_ptr(param->cmdlist),
1758 param->cmdsize, param->numcmds);
1759 if (result)
1760 goto done;
1761
1762 result = kgsl_drawobj_cmd_add_memlist(device, cmdobj,
1763 to_user_ptr(param->objlist),
1764 param->objsize, param->numobjs);
1765 if (result)
1766 goto done;
1767
1768 /* If no profiling buffer was specified, clear the flag */
1769 if (cmdobj->profiling_buf_entry == NULL)
Lynus Vazeb7af682017-04-17 18:36:01 +05301770 DRAWOBJ(cmdobj)->flags &=
1771 ~(unsigned long)KGSL_DRAWOBJ_PROFILING;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001772 }
1773
1774 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1775 i, &param->timestamp);
1776
1777done:
1778 /*
1779 * -EPROTO is a "success" error - it just tells the user that the
1780 * context had previously faulted
1781 */
1782 if (result && result != -EPROTO)
1783 while (i--)
1784 kgsl_drawobj_destroy(drawobj[i]);
1785
1786 kgsl_context_put(context);
1787 return result;
1788}
1789
1790long kgsl_ioctl_cmdstream_readtimestamp_ctxtid(struct kgsl_device_private
1791 *dev_priv, unsigned int cmd,
1792 void *data)
1793{
1794 struct kgsl_cmdstream_readtimestamp_ctxtid *param = data;
1795 struct kgsl_device *device = dev_priv->device;
1796 struct kgsl_context *context;
1797 long result = -EINVAL;
1798
1799 mutex_lock(&device->mutex);
1800 context = kgsl_context_get_owner(dev_priv, param->context_id);
1801
1802 if (context) {
1803 result = kgsl_readtimestamp(device, context,
1804 param->type, &param->timestamp);
1805
1806 trace_kgsl_readtimestamp(device, context->id,
1807 param->type, param->timestamp);
1808 }
1809
1810 kgsl_context_put(context);
1811 mutex_unlock(&device->mutex);
1812 return result;
1813}
1814
1815long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1816 unsigned int cmd, void *data)
1817{
1818 int result = 0;
1819 struct kgsl_drawctxt_create *param = data;
1820 struct kgsl_context *context = NULL;
1821 struct kgsl_device *device = dev_priv->device;
1822
1823 context = device->ftbl->drawctxt_create(dev_priv, &param->flags);
1824 if (IS_ERR(context)) {
1825 result = PTR_ERR(context);
1826 goto done;
1827 }
1828 trace_kgsl_context_create(dev_priv->device, context, param->flags);
1829
1830 /* Commit the pointer to the context in context_idr */
1831 write_lock(&device->context_lock);
1832 idr_replace(&device->context_idr, context, context->id);
Sunil Khatridd90d682017-04-06 18:28:31 +05301833 param->drawctxt_id = context->id;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001834 write_unlock(&device->context_lock);
1835
Shrenuj Bansala419c792016-10-20 14:05:11 -07001836done:
1837 return result;
1838}
1839
1840long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1841 unsigned int cmd, void *data)
1842{
1843 struct kgsl_drawctxt_destroy *param = data;
1844 struct kgsl_context *context;
1845
1846 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1847 if (context == NULL)
1848 return -EINVAL;
1849
1850 kgsl_context_detach(context);
1851 kgsl_context_put(context);
1852
1853 return 0;
1854}
1855
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -06001856long gpumem_free_entry(struct kgsl_mem_entry *entry)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001857{
Shrenuj Bansala419c792016-10-20 14:05:11 -07001858 if (!kgsl_mem_entry_set_pend(entry))
1859 return -EBUSY;
1860
1861 trace_kgsl_mem_free(entry);
Lynus Vaz3fe67582017-11-08 15:22:32 +05301862 kgsl_memfree_add(entry->priv->pid,
1863 entry->memdesc.pagetable ?
1864 entry->memdesc.pagetable->name : 0,
1865 entry->memdesc.gpuaddr, entry->memdesc.size,
1866 entry->memdesc.flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001867
1868 kgsl_mem_entry_put(entry);
1869
1870 return 0;
1871}
1872
1873static void gpumem_free_func(struct kgsl_device *device,
1874 struct kgsl_event_group *group, void *priv, int ret)
1875{
1876 struct kgsl_context *context = group->context;
1877 struct kgsl_mem_entry *entry = priv;
1878 unsigned int timestamp;
1879
1880 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &timestamp);
1881
1882 /* Free the memory for all event types */
1883 trace_kgsl_mem_timestamp_free(device, entry, KGSL_CONTEXT_ID(context),
1884 timestamp, 0);
Lynus Vaz3fe67582017-11-08 15:22:32 +05301885 kgsl_memfree_add(entry->priv->pid,
1886 entry->memdesc.pagetable ?
1887 entry->memdesc.pagetable->name : 0,
1888 entry->memdesc.gpuaddr, entry->memdesc.size,
1889 entry->memdesc.flags);
1890
Shrenuj Bansala419c792016-10-20 14:05:11 -07001891 kgsl_mem_entry_put(entry);
1892}
1893
1894static long gpumem_free_entry_on_timestamp(struct kgsl_device *device,
1895 struct kgsl_mem_entry *entry,
1896 struct kgsl_context *context, unsigned int timestamp)
1897{
1898 int ret;
1899 unsigned int temp;
1900
1901 if (!kgsl_mem_entry_set_pend(entry))
1902 return -EBUSY;
1903
1904 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &temp);
1905 trace_kgsl_mem_timestamp_queue(device, entry, context->id, temp,
1906 timestamp);
1907 ret = kgsl_add_event(device, &context->events,
1908 timestamp, gpumem_free_func, entry);
1909
1910 if (ret)
1911 kgsl_mem_entry_unset_pend(entry);
1912
1913 return ret;
1914}
1915
1916long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1917 unsigned int cmd, void *data)
1918{
1919 struct kgsl_sharedmem_free *param = data;
1920 struct kgsl_process_private *private = dev_priv->process_priv;
1921 struct kgsl_mem_entry *entry;
1922 long ret;
1923
1924 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
1925 if (entry == NULL)
1926 return -EINVAL;
1927
1928 ret = gpumem_free_entry(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05301929 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001930
1931 return ret;
1932}
1933
1934long kgsl_ioctl_gpumem_free_id(struct kgsl_device_private *dev_priv,
1935 unsigned int cmd, void *data)
1936{
1937 struct kgsl_gpumem_free_id *param = data;
1938 struct kgsl_process_private *private = dev_priv->process_priv;
1939 struct kgsl_mem_entry *entry;
1940 long ret;
1941
1942 entry = kgsl_sharedmem_find_id(private, param->id);
1943 if (entry == NULL)
1944 return -EINVAL;
1945
1946 ret = gpumem_free_entry(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05301947 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001948
1949 return ret;
1950}
1951
1952static long gpuobj_free_on_timestamp(struct kgsl_device_private *dev_priv,
1953 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1954{
1955 struct kgsl_gpu_event_timestamp event;
1956 struct kgsl_context *context;
1957 long ret;
1958
1959 memset(&event, 0, sizeof(event));
1960
1961 ret = _copy_from_user(&event, to_user_ptr(param->priv),
1962 sizeof(event), param->len);
1963 if (ret)
1964 return ret;
1965
1966 if (event.context_id == 0)
1967 return -EINVAL;
1968
1969 context = kgsl_context_get_owner(dev_priv, event.context_id);
1970 if (context == NULL)
1971 return -EINVAL;
1972
1973 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry, context,
1974 event.timestamp);
1975
1976 kgsl_context_put(context);
1977 return ret;
1978}
1979
Lynus Vaz27da44d2017-07-26 13:50:10 +05301980static bool gpuobj_free_fence_func(void *priv)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001981{
1982 struct kgsl_mem_entry *entry = priv;
1983
Lynus Vaz3fe67582017-11-08 15:22:32 +05301984 trace_kgsl_mem_free(entry);
1985 kgsl_memfree_add(entry->priv->pid,
1986 entry->memdesc.pagetable ?
1987 entry->memdesc.pagetable->name : 0,
1988 entry->memdesc.gpuaddr, entry->memdesc.size,
1989 entry->memdesc.flags);
1990
Hareesh Gundu615439d2017-06-16 17:06:57 +05301991 INIT_WORK(&entry->work, _deferred_put);
1992 queue_work(kgsl_driver.mem_workqueue, &entry->work);
Lynus Vaz27da44d2017-07-26 13:50:10 +05301993 return true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001994}
1995
1996static long gpuobj_free_on_fence(struct kgsl_device_private *dev_priv,
1997 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1998{
Lynus Vazc031a9b2017-01-25 13:00:13 +05301999 struct kgsl_sync_fence_cb *handle;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002000 struct kgsl_gpu_event_fence event;
2001 long ret;
2002
2003 if (!kgsl_mem_entry_set_pend(entry))
2004 return -EBUSY;
2005
2006 memset(&event, 0, sizeof(event));
2007
2008 ret = _copy_from_user(&event, to_user_ptr(param->priv),
2009 sizeof(event), param->len);
2010 if (ret) {
2011 kgsl_mem_entry_unset_pend(entry);
2012 return ret;
2013 }
2014
2015 if (event.fd < 0) {
2016 kgsl_mem_entry_unset_pend(entry);
2017 return -EINVAL;
2018 }
2019
2020 handle = kgsl_sync_fence_async_wait(event.fd,
Puranam V G Tejaswi0ebbdcd2018-12-13 15:47:00 +05302021 gpuobj_free_fence_func, entry, NULL);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002022
Shrenuj Bansala419c792016-10-20 14:05:11 -07002023 if (IS_ERR(handle)) {
2024 kgsl_mem_entry_unset_pend(entry);
2025 return PTR_ERR(handle);
2026 }
2027
Lynus Vaz3fe67582017-11-08 15:22:32 +05302028 /* if handle is NULL the fence has already signaled */
2029 if (handle == NULL)
2030 gpuobj_free_fence_func(entry);
2031
Shrenuj Bansala419c792016-10-20 14:05:11 -07002032 return 0;
2033}
2034
2035long kgsl_ioctl_gpuobj_free(struct kgsl_device_private *dev_priv,
2036 unsigned int cmd, void *data)
2037{
2038 struct kgsl_gpuobj_free *param = data;
2039 struct kgsl_process_private *private = dev_priv->process_priv;
2040 struct kgsl_mem_entry *entry;
2041 long ret;
2042
2043 entry = kgsl_sharedmem_find_id(private, param->id);
2044 if (entry == NULL)
2045 return -EINVAL;
2046
2047 /* If no event is specified then free immediately */
2048 if (!(param->flags & KGSL_GPUOBJ_FREE_ON_EVENT))
2049 ret = gpumem_free_entry(entry);
2050 else if (param->type == KGSL_GPU_EVENT_TIMESTAMP)
2051 ret = gpuobj_free_on_timestamp(dev_priv, entry, param);
2052 else if (param->type == KGSL_GPU_EVENT_FENCE)
2053 ret = gpuobj_free_on_fence(dev_priv, entry, param);
2054 else
2055 ret = -EINVAL;
2056
Hareesh Gundu615439d2017-06-16 17:06:57 +05302057 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002058 return ret;
2059}
2060
2061long kgsl_ioctl_cmdstream_freememontimestamp_ctxtid(
2062 struct kgsl_device_private *dev_priv,
2063 unsigned int cmd, void *data)
2064{
2065 struct kgsl_cmdstream_freememontimestamp_ctxtid *param = data;
2066 struct kgsl_context *context = NULL;
2067 struct kgsl_mem_entry *entry;
2068 long ret = -EINVAL;
2069
2070 if (param->type != KGSL_TIMESTAMP_RETIRED)
2071 return -EINVAL;
2072
2073 context = kgsl_context_get_owner(dev_priv, param->context_id);
2074 if (context == NULL)
2075 return -EINVAL;
2076
2077 entry = kgsl_sharedmem_find(dev_priv->process_priv,
2078 (uint64_t) param->gpuaddr);
2079 if (entry == NULL) {
2080 kgsl_context_put(context);
2081 return -EINVAL;
2082 }
2083
2084 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry,
2085 context, param->timestamp);
2086
2087 kgsl_mem_entry_put(entry);
2088 kgsl_context_put(context);
2089
2090 return ret;
2091}
2092
2093static inline int _check_region(unsigned long start, unsigned long size,
2094 uint64_t len)
2095{
2096 uint64_t end = ((uint64_t) start) + size;
2097
2098 return (end > len);
2099}
2100
2101static int check_vma_flags(struct vm_area_struct *vma,
2102 unsigned int flags)
2103{
2104 unsigned long flags_requested = (VM_READ | VM_WRITE);
2105
2106 if (flags & KGSL_MEMFLAGS_GPUREADONLY)
Lynus Vazeb7af682017-04-17 18:36:01 +05302107 flags_requested &= ~(unsigned long)VM_WRITE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002108
2109 if ((vma->vm_flags & flags_requested) == flags_requested)
2110 return 0;
2111
2112 return -EFAULT;
2113}
2114
2115static int check_vma(struct vm_area_struct *vma, struct file *vmfile,
2116 struct kgsl_memdesc *memdesc)
2117{
2118 if (vma == NULL || vma->vm_file != vmfile)
2119 return -EINVAL;
2120
2121 /* userspace may not know the size, in which case use the whole vma */
2122 if (memdesc->size == 0)
2123 memdesc->size = vma->vm_end - vma->vm_start;
2124 /* range checking */
2125 if (vma->vm_start != memdesc->useraddr ||
2126 (memdesc->useraddr + memdesc->size) != vma->vm_end)
2127 return -EINVAL;
2128 return check_vma_flags(vma, memdesc->flags);
2129}
2130
2131static int memdesc_sg_virt(struct kgsl_memdesc *memdesc, struct file *vmfile)
2132{
2133 int ret = 0;
2134 long npages = 0, i;
2135 size_t sglen = (size_t) (memdesc->size / PAGE_SIZE);
2136 struct page **pages = NULL;
2137 int write = ((memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY) ? 0 :
2138 FOLL_WRITE);
2139
2140 if (sglen == 0 || sglen >= LONG_MAX)
2141 return -EINVAL;
2142
2143 pages = kgsl_malloc(sglen * sizeof(struct page *));
2144 if (pages == NULL)
2145 return -ENOMEM;
2146
2147 memdesc->sgt = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
2148 if (memdesc->sgt == NULL) {
2149 ret = -ENOMEM;
2150 goto out;
2151 }
2152
2153 down_read(&current->mm->mmap_sem);
2154 /* If we have vmfile, make sure we map the correct vma and map it all */
2155 if (vmfile != NULL)
2156 ret = check_vma(find_vma(current->mm, memdesc->useraddr),
2157 vmfile, memdesc);
2158
2159 if (ret == 0) {
2160 npages = get_user_pages(memdesc->useraddr,
2161 sglen, write, pages, NULL);
2162 ret = (npages < 0) ? (int)npages : 0;
2163 }
2164 up_read(&current->mm->mmap_sem);
2165
2166 if (ret)
2167 goto out;
2168
2169 if ((unsigned long) npages != sglen) {
2170 ret = -EINVAL;
2171 goto out;
2172 }
2173
2174 ret = sg_alloc_table_from_pages(memdesc->sgt, pages, npages,
2175 0, memdesc->size, GFP_KERNEL);
2176out:
2177 if (ret) {
2178 for (i = 0; i < npages; i++)
2179 put_page(pages[i]);
2180
2181 kfree(memdesc->sgt);
2182 memdesc->sgt = NULL;
2183 }
2184 kgsl_free(pages);
2185 return ret;
2186}
2187
2188static int kgsl_setup_anon_useraddr(struct kgsl_pagetable *pagetable,
2189 struct kgsl_mem_entry *entry, unsigned long hostptr,
2190 size_t offset, size_t size)
2191{
2192 /* Map an anonymous memory chunk */
2193
2194 if (size == 0 || offset != 0 ||
2195 !IS_ALIGNED(size, PAGE_SIZE))
2196 return -EINVAL;
2197
2198 entry->memdesc.pagetable = pagetable;
2199 entry->memdesc.size = (uint64_t) size;
2200 entry->memdesc.useraddr = hostptr;
Lynus Vazeb7af682017-04-17 18:36:01 +05302201 entry->memdesc.flags |= (uint64_t)KGSL_MEMFLAGS_USERMEM_ADDR;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002202
2203 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
2204 int ret;
2205
2206 /* Register the address in the database */
2207 ret = kgsl_mmu_set_svm_region(pagetable,
2208 (uint64_t) entry->memdesc.useraddr, (uint64_t) size);
2209
2210 if (ret)
2211 return ret;
2212
2213 entry->memdesc.gpuaddr = (uint64_t) entry->memdesc.useraddr;
2214 }
2215
2216 return memdesc_sg_virt(&entry->memdesc, NULL);
2217}
2218
2219#ifdef CONFIG_DMA_SHARED_BUFFER
2220static int match_file(const void *p, struct file *file, unsigned int fd)
2221{
2222 /*
2223 * We must return fd + 1 because iterate_fd stops searching on
2224 * non-zero return, but 0 is a valid fd.
2225 */
2226 return (p == file) ? (fd + 1) : 0;
2227}
2228
2229static void _setup_cache_mode(struct kgsl_mem_entry *entry,
2230 struct vm_area_struct *vma)
2231{
Lynus Vazeb7af682017-04-17 18:36:01 +05302232 uint64_t mode;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002233 pgprot_t pgprot = vma->vm_page_prot;
2234
2235 if (pgprot_val(pgprot) == pgprot_val(pgprot_noncached(pgprot)))
2236 mode = KGSL_CACHEMODE_UNCACHED;
2237 else if (pgprot_val(pgprot) == pgprot_val(pgprot_writecombine(pgprot)))
2238 mode = KGSL_CACHEMODE_WRITECOMBINE;
2239 else
2240 mode = KGSL_CACHEMODE_WRITEBACK;
2241
2242 entry->memdesc.flags |= (mode << KGSL_CACHEMODE_SHIFT);
2243}
2244
2245static int kgsl_setup_dma_buf(struct kgsl_device *device,
2246 struct kgsl_pagetable *pagetable,
2247 struct kgsl_mem_entry *entry,
2248 struct dma_buf *dmabuf);
2249
2250static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2251 struct kgsl_pagetable *pagetable,
2252 struct kgsl_mem_entry *entry, unsigned long hostptr)
2253{
2254 struct vm_area_struct *vma;
2255 struct dma_buf *dmabuf = NULL;
2256 int ret;
2257
2258 /*
2259 * Find the VMA containing this pointer and figure out if it
2260 * is a dma-buf.
2261 */
2262 down_read(&current->mm->mmap_sem);
2263 vma = find_vma(current->mm, hostptr);
2264
2265 if (vma && vma->vm_file) {
2266 int fd;
2267
2268 ret = check_vma_flags(vma, entry->memdesc.flags);
2269 if (ret) {
2270 up_read(&current->mm->mmap_sem);
2271 return ret;
2272 }
2273
2274 /*
2275 * Check to see that this isn't our own memory that we have
2276 * already mapped
2277 */
2278 if (vma->vm_file->f_op == &kgsl_fops) {
2279 up_read(&current->mm->mmap_sem);
2280 return -EFAULT;
2281 }
2282
2283 /* Look for the fd that matches this the vma file */
2284 fd = iterate_fd(current->files, 0, match_file, vma->vm_file);
2285 if (fd != 0)
2286 dmabuf = dma_buf_get(fd - 1);
2287 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002288
Sunil Khatri51a79372017-07-06 15:09:35 +05302289 if (IS_ERR_OR_NULL(dmabuf)) {
2290 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002291 return dmabuf ? PTR_ERR(dmabuf) : -ENODEV;
Sunil Khatri51a79372017-07-06 15:09:35 +05302292 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002293
2294 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2295 if (ret) {
2296 dma_buf_put(dmabuf);
Sunil Khatri51a79372017-07-06 15:09:35 +05302297 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002298 return ret;
2299 }
2300
2301 /* Setup the user addr/cache mode for cache operations */
2302 entry->memdesc.useraddr = hostptr;
2303 _setup_cache_mode(entry, vma);
Sunil Khatri51a79372017-07-06 15:09:35 +05302304 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002305 return 0;
2306}
2307#else
2308static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2309 struct kgsl_pagetable *pagetable,
2310 struct kgsl_mem_entry *entry, unsigned long hostptr)
2311{
2312 return -ENODEV;
2313}
2314#endif
2315
2316static int kgsl_setup_useraddr(struct kgsl_device *device,
2317 struct kgsl_pagetable *pagetable,
2318 struct kgsl_mem_entry *entry,
2319 unsigned long hostptr, size_t offset, size_t size)
2320{
2321 int ret;
2322
2323 if (hostptr == 0 || !IS_ALIGNED(hostptr, PAGE_SIZE))
2324 return -EINVAL;
2325
2326 /* Try to set up a dmabuf - if it returns -ENODEV assume anonymous */
2327 ret = kgsl_setup_dmabuf_useraddr(device, pagetable, entry, hostptr);
2328 if (ret != -ENODEV)
2329 return ret;
2330
2331 /* Okay - lets go legacy */
2332 return kgsl_setup_anon_useraddr(pagetable, entry,
2333 hostptr, offset, size);
2334}
2335
2336static long _gpuobj_map_useraddr(struct kgsl_device *device,
2337 struct kgsl_pagetable *pagetable,
2338 struct kgsl_mem_entry *entry,
2339 struct kgsl_gpuobj_import *param)
2340{
Archana Obannagarice60fec2017-09-08 20:35:28 +05302341 struct kgsl_gpuobj_import_useraddr useraddr = {0};
Shrenuj Bansala419c792016-10-20 14:05:11 -07002342 int ret;
2343
2344 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2345 | KGSL_CACHEMODE_MASK
2346 | KGSL_MEMTYPE_MASK
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07002347 | KGSL_MEMFLAGS_FORCE_32BIT
2348 | KGSL_MEMFLAGS_IOCOHERENT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002349
2350 /* Specifying SECURE is an explicit error */
2351 if (param->flags & KGSL_MEMFLAGS_SECURE)
2352 return -ENOTSUPP;
2353
2354 ret = _copy_from_user(&useraddr,
2355 to_user_ptr(param->priv), sizeof(useraddr),
2356 param->priv_len);
2357 if (ret)
2358 return ret;
2359
2360 /* Verify that the virtaddr and len are within bounds */
2361 if (useraddr.virtaddr > ULONG_MAX)
2362 return -EINVAL;
2363
2364 return kgsl_setup_useraddr(device, pagetable, entry,
2365 (unsigned long) useraddr.virtaddr, 0, param->priv_len);
2366}
2367
2368#ifdef CONFIG_DMA_SHARED_BUFFER
2369static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2370 struct kgsl_pagetable *pagetable,
2371 struct kgsl_mem_entry *entry,
2372 struct kgsl_gpuobj_import *param,
2373 int *fd)
2374{
2375 struct kgsl_gpuobj_import_dma_buf buf;
2376 struct dma_buf *dmabuf;
2377 int ret;
2378
2379 /*
2380 * If content protection is not enabled and secure buffer
2381 * is requested to be mapped return error.
2382 */
2383 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2384 if (!kgsl_mmu_is_secured(&device->mmu)) {
2385 dev_WARN_ONCE(device->dev, 1,
2386 "Secure buffer not supported");
2387 return -ENOTSUPP;
2388 }
2389
2390 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2391 }
2392
2393 ret = _copy_from_user(&buf, to_user_ptr(param->priv),
2394 sizeof(buf), param->priv_len);
2395 if (ret)
2396 return ret;
2397
2398 if (buf.fd < 0)
2399 return -EINVAL;
2400
2401 *fd = buf.fd;
2402 dmabuf = dma_buf_get(buf.fd);
2403
2404 if (IS_ERR_OR_NULL(dmabuf))
2405 return (dmabuf == NULL) ? -EINVAL : PTR_ERR(dmabuf);
2406
2407 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2408 if (ret)
2409 dma_buf_put(dmabuf);
2410
2411 return ret;
2412}
2413#else
2414static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2415 struct kgsl_pagetable *pagetable,
2416 struct kgsl_mem_entry *entry,
2417 struct kgsl_gpuobj_import *param,
2418 int *fd)
2419{
2420 return -EINVAL;
2421}
2422#endif
2423
2424long kgsl_ioctl_gpuobj_import(struct kgsl_device_private *dev_priv,
2425 unsigned int cmd, void *data)
2426{
2427 struct kgsl_process_private *private = dev_priv->process_priv;
2428 struct kgsl_gpuobj_import *param = data;
2429 struct kgsl_mem_entry *entry;
2430 int ret, fd = -1;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002431
2432 entry = kgsl_mem_entry_create();
2433 if (entry == NULL)
2434 return -ENOMEM;
2435
2436 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2437 | KGSL_MEMTYPE_MASK
2438 | KGSL_MEMALIGN_MASK
2439 | KGSL_MEMFLAGS_USE_CPU_MAP
2440 | KGSL_MEMFLAGS_SECURE
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07002441 | KGSL_MEMFLAGS_FORCE_32BIT
2442 | KGSL_MEMFLAGS_IOCOHERENT;
2443
Deepak Kumarcf056d12018-04-17 15:59:42 +05302444 if (kgsl_is_compat_task())
2445 param->flags |= KGSL_MEMFLAGS_FORCE_32BIT;
2446
Lynus Vaz90d98b52018-04-09 14:45:36 +05302447 kgsl_memdesc_init(dev_priv->device, &entry->memdesc, param->flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002448 if (param->type == KGSL_USER_MEM_TYPE_ADDR)
2449 ret = _gpuobj_map_useraddr(dev_priv->device, private->pagetable,
2450 entry, param);
2451 else if (param->type == KGSL_USER_MEM_TYPE_DMABUF)
2452 ret = _gpuobj_map_dma_buf(dev_priv->device, private->pagetable,
2453 entry, param, &fd);
2454 else
2455 ret = -ENOTSUPP;
2456
2457 if (ret)
2458 goto out;
2459
2460 if (entry->memdesc.size >= SZ_1M)
2461 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2462 else if (entry->memdesc.size >= SZ_64K)
2463 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64K));
2464
2465 param->flags = entry->memdesc.flags;
2466
2467 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
2468 if (ret)
2469 goto unmap;
2470
2471 param->id = entry->id;
2472
2473 KGSL_STATS_ADD(entry->memdesc.size, &kgsl_driver.stats.mapped,
2474 &kgsl_driver.stats.mapped_max);
2475
2476 kgsl_process_add_stats(private,
2477 kgsl_memdesc_usermem_type(&entry->memdesc),
2478 entry->memdesc.size);
2479
2480 trace_kgsl_mem_map(entry, fd);
2481
2482 kgsl_mem_entry_commit_process(entry);
Tarun Karra24d3fe12017-04-05 15:23:03 -07002483
2484 /* Put the extra ref from kgsl_mem_entry_create() */
2485 kgsl_mem_entry_put(entry);
2486
Shrenuj Bansala419c792016-10-20 14:05:11 -07002487 return 0;
2488
2489unmap:
2490 if (param->type == KGSL_USER_MEM_TYPE_DMABUF) {
2491 kgsl_destroy_ion(entry->priv_data);
2492 entry->memdesc.sgt = NULL;
2493 }
2494
2495 kgsl_sharedmem_free(&entry->memdesc);
2496
2497out:
2498 kfree(entry);
2499 return ret;
2500}
2501
2502static long _map_usermem_addr(struct kgsl_device *device,
2503 struct kgsl_pagetable *pagetable, struct kgsl_mem_entry *entry,
2504 unsigned long hostptr, size_t offset, size_t size)
2505{
2506 if (!MMU_FEATURE(&device->mmu, KGSL_MMU_PAGED))
2507 return -EINVAL;
2508
2509 /* No CPU mapped buffer could ever be secure */
2510 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE)
2511 return -EINVAL;
2512
2513 return kgsl_setup_useraddr(device, pagetable, entry, hostptr,
2514 offset, size);
2515}
2516
2517#ifdef CONFIG_DMA_SHARED_BUFFER
2518static int _map_usermem_dma_buf(struct kgsl_device *device,
2519 struct kgsl_pagetable *pagetable,
2520 struct kgsl_mem_entry *entry,
2521 unsigned int fd)
2522{
2523 int ret;
2524 struct dma_buf *dmabuf;
2525
2526 /*
2527 * If content protection is not enabled and secure buffer
2528 * is requested to be mapped return error.
2529 */
2530
2531 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2532 if (!kgsl_mmu_is_secured(&device->mmu)) {
2533 dev_WARN_ONCE(device->dev, 1,
2534 "Secure buffer not supported");
2535 return -EINVAL;
2536 }
2537
2538 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2539 }
2540
2541 dmabuf = dma_buf_get(fd);
2542 if (IS_ERR_OR_NULL(dmabuf)) {
2543 ret = PTR_ERR(dmabuf);
2544 return ret ? ret : -EINVAL;
2545 }
2546 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2547 if (ret)
2548 dma_buf_put(dmabuf);
2549 return ret;
2550}
2551#else
2552static int _map_usermem_dma_buf(struct kgsl_device *device,
2553 struct kgsl_pagetable *pagetable,
2554 struct kgsl_mem_entry *entry,
2555 unsigned int fd)
2556{
2557 return -EINVAL;
2558}
2559#endif
2560
2561#ifdef CONFIG_DMA_SHARED_BUFFER
2562static int kgsl_setup_dma_buf(struct kgsl_device *device,
2563 struct kgsl_pagetable *pagetable,
2564 struct kgsl_mem_entry *entry,
2565 struct dma_buf *dmabuf)
2566{
2567 int ret = 0;
2568 struct scatterlist *s;
2569 struct sg_table *sg_table;
2570 struct dma_buf_attachment *attach = NULL;
2571 struct kgsl_dma_buf_meta *meta;
2572
2573 meta = kzalloc(sizeof(*meta), GFP_KERNEL);
2574 if (!meta)
2575 return -ENOMEM;
2576
2577 attach = dma_buf_attach(dmabuf, device->dev);
2578 if (IS_ERR_OR_NULL(attach)) {
2579 ret = attach ? PTR_ERR(attach) : -EINVAL;
2580 goto out;
2581 }
2582
2583 meta->dmabuf = dmabuf;
2584 meta->attach = attach;
2585
2586 attach->priv = entry;
2587
2588 entry->priv_data = meta;
2589 entry->memdesc.pagetable = pagetable;
2590 entry->memdesc.size = 0;
2591 /* USE_CPU_MAP is not impemented for ION. */
2592 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
Lynus Vazeb7af682017-04-17 18:36:01 +05302593 entry->memdesc.flags |= (uint64_t)KGSL_MEMFLAGS_USERMEM_ION;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002594
2595 sg_table = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
2596
2597 if (IS_ERR_OR_NULL(sg_table)) {
2598 ret = PTR_ERR(sg_table);
2599 goto out;
2600 }
2601
2602 meta->table = sg_table;
2603 entry->priv_data = meta;
2604 entry->memdesc.sgt = sg_table;
2605
2606 /* Calculate the size of the memdesc from the sglist */
2607 for (s = entry->memdesc.sgt->sgl; s != NULL; s = sg_next(s)) {
2608 int priv = (entry->memdesc.priv & KGSL_MEMDESC_SECURE) ? 1 : 0;
2609
2610 /*
2611 * Check that each chunk of of the sg table matches the secure
2612 * flag.
2613 */
2614
2615 if (PagePrivate(sg_page(s)) != priv) {
2616 ret = -EPERM;
2617 goto out;
2618 }
2619
2620 entry->memdesc.size += (uint64_t) s->length;
2621 }
2622
2623 entry->memdesc.size = PAGE_ALIGN(entry->memdesc.size);
2624
2625out:
2626 if (ret) {
2627 if (!IS_ERR_OR_NULL(attach))
2628 dma_buf_detach(dmabuf, attach);
2629
2630
2631 kfree(meta);
2632 }
2633
2634 return ret;
2635}
2636#endif
2637
2638#ifdef CONFIG_DMA_SHARED_BUFFER
2639void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2640 int *egl_surface_count, int *egl_image_count)
2641{
2642 struct kgsl_dma_buf_meta *meta = entry->priv_data;
2643 struct dma_buf *dmabuf = meta->dmabuf;
2644 struct dma_buf_attachment *mem_entry_buf_attachment = meta->attach;
2645 struct device *buf_attachment_dev = mem_entry_buf_attachment->dev;
2646 struct dma_buf_attachment *attachment = NULL;
2647
2648 mutex_lock(&dmabuf->lock);
2649 list_for_each_entry(attachment, &dmabuf->attachments, node) {
2650 struct kgsl_mem_entry *scan_mem_entry = NULL;
2651
2652 if (attachment->dev != buf_attachment_dev)
2653 continue;
2654
2655 scan_mem_entry = attachment->priv;
2656 if (!scan_mem_entry)
2657 continue;
2658
2659 switch (kgsl_memdesc_get_memtype(&scan_mem_entry->memdesc)) {
2660 case KGSL_MEMTYPE_EGL_SURFACE:
2661 (*egl_surface_count)++;
2662 break;
2663 case KGSL_MEMTYPE_EGL_IMAGE:
2664 (*egl_image_count)++;
2665 break;
2666 }
2667 }
2668 mutex_unlock(&dmabuf->lock);
2669}
2670#else
2671void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2672 int *egl_surface_count, int *egl_image_count)
2673{
2674}
2675#endif
2676
2677long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
2678 unsigned int cmd, void *data)
2679{
2680 int result = -EINVAL;
2681 struct kgsl_map_user_mem *param = data;
2682 struct kgsl_mem_entry *entry = NULL;
2683 struct kgsl_process_private *private = dev_priv->process_priv;
2684 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
2685 unsigned int memtype;
Lynus Vaz90d98b52018-04-09 14:45:36 +05302686 uint64_t flags;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002687
2688 /*
2689 * If content protection is not enabled and secure buffer
2690 * is requested to be mapped return error.
2691 */
2692
2693 if (param->flags & KGSL_MEMFLAGS_SECURE) {
2694 /* Log message and return if context protection isn't enabled */
2695 if (!kgsl_mmu_is_secured(mmu)) {
2696 dev_WARN_ONCE(dev_priv->device->dev, 1,
2697 "Secure buffer not supported");
2698 return -EOPNOTSUPP;
2699 }
2700
2701 /* Can't use CPU map with secure buffers */
2702 if (param->flags & KGSL_MEMFLAGS_USE_CPU_MAP)
2703 return -EINVAL;
2704 }
2705
2706 entry = kgsl_mem_entry_create();
2707
2708 if (entry == NULL)
2709 return -ENOMEM;
2710
2711 /*
2712 * Convert from enum value to KGSL_MEM_ENTRY value, so that
2713 * we can use the latter consistently everywhere.
2714 */
2715 memtype = param->memtype + 1;
2716
2717 /*
2718 * Mask off unknown flags from userspace. This way the caller can
2719 * check if a flag is supported by looking at the returned flags.
2720 * Note: CACHEMODE is ignored for this call. Caching should be
2721 * determined by type of allocation being mapped.
2722 */
Lynus Vaz90d98b52018-04-09 14:45:36 +05302723 flags = param->flags & (KGSL_MEMFLAGS_GPUREADONLY
2724 | KGSL_MEMTYPE_MASK
2725 | KGSL_MEMALIGN_MASK
2726 | KGSL_MEMFLAGS_USE_CPU_MAP
2727 | KGSL_MEMFLAGS_SECURE
2728 | KGSL_MEMFLAGS_IOCOHERENT);
Deepak Kumarcf056d12018-04-17 15:59:42 +05302729
2730 if (kgsl_is_compat_task())
Lynus Vaz90d98b52018-04-09 14:45:36 +05302731 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002732
Lynus Vaz90d98b52018-04-09 14:45:36 +05302733 kgsl_memdesc_init(dev_priv->device, &entry->memdesc, flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002734
2735 switch (memtype) {
2736 case KGSL_MEM_ENTRY_USER:
2737 result = _map_usermem_addr(dev_priv->device, private->pagetable,
2738 entry, param->hostptr, param->offset, param->len);
2739 break;
2740 case KGSL_MEM_ENTRY_ION:
2741 if (param->offset != 0)
2742 result = -EINVAL;
2743 else
2744 result = _map_usermem_dma_buf(dev_priv->device,
2745 private->pagetable, entry, param->fd);
2746 break;
2747 default:
2748 result = -EOPNOTSUPP;
2749 break;
2750 }
2751
2752 if (result)
2753 goto error;
2754
2755 if ((param->flags & KGSL_MEMFLAGS_SECURE) &&
2756 (entry->memdesc.size & mmu->secure_align_mask)) {
2757 result = -EINVAL;
2758 goto error_attach;
2759 }
2760
2761 if (entry->memdesc.size >= SZ_2M)
2762 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_2M));
2763 else if (entry->memdesc.size >= SZ_1M)
2764 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2765 else if (entry->memdesc.size >= SZ_64K)
2766 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64));
2767
2768 /* echo back flags */
2769 param->flags = (unsigned int) entry->memdesc.flags;
2770
2771 result = kgsl_mem_entry_attach_process(dev_priv->device, private,
2772 entry);
2773 if (result)
2774 goto error_attach;
2775
2776 /* Adjust the returned value for a non 4k aligned offset */
2777 param->gpuaddr = (unsigned long)
2778 entry->memdesc.gpuaddr + (param->offset & PAGE_MASK);
2779
2780 KGSL_STATS_ADD(param->len, &kgsl_driver.stats.mapped,
2781 &kgsl_driver.stats.mapped_max);
2782
2783 kgsl_process_add_stats(private,
2784 kgsl_memdesc_usermem_type(&entry->memdesc), param->len);
2785
2786 trace_kgsl_mem_map(entry, param->fd);
2787
2788 kgsl_mem_entry_commit_process(entry);
Tarun Karra24d3fe12017-04-05 15:23:03 -07002789
2790 /* Put the extra ref from kgsl_mem_entry_create() */
2791 kgsl_mem_entry_put(entry);
2792
Shrenuj Bansala419c792016-10-20 14:05:11 -07002793 return result;
2794
2795error_attach:
2796 switch (memtype) {
2797 case KGSL_MEM_ENTRY_ION:
2798 kgsl_destroy_ion(entry->priv_data);
2799 entry->memdesc.sgt = NULL;
2800 break;
2801 default:
2802 break;
2803 }
2804 kgsl_sharedmem_free(&entry->memdesc);
2805error:
2806 /* Clear gpuaddr here so userspace doesn't get any wrong ideas */
2807 param->gpuaddr = 0;
2808
2809 kfree(entry);
2810 return result;
2811}
2812
2813static int _kgsl_gpumem_sync_cache(struct kgsl_mem_entry *entry,
2814 uint64_t offset, uint64_t length, unsigned int op)
2815{
2816 int ret = 0;
2817 int cacheop;
2818 int mode;
2819
Akhil P Oommen4323d4ca2017-06-21 12:54:18 +05302820 /* Cache ops are not allowed on secure memory */
2821 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE)
2822 return 0;
2823
Shrenuj Bansala419c792016-10-20 14:05:11 -07002824 /*
2825 * Flush is defined as (clean | invalidate). If both bits are set, then
2826 * do a flush, otherwise check for the individual bits and clean or inv
2827 * as requested
2828 */
2829
2830 if ((op & KGSL_GPUMEM_CACHE_FLUSH) == KGSL_GPUMEM_CACHE_FLUSH)
2831 cacheop = KGSL_CACHE_OP_FLUSH;
2832 else if (op & KGSL_GPUMEM_CACHE_CLEAN)
2833 cacheop = KGSL_CACHE_OP_CLEAN;
2834 else if (op & KGSL_GPUMEM_CACHE_INV)
2835 cacheop = KGSL_CACHE_OP_INV;
2836 else {
2837 ret = -EINVAL;
2838 goto done;
2839 }
2840
2841 if (!(op & KGSL_GPUMEM_CACHE_RANGE)) {
2842 offset = 0;
2843 length = entry->memdesc.size;
2844 }
2845
2846 mode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2847 if (mode != KGSL_CACHEMODE_UNCACHED
2848 && mode != KGSL_CACHEMODE_WRITECOMBINE) {
2849 trace_kgsl_mem_sync_cache(entry, offset, length, op);
2850 ret = kgsl_cache_range_op(&entry->memdesc, offset,
2851 length, cacheop);
2852 }
2853
2854done:
2855 return ret;
2856}
2857
2858/* New cache sync function - supports both directions (clean and invalidate) */
2859
2860long kgsl_ioctl_gpumem_sync_cache(struct kgsl_device_private *dev_priv,
2861 unsigned int cmd, void *data)
2862{
2863 struct kgsl_gpumem_sync_cache *param = data;
2864 struct kgsl_process_private *private = dev_priv->process_priv;
2865 struct kgsl_mem_entry *entry = NULL;
2866 long ret;
2867
2868 if (param->id != 0)
2869 entry = kgsl_sharedmem_find_id(private, param->id);
2870 else if (param->gpuaddr != 0)
2871 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
2872
2873 if (entry == NULL)
2874 return -EINVAL;
2875
2876 ret = _kgsl_gpumem_sync_cache(entry, (uint64_t) param->offset,
2877 (uint64_t) param->length, param->op);
2878 kgsl_mem_entry_put(entry);
2879 return ret;
2880}
2881
2882static int mem_id_cmp(const void *_a, const void *_b)
2883{
2884 const unsigned int *a = _a, *b = _b;
2885
2886 if (*a == *b)
2887 return 0;
2888 return (*a > *b) ? 1 : -1;
2889}
2890
2891#ifdef CONFIG_ARM64
2892/* Do not support full flush on ARM64 targets */
2893static inline bool check_full_flush(size_t size, int op)
2894{
2895 return false;
2896}
2897#else
2898/* Support full flush if the size is bigger than the threshold */
2899static inline bool check_full_flush(size_t size, int op)
2900{
2901 /* If we exceed the breakeven point, flush the entire cache */
2902 bool ret = (kgsl_driver.full_cache_threshold != 0) &&
2903 (size >= kgsl_driver.full_cache_threshold) &&
2904 (op == KGSL_GPUMEM_CACHE_FLUSH);
Maria Yuceafc602017-09-26 15:45:02 +08002905 if (ret)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002906 flush_cache_all();
Shrenuj Bansala419c792016-10-20 14:05:11 -07002907 return ret;
2908}
2909#endif
2910
2911long kgsl_ioctl_gpumem_sync_cache_bulk(struct kgsl_device_private *dev_priv,
2912 unsigned int cmd, void *data)
2913{
2914 int i;
2915 struct kgsl_gpumem_sync_cache_bulk *param = data;
2916 struct kgsl_process_private *private = dev_priv->process_priv;
2917 unsigned int id, last_id = 0, *id_list = NULL, actual_count = 0;
2918 struct kgsl_mem_entry **entries = NULL;
2919 long ret = 0;
2920 uint64_t op_size = 0;
2921 bool full_flush = false;
2922
2923 if (param->id_list == NULL || param->count == 0
2924 || param->count > (PAGE_SIZE / sizeof(unsigned int)))
2925 return -EINVAL;
2926
2927 id_list = kcalloc(param->count, sizeof(unsigned int), GFP_KERNEL);
2928 if (id_list == NULL)
2929 return -ENOMEM;
2930
2931 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
2932 if (entries == NULL) {
2933 ret = -ENOMEM;
2934 goto end;
2935 }
2936
2937 if (copy_from_user(id_list, param->id_list,
2938 param->count * sizeof(unsigned int))) {
2939 ret = -EFAULT;
2940 goto end;
2941 }
2942 /* sort the ids so we can weed out duplicates */
2943 sort(id_list, param->count, sizeof(*id_list), mem_id_cmp, NULL);
2944
2945 for (i = 0; i < param->count; i++) {
2946 unsigned int cachemode;
2947 struct kgsl_mem_entry *entry = NULL;
2948
2949 id = id_list[i];
2950 /* skip 0 ids or duplicates */
2951 if (id == last_id)
2952 continue;
2953
2954 entry = kgsl_sharedmem_find_id(private, id);
2955 if (entry == NULL)
2956 continue;
2957
2958 /* skip uncached memory */
2959 cachemode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2960 if (cachemode != KGSL_CACHEMODE_WRITETHROUGH &&
2961 cachemode != KGSL_CACHEMODE_WRITEBACK) {
2962 kgsl_mem_entry_put(entry);
2963 continue;
2964 }
2965
2966 op_size += entry->memdesc.size;
2967 entries[actual_count++] = entry;
2968
2969 full_flush = check_full_flush(op_size, param->op);
Maria Yuceafc602017-09-26 15:45:02 +08002970 if (full_flush) {
2971 trace_kgsl_mem_sync_full_cache(actual_count, op_size);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002972 break;
Maria Yuceafc602017-09-26 15:45:02 +08002973 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002974
2975 last_id = id;
2976 }
2977
2978 param->op &= ~KGSL_GPUMEM_CACHE_RANGE;
2979
2980 for (i = 0; i < actual_count; i++) {
2981 if (!full_flush)
2982 _kgsl_gpumem_sync_cache(entries[i], 0,
2983 entries[i]->memdesc.size,
2984 param->op);
2985 kgsl_mem_entry_put(entries[i]);
2986 }
2987end:
2988 kfree(entries);
2989 kfree(id_list);
2990 return ret;
2991}
2992
2993/* Legacy cache function, does a flush (clean + invalidate) */
2994
2995long kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
2996 unsigned int cmd, void *data)
2997{
2998 struct kgsl_sharedmem_free *param = data;
2999 struct kgsl_process_private *private = dev_priv->process_priv;
3000 struct kgsl_mem_entry *entry = NULL;
3001 long ret;
3002
3003 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
3004 if (entry == NULL)
3005 return -EINVAL;
3006
3007 ret = _kgsl_gpumem_sync_cache(entry, 0, entry->memdesc.size,
3008 KGSL_GPUMEM_CACHE_FLUSH);
3009 kgsl_mem_entry_put(entry);
3010 return ret;
3011}
3012
3013long kgsl_ioctl_gpuobj_sync(struct kgsl_device_private *dev_priv,
3014 unsigned int cmd, void *data)
3015{
3016 struct kgsl_process_private *private = dev_priv->process_priv;
3017 struct kgsl_gpuobj_sync *param = data;
3018 struct kgsl_gpuobj_sync_obj *objs;
3019 struct kgsl_mem_entry **entries;
3020 long ret = 0;
3021 bool full_flush = false;
3022 uint64_t size = 0;
Carter Cooper69355b82018-01-17 09:49:00 -07003023 int i;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003024 void __user *ptr;
3025
3026 if (param->count == 0 || param->count > 128)
3027 return -EINVAL;
3028
3029 objs = kcalloc(param->count, sizeof(*objs), GFP_KERNEL);
3030 if (objs == NULL)
3031 return -ENOMEM;
3032
3033 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
3034 if (entries == NULL) {
Carter Cooper69355b82018-01-17 09:49:00 -07003035 kfree(objs);
3036 return -ENOMEM;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003037 }
3038
3039 ptr = to_user_ptr(param->objs);
3040
3041 for (i = 0; i < param->count; i++) {
3042 ret = _copy_from_user(&objs[i], ptr, sizeof(*objs),
3043 param->obj_len);
3044 if (ret)
3045 goto out;
3046
3047 entries[i] = kgsl_sharedmem_find_id(private, objs[i].id);
3048
3049 /* Not finding the ID is not a fatal failure - just skip it */
3050 if (entries[i] == NULL)
3051 continue;
3052
Shrenuj Bansala419c792016-10-20 14:05:11 -07003053 if (!(objs[i].op & KGSL_GPUMEM_CACHE_RANGE))
3054 size += entries[i]->memdesc.size;
3055 else if (objs[i].offset < entries[i]->memdesc.size)
3056 size += (entries[i]->memdesc.size - objs[i].offset);
3057
3058 full_flush = check_full_flush(size, objs[i].op);
Maria Yuceafc602017-09-26 15:45:02 +08003059 if (full_flush) {
3060 trace_kgsl_mem_sync_full_cache(i, size);
Carter Cooper69355b82018-01-17 09:49:00 -07003061 goto out;
Maria Yuceafc602017-09-26 15:45:02 +08003062 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003063
3064 ptr += sizeof(*objs);
3065 }
3066
Carter Cooper69355b82018-01-17 09:49:00 -07003067 for (i = 0; !ret && i < param->count; i++)
3068 if (entries[i])
3069 ret = _kgsl_gpumem_sync_cache(entries[i],
3070 objs[i].offset, objs[i].length,
3071 objs[i].op);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003072
Carter Cooper69355b82018-01-17 09:49:00 -07003073out:
Shrenuj Bansala419c792016-10-20 14:05:11 -07003074 for (i = 0; i < param->count; i++)
3075 if (entries[i])
3076 kgsl_mem_entry_put(entries[i]);
3077
Shrenuj Bansala419c792016-10-20 14:05:11 -07003078 kfree(entries);
3079 kfree(objs);
3080
3081 return ret;
3082}
3083
3084#ifdef CONFIG_ARM64
3085static uint64_t kgsl_filter_cachemode(uint64_t flags)
3086{
3087 /*
3088 * WRITETHROUGH is not supported in arm64, so we tell the user that we
3089 * use WRITEBACK which is the default caching policy.
3090 */
3091 if ((flags & KGSL_CACHEMODE_MASK) >> KGSL_CACHEMODE_SHIFT ==
3092 KGSL_CACHEMODE_WRITETHROUGH) {
3093 flags &= ~((uint64_t) KGSL_CACHEMODE_MASK);
Lynus Vazeb7af682017-04-17 18:36:01 +05303094 flags |= (uint64_t)((KGSL_CACHEMODE_WRITEBACK <<
3095 KGSL_CACHEMODE_SHIFT) &
3096 KGSL_CACHEMODE_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003097 }
3098 return flags;
3099}
3100#else
3101static uint64_t kgsl_filter_cachemode(uint64_t flags)
3102{
3103 return flags;
3104}
3105#endif
3106
3107/* The largest allowable alignment for a GPU object is 32MB */
3108#define KGSL_MAX_ALIGN (32 * SZ_1M)
3109
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -06003110struct kgsl_mem_entry *gpumem_alloc_entry(
Shrenuj Bansala419c792016-10-20 14:05:11 -07003111 struct kgsl_device_private *dev_priv,
3112 uint64_t size, uint64_t flags)
3113{
3114 int ret;
3115 struct kgsl_process_private *private = dev_priv->process_priv;
3116 struct kgsl_mem_entry *entry;
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003117 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003118 unsigned int align;
3119
3120 flags &= KGSL_MEMFLAGS_GPUREADONLY
3121 | KGSL_CACHEMODE_MASK
3122 | KGSL_MEMTYPE_MASK
3123 | KGSL_MEMALIGN_MASK
3124 | KGSL_MEMFLAGS_USE_CPU_MAP
3125 | KGSL_MEMFLAGS_SECURE
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003126 | KGSL_MEMFLAGS_FORCE_32BIT
3127 | KGSL_MEMFLAGS_IOCOHERENT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003128
Shrenuj Bansala419c792016-10-20 14:05:11 -07003129 /* Return not supported error if secure memory isn't enabled */
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003130 if (!kgsl_mmu_is_secured(mmu) &&
Shrenuj Bansala419c792016-10-20 14:05:11 -07003131 (flags & KGSL_MEMFLAGS_SECURE)) {
3132 dev_WARN_ONCE(dev_priv->device->dev, 1,
3133 "Secure memory not supported");
3134 return ERR_PTR(-EOPNOTSUPP);
3135 }
3136
Shrenuj Bansala419c792016-10-20 14:05:11 -07003137 /* Cap the alignment bits to the highest number we can handle */
3138 align = MEMFLAGS(flags, KGSL_MEMALIGN_MASK, KGSL_MEMALIGN_SHIFT);
3139 if (align >= ilog2(KGSL_MAX_ALIGN)) {
3140 KGSL_CORE_ERR("Alignment too large; restricting to %dK\n",
3141 KGSL_MAX_ALIGN >> 10);
3142
3143 flags &= ~((uint64_t) KGSL_MEMALIGN_MASK);
Lynus Vazeb7af682017-04-17 18:36:01 +05303144 flags |= (uint64_t)((ilog2(KGSL_MAX_ALIGN) <<
3145 KGSL_MEMALIGN_SHIFT) &
3146 KGSL_MEMALIGN_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003147 }
3148
3149 /* For now only allow allocations up to 4G */
3150 if (size == 0 || size > UINT_MAX)
3151 return ERR_PTR(-EINVAL);
3152
3153 flags = kgsl_filter_cachemode(flags);
3154
3155 entry = kgsl_mem_entry_create();
3156 if (entry == NULL)
3157 return ERR_PTR(-ENOMEM);
3158
Shrenuj Bansala419c792016-10-20 14:05:11 -07003159 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
3160 size, flags);
3161 if (ret != 0)
3162 goto err;
3163
3164 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3165 if (ret != 0) {
3166 kgsl_sharedmem_free(&entry->memdesc);
3167 goto err;
3168 }
3169
3170 kgsl_process_add_stats(private,
3171 kgsl_memdesc_usermem_type(&entry->memdesc),
3172 entry->memdesc.size);
3173 trace_kgsl_mem_alloc(entry);
3174
3175 kgsl_mem_entry_commit_process(entry);
3176 return entry;
3177err:
3178 kfree(entry);
3179 return ERR_PTR(ret);
3180}
3181
3182static void copy_metadata(struct kgsl_mem_entry *entry, uint64_t metadata,
3183 unsigned int len)
3184{
3185 unsigned int i, size;
3186
3187 if (len == 0)
3188 return;
3189
3190 size = min_t(unsigned int, len, sizeof(entry->metadata) - 1);
3191
3192 if (copy_from_user(entry->metadata, to_user_ptr(metadata), size)) {
3193 memset(entry->metadata, 0, sizeof(entry->metadata));
3194 return;
3195 }
3196
3197 /* Clean up non printable characters in the string */
3198 for (i = 0; i < size && entry->metadata[i] != 0; i++) {
3199 if (!isprint(entry->metadata[i]))
3200 entry->metadata[i] = '?';
3201 }
3202}
3203
3204long kgsl_ioctl_gpuobj_alloc(struct kgsl_device_private *dev_priv,
3205 unsigned int cmd, void *data)
3206{
3207 struct kgsl_gpuobj_alloc *param = data;
3208 struct kgsl_mem_entry *entry;
3209
Deepak Kumarcf056d12018-04-17 15:59:42 +05303210 if (kgsl_is_compat_task())
3211 param->flags |= KGSL_MEMFLAGS_FORCE_32BIT;
3212
Shrenuj Bansala419c792016-10-20 14:05:11 -07003213 entry = gpumem_alloc_entry(dev_priv, param->size, param->flags);
3214
3215 if (IS_ERR(entry))
3216 return PTR_ERR(entry);
3217
3218 copy_metadata(entry, param->metadata, param->metadata_len);
3219
3220 param->size = entry->memdesc.size;
3221 param->flags = entry->memdesc.flags;
3222 param->mmapsize = kgsl_memdesc_footprint(&entry->memdesc);
3223 param->id = entry->id;
3224
Tarun Karra24d3fe12017-04-05 15:23:03 -07003225 /* Put the extra ref from kgsl_mem_entry_create() */
3226 kgsl_mem_entry_put(entry);
3227
Shrenuj Bansala419c792016-10-20 14:05:11 -07003228 return 0;
3229}
3230
3231long kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
3232 unsigned int cmd, void *data)
3233{
3234 struct kgsl_gpumem_alloc *param = data;
3235 struct kgsl_mem_entry *entry;
3236 uint64_t flags = param->flags;
3237
3238 /* Legacy functions doesn't support these advanced features */
3239 flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
Deepak Kumarcf056d12018-04-17 15:59:42 +05303240
3241 if (kgsl_is_compat_task())
3242 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003243
3244 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3245
3246 if (IS_ERR(entry))
3247 return PTR_ERR(entry);
3248
3249 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3250 param->size = (size_t) entry->memdesc.size;
3251 param->flags = (unsigned int) entry->memdesc.flags;
3252
Tarun Karra24d3fe12017-04-05 15:23:03 -07003253 /* Put the extra ref from kgsl_mem_entry_create() */
3254 kgsl_mem_entry_put(entry);
3255
Shrenuj Bansala419c792016-10-20 14:05:11 -07003256 return 0;
3257}
3258
3259long kgsl_ioctl_gpumem_alloc_id(struct kgsl_device_private *dev_priv,
3260 unsigned int cmd, void *data)
3261{
3262 struct kgsl_gpumem_alloc_id *param = data;
3263 struct kgsl_mem_entry *entry;
3264 uint64_t flags = param->flags;
3265
Deepak Kumarcf056d12018-04-17 15:59:42 +05303266 if (kgsl_is_compat_task())
3267 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003268
3269 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3270
3271 if (IS_ERR(entry))
3272 return PTR_ERR(entry);
3273
3274 param->id = entry->id;
3275 param->flags = (unsigned int) entry->memdesc.flags;
3276 param->size = (size_t) entry->memdesc.size;
3277 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
3278 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3279
Tarun Karra24d3fe12017-04-05 15:23:03 -07003280 /* Put the extra ref from kgsl_mem_entry_create() */
3281 kgsl_mem_entry_put(entry);
3282
Shrenuj Bansala419c792016-10-20 14:05:11 -07003283 return 0;
3284}
3285
3286long kgsl_ioctl_gpumem_get_info(struct kgsl_device_private *dev_priv,
3287 unsigned int cmd, void *data)
3288{
3289 struct kgsl_process_private *private = dev_priv->process_priv;
3290 struct kgsl_gpumem_get_info *param = data;
3291 struct kgsl_mem_entry *entry = NULL;
3292 int result = 0;
3293
3294 if (param->id != 0)
3295 entry = kgsl_sharedmem_find_id(private, param->id);
3296 else if (param->gpuaddr != 0)
3297 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
3298
3299 if (entry == NULL)
3300 return -EINVAL;
3301
3302 /*
3303 * If any of the 64 bit address / sizes would end up being
3304 * truncated, return -ERANGE. That will signal the user that they
3305 * should use a more modern API
3306 */
3307 if (entry->memdesc.gpuaddr > ULONG_MAX)
3308 result = -ERANGE;
3309
3310 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3311 param->id = entry->id;
3312 param->flags = (unsigned int) entry->memdesc.flags;
3313 param->size = (size_t) entry->memdesc.size;
3314 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
3315 param->useraddr = entry->memdesc.useraddr;
3316
3317 kgsl_mem_entry_put(entry);
3318 return result;
3319}
3320
3321static inline int _sparse_alloc_param_sanity_check(uint64_t size,
3322 uint64_t pagesize)
3323{
3324 if (size == 0 || pagesize == 0)
3325 return -EINVAL;
3326
3327 if (pagesize != PAGE_SIZE && pagesize != SZ_64K)
3328 return -EINVAL;
3329
3330 if (pagesize > size || !IS_ALIGNED(size, pagesize))
3331 return -EINVAL;
3332
3333 return 0;
3334}
3335
3336long kgsl_ioctl_sparse_phys_alloc(struct kgsl_device_private *dev_priv,
3337 unsigned int cmd, void *data)
3338{
3339 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003340 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003341 struct kgsl_sparse_phys_alloc *param = data;
3342 struct kgsl_mem_entry *entry;
Lynus Vaz90d98b52018-04-09 14:45:36 +05303343 uint64_t flags;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003344 int ret;
3345 int id;
3346
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003347 if (!(device->flags & KGSL_FLAG_SPARSE))
3348 return -ENOTSUPP;
3349
Shrenuj Bansala419c792016-10-20 14:05:11 -07003350 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3351 if (ret)
3352 return ret;
3353
3354 entry = kgsl_mem_entry_create();
3355 if (entry == NULL)
3356 return -ENOMEM;
3357
3358 ret = kgsl_process_private_get(process);
3359 if (!ret) {
3360 ret = -EBADF;
3361 goto err_free_entry;
3362 }
3363
3364 idr_preload(GFP_KERNEL);
3365 spin_lock(&process->mem_lock);
3366 /* Allocate the ID but don't attach the pointer just yet */
3367 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
3368 spin_unlock(&process->mem_lock);
3369 idr_preload_end();
3370
3371 if (id < 0) {
3372 ret = id;
3373 goto err_put_proc_priv;
3374 }
3375
3376 entry->id = id;
3377 entry->priv = process;
3378
Lynus Vaz90d98b52018-04-09 14:45:36 +05303379 flags = KGSL_MEMFLAGS_SPARSE_PHYS |
3380 ((ilog2(param->pagesize) << KGSL_MEMALIGN_SHIFT) &
3381 KGSL_MEMALIGN_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003382
3383 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
Lynus Vaz90d98b52018-04-09 14:45:36 +05303384 param->size, flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003385 if (ret)
3386 goto err_remove_idr;
3387
3388 /* Sanity check to verify we got correct pagesize */
3389 if (param->pagesize != PAGE_SIZE && entry->memdesc.sgt != NULL) {
3390 struct scatterlist *s;
3391 int i;
3392
3393 for_each_sg(entry->memdesc.sgt->sgl, s,
3394 entry->memdesc.sgt->nents, i) {
3395 if (!IS_ALIGNED(s->length, param->pagesize))
3396 goto err_invalid_pages;
3397 }
3398 }
3399
3400 param->id = entry->id;
3401 param->flags = entry->memdesc.flags;
3402
3403 trace_sparse_phys_alloc(entry->id, param->size, param->pagesize);
3404 kgsl_mem_entry_commit_process(entry);
3405
Tarun Karra24d3fe12017-04-05 15:23:03 -07003406 /* Put the extra ref from kgsl_mem_entry_create() */
3407 kgsl_mem_entry_put(entry);
3408
Shrenuj Bansala419c792016-10-20 14:05:11 -07003409 return 0;
3410
3411err_invalid_pages:
3412 kgsl_sharedmem_free(&entry->memdesc);
3413err_remove_idr:
3414 spin_lock(&process->mem_lock);
3415 idr_remove(&process->mem_idr, entry->id);
3416 spin_unlock(&process->mem_lock);
3417err_put_proc_priv:
3418 kgsl_process_private_put(process);
3419err_free_entry:
3420 kfree(entry);
3421
3422 return ret;
3423}
3424
3425long kgsl_ioctl_sparse_phys_free(struct kgsl_device_private *dev_priv,
3426 unsigned int cmd, void *data)
3427{
3428 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003429 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003430 struct kgsl_sparse_phys_free *param = data;
3431 struct kgsl_mem_entry *entry;
3432
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003433 if (!(device->flags & KGSL_FLAG_SPARSE))
3434 return -ENOTSUPP;
3435
Shrenuj Bansala419c792016-10-20 14:05:11 -07003436 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3437 KGSL_MEMFLAGS_SPARSE_PHYS);
3438 if (entry == NULL)
3439 return -EINVAL;
3440
Deepak Kumar32814682018-02-16 11:46:26 +05303441 if (!kgsl_mem_entry_set_pend(entry)) {
3442 kgsl_mem_entry_put(entry);
3443 return -EBUSY;
3444 }
3445
Shrenuj Bansala419c792016-10-20 14:05:11 -07003446 if (entry->memdesc.cur_bindings != 0) {
Deepak Kumar32814682018-02-16 11:46:26 +05303447 kgsl_mem_entry_unset_pend(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003448 kgsl_mem_entry_put(entry);
3449 return -EINVAL;
3450 }
3451
3452 trace_sparse_phys_free(entry->id);
3453
3454 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3455 kgsl_mem_entry_put(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05303456 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003457
3458 return 0;
3459}
3460
3461long kgsl_ioctl_sparse_virt_alloc(struct kgsl_device_private *dev_priv,
3462 unsigned int cmd, void *data)
3463{
3464 struct kgsl_process_private *private = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003465 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003466 struct kgsl_sparse_virt_alloc *param = data;
3467 struct kgsl_mem_entry *entry;
3468 int ret;
3469
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003470 if (!(device->flags & KGSL_FLAG_SPARSE))
3471 return -ENOTSUPP;
3472
Shrenuj Bansala419c792016-10-20 14:05:11 -07003473 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3474 if (ret)
3475 return ret;
3476
3477 entry = kgsl_mem_entry_create();
3478 if (entry == NULL)
3479 return -ENOMEM;
3480
Lynus Vaz90d98b52018-04-09 14:45:36 +05303481 kgsl_memdesc_init(dev_priv->device, &entry->memdesc,
3482 KGSL_MEMFLAGS_SPARSE_VIRT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003483 entry->memdesc.size = param->size;
3484 entry->memdesc.cur_bindings = 0;
3485 kgsl_memdesc_set_align(&entry->memdesc, ilog2(param->pagesize));
3486
3487 spin_lock_init(&entry->bind_lock);
3488 entry->bind_tree = RB_ROOT;
3489
3490 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3491 if (ret) {
3492 kfree(entry);
3493 return ret;
3494 }
3495
3496 param->id = entry->id;
3497 param->gpuaddr = entry->memdesc.gpuaddr;
3498 param->flags = entry->memdesc.flags;
3499
3500 trace_sparse_virt_alloc(entry->id, param->size, param->pagesize);
3501 kgsl_mem_entry_commit_process(entry);
3502
Tarun Karra24d3fe12017-04-05 15:23:03 -07003503 /* Put the extra ref from kgsl_mem_entry_create() */
3504 kgsl_mem_entry_put(entry);
3505
Shrenuj Bansala419c792016-10-20 14:05:11 -07003506 return 0;
3507}
3508
3509long kgsl_ioctl_sparse_virt_free(struct kgsl_device_private *dev_priv,
3510 unsigned int cmd, void *data)
3511{
3512 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003513 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003514 struct kgsl_sparse_virt_free *param = data;
3515 struct kgsl_mem_entry *entry = NULL;
3516
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003517 if (!(device->flags & KGSL_FLAG_SPARSE))
3518 return -ENOTSUPP;
3519
Shrenuj Bansala419c792016-10-20 14:05:11 -07003520 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3521 KGSL_MEMFLAGS_SPARSE_VIRT);
3522 if (entry == NULL)
3523 return -EINVAL;
3524
Deepak Kumar32814682018-02-16 11:46:26 +05303525 if (!kgsl_mem_entry_set_pend(entry)) {
3526 kgsl_mem_entry_put(entry);
3527 return -EBUSY;
3528 }
3529
Shrenuj Bansala419c792016-10-20 14:05:11 -07003530 if (entry->bind_tree.rb_node != NULL) {
Deepak Kumar32814682018-02-16 11:46:26 +05303531 kgsl_mem_entry_unset_pend(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003532 kgsl_mem_entry_put(entry);
3533 return -EINVAL;
3534 }
3535
3536 trace_sparse_virt_free(entry->id);
3537
3538 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3539 kgsl_mem_entry_put(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05303540 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003541
3542 return 0;
3543}
3544
Lynus Vaz4930cb12017-09-08 18:32:53 +05303545/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003546static int _sparse_add_to_bind_tree(struct kgsl_mem_entry *entry,
3547 uint64_t v_offset,
3548 struct kgsl_memdesc *memdesc,
3549 uint64_t p_offset,
3550 uint64_t size,
3551 uint64_t flags)
3552{
3553 struct sparse_bind_object *new;
3554 struct rb_node **node, *parent = NULL;
3555
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303556 new = kzalloc(sizeof(*new), GFP_ATOMIC);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003557 if (new == NULL)
3558 return -ENOMEM;
3559
3560 new->v_off = v_offset;
3561 new->p_off = p_offset;
3562 new->p_memdesc = memdesc;
3563 new->size = size;
3564 new->flags = flags;
3565
3566 node = &entry->bind_tree.rb_node;
3567
3568 while (*node != NULL) {
3569 struct sparse_bind_object *this;
3570
3571 parent = *node;
3572 this = rb_entry(parent, struct sparse_bind_object, node);
3573
Lynus Vaze8c82572017-09-08 17:27:56 +05303574 if ((new->v_off < this->v_off) &&
3575 ((new->v_off + new->size) <= this->v_off))
Shrenuj Bansala419c792016-10-20 14:05:11 -07003576 node = &parent->rb_left;
Lynus Vaze8c82572017-09-08 17:27:56 +05303577 else if ((new->v_off > this->v_off) &&
3578 (new->v_off >= (this->v_off + this->size)))
Shrenuj Bansala419c792016-10-20 14:05:11 -07003579 node = &parent->rb_right;
Lynus Vaze8c82572017-09-08 17:27:56 +05303580 else {
3581 kfree(new);
3582 return -EADDRINUSE;
3583 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003584 }
3585
3586 rb_link_node(&new->node, parent, node);
3587 rb_insert_color(&new->node, &entry->bind_tree);
3588
3589 return 0;
3590}
3591
3592static int _sparse_rm_from_bind_tree(struct kgsl_mem_entry *entry,
3593 struct sparse_bind_object *obj,
3594 uint64_t v_offset, uint64_t size)
3595{
Shrenuj Bansala419c792016-10-20 14:05:11 -07003596 if (v_offset == obj->v_off && size >= obj->size) {
3597 /*
3598 * We are all encompassing, remove the entry and free
3599 * things up
3600 */
3601 rb_erase(&obj->node, &entry->bind_tree);
3602 kfree(obj);
3603 } else if (v_offset == obj->v_off) {
3604 /*
3605 * We are the front of the node, adjust the front of
3606 * the node
3607 */
3608 obj->v_off += size;
3609 obj->p_off += size;
3610 obj->size -= size;
3611 } else if ((v_offset + size) == (obj->v_off + obj->size)) {
3612 /*
3613 * We are at the end of the obj, adjust the beginning
3614 * points
3615 */
3616 obj->size -= size;
3617 } else {
3618 /*
3619 * We are in the middle of a node, split it up and
3620 * create a new mini node. Adjust this node's bounds
3621 * and add the new node to the list.
3622 */
3623 uint64_t tmp_size = obj->size;
3624 int ret;
3625
3626 obj->size = v_offset - obj->v_off;
3627
Shrenuj Bansala419c792016-10-20 14:05:11 -07003628 ret = _sparse_add_to_bind_tree(entry, v_offset + size,
3629 obj->p_memdesc,
3630 obj->p_off + (v_offset - obj->v_off) + size,
3631 tmp_size - (v_offset - obj->v_off) - size,
3632 obj->flags);
3633
3634 return ret;
3635 }
3636
Shrenuj Bansala419c792016-10-20 14:05:11 -07003637 return 0;
3638}
3639
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303640/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003641static struct sparse_bind_object *_find_containing_bind_obj(
3642 struct kgsl_mem_entry *entry,
3643 uint64_t offset, uint64_t size)
3644{
3645 struct sparse_bind_object *obj = NULL;
3646 struct rb_node *node = entry->bind_tree.rb_node;
3647
Shrenuj Bansala419c792016-10-20 14:05:11 -07003648 while (node != NULL) {
3649 obj = rb_entry(node, struct sparse_bind_object, node);
3650
3651 if (offset == obj->v_off) {
3652 break;
3653 } else if (offset < obj->v_off) {
3654 if (offset + size > obj->v_off)
3655 break;
3656 node = node->rb_left;
3657 obj = NULL;
3658 } else if (offset > obj->v_off) {
3659 if (offset < obj->v_off + obj->size)
3660 break;
3661 node = node->rb_right;
3662 obj = NULL;
3663 }
3664 }
3665
Shrenuj Bansala419c792016-10-20 14:05:11 -07003666 return obj;
3667}
3668
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303669/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003670static int _sparse_unbind(struct kgsl_mem_entry *entry,
3671 struct sparse_bind_object *bind_obj,
3672 uint64_t offset, uint64_t size)
3673{
Shrenuj Bansala419c792016-10-20 14:05:11 -07003674 int ret;
3675
Shrenuj Bansala419c792016-10-20 14:05:11 -07003676 ret = _sparse_rm_from_bind_tree(entry, bind_obj, offset, size);
3677 if (ret == 0) {
3678 atomic_long_sub(size, &kgsl_driver.stats.mapped);
3679 trace_sparse_unbind(entry->id, offset, size);
3680 }
3681
3682 return ret;
3683}
3684
3685static long sparse_unbind_range(struct kgsl_sparse_binding_object *obj,
3686 struct kgsl_mem_entry *virt_entry)
3687{
3688 struct sparse_bind_object *bind_obj;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303689 struct kgsl_memdesc *memdesc;
3690 struct kgsl_pagetable *pt;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003691 int ret = 0;
3692 uint64_t size = obj->size;
3693 uint64_t tmp_size = obj->size;
3694 uint64_t offset = obj->virtoffset;
3695
3696 while (size > 0 && ret == 0) {
3697 tmp_size = size;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303698
3699 spin_lock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003700 bind_obj = _find_containing_bind_obj(virt_entry, offset, size);
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303701
3702 if (bind_obj == NULL) {
3703 spin_unlock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003704 return 0;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303705 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003706
3707 if (bind_obj->v_off > offset) {
3708 tmp_size = size - bind_obj->v_off - offset;
3709 if (tmp_size > bind_obj->size)
3710 tmp_size = bind_obj->size;
3711 offset = bind_obj->v_off;
3712 } else if (bind_obj->v_off < offset) {
3713 uint64_t diff = offset - bind_obj->v_off;
3714
3715 if (diff + size > bind_obj->size)
3716 tmp_size = bind_obj->size - diff;
3717 } else {
3718 if (tmp_size > bind_obj->size)
3719 tmp_size = bind_obj->size;
3720 }
3721
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303722 memdesc = bind_obj->p_memdesc;
3723 pt = memdesc->pagetable;
3724
3725 if (memdesc->cur_bindings < (tmp_size / PAGE_SIZE)) {
3726 spin_unlock(&virt_entry->bind_lock);
3727 return -EINVAL;
3728 }
3729
3730 memdesc->cur_bindings -= tmp_size / PAGE_SIZE;
3731
Shrenuj Bansala419c792016-10-20 14:05:11 -07003732 ret = _sparse_unbind(virt_entry, bind_obj, offset, tmp_size);
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303733 spin_unlock(&virt_entry->bind_lock);
3734
3735 ret = kgsl_mmu_unmap_offset(pt, memdesc,
3736 virt_entry->memdesc.gpuaddr, offset, tmp_size);
3737 if (ret)
3738 return ret;
3739
3740 ret = kgsl_mmu_sparse_dummy_map(pt, memdesc, offset, tmp_size);
3741 if (ret)
3742 return ret;
3743
Shrenuj Bansala419c792016-10-20 14:05:11 -07003744 if (ret == 0) {
3745 offset += tmp_size;
3746 size -= tmp_size;
3747 }
3748 }
3749
3750 return ret;
3751}
3752
3753static inline bool _is_phys_bindable(struct kgsl_mem_entry *phys_entry,
3754 uint64_t offset, uint64_t size, uint64_t flags)
3755{
3756 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3757
3758 if (!IS_ALIGNED(offset | size, kgsl_memdesc_get_pagesize(memdesc)))
3759 return false;
3760
Sudeep Yedalapure8ff97992017-01-20 20:12:51 +05303761 if (offset + size < offset)
3762 return false;
3763
Shrenuj Bansala419c792016-10-20 14:05:11 -07003764 if (!(flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
3765 offset + size > memdesc->size)
3766 return false;
3767
3768 return true;
3769}
3770
3771static int _sparse_bind(struct kgsl_process_private *process,
3772 struct kgsl_mem_entry *virt_entry, uint64_t v_offset,
3773 struct kgsl_mem_entry *phys_entry, uint64_t p_offset,
3774 uint64_t size, uint64_t flags)
3775{
3776 int ret;
3777 struct kgsl_pagetable *pagetable;
3778 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3779
3780 /* map the memory after unlocking if gpuaddr has been assigned */
3781 if (memdesc->gpuaddr)
3782 return -EINVAL;
3783
3784 if (memdesc->useraddr != 0)
3785 return -EINVAL;
3786
3787 pagetable = memdesc->pagetable;
3788
3789 /* Clear out any mappings */
3790 ret = kgsl_mmu_unmap_offset(pagetable, &virt_entry->memdesc,
3791 virt_entry->memdesc.gpuaddr, v_offset, size);
3792 if (ret)
3793 return ret;
3794
3795 ret = kgsl_mmu_map_offset(pagetable, virt_entry->memdesc.gpuaddr,
3796 v_offset, memdesc, p_offset, size, flags);
3797 if (ret) {
3798 /* Try to clean up, but not the end of the world */
3799 kgsl_mmu_sparse_dummy_map(pagetable, &virt_entry->memdesc,
3800 v_offset, size);
3801 return ret;
3802 }
3803
Lynus Vaz4930cb12017-09-08 18:32:53 +05303804 spin_lock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003805 ret = _sparse_add_to_bind_tree(virt_entry, v_offset, memdesc,
3806 p_offset, size, flags);
Lynus Vaz4930cb12017-09-08 18:32:53 +05303807 spin_unlock(&virt_entry->bind_lock);
3808
Shrenuj Bansala419c792016-10-20 14:05:11 -07003809 if (ret == 0)
3810 memdesc->cur_bindings += size / PAGE_SIZE;
3811
3812 return ret;
3813}
3814
3815static long sparse_bind_range(struct kgsl_process_private *private,
3816 struct kgsl_sparse_binding_object *obj,
3817 struct kgsl_mem_entry *virt_entry)
3818{
3819 struct kgsl_mem_entry *phys_entry;
3820 int ret;
3821
3822 phys_entry = kgsl_sharedmem_find_id_flags(private, obj->id,
3823 KGSL_MEMFLAGS_SPARSE_PHYS);
3824 if (phys_entry == NULL)
3825 return -EINVAL;
3826
3827 if (!_is_phys_bindable(phys_entry, obj->physoffset, obj->size,
3828 obj->flags)) {
3829 kgsl_mem_entry_put(phys_entry);
3830 return -EINVAL;
3831 }
3832
3833 if (kgsl_memdesc_get_align(&virt_entry->memdesc) !=
3834 kgsl_memdesc_get_align(&phys_entry->memdesc)) {
3835 kgsl_mem_entry_put(phys_entry);
3836 return -EINVAL;
3837 }
3838
3839 ret = sparse_unbind_range(obj, virt_entry);
3840 if (ret) {
3841 kgsl_mem_entry_put(phys_entry);
3842 return -EINVAL;
3843 }
3844
3845 ret = _sparse_bind(private, virt_entry, obj->virtoffset,
3846 phys_entry, obj->physoffset, obj->size,
3847 obj->flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS);
3848 if (ret == 0) {
3849 KGSL_STATS_ADD(obj->size, &kgsl_driver.stats.mapped,
3850 &kgsl_driver.stats.mapped_max);
3851
3852 trace_sparse_bind(virt_entry->id, obj->virtoffset,
3853 phys_entry->id, obj->physoffset,
3854 obj->size, obj->flags);
3855 }
3856
3857 kgsl_mem_entry_put(phys_entry);
3858
3859 return ret;
3860}
3861
3862long kgsl_ioctl_sparse_bind(struct kgsl_device_private *dev_priv,
3863 unsigned int cmd, void *data)
3864{
3865 struct kgsl_process_private *private = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003866 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003867 struct kgsl_sparse_bind *param = data;
3868 struct kgsl_sparse_binding_object obj;
3869 struct kgsl_mem_entry *virt_entry;
3870 int pg_sz;
3871 void __user *ptr;
3872 int ret = 0;
3873 int i = 0;
3874
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003875 if (!(device->flags & KGSL_FLAG_SPARSE))
3876 return -ENOTSUPP;
3877
Shrenuj Bansala419c792016-10-20 14:05:11 -07003878 ptr = (void __user *) (uintptr_t) param->list;
3879
3880 if (param->size > sizeof(struct kgsl_sparse_binding_object) ||
3881 param->count == 0 || ptr == NULL)
3882 return -EINVAL;
3883
3884 virt_entry = kgsl_sharedmem_find_id_flags(private, param->id,
3885 KGSL_MEMFLAGS_SPARSE_VIRT);
3886 if (virt_entry == NULL)
3887 return -EINVAL;
3888
3889 pg_sz = kgsl_memdesc_get_pagesize(&virt_entry->memdesc);
3890
3891 for (i = 0; i < param->count; i++) {
3892 memset(&obj, 0, sizeof(obj));
3893 ret = _copy_from_user(&obj, ptr, sizeof(obj), param->size);
3894 if (ret)
3895 break;
3896
3897 /* Sanity check initial range */
Sudeep Yedalapure8ff97992017-01-20 20:12:51 +05303898 if (obj.size == 0 || obj.virtoffset + obj.size < obj.size ||
Shrenuj Bansala419c792016-10-20 14:05:11 -07003899 obj.virtoffset + obj.size > virt_entry->memdesc.size ||
3900 !(IS_ALIGNED(obj.virtoffset | obj.size, pg_sz))) {
3901 ret = -EINVAL;
3902 break;
3903 }
3904
3905 if (obj.flags & KGSL_SPARSE_BIND)
3906 ret = sparse_bind_range(private, &obj, virt_entry);
3907 else if (obj.flags & KGSL_SPARSE_UNBIND)
3908 ret = sparse_unbind_range(&obj, virt_entry);
3909 else
3910 ret = -EINVAL;
3911 if (ret)
3912 break;
3913
3914 ptr += sizeof(obj);
3915 }
3916
3917 kgsl_mem_entry_put(virt_entry);
3918
3919 return ret;
3920}
3921
Tarun Karra2b8b3632016-11-14 16:38:27 -08003922long kgsl_ioctl_gpu_sparse_command(struct kgsl_device_private *dev_priv,
3923 unsigned int cmd, void *data)
3924{
3925 struct kgsl_gpu_sparse_command *param = data;
3926 struct kgsl_device *device = dev_priv->device;
3927 struct kgsl_context *context;
3928 struct kgsl_drawobj *drawobj[2];
3929 struct kgsl_drawobj_sparse *sparseobj;
3930 long result;
3931 unsigned int i = 0;
3932
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003933 if (!(device->flags & KGSL_FLAG_SPARSE))
3934 return -ENOTSUPP;
3935
Tarun Karra2b8b3632016-11-14 16:38:27 -08003936 /* Make sure sparse and syncpoint count isn't too big */
3937 if (param->numsparse > KGSL_MAX_SPARSE ||
3938 param->numsyncs > KGSL_MAX_SYNCPOINTS)
3939 return -EINVAL;
3940
3941 /* Make sure there is atleast one sparse or sync */
3942 if (param->numsparse == 0 && param->numsyncs == 0)
3943 return -EINVAL;
3944
3945 /* Only Sparse commands are supported in this ioctl */
3946 if (!(param->flags & KGSL_DRAWOBJ_SPARSE) || (param->flags &
3947 (KGSL_DRAWOBJ_SUBMIT_IB_LIST | KGSL_DRAWOBJ_MARKER
3948 | KGSL_DRAWOBJ_SYNC)))
3949 return -EINVAL;
3950
3951 context = kgsl_context_get_owner(dev_priv, param->context_id);
3952 if (context == NULL)
3953 return -EINVAL;
3954
3955 /* Restrict bind commands to bind context */
3956 if (!(context->flags & KGSL_CONTEXT_SPARSE)) {
3957 kgsl_context_put(context);
3958 return -EINVAL;
3959 }
3960
3961 if (param->numsyncs) {
3962 struct kgsl_drawobj_sync *syncobj = kgsl_drawobj_sync_create(
3963 device, context);
3964 if (IS_ERR(syncobj)) {
3965 result = PTR_ERR(syncobj);
3966 goto done;
3967 }
3968
3969 drawobj[i++] = DRAWOBJ(syncobj);
3970 result = kgsl_drawobj_sync_add_synclist(device, syncobj,
3971 to_user_ptr(param->synclist),
3972 param->syncsize, param->numsyncs);
3973 if (result)
3974 goto done;
3975 }
3976
3977 if (param->numsparse) {
3978 sparseobj = kgsl_drawobj_sparse_create(device, context,
3979 param->flags);
3980 if (IS_ERR(sparseobj)) {
3981 result = PTR_ERR(sparseobj);
3982 goto done;
3983 }
3984
3985 sparseobj->id = param->id;
3986 drawobj[i++] = DRAWOBJ(sparseobj);
3987 result = kgsl_drawobj_sparse_add_sparselist(device, sparseobj,
3988 param->id, to_user_ptr(param->sparselist),
3989 param->sparsesize, param->numsparse);
3990 if (result)
3991 goto done;
3992 }
3993
3994 result = dev_priv->device->ftbl->queue_cmds(dev_priv, context,
3995 drawobj, i, &param->timestamp);
3996
3997done:
3998 /*
3999 * -EPROTO is a "success" error - it just tells the user that the
4000 * context had previously faulted
4001 */
4002 if (result && result != -EPROTO)
4003 while (i--)
4004 kgsl_drawobj_destroy(drawobj[i]);
4005
4006 kgsl_context_put(context);
4007 return result;
4008}
4009
4010void kgsl_sparse_bind(struct kgsl_process_private *private,
4011 struct kgsl_drawobj_sparse *sparseobj)
4012{
4013 struct kgsl_sparseobj_node *sparse_node;
4014 struct kgsl_mem_entry *virt_entry = NULL;
4015 long ret = 0;
4016 char *name;
4017
4018 virt_entry = kgsl_sharedmem_find_id_flags(private, sparseobj->id,
4019 KGSL_MEMFLAGS_SPARSE_VIRT);
4020 if (virt_entry == NULL)
4021 return;
4022
4023 list_for_each_entry(sparse_node, &sparseobj->sparselist, node) {
4024 if (sparse_node->obj.flags & KGSL_SPARSE_BIND) {
4025 ret = sparse_bind_range(private, &sparse_node->obj,
4026 virt_entry);
4027 name = "bind";
4028 } else {
4029 ret = sparse_unbind_range(&sparse_node->obj,
4030 virt_entry);
4031 name = "unbind";
4032 }
4033
4034 if (ret)
4035 KGSL_CORE_ERR("kgsl: Unable to '%s' ret %ld virt_id %d, phys_id %d, virt_offset %16.16llX, phys_offset %16.16llX, size %16.16llX, flags %16.16llX\n",
4036 name, ret, sparse_node->virt_id,
4037 sparse_node->obj.id,
4038 sparse_node->obj.virtoffset,
4039 sparse_node->obj.physoffset,
4040 sparse_node->obj.size, sparse_node->obj.flags);
4041 }
4042
4043 kgsl_mem_entry_put(virt_entry);
4044}
4045EXPORT_SYMBOL(kgsl_sparse_bind);
4046
Shrenuj Bansala419c792016-10-20 14:05:11 -07004047long kgsl_ioctl_gpuobj_info(struct kgsl_device_private *dev_priv,
4048 unsigned int cmd, void *data)
4049{
4050 struct kgsl_process_private *private = dev_priv->process_priv;
4051 struct kgsl_gpuobj_info *param = data;
4052 struct kgsl_mem_entry *entry;
4053
4054 if (param->id == 0)
4055 return -EINVAL;
4056
4057 entry = kgsl_sharedmem_find_id(private, param->id);
4058 if (entry == NULL)
4059 return -EINVAL;
4060
4061 param->id = entry->id;
4062 param->gpuaddr = entry->memdesc.gpuaddr;
4063 param->flags = entry->memdesc.flags;
4064 param->size = entry->memdesc.size;
4065 param->va_len = kgsl_memdesc_footprint(&entry->memdesc);
4066 param->va_addr = (uint64_t) entry->memdesc.useraddr;
4067
4068 kgsl_mem_entry_put(entry);
4069 return 0;
4070}
4071
4072long kgsl_ioctl_gpuobj_set_info(struct kgsl_device_private *dev_priv,
4073 unsigned int cmd, void *data)
4074{
4075 struct kgsl_process_private *private = dev_priv->process_priv;
4076 struct kgsl_gpuobj_set_info *param = data;
4077 struct kgsl_mem_entry *entry;
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304078 int ret = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004079
4080 if (param->id == 0)
4081 return -EINVAL;
4082
4083 entry = kgsl_sharedmem_find_id(private, param->id);
4084 if (entry == NULL)
4085 return -EINVAL;
4086
4087 if (param->flags & KGSL_GPUOBJ_SET_INFO_METADATA)
4088 copy_metadata(entry, param->metadata, param->metadata_len);
4089
4090 if (param->flags & KGSL_GPUOBJ_SET_INFO_TYPE) {
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304091 if (param->type <= (KGSL_MEMTYPE_MASK >> KGSL_MEMTYPE_SHIFT)) {
4092 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMTYPE_MASK);
4093 entry->memdesc.flags |= (uint64_t)((param->type <<
4094 KGSL_MEMTYPE_SHIFT) & KGSL_MEMTYPE_MASK);
4095 } else
4096 ret = -EINVAL;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004097 }
4098
4099 kgsl_mem_entry_put(entry);
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304100 return ret;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004101}
4102
4103/**
4104 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
4105 * @dev_priv - pointer to the private device structure
4106 * @cmd - the ioctl cmd passed from kgsl_ioctl
4107 * @data - the user data buffer from kgsl_ioctl
4108 * @returns 0 on success or error code on failure
4109 */
4110
4111long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
4112 unsigned int cmd, void *data)
4113{
4114 struct kgsl_timestamp_event *param = data;
4115 int ret;
4116
4117 switch (param->type) {
4118 case KGSL_TIMESTAMP_EVENT_FENCE:
4119 ret = kgsl_add_fence_event(dev_priv->device,
4120 param->context_id, param->timestamp, param->priv,
4121 param->len, dev_priv);
4122 break;
4123 default:
4124 ret = -EINVAL;
4125 }
4126
4127 return ret;
4128}
4129
4130static int
4131kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
4132{
4133 struct kgsl_memdesc *memdesc = &device->memstore;
4134 int result;
4135 unsigned int vma_size = vma->vm_end - vma->vm_start;
4136
4137 /* The memstore can only be mapped as read only */
4138
4139 if (vma->vm_flags & VM_WRITE)
4140 return -EPERM;
4141
4142 if (memdesc->size != vma_size) {
4143 KGSL_MEM_ERR(device, "memstore bad size: %d should be %llu\n",
4144 vma_size, memdesc->size);
4145 return -EINVAL;
4146 }
4147
4148 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
4149
4150 result = remap_pfn_range(vma, vma->vm_start,
4151 device->memstore.physaddr >> PAGE_SHIFT,
4152 vma_size, vma->vm_page_prot);
4153 if (result != 0)
4154 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
4155 result);
4156
4157 return result;
4158}
4159
4160/*
4161 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
4162 * Increase the refcount to make sure that the accounting stays correct
4163 */
4164
4165static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
4166{
4167 struct kgsl_mem_entry *entry = vma->vm_private_data;
4168
4169 if (kgsl_mem_entry_get(entry) == 0)
4170 vma->vm_private_data = NULL;
4171}
4172
4173static int
4174kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4175{
4176 struct kgsl_mem_entry *entry = vma->vm_private_data;
Amit Kushwaha7c843c22018-04-09 20:41:14 +05304177 int ret;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004178
4179 if (!entry)
4180 return VM_FAULT_SIGBUS;
4181 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
4182 return VM_FAULT_SIGBUS;
4183
Amit Kushwaha7c843c22018-04-09 20:41:14 +05304184 ret = entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
4185 if ((ret == 0) || (ret == VM_FAULT_NOPAGE))
4186 entry->priv->gpumem_mapped += PAGE_SIZE;
4187
4188 return ret;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004189}
4190
4191static void
4192kgsl_gpumem_vm_close(struct vm_area_struct *vma)
4193{
4194 struct kgsl_mem_entry *entry = vma->vm_private_data;
4195
4196 if (!entry)
4197 return;
4198
4199 entry->memdesc.useraddr = 0;
4200 kgsl_mem_entry_put(entry);
4201}
4202
4203static const struct vm_operations_struct kgsl_gpumem_vm_ops = {
4204 .open = kgsl_gpumem_vm_open,
4205 .fault = kgsl_gpumem_vm_fault,
4206 .close = kgsl_gpumem_vm_close,
4207};
4208
4209static int
4210get_mmap_entry(struct kgsl_process_private *private,
4211 struct kgsl_mem_entry **out_entry, unsigned long pgoff,
4212 unsigned long len)
4213{
4214 int ret = 0;
4215 struct kgsl_mem_entry *entry;
4216
4217 entry = kgsl_sharedmem_find_id(private, pgoff);
4218 if (entry == NULL)
4219 entry = kgsl_sharedmem_find(private, pgoff << PAGE_SHIFT);
4220
4221 if (!entry)
4222 return -EINVAL;
4223
4224 if (!entry->memdesc.ops ||
4225 !entry->memdesc.ops->vmflags ||
4226 !entry->memdesc.ops->vmfault) {
4227 ret = -EINVAL;
4228 goto err_put;
4229 }
4230
4231 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_PHYS) {
4232 if (len != entry->memdesc.size) {
4233 ret = -EINVAL;
4234 goto err_put;
4235 }
4236 }
4237
4238 if (entry->memdesc.useraddr != 0) {
4239 ret = -EBUSY;
4240 goto err_put;
4241 }
4242
4243 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
4244 if (len != kgsl_memdesc_footprint(&entry->memdesc)) {
4245 ret = -ERANGE;
4246 goto err_put;
4247 }
4248 } else if (len != kgsl_memdesc_footprint(&entry->memdesc) &&
4249 len != entry->memdesc.size) {
4250 /*
4251 * If cpu_map != gpumap then user can map either the
4252 * footprint or the entry size
4253 */
4254 ret = -ERANGE;
4255 goto err_put;
4256 }
4257
4258 *out_entry = entry;
4259 return 0;
4260err_put:
4261 kgsl_mem_entry_put(entry);
4262 return ret;
4263}
4264
4265static unsigned long _gpu_set_svm_region(struct kgsl_process_private *private,
4266 struct kgsl_mem_entry *entry, unsigned long addr,
4267 unsigned long size)
4268{
4269 int ret;
4270
4271 ret = kgsl_mmu_set_svm_region(private->pagetable, (uint64_t) addr,
4272 (uint64_t) size);
4273
4274 if (ret != 0)
4275 return ret;
4276
4277 entry->memdesc.gpuaddr = (uint64_t) addr;
4278 entry->memdesc.pagetable = private->pagetable;
4279
4280 ret = kgsl_mmu_map(private->pagetable, &entry->memdesc);
4281 if (ret) {
4282 kgsl_mmu_put_gpuaddr(&entry->memdesc);
4283 return ret;
4284 }
4285
4286 kgsl_memfree_purge(private->pagetable, entry->memdesc.gpuaddr,
4287 entry->memdesc.size);
4288
4289 return addr;
4290}
4291
4292static unsigned long _gpu_find_svm(struct kgsl_process_private *private,
4293 unsigned long start, unsigned long end, unsigned long len,
4294 unsigned int align)
4295{
4296 uint64_t addr = kgsl_mmu_find_svm_region(private->pagetable,
4297 (uint64_t) start, (uint64_t)end, (uint64_t) len, align);
4298
4299 BUG_ON(!IS_ERR_VALUE((unsigned long)addr) && (addr > ULONG_MAX));
4300
4301 return (unsigned long) addr;
4302}
4303
4304/* Search top down in the CPU VM region for a free address */
4305static unsigned long _cpu_get_unmapped_area(unsigned long bottom,
4306 unsigned long top, unsigned long len, unsigned long align)
4307{
4308 struct vm_unmapped_area_info info;
4309 unsigned long addr, err;
4310
4311 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
4312 info.low_limit = bottom;
4313 info.high_limit = top;
4314 info.length = len;
4315 info.align_offset = 0;
4316 info.align_mask = align - 1;
4317
4318 addr = vm_unmapped_area(&info);
4319
4320 if (IS_ERR_VALUE(addr))
4321 return addr;
4322
4323 err = security_mmap_addr(addr);
4324 return err ? err : addr;
4325}
4326
4327static unsigned long _search_range(struct kgsl_process_private *private,
4328 struct kgsl_mem_entry *entry,
4329 unsigned long start, unsigned long end,
4330 unsigned long len, uint64_t align)
4331{
4332 unsigned long cpu, gpu = end, result = -ENOMEM;
4333
4334 while (gpu > start) {
4335 /* find a new empty spot on the CPU below the last one */
4336 cpu = _cpu_get_unmapped_area(start, gpu, len,
4337 (unsigned long) align);
4338 if (IS_ERR_VALUE(cpu)) {
4339 result = cpu;
4340 break;
4341 }
4342 /* try to map it on the GPU */
4343 result = _gpu_set_svm_region(private, entry, cpu, len);
4344 if (!IS_ERR_VALUE(result))
4345 break;
4346
4347 trace_kgsl_mem_unmapped_area_collision(entry, cpu, len);
4348
4349 if (cpu <= start) {
4350 result = -ENOMEM;
4351 break;
4352 }
4353
4354 /* move downward to the next empty spot on the GPU */
4355 gpu = _gpu_find_svm(private, start, cpu, len, align);
4356 if (IS_ERR_VALUE(gpu)) {
4357 result = gpu;
4358 break;
4359 }
4360
4361 /* Check that_gpu_find_svm doesn't put us in a loop */
4362 if (gpu >= cpu) {
4363 result = -ENOMEM;
4364 break;
4365 }
4366
4367 /* Break if the recommended GPU address is out of range */
4368 if (gpu < start) {
4369 result = -ENOMEM;
4370 break;
4371 }
4372
4373 /*
4374 * Add the length of the chunk to the GPU address to yield the
4375 * upper bound for the CPU search
4376 */
4377 gpu += len;
4378 }
4379 return result;
4380}
4381
4382static unsigned long _get_svm_area(struct kgsl_process_private *private,
4383 struct kgsl_mem_entry *entry, unsigned long hint,
4384 unsigned long len, unsigned long flags)
4385{
4386 uint64_t start, end;
4387 int align_shift = kgsl_memdesc_get_align(&entry->memdesc);
4388 uint64_t align;
4389 unsigned long result;
4390 unsigned long addr;
4391
4392 if (align_shift >= ilog2(SZ_2M))
4393 align = SZ_2M;
4394 else if (align_shift >= ilog2(SZ_1M))
4395 align = SZ_1M;
4396 else if (align_shift >= ilog2(SZ_64K))
4397 align = SZ_64K;
4398 else
4399 align = SZ_4K;
4400
4401 /* get the GPU pagetable's SVM range */
4402 if (kgsl_mmu_svm_range(private->pagetable, &start, &end,
4403 entry->memdesc.flags))
4404 return -ERANGE;
4405
4406 /* now clamp the range based on the CPU's requirements */
4407 start = max_t(uint64_t, start, mmap_min_addr);
4408 end = min_t(uint64_t, end, current->mm->mmap_base);
4409 if (start >= end)
4410 return -ERANGE;
4411
4412 if (flags & MAP_FIXED) {
4413 /* we must use addr 'hint' or fail */
4414 return _gpu_set_svm_region(private, entry, hint, len);
4415 } else if (hint != 0) {
4416 struct vm_area_struct *vma;
4417
4418 /*
4419 * See if the hint is usable, if not we will use
4420 * it as the start point for searching.
4421 */
4422 addr = clamp_t(unsigned long, hint & ~(align - 1),
4423 start, (end - len) & ~(align - 1));
4424
4425 vma = find_vma(current->mm, addr);
4426
4427 if (vma == NULL || ((addr + len) <= vma->vm_start)) {
4428 result = _gpu_set_svm_region(private, entry, addr, len);
4429
4430 /* On failure drop down to keep searching */
4431 if (!IS_ERR_VALUE(result))
4432 return result;
4433 }
4434 } else {
4435 /* no hint, start search at the top and work down */
4436 addr = end & ~(align - 1);
4437 }
4438
4439 /*
4440 * Search downwards from the hint first. If that fails we
4441 * must try to search above it.
4442 */
4443 result = _search_range(private, entry, start, addr, len, align);
4444 if (IS_ERR_VALUE(result) && hint != 0)
4445 result = _search_range(private, entry, addr, end, len, align);
4446
4447 return result;
4448}
4449
4450static unsigned long
4451kgsl_get_unmapped_area(struct file *file, unsigned long addr,
4452 unsigned long len, unsigned long pgoff,
4453 unsigned long flags)
4454{
4455 unsigned long val;
4456 unsigned long vma_offset = pgoff << PAGE_SHIFT;
4457 struct kgsl_device_private *dev_priv = file->private_data;
4458 struct kgsl_process_private *private = dev_priv->process_priv;
4459 struct kgsl_device *device = dev_priv->device;
4460 struct kgsl_mem_entry *entry = NULL;
4461
4462 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4463 return get_unmapped_area(NULL, addr, len, pgoff, flags);
4464
4465 val = get_mmap_entry(private, &entry, pgoff, len);
4466 if (val)
4467 return val;
4468
4469 /* Do not allow CPU mappings for secure buffers */
4470 if (kgsl_memdesc_is_secured(&entry->memdesc)) {
4471 val = -EPERM;
4472 goto put;
4473 }
4474
4475 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
4476 val = get_unmapped_area(NULL, addr, len, 0, flags);
4477 if (IS_ERR_VALUE(val))
Venkateswara Rao Tadikondad57f7e52017-08-29 11:02:38 +05304478 KGSL_DRV_ERR_RATELIMIT(device,
Shrenuj Bansala419c792016-10-20 14:05:11 -07004479 "get_unmapped_area: pid %d addr %lx pgoff %lx len %ld failed error %d\n",
4480 private->pid, addr, pgoff, len, (int) val);
4481 } else {
4482 val = _get_svm_area(private, entry, addr, len, flags);
4483 if (IS_ERR_VALUE(val))
Venkateswara Rao Tadikondad57f7e52017-08-29 11:02:38 +05304484 KGSL_DRV_ERR_RATELIMIT(device,
Hareesh Gunduca522a12017-02-15 16:02:06 +05304485 "_get_svm_area: pid %d mmap_base %lx addr %lx pgoff %lx len %ld failed error %d\n",
4486 private->pid, current->mm->mmap_base, addr,
4487 pgoff, len, (int) val);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004488 }
4489
4490put:
4491 kgsl_mem_entry_put(entry);
4492 return val;
4493}
4494
4495static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
4496{
4497 unsigned int ret, cache;
4498 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
4499 struct kgsl_device_private *dev_priv = file->private_data;
4500 struct kgsl_process_private *private = dev_priv->process_priv;
4501 struct kgsl_mem_entry *entry = NULL;
4502 struct kgsl_device *device = dev_priv->device;
4503
4504 /* Handle leagacy behavior for memstore */
4505
4506 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4507 return kgsl_mmap_memstore(device, vma);
4508
4509 /*
4510 * The reference count on the entry that we get from
4511 * get_mmap_entry() will be held until kgsl_gpumem_vm_close().
4512 */
4513 ret = get_mmap_entry(private, &entry, vma->vm_pgoff,
4514 vma->vm_end - vma->vm_start);
4515 if (ret)
4516 return ret;
4517
4518 vma->vm_flags |= entry->memdesc.ops->vmflags;
4519
4520 vma->vm_private_data = entry;
4521
4522 /* Determine user-side caching policy */
4523
4524 cache = kgsl_memdesc_get_cachemode(&entry->memdesc);
4525
4526 switch (cache) {
4527 case KGSL_CACHEMODE_UNCACHED:
4528 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
4529 break;
4530 case KGSL_CACHEMODE_WRITETHROUGH:
4531 vma->vm_page_prot = pgprot_writethroughcache(vma->vm_page_prot);
4532 if (pgprot_val(vma->vm_page_prot) ==
4533 pgprot_val(pgprot_writebackcache(vma->vm_page_prot)))
4534 WARN_ONCE(1, "WRITETHROUGH is deprecated for arm64");
4535 break;
4536 case KGSL_CACHEMODE_WRITEBACK:
4537 vma->vm_page_prot = pgprot_writebackcache(vma->vm_page_prot);
4538 break;
4539 case KGSL_CACHEMODE_WRITECOMBINE:
4540 default:
4541 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
4542 break;
4543 }
4544
4545 vma->vm_ops = &kgsl_gpumem_vm_ops;
4546
4547 if (cache == KGSL_CACHEMODE_WRITEBACK
4548 || cache == KGSL_CACHEMODE_WRITETHROUGH) {
4549 int i;
4550 unsigned long addr = vma->vm_start;
4551 struct kgsl_memdesc *m = &entry->memdesc;
4552
4553 for (i = 0; i < m->page_count; i++) {
4554 struct page *page = m->pages[i];
4555
4556 vm_insert_page(vma, addr, page);
4557 addr += PAGE_SIZE;
4558 }
4559 }
4560
4561 vma->vm_file = file;
4562
4563 entry->memdesc.useraddr = vma->vm_start;
4564
4565 trace_kgsl_mem_mmap(entry);
4566 return 0;
4567}
4568
4569static irqreturn_t kgsl_irq_handler(int irq, void *data)
4570{
4571 struct kgsl_device *device = data;
4572
4573 return device->ftbl->irq_handler(device);
4574
4575}
4576
4577#define KGSL_READ_MESSAGE "OH HAI GPU\n"
4578
4579static ssize_t kgsl_read(struct file *filep, char __user *buf, size_t count,
4580 loff_t *pos)
4581{
4582 return simple_read_from_buffer(buf, count, pos,
4583 KGSL_READ_MESSAGE, strlen(KGSL_READ_MESSAGE) + 1);
4584}
4585
4586static const struct file_operations kgsl_fops = {
4587 .owner = THIS_MODULE,
4588 .release = kgsl_release,
4589 .open = kgsl_open,
4590 .mmap = kgsl_mmap,
4591 .read = kgsl_read,
4592 .get_unmapped_area = kgsl_get_unmapped_area,
4593 .unlocked_ioctl = kgsl_ioctl,
4594 .compat_ioctl = kgsl_compat_ioctl,
4595};
4596
4597struct kgsl_driver kgsl_driver = {
4598 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
4599 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
4600 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
4601 /*
4602 * Full cache flushes are faster than line by line on at least
4603 * 8064 and 8974 once the region to be flushed is > 16mb.
4604 */
4605 .full_cache_threshold = SZ_16M,
4606
4607 .stats.vmalloc = ATOMIC_LONG_INIT(0),
4608 .stats.vmalloc_max = ATOMIC_LONG_INIT(0),
4609 .stats.page_alloc = ATOMIC_LONG_INIT(0),
4610 .stats.page_alloc_max = ATOMIC_LONG_INIT(0),
4611 .stats.coherent = ATOMIC_LONG_INIT(0),
4612 .stats.coherent_max = ATOMIC_LONG_INIT(0),
4613 .stats.secure = ATOMIC_LONG_INIT(0),
4614 .stats.secure_max = ATOMIC_LONG_INIT(0),
4615 .stats.mapped = ATOMIC_LONG_INIT(0),
4616 .stats.mapped_max = ATOMIC_LONG_INIT(0),
4617};
4618EXPORT_SYMBOL(kgsl_driver);
4619
4620static void _unregister_device(struct kgsl_device *device)
4621{
4622 int minor;
4623
4624 mutex_lock(&kgsl_driver.devlock);
4625 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4626 if (device == kgsl_driver.devp[minor])
4627 break;
4628 }
4629 if (minor != KGSL_DEVICE_MAX) {
4630 device_destroy(kgsl_driver.class,
4631 MKDEV(MAJOR(kgsl_driver.major), minor));
4632 kgsl_driver.devp[minor] = NULL;
4633 }
4634 mutex_unlock(&kgsl_driver.devlock);
4635}
4636
4637static int _register_device(struct kgsl_device *device)
4638{
4639 int minor, ret;
4640 dev_t dev;
4641
4642 /* Find a minor for the device */
4643
4644 mutex_lock(&kgsl_driver.devlock);
4645 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4646 if (kgsl_driver.devp[minor] == NULL) {
4647 kgsl_driver.devp[minor] = device;
4648 break;
4649 }
4650 }
4651 mutex_unlock(&kgsl_driver.devlock);
4652
4653 if (minor == KGSL_DEVICE_MAX) {
4654 KGSL_CORE_ERR("minor devices exhausted\n");
4655 return -ENODEV;
4656 }
4657
4658 /* Create the device */
4659 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
4660 device->dev = device_create(kgsl_driver.class,
4661 &device->pdev->dev,
4662 dev, device,
4663 device->name);
4664
4665 if (IS_ERR(device->dev)) {
4666 mutex_lock(&kgsl_driver.devlock);
4667 kgsl_driver.devp[minor] = NULL;
4668 mutex_unlock(&kgsl_driver.devlock);
4669 ret = PTR_ERR(device->dev);
4670 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
4671 return ret;
4672 }
4673
4674 dev_set_drvdata(&device->pdev->dev, device);
4675 return 0;
4676}
4677
4678int kgsl_device_platform_probe(struct kgsl_device *device)
4679{
4680 int status = -EINVAL;
4681 struct resource *res;
4682 int cpu;
4683
4684 status = _register_device(device);
4685 if (status)
4686 return status;
4687
4688 /* Initialize logging first, so that failures below actually print. */
4689 kgsl_device_debugfs_init(device);
4690
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06004691 /* Disable the sparse ioctl invocation as they are not used */
4692 device->flags &= ~KGSL_FLAG_SPARSE;
4693
Shrenuj Bansala419c792016-10-20 14:05:11 -07004694 status = kgsl_pwrctrl_init(device);
4695 if (status)
4696 goto error;
4697
Shrenuj Bansala419c792016-10-20 14:05:11 -07004698 /*
4699 * Check if a shadermemname is defined, and then get shader memory
4700 * details including shader memory starting physical address
4701 * and shader memory length
4702 */
4703 if (device->shadermemname != NULL) {
4704 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
4705 device->shadermemname);
4706
4707 if (res == NULL) {
4708 KGSL_DRV_WARN(device,
4709 "Shader memory: platform_get_resource_byname failed\n");
4710 }
4711
4712 else {
4713 device->shader_mem_phys = res->start;
4714 device->shader_mem_len = resource_size(res);
4715 }
4716
4717 if (!devm_request_mem_region(device->dev,
4718 device->shader_mem_phys,
4719 device->shader_mem_len,
4720 device->name)) {
4721 KGSL_DRV_WARN(device, "request_mem_region_failed\n");
4722 }
4723 }
4724
4725 if (!devm_request_mem_region(device->dev, device->reg_phys,
4726 device->reg_len, device->name)) {
4727 KGSL_DRV_ERR(device, "request_mem_region failed\n");
4728 status = -ENODEV;
4729 goto error_pwrctrl_close;
4730 }
4731
4732 device->reg_virt = devm_ioremap(device->dev, device->reg_phys,
4733 device->reg_len);
4734
4735 if (device->reg_virt == NULL) {
4736 KGSL_DRV_ERR(device, "ioremap failed\n");
4737 status = -ENODEV;
4738 goto error_pwrctrl_close;
4739 }
4740 /*acquire interrupt */
4741 device->pwrctrl.interrupt_num =
4742 platform_get_irq_byname(device->pdev, device->pwrctrl.irq_name);
4743
4744 if (device->pwrctrl.interrupt_num <= 0) {
4745 KGSL_DRV_ERR(device, "platform_get_irq_byname failed: %d\n",
4746 device->pwrctrl.interrupt_num);
4747 status = -EINVAL;
4748 goto error_pwrctrl_close;
4749 }
4750
4751 status = devm_request_irq(device->dev, device->pwrctrl.interrupt_num,
4752 kgsl_irq_handler, IRQF_TRIGGER_HIGH,
4753 device->name, device);
4754 if (status) {
4755 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
4756 device->pwrctrl.interrupt_num, status);
4757 goto error_pwrctrl_close;
4758 }
4759 disable_irq(device->pwrctrl.interrupt_num);
4760
4761 KGSL_DRV_INFO(device,
4762 "dev_id %d regs phys 0x%08lx size 0x%08x\n",
4763 device->id, device->reg_phys, device->reg_len);
4764
4765 rwlock_init(&device->context_lock);
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05304766 spin_lock_init(&device->submit_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004767
4768 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
4769
4770 status = kgsl_mmu_probe(device, kgsl_mmu_type);
4771 if (status != 0)
4772 goto error_pwrctrl_close;
4773
4774 /* Check to see if our device can perform DMA correctly */
4775 status = dma_set_coherent_mask(&device->pdev->dev, KGSL_DMA_BIT_MASK);
4776 if (status)
4777 goto error_close_mmu;
4778
4779 /* Initialize the memory pools */
4780 kgsl_init_page_pools(device->pdev);
4781
4782 status = kgsl_allocate_global(device, &device->memstore,
4783 KGSL_MEMSTORE_SIZE, 0, KGSL_MEMDESC_CONTIG, "memstore");
4784
4785 if (status != 0)
4786 goto error_close_mmu;
4787
Shrenuj Bansala419c792016-10-20 14:05:11 -07004788 /*
4789 * The default request type PM_QOS_REQ_ALL_CORES is
4790 * applicable to all CPU cores that are online and
4791 * would have a power impact when there are more
4792 * number of CPUs. PM_QOS_REQ_AFFINE_IRQ request
4793 * type shall update/apply the vote only to that CPU to
4794 * which IRQ's affinity is set to.
4795 */
4796#ifdef CONFIG_SMP
4797
4798 device->pwrctrl.pm_qos_req_dma.type = PM_QOS_REQ_AFFINE_IRQ;
4799 device->pwrctrl.pm_qos_req_dma.irq = device->pwrctrl.interrupt_num;
4800
4801#endif
4802 pm_qos_add_request(&device->pwrctrl.pm_qos_req_dma,
4803 PM_QOS_CPU_DMA_LATENCY,
4804 PM_QOS_DEFAULT_VALUE);
4805
4806 if (device->pwrctrl.l2pc_cpus_mask) {
4807
4808 device->pwrctrl.l2pc_cpus_qos.type =
4809 PM_QOS_REQ_AFFINE_CORES;
4810 cpumask_empty(&device->pwrctrl.l2pc_cpus_qos.cpus_affine);
4811 for_each_possible_cpu(cpu) {
4812 if ((1 << cpu) & device->pwrctrl.l2pc_cpus_mask)
4813 cpumask_set_cpu(cpu, &device->pwrctrl.
4814 l2pc_cpus_qos.cpus_affine);
4815 }
4816
4817 pm_qos_add_request(&device->pwrctrl.l2pc_cpus_qos,
4818 PM_QOS_CPU_DMA_LATENCY,
4819 PM_QOS_DEFAULT_VALUE);
4820 }
4821
4822 device->events_wq = alloc_workqueue("kgsl-events",
4823 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4824
4825 /* Initialize the snapshot engine */
4826 kgsl_device_snapshot_init(device);
4827
4828 /* Initialize common sysfs entries */
4829 kgsl_pwrctrl_init_sysfs(device);
4830
4831 return 0;
4832
Shrenuj Bansala419c792016-10-20 14:05:11 -07004833error_close_mmu:
4834 kgsl_mmu_close(device);
4835error_pwrctrl_close:
4836 kgsl_pwrctrl_close(device);
4837error:
Lynus Vaz519dacfd2017-02-14 12:17:37 +05304838 kgsl_device_debugfs_close(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004839 _unregister_device(device);
4840 return status;
4841}
4842EXPORT_SYMBOL(kgsl_device_platform_probe);
4843
4844void kgsl_device_platform_remove(struct kgsl_device *device)
4845{
4846 destroy_workqueue(device->events_wq);
4847
4848 kgsl_device_snapshot_close(device);
4849
4850 kgsl_exit_page_pools();
4851
4852 kgsl_pwrctrl_uninit_sysfs(device);
4853
4854 pm_qos_remove_request(&device->pwrctrl.pm_qos_req_dma);
4855 if (device->pwrctrl.l2pc_cpus_mask)
4856 pm_qos_remove_request(&device->pwrctrl.l2pc_cpus_qos);
4857
4858 idr_destroy(&device->context_idr);
4859
Shrenuj Bansala419c792016-10-20 14:05:11 -07004860 kgsl_free_global(device, &device->memstore);
4861
4862 kgsl_mmu_close(device);
4863
4864 kgsl_pwrctrl_close(device);
4865
Lynus Vaz519dacfd2017-02-14 12:17:37 +05304866 kgsl_device_debugfs_close(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004867 _unregister_device(device);
4868}
4869EXPORT_SYMBOL(kgsl_device_platform_remove);
4870
4871static void kgsl_core_exit(void)
4872{
4873 kgsl_events_exit();
4874 kgsl_core_debugfs_close();
4875
4876 /*
4877 * We call kgsl_sharedmem_uninit_sysfs() and device_unregister()
4878 * only if kgsl_driver.virtdev has been populated.
4879 * We check at least one member of kgsl_driver.virtdev to
4880 * see if it is not NULL (and thus, has been populated).
4881 */
4882 if (kgsl_driver.virtdev.class) {
4883 kgsl_sharedmem_uninit_sysfs();
4884 device_unregister(&kgsl_driver.virtdev);
4885 }
4886
4887 if (kgsl_driver.class) {
4888 class_destroy(kgsl_driver.class);
4889 kgsl_driver.class = NULL;
4890 }
4891
Tarun Karra2b8b3632016-11-14 16:38:27 -08004892 kgsl_drawobjs_cache_exit();
Shrenuj Bansala419c792016-10-20 14:05:11 -07004893
4894 kgsl_memfree_exit();
4895 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
4896}
4897
4898static int __init kgsl_core_init(void)
4899{
4900 int result = 0;
Tim Murray85040432017-02-20 15:59:32 +05304901 struct sched_param param = { .sched_priority = 2 };
4902
Shrenuj Bansala419c792016-10-20 14:05:11 -07004903 /* alloc major and minor device numbers */
4904 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
4905 "kgsl");
4906
4907 if (result < 0) {
4908
4909 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
4910 goto err;
4911 }
4912
4913 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
4914 kgsl_driver.cdev.owner = THIS_MODULE;
4915 kgsl_driver.cdev.ops = &kgsl_fops;
4916 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
4917 KGSL_DEVICE_MAX);
4918
4919 if (result) {
4920 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d, result= %d\n",
4921 kgsl_driver.major, result);
4922 goto err;
4923 }
4924
4925 kgsl_driver.class = class_create(THIS_MODULE, "kgsl");
4926
4927 if (IS_ERR(kgsl_driver.class)) {
4928 result = PTR_ERR(kgsl_driver.class);
4929 KGSL_CORE_ERR("failed to create class for kgsl");
4930 goto err;
4931 }
4932
4933 /*
4934 * Make a virtual device for managing core related things
4935 * in sysfs
4936 */
4937 kgsl_driver.virtdev.class = kgsl_driver.class;
4938 dev_set_name(&kgsl_driver.virtdev, "kgsl");
4939 result = device_register(&kgsl_driver.virtdev);
4940 if (result) {
4941 KGSL_CORE_ERR("driver_register failed\n");
4942 goto err;
4943 }
4944
4945 /* Make kobjects in the virtual device for storing statistics */
4946
4947 kgsl_driver.ptkobj =
4948 kobject_create_and_add("pagetables",
4949 &kgsl_driver.virtdev.kobj);
4950
4951 kgsl_driver.prockobj =
4952 kobject_create_and_add("proc",
4953 &kgsl_driver.virtdev.kobj);
4954
4955 kgsl_core_debugfs_init();
4956
4957 kgsl_sharedmem_init_sysfs();
4958
4959 INIT_LIST_HEAD(&kgsl_driver.process_list);
4960
4961 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
4962
4963 kgsl_driver.workqueue = alloc_workqueue("kgsl-workqueue",
4964 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4965
4966 kgsl_driver.mem_workqueue = alloc_workqueue("kgsl-mementry",
Hareesh Gundu615439d2017-06-16 17:06:57 +05304967 WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004968
Tim Murray85040432017-02-20 15:59:32 +05304969 kthread_init_worker(&kgsl_driver.worker);
4970
4971 kgsl_driver.worker_thread = kthread_run(kthread_worker_fn,
4972 &kgsl_driver.worker, "kgsl_worker_thread");
4973
4974 if (IS_ERR(kgsl_driver.worker_thread)) {
4975 pr_err("unable to start kgsl thread\n");
4976 goto err;
4977 }
4978
4979 sched_setscheduler(kgsl_driver.worker_thread, SCHED_FIFO, &param);
4980
Shrenuj Bansala419c792016-10-20 14:05:11 -07004981 kgsl_events_init();
4982
Tarun Karra2b8b3632016-11-14 16:38:27 -08004983 result = kgsl_drawobjs_cache_init();
Shrenuj Bansala419c792016-10-20 14:05:11 -07004984 if (result)
4985 goto err;
4986
4987 kgsl_memfree_init();
4988
4989 return 0;
4990
4991err:
4992 kgsl_core_exit();
4993 return result;
4994}
4995
4996module_init(kgsl_core_init);
4997module_exit(kgsl_core_exit);
4998
4999MODULE_DESCRIPTION("MSM GPU driver");
5000MODULE_LICENSE("GPL");