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