mali_kbase_mem_linux.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  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_linux.c
  17. * Base kernel memory APIs, Linux implementation.
  18. */
  19. #include <linux/compat.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/mm.h>
  23. #include <linux/mman.h>
  24. #include <linux/fs.h>
  25. #include <linux/version.h>
  26. #include <linux/dma-mapping.h>
  27. #ifdef CONFIG_DMA_SHARED_BUFFER
  28. #include <linux/dma-buf.h>
  29. #endif /* defined(CONFIG_DMA_SHARED_BUFFER) */
  30. #include <linux/shrinker.h>
  31. #include <mali_kbase.h>
  32. #include <mali_kbase_mem_linux.h>
  33. #include <mali_kbase_config_defaults.h>
  34. #include <mali_kbase_hwaccess_time.h>
  35. #include <mali_kbase_tlstream.h>
  36. static int kbase_tracking_page_setup(struct kbase_context *kctx, struct vm_area_struct *vma);
  37. static const struct vm_operations_struct kbase_vm_ops;
  38. /**
  39. * kbase_mem_shrink_cpu_mapping - Shrink the CPU mapping(s) of an allocation
  40. * @kctx: Context the region belongs to
  41. * @reg: The GPU region
  42. * @new_pages: The number of pages after the shrink
  43. * @old_pages: The number of pages before the shrink
  44. *
  45. * Return: 0 on success, -errno on error.
  46. *
  47. * Shrink (or completely remove) all CPU mappings which reference the shrunk
  48. * part of the allocation.
  49. *
  50. * Note: Caller must be holding the processes mmap_sem lock.
  51. */
  52. static int kbase_mem_shrink_cpu_mapping(struct kbase_context *kctx,
  53. struct kbase_va_region *reg,
  54. u64 new_pages, u64 old_pages);
  55. /**
  56. * kbase_mem_shrink_gpu_mapping - Shrink the GPU mapping of an allocation
  57. * @kctx: Context the region belongs to
  58. * @reg: The GPU region or NULL if there isn't one
  59. * @new_pages: The number of pages after the shrink
  60. * @old_pages: The number of pages before the shrink
  61. *
  62. * Return: 0 on success, negative -errno on error
  63. *
  64. * Unmap the shrunk pages from the GPU mapping. Note that the size of the region
  65. * itself is unmodified as we still need to reserve the VA, only the page tables
  66. * will be modified by this function.
  67. */
  68. static int kbase_mem_shrink_gpu_mapping(struct kbase_context *kctx,
  69. struct kbase_va_region *reg,
  70. u64 new_pages, u64 old_pages);
  71. struct kbase_va_region *kbase_mem_alloc(struct kbase_context *kctx, u64 va_pages, u64 commit_pages, u64 extent, u64 *flags, u64 *gpu_va, u16 *va_alignment)
  72. {
  73. int zone;
  74. int gpu_pc_bits;
  75. int cpu_va_bits;
  76. struct kbase_va_region *reg;
  77. struct device *dev;
  78. KBASE_DEBUG_ASSERT(kctx);
  79. KBASE_DEBUG_ASSERT(flags);
  80. KBASE_DEBUG_ASSERT(gpu_va);
  81. KBASE_DEBUG_ASSERT(va_alignment);
  82. dev = kctx->kbdev->dev;
  83. *va_alignment = 0; /* no alignment by default */
  84. *gpu_va = 0; /* return 0 on failure */
  85. gpu_pc_bits = kctx->kbdev->gpu_props.props.core_props.log2_program_counter_size;
  86. cpu_va_bits = BITS_PER_LONG;
  87. if (va_pages == 0) {
  88. dev_warn(dev, "kbase_mem_alloc called with 0 va_pages!");
  89. goto bad_size;
  90. }
  91. if (va_pages > (U64_MAX / PAGE_SIZE))
  92. /* 64-bit address range is the max */
  93. goto bad_size;
  94. #if defined(CONFIG_64BIT)
  95. if (kctx->is_compat)
  96. cpu_va_bits = 32;
  97. #endif
  98. if (!kbase_check_alloc_flags(*flags)) {
  99. dev_warn(dev,
  100. "kbase_mem_alloc called with bad flags (%llx)",
  101. (unsigned long long)*flags);
  102. goto bad_flags;
  103. }
  104. if ((*flags & BASE_MEM_COHERENT_SYSTEM_REQUIRED) != 0 &&
  105. !kbase_device_is_cpu_coherent(kctx->kbdev)) {
  106. dev_warn(dev, "kbase_mem_alloc call required coherent mem when unavailable");
  107. goto bad_flags;
  108. }
  109. if ((*flags & BASE_MEM_COHERENT_SYSTEM) != 0 &&
  110. !kbase_device_is_cpu_coherent(kctx->kbdev)) {
  111. /* Remove COHERENT_SYSTEM flag if coherent mem is unavailable */
  112. *flags &= ~BASE_MEM_COHERENT_SYSTEM;
  113. }
  114. /* Limit GPU executable allocs to GPU PC size */
  115. if ((*flags & BASE_MEM_PROT_GPU_EX) &&
  116. (va_pages > (1ULL << gpu_pc_bits >> PAGE_SHIFT)))
  117. goto bad_ex_size;
  118. /* find out which VA zone to use */
  119. if (*flags & BASE_MEM_SAME_VA)
  120. zone = KBASE_REG_ZONE_SAME_VA;
  121. else if (*flags & BASE_MEM_PROT_GPU_EX)
  122. zone = KBASE_REG_ZONE_EXEC;
  123. else
  124. zone = KBASE_REG_ZONE_CUSTOM_VA;
  125. reg = kbase_alloc_free_region(kctx, 0, va_pages, zone);
  126. if (!reg) {
  127. dev_err(dev, "Failed to allocate free region");
  128. goto no_region;
  129. }
  130. kbase_update_region_flags(kctx, reg, *flags);
  131. if (kbase_reg_prepare_native(reg, kctx) != 0) {
  132. dev_err(dev, "Failed to prepare region");
  133. goto prepare_failed;
  134. }
  135. if (*flags & BASE_MEM_GROW_ON_GPF)
  136. reg->extent = extent;
  137. else
  138. reg->extent = 0;
  139. if (kbase_alloc_phy_pages(reg, va_pages, commit_pages) != 0) {
  140. dev_warn(dev, "Failed to allocate %lld pages (va_pages=%lld)",
  141. (unsigned long long)commit_pages,
  142. (unsigned long long)va_pages);
  143. goto no_mem;
  144. }
  145. kbase_gpu_vm_lock(kctx);
  146. /* mmap needed to setup VA? */
  147. if (*flags & BASE_MEM_SAME_VA) {
  148. unsigned long prot = PROT_NONE;
  149. unsigned long va_size = va_pages << PAGE_SHIFT;
  150. unsigned long va_map = va_size;
  151. unsigned long cookie, cookie_nr;
  152. unsigned long cpu_addr;
  153. /* Bind to a cookie */
  154. if (!kctx->cookies) {
  155. dev_err(dev, "No cookies available for allocation!");
  156. kbase_gpu_vm_unlock(kctx);
  157. goto no_cookie;
  158. }
  159. /* return a cookie */
  160. cookie_nr = __ffs(kctx->cookies);
  161. kctx->cookies &= ~(1UL << cookie_nr);
  162. BUG_ON(kctx->pending_regions[cookie_nr]);
  163. kctx->pending_regions[cookie_nr] = reg;
  164. kbase_gpu_vm_unlock(kctx);
  165. /* relocate to correct base */
  166. cookie = cookie_nr + PFN_DOWN(BASE_MEM_COOKIE_BASE);
  167. cookie <<= PAGE_SHIFT;
  168. /* See if we must align memory due to GPU PC bits vs CPU VA */
  169. if ((*flags & BASE_MEM_PROT_GPU_EX) &&
  170. (cpu_va_bits > gpu_pc_bits)) {
  171. *va_alignment = gpu_pc_bits;
  172. reg->flags |= KBASE_REG_ALIGNED;
  173. }
  174. /*
  175. * Pre-10.1 UKU userland calls mmap for us so return the
  176. * unaligned address and skip the map.
  177. */
  178. if (kctx->api_version < KBASE_API_VERSION(10, 1)) {
  179. *gpu_va = (u64) cookie;
  180. return reg;
  181. }
  182. /*
  183. * GPUCORE-2190:
  184. *
  185. * We still need to return alignment for old userspace.
  186. */
  187. if (*va_alignment)
  188. va_map += 3 * (1UL << *va_alignment);
  189. if (*flags & BASE_MEM_PROT_CPU_RD)
  190. prot |= PROT_READ;
  191. if (*flags & BASE_MEM_PROT_CPU_WR)
  192. prot |= PROT_WRITE;
  193. cpu_addr = vm_mmap(kctx->filp, 0, va_map, prot,
  194. MAP_SHARED, cookie);
  195. if (IS_ERR_VALUE(cpu_addr)) {
  196. kctx->pending_regions[cookie_nr] = NULL;
  197. kctx->cookies |= (1UL << cookie_nr);
  198. goto no_mmap;
  199. }
  200. /*
  201. * If we had to allocate extra VA space to force the
  202. * alignment release it.
  203. */
  204. if (*va_alignment) {
  205. unsigned long alignment = 1UL << *va_alignment;
  206. unsigned long align_mask = alignment - 1;
  207. unsigned long addr;
  208. unsigned long addr_end;
  209. unsigned long aligned_addr;
  210. unsigned long aligned_addr_end;
  211. addr = cpu_addr;
  212. addr_end = addr + va_map;
  213. aligned_addr = (addr + align_mask) &
  214. ~((u64) align_mask);
  215. aligned_addr_end = aligned_addr + va_size;
  216. if ((aligned_addr_end & BASE_MEM_MASK_4GB) == 0) {
  217. /*
  218. * Can't end at 4GB boundary on some GPUs as
  219. * it will halt the shader.
  220. */
  221. aligned_addr += 2 * alignment;
  222. aligned_addr_end += 2 * alignment;
  223. } else if ((aligned_addr & BASE_MEM_MASK_4GB) == 0) {
  224. /*
  225. * Can't start at 4GB boundary on some GPUs as
  226. * it will halt the shader.
  227. */
  228. aligned_addr += alignment;
  229. aligned_addr_end += alignment;
  230. }
  231. /* anything to chop off at the start? */
  232. if (addr != aligned_addr)
  233. vm_munmap(addr, aligned_addr - addr);
  234. /* anything at the end? */
  235. if (addr_end != aligned_addr_end)
  236. vm_munmap(aligned_addr_end,
  237. addr_end - aligned_addr_end);
  238. *gpu_va = (u64) aligned_addr;
  239. } else
  240. *gpu_va = (u64) cpu_addr;
  241. } else /* we control the VA */ {
  242. if (kbase_gpu_mmap(kctx, reg, 0, va_pages, 1) != 0) {
  243. dev_warn(dev, "Failed to map memory on GPU");
  244. kbase_gpu_vm_unlock(kctx);
  245. goto no_mmap;
  246. }
  247. /* return real GPU VA */
  248. *gpu_va = reg->start_pfn << PAGE_SHIFT;
  249. kbase_gpu_vm_unlock(kctx);
  250. }
  251. return reg;
  252. no_mmap:
  253. no_cookie:
  254. no_mem:
  255. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  256. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  257. prepare_failed:
  258. kfree(reg);
  259. no_region:
  260. bad_ex_size:
  261. bad_flags:
  262. bad_size:
  263. return NULL;
  264. }
  265. int kbase_mem_query(struct kbase_context *kctx, u64 gpu_addr, int query, u64 * const out)
  266. {
  267. struct kbase_va_region *reg;
  268. int ret = -EINVAL;
  269. KBASE_DEBUG_ASSERT(kctx);
  270. KBASE_DEBUG_ASSERT(out);
  271. kbase_gpu_vm_lock(kctx);
  272. /* Validate the region */
  273. reg = kbase_region_tracker_find_region_base_address(kctx, gpu_addr);
  274. if (!reg || (reg->flags & KBASE_REG_FREE))
  275. goto out_unlock;
  276. switch (query) {
  277. case KBASE_MEM_QUERY_COMMIT_SIZE:
  278. if (reg->cpu_alloc->type != KBASE_MEM_TYPE_ALIAS) {
  279. *out = kbase_reg_current_backed_size(reg);
  280. } else {
  281. size_t i;
  282. struct kbase_aliased *aliased;
  283. *out = 0;
  284. aliased = reg->cpu_alloc->imported.alias.aliased;
  285. for (i = 0; i < reg->cpu_alloc->imported.alias.nents; i++)
  286. *out += aliased[i].length;
  287. }
  288. break;
  289. case KBASE_MEM_QUERY_VA_SIZE:
  290. *out = reg->nr_pages;
  291. break;
  292. case KBASE_MEM_QUERY_FLAGS:
  293. {
  294. *out = 0;
  295. if (KBASE_REG_CPU_WR & reg->flags)
  296. *out |= BASE_MEM_PROT_CPU_WR;
  297. if (KBASE_REG_CPU_RD & reg->flags)
  298. *out |= BASE_MEM_PROT_CPU_RD;
  299. if (KBASE_REG_CPU_CACHED & reg->flags)
  300. *out |= BASE_MEM_CACHED_CPU;
  301. if (KBASE_REG_GPU_WR & reg->flags)
  302. *out |= BASE_MEM_PROT_GPU_WR;
  303. if (KBASE_REG_GPU_RD & reg->flags)
  304. *out |= BASE_MEM_PROT_GPU_RD;
  305. if (!(KBASE_REG_GPU_NX & reg->flags))
  306. *out |= BASE_MEM_PROT_GPU_EX;
  307. if (KBASE_REG_SHARE_BOTH & reg->flags)
  308. *out |= BASE_MEM_COHERENT_SYSTEM;
  309. if (KBASE_REG_SHARE_IN & reg->flags)
  310. *out |= BASE_MEM_COHERENT_LOCAL;
  311. break;
  312. }
  313. default:
  314. *out = 0;
  315. goto out_unlock;
  316. }
  317. ret = 0;
  318. out_unlock:
  319. kbase_gpu_vm_unlock(kctx);
  320. return ret;
  321. }
  322. /**
  323. * kbase_mem_evictable_reclaim_count_objects - Count number of pages in the
  324. * Ephemeral memory eviction list.
  325. * @s: Shrinker
  326. * @sc: Shrinker control
  327. *
  328. * Return: Number of pages which can be freed.
  329. */
  330. static
  331. unsigned long kbase_mem_evictable_reclaim_count_objects(struct shrinker *s,
  332. struct shrink_control *sc)
  333. {
  334. struct kbase_context *kctx;
  335. struct kbase_mem_phy_alloc *alloc;
  336. unsigned long pages = 0;
  337. kctx = container_of(s, struct kbase_context, reclaim);
  338. mutex_lock(&kctx->evict_lock);
  339. list_for_each_entry(alloc, &kctx->evict_list, evict_node)
  340. pages += alloc->nents;
  341. mutex_unlock(&kctx->evict_lock);
  342. return pages;
  343. }
  344. /**
  345. * kbase_mem_evictable_reclaim_scan_objects - Scan the Ephemeral memory eviction
  346. * list for pages and try to reclaim them.
  347. * @s: Shrinker
  348. * @sc: Shrinker control
  349. *
  350. * Return: Number of pages freed (can be less then requested) or -1 if the
  351. * shrinker failed to free pages in its pool.
  352. *
  353. * Note:
  354. * This function accesses region structures without taking the region lock,
  355. * this is required as the OOM killer can call the shrinker after the region
  356. * lock has already been held.
  357. * This is safe as we can guarantee that a region on the eviction list will
  358. * not be freed (kbase_mem_free_region removes the allocation from the list
  359. * before destroying it), or modified by other parts of the driver.
  360. * The eviction list itself is guarded by the eviction lock and the MMU updates
  361. * are protected by their own lock.
  362. */
  363. static
  364. unsigned long kbase_mem_evictable_reclaim_scan_objects(struct shrinker *s,
  365. struct shrink_control *sc)
  366. {
  367. struct kbase_context *kctx;
  368. struct kbase_mem_phy_alloc *alloc;
  369. struct kbase_mem_phy_alloc *tmp;
  370. unsigned long freed = 0;
  371. kctx = container_of(s, struct kbase_context, reclaim);
  372. mutex_lock(&kctx->evict_lock);
  373. list_for_each_entry_safe(alloc, tmp, &kctx->evict_list, evict_node) {
  374. int err;
  375. err = kbase_mem_shrink_gpu_mapping(kctx, alloc->reg,
  376. 0, alloc->nents);
  377. if (err != 0) {
  378. /*
  379. * Failed to remove GPU mapping, tell the shrinker
  380. * to stop trying to shrink our slab even though we
  381. * have pages in it.
  382. */
  383. freed = -1;
  384. goto out_unlock;
  385. }
  386. /*
  387. * Update alloc->evicted before freeing the backing so the
  388. * helper can determine that it needs to bypass the accounting
  389. * and memory pool.
  390. */
  391. alloc->evicted = alloc->nents;
  392. kbase_free_phy_pages_helper(alloc, alloc->evicted);
  393. freed += alloc->evicted;
  394. list_del_init(&alloc->evict_node);
  395. /*
  396. * Inform the JIT allocator this region has lost backing
  397. * as it might need to free the allocation.
  398. */
  399. kbase_jit_backing_lost(alloc->reg);
  400. /* Enough pages have been freed so stop now */
  401. if (freed > sc->nr_to_scan)
  402. break;
  403. }
  404. out_unlock:
  405. mutex_unlock(&kctx->evict_lock);
  406. return freed;
  407. }
  408. int kbase_mem_evictable_init(struct kbase_context *kctx)
  409. {
  410. INIT_LIST_HEAD(&kctx->evict_list);
  411. mutex_init(&kctx->evict_lock);
  412. /* Register shrinker */
  413. kctx->reclaim.count_objects = kbase_mem_evictable_reclaim_count_objects;
  414. kctx->reclaim.scan_objects = kbase_mem_evictable_reclaim_scan_objects;
  415. kctx->reclaim.seeks = DEFAULT_SEEKS;
  416. /* Kernel versions prior to 3.1 :
  417. * struct shrinker does not define batch */
  418. kctx->reclaim.batch = 0;
  419. register_shrinker(&kctx->reclaim);
  420. return 0;
  421. }
  422. void kbase_mem_evictable_deinit(struct kbase_context *kctx)
  423. {
  424. unregister_shrinker(&kctx->reclaim);
  425. }
  426. struct kbase_mem_zone_cache_entry {
  427. /* List head used to link the cache entry to the memory allocation. */
  428. struct list_head zone_node;
  429. /* The zone the cacheline is for. */
  430. struct zone *zone;
  431. /* The number of pages in the allocation which belong to this zone. */
  432. u64 count;
  433. };
  434. static bool kbase_zone_cache_builder(struct kbase_mem_phy_alloc *alloc,
  435. size_t start_offset)
  436. {
  437. struct kbase_mem_zone_cache_entry *cache = NULL;
  438. size_t i;
  439. int ret = 0;
  440. for (i = start_offset; i < alloc->nents; i++) {
  441. struct page *p = phys_to_page(alloc->pages[i]);
  442. struct zone *zone = page_zone(p);
  443. bool create = true;
  444. if (cache && (cache->zone == zone)) {
  445. /*
  446. * Fast path check as most of the time adjacent
  447. * pages come from the same zone.
  448. */
  449. create = false;
  450. } else {
  451. /*
  452. * Slow path check, walk all the cache entries to see
  453. * if we already know about this zone.
  454. */
  455. list_for_each_entry(cache, &alloc->zone_cache, zone_node) {
  456. if (cache->zone == zone) {
  457. create = false;
  458. break;
  459. }
  460. }
  461. }
  462. /* This zone wasn't found in the cache, create an entry for it */
  463. if (create) {
  464. cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  465. if (!cache) {
  466. ret = -ENOMEM;
  467. goto bail;
  468. }
  469. cache->zone = zone;
  470. cache->count = 0;
  471. list_add(&cache->zone_node, &alloc->zone_cache);
  472. }
  473. cache->count++;
  474. }
  475. return 0;
  476. bail:
  477. return ret;
  478. }
  479. int kbase_zone_cache_update(struct kbase_mem_phy_alloc *alloc,
  480. size_t start_offset)
  481. {
  482. /*
  483. * Bail if the zone cache is empty, only update the cache if it
  484. * existed in the first place.
  485. */
  486. if (list_empty(&alloc->zone_cache))
  487. return 0;
  488. return kbase_zone_cache_builder(alloc, start_offset);
  489. }
  490. int kbase_zone_cache_build(struct kbase_mem_phy_alloc *alloc)
  491. {
  492. /* Bail if the zone cache already exists */
  493. if (!list_empty(&alloc->zone_cache))
  494. return 0;
  495. return kbase_zone_cache_builder(alloc, 0);
  496. }
  497. void kbase_zone_cache_clear(struct kbase_mem_phy_alloc *alloc)
  498. {
  499. struct kbase_mem_zone_cache_entry *walker;
  500. while (!list_empty(&alloc->zone_cache)) {
  501. walker = list_first_entry(&alloc->zone_cache,
  502. struct kbase_mem_zone_cache_entry, zone_node);
  503. list_del(&walker->zone_node);
  504. kfree(walker);
  505. }
  506. }
  507. /**
  508. * kbase_mem_evictable_mark_reclaim - Mark the pages as reclaimable.
  509. * @alloc: The physical allocation
  510. */
  511. static void kbase_mem_evictable_mark_reclaim(struct kbase_mem_phy_alloc *alloc)
  512. {
  513. struct kbase_context *kctx = alloc->imported.kctx;
  514. struct kbase_mem_zone_cache_entry *zone_cache;
  515. int __maybe_unused new_page_count;
  516. int err;
  517. /* Attempt to build a zone cache of tracking */
  518. err = kbase_zone_cache_build(alloc);
  519. if (err == 0) {
  520. /* Bulk update all the zones */
  521. list_for_each_entry(zone_cache, &alloc->zone_cache, zone_node) {
  522. zone_page_state_add(zone_cache->count,
  523. zone_cache->zone, NR_SLAB_RECLAIMABLE);
  524. }
  525. } else {
  526. /* Fall-back to page by page updates */
  527. int i;
  528. for (i = 0; i < alloc->nents; i++) {
  529. struct page *p = phys_to_page(alloc->pages[i]);
  530. struct zone *zone = page_zone(p);
  531. zone_page_state_add(1, zone, NR_SLAB_RECLAIMABLE);
  532. }
  533. }
  534. kbase_process_page_usage_dec(kctx, alloc->nents);
  535. new_page_count = kbase_atomic_sub_pages(alloc->nents,
  536. &kctx->used_pages);
  537. kbase_atomic_sub_pages(alloc->nents, &kctx->kbdev->memdev.used_pages);
  538. kbase_tlstream_aux_pagesalloc(
  539. (u32)kctx->id,
  540. (u64)new_page_count);
  541. }
  542. /**
  543. * kbase_mem_evictable_unmark_reclaim - Mark the pages as no longer reclaimable.
  544. * @alloc: The physical allocation
  545. */
  546. static
  547. void kbase_mem_evictable_unmark_reclaim(struct kbase_mem_phy_alloc *alloc)
  548. {
  549. struct kbase_context *kctx = alloc->imported.kctx;
  550. struct kbase_mem_zone_cache_entry *zone_cache;
  551. int __maybe_unused new_page_count;
  552. int err;
  553. new_page_count = kbase_atomic_add_pages(alloc->nents,
  554. &kctx->used_pages);
  555. kbase_atomic_add_pages(alloc->nents, &kctx->kbdev->memdev.used_pages);
  556. /* Increase mm counters so that the allocation is accounted for
  557. * against the process and thus is visible to the OOM killer,
  558. * then remove it from the reclaimable accounting. */
  559. kbase_process_page_usage_inc(kctx, alloc->nents);
  560. /* Attempt to build a zone cache of tracking */
  561. err = kbase_zone_cache_build(alloc);
  562. if (err == 0) {
  563. /* Bulk update all the zones */
  564. list_for_each_entry(zone_cache, &alloc->zone_cache, zone_node) {
  565. zone_page_state_add(-zone_cache->count,
  566. zone_cache->zone, NR_SLAB_RECLAIMABLE);
  567. }
  568. } else {
  569. /* Fall-back to page by page updates */
  570. int i;
  571. for (i = 0; i < alloc->nents; i++) {
  572. struct page *p = phys_to_page(alloc->pages[i]);
  573. struct zone *zone = page_zone(p);
  574. zone_page_state_add(-1, zone, NR_SLAB_RECLAIMABLE);
  575. }
  576. }
  577. kbase_tlstream_aux_pagesalloc(
  578. (u32)kctx->id,
  579. (u64)new_page_count);
  580. }
  581. int kbase_mem_evictable_make(struct kbase_mem_phy_alloc *gpu_alloc)
  582. {
  583. struct kbase_context *kctx = gpu_alloc->imported.kctx;
  584. int err;
  585. lockdep_assert_held(&kctx->reg_lock);
  586. /* This alloction can't already be on a list. */
  587. WARN_ON(!list_empty(&gpu_alloc->evict_node));
  588. /*
  589. * Try to shrink the CPU mappings as required, if we fail then
  590. * fail the process of making this allocation evictable.
  591. */
  592. err = kbase_mem_shrink_cpu_mapping(kctx, gpu_alloc->reg,
  593. 0, gpu_alloc->nents);
  594. if (err)
  595. return -EINVAL;
  596. /*
  597. * Add the allocation to the eviction list, after this point the shrink
  598. * can reclaim it.
  599. */
  600. mutex_lock(&kctx->evict_lock);
  601. list_add(&gpu_alloc->evict_node, &kctx->evict_list);
  602. mutex_unlock(&kctx->evict_lock);
  603. kbase_mem_evictable_mark_reclaim(gpu_alloc);
  604. gpu_alloc->reg->flags |= KBASE_REG_DONT_NEED;
  605. return 0;
  606. }
  607. bool kbase_mem_evictable_unmake(struct kbase_mem_phy_alloc *gpu_alloc)
  608. {
  609. struct kbase_context *kctx = gpu_alloc->imported.kctx;
  610. int err = 0;
  611. lockdep_assert_held(&kctx->reg_lock);
  612. /*
  613. * First remove the allocation from the eviction list as it's no
  614. * longer eligible for eviction.
  615. */
  616. mutex_lock(&kctx->evict_lock);
  617. list_del_init(&gpu_alloc->evict_node);
  618. mutex_unlock(&kctx->evict_lock);
  619. if (gpu_alloc->evicted == 0) {
  620. /*
  621. * The backing is still present, update the VM stats as it's
  622. * in use again.
  623. */
  624. kbase_mem_evictable_unmark_reclaim(gpu_alloc);
  625. } else {
  626. /* If the region is still alive ... */
  627. if (gpu_alloc->reg) {
  628. /* ... allocate replacement backing ... */
  629. err = kbase_alloc_phy_pages_helper(gpu_alloc,
  630. gpu_alloc->evicted);
  631. /*
  632. * ... and grow the mapping back to its
  633. * pre-eviction size.
  634. */
  635. if (!err)
  636. err = kbase_mem_grow_gpu_mapping(kctx,
  637. gpu_alloc->reg,
  638. gpu_alloc->evicted, 0);
  639. gpu_alloc->evicted = 0;
  640. }
  641. }
  642. /* If the region is still alive remove the DONT_NEED attribute. */
  643. if (gpu_alloc->reg)
  644. gpu_alloc->reg->flags &= ~KBASE_REG_DONT_NEED;
  645. return (err == 0);
  646. }
  647. int kbase_mem_flags_change(struct kbase_context *kctx, u64 gpu_addr, unsigned int flags, unsigned int mask)
  648. {
  649. struct kbase_va_region *reg;
  650. int ret = -EINVAL;
  651. unsigned int real_flags = 0;
  652. unsigned int prev_flags = 0;
  653. bool prev_needed, new_needed;
  654. KBASE_DEBUG_ASSERT(kctx);
  655. if (!gpu_addr)
  656. return -EINVAL;
  657. /* nuke other bits */
  658. flags &= mask;
  659. /* check for only supported flags */
  660. if (flags & ~(BASE_MEM_FLAGS_MODIFIABLE))
  661. goto out;
  662. /* mask covers bits we don't support? */
  663. if (mask & ~(BASE_MEM_FLAGS_MODIFIABLE))
  664. goto out;
  665. /* convert flags */
  666. if (BASE_MEM_COHERENT_SYSTEM & flags)
  667. real_flags |= KBASE_REG_SHARE_BOTH;
  668. else if (BASE_MEM_COHERENT_LOCAL & flags)
  669. real_flags |= KBASE_REG_SHARE_IN;
  670. /* now we can lock down the context, and find the region */
  671. down_write(&current->mm->mmap_sem);
  672. kbase_gpu_vm_lock(kctx);
  673. /* Validate the region */
  674. reg = kbase_region_tracker_find_region_base_address(kctx, gpu_addr);
  675. if (!reg || (reg->flags & KBASE_REG_FREE))
  676. goto out_unlock;
  677. /* Is the region being transitioning between not needed and needed? */
  678. prev_needed = (KBASE_REG_DONT_NEED & reg->flags) == KBASE_REG_DONT_NEED;
  679. new_needed = (BASE_MEM_DONT_NEED & flags) == BASE_MEM_DONT_NEED;
  680. if (prev_needed != new_needed) {
  681. /* Aliased allocations can't be made ephemeral */
  682. if (atomic_read(&reg->cpu_alloc->gpu_mappings) > 1)
  683. goto out_unlock;
  684. if (new_needed) {
  685. /* Only native allocations can be marked not needed */
  686. if (reg->cpu_alloc->type != KBASE_MEM_TYPE_NATIVE) {
  687. ret = -EINVAL;
  688. goto out_unlock;
  689. }
  690. ret = kbase_mem_evictable_make(reg->gpu_alloc);
  691. if (ret)
  692. goto out_unlock;
  693. } else {
  694. kbase_mem_evictable_unmake(reg->gpu_alloc);
  695. }
  696. }
  697. /* limit to imported memory */
  698. if ((reg->gpu_alloc->type != KBASE_MEM_TYPE_IMPORTED_UMP) &&
  699. (reg->gpu_alloc->type != KBASE_MEM_TYPE_IMPORTED_UMM))
  700. goto out_unlock;
  701. /* no change? */
  702. if (real_flags == (reg->flags & (KBASE_REG_SHARE_IN | KBASE_REG_SHARE_BOTH))) {
  703. ret = 0;
  704. goto out_unlock;
  705. }
  706. /* save for roll back */
  707. prev_flags = reg->flags;
  708. reg->flags &= ~(KBASE_REG_SHARE_IN | KBASE_REG_SHARE_BOTH);
  709. reg->flags |= real_flags;
  710. /* Currently supporting only imported memory */
  711. switch (reg->gpu_alloc->type) {
  712. #ifdef CONFIG_UMP
  713. case KBASE_MEM_TYPE_IMPORTED_UMP:
  714. ret = kbase_mmu_update_pages(kctx, reg->start_pfn, kbase_get_cpu_phy_pages(reg), reg->gpu_alloc->nents, reg->flags);
  715. break;
  716. #endif
  717. #ifdef CONFIG_DMA_SHARED_BUFFER
  718. case KBASE_MEM_TYPE_IMPORTED_UMM:
  719. /* Future use will use the new flags, existing mapping will NOT be updated
  720. * as memory should not be in use by the GPU when updating the flags.
  721. */
  722. ret = 0;
  723. WARN_ON(reg->gpu_alloc->imported.umm.current_mapping_usage_count);
  724. break;
  725. #endif
  726. default:
  727. break;
  728. }
  729. /* roll back on error, i.e. not UMP */
  730. if (ret)
  731. reg->flags = prev_flags;
  732. out_unlock:
  733. kbase_gpu_vm_unlock(kctx);
  734. up_write(&current->mm->mmap_sem);
  735. out:
  736. return ret;
  737. }
  738. #define KBASE_MEM_IMPORT_HAVE_PAGES (1UL << BASE_MEM_FLAGS_NR_BITS)
  739. #ifdef CONFIG_UMP
  740. static struct kbase_va_region *kbase_mem_from_ump(struct kbase_context *kctx, ump_secure_id id, u64 *va_pages, u64 *flags)
  741. {
  742. struct kbase_va_region *reg;
  743. ump_dd_handle umph;
  744. u64 block_count;
  745. const ump_dd_physical_block_64 *block_array;
  746. u64 i, j;
  747. int page = 0;
  748. ump_alloc_flags ump_flags;
  749. ump_alloc_flags cpu_flags;
  750. ump_alloc_flags gpu_flags;
  751. if (*flags & BASE_MEM_SECURE)
  752. goto bad_flags;
  753. umph = ump_dd_from_secure_id(id);
  754. if (umph == UMP_DD_INVALID_MEMORY_HANDLE)
  755. goto bad_id;
  756. ump_flags = ump_dd_allocation_flags_get(umph);
  757. cpu_flags = (ump_flags >> UMP_DEVICE_CPU_SHIFT) & UMP_DEVICE_MASK;
  758. gpu_flags = (ump_flags >> DEFAULT_UMP_GPU_DEVICE_SHIFT) &
  759. UMP_DEVICE_MASK;
  760. *va_pages = ump_dd_size_get_64(umph);
  761. *va_pages >>= PAGE_SHIFT;
  762. if (!*va_pages)
  763. goto bad_size;
  764. if (*va_pages > (U64_MAX / PAGE_SIZE))
  765. /* 64-bit address range is the max */
  766. goto bad_size;
  767. if (*flags & BASE_MEM_SAME_VA)
  768. reg = kbase_alloc_free_region(kctx, 0, *va_pages, KBASE_REG_ZONE_SAME_VA);
  769. else
  770. reg = kbase_alloc_free_region(kctx, 0, *va_pages, KBASE_REG_ZONE_CUSTOM_VA);
  771. if (!reg)
  772. goto no_region;
  773. /* we've got pages to map now, and support SAME_VA */
  774. *flags |= KBASE_MEM_IMPORT_HAVE_PAGES;
  775. reg->gpu_alloc = kbase_alloc_create(*va_pages, KBASE_MEM_TYPE_IMPORTED_UMP);
  776. if (IS_ERR_OR_NULL(reg->gpu_alloc))
  777. goto no_alloc_obj;
  778. reg->cpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
  779. reg->gpu_alloc->imported.ump_handle = umph;
  780. reg->flags &= ~KBASE_REG_FREE;
  781. reg->flags |= KBASE_REG_GPU_NX; /* UMP is always No eXecute */
  782. reg->flags &= ~KBASE_REG_GROWABLE; /* UMP cannot be grown */
  783. /* Override import flags based on UMP flags */
  784. *flags &= ~(BASE_MEM_CACHED_CPU);
  785. *flags &= ~(BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_CPU_WR);
  786. *flags &= ~(BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR);
  787. if ((cpu_flags & (UMP_HINT_DEVICE_RD | UMP_HINT_DEVICE_WR)) ==
  788. (UMP_HINT_DEVICE_RD | UMP_HINT_DEVICE_WR)) {
  789. reg->flags |= KBASE_REG_CPU_CACHED;
  790. *flags |= BASE_MEM_CACHED_CPU;
  791. }
  792. if (cpu_flags & UMP_PROT_CPU_WR) {
  793. reg->flags |= KBASE_REG_CPU_WR;
  794. *flags |= BASE_MEM_PROT_CPU_WR;
  795. }
  796. if (cpu_flags & UMP_PROT_CPU_RD) {
  797. reg->flags |= KBASE_REG_CPU_RD;
  798. *flags |= BASE_MEM_PROT_CPU_RD;
  799. }
  800. if ((gpu_flags & (UMP_HINT_DEVICE_RD | UMP_HINT_DEVICE_WR)) ==
  801. (UMP_HINT_DEVICE_RD | UMP_HINT_DEVICE_WR))
  802. reg->flags |= KBASE_REG_GPU_CACHED;
  803. if (gpu_flags & UMP_PROT_DEVICE_WR) {
  804. reg->flags |= KBASE_REG_GPU_WR;
  805. *flags |= BASE_MEM_PROT_GPU_WR;
  806. }
  807. if (gpu_flags & UMP_PROT_DEVICE_RD) {
  808. reg->flags |= KBASE_REG_GPU_RD;
  809. *flags |= BASE_MEM_PROT_GPU_RD;
  810. }
  811. /* ump phys block query */
  812. ump_dd_phys_blocks_get_64(umph, &block_count, &block_array);
  813. for (i = 0; i < block_count; i++) {
  814. for (j = 0; j < (block_array[i].size >> PAGE_SHIFT); j++) {
  815. reg->gpu_alloc->pages[page] = block_array[i].addr + (j << PAGE_SHIFT);
  816. page++;
  817. }
  818. }
  819. reg->gpu_alloc->nents = *va_pages;
  820. reg->extent = 0;
  821. return reg;
  822. no_alloc_obj:
  823. kfree(reg);
  824. no_region:
  825. bad_size:
  826. ump_dd_release(umph);
  827. bad_id:
  828. bad_flags:
  829. return NULL;
  830. }
  831. #endif /* CONFIG_UMP */
  832. #ifdef CONFIG_DMA_SHARED_BUFFER
  833. static struct kbase_va_region *kbase_mem_from_umm(struct kbase_context *kctx, int fd, u64 *va_pages, u64 *flags)
  834. {
  835. struct kbase_va_region *reg;
  836. struct dma_buf *dma_buf;
  837. struct dma_buf_attachment *dma_attachment;
  838. bool shared_zone = false;
  839. dma_buf = dma_buf_get(fd);
  840. if (IS_ERR_OR_NULL(dma_buf))
  841. goto no_buf;
  842. dma_attachment = dma_buf_attach(dma_buf, kctx->kbdev->dev);
  843. if (!dma_attachment)
  844. goto no_attachment;
  845. *va_pages = PAGE_ALIGN(dma_buf->size) >> PAGE_SHIFT;
  846. if (!*va_pages)
  847. goto bad_size;
  848. if (*va_pages > (U64_MAX / PAGE_SIZE))
  849. /* 64-bit address range is the max */
  850. goto bad_size;
  851. /* ignore SAME_VA */
  852. *flags &= ~BASE_MEM_SAME_VA;
  853. if (*flags & BASE_MEM_IMPORT_SHARED)
  854. shared_zone = true;
  855. #ifdef CONFIG_64BIT
  856. if (!kctx->is_compat) {
  857. /*
  858. * 64-bit tasks require us to reserve VA on the CPU that we use
  859. * on the GPU.
  860. */
  861. shared_zone = true;
  862. }
  863. #endif
  864. if (shared_zone) {
  865. *flags |= BASE_MEM_NEED_MMAP;
  866. reg = kbase_alloc_free_region(kctx, 0, *va_pages, KBASE_REG_ZONE_SAME_VA);
  867. } else {
  868. reg = kbase_alloc_free_region(kctx, 0, *va_pages, KBASE_REG_ZONE_CUSTOM_VA);
  869. }
  870. if (!reg)
  871. goto no_region;
  872. reg->gpu_alloc = kbase_alloc_create(*va_pages, KBASE_MEM_TYPE_IMPORTED_UMM);
  873. if (IS_ERR_OR_NULL(reg->gpu_alloc))
  874. goto no_alloc_obj;
  875. reg->cpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
  876. /* No pages to map yet */
  877. reg->gpu_alloc->nents = 0;
  878. reg->flags &= ~KBASE_REG_FREE;
  879. reg->flags |= KBASE_REG_GPU_NX; /* UMM is always No eXecute */
  880. reg->flags &= ~KBASE_REG_GROWABLE; /* UMM cannot be grown */
  881. reg->flags |= KBASE_REG_GPU_CACHED;
  882. if (*flags & BASE_MEM_PROT_CPU_WR)
  883. reg->flags |= KBASE_REG_CPU_WR;
  884. if (*flags & BASE_MEM_PROT_CPU_RD)
  885. reg->flags |= KBASE_REG_CPU_RD;
  886. if (*flags & BASE_MEM_PROT_GPU_WR)
  887. reg->flags |= KBASE_REG_GPU_WR;
  888. if (*flags & BASE_MEM_PROT_GPU_RD)
  889. reg->flags |= KBASE_REG_GPU_RD;
  890. if (*flags & BASE_MEM_SECURE)
  891. reg->flags |= KBASE_REG_SECURE;
  892. /* no read or write permission given on import, only on run do we give the right permissions */
  893. reg->gpu_alloc->type = BASE_MEM_IMPORT_TYPE_UMM;
  894. reg->gpu_alloc->imported.umm.sgt = NULL;
  895. reg->gpu_alloc->imported.umm.dma_buf = dma_buf;
  896. reg->gpu_alloc->imported.umm.dma_attachment = dma_attachment;
  897. reg->gpu_alloc->imported.umm.current_mapping_usage_count = 0;
  898. reg->extent = 0;
  899. return reg;
  900. no_alloc_obj:
  901. kfree(reg);
  902. no_region:
  903. bad_size:
  904. dma_buf_detach(dma_buf, dma_attachment);
  905. no_attachment:
  906. dma_buf_put(dma_buf);
  907. no_buf:
  908. return NULL;
  909. }
  910. #endif /* CONFIG_DMA_SHARED_BUFFER */
  911. static struct kbase_va_region *kbase_mem_from_user_buffer(
  912. struct kbase_context *kctx, unsigned long address,
  913. unsigned long size, u64 *va_pages, u64 *flags)
  914. {
  915. struct kbase_va_region *reg;
  916. long faulted_pages;
  917. int zone = KBASE_REG_ZONE_CUSTOM_VA;
  918. bool shared_zone = false;
  919. *va_pages = (PAGE_ALIGN(address + size) >> PAGE_SHIFT) -
  920. PFN_DOWN(address);
  921. if (!*va_pages)
  922. goto bad_size;
  923. if (*va_pages > (UINT64_MAX / PAGE_SIZE))
  924. /* 64-bit address range is the max */
  925. goto bad_size;
  926. /* SAME_VA generally not supported with imported memory (no known use cases) */
  927. *flags &= ~BASE_MEM_SAME_VA;
  928. if (*flags & BASE_MEM_IMPORT_SHARED)
  929. shared_zone = true;
  930. #ifdef CONFIG_64BIT
  931. if (!kctx->is_compat) {
  932. /*
  933. * 64-bit tasks require us to reserve VA on the CPU that we use
  934. * on the GPU.
  935. */
  936. shared_zone = true;
  937. }
  938. #endif
  939. if (shared_zone) {
  940. *flags |= BASE_MEM_NEED_MMAP;
  941. zone = KBASE_REG_ZONE_SAME_VA;
  942. }
  943. reg = kbase_alloc_free_region(kctx, 0, *va_pages, zone);
  944. if (!reg)
  945. goto no_region;
  946. reg->gpu_alloc = kbase_alloc_create(*va_pages,
  947. KBASE_MEM_TYPE_IMPORTED_USER_BUF);
  948. if (IS_ERR_OR_NULL(reg->gpu_alloc))
  949. goto no_alloc_obj;
  950. reg->cpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
  951. reg->flags &= ~KBASE_REG_FREE;
  952. reg->flags |= KBASE_REG_GPU_NX; /* User-buffers are always No eXecute */
  953. reg->flags &= ~KBASE_REG_GROWABLE; /* Cannot be grown */
  954. if (*flags & BASE_MEM_PROT_CPU_WR)
  955. reg->flags |= KBASE_REG_CPU_WR;
  956. if (*flags & BASE_MEM_PROT_CPU_RD)
  957. reg->flags |= KBASE_REG_CPU_RD;
  958. if (*flags & BASE_MEM_PROT_GPU_WR)
  959. reg->flags |= KBASE_REG_GPU_WR;
  960. if (*flags & BASE_MEM_PROT_GPU_RD)
  961. reg->flags |= KBASE_REG_GPU_RD;
  962. down_read(&current->mm->mmap_sem);
  963. /* A sanity check that get_user_pages will work on the memory */
  964. /* (so the initial import fails on weird memory regions rather than */
  965. /* the job failing when we try to handle the external resources). */
  966. /* It doesn't take a reference to the pages (because the page list is NULL). */
  967. /* We can't really store the page list because that would involve */
  968. /* keeping the pages pinned - instead we pin/unpin around the job */
  969. /* (as part of the external resources handling code) */
  970. faulted_pages = get_user_pages(address, *va_pages,
  971. (reg->flags & KBASE_REG_GPU_WR) ? FOLL_WRITE : 0, NULL, NULL);
  972. up_read(&current->mm->mmap_sem);
  973. if (faulted_pages != *va_pages)
  974. goto fault_mismatch;
  975. reg->gpu_alloc->imported.user_buf.size = size;
  976. reg->gpu_alloc->imported.user_buf.address = address;
  977. reg->gpu_alloc->imported.user_buf.nr_pages = faulted_pages;
  978. reg->gpu_alloc->imported.user_buf.pages = kmalloc_array(faulted_pages,
  979. sizeof(struct page *), GFP_KERNEL);
  980. reg->gpu_alloc->imported.user_buf.mm = current->mm;
  981. atomic_inc(&current->mm->mm_count);
  982. if (!reg->gpu_alloc->imported.user_buf.pages)
  983. goto no_page_array;
  984. reg->gpu_alloc->nents = 0;
  985. reg->extent = 0;
  986. return reg;
  987. no_page_array:
  988. fault_mismatch:
  989. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  990. no_alloc_obj:
  991. kfree(reg);
  992. no_region:
  993. bad_size:
  994. return NULL;
  995. }
  996. u64 kbase_mem_alias(struct kbase_context *kctx, u64 *flags, u64 stride,
  997. u64 nents, struct base_mem_aliasing_info *ai,
  998. u64 *num_pages)
  999. {
  1000. struct kbase_va_region *reg;
  1001. u64 gpu_va;
  1002. size_t i;
  1003. bool coherent;
  1004. KBASE_DEBUG_ASSERT(kctx);
  1005. KBASE_DEBUG_ASSERT(flags);
  1006. KBASE_DEBUG_ASSERT(ai);
  1007. KBASE_DEBUG_ASSERT(num_pages);
  1008. /* mask to only allowed flags */
  1009. *flags &= (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR |
  1010. BASE_MEM_COHERENT_SYSTEM | BASE_MEM_COHERENT_LOCAL |
  1011. BASE_MEM_COHERENT_SYSTEM_REQUIRED);
  1012. if (!(*flags & (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR))) {
  1013. dev_warn(kctx->kbdev->dev,
  1014. "kbase_mem_alias called with bad flags (%llx)",
  1015. (unsigned long long)*flags);
  1016. goto bad_flags;
  1017. }
  1018. coherent = (*flags & BASE_MEM_COHERENT_SYSTEM) != 0 ||
  1019. (*flags & BASE_MEM_COHERENT_SYSTEM_REQUIRED) != 0;
  1020. if (!stride)
  1021. goto bad_stride;
  1022. if (!nents)
  1023. goto bad_nents;
  1024. if ((nents * stride) > (U64_MAX / PAGE_SIZE))
  1025. /* 64-bit address range is the max */
  1026. goto bad_size;
  1027. /* calculate the number of pages this alias will cover */
  1028. *num_pages = nents * stride;
  1029. #ifdef CONFIG_64BIT
  1030. if (!kctx->is_compat) {
  1031. /* 64-bit tasks must MMAP anyway, but not expose this address to
  1032. * clients */
  1033. *flags |= BASE_MEM_NEED_MMAP;
  1034. reg = kbase_alloc_free_region(kctx, 0, *num_pages,
  1035. KBASE_REG_ZONE_SAME_VA);
  1036. } else {
  1037. #else
  1038. if (1) {
  1039. #endif
  1040. reg = kbase_alloc_free_region(kctx, 0, *num_pages,
  1041. KBASE_REG_ZONE_CUSTOM_VA);
  1042. }
  1043. if (!reg)
  1044. goto no_reg;
  1045. /* zero-sized page array, as we don't need one/can support one */
  1046. reg->gpu_alloc = kbase_alloc_create(0, KBASE_MEM_TYPE_ALIAS);
  1047. if (IS_ERR_OR_NULL(reg->gpu_alloc))
  1048. goto no_alloc_obj;
  1049. reg->cpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
  1050. kbase_update_region_flags(kctx, reg, *flags);
  1051. reg->gpu_alloc->imported.alias.nents = nents;
  1052. reg->gpu_alloc->imported.alias.stride = stride;
  1053. reg->gpu_alloc->imported.alias.aliased = vzalloc(sizeof(*reg->gpu_alloc->imported.alias.aliased) * nents);
  1054. if (!reg->gpu_alloc->imported.alias.aliased)
  1055. goto no_aliased_array;
  1056. kbase_gpu_vm_lock(kctx);
  1057. /* validate and add src handles */
  1058. for (i = 0; i < nents; i++) {
  1059. if (ai[i].handle.basep.handle < BASE_MEM_FIRST_FREE_ADDRESS) {
  1060. if (ai[i].handle.basep.handle !=
  1061. BASEP_MEM_WRITE_ALLOC_PAGES_HANDLE)
  1062. goto bad_handle; /* unsupported magic handle */
  1063. if (!ai[i].length)
  1064. goto bad_handle; /* must be > 0 */
  1065. if (ai[i].length > stride)
  1066. goto bad_handle; /* can't be larger than the
  1067. stride */
  1068. reg->gpu_alloc->imported.alias.aliased[i].length = ai[i].length;
  1069. } else {
  1070. struct kbase_va_region *aliasing_reg;
  1071. struct kbase_mem_phy_alloc *alloc;
  1072. aliasing_reg = kbase_region_tracker_find_region_base_address(
  1073. kctx,
  1074. (ai[i].handle.basep.handle >> PAGE_SHIFT) << PAGE_SHIFT);
  1075. /* validate found region */
  1076. if (!aliasing_reg)
  1077. goto bad_handle; /* Not found */
  1078. if (aliasing_reg->flags & KBASE_REG_FREE)
  1079. goto bad_handle; /* Free region */
  1080. if (aliasing_reg->flags & KBASE_REG_DONT_NEED)
  1081. goto bad_handle; /* Ephemeral region */
  1082. if (!aliasing_reg->gpu_alloc)
  1083. goto bad_handle; /* No alloc */
  1084. if (aliasing_reg->gpu_alloc->type != KBASE_MEM_TYPE_NATIVE)
  1085. goto bad_handle; /* Not a native alloc */
  1086. if (coherent != ((aliasing_reg->flags & KBASE_REG_SHARE_BOTH) != 0))
  1087. goto bad_handle;
  1088. /* Non-coherent memory cannot alias
  1089. coherent memory, and vice versa.*/
  1090. /* check size against stride */
  1091. if (!ai[i].length)
  1092. goto bad_handle; /* must be > 0 */
  1093. if (ai[i].length > stride)
  1094. goto bad_handle; /* can't be larger than the
  1095. stride */
  1096. alloc = aliasing_reg->gpu_alloc;
  1097. /* check against the alloc's size */
  1098. if (ai[i].offset > alloc->nents)
  1099. goto bad_handle; /* beyond end */
  1100. if (ai[i].offset + ai[i].length > alloc->nents)
  1101. goto bad_handle; /* beyond end */
  1102. reg->gpu_alloc->imported.alias.aliased[i].alloc = kbase_mem_phy_alloc_get(alloc);
  1103. reg->gpu_alloc->imported.alias.aliased[i].length = ai[i].length;
  1104. reg->gpu_alloc->imported.alias.aliased[i].offset = ai[i].offset;
  1105. }
  1106. }
  1107. #ifdef CONFIG_64BIT
  1108. if (!kctx->is_compat) {
  1109. /* Bind to a cookie */
  1110. if (!kctx->cookies) {
  1111. dev_err(kctx->kbdev->dev, "No cookies available for allocation!");
  1112. goto no_cookie;
  1113. }
  1114. /* return a cookie */
  1115. gpu_va = __ffs(kctx->cookies);
  1116. kctx->cookies &= ~(1UL << gpu_va);
  1117. BUG_ON(kctx->pending_regions[gpu_va]);
  1118. kctx->pending_regions[gpu_va] = reg;
  1119. /* relocate to correct base */
  1120. gpu_va += PFN_DOWN(BASE_MEM_COOKIE_BASE);
  1121. gpu_va <<= PAGE_SHIFT;
  1122. } else /* we control the VA */ {
  1123. #else
  1124. if (1) {
  1125. #endif
  1126. if (kbase_gpu_mmap(kctx, reg, 0, *num_pages, 1) != 0) {
  1127. dev_warn(kctx->kbdev->dev, "Failed to map memory on GPU");
  1128. goto no_mmap;
  1129. }
  1130. /* return real GPU VA */
  1131. gpu_va = reg->start_pfn << PAGE_SHIFT;
  1132. }
  1133. reg->flags &= ~KBASE_REG_FREE;
  1134. reg->flags &= ~KBASE_REG_GROWABLE;
  1135. kbase_gpu_vm_unlock(kctx);
  1136. return gpu_va;
  1137. #ifdef CONFIG_64BIT
  1138. no_cookie:
  1139. #endif
  1140. no_mmap:
  1141. bad_handle:
  1142. kbase_gpu_vm_unlock(kctx);
  1143. no_aliased_array:
  1144. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  1145. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  1146. no_alloc_obj:
  1147. kfree(reg);
  1148. no_reg:
  1149. bad_size:
  1150. bad_nents:
  1151. bad_stride:
  1152. bad_flags:
  1153. return 0;
  1154. }
  1155. int kbase_mem_import(struct kbase_context *kctx, enum base_mem_import_type type,
  1156. void __user *phandle, u64 *gpu_va, u64 *va_pages,
  1157. u64 *flags)
  1158. {
  1159. struct kbase_va_region *reg;
  1160. KBASE_DEBUG_ASSERT(kctx);
  1161. KBASE_DEBUG_ASSERT(gpu_va);
  1162. KBASE_DEBUG_ASSERT(va_pages);
  1163. KBASE_DEBUG_ASSERT(flags);
  1164. #ifdef CONFIG_64BIT
  1165. if (!kctx->is_compat)
  1166. *flags |= BASE_MEM_SAME_VA;
  1167. #endif
  1168. if (!kbase_check_import_flags(*flags)) {
  1169. dev_warn(kctx->kbdev->dev,
  1170. "kbase_mem_import called with bad flags (%llx)",
  1171. (unsigned long long)*flags);
  1172. goto bad_flags;
  1173. }
  1174. switch (type) {
  1175. #ifdef CONFIG_UMP
  1176. case BASE_MEM_IMPORT_TYPE_UMP: {
  1177. ump_secure_id id;
  1178. if (get_user(id, (ump_secure_id __user *)phandle))
  1179. reg = NULL;
  1180. else
  1181. reg = kbase_mem_from_ump(kctx, id, va_pages, flags);
  1182. }
  1183. break;
  1184. #endif /* CONFIG_UMP */
  1185. #ifdef CONFIG_DMA_SHARED_BUFFER
  1186. case BASE_MEM_IMPORT_TYPE_UMM: {
  1187. int fd;
  1188. if (get_user(fd, (int __user *)phandle))
  1189. reg = NULL;
  1190. else
  1191. reg = kbase_mem_from_umm(kctx, fd, va_pages, flags);
  1192. }
  1193. break;
  1194. #endif /* CONFIG_DMA_SHARED_BUFFER */
  1195. case BASE_MEM_IMPORT_TYPE_USER_BUFFER: {
  1196. struct base_mem_import_user_buffer user_buffer;
  1197. void __user *uptr;
  1198. if (copy_from_user(&user_buffer, phandle,
  1199. sizeof(user_buffer))) {
  1200. reg = NULL;
  1201. } else {
  1202. #ifdef CONFIG_COMPAT
  1203. if (kctx->is_compat)
  1204. uptr = compat_ptr(user_buffer.ptr.compat_value);
  1205. else
  1206. #endif
  1207. uptr = user_buffer.ptr.value;
  1208. reg = kbase_mem_from_user_buffer(kctx,
  1209. (unsigned long)uptr, user_buffer.length,
  1210. va_pages, flags);
  1211. }
  1212. break;
  1213. }
  1214. default: {
  1215. reg = NULL;
  1216. break;
  1217. }
  1218. }
  1219. if (!reg)
  1220. goto no_reg;
  1221. kbase_gpu_vm_lock(kctx);
  1222. /* mmap needed to setup VA? */
  1223. if (*flags & (BASE_MEM_SAME_VA | BASE_MEM_NEED_MMAP)) {
  1224. /* Bind to a cookie */
  1225. if (!kctx->cookies)
  1226. goto no_cookie;
  1227. /* return a cookie */
  1228. *gpu_va = __ffs(kctx->cookies);
  1229. kctx->cookies &= ~(1UL << *gpu_va);
  1230. BUG_ON(kctx->pending_regions[*gpu_va]);
  1231. kctx->pending_regions[*gpu_va] = reg;
  1232. /* relocate to correct base */
  1233. *gpu_va += PFN_DOWN(BASE_MEM_COOKIE_BASE);
  1234. *gpu_va <<= PAGE_SHIFT;
  1235. } else if (*flags & KBASE_MEM_IMPORT_HAVE_PAGES) {
  1236. /* we control the VA, mmap now to the GPU */
  1237. if (kbase_gpu_mmap(kctx, reg, 0, *va_pages, 1) != 0)
  1238. goto no_gpu_va;
  1239. /* return real GPU VA */
  1240. *gpu_va = reg->start_pfn << PAGE_SHIFT;
  1241. } else {
  1242. /* we control the VA, but nothing to mmap yet */
  1243. if (kbase_add_va_region(kctx, reg, 0, *va_pages, 1) != 0)
  1244. goto no_gpu_va;
  1245. /* return real GPU VA */
  1246. *gpu_va = reg->start_pfn << PAGE_SHIFT;
  1247. }
  1248. /* clear out private flags */
  1249. *flags &= ((1UL << BASE_MEM_FLAGS_NR_BITS) - 1);
  1250. kbase_gpu_vm_unlock(kctx);
  1251. return 0;
  1252. no_gpu_va:
  1253. no_cookie:
  1254. kbase_gpu_vm_unlock(kctx);
  1255. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  1256. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  1257. kfree(reg);
  1258. no_reg:
  1259. bad_flags:
  1260. *gpu_va = 0;
  1261. *va_pages = 0;
  1262. *flags = 0;
  1263. return -ENOMEM;
  1264. }
  1265. static int zap_range_nolock(struct mm_struct *mm,
  1266. const struct vm_operations_struct *vm_ops,
  1267. unsigned long start, unsigned long end)
  1268. {
  1269. struct vm_area_struct *vma;
  1270. int err = -EINVAL; /* in case end < start */
  1271. while (start < end) {
  1272. unsigned long local_start;
  1273. unsigned long local_end;
  1274. vma = find_vma_intersection(mm, start, end);
  1275. if (!vma)
  1276. break;
  1277. /* is it ours? */
  1278. if (vma->vm_ops != vm_ops)
  1279. goto try_next;
  1280. local_start = vma->vm_start;
  1281. if (start > local_start)
  1282. local_start = start;
  1283. local_end = vma->vm_end;
  1284. if (end < local_end)
  1285. local_end = end;
  1286. err = zap_vma_ptes(vma, local_start, local_end - local_start);
  1287. if (unlikely(err))
  1288. break;
  1289. try_next:
  1290. /* go to next vma, if any */
  1291. start = vma->vm_end;
  1292. }
  1293. return err;
  1294. }
  1295. int kbase_mem_grow_gpu_mapping(struct kbase_context *kctx,
  1296. struct kbase_va_region *reg,
  1297. u64 new_pages, u64 old_pages)
  1298. {
  1299. phys_addr_t *phy_pages;
  1300. u64 delta = new_pages - old_pages;
  1301. int ret = 0;
  1302. lockdep_assert_held(&kctx->reg_lock);
  1303. /* Map the new pages into the GPU */
  1304. phy_pages = kbase_get_gpu_phy_pages(reg);
  1305. ret = kbase_mmu_insert_pages(kctx, reg->start_pfn + old_pages,
  1306. phy_pages + old_pages, delta, reg->flags);
  1307. return ret;
  1308. }
  1309. static int kbase_mem_shrink_cpu_mapping(struct kbase_context *kctx,
  1310. struct kbase_va_region *reg,
  1311. u64 new_pages, u64 old_pages)
  1312. {
  1313. struct kbase_mem_phy_alloc *cpu_alloc = reg->cpu_alloc;
  1314. struct kbase_cpu_mapping *mapping;
  1315. int err;
  1316. lockdep_assert_held(&kctx->process_mm->mmap_sem);
  1317. list_for_each_entry(mapping, &cpu_alloc->mappings, mappings_list) {
  1318. unsigned long mapping_size;
  1319. mapping_size = (mapping->vm_end - mapping->vm_start)
  1320. >> PAGE_SHIFT;
  1321. /* is this mapping affected ?*/
  1322. if ((mapping->page_off + mapping_size) > new_pages) {
  1323. unsigned long first_bad = 0;
  1324. if (new_pages > mapping->page_off)
  1325. first_bad = new_pages - mapping->page_off;
  1326. err = zap_range_nolock(current->mm,
  1327. &kbase_vm_ops,
  1328. mapping->vm_start +
  1329. (first_bad << PAGE_SHIFT),
  1330. mapping->vm_end);
  1331. WARN(err,
  1332. "Failed to zap VA range (0x%lx - 0x%lx);\n",
  1333. mapping->vm_start +
  1334. (first_bad << PAGE_SHIFT),
  1335. mapping->vm_end
  1336. );
  1337. /* The zap failed, give up and exit */
  1338. if (err)
  1339. goto failed;
  1340. }
  1341. }
  1342. return 0;
  1343. failed:
  1344. return err;
  1345. }
  1346. static int kbase_mem_shrink_gpu_mapping(struct kbase_context *kctx,
  1347. struct kbase_va_region *reg,
  1348. u64 new_pages, u64 old_pages)
  1349. {
  1350. u64 delta = old_pages - new_pages;
  1351. int ret = 0;
  1352. ret = kbase_mmu_teardown_pages(kctx,
  1353. reg->start_pfn + new_pages, delta);
  1354. if (ret)
  1355. return ret;
  1356. if (kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_6367)) {
  1357. /*
  1358. * Wait for GPU to flush write buffer before freeing
  1359. * physical pages.
  1360. */
  1361. kbase_wait_write_flush(kctx);
  1362. }
  1363. return ret;
  1364. }
  1365. int kbase_mem_commit(struct kbase_context *kctx, u64 gpu_addr, u64 new_pages, enum base_backing_threshold_status *failure_reason)
  1366. {
  1367. u64 old_pages;
  1368. u64 delta;
  1369. int res = -EINVAL;
  1370. struct kbase_va_region *reg;
  1371. bool read_locked = false;
  1372. KBASE_DEBUG_ASSERT(kctx);
  1373. KBASE_DEBUG_ASSERT(failure_reason);
  1374. KBASE_DEBUG_ASSERT(gpu_addr != 0);
  1375. down_write(&current->mm->mmap_sem);
  1376. kbase_gpu_vm_lock(kctx);
  1377. /* Validate the region */
  1378. reg = kbase_region_tracker_find_region_base_address(kctx, gpu_addr);
  1379. if (!reg || (reg->flags & KBASE_REG_FREE)) {
  1380. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_INVALID_ARGUMENTS;
  1381. goto out_unlock;
  1382. }
  1383. KBASE_DEBUG_ASSERT(reg->cpu_alloc);
  1384. KBASE_DEBUG_ASSERT(reg->gpu_alloc);
  1385. if (reg->gpu_alloc->type != KBASE_MEM_TYPE_NATIVE) {
  1386. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_NOT_GROWABLE;
  1387. goto out_unlock;
  1388. }
  1389. if (0 == (reg->flags & KBASE_REG_GROWABLE)) {
  1390. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_NOT_GROWABLE;
  1391. goto out_unlock;
  1392. }
  1393. if (new_pages > reg->nr_pages) {
  1394. /* Would overflow the VA region */
  1395. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_INVALID_ARGUMENTS;
  1396. goto out_unlock;
  1397. }
  1398. /* can't be mapped more than once on the GPU */
  1399. if (atomic_read(&reg->gpu_alloc->gpu_mappings) > 1) {
  1400. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_NOT_GROWABLE;
  1401. goto out_unlock;
  1402. }
  1403. /* can't grow regions which are ephemeral */
  1404. if (reg->flags & KBASE_REG_DONT_NEED) {
  1405. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_NOT_GROWABLE;
  1406. goto out_unlock;
  1407. }
  1408. if (new_pages == reg->gpu_alloc->nents) {
  1409. /* no change */
  1410. res = 0;
  1411. goto out_unlock;
  1412. }
  1413. old_pages = kbase_reg_current_backed_size(reg);
  1414. if (new_pages > old_pages) {
  1415. delta = new_pages - old_pages;
  1416. /*
  1417. * No update to the mm so downgrade the writer lock to a read
  1418. * lock so other readers aren't blocked after this point.
  1419. */
  1420. downgrade_write(&current->mm->mmap_sem);
  1421. read_locked = true;
  1422. /* Allocate some more pages */
  1423. if (kbase_alloc_phy_pages_helper(reg->cpu_alloc, delta) != 0) {
  1424. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_OOM;
  1425. goto out_unlock;
  1426. }
  1427. if (reg->cpu_alloc != reg->gpu_alloc) {
  1428. if (kbase_alloc_phy_pages_helper(
  1429. reg->gpu_alloc, delta) != 0) {
  1430. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_OOM;
  1431. kbase_free_phy_pages_helper(reg->cpu_alloc,
  1432. delta);
  1433. goto out_unlock;
  1434. }
  1435. }
  1436. /* No update required for CPU mappings, that's done on fault. */
  1437. /* Update GPU mapping. */
  1438. res = kbase_mem_grow_gpu_mapping(kctx, reg,
  1439. new_pages, old_pages);
  1440. /* On error free the new pages */
  1441. if (res) {
  1442. kbase_free_phy_pages_helper(reg->cpu_alloc, delta);
  1443. if (reg->cpu_alloc != reg->gpu_alloc)
  1444. kbase_free_phy_pages_helper(reg->gpu_alloc,
  1445. delta);
  1446. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_OOM;
  1447. goto out_unlock;
  1448. }
  1449. } else {
  1450. delta = old_pages - new_pages;
  1451. /* Update all CPU mapping(s) */
  1452. res = kbase_mem_shrink_cpu_mapping(kctx, reg,
  1453. new_pages, old_pages);
  1454. if (res) {
  1455. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_OOM;
  1456. goto out_unlock;
  1457. }
  1458. /* Update the GPU mapping */
  1459. res = kbase_mem_shrink_gpu_mapping(kctx, reg,
  1460. new_pages, old_pages);
  1461. if (res) {
  1462. *failure_reason = BASE_BACKING_THRESHOLD_ERROR_OOM;
  1463. goto out_unlock;
  1464. }
  1465. kbase_free_phy_pages_helper(reg->cpu_alloc, delta);
  1466. if (reg->cpu_alloc != reg->gpu_alloc)
  1467. kbase_free_phy_pages_helper(reg->gpu_alloc, delta);
  1468. }
  1469. out_unlock:
  1470. kbase_gpu_vm_unlock(kctx);
  1471. if (read_locked)
  1472. up_read(&current->mm->mmap_sem);
  1473. else
  1474. up_write(&current->mm->mmap_sem);
  1475. return res;
  1476. }
  1477. static void kbase_cpu_vm_open(struct vm_area_struct *vma)
  1478. {
  1479. struct kbase_cpu_mapping *map = vma->vm_private_data;
  1480. KBASE_DEBUG_ASSERT(map);
  1481. KBASE_DEBUG_ASSERT(map->count > 0);
  1482. /* non-atomic as we're under Linux' mm lock */
  1483. map->count++;
  1484. }
  1485. static void kbase_cpu_vm_close(struct vm_area_struct *vma)
  1486. {
  1487. struct kbase_cpu_mapping *map = vma->vm_private_data;
  1488. KBASE_DEBUG_ASSERT(map);
  1489. KBASE_DEBUG_ASSERT(map->count > 0);
  1490. /* non-atomic as we're under Linux' mm lock */
  1491. if (--map->count)
  1492. return;
  1493. KBASE_DEBUG_ASSERT(map->kctx);
  1494. KBASE_DEBUG_ASSERT(map->alloc);
  1495. kbase_gpu_vm_lock(map->kctx);
  1496. if (map->region) {
  1497. KBASE_DEBUG_ASSERT((map->region->flags & KBASE_REG_ZONE_MASK) ==
  1498. KBASE_REG_ZONE_SAME_VA);
  1499. /* Avoid freeing memory on the process death which results in
  1500. * GPU Page Fault. Memory will be freed in kbase_destroy_context
  1501. */
  1502. if (!(current->flags & PF_EXITING))
  1503. kbase_mem_free_region(map->kctx, map->region);
  1504. }
  1505. list_del(&map->mappings_list);
  1506. kbase_gpu_vm_unlock(map->kctx);
  1507. kbase_mem_phy_alloc_put(map->alloc);
  1508. kfree(map);
  1509. }
  1510. static int kbase_cpu_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1511. {
  1512. struct kbase_cpu_mapping *map = vma->vm_private_data;
  1513. pgoff_t rel_pgoff;
  1514. size_t i;
  1515. KBASE_DEBUG_ASSERT(map);
  1516. KBASE_DEBUG_ASSERT(map->count > 0);
  1517. KBASE_DEBUG_ASSERT(map->kctx);
  1518. KBASE_DEBUG_ASSERT(map->alloc);
  1519. /* we don't use vmf->pgoff as it's affected by our mmap with
  1520. * offset being a GPU VA or a cookie */
  1521. rel_pgoff = ((unsigned long)vmf->address - map->vm_start)
  1522. >> PAGE_SHIFT;
  1523. kbase_gpu_vm_lock(map->kctx);
  1524. if (map->page_off + rel_pgoff >= map->alloc->nents)
  1525. goto locked_bad_fault;
  1526. /* Fault on access to DONT_NEED regions */
  1527. if (map->alloc->reg && (map->alloc->reg->flags & KBASE_REG_DONT_NEED))
  1528. goto locked_bad_fault;
  1529. /* insert all valid pages from the fault location */
  1530. for (i = rel_pgoff;
  1531. i < MIN((vma->vm_end - vma->vm_start) >> PAGE_SHIFT,
  1532. map->alloc->nents - map->page_off); i++) {
  1533. int ret = vm_insert_pfn(vma, map->vm_start + (i << PAGE_SHIFT),
  1534. PFN_DOWN(map->alloc->pages[map->page_off + i]));
  1535. if (ret < 0 && ret != -EBUSY)
  1536. goto locked_bad_fault;
  1537. }
  1538. kbase_gpu_vm_unlock(map->kctx);
  1539. /* we resolved it, nothing for VM to do */
  1540. return VM_FAULT_NOPAGE;
  1541. locked_bad_fault:
  1542. kbase_gpu_vm_unlock(map->kctx);
  1543. return VM_FAULT_SIGBUS;
  1544. }
  1545. static const struct vm_operations_struct kbase_vm_ops = {
  1546. .open = kbase_cpu_vm_open,
  1547. .close = kbase_cpu_vm_close,
  1548. .fault = kbase_cpu_vm_fault
  1549. };
  1550. static int kbase_cpu_mmap(struct kbase_va_region *reg, struct vm_area_struct *vma, void *kaddr, size_t nr_pages, unsigned long aligned_offset, int free_on_close)
  1551. {
  1552. struct kbase_cpu_mapping *map;
  1553. u64 start_off = vma->vm_pgoff - reg->start_pfn;
  1554. phys_addr_t *page_array;
  1555. int err = 0;
  1556. int i;
  1557. map = kzalloc(sizeof(*map), GFP_KERNEL);
  1558. if (!map) {
  1559. WARN_ON(1);
  1560. err = -ENOMEM;
  1561. goto out;
  1562. }
  1563. /*
  1564. * VM_DONTCOPY - don't make this mapping available in fork'ed processes
  1565. * VM_DONTEXPAND - disable mremap on this region
  1566. * VM_IO - disables paging
  1567. * VM_DONTDUMP - Don't include in core dumps (3.7 only)
  1568. * VM_MIXEDMAP - Support mixing struct page*s and raw pfns.
  1569. * This is needed to support using the dedicated and
  1570. * the OS based memory backends together.
  1571. */
  1572. /*
  1573. * This will need updating to propagate coherency flags
  1574. * See MIDBASE-1057
  1575. */
  1576. vma->vm_flags |= VM_DONTCOPY | VM_DONTDUMP | VM_DONTEXPAND | VM_IO;
  1577. vma->vm_ops = &kbase_vm_ops;
  1578. vma->vm_private_data = map;
  1579. page_array = kbase_get_cpu_phy_pages(reg);
  1580. if (!(reg->flags & KBASE_REG_CPU_CACHED) &&
  1581. (reg->flags & (KBASE_REG_CPU_WR|KBASE_REG_CPU_RD))) {
  1582. /* We can't map vmalloc'd memory uncached.
  1583. * Other memory will have been returned from
  1584. * kbase_mem_pool which would be
  1585. * suitable for mapping uncached.
  1586. */
  1587. BUG_ON(kaddr);
  1588. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  1589. }
  1590. if (!kaddr) {
  1591. unsigned long addr = vma->vm_start + aligned_offset;
  1592. vma->vm_flags |= VM_PFNMAP;
  1593. for (i = 0; i < nr_pages; i++) {
  1594. unsigned long pfn = PFN_DOWN(page_array[i + start_off]);
  1595. err = vm_insert_pfn(vma, addr, pfn);
  1596. if (WARN_ON(err))
  1597. break;
  1598. addr += PAGE_SIZE;
  1599. }
  1600. } else {
  1601. WARN_ON(aligned_offset);
  1602. /* MIXEDMAP so we can vfree the kaddr early and not track it after map time */
  1603. vma->vm_flags |= VM_MIXEDMAP;
  1604. /* vmalloc remaping is easy... */
  1605. err = remap_vmalloc_range(vma, kaddr, 0);
  1606. WARN_ON(err);
  1607. }
  1608. if (err) {
  1609. kfree(map);
  1610. goto out;
  1611. }
  1612. map->page_off = start_off;
  1613. map->region = free_on_close ? reg : NULL;
  1614. map->kctx = reg->kctx;
  1615. map->vm_start = vma->vm_start + aligned_offset;
  1616. if (aligned_offset) {
  1617. KBASE_DEBUG_ASSERT(!start_off);
  1618. map->vm_end = map->vm_start + (reg->nr_pages << PAGE_SHIFT);
  1619. } else {
  1620. map->vm_end = vma->vm_end;
  1621. }
  1622. map->alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
  1623. map->count = 1; /* start with one ref */
  1624. if (reg->flags & KBASE_REG_CPU_CACHED)
  1625. map->alloc->properties |= KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED;
  1626. list_add(&map->mappings_list, &map->alloc->mappings);
  1627. out:
  1628. return err;
  1629. }
  1630. static int kbase_trace_buffer_mmap(struct kbase_context *kctx, struct vm_area_struct *vma, struct kbase_va_region **const reg, void **const kaddr)
  1631. {
  1632. struct kbase_va_region *new_reg;
  1633. u32 nr_pages;
  1634. size_t size;
  1635. int err = 0;
  1636. u32 *tb;
  1637. int owns_tb = 1;
  1638. dev_dbg(kctx->kbdev->dev, "in %s\n", __func__);
  1639. size = (vma->vm_end - vma->vm_start);
  1640. nr_pages = size >> PAGE_SHIFT;
  1641. if (!kctx->jctx.tb) {
  1642. KBASE_DEBUG_ASSERT(size != 0);
  1643. tb = vmalloc_user(size);
  1644. if (tb == NULL) {
  1645. err = -ENOMEM;
  1646. goto out;
  1647. }
  1648. err = kbase_device_trace_buffer_install(kctx, tb, size);
  1649. if (err) {
  1650. vfree(tb);
  1651. goto out;
  1652. }
  1653. } else {
  1654. err = -EINVAL;
  1655. goto out;
  1656. }
  1657. *kaddr = kctx->jctx.tb;
  1658. new_reg = kbase_alloc_free_region(kctx, 0, nr_pages, KBASE_REG_ZONE_SAME_VA);
  1659. if (!new_reg) {
  1660. err = -ENOMEM;
  1661. WARN_ON(1);
  1662. goto out_no_region;
  1663. }
  1664. new_reg->cpu_alloc = kbase_alloc_create(0, KBASE_MEM_TYPE_TB);
  1665. if (IS_ERR_OR_NULL(new_reg->cpu_alloc)) {
  1666. err = -ENOMEM;
  1667. new_reg->cpu_alloc = NULL;
  1668. WARN_ON(1);
  1669. goto out_no_alloc;
  1670. }
  1671. new_reg->gpu_alloc = kbase_mem_phy_alloc_get(new_reg->cpu_alloc);
  1672. new_reg->cpu_alloc->imported.kctx = kctx;
  1673. new_reg->flags &= ~KBASE_REG_FREE;
  1674. new_reg->flags |= KBASE_REG_CPU_CACHED;
  1675. /* alloc now owns the tb */
  1676. owns_tb = 0;
  1677. if (kbase_add_va_region(kctx, new_reg, vma->vm_start, nr_pages, 1) != 0) {
  1678. err = -ENOMEM;
  1679. WARN_ON(1);
  1680. goto out_no_va_region;
  1681. }
  1682. *reg = new_reg;
  1683. /* map read only, noexec */
  1684. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE | VM_EXEC | VM_MAYEXEC);
  1685. /* the rest of the flags is added by the cpu_mmap handler */
  1686. dev_dbg(kctx->kbdev->dev, "%s done\n", __func__);
  1687. return 0;
  1688. out_no_va_region:
  1689. out_no_alloc:
  1690. kbase_free_alloced_region(new_reg);
  1691. out_no_region:
  1692. if (owns_tb) {
  1693. kbase_device_trace_buffer_uninstall(kctx);
  1694. vfree(tb);
  1695. }
  1696. out:
  1697. return err;
  1698. }
  1699. static int kbase_mmu_dump_mmap(struct kbase_context *kctx, struct vm_area_struct *vma, struct kbase_va_region **const reg, void **const kmap_addr)
  1700. {
  1701. struct kbase_va_region *new_reg;
  1702. void *kaddr;
  1703. u32 nr_pages;
  1704. size_t size;
  1705. int err = 0;
  1706. dev_dbg(kctx->kbdev->dev, "in kbase_mmu_dump_mmap\n");
  1707. size = (vma->vm_end - vma->vm_start);
  1708. nr_pages = size >> PAGE_SHIFT;
  1709. kaddr = kbase_mmu_dump(kctx, nr_pages);
  1710. if (!kaddr) {
  1711. err = -ENOMEM;
  1712. goto out;
  1713. }
  1714. new_reg = kbase_alloc_free_region(kctx, 0, nr_pages, KBASE_REG_ZONE_SAME_VA);
  1715. if (!new_reg) {
  1716. err = -ENOMEM;
  1717. WARN_ON(1);
  1718. goto out;
  1719. }
  1720. new_reg->cpu_alloc = kbase_alloc_create(0, KBASE_MEM_TYPE_RAW);
  1721. if (IS_ERR_OR_NULL(new_reg->cpu_alloc)) {
  1722. err = -ENOMEM;
  1723. new_reg->cpu_alloc = NULL;
  1724. WARN_ON(1);
  1725. goto out_no_alloc;
  1726. }
  1727. new_reg->gpu_alloc = kbase_mem_phy_alloc_get(new_reg->cpu_alloc);
  1728. new_reg->flags &= ~KBASE_REG_FREE;
  1729. new_reg->flags |= KBASE_REG_CPU_CACHED;
  1730. if (kbase_add_va_region(kctx, new_reg, vma->vm_start, nr_pages, 1) != 0) {
  1731. err = -ENOMEM;
  1732. WARN_ON(1);
  1733. goto out_va_region;
  1734. }
  1735. *kmap_addr = kaddr;
  1736. *reg = new_reg;
  1737. dev_dbg(kctx->kbdev->dev, "kbase_mmu_dump_mmap done\n");
  1738. return 0;
  1739. out_no_alloc:
  1740. out_va_region:
  1741. kbase_free_alloced_region(new_reg);
  1742. out:
  1743. return err;
  1744. }
  1745. void kbase_os_mem_map_lock(struct kbase_context *kctx)
  1746. {
  1747. struct mm_struct *mm = current->mm;
  1748. (void)kctx;
  1749. down_read(&mm->mmap_sem);
  1750. }
  1751. void kbase_os_mem_map_unlock(struct kbase_context *kctx)
  1752. {
  1753. struct mm_struct *mm = current->mm;
  1754. (void)kctx;
  1755. up_read(&mm->mmap_sem);
  1756. }
  1757. #if defined(CONFIG_DMA_SHARED_BUFFER) && defined(CONFIG_MALI_TRACE_TIMELINE)
  1758. /* This section is required only for instrumentation. */
  1759. static void kbase_dma_buf_vm_open(struct vm_area_struct *vma)
  1760. {
  1761. struct kbase_cpu_mapping *map = vma->vm_private_data;
  1762. KBASE_DEBUG_ASSERT(map);
  1763. KBASE_DEBUG_ASSERT(map->count > 0);
  1764. /* Non-atomic as we're under Linux's mm lock. */
  1765. map->count++;
  1766. }
  1767. static void kbase_dma_buf_vm_close(struct vm_area_struct *vma)
  1768. {
  1769. struct kbase_cpu_mapping *map = vma->vm_private_data;
  1770. KBASE_DEBUG_ASSERT(map);
  1771. KBASE_DEBUG_ASSERT(map->count > 0);
  1772. /* Non-atomic as we're under Linux's mm lock. */
  1773. if (--map->count)
  1774. return;
  1775. KBASE_DEBUG_ASSERT(map->kctx);
  1776. kbase_gpu_vm_lock(map->kctx);
  1777. list_del(&map->mappings_list);
  1778. kbase_gpu_vm_unlock(map->kctx);
  1779. kfree(map);
  1780. }
  1781. static const struct vm_operations_struct kbase_dma_mmap_ops = {
  1782. .open = kbase_dma_buf_vm_open,
  1783. .close = kbase_dma_buf_vm_close,
  1784. };
  1785. #endif /* CONFIG_DMA_SHARED_BUFFER && CONFIG_MALI_TRACE_TIMELINE */
  1786. int kbase_mmap(struct file *file, struct vm_area_struct *vma)
  1787. {
  1788. struct kbase_context *kctx = file->private_data;
  1789. struct kbase_va_region *reg;
  1790. void *kaddr = NULL;
  1791. size_t nr_pages;
  1792. int err = 0;
  1793. int free_on_close = 0;
  1794. struct device *dev = kctx->kbdev->dev;
  1795. size_t aligned_offset = 0;
  1796. dev_dbg(dev, "kbase_mmap\n");
  1797. nr_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  1798. /* strip away corresponding VM_MAY% flags to the VM_% flags requested */
  1799. vma->vm_flags &= ~((vma->vm_flags & (VM_READ | VM_WRITE)) << 4);
  1800. if (nr_pages == 0) {
  1801. err = -EINVAL;
  1802. goto out;
  1803. }
  1804. if (!(vma->vm_flags & VM_SHARED)) {
  1805. err = -EINVAL;
  1806. goto out;
  1807. }
  1808. kbase_gpu_vm_lock(kctx);
  1809. if (vma->vm_pgoff == PFN_DOWN(BASE_MEM_MAP_TRACKING_HANDLE)) {
  1810. /* The non-mapped tracking helper page */
  1811. err = kbase_tracking_page_setup(kctx, vma);
  1812. goto out_unlock;
  1813. }
  1814. /* if not the MTP, verify that the MTP has been mapped */
  1815. rcu_read_lock();
  1816. /* catches both when the special page isn't present or
  1817. * when we've forked */
  1818. if (rcu_dereference(kctx->process_mm) != current->mm) {
  1819. err = -EINVAL;
  1820. rcu_read_unlock();
  1821. goto out_unlock;
  1822. }
  1823. rcu_read_unlock();
  1824. switch (vma->vm_pgoff) {
  1825. case PFN_DOWN(BASEP_MEM_INVALID_HANDLE):
  1826. case PFN_DOWN(BASEP_MEM_WRITE_ALLOC_PAGES_HANDLE):
  1827. /* Illegal handle for direct map */
  1828. err = -EINVAL;
  1829. goto out_unlock;
  1830. case PFN_DOWN(BASE_MEM_TRACE_BUFFER_HANDLE):
  1831. err = kbase_trace_buffer_mmap(kctx, vma, &reg, &kaddr);
  1832. if (err != 0)
  1833. goto out_unlock;
  1834. dev_dbg(dev, "kbase_trace_buffer_mmap ok\n");
  1835. /* free the region on munmap */
  1836. free_on_close = 1;
  1837. goto map;
  1838. case PFN_DOWN(BASE_MEM_MMU_DUMP_HANDLE):
  1839. /* MMU dump */
  1840. err = kbase_mmu_dump_mmap(kctx, vma, &reg, &kaddr);
  1841. if (err != 0)
  1842. goto out_unlock;
  1843. /* free the region on munmap */
  1844. free_on_close = 1;
  1845. goto map;
  1846. case PFN_DOWN(BASE_MEM_COOKIE_BASE) ...
  1847. PFN_DOWN(BASE_MEM_FIRST_FREE_ADDRESS) - 1: {
  1848. /* SAME_VA stuff, fetch the right region */
  1849. int gpu_pc_bits;
  1850. int cookie = vma->vm_pgoff - PFN_DOWN(BASE_MEM_COOKIE_BASE);
  1851. gpu_pc_bits = kctx->kbdev->gpu_props.props.core_props.log2_program_counter_size;
  1852. reg = kctx->pending_regions[cookie];
  1853. if (!reg) {
  1854. err = -ENOMEM;
  1855. goto out_unlock;
  1856. }
  1857. if (reg->flags & KBASE_REG_ALIGNED) {
  1858. /* nr_pages must be able to hold alignment pages
  1859. * plus actual pages */
  1860. unsigned long align = 1ULL << gpu_pc_bits;
  1861. unsigned long extra_pages = 3 * PFN_DOWN(align);
  1862. unsigned long aligned_addr;
  1863. unsigned long aligned_addr_end;
  1864. unsigned long nr_bytes = reg->nr_pages << PAGE_SHIFT;
  1865. if (kctx->api_version < KBASE_API_VERSION(8, 5))
  1866. /* Maintain compatibility with old userspace */
  1867. extra_pages = PFN_DOWN(align);
  1868. if (nr_pages != reg->nr_pages + extra_pages) {
  1869. /* incorrect mmap size */
  1870. /* leave the cookie for a potential
  1871. * later mapping, or to be reclaimed
  1872. * later when the context is freed */
  1873. err = -ENOMEM;
  1874. goto out_unlock;
  1875. }
  1876. aligned_addr = ALIGN(vma->vm_start, align);
  1877. aligned_addr_end = aligned_addr + nr_bytes;
  1878. if (kctx->api_version >= KBASE_API_VERSION(8, 5)) {
  1879. if ((aligned_addr_end & BASE_MEM_MASK_4GB) == 0) {
  1880. /* Can't end at 4GB boundary */
  1881. aligned_addr += 2 * align;
  1882. } else if ((aligned_addr & BASE_MEM_MASK_4GB) == 0) {
  1883. /* Can't start at 4GB boundary */
  1884. aligned_addr += align;
  1885. }
  1886. }
  1887. aligned_offset = aligned_addr - vma->vm_start;
  1888. } else if (reg->nr_pages != nr_pages) {
  1889. /* incorrect mmap size */
  1890. /* leave the cookie for a potential later
  1891. * mapping, or to be reclaimed later when the
  1892. * context is freed */
  1893. err = -ENOMEM;
  1894. goto out_unlock;
  1895. }
  1896. if ((vma->vm_flags & VM_READ &&
  1897. !(reg->flags & KBASE_REG_CPU_RD)) ||
  1898. (vma->vm_flags & VM_WRITE &&
  1899. !(reg->flags & KBASE_REG_CPU_WR))) {
  1900. /* VM flags inconsistent with region flags */
  1901. err = -EPERM;
  1902. dev_err(dev, "%s:%d inconsistent VM flags\n",
  1903. __FILE__, __LINE__);
  1904. goto out_unlock;
  1905. }
  1906. /* adjust down nr_pages to what we have physically */
  1907. nr_pages = kbase_reg_current_backed_size(reg);
  1908. if (kbase_gpu_mmap(kctx, reg,
  1909. vma->vm_start + aligned_offset,
  1910. reg->nr_pages, 1) != 0) {
  1911. dev_err(dev, "%s:%d\n", __FILE__, __LINE__);
  1912. /* Unable to map in GPU space. */
  1913. WARN_ON(1);
  1914. err = -ENOMEM;
  1915. goto out_unlock;
  1916. }
  1917. /* no need for the cookie anymore */
  1918. kctx->pending_regions[cookie] = NULL;
  1919. kctx->cookies |= (1UL << cookie);
  1920. /*
  1921. * Overwrite the offset with the
  1922. * region start_pfn, so we effectively
  1923. * map from offset 0 in the region.
  1924. */
  1925. vma->vm_pgoff = reg->start_pfn;
  1926. /* free the region on munmap */
  1927. free_on_close = 1;
  1928. goto map;
  1929. }
  1930. default: {
  1931. reg = kbase_region_tracker_find_region_enclosing_address(kctx, (u64)vma->vm_pgoff << PAGE_SHIFT);
  1932. if (reg && !(reg->flags & KBASE_REG_FREE)) {
  1933. /* will this mapping overflow the size of the region? */
  1934. if (nr_pages > (reg->nr_pages - (vma->vm_pgoff - reg->start_pfn)))
  1935. goto overflow;
  1936. if ((vma->vm_flags & VM_READ &&
  1937. !(reg->flags & KBASE_REG_CPU_RD)) ||
  1938. (vma->vm_flags & VM_WRITE &&
  1939. !(reg->flags & KBASE_REG_CPU_WR))) {
  1940. /* VM flags inconsistent with region flags */
  1941. err = -EPERM;
  1942. dev_err(dev, "%s:%d inconsistent VM flags\n",
  1943. __FILE__, __LINE__);
  1944. goto out_unlock;
  1945. }
  1946. #ifdef CONFIG_DMA_SHARED_BUFFER
  1947. if (reg->cpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM)
  1948. goto dma_map;
  1949. #endif /* CONFIG_DMA_SHARED_BUFFER */
  1950. /* limit what we map to the amount currently backed */
  1951. if (reg->cpu_alloc->nents < (vma->vm_pgoff - reg->start_pfn + nr_pages)) {
  1952. if ((vma->vm_pgoff - reg->start_pfn) >= reg->cpu_alloc->nents)
  1953. nr_pages = 0;
  1954. else
  1955. nr_pages = reg->cpu_alloc->nents - (vma->vm_pgoff - reg->start_pfn);
  1956. }
  1957. goto map;
  1958. }
  1959. overflow:
  1960. err = -ENOMEM;
  1961. goto out_unlock;
  1962. } /* default */
  1963. } /* switch */
  1964. map:
  1965. printk("mmap %LX -> %lX", (uint64_t) vma->vm_pgoff << PAGE_SHIFT, vma->vm_start);
  1966. err = kbase_cpu_mmap(reg, vma, kaddr, nr_pages, aligned_offset, free_on_close);
  1967. if (vma->vm_pgoff == PFN_DOWN(BASE_MEM_MMU_DUMP_HANDLE)) {
  1968. /* MMU dump - userspace should now have a reference on
  1969. * the pages, so we can now free the kernel mapping */
  1970. vfree(kaddr);
  1971. }
  1972. goto out_unlock;
  1973. #ifdef CONFIG_DMA_SHARED_BUFFER
  1974. dma_map:
  1975. err = dma_buf_mmap(reg->cpu_alloc->imported.umm.dma_buf, vma, vma->vm_pgoff - reg->start_pfn);
  1976. #if defined(CONFIG_MALI_TRACE_TIMELINE)
  1977. /* This section is required only for instrumentation. */
  1978. /* Add created mapping to imported region mapping list.
  1979. * It is important to make it visible to dumping infrastructure.
  1980. * Add mapping only if vm_ops structure is not used by memory owner. */
  1981. WARN_ON(vma->vm_ops);
  1982. WARN_ON(vma->vm_private_data);
  1983. if (!err && !vma->vm_ops && !vma->vm_private_data) {
  1984. struct kbase_cpu_mapping *map = kzalloc(
  1985. sizeof(*map),
  1986. GFP_KERNEL);
  1987. if (map) {
  1988. map->kctx = reg->kctx;
  1989. map->region = NULL;
  1990. map->page_off = vma->vm_pgoff;
  1991. map->vm_start = vma->vm_start;
  1992. map->vm_end = vma->vm_end;
  1993. map->count = 1; /* start with one ref */
  1994. vma->vm_ops = &kbase_dma_mmap_ops;
  1995. vma->vm_private_data = map;
  1996. list_add(
  1997. &map->mappings_list,
  1998. &reg->cpu_alloc->mappings);
  1999. }
  2000. }
  2001. #endif /* CONFIG_MALI_TRACE_TIMELINE */
  2002. #endif /* CONFIG_DMA_SHARED_BUFFER */
  2003. out_unlock:
  2004. kbase_gpu_vm_unlock(kctx);
  2005. out:
  2006. if (err)
  2007. dev_err(dev, "mmap failed %d\n", err);
  2008. return err;
  2009. }
  2010. void *kbase_vmap(struct kbase_context *kctx, u64 gpu_addr, size_t size,
  2011. struct kbase_vmap_struct *map)
  2012. {
  2013. struct kbase_va_region *reg;
  2014. unsigned long page_index;
  2015. unsigned int offset = gpu_addr & ~PAGE_MASK;
  2016. size_t page_count = PFN_UP(offset + size);
  2017. phys_addr_t *page_array;
  2018. struct page **pages;
  2019. void *cpu_addr = NULL;
  2020. pgprot_t prot;
  2021. size_t i;
  2022. bool sync_needed;
  2023. if (!size || !map)
  2024. return NULL;
  2025. /* check if page_count calculation will wrap */
  2026. if (size > ((size_t)-1 / PAGE_SIZE))
  2027. return NULL;
  2028. kbase_gpu_vm_lock(kctx);
  2029. reg = kbase_region_tracker_find_region_enclosing_address(kctx, gpu_addr);
  2030. if (!reg || (reg->flags & KBASE_REG_FREE))
  2031. goto out_unlock;
  2032. page_index = (gpu_addr >> PAGE_SHIFT) - reg->start_pfn;
  2033. /* check if page_index + page_count will wrap */
  2034. if (-1UL - page_count < page_index)
  2035. goto out_unlock;
  2036. if (page_index + page_count > kbase_reg_current_backed_size(reg))
  2037. goto out_unlock;
  2038. if (reg->flags & KBASE_REG_DONT_NEED)
  2039. goto out_unlock;
  2040. page_array = kbase_get_cpu_phy_pages(reg);
  2041. if (!page_array)
  2042. goto out_unlock;
  2043. pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
  2044. if (!pages)
  2045. goto out_unlock;
  2046. for (i = 0; i < page_count; i++)
  2047. pages[i] = pfn_to_page(PFN_DOWN(page_array[page_index + i]));
  2048. prot = PAGE_KERNEL;
  2049. if (!(reg->flags & KBASE_REG_CPU_CACHED)) {
  2050. /* Map uncached */
  2051. prot = pgprot_writecombine(prot);
  2052. }
  2053. cpu_addr = vmap(pages, page_count, VM_MAP, prot);
  2054. kfree(pages);
  2055. if (!cpu_addr)
  2056. goto out_unlock;
  2057. map->gpu_addr = gpu_addr;
  2058. map->cpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
  2059. map->cpu_pages = &kbase_get_cpu_phy_pages(reg)[page_index];
  2060. map->gpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
  2061. map->gpu_pages = &kbase_get_gpu_phy_pages(reg)[page_index];
  2062. map->addr = (void *)((uintptr_t)cpu_addr + offset);
  2063. map->size = size;
  2064. map->is_cached = (reg->flags & KBASE_REG_CPU_CACHED) != 0;
  2065. sync_needed = map->is_cached;
  2066. #ifdef CONFIG_MALI_COH_KERN
  2067. /* kernel can use coherent memory if supported */
  2068. if (kctx->kbdev->system_coherency == COHERENCY_ACE)
  2069. sync_needed = false;
  2070. #endif
  2071. if (sync_needed) {
  2072. /* Sync first page */
  2073. size_t sz = MIN(((size_t) PAGE_SIZE - offset), size);
  2074. phys_addr_t cpu_pa = map->cpu_pages[0];
  2075. phys_addr_t gpu_pa = map->gpu_pages[0];
  2076. kbase_sync_single(kctx, cpu_pa, gpu_pa, offset, sz,
  2077. KBASE_SYNC_TO_CPU);
  2078. /* Sync middle pages (if any) */
  2079. for (i = 1; page_count > 2 && i < page_count - 1; i++) {
  2080. cpu_pa = map->cpu_pages[i];
  2081. gpu_pa = map->gpu_pages[i];
  2082. kbase_sync_single(kctx, cpu_pa, gpu_pa, 0, PAGE_SIZE,
  2083. KBASE_SYNC_TO_CPU);
  2084. }
  2085. /* Sync last page (if any) */
  2086. if (page_count > 1) {
  2087. cpu_pa = map->cpu_pages[page_count - 1];
  2088. gpu_pa = map->gpu_pages[page_count - 1];
  2089. sz = ((offset + size - 1) & ~PAGE_MASK) + 1;
  2090. kbase_sync_single(kctx, cpu_pa, gpu_pa, 0, sz,
  2091. KBASE_SYNC_TO_CPU);
  2092. }
  2093. }
  2094. kbase_gpu_vm_unlock(kctx);
  2095. return map->addr;
  2096. out_unlock:
  2097. kbase_gpu_vm_unlock(kctx);
  2098. return NULL;
  2099. }
  2100. void kbase_vunmap(struct kbase_context *kctx, struct kbase_vmap_struct *map)
  2101. {
  2102. void *addr = (void *)((uintptr_t)map->addr & PAGE_MASK);
  2103. bool sync_needed = map->is_cached;
  2104. vunmap(addr);
  2105. #ifdef CONFIG_MALI_COH_KERN
  2106. /* kernel can use coherent memory if supported */
  2107. if (kctx->kbdev->system_coherency == COHERENCY_ACE)
  2108. sync_needed = false;
  2109. #endif
  2110. if (sync_needed) {
  2111. off_t offset = (uintptr_t)map->addr & ~PAGE_MASK;
  2112. size_t size = map->size;
  2113. size_t page_count = PFN_UP(offset + size);
  2114. size_t i;
  2115. /* Sync first page */
  2116. size_t sz = MIN(((size_t) PAGE_SIZE - offset), size);
  2117. phys_addr_t cpu_pa = map->cpu_pages[0];
  2118. phys_addr_t gpu_pa = map->gpu_pages[0];
  2119. kbase_sync_single(kctx, cpu_pa, gpu_pa, offset, sz,
  2120. KBASE_SYNC_TO_DEVICE);
  2121. /* Sync middle pages (if any) */
  2122. for (i = 1; page_count > 2 && i < page_count - 1; i++) {
  2123. cpu_pa = map->cpu_pages[i];
  2124. gpu_pa = map->gpu_pages[i];
  2125. kbase_sync_single(kctx, cpu_pa, gpu_pa, 0, PAGE_SIZE,
  2126. KBASE_SYNC_TO_DEVICE);
  2127. }
  2128. /* Sync last page (if any) */
  2129. if (page_count > 1) {
  2130. cpu_pa = map->cpu_pages[page_count - 1];
  2131. gpu_pa = map->gpu_pages[page_count - 1];
  2132. sz = ((offset + size - 1) & ~PAGE_MASK) + 1;
  2133. kbase_sync_single(kctx, cpu_pa, gpu_pa, 0, sz,
  2134. KBASE_SYNC_TO_DEVICE);
  2135. }
  2136. }
  2137. map->gpu_addr = 0;
  2138. map->cpu_alloc = kbase_mem_phy_alloc_put(map->cpu_alloc);
  2139. map->gpu_alloc = kbase_mem_phy_alloc_put(map->gpu_alloc);
  2140. map->cpu_pages = NULL;
  2141. map->gpu_pages = NULL;
  2142. map->addr = NULL;
  2143. map->size = 0;
  2144. map->is_cached = false;
  2145. }
  2146. void kbasep_os_process_page_usage_update(struct kbase_context *kctx, int pages)
  2147. {
  2148. struct mm_struct *mm;
  2149. rcu_read_lock();
  2150. mm = rcu_dereference(kctx->process_mm);
  2151. if (mm) {
  2152. atomic_add(pages, &kctx->nonmapped_pages);
  2153. #ifdef SPLIT_RSS_COUNTING
  2154. add_mm_counter(mm, MM_FILEPAGES, pages);
  2155. #else
  2156. spin_lock(&mm->page_table_lock);
  2157. add_mm_counter(mm, MM_FILEPAGES, pages);
  2158. spin_unlock(&mm->page_table_lock);
  2159. #endif
  2160. }
  2161. rcu_read_unlock();
  2162. }
  2163. static void kbasep_os_process_page_usage_drain(struct kbase_context *kctx)
  2164. {
  2165. int pages;
  2166. struct mm_struct *mm;
  2167. spin_lock(&kctx->mm_update_lock);
  2168. mm = rcu_dereference_protected(kctx->process_mm, lockdep_is_held(&kctx->mm_update_lock));
  2169. if (!mm) {
  2170. spin_unlock(&kctx->mm_update_lock);
  2171. return;
  2172. }
  2173. rcu_assign_pointer(kctx->process_mm, NULL);
  2174. spin_unlock(&kctx->mm_update_lock);
  2175. synchronize_rcu();
  2176. pages = atomic_xchg(&kctx->nonmapped_pages, 0);
  2177. #ifdef SPLIT_RSS_COUNTING
  2178. add_mm_counter(mm, MM_FILEPAGES, -pages);
  2179. #else
  2180. spin_lock(&mm->page_table_lock);
  2181. add_mm_counter(mm, MM_FILEPAGES, -pages);
  2182. spin_unlock(&mm->page_table_lock);
  2183. #endif
  2184. }
  2185. static void kbase_special_vm_close(struct vm_area_struct *vma)
  2186. {
  2187. struct kbase_context *kctx;
  2188. kctx = vma->vm_private_data;
  2189. kbasep_os_process_page_usage_drain(kctx);
  2190. }
  2191. static const struct vm_operations_struct kbase_vm_special_ops = {
  2192. .close = kbase_special_vm_close,
  2193. };
  2194. static int kbase_tracking_page_setup(struct kbase_context *kctx, struct vm_area_struct *vma)
  2195. {
  2196. /* check that this is the only tracking page */
  2197. spin_lock(&kctx->mm_update_lock);
  2198. if (rcu_dereference_protected(kctx->process_mm, lockdep_is_held(&kctx->mm_update_lock))) {
  2199. spin_unlock(&kctx->mm_update_lock);
  2200. return -EFAULT;
  2201. }
  2202. rcu_assign_pointer(kctx->process_mm, current->mm);
  2203. spin_unlock(&kctx->mm_update_lock);
  2204. /* no real access */
  2205. vma->vm_flags &= ~(VM_READ | VM_MAYREAD | VM_WRITE | VM_MAYWRITE | VM_EXEC | VM_MAYEXEC);
  2206. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP | VM_IO;
  2207. vma->vm_ops = &kbase_vm_special_ops;
  2208. vma->vm_private_data = kctx;
  2209. return 0;
  2210. }
  2211. void *kbase_va_alloc(struct kbase_context *kctx, u32 size, struct kbase_hwc_dma_mapping *handle)
  2212. {
  2213. int i;
  2214. int res;
  2215. void *va;
  2216. dma_addr_t dma_pa;
  2217. struct kbase_va_region *reg;
  2218. phys_addr_t *page_array;
  2219. unsigned long attrs;
  2220. u32 pages = ((size - 1) >> PAGE_SHIFT) + 1;
  2221. u32 flags = BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_CPU_WR |
  2222. BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR;
  2223. KBASE_DEBUG_ASSERT(kctx != NULL);
  2224. KBASE_DEBUG_ASSERT(size != 0);
  2225. KBASE_DEBUG_ASSERT(pages != 0);
  2226. if (size == 0)
  2227. goto err;
  2228. /* All the alloc calls return zeroed memory */
  2229. attrs = DMA_ATTR_WRITE_COMBINE;
  2230. va = dma_alloc_attrs(kctx->kbdev->dev, size, &dma_pa, GFP_KERNEL, attrs);
  2231. if (!va)
  2232. goto err;
  2233. /* Store the state so we can free it later. */
  2234. handle->cpu_va = va;
  2235. handle->dma_pa = dma_pa;
  2236. handle->size = size;
  2237. reg = kbase_alloc_free_region(kctx, 0, pages, KBASE_REG_ZONE_SAME_VA);
  2238. if (!reg)
  2239. goto no_reg;
  2240. reg->flags &= ~KBASE_REG_FREE;
  2241. kbase_update_region_flags(kctx, reg, flags);
  2242. reg->cpu_alloc = kbase_alloc_create(pages, KBASE_MEM_TYPE_RAW);
  2243. if (IS_ERR_OR_NULL(reg->cpu_alloc))
  2244. goto no_alloc;
  2245. reg->gpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
  2246. page_array = kbase_get_cpu_phy_pages(reg);
  2247. for (i = 0; i < pages; i++)
  2248. page_array[i] = dma_pa + (i << PAGE_SHIFT);
  2249. reg->cpu_alloc->nents = pages;
  2250. kbase_gpu_vm_lock(kctx);
  2251. res = kbase_gpu_mmap(kctx, reg, (uintptr_t) va, pages, 1);
  2252. kbase_gpu_vm_unlock(kctx);
  2253. if (res)
  2254. goto no_mmap;
  2255. return va;
  2256. no_mmap:
  2257. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  2258. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  2259. no_alloc:
  2260. kfree(reg);
  2261. no_reg:
  2262. dma_free_attrs(kctx->kbdev->dev, size, va, dma_pa, attrs);
  2263. err:
  2264. return NULL;
  2265. }
  2266. KBASE_EXPORT_SYMBOL(kbase_va_alloc);
  2267. void kbase_va_free(struct kbase_context *kctx, struct kbase_hwc_dma_mapping *handle)
  2268. {
  2269. struct kbase_va_region *reg;
  2270. int err;
  2271. unsigned long attrs;
  2272. KBASE_DEBUG_ASSERT(kctx != NULL);
  2273. KBASE_DEBUG_ASSERT(handle->cpu_va != NULL);
  2274. kbase_gpu_vm_lock(kctx);
  2275. reg = kbase_region_tracker_find_region_base_address(kctx, (uintptr_t)handle->cpu_va);
  2276. KBASE_DEBUG_ASSERT(reg);
  2277. err = kbase_gpu_munmap(kctx, reg);
  2278. kbase_gpu_vm_unlock(kctx);
  2279. KBASE_DEBUG_ASSERT(!err);
  2280. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  2281. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  2282. kfree(reg);
  2283. attrs = DMA_ATTR_WRITE_COMBINE;
  2284. dma_free_attrs(kctx->kbdev->dev, handle->size,
  2285. handle->cpu_va, handle->dma_pa, attrs);
  2286. }
  2287. KBASE_EXPORT_SYMBOL(kbase_va_free);