mali_kbase_mem.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. *
  3. * (C) COPYRIGHT 2010-2016 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /**
  16. * @file mali_kbase_mem.h
  17. * Base kernel memory APIs
  18. */
  19. #ifndef _KBASE_MEM_H_
  20. #define _KBASE_MEM_H_
  21. #ifndef _KBASE_H_
  22. #error "Don't include this file directly, use mali_kbase.h instead"
  23. #endif
  24. #include <linux/kref.h>
  25. #ifdef CONFIG_KDS
  26. #include <linux/kds.h>
  27. #endif
  28. #ifdef CONFIG_UMP
  29. #include <linux/ump.h>
  30. #endif
  31. #include "mali_base_kernel.h"
  32. #include <mali_kbase_hw.h>
  33. #include "mali_kbase_pm.h"
  34. #include "mali_kbase_defs.h"
  35. /* Required for kbase_mem_evictable_unmake */
  36. #include "mali_kbase_mem_linux.h"
  37. /* Part of the workaround for uTLB invalid pages is to ensure we grow/shrink tmem by 4 pages at a time */
  38. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316 (2) /* round to 4 pages */
  39. /* Part of the workaround for PRLAM-9630 requires us to grow/shrink memory by 8 pages.
  40. The MMU reads in 8 page table entries from memory at a time, if we have more than one page fault within the same 8 pages and
  41. page tables are updated accordingly, the MMU does not re-read the page table entries from memory for the subsequent page table
  42. updates and generates duplicate page faults as the page table information used by the MMU is not valid. */
  43. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630 (3) /* round to 8 pages */
  44. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2 (0) /* round to 1 page */
  45. /* This must always be a power of 2 */
  46. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2)
  47. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_8316 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316)
  48. #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_9630 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630)
  49. /**
  50. * A CPU mapping
  51. */
  52. struct kbase_cpu_mapping {
  53. struct list_head mappings_list;
  54. struct kbase_mem_phy_alloc *alloc;
  55. struct kbase_context *kctx;
  56. struct kbase_va_region *region;
  57. pgoff_t page_off;
  58. int count;
  59. unsigned long vm_start;
  60. unsigned long vm_end;
  61. };
  62. enum kbase_memory_type {
  63. KBASE_MEM_TYPE_NATIVE,
  64. KBASE_MEM_TYPE_IMPORTED_UMP,
  65. KBASE_MEM_TYPE_IMPORTED_UMM,
  66. KBASE_MEM_TYPE_IMPORTED_USER_BUF,
  67. KBASE_MEM_TYPE_ALIAS,
  68. KBASE_MEM_TYPE_TB,
  69. KBASE_MEM_TYPE_RAW
  70. };
  71. /* internal structure, mirroring base_mem_aliasing_info,
  72. * but with alloc instead of a gpu va (handle) */
  73. struct kbase_aliased {
  74. struct kbase_mem_phy_alloc *alloc; /* NULL for special, non-NULL for native */
  75. u64 offset; /* in pages */
  76. u64 length; /* in pages */
  77. };
  78. /**
  79. * @brief Physical pages tracking object properties
  80. */
  81. #define KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED (1ul << 0)
  82. #define KBASE_MEM_PHY_ALLOC_LARGE (1ul << 1)
  83. /* physical pages tracking object.
  84. * Set up to track N pages.
  85. * N not stored here, the creator holds that info.
  86. * This object only tracks how many elements are actually valid (present).
  87. * Changing of nents or *pages should only happen if the kbase_mem_phy_alloc is not
  88. * shared with another region or client. CPU mappings are OK to exist when changing, as
  89. * long as the tracked mappings objects are updated as part of the change.
  90. */
  91. struct kbase_mem_phy_alloc {
  92. struct kref kref; /* number of users of this alloc */
  93. atomic_t gpu_mappings;
  94. size_t nents; /* 0..N */
  95. phys_addr_t *pages; /* N elements, only 0..nents are valid */
  96. /* kbase_cpu_mappings */
  97. struct list_head mappings;
  98. /* Node used to store this allocation on the eviction list */
  99. struct list_head evict_node;
  100. /* Physical backing size when the pages where evicted */
  101. size_t evicted;
  102. /*
  103. * Back reference to the region structure which created this
  104. * allocation, or NULL if it has been freed.
  105. */
  106. struct kbase_va_region *reg;
  107. /* type of buffer */
  108. enum kbase_memory_type type;
  109. unsigned long properties;
  110. struct list_head zone_cache;
  111. /* member in union valid based on @a type */
  112. union {
  113. #ifdef CONFIG_UMP
  114. ump_dd_handle ump_handle;
  115. #endif
  116. #ifdef CONFIG_DMA_SHARED_BUFFER
  117. struct {
  118. struct dma_buf *dma_buf;
  119. struct dma_buf_attachment *dma_attachment;
  120. unsigned int current_mapping_usage_count;
  121. struct sg_table *sgt;
  122. } umm;
  123. #endif // ifdef CONFIG_DMA_SHARED_BUFFER
  124. struct {
  125. u64 stride;
  126. size_t nents;
  127. struct kbase_aliased *aliased;
  128. } alias;
  129. /* Used by type = (KBASE_MEM_TYPE_NATIVE, KBASE_MEM_TYPE_TB) */
  130. struct kbase_context *kctx;
  131. struct {
  132. unsigned long address;
  133. unsigned long size;
  134. unsigned long nr_pages;
  135. struct page **pages;
  136. unsigned int current_mapping_usage_count;
  137. struct mm_struct *mm;
  138. dma_addr_t *dma_addrs;
  139. } user_buf;
  140. } imported;
  141. };
  142. static inline void kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc)
  143. {
  144. KBASE_DEBUG_ASSERT(alloc);
  145. /* we only track mappings of NATIVE buffers */
  146. if (alloc->type == KBASE_MEM_TYPE_NATIVE)
  147. atomic_inc(&alloc->gpu_mappings);
  148. }
  149. static inline void kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc *alloc)
  150. {
  151. KBASE_DEBUG_ASSERT(alloc);
  152. /* we only track mappings of NATIVE buffers */
  153. if (alloc->type == KBASE_MEM_TYPE_NATIVE)
  154. if (atomic_dec_return(&alloc->gpu_mappings) < 0) {
  155. pr_err("Mismatched %s:\n", __func__);
  156. dump_stack();
  157. }
  158. }
  159. void kbase_mem_kref_free(struct kref *kref);
  160. int kbase_mem_init(struct kbase_device *kbdev);
  161. void kbase_mem_halt(struct kbase_device *kbdev);
  162. void kbase_mem_term(struct kbase_device *kbdev);
  163. static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc *alloc)
  164. {
  165. kref_get(&alloc->kref);
  166. return alloc;
  167. }
  168. static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc *alloc)
  169. {
  170. kref_put(&alloc->kref, kbase_mem_kref_free);
  171. return NULL;
  172. }
  173. /**
  174. * A GPU memory region, and attributes for CPU mappings.
  175. */
  176. struct kbase_va_region {
  177. struct rb_node rblink;
  178. struct list_head link;
  179. struct kbase_context *kctx; /* Backlink to base context */
  180. u64 start_pfn; /* The PFN in GPU space */
  181. size_t nr_pages;
  182. /* Free region */
  183. #define KBASE_REG_FREE (1ul << 0)
  184. /* CPU write access */
  185. #define KBASE_REG_CPU_WR (1ul << 1)
  186. /* GPU write access */
  187. #define KBASE_REG_GPU_WR (1ul << 2)
  188. /* No eXecute flag */
  189. #define KBASE_REG_GPU_NX (1ul << 3)
  190. /* Is CPU cached? */
  191. #define KBASE_REG_CPU_CACHED (1ul << 4)
  192. /* Is GPU cached? */
  193. #define KBASE_REG_GPU_CACHED (1ul << 5)
  194. #define KBASE_REG_GROWABLE (1ul << 6)
  195. /* Can grow on pf? */
  196. #define KBASE_REG_PF_GROW (1ul << 7)
  197. /* VA managed by us */
  198. #define KBASE_REG_CUSTOM_VA (1ul << 8)
  199. /* inner shareable coherency */
  200. #define KBASE_REG_SHARE_IN (1ul << 9)
  201. /* inner & outer shareable coherency */
  202. #define KBASE_REG_SHARE_BOTH (1ul << 10)
  203. /* Space for 4 different zones */
  204. #define KBASE_REG_ZONE_MASK (3ul << 11)
  205. #define KBASE_REG_ZONE(x) (((x) & 3) << 11)
  206. /* GPU read access */
  207. #define KBASE_REG_GPU_RD (1ul<<13)
  208. /* CPU read access */
  209. #define KBASE_REG_CPU_RD (1ul<<14)
  210. /* Aligned for GPU EX in SAME_VA */
  211. #define KBASE_REG_ALIGNED (1ul<<15)
  212. /* Index of chosen MEMATTR for this region (0..7) */
  213. #define KBASE_REG_MEMATTR_MASK (7ul << 16)
  214. #define KBASE_REG_MEMATTR_INDEX(x) (((x) & 7) << 16)
  215. #define KBASE_REG_MEMATTR_VALUE(x) (((x) & KBASE_REG_MEMATTR_MASK) >> 16)
  216. #define KBASE_REG_SECURE (1ul << 19)
  217. #define KBASE_REG_DONT_NEED (1ul << 20)
  218. #define KBASE_REG_ZONE_SAME_VA KBASE_REG_ZONE(0)
  219. /* only used with 32-bit clients */
  220. /*
  221. * On a 32bit platform, custom VA should be wired from (4GB + shader region)
  222. * to the VA limit of the GPU. Unfortunately, the Linux mmap() interface
  223. * limits us to 2^32 pages (2^44 bytes, see mmap64 man page for reference).
  224. * So we put the default limit to the maximum possible on Linux and shrink
  225. * it down, if required by the GPU, during initialization.
  226. */
  227. /*
  228. * Dedicated 16MB region for shader code:
  229. * VA range 0x101000000-0x102000000
  230. */
  231. #define KBASE_REG_ZONE_EXEC KBASE_REG_ZONE(1)
  232. #define KBASE_REG_ZONE_EXEC_BASE (0x101000000ULL >> PAGE_SHIFT)
  233. #define KBASE_REG_ZONE_EXEC_SIZE ((16ULL * 1024 * 1024) >> PAGE_SHIFT)
  234. #define KBASE_REG_ZONE_CUSTOM_VA KBASE_REG_ZONE(2)
  235. #define KBASE_REG_ZONE_CUSTOM_VA_BASE (KBASE_REG_ZONE_EXEC_BASE + KBASE_REG_ZONE_EXEC_SIZE) /* Starting after KBASE_REG_ZONE_EXEC */
  236. #define KBASE_REG_ZONE_CUSTOM_VA_SIZE (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE)
  237. /* end 32-bit clients only */
  238. unsigned long flags;
  239. size_t extent; /* nr of pages alloc'd on PF */
  240. struct kbase_mem_phy_alloc *cpu_alloc; /* the one alloc object we mmap to the CPU when mapping this region */
  241. struct kbase_mem_phy_alloc *gpu_alloc; /* the one alloc object we mmap to the GPU when mapping this region */
  242. /* non-NULL if this memory object is a kds_resource */
  243. struct kds_resource *kds_res;
  244. /* List head used to store the region in the JIT allocation pool */
  245. struct list_head jit_node;
  246. };
  247. /* Common functions */
  248. static inline phys_addr_t *kbase_get_cpu_phy_pages(struct kbase_va_region *reg)
  249. {
  250. KBASE_DEBUG_ASSERT(reg);
  251. KBASE_DEBUG_ASSERT(reg->cpu_alloc);
  252. KBASE_DEBUG_ASSERT(reg->gpu_alloc);
  253. KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
  254. return reg->cpu_alloc->pages;
  255. }
  256. static inline phys_addr_t *kbase_get_gpu_phy_pages(struct kbase_va_region *reg)
  257. {
  258. KBASE_DEBUG_ASSERT(reg);
  259. KBASE_DEBUG_ASSERT(reg->cpu_alloc);
  260. KBASE_DEBUG_ASSERT(reg->gpu_alloc);
  261. KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
  262. return reg->gpu_alloc->pages;
  263. }
  264. static inline size_t kbase_reg_current_backed_size(struct kbase_va_region *reg)
  265. {
  266. KBASE_DEBUG_ASSERT(reg);
  267. /* if no alloc object the backed size naturally is 0 */
  268. if (!reg->cpu_alloc)
  269. return 0;
  270. KBASE_DEBUG_ASSERT(reg->cpu_alloc);
  271. KBASE_DEBUG_ASSERT(reg->gpu_alloc);
  272. KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
  273. return reg->cpu_alloc->nents;
  274. }
  275. #define KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD ((size_t)(4*1024)) /* size above which vmalloc is used over kmalloc */
  276. static inline struct kbase_mem_phy_alloc *kbase_alloc_create(size_t nr_pages, enum kbase_memory_type type)
  277. {
  278. struct kbase_mem_phy_alloc *alloc;
  279. size_t alloc_size = sizeof(*alloc) + sizeof(*alloc->pages) * nr_pages;
  280. size_t per_page_size = sizeof(*alloc->pages);
  281. /* Imported pages may have page private data already in use */
  282. if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
  283. alloc_size += nr_pages *
  284. sizeof(*alloc->imported.user_buf.dma_addrs);
  285. per_page_size += sizeof(*alloc->imported.user_buf.dma_addrs);
  286. }
  287. /*
  288. * Prevent nr_pages*per_page_size + sizeof(*alloc) from
  289. * wrapping around.
  290. */
  291. if (nr_pages > ((((size_t) -1) - sizeof(*alloc))
  292. / per_page_size))
  293. return ERR_PTR(-ENOMEM);
  294. /* Allocate based on the size to reduce internal fragmentation of vmem */
  295. if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
  296. alloc = vzalloc(alloc_size);
  297. else
  298. alloc = kzalloc(alloc_size, GFP_KERNEL);
  299. if (!alloc)
  300. return ERR_PTR(-ENOMEM);
  301. /* Store allocation method */
  302. if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
  303. alloc->properties |= KBASE_MEM_PHY_ALLOC_LARGE;
  304. kref_init(&alloc->kref);
  305. atomic_set(&alloc->gpu_mappings, 0);
  306. alloc->nents = 0;
  307. alloc->pages = (void *)(alloc + 1);
  308. INIT_LIST_HEAD(&alloc->mappings);
  309. alloc->type = type;
  310. INIT_LIST_HEAD(&alloc->zone_cache);
  311. if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF)
  312. alloc->imported.user_buf.dma_addrs =
  313. (void *) (alloc->pages + nr_pages);
  314. return alloc;
  315. }
  316. static inline int kbase_reg_prepare_native(struct kbase_va_region *reg,
  317. struct kbase_context *kctx)
  318. {
  319. KBASE_DEBUG_ASSERT(reg);
  320. KBASE_DEBUG_ASSERT(!reg->cpu_alloc);
  321. KBASE_DEBUG_ASSERT(!reg->gpu_alloc);
  322. KBASE_DEBUG_ASSERT(reg->flags & KBASE_REG_FREE);
  323. reg->cpu_alloc = kbase_alloc_create(reg->nr_pages,
  324. KBASE_MEM_TYPE_NATIVE);
  325. if (IS_ERR(reg->cpu_alloc))
  326. return PTR_ERR(reg->cpu_alloc);
  327. else if (!reg->cpu_alloc)
  328. return -ENOMEM;
  329. reg->cpu_alloc->imported.kctx = kctx;
  330. INIT_LIST_HEAD(&reg->cpu_alloc->evict_node);
  331. if (kctx->infinite_cache_active && (reg->flags & KBASE_REG_CPU_CACHED)) {
  332. reg->gpu_alloc = kbase_alloc_create(reg->nr_pages,
  333. KBASE_MEM_TYPE_NATIVE);
  334. reg->gpu_alloc->imported.kctx = kctx;
  335. INIT_LIST_HEAD(&reg->gpu_alloc->evict_node);
  336. } else {
  337. reg->gpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
  338. }
  339. INIT_LIST_HEAD(&reg->jit_node);
  340. reg->flags &= ~KBASE_REG_FREE;
  341. return 0;
  342. }
  343. static inline int kbase_atomic_add_pages(int num_pages, atomic_t *used_pages)
  344. {
  345. int new_val = atomic_add_return(num_pages, used_pages);
  346. return new_val;
  347. }
  348. static inline int kbase_atomic_sub_pages(int num_pages, atomic_t *used_pages)
  349. {
  350. int new_val = atomic_sub_return(num_pages, used_pages);
  351. return new_val;
  352. }
  353. /*
  354. * Max size for kbdev memory pool (in pages)
  355. */
  356. #define KBASE_MEM_POOL_MAX_SIZE_KBDEV (SZ_64M >> PAGE_SHIFT)
  357. /*
  358. * Max size for kctx memory pool (in pages)
  359. */
  360. #define KBASE_MEM_POOL_MAX_SIZE_KCTX (SZ_64M >> PAGE_SHIFT)
  361. /**
  362. * kbase_mem_pool_init - Create a memory pool for a kbase device
  363. * @pool: Memory pool to initialize
  364. * @max_size: Maximum number of free pages the pool can hold
  365. * @kbdev: Kbase device where memory is used
  366. * @next_pool: Pointer to the next pool or NULL.
  367. *
  368. * Allocations from @pool are in whole pages. Each @pool has a free list where
  369. * pages can be quickly allocated from. The free list is initially empty and
  370. * filled whenever pages are freed back to the pool. The number of free pages
  371. * in the pool will in general not exceed @max_size, but the pool may in
  372. * certain corner cases grow above @max_size.
  373. *
  374. * If @next_pool is not NULL, we will allocate from @next_pool before going to
  375. * the kernel allocator. Similarily pages can spill over to @next_pool when
  376. * @pool is full. Pages are zeroed before they spill over to another pool, to
  377. * prevent leaking information between applications.
  378. *
  379. * A shrinker is registered so that Linux mm can reclaim pages from the pool as
  380. * needed.
  381. *
  382. * Return: 0 on success, negative -errno on error
  383. */
  384. int kbase_mem_pool_init(struct kbase_mem_pool *pool,
  385. size_t max_size,
  386. struct kbase_device *kbdev,
  387. struct kbase_mem_pool *next_pool);
  388. /**
  389. * kbase_mem_pool_term - Destroy a memory pool
  390. * @pool: Memory pool to destroy
  391. *
  392. * Pages in the pool will spill over to @next_pool (if available) or freed to
  393. * the kernel.
  394. */
  395. void kbase_mem_pool_term(struct kbase_mem_pool *pool);
  396. /**
  397. * kbase_mem_pool_alloc - Allocate a page from memory pool
  398. * @pool: Memory pool to allocate from
  399. *
  400. * Allocations from the pool are made as follows:
  401. * 1. If there are free pages in the pool, allocate a page from @pool.
  402. * 2. Otherwise, if @next_pool is not NULL and has free pages, allocate a page
  403. * from @next_pool.
  404. * 3. Finally, allocate a page from the kernel.
  405. *
  406. * Return: Pointer to allocated page, or NULL if allocation failed.
  407. */
  408. struct page *kbase_mem_pool_alloc(struct kbase_mem_pool *pool);
  409. /**
  410. * kbase_mem_pool_free - Free a page to memory pool
  411. * @pool: Memory pool where page should be freed
  412. * @page: Page to free to the pool
  413. * @dirty: Whether some of the page may be dirty in the cache.
  414. *
  415. * Pages are freed to the pool as follows:
  416. * 1. If @pool is not full, add @page to @pool.
  417. * 2. Otherwise, if @next_pool is not NULL and not full, add @page to
  418. * @next_pool.
  419. * 3. Finally, free @page to the kernel.
  420. */
  421. void kbase_mem_pool_free(struct kbase_mem_pool *pool, struct page *page,
  422. bool dirty);
  423. /**
  424. * kbase_mem_pool_alloc_pages - Allocate pages from memory pool
  425. * @pool: Memory pool to allocate from
  426. * @nr_pages: Number of pages to allocate
  427. * @pages: Pointer to array where the physical address of the allocated
  428. * pages will be stored.
  429. *
  430. * Like kbase_mem_pool_alloc() but optimized for allocating many pages.
  431. *
  432. * Return: 0 on success, negative -errno on error
  433. */
  434. int kbase_mem_pool_alloc_pages(struct kbase_mem_pool *pool, size_t nr_pages,
  435. phys_addr_t *pages);
  436. /**
  437. * kbase_mem_pool_free_pages - Free pages to memory pool
  438. * @pool: Memory pool where pages should be freed
  439. * @nr_pages: Number of pages to free
  440. * @pages: Pointer to array holding the physical addresses of the pages to
  441. * free.
  442. * @dirty: Whether any pages may be dirty in the cache.
  443. * @reclaimed: Whether the pages where reclaimable and thus should bypass
  444. * the pool and go straight to the kernel.
  445. *
  446. * Like kbase_mem_pool_free() but optimized for freeing many pages.
  447. */
  448. void kbase_mem_pool_free_pages(struct kbase_mem_pool *pool, size_t nr_pages,
  449. phys_addr_t *pages, bool dirty, bool reclaimed);
  450. /**
  451. * kbase_mem_pool_size - Get number of free pages in memory pool
  452. * @pool: Memory pool to inspect
  453. *
  454. * Note: the size of the pool may in certain corner cases exceed @max_size!
  455. *
  456. * Return: Number of free pages in the pool
  457. */
  458. static inline size_t kbase_mem_pool_size(struct kbase_mem_pool *pool)
  459. {
  460. return READ_ONCE(pool->cur_size);
  461. }
  462. /**
  463. * kbase_mem_pool_max_size - Get maximum number of free pages in memory pool
  464. * @pool: Memory pool to inspect
  465. *
  466. * Return: Maximum number of free pages in the pool
  467. */
  468. static inline size_t kbase_mem_pool_max_size(struct kbase_mem_pool *pool)
  469. {
  470. return pool->max_size;
  471. }
  472. /**
  473. * kbase_mem_pool_set_max_size - Set maximum number of free pages in memory pool
  474. * @pool: Memory pool to inspect
  475. * @max_size: Maximum number of free pages the pool can hold
  476. *
  477. * If @max_size is reduced, the pool will be shrunk to adhere to the new limit.
  478. * For details see kbase_mem_pool_shrink().
  479. */
  480. void kbase_mem_pool_set_max_size(struct kbase_mem_pool *pool, size_t max_size);
  481. /**
  482. * kbase_mem_pool_trim - Grow or shrink the pool to a new size
  483. * @pool: Memory pool to trim
  484. * @new_size: New number of pages in the pool
  485. *
  486. * If @new_size > @cur_size, fill the pool with new pages from the kernel, but
  487. * not above @max_size.
  488. * If @new_size < @cur_size, shrink the pool by freeing pages to the kernel.
  489. *
  490. * Return: The new size of the pool
  491. */
  492. size_t kbase_mem_pool_trim(struct kbase_mem_pool *pool, size_t new_size);
  493. int kbase_region_tracker_init(struct kbase_context *kctx);
  494. int kbase_region_tracker_init_jit(struct kbase_context *kctx, u64 jit_va_pages);
  495. void kbase_region_tracker_term(struct kbase_context *kctx);
  496. struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(struct kbase_context *kctx, u64 gpu_addr);
  497. /**
  498. * @brief Check that a pointer is actually a valid region.
  499. *
  500. * Must be called with context lock held.
  501. */
  502. struct kbase_va_region *kbase_region_tracker_find_region_base_address(struct kbase_context *kctx, u64 gpu_addr);
  503. struct kbase_va_region *kbase_alloc_free_region(struct kbase_context *kctx, u64 start_pfn, size_t nr_pages, int zone);
  504. void kbase_free_alloced_region(struct kbase_va_region *reg);
  505. int kbase_add_va_region(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align);
  506. bool kbase_check_alloc_flags(unsigned long flags);
  507. bool kbase_check_import_flags(unsigned long flags);
  508. void kbase_update_region_flags(struct kbase_context *kctx,
  509. struct kbase_va_region *reg, unsigned long flags);
  510. void kbase_gpu_vm_lock(struct kbase_context *kctx);
  511. void kbase_gpu_vm_unlock(struct kbase_context *kctx);
  512. int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size);
  513. int kbase_mmu_init(struct kbase_context *kctx);
  514. void kbase_mmu_term(struct kbase_context *kctx);
  515. phys_addr_t kbase_mmu_alloc_pgd(struct kbase_context *kctx);
  516. void kbase_mmu_free_pgd(struct kbase_context *kctx);
  517. int kbase_mmu_insert_pages(struct kbase_context *kctx, u64 vpfn,
  518. phys_addr_t *phys, size_t nr,
  519. unsigned long flags);
  520. int kbase_mmu_insert_single_page(struct kbase_context *kctx, u64 vpfn,
  521. phys_addr_t phys, size_t nr,
  522. unsigned long flags);
  523. int kbase_mmu_teardown_pages(struct kbase_context *kctx, u64 vpfn, size_t nr);
  524. int kbase_mmu_update_pages(struct kbase_context *kctx, u64 vpfn, phys_addr_t *phys, size_t nr, unsigned long flags);
  525. /**
  526. * @brief Register region and map it on the GPU.
  527. *
  528. * Call kbase_add_va_region() and map the region on the GPU.
  529. */
  530. int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align);
  531. /**
  532. * @brief Remove the region from the GPU and unregister it.
  533. *
  534. * Must be called with context lock held.
  535. */
  536. int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg);
  537. /**
  538. * The caller has the following locking conditions:
  539. * - It must hold kbase_as::transaction_mutex on kctx's address space
  540. * - It must hold the kbasep_js_device_data::runpool_irq::lock
  541. */
  542. void kbase_mmu_update(struct kbase_context *kctx);
  543. /**
  544. * The caller has the following locking conditions:
  545. * - It must hold kbase_as::transaction_mutex on kctx's address space
  546. * - It must hold the kbasep_js_device_data::runpool_irq::lock
  547. */
  548. void kbase_mmu_disable(struct kbase_context *kctx);
  549. /**
  550. * kbase_mmu_disable_as() - set the MMU in unmapped mode for an address space.
  551. *
  552. * @kbdev: Kbase device
  553. * @as_nr: Number of the address space for which the MMU
  554. * should be set in unmapped mode.
  555. *
  556. * The caller must hold kbdev->as[as_nr].transaction_mutex.
  557. */
  558. void kbase_mmu_disable_as(struct kbase_device *kbdev, int as_nr);
  559. void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat);
  560. /** Dump the MMU tables to a buffer
  561. *
  562. * This function allocates a buffer (of @c nr_pages pages) to hold a dump of the MMU tables and fills it. If the
  563. * buffer is too small then the return value will be NULL.
  564. *
  565. * The GPU vm lock must be held when calling this function.
  566. *
  567. * The buffer returned should be freed with @ref vfree when it is no longer required.
  568. *
  569. * @param[in] kctx The kbase context to dump
  570. * @param[in] nr_pages The number of pages to allocate for the buffer.
  571. *
  572. * @return The address of the buffer containing the MMU dump or NULL on error (including if the @c nr_pages is too
  573. * small)
  574. */
  575. void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages);
  576. int kbase_sync_now(struct kbase_context *kctx, struct base_syncset *syncset);
  577. void kbase_sync_single(struct kbase_context *kctx, phys_addr_t cpu_pa,
  578. phys_addr_t gpu_pa, off_t offset, size_t size,
  579. enum kbase_sync_type sync_fn);
  580. void kbase_pre_job_sync(struct kbase_context *kctx, struct base_syncset *syncsets, size_t nr);
  581. void kbase_post_job_sync(struct kbase_context *kctx, struct base_syncset *syncsets, size_t nr);
  582. /* OS specific functions */
  583. int kbase_mem_free(struct kbase_context *kctx, u64 gpu_addr);
  584. int kbase_mem_free_region(struct kbase_context *kctx, struct kbase_va_region *reg);
  585. void kbase_os_mem_map_lock(struct kbase_context *kctx);
  586. void kbase_os_mem_map_unlock(struct kbase_context *kctx);
  587. /**
  588. * @brief Update the memory allocation counters for the current process
  589. *
  590. * OS specific call to updates the current memory allocation counters for the current process with
  591. * the supplied delta.
  592. *
  593. * @param[in] kctx The kbase context
  594. * @param[in] pages The desired delta to apply to the memory usage counters.
  595. */
  596. void kbasep_os_process_page_usage_update(struct kbase_context *kctx, int pages);
  597. /**
  598. * @brief Add to the memory allocation counters for the current process
  599. *
  600. * OS specific call to add to the current memory allocation counters for the current process by
  601. * the supplied amount.
  602. *
  603. * @param[in] kctx The kernel base context used for the allocation.
  604. * @param[in] pages The desired delta to apply to the memory usage counters.
  605. */
  606. static inline void kbase_process_page_usage_inc(struct kbase_context *kctx, int pages)
  607. {
  608. kbasep_os_process_page_usage_update(kctx, pages);
  609. }
  610. /**
  611. * @brief Subtract from the memory allocation counters for the current process
  612. *
  613. * OS specific call to subtract from the current memory allocation counters for the current process by
  614. * the supplied amount.
  615. *
  616. * @param[in] kctx The kernel base context used for the allocation.
  617. * @param[in] pages The desired delta to apply to the memory usage counters.
  618. */
  619. static inline void kbase_process_page_usage_dec(struct kbase_context *kctx, int pages)
  620. {
  621. kbasep_os_process_page_usage_update(kctx, 0 - pages);
  622. }
  623. /**
  624. * @brief Find the offset of the CPU mapping of a memory allocation containing
  625. * a given address range
  626. *
  627. * Searches for a CPU mapping of any part of the region starting at @p gpu_addr
  628. * that fully encloses the CPU virtual address range specified by @p uaddr and
  629. * @p size. Returns a failure indication if only part of the address range lies
  630. * within a CPU mapping, or the address range lies within a CPU mapping of a
  631. * different region.
  632. *
  633. * @param[in,out] kctx The kernel base context used for the allocation.
  634. * @param[in] gpu_addr GPU address of the start of the allocated region
  635. * within which to search.
  636. * @param[in] uaddr Start of the CPU virtual address range.
  637. * @param[in] size Size of the CPU virtual address range (in bytes).
  638. * @param[out] offset The offset from the start of the allocation to the
  639. * specified CPU virtual address.
  640. *
  641. * @return 0 if offset was obtained successfully. Error code
  642. * otherwise.
  643. */
  644. int kbasep_find_enclosing_cpu_mapping_offset(struct kbase_context *kctx,
  645. u64 gpu_addr,
  646. unsigned long uaddr,
  647. size_t size,
  648. u64 *offset);
  649. enum hrtimer_restart kbasep_as_poke_timer_callback(struct hrtimer *timer);
  650. void kbase_as_poking_timer_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom);
  651. void kbase_as_poking_timer_release_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom);
  652. /**
  653. * @brief Allocates physical pages.
  654. *
  655. * Allocates \a nr_pages_requested and updates the alloc object.
  656. *
  657. * @param[in] alloc allocation object to add pages to
  658. * @param[in] nr_pages_requested number of physical pages to allocate
  659. *
  660. * @return 0 if all pages have been successfully allocated. Error code otherwise
  661. */
  662. int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, size_t nr_pages_requested);
  663. /**
  664. * @brief Free physical pages.
  665. *
  666. * Frees \a nr_pages and updates the alloc object.
  667. *
  668. * @param[in] alloc allocation object to free pages from
  669. * @param[in] nr_pages_to_free number of physical pages to free
  670. */
  671. int kbase_free_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, size_t nr_pages_to_free);
  672. static inline void kbase_set_dma_addr(struct page *p, dma_addr_t dma_addr)
  673. {
  674. SetPagePrivate(p);
  675. if (sizeof(dma_addr_t) > sizeof(p->private)) {
  676. /* on 32-bit ARM with LPAE dma_addr_t becomes larger, but the
  677. * private field stays the same. So we have to be clever and
  678. * use the fact that we only store DMA addresses of whole pages,
  679. * so the low bits should be zero */
  680. KBASE_DEBUG_ASSERT(!(dma_addr & (PAGE_SIZE - 1)));
  681. set_page_private(p, dma_addr >> PAGE_SHIFT);
  682. } else {
  683. set_page_private(p, dma_addr);
  684. }
  685. }
  686. static inline dma_addr_t kbase_dma_addr(struct page *p)
  687. {
  688. if (sizeof(dma_addr_t) > sizeof(p->private))
  689. return ((dma_addr_t)page_private(p)) << PAGE_SHIFT;
  690. return (dma_addr_t)page_private(p);
  691. }
  692. static inline void kbase_clear_dma_addr(struct page *p)
  693. {
  694. ClearPagePrivate(p);
  695. }
  696. /**
  697. * @brief Process a bus or page fault.
  698. *
  699. * This function will process a fault on a specific address space
  700. *
  701. * @param[in] kbdev The @ref kbase_device the fault happened on
  702. * @param[in] kctx The @ref kbase_context for the faulting address space if
  703. * one was found.
  704. * @param[in] as The address space that has the fault
  705. */
  706. void kbase_mmu_interrupt_process(struct kbase_device *kbdev,
  707. struct kbase_context *kctx, struct kbase_as *as);
  708. /**
  709. * @brief Process a page fault.
  710. *
  711. * @param[in] data work_struct passed by queue_work()
  712. */
  713. void page_fault_worker(struct work_struct *data);
  714. /**
  715. * @brief Process a bus fault.
  716. *
  717. * @param[in] data work_struct passed by queue_work()
  718. */
  719. void bus_fault_worker(struct work_struct *data);
  720. /**
  721. * @brief Flush MMU workqueues.
  722. *
  723. * This function will cause any outstanding page or bus faults to be processed.
  724. * It should be called prior to powering off the GPU.
  725. *
  726. * @param[in] kbdev Device pointer
  727. */
  728. void kbase_flush_mmu_wqs(struct kbase_device *kbdev);
  729. /**
  730. * kbase_sync_single_for_device - update physical memory and give GPU ownership
  731. * @kbdev: Device pointer
  732. * @handle: DMA address of region
  733. * @size: Size of region to sync
  734. * @dir: DMA data direction
  735. */
  736. void kbase_sync_single_for_device(struct kbase_device *kbdev, dma_addr_t handle,
  737. size_t size, enum dma_data_direction dir);
  738. /**
  739. * kbase_sync_single_for_cpu - update physical memory and give CPU ownership
  740. * @kbdev: Device pointer
  741. * @handle: DMA address of region
  742. * @size: Size of region to sync
  743. * @dir: DMA data direction
  744. */
  745. void kbase_sync_single_for_cpu(struct kbase_device *kbdev, dma_addr_t handle,
  746. size_t size, enum dma_data_direction dir);
  747. /**
  748. * kbase_jit_debugfs_add - Add per context debugfs entry for JIT.
  749. * @kctx: kbase context
  750. */
  751. void kbase_jit_debugfs_add(struct kbase_context *kctx);
  752. /**
  753. * kbase_jit_init - Initialize the JIT memory pool management
  754. * @kctx: kbase context
  755. *
  756. * Returns zero on success or negative error number on failure.
  757. */
  758. int kbase_jit_init(struct kbase_context *kctx);
  759. /**
  760. * kbase_jit_allocate - Allocate JIT memory
  761. * @kctx: kbase context
  762. * @info: JIT allocation information
  763. *
  764. * Return: JIT allocation on success or NULL on failure.
  765. */
  766. struct kbase_va_region *kbase_jit_allocate(struct kbase_context *kctx,
  767. struct base_jit_alloc_info *info);
  768. /**
  769. * kbase_jit_free - Free a JIT allocation
  770. * @kctx: kbase context
  771. * @reg: JIT allocation
  772. *
  773. * Frees a JIT allocation and places it into the free pool for later reuse.
  774. */
  775. void kbase_jit_free(struct kbase_context *kctx, struct kbase_va_region *reg);
  776. /**
  777. * kbase_jit_backing_lost - Inform JIT that an allocation has lost backing
  778. * @reg: JIT allocation
  779. */
  780. void kbase_jit_backing_lost(struct kbase_va_region *reg);
  781. /**
  782. * kbase_jit_evict - Evict a JIT allocation from the pool
  783. * @kctx: kbase context
  784. *
  785. * Evict the least recently used JIT allocation from the pool. This can be
  786. * required if normal VA allocations are failing due to VA exhaustion.
  787. *
  788. * Return: True if a JIT allocation was freed, false otherwise.
  789. */
  790. bool kbase_jit_evict(struct kbase_context *kctx);
  791. /**
  792. * kbase_jit_term - Terminate the JIT memory pool management
  793. * @kctx: kbase context
  794. */
  795. void kbase_jit_term(struct kbase_context *kctx);
  796. /**
  797. * kbase_map_external_resource - Map an external resource to the GPU.
  798. * @kctx: kbase context.
  799. * @reg: The region to map.
  800. * @locked_mm: The mm_struct which has been locked for this operation.
  801. * @kds_res_count: The number of KDS resources.
  802. * @kds_resources: Array of KDS resources.
  803. * @kds_access_bitmap: Access bitmap for KDS.
  804. * @exclusive: If the KDS resource requires exclusive access.
  805. *
  806. * Return: The physical allocation which backs the region on success or NULL
  807. * on failure.
  808. */
  809. struct kbase_mem_phy_alloc *kbase_map_external_resource(
  810. struct kbase_context *kctx, struct kbase_va_region *reg,
  811. struct mm_struct *locked_mm
  812. #ifdef CONFIG_KDS
  813. , u32 *kds_res_count, struct kds_resource **kds_resources,
  814. unsigned long *kds_access_bitmap, bool exclusive
  815. #endif
  816. );
  817. /**
  818. * kbase_unmap_external_resource - Unmap an external resource from the GPU.
  819. * @kctx: kbase context.
  820. * @reg: The region to unmap or NULL if it has already been released.
  821. * @alloc: The physical allocation being unmapped.
  822. */
  823. void kbase_unmap_external_resource(struct kbase_context *kctx,
  824. struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc);
  825. /**
  826. * kbase_sticky_resource_init - Initialize sticky resource management.
  827. * @kctx: kbase context
  828. *
  829. * Returns zero on success or negative error number on failure.
  830. */
  831. int kbase_sticky_resource_init(struct kbase_context *kctx);
  832. /**
  833. * kbase_sticky_resource_acquire - Acquire a reference on a sticky resource.
  834. * @kctx: kbase context.
  835. * @gpu_addr: The GPU address of the external resource.
  836. *
  837. * Return: The metadata object which represents the binding between the
  838. * external resource and the kbase context on success or NULL on failure.
  839. */
  840. struct kbase_ctx_ext_res_meta *kbase_sticky_resource_acquire(
  841. struct kbase_context *kctx, u64 gpu_addr);
  842. /**
  843. * kbase_sticky_resource_release - Release a reference on a sticky resource.
  844. * @kctx: kbase context.
  845. * @meta: Binding metadata.
  846. * @gpu_addr: GPU address of the external resource.
  847. *
  848. * If meta is NULL then gpu_addr will be used to scan the metadata list and
  849. * find the matching metadata (if any), otherwise the provided meta will be
  850. * used and gpu_addr will be ignored.
  851. *
  852. * Return: True if the release found the metadata and the reference was dropped.
  853. */
  854. bool kbase_sticky_resource_release(struct kbase_context *kctx,
  855. struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr);
  856. /**
  857. * kbase_sticky_resource_term - Terminate sticky resource management.
  858. * @kctx: kbase context
  859. */
  860. void kbase_sticky_resource_term(struct kbase_context *kctx);
  861. /**
  862. * kbase_zone_cache_update - Update the memory zone cache after new pages have
  863. * been added.
  864. * @alloc: The physical memory allocation to build the cache for.
  865. * @start_offset: Offset to where the new pages start.
  866. *
  867. * Updates an existing memory zone cache, updating the counters for the
  868. * various zones.
  869. * If the memory allocation doesn't already have a zone cache assume that
  870. * one isn't created and thus don't do anything.
  871. *
  872. * Return: Zero cache was updated, negative error code on error.
  873. */
  874. int kbase_zone_cache_update(struct kbase_mem_phy_alloc *alloc,
  875. size_t start_offset);
  876. /**
  877. * kbase_zone_cache_build - Build the memory zone cache.
  878. * @alloc: The physical memory allocation to build the cache for.
  879. *
  880. * Create a new zone cache for the provided physical memory allocation if
  881. * one doesn't already exist, if one does exist then just return.
  882. *
  883. * Return: Zero if the zone cache was created, negative error code on error.
  884. */
  885. int kbase_zone_cache_build(struct kbase_mem_phy_alloc *alloc);
  886. /**
  887. * kbase_zone_cache_clear - Clear the memory zone cache.
  888. * @alloc: The physical memory allocation to clear the cache on.
  889. */
  890. void kbase_zone_cache_clear(struct kbase_mem_phy_alloc *alloc);
  891. #endif // ifndef _KBASE_MEM_H_