blob: bfae8270d7ad7bfd0f0258d6c6551b84768f9df7 [file] [log] [blame]
Kamal Agrawal8f0fb822020-08-19 10:25:15 +05301/* Copyright (c) 2008-2020, 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
Jordan Crouse6bce65c2020-12-28 16:06:42 +0530268 atomic_set(&entry->map_count, 0);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700269 return entry;
270}
271#ifdef CONFIG_DMA_SHARED_BUFFER
272static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
273{
274 if (meta != NULL) {
275 dma_buf_unmap_attachment(meta->attach, meta->table,
276 DMA_FROM_DEVICE);
277 dma_buf_detach(meta->dmabuf, meta->attach);
278 dma_buf_put(meta->dmabuf);
279 kfree(meta);
280 }
281}
282#else
283static void kgsl_destroy_ion(struct kgsl_dma_buf_meta *meta)
284{
285
286}
287#endif
288
289void
290kgsl_mem_entry_destroy(struct kref *kref)
291{
292 struct kgsl_mem_entry *entry = container_of(kref,
293 struct kgsl_mem_entry,
294 refcount);
295 unsigned int memtype;
296
297 if (entry == NULL)
298 return;
299
300 /* pull out the memtype before the flags get cleared */
301 memtype = kgsl_memdesc_usermem_type(&entry->memdesc);
302
303 /* Detach from process list */
304 kgsl_mem_entry_detach_process(entry);
305
306 if (memtype != KGSL_MEM_ENTRY_KERNEL)
307 atomic_long_sub(entry->memdesc.size,
308 &kgsl_driver.stats.mapped);
309
310 /*
311 * Ion takes care of freeing the sg_table for us so
312 * clear the sg table before freeing the sharedmem
313 * so kgsl_sharedmem_free doesn't try to free it again
314 */
315 if (memtype == KGSL_MEM_ENTRY_ION)
316 entry->memdesc.sgt = NULL;
317
318 if ((memtype == KGSL_MEM_ENTRY_USER)
319 && !(entry->memdesc.flags & KGSL_MEMFLAGS_GPUREADONLY)) {
320 int i = 0, j;
321 struct scatterlist *sg;
322 struct page *page;
323 /*
324 * Mark all of pages in the scatterlist as dirty since they
325 * were writable by the GPU.
326 */
327 for_each_sg(entry->memdesc.sgt->sgl, sg,
328 entry->memdesc.sgt->nents, i) {
329 page = sg_page(sg);
330 for (j = 0; j < (sg->length >> PAGE_SHIFT); j++)
Rajesh Kemisettif7a4df12019-04-30 19:04:30 +0530331 set_page_dirty_lock(nth_page(page, j));
Shrenuj Bansala419c792016-10-20 14:05:11 -0700332 }
333 }
334
335 kgsl_sharedmem_free(&entry->memdesc);
336
337 switch (memtype) {
338 case KGSL_MEM_ENTRY_ION:
339 kgsl_destroy_ion(entry->priv_data);
340 break;
341 default:
342 break;
343 }
344
345 kfree(entry);
346}
347EXPORT_SYMBOL(kgsl_mem_entry_destroy);
348
349/* Allocate a IOVA for memory objects that don't use SVM */
350static int kgsl_mem_entry_track_gpuaddr(struct kgsl_device *device,
351 struct kgsl_process_private *process,
352 struct kgsl_mem_entry *entry)
353{
354 struct kgsl_pagetable *pagetable;
355
356 /*
357 * If SVM is enabled for this object then the address needs to be
358 * assigned elsewhere
359 * Also do not proceed further in case of NoMMU.
360 */
361 if (kgsl_memdesc_use_cpu_map(&entry->memdesc) ||
362 (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_NONE))
363 return 0;
364
365 pagetable = kgsl_memdesc_is_secured(&entry->memdesc) ?
366 device->mmu.securepagetable : process->pagetable;
367
368 return kgsl_mmu_get_gpuaddr(pagetable, &entry->memdesc);
369}
370
371/* Commit the entry to the process so it can be accessed by other operations */
372static void kgsl_mem_entry_commit_process(struct kgsl_mem_entry *entry)
373{
374 if (!entry)
375 return;
376
377 spin_lock(&entry->priv->mem_lock);
378 idr_replace(&entry->priv->mem_idr, entry, entry->id);
379 spin_unlock(&entry->priv->mem_lock);
380}
381
382/*
383 * Attach the memory object to a process by (possibly) getting a GPU address and
384 * (possibly) mapping it
385 */
386static int kgsl_mem_entry_attach_process(struct kgsl_device *device,
387 struct kgsl_process_private *process,
388 struct kgsl_mem_entry *entry)
389{
390 int id, ret;
391
392 ret = kgsl_process_private_get(process);
393 if (!ret)
394 return -EBADF;
395
396 ret = kgsl_mem_entry_track_gpuaddr(device, process, entry);
397 if (ret) {
398 kgsl_process_private_put(process);
399 return ret;
400 }
401
402 idr_preload(GFP_KERNEL);
403 spin_lock(&process->mem_lock);
404 /* Allocate the ID but don't attach the pointer just yet */
405 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
406 spin_unlock(&process->mem_lock);
407 idr_preload_end();
408
409 if (id < 0) {
410 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc))
411 kgsl_mmu_put_gpuaddr(&entry->memdesc);
412 kgsl_process_private_put(process);
413 return id;
414 }
415
416 entry->id = id;
417 entry->priv = process;
418
419 /*
420 * Map the memory if a GPU address is already assigned, either through
421 * kgsl_mem_entry_track_gpuaddr() or via some other SVM process
422 */
423 if (entry->memdesc.gpuaddr) {
424 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_VIRT)
425 ret = kgsl_mmu_sparse_dummy_map(
426 entry->memdesc.pagetable,
427 &entry->memdesc, 0,
428 entry->memdesc.size);
429 else if (entry->memdesc.gpuaddr)
430 ret = kgsl_mmu_map(entry->memdesc.pagetable,
431 &entry->memdesc);
432
433 if (ret)
434 kgsl_mem_entry_detach_process(entry);
435 }
436
437 kgsl_memfree_purge(entry->memdesc.pagetable, entry->memdesc.gpuaddr,
438 entry->memdesc.size);
439
440 return ret;
441}
442
443/* Detach a memory entry from a process and unmap it from the MMU */
444static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry)
445{
446 unsigned int type;
447
448 if (entry == NULL)
449 return;
450
451 /*
452 * First remove the entry from mem_idr list
453 * so that no one can operate on obsolete values
454 */
455 spin_lock(&entry->priv->mem_lock);
456 if (entry->id != 0)
457 idr_remove(&entry->priv->mem_idr, entry->id);
458 entry->id = 0;
459
460 type = kgsl_memdesc_usermem_type(&entry->memdesc);
461 entry->priv->stats[type].cur -= entry->memdesc.size;
Amit Kushwaha7c843c22018-04-09 20:41:14 +0530462
Shrenuj Bansala419c792016-10-20 14:05:11 -0700463 spin_unlock(&entry->priv->mem_lock);
464
465 kgsl_mmu_put_gpuaddr(&entry->memdesc);
466
467 kgsl_process_private_put(entry->priv);
468
469 entry->priv = NULL;
470}
471
472/**
473 * kgsl_context_dump() - dump information about a draw context
474 * @device: KGSL device that owns the context
475 * @context: KGSL context to dump information about
476 *
477 * Dump specific information about the context to the kernel log. Used for
478 * fence timeout callbacks
479 */
480void kgsl_context_dump(struct kgsl_context *context)
481{
482 struct kgsl_device *device;
483
484 if (_kgsl_context_get(context) == 0)
485 return;
486
487 device = context->device;
488
489 if (kgsl_context_detached(context)) {
490 dev_err(device->dev, " context[%d]: context detached\n",
491 context->id);
492 } else if (device->ftbl->drawctxt_dump != NULL)
493 device->ftbl->drawctxt_dump(device, context);
494
495 kgsl_context_put(context);
496}
497EXPORT_SYMBOL(kgsl_context_dump);
498
499/* Allocate a new context ID */
500static int _kgsl_get_context_id(struct kgsl_device *device)
501{
502 int id;
503
504 idr_preload(GFP_KERNEL);
505 write_lock(&device->context_lock);
506 /* Allocate the slot but don't put a pointer in it yet */
507 id = idr_alloc(&device->context_idr, NULL, 1,
508 KGSL_MEMSTORE_MAX, GFP_NOWAIT);
509 write_unlock(&device->context_lock);
510 idr_preload_end();
511
512 return id;
513}
514
515/**
516 * kgsl_context_init() - helper to initialize kgsl_context members
517 * @dev_priv: the owner of the context
518 * @context: the newly created context struct, should be allocated by
519 * the device specific drawctxt_create function.
520 *
521 * This is a helper function for the device specific drawctxt_create
522 * function to initialize the common members of its context struct.
523 * If this function succeeds, reference counting is active in the context
524 * struct and the caller should kgsl_context_put() it on error.
525 * If it fails, the caller should just free the context structure
526 * it passed in.
527 */
528int kgsl_context_init(struct kgsl_device_private *dev_priv,
529 struct kgsl_context *context)
530{
531 struct kgsl_device *device = dev_priv->device;
532 char name[64];
533 int ret = 0, id;
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700534 struct kgsl_process_private *proc_priv = dev_priv->process_priv;
535
Deepak Kumare0d19f92018-03-05 16:51:25 +0530536 /*
537 * Read and increment the context count under lock to make sure
538 * no process goes beyond the specified context limit.
539 */
540 spin_lock(&proc_priv->ctxt_count_lock);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700541 if (atomic_read(&proc_priv->ctxt_count) > KGSL_MAX_CONTEXTS_PER_PROC) {
Rajesh Kemisetti7ac20532018-08-10 13:30:07 +0530542 KGSL_DRV_ERR_RATELIMIT(device,
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700543 "Per process context limit reached for pid %u",
544 dev_priv->process_priv->pid);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530545 spin_unlock(&proc_priv->ctxt_count_lock);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700546 return -ENOSPC;
547 }
548
549 atomic_inc(&proc_priv->ctxt_count);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530550 spin_unlock(&proc_priv->ctxt_count_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700551
552 id = _kgsl_get_context_id(device);
553 if (id == -ENOSPC) {
554 /*
555 * Before declaring that there are no contexts left try
556 * flushing the event workqueue just in case there are
557 * detached contexts waiting to finish
558 */
559
560 flush_workqueue(device->events_wq);
561 id = _kgsl_get_context_id(device);
562 }
563
564 if (id < 0) {
565 if (id == -ENOSPC)
566 KGSL_DRV_INFO(device,
567 "cannot have more than %zu contexts due to memstore limitation\n",
568 KGSL_MEMSTORE_MAX);
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700569 atomic_dec(&proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700570 return id;
571 }
572
573 context->id = id;
574
575 kref_init(&context->refcount);
576 /*
577 * Get a refernce to the process private so its not destroyed, until
578 * the context is destroyed. This will also prevent the pagetable
579 * from being destroyed
580 */
581 if (!kgsl_process_private_get(dev_priv->process_priv)) {
582 ret = -EBADF;
583 goto out;
584 }
585 context->device = dev_priv->device;
586 context->dev_priv = dev_priv;
587 context->proc_priv = dev_priv->process_priv;
588 context->tid = task_pid_nr(current);
589
590 ret = kgsl_sync_timeline_create(context);
Harshdeep Dhattb1273892017-06-01 13:12:07 -0600591 if (ret) {
592 kgsl_process_private_put(dev_priv->process_priv);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700593 goto out;
Harshdeep Dhattb1273892017-06-01 13:12:07 -0600594 }
Shrenuj Bansala419c792016-10-20 14:05:11 -0700595
596 snprintf(name, sizeof(name), "context-%d", id);
597 kgsl_add_event_group(&context->events, context, name,
598 kgsl_readtimestamp, context);
599
600out:
601 if (ret) {
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700602 atomic_dec(&proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700603 write_lock(&device->context_lock);
604 idr_remove(&dev_priv->device->context_idr, id);
605 write_unlock(&device->context_lock);
606 }
607
608 return ret;
609}
610EXPORT_SYMBOL(kgsl_context_init);
611
612/**
613 * kgsl_context_detach() - Release the "master" context reference
614 * @context: The context that will be detached
615 *
616 * This is called when a context becomes unusable, because userspace
617 * has requested for it to be destroyed. The context itself may
618 * exist a bit longer until its reference count goes to zero.
619 * Other code referencing the context can detect that it has been
620 * detached by checking the KGSL_CONTEXT_PRIV_DETACHED bit in
621 * context->priv.
622 */
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -0600623void kgsl_context_detach(struct kgsl_context *context)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700624{
625 struct kgsl_device *device;
626
627 if (context == NULL)
628 return;
629
630 /*
631 * Mark the context as detached to keep others from using
632 * the context before it gets fully removed, and to make sure
633 * we don't try to detach twice.
634 */
635 if (test_and_set_bit(KGSL_CONTEXT_PRIV_DETACHED, &context->priv))
636 return;
637
638 device = context->device;
639
640 trace_kgsl_context_detach(device, context);
641
642 context->device->ftbl->drawctxt_detach(context);
643
644 /*
645 * Cancel all pending events after the device-specific context is
646 * detached, to avoid possibly freeing memory while it is still
647 * in use by the GPU.
648 */
649 kgsl_cancel_events(device, &context->events);
650
651 /* Remove the event group from the list */
652 kgsl_del_event_group(&context->events);
653
Lynus Vazc031a9b2017-01-25 13:00:13 +0530654 kgsl_sync_timeline_put(context->ktimeline);
655
Shrenuj Bansala419c792016-10-20 14:05:11 -0700656 kgsl_context_put(context);
657}
658
659void
660kgsl_context_destroy(struct kref *kref)
661{
662 struct kgsl_context *context = container_of(kref, struct kgsl_context,
663 refcount);
664 struct kgsl_device *device = context->device;
665
666 trace_kgsl_context_destroy(device, context);
667
668 /*
669 * It's not safe to destroy the context if it's not detached as GPU
670 * may still be executing commands
671 */
672 BUG_ON(!kgsl_context_detached(context));
673
674 write_lock(&device->context_lock);
675 if (context->id != KGSL_CONTEXT_INVALID) {
676
677 /* Clear the timestamps in the memstore during destroy */
678 kgsl_sharedmem_writel(device, &device->memstore,
679 KGSL_MEMSTORE_OFFSET(context->id, soptimestamp), 0);
680 kgsl_sharedmem_writel(device, &device->memstore,
681 KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp), 0);
682
683 /* clear device power constraint */
684 if (context->id == device->pwrctrl.constraint.owner_id) {
685 trace_kgsl_constraint(device,
686 device->pwrctrl.constraint.type,
687 device->pwrctrl.active_pwrlevel,
688 0);
689 device->pwrctrl.constraint.type = KGSL_CONSTRAINT_NONE;
690 }
691
Harshdeep Dhatt410e83932017-12-12 14:56:20 -0700692 atomic_dec(&context->proc_priv->ctxt_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700693 idr_remove(&device->context_idr, context->id);
694 context->id = KGSL_CONTEXT_INVALID;
695 }
696 write_unlock(&device->context_lock);
697 kgsl_sync_timeline_destroy(context);
698 kgsl_process_private_put(context->proc_priv);
699
700 device->ftbl->drawctxt_destroy(context);
701}
702
703struct kgsl_device *kgsl_get_device(int dev_idx)
704{
705 int i;
706 struct kgsl_device *ret = NULL;
707
708 mutex_lock(&kgsl_driver.devlock);
709
710 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
711 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
712 ret = kgsl_driver.devp[i];
713 break;
714 }
715 }
716
717 mutex_unlock(&kgsl_driver.devlock);
718 return ret;
719}
720EXPORT_SYMBOL(kgsl_get_device);
721
722static struct kgsl_device *kgsl_get_minor(int minor)
723{
724 struct kgsl_device *ret = NULL;
725
726 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
727 return NULL;
728
729 mutex_lock(&kgsl_driver.devlock);
730 ret = kgsl_driver.devp[minor];
731 mutex_unlock(&kgsl_driver.devlock);
732
733 return ret;
734}
735
736/**
737 * kgsl_check_timestamp() - return true if the specified timestamp is retired
738 * @device: Pointer to the KGSL device to check
739 * @context: Pointer to the context for the timestamp
740 * @timestamp: The timestamp to compare
741 */
742int kgsl_check_timestamp(struct kgsl_device *device,
743 struct kgsl_context *context, unsigned int timestamp)
744{
745 unsigned int ts_processed;
746
747 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
748 &ts_processed);
749
750 return (timestamp_cmp(ts_processed, timestamp) >= 0);
751}
752EXPORT_SYMBOL(kgsl_check_timestamp);
753
754static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
755{
756 int status = -EINVAL;
757
758 if (!device)
759 return -EINVAL;
760
761 KGSL_PWR_WARN(device, "suspend start\n");
762
763 mutex_lock(&device->mutex);
764 status = kgsl_pwrctrl_change_state(device, KGSL_STATE_SUSPEND);
Deepak Kumar10e2bc52018-08-01 11:57:33 +0530765 if (status == 0 && device->state == KGSL_STATE_SUSPEND)
Deepak Kumar79908f52018-02-28 11:06:38 +0530766 device->ftbl->dispatcher_halt(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700767 mutex_unlock(&device->mutex);
768
769 KGSL_PWR_WARN(device, "suspend end\n");
770 return status;
771}
772
773static int kgsl_resume_device(struct kgsl_device *device)
774{
775 if (!device)
776 return -EINVAL;
777
778 KGSL_PWR_WARN(device, "resume start\n");
779 mutex_lock(&device->mutex);
780 if (device->state == KGSL_STATE_SUSPEND) {
Deepak Kumar79908f52018-02-28 11:06:38 +0530781 device->ftbl->dispatcher_unhalt(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700782 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
783 } else if (device->state != KGSL_STATE_INIT) {
784 /*
785 * This is an error situation,so wait for the device
786 * to idle and then put the device to SLUMBER state.
787 * This will put the device to the right state when
788 * we resume.
789 */
790 if (device->state == KGSL_STATE_ACTIVE)
791 device->ftbl->idle(device);
792 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
793 KGSL_PWR_ERR(device,
794 "resume invoked without a suspend\n");
795 }
796
797 mutex_unlock(&device->mutex);
798 KGSL_PWR_WARN(device, "resume end\n");
799 return 0;
800}
801
802static int kgsl_suspend(struct device *dev)
803{
804
805 pm_message_t arg = {0};
806 struct kgsl_device *device = dev_get_drvdata(dev);
807
808 return kgsl_suspend_device(device, arg);
809}
810
811static int kgsl_resume(struct device *dev)
812{
813 struct kgsl_device *device = dev_get_drvdata(dev);
814
815 return kgsl_resume_device(device);
816}
817
818static int kgsl_runtime_suspend(struct device *dev)
819{
820 return 0;
821}
822
823static int kgsl_runtime_resume(struct device *dev)
824{
825 return 0;
826}
827
828const struct dev_pm_ops kgsl_pm_ops = {
829 .suspend = kgsl_suspend,
830 .resume = kgsl_resume,
831 .runtime_suspend = kgsl_runtime_suspend,
832 .runtime_resume = kgsl_runtime_resume,
833};
834EXPORT_SYMBOL(kgsl_pm_ops);
835
836int kgsl_suspend_driver(struct platform_device *pdev,
837 pm_message_t state)
838{
839 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
840
841 return kgsl_suspend_device(device, state);
842}
843EXPORT_SYMBOL(kgsl_suspend_driver);
844
845int kgsl_resume_driver(struct platform_device *pdev)
846{
847 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
848
849 return kgsl_resume_device(device);
850}
851EXPORT_SYMBOL(kgsl_resume_driver);
852
853/**
854 * kgsl_destroy_process_private() - Cleanup function to free process private
855 * @kref: - Pointer to object being destroyed's kref struct
856 * Free struct object and all other resources attached to it.
857 * Since the function can be used when not all resources inside process
858 * private have been allocated, there is a check to (before each resource
859 * cleanup) see if the struct member being cleaned is in fact allocated or not.
860 * If the value is not NULL, resource is freed.
861 */
862static void kgsl_destroy_process_private(struct kref *kref)
863{
864 struct kgsl_process_private *private = container_of(kref,
865 struct kgsl_process_private, refcount);
866
867 idr_destroy(&private->mem_idr);
868 idr_destroy(&private->syncsource_idr);
869
870 /* When using global pagetables, do not detach global pagetable */
871 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
872 kgsl_mmu_putpagetable(private->pagetable);
873
874 kfree(private);
875}
876
877void
878kgsl_process_private_put(struct kgsl_process_private *private)
879{
880 if (private)
881 kref_put(&private->refcount, kgsl_destroy_process_private);
882}
883
884/**
885 * kgsl_process_private_find() - Find the process associated with the specified
886 * name
887 * @name: pid_t of the process to search for
888 * Return the process struct for the given ID.
889 */
890struct kgsl_process_private *kgsl_process_private_find(pid_t pid)
891{
892 struct kgsl_process_private *p, *private = NULL;
893
894 mutex_lock(&kgsl_driver.process_mutex);
895 list_for_each_entry(p, &kgsl_driver.process_list, list) {
896 if (p->pid == pid) {
897 if (kgsl_process_private_get(p))
898 private = p;
899 break;
900 }
901 }
902 mutex_unlock(&kgsl_driver.process_mutex);
903 return private;
904}
905
906static struct kgsl_process_private *kgsl_process_private_new(
907 struct kgsl_device *device)
908{
909 struct kgsl_process_private *private;
910 pid_t tgid = task_tgid_nr(current);
911
912 /* Search in the process list */
913 list_for_each_entry(private, &kgsl_driver.process_list, list) {
914 if (private->pid == tgid) {
915 if (!kgsl_process_private_get(private))
916 private = ERR_PTR(-EINVAL);
917 return private;
918 }
919 }
920
921 /* Create a new object */
922 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
923 if (private == NULL)
924 return ERR_PTR(-ENOMEM);
925
926 kref_init(&private->refcount);
927
928 private->pid = tgid;
929 get_task_comm(private->comm, current->group_leader);
930
931 spin_lock_init(&private->mem_lock);
932 spin_lock_init(&private->syncsource_lock);
Deepak Kumare0d19f92018-03-05 16:51:25 +0530933 spin_lock_init(&private->ctxt_count_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700934
935 idr_init(&private->mem_idr);
936 idr_init(&private->syncsource_idr);
937
938 /* Allocate a pagetable for the new process object */
939 private->pagetable = kgsl_mmu_getpagetable(&device->mmu, tgid);
940 if (IS_ERR(private->pagetable)) {
941 int err = PTR_ERR(private->pagetable);
942
943 idr_destroy(&private->mem_idr);
944 idr_destroy(&private->syncsource_idr);
945
946 kfree(private);
947 private = ERR_PTR(err);
948 }
949
950 return private;
951}
952
953static void process_release_memory(struct kgsl_process_private *private)
954{
955 struct kgsl_mem_entry *entry;
956 int next = 0;
957
958 while (1) {
959 spin_lock(&private->mem_lock);
960 entry = idr_get_next(&private->mem_idr, &next);
961 if (entry == NULL) {
962 spin_unlock(&private->mem_lock);
963 break;
964 }
965 /*
966 * If the free pending flag is not set it means that user space
967 * did not free it's reference to this entry, in that case
968 * free a reference to this entry, other references are from
969 * within kgsl so they will be freed eventually by kgsl
970 */
971 if (!entry->pending_free) {
972 entry->pending_free = 1;
973 spin_unlock(&private->mem_lock);
974 kgsl_mem_entry_put(entry);
975 } else {
976 spin_unlock(&private->mem_lock);
977 }
978 next = next + 1;
979 }
980}
981
Shrenuj Bansala419c792016-10-20 14:05:11 -0700982static void kgsl_process_private_close(struct kgsl_device_private *dev_priv,
983 struct kgsl_process_private *private)
984{
985 mutex_lock(&kgsl_driver.process_mutex);
986
987 if (--private->fd_count > 0) {
988 mutex_unlock(&kgsl_driver.process_mutex);
989 kgsl_process_private_put(private);
990 return;
991 }
992
993 /*
994 * If this is the last file on the process take down the debug
995 * directories and garbage collect any outstanding resources
996 */
997
998 kgsl_process_uninit_sysfs(private);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700999
Akhil P Oommenb5f1b822018-03-08 20:10:52 +05301000 /* Release all syncsource objects from process private */
1001 kgsl_syncsource_process_release_syncsources(private);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001002
1003 /* When using global pagetables, do not detach global pagetable */
1004 if (private->pagetable->name != KGSL_MMU_GLOBAL_PT)
1005 kgsl_mmu_detach_pagetable(private->pagetable);
1006
1007 /* Remove the process struct from the master list */
1008 list_del(&private->list);
1009
1010 /*
Lynus Vaz98ffb322017-07-27 15:13:09 +05301011 * Unlock the mutex before releasing the memory and the debugfs
1012 * nodes - this prevents deadlocks with the IOMMU and debugfs
1013 * locks.
Shrenuj Bansala419c792016-10-20 14:05:11 -07001014 */
1015 mutex_unlock(&kgsl_driver.process_mutex);
1016
1017 process_release_memory(private);
Lynus Vaz98ffb322017-07-27 15:13:09 +05301018 debugfs_remove_recursive(private->debug_root);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001019
1020 kgsl_process_private_put(private);
1021}
1022
1023
1024static struct kgsl_process_private *kgsl_process_private_open(
1025 struct kgsl_device *device)
1026{
1027 struct kgsl_process_private *private;
1028
1029 mutex_lock(&kgsl_driver.process_mutex);
1030 private = kgsl_process_private_new(device);
1031
1032 if (IS_ERR(private))
1033 goto done;
1034
1035 /*
1036 * If this is a new process create the debug directories and add it to
1037 * the process list
1038 */
1039
1040 if (private->fd_count++ == 0) {
1041 kgsl_process_init_sysfs(device, private);
1042 kgsl_process_init_debugfs(private);
1043
1044 list_add(&private->list, &kgsl_driver.process_list);
1045 }
1046
1047done:
1048 mutex_unlock(&kgsl_driver.process_mutex);
1049 return private;
1050}
1051
1052static int kgsl_close_device(struct kgsl_device *device)
1053{
1054 int result = 0;
1055
1056 mutex_lock(&device->mutex);
Kyle Piefer89d64fe2017-05-15 09:15:43 -07001057 device->open_count--;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001058 if (device->open_count == 0) {
1059
1060 /* Wait for the active count to go to 0 */
1061 kgsl_active_count_wait(device, 0);
1062
1063 /* Fail if the wait times out */
1064 BUG_ON(atomic_read(&device->active_cnt) > 0);
1065
1066 result = kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1067 }
1068 mutex_unlock(&device->mutex);
1069 return result;
1070
1071}
1072
1073static void device_release_contexts(struct kgsl_device_private *dev_priv)
1074{
1075 struct kgsl_device *device = dev_priv->device;
1076 struct kgsl_context *context;
1077 int next = 0;
1078 int result = 0;
1079
1080 while (1) {
1081 read_lock(&device->context_lock);
1082 context = idr_get_next(&device->context_idr, &next);
1083
1084 if (context == NULL) {
1085 read_unlock(&device->context_lock);
1086 break;
1087 } else if (context->dev_priv == dev_priv) {
1088 /*
1089 * Hold a reference to the context in case somebody
1090 * tries to put it while we are detaching
1091 */
1092 result = _kgsl_context_get(context);
1093 }
1094 read_unlock(&device->context_lock);
1095
1096 if (result) {
1097 kgsl_context_detach(context);
1098 kgsl_context_put(context);
1099 result = 0;
1100 }
1101
1102 next = next + 1;
1103 }
1104}
1105
1106static int kgsl_release(struct inode *inodep, struct file *filep)
1107{
1108 struct kgsl_device_private *dev_priv = filep->private_data;
1109 struct kgsl_device *device = dev_priv->device;
1110 int result;
1111
1112 filep->private_data = NULL;
1113
1114 /* Release the contexts for the file */
1115 device_release_contexts(dev_priv);
1116
1117 /* Close down the process wide resources for the file */
1118 kgsl_process_private_close(dev_priv, dev_priv->process_priv);
1119
Harshdeep Dhattf61e3b52014-12-15 13:45:19 -07001120 /* Destroy the device-specific structure */
1121 device->ftbl->device_private_destroy(dev_priv);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001122
1123 result = kgsl_close_device(device);
1124 pm_runtime_put(&device->pdev->dev);
1125
1126 return result;
1127}
1128
1129static int kgsl_open_device(struct kgsl_device *device)
1130{
1131 int result = 0;
1132
1133 mutex_lock(&device->mutex);
1134 if (device->open_count == 0) {
1135 /*
1136 * active_cnt special case: we are starting up for the first
1137 * time, so use this sequence instead of the kgsl_pwrctrl_wake()
1138 * which will be called by kgsl_active_count_get().
1139 */
1140 atomic_inc(&device->active_cnt);
1141 kgsl_sharedmem_set(device, &device->memstore, 0, 0,
1142 device->memstore.size);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001143
1144 result = device->ftbl->init(device);
1145 if (result)
1146 goto err;
1147
1148 result = device->ftbl->start(device, 0);
1149 if (result)
1150 goto err;
1151 /*
1152 * Make sure the gates are open, so they don't block until
1153 * we start suspend or FT.
1154 */
1155 complete_all(&device->hwaccess_gate);
1156 kgsl_pwrctrl_change_state(device, KGSL_STATE_ACTIVE);
1157 kgsl_active_count_put(device);
1158 }
1159 device->open_count++;
1160err:
1161 if (result) {
1162 kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1163 atomic_dec(&device->active_cnt);
1164 }
1165
1166 mutex_unlock(&device->mutex);
1167 return result;
1168}
1169
1170static int kgsl_open(struct inode *inodep, struct file *filep)
1171{
1172 int result;
1173 struct kgsl_device_private *dev_priv;
1174 struct kgsl_device *device;
1175 unsigned int minor = iminor(inodep);
1176
1177 device = kgsl_get_minor(minor);
1178 BUG_ON(device == NULL);
1179
1180 result = pm_runtime_get_sync(&device->pdev->dev);
1181 if (result < 0) {
1182 KGSL_DRV_ERR(device,
1183 "Runtime PM: Unable to wake up the device, rc = %d\n",
1184 result);
1185 return result;
1186 }
1187 result = 0;
1188
Harshdeep Dhattf61e3b52014-12-15 13:45:19 -07001189 dev_priv = device->ftbl->device_private_create();
Shrenuj Bansala419c792016-10-20 14:05:11 -07001190 if (dev_priv == NULL) {
1191 result = -ENOMEM;
1192 goto err;
1193 }
1194
1195 dev_priv->device = device;
1196 filep->private_data = dev_priv;
1197
1198 result = kgsl_open_device(device);
1199 if (result)
1200 goto err;
1201
1202 /*
1203 * Get file (per process) private struct. This must be done
1204 * after the first start so that the global pagetable mappings
1205 * are set up before we create the per-process pagetable.
1206 */
1207 dev_priv->process_priv = kgsl_process_private_open(device);
1208 if (IS_ERR(dev_priv->process_priv)) {
1209 result = PTR_ERR(dev_priv->process_priv);
1210 kgsl_close_device(device);
1211 goto err;
1212 }
1213
1214err:
1215 if (result) {
1216 filep->private_data = NULL;
1217 kfree(dev_priv);
1218 pm_runtime_put(&device->pdev->dev);
1219 }
1220 return result;
1221}
1222
1223#define GPUADDR_IN_MEMDESC(_val, _memdesc) \
1224 (((_val) >= (_memdesc)->gpuaddr) && \
1225 ((_val) < ((_memdesc)->gpuaddr + (_memdesc)->size)))
1226
1227/**
1228 * kgsl_sharedmem_find() - Find a gpu memory allocation
1229 *
1230 * @private: private data for the process to check.
1231 * @gpuaddr: start address of the region
1232 *
1233 * Find a gpu allocation. Caller must kgsl_mem_entry_put()
1234 * the returned entry when finished using it.
1235 */
1236struct kgsl_mem_entry * __must_check
1237kgsl_sharedmem_find(struct kgsl_process_private *private, uint64_t gpuaddr)
1238{
1239 int ret = 0, id;
1240 struct kgsl_mem_entry *entry = NULL;
1241
1242 if (!private)
1243 return NULL;
1244
1245 if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, gpuaddr))
1246 return NULL;
1247
1248 spin_lock(&private->mem_lock);
1249 idr_for_each_entry(&private->mem_idr, entry, id) {
1250 if (GPUADDR_IN_MEMDESC(gpuaddr, &entry->memdesc)) {
Deepak Kumar0a4ef64e2017-05-08 15:29:03 +05301251 if (!entry->pending_free)
1252 ret = kgsl_mem_entry_get(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001253 break;
1254 }
1255 }
1256 spin_unlock(&private->mem_lock);
1257
1258 return (ret == 0) ? NULL : entry;
1259}
1260EXPORT_SYMBOL(kgsl_sharedmem_find);
1261
1262struct kgsl_mem_entry * __must_check
1263kgsl_sharedmem_find_id_flags(struct kgsl_process_private *process,
1264 unsigned int id, uint64_t flags)
1265{
1266 int count = 0;
1267 struct kgsl_mem_entry *entry;
1268
1269 spin_lock(&process->mem_lock);
1270 entry = idr_find(&process->mem_idr, id);
1271 if (entry)
1272 if (!entry->pending_free &&
1273 (flags & entry->memdesc.flags) == flags)
1274 count = kgsl_mem_entry_get(entry);
1275 spin_unlock(&process->mem_lock);
1276
1277 return (count == 0) ? NULL : entry;
1278}
1279
1280/**
1281 * kgsl_sharedmem_find_id() - find a memory entry by id
1282 * @process: the owning process
1283 * @id: id to find
1284 *
1285 * @returns - the mem_entry or NULL
1286 *
1287 * Caller must kgsl_mem_entry_put() the returned entry, when finished using
1288 * it.
1289 */
1290struct kgsl_mem_entry * __must_check
1291kgsl_sharedmem_find_id(struct kgsl_process_private *process, unsigned int id)
1292{
1293 return kgsl_sharedmem_find_id_flags(process, id, 0);
1294}
1295
1296/**
1297 * kgsl_mem_entry_unset_pend() - Unset the pending free flag of an entry
1298 * @entry - The memory entry
1299 */
1300static inline void kgsl_mem_entry_unset_pend(struct kgsl_mem_entry *entry)
1301{
1302 if (entry == NULL)
1303 return;
1304 spin_lock(&entry->priv->mem_lock);
1305 entry->pending_free = 0;
1306 spin_unlock(&entry->priv->mem_lock);
1307}
1308
1309/**
1310 * kgsl_mem_entry_set_pend() - Set the pending free flag of a memory entry
1311 * @entry - The memory entry
1312 *
1313 * @returns - true if pending flag was 0 else false
1314 *
1315 * This function will set the pending free flag if it is previously unset. Used
1316 * to prevent race condition between ioctls calling free/freememontimestamp
1317 * on the same entry. Whichever thread set's the flag first will do the free.
1318 */
1319static inline bool kgsl_mem_entry_set_pend(struct kgsl_mem_entry *entry)
1320{
1321 bool ret = false;
1322
1323 if (entry == NULL)
1324 return false;
1325
1326 spin_lock(&entry->priv->mem_lock);
1327 if (!entry->pending_free) {
1328 entry->pending_free = 1;
1329 ret = true;
1330 }
1331 spin_unlock(&entry->priv->mem_lock);
1332 return ret;
1333}
1334
1335/*call all ioctl sub functions with driver locked*/
1336long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
1337 unsigned int cmd, void *data)
1338{
1339 int result = 0;
1340 struct kgsl_device_getproperty *param = data;
1341
1342 switch (param->type) {
1343 case KGSL_PROP_VERSION:
1344 {
1345 struct kgsl_version version;
1346
1347 if (param->sizebytes != sizeof(version)) {
1348 result = -EINVAL;
1349 break;
1350 }
1351
1352 version.drv_major = KGSL_VERSION_MAJOR;
1353 version.drv_minor = KGSL_VERSION_MINOR;
1354 version.dev_major = dev_priv->device->ver_major;
1355 version.dev_minor = dev_priv->device->ver_minor;
1356
1357 if (copy_to_user(param->value, &version, sizeof(version)))
1358 result = -EFAULT;
1359
1360 break;
1361 }
1362 case KGSL_PROP_GPU_RESET_STAT:
1363 {
1364 /* Return reset status of given context and clear it */
1365 uint32_t id;
1366 struct kgsl_context *context;
1367
1368 if (param->sizebytes != sizeof(unsigned int)) {
1369 result = -EINVAL;
1370 break;
1371 }
1372 /* We expect the value passed in to contain the context id */
1373 if (copy_from_user(&id, param->value,
1374 sizeof(unsigned int))) {
1375 result = -EFAULT;
1376 break;
1377 }
1378 context = kgsl_context_get_owner(dev_priv, id);
1379 if (!context) {
1380 result = -EINVAL;
1381 break;
1382 }
1383 /*
1384 * Copy the reset status to value which also serves as
1385 * the out parameter
1386 */
1387 if (copy_to_user(param->value, &(context->reset_status),
1388 sizeof(unsigned int)))
1389 result = -EFAULT;
1390 else {
1391 /* Clear reset status once its been queried */
1392 context->reset_status = KGSL_CTX_STAT_NO_ERROR;
1393 }
1394
1395 kgsl_context_put(context);
1396 break;
1397 }
Sunil Khatrib055b6b2018-07-19 17:10:39 +05301398 case KGSL_PROP_SECURE_BUFFER_ALIGNMENT:
1399 {
1400 unsigned int align;
1401
1402 if (param->sizebytes != sizeof(unsigned int)) {
1403 result = -EINVAL;
1404 break;
1405 }
1406 /*
1407 * XPUv2 impose the constraint of 1MB memory alignment,
1408 * on the other hand Hypervisor does not have such
1409 * constraints. So driver should fulfill such
1410 * requirements when allocating secure memory.
1411 */
1412 align = MMU_FEATURE(&dev_priv->device->mmu,
1413 KGSL_MMU_HYP_SECURE_ALLOC) ? PAGE_SIZE : SZ_1M;
1414
1415 if (copy_to_user(param->value, &align, sizeof(align)))
1416 result = -EFAULT;
1417
1418 break;
1419 }
Sunil Khatric4d4c2d2018-08-10 11:46:58 +05301420 case KGSL_PROP_SECURE_CTXT_SUPPORT:
1421 {
1422 unsigned int secure_ctxt;
1423
1424 if (param->sizebytes != sizeof(unsigned int)) {
1425 result = -EINVAL;
1426 break;
1427 }
1428
1429 secure_ctxt = dev_priv->device->mmu.secured ? 1 : 0;
1430
1431 if (copy_to_user(param->value, &secure_ctxt,
1432 sizeof(secure_ctxt)))
1433 result = -EFAULT;
1434
1435 break;
1436 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07001437 default:
1438 if (is_compat_task())
1439 result = dev_priv->device->ftbl->getproperty_compat(
1440 dev_priv->device, param->type,
1441 param->value, param->sizebytes);
1442 else
1443 result = dev_priv->device->ftbl->getproperty(
1444 dev_priv->device, param->type,
1445 param->value, param->sizebytes);
1446 }
1447
1448
1449 return result;
1450}
1451
1452long kgsl_ioctl_device_setproperty(struct kgsl_device_private *dev_priv,
1453 unsigned int cmd, void *data)
1454{
1455 int result = 0;
1456 /* The getproperty struct is reused for setproperty too */
1457 struct kgsl_device_getproperty *param = data;
1458
1459 /* Reroute to compat version if coming from compat_ioctl */
1460 if (is_compat_task())
1461 result = dev_priv->device->ftbl->setproperty_compat(
1462 dev_priv, param->type, param->value,
1463 param->sizebytes);
1464 else if (dev_priv->device->ftbl->setproperty)
1465 result = dev_priv->device->ftbl->setproperty(
1466 dev_priv, param->type, param->value,
1467 param->sizebytes);
1468
1469 return result;
1470}
1471
1472long kgsl_ioctl_device_waittimestamp_ctxtid(
1473 struct kgsl_device_private *dev_priv, unsigned int cmd,
1474 void *data)
1475{
1476 struct kgsl_device_waittimestamp_ctxtid *param = data;
1477 struct kgsl_device *device = dev_priv->device;
1478 long result = -EINVAL;
1479 unsigned int temp_cur_ts = 0;
1480 struct kgsl_context *context;
1481
1482 context = kgsl_context_get_owner(dev_priv, param->context_id);
1483 if (context == NULL)
1484 return result;
1485
1486 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1487 &temp_cur_ts);
1488
1489 trace_kgsl_waittimestamp_entry(device, context->id, temp_cur_ts,
1490 param->timestamp, param->timeout);
1491
1492 result = device->ftbl->waittimestamp(device, context, param->timestamp,
1493 param->timeout);
1494
1495 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
1496 &temp_cur_ts);
1497 trace_kgsl_waittimestamp_exit(device, temp_cur_ts, result);
1498
1499 kgsl_context_put(context);
1500
1501 return result;
1502}
1503
Tarun Karra2b8b3632016-11-14 16:38:27 -08001504static inline bool _check_context_is_sparse(struct kgsl_context *context,
1505 uint64_t flags)
1506{
1507 if ((context->flags & KGSL_CONTEXT_SPARSE) ||
1508 (flags & KGSL_DRAWOBJ_SPARSE))
1509 return true;
1510
1511 return false;
1512}
1513
1514
Shrenuj Bansala419c792016-10-20 14:05:11 -07001515long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
1516 unsigned int cmd, void *data)
1517{
1518 struct kgsl_ringbuffer_issueibcmds *param = data;
1519 struct kgsl_device *device = dev_priv->device;
1520 struct kgsl_context *context;
1521 struct kgsl_drawobj *drawobj;
1522 struct kgsl_drawobj_cmd *cmdobj;
1523 long result = -EINVAL;
1524
1525 /* The legacy functions don't support synchronization commands */
1526 if ((param->flags & (KGSL_DRAWOBJ_SYNC | KGSL_DRAWOBJ_MARKER)))
1527 return -EINVAL;
1528
1529 /* Sanity check the number of IBs */
1530 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST &&
1531 (param->numibs == 0 || param->numibs > KGSL_MAX_NUMIBS))
1532 return -EINVAL;
1533
1534 /* Get the context */
1535 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1536 if (context == NULL)
1537 return -EINVAL;
1538
Tarun Karra2b8b3632016-11-14 16:38:27 -08001539 if (_check_context_is_sparse(context, param->flags)) {
1540 kgsl_context_put(context);
1541 return -EINVAL;
1542 }
1543
Shrenuj Bansala419c792016-10-20 14:05:11 -07001544 cmdobj = kgsl_drawobj_cmd_create(device, context, param->flags,
1545 CMDOBJ_TYPE);
1546 if (IS_ERR(cmdobj)) {
1547 kgsl_context_put(context);
1548 return PTR_ERR(cmdobj);
1549 }
1550
1551 drawobj = DRAWOBJ(cmdobj);
1552
1553 if (param->flags & KGSL_DRAWOBJ_SUBMIT_IB_LIST)
1554 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1555 (void __user *) param->ibdesc_addr,
1556 param->numibs);
1557 else {
1558 struct kgsl_ibdesc ibdesc;
1559 /* Ultra legacy path */
1560
1561 ibdesc.gpuaddr = param->ibdesc_addr;
1562 ibdesc.sizedwords = param->numibs;
1563 ibdesc.ctrl = 0;
1564
1565 result = kgsl_drawobj_cmd_add_ibdesc(device, cmdobj, &ibdesc);
1566 }
1567
1568 if (result == 0)
1569 result = dev_priv->device->ftbl->queue_cmds(dev_priv, context,
1570 &drawobj, 1, &param->timestamp);
1571
1572 /*
1573 * -EPROTO is a "success" error - it just tells the user that the
1574 * context had previously faulted
1575 */
1576 if (result && result != -EPROTO)
1577 kgsl_drawobj_destroy(drawobj);
1578
1579 kgsl_context_put(context);
1580 return result;
1581}
1582
1583/* Returns 0 on failure. Returns command type(s) on success */
1584static unsigned int _process_command_input(struct kgsl_device *device,
1585 unsigned int flags, unsigned int numcmds,
1586 unsigned int numobjs, unsigned int numsyncs)
1587{
1588 if (numcmds > KGSL_MAX_NUMIBS ||
1589 numobjs > KGSL_MAX_NUMIBS ||
1590 numsyncs > KGSL_MAX_SYNCPOINTS)
1591 return 0;
1592
1593 /*
1594 * The SYNC bit is supposed to identify a dummy sync object
1595 * so warn the user if they specified any IBs with it.
1596 * A MARKER command can either have IBs or not but if the
1597 * command has 0 IBs it is automatically assumed to be a marker.
1598 */
1599
1600 /* If they specify the flag, go with what they say */
1601 if (flags & KGSL_DRAWOBJ_MARKER)
1602 return MARKEROBJ_TYPE;
1603 else if (flags & KGSL_DRAWOBJ_SYNC)
1604 return SYNCOBJ_TYPE;
1605
1606 /* If not, deduce what they meant */
1607 if (numsyncs && numcmds)
1608 return SYNCOBJ_TYPE | CMDOBJ_TYPE;
1609 else if (numsyncs)
1610 return SYNCOBJ_TYPE;
1611 else if (numcmds)
1612 return CMDOBJ_TYPE;
1613 else if (numcmds == 0)
1614 return MARKEROBJ_TYPE;
1615
1616 return 0;
1617}
1618
1619long kgsl_ioctl_submit_commands(struct kgsl_device_private *dev_priv,
1620 unsigned int cmd, void *data)
1621{
1622 struct kgsl_submit_commands *param = data;
1623 struct kgsl_device *device = dev_priv->device;
1624 struct kgsl_context *context;
1625 struct kgsl_drawobj *drawobj[2];
1626 unsigned int type;
1627 long result;
1628 unsigned int i = 0;
1629
1630 type = _process_command_input(device, param->flags, param->numcmds, 0,
1631 param->numsyncs);
1632 if (!type)
1633 return -EINVAL;
1634
1635 context = kgsl_context_get_owner(dev_priv, param->context_id);
1636 if (context == NULL)
1637 return -EINVAL;
1638
Tarun Karra2b8b3632016-11-14 16:38:27 -08001639 if (_check_context_is_sparse(context, param->flags)) {
1640 kgsl_context_put(context);
1641 return -EINVAL;
1642 }
1643
Shrenuj Bansala419c792016-10-20 14:05:11 -07001644 if (type & SYNCOBJ_TYPE) {
1645 struct kgsl_drawobj_sync *syncobj =
1646 kgsl_drawobj_sync_create(device, context);
1647 if (IS_ERR(syncobj)) {
1648 result = PTR_ERR(syncobj);
1649 goto done;
1650 }
1651
1652 drawobj[i++] = DRAWOBJ(syncobj);
1653
1654 result = kgsl_drawobj_sync_add_syncpoints(device, syncobj,
1655 param->synclist, param->numsyncs);
1656 if (result)
1657 goto done;
1658 }
1659
1660 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1661 struct kgsl_drawobj_cmd *cmdobj =
1662 kgsl_drawobj_cmd_create(device,
1663 context, param->flags, type);
1664 if (IS_ERR(cmdobj)) {
1665 result = PTR_ERR(cmdobj);
1666 goto done;
1667 }
1668
1669 drawobj[i++] = DRAWOBJ(cmdobj);
1670
1671 result = kgsl_drawobj_cmd_add_ibdesc_list(device, cmdobj,
1672 param->cmdlist, param->numcmds);
1673 if (result)
1674 goto done;
1675
1676 /* If no profiling buffer was specified, clear the flag */
1677 if (cmdobj->profiling_buf_entry == NULL)
Lynus Vazeb7af682017-04-17 18:36:01 +05301678 DRAWOBJ(cmdobj)->flags &=
1679 ~(unsigned long)KGSL_DRAWOBJ_PROFILING;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001680 }
1681
1682 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1683 i, &param->timestamp);
1684
1685done:
1686 /*
1687 * -EPROTO is a "success" error - it just tells the user that the
1688 * context had previously faulted
1689 */
1690 if (result && result != -EPROTO)
1691 while (i--)
1692 kgsl_drawobj_destroy(drawobj[i]);
1693
1694
1695 kgsl_context_put(context);
1696 return result;
1697}
1698
1699long kgsl_ioctl_gpu_command(struct kgsl_device_private *dev_priv,
1700 unsigned int cmd, void *data)
1701{
1702 struct kgsl_gpu_command *param = data;
1703 struct kgsl_device *device = dev_priv->device;
1704 struct kgsl_context *context;
1705 struct kgsl_drawobj *drawobj[2];
1706 unsigned int type;
1707 long result;
1708 unsigned int i = 0;
1709
1710 type = _process_command_input(device, param->flags, param->numcmds,
1711 param->numobjs, param->numsyncs);
1712 if (!type)
1713 return -EINVAL;
1714
1715 context = kgsl_context_get_owner(dev_priv, param->context_id);
1716 if (context == NULL)
1717 return -EINVAL;
1718
Tarun Karra2b8b3632016-11-14 16:38:27 -08001719 if (_check_context_is_sparse(context, param->flags)) {
1720 kgsl_context_put(context);
1721 return -EINVAL;
1722 }
1723
Shrenuj Bansala419c792016-10-20 14:05:11 -07001724 if (type & SYNCOBJ_TYPE) {
1725 struct kgsl_drawobj_sync *syncobj =
1726 kgsl_drawobj_sync_create(device, context);
1727
1728 if (IS_ERR(syncobj)) {
1729 result = PTR_ERR(syncobj);
1730 goto done;
1731 }
1732
1733 drawobj[i++] = DRAWOBJ(syncobj);
1734
1735 result = kgsl_drawobj_sync_add_synclist(device, syncobj,
1736 to_user_ptr(param->synclist),
1737 param->syncsize, param->numsyncs);
1738 if (result)
1739 goto done;
1740 }
1741
1742 if (type & (CMDOBJ_TYPE | MARKEROBJ_TYPE)) {
1743 struct kgsl_drawobj_cmd *cmdobj =
1744 kgsl_drawobj_cmd_create(device,
1745 context, param->flags, type);
1746
1747 if (IS_ERR(cmdobj)) {
1748 result = PTR_ERR(cmdobj);
1749 goto done;
1750 }
1751
1752 drawobj[i++] = DRAWOBJ(cmdobj);
1753
1754 result = kgsl_drawobj_cmd_add_cmdlist(device, cmdobj,
1755 to_user_ptr(param->cmdlist),
1756 param->cmdsize, param->numcmds);
1757 if (result)
1758 goto done;
1759
1760 result = kgsl_drawobj_cmd_add_memlist(device, cmdobj,
1761 to_user_ptr(param->objlist),
1762 param->objsize, param->numobjs);
1763 if (result)
1764 goto done;
1765
1766 /* If no profiling buffer was specified, clear the flag */
1767 if (cmdobj->profiling_buf_entry == NULL)
Lynus Vazeb7af682017-04-17 18:36:01 +05301768 DRAWOBJ(cmdobj)->flags &=
1769 ~(unsigned long)KGSL_DRAWOBJ_PROFILING;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001770 }
1771
1772 result = device->ftbl->queue_cmds(dev_priv, context, drawobj,
1773 i, &param->timestamp);
1774
1775done:
1776 /*
1777 * -EPROTO is a "success" error - it just tells the user that the
1778 * context had previously faulted
1779 */
1780 if (result && result != -EPROTO)
1781 while (i--)
1782 kgsl_drawobj_destroy(drawobj[i]);
1783
1784 kgsl_context_put(context);
1785 return result;
1786}
1787
1788long kgsl_ioctl_cmdstream_readtimestamp_ctxtid(struct kgsl_device_private
1789 *dev_priv, unsigned int cmd,
1790 void *data)
1791{
1792 struct kgsl_cmdstream_readtimestamp_ctxtid *param = data;
1793 struct kgsl_device *device = dev_priv->device;
1794 struct kgsl_context *context;
1795 long result = -EINVAL;
1796
1797 mutex_lock(&device->mutex);
1798 context = kgsl_context_get_owner(dev_priv, param->context_id);
1799
1800 if (context) {
1801 result = kgsl_readtimestamp(device, context,
1802 param->type, &param->timestamp);
1803
1804 trace_kgsl_readtimestamp(device, context->id,
1805 param->type, param->timestamp);
1806 }
1807
1808 kgsl_context_put(context);
1809 mutex_unlock(&device->mutex);
1810 return result;
1811}
1812
1813long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1814 unsigned int cmd, void *data)
1815{
1816 int result = 0;
1817 struct kgsl_drawctxt_create *param = data;
1818 struct kgsl_context *context = NULL;
1819 struct kgsl_device *device = dev_priv->device;
1820
1821 context = device->ftbl->drawctxt_create(dev_priv, &param->flags);
1822 if (IS_ERR(context)) {
1823 result = PTR_ERR(context);
1824 goto done;
1825 }
1826 trace_kgsl_context_create(dev_priv->device, context, param->flags);
1827
1828 /* Commit the pointer to the context in context_idr */
1829 write_lock(&device->context_lock);
1830 idr_replace(&device->context_idr, context, context->id);
Sunil Khatridd90d682017-04-06 18:28:31 +05301831 param->drawctxt_id = context->id;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001832 write_unlock(&device->context_lock);
1833
Shrenuj Bansala419c792016-10-20 14:05:11 -07001834done:
1835 return result;
1836}
1837
1838long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1839 unsigned int cmd, void *data)
1840{
1841 struct kgsl_drawctxt_destroy *param = data;
1842 struct kgsl_context *context;
1843
1844 context = kgsl_context_get_owner(dev_priv, param->drawctxt_id);
1845 if (context == NULL)
1846 return -EINVAL;
1847
1848 kgsl_context_detach(context);
1849 kgsl_context_put(context);
1850
1851 return 0;
1852}
1853
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -06001854long gpumem_free_entry(struct kgsl_mem_entry *entry)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001855{
Shrenuj Bansala419c792016-10-20 14:05:11 -07001856 if (!kgsl_mem_entry_set_pend(entry))
1857 return -EBUSY;
1858
1859 trace_kgsl_mem_free(entry);
Lynus Vaz3fe67582017-11-08 15:22:32 +05301860 kgsl_memfree_add(entry->priv->pid,
1861 entry->memdesc.pagetable ?
1862 entry->memdesc.pagetable->name : 0,
1863 entry->memdesc.gpuaddr, entry->memdesc.size,
1864 entry->memdesc.flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001865
1866 kgsl_mem_entry_put(entry);
1867
1868 return 0;
1869}
1870
1871static void gpumem_free_func(struct kgsl_device *device,
1872 struct kgsl_event_group *group, void *priv, int ret)
1873{
1874 struct kgsl_context *context = group->context;
1875 struct kgsl_mem_entry *entry = priv;
1876 unsigned int timestamp;
1877
1878 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &timestamp);
1879
1880 /* Free the memory for all event types */
1881 trace_kgsl_mem_timestamp_free(device, entry, KGSL_CONTEXT_ID(context),
1882 timestamp, 0);
Lynus Vaz3fe67582017-11-08 15:22:32 +05301883 kgsl_memfree_add(entry->priv->pid,
1884 entry->memdesc.pagetable ?
1885 entry->memdesc.pagetable->name : 0,
1886 entry->memdesc.gpuaddr, entry->memdesc.size,
1887 entry->memdesc.flags);
1888
Shrenuj Bansala419c792016-10-20 14:05:11 -07001889 kgsl_mem_entry_put(entry);
1890}
1891
1892static long gpumem_free_entry_on_timestamp(struct kgsl_device *device,
1893 struct kgsl_mem_entry *entry,
1894 struct kgsl_context *context, unsigned int timestamp)
1895{
1896 int ret;
1897 unsigned int temp;
1898
1899 if (!kgsl_mem_entry_set_pend(entry))
1900 return -EBUSY;
1901
1902 kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &temp);
1903 trace_kgsl_mem_timestamp_queue(device, entry, context->id, temp,
1904 timestamp);
1905 ret = kgsl_add_event(device, &context->events,
1906 timestamp, gpumem_free_func, entry);
1907
1908 if (ret)
1909 kgsl_mem_entry_unset_pend(entry);
1910
1911 return ret;
1912}
1913
1914long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1915 unsigned int cmd, void *data)
1916{
1917 struct kgsl_sharedmem_free *param = data;
1918 struct kgsl_process_private *private = dev_priv->process_priv;
1919 struct kgsl_mem_entry *entry;
1920 long ret;
1921
1922 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
1923 if (entry == NULL)
1924 return -EINVAL;
1925
1926 ret = gpumem_free_entry(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05301927 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001928
1929 return ret;
1930}
1931
1932long kgsl_ioctl_gpumem_free_id(struct kgsl_device_private *dev_priv,
1933 unsigned int cmd, void *data)
1934{
1935 struct kgsl_gpumem_free_id *param = data;
1936 struct kgsl_process_private *private = dev_priv->process_priv;
1937 struct kgsl_mem_entry *entry;
1938 long ret;
1939
1940 entry = kgsl_sharedmem_find_id(private, param->id);
1941 if (entry == NULL)
1942 return -EINVAL;
1943
1944 ret = gpumem_free_entry(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05301945 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001946
1947 return ret;
1948}
1949
1950static long gpuobj_free_on_timestamp(struct kgsl_device_private *dev_priv,
1951 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1952{
1953 struct kgsl_gpu_event_timestamp event;
1954 struct kgsl_context *context;
1955 long ret;
1956
1957 memset(&event, 0, sizeof(event));
1958
1959 ret = _copy_from_user(&event, to_user_ptr(param->priv),
1960 sizeof(event), param->len);
1961 if (ret)
1962 return ret;
1963
1964 if (event.context_id == 0)
1965 return -EINVAL;
1966
1967 context = kgsl_context_get_owner(dev_priv, event.context_id);
1968 if (context == NULL)
1969 return -EINVAL;
1970
1971 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry, context,
1972 event.timestamp);
1973
1974 kgsl_context_put(context);
1975 return ret;
1976}
1977
Lynus Vaz27da44d2017-07-26 13:50:10 +05301978static bool gpuobj_free_fence_func(void *priv)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001979{
1980 struct kgsl_mem_entry *entry = priv;
1981
Lynus Vaz3fe67582017-11-08 15:22:32 +05301982 trace_kgsl_mem_free(entry);
1983 kgsl_memfree_add(entry->priv->pid,
1984 entry->memdesc.pagetable ?
1985 entry->memdesc.pagetable->name : 0,
1986 entry->memdesc.gpuaddr, entry->memdesc.size,
1987 entry->memdesc.flags);
1988
Hareesh Gundu615439d2017-06-16 17:06:57 +05301989 INIT_WORK(&entry->work, _deferred_put);
1990 queue_work(kgsl_driver.mem_workqueue, &entry->work);
Lynus Vaz27da44d2017-07-26 13:50:10 +05301991 return true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001992}
1993
1994static long gpuobj_free_on_fence(struct kgsl_device_private *dev_priv,
1995 struct kgsl_mem_entry *entry, struct kgsl_gpuobj_free *param)
1996{
Lynus Vazc031a9b2017-01-25 13:00:13 +05301997 struct kgsl_sync_fence_cb *handle;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001998 struct kgsl_gpu_event_fence event;
1999 long ret;
2000
2001 if (!kgsl_mem_entry_set_pend(entry))
2002 return -EBUSY;
2003
2004 memset(&event, 0, sizeof(event));
2005
2006 ret = _copy_from_user(&event, to_user_ptr(param->priv),
2007 sizeof(event), param->len);
2008 if (ret) {
2009 kgsl_mem_entry_unset_pend(entry);
2010 return ret;
2011 }
2012
2013 if (event.fd < 0) {
2014 kgsl_mem_entry_unset_pend(entry);
2015 return -EINVAL;
2016 }
2017
2018 handle = kgsl_sync_fence_async_wait(event.fd,
Puranam V G Tejaswi0ebbdcd2018-12-13 15:47:00 +05302019 gpuobj_free_fence_func, entry, NULL);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002020
Shrenuj Bansala419c792016-10-20 14:05:11 -07002021 if (IS_ERR(handle)) {
2022 kgsl_mem_entry_unset_pend(entry);
2023 return PTR_ERR(handle);
2024 }
2025
Lynus Vaz3fe67582017-11-08 15:22:32 +05302026 /* if handle is NULL the fence has already signaled */
2027 if (handle == NULL)
2028 gpuobj_free_fence_func(entry);
2029
Shrenuj Bansala419c792016-10-20 14:05:11 -07002030 return 0;
2031}
2032
2033long kgsl_ioctl_gpuobj_free(struct kgsl_device_private *dev_priv,
2034 unsigned int cmd, void *data)
2035{
2036 struct kgsl_gpuobj_free *param = data;
2037 struct kgsl_process_private *private = dev_priv->process_priv;
2038 struct kgsl_mem_entry *entry;
2039 long ret;
2040
2041 entry = kgsl_sharedmem_find_id(private, param->id);
2042 if (entry == NULL)
2043 return -EINVAL;
2044
2045 /* If no event is specified then free immediately */
2046 if (!(param->flags & KGSL_GPUOBJ_FREE_ON_EVENT))
2047 ret = gpumem_free_entry(entry);
2048 else if (param->type == KGSL_GPU_EVENT_TIMESTAMP)
2049 ret = gpuobj_free_on_timestamp(dev_priv, entry, param);
2050 else if (param->type == KGSL_GPU_EVENT_FENCE)
2051 ret = gpuobj_free_on_fence(dev_priv, entry, param);
2052 else
2053 ret = -EINVAL;
2054
Hareesh Gundu615439d2017-06-16 17:06:57 +05302055 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002056 return ret;
2057}
2058
2059long kgsl_ioctl_cmdstream_freememontimestamp_ctxtid(
2060 struct kgsl_device_private *dev_priv,
2061 unsigned int cmd, void *data)
2062{
2063 struct kgsl_cmdstream_freememontimestamp_ctxtid *param = data;
2064 struct kgsl_context *context = NULL;
2065 struct kgsl_mem_entry *entry;
2066 long ret = -EINVAL;
2067
2068 if (param->type != KGSL_TIMESTAMP_RETIRED)
2069 return -EINVAL;
2070
2071 context = kgsl_context_get_owner(dev_priv, param->context_id);
2072 if (context == NULL)
2073 return -EINVAL;
2074
2075 entry = kgsl_sharedmem_find(dev_priv->process_priv,
2076 (uint64_t) param->gpuaddr);
2077 if (entry == NULL) {
2078 kgsl_context_put(context);
2079 return -EINVAL;
2080 }
2081
2082 ret = gpumem_free_entry_on_timestamp(dev_priv->device, entry,
2083 context, param->timestamp);
2084
2085 kgsl_mem_entry_put(entry);
2086 kgsl_context_put(context);
2087
2088 return ret;
2089}
2090
Shrenuj Bansala419c792016-10-20 14:05:11 -07002091static int check_vma_flags(struct vm_area_struct *vma,
2092 unsigned int flags)
2093{
2094 unsigned long flags_requested = (VM_READ | VM_WRITE);
2095
2096 if (flags & KGSL_MEMFLAGS_GPUREADONLY)
Lynus Vazeb7af682017-04-17 18:36:01 +05302097 flags_requested &= ~(unsigned long)VM_WRITE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002098
2099 if ((vma->vm_flags & flags_requested) == flags_requested)
2100 return 0;
2101
2102 return -EFAULT;
2103}
2104
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002105static int check_vma(unsigned long hostptr, u64 size)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002106{
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002107 struct vm_area_struct *vma;
2108 unsigned long cur = hostptr;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002109
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002110 while (cur < (hostptr + size)) {
2111 vma = find_vma(current->mm, cur);
2112 if (!vma)
2113 return false;
2114
2115 /* Don't remap memory that we already own */
2116 if (vma->vm_file && vma->vm_file->f_op == &kgsl_fops)
2117 return false;
2118
2119 cur = vma->vm_end;
2120 }
2121
2122 return true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002123}
2124
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302125static int memdesc_sg_virt(struct kgsl_memdesc *memdesc, unsigned long useraddr)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002126{
2127 int ret = 0;
2128 long npages = 0, i;
2129 size_t sglen = (size_t) (memdesc->size / PAGE_SIZE);
2130 struct page **pages = NULL;
2131 int write = ((memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY) ? 0 :
2132 FOLL_WRITE);
2133
2134 if (sglen == 0 || sglen >= LONG_MAX)
2135 return -EINVAL;
2136
2137 pages = kgsl_malloc(sglen * sizeof(struct page *));
2138 if (pages == NULL)
2139 return -ENOMEM;
2140
2141 memdesc->sgt = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
2142 if (memdesc->sgt == NULL) {
2143 ret = -ENOMEM;
2144 goto out;
2145 }
2146
2147 down_read(&current->mm->mmap_sem);
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302148 if (!check_vma(useraddr, memdesc->size)) {
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002149 up_read(&current->mm->mmap_sem);
2150 ret = -EFAULT;
2151 goto out;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002152 }
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002153
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302154 npages = get_user_pages(useraddr,
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002155 sglen, write, pages, NULL);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002156 up_read(&current->mm->mmap_sem);
2157
Jordan Crouse9ea6cad2020-09-24 09:05:53 -06002158 ret = (npages < 0) ? (int)npages : 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002159 if (ret)
2160 goto out;
2161
2162 if ((unsigned long) npages != sglen) {
2163 ret = -EINVAL;
2164 goto out;
2165 }
2166
2167 ret = sg_alloc_table_from_pages(memdesc->sgt, pages, npages,
2168 0, memdesc->size, GFP_KERNEL);
2169out:
2170 if (ret) {
2171 for (i = 0; i < npages; i++)
2172 put_page(pages[i]);
2173
2174 kfree(memdesc->sgt);
2175 memdesc->sgt = NULL;
2176 }
2177 kgsl_free(pages);
2178 return ret;
2179}
2180
2181static int kgsl_setup_anon_useraddr(struct kgsl_pagetable *pagetable,
2182 struct kgsl_mem_entry *entry, unsigned long hostptr,
2183 size_t offset, size_t size)
2184{
2185 /* Map an anonymous memory chunk */
2186
Neeraja Pb8350672021-01-07 20:48:09 +05302187 int ret;
2188
Shrenuj Bansala419c792016-10-20 14:05:11 -07002189 if (size == 0 || offset != 0 ||
2190 !IS_ALIGNED(size, PAGE_SIZE))
2191 return -EINVAL;
2192
2193 entry->memdesc.pagetable = pagetable;
2194 entry->memdesc.size = (uint64_t) size;
Lynus Vazeb7af682017-04-17 18:36:01 +05302195 entry->memdesc.flags |= (uint64_t)KGSL_MEMFLAGS_USERMEM_ADDR;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002196
2197 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002198
2199 /* Register the address in the database */
2200 ret = kgsl_mmu_set_svm_region(pagetable,
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302201 (uint64_t) hostptr, (uint64_t) size);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002202
2203 if (ret)
2204 return ret;
2205
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302206 entry->memdesc.gpuaddr = (uint64_t) hostptr;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002207 }
2208
Neeraja Pb8350672021-01-07 20:48:09 +05302209 ret = memdesc_sg_virt(&entry->memdesc, hostptr);
2210
2211 if (ret && kgsl_memdesc_use_cpu_map(&entry->memdesc))
2212 kgsl_mmu_put_gpuaddr(&entry->memdesc);
2213
2214 return ret;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002215}
2216
2217#ifdef CONFIG_DMA_SHARED_BUFFER
2218static int match_file(const void *p, struct file *file, unsigned int fd)
2219{
2220 /*
2221 * We must return fd + 1 because iterate_fd stops searching on
2222 * non-zero return, but 0 is a valid fd.
2223 */
2224 return (p == file) ? (fd + 1) : 0;
2225}
2226
2227static void _setup_cache_mode(struct kgsl_mem_entry *entry,
2228 struct vm_area_struct *vma)
2229{
Lynus Vazeb7af682017-04-17 18:36:01 +05302230 uint64_t mode;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002231 pgprot_t pgprot = vma->vm_page_prot;
2232
2233 if (pgprot_val(pgprot) == pgprot_val(pgprot_noncached(pgprot)))
2234 mode = KGSL_CACHEMODE_UNCACHED;
2235 else if (pgprot_val(pgprot) == pgprot_val(pgprot_writecombine(pgprot)))
2236 mode = KGSL_CACHEMODE_WRITECOMBINE;
2237 else
2238 mode = KGSL_CACHEMODE_WRITEBACK;
2239
2240 entry->memdesc.flags |= (mode << KGSL_CACHEMODE_SHIFT);
2241}
2242
2243static int kgsl_setup_dma_buf(struct kgsl_device *device,
2244 struct kgsl_pagetable *pagetable,
2245 struct kgsl_mem_entry *entry,
2246 struct dma_buf *dmabuf);
2247
2248static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2249 struct kgsl_pagetable *pagetable,
2250 struct kgsl_mem_entry *entry, unsigned long hostptr)
2251{
2252 struct vm_area_struct *vma;
2253 struct dma_buf *dmabuf = NULL;
2254 int ret;
2255
2256 /*
2257 * Find the VMA containing this pointer and figure out if it
2258 * is a dma-buf.
2259 */
2260 down_read(&current->mm->mmap_sem);
2261 vma = find_vma(current->mm, hostptr);
2262
2263 if (vma && vma->vm_file) {
2264 int fd;
2265
2266 ret = check_vma_flags(vma, entry->memdesc.flags);
2267 if (ret) {
2268 up_read(&current->mm->mmap_sem);
2269 return ret;
2270 }
2271
2272 /*
2273 * Check to see that this isn't our own memory that we have
2274 * already mapped
2275 */
2276 if (vma->vm_file->f_op == &kgsl_fops) {
2277 up_read(&current->mm->mmap_sem);
2278 return -EFAULT;
2279 }
2280
2281 /* Look for the fd that matches this the vma file */
2282 fd = iterate_fd(current->files, 0, match_file, vma->vm_file);
2283 if (fd != 0)
2284 dmabuf = dma_buf_get(fd - 1);
2285 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002286
Sunil Khatri51a79372017-07-06 15:09:35 +05302287 if (IS_ERR_OR_NULL(dmabuf)) {
2288 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002289 return dmabuf ? PTR_ERR(dmabuf) : -ENODEV;
Sunil Khatri51a79372017-07-06 15:09:35 +05302290 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002291
2292 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2293 if (ret) {
2294 dma_buf_put(dmabuf);
Sunil Khatri51a79372017-07-06 15:09:35 +05302295 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002296 return ret;
2297 }
2298
Jordan Crouse6bce65c2020-12-28 16:06:42 +05302299 /* Setup the cache mode for cache operations */
Shrenuj Bansala419c792016-10-20 14:05:11 -07002300 _setup_cache_mode(entry, vma);
Sunil Khatri51a79372017-07-06 15:09:35 +05302301 up_read(&current->mm->mmap_sem);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002302 return 0;
2303}
2304#else
2305static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
2306 struct kgsl_pagetable *pagetable,
2307 struct kgsl_mem_entry *entry, unsigned long hostptr)
2308{
2309 return -ENODEV;
2310}
2311#endif
2312
2313static int kgsl_setup_useraddr(struct kgsl_device *device,
2314 struct kgsl_pagetable *pagetable,
2315 struct kgsl_mem_entry *entry,
2316 unsigned long hostptr, size_t offset, size_t size)
2317{
2318 int ret;
2319
2320 if (hostptr == 0 || !IS_ALIGNED(hostptr, PAGE_SIZE))
2321 return -EINVAL;
2322
2323 /* Try to set up a dmabuf - if it returns -ENODEV assume anonymous */
2324 ret = kgsl_setup_dmabuf_useraddr(device, pagetable, entry, hostptr);
2325 if (ret != -ENODEV)
2326 return ret;
2327
2328 /* Okay - lets go legacy */
2329 return kgsl_setup_anon_useraddr(pagetable, entry,
2330 hostptr, offset, size);
2331}
2332
2333static long _gpuobj_map_useraddr(struct kgsl_device *device,
2334 struct kgsl_pagetable *pagetable,
2335 struct kgsl_mem_entry *entry,
2336 struct kgsl_gpuobj_import *param)
2337{
Archana Obannagarice60fec2017-09-08 20:35:28 +05302338 struct kgsl_gpuobj_import_useraddr useraddr = {0};
Shrenuj Bansala419c792016-10-20 14:05:11 -07002339 int ret;
2340
2341 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2342 | KGSL_CACHEMODE_MASK
2343 | KGSL_MEMTYPE_MASK
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07002344 | KGSL_MEMFLAGS_FORCE_32BIT
2345 | KGSL_MEMFLAGS_IOCOHERENT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002346
2347 /* Specifying SECURE is an explicit error */
2348 if (param->flags & KGSL_MEMFLAGS_SECURE)
2349 return -ENOTSUPP;
2350
2351 ret = _copy_from_user(&useraddr,
2352 to_user_ptr(param->priv), sizeof(useraddr),
2353 param->priv_len);
2354 if (ret)
2355 return ret;
2356
2357 /* Verify that the virtaddr and len are within bounds */
2358 if (useraddr.virtaddr > ULONG_MAX)
2359 return -EINVAL;
2360
2361 return kgsl_setup_useraddr(device, pagetable, entry,
2362 (unsigned long) useraddr.virtaddr, 0, param->priv_len);
2363}
2364
2365#ifdef CONFIG_DMA_SHARED_BUFFER
2366static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2367 struct kgsl_pagetable *pagetable,
2368 struct kgsl_mem_entry *entry,
2369 struct kgsl_gpuobj_import *param,
2370 int *fd)
2371{
2372 struct kgsl_gpuobj_import_dma_buf buf;
2373 struct dma_buf *dmabuf;
2374 int ret;
2375
2376 /*
2377 * If content protection is not enabled and secure buffer
2378 * is requested to be mapped return error.
2379 */
2380 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2381 if (!kgsl_mmu_is_secured(&device->mmu)) {
2382 dev_WARN_ONCE(device->dev, 1,
2383 "Secure buffer not supported");
2384 return -ENOTSUPP;
2385 }
2386
2387 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2388 }
2389
2390 ret = _copy_from_user(&buf, to_user_ptr(param->priv),
2391 sizeof(buf), param->priv_len);
2392 if (ret)
2393 return ret;
2394
2395 if (buf.fd < 0)
2396 return -EINVAL;
2397
2398 *fd = buf.fd;
2399 dmabuf = dma_buf_get(buf.fd);
2400
2401 if (IS_ERR_OR_NULL(dmabuf))
2402 return (dmabuf == NULL) ? -EINVAL : PTR_ERR(dmabuf);
2403
2404 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2405 if (ret)
2406 dma_buf_put(dmabuf);
2407
2408 return ret;
2409}
2410#else
2411static long _gpuobj_map_dma_buf(struct kgsl_device *device,
2412 struct kgsl_pagetable *pagetable,
2413 struct kgsl_mem_entry *entry,
2414 struct kgsl_gpuobj_import *param,
2415 int *fd)
2416{
2417 return -EINVAL;
2418}
2419#endif
2420
2421long kgsl_ioctl_gpuobj_import(struct kgsl_device_private *dev_priv,
2422 unsigned int cmd, void *data)
2423{
2424 struct kgsl_process_private *private = dev_priv->process_priv;
2425 struct kgsl_gpuobj_import *param = data;
2426 struct kgsl_mem_entry *entry;
2427 int ret, fd = -1;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002428
2429 entry = kgsl_mem_entry_create();
2430 if (entry == NULL)
2431 return -ENOMEM;
2432
2433 param->flags &= KGSL_MEMFLAGS_GPUREADONLY
2434 | KGSL_MEMTYPE_MASK
2435 | KGSL_MEMALIGN_MASK
2436 | KGSL_MEMFLAGS_USE_CPU_MAP
2437 | KGSL_MEMFLAGS_SECURE
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07002438 | KGSL_MEMFLAGS_FORCE_32BIT
2439 | KGSL_MEMFLAGS_IOCOHERENT;
2440
Deepak Kumarcf056d12018-04-17 15:59:42 +05302441 if (kgsl_is_compat_task())
2442 param->flags |= KGSL_MEMFLAGS_FORCE_32BIT;
2443
Lynus Vaz90d98b52018-04-09 14:45:36 +05302444 kgsl_memdesc_init(dev_priv->device, &entry->memdesc, param->flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002445 if (param->type == KGSL_USER_MEM_TYPE_ADDR)
2446 ret = _gpuobj_map_useraddr(dev_priv->device, private->pagetable,
2447 entry, param);
2448 else if (param->type == KGSL_USER_MEM_TYPE_DMABUF)
2449 ret = _gpuobj_map_dma_buf(dev_priv->device, private->pagetable,
2450 entry, param, &fd);
2451 else
2452 ret = -ENOTSUPP;
2453
2454 if (ret)
2455 goto out;
2456
2457 if (entry->memdesc.size >= SZ_1M)
2458 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2459 else if (entry->memdesc.size >= SZ_64K)
2460 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64K));
2461
2462 param->flags = entry->memdesc.flags;
2463
2464 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
2465 if (ret)
2466 goto unmap;
2467
2468 param->id = entry->id;
2469
2470 KGSL_STATS_ADD(entry->memdesc.size, &kgsl_driver.stats.mapped,
2471 &kgsl_driver.stats.mapped_max);
2472
2473 kgsl_process_add_stats(private,
2474 kgsl_memdesc_usermem_type(&entry->memdesc),
2475 entry->memdesc.size);
2476
2477 trace_kgsl_mem_map(entry, fd);
2478
2479 kgsl_mem_entry_commit_process(entry);
Tarun Karra24d3fe12017-04-05 15:23:03 -07002480
2481 /* Put the extra ref from kgsl_mem_entry_create() */
2482 kgsl_mem_entry_put(entry);
2483
Shrenuj Bansala419c792016-10-20 14:05:11 -07002484 return 0;
2485
2486unmap:
Kamal Agrawal8f0fb822020-08-19 10:25:15 +05302487 if (kgsl_memdesc_usermem_type(&entry->memdesc) == KGSL_MEM_ENTRY_ION) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002488 kgsl_destroy_ion(entry->priv_data);
2489 entry->memdesc.sgt = NULL;
2490 }
2491
2492 kgsl_sharedmem_free(&entry->memdesc);
2493
2494out:
2495 kfree(entry);
2496 return ret;
2497}
2498
2499static long _map_usermem_addr(struct kgsl_device *device,
2500 struct kgsl_pagetable *pagetable, struct kgsl_mem_entry *entry,
2501 unsigned long hostptr, size_t offset, size_t size)
2502{
2503 if (!MMU_FEATURE(&device->mmu, KGSL_MMU_PAGED))
2504 return -EINVAL;
2505
2506 /* No CPU mapped buffer could ever be secure */
2507 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE)
2508 return -EINVAL;
2509
2510 return kgsl_setup_useraddr(device, pagetable, entry, hostptr,
2511 offset, size);
2512}
2513
2514#ifdef CONFIG_DMA_SHARED_BUFFER
2515static int _map_usermem_dma_buf(struct kgsl_device *device,
2516 struct kgsl_pagetable *pagetable,
2517 struct kgsl_mem_entry *entry,
2518 unsigned int fd)
2519{
2520 int ret;
2521 struct dma_buf *dmabuf;
2522
2523 /*
2524 * If content protection is not enabled and secure buffer
2525 * is requested to be mapped return error.
2526 */
2527
2528 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
2529 if (!kgsl_mmu_is_secured(&device->mmu)) {
2530 dev_WARN_ONCE(device->dev, 1,
2531 "Secure buffer not supported");
2532 return -EINVAL;
2533 }
2534
2535 entry->memdesc.priv |= KGSL_MEMDESC_SECURE;
2536 }
2537
2538 dmabuf = dma_buf_get(fd);
2539 if (IS_ERR_OR_NULL(dmabuf)) {
2540 ret = PTR_ERR(dmabuf);
2541 return ret ? ret : -EINVAL;
2542 }
2543 ret = kgsl_setup_dma_buf(device, pagetable, entry, dmabuf);
2544 if (ret)
2545 dma_buf_put(dmabuf);
2546 return ret;
2547}
2548#else
2549static int _map_usermem_dma_buf(struct kgsl_device *device,
2550 struct kgsl_pagetable *pagetable,
2551 struct kgsl_mem_entry *entry,
2552 unsigned int fd)
2553{
2554 return -EINVAL;
2555}
2556#endif
2557
2558#ifdef CONFIG_DMA_SHARED_BUFFER
2559static int kgsl_setup_dma_buf(struct kgsl_device *device,
2560 struct kgsl_pagetable *pagetable,
2561 struct kgsl_mem_entry *entry,
2562 struct dma_buf *dmabuf)
2563{
2564 int ret = 0;
2565 struct scatterlist *s;
2566 struct sg_table *sg_table;
2567 struct dma_buf_attachment *attach = NULL;
2568 struct kgsl_dma_buf_meta *meta;
2569
2570 meta = kzalloc(sizeof(*meta), GFP_KERNEL);
2571 if (!meta)
2572 return -ENOMEM;
2573
2574 attach = dma_buf_attach(dmabuf, device->dev);
2575 if (IS_ERR_OR_NULL(attach)) {
2576 ret = attach ? PTR_ERR(attach) : -EINVAL;
2577 goto out;
2578 }
2579
2580 meta->dmabuf = dmabuf;
2581 meta->attach = attach;
2582
2583 attach->priv = entry;
2584
2585 entry->priv_data = meta;
2586 entry->memdesc.pagetable = pagetable;
2587 entry->memdesc.size = 0;
2588 /* USE_CPU_MAP is not impemented for ION. */
2589 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
Lynus Vazeb7af682017-04-17 18:36:01 +05302590 entry->memdesc.flags |= (uint64_t)KGSL_MEMFLAGS_USERMEM_ION;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002591
2592 sg_table = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
2593
2594 if (IS_ERR_OR_NULL(sg_table)) {
2595 ret = PTR_ERR(sg_table);
2596 goto out;
2597 }
2598
2599 meta->table = sg_table;
2600 entry->priv_data = meta;
2601 entry->memdesc.sgt = sg_table;
2602
2603 /* Calculate the size of the memdesc from the sglist */
2604 for (s = entry->memdesc.sgt->sgl; s != NULL; s = sg_next(s)) {
2605 int priv = (entry->memdesc.priv & KGSL_MEMDESC_SECURE) ? 1 : 0;
2606
2607 /*
2608 * Check that each chunk of of the sg table matches the secure
2609 * flag.
2610 */
2611
2612 if (PagePrivate(sg_page(s)) != priv) {
2613 ret = -EPERM;
2614 goto out;
2615 }
2616
2617 entry->memdesc.size += (uint64_t) s->length;
2618 }
2619
2620 entry->memdesc.size = PAGE_ALIGN(entry->memdesc.size);
2621
2622out:
2623 if (ret) {
2624 if (!IS_ERR_OR_NULL(attach))
2625 dma_buf_detach(dmabuf, attach);
2626
2627
2628 kfree(meta);
2629 }
2630
2631 return ret;
2632}
2633#endif
2634
2635#ifdef CONFIG_DMA_SHARED_BUFFER
2636void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2637 int *egl_surface_count, int *egl_image_count)
2638{
2639 struct kgsl_dma_buf_meta *meta = entry->priv_data;
2640 struct dma_buf *dmabuf = meta->dmabuf;
2641 struct dma_buf_attachment *mem_entry_buf_attachment = meta->attach;
2642 struct device *buf_attachment_dev = mem_entry_buf_attachment->dev;
2643 struct dma_buf_attachment *attachment = NULL;
2644
2645 mutex_lock(&dmabuf->lock);
2646 list_for_each_entry(attachment, &dmabuf->attachments, node) {
2647 struct kgsl_mem_entry *scan_mem_entry = NULL;
2648
2649 if (attachment->dev != buf_attachment_dev)
2650 continue;
2651
2652 scan_mem_entry = attachment->priv;
2653 if (!scan_mem_entry)
2654 continue;
2655
2656 switch (kgsl_memdesc_get_memtype(&scan_mem_entry->memdesc)) {
2657 case KGSL_MEMTYPE_EGL_SURFACE:
2658 (*egl_surface_count)++;
2659 break;
2660 case KGSL_MEMTYPE_EGL_IMAGE:
2661 (*egl_image_count)++;
2662 break;
2663 }
2664 }
2665 mutex_unlock(&dmabuf->lock);
2666}
2667#else
2668void kgsl_get_egl_counts(struct kgsl_mem_entry *entry,
2669 int *egl_surface_count, int *egl_image_count)
2670{
2671}
2672#endif
2673
2674long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
2675 unsigned int cmd, void *data)
2676{
2677 int result = -EINVAL;
2678 struct kgsl_map_user_mem *param = data;
2679 struct kgsl_mem_entry *entry = NULL;
2680 struct kgsl_process_private *private = dev_priv->process_priv;
2681 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
2682 unsigned int memtype;
Lynus Vaz90d98b52018-04-09 14:45:36 +05302683 uint64_t flags;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002684
2685 /*
2686 * If content protection is not enabled and secure buffer
2687 * is requested to be mapped return error.
2688 */
2689
2690 if (param->flags & KGSL_MEMFLAGS_SECURE) {
2691 /* Log message and return if context protection isn't enabled */
2692 if (!kgsl_mmu_is_secured(mmu)) {
2693 dev_WARN_ONCE(dev_priv->device->dev, 1,
2694 "Secure buffer not supported");
2695 return -EOPNOTSUPP;
2696 }
2697
2698 /* Can't use CPU map with secure buffers */
2699 if (param->flags & KGSL_MEMFLAGS_USE_CPU_MAP)
2700 return -EINVAL;
2701 }
2702
2703 entry = kgsl_mem_entry_create();
2704
2705 if (entry == NULL)
2706 return -ENOMEM;
2707
2708 /*
2709 * Convert from enum value to KGSL_MEM_ENTRY value, so that
2710 * we can use the latter consistently everywhere.
2711 */
2712 memtype = param->memtype + 1;
2713
2714 /*
2715 * Mask off unknown flags from userspace. This way the caller can
2716 * check if a flag is supported by looking at the returned flags.
2717 * Note: CACHEMODE is ignored for this call. Caching should be
2718 * determined by type of allocation being mapped.
2719 */
Lynus Vaz90d98b52018-04-09 14:45:36 +05302720 flags = param->flags & (KGSL_MEMFLAGS_GPUREADONLY
2721 | KGSL_MEMTYPE_MASK
2722 | KGSL_MEMALIGN_MASK
2723 | KGSL_MEMFLAGS_USE_CPU_MAP
2724 | KGSL_MEMFLAGS_SECURE
2725 | KGSL_MEMFLAGS_IOCOHERENT);
Deepak Kumarcf056d12018-04-17 15:59:42 +05302726
2727 if (kgsl_is_compat_task())
Lynus Vaz90d98b52018-04-09 14:45:36 +05302728 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002729
Lynus Vaz90d98b52018-04-09 14:45:36 +05302730 kgsl_memdesc_init(dev_priv->device, &entry->memdesc, flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002731
2732 switch (memtype) {
2733 case KGSL_MEM_ENTRY_USER:
2734 result = _map_usermem_addr(dev_priv->device, private->pagetable,
2735 entry, param->hostptr, param->offset, param->len);
2736 break;
2737 case KGSL_MEM_ENTRY_ION:
2738 if (param->offset != 0)
2739 result = -EINVAL;
2740 else
2741 result = _map_usermem_dma_buf(dev_priv->device,
2742 private->pagetable, entry, param->fd);
2743 break;
2744 default:
2745 result = -EOPNOTSUPP;
2746 break;
2747 }
2748
2749 if (result)
2750 goto error;
2751
2752 if ((param->flags & KGSL_MEMFLAGS_SECURE) &&
2753 (entry->memdesc.size & mmu->secure_align_mask)) {
2754 result = -EINVAL;
2755 goto error_attach;
2756 }
2757
2758 if (entry->memdesc.size >= SZ_2M)
2759 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_2M));
2760 else if (entry->memdesc.size >= SZ_1M)
2761 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M));
2762 else if (entry->memdesc.size >= SZ_64K)
2763 kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64));
2764
2765 /* echo back flags */
2766 param->flags = (unsigned int) entry->memdesc.flags;
2767
2768 result = kgsl_mem_entry_attach_process(dev_priv->device, private,
2769 entry);
2770 if (result)
2771 goto error_attach;
2772
2773 /* Adjust the returned value for a non 4k aligned offset */
2774 param->gpuaddr = (unsigned long)
2775 entry->memdesc.gpuaddr + (param->offset & PAGE_MASK);
2776
2777 KGSL_STATS_ADD(param->len, &kgsl_driver.stats.mapped,
2778 &kgsl_driver.stats.mapped_max);
2779
2780 kgsl_process_add_stats(private,
2781 kgsl_memdesc_usermem_type(&entry->memdesc), param->len);
2782
2783 trace_kgsl_mem_map(entry, param->fd);
2784
2785 kgsl_mem_entry_commit_process(entry);
Tarun Karra24d3fe12017-04-05 15:23:03 -07002786
2787 /* Put the extra ref from kgsl_mem_entry_create() */
2788 kgsl_mem_entry_put(entry);
2789
Shrenuj Bansala419c792016-10-20 14:05:11 -07002790 return result;
2791
2792error_attach:
Kamal Agrawal8f0fb822020-08-19 10:25:15 +05302793 switch (kgsl_memdesc_usermem_type(&entry->memdesc)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002794 case KGSL_MEM_ENTRY_ION:
2795 kgsl_destroy_ion(entry->priv_data);
2796 entry->memdesc.sgt = NULL;
2797 break;
2798 default:
2799 break;
2800 }
2801 kgsl_sharedmem_free(&entry->memdesc);
2802error:
2803 /* Clear gpuaddr here so userspace doesn't get any wrong ideas */
2804 param->gpuaddr = 0;
2805
2806 kfree(entry);
2807 return result;
2808}
2809
2810static int _kgsl_gpumem_sync_cache(struct kgsl_mem_entry *entry,
2811 uint64_t offset, uint64_t length, unsigned int op)
2812{
2813 int ret = 0;
2814 int cacheop;
2815 int mode;
2816
Akhil P Oommen4323d4ca2017-06-21 12:54:18 +05302817 /* Cache ops are not allowed on secure memory */
2818 if (entry->memdesc.flags & KGSL_MEMFLAGS_SECURE)
2819 return 0;
2820
Shrenuj Bansala419c792016-10-20 14:05:11 -07002821 /*
2822 * Flush is defined as (clean | invalidate). If both bits are set, then
2823 * do a flush, otherwise check for the individual bits and clean or inv
2824 * as requested
2825 */
2826
2827 if ((op & KGSL_GPUMEM_CACHE_FLUSH) == KGSL_GPUMEM_CACHE_FLUSH)
2828 cacheop = KGSL_CACHE_OP_FLUSH;
2829 else if (op & KGSL_GPUMEM_CACHE_CLEAN)
2830 cacheop = KGSL_CACHE_OP_CLEAN;
2831 else if (op & KGSL_GPUMEM_CACHE_INV)
2832 cacheop = KGSL_CACHE_OP_INV;
2833 else {
2834 ret = -EINVAL;
2835 goto done;
2836 }
2837
2838 if (!(op & KGSL_GPUMEM_CACHE_RANGE)) {
2839 offset = 0;
2840 length = entry->memdesc.size;
2841 }
2842
2843 mode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2844 if (mode != KGSL_CACHEMODE_UNCACHED
2845 && mode != KGSL_CACHEMODE_WRITECOMBINE) {
2846 trace_kgsl_mem_sync_cache(entry, offset, length, op);
2847 ret = kgsl_cache_range_op(&entry->memdesc, offset,
2848 length, cacheop);
2849 }
2850
2851done:
2852 return ret;
2853}
2854
2855/* New cache sync function - supports both directions (clean and invalidate) */
2856
2857long kgsl_ioctl_gpumem_sync_cache(struct kgsl_device_private *dev_priv,
2858 unsigned int cmd, void *data)
2859{
2860 struct kgsl_gpumem_sync_cache *param = data;
2861 struct kgsl_process_private *private = dev_priv->process_priv;
2862 struct kgsl_mem_entry *entry = NULL;
2863 long ret;
2864
2865 if (param->id != 0)
2866 entry = kgsl_sharedmem_find_id(private, param->id);
2867 else if (param->gpuaddr != 0)
2868 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
2869
2870 if (entry == NULL)
2871 return -EINVAL;
2872
2873 ret = _kgsl_gpumem_sync_cache(entry, (uint64_t) param->offset,
2874 (uint64_t) param->length, param->op);
2875 kgsl_mem_entry_put(entry);
2876 return ret;
2877}
2878
2879static int mem_id_cmp(const void *_a, const void *_b)
2880{
2881 const unsigned int *a = _a, *b = _b;
2882
2883 if (*a == *b)
2884 return 0;
2885 return (*a > *b) ? 1 : -1;
2886}
2887
2888#ifdef CONFIG_ARM64
2889/* Do not support full flush on ARM64 targets */
2890static inline bool check_full_flush(size_t size, int op)
2891{
2892 return false;
2893}
2894#else
2895/* Support full flush if the size is bigger than the threshold */
2896static inline bool check_full_flush(size_t size, int op)
2897{
2898 /* If we exceed the breakeven point, flush the entire cache */
2899 bool ret = (kgsl_driver.full_cache_threshold != 0) &&
2900 (size >= kgsl_driver.full_cache_threshold) &&
2901 (op == KGSL_GPUMEM_CACHE_FLUSH);
Maria Yuceafc602017-09-26 15:45:02 +08002902 if (ret)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002903 flush_cache_all();
Shrenuj Bansala419c792016-10-20 14:05:11 -07002904 return ret;
2905}
2906#endif
2907
2908long kgsl_ioctl_gpumem_sync_cache_bulk(struct kgsl_device_private *dev_priv,
2909 unsigned int cmd, void *data)
2910{
2911 int i;
2912 struct kgsl_gpumem_sync_cache_bulk *param = data;
2913 struct kgsl_process_private *private = dev_priv->process_priv;
2914 unsigned int id, last_id = 0, *id_list = NULL, actual_count = 0;
2915 struct kgsl_mem_entry **entries = NULL;
2916 long ret = 0;
2917 uint64_t op_size = 0;
2918 bool full_flush = false;
2919
2920 if (param->id_list == NULL || param->count == 0
2921 || param->count > (PAGE_SIZE / sizeof(unsigned int)))
2922 return -EINVAL;
2923
2924 id_list = kcalloc(param->count, sizeof(unsigned int), GFP_KERNEL);
2925 if (id_list == NULL)
2926 return -ENOMEM;
2927
2928 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
2929 if (entries == NULL) {
2930 ret = -ENOMEM;
2931 goto end;
2932 }
2933
2934 if (copy_from_user(id_list, param->id_list,
2935 param->count * sizeof(unsigned int))) {
2936 ret = -EFAULT;
2937 goto end;
2938 }
2939 /* sort the ids so we can weed out duplicates */
2940 sort(id_list, param->count, sizeof(*id_list), mem_id_cmp, NULL);
2941
2942 for (i = 0; i < param->count; i++) {
2943 unsigned int cachemode;
2944 struct kgsl_mem_entry *entry = NULL;
2945
2946 id = id_list[i];
2947 /* skip 0 ids or duplicates */
2948 if (id == last_id)
2949 continue;
2950
2951 entry = kgsl_sharedmem_find_id(private, id);
2952 if (entry == NULL)
2953 continue;
2954
2955 /* skip uncached memory */
2956 cachemode = kgsl_memdesc_get_cachemode(&entry->memdesc);
2957 if (cachemode != KGSL_CACHEMODE_WRITETHROUGH &&
2958 cachemode != KGSL_CACHEMODE_WRITEBACK) {
2959 kgsl_mem_entry_put(entry);
2960 continue;
2961 }
2962
2963 op_size += entry->memdesc.size;
2964 entries[actual_count++] = entry;
2965
2966 full_flush = check_full_flush(op_size, param->op);
Maria Yuceafc602017-09-26 15:45:02 +08002967 if (full_flush) {
2968 trace_kgsl_mem_sync_full_cache(actual_count, op_size);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002969 break;
Maria Yuceafc602017-09-26 15:45:02 +08002970 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002971
2972 last_id = id;
2973 }
2974
2975 param->op &= ~KGSL_GPUMEM_CACHE_RANGE;
2976
2977 for (i = 0; i < actual_count; i++) {
2978 if (!full_flush)
2979 _kgsl_gpumem_sync_cache(entries[i], 0,
2980 entries[i]->memdesc.size,
2981 param->op);
2982 kgsl_mem_entry_put(entries[i]);
2983 }
2984end:
2985 kfree(entries);
2986 kfree(id_list);
2987 return ret;
2988}
2989
2990/* Legacy cache function, does a flush (clean + invalidate) */
2991
2992long kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
2993 unsigned int cmd, void *data)
2994{
2995 struct kgsl_sharedmem_free *param = data;
2996 struct kgsl_process_private *private = dev_priv->process_priv;
2997 struct kgsl_mem_entry *entry = NULL;
2998 long ret;
2999
3000 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
3001 if (entry == NULL)
3002 return -EINVAL;
3003
3004 ret = _kgsl_gpumem_sync_cache(entry, 0, entry->memdesc.size,
3005 KGSL_GPUMEM_CACHE_FLUSH);
3006 kgsl_mem_entry_put(entry);
3007 return ret;
3008}
3009
3010long kgsl_ioctl_gpuobj_sync(struct kgsl_device_private *dev_priv,
3011 unsigned int cmd, void *data)
3012{
3013 struct kgsl_process_private *private = dev_priv->process_priv;
3014 struct kgsl_gpuobj_sync *param = data;
3015 struct kgsl_gpuobj_sync_obj *objs;
3016 struct kgsl_mem_entry **entries;
3017 long ret = 0;
3018 bool full_flush = false;
3019 uint64_t size = 0;
Carter Cooper69355b82018-01-17 09:49:00 -07003020 int i;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003021 void __user *ptr;
3022
3023 if (param->count == 0 || param->count > 128)
3024 return -EINVAL;
3025
3026 objs = kcalloc(param->count, sizeof(*objs), GFP_KERNEL);
3027 if (objs == NULL)
3028 return -ENOMEM;
3029
3030 entries = kcalloc(param->count, sizeof(*entries), GFP_KERNEL);
3031 if (entries == NULL) {
Carter Cooper69355b82018-01-17 09:49:00 -07003032 kfree(objs);
3033 return -ENOMEM;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003034 }
3035
3036 ptr = to_user_ptr(param->objs);
3037
3038 for (i = 0; i < param->count; i++) {
3039 ret = _copy_from_user(&objs[i], ptr, sizeof(*objs),
3040 param->obj_len);
3041 if (ret)
3042 goto out;
3043
3044 entries[i] = kgsl_sharedmem_find_id(private, objs[i].id);
3045
3046 /* Not finding the ID is not a fatal failure - just skip it */
3047 if (entries[i] == NULL)
3048 continue;
3049
Shrenuj Bansala419c792016-10-20 14:05:11 -07003050 if (!(objs[i].op & KGSL_GPUMEM_CACHE_RANGE))
3051 size += entries[i]->memdesc.size;
3052 else if (objs[i].offset < entries[i]->memdesc.size)
3053 size += (entries[i]->memdesc.size - objs[i].offset);
3054
3055 full_flush = check_full_flush(size, objs[i].op);
Maria Yuceafc602017-09-26 15:45:02 +08003056 if (full_flush) {
3057 trace_kgsl_mem_sync_full_cache(i, size);
Carter Cooper69355b82018-01-17 09:49:00 -07003058 goto out;
Maria Yuceafc602017-09-26 15:45:02 +08003059 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003060
3061 ptr += sizeof(*objs);
3062 }
3063
Carter Cooper69355b82018-01-17 09:49:00 -07003064 for (i = 0; !ret && i < param->count; i++)
3065 if (entries[i])
3066 ret = _kgsl_gpumem_sync_cache(entries[i],
3067 objs[i].offset, objs[i].length,
3068 objs[i].op);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003069
Carter Cooper69355b82018-01-17 09:49:00 -07003070out:
Shrenuj Bansala419c792016-10-20 14:05:11 -07003071 for (i = 0; i < param->count; i++)
3072 if (entries[i])
3073 kgsl_mem_entry_put(entries[i]);
3074
Shrenuj Bansala419c792016-10-20 14:05:11 -07003075 kfree(entries);
3076 kfree(objs);
3077
3078 return ret;
3079}
3080
3081#ifdef CONFIG_ARM64
3082static uint64_t kgsl_filter_cachemode(uint64_t flags)
3083{
3084 /*
3085 * WRITETHROUGH is not supported in arm64, so we tell the user that we
3086 * use WRITEBACK which is the default caching policy.
3087 */
3088 if ((flags & KGSL_CACHEMODE_MASK) >> KGSL_CACHEMODE_SHIFT ==
3089 KGSL_CACHEMODE_WRITETHROUGH) {
3090 flags &= ~((uint64_t) KGSL_CACHEMODE_MASK);
Lynus Vazeb7af682017-04-17 18:36:01 +05303091 flags |= (uint64_t)((KGSL_CACHEMODE_WRITEBACK <<
3092 KGSL_CACHEMODE_SHIFT) &
3093 KGSL_CACHEMODE_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003094 }
3095 return flags;
3096}
3097#else
3098static uint64_t kgsl_filter_cachemode(uint64_t flags)
3099{
3100 return flags;
3101}
3102#endif
3103
3104/* The largest allowable alignment for a GPU object is 32MB */
3105#define KGSL_MAX_ALIGN (32 * SZ_1M)
3106
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -06003107struct kgsl_mem_entry *gpumem_alloc_entry(
Shrenuj Bansala419c792016-10-20 14:05:11 -07003108 struct kgsl_device_private *dev_priv,
3109 uint64_t size, uint64_t flags)
3110{
3111 int ret;
3112 struct kgsl_process_private *private = dev_priv->process_priv;
3113 struct kgsl_mem_entry *entry;
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003114 struct kgsl_mmu *mmu = &dev_priv->device->mmu;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003115 unsigned int align;
3116
3117 flags &= KGSL_MEMFLAGS_GPUREADONLY
3118 | KGSL_CACHEMODE_MASK
3119 | KGSL_MEMTYPE_MASK
3120 | KGSL_MEMALIGN_MASK
3121 | KGSL_MEMFLAGS_USE_CPU_MAP
3122 | KGSL_MEMFLAGS_SECURE
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003123 | KGSL_MEMFLAGS_FORCE_32BIT
3124 | KGSL_MEMFLAGS_IOCOHERENT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003125
Shrenuj Bansala419c792016-10-20 14:05:11 -07003126 /* Return not supported error if secure memory isn't enabled */
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07003127 if (!kgsl_mmu_is_secured(mmu) &&
Shrenuj Bansala419c792016-10-20 14:05:11 -07003128 (flags & KGSL_MEMFLAGS_SECURE)) {
3129 dev_WARN_ONCE(dev_priv->device->dev, 1,
3130 "Secure memory not supported");
3131 return ERR_PTR(-EOPNOTSUPP);
3132 }
3133
Shrenuj Bansala419c792016-10-20 14:05:11 -07003134 /* Cap the alignment bits to the highest number we can handle */
3135 align = MEMFLAGS(flags, KGSL_MEMALIGN_MASK, KGSL_MEMALIGN_SHIFT);
3136 if (align >= ilog2(KGSL_MAX_ALIGN)) {
3137 KGSL_CORE_ERR("Alignment too large; restricting to %dK\n",
3138 KGSL_MAX_ALIGN >> 10);
3139
3140 flags &= ~((uint64_t) KGSL_MEMALIGN_MASK);
Lynus Vazeb7af682017-04-17 18:36:01 +05303141 flags |= (uint64_t)((ilog2(KGSL_MAX_ALIGN) <<
3142 KGSL_MEMALIGN_SHIFT) &
3143 KGSL_MEMALIGN_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003144 }
3145
3146 /* For now only allow allocations up to 4G */
3147 if (size == 0 || size > UINT_MAX)
3148 return ERR_PTR(-EINVAL);
3149
3150 flags = kgsl_filter_cachemode(flags);
3151
3152 entry = kgsl_mem_entry_create();
3153 if (entry == NULL)
3154 return ERR_PTR(-ENOMEM);
3155
Shrenuj Bansala419c792016-10-20 14:05:11 -07003156 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
3157 size, flags);
3158 if (ret != 0)
3159 goto err;
3160
3161 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3162 if (ret != 0) {
3163 kgsl_sharedmem_free(&entry->memdesc);
3164 goto err;
3165 }
3166
3167 kgsl_process_add_stats(private,
3168 kgsl_memdesc_usermem_type(&entry->memdesc),
3169 entry->memdesc.size);
3170 trace_kgsl_mem_alloc(entry);
3171
3172 kgsl_mem_entry_commit_process(entry);
3173 return entry;
3174err:
3175 kfree(entry);
3176 return ERR_PTR(ret);
3177}
3178
3179static void copy_metadata(struct kgsl_mem_entry *entry, uint64_t metadata,
3180 unsigned int len)
3181{
3182 unsigned int i, size;
3183
3184 if (len == 0)
3185 return;
3186
3187 size = min_t(unsigned int, len, sizeof(entry->metadata) - 1);
3188
3189 if (copy_from_user(entry->metadata, to_user_ptr(metadata), size)) {
3190 memset(entry->metadata, 0, sizeof(entry->metadata));
3191 return;
3192 }
3193
3194 /* Clean up non printable characters in the string */
3195 for (i = 0; i < size && entry->metadata[i] != 0; i++) {
3196 if (!isprint(entry->metadata[i]))
3197 entry->metadata[i] = '?';
3198 }
3199}
3200
3201long kgsl_ioctl_gpuobj_alloc(struct kgsl_device_private *dev_priv,
3202 unsigned int cmd, void *data)
3203{
3204 struct kgsl_gpuobj_alloc *param = data;
3205 struct kgsl_mem_entry *entry;
3206
Deepak Kumarcf056d12018-04-17 15:59:42 +05303207 if (kgsl_is_compat_task())
3208 param->flags |= KGSL_MEMFLAGS_FORCE_32BIT;
3209
Shrenuj Bansala419c792016-10-20 14:05:11 -07003210 entry = gpumem_alloc_entry(dev_priv, param->size, param->flags);
3211
3212 if (IS_ERR(entry))
3213 return PTR_ERR(entry);
3214
3215 copy_metadata(entry, param->metadata, param->metadata_len);
3216
3217 param->size = entry->memdesc.size;
3218 param->flags = entry->memdesc.flags;
3219 param->mmapsize = kgsl_memdesc_footprint(&entry->memdesc);
3220 param->id = entry->id;
3221
Tarun Karra24d3fe12017-04-05 15:23:03 -07003222 /* Put the extra ref from kgsl_mem_entry_create() */
3223 kgsl_mem_entry_put(entry);
3224
Shrenuj Bansala419c792016-10-20 14:05:11 -07003225 return 0;
3226}
3227
3228long kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
3229 unsigned int cmd, void *data)
3230{
3231 struct kgsl_gpumem_alloc *param = data;
3232 struct kgsl_mem_entry *entry;
3233 uint64_t flags = param->flags;
3234
3235 /* Legacy functions doesn't support these advanced features */
3236 flags &= ~((uint64_t) KGSL_MEMFLAGS_USE_CPU_MAP);
Deepak Kumarcf056d12018-04-17 15:59:42 +05303237
3238 if (kgsl_is_compat_task())
3239 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003240
3241 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3242
3243 if (IS_ERR(entry))
3244 return PTR_ERR(entry);
3245
3246 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3247 param->size = (size_t) entry->memdesc.size;
3248 param->flags = (unsigned int) entry->memdesc.flags;
3249
Tarun Karra24d3fe12017-04-05 15:23:03 -07003250 /* Put the extra ref from kgsl_mem_entry_create() */
3251 kgsl_mem_entry_put(entry);
3252
Shrenuj Bansala419c792016-10-20 14:05:11 -07003253 return 0;
3254}
3255
3256long kgsl_ioctl_gpumem_alloc_id(struct kgsl_device_private *dev_priv,
3257 unsigned int cmd, void *data)
3258{
3259 struct kgsl_gpumem_alloc_id *param = data;
3260 struct kgsl_mem_entry *entry;
3261 uint64_t flags = param->flags;
3262
Deepak Kumarcf056d12018-04-17 15:59:42 +05303263 if (kgsl_is_compat_task())
3264 flags |= KGSL_MEMFLAGS_FORCE_32BIT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003265
3266 entry = gpumem_alloc_entry(dev_priv, (uint64_t) param->size, flags);
3267
3268 if (IS_ERR(entry))
3269 return PTR_ERR(entry);
3270
3271 param->id = entry->id;
3272 param->flags = (unsigned int) entry->memdesc.flags;
3273 param->size = (size_t) entry->memdesc.size;
3274 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
3275 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3276
Tarun Karra24d3fe12017-04-05 15:23:03 -07003277 /* Put the extra ref from kgsl_mem_entry_create() */
3278 kgsl_mem_entry_put(entry);
3279
Shrenuj Bansala419c792016-10-20 14:05:11 -07003280 return 0;
3281}
3282
3283long kgsl_ioctl_gpumem_get_info(struct kgsl_device_private *dev_priv,
3284 unsigned int cmd, void *data)
3285{
3286 struct kgsl_process_private *private = dev_priv->process_priv;
3287 struct kgsl_gpumem_get_info *param = data;
3288 struct kgsl_mem_entry *entry = NULL;
3289 int result = 0;
3290
3291 if (param->id != 0)
3292 entry = kgsl_sharedmem_find_id(private, param->id);
3293 else if (param->gpuaddr != 0)
3294 entry = kgsl_sharedmem_find(private, (uint64_t) param->gpuaddr);
3295
3296 if (entry == NULL)
3297 return -EINVAL;
3298
3299 /*
3300 * If any of the 64 bit address / sizes would end up being
3301 * truncated, return -ERANGE. That will signal the user that they
3302 * should use a more modern API
3303 */
3304 if (entry->memdesc.gpuaddr > ULONG_MAX)
3305 result = -ERANGE;
3306
3307 param->gpuaddr = (unsigned long) entry->memdesc.gpuaddr;
3308 param->id = entry->id;
3309 param->flags = (unsigned int) entry->memdesc.flags;
3310 param->size = (size_t) entry->memdesc.size;
3311 param->mmapsize = (size_t) kgsl_memdesc_footprint(&entry->memdesc);
Jordan Crouse6bce65c2020-12-28 16:06:42 +05303312 /*
3313 * Entries can have multiple user mappings so thre isn't any one address
3314 * we can report. Plus, the user should already know their mappings, so
3315 * there isn't any value in reporting it back to them.
3316 */
3317 param->useraddr = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003318
3319 kgsl_mem_entry_put(entry);
3320 return result;
3321}
3322
3323static inline int _sparse_alloc_param_sanity_check(uint64_t size,
3324 uint64_t pagesize)
3325{
3326 if (size == 0 || pagesize == 0)
3327 return -EINVAL;
3328
3329 if (pagesize != PAGE_SIZE && pagesize != SZ_64K)
3330 return -EINVAL;
3331
3332 if (pagesize > size || !IS_ALIGNED(size, pagesize))
3333 return -EINVAL;
3334
3335 return 0;
3336}
3337
3338long kgsl_ioctl_sparse_phys_alloc(struct kgsl_device_private *dev_priv,
3339 unsigned int cmd, void *data)
3340{
3341 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003342 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003343 struct kgsl_sparse_phys_alloc *param = data;
3344 struct kgsl_mem_entry *entry;
Lynus Vaz90d98b52018-04-09 14:45:36 +05303345 uint64_t flags;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003346 int ret;
3347 int id;
3348
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003349 if (!(device->flags & KGSL_FLAG_SPARSE))
3350 return -ENOTSUPP;
3351
Shrenuj Bansala419c792016-10-20 14:05:11 -07003352 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3353 if (ret)
3354 return ret;
3355
3356 entry = kgsl_mem_entry_create();
3357 if (entry == NULL)
3358 return -ENOMEM;
3359
3360 ret = kgsl_process_private_get(process);
3361 if (!ret) {
3362 ret = -EBADF;
3363 goto err_free_entry;
3364 }
3365
3366 idr_preload(GFP_KERNEL);
3367 spin_lock(&process->mem_lock);
3368 /* Allocate the ID but don't attach the pointer just yet */
3369 id = idr_alloc(&process->mem_idr, NULL, 1, 0, GFP_NOWAIT);
3370 spin_unlock(&process->mem_lock);
3371 idr_preload_end();
3372
3373 if (id < 0) {
3374 ret = id;
3375 goto err_put_proc_priv;
3376 }
3377
3378 entry->id = id;
3379 entry->priv = process;
3380
Lynus Vaz90d98b52018-04-09 14:45:36 +05303381 flags = KGSL_MEMFLAGS_SPARSE_PHYS |
3382 ((ilog2(param->pagesize) << KGSL_MEMALIGN_SHIFT) &
3383 KGSL_MEMALIGN_MASK);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003384
3385 ret = kgsl_allocate_user(dev_priv->device, &entry->memdesc,
Lynus Vaz90d98b52018-04-09 14:45:36 +05303386 param->size, flags);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003387 if (ret)
3388 goto err_remove_idr;
3389
3390 /* Sanity check to verify we got correct pagesize */
3391 if (param->pagesize != PAGE_SIZE && entry->memdesc.sgt != NULL) {
3392 struct scatterlist *s;
3393 int i;
3394
3395 for_each_sg(entry->memdesc.sgt->sgl, s,
3396 entry->memdesc.sgt->nents, i) {
3397 if (!IS_ALIGNED(s->length, param->pagesize))
3398 goto err_invalid_pages;
3399 }
3400 }
3401
3402 param->id = entry->id;
3403 param->flags = entry->memdesc.flags;
3404
3405 trace_sparse_phys_alloc(entry->id, param->size, param->pagesize);
3406 kgsl_mem_entry_commit_process(entry);
3407
Tarun Karra24d3fe12017-04-05 15:23:03 -07003408 /* Put the extra ref from kgsl_mem_entry_create() */
3409 kgsl_mem_entry_put(entry);
3410
Shrenuj Bansala419c792016-10-20 14:05:11 -07003411 return 0;
3412
3413err_invalid_pages:
3414 kgsl_sharedmem_free(&entry->memdesc);
3415err_remove_idr:
3416 spin_lock(&process->mem_lock);
3417 idr_remove(&process->mem_idr, entry->id);
3418 spin_unlock(&process->mem_lock);
3419err_put_proc_priv:
3420 kgsl_process_private_put(process);
3421err_free_entry:
3422 kfree(entry);
3423
3424 return ret;
3425}
3426
3427long kgsl_ioctl_sparse_phys_free(struct kgsl_device_private *dev_priv,
3428 unsigned int cmd, void *data)
3429{
3430 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003431 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003432 struct kgsl_sparse_phys_free *param = data;
3433 struct kgsl_mem_entry *entry;
3434
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003435 if (!(device->flags & KGSL_FLAG_SPARSE))
3436 return -ENOTSUPP;
3437
Shrenuj Bansala419c792016-10-20 14:05:11 -07003438 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3439 KGSL_MEMFLAGS_SPARSE_PHYS);
3440 if (entry == NULL)
3441 return -EINVAL;
3442
Deepak Kumar32814682018-02-16 11:46:26 +05303443 if (!kgsl_mem_entry_set_pend(entry)) {
3444 kgsl_mem_entry_put(entry);
3445 return -EBUSY;
3446 }
3447
Shrenuj Bansala419c792016-10-20 14:05:11 -07003448 if (entry->memdesc.cur_bindings != 0) {
Deepak Kumar32814682018-02-16 11:46:26 +05303449 kgsl_mem_entry_unset_pend(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003450 kgsl_mem_entry_put(entry);
3451 return -EINVAL;
3452 }
3453
3454 trace_sparse_phys_free(entry->id);
3455
3456 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3457 kgsl_mem_entry_put(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05303458 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003459
3460 return 0;
3461}
3462
3463long kgsl_ioctl_sparse_virt_alloc(struct kgsl_device_private *dev_priv,
3464 unsigned int cmd, void *data)
3465{
3466 struct kgsl_process_private *private = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003467 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003468 struct kgsl_sparse_virt_alloc *param = data;
3469 struct kgsl_mem_entry *entry;
3470 int ret;
3471
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003472 if (!(device->flags & KGSL_FLAG_SPARSE))
3473 return -ENOTSUPP;
3474
Shrenuj Bansala419c792016-10-20 14:05:11 -07003475 ret = _sparse_alloc_param_sanity_check(param->size, param->pagesize);
3476 if (ret)
3477 return ret;
3478
3479 entry = kgsl_mem_entry_create();
3480 if (entry == NULL)
3481 return -ENOMEM;
3482
Lynus Vaz90d98b52018-04-09 14:45:36 +05303483 kgsl_memdesc_init(dev_priv->device, &entry->memdesc,
3484 KGSL_MEMFLAGS_SPARSE_VIRT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003485 entry->memdesc.size = param->size;
3486 entry->memdesc.cur_bindings = 0;
3487 kgsl_memdesc_set_align(&entry->memdesc, ilog2(param->pagesize));
3488
3489 spin_lock_init(&entry->bind_lock);
3490 entry->bind_tree = RB_ROOT;
3491
3492 ret = kgsl_mem_entry_attach_process(dev_priv->device, private, entry);
3493 if (ret) {
3494 kfree(entry);
3495 return ret;
3496 }
3497
3498 param->id = entry->id;
3499 param->gpuaddr = entry->memdesc.gpuaddr;
3500 param->flags = entry->memdesc.flags;
3501
3502 trace_sparse_virt_alloc(entry->id, param->size, param->pagesize);
3503 kgsl_mem_entry_commit_process(entry);
3504
Tarun Karra24d3fe12017-04-05 15:23:03 -07003505 /* Put the extra ref from kgsl_mem_entry_create() */
3506 kgsl_mem_entry_put(entry);
3507
Shrenuj Bansala419c792016-10-20 14:05:11 -07003508 return 0;
3509}
3510
3511long kgsl_ioctl_sparse_virt_free(struct kgsl_device_private *dev_priv,
3512 unsigned int cmd, void *data)
3513{
3514 struct kgsl_process_private *process = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003515 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003516 struct kgsl_sparse_virt_free *param = data;
3517 struct kgsl_mem_entry *entry = NULL;
3518
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003519 if (!(device->flags & KGSL_FLAG_SPARSE))
3520 return -ENOTSUPP;
3521
Shrenuj Bansala419c792016-10-20 14:05:11 -07003522 entry = kgsl_sharedmem_find_id_flags(process, param->id,
3523 KGSL_MEMFLAGS_SPARSE_VIRT);
3524 if (entry == NULL)
3525 return -EINVAL;
3526
Deepak Kumar32814682018-02-16 11:46:26 +05303527 if (!kgsl_mem_entry_set_pend(entry)) {
3528 kgsl_mem_entry_put(entry);
3529 return -EBUSY;
3530 }
3531
Shrenuj Bansala419c792016-10-20 14:05:11 -07003532 if (entry->bind_tree.rb_node != NULL) {
Deepak Kumar32814682018-02-16 11:46:26 +05303533 kgsl_mem_entry_unset_pend(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003534 kgsl_mem_entry_put(entry);
3535 return -EINVAL;
3536 }
3537
3538 trace_sparse_virt_free(entry->id);
3539
3540 /* One put for find_id(), one put for the kgsl_mem_entry_create() */
3541 kgsl_mem_entry_put(entry);
Hareesh Gundu615439d2017-06-16 17:06:57 +05303542 kgsl_mem_entry_put(entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003543
3544 return 0;
3545}
3546
Lynus Vaz4930cb12017-09-08 18:32:53 +05303547/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003548static int _sparse_add_to_bind_tree(struct kgsl_mem_entry *entry,
3549 uint64_t v_offset,
3550 struct kgsl_memdesc *memdesc,
3551 uint64_t p_offset,
3552 uint64_t size,
3553 uint64_t flags)
3554{
3555 struct sparse_bind_object *new;
3556 struct rb_node **node, *parent = NULL;
3557
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303558 new = kzalloc(sizeof(*new), GFP_ATOMIC);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003559 if (new == NULL)
3560 return -ENOMEM;
3561
3562 new->v_off = v_offset;
3563 new->p_off = p_offset;
3564 new->p_memdesc = memdesc;
3565 new->size = size;
3566 new->flags = flags;
3567
3568 node = &entry->bind_tree.rb_node;
3569
3570 while (*node != NULL) {
3571 struct sparse_bind_object *this;
3572
3573 parent = *node;
3574 this = rb_entry(parent, struct sparse_bind_object, node);
3575
Lynus Vaze8c82572017-09-08 17:27:56 +05303576 if ((new->v_off < this->v_off) &&
3577 ((new->v_off + new->size) <= this->v_off))
Shrenuj Bansala419c792016-10-20 14:05:11 -07003578 node = &parent->rb_left;
Lynus Vaze8c82572017-09-08 17:27:56 +05303579 else if ((new->v_off > this->v_off) &&
3580 (new->v_off >= (this->v_off + this->size)))
Shrenuj Bansala419c792016-10-20 14:05:11 -07003581 node = &parent->rb_right;
Lynus Vaze8c82572017-09-08 17:27:56 +05303582 else {
3583 kfree(new);
3584 return -EADDRINUSE;
3585 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003586 }
3587
3588 rb_link_node(&new->node, parent, node);
3589 rb_insert_color(&new->node, &entry->bind_tree);
3590
3591 return 0;
3592}
3593
3594static int _sparse_rm_from_bind_tree(struct kgsl_mem_entry *entry,
3595 struct sparse_bind_object *obj,
3596 uint64_t v_offset, uint64_t size)
3597{
Shrenuj Bansala419c792016-10-20 14:05:11 -07003598 if (v_offset == obj->v_off && size >= obj->size) {
3599 /*
3600 * We are all encompassing, remove the entry and free
3601 * things up
3602 */
3603 rb_erase(&obj->node, &entry->bind_tree);
3604 kfree(obj);
3605 } else if (v_offset == obj->v_off) {
3606 /*
3607 * We are the front of the node, adjust the front of
3608 * the node
3609 */
3610 obj->v_off += size;
3611 obj->p_off += size;
3612 obj->size -= size;
3613 } else if ((v_offset + size) == (obj->v_off + obj->size)) {
3614 /*
3615 * We are at the end of the obj, adjust the beginning
3616 * points
3617 */
3618 obj->size -= size;
3619 } else {
3620 /*
3621 * We are in the middle of a node, split it up and
3622 * create a new mini node. Adjust this node's bounds
3623 * and add the new node to the list.
3624 */
3625 uint64_t tmp_size = obj->size;
3626 int ret;
3627
3628 obj->size = v_offset - obj->v_off;
3629
Shrenuj Bansala419c792016-10-20 14:05:11 -07003630 ret = _sparse_add_to_bind_tree(entry, v_offset + size,
3631 obj->p_memdesc,
3632 obj->p_off + (v_offset - obj->v_off) + size,
3633 tmp_size - (v_offset - obj->v_off) - size,
3634 obj->flags);
3635
3636 return ret;
3637 }
3638
Shrenuj Bansala419c792016-10-20 14:05:11 -07003639 return 0;
3640}
3641
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303642/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003643static struct sparse_bind_object *_find_containing_bind_obj(
3644 struct kgsl_mem_entry *entry,
3645 uint64_t offset, uint64_t size)
3646{
3647 struct sparse_bind_object *obj = NULL;
3648 struct rb_node *node = entry->bind_tree.rb_node;
3649
Shrenuj Bansala419c792016-10-20 14:05:11 -07003650 while (node != NULL) {
3651 obj = rb_entry(node, struct sparse_bind_object, node);
3652
3653 if (offset == obj->v_off) {
3654 break;
3655 } else if (offset < obj->v_off) {
3656 if (offset + size > obj->v_off)
3657 break;
3658 node = node->rb_left;
3659 obj = NULL;
3660 } else if (offset > obj->v_off) {
3661 if (offset < obj->v_off + obj->size)
3662 break;
3663 node = node->rb_right;
3664 obj = NULL;
3665 }
3666 }
3667
Shrenuj Bansala419c792016-10-20 14:05:11 -07003668 return obj;
3669}
3670
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303671/* entry->bind_lock must be held by the caller */
Shrenuj Bansala419c792016-10-20 14:05:11 -07003672static int _sparse_unbind(struct kgsl_mem_entry *entry,
3673 struct sparse_bind_object *bind_obj,
3674 uint64_t offset, uint64_t size)
3675{
Shrenuj Bansala419c792016-10-20 14:05:11 -07003676 int ret;
3677
Shrenuj Bansala419c792016-10-20 14:05:11 -07003678 ret = _sparse_rm_from_bind_tree(entry, bind_obj, offset, size);
3679 if (ret == 0) {
3680 atomic_long_sub(size, &kgsl_driver.stats.mapped);
3681 trace_sparse_unbind(entry->id, offset, size);
3682 }
3683
3684 return ret;
3685}
3686
3687static long sparse_unbind_range(struct kgsl_sparse_binding_object *obj,
3688 struct kgsl_mem_entry *virt_entry)
3689{
3690 struct sparse_bind_object *bind_obj;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303691 struct kgsl_memdesc *memdesc;
3692 struct kgsl_pagetable *pt;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003693 int ret = 0;
3694 uint64_t size = obj->size;
3695 uint64_t tmp_size = obj->size;
3696 uint64_t offset = obj->virtoffset;
3697
3698 while (size > 0 && ret == 0) {
3699 tmp_size = size;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303700
3701 spin_lock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003702 bind_obj = _find_containing_bind_obj(virt_entry, offset, size);
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303703
3704 if (bind_obj == NULL) {
3705 spin_unlock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003706 return 0;
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303707 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003708
3709 if (bind_obj->v_off > offset) {
3710 tmp_size = size - bind_obj->v_off - offset;
3711 if (tmp_size > bind_obj->size)
3712 tmp_size = bind_obj->size;
3713 offset = bind_obj->v_off;
3714 } else if (bind_obj->v_off < offset) {
3715 uint64_t diff = offset - bind_obj->v_off;
3716
3717 if (diff + size > bind_obj->size)
3718 tmp_size = bind_obj->size - diff;
3719 } else {
3720 if (tmp_size > bind_obj->size)
3721 tmp_size = bind_obj->size;
3722 }
3723
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303724 memdesc = bind_obj->p_memdesc;
3725 pt = memdesc->pagetable;
3726
3727 if (memdesc->cur_bindings < (tmp_size / PAGE_SIZE)) {
3728 spin_unlock(&virt_entry->bind_lock);
3729 return -EINVAL;
3730 }
3731
3732 memdesc->cur_bindings -= tmp_size / PAGE_SIZE;
3733
Shrenuj Bansala419c792016-10-20 14:05:11 -07003734 ret = _sparse_unbind(virt_entry, bind_obj, offset, tmp_size);
Sunil Khatri94dce8f2017-09-04 14:05:03 +05303735 spin_unlock(&virt_entry->bind_lock);
3736
3737 ret = kgsl_mmu_unmap_offset(pt, memdesc,
3738 virt_entry->memdesc.gpuaddr, offset, tmp_size);
3739 if (ret)
3740 return ret;
3741
3742 ret = kgsl_mmu_sparse_dummy_map(pt, memdesc, offset, tmp_size);
3743 if (ret)
3744 return ret;
3745
Shrenuj Bansala419c792016-10-20 14:05:11 -07003746 if (ret == 0) {
3747 offset += tmp_size;
3748 size -= tmp_size;
3749 }
3750 }
3751
3752 return ret;
3753}
3754
3755static inline bool _is_phys_bindable(struct kgsl_mem_entry *phys_entry,
3756 uint64_t offset, uint64_t size, uint64_t flags)
3757{
3758 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3759
3760 if (!IS_ALIGNED(offset | size, kgsl_memdesc_get_pagesize(memdesc)))
3761 return false;
3762
Sudeep Yedalapure8ff97992017-01-20 20:12:51 +05303763 if (offset + size < offset)
3764 return false;
3765
Shrenuj Bansala419c792016-10-20 14:05:11 -07003766 if (!(flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
3767 offset + size > memdesc->size)
3768 return false;
3769
3770 return true;
3771}
3772
3773static int _sparse_bind(struct kgsl_process_private *process,
3774 struct kgsl_mem_entry *virt_entry, uint64_t v_offset,
3775 struct kgsl_mem_entry *phys_entry, uint64_t p_offset,
3776 uint64_t size, uint64_t flags)
3777{
3778 int ret;
3779 struct kgsl_pagetable *pagetable;
3780 struct kgsl_memdesc *memdesc = &phys_entry->memdesc;
3781
3782 /* map the memory after unlocking if gpuaddr has been assigned */
3783 if (memdesc->gpuaddr)
3784 return -EINVAL;
3785
Shrenuj Bansala419c792016-10-20 14:05:11 -07003786 pagetable = memdesc->pagetable;
3787
3788 /* Clear out any mappings */
3789 ret = kgsl_mmu_unmap_offset(pagetable, &virt_entry->memdesc,
3790 virt_entry->memdesc.gpuaddr, v_offset, size);
3791 if (ret)
3792 return ret;
3793
3794 ret = kgsl_mmu_map_offset(pagetable, virt_entry->memdesc.gpuaddr,
3795 v_offset, memdesc, p_offset, size, flags);
3796 if (ret) {
3797 /* Try to clean up, but not the end of the world */
3798 kgsl_mmu_sparse_dummy_map(pagetable, &virt_entry->memdesc,
3799 v_offset, size);
3800 return ret;
3801 }
3802
Lynus Vaz4930cb12017-09-08 18:32:53 +05303803 spin_lock(&virt_entry->bind_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003804 ret = _sparse_add_to_bind_tree(virt_entry, v_offset, memdesc,
3805 p_offset, size, flags);
Lynus Vaz4930cb12017-09-08 18:32:53 +05303806 spin_unlock(&virt_entry->bind_lock);
3807
Shrenuj Bansala419c792016-10-20 14:05:11 -07003808 if (ret == 0)
3809 memdesc->cur_bindings += size / PAGE_SIZE;
3810
3811 return ret;
3812}
3813
3814static long sparse_bind_range(struct kgsl_process_private *private,
3815 struct kgsl_sparse_binding_object *obj,
3816 struct kgsl_mem_entry *virt_entry)
3817{
3818 struct kgsl_mem_entry *phys_entry;
3819 int ret;
3820
3821 phys_entry = kgsl_sharedmem_find_id_flags(private, obj->id,
3822 KGSL_MEMFLAGS_SPARSE_PHYS);
3823 if (phys_entry == NULL)
3824 return -EINVAL;
3825
3826 if (!_is_phys_bindable(phys_entry, obj->physoffset, obj->size,
3827 obj->flags)) {
3828 kgsl_mem_entry_put(phys_entry);
3829 return -EINVAL;
3830 }
3831
3832 if (kgsl_memdesc_get_align(&virt_entry->memdesc) !=
3833 kgsl_memdesc_get_align(&phys_entry->memdesc)) {
3834 kgsl_mem_entry_put(phys_entry);
3835 return -EINVAL;
3836 }
3837
3838 ret = sparse_unbind_range(obj, virt_entry);
3839 if (ret) {
3840 kgsl_mem_entry_put(phys_entry);
3841 return -EINVAL;
3842 }
3843
3844 ret = _sparse_bind(private, virt_entry, obj->virtoffset,
3845 phys_entry, obj->physoffset, obj->size,
3846 obj->flags & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS);
3847 if (ret == 0) {
3848 KGSL_STATS_ADD(obj->size, &kgsl_driver.stats.mapped,
3849 &kgsl_driver.stats.mapped_max);
3850
3851 trace_sparse_bind(virt_entry->id, obj->virtoffset,
3852 phys_entry->id, obj->physoffset,
3853 obj->size, obj->flags);
3854 }
3855
3856 kgsl_mem_entry_put(phys_entry);
3857
3858 return ret;
3859}
3860
3861long kgsl_ioctl_sparse_bind(struct kgsl_device_private *dev_priv,
3862 unsigned int cmd, void *data)
3863{
3864 struct kgsl_process_private *private = dev_priv->process_priv;
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003865 struct kgsl_device *device = dev_priv->device;
Shrenuj Bansala419c792016-10-20 14:05:11 -07003866 struct kgsl_sparse_bind *param = data;
3867 struct kgsl_sparse_binding_object obj;
3868 struct kgsl_mem_entry *virt_entry;
3869 int pg_sz;
3870 void __user *ptr;
3871 int ret = 0;
3872 int i = 0;
3873
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003874 if (!(device->flags & KGSL_FLAG_SPARSE))
3875 return -ENOTSUPP;
3876
Shrenuj Bansala419c792016-10-20 14:05:11 -07003877 ptr = (void __user *) (uintptr_t) param->list;
3878
3879 if (param->size > sizeof(struct kgsl_sparse_binding_object) ||
3880 param->count == 0 || ptr == NULL)
3881 return -EINVAL;
3882
3883 virt_entry = kgsl_sharedmem_find_id_flags(private, param->id,
3884 KGSL_MEMFLAGS_SPARSE_VIRT);
3885 if (virt_entry == NULL)
3886 return -EINVAL;
3887
3888 pg_sz = kgsl_memdesc_get_pagesize(&virt_entry->memdesc);
3889
3890 for (i = 0; i < param->count; i++) {
3891 memset(&obj, 0, sizeof(obj));
3892 ret = _copy_from_user(&obj, ptr, sizeof(obj), param->size);
3893 if (ret)
3894 break;
3895
3896 /* Sanity check initial range */
Sudeep Yedalapure8ff97992017-01-20 20:12:51 +05303897 if (obj.size == 0 || obj.virtoffset + obj.size < obj.size ||
Shrenuj Bansala419c792016-10-20 14:05:11 -07003898 obj.virtoffset + obj.size > virt_entry->memdesc.size ||
3899 !(IS_ALIGNED(obj.virtoffset | obj.size, pg_sz))) {
3900 ret = -EINVAL;
3901 break;
3902 }
3903
3904 if (obj.flags & KGSL_SPARSE_BIND)
3905 ret = sparse_bind_range(private, &obj, virt_entry);
3906 else if (obj.flags & KGSL_SPARSE_UNBIND)
3907 ret = sparse_unbind_range(&obj, virt_entry);
3908 else
3909 ret = -EINVAL;
3910 if (ret)
3911 break;
3912
3913 ptr += sizeof(obj);
3914 }
3915
3916 kgsl_mem_entry_put(virt_entry);
3917
3918 return ret;
3919}
3920
Tarun Karra2b8b3632016-11-14 16:38:27 -08003921long kgsl_ioctl_gpu_sparse_command(struct kgsl_device_private *dev_priv,
3922 unsigned int cmd, void *data)
3923{
3924 struct kgsl_gpu_sparse_command *param = data;
3925 struct kgsl_device *device = dev_priv->device;
3926 struct kgsl_context *context;
3927 struct kgsl_drawobj *drawobj[2];
3928 struct kgsl_drawobj_sparse *sparseobj;
3929 long result;
3930 unsigned int i = 0;
3931
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06003932 if (!(device->flags & KGSL_FLAG_SPARSE))
3933 return -ENOTSUPP;
3934
Tarun Karra2b8b3632016-11-14 16:38:27 -08003935 /* Make sure sparse and syncpoint count isn't too big */
3936 if (param->numsparse > KGSL_MAX_SPARSE ||
3937 param->numsyncs > KGSL_MAX_SYNCPOINTS)
3938 return -EINVAL;
3939
3940 /* Make sure there is atleast one sparse or sync */
3941 if (param->numsparse == 0 && param->numsyncs == 0)
3942 return -EINVAL;
3943
3944 /* Only Sparse commands are supported in this ioctl */
3945 if (!(param->flags & KGSL_DRAWOBJ_SPARSE) || (param->flags &
3946 (KGSL_DRAWOBJ_SUBMIT_IB_LIST | KGSL_DRAWOBJ_MARKER
3947 | KGSL_DRAWOBJ_SYNC)))
3948 return -EINVAL;
3949
3950 context = kgsl_context_get_owner(dev_priv, param->context_id);
3951 if (context == NULL)
3952 return -EINVAL;
3953
3954 /* Restrict bind commands to bind context */
3955 if (!(context->flags & KGSL_CONTEXT_SPARSE)) {
3956 kgsl_context_put(context);
3957 return -EINVAL;
3958 }
3959
3960 if (param->numsyncs) {
3961 struct kgsl_drawobj_sync *syncobj = kgsl_drawobj_sync_create(
3962 device, context);
3963 if (IS_ERR(syncobj)) {
3964 result = PTR_ERR(syncobj);
3965 goto done;
3966 }
3967
3968 drawobj[i++] = DRAWOBJ(syncobj);
3969 result = kgsl_drawobj_sync_add_synclist(device, syncobj,
3970 to_user_ptr(param->synclist),
3971 param->syncsize, param->numsyncs);
3972 if (result)
3973 goto done;
3974 }
3975
3976 if (param->numsparse) {
3977 sparseobj = kgsl_drawobj_sparse_create(device, context,
3978 param->flags);
3979 if (IS_ERR(sparseobj)) {
3980 result = PTR_ERR(sparseobj);
3981 goto done;
3982 }
3983
3984 sparseobj->id = param->id;
3985 drawobj[i++] = DRAWOBJ(sparseobj);
3986 result = kgsl_drawobj_sparse_add_sparselist(device, sparseobj,
3987 param->id, to_user_ptr(param->sparselist),
3988 param->sparsesize, param->numsparse);
3989 if (result)
3990 goto done;
3991 }
3992
3993 result = dev_priv->device->ftbl->queue_cmds(dev_priv, context,
3994 drawobj, i, &param->timestamp);
3995
3996done:
3997 /*
3998 * -EPROTO is a "success" error - it just tells the user that the
3999 * context had previously faulted
4000 */
4001 if (result && result != -EPROTO)
4002 while (i--)
4003 kgsl_drawobj_destroy(drawobj[i]);
4004
4005 kgsl_context_put(context);
4006 return result;
4007}
4008
4009void kgsl_sparse_bind(struct kgsl_process_private *private,
4010 struct kgsl_drawobj_sparse *sparseobj)
4011{
4012 struct kgsl_sparseobj_node *sparse_node;
4013 struct kgsl_mem_entry *virt_entry = NULL;
4014 long ret = 0;
4015 char *name;
4016
4017 virt_entry = kgsl_sharedmem_find_id_flags(private, sparseobj->id,
4018 KGSL_MEMFLAGS_SPARSE_VIRT);
4019 if (virt_entry == NULL)
4020 return;
4021
4022 list_for_each_entry(sparse_node, &sparseobj->sparselist, node) {
4023 if (sparse_node->obj.flags & KGSL_SPARSE_BIND) {
4024 ret = sparse_bind_range(private, &sparse_node->obj,
4025 virt_entry);
4026 name = "bind";
4027 } else {
4028 ret = sparse_unbind_range(&sparse_node->obj,
4029 virt_entry);
4030 name = "unbind";
4031 }
4032
4033 if (ret)
4034 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",
4035 name, ret, sparse_node->virt_id,
4036 sparse_node->obj.id,
4037 sparse_node->obj.virtoffset,
4038 sparse_node->obj.physoffset,
4039 sparse_node->obj.size, sparse_node->obj.flags);
4040 }
4041
4042 kgsl_mem_entry_put(virt_entry);
4043}
4044EXPORT_SYMBOL(kgsl_sparse_bind);
4045
Shrenuj Bansala419c792016-10-20 14:05:11 -07004046long kgsl_ioctl_gpuobj_info(struct kgsl_device_private *dev_priv,
4047 unsigned int cmd, void *data)
4048{
4049 struct kgsl_process_private *private = dev_priv->process_priv;
4050 struct kgsl_gpuobj_info *param = data;
4051 struct kgsl_mem_entry *entry;
4052
4053 if (param->id == 0)
4054 return -EINVAL;
4055
4056 entry = kgsl_sharedmem_find_id(private, param->id);
4057 if (entry == NULL)
4058 return -EINVAL;
4059
4060 param->id = entry->id;
4061 param->gpuaddr = entry->memdesc.gpuaddr;
4062 param->flags = entry->memdesc.flags;
4063 param->size = entry->memdesc.size;
4064 param->va_len = kgsl_memdesc_footprint(&entry->memdesc);
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304065 /*
4066 * Entries can have multiple user mappings so thre isn't any one address
4067 * we can report. Plus, the user should already know their mappings, so
4068 * there isn't any value in reporting it back to them.
4069 */
4070 param->va_addr = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004071
4072 kgsl_mem_entry_put(entry);
4073 return 0;
4074}
4075
4076long kgsl_ioctl_gpuobj_set_info(struct kgsl_device_private *dev_priv,
4077 unsigned int cmd, void *data)
4078{
4079 struct kgsl_process_private *private = dev_priv->process_priv;
4080 struct kgsl_gpuobj_set_info *param = data;
4081 struct kgsl_mem_entry *entry;
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304082 int ret = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004083
4084 if (param->id == 0)
4085 return -EINVAL;
4086
4087 entry = kgsl_sharedmem_find_id(private, param->id);
4088 if (entry == NULL)
4089 return -EINVAL;
4090
4091 if (param->flags & KGSL_GPUOBJ_SET_INFO_METADATA)
4092 copy_metadata(entry, param->metadata, param->metadata_len);
4093
4094 if (param->flags & KGSL_GPUOBJ_SET_INFO_TYPE) {
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304095 if (param->type <= (KGSL_MEMTYPE_MASK >> KGSL_MEMTYPE_SHIFT)) {
4096 entry->memdesc.flags &= ~((uint64_t) KGSL_MEMTYPE_MASK);
4097 entry->memdesc.flags |= (uint64_t)((param->type <<
4098 KGSL_MEMTYPE_SHIFT) & KGSL_MEMTYPE_MASK);
4099 } else
4100 ret = -EINVAL;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004101 }
4102
4103 kgsl_mem_entry_put(entry);
Deepak Kumar7d13ed22018-02-23 16:31:46 +05304104 return ret;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004105}
4106
4107/**
4108 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
4109 * @dev_priv - pointer to the private device structure
4110 * @cmd - the ioctl cmd passed from kgsl_ioctl
4111 * @data - the user data buffer from kgsl_ioctl
4112 * @returns 0 on success or error code on failure
4113 */
4114
4115long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
4116 unsigned int cmd, void *data)
4117{
4118 struct kgsl_timestamp_event *param = data;
4119 int ret;
4120
4121 switch (param->type) {
4122 case KGSL_TIMESTAMP_EVENT_FENCE:
4123 ret = kgsl_add_fence_event(dev_priv->device,
4124 param->context_id, param->timestamp, param->priv,
4125 param->len, dev_priv);
4126 break;
4127 default:
4128 ret = -EINVAL;
4129 }
4130
4131 return ret;
4132}
4133
4134static int
4135kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
4136{
4137 struct kgsl_memdesc *memdesc = &device->memstore;
4138 int result;
4139 unsigned int vma_size = vma->vm_end - vma->vm_start;
4140
4141 /* The memstore can only be mapped as read only */
4142
4143 if (vma->vm_flags & VM_WRITE)
4144 return -EPERM;
4145
Indira Biruduraju1e6b63e2020-08-11 15:24:16 +05304146 vma->vm_flags &= ~VM_MAYWRITE;
4147
Shrenuj Bansala419c792016-10-20 14:05:11 -07004148 if (memdesc->size != vma_size) {
4149 KGSL_MEM_ERR(device, "memstore bad size: %d should be %llu\n",
4150 vma_size, memdesc->size);
4151 return -EINVAL;
4152 }
4153
4154 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
4155
4156 result = remap_pfn_range(vma, vma->vm_start,
4157 device->memstore.physaddr >> PAGE_SHIFT,
4158 vma_size, vma->vm_page_prot);
4159 if (result != 0)
4160 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
4161 result);
4162
4163 return result;
4164}
4165
4166/*
4167 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
4168 * Increase the refcount to make sure that the accounting stays correct
4169 */
4170
4171static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
4172{
4173 struct kgsl_mem_entry *entry = vma->vm_private_data;
4174
4175 if (kgsl_mem_entry_get(entry) == 0)
4176 vma->vm_private_data = NULL;
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304177
4178 atomic_inc(&entry->map_count);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004179}
4180
4181static int
4182kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4183{
4184 struct kgsl_mem_entry *entry = vma->vm_private_data;
4185
4186 if (!entry)
4187 return VM_FAULT_SIGBUS;
4188 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
4189 return VM_FAULT_SIGBUS;
4190
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304191 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004192}
4193
4194static void
4195kgsl_gpumem_vm_close(struct vm_area_struct *vma)
4196{
4197 struct kgsl_mem_entry *entry = vma->vm_private_data;
4198
4199 if (!entry)
4200 return;
4201
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304202 /*
4203 * Remove the memdesc from the mapped stat once all the mappings have
4204 * gone away
4205 */
4206 if (!atomic_dec_return(&entry->map_count))
4207 entry->priv->gpumem_mapped -= entry->memdesc.size;
4208
Shrenuj Bansala419c792016-10-20 14:05:11 -07004209 kgsl_mem_entry_put(entry);
4210}
4211
4212static const struct vm_operations_struct kgsl_gpumem_vm_ops = {
4213 .open = kgsl_gpumem_vm_open,
4214 .fault = kgsl_gpumem_vm_fault,
4215 .close = kgsl_gpumem_vm_close,
4216};
4217
4218static int
4219get_mmap_entry(struct kgsl_process_private *private,
4220 struct kgsl_mem_entry **out_entry, unsigned long pgoff,
4221 unsigned long len)
4222{
4223 int ret = 0;
4224 struct kgsl_mem_entry *entry;
4225
4226 entry = kgsl_sharedmem_find_id(private, pgoff);
4227 if (entry == NULL)
4228 entry = kgsl_sharedmem_find(private, pgoff << PAGE_SHIFT);
4229
4230 if (!entry)
4231 return -EINVAL;
4232
4233 if (!entry->memdesc.ops ||
4234 !entry->memdesc.ops->vmflags ||
4235 !entry->memdesc.ops->vmfault) {
4236 ret = -EINVAL;
4237 goto err_put;
4238 }
4239
4240 if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_PHYS) {
4241 if (len != entry->memdesc.size) {
4242 ret = -EINVAL;
4243 goto err_put;
4244 }
4245 }
4246
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304247 /* Don't allow ourselves to remap user memory */
4248 if (entry->memdesc.flags & KGSL_MEMFLAGS_USERMEM_ADDR) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07004249 ret = -EBUSY;
4250 goto err_put;
4251 }
4252
4253 if (kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
4254 if (len != kgsl_memdesc_footprint(&entry->memdesc)) {
4255 ret = -ERANGE;
4256 goto err_put;
4257 }
4258 } else if (len != kgsl_memdesc_footprint(&entry->memdesc) &&
4259 len != entry->memdesc.size) {
4260 /*
4261 * If cpu_map != gpumap then user can map either the
4262 * footprint or the entry size
4263 */
4264 ret = -ERANGE;
4265 goto err_put;
4266 }
4267
4268 *out_entry = entry;
4269 return 0;
4270err_put:
4271 kgsl_mem_entry_put(entry);
4272 return ret;
4273}
4274
4275static unsigned long _gpu_set_svm_region(struct kgsl_process_private *private,
4276 struct kgsl_mem_entry *entry, unsigned long addr,
4277 unsigned long size)
4278{
4279 int ret;
4280
4281 ret = kgsl_mmu_set_svm_region(private->pagetable, (uint64_t) addr,
4282 (uint64_t) size);
4283
4284 if (ret != 0)
4285 return ret;
4286
4287 entry->memdesc.gpuaddr = (uint64_t) addr;
4288 entry->memdesc.pagetable = private->pagetable;
4289
4290 ret = kgsl_mmu_map(private->pagetable, &entry->memdesc);
4291 if (ret) {
4292 kgsl_mmu_put_gpuaddr(&entry->memdesc);
4293 return ret;
4294 }
4295
4296 kgsl_memfree_purge(private->pagetable, entry->memdesc.gpuaddr,
4297 entry->memdesc.size);
4298
4299 return addr;
4300}
4301
4302static unsigned long _gpu_find_svm(struct kgsl_process_private *private,
4303 unsigned long start, unsigned long end, unsigned long len,
4304 unsigned int align)
4305{
4306 uint64_t addr = kgsl_mmu_find_svm_region(private->pagetable,
4307 (uint64_t) start, (uint64_t)end, (uint64_t) len, align);
4308
4309 BUG_ON(!IS_ERR_VALUE((unsigned long)addr) && (addr > ULONG_MAX));
4310
4311 return (unsigned long) addr;
4312}
4313
4314/* Search top down in the CPU VM region for a free address */
4315static unsigned long _cpu_get_unmapped_area(unsigned long bottom,
4316 unsigned long top, unsigned long len, unsigned long align)
4317{
4318 struct vm_unmapped_area_info info;
4319 unsigned long addr, err;
4320
4321 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
4322 info.low_limit = bottom;
4323 info.high_limit = top;
4324 info.length = len;
4325 info.align_offset = 0;
4326 info.align_mask = align - 1;
4327
4328 addr = vm_unmapped_area(&info);
4329
4330 if (IS_ERR_VALUE(addr))
4331 return addr;
4332
4333 err = security_mmap_addr(addr);
4334 return err ? err : addr;
4335}
4336
4337static unsigned long _search_range(struct kgsl_process_private *private,
4338 struct kgsl_mem_entry *entry,
4339 unsigned long start, unsigned long end,
4340 unsigned long len, uint64_t align)
4341{
4342 unsigned long cpu, gpu = end, result = -ENOMEM;
4343
4344 while (gpu > start) {
4345 /* find a new empty spot on the CPU below the last one */
4346 cpu = _cpu_get_unmapped_area(start, gpu, len,
4347 (unsigned long) align);
4348 if (IS_ERR_VALUE(cpu)) {
4349 result = cpu;
4350 break;
4351 }
4352 /* try to map it on the GPU */
4353 result = _gpu_set_svm_region(private, entry, cpu, len);
4354 if (!IS_ERR_VALUE(result))
4355 break;
4356
4357 trace_kgsl_mem_unmapped_area_collision(entry, cpu, len);
4358
4359 if (cpu <= start) {
4360 result = -ENOMEM;
4361 break;
4362 }
4363
4364 /* move downward to the next empty spot on the GPU */
4365 gpu = _gpu_find_svm(private, start, cpu, len, align);
4366 if (IS_ERR_VALUE(gpu)) {
4367 result = gpu;
4368 break;
4369 }
4370
4371 /* Check that_gpu_find_svm doesn't put us in a loop */
4372 if (gpu >= cpu) {
4373 result = -ENOMEM;
4374 break;
4375 }
4376
4377 /* Break if the recommended GPU address is out of range */
4378 if (gpu < start) {
4379 result = -ENOMEM;
4380 break;
4381 }
4382
4383 /*
4384 * Add the length of the chunk to the GPU address to yield the
4385 * upper bound for the CPU search
4386 */
4387 gpu += len;
4388 }
4389 return result;
4390}
4391
4392static unsigned long _get_svm_area(struct kgsl_process_private *private,
4393 struct kgsl_mem_entry *entry, unsigned long hint,
4394 unsigned long len, unsigned long flags)
4395{
4396 uint64_t start, end;
4397 int align_shift = kgsl_memdesc_get_align(&entry->memdesc);
4398 uint64_t align;
4399 unsigned long result;
4400 unsigned long addr;
4401
4402 if (align_shift >= ilog2(SZ_2M))
4403 align = SZ_2M;
4404 else if (align_shift >= ilog2(SZ_1M))
4405 align = SZ_1M;
4406 else if (align_shift >= ilog2(SZ_64K))
4407 align = SZ_64K;
4408 else
4409 align = SZ_4K;
4410
4411 /* get the GPU pagetable's SVM range */
4412 if (kgsl_mmu_svm_range(private->pagetable, &start, &end,
4413 entry->memdesc.flags))
4414 return -ERANGE;
4415
4416 /* now clamp the range based on the CPU's requirements */
4417 start = max_t(uint64_t, start, mmap_min_addr);
4418 end = min_t(uint64_t, end, current->mm->mmap_base);
4419 if (start >= end)
4420 return -ERANGE;
4421
4422 if (flags & MAP_FIXED) {
4423 /* we must use addr 'hint' or fail */
4424 return _gpu_set_svm_region(private, entry, hint, len);
4425 } else if (hint != 0) {
4426 struct vm_area_struct *vma;
4427
4428 /*
4429 * See if the hint is usable, if not we will use
4430 * it as the start point for searching.
4431 */
4432 addr = clamp_t(unsigned long, hint & ~(align - 1),
4433 start, (end - len) & ~(align - 1));
4434
4435 vma = find_vma(current->mm, addr);
4436
4437 if (vma == NULL || ((addr + len) <= vma->vm_start)) {
4438 result = _gpu_set_svm_region(private, entry, addr, len);
4439
4440 /* On failure drop down to keep searching */
4441 if (!IS_ERR_VALUE(result))
4442 return result;
4443 }
4444 } else {
4445 /* no hint, start search at the top and work down */
4446 addr = end & ~(align - 1);
4447 }
4448
4449 /*
4450 * Search downwards from the hint first. If that fails we
4451 * must try to search above it.
4452 */
4453 result = _search_range(private, entry, start, addr, len, align);
4454 if (IS_ERR_VALUE(result) && hint != 0)
4455 result = _search_range(private, entry, addr, end, len, align);
4456
4457 return result;
4458}
4459
4460static unsigned long
4461kgsl_get_unmapped_area(struct file *file, unsigned long addr,
4462 unsigned long len, unsigned long pgoff,
4463 unsigned long flags)
4464{
4465 unsigned long val;
4466 unsigned long vma_offset = pgoff << PAGE_SHIFT;
4467 struct kgsl_device_private *dev_priv = file->private_data;
4468 struct kgsl_process_private *private = dev_priv->process_priv;
4469 struct kgsl_device *device = dev_priv->device;
4470 struct kgsl_mem_entry *entry = NULL;
4471
4472 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4473 return get_unmapped_area(NULL, addr, len, pgoff, flags);
4474
4475 val = get_mmap_entry(private, &entry, pgoff, len);
4476 if (val)
4477 return val;
4478
4479 /* Do not allow CPU mappings for secure buffers */
4480 if (kgsl_memdesc_is_secured(&entry->memdesc)) {
4481 val = -EPERM;
4482 goto put;
4483 }
4484
4485 if (!kgsl_memdesc_use_cpu_map(&entry->memdesc)) {
4486 val = get_unmapped_area(NULL, addr, len, 0, flags);
4487 if (IS_ERR_VALUE(val))
Venkateswara Rao Tadikondad57f7e52017-08-29 11:02:38 +05304488 KGSL_DRV_ERR_RATELIMIT(device,
Shrenuj Bansala419c792016-10-20 14:05:11 -07004489 "get_unmapped_area: pid %d addr %lx pgoff %lx len %ld failed error %d\n",
4490 private->pid, addr, pgoff, len, (int) val);
4491 } else {
4492 val = _get_svm_area(private, entry, addr, len, flags);
4493 if (IS_ERR_VALUE(val))
Venkateswara Rao Tadikondad57f7e52017-08-29 11:02:38 +05304494 KGSL_DRV_ERR_RATELIMIT(device,
Hareesh Gunduca522a12017-02-15 16:02:06 +05304495 "_get_svm_area: pid %d mmap_base %lx addr %lx pgoff %lx len %ld failed error %d\n",
4496 private->pid, current->mm->mmap_base, addr,
4497 pgoff, len, (int) val);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004498 }
4499
4500put:
4501 kgsl_mem_entry_put(entry);
4502 return val;
4503}
4504
4505static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
4506{
4507 unsigned int ret, cache;
4508 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
4509 struct kgsl_device_private *dev_priv = file->private_data;
4510 struct kgsl_process_private *private = dev_priv->process_priv;
4511 struct kgsl_mem_entry *entry = NULL;
4512 struct kgsl_device *device = dev_priv->device;
4513
4514 /* Handle leagacy behavior for memstore */
4515
4516 if (vma_offset == (unsigned long) device->memstore.gpuaddr)
4517 return kgsl_mmap_memstore(device, vma);
4518
4519 /*
4520 * The reference count on the entry that we get from
4521 * get_mmap_entry() will be held until kgsl_gpumem_vm_close().
4522 */
4523 ret = get_mmap_entry(private, &entry, vma->vm_pgoff,
4524 vma->vm_end - vma->vm_start);
4525 if (ret)
4526 return ret;
4527
4528 vma->vm_flags |= entry->memdesc.ops->vmflags;
4529
4530 vma->vm_private_data = entry;
4531
4532 /* Determine user-side caching policy */
4533
4534 cache = kgsl_memdesc_get_cachemode(&entry->memdesc);
4535
4536 switch (cache) {
4537 case KGSL_CACHEMODE_UNCACHED:
4538 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
4539 break;
4540 case KGSL_CACHEMODE_WRITETHROUGH:
4541 vma->vm_page_prot = pgprot_writethroughcache(vma->vm_page_prot);
4542 if (pgprot_val(vma->vm_page_prot) ==
4543 pgprot_val(pgprot_writebackcache(vma->vm_page_prot)))
4544 WARN_ONCE(1, "WRITETHROUGH is deprecated for arm64");
4545 break;
4546 case KGSL_CACHEMODE_WRITEBACK:
4547 vma->vm_page_prot = pgprot_writebackcache(vma->vm_page_prot);
4548 break;
4549 case KGSL_CACHEMODE_WRITECOMBINE:
4550 default:
4551 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
4552 break;
4553 }
4554
4555 vma->vm_ops = &kgsl_gpumem_vm_ops;
4556
4557 if (cache == KGSL_CACHEMODE_WRITEBACK
4558 || cache == KGSL_CACHEMODE_WRITETHROUGH) {
4559 int i;
4560 unsigned long addr = vma->vm_start;
4561 struct kgsl_memdesc *m = &entry->memdesc;
4562
4563 for (i = 0; i < m->page_count; i++) {
4564 struct page *page = m->pages[i];
4565
4566 vm_insert_page(vma, addr, page);
4567 addr += PAGE_SIZE;
4568 }
4569 }
4570
4571 vma->vm_file = file;
4572
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304573 if (atomic_inc_return(&entry->map_count) == 1)
4574 entry->priv->gpumem_mapped += entry->memdesc.size;
Shrenuj Bansala419c792016-10-20 14:05:11 -07004575
Jordan Crouse6bce65c2020-12-28 16:06:42 +05304576 trace_kgsl_mem_mmap(entry, vma->vm_start);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004577 return 0;
4578}
4579
4580static irqreturn_t kgsl_irq_handler(int irq, void *data)
4581{
4582 struct kgsl_device *device = data;
4583
4584 return device->ftbl->irq_handler(device);
4585
4586}
4587
4588#define KGSL_READ_MESSAGE "OH HAI GPU\n"
4589
4590static ssize_t kgsl_read(struct file *filep, char __user *buf, size_t count,
4591 loff_t *pos)
4592{
4593 return simple_read_from_buffer(buf, count, pos,
4594 KGSL_READ_MESSAGE, strlen(KGSL_READ_MESSAGE) + 1);
4595}
4596
4597static const struct file_operations kgsl_fops = {
4598 .owner = THIS_MODULE,
4599 .release = kgsl_release,
4600 .open = kgsl_open,
4601 .mmap = kgsl_mmap,
4602 .read = kgsl_read,
4603 .get_unmapped_area = kgsl_get_unmapped_area,
4604 .unlocked_ioctl = kgsl_ioctl,
4605 .compat_ioctl = kgsl_compat_ioctl,
4606};
4607
4608struct kgsl_driver kgsl_driver = {
4609 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
4610 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
4611 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
4612 /*
4613 * Full cache flushes are faster than line by line on at least
4614 * 8064 and 8974 once the region to be flushed is > 16mb.
4615 */
4616 .full_cache_threshold = SZ_16M,
4617
4618 .stats.vmalloc = ATOMIC_LONG_INIT(0),
4619 .stats.vmalloc_max = ATOMIC_LONG_INIT(0),
4620 .stats.page_alloc = ATOMIC_LONG_INIT(0),
4621 .stats.page_alloc_max = ATOMIC_LONG_INIT(0),
4622 .stats.coherent = ATOMIC_LONG_INIT(0),
4623 .stats.coherent_max = ATOMIC_LONG_INIT(0),
4624 .stats.secure = ATOMIC_LONG_INIT(0),
4625 .stats.secure_max = ATOMIC_LONG_INIT(0),
4626 .stats.mapped = ATOMIC_LONG_INIT(0),
4627 .stats.mapped_max = ATOMIC_LONG_INIT(0),
4628};
4629EXPORT_SYMBOL(kgsl_driver);
4630
4631static void _unregister_device(struct kgsl_device *device)
4632{
4633 int minor;
4634
4635 mutex_lock(&kgsl_driver.devlock);
4636 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4637 if (device == kgsl_driver.devp[minor])
4638 break;
4639 }
4640 if (minor != KGSL_DEVICE_MAX) {
4641 device_destroy(kgsl_driver.class,
4642 MKDEV(MAJOR(kgsl_driver.major), minor));
4643 kgsl_driver.devp[minor] = NULL;
4644 }
4645 mutex_unlock(&kgsl_driver.devlock);
4646}
4647
4648static int _register_device(struct kgsl_device *device)
4649{
4650 int minor, ret;
4651 dev_t dev;
4652
4653 /* Find a minor for the device */
4654
4655 mutex_lock(&kgsl_driver.devlock);
4656 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
4657 if (kgsl_driver.devp[minor] == NULL) {
4658 kgsl_driver.devp[minor] = device;
4659 break;
4660 }
4661 }
4662 mutex_unlock(&kgsl_driver.devlock);
4663
4664 if (minor == KGSL_DEVICE_MAX) {
4665 KGSL_CORE_ERR("minor devices exhausted\n");
4666 return -ENODEV;
4667 }
4668
4669 /* Create the device */
4670 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
4671 device->dev = device_create(kgsl_driver.class,
4672 &device->pdev->dev,
4673 dev, device,
4674 device->name);
4675
4676 if (IS_ERR(device->dev)) {
4677 mutex_lock(&kgsl_driver.devlock);
4678 kgsl_driver.devp[minor] = NULL;
4679 mutex_unlock(&kgsl_driver.devlock);
4680 ret = PTR_ERR(device->dev);
4681 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
4682 return ret;
4683 }
4684
4685 dev_set_drvdata(&device->pdev->dev, device);
4686 return 0;
4687}
4688
4689int kgsl_device_platform_probe(struct kgsl_device *device)
4690{
4691 int status = -EINVAL;
4692 struct resource *res;
4693 int cpu;
4694
4695 status = _register_device(device);
4696 if (status)
4697 return status;
4698
4699 /* Initialize logging first, so that failures below actually print. */
4700 kgsl_device_debugfs_init(device);
4701
Harshdeep Dhatt67d23bf2019-09-09 11:05:48 -06004702 /* Disable the sparse ioctl invocation as they are not used */
4703 device->flags &= ~KGSL_FLAG_SPARSE;
4704
Shrenuj Bansala419c792016-10-20 14:05:11 -07004705 status = kgsl_pwrctrl_init(device);
4706 if (status)
4707 goto error;
4708
Shrenuj Bansala419c792016-10-20 14:05:11 -07004709 /*
4710 * Check if a shadermemname is defined, and then get shader memory
4711 * details including shader memory starting physical address
4712 * and shader memory length
4713 */
4714 if (device->shadermemname != NULL) {
4715 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
4716 device->shadermemname);
4717
4718 if (res == NULL) {
4719 KGSL_DRV_WARN(device,
4720 "Shader memory: platform_get_resource_byname failed\n");
4721 }
4722
4723 else {
4724 device->shader_mem_phys = res->start;
4725 device->shader_mem_len = resource_size(res);
4726 }
4727
4728 if (!devm_request_mem_region(device->dev,
4729 device->shader_mem_phys,
4730 device->shader_mem_len,
4731 device->name)) {
4732 KGSL_DRV_WARN(device, "request_mem_region_failed\n");
4733 }
4734 }
4735
4736 if (!devm_request_mem_region(device->dev, device->reg_phys,
4737 device->reg_len, device->name)) {
4738 KGSL_DRV_ERR(device, "request_mem_region failed\n");
4739 status = -ENODEV;
4740 goto error_pwrctrl_close;
4741 }
4742
4743 device->reg_virt = devm_ioremap(device->dev, device->reg_phys,
4744 device->reg_len);
4745
4746 if (device->reg_virt == NULL) {
4747 KGSL_DRV_ERR(device, "ioremap failed\n");
4748 status = -ENODEV;
4749 goto error_pwrctrl_close;
4750 }
4751 /*acquire interrupt */
4752 device->pwrctrl.interrupt_num =
4753 platform_get_irq_byname(device->pdev, device->pwrctrl.irq_name);
4754
4755 if (device->pwrctrl.interrupt_num <= 0) {
4756 KGSL_DRV_ERR(device, "platform_get_irq_byname failed: %d\n",
4757 device->pwrctrl.interrupt_num);
4758 status = -EINVAL;
4759 goto error_pwrctrl_close;
4760 }
4761
4762 status = devm_request_irq(device->dev, device->pwrctrl.interrupt_num,
4763 kgsl_irq_handler, IRQF_TRIGGER_HIGH,
4764 device->name, device);
4765 if (status) {
4766 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
4767 device->pwrctrl.interrupt_num, status);
4768 goto error_pwrctrl_close;
4769 }
4770 disable_irq(device->pwrctrl.interrupt_num);
4771
4772 KGSL_DRV_INFO(device,
4773 "dev_id %d regs phys 0x%08lx size 0x%08x\n",
4774 device->id, device->reg_phys, device->reg_len);
4775
4776 rwlock_init(&device->context_lock);
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05304777 spin_lock_init(&device->submit_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004778
4779 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
4780
4781 status = kgsl_mmu_probe(device, kgsl_mmu_type);
4782 if (status != 0)
4783 goto error_pwrctrl_close;
4784
4785 /* Check to see if our device can perform DMA correctly */
4786 status = dma_set_coherent_mask(&device->pdev->dev, KGSL_DMA_BIT_MASK);
4787 if (status)
4788 goto error_close_mmu;
4789
4790 /* Initialize the memory pools */
4791 kgsl_init_page_pools(device->pdev);
4792
4793 status = kgsl_allocate_global(device, &device->memstore,
4794 KGSL_MEMSTORE_SIZE, 0, KGSL_MEMDESC_CONTIG, "memstore");
4795
4796 if (status != 0)
4797 goto error_close_mmu;
4798
Shrenuj Bansala419c792016-10-20 14:05:11 -07004799 /*
4800 * The default request type PM_QOS_REQ_ALL_CORES is
4801 * applicable to all CPU cores that are online and
4802 * would have a power impact when there are more
4803 * number of CPUs. PM_QOS_REQ_AFFINE_IRQ request
4804 * type shall update/apply the vote only to that CPU to
4805 * which IRQ's affinity is set to.
4806 */
4807#ifdef CONFIG_SMP
4808
4809 device->pwrctrl.pm_qos_req_dma.type = PM_QOS_REQ_AFFINE_IRQ;
4810 device->pwrctrl.pm_qos_req_dma.irq = device->pwrctrl.interrupt_num;
4811
4812#endif
4813 pm_qos_add_request(&device->pwrctrl.pm_qos_req_dma,
4814 PM_QOS_CPU_DMA_LATENCY,
4815 PM_QOS_DEFAULT_VALUE);
4816
4817 if (device->pwrctrl.l2pc_cpus_mask) {
4818
4819 device->pwrctrl.l2pc_cpus_qos.type =
4820 PM_QOS_REQ_AFFINE_CORES;
4821 cpumask_empty(&device->pwrctrl.l2pc_cpus_qos.cpus_affine);
4822 for_each_possible_cpu(cpu) {
4823 if ((1 << cpu) & device->pwrctrl.l2pc_cpus_mask)
4824 cpumask_set_cpu(cpu, &device->pwrctrl.
4825 l2pc_cpus_qos.cpus_affine);
4826 }
4827
4828 pm_qos_add_request(&device->pwrctrl.l2pc_cpus_qos,
4829 PM_QOS_CPU_DMA_LATENCY,
4830 PM_QOS_DEFAULT_VALUE);
4831 }
4832
4833 device->events_wq = alloc_workqueue("kgsl-events",
4834 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4835
4836 /* Initialize the snapshot engine */
4837 kgsl_device_snapshot_init(device);
4838
4839 /* Initialize common sysfs entries */
4840 kgsl_pwrctrl_init_sysfs(device);
4841
4842 return 0;
4843
Shrenuj Bansala419c792016-10-20 14:05:11 -07004844error_close_mmu:
4845 kgsl_mmu_close(device);
4846error_pwrctrl_close:
4847 kgsl_pwrctrl_close(device);
4848error:
Lynus Vaz519dacfd2017-02-14 12:17:37 +05304849 kgsl_device_debugfs_close(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004850 _unregister_device(device);
4851 return status;
4852}
4853EXPORT_SYMBOL(kgsl_device_platform_probe);
4854
4855void kgsl_device_platform_remove(struct kgsl_device *device)
4856{
4857 destroy_workqueue(device->events_wq);
4858
4859 kgsl_device_snapshot_close(device);
4860
4861 kgsl_exit_page_pools();
4862
4863 kgsl_pwrctrl_uninit_sysfs(device);
4864
4865 pm_qos_remove_request(&device->pwrctrl.pm_qos_req_dma);
4866 if (device->pwrctrl.l2pc_cpus_mask)
4867 pm_qos_remove_request(&device->pwrctrl.l2pc_cpus_qos);
4868
4869 idr_destroy(&device->context_idr);
4870
Shrenuj Bansala419c792016-10-20 14:05:11 -07004871 kgsl_free_global(device, &device->memstore);
4872
4873 kgsl_mmu_close(device);
4874
4875 kgsl_pwrctrl_close(device);
4876
Lynus Vaz519dacfd2017-02-14 12:17:37 +05304877 kgsl_device_debugfs_close(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004878 _unregister_device(device);
4879}
4880EXPORT_SYMBOL(kgsl_device_platform_remove);
4881
4882static void kgsl_core_exit(void)
4883{
4884 kgsl_events_exit();
4885 kgsl_core_debugfs_close();
4886
4887 /*
4888 * We call kgsl_sharedmem_uninit_sysfs() and device_unregister()
4889 * only if kgsl_driver.virtdev has been populated.
4890 * We check at least one member of kgsl_driver.virtdev to
4891 * see if it is not NULL (and thus, has been populated).
4892 */
4893 if (kgsl_driver.virtdev.class) {
4894 kgsl_sharedmem_uninit_sysfs();
4895 device_unregister(&kgsl_driver.virtdev);
4896 }
4897
4898 if (kgsl_driver.class) {
4899 class_destroy(kgsl_driver.class);
4900 kgsl_driver.class = NULL;
4901 }
4902
Tarun Karra2b8b3632016-11-14 16:38:27 -08004903 kgsl_drawobjs_cache_exit();
Shrenuj Bansala419c792016-10-20 14:05:11 -07004904
4905 kgsl_memfree_exit();
4906 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
4907}
4908
4909static int __init kgsl_core_init(void)
4910{
4911 int result = 0;
Tim Murray85040432017-02-20 15:59:32 +05304912 struct sched_param param = { .sched_priority = 2 };
4913
Shrenuj Bansala419c792016-10-20 14:05:11 -07004914 /* alloc major and minor device numbers */
4915 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
4916 "kgsl");
4917
4918 if (result < 0) {
4919
4920 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
4921 goto err;
4922 }
4923
4924 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
4925 kgsl_driver.cdev.owner = THIS_MODULE;
4926 kgsl_driver.cdev.ops = &kgsl_fops;
4927 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
4928 KGSL_DEVICE_MAX);
4929
4930 if (result) {
4931 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d, result= %d\n",
4932 kgsl_driver.major, result);
4933 goto err;
4934 }
4935
4936 kgsl_driver.class = class_create(THIS_MODULE, "kgsl");
4937
4938 if (IS_ERR(kgsl_driver.class)) {
4939 result = PTR_ERR(kgsl_driver.class);
4940 KGSL_CORE_ERR("failed to create class for kgsl");
4941 goto err;
4942 }
4943
4944 /*
4945 * Make a virtual device for managing core related things
4946 * in sysfs
4947 */
4948 kgsl_driver.virtdev.class = kgsl_driver.class;
4949 dev_set_name(&kgsl_driver.virtdev, "kgsl");
4950 result = device_register(&kgsl_driver.virtdev);
4951 if (result) {
4952 KGSL_CORE_ERR("driver_register failed\n");
4953 goto err;
4954 }
4955
4956 /* Make kobjects in the virtual device for storing statistics */
4957
4958 kgsl_driver.ptkobj =
4959 kobject_create_and_add("pagetables",
4960 &kgsl_driver.virtdev.kobj);
4961
4962 kgsl_driver.prockobj =
4963 kobject_create_and_add("proc",
4964 &kgsl_driver.virtdev.kobj);
4965
4966 kgsl_core_debugfs_init();
4967
4968 kgsl_sharedmem_init_sysfs();
4969
4970 INIT_LIST_HEAD(&kgsl_driver.process_list);
4971
4972 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
4973
4974 kgsl_driver.workqueue = alloc_workqueue("kgsl-workqueue",
4975 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
4976
4977 kgsl_driver.mem_workqueue = alloc_workqueue("kgsl-mementry",
Hareesh Gundu615439d2017-06-16 17:06:57 +05304978 WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Shrenuj Bansala419c792016-10-20 14:05:11 -07004979
Tim Murray85040432017-02-20 15:59:32 +05304980 kthread_init_worker(&kgsl_driver.worker);
4981
4982 kgsl_driver.worker_thread = kthread_run(kthread_worker_fn,
4983 &kgsl_driver.worker, "kgsl_worker_thread");
4984
4985 if (IS_ERR(kgsl_driver.worker_thread)) {
4986 pr_err("unable to start kgsl thread\n");
4987 goto err;
4988 }
4989
4990 sched_setscheduler(kgsl_driver.worker_thread, SCHED_FIFO, &param);
4991
Shrenuj Bansala419c792016-10-20 14:05:11 -07004992 kgsl_events_init();
4993
Tarun Karra2b8b3632016-11-14 16:38:27 -08004994 result = kgsl_drawobjs_cache_init();
Shrenuj Bansala419c792016-10-20 14:05:11 -07004995 if (result)
4996 goto err;
4997
4998 kgsl_memfree_init();
4999
5000 return 0;
5001
5002err:
5003 kgsl_core_exit();
5004 return result;
5005}
5006
5007module_init(kgsl_core_init);
5008module_exit(kgsl_core_exit);
5009
5010MODULE_DESCRIPTION("MSM GPU driver");
5011MODULE_LICENSE("GPL");