2 * drivers/video/tegra/nvmap/nvmap.h
4 * GPU memory management driver for Tegra
6 * Copyright (c) 2009-2015, NVIDIA CORPORATION. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #ifndef __VIDEO_TEGRA_NVMAP_NVMAP_H
24 #define __VIDEO_TEGRA_NVMAP_NVMAP_H
26 #include <linux/list.h>
28 #include <linux/mutex.h>
29 #include <linux/rbtree.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/atomic.h>
33 #include <linux/dma-buf.h>
34 #include <linux/syscalls.h>
36 #include <linux/miscdevice.h>
37 #include <linux/nvmap.h>
38 #include <linux/vmalloc.h>
39 #include <linux/slab.h>
41 #include <linux/workqueue.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/dma-direction.h>
44 #include <linux/platform_device.h>
46 #include <asm/cacheflush.h>
48 #include <asm/outercache.h>
50 #include "nvmap_heap.h"
52 #ifdef CONFIG_NVMAP_HIGHMEM_ONLY
53 #define __GFP_NVMAP __GFP_HIGHMEM
55 #define __GFP_NVMAP (GFP_KERNEL | __GFP_HIGHMEM)
58 #define GFP_NVMAP (__GFP_NVMAP | __GFP_NOWARN)
61 #define NVMAP_LAZY_VFREE
67 void _nvmap_handle_free(struct nvmap_handle *h);
68 /* holds max number of handles allocted per process at any time */
69 extern u32 nvmap_max_handle_count;
71 /* If set force zeroed memory to userspace. */
72 extern bool zero_memory;
75 #define PG_PROT_KERNEL PAGE_KERNEL
76 #define FLUSH_DCACHE_AREA __flush_dcache_area
77 #define outer_flush_range(s, e)
78 #define outer_inv_range(s, e)
79 #define outer_clean_range(s, e)
80 #define outer_flush_all()
81 #define outer_clean_all()
82 extern void __clean_dcache_page(struct page *);
83 extern void __flush_dcache_page(struct page *);
85 #define PG_PROT_KERNEL pgprot_kernel
86 #define FLUSH_DCACHE_AREA __cpuc_flush_dcache_area
87 extern void __flush_dcache_page(struct address_space *, struct page *);
90 struct nvmap_vma_list {
91 struct list_head list;
92 struct vm_area_struct *vma;
96 struct nvmap_carveout_node {
97 unsigned int heap_bit;
98 struct nvmap_heap *carveout;
104 /* handles allocated using shared system memory (either IOVMM- or high-order
105 * page allocations */
106 struct nvmap_pgalloc {
108 bool contig; /* contiguous system memory */
109 atomic_t ndirty; /* count number of dirty pages */
112 struct nvmap_handle {
113 struct rb_node node; /* entry on global handle tree */
114 atomic_t ref; /* reference count (i.e., # of duplications) */
115 atomic_t pin; /* pin count */
116 u32 flags; /* caching flags */
117 size_t size; /* padded (as-allocated) size */
118 size_t orig_size; /* original (as-requested) size */
120 u8 kind; /* memory kind (0=pitch, !0 -> blocklinear) */
121 struct nvmap_client *owner;
124 * dma_buf necessities. An attachment is made on dma_buf allocation to
125 * facilitate the nvmap_pin* APIs.
127 struct dma_buf *dmabuf;
128 struct dma_buf_attachment *attachment;
131 struct nvmap_pgalloc pgalloc;
132 struct nvmap_heap_block *carveout;
134 bool heap_pgalloc; /* handle is page allocated (sysmem / iovmm) */
135 bool alloc; /* handle has memory allocated */
136 u32 heap_type; /* handle heap is allocated from */
137 u32 userflags; /* flags passed from userspace */
138 void *vaddr; /* mapping used inside kernel */
139 struct list_head vmas; /* list of all user vma's */
140 atomic_t umap_count; /* number of outstanding maps from user */
141 atomic_t kmap_count; /* number of outstanding map from kernel */
142 atomic_t share_count; /* number of processes sharing the handle */
143 struct list_head lru; /* list head to track the lru */
145 void *nvhost_priv; /* nvhost private data */
146 void (*nvhost_priv_delete)(void *priv);
148 int peer; /* Peer VM number */
151 /* handle_ref objects are client-local references to an nvmap_handle;
152 * they are distinct objects so that handles can be unpinned and
153 * unreferenced the correct number of times when a client abnormally
155 struct nvmap_handle_ref {
156 struct nvmap_handle *handle;
158 atomic_t dupes; /* number of times to free on file close */
159 atomic_t pin; /* number of times to unpin on free */
162 #ifdef CONFIG_NVMAP_PAGE_POOLS
165 * This is the default ratio defining pool size. It can be thought of as pool
166 * size in either MB per GB or KB per MB. That means the max this number can
167 * be is 1024 (all physical memory - not a very good idea) or 0 (no page pool
170 #define NVMAP_PP_POOL_SIZE (128)
173 * The wakeup threshold is how many empty page slots there need to be in order
174 * for the background allocater to be woken up.
176 #define NVMAP_PP_DEF_FILL_THRESH (4096)
179 * For when memory does not require zeroing this is the minimum number of pages
180 * remaining in the page pools before the background allocer is woken up. This
181 * essentially disables the background allocator (unless its extremely small).
183 #define NVMAP_PP_DEF_ZERO_MEM_FILL_MIN (2048)
186 * Minimum amount of memory we try to keep available. That is if memory sinks
187 * below this amount the page pools will stop filling.
189 #define NVMAP_PP_DEF_MIN_AVAILABLE_MB (128)
191 struct nvmap_page_pool {
193 u32 count; /* Number of pages in the page list. */
194 u32 max; /* Max length of the page list. */
195 int to_zero; /* Number of pages on the zero list */
196 struct list_head page_list;
197 struct list_head zero_list;
200 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
208 int nvmap_page_pool_init(struct nvmap_device *dev);
209 int nvmap_page_pool_fini(struct nvmap_device *dev);
210 struct page *nvmap_page_pool_alloc(struct nvmap_page_pool *pool);
211 int nvmap_page_pool_alloc_lots(struct nvmap_page_pool *pool,
212 struct page **pages, u32 nr);
213 int nvmap_page_pool_fill_lots(struct nvmap_page_pool *pool,
214 struct page **pages, u32 nr);
215 int nvmap_page_pool_clear(void);
216 int nvmap_page_pool_debugfs_init(struct dentry *nvmap_root);
219 #define NVMAP_IVM_INVALID_PEER (-1)
221 struct nvmap_client {
223 struct rb_root handle_refs;
224 struct mutex ref_lock;
227 struct task_struct *task;
228 struct list_head list;
234 struct nvmap_vma_priv {
235 struct nvmap_handle *handle;
237 atomic_t count; /* number of processes cloning the VMA */
240 struct nvmap_device {
241 struct rb_root handles;
242 spinlock_t handle_lock;
243 struct miscdevice dev_user;
244 struct nvmap_carveout_node *heaps;
246 #ifdef CONFIG_NVMAP_PAGE_POOLS
247 struct nvmap_page_pool pool;
249 struct list_head clients;
251 struct mutex clients_lock;
252 struct list_head lru_handles;
254 struct dentry *handles_by_pid;
275 atomic64_t stats[NS_NUM];
279 extern struct nvmap_stats nvmap_stats;
280 extern struct nvmap_device *nvmap_dev;
282 void nvmap_stats_inc(enum nvmap_stats_t, size_t size);
283 void nvmap_stats_dec(enum nvmap_stats_t, size_t size);
284 u64 nvmap_stats_read(enum nvmap_stats_t);
286 static inline void nvmap_ref_lock(struct nvmap_client *priv)
288 mutex_lock(&priv->ref_lock);
291 static inline void nvmap_ref_unlock(struct nvmap_client *priv)
293 mutex_unlock(&priv->ref_lock);
297 * NOTE: this does not ensure the continued existence of the underlying
298 * dma_buf. If you want ensure the existence of the dma_buf you must get an
299 * nvmap_handle_ref as that is what tracks the dma_buf refs.
301 static inline struct nvmap_handle *nvmap_handle_get(struct nvmap_handle *h)
303 if (WARN_ON(!virt_addr_valid(h))) {
304 pr_err("%s: invalid handle\n", current->group_leader->comm);
308 if (unlikely(atomic_inc_return(&h->ref) <= 1)) {
309 pr_err("%s: %s attempt to get a freed handle\n",
310 __func__, current->group_leader->comm);
317 static inline pgprot_t nvmap_pgprot(struct nvmap_handle *h, pgprot_t prot)
319 if (h->heap_type == NVMAP_HEAP_CARVEOUT_VPR) {
320 #ifdef pgprot_device_writecombine
321 return pgprot_device_writecombine(prot);
323 return pgprot_noncached(prot);
327 if (h->flags == NVMAP_HANDLE_UNCACHEABLE) {
329 if (h->owner && !h->owner->warned) {
330 char task_comm[TASK_COMM_LEN];
331 h->owner->warned = 1;
332 get_task_comm(task_comm, h->owner->task);
333 pr_err("PID %d: %s: TAG: 0x%04x WARNING: "
334 "NVMAP_HANDLE_WRITE_COMBINE "
335 "should be used in place of "
336 "NVMAP_HANDLE_UNCACHEABLE on ARM64\n",
337 h->owner->task->pid, task_comm,
341 return pgprot_noncached(prot);
343 else if (h->flags == NVMAP_HANDLE_WRITE_COMBINE)
344 return pgprot_dmacoherent(prot);
348 int nvmap_probe(struct platform_device *pdev);
349 int nvmap_remove(struct platform_device *pdev);
350 int nvmap_init(struct platform_device *pdev);
352 struct nvmap_heap_block *nvmap_carveout_alloc(struct nvmap_client *dev,
353 struct nvmap_handle *handle,
357 unsigned long nvmap_carveout_usage(struct nvmap_client *c,
358 struct nvmap_heap_block *b);
360 struct nvmap_carveout_node;
362 void nvmap_handle_put(struct nvmap_handle *h);
364 struct nvmap_handle_ref *__nvmap_validate_locked(struct nvmap_client *priv,
365 struct nvmap_handle *h);
367 struct nvmap_handle *nvmap_validate_get(struct nvmap_handle *h);
369 struct nvmap_handle_ref *nvmap_create_handle(struct nvmap_client *client,
372 struct nvmap_handle_ref *nvmap_duplicate_handle(struct nvmap_client *client,
373 struct nvmap_handle *h, bool skip_val);
375 struct nvmap_handle *nvmap_validate_get_by_ivmid(struct nvmap_client *client,
376 unsigned int ivm_id);
378 struct nvmap_handle_ref *nvmap_create_handle_from_fd(
379 struct nvmap_client *client, int fd);
381 void inner_cache_maint(unsigned int op, void *vaddr, size_t size);
382 void outer_cache_maint(unsigned int op, phys_addr_t paddr, size_t size);
384 int nvmap_alloc_handle(struct nvmap_client *client,
385 struct nvmap_handle *h, unsigned int heap_mask,
386 size_t align, u8 kind,
387 unsigned int flags, int peer);
389 void nvmap_free_handle(struct nvmap_client *c, struct nvmap_handle *h);
391 void nvmap_free_handle_fd(struct nvmap_client *c, int fd);
393 int nvmap_pin_handles(struct nvmap_client *client, unsigned int nr,
394 struct nvmap_handle * const *handles);
396 void nvmap_unpin_handles(struct nvmap_client *priv, unsigned int nr,
397 struct nvmap_handle * const *handles);
399 int nvmap_handle_remove(struct nvmap_device *dev, struct nvmap_handle *h);
401 void nvmap_handle_add(struct nvmap_device *dev, struct nvmap_handle *h);
403 int is_nvmap_vma(struct vm_area_struct *vma);
405 int nvmap_get_dmabuf_fd(struct nvmap_client *client, struct nvmap_handle *h);
406 struct nvmap_handle *nvmap_handle_get_from_dmabuf_fd(
407 struct nvmap_client *client, int fd);
409 int nvmap_get_handle_param(struct nvmap_client *client,
410 struct nvmap_handle_ref *ref, u32 param, u64 *result);
412 struct nvmap_client *nvmap_client_get(struct nvmap_client *client);
414 void nvmap_client_put(struct nvmap_client *c);
416 struct nvmap_handle *nvmap_handle_get_from_fd(int fd);
418 /* MM definitions. */
419 extern size_t cache_maint_inner_threshold;
420 extern size_t cache_maint_outer_threshold;
422 extern void v7_flush_kern_cache_all(void);
423 extern void v7_clean_kern_cache_all(void *);
424 extern void __flush_dcache_all(void *arg);
425 extern void __clean_dcache_all(void *arg);
427 void inner_flush_cache_all(void);
428 void inner_clean_cache_all(void);
429 void nvmap_clean_cache(struct page **pages, int numpages);
430 void nvmap_clean_cache_page(struct page *page);
431 void nvmap_flush_cache(struct page **pages, int numpages);
433 int nvmap_do_cache_maint_list(struct nvmap_handle **handles, u32 *offsets,
434 u32 *sizes, int op, int nr);
436 /* Internal API to support dmabuf */
437 struct dma_buf *__nvmap_dmabuf_export(struct nvmap_client *client,
438 struct nvmap_handle *handle);
439 struct dma_buf *__nvmap_make_dmabuf(struct nvmap_client *client,
440 struct nvmap_handle *handle);
441 struct sg_table *__nvmap_sg_table(struct nvmap_client *client,
442 struct nvmap_handle *h);
443 void __nvmap_free_sg_table(struct nvmap_client *client,
444 struct nvmap_handle *h, struct sg_table *sgt);
445 void *__nvmap_kmap(struct nvmap_handle *h, unsigned int pagenum);
446 void __nvmap_kunmap(struct nvmap_handle *h, unsigned int pagenum, void *addr);
447 void *__nvmap_mmap(struct nvmap_handle *h);
448 void __nvmap_munmap(struct nvmap_handle *h, void *addr);
449 int __nvmap_map(struct nvmap_handle *h, struct vm_area_struct *vma);
450 int __nvmap_get_handle_param(struct nvmap_client *client,
451 struct nvmap_handle *h, u32 param, u64 *result);
452 int __nvmap_do_cache_maint(struct nvmap_client *client, struct nvmap_handle *h,
453 unsigned long start, unsigned long end,
454 unsigned int op, bool clean_only_dirty);
455 struct nvmap_client *__nvmap_create_client(struct nvmap_device *dev,
457 struct dma_buf *__nvmap_dmabuf_export_from_ref(struct nvmap_handle_ref *ref);
458 struct nvmap_handle *__nvmap_ref_to_handle(struct nvmap_handle_ref *ref);
459 int __nvmap_pin(struct nvmap_handle_ref *ref, phys_addr_t *phys);
460 void __nvmap_unpin(struct nvmap_handle_ref *ref);
461 int __nvmap_dmabuf_fd(struct nvmap_client *client,
462 struct dma_buf *dmabuf, int flags);
464 void nvmap_dmabuf_debugfs_init(struct dentry *nvmap_root);
465 int nvmap_dmabuf_stash_init(void);
467 void *nvmap_altalloc(size_t len);
468 void nvmap_altfree(void *ptr, size_t len);
470 void do_set_pte(struct vm_area_struct *vma, unsigned long address,
471 struct page *page, pte_t *pte, bool write, bool anon);
473 static inline struct page *nvmap_to_page(struct page *page)
475 return (struct page *)((unsigned long)page & ~3UL);
478 static inline bool nvmap_page_dirty(struct page *page)
480 return (unsigned long)page & 1UL;
483 static inline bool nvmap_page_mkdirty(struct page **page)
485 if (nvmap_page_dirty(*page))
487 *page = (struct page *)((unsigned long)*page | 1UL);
491 static inline bool nvmap_page_mkclean(struct page **page)
493 if (!nvmap_page_dirty(*page))
495 *page = (struct page *)((unsigned long)*page & ~1UL);
499 static inline bool nvmap_page_reserved(struct page *page)
501 return !!((unsigned long)page & 2UL);
504 static inline bool nvmap_page_mkreserved(struct page **page)
506 if (nvmap_page_reserved(*page))
508 *page = (struct page *)((unsigned long)*page | 2UL);
512 static inline bool nvmap_page_mkunreserved(struct page **page)
514 if (!nvmap_page_reserved(*page))
516 *page = (struct page *)((unsigned long)*page & ~2UL);
521 * FIXME: assume user space requests for reserve operations
524 static inline int nvmap_handle_mk(struct nvmap_handle *h,
525 u32 offset, u32 size,
526 bool (*fn)(struct page **))
529 int start_page = PAGE_ALIGN(offset) >> PAGE_SHIFT;
530 int end_page = (offset + size) >> PAGE_SHIFT;
532 mutex_lock(&h->lock);
533 if (h->heap_pgalloc) {
534 for (i = start_page; i < end_page; i++)
535 nchanged += fn(&h->pgalloc.pages[i]) ? 1 : 0;
537 mutex_unlock(&h->lock);
541 static inline void nvmap_handle_mkclean(struct nvmap_handle *h,
542 u32 offset, u32 size)
544 int nchanged = nvmap_handle_mk(h, offset, size, nvmap_page_mkclean);
546 atomic_sub(nchanged, &h->pgalloc.ndirty);
549 static inline void nvmap_handle_mkdirty(struct nvmap_handle *h,
550 u32 offset, u32 size)
552 int nchanged = nvmap_handle_mk(h, offset, size, nvmap_page_mkdirty);
554 atomic_sub(nchanged, &h->pgalloc.ndirty);
557 static inline void nvmap_handle_mkunreserved(struct nvmap_handle *h,
558 u32 offset, u32 size)
560 nvmap_handle_mk(h, offset, size, nvmap_page_mkunreserved);
563 static inline void nvmap_handle_mkreserved(struct nvmap_handle *h,
564 u32 offset, u32 size)
566 nvmap_handle_mk(h, offset, size, nvmap_page_mkreserved);
569 static inline struct page **nvmap_pages(struct page **pg_pages, u32 nr_pages)
574 pages = nvmap_altalloc(sizeof(*pages) * nr_pages);
578 for (i = 0; i < nr_pages; i++)
579 pages[i] = nvmap_to_page(pg_pages[i]);
584 void nvmap_zap_handle(struct nvmap_handle *handle, u32 offset, u32 size);
586 void nvmap_zap_handles(struct nvmap_handle **handles, u32 *offsets,
589 void nvmap_vma_open(struct vm_area_struct *vma);
591 int nvmap_reserve_pages(struct nvmap_handle **handles, u32 *offsets,
592 u32 *sizes, u32 nr, u32 op);
594 static inline void nvmap_kmaps_inc(struct nvmap_handle *h)
596 mutex_lock(&h->lock);
597 atomic_inc(&h->kmap_count);
598 mutex_unlock(&h->lock);
601 static inline void nvmap_kmaps_inc_no_lock(struct nvmap_handle *h)
603 atomic_inc(&h->kmap_count);
606 static inline void nvmap_kmaps_dec(struct nvmap_handle *h)
608 atomic_dec(&h->kmap_count);
611 static inline void nvmap_umaps_inc(struct nvmap_handle *h)
613 mutex_lock(&h->lock);
614 atomic_inc(&h->umap_count);
615 mutex_unlock(&h->lock);
618 static inline void nvmap_umaps_dec(struct nvmap_handle *h)
620 atomic_dec(&h->umap_count);
623 static inline void nvmap_lru_add(struct nvmap_handle *h)
625 spin_lock(&nvmap_dev->lru_lock);
626 BUG_ON(!list_empty(&h->lru));
627 list_add_tail(&h->lru, &nvmap_dev->lru_handles);
628 spin_unlock(&nvmap_dev->lru_lock);
631 static inline void nvmap_lru_del(struct nvmap_handle *h)
633 spin_lock(&nvmap_dev->lru_lock);
635 INIT_LIST_HEAD(&h->lru);
636 spin_unlock(&nvmap_dev->lru_lock);
639 static inline void nvmap_lru_reset(struct nvmap_handle *h)
641 spin_lock(&nvmap_dev->lru_lock);
642 BUG_ON(list_empty(&h->lru));
644 list_add_tail(&h->lru, &nvmap_dev->lru_handles);
645 spin_unlock(&nvmap_dev->lru_lock);
648 static inline bool nvmap_handle_track_dirty(struct nvmap_handle *h)
650 if (!h->heap_pgalloc)
653 return h->userflags & (NVMAP_HANDLE_CACHE_SYNC |
654 NVMAP_HANDLE_CACHE_SYNC_AT_RESERVE);
657 void nvmap_dmabuf_release_stashed_maps(struct dma_buf *dmabuf);
659 #endif /* __VIDEO_TEGRA_NVMAP_NVMAP_H */